@sema-agent/core
Version:
Stateless, task-oriented AI agent core
768 lines (767 loc) • 43.2 kB
JavaScript
import {} from "../internal/llm.js";
import { BrainError } from "./errors.js";
import { DEGENERATE_MESSAGE, trimDegenerateTail } from "./repetition.js";
import { STREAM_TORN_WARNING, createRepetitionPoll, noteResponseModel, parseStreamedToolArgs, placeTurnNotes } from "./stream-shared.js";
import { admitToolCallId, providerResultIds, providerToolCallId, responseToolCallIds } from "./tool-call-id.js";
import { emitBrainTelemetry } from "./status-sink.js";
import { errorResultMediaNote, IMAGE_OMITTED_NO_VISION, imagesOmittedNoVisionNote, modelSupportsVision, sendableImages } from "./media-degrade.js";
import { OUTPUT_CAP_KEYS, RESPONSES_RESERVED, applyExtraBody, effectiveOutputCap, lockHeader, mergeHeaders } from "./request-params.js";
import { adjudicateModelRoute, applyRouteCredentialHeaders, assertServableRequestRoot, createBrainRouteJudge, joinRequestUrl, resolveRequestRoot, resolveRouteCredential, routeRefusalText } from "./route-adjudicator.js";
import { declaredOffSpelling, mintEffortWireValue, reasoningRequestCarried } from "./reasoning.js";
import { runStreamingBrain } from "./stream-engine.js";
import { refuseRetiredTimeoutKnobs } from "./timeout.js";
import { resolveFetch } from "./transport.js";
const DEGENERATE_POLL_CHARS = 64;
const MALFORMED_SAMPLE_CHARS = 160;
const MALFORMED_ARGS_CHARS = 200;
const CAPABILITY_DIAGNOSTIC_TYPE = "responses_capability";
function responsesCompat(model) {
return (model.compat ?? {});
}
const NO_CONTENT = "(no content)";
const DOCUMENT_OMITTED = "[PDF document omitted: PDF direct read requires an Anthropic-family model; re-Read the file (the engine will extract text/render pages when possible) or extract the text via Bash instead (e.g. `pdftotext`).]";
function textOf(content) {
if (typeof content === "string")
return content;
return content
.filter((c) => c.type === "text" && typeof c.text === "string")
.map((c) => c.text)
.join("");
}
function functionCallOutputText(text, hasCarriedImages, original) {
if (text !== "")
return text;
if (hasCarriedImages)
return "(image attached below)";
return Array.isArray(original) && original.length > 0 ? "(non-text tool result omitted)" : "";
}
function toUserParts(content, supportsVision) {
if (typeof content === "string") {
return content === "" ? [] : [{ type: "input_text", text: content }];
}
const nDocs = content.filter((c) => c.type === "document").length;
if (nDocs > 0)
emitBrainTelemetry({ kind: "document_placeholder", count: nDocs });
const images = content.filter((c) => c.type === "image" && c.data !== undefined && c.mimeType !== undefined);
if (!supportsVision && images.length > 0)
emitBrainTelemetry({ kind: "vision_placeholder", count: images.length });
const parts = [];
for (const c of content) {
if (c.type === "text" && typeof c.text === "string" && c.text.length > 0) {
parts.push({ type: "input_text", text: c.text });
}
else if (c.type === "image" && c.data !== undefined && c.mimeType !== undefined) {
if (supportsVision)
parts.push({ type: "input_image", image_url: `data:${c.mimeType};base64,${c.data}` });
}
else if (c.type === "document") {
parts.push({ type: "input_text", text: DOCUMENT_OMITTED });
}
}
if (!supportsVision && images.length > 0)
parts.push({ type: "input_text", text: imagesOmittedNoVisionNote(images.length) });
return parts;
}
function toResponsesInput(ctx, model, replayReasoning) {
const supportsVision = modelSupportsVision(model);
const out = [];
let pendingImages = [];
const flushImages = () => {
if (pendingImages.length === 0)
return;
const ids = [...new Set(pendingImages.map((i) => i.toolCallId))].join(", ");
out.push({
type: "message",
role: "user",
content: [
{ type: "input_text", text: `[Image${pendingImages.length > 1 ? "s" : ""} from tool result ${ids}]` },
...pendingImages.map((i) => ({ type: "input_image", image_url: `data:${i.mimeType};base64,${i.data}` })),
],
});
pendingImages = [];
};
const resultWireId = providerResultIds(ctx.messages);
for (const m of ctx.messages) {
if (m.role !== "toolResult")
flushImages();
if (m.role === "user") {
const parts = toUserParts(m.content, supportsVision);
out.push({
type: "message",
role: "user",
content: parts.length > 0 ? parts : [{ type: "input_text", text: NO_CONTENT }],
});
}
else if (m.role === "assistant") {
const toolCalls = m.content.filter((c) => c.type === "toolCall");
const thinking = m.content.filter((c) => c.type === "thinking");
const text = m.content
.filter((c) => c.type === "text")
.map((c) => c.text)
.join("");
if (replayReasoning && toolCalls.length > 0) {
for (const t of thinking) {
if (t.thinking === "")
continue;
out.push({
type: "reasoning",
...(t.thinkingSignature !== undefined ? { id: t.thinkingSignature } : {}),
content: [{ type: "reasoning_text", text: t.thinking }],
summary: [],
});
}
}
if (text !== "")
out.push({ type: "message", role: "assistant", content: [{ type: "output_text", text }] });
for (const tc of toolCalls) {
out.push({ type: "function_call", call_id: providerToolCallId(tc), name: tc.name, arguments: JSON.stringify(tc.arguments ?? {}) });
}
}
else if (m.role === "toolResult") {
const mediaImages = sendableImages(Array.isArray(m.content) ? m.content : []);
const rawImages = m.isError ? [] : mediaImages;
const images = supportsVision ? rawImages : [];
if (!supportsVision && rawImages.length > 0)
emitBrainTelemetry({ kind: "vision_placeholder", count: rawImages.length });
const nDocs = Array.isArray(m.content) ? m.content.filter((c) => c.type === "document").length : 0;
if (nDocs > 0)
emitBrainTelemetry({ kind: "document_placeholder", count: nDocs });
const baseText0 = textOf(m.content);
const baseText = nDocs > 0 ? (baseText0 ? `${baseText0}\n${DOCUMENT_OMITTED}` : DOCUMENT_OMITTED) : baseText0;
const withVisionNote = !supportsVision && rawImages.length > 0 ? (baseText ? `${baseText}\n${IMAGE_OMITTED_NO_VISION}` : IMAGE_OMITTED_NO_VISION) : baseText;
const errorNote = m.isError ? errorResultMediaNote({ images: mediaImages.length, documents: 0 }) : undefined;
const finalText = errorNote === undefined ? withVisionNote : withVisionNote ? `${withVisionNote}\n${errorNote}` : errorNote;
out.push({
type: "function_call_output",
call_id: resultWireId(m.toolCallId),
output: functionCallOutputText(finalText, images.length > 0, m.content),
});
for (const img of images)
pendingImages.push({ toolCallId: resultWireId(m.toolCallId), data: img.data, mimeType: img.mimeType });
}
}
flushImages();
return out;
}
function toResponsesTools(ctx) {
if (!ctx.tools || ctx.tools.length === 0)
return undefined;
return ctx.tools.map((t) => ({
type: "function",
name: t.name,
description: t.description,
parameters: t.parameters,
strict: false,
}));
}
function resolveWireEffort(model, reasoning) {
if (model.reasoning && reasoning === "off")
return declaredOffSpelling(model);
const compat = responsesCompat(model);
if (compat.supportsReasoningEffort === false)
return undefined;
if (!reasoningRequestCarried(model, reasoning))
return undefined;
return mintEffortWireValue(reasoning, model, compat.reasoningEffortLevels).wireValue;
}
function computeUsage(model, raw) {
const input = raw?.input_tokens ?? 0;
const output = raw?.output_tokens ?? 0;
const total = raw?.total_tokens ?? input + output;
const cacheRead = raw?.input_tokens_details?.cached_tokens ?? 0;
const c = model.cost;
const costInput = (input / 1e6) * (c?.input ?? 0);
const costOutput = (output / 1e6) * (c?.output ?? 0);
return {
input,
output,
cacheRead,
cacheWrite: 0,
totalTokens: total,
cost: { input: costInput, output: costOutput, cacheRead: 0, cacheWrite: 0, total: costInput + costOutput },
};
}
function responsesErrorCode(code) {
switch (code) {
case "rate_limit_exceeded":
return "rate_limit";
case "authentication_error":
case "invalid_api_key":
case "permission_error":
return "auth";
case "invalid_request_error":
case "invalid_prompt":
return "invalid_request";
default:
return "server";
}
}
const INCOMPLETE_MAX_OUTPUT_TOKENS = "max_output_tokens";
const INCOMPLETE_CONTENT_FILTER = "content_filter";
const RESPONSE_INCOMPLETE_WARNING = 'The response above may be incomplete: the endpoint reported status "incomplete" with no reason this client recognises.';
function closeFunctionCall(acc, responseIds) {
if (acc.closedTc)
return acc.closedTc;
if (!acc.name)
return undefined;
const args = parseStreamedToolArgs(acc.args);
if (args === undefined)
return undefined;
const { engineId, wireId } = admitToolCallId(acc.callId, "call_", responseIds);
acc.closedTc = { type: "toolCall", id: engineId, ...(wireId !== engineId ? { wireId } : {}), name: acc.name, arguments: args };
return acc.closedTc;
}
function mapTerminalStopReason(incompleteReason, hadToolCalls) {
if (hadToolCalls)
return "toolUse";
if (incompleteReason === INCOMPLETE_MAX_OUTPUT_TOKENS)
return "length";
return "stop";
}
export function createOpenResponsesBrain(config = {}) {
assertServableRequestRoot(config.baseUrl, "config.baseUrl");
refuseRetiredTimeoutKnobs(config, "construction", "createOpenResponsesBrain config");
const doFetch = resolveFetch(config);
const stream = (model, context, options) => {
let sentMaxTokens;
let sentMaxTokensLane = "unset";
let sentEffort;
let builtMaxTokens;
let builtMaxTokensLane = "unset";
return runStreamingBrain({
model,
doFetch,
signal: options?.signal,
config,
httpLabel: "responses",
maxRetries: options?.maxRetries,
stallTimeouts: options?.stallTimeouts,
buildRequest: (overrides) => {
const root = resolveRequestRoot(model.baseUrl, config.baseUrl);
if (!root) {
throw new Error("createOpenResponsesBrain: no baseUrl configured (set config.baseUrl or model.baseUrl)");
}
const routeCredential = resolveRouteCredential({
optionsApiKey: options?.apiKey,
optionsHeaders: options?.headers,
modelHeaders: model.headers,
configApiKey: config.apiKey,
configHeaders: config.headers,
});
const routeVerdict = adjudicateModelRoute(model, routeCredential, { baseUrl: config.baseUrl });
if (!routeVerdict.ok)
throw new BrainError("invalid_request", routeRefusalText(routeVerdict));
const apiKey = routeCredential.apiKey;
if (options?.stop !== undefined && options.stop.length > 0) {
throw new Error("createOpenResponsesBrain: options.stop is not expressible in the Open Responses request (the wire form has no stop-sequence field); " +
"remove it, or serve this model through the chat-completions lane, which does support it");
}
const requestedMaxTokens = overrides?.maxOutputTokens ?? options?.maxTokens ?? model.maxTokens;
const effort = resolveWireEffort(model, options?.reasoning);
const body = {
model: model.id,
input: toResponsesInput(context, model, config.replayReasoning !== false),
stream: true,
};
if (context.systemPrompt)
body.instructions = context.systemPrompt;
const tools = toResponsesTools(context);
if (tools)
body.tools = tools;
if (options?.temperature !== undefined)
body.temperature = options.temperature;
if (requestedMaxTokens !== undefined) {
const capped = Math.max(1, requestedMaxTokens);
body.max_output_tokens = capped;
builtMaxTokens = capped;
builtMaxTokensLane =
overrides?.maxOutputTokens !== undefined ? "engine" : options?.maxTokens !== undefined ? "options" : "model";
}
else {
builtMaxTokens = undefined;
builtMaxTokensLane = "unset";
}
if (effort !== undefined)
body.reasoning = { effort };
const headers = mergeHeaders(model.headers, config.headers, options?.headers);
applyRouteCredentialHeaders(headers, routeCredential, { model: model.headers, options: options?.headers });
lockHeader(headers, "content-type", "application/json");
if (apiKey)
headers["authorization"] = `Bearer ${apiKey}`;
const wire = applyExtraBody(body, model.extraBody, RESPONSES_RESERVED);
return {
url: joinRequestUrl(root, "/responses"),
headers,
body: JSON.stringify(wire),
outputCapTokens: effectiveOutputCap(wire, OUTPUT_CAP_KEYS.responses),
capDerivedPaths: OUTPUT_CAP_KEYS.responses,
onCommitted: () => {
sentMaxTokens = builtMaxTokens;
sentMaxTokensLane = builtMaxTokensLane;
sentEffort = effort;
},
};
},
makeParser: (ctrl) => {
const { out, partial } = ctrl;
const items = new Map();
const responseIds = responseToolCallIds();
let usageRaw;
let responseId;
let terminalStatus;
const terminalFrameSeen = () => terminalStatus !== undefined;
let incompleteReason;
let accumRefusal = "";
const capabilityNotes = [];
let degenerate = false;
let degenFace;
let degenItem;
const rep = createRepetitionPoll();
let lastDegenCheckText = 0;
let lastDegenCheckReasoning = 0;
const detectRep = config.detectRepetition !== false;
let malformedFrames = 0;
let malformedSample;
const auditEcho = (env) => {
if (env.store === true) {
capabilityNotes.push("endpoint reports store=true although the request never sent `store` — server-side response state may be retained");
}
if (env.previous_response_id !== undefined && env.previous_response_id !== null) {
capabilityNotes.push("endpoint echoed a previous_response_id although the request never sent one — the turn may have been linked server-side");
}
if (env.conversation !== undefined && env.conversation !== null) {
capabilityNotes.push("endpoint echoed a conversation binding although the request never sent one — the turn may have been linked server-side");
}
if (sentEffort !== undefined) {
const echoed = env.reasoning?.effort;
if (echoed !== sentEffort) {
capabilityNotes.push(`reasoning effort "${sentEffort}" was requested but the endpoint reports ${echoed === null || echoed === undefined ? "none" : `"${echoed}"`}`);
}
}
if (env.parallel_tool_calls === false) {
capabilityNotes.push("endpoint reports parallel_tool_calls=false — tool calls will arrive one per turn on this lane");
}
};
const openItem = (index, kind, itemId) => {
const acc = { kind, text: "", itemId, callId: "", name: "", args: "", closed: false };
items.set(index, acc);
return acc;
};
const pushReasoningDelta = (acc, index, delta) => {
if (delta === "")
return;
ctrl.sawContentToken();
if (acc.closed)
return;
if (!acc.block) {
acc.block = { type: "thinking", thinking: "" };
partial.content = [acc.block];
out.push({ type: "thinking_start", contentIndex: index, partial: { ...partial } });
}
acc.text += delta;
if (acc.block.type === "thinking")
acc.block.thinking = acc.text;
partial.content = [acc.block];
out.push({ type: "thinking_delta", contentIndex: index, delta, partial: { ...partial } });
if (detectRep && !degenerate && acc.text.length - lastDegenCheckReasoning >= DEGENERATE_POLL_CHARS) {
lastDegenCheckReasoning = acc.text.length;
if (rep.poll(acc.text)) {
degenerate = true;
degenFace = "reasoning";
degenItem = acc;
ctrl.cancel();
}
}
};
const pushTextDelta = (acc, index, delta) => {
if (delta === "")
return;
ctrl.sawContentToken();
if (acc.closed)
return;
if (!acc.block) {
acc.block = { type: "text", text: "" };
partial.content = [acc.block];
out.push({ type: "text_start", contentIndex: index, partial: { ...partial } });
}
acc.text += delta;
if (acc.block.type === "text")
acc.block.text = acc.text;
partial.content = [acc.block];
out.push({ type: "text_delta", contentIndex: index, delta, partial: { ...partial } });
if (detectRep && !degenerate && acc.text.length - lastDegenCheckText >= DEGENERATE_POLL_CHARS) {
lastDegenCheckText = acc.text.length;
if (rep.poll(acc.text)) {
degenerate = true;
degenFace = "text";
degenItem = acc;
ctrl.cancel();
}
}
};
const messageFace = (acc) => acc === degenItem && degenFace === "text" && rep.cut !== undefined ? trimDegenerateTail(acc.text, rep.cut) : acc.text;
const closeItem = (index, acc) => {
if (acc.closed)
return;
acc.closed = true;
if (acc.kind === "reasoning") {
if (acc.block)
out.push({ type: "thinking_end", contentIndex: index, content: acc.text, partial: { ...partial } });
}
else if (acc.kind === "message") {
if (acc.block)
out.push({ type: "text_end", contentIndex: index, content: messageFace(acc), partial: { ...partial } });
}
else {
const tc = closeFunctionCall(acc, responseIds);
if (tc)
out.push({ type: "toolcall_end", contentIndex: index, toolCall: tc, partial: { ...partial } });
}
};
const handleFrame = (frame) => {
const type = frame.type;
if (type === undefined)
return;
switch (type) {
case "response.created": {
const env = frame.response;
if (env) {
if (env.id !== undefined)
responseId = env.id;
noteResponseModel(partial, env.model, model.id);
auditEcho(env);
}
return;
}
case "response.output_item.added": {
ctrl.sawContentToken();
const index = frame.output_index;
if (index === undefined)
return;
const itemType = frame.item?.type;
const itemId = frame.item?.id ?? "";
if (itemType === "reasoning") {
openItem(index, "reasoning", itemId);
}
else if (itemType === "message") {
openItem(index, "message", itemId);
}
else if (itemType === "function_call") {
const acc = openItem(index, "function_call", itemId);
acc.callId = frame.item?.call_id ?? "";
acc.name = frame.item?.name ?? "";
if (frame.item?.arguments)
acc.args += frame.item.arguments;
}
return;
}
case "response.reasoning_text.delta": {
const index = frame.output_index;
if (index === undefined || frame.delta === undefined)
return;
const acc = items.get(index) ?? openItem(index, "reasoning", "");
pushReasoningDelta(acc, index, frame.delta);
return;
}
case "response.output_text.delta": {
const index = frame.output_index;
if (index === undefined || frame.delta === undefined)
return;
const acc = items.get(index) ?? openItem(index, "message", "");
pushTextDelta(acc, index, frame.delta);
return;
}
case "response.refusal.delta": {
if (frame.delta === undefined)
return;
ctrl.sawContentToken();
accumRefusal += frame.delta;
return;
}
case "response.refusal.done": {
if (frame.refusal === undefined || accumRefusal !== "")
return;
ctrl.sawContentToken();
accumRefusal = frame.refusal;
return;
}
case "response.reasoning_text.done": {
const index = frame.output_index;
if (index === undefined || frame.text === undefined)
return;
const acc = items.get(index) ?? openItem(index, "reasoning", "");
if (acc.text === "")
pushReasoningDelta(acc, index, frame.text);
return;
}
case "response.output_text.done": {
const index = frame.output_index;
if (index === undefined || frame.text === undefined)
return;
const acc = items.get(index) ?? openItem(index, "message", "");
if (acc.text === "")
pushTextDelta(acc, index, frame.text);
return;
}
case "response.function_call_arguments.delta": {
const index = frame.output_index;
if (index === undefined || frame.delta === undefined)
return;
const acc = items.get(index);
if (!acc || acc.kind !== "function_call")
return;
ctrl.sawContentToken();
if (!acc.closed)
acc.args += frame.delta;
return;
}
case "response.function_call_arguments.done": {
const index = frame.output_index;
if (index === undefined || frame.arguments === undefined)
return;
const acc = items.get(index);
if (!acc || acc.kind !== "function_call" || acc.closed)
return;
ctrl.sawContentToken();
if (acc.args === "")
acc.args = frame.arguments;
return;
}
case "response.output_item.done": {
const index = frame.output_index;
if (index === undefined)
return;
const acc = items.get(index);
if (!acc)
return;
if (acc.kind === "function_call") {
if (!acc.callId && frame.item?.call_id)
acc.callId = frame.item.call_id;
if (!acc.name && frame.item?.name)
acc.name = frame.item.name;
if (acc.args === "" && frame.item?.arguments)
acc.args = frame.item.arguments;
}
if (!acc.itemId && frame.item?.id)
acc.itemId = frame.item.id;
closeItem(index, acc);
return;
}
case "response.completed":
case "response.incomplete": {
const env = frame.response;
terminalStatus = env?.status ?? (type === "response.completed" ? "completed" : "incomplete");
incompleteReason = env?.incomplete_details?.reason;
if (env?.usage)
usageRaw = env.usage;
if (env?.id !== undefined)
responseId = env.id;
noteResponseModel(partial, env?.model, model.id);
return;
}
case "response.failed": {
noteResponseModel(partial, frame.response?.model, model.id);
const err = frame.response?.error;
throw new BrainError(responsesErrorCode(err?.code), `responses stream failed (${err?.code ?? "unknown"}): ${err?.message ?? ""}`, undefined, { apiError: true });
}
case "error": {
throw new BrainError(responsesErrorCode(frame.code), `responses stream error (${frame.code ?? "unknown"}): ${frame.message ?? ""}`, undefined, { apiError: true });
}
default:
return;
}
};
return {
snapshot() {
const accs = [...items.values()];
const hasCompletedToolCall = accs.some((a) => a.closedTc !== undefined);
const hasSubstantiveText = accs.some((a) => a.kind === "message" && a.text.trim() !== "") || accumRefusal.trim() !== "";
const hasReasoning = accs.some((a) => a.kind === "reasoning" && a.text.length > 0);
return {
hasSubstantiveText,
hasCompletedToolCall,
hasOnlyThinking: hasReasoning && !hasSubstantiveText && !hasCompletedToolCall,
terminalFrameSeen: terminalFrameSeen(),
};
},
sealForRetry() {
for (const [index, acc] of [...items.entries()].sort((a, b) => a[0] - b[0])) {
if (acc.kind === "reasoning" && !acc.closed) {
acc.closed = true;
if (acc.block)
out.push({ type: "thinking_end", contentIndex: index, content: acc.text, partial: { ...partial } });
}
}
},
onLine(rawLine) {
const line = rawLine.trim();
if (!line.startsWith("data:"))
return;
const data = line.slice(5).trim();
if (!data)
return;
let frame;
try {
frame = JSON.parse(data);
}
catch {
malformedFrames++;
malformedSample ??= data.slice(0, MALFORMED_SAMPLE_CHARS);
return;
}
handleFrame(frame);
},
finalize(opts) {
for (const [index, acc] of [...items.entries()].sort((a, b) => a[0] - b[0])) {
if (acc.closed)
continue;
if (acc.kind === "function_call") {
acc.closed = true;
continue;
}
if (acc.kind === "message" && opts?.truncated === true) {
acc.closed = true;
continue;
}
closeItem(index, acc);
}
const finalContent = [];
const toolCalls = [];
const malformed = [];
const unnamed = [];
let anyText = false;
let reasoningSeen = false;
for (const [, acc] of [...items.entries()].sort((a, b) => a[0] - b[0])) {
if (acc.kind === "reasoning") {
if (acc.text.length === 0)
continue;
reasoningSeen = true;
finalContent.push({
type: "thinking",
thinking: acc.text,
...(acc.itemId !== "" ? { thinkingSignature: acc.itemId } : {}),
});
}
else if (acc.kind === "message") {
const text = messageFace(acc);
if (text !== "") {
finalContent.push({ type: "text", text });
if (text.trim() !== "")
anyText = true;
}
}
else {
const tc = closeFunctionCall(acc, responseIds);
if (tc) {
toolCalls.push(tc);
finalContent.push(tc);
}
else if (acc.name) {
malformed.push(`${acc.name}(${acc.args.slice(0, MALFORMED_ARGS_CHARS)})`);
}
else {
unnamed.push(`call_id="${acc.callId || "?"}"(${acc.args.slice(0, MALFORMED_ARGS_CHARS)})`);
}
}
}
const noUsableContent = toolCalls.length === 0 && !anyText;
const terminalLabel = terminalStatus ?? "?";
const toolErrorParts = [];
if (malformed.length > 0) {
toolErrorParts.push(`tool call argument(s) not valid JSON (likely truncated, status="${terminalLabel}"): ${malformed.join("; ")}`);
}
if (unnamed.length > 0) {
toolErrorParts.push(`tool call(s) arrived with no tool name and cannot be executed (status="${terminalLabel}"): ${unnamed.join("; ")}`);
}
const toolError = toolErrorParts.length > 0 ? toolErrorParts.join(" | ") : undefined;
const notes = [];
if (malformed.length > 0) {
notes.push({ cause: "truncated", count: malformed.length, text: `\n[note: ${malformed.length} tool call(s) were truncated (status="${terminalLabel}") and dropped — re-issue them next turn: ${malformed.join("; ")}]` });
}
if (unnamed.length > 0) {
notes.push({ cause: "unnamed", count: unnamed.length, text: `\n[note: ${unnamed.length} tool call(s) arrived with no tool name and were dropped — re-issue them next turn with an explicit tool name: ${unnamed.join("; ")}]` });
}
if (malformedFrames > 0) {
notes.push({ cause: "bad_frame", count: malformedFrames, text: `\n[note: ${malformedFrames} provider stream frame(s) were not valid JSON and were dropped — the streamed output may be missing content (first sample: ${JSON.stringify(malformedSample)})]` });
}
placeTurnNotes(partial, finalContent, notes);
const truncated = incompleteReason === INCOMPLETE_MAX_OUTPUT_TOKENS;
const truncatedEmpty = truncated && noUsableContent && !toolError;
const dynamicCut = truncatedEmpty && options?.maxTokensDynamic === true;
const staticReasoningCut = truncatedEmpty && !dynamicCut && reasoningSeen && options?.staticReasoningCutDowngrade === true;
const safetyCut = incompleteReason === INCOMPLETE_CONTENT_FILTER;
const refused = accumRefusal.trim().length > 0;
const incompleteUnmapped = terminalStatus === "incomplete" && incompleteReason !== INCOMPLETE_MAX_OUTPUT_TOKENS && incompleteReason !== INCOMPLETE_CONTENT_FILTER;
const emptyNoFinish = noUsableContent && (!terminalFrameSeen() || incompleteUnmapped) && !degenerate;
const maxTokensNote = sentMaxTokens !== undefined
? `effective max_output_tokens=${sentMaxTokens} (from ${sentMaxTokensLane === "options"
? "options.maxTokens"
: sentMaxTokensLane === "engine"
? "the engine's context-overflow recovery"
: "model.maxTokens"})`
: "max_output_tokens not set (neither options.maxTokens nor model.maxTokens — the endpoint's default cap applied)";
const errorMessage = degenerate
? DEGENERATE_MESSAGE
: safetyCut
? `[refusal] response cut by the provider content filter (incomplete_details.reason="${INCOMPLETE_CONTENT_FILTER}") — the output was censored/truncated for policy and is unreliable`
: refused
? `[refusal] response refused by the model for policy reasons: ${accumRefusal.trim()}`
: (toolError ??
(truncatedEmpty && !dynamicCut && !staticReasoningCut
? reasoningSeen
? `[length_empty] response truncated at max_output_tokens (${maxTokensNote}) with no answer text — the model spent the whole budget on reasoning; raise the cap, or lower the reasoning effort for this task.`
: `[length_empty] response truncated at max_output_tokens (${maxTokensNote}) with no answer text — raise the cap`
: emptyNoFinish
? `[stream_torn] model stream ended with no content and ${terminalStatus === undefined ? "no terminal response event" : `a terminal status "${terminalStatus}" with no reason this client recognises (incomplete_details.reason=${incompleteReason ?? "absent"})`} — the response was lost (a torn stream or an in-band provider error frame)${malformedFrames > 0 ? ` — ${malformedFrames} unparseable data frame(s) were dropped on this stream (first sample: ${JSON.stringify(malformedSample)})` : ""}`
: undefined));
const errored = degenerate ||
safetyCut ||
refused ||
emptyNoFinish ||
(toolError !== undefined && noUsableContent) ||
(truncatedEmpty && !dynamicCut && !staticReasoningCut);
const tornWithContent = !errored && opts?.truncated !== true && (!terminalFrameSeen() || incompleteUnmapped) && !noUsableContent && !degenerate;
const partialWarning = terminalStatus === undefined ? STREAM_TORN_WARNING : RESPONSE_INCOMPLETE_WARNING;
const errorMessageOut = tornWithContent ? (errorMessage ? `${errorMessage}\n${partialWarning}` : partialWarning) : errorMessage;
const errorKind = degenerate
? "degenerate"
: !safetyCut && !refused && toolError === undefined && truncatedEmpty && !dynamicCut && !staticReasoningCut
? "length_empty"
: undefined;
const diagnostics = capabilityNotes.length > 0
? [...(partial.diagnostics ?? []), { type: CAPABILITY_DIAGNOSTIC_TYPE, timestamp: Date.now(), details: { notes: [...capabilityNotes] } }]
: [];
const finalMessage = {
...partial,
content: finalContent.length > 0 ? finalContent : [{ type: "text", text: "" }],
stopReason: errored ? "error" : mapTerminalStopReason(incompleteReason, toolCalls.length > 0),
usage: computeUsage(model, usageRaw),
...(usageRaw === undefined ? { usageMissing: true } : {}),
timestamp: Date.now(),
...(responseId !== undefined ? { responseId } : {}),
...(errorMessageOut ? { errorMessage: errorMessageOut } : {}),
...(errorKind ? { errorKind } : {}),
...(tornWithContent ? { partialFinalized: true } : {}),
...(rep.cut !== undefined || rep.spared.length > 0
? { repetition: { ...(rep.cut !== undefined ? { cut: rep.cut } : {}), ...(rep.spared.length > 0 ? { spared: rep.spared } : {}) } }
: {}),
...(staticReasoningCut ? { staticReasoningCut: true } : {}),
...(diagnostics.length > 0 ? { diagnostics } : {}),
};
if (errored) {
out.push({ type: "error", reason: "error", error: finalMessage });
}
else {
out.push({
type: "done",
reason: finalMessage.stopReason,
message: finalMessage,
});
}
},
};
},
});
};
return { stream, adjudicateRoute: createBrainRouteJudge(config) };
}