Open-source Grok Bot alternative with a virtual machine that bots can use
TypeScript ai-agentsclaude-codecodexcomputer-usedockerelectrongrokgrok-botgrok-bot-alternativeself-hostedtypescript
import { RuntimeNoticeError, runtimeErrorText } from "./runtime-notice-error.ts";
import { createChatReplay, keepRecent, type DeliveredUserMessage } from "./openai-chat-replay.ts";
import type { DriverCreateInput, ModelCatalog, ProviderInstance, ProviderSnapshot, RuntimeEvent, RuntimeEventListener, SendTurnInput, TextGenerationOptions } from "../contracts.ts";
import { newEventId, newId } from "../contracts.ts";
import { CustomHeaderError, customHeaderSecrets, maskCustomSecrets, resolveCustomHeaders, scrubErrorSecrets, type OpenAICompatCustomHeadersConfig, type RuntimeHeaderContext } from "./openai-compat-headers.ts";
import { ASK_USER_TOOL, ASK_USER_TOOL_DEFINITION, askQuestionSummary, parseAskQuestions, questionChoices } from "../../shared/ask-question.ts";
import { allowsTool, parseToolScope } from "../../shared/tool-scope.ts";
import { redactSecretsInText } from "../redact.ts";
import { toolDetailPreview } from "../tool-summary.ts";
import { ChatToolSessionError, chatToolNotices, joinLateChatTools, mountChatTools, refitChatTools, withHostFileTools, type ChatToolDefinition, type ChatToolSession, type ChatToolResult } from "./chat-mcp-tools.ts";
import { noteToolBudget } from "../tool-budgets.ts";
import { chatToolLimitFor, chatToolLimitKey } from "../tool-count-limit.ts";
import { assertImageTransport, chatImageBudget, chatToolImages, chatUserContent, type ChatContentPart } from "./chat-images.ts";
import { promptHalves, volatileContextNote, withContextNote } from "./prompt-split.ts";
import { createChatToolApproval, refusal } from "./chat-tool-approval.ts";
import { ChatProtocolError, ChatReasoningDetails, ChatToolCalls, CUT_REPLY_NOTE, cutReplyContinues, cutReplyWillContinue, object, redactAskQuestions, replayableToolCalls, toolArguments, type ChatToolCall } from "./openai-chat-protocol.ts";
import { ContinuedReply, MaskedDeltas, maskSecretHalves } from "./cut-reply-text.ts";
import { CORRECTIVE_ROUND, NUDGE_ANNOUNCED_ACTION, TEXT_ONLY_ATTACHMENT_NOTE, TEXT_ONLY_SCREENSHOT_NOTE, UNREADABLE_CALL } from "./openai-chat-prompts.ts";
import { ChatHttpError, completionError, httpErrorDetail, refusedToolCall, refusedToolCallFailure, rejectsReasoningReplay, rejectsToolsParameter, RejectedToolCallError, sseData, StreamTruncatedError, UnsupportedChatToolsError, UnsupportedReasoningReplayError, type ProviderBody } from "./openai-chat-refusals.ts";
import { ChatRepeatGuard } from "./chat-repeat-guard.ts";
import { TextCallHoldback, textToolCalls } from "./chat-text-tool-calls.ts";
import { createModelRoutes, isOpenAIAddress, ModelRouteRefused, noRouteLeft, readResponsesStream, responsesRequest, routeRefusal, selectedReasoningEffort, type ModelRoute, type ResponseItem } from "./openai-responses.ts";
import { appendNative } from "./native.ts";
import { classifyError, computeBackoff, interruptibleDelay, RETRY_MAX_ATTEMPTS } from "./retry.ts";
import { plainFailure } from "./engine-errors.ts";
import { noticeFailure } from "../chat-notices.ts";
import { toolFailureMessage } from "./tool-failure.ts";
import { refusedWindow, refusesRequestSize, RequestTooLargeError, resendsAllowed, shareThatFits, sizeChange, smallerResend } from "./chat-size-limit.ts";
import { requestTooLargeMessage } from "../../shared/request-too-large.ts";
import { toolSchemaProfileFor } from "../tool-schema-profile.ts";
import { classifyContinuable, writeTurnHandoff } from "../turn-continuation.ts";
import { KEY_REJECTED_REASON, keyRejected, noteKeyAccepted, noteKeyRejected, rejectsKey } from "../key-rejections.ts";
import { FETCH_LIMIT_MS, fetchPastLimit, prepareChatModel } from "./openai-chat-fetch.ts";
import { announcesAction } from "./chat-announcement.ts"; import { shellOff } from "./shell-off.ts";
export interface OpenAIChatMessage {
role: "system" | "user" | "assistant" | "tool";
content: string | ChatContentPart[] | null;
tool_calls?: ChatToolCall[];
tool_call_id?: string;
reasoning_content?: string;
reasoning?: string;
reasoning_details?: Record<string, unknown>[];
}
interface Usage {
input: number;
output: number;
cachedInput?: number;
}
export interface Completion {
text: string;
reasoning: string;
usage: Usage | null;
toolCalls: ChatToolCall[];
finishReason: string | null;
protocolReasoning: string;
protocolReasoningDetails: Record<string, unknown>[];
/** Every output item of a Responses API reply, encrypted reasoning included, for the next round of this turn only. */
responseOutput?: ResponseItem[];
}
interface CompletionJson extends ProviderBody {
model?: string;
choices?: Array<{
index?: number;
message?: { content?: unknown; reasoning_content?: unknown; reasoning?: unknown; reasoning_details?: unknown; tool_calls?: unknown; function_call?: unknown };
delta?: { content?: unknown; reasoning_content?: unknown; reasoning?: unknown; reasoning_details?: unknown; tool_calls?: unknown; function_call?: unknown };
finish_reason?: string | null;
}>;
usage?: { prompt_tokens?: number; completion_tokens?: number; prompt_tokens_details?: { cached_tokens?: number }; cost?: number };
/** Groq reports streaming usage in its final x_groq envelope. */
x_groq?: { usage?: CompletionJson["usage"] };
}
interface NativeLog {
source: string;
outgoing(turn: SendTurnInput, messages: OpenAIChatMessage[], model: string, tools: ChatToolDefinition[]): unknown;
incoming(completion: Completion): unknown;
}
const LIVENESS_MISSES = 3;
function finalResponseText(output: ResponseItem[]): string {
return output.filter(({ type, role, phase }) => type === "message" && role === "assistant" && (phase == null || phase === "final_answer"))
.map(({ content }) => Array.isArray(content) ? content.map((part) => part?.type === "output_text" && typeof part.text === "string" ? part.text : "").join("") : "")
.filter(Boolean).join("\n\n");
}
interface RuntimeOptions<Config> {
input: DriverCreateInput<Config>;
driverKind: string;
apiKey: string;
apiUrl: string;
models: () => ModelCatalog;
prepareTurn?: (turn: SendTurnInput, signal: AbortSignal) => Promise<void> | undefined;
selectedReasoning?: (model: string, variant?: string) => boolean;
requestBody(model: string, messages: OpenAIChatMessage[], stream: boolean, variant?: string, threadId?: string, selectedModel?: string): Record<string, unknown>;
httpErrorLabel: string;
/** A refusal this provider words itself: the sentence the person sees instead of the HTTP line, or undefined. */
plainRefusal?: (status: number, body: string) => string | undefined;
missingKeyError: string;
unavailableReason: string;
timeoutMs: number;
customHeaders?: OpenAICompatCustomHeadersConfig;
/** Ask a server on this machine or network for its models this often while a reply is pending, and stop the reply
* when it misses LIVENESS_MISSES checks in a row: it crashed, froze or left the network. */
livenessEveryMs?: number;
nativeLog: NativeLog;
refreshModels?: () => Promise<void>;
generateModel?: () => string;
reasoning?: boolean;
/** Cerebras rejects `reasoning_content` and takes `reasoning`. */ reasoningReplayField?: "reasoning_content" | "reasoning";
/** Sent on reasoning-off by a provider with no `reasoning_effort: "none"`: its own switch, or `{}`. Unset sends "none". */ reasoningOffBody?: (model: string) => Record<string, unknown>;
contentText?: (content: unknown) => string;
billing?: "metered";
includeUsageInCompleted?: boolean;
noBodyError?: string;
retryScale?: number;
tools?: boolean;
computerUse?: boolean;
/** A text-only model keeps the computer and browser tools but never receives a screenshot or attachment. */
imageInput?: (model: string) => boolean; /** Bytes of video clips a turn may send as they are; unset sends frames. */ videoBytes?: number;
/** Some models list `variants`; the picked one reaches `requestBody`. */ modelVariants?: boolean;
/** False for a model its server says cannot call tools: its turns go without them instead of being refused. */ toolUse?: (model: string) => boolean;
/** Why the server cannot answer now (not running, nothing to run), asked before the key is. */
serverUnavailable?: () => Promise<Pick<ProviderSnapshot, "reason" | "ollama"> | null>;
/** The name to send for a model, when its server knows it by another. */
servedModel?: (model: string) => Promise<string>;
/** Smaller models announce a step and end the turn without calling a tool; such a reply gets a nudge, at most three
* per turn and each only after tool progress since the last. */
nudgeAnnouncedAction?: boolean;
/** A model on this machine or network: read a tool call it wrote as text, stop it repeating itself, and send it back
* to fix a failed call instead of ending the turn on it. */
localModel?: boolean;
}
const usageFrom = (usage: CompletionJson["usage"]): Usage | null => {
const cachedInput = usage?.prompt_tokens_details?.cached_tokens;
return usage
? { input: usage.prompt_tokens ?? 0, output: usage.completion_tokens ?? 0,
...(typeof cachedInput === "number" && Number.isFinite(cachedInput) && cachedInput >= 0 ? { cachedInput } : {}) }
: null;
};
const completionUsage = (json: CompletionJson) => json.usage ?? json.x_groq?.usage;
const asError = (value: unknown): Error =>
value instanceof Error ? value : new Error(String(value));
const MAX_TURN_TOOL_CALLS = 200;
const MAX_ANNOUNCEMENT_NUDGES = 3;
const LOCAL_CORRECTIVE_ROUNDS = 2;
export function createOpenAIChatRuntime<Config>(options: RuntimeOptions<Config>): ProviderInstance {
const { input } = options;
const listeners = new Set<RuntimeEventListener>();
const active = new Map<string, {
abort: AbortController;
turnId: string;
done: Promise<void>;
approval: ReturnType<typeof createChatToolApproval>;
/** Mid-turn input parked by adapter.steer, spliced in at the next round, never mid-tool-batch. */
asides: string[];
/** Set once the turn stops taking rounds: a later steer goes to the next turn instead. */
ending: boolean;
}>();
const reasoningReplayRejected = new Set<string>();
const routes = createModelRoutes(options.apiUrl);
/** Beside the messages, not on them, so encrypted reasoning never reaches a Chat Completions body, a log or a handoff. */
const responseOutputs = new WeakMap<OpenAIChatMessage, ResponseItem[]>();
const assistantReply = (model: string, completion: Completion, content: string | null, toolCalls?: ChatToolCall[]): OpenAIChatMessage => {
const reply: OpenAIChatMessage = { role: "assistant", content,
...(completion.protocolReasoning && !reasoningReplayRejected.has(model) ? { [options.reasoningReplayField ?? "reasoning_content"]: completion.protocolReasoning } : {}),
...(completion.protocolReasoningDetails.length ? { reasoning_details: completion.protocolReasoningDetails } : {}),
...(toolCalls ? { tool_calls: toolCalls } : {}) };
if (completion.responseOutput?.length) responseOutputs.set(reply, completion.responseOutput);
return reply;
};
const conversations = createChatReplay();
/** Per bot (or conversation): the MCP servers its last turn listed tools from, started before the next turn's first request. */
const listedServers = new Map<string, Set<string>>();
/** The window a local server refused an oversized request with (llama.cpp's n_ctx), for compaction to time folds by. */
const refusedWindows = new Map<string, number>();
/** Not cleared on `sessionReset`: with no cursor the harness sends it on nearly every turn. */
const sessionGrants = new Map<string, Set<string>>();
const emit = (event: RuntimeEvent) => {
for (const listener of Array.from(listeners)) listener(event);
};
const base = (threadId: string, turnId: string) => ({
eventId: newEventId(),
provider: options.driverKind,
threadId,
turnId,
createdAt: new Date().toISOString(),
});
const watchServer = (everyMs: number, stop: (reason: Error) => void) => {
let misses = 0;
const timer = setInterval(() => {
fetch(`${options.apiUrl}/models`, { headers: { authorization: `Bearer ${options.apiKey}` }, signal: AbortSignal.timeout(everyMs) })
.then((response) => { misses = 0; void response.body?.cancel(); })
.catch(() => {
if (++misses === LIVENESS_MISSES) stop(new RuntimeNoticeError(`The model server at ${new URL(options.apiUrl).host} stopped answering, so the reply was stopped.`, "chat.notice.modelServerStoppedAnswering", { host: new URL(options.apiUrl).host }));
});
}, everyMs);
timer.unref();
return timer;
};
const completeOn = async (
route: ModelRoute,
messages: OpenAIChatMessage[],
model: string,
stream: boolean,
signal?: AbortSignal,
onDelta?: (delta: string, kind: "assistant_text" | "reasoning_text") => void,
tools: ChatToolDefinition[] = [],
variant?: string, threadId?: string, { onUsage, maxOutputTokens, headerContext }: Pick<TextGenerationOptions, "onUsage" | "maxOutputTokens"> & { headerContext?: RuntimeHeaderContext } = {},
): Promise<Completion> => {
const timeoutController = new AbortController();
let idleTimer: NodeJS.Timeout | null = null;
const resetIdleTimer = () => {
if (idleTimer) clearTimeout(idleTimer);
idleTimer = setTimeout(() => {
timeoutController.abort(new DOMException("Streaming idle timeout elapsed", "AbortError"));
}, options.timeoutMs);
};
resetIdleTimer();
const liveness = options.livenessEveryMs ? watchServer(options.livenessEveryMs, (reason) => timeoutController.abort(reason)) : null;
try {
const activeSignal = signal
? AbortSignal.any([signal, timeoutController.signal])
: timeoutController.signal;
const served = options.servedModel ? await prepareChatModel(() => options.servedModel!(model), activeSignal) : model;
const requestBody = options.requestBody(served, messages, stream, variant, threadId, model);
const effort = selectedReasoningEffort(requestBody, route, variant, options.selectedReasoning?.(model, variant));
const payload = JSON.stringify(route === "responses"
? { ...responsesRequest({ model: served, messages, tools, replay: (message) => responseOutputs.get(message), summaries: tools.length > 0 && routes.summaries(model), effort }), max_output_tokens: maxOutputTokens }
: {
...requestBody,
...(route === "chat-reasoning-off" ? (options.reasoningOffBody?.(served) ?? { reasoning_effort: "none" }) : {}),
...(maxOutputTokens === undefined ? {} : { [isOpenAIAddress(options.apiUrl) ? "max_completion_tokens" : "max_tokens"]: maxOutputTokens }),
...(tools.length ? { tools } : {}),
});
const send = options.timeoutMs > FETCH_LIMIT_MS ? fetchPastLimit : fetch;
const customHeaders = options.customHeaders ? resolveCustomHeaders(options.customHeaders, headerContext ?? { threadId, providerInstanceId: input.instanceId }) : undefined;
const response = await send(`${options.apiUrl}/${route === "responses" ? "responses" : "chat/completions"}`, {
method: "POST",
...((Object.keys(customHeaders ?? {}).length || messages.some(message => Array.isArray(message.content) && message.content.some(part => part.type === "image_url"))) ? { redirect: "error" as const } : {}),
headers: { ...customHeaders, authorization: `Bearer ${options.apiKey}`, "content-type": "application/json" },
body: payload,
signal: activeSignal,
});
const failure = (status: number, body: string): Error => {
const plain = options.plainRefusal?.(status, body); if (plain) return new Error(plain);
const message = `${options.httpErrorLabel} HTTP ${status}${body ? `: ${httpErrorDetail(body)}` : ""}`;
const refused = tools.length ? routeRefusal(route, status, body) : null;
if (refused) return new ModelRouteRefused(refused, message);
if (rejectsKey(status, body)) noteKeyRejected(options.apiUrl, options.apiKey);
if (refusesRequestSize(status, body)) return new RequestTooLargeError(message, Buffer.byteLength(payload), shareThatFits(body), refusedWindow(body));
if (rejectsToolsParameter(status, body)) return new UnsupportedChatToolsError(message);
const refusal = refusedToolCall(status, body);
if (refusal) return new RejectedToolCallError(message, refusal);
if (messages.some((entry) => entry.reasoning_content !== undefined) && rejectsReasoningReplay(status, body)) {
return new UnsupportedReasoningReplayError(message);
}
return new ChatHttpError(status, message);
};
if (!response.ok) throw failure(response.status, await response.text().catch(() => ""));
noteKeyAccepted(options.apiUrl, options.apiKey);
if (route === "responses") {
const reply = await readResponsesStream(response.body, { reasoning: Boolean(options.reasoning), onBytes: resetIdleTimer, onDelta: (text, kind) => onDelta?.(text, kind), fail: failure });
activeSignal.throwIfAborted();
onUsage?.({ model, input: reply.usage?.input, output: reply.usage?.output, cachedInput: reply.usage?.cachedInput });
return reply;
}
if (!stream || response.headers.get("content-type")?.includes("application/json")) {
const json = await response.json() as CompletionJson;
const tokenUsage = completionUsage(json);
if (onUsage) {
activeSignal.throwIfAborted();
const count = (value: unknown) => typeof value === "number" && Number.isFinite(value) && value >= 0 ? value : undefined;
onUsage({
model: typeof json.model === "string" && json.model.trim() ? json.model : model,
input: count(tokenUsage?.prompt_tokens),
output: count(tokenUsage?.completion_tokens),
cachedInput: count(tokenUsage?.prompt_tokens_details?.cached_tokens),
costUsd: count(tokenUsage?.cost),
});
}
const bodyError = completionError(json, "completion error");
if (bodyError) throw bodyError;
const message = json.choices?.[0]?.message;
if (!message || !object(message) || !["content", "reasoning_content", "reasoning", "reasoning_details", "tool_calls", "function_call"].some((key) => key in message)) {
throw new ChatProtocolError("provider returned no completion message");
}
const calls = new ChatToolCalls();
calls.add(message?.tool_calls, false);
calls.addLegacy(message.function_call);
const details = new ChatReasoningDetails();
details.add(message.reasoning_details);
const reasoning = message.reasoning_content ?? message.reasoning;
const finishReason = json.choices?.[0]?.finish_reason ?? null;
activeSignal.throwIfAborted();
return {
text: options.contentText ? options.contentText(message?.content) : typeof message?.content === "string" ? message.content : "",
reasoning: options.reasoning && typeof reasoning === "string" ? reasoning : "",
usage: usageFrom(tokenUsage),
toolCalls: calls.finish(finishReason, false),
finishReason,
protocolReasoning: typeof reasoning === "string" ? reasoning : "",
protocolReasoningDetails: details.blocks,
};
}
if (!response.body) throw new Error(options.noBodyError ?? `${options.httpErrorLabel} returned no response body`);
let text = "";
let reasoning = "";
let protocolReasoning = "";
let usage: Usage | null = null;
let finishReason: string | null = null;
let malformedFrame = false;
let sawChoice = false;
const calls = new ChatToolCalls();
const details = new ChatReasoningDetails();
const reader = response.body.getReader();
const decoder = new TextDecoder();
let buffer = "";
const consumeDataLine = (line: string, atEof = false): boolean => {
const data = sseData(line, failure);
if (data === undefined) return false;
if (data === "[DONE]") return true;
let chunk: CompletionJson;
try {
chunk = JSON.parse(data) as CompletionJson;
} catch {
// A tail left when the socket closed is an incomplete frame (stop, abort, drop), not a malformed one.
if (!atEof) malformedFrame = true;
return false;
}
const chunkError = completionError(chunk, "streaming completion error");
if (chunkError) throw chunkError;
const choice = chunk.choices?.find((row) => row.index === undefined || row.index === 0);
const delta = choice?.delta;
if (object(delta)) sawChoice = true;
calls.add(delta?.tool_calls, true);
calls.addLegacy(delta?.function_call);
details.add(delta?.reasoning_details);
if (choice?.finish_reason) finishReason = choice.finish_reason;
const reasoningPart = delta?.reasoning_content ?? delta?.reasoning;
if (typeof reasoningPart === "string") protocolReasoning += reasoningPart;
const reasoningDelta = options.reasoning && typeof reasoningPart === "string" ? reasoningPart : "";
const contentDelta = options.contentText ? options.contentText(delta?.content) : typeof delta?.content === "string" ? delta.content : "";
if (reasoningDelta) {
reasoning += reasoningDelta;
onDelta?.(reasoningDelta, "reasoning_text");
}
if (contentDelta) {
text += contentDelta;
onDelta?.(contentDelta, "assistant_text");
}
const tokenUsage = completionUsage(chunk);
if (tokenUsage) usage = usageFrom(tokenUsage);
return false;
};
try {
readLoop: for (;;) {
const { done, value } = await reader.read();
if (done) {
buffer += decoder.decode();
const line = buffer.trim();
if (line && line !== "data: [DONE]") consumeDataLine(line, true);
// MiniMax's api.minimax.io/v1 closes the connection after the
// finish_reason chunk and never sends `[DONE]`.
if (buffer.trim() === "data: [DONE]" || finishReason) break;
if (!sawChoice) {
let body: CompletionJson | undefined;
try { body = JSON.parse(buffer) as CompletionJson; } catch { body = undefined; }
const bodyError = body ? completionError(body, "completion error") : null;
if (bodyError) throw bodyError;
}
throw new StreamTruncatedError("Stream ended before completion");
}
resetIdleTimer();
buffer += decoder.decode(value, { stream: true });
if (buffer.length > 2_000_000) throw new ChatProtocolError("provider stream frame exceeded the size limit");
let newline: number;
while ((newline = buffer.indexOf("\n")) !== -1) {
const line = buffer.slice(0, newline).trim();
buffer = buffer.slice(newline + 1);
if (consumeDataLine(line)) break readLoop;
}
}
} finally {
await reader.cancel().catch(() => {});
}
activeSignal.throwIfAborted();
if (!sawChoice) throw new ChatProtocolError("provider returned no streaming completion choice");
return { text, reasoning, usage, toolCalls: calls.finish(finishReason, malformedFrame), finishReason, protocolReasoning, protocolReasoningDetails: details.blocks };
} catch (error) {
if (timeoutController.signal.reason instanceof RuntimeNoticeError) throw timeoutController.signal.reason;
throw error;
} finally {
if (idleTimer) clearTimeout(idleTimer);
if (liveness) clearInterval(liveness);
}
};
const complete = async (
messages: OpenAIChatMessage[],
model: string,
stream: boolean,
signal?: AbortSignal,
onDelta?: (delta: string, kind: "assistant_text" | "reasoning_text") => void,
tools: ChatToolDefinition[] = [],
variant?: string, threadId?: string, extras: { headerContext?: RuntimeHeaderContext } = {},
): Promise<Completion> => {
for (;;) {
const attemptedRoute = routes.routeFor(model, tools.length > 0); let replied = false;
try {
return await completeOn(attemptedRoute, messages, model, stream, signal, onDelta && ((text, kind) => {
replied = true;
onDelta(text, kind);
}), tools, variant, threadId, extras);
} catch (error) {
if (!(error instanceof ModelRouteRefused) || replied || signal?.aborted) throw error;
if (routes.routeFor(model, tools.length > 0) === attemptedRoute && !routes.advance(model, error.refusal)) throw new ChatProtocolError(noRouteLeft(error.message));
console.warn(`${options.driverKind}: ${model} answered ${error.refusal}; now on ${routes.routeFor(model, true)}`);
}
}
};
const seesImages = (turn: SendTurnInput) =>
options.imageInput?.(turn.model || options.models().default) ?? true;
/** Thread ids may coincide across bots, so what one bot sent is never replayed to another. */
const conversationKey = (turn: SendTurnInput) => JSON.stringify([turn.threadId, turn.botId ?? null]);
const messagesFor = (turn: SendTurnInput, model: string): { messages: OpenAIChatMessage[]; record: DeliveredUserMessage } => {
const halves = promptHalves(turn);
const transcript = turn.transcript ?? [];
const replay = conversations.replay(conversationKey(turn), transcript, model);
const volatile = halves.volatile.trim();
const note = halves.stable === null || (!turn.mentionTurn && replay.volatile === volatile) ? ""
: volatileContextNote(volatile, Boolean(replay.volatile));
const textOnly = (text: string) => !options.computerUse || seesImages(turn) || !(turn.images?.length || turn.videos?.length) ? text
: `${text}\n\n${TEXT_ONLY_ATTACHMENT_NOTE}`;
const userTurn = note ? { ...turn, text: withContextNote(note, turn.text) } : turn;
const content = options.computerUse && seesImages(turn) ? chatUserContent(userTurn) : textOnly(userTurn.text);
const kept = turn.recalled ? turn.text.replace(`${turn.recalled}\n\n`, "") : turn.text;
const record = replay.record({ text: kept, sent: textOnly(userTurn.text), ...(note ? { volatile } : {}) });
const system = halves.stable ?? turn.system;
return { messages: [...(system ? [{ role: "system" as const, content: system }] : []), ...replay.history, { role: "user", content }], record };
};
const sendTurn = async (turn: SendTurnInput) => {
if (!options.apiKey) throw new Error(options.missingKeyError);
if (active.has(turn.threadId)) throw new Error("a turn is already running on this thread");
if (options.computerUse && (turn.integrations?.localComputer || turn.integrations?.browser)) assertImageTransport(options.apiUrl);
const turnId = newId();
const abort = new AbortController();
let messages: OpenAIChatMessage[] = [];
let request: OpenAIChatMessage | undefined;
let record: DeliveredUserMessage | undefined;
const replies: string[] = [];
const serversKey = turn.botId ?? turn.threadId;
const retainImages = chatImageBudget();
let model = turn.model || options.models().default;
const maskable = (value: unknown): value is string => typeof value === "string" && value.length >= 8; // a short placeholder key ("1") would redact every match
const headerContext: RuntimeHeaderContext = { threadId: turn.threadId, botId: turn.botId, turnId, providerInstanceId: input.instanceId };
const secrets = [...[options.apiKey].filter(maskable), ...customHeaderSecrets(options.customHeaders, headerContext)];
for (const integration of Object.values(turn.integrations ?? {})) {
const entries = object(integration);
const specs = entries && "command" in entries ? [entries] : Object.values(entries ?? {}).map(object);
for (const spec of specs) {
for (const [key, value] of Object.entries(object(spec?.env) ?? {})) {
if (/key|token|password|secret|authorization/i.test(key) && maskable(value)) secrets.push(value);
}
}
}
const safeText = (text: string) => redactSecretsInText(maskCustomSecrets(text, secrets));
const maskSecrets = (text: string, flush: boolean) => maskSecretHalves(text, secrets, safeText, flush);
const preview = (value: unknown) => toolDetailPreview(JSON.parse(JSON.stringify(value, (_key, part) =>
typeof part === "string" ? safeText(part) : part)));
const native = (dir: "out" | "in", msg: unknown) => appendNative(turn.threadId, {
dir, source: options.nativeLog.source,
msg: JSON.parse(JSON.stringify(msg, (_key, part) => typeof part === "string" ? safeText(part) : part)),
});
const grantsKey = conversationKey(turn);
const granted = sessionGrants.get(grantsKey) ?? new Set<string>();
keepRecent(sessionGrants, grantsKey, granted);
const approval = createChatToolApproval({
signal: abort.signal,
granted: shellOff(turn) ? undefined : granted,
open: (ask) => emit({
...base(turn.threadId, turnId), type: "request.opened", requestType: "permission",
requestId: ask.id, tool: ask.tool, summary: ask.summary, allowSession: ask.grant !== undefined,
}),
resolved: (ask, allowed, source) => emit({
...base(turn.threadId, turnId), type: "request.resolved", requestId: ask.id,
behavior: allowed ? "allow" : "deny", source,
}),
openQuestion: (ask, questions) => {
const choices = questionChoices(questions);
emit({
...base(turn.threadId, turnId), type: "request.opened", requestType: "question",
requestId: ask.id, tool: ask.tool, summary: ask.summary,
questions, ...(choices ? { choices } : {}),
});
},
resolvedQuestion: (ask, answered, source) => emit({
...base(turn.threadId, turnId), type: "request.resolved", requestId: ask.id,
behavior: answered ? "answer" : "deny", source,
}),
});
let resolveDone!: () => void;
const done = new Promise<void>((resolve) => { resolveDone = resolve; });
const turnEntry = { abort, turnId, done, approval, asides: [] as string[], ending: false };
active.set(turn.threadId, turnEntry);
emit({ ...base(turn.threadId, turnId), type: "turn.started" });
void (async () => {
let tools: ChatToolSession | undefined;
const usage: Usage = { input: 0, output: 0 };
let hasUsage = false;
let ok = false;
let stopReason: string | null = null;
let failure: string | undefined;
let failureText: ReturnType<typeof runtimeErrorText>;
let toolFailed = false, spoke = false, unfloored = 0, blockedAfterReply = false; // unfloored: failed calls the safety floor did not block after the bot spoke
let correctiveRounds = 0;
const cutReplies = { count: 0 };
const answer = new ContinuedReply((text) => { replies.push(text); emit({ ...base(turn.threadId, turnId), type: "item.completed", itemType: "assistant_text", text }); });
let carried = "", carriedReasoning = "";
const denials: string[] = [], failedCalls: string[] = [];
const seenCalls = new Set<string>();
let nudges = 0;
let unread: string[] = [];
const deliveredAsides: string[] = [];
let callsAtNudge = -1;
const repeats = options.localModel ? new ChatRepeatGuard() : undefined;
let unreadable = false, askedAgain = false;
try {
const preparation = options.prepareTurn?.(turn, abort.signal);
if (preparation) { await preparation; model = turn.model || options.models().default; }
abort.signal.throwIfAborted();
if (options.computerUse && turn.images?.length && seesImages(turn)) assertImageTransport(options.apiUrl);
({ messages, record } = messagesFor(turn, model));
request = messages.at(-1)!;
for (const message of messages) retainImages(message.content);
emit({ ...base(turn.threadId, turnId), type: "session.started", sessionId: null, model });
const parsed = parseToolScope(turn.toolScope);
if (!parsed.ok) throw new Error(parsed.error);
const scope = parsed.scope;
const toolsOff = options.tools === false || options.toolUse?.(model) === false;
// The tool list leads the prompt: a server joining mid-turn makes a local hybrid model (Qwen3.6) read all of it again,
// minutes per step, so a local model waits for every server and others wait for the ones this bot used last turn.
tools = await mountChatTools(toolsOff ? undefined : withHostFileTools(turn.integrations), abort.signal, options.computerUse, scope, turn.mcpCallTimeoutMs,
toolSchemaProfileFor({ url: options.apiUrl, model }), turn.cwd ?? turn.toolResultFolder, safeText, chatToolLimitFor(options.apiUrl, model), { background: !options.localModel, waitFor: listedServers.get(serversKey), conversation: turn.threadId, notice: ({ message, text }) => emit({ ...base(turn.threadId, turnId), type: "runtime.notice", message: safeText(message), text }) });
noteToolBudget(turn.botId, tools.budget);
for (const message of chatToolNotices(tools)) emit({ ...base(turn.threadId, turnId), type: "runtime.notice", message: safeText(message) });
const questionAllowed = !toolsOff && allowsTool(scope, { kind: "native", name: ASK_USER_TOOL });
// An MCP server squatting ask_user cannot shadow it (dispatch intercepts the name); skip its definition.
if (questionAllowed && !tools.definitions.some((definition) => definition.function.name === ASK_USER_TOOL)) {
tools.definitions.push(ASK_USER_TOOL_DEFINITION);
}
for (let round = 0; ; round++) {
abort.signal.throwIfAborted();
// Between rounds, never inside a tool batch.
unread = turnEntry.asides.splice(0);
if (unread.length) {
messages.push({ role: "user", content: unread.join("\n\n") });
deliveredAsides.push(...unread);
}
// Servers still starting join between rounds, never once the tools were dropped (none left while ask_user is allowed).
const joined = !questionAllowed || tools.definitions.length ? await joinLateChatTools(tools, [ASK_USER_TOOL_DEFINITION]) : undefined;
for (const message of joined ?? []) emit({ ...base(turn.threadId, turnId), type: "runtime.notice", message: safeText(message) });
native("out", { ...object(options.nativeLog.outgoing(turn, messages, model, tools.definitions)), ...conversations.repeated(conversationKey(turn), messages, tools.definitions),
...(process.env.OPENMAUS_LOG_CHAT_REQUESTS === "1" ? { messages, tools: tools.definitions } : {}) });
let attempt = 0;
let resends = 0;
let completion: Completion, prefix = "", willContinue = false;
for (;;) {
let streamed = false;
let answerStreamed = false;
const holdback = options.localModel && tools.definitions.length ? new TextCallHoldback() : undefined;
prefix = carried;
const deltas = new MaskedDeltas(secrets, safeText, (delta, streamKind) => emit({ ...base(turn.threadId, turnId), type: "content.delta", streamKind, delta }));
deltas.seed("assistant_text", prefix); deltas.seed("reasoning_text", carriedReasoning);
const delta = deltas.feed.bind(deltas);
try {
completion = await complete(messages, model, true, abort.signal, (text, streamKind) => {
streamed = true;
if (streamKind === "assistant_text") answerStreamed = true;
const shown = holdback && streamKind === "assistant_text" ? holdback.feed(text) : text;
if (shown) delta(shown, streamKind);
}, tools.definitions, turn.variant, turn.threadId, { headerContext });
// A reply cut off at the token limit may be a call cut short; running half of one is worse than saying so.
const written = holdback && !completion.toolCalls.length && completion.finishReason !== "length" ? textToolCalls(completion.text, tools.definitions) : null;
unreadable = written?.kind === "unreadable" && !askedAgain;
if (written?.kind === "calls") {
completion = { ...completion, text: written.text, toolCalls: written.calls, finishReason: "tool_calls" };
native("in", { recovered: "text_tool_calls", calls: written.calls.map((call) => call.function.name) });
} else if (unreadable) {
completion = { ...completion, text: written!.text };
native("in", { recovered: "unreadable_tool_call" });
} else {
const rest = holdback?.rest();
if (rest) delta(rest, "assistant_text");
}
willContinue = !completion.toolCalls.length && cutReplyWillContinue(cutReplies, completion, Boolean(options.localModel && tools.definitions.length));
// Only a cut reply's continuation joins this one in a single message, so only there does a held credential prefix carry over.
delta("", "assistant_text", !willContinue); delta("", "reasoning_text", !willContinue);
carried = willContinue ? maskSecrets(prefix + completion.text, false).held : ""; carriedReasoning = willContinue ? maskSecrets(carriedReasoning + completion.reasoning, false).held : "";
break;
} catch (value) {
const error = asError(value);
const verdict = classifyError(error, { openAi: isOpenAIAddress(options.apiUrl) });
// Refused for too many tools, nothing ran: learn the limit and list fewer (after a call only when servers just joined).
const listedBefore = tools.definitions.length, refit = streamed || (seenCalls.size && !joined) || abort.signal.aborted ? undefined : await refitChatTools(tools, error, chatToolLimitKey(options.apiUrl, model), [ASK_USER_TOOL_DEFINITION]);
if (refit && tools.definitions.length < listedBefore) {
for (const message of refit) emit({ ...base(turn.threadId, turnId), type: "runtime.notice", message: safeText(message) });
noteToolBudget(turn.botId, tools.budget);
emit({ ...base(turn.threadId, turnId), type: "turn.retrying", attempt: 1, delayMs: 0, reason: "tool_limit" });
continue;
}
// A rejected reasoning echo executed nothing upstream, so the stripped resend repeats nothing.
if (error instanceof UnsupportedReasoningReplayError && !streamed && !abort.signal.aborted) {
reasoningReplayRejected.add(model);
for (const message of messages) delete message.reasoning_content;
continue;
}
// A provider refuses an oversized request before running anything, so a smaller copy may go again.
if (error instanceof RequestTooLargeError && !abort.signal.aborted) {
if (error.window) refusedWindows.set(model, error.window);
const resend = resends < resendsAllowed(error) ? smallerResend(messages, request, error.bytes, error.fits) : null;
native("out", { refused: "request_too_large", bytes: error.bytes, ...(resend ? { before: resend.before, after: resend.after } : {}) });
console.warn(`${options.driverKind}: ${model} refused a ${error.bytes}-byte request as too large; ${resend ? `resending smaller (${sizeChange(resend.before, resend.after)})` : "not resending"}`);
if (!resend) throw new ChatProtocolError(requestTooLargeMessage(options.models().options.find((option) => option.id === model)?.label ?? model));
messages.splice(0, messages.length, ...resend.messages);
resends += 1;
emit({ ...base(turn.threadId, turnId), type: "turn.retrying", attempt: resends, delayMs: 0, reason: "request_too_large" });
continue;
}
// A refused tool call ran nothing, so the request goes again, but not once answer text streamed.
if (error instanceof RejectedToolCallError) {
native("in", { refused: "tool_use_failed", attempt: attempt + 1, error: error.refusal });
if (!answerStreamed && !abort.signal.aborted && attempt < RETRY_MAX_ATTEMPTS - 1) {
emit({ ...base(turn.threadId, turnId), type: "turn.retrying", attempt: ++attempt, delayMs: 0, reason: "tool_use_failed" });
continue;
}
if (!abort.signal.aborted) throw new ChatProtocolError(refusedToolCallFailure(error, seenCalls.size > 0));
}
// A structured parameter rejection executed nothing; never downgrade mounted tools or a handled call.
if (questionAllowed && round === 0 && tools.definitions.length && tools.definitions.every((definition) => definition === ASK_USER_TOOL_DEFINITION) && !streamed && !seenCalls.size && !abort.signal.aborted &&
error instanceof UnsupportedChatToolsError && verdict.reason === "invalid_request") {
tools.definitions.length = 0;
continue;
}
// Never resend once this call streamed (the person already saw or heard it). A stream cut ran nothing upstream, so it resends after earlier tool rounds too (their results are in the request); without a caller opt-in only the cut itself retries.
const truncation = error instanceof StreamTruncatedError, retryScale = truncation ? options.retryScale ?? 1 : options.retryScale;
if (retryScale === undefined || abort.signal.aborted || streamed || error instanceof RuntimeNoticeError || error instanceof CustomHeaderError || (!truncation && ((error instanceof ChatProtocolError && verdict.reason !== "stream_cut") || !verdict.transient)) || (verdict.reason !== "stream_cut" && seenCalls.size) || attempt >= RETRY_MAX_ATTEMPTS - 1) throw error;
const delayMs = computeBackoff(attempt++);
emit({ ...base(turn.threadId, turnId), type: "turn.retrying", attempt, delayMs, reason: truncation ? "stream_truncated" : verdict.reason });
await interruptibleDelay(delayMs * retryScale, abort.signal).promise;
abort.signal.throwIfAborted();
}
}
native("in", options.nativeLog.incoming(completion));
unread = [];
if (completion.usage) {
usage.input += completion.usage.input;
usage.output += completion.usage.output;
if (completion.usage.cachedInput !== undefined) usage.cachedInput = (usage.cachedInput ?? 0) + completion.usage.cachedInput;
hasUsage = true;
// A request bigger than the refused window went through: the server was given more context since.
if ((refusedWindows.get(model) ?? Infinity) < completion.usage.input + completion.usage.output) refusedWindows.delete(model);
const contextWindow = refusedWindows.get(model) ?? options.models().options.find((entry) => entry.id === model)?.contextWindow;
// Accounting sums the turn; context occupancy is the latest round only.
emit({ ...base(turn.threadId, turnId), type: "thread.token-usage.updated", ...usage,
contextTokens: completion.usage.input + completion.usage.output, ...(contextWindow ? { contextWindow } : {}) });
}
// Reasoning on a tool-call round belongs to the protocol, not a final reply.
const reply = completion.text.trim() ? completion.text : completion.toolCalls.length || unreadable ? "" : completion.reasoning;
const shownReply = prefix || willContinue || completion.text.trim() ? maskSecrets(prefix + completion.text, !willContinue).visible : safeText(reply);
if (reply.trim() || prefix) { spoke = true; answer.add(shownReply); }
if (!willContinue) answer.end();
abort.signal.throwIfAborted();
if (unreadable) {
askedAgain = true;
messages.push(assistantReply(model, completion, completion.text), { role: "user", content: UNREADABLE_CALL });
continue;
}
if (!completion.toolCalls.length) {
if (!reply.trim()) throw new ChatProtocolError("provider returned an empty response");
if (cutReplyContinues(cutReplies, completion, Boolean(options.localModel && tools.definitions.length))) {
messages.push(assistantReply(model, completion, completion.text), { role: "user", content: CUT_REPLY_NOTE });
continue;
}
if (completion.finishReason && completion.finishReason !== "stop") {
throw new ChatProtocolError(`provider did not finish the response (${completion.finishReason})`);
}
if (toolFailed) {
// A person's "no" is never answered with another attempt; a failure may first get the configured corrective rounds.
if (!denials.length && correctiveRounds < Math.max(turn.toolErrorCorrectiveRounds ?? 0, options.localModel ? LOCAL_CORRECTIVE_ROUNDS : 0)) {
correctiveRounds += 1;
messages.push({ role: "user", content: CORRECTIVE_ROUND });
continue;
}
stopReason = "tool_error"; blockedAfterReply = failedCalls.length > 0 && unfloored === 0;
throw new ChatProtocolError(toolFailureMessage(failedCalls, denials.filter((name) => name !== ASK_USER_TOOL)));
}
if (options.nudgeAnnouncedAction && tools.definitions.length && announcesAction(completion.text)) {
if (nudges >= MAX_ANNOUNCEMENT_NUDGES) {
stopReason = "incomplete_action";
throw new ChatProtocolError(`Stopped after ${MAX_ANNOUNCEMENT_NUDGES} announcement nudges without a verified final answer. Earlier operations may have taken effect; inspect their state and continue only the remaining work.`);
}
if (callsAtNudge === seenCalls.size) {
stopReason = "incomplete_action";
throw new ChatProtocolError("The model announced another action without tool progress after a nudge. The task is incomplete; earlier operations may have taken effect. Inspect their state before continuing.");
}
nudges += 1;
callsAtNudge = seenCalls.size;
messages.push(assistantReply(model, completion, completion.text), { role: "user", content: NUDGE_ANNOUNCED_ACTION });
continue;
}
if (turnEntry.asides.length) {
messages.push(assistantReply(model, completion, reply));
continue;
}
messages.push(assistantReply(model, completion, reply));
ok = true;
break;
}
if (!tools.definitions.length) throw new ChatProtocolError("provider returned tool calls, but no tools are available for this turn");
// Validate IDs for the entire batch before executing any of its calls.
for (const call of completion.toolCalls) {
if (seenCalls.has(call.id)) throw new ChatProtocolError("provider reused a tool-call ID; refusing to repeat an operation");
seenCalls.add(call.id);
}
if (seenCalls.size > MAX_TURN_TOOL_CALLS) throw new ChatProtocolError(`Stopped after ${MAX_TURN_TOOL_CALLS} tool calls without a final answer. The steps so far already ran, so ask only for what's left.`);
messages.push(assistantReply(model, completion, completion.text || null, replayableToolCalls(completion.toolCalls)));
// A failure stays pending only until the bot runs tools again: one that went on to use more tools has read the error
// and reacted (a wrong guess, a bad query, then the right call), so its turn is not failed for it, on any model. The
// failed step still shows as failed; a failure in the last calls before the answer, or a person's no, still ends it.
if (!denials.length) { toolFailed = false; failedCalls.length = 0; unfloored = 0; }
const screenshotParts: ChatContentPart[] = [];
for (const call of completion.toolCalls) {
abort.signal.throwIfAborted();
let result: ChatToolResult;
let started = false;
let fatal: Error | undefined, title = call.function.name, skipRepeatCount = false, flooredAfterReply = false;
try {
const args = toolArguments(call.function.arguments, safeText);
const inputPreview = preview(args);
if (call.function.name === ASK_USER_TOOL) {
if (!questionAllowed || !allowsTool(scope, { kind: "native", name: ASK_USER_TOOL })) throw new Error("Tool selection excludes this tool");
// A question goes before the gate: Full would auto-run an unanswered ask, Ask would show Allow/Deny.
const questions = parseAskQuestions(args);
abort.signal.throwIfAborted();
emit({ ...base(turn.threadId, turnId), type: "item.started", itemType: "tool", itemId: call.id,
title: call.function.name, ...(inputPreview ? { input: inputPreview } : {}),
});
started = true;
if (!questions) {
denials.push(ASK_USER_TOOL);
result = { ok: false, text: "The ask_user arguments are malformed: pass a JSON object with a questions array of one to six questions, each with a question string and at most twelve options carrying a label. Ask again with valid arguments." };
} else {
const safeQuestions = redactAskQuestions(questions, safeText);
const answer = await approval.question(ASK_USER_TOOL, askQuestionSummary(safeQuestions), safeQuestions);
abort.signal.throwIfAborted();
if (answer === null) {
result = { ok: true, text: "The person did not answer this question. Do not guess an answer; ask again later or proceed without it." };
} else {
result = { ok: true, text: answer };
}
}
} else {
const refused = repeats?.refuses(call.function.name, call.function.arguments);
if (refused) { skipRepeatCount = true; throw new Error(refused); }
tools.validate(call.function.name, args);
const shown = tools.view(call.function.name, args); title = shown.title;
const shownPreview = shown.input === args ? inputPreview : preview(shown.input);
// This runtime has no provider reviewer, so Full access is honoured here.
const full = (turn.approvalModeNow?.() ?? turn.approvalMode) === "full";
const floored = approval.floored({ full, threadId: turn.threadId, botId: turn.botId, shown, summary: shownPreview });
const allowed = !floored && (full || !shown.ask
|| await approval.ask(shown.permissionTool ?? shown.title, shownPreview ?? "This tool has no arguments.", shown.grant));
abort.signal.throwIfAborted();
emit({ ...base(turn.threadId, turnId), type: "item.started", itemType: "tool", itemId: call.id,
title: shown.title, ...(shownPreview ? { input: shownPreview } : {}),
});
started = true;
if (allowed) {
result = await tools.execute(call.function.name, args, abort.signal);
if (result.images?.length && !seesImages(turn)) {
const { images: _dropped, ...rest } = result;
result = { ...rest, text: `${rest.text}\n${TEXT_ONLY_SCREENSHOT_NOTE}` };
}
if (result.images?.length) {
try {
assertImageTransport(options.apiUrl);
retainImages(result.images);
} catch (error) {
throw new ChatToolSessionError(asError(error).message);
}
}
} else {
denials.push(shown.title); flooredAfterReply = Boolean(floored) && spoke;
result = { ok: false, text: refusal(floored, (message) => emit({ ...base(turn.threadId, turnId), type: "runtime.notice", message })) };
}
}
} catch (error) {
if (error instanceof ChatToolSessionError) fatal = error;
result = { ok: false, text: abort.signal.aborted
? "Tool interrupted; an operation already dispatched may have taken effect. Verify its state before retrying."
: safeText(asError(error).message).slice(0, 2_000) };
}
if (!started) emit({ ...base(turn.threadId, turnId), type: "item.started", itemType: "tool", itemId: call.id, title: call.function.name });
const repeatNote = skipRepeatCount ? undefined : repeats?.noteAfter(call.function.name, call.function.arguments, result);
const text = safeText(result.text);
const output = preview({ ok: result.ok, result: text });
emit({ ...base(turn.threadId, turnId), type: "item.completed", itemType: "tool", itemId: call.id, ok: result.ok, output });
if (!result.ok) { toolFailed = true; failedCalls.push(title); if (!flooredAfterReply) unfloored += 1; }
messages.push({ role: "tool", tool_call_id: call.id, content: JSON.stringify({ ok: result.ok, result: repeatNote ? `${text}\n\n${repeatNote}` : text }) });
screenshotParts.push(...chatToolImages(call.id, result.images));
abort.signal.throwIfAborted();
if (fatal) throw fatal;
if (repeats?.stopped) { stopReason = "incomplete_action"; throw new ChatProtocolError(repeats.stopped); }
}
if (screenshotParts.length) messages.push({ role: "user", content: screenshotParts });
}
} catch (value) {
stopReason = abort.signal.aborted ? "interrupted" : stopReason ?? "error";
failure = safeText(asError(value).message).slice(0, 2_000);
failureText = runtimeErrorText(value);
} finally {
turnEntry.ending = true;
approval.close();
answer.end();
if (tools) keepRecent(listedServers, serversKey, new Set(tools.listedServers()));
let cleanupFailed = false;
try { await tools?.close(); }
catch {
ok = false;
stopReason = "error";
cleanupFailed = true;
failure = "Tool processes could not be stopped; their execution state is uncertain.";
}
if (abort.signal.aborted) { ok = false; stopReason = "interrupted"; }
const asked = request ? messages.indexOf(request) : -1;
if (ok && record && asked >= 0) conversations.keepTurn(record, model, messages.slice(asked + 1), replies);
if (!ok && (unread.length || turnEntry.asides.length)) emit({ ...base(turn.threadId, turnId), type: "runtime.notice", ...noticeFailure("chat.notice.unreadMessage") });
if (failure && (!abort.signal.aborted || cleanupFailed)) {
// "upstream" (an OpenAI-compatible URL) names no provider, and the Cloud trial credit reads its raw line.
const words = options.httpErrorLabel === "upstream" ? { message: failure } : plainFailure(options.httpErrorLabel, failure);
const replyShownFirst = stopReason === "tool_error";
emit({ ...base(turn.threadId, turnId), type: "runtime.error", ...words, ...(!cleanupFailed && failureText ? { text: failureText } : {}), terminal: !abort.signal.aborted, ...(replyShownFirst ? { partial: true, ...(blockedAfterReply ? { blockedAfterReply: true } : {}) } : {}) });
}
if (!ok && !abort.signal.aborted) {
const continuable = classifyContinuable(stopReason, failure);
if (continuable) {
const handoffPath = writeTurnHandoff({
threadId: turn.threadId, turnId, model,
usage, hasUsage, failure, toolCalls: seenCalls.size,
task: turn.text, updates: deliveredAsides,
messages: request ? messages.slice(messages.indexOf(request) + 1).map((msg) => JSON.parse(JSON.stringify(msg, (_key, part) => typeof part === "string" ? safeText(part) : part))) : [],
});
if (handoffPath) emit({ ...base(turn.threadId, turnId), type: "cap.exhausted", handoffPath, reason: continuable });
}
}
active.delete(turn.threadId);
emit({ ...base(turn.threadId, turnId), type: "turn.completed", ok, stopReason, cost: null,
...(hasUsage && (options.includeUsageInCompleted || seenCalls.size) ? { usage } : {}),
...(denials.length ? { denials } : {}),
});
resolveDone();
}
})();
return { turnId };
};
return {
instanceId: input.instanceId,
driverKind: options.driverKind,
displayName: input.displayName,
enabled: input.enabled,
get models() {
return options.models();
},
...(options.refreshModels ? { refreshModels: options.refreshModels } : {}),
snapshot: async () => {
const down = await options.serverUnavailable?.();
if (down) return { state: "unavailable", ...down };
if (!options.apiKey) return { state: "unavailable", reason: options.unavailableReason };
if (keyRejected(options.apiUrl, options.apiKey)) return { state: "available", authenticated: false, reason: KEY_REJECTED_REASON, version: null };
return { state: "available", authenticated: true, version: null, ...(options.billing ? { billing: options.billing } : {}) };
},
adapter: {
provider: options.driverKind,
capabilities: { ...(options.computerUse ? { computerMcp: options.tools !== false, localComputerMcp: options.tools !== false,
browserMcp: options.tools !== false, nativeImageInput: true, images: true, ...(options.videoBytes ? { nativeVideoBytes: options.videoBytes } : {}) } : {}),
...(options.modelVariants ? { modelVariants: true } : {}), sessionModelSwitch: "in-session", customMcp: options.tools !== false, agentsMcp: options.tools !== false, composioMcp: options.tools !== false,
dataMcp: options.tools !== false,
emailMcp: options.tools !== false,
deviceMcp: options.tools !== false,
queueing: true,
...(options.tools !== false ? { askTool: { kind: "native" as const, name: ASK_USER_TOOL } } : {}),
guestTurns: "confined" },
sendTurn,
interruptTurn: async (threadId, turnId) => {
const turn = active.get(threadId);
if (!turn || (turnId && turn.turnId !== turnId)) return;
turn.abort.abort();
await turn.done;
},
respondToRequest: async (threadId, requestId, decision) =>
active.get(threadId)?.approval.answer(requestId, decision.behavior, decision.message, decision.always) ?? "unavailable",
steer: async (threadId, text) => {
const running = active.get(threadId);
if (!running || running.ending) return "refused";
running.asides.push(text);
return "steered";
},
hasSession: (threadId) => active.has(threadId),
stopAll: async () => {
const turns = [...active.values()];
for (const turn of turns) turn.abort.abort();
await Promise.all(turns.map((turn) => turn.done));
},
onEvent: (listener) => {
listeners.add(listener);
return () => listeners.delete(listener);
},
},
generateText: async (prompt, { signal, onUsage, model: named, checkModel, maxOutputTokens, finalTextOnly, threadId, fallback } = {}) => {
const model = named ?? options.generateModel?.() ?? fallback?.model ?? options.models().default;
signal?.throwIfAborted();
checkModel?.(model);
const headerContext: RuntimeHeaderContext = { ...(threadId ? { threadId } : {}), providerInstanceId: input.instanceId };
const headerSecrets = customHeaderSecrets(options.customHeaders, headerContext);
try {
const route = finalTextOnly && routes.routeFor(model, true) === "responses" ? "responses" : "chat";
const { text, reasoning, toolCalls, responseOutput } = await completeOn(route,
[{ role: "user", content: prompt }], model, false, signal, undefined, [], undefined, threadId, { onUsage, maxOutputTokens, headerContext });
if (toolCalls.length) throw new ChatProtocolError("provider returned tool calls to a text-only helper");
const answer = finalTextOnly && responseOutput ? finalResponseText(responseOutput) : finalTextOnly || text.trim() ? text : reasoning;
return maskCustomSecrets(answer, headerSecrets);
} catch (error) {
throw scrubErrorSecrets(error, headerSecrets);
}
},
dispose: async () => {
const turns = [...active.values()];
for (const turn of turns) turn.abort.abort();
await Promise.all(turns.map((turn) => turn.done));
listeners.clear();
},
};
}