old openai.ts

This commit is contained in:
akdeb 2026-04-17 14:29:46 +05:30
parent 848275fb86
commit 141addd3c3

View file

@ -12,6 +12,10 @@ interface OpenAIChatMessage {
content: string; content: string;
} }
interface SessionState {
history: OpenAIChatMessage[];
}
function createAuthMessage() { function createAuthMessage() {
return { return {
type: "auth", type: "auth",
@ -110,23 +114,18 @@ export class ElatoOpenAiVoiceAgent extends DurableObject<Env> {
private hasStartedConversation = false; private hasStartedConversation = false;
private transcriberSession: TranscriberSession | null = null; private transcriberSession: TranscriberSession | null = null;
private currentWebSocket: WebSocket | null = null; private currentWebSocket: WebSocket | null = null;
private history: OpenAIChatMessage[] = [];
constructor(ctx: DurableObjectState, env: Env) { constructor(ctx: DurableObjectState, env: Env) {
super(ctx, env); super(ctx, env);
} }
private resetSession() { private async loadSessionState(): Promise<SessionState> {
this.isGenerating = false; const stored = await this.ctx.storage.get<SessionState>("session_state");
this.hasStartedConversation = false; return stored || { history: [] };
this.currentWebSocket = null; }
this.history = [];
this.transcriberSession?.close(); private async saveSessionState(state: SessionState) {
this.transcriberSession = null; await this.ctx.storage.put("session_state", state);
if (this.opusPromise) {
void this.opusPromise.then((opus) => opus.close()).catch(() => {});
this.opusPromise = null;
}
} }
private getOpusPacketizer(websocket: WebSocket) { private getOpusPacketizer(websocket: WebSocket) {
@ -221,12 +220,14 @@ export class ElatoOpenAiVoiceAgent extends DurableObject<Env> {
console.log(`[cloudflare][stt] transcript: ${transcript}`); console.log(`[cloudflare][stt] transcript: ${transcript}`);
/* Add user transcript DB call here */ /* Add user transcript DB call here */
const reply = await generateOpenAIReply(this.env, transcript, this.history); const session = await this.loadSessionState();
const reply = await generateOpenAIReply(this.env, transcript, session.history);
console.log(`[cloudflare][llm] generated reply (${reply.length} chars)`); console.log(`[cloudflare][llm] generated reply (${reply.length} chars)`);
this.history.push( session.history.push(
{ role: "user", content: transcript }, { role: "user", content: transcript },
{ role: "assistant", content: reply }, { role: "assistant", content: reply },
); );
await this.saveSessionState(session);
/* Add AI transcript DB call here */ /* Add AI transcript DB call here */
await this.streamAssistantReply(websocket, reply); await this.streamAssistantReply(websocket, reply);
} }
@ -240,9 +241,11 @@ export class ElatoOpenAiVoiceAgent extends DurableObject<Env> {
this.isGenerating = true; this.isGenerating = true;
try { try {
const reply = await generateOpenAIReply(this.env, null, this.history); const session = await this.loadSessionState();
const reply = await generateOpenAIReply(this.env, null, session.history);
console.log(`[cloudflare][llm] initial reply (${reply.length} chars)`); console.log(`[cloudflare][llm] initial reply (${reply.length} chars)`);
this.history.push({ role: "assistant", content: reply }); session.history.push({ role: "assistant", content: reply });
await this.saveSessionState(session);
/* Add AI transcript DB call here */ /* Add AI transcript DB call here */
await this.streamAssistantReply(websocket, reply); await this.streamAssistantReply(websocket, reply);
} catch (error) { } catch (error) {
@ -257,8 +260,6 @@ export class ElatoOpenAiVoiceAgent extends DurableObject<Env> {
return new Response("Expected websocket", { status: 426 }); return new Response("Expected websocket", { status: 426 });
} }
this.resetSession();
const pair = new WebSocketPair(); const pair = new WebSocketPair();
const [client, server] = Object.values(pair); const [client, server] = Object.values(pair);
server.accept(); server.accept();
@ -319,7 +320,14 @@ export class ElatoOpenAiVoiceAgent extends DurableObject<Env> {
}); });
server.addEventListener("close", () => { server.addEventListener("close", () => {
this.resetSession(); this.isGenerating = false;
this.currentWebSocket = null;
this.transcriberSession?.close();
this.transcriberSession = null;
if (this.opusPromise) {
void this.opusPromise.then((opus) => opus.close()).catch(() => {});
this.opusPromise = null;
}
}); });
return new Response(null, { status: 101, webSocket: client }); return new Response(null, { status: 101, webSocket: client });