@sema-agent/core
Version:
Stateless, task-oriented AI agent core
983 lines • 56.7 kB
JavaScript
import {} from "../internal/llm.js";
import { BrainError } from "./errors.js";
import { promptCacheKeyDefect } from "./prompt-cache-key.js";
import { DEGENERATE_MESSAGE, trimDegenerateTail } from "./repetition.js";
import { NO_WORD, STREAM_TORN_WARNING, carriesNoContent, createCallLoss, createRepetitionPoll, createStreamEndLedger, droppedCallsEnding, droppedFramesNote, memberReader, modelOutputInvalidOf, noteResponseModel, nameMissingCause, parseStreamedToolArgs, placeTurnNotes, withheldCallsClause } from "./stream-shared.js";
import { reasoningTokensOf } from "./usage-reasoning.js";
import { usageOfStatement } from "./usage-reading.js";
import { admitToolCallId, providerResultIds, providerToolCallId, responseToolCallIds } from "./tool-call-id.js";
import { emitBrainTelemetry } from "./status-sink.js";
import { DOCUMENT_OMITTED, errorResultMediaNote, IMAGE_OMITTED_NO_VISION, imageWithholdOf, imagesOmittedNoVisionNote, reportImagesWithheld, sendableImages, toolImagesPrefix } from "./media-degrade.js";
import { OUTPUT_CAP_KEYS, RESPONSES_RESERVED, applyExtraBody, declaredRequestValues, effectiveOutputCap, headerBagOnce, lockHeader, mergeHeaders } from "./request-params.js";
import { admitRouteRequest, applyRouteCredentialHeaders, assertServableRequestRoot, brainConfigSnapshot, createBrainRouteJudge, joinRequestUrl, resolveRequestRootOf, resolveRouteCredential } from "./route-adjudicator.js";
import { requestRouteOf } from "./request-route.js";
import { NAMES_AS_WRITTEN, resolveProviderDialect, endpointToolNames } from "./provider-dialect.js";
import { applyReasoningToRequest } from "./reasoning-request.js";
import { applyFastModeToRequest, requestSpeedOf } from "./fast-mode.js";
import { requestReasoning } from "./reasoning-stamp.js";
import { requestWithheld } from "./model-capability.js";
import { runStreamingBrain } from "./stream-engine.js";
import { refuseRetiredTimeoutKnobs } from "./timeout.js";
import { resolveFetch } from "./transport.js";
import { projectToolSchema } from "./tool-schema-projection.js";
const OPEN_RESPONSES_KEY_HEADER = "authorization";
const DEGENERATE_POLL_CHARS = 64;
const MALFORMED_SAMPLE_CHARS = 160;
const MALFORMED_ARGS_CHARS = 200;
const CAPABILITY_DIAGNOSTIC_TYPE = "responses_capability";
const NO_CONTENT = "(no content)";
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, withhold) {
if (typeof content === "string") {
return content === "" ? [] : [{ type: "input_text", text: content }];
}
const supportsVision = withhold === undefined;
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 (withhold !== undefined)
reportImagesWithheld(withhold, 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, toolNames) {
const imageWithhold = imageWithholdOf(model);
const supportsVision = imageWithhold === undefined;
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: toolImagesPrefix(pendingImages.length, ids, ctx.promptLocale) },
...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, imageWithhold);
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: toolNames.sent(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 (imageWithhold !== undefined)
reportImagesWithheld(imageWithhold, 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, toolNames) {
if (!ctx.tools || ctx.tools.length === 0)
return undefined;
return ctx.tools.map((t) => ({
type: "function",
name: toolNames.sent(t.name),
description: t.description,
parameters: projectToolSchema(t.parameters),
strict: false,
}));
}
function reasoningOf(raw) {
if (raw === undefined)
return { unreadable: false };
const claimed = memberReader().claimIn(raw.output_tokens_details, "reasoning_tokens");
const { reasoning, unreadable } = reasoningTokensOf(typeof raw.output_tokens === "number" ? raw.output_tokens : 0, claimed);
return { ...(reasoning !== undefined ? { reasoning } : {}), unreadable };
}
function reportedUsageOf(model, raw) {
return usageOfStatement(model, "input-includes-cached", { input: raw?.input_tokens, output: raw?.output_tokens, cacheRead: raw?.input_tokens_details?.cached_tokens, total: raw?.total_tokens }, reasoningOf(raw));
}
const CALL_FRAME_KINDS = new Set(["response.output_item.added", "response.output_item.done", "response.function_call_arguments.delta", "response.function_call_arguments.done"]);
function readFrame(raw) {
const frame = memberReader();
const callStream = memberReader();
const obj = frame.object(raw);
if (obj === undefined)
return frame.outOfShape ? undefined : { frame: {}, modeled: false, outOfShape: false, callsUnreadable: false };
const type = frame.word(obj.type);
if (type === undefined)
return undefined;
const placing = CALL_FRAME_KINDS.has(type) ? callStream : frame;
const read = { type };
let modeled = true;
let slotMissing = false;
const noSlot = (v) => v === undefined || v === null;
switch (type) {
case "response.created":
case "response.completed":
case "response.incomplete":
case "response.failed":
read.response = frame.object(obj.response);
break;
case "response.output_item.added":
case "response.output_item.done": {
read.output_index = placing.count(obj.output_index);
const item = placing.object(obj.item);
const itemType = placing.word(item?.type);
const ofCall = itemType === undefined || itemType === "function_call";
const members = ofCall ? callStream : frame;
if (ofCall && noSlot(obj.output_index))
slotMissing = true;
if (item !== undefined)
read.item = { type: itemType, id: members.word(item.id), call_id: members.word(item.call_id), name: members.word(item.name), arguments: members.word(item.arguments) };
break;
}
case "response.function_call_arguments.delta":
case "response.reasoning_text.delta":
case "response.output_text.delta":
read.output_index = placing.count(obj.output_index);
read.delta = placing.word(obj.delta);
if (placing === callStream && noSlot(obj.output_index))
slotMissing = true;
break;
case "response.function_call_arguments.done":
read.output_index = placing.count(obj.output_index);
read.arguments = placing.word(obj.arguments);
if (noSlot(obj.output_index))
slotMissing = true;
break;
case "response.reasoning_text.done":
case "response.output_text.done":
read.output_index = frame.count(obj.output_index);
read.text = frame.word(obj.text);
break;
case "response.refusal.delta":
read.delta = frame.word(obj.delta);
break;
case "response.refusal.done":
read.refusal = frame.word(obj.refusal);
break;
case "error":
read.code = frame.word(obj.code);
read.message = frame.word(obj.message);
break;
default:
modeled = false;
}
const callsUnreadable = callStream.outOfShape || slotMissing;
return { frame: read, modeled, outOfShape: frame.outOfShape || callsUnreadable, callsUnreadable };
}
const wordOf = (v) => (typeof v === "string" && v !== "" ? v : NO_WORD);
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, closed) {
if (acc.closedTc)
return acc.closedTc;
if (!acc.name)
return undefined;
const args = parseStreamedToolArgs(acc.args, closed);
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(given = {}) {
const config = brainConfigSnapshot(given, "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";
let sentToolNames = NAMES_AS_WRITTEN;
return runStreamingBrain({
model,
doFetch,
signal: options?.signal,
config,
httpLabel: "responses",
maxRetries: options?.maxRetries,
stallTimeouts: options?.stallTimeouts,
buildRequest: (overrides) => {
const routed = { id: model.id, baseUrl: model.baseUrl };
const { root, origin: rootOrigin } = resolveRequestRootOf(routed.baseUrl, config.baseUrl);
if (!root) {
throw new Error("createOpenResponsesBrain: no baseUrl configured (set config.baseUrl or model.baseUrl)");
}
const url = joinRequestUrl(root, "/responses");
const speed = requestSpeedOf({ model, call: options?.fastMode, standardSpeed: overrides?.standardSpeed === true });
const withheld = requestWithheld(model, url, speed, overrides?.withhold, options?.tryUndeclared);
const builtToolNames = endpointToolNames(resolveProviderDialect(url, model.api), model.api, (context.tools ?? []).map((t) => t.name));
const modelHeaders = headerBagOnce(model.headers);
const optionsHeaders = headerBagOnce(options?.headers);
const routeCredential = resolveRouteCredential({
optionsApiKey: options?.apiKey,
optionsHeaders,
optionsCredentialOrigin: options?.credentialOrigin,
optionsCredentialOriginUnsettled: options?.credentialOriginUnsettled === true,
modelHeaders,
configApiKey: config.apiKey,
configHeaders: config.headers,
keyHeader: OPEN_RESPONSES_KEY_HEADER,
});
const routeAdmission = admitRouteRequest(routed, routeCredential, { baseUrl: config.baseUrl, requireCredential: config.requireCredential, ...(options?.plaintextOrigins !== undefined ? { plaintextOrigins: options.plaintextOrigins } : {}) });
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 cacheKeyDefect = promptCacheKeyDefect(model);
if (cacheKeyDefect !== undefined)
throw new BrainError("invalid_request", `config.invalid_argument: ${cacheKeyDefect} (model "${model.id}")`);
const body = {
model: model.id,
input: toResponsesInput(context, model, config.replayReasoning !== false, builtToolNames),
stream: true,
};
if (context.systemPrompt)
body.instructions = context.systemPrompt;
const tools = toResponsesTools(context, builtToolNames);
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";
}
const { body: reasoningBody, effort, carried } = applyReasoningToRequest({ api: "openai-responses", model, level: options?.reasoning, body, withheld });
const headers = mergeHeaders(modelHeaders, config.headers, optionsHeaders);
applyRouteCredentialHeaders(headers, routeCredential);
const declared = declaredRequestValues([modelHeaders, config.headers, optionsHeaders, headers], [], apiKey);
lockHeader(headers, "content-type", "application/json");
if (apiKey !== undefined)
lockHeader(headers, OPEN_RESPONSES_KEY_HEADER, `Bearer ${apiKey}`);
const fastBody = applyFastModeToRequest({ model, call: options?.fastMode, speed, body: reasoningBody }).body;
const { body: wire, dropped: extraBodyDropped } = applyExtraBody(fastBody, model, RESPONSES_RESERVED);
return {
url,
headers,
declared,
body: JSON.stringify(wire),
...((context.tools?.length ?? 0) > 0 ? { toolNames: context.tools.map((t) => t.name) } : {}),
extraBodyDropped,
route: requestRouteOf(routeCredential, rootOrigin),
outputCapTokens: effectiveOutputCap(wire, OUTPUT_CAP_KEYS.responses),
capDerivedPaths: OUTPUT_CAP_KEYS.responses,
reasoning: requestReasoning(options?.reasoning, carried),
...(withheld !== undefined && withheld.length > 0 ? { withheld } : {}),
onCommitted: () => {
routeAdmission();
sentMaxTokens = builtMaxTokens;
sentMaxTokensLane = builtMaxTokensLane;
sentEffort = effort;
sentToolNames = builtToolNames;
},
};
},
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;
const detectRep = config.detectRepetition !== false;
let malformedFrames = 0;
let malformedSample;
const dropFrame = (data) => {
malformedFrames++;
malformedSample ??= ctrl.providerWords(data).slice(0, MALFORMED_SAMPLE_CHARS);
};
const loss = createCallLoss();
const afterEnd = createStreamEndLedger(ctrl.providerWords);
const strayHeld = () => [...items.values()].some((a) => a.kind === "function_call" && a.name === "" && a.args !== "");
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, itemDone: 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 - (acc.lastDegenCheck ?? 0) >= DEGENERATE_POLL_CHARS) {
acc.lastDegenCheck = 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?.type === "thinking")
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 = loss.lost || strayHeld() ? undefined : closeFunctionCall(acc, responseIds, true);
if (tc)
out.push({ type: "toolcall_end", contentIndex: index, toolCall: tc, partial: { ...partial } });
}
};
const handleFrame = (frame, unplaced) => {
const type = frame.type;
if (type === undefined)
return;
const onCall = (acc, bytes) => {
if (acc.kind !== "function_call")
return false;
if (bytes !== "")
unplaced();
return true;
};
switch (type) {
case "response.created": {
const env = frame.response;
if (env) {
if (env.id !== undefined)
responseId = env.id;
noteResponseModel(partial, env.model, model.id, ctrl.providerWords);
auditEcho(env);
}
return;
}
case "response.output_item.added": {
ctrl.sawProgress();
const index = frame.output_index;
if (index === undefined)
return;
const taken = items.get(index);
if (taken?.kind === "function_call" && taken.itemDone) {
unplaced();
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 = sentToolNames.called(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", "");
if (onCall(acc, frame.delta))
return;
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", "");
if (onCall(acc, frame.delta))
return;
pushTextDelta(acc, index, frame.delta);
return;
}
case "response.refusal.delta": {
if (frame.delta === undefined)
return;
if (frame.delta !== "")
ctrl.sawContentToken();
else
ctrl.sawProgress();
accumRefusal += frame.delta;
return;
}
case "response.refusal.done": {
if (frame.refusal === undefined || accumRefusal !== "")
return;
if (frame.refusal !== "")
ctrl.sawContentToken();
else
ctrl.sawProgress();
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 (onCall(acc, frame.text))
return;
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 (onCall(acc, frame.text))
return;
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" || acc.closed) {
if (frame.delta !== "")
unplaced();
return;
}
if (frame.delta !== "")
ctrl.sawContentToken();
else
ctrl.sawProgress();
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") {
if (frame.arguments !== "")
unplaced();
return;
}
if (acc.closed)
return;
if (frame.arguments !== "")
ctrl.sawContentToken();
else
ctrl.sawProgress();
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) {
if (frame.item?.type === "function_call" && Boolean(frame.item.name || frame.item.arguments))
unplaced();
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 = sentToolNames.called(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;
acc.itemDone = true;
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, ctrl.providerWords);
return;
}
case "response.failed": {
noteResponseModel(partial, frame.response?.model, model.id, ctrl.providerWords);
const err = frame.response?.error;
if (err !== undefined && err !== null)
ctrl.inBandFrame(err);
throw new BrainError(responsesErrorCode(err?.code), `responses stream failed ${ctrl.providerWords(`(${err?.code ?? "unknown"}): ${err?.message ?? ""}`)}`, undefined, { apiError: true });
}
case "error": {
ctrl.inBandFrame({ code: frame.code, message: frame.message });
throw new BrainError(responsesErrorCode(frame.code), `responses stream error ${ctrl.providerWords(`(${frame.code ?? "unknown"}): ${frame.message ?? ""}`)}`, undefined, { apiError: true });
}
default:
return;
}
};
const refuseAfterEnd = (reading) => {
if (reading === undefined) {
afterEnd.note("content", NO_WORD);
return;
}
const f = reading.frame;
const type = f.type ?? NO_WORD;
switch (type) {
case "response.completed":
case "response.incomplete": {
const status = f.response?.status ?? (type === "response.completed" ? "completed" : "incomplete");
if (status !== terminalStatus)
afterEnd.note("ending", wordOf(status));
else if (reading.outOfShape)
afterEnd.note("content", NO_WORD);
return;
}
case "response.failed":
afterEnd.note("error", wordOf(f.response?.error?.code));
return;
case "error":
afterEnd.note("error", wordOf(f.code));
return;
default: {
if (!reading.modeled) {
if (reading.outOfShape)
afterEnd.note("content", NO_WORD);
return;
}
const offered = Boolean(f.delta || f.text || f.arguments || f.refusal) || type === "response.output_item.added";
afterEnd.note("content", type, offered ? type : undefined);
}
}
};
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?.type === "thinking")
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 (carriesNoContent(data))
return;
let parsed;
try {
parsed = JSON.parse(data);
}
catch {
if (terminalFrameSeen()) {
afterEnd.note("content", NO_WORD);
return;
}
dropFrame(data);
loss.lose();
return;
}
const reading = readFrame(parsed);
if (terminalFrameSeen()) {
refuseAfterEnd(reading);
return;
}
let dropped = false;
const drop = () => {
if (dropped)
return;
dropped = true;
dropFrame(data);
};
if (reading === undefined) {
drop();
loss.lose();
return;
}
if (reading.outOfShape)
drop();
if (reading.callsUnreadable)
loss.lose();
handleFrame(reading.frame, () => {
drop();
loss.lose();
});
},
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);
}
if (strayHeld())
loss.lose();
const finalContent = [];
const toolCalls = [];
const malformed = [];
const withheld = [];
const unnamed = [];
let anyText = false;
let reasoningSeen = false;
const endedCut = terminalStatus === undefined || terminalStatus === "incomplete";
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 closed = acc.itemDone || !endedCut;
const held = acc.name !== "" && loss.withholds(acc.closedTc !== undefined);
const tc = held ? undefined : closeFunctionCall(acc, responseIds, closed);
if (held) {
withheld.push(`${ctrl.providerWords(acc.name)}(${ctrl.providerWords(acc.args).slice(0, MALFORMED_ARGS_CHARS)})`);
}
else if (tc) {
toolCalls.push(tc);
finalContent.push(tc);
}
else if (acc.name) {
malformed.push(`${ctrl.providerWords(acc.name)}(${ctrl.providerWords(acc.args).slice(0, MALFORMED_ARGS_CHARS)})`);
}
else {
(nameMissingCause(closed) === "truncated" ? malformed : unnamed).push(`call_id="${ctrl.providerWords(acc.callId || "?")}"(${ctrl.providerWords(acc.args).slice(0, MALFORMED_ARGS_CHARS)})`);
}
}
}
const noUsableContent = toolCalls.length === 0 && !anyText;
const cut = [...malformed, ...withheld];
const dropEnding = droppedCallsEnding({ dropped: cut.length > 0 || unnamed.length > 0, survivingCalls: toolCalls.length }, options);
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 (withheld.length > 0)
toolErrorParts.push(withheldCallsClause(`status="${terminalLabel}"`, withheld));
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 (cut.length > 0) {
notes.push({ cause: "truncated", count: cut.length, text: `\n[note: ${cut.length} tool call(s) were truncated (status="${terminalLabel}") and dropped — re-issue them next turn: ${cut.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: droppedFramesNote(malformedFrames, malformedSample) });
}
placeTurnNotes(partial, finalContent, notes, { endWord: terminalStatus, contextWindow: model.contextWindow, maxTokens: model.maxTokens });
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 && dropEnding !== "toolUse";
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: ${ctrl.providerWords(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 "${ctrl.providerWords(terminalStatus)}" with no reason this client recognises (incomplete_details.reason=${incompleteReason === undefined ? "absent" : ctrl.providerWords(String(incompleteReason))})`} — 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 ||
dropEnding === "failed" ||
(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 capFacts = { capStop: truncated, outputTokens: usageRaw?.output_tokens, capTokens: sentMaxTokens };
const usageReading = reportedUsageOf(model, usageRaw);
const modelOutputInvalid = errored && toolError !== undefined && !degenerate && !safetyCut && !refused ? modelOutputInvalidOf({ truncated: cut.length, unnamed: unnamed.length }, capFacts) : 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" : dropEnding === "toolUse" ? "toolUse" : mapTerminalStopReason(incompleteReason, toolCalls.length > 0),
usage: usageReading.usage,
...(usageReading.missing ? { usageMissing: true } : {}),
timestamp: Date.now(),
...(responseId !== undefined ? { responseId } : {}),
...(errorMessageOut ? { errorMessage: errorMessageOut } : {}),
...(errorKind ? { errorKind } : {}),
...(modelOutputInvalid !== undefined ? { modelOutputInvalid } : {}),
...(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 } : {}),
...(afterEnd.ignored !== undefined ? { streamEndIgnored: afterEnd.ignored } : {}),
...(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, keyHeader: OPEN_RESPONSES_KEY_HEADER }) };
}