UNPKG

@sema-agent/core

Version:

Stateless, task-oriented AI agent core

52 lines (51 loc) • 3.25 kB
import { deliverEngineNotice } from "../engine-notice.js"; import { readMember } from "../thrown-value.js"; export function createFinishReasonSeat(onNotice, ids) { const announced = new Set(); const once = (key) => { if (announced.has(key)) return false; announced.add(key); return true; }; return { observe(message) { const word = readMember(message, "finishReasonUnknown"); if (typeof word === "string" && once(`unknown\u0000${word}`)) { deliverEngineNotice(onNotice, { code: "route.finish_reason_unknown", message: `The provider ended a response with "${word}", a finish reason this engine does not read. The turn was kept ` + `as the model left it, but the provider may have cut it short — the text above may be incomplete.`, detail: { finishReason: word, sessionId: ids.sessionId, runId: ids.runId }, }); } const late = readMember(message, "streamEndIgnored"); const statement = readMember(late, "statement"); const lateWord = readMember(late, "word"); const frames = readMember(late, "frames"); const dropped = readMember(late, "contentDropped"); const valid = (statement === "ending" || statement === "error" || statement === "content") && typeof lateWord === "string" && typeof frames === "number"; if (valid && typeof dropped === "string") { const ending = statement === "ending" ? lateWord : undefined; if (once(`dropped\u0000${dropped}`)) { deliverEngineNotice(onNotice, { code: "route.stream_content_dropped", message: `After the provider had already ended a response, its stream sent more of the model's output (first: ${dropped} content` + `${ending !== undefined ? ` alongside the ending "${ending}"` : ""}; ${frames} frame${frames === 1 ? "" : "s"} after the end in all). ` + `The first ending stands and that output was not added to the turn — the answer above may be missing what the model sent after it.`, detail: { word: dropped, ...(ending !== undefined ? { ending } : {}), frames, sessionId: ids.sessionId, runId: ids.runId }, }); } } else if (valid && once(`late\u0000${statement}\u0000${lateWord}`)) { const what = statement === "error" ? `an error "${lateWord}"` : statement === "ending" ? `the ending "${lateWord}"` : `${lateWord} content`; deliverEngineNotice(onNotice, { code: "route.stream_end_ignored", message: `After the provider had already ended a response, its stream sent more (first: ${what}; ${frames} frame${frames === 1 ? "" : "s"} in all). ` + `The first ending stands; nothing after it was added to the turn.`, detail: { statement, word: lateWord, frames, sessionId: ids.sessionId, runId: ids.runId }, }); } }, }; }