diff --git a/examples/06_transcript_replay.ts b/examples/06_transcript_replay.ts index 8ef1c04..f847dbe 100644 --- a/examples/06_transcript_replay.ts +++ b/examples/06_transcript_replay.ts @@ -7,29 +7,6 @@ type TranscriptMessage = { content: unknown; }; -function applyTranscriptOnCurrentEngine( - engine: ReturnType, - messages: TranscriptMessage[] -): TranscriptResult { - for (const message of messages) { - if (message.role !== 'user' || typeof message.content !== 'string') { - continue; - } - const decision = engine.step(message.content); - if (decision.kind === 'clarify') { - return { - kind: 'confirm', - prompt_to_user: decision.prompt_to_user as string - }; - } - } - - return { - kind: 'state', - state: engine.state - }; -} - export function runExample06(): { freshReplayKind: string; currentReplayKind: string; @@ -48,7 +25,7 @@ export function runExample06(): { const engine = createEngine(); engine.step('prohibit shellfish'); - const currentReplay = applyTranscriptOnCurrentEngine(engine, transcript); + const currentReplay: TranscriptResult = engine.apply_transcript(transcript); const freshPolicies = freshReplay.kind === 'state' ? getPolicyItems(freshReplay.state) : []; diff --git a/examples/nextjs-basic/app/api/chat/route.ts b/examples/nextjs-basic/app/api/chat/route.ts index 614ef25..498768d 100644 --- a/examples/nextjs-basic/app/api/chat/route.ts +++ b/examples/nextjs-basic/app/api/chat/route.ts @@ -57,20 +57,17 @@ export async function POST(req: Request): Promise { if (saved) { engine.importJson(saved); } else if (history?.length) { - for (const m of history) { - if (m.role !== 'user' || typeof m.content !== 'string') { - continue; - } - - const d = engine.step(m.content); - if (d.kind === 'clarify') { - saveSessionState(sessionId, engine.exportJson()); - const payload: ChatResponse = { - kind: 'clarify', - prompt_to_user: d.prompt_to_user - }; - return Response.json(payload); - } + const replayMessages = history.filter( + (m): m is { role: 'user'; content: string } => m.role === 'user' && typeof m.content === 'string' + ); + const replay = engine.apply_transcript(replayMessages); + if (replay.kind === 'confirm') { + saveSessionState(sessionId, engine.exportJson()); + const payload: ChatResponse = { + kind: 'clarify', + prompt_to_user: replay.prompt_to_user + }; + return Response.json(payload); } saveSessionState(sessionId, engine.exportJson()); diff --git a/examples/node-basic/server.ts b/examples/node-basic/server.ts index 412fe8b..71f3515 100644 --- a/examples/node-basic/server.ts +++ b/examples/node-basic/server.ts @@ -82,15 +82,15 @@ const server = http.createServer(async (req, res) => { if (saved) { engine.importJson(saved); } else if (history?.length) { - for (const m of history) { - if (m.role !== 'user' || typeof m.content !== 'string') continue; - const d = engine.step(m.content); - if (d.kind === 'clarify') { - saveState(sessionId, engine.exportJson()); - const payload: ChatResponse = { kind: 'clarify', prompt_to_user: d.prompt_to_user }; - sendJson(res, 200, payload); - return; - } + const replayMessages = history.filter( + (m): m is { role: 'user'; content: string } => m.role === 'user' && typeof m.content === 'string' + ); + const replay = engine.apply_transcript(replayMessages); + if (replay.kind === 'confirm') { + saveState(sessionId, engine.exportJson()); + const payload: ChatResponse = { kind: 'clarify', prompt_to_user: replay.prompt_to_user }; + sendJson(res, 200, payload); + return; } saveState(sessionId, engine.exportJson()); }