@kenkaiiii/gg-agent 4.3.183 → 4.3.185

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.cjs CHANGED
@@ -34,6 +34,7 @@ module.exports = __toCommonJS(index_exports);
34
34
  var import_gg_ai2 = require("@kenkaiiii/gg-ai");
35
35
 
36
36
  // src/agent-loop.ts
37
+ var import_zod = require("zod");
37
38
  var import_gg_ai = require("@kenkaiiii/gg-ai");
38
39
  var DEFAULT_MAX_TURNS = 200;
39
40
  var _diagFn = null;
@@ -694,7 +695,12 @@ async function* agentLoop(messages, options) {
694
695
  details = normalized.details;
695
696
  } catch (err) {
696
697
  isError = true;
697
- resultContent = err instanceof Error ? err.message : String(err);
698
+ if (err instanceof import_zod.ZodError) {
699
+ resultContent = `Invalid arguments for tool \`${toolCall.name}\`:
700
+ ` + (0, import_zod.prettifyError)(err) + "\nRe-issue the call with each field as the correct type.";
701
+ } else {
702
+ resultContent = err instanceof Error ? err.message : String(err);
703
+ }
698
704
  }
699
705
  }
700
706
  const durationMs = Date.now() - startTime;
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/index.ts","../src/agent.ts","../src/agent-loop.ts"],"sourcesContent":["// Core\nexport { Agent, AgentStream } from \"./agent.js\";\nexport {\n agentLoop,\n isAbortError,\n isContextOverflow,\n isBillingError,\n setStreamDiagnostic,\n} from \"./agent-loop.js\";\nexport type { StreamDiagnosticFn } from \"./agent-loop.js\";\n\n// Types\nexport type {\n StructuredToolResult,\n ToolExecuteResult,\n ToolContext,\n AgentTool,\n AgentTextDeltaEvent,\n AgentThinkingDeltaEvent,\n AgentToolCallStartEvent,\n AgentToolCallUpdateEvent,\n AgentToolCallEndEvent,\n AgentToolCallDeltaEvent,\n AgentServerToolCallEvent,\n AgentServerToolResultEvent,\n AgentSteeringMessageEvent,\n AgentFollowUpMessageEvent,\n AgentRetryEvent,\n AgentTurnEndEvent,\n AgentDoneEvent,\n AgentErrorEvent,\n AgentEvent,\n AgentOptions,\n AgentResult,\n} from \"./types.js\";\n","import { EventStream, type Message } from \"@kenkaiiii/gg-ai\";\nimport { agentLoop } from \"./agent-loop.js\";\nimport type { AgentEvent, AgentOptions, AgentResult } from \"./types.js\";\n\n// ── AgentStream ─────────────────────────────────────────────\n\n/**\n * Dual-nature result: async iterable for streaming events,\n * thenable for awaiting the final AgentResult.\n *\n * ```ts\n * // Stream events\n * for await (const event of agent.prompt(\"hello\")) { ... }\n *\n * // Or just await the result\n * const result = await agent.prompt(\"hello\");\n * ```\n */\nexport class AgentStream implements AsyncIterable<AgentEvent> {\n private events: EventStream<AgentEvent>;\n private resultPromise: Promise<AgentResult>;\n private resolveResult!: (r: AgentResult) => void;\n private rejectResult!: (e: Error) => void;\n private hasConsumer = false;\n\n constructor(generator: AsyncGenerator<AgentEvent, AgentResult>, onDone: () => void) {\n this.events = new EventStream<AgentEvent>();\n this.resultPromise = new Promise<AgentResult>((resolve, reject) => {\n this.resolveResult = resolve;\n this.rejectResult = reject;\n });\n this.pump(generator, onDone);\n }\n\n private async pump(\n generator: AsyncGenerator<AgentEvent, AgentResult>,\n onDone: () => void,\n ): Promise<void> {\n try {\n let next = await generator.next();\n while (!next.done) {\n this.events.push(next.value);\n next = await generator.next();\n }\n this.events.close();\n this.resolveResult(next.value);\n } catch (err) {\n const error = err instanceof Error ? err : new Error(String(err));\n this.events.abort(error);\n this.rejectResult(error);\n } finally {\n onDone();\n }\n }\n\n [Symbol.asyncIterator](): AsyncIterator<AgentEvent> {\n this.hasConsumer = true;\n return this.events[Symbol.asyncIterator]();\n }\n\n then<TResult1 = AgentResult, TResult2 = never>(\n onfulfilled?: ((value: AgentResult) => TResult1 | PromiseLike<TResult1>) | null,\n onrejected?: ((reason: unknown) => TResult2 | PromiseLike<TResult2>) | null,\n ): Promise<TResult1 | TResult2> {\n this.drainEvents().catch(() => {});\n return this.resultPromise.then(onfulfilled, onrejected);\n }\n\n private async drainEvents(): Promise<void> {\n if (this.hasConsumer) return;\n this.hasConsumer = true;\n for await (const _ of this.events) {\n // consume silently\n }\n }\n}\n\n// ── Agent ───────────────────────────────────────────────────\n\nexport class Agent {\n private messages: Message[] = [];\n private _running = false;\n private options: AgentOptions;\n private steeringQueue: Message[] = [];\n private followUpQueue: Message[] = [];\n\n constructor(options: AgentOptions) {\n this.options = options;\n if (options.system) {\n this.messages.push({ role: \"system\", content: options.system });\n }\n if (options.priorMessages && options.priorMessages.length > 0) {\n this.messages.push(...options.priorMessages);\n }\n }\n\n /** Snapshot of the current message history. Used for session persistence. */\n getMessages(): Message[] {\n return [...this.messages];\n }\n\n /**\n * Swap the abort signal used for subsequent prompts. Call this after\n * `controller.abort()` so the next prompt() call gets a fresh, unaborted\n * signal — without losing message history.\n */\n setSignal(signal: AbortSignal | undefined): void {\n this.options = { ...this.options, signal };\n }\n\n get running(): boolean {\n return this._running;\n }\n\n /** Queue a steering message for injection after current tool execution completes. */\n steer(msg: Message): void {\n this.steeringQueue.push(msg);\n }\n\n /** Queue a follow-up message for injection when the agent would otherwise stop. */\n followUp(msg: Message): void {\n this.followUpQueue.push(msg);\n }\n\n prompt(content: string): AgentStream {\n if (this._running) {\n throw new Error(\"Agent is already running\");\n }\n this._running = true;\n\n this.messages.push({ role: \"user\", content });\n\n const optionsWithQueues: AgentOptions = {\n ...this.options,\n getSteeringMessages: async () => {\n const callerResult = (await this.options.getSteeringMessages?.()) ?? [];\n const queued = this.steeringQueue.splice(0);\n const all = [...(callerResult ?? []), ...queued];\n return all.length > 0 ? all : null;\n },\n getFollowUpMessages: async () => {\n const callerResult = (await this.options.getFollowUpMessages?.()) ?? [];\n const queued = this.followUpQueue.splice(0);\n const all = [...(callerResult ?? []), ...queued];\n return all.length > 0 ? all : null;\n },\n };\n\n const generator = agentLoop(this.messages, optionsWithQueues);\n return new AgentStream(generator, () => {\n this._running = false;\n });\n }\n}\n","import {\n stream,\n EventStream,\n type Message,\n type ToolCall,\n type ToolResult,\n type ToolResultContent,\n type Usage,\n type ContentPart,\n type AssistantMessage,\n} from \"@kenkaiiii/gg-ai\";\nimport type {\n AgentEvent,\n AgentOptions,\n AgentResult,\n AgentTool,\n ToolContext,\n ToolExecuteResult,\n StructuredToolResult,\n} from \"./types.js\";\n\nconst DEFAULT_MAX_TURNS = 200;\n\n/**\n * Lightweight stream diagnostic callback. When set, the agent loop calls this\n * at every phase boundary with timing and state info. This lets the hosting\n * app (ggcoder, come-alive, etc.) log stall diagnostics without the agent\n * package needing fs/process dependencies.\n */\nexport type StreamDiagnosticFn = (phase: string, data?: Record<string, unknown>) => void;\n\n/** Global diagnostic hook — set by the hosting app before calling agentLoop. */\nlet _diagFn: StreamDiagnosticFn | null = null;\n\n/** Register a diagnostic callback for stream stall tracing. */\nexport function setStreamDiagnostic(fn: StreamDiagnosticFn | null): void {\n _diagFn = fn;\n}\n\nfunction diag(phase: string, data?: Record<string, unknown>): void {\n _diagFn?.(phase, data);\n}\n\n/**\n * Detect abort errors — user-initiated cancellation or AbortSignal.\n * These should be caught and handled gracefully, not re-thrown.\n */\nexport function isAbortError(err: unknown): boolean {\n if (!(err instanceof Error)) return false;\n if (err.name === \"AbortError\") return true;\n const msg = err.message.toLowerCase();\n return msg.includes(\"aborted\") || msg.includes(\"abort\");\n}\n\n/**\n * Detect context window overflow errors from LLM providers.\n *\n * Patterns drawn from observed errors across Anthropic, OpenAI, OpenAI Codex,\n * Bedrock, Ollama, and OpenAI-compatible Chinese providers (GLM, Kimi, MiniMax).\n */\nexport function isContextOverflow(err: unknown): boolean {\n if (!(err instanceof Error)) return false;\n if (isBillingError(err)) return false;\n const msg = err.message.toLowerCase();\n return (\n msg.includes(\"prompt is too long\") ||\n msg.includes(\"prompt too long\") ||\n msg.includes(\"input is too long\") ||\n msg.includes(\"context_length_exceeded\") ||\n msg.includes(\"context_window_exceeded\") ||\n msg.includes(\"maximum context length\") ||\n msg.includes(\"exceeds model context window\") ||\n msg.includes(\"exceeds the context window\") ||\n msg.includes(\"content_too_large\") ||\n msg.includes(\"request_too_large\") ||\n msg.includes(\"reduce the length\") ||\n msg.includes(\"please shorten\") ||\n (msg.includes(\"token\") && msg.includes(\"exceed\"))\n );\n}\n\n/**\n * Detect billing/quota errors — these should NOT be retried.\n * GLM returns HTTP 429 with \"Insufficient balance\" for quota exhaustion.\n */\nexport function isBillingError(err: unknown): boolean {\n if (!(err instanceof Error)) return false;\n const msg = err.message.toLowerCase();\n return (\n msg.includes(\"insufficient balance\") ||\n msg.includes(\"no resource package\") ||\n msg.includes(\"quota exceeded\") ||\n msg.includes(\"billing\") ||\n msg.includes(\"recharge\") ||\n msg.includes(\"subscription plan\") ||\n msg.includes(\"does not yet include access\") ||\n msg.includes(\"token quota\") ||\n msg.includes(\"exceeded_current_quota_error\") ||\n msg.includes(\"check your account balance\")\n );\n}\n\n/**\n * Detect overloaded/rate-limit errors from LLM providers.\n * HTTP 429 (rate limit) or 529/503 (overloaded).\n * Excludes billing/quota errors which won't resolve with a retry.\n */\n/**\n * Detect tool pairing errors — orphaned tool_use or tool_result blocks.\n * These are 400 errors that can be recovered by repairing the message history.\n */\nexport function isToolPairingError(err: unknown): boolean {\n if (!(err instanceof Error)) return false;\n const msg = err.message.toLowerCase();\n return (\n (msg.includes(\"tool_use\") && msg.includes(\"tool_result\")) ||\n msg.includes(\"unexpected `tool_use_id`\") ||\n msg.includes(\"tool_use ids found without\") ||\n // Moonshot/OpenAI-compatible: \"tool call id <id> is not found\"\n (msg.includes(\"tool call id\") && msg.includes(\"is not found\"))\n );\n}\n\n/**\n * Distinguish rate-limit (HTTP 429) from server-side overload (HTTP 529).\n * Returns null for errors that should not enter the overload-retry bucket.\n * Both kinds use the same backoff schedule, but the UI shows different copy\n * and the log line records the true cause.\n */\nexport function classifyOverload(err: unknown): \"rate_limit\" | \"overloaded\" | null {\n if (!(err instanceof Error)) return null;\n if (isBillingError(err)) return null;\n const msg = err.message.toLowerCase();\n if (\n msg.includes(\"rate_limit\") ||\n msg.includes(\"rate limit\") ||\n msg.includes(\"too many requests\") ||\n msg.includes(\"429\")\n ) {\n return \"rate_limit\";\n }\n if (msg.includes(\"overloaded\") || msg.includes(\"529\")) {\n return \"overloaded\";\n }\n return null;\n}\n\nexport function isOverloaded(err: unknown): boolean {\n return classifyOverload(err) !== null;\n}\n\n/**\n * Detect malformed-stream errors — the SDK's SSE decoder threw a JSON parse\n * error mid-stream, typically because a chunk was truncated or corrupted by\n * an intermediary (CDN, proxy). Same class of transport failure as a stall:\n * replaying the request — and ideally flipping to non-streaming — recovers.\n */\nexport function isMalformedStream(err: unknown): boolean {\n if (!(err instanceof Error)) return false;\n if (err.name === \"SyntaxError\") return true;\n const cause = (err as { cause?: unknown }).cause;\n if (cause instanceof Error && cause.name === \"SyntaxError\") return true;\n const msg = err.message;\n // V8 JSON.parse error messages: \"Expected ... in JSON at position N\"\n // and \"Unexpected token ... in JSON at position N\"\n return /\\bin JSON at position \\d+/i.test(msg);\n}\n\n/**\n * Detect socket-level transport failures — the remote peer (or an\n * intermediary) closed the TCP connection mid-stream before the response\n * finished. Surfaces as `TypeError: terminated` from undici/fetch, or as\n * `ECONNRESET` / `socket hang up` / `UND_ERR_SOCKET` from the underlying\n * Node http layer. Undici nests the real cause one or more levels deep,\n * so we walk the `.cause` chain. Same recovery as a stall: replay the\n * request, optionally as non-streaming.\n */\nexport function isTransportFailure(err: unknown): boolean {\n const codes = new Set([\n \"ECONNRESET\",\n \"ECONNREFUSED\",\n \"ECONNABORTED\",\n \"ETIMEDOUT\",\n \"EPIPE\",\n \"EHOSTUNREACH\",\n \"ENETUNREACH\",\n \"ENOTFOUND\",\n \"UND_ERR_SOCKET\",\n \"UND_ERR_CONNECT_TIMEOUT\",\n \"UND_ERR_HEADERS_TIMEOUT\",\n \"UND_ERR_BODY_TIMEOUT\",\n \"UND_ERR_RESPONSE_STATUS_CODE\",\n \"UND_ERR_REQ_CONTENT_LENGTH_MISMATCH\",\n \"UND_ERR_RES_CONTENT_LENGTH_MISMATCH\",\n ]);\n const messages = [\n /^terminated$/i,\n /\\bother side closed\\b/i,\n /\\bsocket hang up\\b/i,\n /\\bfetch failed\\b/i,\n /\\bbody timeout error\\b/i,\n /\\bsse stream disconnected\\b/i,\n /\\bfailed to reconnect sse stream\\b/i,\n ];\n const seen = new Set<unknown>();\n let cur: unknown = err;\n while (cur && typeof cur === \"object\" && !seen.has(cur)) {\n seen.add(cur);\n const e = cur as { code?: unknown; message?: unknown; cause?: unknown };\n if (typeof e.code === \"string\" && codes.has(e.code)) return true;\n if (typeof e.message === \"string\") {\n for (const re of messages) if (re.test(e.message)) return true;\n }\n cur = e.cause;\n }\n return false;\n}\n\n/**\n * Promise-returning sleep that rejects with AbortError if `signal` fires.\n * Used by retry backoffs so ESC/Ctrl+C cancel immediately instead of having\n * to wait out the full delay (up to 30s per overload retry × 10 retries).\n */\nfunction abortableSleep(ms: number, signal?: AbortSignal): Promise<void> {\n if (signal?.aborted) {\n return Promise.reject(new DOMException(\"Aborted\", \"AbortError\"));\n }\n return new Promise<void>((resolve, reject) => {\n let onAbort: (() => void) | null = null;\n const timer = setTimeout(() => {\n if (onAbort) signal?.removeEventListener(\"abort\", onAbort);\n resolve();\n }, ms);\n onAbort = () => {\n clearTimeout(timer);\n reject(new DOMException(\"Aborted\", \"AbortError\"));\n };\n signal?.addEventListener(\"abort\", onAbort, { once: true });\n });\n}\n\nexport async function* agentLoop(\n messages: Message[],\n options: AgentOptions,\n): AsyncGenerator<AgentEvent, AgentResult> {\n const maxTurns = options.maxTurns ?? DEFAULT_MAX_TURNS;\n const maxContinuations = options.maxContinuations ?? 5;\n const toolMap = new Map<string, AgentTool>((options.tools ?? []).map((t) => [t.name, t]));\n\n const totalUsage: Usage = { inputTokens: 0, outputTokens: 0 };\n let turn = 0;\n let firstTurn = true;\n let consecutivePauses = 0;\n let toolPairingRepaired = false;\n let overloadRetries = 0;\n let emptyResponseRetries = 0;\n let stallRetries = 0;\n let overflowCompactionAttempts = 0;\n // Non-streaming fallback mode. After repeated stream stalls, flip to a\n // plain non-streaming request/response -- often survives broken SSE\n // connections (transient CDN / proxy issues) that streaming retries cannot.\n let useNonStreamingFallback = false;\n const MAX_OVERLOAD_RETRIES = 10;\n const MAX_EMPTY_RESPONSE_RETRIES = 2;\n const MAX_STALL_RETRIES = 5;\n const MAX_OVERFLOW_COMPACTIONS = 2;\n // After this many streaming stalls in a row, switch to non-streaming mode\n // for the remaining stall retries. Keeps the first two retries fast (the\n // cheap \"transient glitch\" case) before paying for a full response round-trip.\n const STALL_RETRIES_BEFORE_NON_STREAMING = 2;\n const STALL_DELAY_MS = 1_000; // Brief pause before retry -- just enough to avoid tight loops\n const OVERLOAD_BASE_DELAY_MS = 2_000;\n const OVERLOAD_MAX_DELAY_MS = 30_000;\n const STREAM_FIRST_EVENT_TIMEOUT_MS = 45_000; // 45s to get first event (Opus thinks long)\n const STREAM_IDLE_TIMEOUT_MS = 30_000; // 30s between events once streaming starts\n // Anthropic models can pause 10-20s mid-stream while computing the next chunk\n // (e.g. generating tool call args for a large write). 10s was too aggressive\n // and caused false \"stream stalled\" errors, especially in plan mode.\n const STREAM_HARD_TIMEOUT_MS = 90_000; // 90s absolute cap before output starts\n // Once output events (text_delta) are actively streaming, extend the hard\n // timeout -- long responses (plan mode, detailed explanations) can legitimately\n // take 2-3+ minutes while events flow continuously.\n const STREAM_OUTPUT_HARD_TIMEOUT_MS = 300_000; // 5min hard cap once output is flowing\n // Reasoning models (MiMo) can pause 3-5 minutes between thinking and output\n // generation. Once we've seen thinking events, extend timeouts significantly.\n const STREAM_THINKING_IDLE_TIMEOUT_MS = 300_000; // 5min idle after thinking\n const STREAM_THINKING_HARD_TIMEOUT_MS = 600_000; // 10min hard cap with thinking\n // Non-streaming mode has no per-event idle -- the entire response arrives in\n // one HTTP round-trip. Use a single generous hard cap instead. This matches\n // Claude Code's v2.1.110/111 behaviour: cap non-streaming retries so API\n // unreachability doesn't cause multi-minute hangs, but not so aggressively\n // that slow-but-healthy backends get killed.\n const NON_STREAMING_HARD_TIMEOUT_MS = 300_000; // 5min for full non-streaming response\n // Runaway tool-call circuit breaker. When a model glitches mid-tool-call it\n // can emit tens of thousands of toolcall_delta events without ever closing,\n // burning the entire stall-retry budget (~25 min) on what is clearly a\n // non-recoverable model error. Cap accumulated arg chars and event count;\n // exceeding either is a hard, non-retriable failure. Thresholds are generous\n // enough to allow legitimate large file writes through `write`.\n const MAX_TOOLCALL_DELTA_CHARS = 1_000_000; // 1 MB of accumulated tool-call args\n const MAX_TOOLCALL_DELTA_EVENTS = 20_000; // 20k delta events in one stream\n\n try {\n while (turn < maxTurns) {\n options.signal?.throwIfAborted();\n turn++;\n\n // Estimate message payload size for diagnostics\n let msgChars = 0;\n for (const m of messages) {\n if (typeof m.content === \"string\") msgChars += m.content.length;\n else if (Array.isArray(m.content)) {\n for (const p of m.content) {\n if (\"text\" in p && typeof p.text === \"string\") msgChars += p.text.length;\n if (\"content\" in p && typeof p.content === \"string\") msgChars += p.content.length;\n }\n }\n }\n diag(\"turn_start\", {\n turn,\n messages: messages.length,\n chars: msgChars,\n provider: options.provider,\n model: options.model,\n });\n\n // ── Initial steering poll: catch messages queued before the first LLM call ──\n if (firstTurn && options.getSteeringMessages) {\n const steering = await options.getSteeringMessages();\n if (steering && steering.length > 0) {\n for (const msg of steering) {\n yield { type: \"steering_message\" as const, content: msg.content };\n messages.push(msg);\n }\n }\n }\n firstTurn = false;\n\n // ── Mid-loop context transform (compaction / truncation) ──\n if (options.transformContext) {\n diag(\"transform_start\");\n const transformed = await options.transformContext(messages);\n if (transformed !== messages) {\n diag(\"transform_compacted\", {\n before: messages.length,\n after: transformed.length,\n });\n messages.length = 0;\n messages.push(...transformed);\n }\n diag(\"transform_end\");\n }\n\n // ── Repair tool pairing: ensure every tool_use has an adjacent tool_result ──\n repairToolPairingAdjacent(messages);\n\n // ── Call LLM with overflow recovery ──\n let response;\n // Per-attempt abort controller: allows idle timeout to abort the stream\n // without affecting the caller's signal. The caller's abort is forwarded.\n const streamController = new AbortController();\n let idleTimer: ReturnType<typeof setTimeout> | null = null;\n let hardTimer: ReturnType<typeof setTimeout> | null = null;\n let idleTimedOut = false;\n\n // Stream event counters — declared here so timeout callbacks can access them\n let streamEventCount = 0;\n let lastEventTime = Date.now();\n let streamCallStart = Date.now();\n // Track event types for diagnostics — shows what arrived before a stall\n const eventTypeCounts: Record<string, number> = {};\n let lastEventType = \"\";\n // Runaway tool-call detection — accumulated across all toolcall_delta\n // events in this stream attempt. When tripped we abort the stream and\n // bail out without retrying (the model has glitched, retries won't help).\n let toolcallDeltaChars = 0;\n let toolcallDeltaCount = 0;\n let runawayDetected: { kind: \"chars\" | \"events\"; chars: number; events: number } | null =\n null;\n // Track consumer processing time — helps distinguish \"API stopped sending\"\n // from \"our consumer was slow to pull the next event\"\n let lastYieldEndTime = Date.now();\n let maxConsumerLagMs = 0;\n\n // Forward caller abort to the per-attempt controller\n const forwardAbort = () => streamController.abort();\n options.signal?.addEventListener(\"abort\", forwardAbort, { once: true });\n\n // Three-phase idle timeout:\n // - Before first event: STREAM_FIRST_EVENT_TIMEOUT_MS (45s) -- Opus can\n // take 30s+ to start on large contexts, that's not a stall.\n // - After output event (text_delta, server_toolcall): STREAM_IDLE_TIMEOUT_MS\n // (10s) -- once output is streaming, 10s of silence is dead. Retry fast.\n // - After thinking events only: STREAM_THINKING_IDLE_TIMEOUT_MS (5min) --\n // reasoning models (MiMo) can pause minutes between thinking and output.\n //\n // In non-streaming fallback mode the entire response arrives in a single\n // HTTP round-trip, so the idle timer is disabled -- only the hard timeout\n // applies. Synthesized events all arrive at once when the response returns.\n let hasReceivedEvent = false;\n let hasReceivedThinking = false;\n const resetIdleTimer = () => {\n if (useNonStreamingFallback) return; // no inter-event idle in non-streaming mode\n if (idleTimer) clearTimeout(idleTimer);\n const timeoutMs = hasReceivedEvent\n ? STREAM_IDLE_TIMEOUT_MS\n : hasReceivedThinking\n ? STREAM_THINKING_IDLE_TIMEOUT_MS\n : STREAM_FIRST_EVENT_TIMEOUT_MS;\n idleTimer = setTimeout(() => {\n diag(\"idle_timeout_fired\", {\n events: streamEventCount,\n sinceLastEventMs: Date.now() - lastEventTime,\n lastEventType,\n maxConsumerLagMs,\n phase: hasReceivedEvent\n ? \"mid_stream\"\n : hasReceivedThinking\n ? \"post_thinking\"\n : \"first_event\",\n eventTypes: eventTypeCounts,\n });\n idleTimedOut = true;\n streamController.abort();\n }, timeoutMs);\n };\n\n // Hard timeout: absolute cap per LLM call. Safety net for streams that\n // keep sending sparse events (e.g. keep-alive pings) but never complete.\n // Extended dynamically when thinking events arrive (see thinking_delta handler).\n // Non-streaming fallback uses a single larger cap since there's no stream\n // to observe -- just wait for the full response up to the cap.\n let hardTimeoutMs = useNonStreamingFallback\n ? NON_STREAMING_HARD_TIMEOUT_MS\n : STREAM_HARD_TIMEOUT_MS;\n hardTimer = setTimeout(() => {\n diag(\"hard_timeout_fired\", {\n events: typeof streamEventCount !== \"undefined\" ? streamEventCount : 0,\n nonStreaming: useNonStreamingFallback,\n });\n idleTimedOut = true;\n streamController.abort();\n }, hardTimeoutMs);\n\n try {\n diag(\"stream_call\", { nonStreaming: useNonStreamingFallback });\n streamCallStart = Date.now();\n const result = stream({\n provider: options.provider,\n model: options.model,\n messages,\n tools: options.tools,\n serverTools: options.serverTools,\n webSearch: options.webSearch,\n maxTokens: options.maxTokens,\n temperature: options.temperature,\n thinking: options.thinking,\n apiKey: options.apiKey,\n baseUrl: options.baseUrl,\n signal: streamController.signal,\n accountId: options.accountId,\n cacheRetention: options.cacheRetention,\n promptCacheKey: options.promptCacheKey,\n serviceTier: options.serviceTier,\n supportsImages: options.supportsImages,\n compaction: options.compaction,\n clearToolUses: options.clearToolUses,\n userAgent: options.userAgent,\n // Flip to non-streaming fallback after repeated stream stalls.\n ...(useNonStreamingFallback ? { streaming: false } : {}),\n });\n diag(\"stream_created\", { setupMs: Date.now() - streamCallStart });\n\n // Suppress unhandled rejection if the iterator path throws first\n result.response.catch(() => {});\n\n // Forward streaming deltas — reset idle timer on each event\n streamEventCount = 0;\n hasReceivedEvent = false;\n lastEventTime = Date.now();\n streamCallStart = Date.now();\n // Reset to streamCallStart so the first event's consumerLag reflects\n // network/provider latency, not the time spent before stream() returned.\n lastYieldEndTime = Date.now();\n resetIdleTimer();\n for await (const event of result) {\n // Measure consumer lag: time between finishing previous yield and\n // receiving this event. For event #1 this still includes network/\n // provider latency; for subsequent events it isolates how long\n // React/UI rendering held up the next pull.\n const pullTime = Date.now();\n const consumerLag = pullTime - lastYieldEndTime;\n // Only track mid-stream lag — first event lag is dominated by\n // server-side TTFB and would mask real UI starvation issues.\n if (streamEventCount > 0 && consumerLag > maxConsumerLagMs) {\n maxConsumerLagMs = consumerLag;\n }\n\n streamEventCount++;\n eventTypeCounts[event.type] = (eventTypeCounts[event.type] ?? 0) + 1;\n lastEventType = event.type;\n\n // Flip to mid-stream timeout on confirmed output events — text\n // deltas, completed tool calls, and tool call deltas (large file\n // writes can stream toolcall_delta for minutes without any text_delta).\n // Reasoning models (MiMo) are handled separately below — they can\n // stream hundreds of thinking events then pause minutes before output.\n if (\n (event.type === \"text_delta\" ||\n event.type === \"server_toolcall\" ||\n event.type === \"toolcall_delta\") &&\n !hasReceivedEvent\n ) {\n hasReceivedEvent = true;\n // Extend hard timeout now that output is actively streaming.\n // Long responses (plan mode, detailed code) can exceed 90s while\n // events flow continuously — the idle timeout (10s) catches real stalls.\n if (hardTimer && hardTimeoutMs < STREAM_OUTPUT_HARD_TIMEOUT_MS) {\n clearTimeout(hardTimer);\n hardTimeoutMs = STREAM_OUTPUT_HARD_TIMEOUT_MS;\n hardTimer = setTimeout(() => {\n diag(\"hard_timeout_fired\", { events: streamEventCount });\n idleTimedOut = true;\n streamController.abort();\n }, hardTimeoutMs);\n }\n }\n // Track thinking events — extends idle timeout and hard timeout\n // so reasoning models aren't killed during thinking→output transition.\n if (event.type === \"thinking_delta\" && !hasReceivedThinking) {\n hasReceivedThinking = true;\n // Extend the hard timeout now that we know the model is reasoning\n if (hardTimer) clearTimeout(hardTimer);\n hardTimeoutMs = STREAM_THINKING_HARD_TIMEOUT_MS;\n hardTimer = setTimeout(() => {\n diag(\"hard_timeout_fired\", { events: streamEventCount });\n idleTimedOut = true;\n streamController.abort();\n }, hardTimeoutMs);\n }\n\n const now = Date.now();\n const gap = now - lastEventTime;\n // Log first event and any suspiciously long gaps\n if (streamEventCount === 1) {\n diag(\"first_event\", { type: event.type, ttfMs: now - streamCallStart });\n } else if (gap > 3000) {\n diag(\"slow_gap\", {\n type: event.type,\n gapMs: gap,\n eventNum: streamEventCount,\n sinceStartMs: now - streamCallStart,\n });\n }\n lastEventTime = now;\n resetIdleTimer();\n if (event.type === \"text_delta\") {\n yield { type: \"text_delta\" as const, text: event.text };\n } else if (event.type === \"thinking_delta\") {\n yield { type: \"thinking_delta\" as const, text: event.text };\n } else if (event.type === \"server_toolcall\") {\n yield {\n type: \"server_tool_call\" as const,\n id: event.id,\n name: event.name,\n input: event.input,\n };\n } else if (event.type === \"server_toolresult\") {\n yield {\n type: \"server_tool_result\" as const,\n toolUseId: event.toolUseId,\n resultType: event.resultType,\n data: event.data,\n };\n } else if (event.type === \"toolcall_delta\") {\n const chunkChars = event.argsJson?.length ?? 0;\n toolcallDeltaChars += chunkChars;\n toolcallDeltaCount++;\n if (\n !runawayDetected &&\n (toolcallDeltaChars > MAX_TOOLCALL_DELTA_CHARS ||\n toolcallDeltaCount > MAX_TOOLCALL_DELTA_EVENTS)\n ) {\n runawayDetected = {\n kind: toolcallDeltaChars > MAX_TOOLCALL_DELTA_CHARS ? \"chars\" : \"events\",\n chars: toolcallDeltaChars,\n events: toolcallDeltaCount,\n };\n diag(\"runaway_toolcall_detected\", {\n ...runawayDetected,\n provider: options.provider,\n model: options.model,\n });\n streamController.abort();\n }\n yield {\n type: \"toolcall_delta\" as const,\n chars: chunkChars,\n };\n }\n lastYieldEndTime = Date.now();\n }\n\n diag(\"stream_done\", {\n events: streamEventCount,\n totalMs: Date.now() - streamCallStart,\n maxConsumerLagMs,\n eventTypes: eventTypeCounts,\n });\n response = await result.response;\n } catch (err) {\n const errMsg = err instanceof Error ? err.message : String(err);\n diag(\"stream_error\", {\n error: errMsg.slice(0, 200),\n events: streamEventCount,\n totalMs: Date.now() - streamCallStart,\n idleTimedOut,\n aborted: !!options.signal?.aborted,\n eventTypes: eventTypeCounts,\n provider: options.provider,\n model: options.model,\n });\n // Context overflow: try a forced compaction before giving up.\n // The pre-turn transformContext check uses estimated tokens, which can\n // underestimate code-heavy content. When the API confirms overflow we\n // compact unconditionally and retry the turn, capped at\n // MAX_OVERFLOW_COMPACTIONS to avoid loops when compaction can't reduce\n // enough (e.g. single huge user message).\n if (isContextOverflow(err)) {\n if (options.transformContext && overflowCompactionAttempts < MAX_OVERFLOW_COMPACTIONS) {\n overflowCompactionAttempts++;\n diag(\"overflow_compact_start\", {\n attempt: overflowCompactionAttempts,\n maxAttempts: MAX_OVERFLOW_COMPACTIONS,\n messages: messages.length,\n });\n try {\n const compacted = await options.transformContext(messages, { force: true });\n if (compacted !== messages && compacted.length < messages.length) {\n messages.length = 0;\n messages.push(...compacted);\n diag(\"overflow_compact_success\", {\n attempt: overflowCompactionAttempts,\n messages: messages.length,\n });\n yield {\n type: \"retry\" as const,\n reason: \"overflow_compact\" as const,\n attempt: overflowCompactionAttempts,\n maxAttempts: MAX_OVERFLOW_COMPACTIONS,\n delayMs: 0,\n };\n turn--;\n continue;\n }\n diag(\"overflow_compact_noop\", {\n attempt: overflowCompactionAttempts,\n before: messages.length,\n after: compacted.length,\n });\n } catch (compactErr) {\n diag(\"overflow_compact_failed\", {\n error: compactErr instanceof Error ? compactErr.message : String(compactErr),\n });\n }\n }\n yield { type: \"error\" as const, error: err instanceof Error ? err : new Error(errMsg) };\n throw err;\n }\n // Overloaded / rate-limited: exponential backoff, retry up to 10 times\n const overloadKind = classifyOverload(err);\n if (overloadRetries < MAX_OVERLOAD_RETRIES && overloadKind) {\n overloadRetries++;\n const delayMs = Math.min(\n OVERLOAD_BASE_DELAY_MS * 2 ** (overloadRetries - 1),\n OVERLOAD_MAX_DELAY_MS,\n );\n diag(\"retry\", {\n reason: overloadKind,\n attempt: overloadRetries,\n maxAttempts: MAX_OVERLOAD_RETRIES,\n delayMs,\n });\n yield {\n type: \"retry\" as const,\n reason: overloadKind,\n attempt: overloadRetries,\n maxAttempts: MAX_OVERLOAD_RETRIES,\n delayMs,\n };\n await abortableSleep(delayMs, options.signal);\n turn--; // Don't count the failed turn\n continue;\n }\n // Stream stall: the API connection hung without closing.\n // Malformed stream: the SDK's SSE decoder hit truncated/corrupted JSON.\n // Both are transport failures — retry with exponential backoff and flip\n // to non-streaming mode after STALL_RETRIES_BEFORE_NON_STREAMING attempts,\n // since broken SSE often recovers when replayed as plain HTTP.\n // Runaway tool-call: the model never closed a tool-call block and\n // blew past the size/count caps. Retrying just reproduces the loop,\n // so surface a clear error and stop. Checked before the abort branch\n // since we ourselves aborted the stream to break the runaway.\n if (runawayDetected) {\n diag(\"runaway_toolcall_aborted\", {\n ...runawayDetected,\n provider: options.provider,\n model: options.model,\n });\n const detail =\n runawayDetected.kind === \"chars\"\n ? `${(runawayDetected.chars / 1024).toFixed(0)} KB of tool-call arguments`\n : `${runawayDetected.events} tool-call delta events`;\n yield {\n type: \"error\" as const,\n error: new Error(\n `The model glitched mid-tool-call and produced ${detail} without closing the call. ` +\n `This is usually an upstream model bug — try the same request again or switch models. ` +\n `Your conversation is preserved.`,\n ),\n };\n break;\n }\n const malformed = isMalformedStream(err);\n const socketDrop = isTransportFailure(err);\n const transportFailure =\n (idleTimedOut || malformed || socketDrop) && !options.signal?.aborted;\n if (transportFailure && stallRetries < MAX_STALL_RETRIES) {\n stallRetries++;\n const cause = malformed\n ? \"malformed_stream\"\n : socketDrop\n ? \"socket_drop\"\n : \"stream_stall\";\n if (!useNonStreamingFallback && stallRetries >= STALL_RETRIES_BEFORE_NON_STREAMING) {\n useNonStreamingFallback = true;\n diag(\"non_streaming_fallback_enabled\", {\n stallRetries,\n provider: options.provider,\n model: options.model,\n cause,\n });\n }\n const delayMs = Math.min(STALL_DELAY_MS * 2 ** (stallRetries - 1), 8_000);\n diag(\"retry\", {\n reason: cause,\n attempt: stallRetries,\n maxAttempts: MAX_STALL_RETRIES,\n delayMs,\n events: streamEventCount,\n nonStreaming: useNonStreamingFallback,\n });\n yield {\n type: \"retry\" as const,\n reason: \"stream_stall\" as const,\n attempt: stallRetries,\n maxAttempts: MAX_STALL_RETRIES,\n delayMs,\n silent: stallRetries <= 2,\n };\n await abortableSleep(delayMs, options.signal);\n turn--; // Don't count the failed turn\n continue;\n }\n // Stream stall retries exhausted — surface a clear error so the UI\n // can distinguish \"gave up after stalls\" from \"completed normally\".\n if (transportFailure) {\n diag(\"stall_exhausted\", {\n stallRetries: MAX_STALL_RETRIES,\n provider: options.provider,\n model: options.model,\n });\n yield {\n type: \"error\" as const,\n error: new Error(\n `The API provider's stream stalled ${MAX_STALL_RETRIES} times — the provider may be experiencing capacity issues. ` +\n `Your conversation is preserved. Send another message to retry.`,\n ),\n };\n break;\n }\n // Tool pairing 400: orphaned tool_result or tool_use in message history.\n // Run repair and retry once — if repair can't fix it, surface the error.\n if (isToolPairingError(err) && !toolPairingRepaired) {\n toolPairingRepaired = true;\n diag(\"tool_pairing_repair\", { error: errMsg.slice(0, 200) });\n repairToolPairingAdjacent(messages);\n turn--;\n continue;\n }\n // Abort errors (user cancellation) — exit loop cleanly instead of\n // crashing the process with an unhandled rejection.\n if (isAbortError(err) || options.signal?.aborted) {\n diag(\"aborted\", { turn, provider: options.provider, model: options.model });\n break;\n }\n // Unhandled error — log before throwing so the crash is traceable\n diag(\"unhandled_error\", {\n error: errMsg.slice(0, 500),\n turn,\n provider: options.provider,\n model: options.model,\n });\n throw err;\n } finally {\n if (idleTimer) clearTimeout(idleTimer);\n if (hardTimer) clearTimeout(hardTimer);\n options.signal?.removeEventListener(\"abort\", forwardAbort);\n }\n\n overloadRetries = 0;\n stallRetries = 0;\n\n // Detect empty/degenerate responses — the API occasionally returns 0 tokens\n // with no content, or \"thinks\" without producing actionable output.\n // Reasoning models (MiMo, DeepSeek) may report outputTokens > 0 from\n // thinking alone while producing no text or tool calls — still a dud.\n const contentArr = Array.isArray(response.message.content) ? response.message.content : null;\n const hasActionableContent =\n response.message.content !== \"\" &&\n contentArr !== null &&\n contentArr.some(\n (p) => p.type === \"text\" || p.type === \"tool_call\" || p.type === \"server_tool_call\",\n );\n if (!hasActionableContent) {\n if (emptyResponseRetries < MAX_EMPTY_RESPONSE_RETRIES) {\n emptyResponseRetries++;\n diag(\"retry\", {\n reason: \"empty_response\",\n attempt: emptyResponseRetries,\n maxAttempts: MAX_EMPTY_RESPONSE_RETRIES,\n provider: options.provider,\n model: options.model,\n contentTypes: contentArr?.map((p) => p.type).join(\",\") ?? \"empty\",\n });\n yield {\n type: \"retry\" as const,\n reason: \"empty_response\" as const,\n attempt: emptyResponseRetries,\n maxAttempts: MAX_EMPTY_RESPONSE_RETRIES,\n delayMs: 0,\n };\n turn--; // Don't count the failed turn — keep useNonStreamingFallback set\n // so the retry doesn't bounce back into a streaming connection that\n // will stall again with the same upstream problem.\n continue;\n }\n // Exhausted retries — fall through and let the agent finish\n }\n emptyResponseRetries = 0;\n\n // Only clear the non-streaming fallback after an actionable response —\n // an empty non-streaming reply means the upstream issue hasn't resolved,\n // so staying in non-streaming mode avoids retrying into another stall.\n useNonStreamingFallback = false;\n\n // Accumulate usage\n totalUsage.inputTokens += response.usage.inputTokens;\n totalUsage.outputTokens += response.usage.outputTokens;\n if (response.usage.cacheRead) {\n totalUsage.cacheRead = (totalUsage.cacheRead ?? 0) + response.usage.cacheRead;\n }\n if (response.usage.cacheWrite) {\n totalUsage.cacheWrite = (totalUsage.cacheWrite ?? 0) + response.usage.cacheWrite;\n }\n\n // Append assistant message to conversation\n messages.push(response.message);\n\n yield {\n type: \"turn_end\" as const,\n turn,\n stopReason: response.stopReason,\n usage: response.usage,\n };\n\n // Server-side tool hit iteration limit — re-send to continue.\n // Do NOT add an extra user message; the API detects the trailing\n // server_tool_use block and resumes automatically.\n if (response.stopReason === \"pause_turn\") {\n consecutivePauses++;\n if (consecutivePauses >= maxContinuations) {\n break; // Safety limit — fall through to agent_done below\n }\n continue;\n }\n consecutivePauses = 0;\n\n // Extract tool calls — separate client-executed from provider built-in (e.g. Moonshot $web_search)\n const allToolCalls = extractToolCalls(response.message.content);\n\n // If no tool calls to execute, check for steering messages before stopping.\n // Check content (not just stopReason) because some providers (e.g. GLM)\n // return finish_reason=\"stop\" even when tool calls are present.\n if (response.stopReason !== \"tool_use\" && allToolCalls.length === 0) {\n // Check for queued steering messages — if present, inject and continue\n // the loop instead of returning (follow-up pattern).\n if (options.getSteeringMessages) {\n const steering = await options.getSteeringMessages();\n if (steering && steering.length > 0) {\n for (const msg of steering) {\n yield { type: \"steering_message\" as const, content: msg.content };\n messages.push(msg);\n }\n continue; // Next iteration will call LLM with injected messages\n }\n }\n // Follow-up: lower priority than steering — only when agent would otherwise stop.\n if (options.getFollowUpMessages) {\n const followUp = await options.getFollowUpMessages();\n if (followUp && followUp.length > 0) {\n for (const msg of followUp) {\n yield { type: \"follow_up_message\" as const, content: msg.content };\n messages.push(msg);\n }\n continue;\n }\n }\n yield {\n type: \"agent_done\" as const,\n totalTurns: turn,\n totalUsage: { ...totalUsage },\n };\n return {\n message: response.message,\n totalTurns: turn,\n totalUsage: { ...totalUsage },\n };\n }\n const toolCalls: ToolCall[] = [];\n const toolResults: ToolResult[] = [];\n\n for (const tc of allToolCalls) {\n if (tc.name.startsWith(\"$\")) {\n // Provider built-in tool (e.g. Moonshot $web_search) — not locally executed.\n // Still needs a tool_result for the message history round-trip.\n toolResults.push({\n type: \"tool_result\",\n toolCallId: tc.id,\n content: JSON.stringify(tc.args),\n });\n } else {\n toolCalls.push(tc);\n }\n }\n const eventStream = new EventStream<AgentEvent>();\n\n // Launch all tool calls in parallel\n const executions = toolCalls.map(async (toolCall) => {\n const startTime = Date.now();\n\n eventStream.push({\n type: \"tool_call_start\" as const,\n toolCallId: toolCall.id,\n name: toolCall.name,\n args: toolCall.args,\n });\n\n let resultContent: ToolResultContent;\n let details: unknown;\n let isError = false;\n\n const tool = toolMap.get(toolCall.name);\n if (!tool) {\n resultContent = `Unknown tool: ${toolCall.name}`;\n isError = true;\n } else {\n try {\n const parsed = tool.parameters.parse(toolCall.args);\n const ctx: ToolContext = {\n signal: options.signal ?? AbortSignal.timeout(300_000),\n toolCallId: toolCall.id,\n onUpdate: (update: unknown) => {\n eventStream.push({\n type: \"tool_call_update\" as const,\n toolCallId: toolCall.id,\n update,\n });\n },\n };\n const raw = await tool.execute(parsed, ctx);\n const normalized = normalizeToolResult(raw);\n resultContent = normalized.content;\n details = normalized.details;\n } catch (err) {\n isError = true;\n resultContent = err instanceof Error ? err.message : String(err);\n }\n }\n\n const durationMs = Date.now() - startTime;\n\n eventStream.push({\n type: \"tool_call_end\" as const,\n toolCallId: toolCall.id,\n result: toolResultPreview(resultContent),\n details,\n isError,\n durationMs,\n });\n\n return { toolCallId: toolCall.id, content: resultContent, isError };\n });\n\n // Abort the tool event stream when the signal fires so Ctrl+C\n // doesn't hang waiting for long-running tools to finish.\n const abortHandler = () => eventStream.abort(new Error(\"aborted\"));\n options.signal?.addEventListener(\"abort\", abortHandler, { once: true });\n\n // Close event stream when all tools complete.\n // Track whether the finally block has already consumed toolResults\n // to prevent the race where .then() mutates toolResults after\n // messages.push() has already captured the array by reference.\n let toolResultsFinalized = false;\n\n Promise.all(executions)\n .then((results) => {\n if (toolResultsFinalized) return;\n const resultsMap = new Map(results.map((r) => [r.toolCallId, r]));\n for (const tc of toolCalls) {\n const r = resultsMap.get(tc.id)!;\n toolResults.push({\n type: \"tool_result\",\n toolCallId: tc.id,\n content: r.content,\n isError: r.isError || undefined,\n });\n }\n eventStream.close();\n })\n .catch((err) => eventStream.abort(err instanceof Error ? err : new Error(String(err))));\n\n // Yield events as they arrive from parallel tools\n let toolsAborted = false;\n try {\n for await (const event of eventStream) {\n yield event;\n }\n } catch (err) {\n // Tool event stream aborted (Ctrl+C) — don't propagate, just mark\n // so the finally block can clean up and the loop can exit.\n if (isAbortError(err) || options.signal?.aborted) {\n toolsAborted = true;\n } else {\n throw err;\n }\n } finally {\n options.signal?.removeEventListener(\"abort\", abortHandler);\n\n // Prevent the Promise.all .then() from mutating toolResults after\n // we finalize and push them into messages.\n toolResultsFinalized = true;\n\n // Ensure every tool_use has a matching tool_result, even on abort.\n // Without this, an aborted turn leaves an orphaned tool_use in the\n // message history which causes Anthropic API 400 errors on the next\n // request.\n const resolvedIds = new Set(toolResults.map((r) => r.toolCallId));\n for (const tc of toolCalls) {\n if (!resolvedIds.has(tc.id)) {\n toolResults.push({\n type: \"tool_result\",\n toolCallId: tc.id,\n content: \"Tool execution was aborted.\",\n isError: true,\n });\n }\n }\n // Guard: cap oversized tool results before they enter conversation history.\n // Uses head+tail strategy to preserve error messages / closing structure at the end.\n if (options.maxToolResultChars) {\n const HARD_MAX = 400_000; // absolute ceiling regardless of context window\n const max = Math.min(options.maxToolResultChars, HARD_MAX);\n for (const tr of toolResults) {\n // Only truncate string content — array content (text+image blocks)\n // is already size-bounded by the image resizer.\n if (typeof tr.content === \"string\" && tr.content.length > max) {\n // Keep 70% head + 30% tail to preserve errors/diagnostics at the end\n const headChars = Math.floor(max * 0.7);\n const tailChars = max - headChars;\n const head = tr.content.slice(0, headChars);\n const tail = tr.content.slice(-tailChars);\n const omitted = tr.content.length - headChars - tailChars;\n tr.content = head + `\\n\\n[... ${omitted} characters omitted ...]\\n\\n` + tail;\n }\n }\n }\n\n messages.push({ role: \"tool\", content: toolResults });\n }\n\n // Exit loop after cleaning up aborted tools\n if (toolsAborted) break;\n\n // ── Steering messages: inject user messages queued during tool execution ──\n // Polled after tools complete so the next LLM call sees them in context.\n if (options.getSteeringMessages) {\n const steering = await options.getSteeringMessages();\n if (steering && steering.length > 0) {\n for (const msg of steering) {\n yield { type: \"steering_message\" as const, content: msg.content };\n messages.push(msg);\n }\n }\n }\n }\n } finally {\n // Sanitize orphaned server_tool_use blocks on abort.\n // When a stream is aborted mid-server-tool (e.g. web_search), the\n // assistant message containing the server_tool_use may already be in\n // the messages array, but the corresponding web_search_tool_result\n // never arrived. The API rejects the next request with a 400 if it\n // finds an unmatched server_tool_use, so we strip it here.\n sanitizeOrphanedServerTools(messages);\n }\n\n // Exceeded max turns — return last assistant message\n let lastAssistant: AssistantMessage | undefined;\n for (let i = messages.length - 1; i >= 0; i--) {\n if (messages[i]!.role === \"assistant\") {\n lastAssistant = messages[i] as AssistantMessage;\n break;\n }\n }\n\n yield {\n type: \"agent_done\" as const,\n totalTurns: turn,\n totalUsage: { ...totalUsage },\n };\n\n return {\n message: lastAssistant ?? { role: \"assistant\" as const, content: [] },\n totalTurns: turn,\n totalUsage: { ...totalUsage },\n };\n}\n\nfunction normalizeToolResult(raw: ToolExecuteResult): StructuredToolResult {\n return typeof raw === \"string\" ? { content: raw } : raw;\n}\n\n/** Flatten tool result content to a plain-text preview for the tool_call_end event.\n * Image blocks become a \"[image]\" placeholder so the UI has something to render. */\nfunction toolResultPreview(content: ToolResultContent): string {\n if (typeof content === \"string\") return content;\n return content\n .map((block) => (block.type === \"text\" ? block.text : `[image ${block.mediaType}]`))\n .join(\"\\n\");\n}\n\nfunction extractToolCalls(content: string | ContentPart[]): ToolCall[] {\n if (typeof content === \"string\") return [];\n return content.filter((part): part is ToolCall => part.type === \"tool_call\");\n}\n\n/**\n * Remove orphaned server_tool_use blocks from the last assistant message.\n * When a stream is aborted mid-server-tool (e.g. web_search), the assistant\n * message may contain a server_tool_call without a matching server_tool_result.\n * The API rejects the next request if these are unmatched.\n */\nfunction sanitizeOrphanedServerTools(messages: Message[]): void {\n // Find the last assistant message\n for (let i = messages.length - 1; i >= 0; i--) {\n const msg = messages[i]!;\n if (msg.role !== \"assistant\") continue;\n if (typeof msg.content === \"string\" || !Array.isArray(msg.content)) break;\n\n // Collect server_tool_call ids and matched server_tool_result ids\n const serverToolIds = new Set<string>();\n const resultToolIds = new Set<string>();\n for (const part of msg.content) {\n if (part.type === \"server_tool_call\") serverToolIds.add(part.id);\n if (part.type === \"server_tool_result\") resultToolIds.add(part.toolUseId);\n }\n\n // Find unmatched server_tool_call blocks\n const orphanedIds = new Set<string>();\n for (const id of serverToolIds) {\n if (!resultToolIds.has(id)) orphanedIds.add(id);\n }\n\n if (orphanedIds.size === 0) break;\n\n // Strip orphaned server_tool_call blocks from the content\n const filtered = msg.content.filter(\n (part) => !(part.type === \"server_tool_call\" && orphanedIds.has(part.id)),\n );\n\n if (filtered.length === 0) {\n // Nothing left — remove the entire message\n messages.splice(i, 1);\n } else {\n (msg as { content: ContentPart[] }).content = filtered;\n }\n break;\n }\n}\n\n/**\n * Ensure every assistant message with tool_call blocks is immediately followed\n * by a tool message with matching tool_result entries. This prevents Anthropic\n * API 400 errors (\"tool_use ids found without tool_result blocks immediately\n * after\") that can occur after compaction, session restore, or abort recovery.\n *\n * Repairs in-place by inserting synthetic tool_result messages where needed.\n */\nfunction repairToolPairingAdjacent(messages: Message[]): void {\n for (let i = 0; i < messages.length; i++) {\n const msg = messages[i]!;\n if (msg.role !== \"assistant\") continue;\n if (typeof msg.content === \"string\" || !Array.isArray(msg.content)) continue;\n\n const toolCallIds = (msg.content as ContentPart[])\n .filter((p) => p.type === \"tool_call\")\n .map((p) => (p as ContentPart & { type: \"tool_call\"; id: string }).id);\n if (toolCallIds.length === 0) continue;\n\n const next = messages[i + 1];\n if (next?.role === \"tool\" && Array.isArray(next.content)) {\n // Tool message exists — check for missing results\n const existingIds = new Set((next.content as ToolResult[]).map((r) => r.toolCallId));\n const missing = toolCallIds.filter((id) => !existingIds.has(id));\n if (missing.length > 0) {\n for (const id of missing) {\n (next.content as ToolResult[]).push({\n type: \"tool_result\",\n toolCallId: id,\n content: \"Tool execution was interrupted.\",\n isError: true,\n });\n }\n }\n } else {\n // No tool message follows — insert a synthetic one\n messages.splice(i + 1, 0, {\n role: \"tool\" as const,\n content: toolCallIds.map((id) => ({\n type: \"tool_result\" as const,\n toolCallId: id,\n content: \"Tool execution was interrupted.\",\n isError: true,\n })),\n });\n }\n }\n\n // Reverse repair: strip tool_result entries whose tool_use_id has no matching\n // tool_call in the preceding assistant message. This can happen when compaction\n // or stall recovery removes an assistant message but leaves its tool_result behind.\n const toolCallIdSet = new Set<string>();\n for (let i = 0; i < messages.length; i++) {\n const msg = messages[i]!;\n if (msg.role === \"assistant\" && Array.isArray(msg.content)) {\n for (const p of msg.content as ContentPart[]) {\n if (p.type === \"tool_call\") toolCallIdSet.add((p as ToolCall).id);\n }\n }\n if (msg.role === \"tool\" && Array.isArray(msg.content)) {\n const results = msg.content as ToolResult[];\n const filtered = results.filter((r) => toolCallIdSet.has(r.toolCallId));\n if (filtered.length === 0) {\n // Entire tool message is orphaned — remove it\n messages.splice(i, 1);\n i--;\n } else if (filtered.length < results.length) {\n (msg as { content: ToolResult[] }).content = filtered;\n }\n }\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACAA,IAAAA,gBAA0C;;;ACA1C,mBAUO;AAWP,IAAM,oBAAoB;AAW1B,IAAI,UAAqC;AAGlC,SAAS,oBAAoB,IAAqC;AACvE,YAAU;AACZ;AAEA,SAAS,KAAK,OAAe,MAAsC;AACjE,YAAU,OAAO,IAAI;AACvB;AAMO,SAAS,aAAa,KAAuB;AAClD,MAAI,EAAE,eAAe,OAAQ,QAAO;AACpC,MAAI,IAAI,SAAS,aAAc,QAAO;AACtC,QAAM,MAAM,IAAI,QAAQ,YAAY;AACpC,SAAO,IAAI,SAAS,SAAS,KAAK,IAAI,SAAS,OAAO;AACxD;AAQO,SAAS,kBAAkB,KAAuB;AACvD,MAAI,EAAE,eAAe,OAAQ,QAAO;AACpC,MAAI,eAAe,GAAG,EAAG,QAAO;AAChC,QAAM,MAAM,IAAI,QAAQ,YAAY;AACpC,SACE,IAAI,SAAS,oBAAoB,KACjC,IAAI,SA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3C;AAAA,EAEA,IAAI,UAAmB;AACrB,WAAO,KAAK;AAAA,EACd;AAAA;AAAA,EAGA,MAAM,KAAoB;AACxB,SAAK,cAAc,KAAK,GAAG;AAAA,EAC7B;AAAA;AAAA,EAGA,SAAS,KAAoB;AAC3B,SAAK,cAAc,KAAK,GAAG;AAAA,EAC7B;AAAA,EAEA,OAAO,SAA8B;AACnC,QAAI,KAAK,UAAU;AACjB,YAAM,IAAI,MAAM,0BAA0B;AAAA,IAC5C;AACA,SAAK,WAAW;AAEhB,SAAK,SAAS,KAAK,EAAE,MAAM,QAAQ,QAAQ,CAAC;AAE5C,UAAM,oBAAkC;AAAA,MACtC,GAAG,KAAK;AAAA,MACR,qBAAqB,YAAY;AAC/B,cAAM,eAAgB,MAAM,KAAK,QAAQ,sBAAsB,KAAM,CAAC;AACtE,cAAM,SAAS,KAAK,cAAc,OAAO,CAAC;AAC1C,cAAM,MAAM,CAAC,GAAI,gBAAgB,CAAC,GAAI,GAAG,MAAM;AAC/C,eAAO,IAAI,SAAS,IAAI,MAAM;AAAA,MAChC;AAAA,MACA,qBAAqB,YAAY;AAC/B,cAAM,eAAgB,MAAM,KAAK,QAAQ,sBAAsB,KAAM,CAAC;AACtE,cAAM,SAAS,KAAK,cAAc,OAAO,CAAC;AAC1C,cAAM,MAAM,CAAC,GAAI,gBAAgB,CAAC,GAAI,GAAG,MAAM;AAC/C,eAAO,IAAI,SAAS,IAAI,MAAM;AAAA,MAChC;AAAA,IACF;AAEA,UAAM,YAAY,UAAU,KAAK,UAAU,iBAAiB;AAC5D,WAAO,IAAI,YAAY,WAAW,MAAM;AACtC,WAAK,WAAW;AAAA,IAClB,CAAC;AAAA,EACH;AACF;","names":["import_gg_ai"]}
1
+ {"version":3,"sources":["../src/index.ts","../src/agent.ts","../src/agent-loop.ts"],"sourcesContent":["// Core\nexport { Agent, AgentStream } from \"./agent.js\";\nexport {\n agentLoop,\n isAbortError,\n isContextOverflow,\n isBillingError,\n setStreamDiagnostic,\n} from \"./agent-loop.js\";\nexport type { StreamDiagnosticFn } from \"./agent-loop.js\";\n\n// Types\nexport type {\n StructuredToolResult,\n ToolExecuteResult,\n ToolContext,\n AgentTool,\n AgentTextDeltaEvent,\n AgentThinkingDeltaEvent,\n AgentToolCallStartEvent,\n AgentToolCallUpdateEvent,\n AgentToolCallEndEvent,\n AgentToolCallDeltaEvent,\n AgentServerToolCallEvent,\n AgentServerToolResultEvent,\n AgentSteeringMessageEvent,\n AgentFollowUpMessageEvent,\n AgentRetryEvent,\n AgentTurnEndEvent,\n AgentDoneEvent,\n AgentErrorEvent,\n AgentEvent,\n AgentOptions,\n AgentResult,\n} from \"./types.js\";\n","import { EventStream, type Message } from \"@kenkaiiii/gg-ai\";\nimport { agentLoop } from \"./agent-loop.js\";\nimport type { AgentEvent, AgentOptions, AgentResult } from \"./types.js\";\n\n// ── AgentStream ─────────────────────────────────────────────\n\n/**\n * Dual-nature result: async iterable for streaming events,\n * thenable for awaiting the final AgentResult.\n *\n * ```ts\n * // Stream events\n * for await (const event of agent.prompt(\"hello\")) { ... }\n *\n * // Or just await the result\n * const result = await agent.prompt(\"hello\");\n * ```\n */\nexport class AgentStream implements AsyncIterable<AgentEvent> {\n private events: EventStream<AgentEvent>;\n private resultPromise: Promise<AgentResult>;\n private resolveResult!: (r: AgentResult) => void;\n private rejectResult!: (e: Error) => void;\n private hasConsumer = false;\n\n constructor(generator: AsyncGenerator<AgentEvent, AgentResult>, onDone: () => void) {\n this.events = new EventStream<AgentEvent>();\n this.resultPromise = new Promise<AgentResult>((resolve, reject) => {\n this.resolveResult = resolve;\n this.rejectResult = reject;\n });\n this.pump(generator, onDone);\n }\n\n private async pump(\n generator: AsyncGenerator<AgentEvent, AgentResult>,\n onDone: () => void,\n ): Promise<void> {\n try {\n let next = await generator.next();\n while (!next.done) {\n this.events.push(next.value);\n next = await generator.next();\n }\n this.events.close();\n this.resolveResult(next.value);\n } catch (err) {\n const error = err instanceof Error ? err : new Error(String(err));\n this.events.abort(error);\n this.rejectResult(error);\n } finally {\n onDone();\n }\n }\n\n [Symbol.asyncIterator](): AsyncIterator<AgentEvent> {\n this.hasConsumer = true;\n return this.events[Symbol.asyncIterator]();\n }\n\n then<TResult1 = AgentResult, TResult2 = never>(\n onfulfilled?: ((value: AgentResult) => TResult1 | PromiseLike<TResult1>) | null,\n onrejected?: ((reason: unknown) => TResult2 | PromiseLike<TResult2>) | null,\n ): Promise<TResult1 | TResult2> {\n this.drainEvents().catch(() => {});\n return this.resultPromise.then(onfulfilled, onrejected);\n }\n\n private async drainEvents(): Promise<void> {\n if (this.hasConsumer) return;\n this.hasConsumer = true;\n for await (const _ of this.events) {\n // consume silently\n }\n }\n}\n\n// ── Agent ───────────────────────────────────────────────────\n\nexport class Agent {\n private messages: Message[] = [];\n private _running = false;\n private options: AgentOptions;\n private steeringQueue: Message[] = [];\n private followUpQueue: Message[] = [];\n\n constructor(options: AgentOptions) {\n this.options = options;\n if (options.system) {\n this.messages.push({ role: \"system\", content: options.system });\n }\n if (options.priorMessages && options.priorMessages.length > 0) {\n this.messages.push(...options.priorMessages);\n }\n }\n\n /** Snapshot of the current message history. Used for session persistence. */\n getMessages(): Message[] {\n return [...this.messages];\n }\n\n /**\n * Swap the abort signal used for subsequent prompts. Call this after\n * `controller.abort()` so the next prompt() call gets a fresh, unaborted\n * signal — without losing message history.\n */\n setSignal(signal: AbortSignal | undefined): void {\n this.options = { ...this.options, signal };\n }\n\n get running(): boolean {\n return this._running;\n }\n\n /** Queue a steering message for injection after current tool execution completes. */\n steer(msg: Message): void {\n this.steeringQueue.push(msg);\n }\n\n /** Queue a follow-up message for injection when the agent would otherwise stop. */\n followUp(msg: Message): void {\n this.followUpQueue.push(msg);\n }\n\n prompt(content: string): AgentStream {\n if (this._running) {\n throw new Error(\"Agent is already running\");\n }\n this._running = true;\n\n this.messages.push({ role: \"user\", content });\n\n const optionsWithQueues: AgentOptions = {\n ...this.options,\n getSteeringMessages: async () => {\n const callerResult = (await this.options.getSteeringMessages?.()) ?? [];\n const queued = this.steeringQueue.splice(0);\n const all = [...(callerResult ?? []), ...queued];\n return all.length > 0 ? all : null;\n },\n getFollowUpMessages: async () => {\n const callerResult = (await this.options.getFollowUpMessages?.()) ?? [];\n const queued = this.followUpQueue.splice(0);\n const all = [...(callerResult ?? []), ...queued];\n return all.length > 0 ? all : null;\n },\n };\n\n const generator = agentLoop(this.messages, optionsWithQueues);\n return new AgentStream(generator, () => {\n this._running = false;\n });\n }\n}\n","import { ZodError, prettifyError } from \"zod\";\nimport {\n stream,\n EventStream,\n type Message,\n type ToolCall,\n type ToolResult,\n type ToolResultContent,\n type Usage,\n type ContentPart,\n type AssistantMessage,\n} from \"@kenkaiiii/gg-ai\";\nimport type {\n AgentEvent,\n AgentOptions,\n AgentResult,\n AgentTool,\n ToolContext,\n ToolExecuteResult,\n StructuredToolResult,\n} from \"./types.js\";\n\nconst DEFAULT_MAX_TURNS = 200;\n\n/**\n * Lightweight stream diagnostic callback. When set, the agent loop calls this\n * at every phase boundary with timing and state info. This lets the hosting\n * app (ggcoder, come-alive, etc.) log stall diagnostics without the agent\n * package needing fs/process dependencies.\n */\nexport type StreamDiagnosticFn = (phase: string, data?: Record<string, unknown>) => void;\n\n/** Global diagnostic hook — set by the hosting app before calling agentLoop. */\nlet _diagFn: StreamDiagnosticFn | null = null;\n\n/** Register a diagnostic callback for stream stall tracing. */\nexport function setStreamDiagnostic(fn: StreamDiagnosticFn | null): void {\n _diagFn = fn;\n}\n\nfunction diag(phase: string, data?: Record<string, unknown>): void {\n _diagFn?.(phase, data);\n}\n\n/**\n * Detect abort errors — user-initiated cancellation or AbortSignal.\n * These should be caught and handled gracefully, not re-thrown.\n */\nexport function isAbortError(err: unknown): boolean {\n if (!(err instanceof Error)) return false;\n if (err.name === \"AbortError\") return true;\n const msg = err.message.toLowerCase();\n return msg.includes(\"aborted\") || msg.includes(\"abort\");\n}\n\n/**\n * Detect context window overflow errors from LLM providers.\n *\n * Patterns drawn from observed errors across Anthropic, OpenAI, OpenAI Codex,\n * Bedrock, Ollama, and OpenAI-compatible Chinese providers (GLM, Kimi, MiniMax).\n */\nexport function isContextOverflow(err: unknown): boolean {\n if (!(err instanceof Error)) return false;\n if (isBillingError(err)) return false;\n const msg = err.message.toLowerCase();\n return (\n msg.includes(\"prompt is too long\") ||\n msg.includes(\"prompt too long\") ||\n msg.includes(\"input is too long\") ||\n msg.includes(\"context_length_exceeded\") ||\n msg.includes(\"context_window_exceeded\") ||\n msg.includes(\"maximum context length\") ||\n msg.includes(\"exceeds model context window\") ||\n msg.includes(\"exceeds the context window\") ||\n msg.includes(\"content_too_large\") ||\n msg.includes(\"request_too_large\") ||\n msg.includes(\"reduce the length\") ||\n msg.includes(\"please shorten\") ||\n (msg.includes(\"token\") && msg.includes(\"exceed\"))\n );\n}\n\n/**\n * Detect billing/quota errors — these should NOT be retried.\n * GLM returns HTTP 429 with \"Insufficient balance\" for quota exhaustion.\n */\nexport function isBillingError(err: unknown): boolean {\n if (!(err instanceof Error)) return false;\n const msg = err.message.toLowerCase();\n return (\n msg.includes(\"insufficient balance\") ||\n msg.includes(\"no resource package\") ||\n msg.includes(\"quota exceeded\") ||\n msg.includes(\"billing\") ||\n msg.includes(\"recharge\") ||\n msg.includes(\"subscription plan\") ||\n msg.includes(\"does not yet include access\") ||\n msg.includes(\"token quota\") ||\n msg.includes(\"exceeded_current_quota_error\") ||\n msg.includes(\"check your account balance\")\n );\n}\n\n/**\n * Detect overloaded/rate-limit errors from LLM providers.\n * HTTP 429 (rate limit) or 529/503 (overloaded).\n * Excludes billing/quota errors which won't resolve with a retry.\n */\n/**\n * Detect tool pairing errors — orphaned tool_use or tool_result blocks.\n * These are 400 errors that can be recovered by repairing the message history.\n */\nexport function isToolPairingError(err: unknown): boolean {\n if (!(err instanceof Error)) return false;\n const msg = err.message.toLowerCase();\n return (\n (msg.includes(\"tool_use\") && msg.includes(\"tool_result\")) ||\n msg.includes(\"unexpected `tool_use_id`\") ||\n msg.includes(\"tool_use ids found without\") ||\n // Moonshot/OpenAI-compatible: \"tool call id <id> is not found\"\n (msg.includes(\"tool call id\") && msg.includes(\"is not found\"))\n );\n}\n\n/**\n * Distinguish rate-limit (HTTP 429) from server-side overload (HTTP 529).\n * Returns null for errors that should not enter the overload-retry bucket.\n * Both kinds use the same backoff schedule, but the UI shows different copy\n * and the log line records the true cause.\n */\nexport function classifyOverload(err: unknown): \"rate_limit\" | \"overloaded\" | null {\n if (!(err instanceof Error)) return null;\n if (isBillingError(err)) return null;\n const msg = err.message.toLowerCase();\n if (\n msg.includes(\"rate_limit\") ||\n msg.includes(\"rate limit\") ||\n msg.includes(\"too many requests\") ||\n msg.includes(\"429\")\n ) {\n return \"rate_limit\";\n }\n if (msg.includes(\"overloaded\") || msg.includes(\"529\")) {\n return \"overloaded\";\n }\n return null;\n}\n\nexport function isOverloaded(err: unknown): boolean {\n return classifyOverload(err) !== null;\n}\n\n/**\n * Detect malformed-stream errors — the SDK's SSE decoder threw a JSON parse\n * error mid-stream, typically because a chunk was truncated or corrupted by\n * an intermediary (CDN, proxy). Same class of transport failure as a stall:\n * replaying the request — and ideally flipping to non-streaming — recovers.\n */\nexport function isMalformedStream(err: unknown): boolean {\n if (!(err instanceof Error)) return false;\n if (err.name === \"SyntaxError\") return true;\n const cause = (err as { cause?: unknown }).cause;\n if (cause instanceof Error && cause.name === \"SyntaxError\") return true;\n const msg = err.message;\n // V8 JSON.parse error messages: \"Expected ... in JSON at position N\"\n // and \"Unexpected token ... in JSON at position N\"\n return /\\bin JSON at position \\d+/i.test(msg);\n}\n\n/**\n * Detect socket-level transport failures — the remote peer (or an\n * intermediary) closed the TCP connection mid-stream before the response\n * finished. Surfaces as `TypeError: terminated` from undici/fetch, or as\n * `ECONNRESET` / `socket hang up` / `UND_ERR_SOCKET` from the underlying\n * Node http layer. Undici nests the real cause one or more levels deep,\n * so we walk the `.cause` chain. Same recovery as a stall: replay the\n * request, optionally as non-streaming.\n */\nexport function isTransportFailure(err: unknown): boolean {\n const codes = new Set([\n \"ECONNRESET\",\n \"ECONNREFUSED\",\n \"ECONNABORTED\",\n \"ETIMEDOUT\",\n \"EPIPE\",\n \"EHOSTUNREACH\",\n \"ENETUNREACH\",\n \"ENOTFOUND\",\n \"UND_ERR_SOCKET\",\n \"UND_ERR_CONNECT_TIMEOUT\",\n \"UND_ERR_HEADERS_TIMEOUT\",\n \"UND_ERR_BODY_TIMEOUT\",\n \"UND_ERR_RESPONSE_STATUS_CODE\",\n \"UND_ERR_REQ_CONTENT_LENGTH_MISMATCH\",\n \"UND_ERR_RES_CONTENT_LENGTH_MISMATCH\",\n ]);\n const messages = [\n /^terminated$/i,\n /\\bother side closed\\b/i,\n /\\bsocket hang up\\b/i,\n /\\bfetch failed\\b/i,\n /\\bbody timeout error\\b/i,\n /\\bsse stream disconnected\\b/i,\n /\\bfailed to reconnect sse stream\\b/i,\n ];\n const seen = new Set<unknown>();\n let cur: unknown = err;\n while (cur && typeof cur === \"object\" && !seen.has(cur)) {\n seen.add(cur);\n const e = cur as { code?: unknown; message?: unknown; cause?: unknown };\n if (typeof e.code === \"string\" && codes.has(e.code)) return true;\n if (typeof e.message === \"string\") {\n for (const re of messages) if (re.test(e.message)) return true;\n }\n cur = e.cause;\n }\n return false;\n}\n\n/**\n * Promise-returning sleep that rejects with AbortError if `signal` fires.\n * Used by retry backoffs so ESC/Ctrl+C cancel immediately instead of having\n * to wait out the full delay (up to 30s per overload retry × 10 retries).\n */\nfunction abortableSleep(ms: number, signal?: AbortSignal): Promise<void> {\n if (signal?.aborted) {\n return Promise.reject(new DOMException(\"Aborted\", \"AbortError\"));\n }\n return new Promise<void>((resolve, reject) => {\n let onAbort: (() => void) | null = null;\n const timer = setTimeout(() => {\n if (onAbort) signal?.removeEventListener(\"abort\", onAbort);\n resolve();\n }, ms);\n onAbort = () => {\n clearTimeout(timer);\n reject(new DOMException(\"Aborted\", \"AbortError\"));\n };\n signal?.addEventListener(\"abort\", onAbort, { once: true });\n });\n}\n\nexport async function* agentLoop(\n messages: Message[],\n options: AgentOptions,\n): AsyncGenerator<AgentEvent, AgentResult> {\n const maxTurns = options.maxTurns ?? DEFAULT_MAX_TURNS;\n const maxContinuations = options.maxContinuations ?? 5;\n const toolMap = new Map<string, AgentTool>((options.tools ?? []).map((t) => [t.name, t]));\n\n const totalUsage: Usage = { inputTokens: 0, outputTokens: 0 };\n let turn = 0;\n let firstTurn = true;\n let consecutivePauses = 0;\n let toolPairingRepaired = false;\n let overloadRetries = 0;\n let emptyResponseRetries = 0;\n let stallRetries = 0;\n let overflowCompactionAttempts = 0;\n // Non-streaming fallback mode. After repeated stream stalls, flip to a\n // plain non-streaming request/response -- often survives broken SSE\n // connections (transient CDN / proxy issues) that streaming retries cannot.\n let useNonStreamingFallback = false;\n const MAX_OVERLOAD_RETRIES = 10;\n const MAX_EMPTY_RESPONSE_RETRIES = 2;\n const MAX_STALL_RETRIES = 5;\n const MAX_OVERFLOW_COMPACTIONS = 2;\n // After this many streaming stalls in a row, switch to non-streaming mode\n // for the remaining stall retries. Keeps the first two retries fast (the\n // cheap \"transient glitch\" case) before paying for a full response round-trip.\n const STALL_RETRIES_BEFORE_NON_STREAMING = 2;\n const STALL_DELAY_MS = 1_000; // Brief pause before retry -- just enough to avoid tight loops\n const OVERLOAD_BASE_DELAY_MS = 2_000;\n const OVERLOAD_MAX_DELAY_MS = 30_000;\n const STREAM_FIRST_EVENT_TIMEOUT_MS = 45_000; // 45s to get first event (Opus thinks long)\n const STREAM_IDLE_TIMEOUT_MS = 30_000; // 30s between events once streaming starts\n // Anthropic models can pause 10-20s mid-stream while computing the next chunk\n // (e.g. generating tool call args for a large write). 10s was too aggressive\n // and caused false \"stream stalled\" errors, especially in plan mode.\n const STREAM_HARD_TIMEOUT_MS = 90_000; // 90s absolute cap before output starts\n // Once output events (text_delta) are actively streaming, extend the hard\n // timeout -- long responses (plan mode, detailed explanations) can legitimately\n // take 2-3+ minutes while events flow continuously.\n const STREAM_OUTPUT_HARD_TIMEOUT_MS = 300_000; // 5min hard cap once output is flowing\n // Reasoning models (MiMo) can pause 3-5 minutes between thinking and output\n // generation. Once we've seen thinking events, extend timeouts significantly.\n const STREAM_THINKING_IDLE_TIMEOUT_MS = 300_000; // 5min idle after thinking\n const STREAM_THINKING_HARD_TIMEOUT_MS = 600_000; // 10min hard cap with thinking\n // Non-streaming mode has no per-event idle -- the entire response arrives in\n // one HTTP round-trip. Use a single generous hard cap instead. This matches\n // Claude Code's v2.1.110/111 behaviour: cap non-streaming retries so API\n // unreachability doesn't cause multi-minute hangs, but not so aggressively\n // that slow-but-healthy backends get killed.\n const NON_STREAMING_HARD_TIMEOUT_MS = 300_000; // 5min for full non-streaming response\n // Runaway tool-call circuit breaker. When a model glitches mid-tool-call it\n // can emit tens of thousands of toolcall_delta events without ever closing,\n // burning the entire stall-retry budget (~25 min) on what is clearly a\n // non-recoverable model error. Cap accumulated arg chars and event count;\n // exceeding either is a hard, non-retriable failure. Thresholds are generous\n // enough to allow legitimate large file writes through `write`.\n const MAX_TOOLCALL_DELTA_CHARS = 1_000_000; // 1 MB of accumulated tool-call args\n const MAX_TOOLCALL_DELTA_EVENTS = 20_000; // 20k delta events in one stream\n\n try {\n while (turn < maxTurns) {\n options.signal?.throwIfAborted();\n turn++;\n\n // Estimate message payload size for diagnostics\n let msgChars = 0;\n for (const m of messages) {\n if (typeof m.content === \"string\") msgChars += m.content.length;\n else if (Array.isArray(m.content)) {\n for (const p of m.content) {\n if (\"text\" in p && typeof p.text === \"string\") msgChars += p.text.length;\n if (\"content\" in p && typeof p.content === \"string\") msgChars += p.content.length;\n }\n }\n }\n diag(\"turn_start\", {\n turn,\n messages: messages.length,\n chars: msgChars,\n provider: options.provider,\n model: options.model,\n });\n\n // ── Initial steering poll: catch messages queued before the first LLM call ──\n if (firstTurn && options.getSteeringMessages) {\n const steering = await options.getSteeringMessages();\n if (steering && steering.length > 0) {\n for (const msg of steering) {\n yield { type: \"steering_message\" as const, content: msg.content };\n messages.push(msg);\n }\n }\n }\n firstTurn = false;\n\n // ── Mid-loop context transform (compaction / truncation) ──\n if (options.transformContext) {\n diag(\"transform_start\");\n const transformed = await options.transformContext(messages);\n if (transformed !== messages) {\n diag(\"transform_compacted\", {\n before: messages.length,\n after: transformed.length,\n });\n messages.length = 0;\n messages.push(...transformed);\n }\n diag(\"transform_end\");\n }\n\n // ── Repair tool pairing: ensure every tool_use has an adjacent tool_result ──\n repairToolPairingAdjacent(messages);\n\n // ── Call LLM with overflow recovery ──\n let response;\n // Per-attempt abort controller: allows idle timeout to abort the stream\n // without affecting the caller's signal. The caller's abort is forwarded.\n const streamController = new AbortController();\n let idleTimer: ReturnType<typeof setTimeout> | null = null;\n let hardTimer: ReturnType<typeof setTimeout> | null = null;\n let idleTimedOut = false;\n\n // Stream event counters — declared here so timeout callbacks can access them\n let streamEventCount = 0;\n let lastEventTime = Date.now();\n let streamCallStart = Date.now();\n // Track event types for diagnostics — shows what arrived before a stall\n const eventTypeCounts: Record<string, number> = {};\n let lastEventType = \"\";\n // Runaway tool-call detection — accumulated across all toolcall_delta\n // events in this stream attempt. When tripped we abort the stream and\n // bail out without retrying (the model has glitched, retries won't help).\n let toolcallDeltaChars = 0;\n let toolcallDeltaCount = 0;\n let runawayDetected: { kind: \"chars\" | \"events\"; chars: number; events: number } | null =\n null;\n // Track consumer processing time — helps distinguish \"API stopped sending\"\n // from \"our consumer was slow to pull the next event\"\n let lastYieldEndTime = Date.now();\n let maxConsumerLagMs = 0;\n\n // Forward caller abort to the per-attempt controller\n const forwardAbort = () => streamController.abort();\n options.signal?.addEventListener(\"abort\", forwardAbort, { once: true });\n\n // Three-phase idle timeout:\n // - Before first event: STREAM_FIRST_EVENT_TIMEOUT_MS (45s) -- Opus can\n // take 30s+ to start on large contexts, that's not a stall.\n // - After output event (text_delta, server_toolcall): STREAM_IDLE_TIMEOUT_MS\n // (10s) -- once output is streaming, 10s of silence is dead. Retry fast.\n // - After thinking events only: STREAM_THINKING_IDLE_TIMEOUT_MS (5min) --\n // reasoning models (MiMo) can pause minutes between thinking and output.\n //\n // In non-streaming fallback mode the entire response arrives in a single\n // HTTP round-trip, so the idle timer is disabled -- only the hard timeout\n // applies. Synthesized events all arrive at once when the response returns.\n let hasReceivedEvent = false;\n let hasReceivedThinking = false;\n const resetIdleTimer = () => {\n if (useNonStreamingFallback) return; // no inter-event idle in non-streaming mode\n if (idleTimer) clearTimeout(idleTimer);\n const timeoutMs = hasReceivedEvent\n ? STREAM_IDLE_TIMEOUT_MS\n : hasReceivedThinking\n ? STREAM_THINKING_IDLE_TIMEOUT_MS\n : STREAM_FIRST_EVENT_TIMEOUT_MS;\n idleTimer = setTimeout(() => {\n diag(\"idle_timeout_fired\", {\n events: streamEventCount,\n sinceLastEventMs: Date.now() - lastEventTime,\n lastEventType,\n maxConsumerLagMs,\n phase: hasReceivedEvent\n ? \"mid_stream\"\n : hasReceivedThinking\n ? \"post_thinking\"\n : \"first_event\",\n eventTypes: eventTypeCounts,\n });\n idleTimedOut = true;\n streamController.abort();\n }, timeoutMs);\n };\n\n // Hard timeout: absolute cap per LLM call. Safety net for streams that\n // keep sending sparse events (e.g. keep-alive pings) but never complete.\n // Extended dynamically when thinking events arrive (see thinking_delta handler).\n // Non-streaming fallback uses a single larger cap since there's no stream\n // to observe -- just wait for the full response up to the cap.\n let hardTimeoutMs = useNonStreamingFallback\n ? NON_STREAMING_HARD_TIMEOUT_MS\n : STREAM_HARD_TIMEOUT_MS;\n hardTimer = setTimeout(() => {\n diag(\"hard_timeout_fired\", {\n events: typeof streamEventCount !== \"undefined\" ? streamEventCount : 0,\n nonStreaming: useNonStreamingFallback,\n });\n idleTimedOut = true;\n streamController.abort();\n }, hardTimeoutMs);\n\n try {\n diag(\"stream_call\", { nonStreaming: useNonStreamingFallback });\n streamCallStart = Date.now();\n const result = stream({\n provider: options.provider,\n model: options.model,\n messages,\n tools: options.tools,\n serverTools: options.serverTools,\n webSearch: options.webSearch,\n maxTokens: options.maxTokens,\n temperature: options.temperature,\n thinking: options.thinking,\n apiKey: options.apiKey,\n baseUrl: options.baseUrl,\n signal: streamController.signal,\n accountId: options.accountId,\n cacheRetention: options.cacheRetention,\n promptCacheKey: options.promptCacheKey,\n serviceTier: options.serviceTier,\n supportsImages: options.supportsImages,\n compaction: options.compaction,\n clearToolUses: options.clearToolUses,\n userAgent: options.userAgent,\n // Flip to non-streaming fallback after repeated stream stalls.\n ...(useNonStreamingFallback ? { streaming: false } : {}),\n });\n diag(\"stream_created\", { setupMs: Date.now() - streamCallStart });\n\n // Suppress unhandled rejection if the iterator path throws first\n result.response.catch(() => {});\n\n // Forward streaming deltas — reset idle timer on each event\n streamEventCount = 0;\n hasReceivedEvent = false;\n lastEventTime = Date.now();\n streamCallStart = Date.now();\n // Reset to streamCallStart so the first event's consumerLag reflects\n // network/provider latency, not the time spent before stream() returned.\n lastYieldEndTime = Date.now();\n resetIdleTimer();\n for await (const event of result) {\n // Measure consumer lag: time between finishing previous yield and\n // receiving this event. For event #1 this still includes network/\n // provider latency; for subsequent events it isolates how long\n // React/UI rendering held up the next pull.\n const pullTime = Date.now();\n const consumerLag = pullTime - lastYieldEndTime;\n // Only track mid-stream lag — first event lag is dominated by\n // server-side TTFB and would mask real UI starvation issues.\n if (streamEventCount > 0 && consumerLag > maxConsumerLagMs) {\n maxConsumerLagMs = consumerLag;\n }\n\n streamEventCount++;\n eventTypeCounts[event.type] = (eventTypeCounts[event.type] ?? 0) + 1;\n lastEventType = event.type;\n\n // Flip to mid-stream timeout on confirmed output events — text\n // deltas, completed tool calls, and tool call deltas (large file\n // writes can stream toolcall_delta for minutes without any text_delta).\n // Reasoning models (MiMo) are handled separately below — they can\n // stream hundreds of thinking events then pause minutes before output.\n if (\n (event.type === \"text_delta\" ||\n event.type === \"server_toolcall\" ||\n event.type === \"toolcall_delta\") &&\n !hasReceivedEvent\n ) {\n hasReceivedEvent = true;\n // Extend hard timeout now that output is actively streaming.\n // Long responses (plan mode, detailed code) can exceed 90s while\n // events flow continuously — the idle timeout (10s) catches real stalls.\n if (hardTimer && hardTimeoutMs < STREAM_OUTPUT_HARD_TIMEOUT_MS) {\n clearTimeout(hardTimer);\n hardTimeoutMs = STREAM_OUTPUT_HARD_TIMEOUT_MS;\n hardTimer = setTimeout(() => {\n diag(\"hard_timeout_fired\", { events: streamEventCount });\n idleTimedOut = true;\n streamController.abort();\n }, hardTimeoutMs);\n }\n }\n // Track thinking events — extends idle timeout and hard timeout\n // so reasoning models aren't killed during thinking→output transition.\n if (event.type === \"thinking_delta\" && !hasReceivedThinking) {\n hasReceivedThinking = true;\n // Extend the hard timeout now that we know the model is reasoning\n if (hardTimer) clearTimeout(hardTimer);\n hardTimeoutMs = STREAM_THINKING_HARD_TIMEOUT_MS;\n hardTimer = setTimeout(() => {\n diag(\"hard_timeout_fired\", { events: streamEventCount });\n idleTimedOut = true;\n streamController.abort();\n }, hardTimeoutMs);\n }\n\n const now = Date.now();\n const gap = now - lastEventTime;\n // Log first event and any suspiciously long gaps\n if (streamEventCount === 1) {\n diag(\"first_event\", { type: event.type, ttfMs: now - streamCallStart });\n } else if (gap > 3000) {\n diag(\"slow_gap\", {\n type: event.type,\n gapMs: gap,\n eventNum: streamEventCount,\n sinceStartMs: now - streamCallStart,\n });\n }\n lastEventTime = now;\n resetIdleTimer();\n if (event.type === \"text_delta\") {\n yield { type: \"text_delta\" as const, text: event.text };\n } else if (event.type === \"thinking_delta\") {\n yield { type: \"thinking_delta\" as const, text: event.text };\n } else if (event.type === \"server_toolcall\") {\n yield {\n type: \"server_tool_call\" as const,\n id: event.id,\n name: event.name,\n input: event.input,\n };\n } else if (event.type === \"server_toolresult\") {\n yield {\n type: \"server_tool_result\" as const,\n toolUseId: event.toolUseId,\n resultType: event.resultType,\n data: event.data,\n };\n } else if (event.type === \"toolcall_delta\") {\n const chunkChars = event.argsJson?.length ?? 0;\n toolcallDeltaChars += chunkChars;\n toolcallDeltaCount++;\n if (\n !runawayDetected &&\n (toolcallDeltaChars > MAX_TOOLCALL_DELTA_CHARS ||\n toolcallDeltaCount > MAX_TOOLCALL_DELTA_EVENTS)\n ) {\n runawayDetected = {\n kind: toolcallDeltaChars > MAX_TOOLCALL_DELTA_CHARS ? \"chars\" : \"events\",\n chars: toolcallDeltaChars,\n events: toolcallDeltaCount,\n };\n diag(\"runaway_toolcall_detected\", {\n ...runawayDetected,\n provider: options.provider,\n model: options.model,\n });\n streamController.abort();\n }\n yield {\n type: \"toolcall_delta\" as const,\n chars: chunkChars,\n };\n }\n lastYieldEndTime = Date.now();\n }\n\n diag(\"stream_done\", {\n events: streamEventCount,\n totalMs: Date.now() - streamCallStart,\n maxConsumerLagMs,\n eventTypes: eventTypeCounts,\n });\n response = await result.response;\n } catch (err) {\n const errMsg = err instanceof Error ? err.message : String(err);\n diag(\"stream_error\", {\n error: errMsg.slice(0, 200),\n events: streamEventCount,\n totalMs: Date.now() - streamCallStart,\n idleTimedOut,\n aborted: !!options.signal?.aborted,\n eventTypes: eventTypeCounts,\n provider: options.provider,\n model: options.model,\n });\n // Context overflow: try a forced compaction before giving up.\n // The pre-turn transformContext check uses estimated tokens, which can\n // underestimate code-heavy content. When the API confirms overflow we\n // compact unconditionally and retry the turn, capped at\n // MAX_OVERFLOW_COMPACTIONS to avoid loops when compaction can't reduce\n // enough (e.g. single huge user message).\n if (isContextOverflow(err)) {\n if (options.transformContext && overflowCompactionAttempts < MAX_OVERFLOW_COMPACTIONS) {\n overflowCompactionAttempts++;\n diag(\"overflow_compact_start\", {\n attempt: overflowCompactionAttempts,\n maxAttempts: MAX_OVERFLOW_COMPACTIONS,\n messages: messages.length,\n });\n try {\n const compacted = await options.transformContext(messages, { force: true });\n if (compacted !== messages && compacted.length < messages.length) {\n messages.length = 0;\n messages.push(...compacted);\n diag(\"overflow_compact_success\", {\n attempt: overflowCompactionAttempts,\n messages: messages.length,\n });\n yield {\n type: \"retry\" as const,\n reason: \"overflow_compact\" as const,\n attempt: overflowCompactionAttempts,\n maxAttempts: MAX_OVERFLOW_COMPACTIONS,\n delayMs: 0,\n };\n turn--;\n continue;\n }\n diag(\"overflow_compact_noop\", {\n attempt: overflowCompactionAttempts,\n before: messages.length,\n after: compacted.length,\n });\n } catch (compactErr) {\n diag(\"overflow_compact_failed\", {\n error: compactErr instanceof Error ? compactErr.message : String(compactErr),\n });\n }\n }\n yield { type: \"error\" as const, error: err instanceof Error ? err : new Error(errMsg) };\n throw err;\n }\n // Overloaded / rate-limited: exponential backoff, retry up to 10 times\n const overloadKind = classifyOverload(err);\n if (overloadRetries < MAX_OVERLOAD_RETRIES && overloadKind) {\n overloadRetries++;\n const delayMs = Math.min(\n OVERLOAD_BASE_DELAY_MS * 2 ** (overloadRetries - 1),\n OVERLOAD_MAX_DELAY_MS,\n );\n diag(\"retry\", {\n reason: overloadKind,\n attempt: overloadRetries,\n maxAttempts: MAX_OVERLOAD_RETRIES,\n delayMs,\n });\n yield {\n type: \"retry\" as const,\n reason: overloadKind,\n attempt: overloadRetries,\n maxAttempts: MAX_OVERLOAD_RETRIES,\n delayMs,\n };\n await abortableSleep(delayMs, options.signal);\n turn--; // Don't count the failed turn\n continue;\n }\n // Stream stall: the API connection hung without closing.\n // Malformed stream: the SDK's SSE decoder hit truncated/corrupted JSON.\n // Both are transport failures — retry with exponential backoff and flip\n // to non-streaming mode after STALL_RETRIES_BEFORE_NON_STREAMING attempts,\n // since broken SSE often recovers when replayed as plain HTTP.\n // Runaway tool-call: the model never closed a tool-call block and\n // blew past the size/count caps. Retrying just reproduces the loop,\n // so surface a clear error and stop. Checked before the abort branch\n // since we ourselves aborted the stream to break the runaway.\n if (runawayDetected) {\n diag(\"runaway_toolcall_aborted\", {\n ...runawayDetected,\n provider: options.provider,\n model: options.model,\n });\n const detail =\n runawayDetected.kind === \"chars\"\n ? `${(runawayDetected.chars / 1024).toFixed(0)} KB of tool-call arguments`\n : `${runawayDetected.events} tool-call delta events`;\n yield {\n type: \"error\" as const,\n error: new Error(\n `The model glitched mid-tool-call and produced ${detail} without closing the call. ` +\n `This is usually an upstream model bug — try the same request again or switch models. ` +\n `Your conversation is preserved.`,\n ),\n };\n break;\n }\n const malformed = isMalformedStream(err);\n const socketDrop = isTransportFailure(err);\n const transportFailure =\n (idleTimedOut || malformed || socketDrop) && !options.signal?.aborted;\n if (transportFailure && stallRetries < MAX_STALL_RETRIES) {\n stallRetries++;\n const cause = malformed\n ? \"malformed_stream\"\n : socketDrop\n ? \"socket_drop\"\n : \"stream_stall\";\n if (!useNonStreamingFallback && stallRetries >= STALL_RETRIES_BEFORE_NON_STREAMING) {\n useNonStreamingFallback = true;\n diag(\"non_streaming_fallback_enabled\", {\n stallRetries,\n provider: options.provider,\n model: options.model,\n cause,\n });\n }\n const delayMs = Math.min(STALL_DELAY_MS * 2 ** (stallRetries - 1), 8_000);\n diag(\"retry\", {\n reason: cause,\n attempt: stallRetries,\n maxAttempts: MAX_STALL_RETRIES,\n delayMs,\n events: streamEventCount,\n nonStreaming: useNonStreamingFallback,\n });\n yield {\n type: \"retry\" as const,\n reason: \"stream_stall\" as const,\n attempt: stallRetries,\n maxAttempts: MAX_STALL_RETRIES,\n delayMs,\n silent: stallRetries <= 2,\n };\n await abortableSleep(delayMs, options.signal);\n turn--; // Don't count the failed turn\n continue;\n }\n // Stream stall retries exhausted — surface a clear error so the UI\n // can distinguish \"gave up after stalls\" from \"completed normally\".\n if (transportFailure) {\n diag(\"stall_exhausted\", {\n stallRetries: MAX_STALL_RETRIES,\n provider: options.provider,\n model: options.model,\n });\n yield {\n type: \"error\" as const,\n error: new Error(\n `The API provider's stream stalled ${MAX_STALL_RETRIES} times — the provider may be experiencing capacity issues. ` +\n `Your conversation is preserved. Send another message to retry.`,\n ),\n };\n break;\n }\n // Tool pairing 400: orphaned tool_result or tool_use in message history.\n // Run repair and retry once — if repair can't fix it, surface the error.\n if (isToolPairingError(err) && !toolPairingRepaired) {\n toolPairingRepaired = true;\n diag(\"tool_pairing_repair\", { error: errMsg.slice(0, 200) });\n repairToolPairingAdjacent(messages);\n turn--;\n continue;\n }\n // Abort errors (user cancellation) — exit loop cleanly instead of\n // crashing the process with an unhandled rejection.\n if (isAbortError(err) || options.signal?.aborted) {\n diag(\"aborted\", { turn, provider: options.provider, model: options.model });\n break;\n }\n // Unhandled error — log before throwing so the crash is traceable\n diag(\"unhandled_error\", {\n error: errMsg.slice(0, 500),\n turn,\n provider: options.provider,\n model: options.model,\n });\n throw err;\n } finally {\n if (idleTimer) clearTimeout(idleTimer);\n if (hardTimer) clearTimeout(hardTimer);\n options.signal?.removeEventListener(\"abort\", forwardAbort);\n }\n\n overloadRetries = 0;\n stallRetries = 0;\n\n // Detect empty/degenerate responses — the API occasionally returns 0 tokens\n // with no content, or \"thinks\" without producing actionable output.\n // Reasoning models (MiMo, DeepSeek) may report outputTokens > 0 from\n // thinking alone while producing no text or tool calls — still a dud.\n const contentArr = Array.isArray(response.message.content) ? response.message.content : null;\n const hasActionableContent =\n response.message.content !== \"\" &&\n contentArr !== null &&\n contentArr.some(\n (p) => p.type === \"text\" || p.type === \"tool_call\" || p.type === \"server_tool_call\",\n );\n if (!hasActionableContent) {\n if (emptyResponseRetries < MAX_EMPTY_RESPONSE_RETRIES) {\n emptyResponseRetries++;\n diag(\"retry\", {\n reason: \"empty_response\",\n attempt: emptyResponseRetries,\n maxAttempts: MAX_EMPTY_RESPONSE_RETRIES,\n provider: options.provider,\n model: options.model,\n contentTypes: contentArr?.map((p) => p.type).join(\",\") ?? \"empty\",\n });\n yield {\n type: \"retry\" as const,\n reason: \"empty_response\" as const,\n attempt: emptyResponseRetries,\n maxAttempts: MAX_EMPTY_RESPONSE_RETRIES,\n delayMs: 0,\n };\n turn--; // Don't count the failed turn — keep useNonStreamingFallback set\n // so the retry doesn't bounce back into a streaming connection that\n // will stall again with the same upstream problem.\n continue;\n }\n // Exhausted retries — fall through and let the agent finish\n }\n emptyResponseRetries = 0;\n\n // Only clear the non-streaming fallback after an actionable response —\n // an empty non-streaming reply means the upstream issue hasn't resolved,\n // so staying in non-streaming mode avoids retrying into another stall.\n useNonStreamingFallback = false;\n\n // Accumulate usage\n totalUsage.inputTokens += response.usage.inputTokens;\n totalUsage.outputTokens += response.usage.outputTokens;\n if (response.usage.cacheRead) {\n totalUsage.cacheRead = (totalUsage.cacheRead ?? 0) + response.usage.cacheRead;\n }\n if (response.usage.cacheWrite) {\n totalUsage.cacheWrite = (totalUsage.cacheWrite ?? 0) + response.usage.cacheWrite;\n }\n\n // Append assistant message to conversation\n messages.push(response.message);\n\n yield {\n type: \"turn_end\" as const,\n turn,\n stopReason: response.stopReason,\n usage: response.usage,\n };\n\n // Server-side tool hit iteration limit — re-send to continue.\n // Do NOT add an extra user message; the API detects the trailing\n // server_tool_use block and resumes automatically.\n if (response.stopReason === \"pause_turn\") {\n consecutivePauses++;\n if (consecutivePauses >= maxContinuations) {\n break; // Safety limit — fall through to agent_done below\n }\n continue;\n }\n consecutivePauses = 0;\n\n // Extract tool calls — separate client-executed from provider built-in (e.g. Moonshot $web_search)\n const allToolCalls = extractToolCalls(response.message.content);\n\n // If no tool calls to execute, check for steering messages before stopping.\n // Check content (not just stopReason) because some providers (e.g. GLM)\n // return finish_reason=\"stop\" even when tool calls are present.\n if (response.stopReason !== \"tool_use\" && allToolCalls.length === 0) {\n // Check for queued steering messages — if present, inject and continue\n // the loop instead of returning (follow-up pattern).\n if (options.getSteeringMessages) {\n const steering = await options.getSteeringMessages();\n if (steering && steering.length > 0) {\n for (const msg of steering) {\n yield { type: \"steering_message\" as const, content: msg.content };\n messages.push(msg);\n }\n continue; // Next iteration will call LLM with injected messages\n }\n }\n // Follow-up: lower priority than steering — only when agent would otherwise stop.\n if (options.getFollowUpMessages) {\n const followUp = await options.getFollowUpMessages();\n if (followUp && followUp.length > 0) {\n for (const msg of followUp) {\n yield { type: \"follow_up_message\" as const, content: msg.content };\n messages.push(msg);\n }\n continue;\n }\n }\n yield {\n type: \"agent_done\" as const,\n totalTurns: turn,\n totalUsage: { ...totalUsage },\n };\n return {\n message: response.message,\n totalTurns: turn,\n totalUsage: { ...totalUsage },\n };\n }\n const toolCalls: ToolCall[] = [];\n const toolResults: ToolResult[] = [];\n\n for (const tc of allToolCalls) {\n if (tc.name.startsWith(\"$\")) {\n // Provider built-in tool (e.g. Moonshot $web_search) — not locally executed.\n // Still needs a tool_result for the message history round-trip.\n toolResults.push({\n type: \"tool_result\",\n toolCallId: tc.id,\n content: JSON.stringify(tc.args),\n });\n } else {\n toolCalls.push(tc);\n }\n }\n const eventStream = new EventStream<AgentEvent>();\n\n // Launch all tool calls in parallel\n const executions = toolCalls.map(async (toolCall) => {\n const startTime = Date.now();\n\n eventStream.push({\n type: \"tool_call_start\" as const,\n toolCallId: toolCall.id,\n name: toolCall.name,\n args: toolCall.args,\n });\n\n let resultContent: ToolResultContent;\n let details: unknown;\n let isError = false;\n\n const tool = toolMap.get(toolCall.name);\n if (!tool) {\n resultContent = `Unknown tool: ${toolCall.name}`;\n isError = true;\n } else {\n try {\n const parsed = tool.parameters.parse(toolCall.args);\n const ctx: ToolContext = {\n signal: options.signal ?? AbortSignal.timeout(300_000),\n toolCallId: toolCall.id,\n onUpdate: (update: unknown) => {\n eventStream.push({\n type: \"tool_call_update\" as const,\n toolCallId: toolCall.id,\n update,\n });\n },\n };\n const raw = await tool.execute(parsed, ctx);\n const normalized = normalizeToolResult(raw);\n resultContent = normalized.content;\n details = normalized.details;\n } catch (err) {\n isError = true;\n if (err instanceof ZodError) {\n // Zod v4's default `.message` is a JSON dump of `.issues`, which\n // the model can't act on. Prettify into \"field X: expected Y,\n // received Z\" lines so the next call comes back with valid args.\n resultContent =\n `Invalid arguments for tool \\`${toolCall.name}\\`:\\n` +\n prettifyError(err) +\n \"\\nRe-issue the call with each field as the correct type.\";\n } else {\n resultContent = err instanceof Error ? err.message : String(err);\n }\n }\n }\n\n const durationMs = Date.now() - startTime;\n\n eventStream.push({\n type: \"tool_call_end\" as const,\n toolCallId: toolCall.id,\n result: toolResultPreview(resultContent),\n details,\n isError,\n durationMs,\n });\n\n return { toolCallId: toolCall.id, content: resultContent, isError };\n });\n\n // Abort the tool event stream when the signal fires so Ctrl+C\n // doesn't hang waiting for long-running tools to finish.\n const abortHandler = () => eventStream.abort(new Error(\"aborted\"));\n options.signal?.addEventListener(\"abort\", abortHandler, { once: true });\n\n // Close event stream when all tools complete.\n // Track whether the finally block has already consumed toolResults\n // to prevent the race where .then() mutates toolResults after\n // messages.push() has already captured the array by reference.\n let toolResultsFinalized = false;\n\n Promise.all(executions)\n .then((results) => {\n if (toolResultsFinalized) return;\n const resultsMap = new Map(results.map((r) => [r.toolCallId, r]));\n for (const tc of toolCalls) {\n const r = resultsMap.get(tc.id)!;\n toolResults.push({\n type: \"tool_result\",\n toolCallId: tc.id,\n content: r.content,\n isError: r.isError || undefined,\n });\n }\n eventStream.close();\n })\n .catch((err) => eventStream.abort(err instanceof Error ? err : new Error(String(err))));\n\n // Yield events as they arrive from parallel tools\n let toolsAborted = false;\n try {\n for await (const event of eventStream) {\n yield event;\n }\n } catch (err) {\n // Tool event stream aborted (Ctrl+C) — don't propagate, just mark\n // so the finally block can clean up and the loop can exit.\n if (isAbortError(err) || options.signal?.aborted) {\n toolsAborted = true;\n } else {\n throw err;\n }\n } finally {\n options.signal?.removeEventListener(\"abort\", abortHandler);\n\n // Prevent the Promise.all .then() from mutating toolResults after\n // we finalize and push them into messages.\n toolResultsFinalized = true;\n\n // Ensure every tool_use has a matching tool_result, even on abort.\n // Without this, an aborted turn leaves an orphaned tool_use in the\n // message history which causes Anthropic API 400 errors on the next\n // request.\n const resolvedIds = new Set(toolResults.map((r) => r.toolCallId));\n for (const tc of toolCalls) {\n if (!resolvedIds.has(tc.id)) {\n toolResults.push({\n type: \"tool_result\",\n toolCallId: tc.id,\n content: \"Tool execution was aborted.\",\n isError: true,\n });\n }\n }\n // Guard: cap oversized tool results before they enter conversation history.\n // Uses head+tail strategy to preserve error messages / closing structure at the end.\n if (options.maxToolResultChars) {\n const HARD_MAX = 400_000; // absolute ceiling regardless of context window\n const max = Math.min(options.maxToolResultChars, HARD_MAX);\n for (const tr of toolResults) {\n // Only truncate string content — array content (text+image blocks)\n // is already size-bounded by the image resizer.\n if (typeof tr.content === \"string\" && tr.content.length > max) {\n // Keep 70% head + 30% tail to preserve errors/diagnostics at the end\n const headChars = Math.floor(max * 0.7);\n const tailChars = max - headChars;\n const head = tr.content.slice(0, headChars);\n const tail = tr.content.slice(-tailChars);\n const omitted = tr.content.length - headChars - tailChars;\n tr.content = head + `\\n\\n[... ${omitted} characters omitted ...]\\n\\n` + tail;\n }\n }\n }\n\n messages.push({ role: \"tool\", content: toolResults });\n }\n\n // Exit loop after cleaning up aborted tools\n if (toolsAborted) break;\n\n // ── Steering messages: inject user messages queued during tool execution ──\n // Polled after tools complete so the next LLM call sees them in context.\n if (options.getSteeringMessages) {\n const steering = await options.getSteeringMessages();\n if (steering && steering.length > 0) {\n for (const msg of steering) {\n yield { type: \"steering_message\" as const, content: msg.content };\n messages.push(msg);\n }\n }\n }\n }\n } finally {\n // Sanitize orphaned server_tool_use blocks on abort.\n // When a stream is aborted mid-server-tool (e.g. web_search), the\n // assistant message containing the server_tool_use may already be in\n // the messages array, but the corresponding web_search_tool_result\n // never arrived. The API rejects the next request with a 400 if it\n // finds an unmatched server_tool_use, so we strip it here.\n sanitizeOrphanedServerTools(messages);\n }\n\n // Exceeded max turns — return last assistant message\n let lastAssistant: AssistantMessage | undefined;\n for (let i = messages.length - 1; i >= 0; i--) {\n if (messages[i]!.role === \"assistant\") {\n lastAssistant = messages[i] as AssistantMessage;\n break;\n }\n }\n\n yield {\n type: \"agent_done\" as const,\n totalTurns: turn,\n totalUsage: { ...totalUsage },\n };\n\n return {\n message: lastAssistant ?? { role: \"assistant\" as const, content: [] },\n totalTurns: turn,\n totalUsage: { ...totalUsage },\n };\n}\n\nfunction normalizeToolResult(raw: ToolExecuteResult): StructuredToolResult {\n return typeof raw === \"string\" ? { content: raw } : raw;\n}\n\n/** Flatten tool result content to a plain-text preview for the tool_call_end event.\n * Image blocks become a \"[image]\" placeholder so the UI has something to render. */\nfunction toolResultPreview(content: ToolResultContent): string {\n if (typeof content === \"string\") return content;\n return content\n .map((block) => (block.type === \"text\" ? block.text : `[image ${block.mediaType}]`))\n .join(\"\\n\");\n}\n\nfunction extractToolCalls(content: string | ContentPart[]): ToolCall[] {\n if (typeof content === \"string\") return [];\n return content.filter((part): part is ToolCall => part.type === \"tool_call\");\n}\n\n/**\n * Remove orphaned server_tool_use blocks from the last assistant message.\n * When a stream is aborted mid-server-tool (e.g. web_search), the assistant\n * message may contain a server_tool_call without a matching server_tool_result.\n * The API rejects the next request if these are unmatched.\n */\nfunction sanitizeOrphanedServerTools(messages: Message[]): void {\n // Find the last assistant message\n for (let i = messages.length - 1; i >= 0; i--) {\n const msg = messages[i]!;\n if (msg.role !== \"assistant\") continue;\n if (typeof msg.content === \"string\" || !Array.isArray(msg.content)) break;\n\n // Collect server_tool_call ids and matched server_tool_result ids\n const serverToolIds = new Set<string>();\n const resultToolIds = new Set<string>();\n for (const part of msg.content) {\n if (part.type === \"server_tool_call\") serverToolIds.add(part.id);\n if (part.type === \"server_tool_result\") resultToolIds.add(part.toolUseId);\n }\n\n // Find unmatched server_tool_call blocks\n const orphanedIds = new Set<string>();\n for (const id of serverToolIds) {\n if (!resultToolIds.has(id)) orphanedIds.add(id);\n }\n\n if (orphanedIds.size === 0) break;\n\n // Strip orphaned server_tool_call blocks from the content\n const filtered = msg.content.filter(\n (part) => !(part.type === \"server_tool_call\" && orphanedIds.has(part.id)),\n );\n\n if (filtered.length === 0) {\n // Nothing left — remove the entire message\n messages.splice(i, 1);\n } else {\n (msg as { content: ContentPart[] }).content = filtered;\n }\n break;\n }\n}\n\n/**\n * Ensure every assistant message with tool_call blocks is immediately followed\n * by a tool message with matching tool_result entries. This prevents Anthropic\n * API 400 errors (\"tool_use ids found without tool_result blocks immediately\n * after\") that can occur after compaction, session restore, or abort recovery.\n *\n * Repairs in-place by inserting synthetic tool_result messages where needed.\n */\nfunction repairToolPairingAdjacent(messages: Message[]): void {\n for (let i = 0; i < messages.length; i++) {\n const msg = messages[i]!;\n if (msg.role !== \"assistant\") continue;\n if (typeof msg.content === \"string\" || !Array.isArray(msg.content)) continue;\n\n const toolCallIds = (msg.content as ContentPart[])\n .filter((p) => p.type === \"tool_call\")\n .map((p) => (p as ContentPart & { type: \"tool_call\"; id: string }).id);\n if (toolCallIds.length === 0) continue;\n\n const next = messages[i + 1];\n if (next?.role === \"tool\" && Array.isArray(next.content)) {\n // Tool message exists — check for missing results\n const existingIds = new Set((next.content as ToolResult[]).map((r) => r.toolCallId));\n const missing = toolCallIds.filter((id) => !existingIds.has(id));\n if (missing.length > 0) {\n for (const id of missing) {\n (next.content as ToolResult[]).push({\n type: \"tool_result\",\n toolCallId: id,\n content: \"Tool execution was interrupted.\",\n isError: true,\n });\n }\n }\n } else {\n // No tool message follows — insert a synthetic one\n messages.splice(i + 1, 0, {\n role: \"tool\" as const,\n content: toolCallIds.map((id) => ({\n type: \"tool_result\" as const,\n toolCallId: id,\n content: \"Tool execution was interrupted.\",\n isError: true,\n })),\n });\n }\n }\n\n // Reverse repair: strip tool_result entries whose tool_use_id has no matching\n // tool_call in the preceding assistant message. This can happen when compaction\n // or stall recovery removes an assistant message but leaves its tool_result behind.\n const toolCallIdSet = new Set<string>();\n for (let i = 0; i < messages.length; i++) {\n const msg = messages[i]!;\n if (msg.role === \"assistant\" && Array.isArray(msg.content)) {\n for (const p of msg.content as ContentPart[]) {\n if (p.type === \"tool_call\") toolCallIdSet.add((p as ToolCall).id);\n }\n }\n if (msg.role === \"tool\" && Array.isArray(msg.content)) {\n const results = msg.content as ToolResult[];\n const filtered = results.filter((r) => toolCallIdSet.has(r.toolCallId));\n if (filtered.length === 0) {\n // Entire tool message is orphaned — remove it\n messages.splice(i, 1);\n i--;\n } else if (filtered.length < results.length) {\n (msg as { content: ToolResult[] }).content = filtered;\n }\n }\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;A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AAsB,KAAM,CAAC;AACtE,cAAM,SAAS,KAAK,cAAc,OAAO,CAAC;AAC1C,cAAM,MAAM,CAAC,GAAI,gBAAgB,CAAC,GAAI,GAAG,MAAM;AAC/C,eAAO,IAAI,SAAS,IAAI,MAAM;AAAA,MAChC;AAAA,MACA,qBAAqB,YAAY;AAC/B,cAAM,eAAgB,MAAM,KAAK,QAAQ,sBAAsB,KAAM,CAAC;AACtE,cAAM,SAAS,KAAK,cAAc,OAAO,CAAC;AAC1C,cAAM,MAAM,CAAC,GAAI,gBAAgB,CAAC,GAAI,GAAG,MAAM;AAC/C,eAAO,IAAI,SAAS,IAAI,MAAM;AAAA,MAChC;AAAA,IACF;AAEA,UAAM,YAAY,UAAU,KAAK,UAAU,iBAAiB;AAC5D,WAAO,IAAI,YAAY,WAAW,MAAM;AACtC,WAAK,WAAW;AAAA,IAClB,CAAC;AAAA,EACH;AACF;","names":["import_gg_ai"]}
package/dist/index.js CHANGED
@@ -2,6 +2,7 @@
2
2
  import { EventStream as EventStream2 } from "@kenkaiiii/gg-ai";
3
3
 
4
4
  // src/agent-loop.ts
5
+ import { ZodError, prettifyError } from "zod";
5
6
  import {
6
7
  stream,
7
8
  EventStream
@@ -665,7 +666,12 @@ async function* agentLoop(messages, options) {
665
666
  details = normalized.details;
666
667
  } catch (err) {
667
668
  isError = true;
668
- resultContent = err instanceof Error ? err.message : String(err);
669
+ if (err instanceof ZodError) {
670
+ resultContent = `Invalid arguments for tool \`${toolCall.name}\`:
671
+ ` + prettifyError(err) + "\nRe-issue the call with each field as the correct type.";
672
+ } else {
673
+ resultContent = err instanceof Error ? err.message : String(err);
674
+ }
669
675
  }
670
676
  }
671
677
  const durationMs = Date.now() - startTime;
package/dist/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/agent.ts","../src/agent-loop.ts"],"sourcesContent":["import { EventStream, type Message } from \"@kenkaiiii/gg-ai\";\nimport { agentLoop } from \"./agent-loop.js\";\nimport type { AgentEvent, AgentOptions, AgentResult } from \"./types.js\";\n\n// ── AgentStream ─────────────────────────────────────────────\n\n/**\n * Dual-nature result: async iterable for streaming events,\n * thenable for awaiting the final AgentResult.\n *\n * ```ts\n * // Stream events\n * for await (const event of agent.prompt(\"hello\")) { ... }\n *\n * // Or just await the result\n * const result = await agent.prompt(\"hello\");\n * ```\n */\nexport class AgentStream implements AsyncIterable<AgentEvent> {\n private events: EventStream<AgentEvent>;\n private resultPromise: Promise<AgentResult>;\n private resolveResult!: (r: AgentResult) => void;\n private rejectResult!: (e: Error) => void;\n private hasConsumer = false;\n\n constructor(generator: AsyncGenerator<AgentEvent, AgentResult>, onDone: () => void) {\n this.events = new EventStream<AgentEvent>();\n this.resultPromise = new Promise<AgentResult>((resolve, reject) => {\n this.resolveResult = resolve;\n this.rejectResult = reject;\n });\n this.pump(generator, onDone);\n }\n\n private async pump(\n generator: AsyncGenerator<AgentEvent, AgentResult>,\n onDone: () => void,\n ): Promise<void> {\n try {\n let next = await generator.next();\n while (!next.done) {\n this.events.push(next.value);\n next = await generator.next();\n }\n this.events.close();\n this.resolveResult(next.value);\n } catch (err) {\n const error = err instanceof Error ? err : new Error(String(err));\n this.events.abort(error);\n this.rejectResult(error);\n } finally {\n onDone();\n }\n }\n\n [Symbol.asyncIterator](): AsyncIterator<AgentEvent> {\n this.hasConsumer = true;\n return this.events[Symbol.asyncIterator]();\n }\n\n then<TResult1 = AgentResult, TResult2 = never>(\n onfulfilled?: ((value: AgentResult) => TResult1 | PromiseLike<TResult1>) | null,\n onrejected?: ((reason: unknown) => TResult2 | PromiseLike<TResult2>) | null,\n ): Promise<TResult1 | TResult2> {\n this.drainEvents().catch(() => {});\n return this.resultPromise.then(onfulfilled, onrejected);\n }\n\n private async drainEvents(): Promise<void> {\n if (this.hasConsumer) return;\n this.hasConsumer = true;\n for await (const _ of this.events) {\n // consume silently\n }\n }\n}\n\n// ── Agent ───────────────────────────────────────────────────\n\nexport class Agent {\n private messages: Message[] = [];\n private _running = false;\n private options: AgentOptions;\n private steeringQueue: Message[] = [];\n private followUpQueue: Message[] = [];\n\n constructor(options: AgentOptions) {\n this.options = options;\n if (options.system) {\n this.messages.push({ role: \"system\", content: options.system });\n }\n if (options.priorMessages && options.priorMessages.length > 0) {\n this.messages.push(...options.priorMessages);\n }\n }\n\n /** Snapshot of the current message history. Used for session persistence. */\n getMessages(): Message[] {\n return [...this.messages];\n }\n\n /**\n * Swap the abort signal used for subsequent prompts. Call this after\n * `controller.abort()` so the next prompt() call gets a fresh, unaborted\n * signal — without losing message history.\n */\n setSignal(signal: AbortSignal | undefined): void {\n this.options = { ...this.options, signal };\n }\n\n get running(): boolean {\n return this._running;\n }\n\n /** Queue a steering message for injection after current tool execution completes. */\n steer(msg: Message): void {\n this.steeringQueue.push(msg);\n }\n\n /** Queue a follow-up message for injection when the agent would otherwise stop. */\n followUp(msg: Message): void {\n this.followUpQueue.push(msg);\n }\n\n prompt(content: string): AgentStream {\n if (this._running) {\n throw new Error(\"Agent is already running\");\n }\n this._running = true;\n\n this.messages.push({ role: \"user\", content });\n\n const optionsWithQueues: AgentOptions = {\n ...this.options,\n getSteeringMessages: async () => {\n const callerResult = (await this.options.getSteeringMessages?.()) ?? [];\n const queued = this.steeringQueue.splice(0);\n const all = [...(callerResult ?? []), ...queued];\n return all.length > 0 ? all : null;\n },\n getFollowUpMessages: async () => {\n const callerResult = (await this.options.getFollowUpMessages?.()) ?? [];\n const queued = this.followUpQueue.splice(0);\n const all = [...(callerResult ?? []), ...queued];\n return all.length > 0 ? all : null;\n },\n };\n\n const generator = agentLoop(this.messages, optionsWithQueues);\n return new AgentStream(generator, () => {\n this._running = false;\n });\n }\n}\n","import {\n stream,\n EventStream,\n type Message,\n type ToolCall,\n type ToolResult,\n type ToolResultContent,\n type Usage,\n type ContentPart,\n type AssistantMessage,\n} from \"@kenkaiiii/gg-ai\";\nimport type {\n AgentEvent,\n AgentOptions,\n AgentResult,\n AgentTool,\n ToolContext,\n ToolExecuteResult,\n StructuredToolResult,\n} from \"./types.js\";\n\nconst DEFAULT_MAX_TURNS = 200;\n\n/**\n * Lightweight stream diagnostic callback. When set, the agent loop calls this\n * at every phase boundary with timing and state info. This lets the hosting\n * app (ggcoder, come-alive, etc.) log stall diagnostics without the agent\n * package needing fs/process dependencies.\n */\nexport type StreamDiagnosticFn = (phase: string, data?: Record<string, unknown>) => void;\n\n/** Global diagnostic hook — set by the hosting app before calling agentLoop. */\nlet _diagFn: StreamDiagnosticFn | null = null;\n\n/** Register a diagnostic callback for stream stall tracing. */\nexport function setStreamDiagnostic(fn: StreamDiagnosticFn | null): void {\n _diagFn = fn;\n}\n\nfunction diag(phase: string, data?: Record<string, unknown>): void {\n _diagFn?.(phase, data);\n}\n\n/**\n * Detect abort errors — user-initiated cancellation or AbortSignal.\n * These should be caught and handled gracefully, not re-thrown.\n */\nexport function isAbortError(err: unknown): boolean {\n if (!(err instanceof Error)) return false;\n if (err.name === \"AbortError\") return true;\n const msg = err.message.toLowerCase();\n return msg.includes(\"aborted\") || msg.includes(\"abort\");\n}\n\n/**\n * Detect context window overflow errors from LLM providers.\n *\n * Patterns drawn from observed errors across Anthropic, OpenAI, OpenAI Codex,\n * Bedrock, Ollama, and OpenAI-compatible Chinese providers (GLM, Kimi, MiniMax).\n */\nexport function isContextOverflow(err: unknown): boolean {\n if (!(err instanceof Error)) return false;\n if (isBillingError(err)) return false;\n const msg = err.message.toLowerCase();\n return (\n msg.includes(\"prompt is too long\") ||\n msg.includes(\"prompt too long\") ||\n msg.includes(\"input is too long\") ||\n msg.includes(\"context_length_exceeded\") ||\n msg.includes(\"context_window_exceeded\") ||\n msg.includes(\"maximum context length\") ||\n msg.includes(\"exceeds model context window\") ||\n msg.includes(\"exceeds the context window\") ||\n msg.includes(\"content_too_large\") ||\n msg.includes(\"request_too_large\") ||\n msg.includes(\"reduce the length\") ||\n msg.includes(\"please shorten\") ||\n (msg.includes(\"token\") && msg.includes(\"exceed\"))\n );\n}\n\n/**\n * Detect billing/quota errors — these should NOT be retried.\n * GLM returns HTTP 429 with \"Insufficient balance\" for quota exhaustion.\n */\nexport function isBillingError(err: unknown): boolean {\n if (!(err instanceof Error)) return false;\n const msg = err.message.toLowerCase();\n return (\n msg.includes(\"insufficient balance\") ||\n msg.includes(\"no resource package\") ||\n msg.includes(\"quota exceeded\") ||\n msg.includes(\"billing\") ||\n msg.includes(\"recharge\") ||\n msg.includes(\"subscription plan\") ||\n msg.includes(\"does not yet include access\") ||\n msg.includes(\"token quota\") ||\n msg.includes(\"exceeded_current_quota_error\") ||\n msg.includes(\"check your account balance\")\n );\n}\n\n/**\n * Detect overloaded/rate-limit errors from LLM providers.\n * HTTP 429 (rate limit) or 529/503 (overloaded).\n * Excludes billing/quota errors which won't resolve with a retry.\n */\n/**\n * Detect tool pairing errors — orphaned tool_use or tool_result blocks.\n * These are 400 errors that can be recovered by repairing the message history.\n */\nexport function isToolPairingError(err: unknown): boolean {\n if (!(err instanceof Error)) return false;\n const msg = err.message.toLowerCase();\n return (\n (msg.includes(\"tool_use\") && msg.includes(\"tool_result\")) ||\n msg.includes(\"unexpected `tool_use_id`\") ||\n msg.includes(\"tool_use ids found without\") ||\n // Moonshot/OpenAI-compatible: \"tool call id <id> is not found\"\n (msg.includes(\"tool call id\") && msg.includes(\"is not found\"))\n );\n}\n\n/**\n * Distinguish rate-limit (HTTP 429) from server-side overload (HTTP 529).\n * Returns null for errors that should not enter the overload-retry bucket.\n * Both kinds use the same backoff schedule, but the UI shows different copy\n * and the log line records the true cause.\n */\nexport function classifyOverload(err: unknown): \"rate_limit\" | \"overloaded\" | null {\n if (!(err instanceof Error)) return null;\n if (isBillingError(err)) return null;\n const msg = err.message.toLowerCase();\n if (\n msg.includes(\"rate_limit\") ||\n msg.includes(\"rate limit\") ||\n msg.includes(\"too many requests\") ||\n msg.includes(\"429\")\n ) {\n return \"rate_limit\";\n }\n if (msg.includes(\"overloaded\") || msg.includes(\"529\")) {\n return \"overloaded\";\n }\n return null;\n}\n\nexport function isOverloaded(err: unknown): boolean {\n return classifyOverload(err) !== null;\n}\n\n/**\n * Detect malformed-stream errors — the SDK's SSE decoder threw a JSON parse\n * error mid-stream, typically because a chunk was truncated or corrupted by\n * an intermediary (CDN, proxy). Same class of transport failure as a stall:\n * replaying the request — and ideally flipping to non-streaming — recovers.\n */\nexport function isMalformedStream(err: unknown): boolean {\n if (!(err instanceof Error)) return false;\n if (err.name === \"SyntaxError\") return true;\n const cause = (err as { cause?: unknown }).cause;\n if (cause instanceof Error && cause.name === \"SyntaxError\") return true;\n const msg = err.message;\n // V8 JSON.parse error messages: \"Expected ... in JSON at position N\"\n // and \"Unexpected token ... in JSON at position N\"\n return /\\bin JSON at position \\d+/i.test(msg);\n}\n\n/**\n * Detect socket-level transport failures — the remote peer (or an\n * intermediary) closed the TCP connection mid-stream before the response\n * finished. Surfaces as `TypeError: terminated` from undici/fetch, or as\n * `ECONNRESET` / `socket hang up` / `UND_ERR_SOCKET` from the underlying\n * Node http layer. Undici nests the real cause one or more levels deep,\n * so we walk the `.cause` chain. Same recovery as a stall: replay the\n * request, optionally as non-streaming.\n */\nexport function isTransportFailure(err: unknown): boolean {\n const codes = new Set([\n \"ECONNRESET\",\n \"ECONNREFUSED\",\n \"ECONNABORTED\",\n \"ETIMEDOUT\",\n \"EPIPE\",\n \"EHOSTUNREACH\",\n \"ENETUNREACH\",\n \"ENOTFOUND\",\n \"UND_ERR_SOCKET\",\n \"UND_ERR_CONNECT_TIMEOUT\",\n \"UND_ERR_HEADERS_TIMEOUT\",\n \"UND_ERR_BODY_TIMEOUT\",\n \"UND_ERR_RESPONSE_STATUS_CODE\",\n \"UND_ERR_REQ_CONTENT_LENGTH_MISMATCH\",\n \"UND_ERR_RES_CONTENT_LENGTH_MISMATCH\",\n ]);\n const messages = [\n /^terminated$/i,\n /\\bother side closed\\b/i,\n /\\bsocket hang up\\b/i,\n /\\bfetch failed\\b/i,\n /\\bbody timeout error\\b/i,\n /\\bsse stream disconnected\\b/i,\n /\\bfailed to reconnect sse stream\\b/i,\n ];\n const seen = new Set<unknown>();\n let cur: unknown = err;\n while (cur && typeof cur === \"object\" && !seen.has(cur)) {\n seen.add(cur);\n const e = cur as { code?: unknown; message?: unknown; cause?: unknown };\n if (typeof e.code === \"string\" && codes.has(e.code)) return true;\n if (typeof e.message === \"string\") {\n for (const re of messages) if (re.test(e.message)) return true;\n }\n cur = e.cause;\n }\n return false;\n}\n\n/**\n * Promise-returning sleep that rejects with AbortError if `signal` fires.\n * Used by retry backoffs so ESC/Ctrl+C cancel immediately instead of having\n * to wait out the full delay (up to 30s per overload retry × 10 retries).\n */\nfunction abortableSleep(ms: number, signal?: AbortSignal): Promise<void> {\n if (signal?.aborted) {\n return Promise.reject(new DOMException(\"Aborted\", \"AbortError\"));\n }\n return new Promise<void>((resolve, reject) => {\n let onAbort: (() => void) | null = null;\n const timer = setTimeout(() => {\n if (onAbort) signal?.removeEventListener(\"abort\", onAbort);\n resolve();\n }, ms);\n onAbort = () => {\n clearTimeout(timer);\n reject(new DOMException(\"Aborted\", \"AbortError\"));\n };\n signal?.addEventListener(\"abort\", onAbort, { once: true });\n });\n}\n\nexport async function* agentLoop(\n messages: Message[],\n options: AgentOptions,\n): AsyncGenerator<AgentEvent, AgentResult> {\n const maxTurns = options.maxTurns ?? DEFAULT_MAX_TURNS;\n const maxContinuations = options.maxContinuations ?? 5;\n const toolMap = new Map<string, AgentTool>((options.tools ?? []).map((t) => [t.name, t]));\n\n const totalUsage: Usage = { inputTokens: 0, outputTokens: 0 };\n let turn = 0;\n let firstTurn = true;\n let consecutivePauses = 0;\n let toolPairingRepaired = false;\n let overloadRetries = 0;\n let emptyResponseRetries = 0;\n let stallRetries = 0;\n let overflowCompactionAttempts = 0;\n // Non-streaming fallback mode. After repeated stream stalls, flip to a\n // plain non-streaming request/response -- often survives broken SSE\n // connections (transient CDN / proxy issues) that streaming retries cannot.\n let useNonStreamingFallback = false;\n const MAX_OVERLOAD_RETRIES = 10;\n const MAX_EMPTY_RESPONSE_RETRIES = 2;\n const MAX_STALL_RETRIES = 5;\n const MAX_OVERFLOW_COMPACTIONS = 2;\n // After this many streaming stalls in a row, switch to non-streaming mode\n // for the remaining stall retries. Keeps the first two retries fast (the\n // cheap \"transient glitch\" case) before paying for a full response round-trip.\n const STALL_RETRIES_BEFORE_NON_STREAMING = 2;\n const STALL_DELAY_MS = 1_000; // Brief pause before retry -- just enough to avoid tight loops\n const OVERLOAD_BASE_DELAY_MS = 2_000;\n const OVERLOAD_MAX_DELAY_MS = 30_000;\n const STREAM_FIRST_EVENT_TIMEOUT_MS = 45_000; // 45s to get first event (Opus thinks long)\n const STREAM_IDLE_TIMEOUT_MS = 30_000; // 30s between events once streaming starts\n // Anthropic models can pause 10-20s mid-stream while computing the next chunk\n // (e.g. generating tool call args for a large write). 10s was too aggressive\n // and caused false \"stream stalled\" errors, especially in plan mode.\n const STREAM_HARD_TIMEOUT_MS = 90_000; // 90s absolute cap before output starts\n // Once output events (text_delta) are actively streaming, extend the hard\n // timeout -- long responses (plan mode, detailed explanations) can legitimately\n // take 2-3+ minutes while events flow continuously.\n const STREAM_OUTPUT_HARD_TIMEOUT_MS = 300_000; // 5min hard cap once output is flowing\n // Reasoning models (MiMo) can pause 3-5 minutes between thinking and output\n // generation. Once we've seen thinking events, extend timeouts significantly.\n const STREAM_THINKING_IDLE_TIMEOUT_MS = 300_000; // 5min idle after thinking\n const STREAM_THINKING_HARD_TIMEOUT_MS = 600_000; // 10min hard cap with thinking\n // Non-streaming mode has no per-event idle -- the entire response arrives in\n // one HTTP round-trip. Use a single generous hard cap instead. This matches\n // Claude Code's v2.1.110/111 behaviour: cap non-streaming retries so API\n // unreachability doesn't cause multi-minute hangs, but not so aggressively\n // that slow-but-healthy backends get killed.\n const NON_STREAMING_HARD_TIMEOUT_MS = 300_000; // 5min for full non-streaming response\n // Runaway tool-call circuit breaker. When a model glitches mid-tool-call it\n // can emit tens of thousands of toolcall_delta events without ever closing,\n // burning the entire stall-retry budget (~25 min) on what is clearly a\n // non-recoverable model error. Cap accumulated arg chars and event count;\n // exceeding either is a hard, non-retriable failure. Thresholds are generous\n // enough to allow legitimate large file writes through `write`.\n const MAX_TOOLCALL_DELTA_CHARS = 1_000_000; // 1 MB of accumulated tool-call args\n const MAX_TOOLCALL_DELTA_EVENTS = 20_000; // 20k delta events in one stream\n\n try {\n while (turn < maxTurns) {\n options.signal?.throwIfAborted();\n turn++;\n\n // Estimate message payload size for diagnostics\n let msgChars = 0;\n for (const m of messages) {\n if (typeof m.content === \"string\") msgChars += m.content.length;\n else if (Array.isArray(m.content)) {\n for (const p of m.content) {\n if (\"text\" in p && typeof p.text === \"string\") msgChars += p.text.length;\n if (\"content\" in p && typeof p.content === \"string\") msgChars += p.content.length;\n }\n }\n }\n diag(\"turn_start\", {\n turn,\n messages: messages.length,\n chars: msgChars,\n provider: options.provider,\n model: options.model,\n });\n\n // ── Initial steering poll: catch messages queued before the first LLM call ──\n if (firstTurn && options.getSteeringMessages) {\n const steering = await options.getSteeringMessages();\n if (steering && steering.length > 0) {\n for (const msg of steering) {\n yield { type: \"steering_message\" as const, content: msg.content };\n messages.push(msg);\n }\n }\n }\n firstTurn = false;\n\n // ── Mid-loop context transform (compaction / truncation) ──\n if (options.transformContext) {\n diag(\"transform_start\");\n const transformed = await options.transformContext(messages);\n if (transformed !== messages) {\n diag(\"transform_compacted\", {\n before: messages.length,\n after: transformed.length,\n });\n messages.length = 0;\n messages.push(...transformed);\n }\n diag(\"transform_end\");\n }\n\n // ── Repair tool pairing: ensure every tool_use has an adjacent tool_result ──\n repairToolPairingAdjacent(messages);\n\n // ── Call LLM with overflow recovery ──\n let response;\n // Per-attempt abort controller: allows idle timeout to abort the stream\n // without affecting the caller's signal. The caller's abort is forwarded.\n const streamController = new AbortController();\n let idleTimer: ReturnType<typeof setTimeout> | null = null;\n let hardTimer: ReturnType<typeof setTimeout> | null = null;\n let idleTimedOut = false;\n\n // Stream event counters — declared here so timeout callbacks can access them\n let streamEventCount = 0;\n let lastEventTime = Date.now();\n let streamCallStart = Date.now();\n // Track event types for diagnostics — shows what arrived before a stall\n const eventTypeCounts: Record<string, number> = {};\n let lastEventType = \"\";\n // Runaway tool-call detection — accumulated across all toolcall_delta\n // events in this stream attempt. When tripped we abort the stream and\n // bail out without retrying (the model has glitched, retries won't help).\n let toolcallDeltaChars = 0;\n let toolcallDeltaCount = 0;\n let runawayDetected: { kind: \"chars\" | \"events\"; chars: number; events: number } | null =\n null;\n // Track consumer processing time — helps distinguish \"API stopped sending\"\n // from \"our consumer was slow to pull the next event\"\n let lastYieldEndTime = Date.now();\n let maxConsumerLagMs = 0;\n\n // Forward caller abort to the per-attempt controller\n const forwardAbort = () => streamController.abort();\n options.signal?.addEventListener(\"abort\", forwardAbort, { once: true });\n\n // Three-phase idle timeout:\n // - Before first event: STREAM_FIRST_EVENT_TIMEOUT_MS (45s) -- Opus can\n // take 30s+ to start on large contexts, that's not a stall.\n // - After output event (text_delta, server_toolcall): STREAM_IDLE_TIMEOUT_MS\n // (10s) -- once output is streaming, 10s of silence is dead. Retry fast.\n // - After thinking events only: STREAM_THINKING_IDLE_TIMEOUT_MS (5min) --\n // reasoning models (MiMo) can pause minutes between thinking and output.\n //\n // In non-streaming fallback mode the entire response arrives in a single\n // HTTP round-trip, so the idle timer is disabled -- only the hard timeout\n // applies. Synthesized events all arrive at once when the response returns.\n let hasReceivedEvent = false;\n let hasReceivedThinking = false;\n const resetIdleTimer = () => {\n if (useNonStreamingFallback) return; // no inter-event idle in non-streaming mode\n if (idleTimer) clearTimeout(idleTimer);\n const timeoutMs = hasReceivedEvent\n ? STREAM_IDLE_TIMEOUT_MS\n : hasReceivedThinking\n ? STREAM_THINKING_IDLE_TIMEOUT_MS\n : STREAM_FIRST_EVENT_TIMEOUT_MS;\n idleTimer = setTimeout(() => {\n diag(\"idle_timeout_fired\", {\n events: streamEventCount,\n sinceLastEventMs: Date.now() - lastEventTime,\n lastEventType,\n maxConsumerLagMs,\n phase: hasReceivedEvent\n ? \"mid_stream\"\n : hasReceivedThinking\n ? \"post_thinking\"\n : \"first_event\",\n eventTypes: eventTypeCounts,\n });\n idleTimedOut = true;\n streamController.abort();\n }, timeoutMs);\n };\n\n // Hard timeout: absolute cap per LLM call. Safety net for streams that\n // keep sending sparse events (e.g. keep-alive pings) but never complete.\n // Extended dynamically when thinking events arrive (see thinking_delta handler).\n // Non-streaming fallback uses a single larger cap since there's no stream\n // to observe -- just wait for the full response up to the cap.\n let hardTimeoutMs = useNonStreamingFallback\n ? NON_STREAMING_HARD_TIMEOUT_MS\n : STREAM_HARD_TIMEOUT_MS;\n hardTimer = setTimeout(() => {\n diag(\"hard_timeout_fired\", {\n events: typeof streamEventCount !== \"undefined\" ? streamEventCount : 0,\n nonStreaming: useNonStreamingFallback,\n });\n idleTimedOut = true;\n streamController.abort();\n }, hardTimeoutMs);\n\n try {\n diag(\"stream_call\", { nonStreaming: useNonStreamingFallback });\n streamCallStart = Date.now();\n const result = stream({\n provider: options.provider,\n model: options.model,\n messages,\n tools: options.tools,\n serverTools: options.serverTools,\n webSearch: options.webSearch,\n maxTokens: options.maxTokens,\n temperature: options.temperature,\n thinking: options.thinking,\n apiKey: options.apiKey,\n baseUrl: options.baseUrl,\n signal: streamController.signal,\n accountId: options.accountId,\n cacheRetention: options.cacheRetention,\n promptCacheKey: options.promptCacheKey,\n serviceTier: options.serviceTier,\n supportsImages: options.supportsImages,\n compaction: options.compaction,\n clearToolUses: options.clearToolUses,\n userAgent: options.userAgent,\n // Flip to non-streaming fallback after repeated stream stalls.\n ...(useNonStreamingFallback ? { streaming: false } : {}),\n });\n diag(\"stream_created\", { setupMs: Date.now() - streamCallStart });\n\n // Suppress unhandled rejection if the iterator path throws first\n result.response.catch(() => {});\n\n // Forward streaming deltas — reset idle timer on each event\n streamEventCount = 0;\n hasReceivedEvent = false;\n lastEventTime = Date.now();\n streamCallStart = Date.now();\n // Reset to streamCallStart so the first event's consumerLag reflects\n // network/provider latency, not the time spent before stream() returned.\n lastYieldEndTime = Date.now();\n resetIdleTimer();\n for await (const event of result) {\n // Measure consumer lag: time between finishing previous yield and\n // receiving this event. For event #1 this still includes network/\n // provider latency; for subsequent events it isolates how long\n // React/UI rendering held up the next pull.\n const pullTime = Date.now();\n const consumerLag = pullTime - lastYieldEndTime;\n // Only track mid-stream lag — first event lag is dominated by\n // server-side TTFB and would mask real UI starvation issues.\n if (streamEventCount > 0 && consumerLag > maxConsumerLagMs) {\n maxConsumerLagMs = consumerLag;\n }\n\n streamEventCount++;\n eventTypeCounts[event.type] = (eventTypeCounts[event.type] ?? 0) + 1;\n lastEventType = event.type;\n\n // Flip to mid-stream timeout on confirmed output events — text\n // deltas, completed tool calls, and tool call deltas (large file\n // writes can stream toolcall_delta for minutes without any text_delta).\n // Reasoning models (MiMo) are handled separately below — they can\n // stream hundreds of thinking events then pause minutes before output.\n if (\n (event.type === \"text_delta\" ||\n event.type === \"server_toolcall\" ||\n event.type === \"toolcall_delta\") &&\n !hasReceivedEvent\n ) {\n hasReceivedEvent = true;\n // Extend hard timeout now that output is actively streaming.\n // Long responses (plan mode, detailed code) can exceed 90s while\n // events flow continuously — the idle timeout (10s) catches real stalls.\n if (hardTimer && hardTimeoutMs < STREAM_OUTPUT_HARD_TIMEOUT_MS) {\n clearTimeout(hardTimer);\n hardTimeoutMs = STREAM_OUTPUT_HARD_TIMEOUT_MS;\n hardTimer = setTimeout(() => {\n diag(\"hard_timeout_fired\", { events: streamEventCount });\n idleTimedOut = true;\n streamController.abort();\n }, hardTimeoutMs);\n }\n }\n // Track thinking events — extends idle timeout and hard timeout\n // so reasoning models aren't killed during thinking→output transition.\n if (event.type === \"thinking_delta\" && !hasReceivedThinking) {\n hasReceivedThinking = true;\n // Extend the hard timeout now that we know the model is reasoning\n if (hardTimer) clearTimeout(hardTimer);\n hardTimeoutMs = STREAM_THINKING_HARD_TIMEOUT_MS;\n hardTimer = setTimeout(() => {\n diag(\"hard_timeout_fired\", { events: streamEventCount });\n idleTimedOut = true;\n streamController.abort();\n }, hardTimeoutMs);\n }\n\n const now = Date.now();\n const gap = now - lastEventTime;\n // Log first event and any suspiciously long gaps\n if (streamEventCount === 1) {\n diag(\"first_event\", { type: event.type, ttfMs: now - streamCallStart });\n } else if (gap > 3000) {\n diag(\"slow_gap\", {\n type: event.type,\n gapMs: gap,\n eventNum: streamEventCount,\n sinceStartMs: now - streamCallStart,\n });\n }\n lastEventTime = now;\n resetIdleTimer();\n if (event.type === \"text_delta\") {\n yield { type: \"text_delta\" as const, text: event.text };\n } else if (event.type === \"thinking_delta\") {\n yield { type: \"thinking_delta\" as const, text: event.text };\n } else if (event.type === \"server_toolcall\") {\n yield {\n type: \"server_tool_call\" as const,\n id: event.id,\n name: event.name,\n input: event.input,\n };\n } else if (event.type === \"server_toolresult\") {\n yield {\n type: \"server_tool_result\" as const,\n toolUseId: event.toolUseId,\n resultType: event.resultType,\n data: event.data,\n };\n } else if (event.type === \"toolcall_delta\") {\n const chunkChars = event.argsJson?.length ?? 0;\n toolcallDeltaChars += chunkChars;\n toolcallDeltaCount++;\n if (\n !runawayDetected &&\n (toolcallDeltaChars > MAX_TOOLCALL_DELTA_CHARS ||\n toolcallDeltaCount > MAX_TOOLCALL_DELTA_EVENTS)\n ) {\n runawayDetected = {\n kind: toolcallDeltaChars > MAX_TOOLCALL_DELTA_CHARS ? \"chars\" : \"events\",\n chars: toolcallDeltaChars,\n events: toolcallDeltaCount,\n };\n diag(\"runaway_toolcall_detected\", {\n ...runawayDetected,\n provider: options.provider,\n model: options.model,\n });\n streamController.abort();\n }\n yield {\n type: \"toolcall_delta\" as const,\n chars: chunkChars,\n };\n }\n lastYieldEndTime = Date.now();\n }\n\n diag(\"stream_done\", {\n events: streamEventCount,\n totalMs: Date.now() - streamCallStart,\n maxConsumerLagMs,\n eventTypes: eventTypeCounts,\n });\n response = await result.response;\n } catch (err) {\n const errMsg = err instanceof Error ? err.message : String(err);\n diag(\"stream_error\", {\n error: errMsg.slice(0, 200),\n events: streamEventCount,\n totalMs: Date.now() - streamCallStart,\n idleTimedOut,\n aborted: !!options.signal?.aborted,\n eventTypes: eventTypeCounts,\n provider: options.provider,\n model: options.model,\n });\n // Context overflow: try a forced compaction before giving up.\n // The pre-turn transformContext check uses estimated tokens, which can\n // underestimate code-heavy content. When the API confirms overflow we\n // compact unconditionally and retry the turn, capped at\n // MAX_OVERFLOW_COMPACTIONS to avoid loops when compaction can't reduce\n // enough (e.g. single huge user message).\n if (isContextOverflow(err)) {\n if (options.transformContext && overflowCompactionAttempts < MAX_OVERFLOW_COMPACTIONS) {\n overflowCompactionAttempts++;\n diag(\"overflow_compact_start\", {\n attempt: overflowCompactionAttempts,\n maxAttempts: MAX_OVERFLOW_COMPACTIONS,\n messages: messages.length,\n });\n try {\n const compacted = await options.transformContext(messages, { force: true });\n if (compacted !== messages && compacted.length < messages.length) {\n messages.length = 0;\n messages.push(...compacted);\n diag(\"overflow_compact_success\", {\n attempt: overflowCompactionAttempts,\n messages: messages.length,\n });\n yield {\n type: \"retry\" as const,\n reason: \"overflow_compact\" as const,\n attempt: overflowCompactionAttempts,\n maxAttempts: MAX_OVERFLOW_COMPACTIONS,\n delayMs: 0,\n };\n turn--;\n continue;\n }\n diag(\"overflow_compact_noop\", {\n attempt: overflowCompactionAttempts,\n before: messages.length,\n after: compacted.length,\n });\n } catch (compactErr) {\n diag(\"overflow_compact_failed\", {\n error: compactErr instanceof Error ? compactErr.message : String(compactErr),\n });\n }\n }\n yield { type: \"error\" as const, error: err instanceof Error ? err : new Error(errMsg) };\n throw err;\n }\n // Overloaded / rate-limited: exponential backoff, retry up to 10 times\n const overloadKind = classifyOverload(err);\n if (overloadRetries < MAX_OVERLOAD_RETRIES && overloadKind) {\n overloadRetries++;\n const delayMs = Math.min(\n OVERLOAD_BASE_DELAY_MS * 2 ** (overloadRetries - 1),\n OVERLOAD_MAX_DELAY_MS,\n );\n diag(\"retry\", {\n reason: overloadKind,\n attempt: overloadRetries,\n maxAttempts: MAX_OVERLOAD_RETRIES,\n delayMs,\n });\n yield {\n type: \"retry\" as const,\n reason: overloadKind,\n attempt: overloadRetries,\n maxAttempts: MAX_OVERLOAD_RETRIES,\n delayMs,\n };\n await abortableSleep(delayMs, options.signal);\n turn--; // Don't count the failed turn\n continue;\n }\n // Stream stall: the API connection hung without closing.\n // Malformed stream: the SDK's SSE decoder hit truncated/corrupted JSON.\n // Both are transport failures — retry with exponential backoff and flip\n // to non-streaming mode after STALL_RETRIES_BEFORE_NON_STREAMING attempts,\n // since broken SSE often recovers when replayed as plain HTTP.\n // Runaway tool-call: the model never closed a tool-call block and\n // blew past the size/count caps. Retrying just reproduces the loop,\n // so surface a clear error and stop. Checked before the abort branch\n // since we ourselves aborted the stream to break the runaway.\n if (runawayDetected) {\n diag(\"runaway_toolcall_aborted\", {\n ...runawayDetected,\n provider: options.provider,\n model: options.model,\n });\n const detail =\n runawayDetected.kind === \"chars\"\n ? `${(runawayDetected.chars / 1024).toFixed(0)} KB of tool-call arguments`\n : `${runawayDetected.events} tool-call delta events`;\n yield {\n type: \"error\" as const,\n error: new Error(\n `The model glitched mid-tool-call and produced ${detail} without closing the call. ` +\n `This is usually an upstream model bug — try the same request again or switch models. ` +\n `Your conversation is preserved.`,\n ),\n };\n break;\n }\n const malformed = isMalformedStream(err);\n const socketDrop = isTransportFailure(err);\n const transportFailure =\n (idleTimedOut || malformed || socketDrop) && !options.signal?.aborted;\n if (transportFailure && stallRetries < MAX_STALL_RETRIES) {\n stallRetries++;\n const cause = malformed\n ? \"malformed_stream\"\n : socketDrop\n ? \"socket_drop\"\n : \"stream_stall\";\n if (!useNonStreamingFallback && stallRetries >= STALL_RETRIES_BEFORE_NON_STREAMING) {\n useNonStreamingFallback = true;\n diag(\"non_streaming_fallback_enabled\", {\n stallRetries,\n provider: options.provider,\n model: options.model,\n cause,\n });\n }\n const delayMs = Math.min(STALL_DELAY_MS * 2 ** (stallRetries - 1), 8_000);\n diag(\"retry\", {\n reason: cause,\n attempt: stallRetries,\n maxAttempts: MAX_STALL_RETRIES,\n delayMs,\n events: streamEventCount,\n nonStreaming: useNonStreamingFallback,\n });\n yield {\n type: \"retry\" as const,\n reason: \"stream_stall\" as const,\n attempt: stallRetries,\n maxAttempts: MAX_STALL_RETRIES,\n delayMs,\n silent: stallRetries <= 2,\n };\n await abortableSleep(delayMs, options.signal);\n turn--; // Don't count the failed turn\n continue;\n }\n // Stream stall retries exhausted — surface a clear error so the UI\n // can distinguish \"gave up after stalls\" from \"completed normally\".\n if (transportFailure) {\n diag(\"stall_exhausted\", {\n stallRetries: MAX_STALL_RETRIES,\n provider: options.provider,\n model: options.model,\n });\n yield {\n type: \"error\" as const,\n error: new Error(\n `The API provider's stream stalled ${MAX_STALL_RETRIES} times — the provider may be experiencing capacity issues. ` +\n `Your conversation is preserved. Send another message to retry.`,\n ),\n };\n break;\n }\n // Tool pairing 400: orphaned tool_result or tool_use in message history.\n // Run repair and retry once — if repair can't fix it, surface the error.\n if (isToolPairingError(err) && !toolPairingRepaired) {\n toolPairingRepaired = true;\n diag(\"tool_pairing_repair\", { error: errMsg.slice(0, 200) });\n repairToolPairingAdjacent(messages);\n turn--;\n continue;\n }\n // Abort errors (user cancellation) — exit loop cleanly instead of\n // crashing the process with an unhandled rejection.\n if (isAbortError(err) || options.signal?.aborted) {\n diag(\"aborted\", { turn, provider: options.provider, model: options.model });\n break;\n }\n // Unhandled error — log before throwing so the crash is traceable\n diag(\"unhandled_error\", {\n error: errMsg.slice(0, 500),\n turn,\n provider: options.provider,\n model: options.model,\n });\n throw err;\n } finally {\n if (idleTimer) clearTimeout(idleTimer);\n if (hardTimer) clearTimeout(hardTimer);\n options.signal?.removeEventListener(\"abort\", forwardAbort);\n }\n\n overloadRetries = 0;\n stallRetries = 0;\n\n // Detect empty/degenerate responses — the API occasionally returns 0 tokens\n // with no content, or \"thinks\" without producing actionable output.\n // Reasoning models (MiMo, DeepSeek) may report outputTokens > 0 from\n // thinking alone while producing no text or tool calls — still a dud.\n const contentArr = Array.isArray(response.message.content) ? response.message.content : null;\n const hasActionableContent =\n response.message.content !== \"\" &&\n contentArr !== null &&\n contentArr.some(\n (p) => p.type === \"text\" || p.type === \"tool_call\" || p.type === \"server_tool_call\",\n );\n if (!hasActionableContent) {\n if (emptyResponseRetries < MAX_EMPTY_RESPONSE_RETRIES) {\n emptyResponseRetries++;\n diag(\"retry\", {\n reason: \"empty_response\",\n attempt: emptyResponseRetries,\n maxAttempts: MAX_EMPTY_RESPONSE_RETRIES,\n provider: options.provider,\n model: options.model,\n contentTypes: contentArr?.map((p) => p.type).join(\",\") ?? \"empty\",\n });\n yield {\n type: \"retry\" as const,\n reason: \"empty_response\" as const,\n attempt: emptyResponseRetries,\n maxAttempts: MAX_EMPTY_RESPONSE_RETRIES,\n delayMs: 0,\n };\n turn--; // Don't count the failed turn — keep useNonStreamingFallback set\n // so the retry doesn't bounce back into a streaming connection that\n // will stall again with the same upstream problem.\n continue;\n }\n // Exhausted retries — fall through and let the agent finish\n }\n emptyResponseRetries = 0;\n\n // Only clear the non-streaming fallback after an actionable response —\n // an empty non-streaming reply means the upstream issue hasn't resolved,\n // so staying in non-streaming mode avoids retrying into another stall.\n useNonStreamingFallback = false;\n\n // Accumulate usage\n totalUsage.inputTokens += response.usage.inputTokens;\n totalUsage.outputTokens += response.usage.outputTokens;\n if (response.usage.cacheRead) {\n totalUsage.cacheRead = (totalUsage.cacheRead ?? 0) + response.usage.cacheRead;\n }\n if (response.usage.cacheWrite) {\n totalUsage.cacheWrite = (totalUsage.cacheWrite ?? 0) + response.usage.cacheWrite;\n }\n\n // Append assistant message to conversation\n messages.push(response.message);\n\n yield {\n type: \"turn_end\" as const,\n turn,\n stopReason: response.stopReason,\n usage: response.usage,\n };\n\n // Server-side tool hit iteration limit — re-send to continue.\n // Do NOT add an extra user message; the API detects the trailing\n // server_tool_use block and resumes automatically.\n if (response.stopReason === \"pause_turn\") {\n consecutivePauses++;\n if (consecutivePauses >= maxContinuations) {\n break; // Safety limit — fall through to agent_done below\n }\n continue;\n }\n consecutivePauses = 0;\n\n // Extract tool calls — separate client-executed from provider built-in (e.g. Moonshot $web_search)\n const allToolCalls = extractToolCalls(response.message.content);\n\n // If no tool calls to execute, check for steering messages before stopping.\n // Check content (not just stopReason) because some providers (e.g. GLM)\n // return finish_reason=\"stop\" even when tool calls are present.\n if (response.stopReason !== \"tool_use\" && allToolCalls.length === 0) {\n // Check for queued steering messages — if present, inject and continue\n // the loop instead of returning (follow-up pattern).\n if (options.getSteeringMessages) {\n const steering = await options.getSteeringMessages();\n if (steering && steering.length > 0) {\n for (const msg of steering) {\n yield { type: \"steering_message\" as const, content: msg.content };\n messages.push(msg);\n }\n continue; // Next iteration will call LLM with injected messages\n }\n }\n // Follow-up: lower priority than steering — only when agent would otherwise stop.\n if (options.getFollowUpMessages) {\n const followUp = await options.getFollowUpMessages();\n if (followUp && followUp.length > 0) {\n for (const msg of followUp) {\n yield { type: \"follow_up_message\" as const, content: msg.content };\n messages.push(msg);\n }\n continue;\n }\n }\n yield {\n type: \"agent_done\" as const,\n totalTurns: turn,\n totalUsage: { ...totalUsage },\n };\n return {\n message: response.message,\n totalTurns: turn,\n totalUsage: { ...totalUsage },\n };\n }\n const toolCalls: ToolCall[] = [];\n const toolResults: ToolResult[] = [];\n\n for (const tc of allToolCalls) {\n if (tc.name.startsWith(\"$\")) {\n // Provider built-in tool (e.g. Moonshot $web_search) — not locally executed.\n // Still needs a tool_result for the message history round-trip.\n toolResults.push({\n type: \"tool_result\",\n toolCallId: tc.id,\n content: JSON.stringify(tc.args),\n });\n } else {\n toolCalls.push(tc);\n }\n }\n const eventStream = new EventStream<AgentEvent>();\n\n // Launch all tool calls in parallel\n const executions = toolCalls.map(async (toolCall) => {\n const startTime = Date.now();\n\n eventStream.push({\n type: \"tool_call_start\" as const,\n toolCallId: toolCall.id,\n name: toolCall.name,\n args: toolCall.args,\n });\n\n let resultContent: ToolResultContent;\n let details: unknown;\n let isError = false;\n\n const tool = toolMap.get(toolCall.name);\n if (!tool) {\n resultContent = `Unknown tool: ${toolCall.name}`;\n isError = true;\n } else {\n try {\n const parsed = tool.parameters.parse(toolCall.args);\n const ctx: ToolContext = {\n signal: options.signal ?? AbortSignal.timeout(300_000),\n toolCallId: toolCall.id,\n onUpdate: (update: unknown) => {\n eventStream.push({\n type: \"tool_call_update\" as const,\n toolCallId: toolCall.id,\n update,\n });\n },\n };\n const raw = await tool.execute(parsed, ctx);\n const normalized = normalizeToolResult(raw);\n resultContent = normalized.content;\n details = normalized.details;\n } catch (err) {\n isError = true;\n resultContent = err instanceof Error ? err.message : String(err);\n }\n }\n\n const durationMs = Date.now() - startTime;\n\n eventStream.push({\n type: \"tool_call_end\" as const,\n toolCallId: toolCall.id,\n result: toolResultPreview(resultContent),\n details,\n isError,\n durationMs,\n });\n\n return { toolCallId: toolCall.id, content: resultContent, isError };\n });\n\n // Abort the tool event stream when the signal fires so Ctrl+C\n // doesn't hang waiting for long-running tools to finish.\n const abortHandler = () => eventStream.abort(new Error(\"aborted\"));\n options.signal?.addEventListener(\"abort\", abortHandler, { once: true });\n\n // Close event stream when all tools complete.\n // Track whether the finally block has already consumed toolResults\n // to prevent the race where .then() mutates toolResults after\n // messages.push() has already captured the array by reference.\n let toolResultsFinalized = false;\n\n Promise.all(executions)\n .then((results) => {\n if (toolResultsFinalized) return;\n const resultsMap = new Map(results.map((r) => [r.toolCallId, r]));\n for (const tc of toolCalls) {\n const r = resultsMap.get(tc.id)!;\n toolResults.push({\n type: \"tool_result\",\n toolCallId: tc.id,\n content: r.content,\n isError: r.isError || undefined,\n });\n }\n eventStream.close();\n })\n .catch((err) => eventStream.abort(err instanceof Error ? err : new Error(String(err))));\n\n // Yield events as they arrive from parallel tools\n let toolsAborted = false;\n try {\n for await (const event of eventStream) {\n yield event;\n }\n } catch (err) {\n // Tool event stream aborted (Ctrl+C) — don't propagate, just mark\n // so the finally block can clean up and the loop can exit.\n if (isAbortError(err) || options.signal?.aborted) {\n toolsAborted = true;\n } else {\n throw err;\n }\n } finally {\n options.signal?.removeEventListener(\"abort\", abortHandler);\n\n // Prevent the Promise.all .then() from mutating toolResults after\n // we finalize and push them into messages.\n toolResultsFinalized = true;\n\n // Ensure every tool_use has a matching tool_result, even on abort.\n // Without this, an aborted turn leaves an orphaned tool_use in the\n // message history which causes Anthropic API 400 errors on the next\n // request.\n const resolvedIds = new Set(toolResults.map((r) => r.toolCallId));\n for (const tc of toolCalls) {\n if (!resolvedIds.has(tc.id)) {\n toolResults.push({\n type: \"tool_result\",\n toolCallId: tc.id,\n content: \"Tool execution was aborted.\",\n isError: true,\n });\n }\n }\n // Guard: cap oversized tool results before they enter conversation history.\n // Uses head+tail strategy to preserve error messages / closing structure at the end.\n if (options.maxToolResultChars) {\n const HARD_MAX = 400_000; // absolute ceiling regardless of context window\n const max = Math.min(options.maxToolResultChars, HARD_MAX);\n for (const tr of toolResults) {\n // Only truncate string content — array content (text+image blocks)\n // is already size-bounded by the image resizer.\n if (typeof tr.content === \"string\" && tr.content.length > max) {\n // Keep 70% head + 30% tail to preserve errors/diagnostics at the end\n const headChars = Math.floor(max * 0.7);\n const tailChars = max - headChars;\n const head = tr.content.slice(0, headChars);\n const tail = tr.content.slice(-tailChars);\n const omitted = tr.content.length - headChars - tailChars;\n tr.content = head + `\\n\\n[... ${omitted} characters omitted ...]\\n\\n` + tail;\n }\n }\n }\n\n messages.push({ role: \"tool\", content: toolResults });\n }\n\n // Exit loop after cleaning up aborted tools\n if (toolsAborted) break;\n\n // ── Steering messages: inject user messages queued during tool execution ──\n // Polled after tools complete so the next LLM call sees them in context.\n if (options.getSteeringMessages) {\n const steering = await options.getSteeringMessages();\n if (steering && steering.length > 0) {\n for (const msg of steering) {\n yield { type: \"steering_message\" as const, content: msg.content };\n messages.push(msg);\n }\n }\n }\n }\n } finally {\n // Sanitize orphaned server_tool_use blocks on abort.\n // When a stream is aborted mid-server-tool (e.g. web_search), the\n // assistant message containing the server_tool_use may already be in\n // the messages array, but the corresponding web_search_tool_result\n // never arrived. The API rejects the next request with a 400 if it\n // finds an unmatched server_tool_use, so we strip it here.\n sanitizeOrphanedServerTools(messages);\n }\n\n // Exceeded max turns — return last assistant message\n let lastAssistant: AssistantMessage | undefined;\n for (let i = messages.length - 1; i >= 0; i--) {\n if (messages[i]!.role === \"assistant\") {\n lastAssistant = messages[i] as AssistantMessage;\n break;\n }\n }\n\n yield {\n type: \"agent_done\" as const,\n totalTurns: turn,\n totalUsage: { ...totalUsage },\n };\n\n return {\n message: lastAssistant ?? { role: \"assistant\" as const, content: [] },\n totalTurns: turn,\n totalUsage: { ...totalUsage },\n };\n}\n\nfunction normalizeToolResult(raw: ToolExecuteResult): StructuredToolResult {\n return typeof raw === \"string\" ? { content: raw } : raw;\n}\n\n/** Flatten tool result content to a plain-text preview for the tool_call_end event.\n * Image blocks become a \"[image]\" placeholder so the UI has something to render. */\nfunction toolResultPreview(content: ToolResultContent): string {\n if (typeof content === \"string\") return content;\n return content\n .map((block) => (block.type === \"text\" ? block.text : `[image ${block.mediaType}]`))\n .join(\"\\n\");\n}\n\nfunction extractToolCalls(content: string | ContentPart[]): ToolCall[] {\n if (typeof content === \"string\") return [];\n return content.filter((part): part is ToolCall => part.type === \"tool_call\");\n}\n\n/**\n * Remove orphaned server_tool_use blocks from the last assistant message.\n * When a stream is aborted mid-server-tool (e.g. web_search), the assistant\n * message may contain a server_tool_call without a matching server_tool_result.\n * The API rejects the next request if these are unmatched.\n */\nfunction sanitizeOrphanedServerTools(messages: Message[]): void {\n // Find the last assistant message\n for (let i = messages.length - 1; i >= 0; i--) {\n const msg = messages[i]!;\n if (msg.role !== \"assistant\") continue;\n if (typeof msg.content === \"string\" || !Array.isArray(msg.content)) break;\n\n // Collect server_tool_call ids and matched server_tool_result ids\n const serverToolIds = new Set<string>();\n const resultToolIds = new Set<string>();\n for (const part of msg.content) {\n if (part.type === \"server_tool_call\") serverToolIds.add(part.id);\n if (part.type === \"server_tool_result\") resultToolIds.add(part.toolUseId);\n }\n\n // Find unmatched server_tool_call blocks\n const orphanedIds = new Set<string>();\n for (const id of serverToolIds) {\n if (!resultToolIds.has(id)) orphanedIds.add(id);\n }\n\n if (orphanedIds.size === 0) break;\n\n // Strip orphaned server_tool_call blocks from the content\n const filtered = msg.content.filter(\n (part) => !(part.type === \"server_tool_call\" && orphanedIds.has(part.id)),\n );\n\n if (filtered.length === 0) {\n // Nothing left — remove the entire message\n messages.splice(i, 1);\n } else {\n (msg as { content: ContentPart[] }).content = filtered;\n }\n break;\n }\n}\n\n/**\n * Ensure every assistant message with tool_call blocks is immediately followed\n * by a tool message with matching tool_result entries. This prevents Anthropic\n * API 400 errors (\"tool_use ids found without tool_result blocks immediately\n * after\") that can occur after compaction, session restore, or abort recovery.\n *\n * Repairs in-place by inserting synthetic tool_result messages where needed.\n */\nfunction repairToolPairingAdjacent(messages: Message[]): void {\n for (let i = 0; i < messages.length; i++) {\n const msg = messages[i]!;\n if (msg.role !== \"assistant\") continue;\n if (typeof msg.content === \"string\" || !Array.isArray(msg.content)) continue;\n\n const toolCallIds = (msg.content as ContentPart[])\n .filter((p) => p.type === \"tool_call\")\n .map((p) => (p as ContentPart & { type: \"tool_call\"; id: string }).id);\n if (toolCallIds.length === 0) continue;\n\n const next = messages[i + 1];\n if (next?.role === \"tool\" && Array.isArray(next.content)) {\n // Tool message exists — check for missing results\n const existingIds = new Set((next.content as ToolResult[]).map((r) => r.toolCallId));\n const missing = toolCallIds.filter((id) => !existingIds.has(id));\n if (missing.length > 0) {\n for (const id of missing) {\n (next.content as ToolResult[]).push({\n type: \"tool_result\",\n toolCallId: id,\n content: \"Tool execution was interrupted.\",\n isError: true,\n });\n }\n }\n } else {\n // No tool message follows — insert a synthetic one\n messages.splice(i + 1, 0, {\n role: \"tool\" as const,\n content: toolCallIds.map((id) => ({\n type: \"tool_result\" as const,\n toolCallId: id,\n content: \"Tool execution was interrupted.\",\n isError: true,\n })),\n });\n }\n }\n\n // Reverse repair: strip tool_result entries whose tool_use_id has no matching\n // tool_call in the preceding assistant message. This can happen when compaction\n // or stall recovery removes an assistant message but leaves its tool_result behind.\n const toolCallIdSet = new Set<string>();\n for (let i = 0; i < messages.length; i++) {\n const msg = messages[i]!;\n if (msg.role === \"assistant\" && Array.isArray(msg.content)) {\n for (const p of msg.content as ContentPart[]) {\n if (p.type === \"tool_call\") toolCallIdSet.add((p as ToolCall).id);\n }\n }\n if (msg.role === \"tool\" && Array.isArray(msg.content)) {\n const results = msg.content as ToolResult[];\n const filtered = results.filter((r) => toolCallIdSet.has(r.toolCallId));\n if (filtered.length === 0) {\n // Entire tool message is orphaned — remove it\n messages.splice(i, 1);\n i--;\n } else if (filtered.length < results.length) {\n (msg as { content: ToolResult[] }).content = filtered;\n }\n }\n }\n}\n"],"mappings":";AAAA,SAAS,eAAAA,oBAAiC;;;ACA1C;AAAA,EACE;AAAA,EACA;AAAA,OAQK;AAWP,IAAM,oBAAoB;AAW1B,IAAI,UAAqC;AAGlC,SAAS,oBAAoB,IAAqC;AACvE,YAAU;AACZ;AAEA,SAAS,KAAK,OAAe,MAAsC;AACjE,YAAU,OAAO,IAAI;AACvB;AAMO,SAAS,aAAa,KAAuB;AAClD,MAAI,EAAE,eAAe,OAAQ,QAAO;AACpC,MAAI,IAAI,SAAS,aAAc,QAAO;AACtC,QAAM,MAAM,IAAI,QAAQ,YAAY;AACpC,SAAO,IAAI,SAAS,SAAS,KAAK,IAAI,SAAS,OAAO;AACxD;AAQO,SAAS,kBAAkB,KAAuB;AACvD,MAAI,EAAE,eAAe,OAAQ,QAAO;AACpC,MAAI,eAAe,GAAG,EAAG,QAAO;AAChC,QAAM,MAAM,IAAI,QAAQ,YAAY;AACpC,SACE,IAAI,SAAS,oBAAoB,KACjC,IAAI,SAAS,iBAAiB,KAC9B,IAAI,SAAS,mBAAmB,KAChC,IAAI,SAAS,yBAAyB,KACtC,IAAI,SAAS,yBAAyB,KACtC,IAAI,SAAS,wBAAwB,KACrC,IAAI,SAAS,8BAA8B,KAC3C,IAAI,SAAS,4BAA4B,KACzC,IAAI,SAAS,m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AAAA,EAC7B;AAAA,EAEA,OAAO,SAA8B;AACnC,QAAI,KAAK,UAAU;AACjB,YAAM,IAAI,MAAM,0BAA0B;AAAA,IAC5C;AACA,SAAK,WAAW;AAEhB,SAAK,SAAS,KAAK,EAAE,MAAM,QAAQ,QAAQ,CAAC;AAE5C,UAAM,oBAAkC;AAAA,MACtC,GAAG,KAAK;AAAA,MACR,qBAAqB,YAAY;AAC/B,cAAM,eAAgB,MAAM,KAAK,QAAQ,sBAAsB,KAAM,CAAC;AACtE,cAAM,SAAS,KAAK,cAAc,OAAO,CAAC;AAC1C,cAAM,MAAM,CAAC,GAAI,gBAAgB,CAAC,GAAI,GAAG,MAAM;AAC/C,eAAO,IAAI,SAAS,IAAI,MAAM;AAAA,MAChC;AAAA,MACA,qBAAqB,YAAY;AAC/B,cAAM,eAAgB,MAAM,KAAK,QAAQ,sBAAsB,KAAM,CAAC;AACtE,cAAM,SAAS,KAAK,cAAc,OAAO,CAAC;AAC1C,cAAM,MAAM,CAAC,GAAI,gBAAgB,CAAC,GAAI,GAAG,MAAM;AAC/C,eAAO,IAAI,SAAS,IAAI,MAAM;AAAA,MAChC;AAAA,IACF;AAEA,UAAM,YAAY,UAAU,KAAK,UAAU,iBAAiB;AAC5D,WAAO,IAAI,YAAY,WAAW,MAAM;AACtC,WAAK,WAAW;AAAA,IAClB,CAAC;AAAA,EACH;AACF;","names":["EventStream","EventStream"]}
1
+ {"version":3,"sources":["../src/agent.ts","../src/agent-loop.ts"],"sourcesContent":["import { EventStream, type Message } from \"@kenkaiiii/gg-ai\";\nimport { agentLoop } from \"./agent-loop.js\";\nimport type { AgentEvent, AgentOptions, AgentResult } from \"./types.js\";\n\n// ── AgentStream ─────────────────────────────────────────────\n\n/**\n * Dual-nature result: async iterable for streaming events,\n * thenable for awaiting the final AgentResult.\n *\n * ```ts\n * // Stream events\n * for await (const event of agent.prompt(\"hello\")) { ... }\n *\n * // Or just await the result\n * const result = await agent.prompt(\"hello\");\n * ```\n */\nexport class AgentStream implements AsyncIterable<AgentEvent> {\n private events: EventStream<AgentEvent>;\n private resultPromise: Promise<AgentResult>;\n private resolveResult!: (r: AgentResult) => void;\n private rejectResult!: (e: Error) => void;\n private hasConsumer = false;\n\n constructor(generator: AsyncGenerator<AgentEvent, AgentResult>, onDone: () => void) {\n this.events = new EventStream<AgentEvent>();\n this.resultPromise = new Promise<AgentResult>((resolve, reject) => {\n this.resolveResult = resolve;\n this.rejectResult = reject;\n });\n this.pump(generator, onDone);\n }\n\n private async pump(\n generator: AsyncGenerator<AgentEvent, AgentResult>,\n onDone: () => void,\n ): Promise<void> {\n try {\n let next = await generator.next();\n while (!next.done) {\n this.events.push(next.value);\n next = await generator.next();\n }\n this.events.close();\n this.resolveResult(next.value);\n } catch (err) {\n const error = err instanceof Error ? err : new Error(String(err));\n this.events.abort(error);\n this.rejectResult(error);\n } finally {\n onDone();\n }\n }\n\n [Symbol.asyncIterator](): AsyncIterator<AgentEvent> {\n this.hasConsumer = true;\n return this.events[Symbol.asyncIterator]();\n }\n\n then<TResult1 = AgentResult, TResult2 = never>(\n onfulfilled?: ((value: AgentResult) => TResult1 | PromiseLike<TResult1>) | null,\n onrejected?: ((reason: unknown) => TResult2 | PromiseLike<TResult2>) | null,\n ): Promise<TResult1 | TResult2> {\n this.drainEvents().catch(() => {});\n return this.resultPromise.then(onfulfilled, onrejected);\n }\n\n private async drainEvents(): Promise<void> {\n if (this.hasConsumer) return;\n this.hasConsumer = true;\n for await (const _ of this.events) {\n // consume silently\n }\n }\n}\n\n// ── Agent ───────────────────────────────────────────────────\n\nexport class Agent {\n private messages: Message[] = [];\n private _running = false;\n private options: AgentOptions;\n private steeringQueue: Message[] = [];\n private followUpQueue: Message[] = [];\n\n constructor(options: AgentOptions) {\n this.options = options;\n if (options.system) {\n this.messages.push({ role: \"system\", content: options.system });\n }\n if (options.priorMessages && options.priorMessages.length > 0) {\n this.messages.push(...options.priorMessages);\n }\n }\n\n /** Snapshot of the current message history. Used for session persistence. */\n getMessages(): Message[] {\n return [...this.messages];\n }\n\n /**\n * Swap the abort signal used for subsequent prompts. Call this after\n * `controller.abort()` so the next prompt() call gets a fresh, unaborted\n * signal — without losing message history.\n */\n setSignal(signal: AbortSignal | undefined): void {\n this.options = { ...this.options, signal };\n }\n\n get running(): boolean {\n return this._running;\n }\n\n /** Queue a steering message for injection after current tool execution completes. */\n steer(msg: Message): void {\n this.steeringQueue.push(msg);\n }\n\n /** Queue a follow-up message for injection when the agent would otherwise stop. */\n followUp(msg: Message): void {\n this.followUpQueue.push(msg);\n }\n\n prompt(content: string): AgentStream {\n if (this._running) {\n throw new Error(\"Agent is already running\");\n }\n this._running = true;\n\n this.messages.push({ role: \"user\", content });\n\n const optionsWithQueues: AgentOptions = {\n ...this.options,\n getSteeringMessages: async () => {\n const callerResult = (await this.options.getSteeringMessages?.()) ?? [];\n const queued = this.steeringQueue.splice(0);\n const all = [...(callerResult ?? []), ...queued];\n return all.length > 0 ? all : null;\n },\n getFollowUpMessages: async () => {\n const callerResult = (await this.options.getFollowUpMessages?.()) ?? [];\n const queued = this.followUpQueue.splice(0);\n const all = [...(callerResult ?? []), ...queued];\n return all.length > 0 ? all : null;\n },\n };\n\n const generator = agentLoop(this.messages, optionsWithQueues);\n return new AgentStream(generator, () => {\n this._running = false;\n });\n }\n}\n","import { ZodError, prettifyError } from \"zod\";\nimport {\n stream,\n EventStream,\n type Message,\n type ToolCall,\n type ToolResult,\n type ToolResultContent,\n type Usage,\n type ContentPart,\n type AssistantMessage,\n} from \"@kenkaiiii/gg-ai\";\nimport type {\n AgentEvent,\n AgentOptions,\n AgentResult,\n AgentTool,\n ToolContext,\n ToolExecuteResult,\n StructuredToolResult,\n} from \"./types.js\";\n\nconst DEFAULT_MAX_TURNS = 200;\n\n/**\n * Lightweight stream diagnostic callback. When set, the agent loop calls this\n * at every phase boundary with timing and state info. This lets the hosting\n * app (ggcoder, come-alive, etc.) log stall diagnostics without the agent\n * package needing fs/process dependencies.\n */\nexport type StreamDiagnosticFn = (phase: string, data?: Record<string, unknown>) => void;\n\n/** Global diagnostic hook — set by the hosting app before calling agentLoop. */\nlet _diagFn: StreamDiagnosticFn | null = null;\n\n/** Register a diagnostic callback for stream stall tracing. */\nexport function setStreamDiagnostic(fn: StreamDiagnosticFn | null): void {\n _diagFn = fn;\n}\n\nfunction diag(phase: string, data?: Record<string, unknown>): void {\n _diagFn?.(phase, data);\n}\n\n/**\n * Detect abort errors — user-initiated cancellation or AbortSignal.\n * These should be caught and handled gracefully, not re-thrown.\n */\nexport function isAbortError(err: unknown): boolean {\n if (!(err instanceof Error)) return false;\n if (err.name === \"AbortError\") return true;\n const msg = err.message.toLowerCase();\n return msg.includes(\"aborted\") || msg.includes(\"abort\");\n}\n\n/**\n * Detect context window overflow errors from LLM providers.\n *\n * Patterns drawn from observed errors across Anthropic, OpenAI, OpenAI Codex,\n * Bedrock, Ollama, and OpenAI-compatible Chinese providers (GLM, Kimi, MiniMax).\n */\nexport function isContextOverflow(err: unknown): boolean {\n if (!(err instanceof Error)) return false;\n if (isBillingError(err)) return false;\n const msg = err.message.toLowerCase();\n return (\n msg.includes(\"prompt is too long\") ||\n msg.includes(\"prompt too long\") ||\n msg.includes(\"input is too long\") ||\n msg.includes(\"context_length_exceeded\") ||\n msg.includes(\"context_window_exceeded\") ||\n msg.includes(\"maximum context length\") ||\n msg.includes(\"exceeds model context window\") ||\n msg.includes(\"exceeds the context window\") ||\n msg.includes(\"content_too_large\") ||\n msg.includes(\"request_too_large\") ||\n msg.includes(\"reduce the length\") ||\n msg.includes(\"please shorten\") ||\n (msg.includes(\"token\") && msg.includes(\"exceed\"))\n );\n}\n\n/**\n * Detect billing/quota errors — these should NOT be retried.\n * GLM returns HTTP 429 with \"Insufficient balance\" for quota exhaustion.\n */\nexport function isBillingError(err: unknown): boolean {\n if (!(err instanceof Error)) return false;\n const msg = err.message.toLowerCase();\n return (\n msg.includes(\"insufficient balance\") ||\n msg.includes(\"no resource package\") ||\n msg.includes(\"quota exceeded\") ||\n msg.includes(\"billing\") ||\n msg.includes(\"recharge\") ||\n msg.includes(\"subscription plan\") ||\n msg.includes(\"does not yet include access\") ||\n msg.includes(\"token quota\") ||\n msg.includes(\"exceeded_current_quota_error\") ||\n msg.includes(\"check your account balance\")\n );\n}\n\n/**\n * Detect overloaded/rate-limit errors from LLM providers.\n * HTTP 429 (rate limit) or 529/503 (overloaded).\n * Excludes billing/quota errors which won't resolve with a retry.\n */\n/**\n * Detect tool pairing errors — orphaned tool_use or tool_result blocks.\n * These are 400 errors that can be recovered by repairing the message history.\n */\nexport function isToolPairingError(err: unknown): boolean {\n if (!(err instanceof Error)) return false;\n const msg = err.message.toLowerCase();\n return (\n (msg.includes(\"tool_use\") && msg.includes(\"tool_result\")) ||\n msg.includes(\"unexpected `tool_use_id`\") ||\n msg.includes(\"tool_use ids found without\") ||\n // Moonshot/OpenAI-compatible: \"tool call id <id> is not found\"\n (msg.includes(\"tool call id\") && msg.includes(\"is not found\"))\n );\n}\n\n/**\n * Distinguish rate-limit (HTTP 429) from server-side overload (HTTP 529).\n * Returns null for errors that should not enter the overload-retry bucket.\n * Both kinds use the same backoff schedule, but the UI shows different copy\n * and the log line records the true cause.\n */\nexport function classifyOverload(err: unknown): \"rate_limit\" | \"overloaded\" | null {\n if (!(err instanceof Error)) return null;\n if (isBillingError(err)) return null;\n const msg = err.message.toLowerCase();\n if (\n msg.includes(\"rate_limit\") ||\n msg.includes(\"rate limit\") ||\n msg.includes(\"too many requests\") ||\n msg.includes(\"429\")\n ) {\n return \"rate_limit\";\n }\n if (msg.includes(\"overloaded\") || msg.includes(\"529\")) {\n return \"overloaded\";\n }\n return null;\n}\n\nexport function isOverloaded(err: unknown): boolean {\n return classifyOverload(err) !== null;\n}\n\n/**\n * Detect malformed-stream errors — the SDK's SSE decoder threw a JSON parse\n * error mid-stream, typically because a chunk was truncated or corrupted by\n * an intermediary (CDN, proxy). Same class of transport failure as a stall:\n * replaying the request — and ideally flipping to non-streaming — recovers.\n */\nexport function isMalformedStream(err: unknown): boolean {\n if (!(err instanceof Error)) return false;\n if (err.name === \"SyntaxError\") return true;\n const cause = (err as { cause?: unknown }).cause;\n if (cause instanceof Error && cause.name === \"SyntaxError\") return true;\n const msg = err.message;\n // V8 JSON.parse error messages: \"Expected ... in JSON at position N\"\n // and \"Unexpected token ... in JSON at position N\"\n return /\\bin JSON at position \\d+/i.test(msg);\n}\n\n/**\n * Detect socket-level transport failures — the remote peer (or an\n * intermediary) closed the TCP connection mid-stream before the response\n * finished. Surfaces as `TypeError: terminated` from undici/fetch, or as\n * `ECONNRESET` / `socket hang up` / `UND_ERR_SOCKET` from the underlying\n * Node http layer. Undici nests the real cause one or more levels deep,\n * so we walk the `.cause` chain. Same recovery as a stall: replay the\n * request, optionally as non-streaming.\n */\nexport function isTransportFailure(err: unknown): boolean {\n const codes = new Set([\n \"ECONNRESET\",\n \"ECONNREFUSED\",\n \"ECONNABORTED\",\n \"ETIMEDOUT\",\n \"EPIPE\",\n \"EHOSTUNREACH\",\n \"ENETUNREACH\",\n \"ENOTFOUND\",\n \"UND_ERR_SOCKET\",\n \"UND_ERR_CONNECT_TIMEOUT\",\n \"UND_ERR_HEADERS_TIMEOUT\",\n \"UND_ERR_BODY_TIMEOUT\",\n \"UND_ERR_RESPONSE_STATUS_CODE\",\n \"UND_ERR_REQ_CONTENT_LENGTH_MISMATCH\",\n \"UND_ERR_RES_CONTENT_LENGTH_MISMATCH\",\n ]);\n const messages = [\n /^terminated$/i,\n /\\bother side closed\\b/i,\n /\\bsocket hang up\\b/i,\n /\\bfetch failed\\b/i,\n /\\bbody timeout error\\b/i,\n /\\bsse stream disconnected\\b/i,\n /\\bfailed to reconnect sse stream\\b/i,\n ];\n const seen = new Set<unknown>();\n let cur: unknown = err;\n while (cur && typeof cur === \"object\" && !seen.has(cur)) {\n seen.add(cur);\n const e = cur as { code?: unknown; message?: unknown; cause?: unknown };\n if (typeof e.code === \"string\" && codes.has(e.code)) return true;\n if (typeof e.message === \"string\") {\n for (const re of messages) if (re.test(e.message)) return true;\n }\n cur = e.cause;\n }\n return false;\n}\n\n/**\n * Promise-returning sleep that rejects with AbortError if `signal` fires.\n * Used by retry backoffs so ESC/Ctrl+C cancel immediately instead of having\n * to wait out the full delay (up to 30s per overload retry × 10 retries).\n */\nfunction abortableSleep(ms: number, signal?: AbortSignal): Promise<void> {\n if (signal?.aborted) {\n return Promise.reject(new DOMException(\"Aborted\", \"AbortError\"));\n }\n return new Promise<void>((resolve, reject) => {\n let onAbort: (() => void) | null = null;\n const timer = setTimeout(() => {\n if (onAbort) signal?.removeEventListener(\"abort\", onAbort);\n resolve();\n }, ms);\n onAbort = () => {\n clearTimeout(timer);\n reject(new DOMException(\"Aborted\", \"AbortError\"));\n };\n signal?.addEventListener(\"abort\", onAbort, { once: true });\n });\n}\n\nexport async function* agentLoop(\n messages: Message[],\n options: AgentOptions,\n): AsyncGenerator<AgentEvent, AgentResult> {\n const maxTurns = options.maxTurns ?? DEFAULT_MAX_TURNS;\n const maxContinuations = options.maxContinuations ?? 5;\n const toolMap = new Map<string, AgentTool>((options.tools ?? []).map((t) => [t.name, t]));\n\n const totalUsage: Usage = { inputTokens: 0, outputTokens: 0 };\n let turn = 0;\n let firstTurn = true;\n let consecutivePauses = 0;\n let toolPairingRepaired = false;\n let overloadRetries = 0;\n let emptyResponseRetries = 0;\n let stallRetries = 0;\n let overflowCompactionAttempts = 0;\n // Non-streaming fallback mode. After repeated stream stalls, flip to a\n // plain non-streaming request/response -- often survives broken SSE\n // connections (transient CDN / proxy issues) that streaming retries cannot.\n let useNonStreamingFallback = false;\n const MAX_OVERLOAD_RETRIES = 10;\n const MAX_EMPTY_RESPONSE_RETRIES = 2;\n const MAX_STALL_RETRIES = 5;\n const MAX_OVERFLOW_COMPACTIONS = 2;\n // After this many streaming stalls in a row, switch to non-streaming mode\n // for the remaining stall retries. Keeps the first two retries fast (the\n // cheap \"transient glitch\" case) before paying for a full response round-trip.\n const STALL_RETRIES_BEFORE_NON_STREAMING = 2;\n const STALL_DELAY_MS = 1_000; // Brief pause before retry -- just enough to avoid tight loops\n const OVERLOAD_BASE_DELAY_MS = 2_000;\n const OVERLOAD_MAX_DELAY_MS = 30_000;\n const STREAM_FIRST_EVENT_TIMEOUT_MS = 45_000; // 45s to get first event (Opus thinks long)\n const STREAM_IDLE_TIMEOUT_MS = 30_000; // 30s between events once streaming starts\n // Anthropic models can pause 10-20s mid-stream while computing the next chunk\n // (e.g. generating tool call args for a large write). 10s was too aggressive\n // and caused false \"stream stalled\" errors, especially in plan mode.\n const STREAM_HARD_TIMEOUT_MS = 90_000; // 90s absolute cap before output starts\n // Once output events (text_delta) are actively streaming, extend the hard\n // timeout -- long responses (plan mode, detailed explanations) can legitimately\n // take 2-3+ minutes while events flow continuously.\n const STREAM_OUTPUT_HARD_TIMEOUT_MS = 300_000; // 5min hard cap once output is flowing\n // Reasoning models (MiMo) can pause 3-5 minutes between thinking and output\n // generation. Once we've seen thinking events, extend timeouts significantly.\n const STREAM_THINKING_IDLE_TIMEOUT_MS = 300_000; // 5min idle after thinking\n const STREAM_THINKING_HARD_TIMEOUT_MS = 600_000; // 10min hard cap with thinking\n // Non-streaming mode has no per-event idle -- the entire response arrives in\n // one HTTP round-trip. Use a single generous hard cap instead. This matches\n // Claude Code's v2.1.110/111 behaviour: cap non-streaming retries so API\n // unreachability doesn't cause multi-minute hangs, but not so aggressively\n // that slow-but-healthy backends get killed.\n const NON_STREAMING_HARD_TIMEOUT_MS = 300_000; // 5min for full non-streaming response\n // Runaway tool-call circuit breaker. When a model glitches mid-tool-call it\n // can emit tens of thousands of toolcall_delta events without ever closing,\n // burning the entire stall-retry budget (~25 min) on what is clearly a\n // non-recoverable model error. Cap accumulated arg chars and event count;\n // exceeding either is a hard, non-retriable failure. Thresholds are generous\n // enough to allow legitimate large file writes through `write`.\n const MAX_TOOLCALL_DELTA_CHARS = 1_000_000; // 1 MB of accumulated tool-call args\n const MAX_TOOLCALL_DELTA_EVENTS = 20_000; // 20k delta events in one stream\n\n try {\n while (turn < maxTurns) {\n options.signal?.throwIfAborted();\n turn++;\n\n // Estimate message payload size for diagnostics\n let msgChars = 0;\n for (const m of messages) {\n if (typeof m.content === \"string\") msgChars += m.content.length;\n else if (Array.isArray(m.content)) {\n for (const p of m.content) {\n if (\"text\" in p && typeof p.text === \"string\") msgChars += p.text.length;\n if (\"content\" in p && typeof p.content === \"string\") msgChars += p.content.length;\n }\n }\n }\n diag(\"turn_start\", {\n turn,\n messages: messages.length,\n chars: msgChars,\n provider: options.provider,\n model: options.model,\n });\n\n // ── Initial steering poll: catch messages queued before the first LLM call ──\n if (firstTurn && options.getSteeringMessages) {\n const steering = await options.getSteeringMessages();\n if (steering && steering.length > 0) {\n for (const msg of steering) {\n yield { type: \"steering_message\" as const, content: msg.content };\n messages.push(msg);\n }\n }\n }\n firstTurn = false;\n\n // ── Mid-loop context transform (compaction / truncation) ──\n if (options.transformContext) {\n diag(\"transform_start\");\n const transformed = await options.transformContext(messages);\n if (transformed !== messages) {\n diag(\"transform_compacted\", {\n before: messages.length,\n after: transformed.length,\n });\n messages.length = 0;\n messages.push(...transformed);\n }\n diag(\"transform_end\");\n }\n\n // ── Repair tool pairing: ensure every tool_use has an adjacent tool_result ──\n repairToolPairingAdjacent(messages);\n\n // ── Call LLM with overflow recovery ──\n let response;\n // Per-attempt abort controller: allows idle timeout to abort the stream\n // without affecting the caller's signal. The caller's abort is forwarded.\n const streamController = new AbortController();\n let idleTimer: ReturnType<typeof setTimeout> | null = null;\n let hardTimer: ReturnType<typeof setTimeout> | null = null;\n let idleTimedOut = false;\n\n // Stream event counters — declared here so timeout callbacks can access them\n let streamEventCount = 0;\n let lastEventTime = Date.now();\n let streamCallStart = Date.now();\n // Track event types for diagnostics — shows what arrived before a stall\n const eventTypeCounts: Record<string, number> = {};\n let lastEventType = \"\";\n // Runaway tool-call detection — accumulated across all toolcall_delta\n // events in this stream attempt. When tripped we abort the stream and\n // bail out without retrying (the model has glitched, retries won't help).\n let toolcallDeltaChars = 0;\n let toolcallDeltaCount = 0;\n let runawayDetected: { kind: \"chars\" | \"events\"; chars: number; events: number } | null =\n null;\n // Track consumer processing time — helps distinguish \"API stopped sending\"\n // from \"our consumer was slow to pull the next event\"\n let lastYieldEndTime = Date.now();\n let maxConsumerLagMs = 0;\n\n // Forward caller abort to the per-attempt controller\n const forwardAbort = () => streamController.abort();\n options.signal?.addEventListener(\"abort\", forwardAbort, { once: true });\n\n // Three-phase idle timeout:\n // - Before first event: STREAM_FIRST_EVENT_TIMEOUT_MS (45s) -- Opus can\n // take 30s+ to start on large contexts, that's not a stall.\n // - After output event (text_delta, server_toolcall): STREAM_IDLE_TIMEOUT_MS\n // (10s) -- once output is streaming, 10s of silence is dead. Retry fast.\n // - After thinking events only: STREAM_THINKING_IDLE_TIMEOUT_MS (5min) --\n // reasoning models (MiMo) can pause minutes between thinking and output.\n //\n // In non-streaming fallback mode the entire response arrives in a single\n // HTTP round-trip, so the idle timer is disabled -- only the hard timeout\n // applies. Synthesized events all arrive at once when the response returns.\n let hasReceivedEvent = false;\n let hasReceivedThinking = false;\n const resetIdleTimer = () => {\n if (useNonStreamingFallback) return; // no inter-event idle in non-streaming mode\n if (idleTimer) clearTimeout(idleTimer);\n const timeoutMs = hasReceivedEvent\n ? STREAM_IDLE_TIMEOUT_MS\n : hasReceivedThinking\n ? STREAM_THINKING_IDLE_TIMEOUT_MS\n : STREAM_FIRST_EVENT_TIMEOUT_MS;\n idleTimer = setTimeout(() => {\n diag(\"idle_timeout_fired\", {\n events: streamEventCount,\n sinceLastEventMs: Date.now() - lastEventTime,\n lastEventType,\n maxConsumerLagMs,\n phase: hasReceivedEvent\n ? \"mid_stream\"\n : hasReceivedThinking\n ? \"post_thinking\"\n : \"first_event\",\n eventTypes: eventTypeCounts,\n });\n idleTimedOut = true;\n streamController.abort();\n }, timeoutMs);\n };\n\n // Hard timeout: absolute cap per LLM call. Safety net for streams that\n // keep sending sparse events (e.g. keep-alive pings) but never complete.\n // Extended dynamically when thinking events arrive (see thinking_delta handler).\n // Non-streaming fallback uses a single larger cap since there's no stream\n // to observe -- just wait for the full response up to the cap.\n let hardTimeoutMs = useNonStreamingFallback\n ? NON_STREAMING_HARD_TIMEOUT_MS\n : STREAM_HARD_TIMEOUT_MS;\n hardTimer = setTimeout(() => {\n diag(\"hard_timeout_fired\", {\n events: typeof streamEventCount !== \"undefined\" ? streamEventCount : 0,\n nonStreaming: useNonStreamingFallback,\n });\n idleTimedOut = true;\n streamController.abort();\n }, hardTimeoutMs);\n\n try {\n diag(\"stream_call\", { nonStreaming: useNonStreamingFallback });\n streamCallStart = Date.now();\n const result = stream({\n provider: options.provider,\n model: options.model,\n messages,\n tools: options.tools,\n serverTools: options.serverTools,\n webSearch: options.webSearch,\n maxTokens: options.maxTokens,\n temperature: options.temperature,\n thinking: options.thinking,\n apiKey: options.apiKey,\n baseUrl: options.baseUrl,\n signal: streamController.signal,\n accountId: options.accountId,\n cacheRetention: options.cacheRetention,\n promptCacheKey: options.promptCacheKey,\n serviceTier: options.serviceTier,\n supportsImages: options.supportsImages,\n compaction: options.compaction,\n clearToolUses: options.clearToolUses,\n userAgent: options.userAgent,\n // Flip to non-streaming fallback after repeated stream stalls.\n ...(useNonStreamingFallback ? { streaming: false } : {}),\n });\n diag(\"stream_created\", { setupMs: Date.now() - streamCallStart });\n\n // Suppress unhandled rejection if the iterator path throws first\n result.response.catch(() => {});\n\n // Forward streaming deltas — reset idle timer on each event\n streamEventCount = 0;\n hasReceivedEvent = false;\n lastEventTime = Date.now();\n streamCallStart = Date.now();\n // Reset to streamCallStart so the first event's consumerLag reflects\n // network/provider latency, not the time spent before stream() returned.\n lastYieldEndTime = Date.now();\n resetIdleTimer();\n for await (const event of result) {\n // Measure consumer lag: time between finishing previous yield and\n // receiving this event. For event #1 this still includes network/\n // provider latency; for subsequent events it isolates how long\n // React/UI rendering held up the next pull.\n const pullTime = Date.now();\n const consumerLag = pullTime - lastYieldEndTime;\n // Only track mid-stream lag — first event lag is dominated by\n // server-side TTFB and would mask real UI starvation issues.\n if (streamEventCount > 0 && consumerLag > maxConsumerLagMs) {\n maxConsumerLagMs = consumerLag;\n }\n\n streamEventCount++;\n eventTypeCounts[event.type] = (eventTypeCounts[event.type] ?? 0) + 1;\n lastEventType = event.type;\n\n // Flip to mid-stream timeout on confirmed output events — text\n // deltas, completed tool calls, and tool call deltas (large file\n // writes can stream toolcall_delta for minutes without any text_delta).\n // Reasoning models (MiMo) are handled separately below — they can\n // stream hundreds of thinking events then pause minutes before output.\n if (\n (event.type === \"text_delta\" ||\n event.type === \"server_toolcall\" ||\n event.type === \"toolcall_delta\") &&\n !hasReceivedEvent\n ) {\n hasReceivedEvent = true;\n // Extend hard timeout now that output is actively streaming.\n // Long responses (plan mode, detailed code) can exceed 90s while\n // events flow continuously — the idle timeout (10s) catches real stalls.\n if (hardTimer && hardTimeoutMs < STREAM_OUTPUT_HARD_TIMEOUT_MS) {\n clearTimeout(hardTimer);\n hardTimeoutMs = STREAM_OUTPUT_HARD_TIMEOUT_MS;\n hardTimer = setTimeout(() => {\n diag(\"hard_timeout_fired\", { events: streamEventCount });\n idleTimedOut = true;\n streamController.abort();\n }, hardTimeoutMs);\n }\n }\n // Track thinking events — extends idle timeout and hard timeout\n // so reasoning models aren't killed during thinking→output transition.\n if (event.type === \"thinking_delta\" && !hasReceivedThinking) {\n hasReceivedThinking = true;\n // Extend the hard timeout now that we know the model is reasoning\n if (hardTimer) clearTimeout(hardTimer);\n hardTimeoutMs = STREAM_THINKING_HARD_TIMEOUT_MS;\n hardTimer = setTimeout(() => {\n diag(\"hard_timeout_fired\", { events: streamEventCount });\n idleTimedOut = true;\n streamController.abort();\n }, hardTimeoutMs);\n }\n\n const now = Date.now();\n const gap = now - lastEventTime;\n // Log first event and any suspiciously long gaps\n if (streamEventCount === 1) {\n diag(\"first_event\", { type: event.type, ttfMs: now - streamCallStart });\n } else if (gap > 3000) {\n diag(\"slow_gap\", {\n type: event.type,\n gapMs: gap,\n eventNum: streamEventCount,\n sinceStartMs: now - streamCallStart,\n });\n }\n lastEventTime = now;\n resetIdleTimer();\n if (event.type === \"text_delta\") {\n yield { type: \"text_delta\" as const, text: event.text };\n } else if (event.type === \"thinking_delta\") {\n yield { type: \"thinking_delta\" as const, text: event.text };\n } else if (event.type === \"server_toolcall\") {\n yield {\n type: \"server_tool_call\" as const,\n id: event.id,\n name: event.name,\n input: event.input,\n };\n } else if (event.type === \"server_toolresult\") {\n yield {\n type: \"server_tool_result\" as const,\n toolUseId: event.toolUseId,\n resultType: event.resultType,\n data: event.data,\n };\n } else if (event.type === \"toolcall_delta\") {\n const chunkChars = event.argsJson?.length ?? 0;\n toolcallDeltaChars += chunkChars;\n toolcallDeltaCount++;\n if (\n !runawayDetected &&\n (toolcallDeltaChars > MAX_TOOLCALL_DELTA_CHARS ||\n toolcallDeltaCount > MAX_TOOLCALL_DELTA_EVENTS)\n ) {\n runawayDetected = {\n kind: toolcallDeltaChars > MAX_TOOLCALL_DELTA_CHARS ? \"chars\" : \"events\",\n chars: toolcallDeltaChars,\n events: toolcallDeltaCount,\n };\n diag(\"runaway_toolcall_detected\", {\n ...runawayDetected,\n provider: options.provider,\n model: options.model,\n });\n streamController.abort();\n }\n yield {\n type: \"toolcall_delta\" as const,\n chars: chunkChars,\n };\n }\n lastYieldEndTime = Date.now();\n }\n\n diag(\"stream_done\", {\n events: streamEventCount,\n totalMs: Date.now() - streamCallStart,\n maxConsumerLagMs,\n eventTypes: eventTypeCounts,\n });\n response = await result.response;\n } catch (err) {\n const errMsg = err instanceof Error ? err.message : String(err);\n diag(\"stream_error\", {\n error: errMsg.slice(0, 200),\n events: streamEventCount,\n totalMs: Date.now() - streamCallStart,\n idleTimedOut,\n aborted: !!options.signal?.aborted,\n eventTypes: eventTypeCounts,\n provider: options.provider,\n model: options.model,\n });\n // Context overflow: try a forced compaction before giving up.\n // The pre-turn transformContext check uses estimated tokens, which can\n // underestimate code-heavy content. When the API confirms overflow we\n // compact unconditionally and retry the turn, capped at\n // MAX_OVERFLOW_COMPACTIONS to avoid loops when compaction can't reduce\n // enough (e.g. single huge user message).\n if (isContextOverflow(err)) {\n if (options.transformContext && overflowCompactionAttempts < MAX_OVERFLOW_COMPACTIONS) {\n overflowCompactionAttempts++;\n diag(\"overflow_compact_start\", {\n attempt: overflowCompactionAttempts,\n maxAttempts: MAX_OVERFLOW_COMPACTIONS,\n messages: messages.length,\n });\n try {\n const compacted = await options.transformContext(messages, { force: true });\n if (compacted !== messages && compacted.length < messages.length) {\n messages.length = 0;\n messages.push(...compacted);\n diag(\"overflow_compact_success\", {\n attempt: overflowCompactionAttempts,\n messages: messages.length,\n });\n yield {\n type: \"retry\" as const,\n reason: \"overflow_compact\" as const,\n attempt: overflowCompactionAttempts,\n maxAttempts: MAX_OVERFLOW_COMPACTIONS,\n delayMs: 0,\n };\n turn--;\n continue;\n }\n diag(\"overflow_compact_noop\", {\n attempt: overflowCompactionAttempts,\n before: messages.length,\n after: compacted.length,\n });\n } catch (compactErr) {\n diag(\"overflow_compact_failed\", {\n error: compactErr instanceof Error ? compactErr.message : String(compactErr),\n });\n }\n }\n yield { type: \"error\" as const, error: err instanceof Error ? err : new Error(errMsg) };\n throw err;\n }\n // Overloaded / rate-limited: exponential backoff, retry up to 10 times\n const overloadKind = classifyOverload(err);\n if (overloadRetries < MAX_OVERLOAD_RETRIES && overloadKind) {\n overloadRetries++;\n const delayMs = Math.min(\n OVERLOAD_BASE_DELAY_MS * 2 ** (overloadRetries - 1),\n OVERLOAD_MAX_DELAY_MS,\n );\n diag(\"retry\", {\n reason: overloadKind,\n attempt: overloadRetries,\n maxAttempts: MAX_OVERLOAD_RETRIES,\n delayMs,\n });\n yield {\n type: \"retry\" as const,\n reason: overloadKind,\n attempt: overloadRetries,\n maxAttempts: MAX_OVERLOAD_RETRIES,\n delayMs,\n };\n await abortableSleep(delayMs, options.signal);\n turn--; // Don't count the failed turn\n continue;\n }\n // Stream stall: the API connection hung without closing.\n // Malformed stream: the SDK's SSE decoder hit truncated/corrupted JSON.\n // Both are transport failures — retry with exponential backoff and flip\n // to non-streaming mode after STALL_RETRIES_BEFORE_NON_STREAMING attempts,\n // since broken SSE often recovers when replayed as plain HTTP.\n // Runaway tool-call: the model never closed a tool-call block and\n // blew past the size/count caps. Retrying just reproduces the loop,\n // so surface a clear error and stop. Checked before the abort branch\n // since we ourselves aborted the stream to break the runaway.\n if (runawayDetected) {\n diag(\"runaway_toolcall_aborted\", {\n ...runawayDetected,\n provider: options.provider,\n model: options.model,\n });\n const detail =\n runawayDetected.kind === \"chars\"\n ? `${(runawayDetected.chars / 1024).toFixed(0)} KB of tool-call arguments`\n : `${runawayDetected.events} tool-call delta events`;\n yield {\n type: \"error\" as const,\n error: new Error(\n `The model glitched mid-tool-call and produced ${detail} without closing the call. ` +\n `This is usually an upstream model bug — try the same request again or switch models. ` +\n `Your conversation is preserved.`,\n ),\n };\n break;\n }\n const malformed = isMalformedStream(err);\n const socketDrop = isTransportFailure(err);\n const transportFailure =\n (idleTimedOut || malformed || socketDrop) && !options.signal?.aborted;\n if (transportFailure && stallRetries < MAX_STALL_RETRIES) {\n stallRetries++;\n const cause = malformed\n ? \"malformed_stream\"\n : socketDrop\n ? \"socket_drop\"\n : \"stream_stall\";\n if (!useNonStreamingFallback && stallRetries >= STALL_RETRIES_BEFORE_NON_STREAMING) {\n useNonStreamingFallback = true;\n diag(\"non_streaming_fallback_enabled\", {\n stallRetries,\n provider: options.provider,\n model: options.model,\n cause,\n });\n }\n const delayMs = Math.min(STALL_DELAY_MS * 2 ** (stallRetries - 1), 8_000);\n diag(\"retry\", {\n reason: cause,\n attempt: stallRetries,\n maxAttempts: MAX_STALL_RETRIES,\n delayMs,\n events: streamEventCount,\n nonStreaming: useNonStreamingFallback,\n });\n yield {\n type: \"retry\" as const,\n reason: \"stream_stall\" as const,\n attempt: stallRetries,\n maxAttempts: MAX_STALL_RETRIES,\n delayMs,\n silent: stallRetries <= 2,\n };\n await abortableSleep(delayMs, options.signal);\n turn--; // Don't count the failed turn\n continue;\n }\n // Stream stall retries exhausted — surface a clear error so the UI\n // can distinguish \"gave up after stalls\" from \"completed normally\".\n if (transportFailure) {\n diag(\"stall_exhausted\", {\n stallRetries: MAX_STALL_RETRIES,\n provider: options.provider,\n model: options.model,\n });\n yield {\n type: \"error\" as const,\n error: new Error(\n `The API provider's stream stalled ${MAX_STALL_RETRIES} times — the provider may be experiencing capacity issues. ` +\n `Your conversation is preserved. Send another message to retry.`,\n ),\n };\n break;\n }\n // Tool pairing 400: orphaned tool_result or tool_use in message history.\n // Run repair and retry once — if repair can't fix it, surface the error.\n if (isToolPairingError(err) && !toolPairingRepaired) {\n toolPairingRepaired = true;\n diag(\"tool_pairing_repair\", { error: errMsg.slice(0, 200) });\n repairToolPairingAdjacent(messages);\n turn--;\n continue;\n }\n // Abort errors (user cancellation) — exit loop cleanly instead of\n // crashing the process with an unhandled rejection.\n if (isAbortError(err) || options.signal?.aborted) {\n diag(\"aborted\", { turn, provider: options.provider, model: options.model });\n break;\n }\n // Unhandled error — log before throwing so the crash is traceable\n diag(\"unhandled_error\", {\n error: errMsg.slice(0, 500),\n turn,\n provider: options.provider,\n model: options.model,\n });\n throw err;\n } finally {\n if (idleTimer) clearTimeout(idleTimer);\n if (hardTimer) clearTimeout(hardTimer);\n options.signal?.removeEventListener(\"abort\", forwardAbort);\n }\n\n overloadRetries = 0;\n stallRetries = 0;\n\n // Detect empty/degenerate responses — the API occasionally returns 0 tokens\n // with no content, or \"thinks\" without producing actionable output.\n // Reasoning models (MiMo, DeepSeek) may report outputTokens > 0 from\n // thinking alone while producing no text or tool calls — still a dud.\n const contentArr = Array.isArray(response.message.content) ? response.message.content : null;\n const hasActionableContent =\n response.message.content !== \"\" &&\n contentArr !== null &&\n contentArr.some(\n (p) => p.type === \"text\" || p.type === \"tool_call\" || p.type === \"server_tool_call\",\n );\n if (!hasActionableContent) {\n if (emptyResponseRetries < MAX_EMPTY_RESPONSE_RETRIES) {\n emptyResponseRetries++;\n diag(\"retry\", {\n reason: \"empty_response\",\n attempt: emptyResponseRetries,\n maxAttempts: MAX_EMPTY_RESPONSE_RETRIES,\n provider: options.provider,\n model: options.model,\n contentTypes: contentArr?.map((p) => p.type).join(\",\") ?? \"empty\",\n });\n yield {\n type: \"retry\" as const,\n reason: \"empty_response\" as const,\n attempt: emptyResponseRetries,\n maxAttempts: MAX_EMPTY_RESPONSE_RETRIES,\n delayMs: 0,\n };\n turn--; // Don't count the failed turn — keep useNonStreamingFallback set\n // so the retry doesn't bounce back into a streaming connection that\n // will stall again with the same upstream problem.\n continue;\n }\n // Exhausted retries — fall through and let the agent finish\n }\n emptyResponseRetries = 0;\n\n // Only clear the non-streaming fallback after an actionable response —\n // an empty non-streaming reply means the upstream issue hasn't resolved,\n // so staying in non-streaming mode avoids retrying into another stall.\n useNonStreamingFallback = false;\n\n // Accumulate usage\n totalUsage.inputTokens += response.usage.inputTokens;\n totalUsage.outputTokens += response.usage.outputTokens;\n if (response.usage.cacheRead) {\n totalUsage.cacheRead = (totalUsage.cacheRead ?? 0) + response.usage.cacheRead;\n }\n if (response.usage.cacheWrite) {\n totalUsage.cacheWrite = (totalUsage.cacheWrite ?? 0) + response.usage.cacheWrite;\n }\n\n // Append assistant message to conversation\n messages.push(response.message);\n\n yield {\n type: \"turn_end\" as const,\n turn,\n stopReason: response.stopReason,\n usage: response.usage,\n };\n\n // Server-side tool hit iteration limit — re-send to continue.\n // Do NOT add an extra user message; the API detects the trailing\n // server_tool_use block and resumes automatically.\n if (response.stopReason === \"pause_turn\") {\n consecutivePauses++;\n if (consecutivePauses >= maxContinuations) {\n break; // Safety limit — fall through to agent_done below\n }\n continue;\n }\n consecutivePauses = 0;\n\n // Extract tool calls — separate client-executed from provider built-in (e.g. Moonshot $web_search)\n const allToolCalls = extractToolCalls(response.message.content);\n\n // If no tool calls to execute, check for steering messages before stopping.\n // Check content (not just stopReason) because some providers (e.g. GLM)\n // return finish_reason=\"stop\" even when tool calls are present.\n if (response.stopReason !== \"tool_use\" && allToolCalls.length === 0) {\n // Check for queued steering messages — if present, inject and continue\n // the loop instead of returning (follow-up pattern).\n if (options.getSteeringMessages) {\n const steering = await options.getSteeringMessages();\n if (steering && steering.length > 0) {\n for (const msg of steering) {\n yield { type: \"steering_message\" as const, content: msg.content };\n messages.push(msg);\n }\n continue; // Next iteration will call LLM with injected messages\n }\n }\n // Follow-up: lower priority than steering — only when agent would otherwise stop.\n if (options.getFollowUpMessages) {\n const followUp = await options.getFollowUpMessages();\n if (followUp && followUp.length > 0) {\n for (const msg of followUp) {\n yield { type: \"follow_up_message\" as const, content: msg.content };\n messages.push(msg);\n }\n continue;\n }\n }\n yield {\n type: \"agent_done\" as const,\n totalTurns: turn,\n totalUsage: { ...totalUsage },\n };\n return {\n message: response.message,\n totalTurns: turn,\n totalUsage: { ...totalUsage },\n };\n }\n const toolCalls: ToolCall[] = [];\n const toolResults: ToolResult[] = [];\n\n for (const tc of allToolCalls) {\n if (tc.name.startsWith(\"$\")) {\n // Provider built-in tool (e.g. Moonshot $web_search) — not locally executed.\n // Still needs a tool_result for the message history round-trip.\n toolResults.push({\n type: \"tool_result\",\n toolCallId: tc.id,\n content: JSON.stringify(tc.args),\n });\n } else {\n toolCalls.push(tc);\n }\n }\n const eventStream = new EventStream<AgentEvent>();\n\n // Launch all tool calls in parallel\n const executions = toolCalls.map(async (toolCall) => {\n const startTime = Date.now();\n\n eventStream.push({\n type: \"tool_call_start\" as const,\n toolCallId: toolCall.id,\n name: toolCall.name,\n args: toolCall.args,\n });\n\n let resultContent: ToolResultContent;\n let details: unknown;\n let isError = false;\n\n const tool = toolMap.get(toolCall.name);\n if (!tool) {\n resultContent = `Unknown tool: ${toolCall.name}`;\n isError = true;\n } else {\n try {\n const parsed = tool.parameters.parse(toolCall.args);\n const ctx: ToolContext = {\n signal: options.signal ?? AbortSignal.timeout(300_000),\n toolCallId: toolCall.id,\n onUpdate: (update: unknown) => {\n eventStream.push({\n type: \"tool_call_update\" as const,\n toolCallId: toolCall.id,\n update,\n });\n },\n };\n const raw = await tool.execute(parsed, ctx);\n const normalized = normalizeToolResult(raw);\n resultContent = normalized.content;\n details = normalized.details;\n } catch (err) {\n isError = true;\n if (err instanceof ZodError) {\n // Zod v4's default `.message` is a JSON dump of `.issues`, which\n // the model can't act on. Prettify into \"field X: expected Y,\n // received Z\" lines so the next call comes back with valid args.\n resultContent =\n `Invalid arguments for tool \\`${toolCall.name}\\`:\\n` +\n prettifyError(err) +\n \"\\nRe-issue the call with each field as the correct type.\";\n } else {\n resultContent = err instanceof Error ? err.message : String(err);\n }\n }\n }\n\n const durationMs = Date.now() - startTime;\n\n eventStream.push({\n type: \"tool_call_end\" as const,\n toolCallId: toolCall.id,\n result: toolResultPreview(resultContent),\n details,\n isError,\n durationMs,\n });\n\n return { toolCallId: toolCall.id, content: resultContent, isError };\n });\n\n // Abort the tool event stream when the signal fires so Ctrl+C\n // doesn't hang waiting for long-running tools to finish.\n const abortHandler = () => eventStream.abort(new Error(\"aborted\"));\n options.signal?.addEventListener(\"abort\", abortHandler, { once: true });\n\n // Close event stream when all tools complete.\n // Track whether the finally block has already consumed toolResults\n // to prevent the race where .then() mutates toolResults after\n // messages.push() has already captured the array by reference.\n let toolResultsFinalized = false;\n\n Promise.all(executions)\n .then((results) => {\n if (toolResultsFinalized) return;\n const resultsMap = new Map(results.map((r) => [r.toolCallId, r]));\n for (const tc of toolCalls) {\n const r = resultsMap.get(tc.id)!;\n toolResults.push({\n type: \"tool_result\",\n toolCallId: tc.id,\n content: r.content,\n isError: r.isError || undefined,\n });\n }\n eventStream.close();\n })\n .catch((err) => eventStream.abort(err instanceof Error ? err : new Error(String(err))));\n\n // Yield events as they arrive from parallel tools\n let toolsAborted = false;\n try {\n for await (const event of eventStream) {\n yield event;\n }\n } catch (err) {\n // Tool event stream aborted (Ctrl+C) — don't propagate, just mark\n // so the finally block can clean up and the loop can exit.\n if (isAbortError(err) || options.signal?.aborted) {\n toolsAborted = true;\n } else {\n throw err;\n }\n } finally {\n options.signal?.removeEventListener(\"abort\", abortHandler);\n\n // Prevent the Promise.all .then() from mutating toolResults after\n // we finalize and push them into messages.\n toolResultsFinalized = true;\n\n // Ensure every tool_use has a matching tool_result, even on abort.\n // Without this, an aborted turn leaves an orphaned tool_use in the\n // message history which causes Anthropic API 400 errors on the next\n // request.\n const resolvedIds = new Set(toolResults.map((r) => r.toolCallId));\n for (const tc of toolCalls) {\n if (!resolvedIds.has(tc.id)) {\n toolResults.push({\n type: \"tool_result\",\n toolCallId: tc.id,\n content: \"Tool execution was aborted.\",\n isError: true,\n });\n }\n }\n // Guard: cap oversized tool results before they enter conversation history.\n // Uses head+tail strategy to preserve error messages / closing structure at the end.\n if (options.maxToolResultChars) {\n const HARD_MAX = 400_000; // absolute ceiling regardless of context window\n const max = Math.min(options.maxToolResultChars, HARD_MAX);\n for (const tr of toolResults) {\n // Only truncate string content — array content (text+image blocks)\n // is already size-bounded by the image resizer.\n if (typeof tr.content === \"string\" && tr.content.length > max) {\n // Keep 70% head + 30% tail to preserve errors/diagnostics at the end\n const headChars = Math.floor(max * 0.7);\n const tailChars = max - headChars;\n const head = tr.content.slice(0, headChars);\n const tail = tr.content.slice(-tailChars);\n const omitted = tr.content.length - headChars - tailChars;\n tr.content = head + `\\n\\n[... ${omitted} characters omitted ...]\\n\\n` + tail;\n }\n }\n }\n\n messages.push({ role: \"tool\", content: toolResults });\n }\n\n // Exit loop after cleaning up aborted tools\n if (toolsAborted) break;\n\n // ── Steering messages: inject user messages queued during tool execution ──\n // Polled after tools complete so the next LLM call sees them in context.\n if (options.getSteeringMessages) {\n const steering = await options.getSteeringMessages();\n if (steering && steering.length > 0) {\n for (const msg of steering) {\n yield { type: \"steering_message\" as const, content: msg.content };\n messages.push(msg);\n }\n }\n }\n }\n } finally {\n // Sanitize orphaned server_tool_use blocks on abort.\n // When a stream is aborted mid-server-tool (e.g. web_search), the\n // assistant message containing the server_tool_use may already be in\n // the messages array, but the corresponding web_search_tool_result\n // never arrived. The API rejects the next request with a 400 if it\n // finds an unmatched server_tool_use, so we strip it here.\n sanitizeOrphanedServerTools(messages);\n }\n\n // Exceeded max turns — return last assistant message\n let lastAssistant: AssistantMessage | undefined;\n for (let i = messages.length - 1; i >= 0; i--) {\n if (messages[i]!.role === \"assistant\") {\n lastAssistant = messages[i] as AssistantMessage;\n break;\n }\n }\n\n yield {\n type: \"agent_done\" as const,\n totalTurns: turn,\n totalUsage: { ...totalUsage },\n };\n\n return {\n message: lastAssistant ?? { role: \"assistant\" as const, content: [] },\n totalTurns: turn,\n totalUsage: { ...totalUsage },\n };\n}\n\nfunction normalizeToolResult(raw: ToolExecuteResult): StructuredToolResult {\n return typeof raw === \"string\" ? { content: raw } : raw;\n}\n\n/** Flatten tool result content to a plain-text preview for the tool_call_end event.\n * Image blocks become a \"[image]\" placeholder so the UI has something to render. */\nfunction toolResultPreview(content: ToolResultContent): string {\n if (typeof content === \"string\") return content;\n return content\n .map((block) => (block.type === \"text\" ? block.text : `[image ${block.mediaType}]`))\n .join(\"\\n\");\n}\n\nfunction extractToolCalls(content: string | ContentPart[]): ToolCall[] {\n if (typeof content === \"string\") return [];\n return content.filter((part): part is ToolCall => part.type === \"tool_call\");\n}\n\n/**\n * Remove orphaned server_tool_use blocks from the last assistant message.\n * When a stream is aborted mid-server-tool (e.g. web_search), the assistant\n * message may contain a server_tool_call without a matching server_tool_result.\n * The API rejects the next request if these are unmatched.\n */\nfunction sanitizeOrphanedServerTools(messages: Message[]): void {\n // Find the last assistant message\n for (let i = messages.length - 1; i >= 0; i--) {\n const msg = messages[i]!;\n if (msg.role !== \"assistant\") continue;\n if (typeof msg.content === \"string\" || !Array.isArray(msg.content)) break;\n\n // Collect server_tool_call ids and matched server_tool_result ids\n const serverToolIds = new Set<string>();\n const resultToolIds = new Set<string>();\n for (const part of msg.content) {\n if (part.type === \"server_tool_call\") serverToolIds.add(part.id);\n if (part.type === \"server_tool_result\") resultToolIds.add(part.toolUseId);\n }\n\n // Find unmatched server_tool_call blocks\n const orphanedIds = new Set<string>();\n for (const id of serverToolIds) {\n if (!resultToolIds.has(id)) orphanedIds.add(id);\n }\n\n if (orphanedIds.size === 0) break;\n\n // Strip orphaned server_tool_call blocks from the content\n const filtered = msg.content.filter(\n (part) => !(part.type === \"server_tool_call\" && orphanedIds.has(part.id)),\n );\n\n if (filtered.length === 0) {\n // Nothing left — remove the entire message\n messages.splice(i, 1);\n } else {\n (msg as { content: ContentPart[] }).content = filtered;\n }\n break;\n }\n}\n\n/**\n * Ensure every assistant message with tool_call blocks is immediately followed\n * by a tool message with matching tool_result entries. This prevents Anthropic\n * API 400 errors (\"tool_use ids found without tool_result blocks immediately\n * after\") that can occur after compaction, session restore, or abort recovery.\n *\n * Repairs in-place by inserting synthetic tool_result messages where needed.\n */\nfunction repairToolPairingAdjacent(messages: Message[]): void {\n for (let i = 0; i < messages.length; i++) {\n const msg = messages[i]!;\n if (msg.role !== \"assistant\") continue;\n if (typeof msg.content === \"string\" || !Array.isArray(msg.content)) continue;\n\n const toolCallIds = (msg.content as ContentPart[])\n .filter((p) => p.type === \"tool_call\")\n .map((p) => (p as ContentPart & { type: \"tool_call\"; id: string }).id);\n if (toolCallIds.length === 0) continue;\n\n const next = messages[i + 1];\n if (next?.role === \"tool\" && Array.isArray(next.content)) {\n // Tool message exists — check for missing results\n const existingIds = new Set((next.content as ToolResult[]).map((r) => r.toolCallId));\n const missing = toolCallIds.filter((id) => !existingIds.has(id));\n if (missing.length > 0) {\n for (const id of missing) {\n (next.content as ToolResult[]).push({\n type: \"tool_result\",\n toolCallId: id,\n content: \"Tool execution was interrupted.\",\n isError: true,\n });\n }\n }\n } else {\n // No tool message follows — insert a synthetic one\n messages.splice(i + 1, 0, {\n role: \"tool\" as const,\n content: toolCallIds.map((id) => ({\n type: \"tool_result\" as const,\n toolCallId: id,\n content: \"Tool execution was interrupted.\",\n isError: true,\n })),\n });\n }\n }\n\n // Reverse repair: strip tool_result entries whose tool_use_id has no matching\n // tool_call in the preceding assistant message. This can happen when compaction\n // or stall recovery removes an assistant message but leaves its tool_result behind.\n const toolCallIdSet = new Set<string>();\n for (let i = 0; i < messages.length; i++) {\n const msg = messages[i]!;\n if (msg.role === \"assistant\" && Array.isArray(msg.content)) {\n for (const p of msg.content as ContentPart[]) {\n if (p.type === \"tool_call\") toolCallIdSet.add((p as ToolCall).id);\n }\n }\n if (msg.role === \"tool\" && Array.isArray(msg.content)) {\n const results = msg.content as ToolResult[];\n const filtered = results.filter((r) => toolCallIdSet.has(r.toolCallId));\n if (filtered.length === 0) {\n // Entire tool message is orphaned — remove it\n messages.splice(i, 1);\n i--;\n } else if (filtered.length < results.length) {\n (msg as { content: ToolResult[] }).content = filtered;\n }\n }\n }\n}\n"],"mappings":";AAAA,SAAS,eAAAA,oBAAiC;;;ACA1C,SAAS,UAAU,qBAAqB;AACxC;AAAA,EACE;AAAA,EACA;AAAA,OAQK;AAWP,IAAM,oBAAoB;AAW1B,IAAI,UAAqC;AAGlC,SAAS,oBAAoB,IAAqC;AACvE,YAAU;AACZ;AAEA,SAAS,KAAK,OAAe,MAAsC;AACjE,YAAU,OAAO,IAAI;AACvB;AAMO,SAAS,aAAa,KAAuB;AAClD,MAAI,EAAE,eAAe,OAAQ,QAAO;AACpC,MAAI,IAAI,SAAS,aAAc,QAAO;AACtC,QAAM,MAAM,IAAI,QAAQ,YAAY;AACpC,SAAO,IAAI,SAAS,SAAS,KAAK,IAAI,SAAS,OAAO;AACxD;AAQO,SAAS,kBAAkB,KAAuB;AACvD,MAAI,EAAE,eAAe,OAAQ,QAAO;AACpC,MAAI,eAAe,GAAG,EAAG,QAAO;AAChC,QAAM,MAAM,IAAI,QAAQ,YAAY;AACpC,SACE,IAAI,SAAS,oBAAoB,KACjC,IAAI,SAAS,iBAAiB,KAC9B,IAAI,SAAS,mBAAmB,KAChC,IAAI,SAAS,yBAAyB,KACtC,IAAI,SAAS,yBAAyB,KACtC,IAAI,SAAS,wBAAwB,KACrC,IAAI,SAAS,8BAA8B,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AACrB,WAAO,KAAK;AAAA,EACd;AAAA;AAAA,EAGA,MAAM,KAAoB;AACxB,SAAK,cAAc,KAAK,GAAG;AAAA,EAC7B;AAAA;AAAA,EAGA,SAAS,KAAoB;AAC3B,SAAK,cAAc,KAAK,GAAG;AAAA,EAC7B;AAAA,EAEA,OAAO,SAA8B;AACnC,QAAI,KAAK,UAAU;AACjB,YAAM,IAAI,MAAM,0BAA0B;AAAA,IAC5C;AACA,SAAK,WAAW;AAEhB,SAAK,SAAS,KAAK,EAAE,MAAM,QAAQ,QAAQ,CAAC;AAE5C,UAAM,oBAAkC;AAAA,MACtC,GAAG,KAAK;AAAA,MACR,qBAAqB,YAAY;AAC/B,cAAM,eAAgB,MAAM,KAAK,QAAQ,sBAAsB,KAAM,CAAC;AACtE,cAAM,SAAS,KAAK,cAAc,OAAO,CAAC;AAC1C,cAAM,MAAM,CAAC,GAAI,gBAAgB,CAAC,GAAI,GAAG,MAAM;AAC/C,eAAO,IAAI,SAAS,IAAI,MAAM;AAAA,MAChC;AAAA,MACA,qBAAqB,YAAY;AAC/B,cAAM,eAAgB,MAAM,KAAK,QAAQ,sBAAsB,KAAM,CAAC;AACtE,cAAM,SAAS,KAAK,cAAc,OAAO,CAAC;AAC1C,cAAM,MAAM,CAAC,GAAI,gBAAgB,CAAC,GAAI,GAAG,MAAM;AAC/C,eAAO,IAAI,SAAS,IAAI,MAAM;AAAA,MAChC;AAAA,IACF;AAEA,UAAM,YAAY,UAAU,KAAK,UAAU,iBAAiB;AAC5D,WAAO,IAAI,YAAY,WAAW,MAAM;AACtC,WAAK,WAAW;AAAA,IAClB,CAAC;AAAA,EACH;AACF;","names":["EventStream","EventStream"]}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@kenkaiiii/gg-agent",
3
- "version": "4.3.183",
3
+ "version": "4.3.185",
4
4
  "type": "module",
5
5
  "description": "Agentic loop system with tool execution for LLMs",
6
6
  "license": "MIT",
@@ -24,7 +24,7 @@
24
24
  ],
25
25
  "dependencies": {
26
26
  "zod": "^4.4.3",
27
- "@kenkaiiii/gg-ai": "4.3.183"
27
+ "@kenkaiiii/gg-ai": "4.3.185"
28
28
  },
29
29
  "devDependencies": {
30
30
  "typescript": "^6.0.3",