@prometheus-ai/agent-core 0.5.0

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.
Files changed (61) hide show
  1. package/CHANGELOG.md +7 -0
  2. package/README.md +473 -0
  3. package/dist/types/agent-loop.d.ts +55 -0
  4. package/dist/types/agent.d.ts +331 -0
  5. package/dist/types/append-only-context.d.ts +113 -0
  6. package/dist/types/compaction/branch-summarization.d.ts +94 -0
  7. package/dist/types/compaction/compaction.d.ts +183 -0
  8. package/dist/types/compaction/entries.d.ts +103 -0
  9. package/dist/types/compaction/errors.d.ts +26 -0
  10. package/dist/types/compaction/index.d.ts +12 -0
  11. package/dist/types/compaction/messages.d.ts +61 -0
  12. package/dist/types/compaction/openai.d.ts +58 -0
  13. package/dist/types/compaction/pruning.d.ts +19 -0
  14. package/dist/types/compaction/shake.d.ts +82 -0
  15. package/dist/types/compaction/tool-protection.d.ts +17 -0
  16. package/dist/types/compaction/utils.d.ts +32 -0
  17. package/dist/types/compaction.d.ts +1 -0
  18. package/dist/types/harmony-leak.d.ts +118 -0
  19. package/dist/types/index.d.ts +11 -0
  20. package/dist/types/proxy.d.ts +84 -0
  21. package/dist/types/run-collector.d.ts +196 -0
  22. package/dist/types/telemetry.d.ts +588 -0
  23. package/dist/types/thinking.d.ts +17 -0
  24. package/dist/types/types.d.ts +443 -0
  25. package/dist/types/utils/yield.d.ts +52 -0
  26. package/package.json +75 -0
  27. package/src/agent-loop.ts +1418 -0
  28. package/src/agent.ts +1236 -0
  29. package/src/append-only-context.ts +297 -0
  30. package/src/compaction/branch-summarization.ts +339 -0
  31. package/src/compaction/compaction.ts +1155 -0
  32. package/src/compaction/entries.ts +133 -0
  33. package/src/compaction/errors.ts +31 -0
  34. package/src/compaction/index.ts +13 -0
  35. package/src/compaction/messages.ts +212 -0
  36. package/src/compaction/openai.ts +552 -0
  37. package/src/compaction/prompts/auto-handoff-threshold-focus.md +1 -0
  38. package/src/compaction/prompts/branch-summary-context.md +5 -0
  39. package/src/compaction/prompts/branch-summary-preamble.md +2 -0
  40. package/src/compaction/prompts/branch-summary.md +30 -0
  41. package/src/compaction/prompts/compaction-short-summary.md +9 -0
  42. package/src/compaction/prompts/compaction-summary-context.md +5 -0
  43. package/src/compaction/prompts/compaction-summary.md +38 -0
  44. package/src/compaction/prompts/compaction-turn-prefix.md +17 -0
  45. package/src/compaction/prompts/compaction-update-summary.md +45 -0
  46. package/src/compaction/prompts/file-operations.md +10 -0
  47. package/src/compaction/prompts/handoff-document.md +49 -0
  48. package/src/compaction/prompts/summarization-system.md +3 -0
  49. package/src/compaction/pruning.ts +99 -0
  50. package/src/compaction/shake.ts +406 -0
  51. package/src/compaction/tool-protection.ts +55 -0
  52. package/src/compaction/utils.ts +185 -0
  53. package/src/compaction.ts +1 -0
  54. package/src/harmony-leak.ts +456 -0
  55. package/src/index.ts +21 -0
  56. package/src/proxy.ts +326 -0
  57. package/src/run-collector.ts +631 -0
  58. package/src/telemetry.ts +2020 -0
  59. package/src/thinking.ts +19 -0
  60. package/src/types.ts +505 -0
  61. package/src/utils/yield.ts +146 -0
package/src/agent.ts ADDED
@@ -0,0 +1,1236 @@
1
+ /** Agent class that uses the agent-loop directly.
2
+ * No transport abstraction - calls streamSimple via the loop.
3
+ */
4
+ import { isPromise } from "node:util/types";
5
+ import {
6
+ type AssistantMessage,
7
+ type AssistantMessageEvent,
8
+ type CursorExecHandlers,
9
+ type CursorToolResultHandler,
10
+ type Effort,
11
+ getBundledModel,
12
+ type ImageContent,
13
+ type Message,
14
+ type Model,
15
+ type ProviderSessionState,
16
+ type ServiceTier,
17
+ type SimpleStreamOptions,
18
+ streamSimple,
19
+ type TextContent,
20
+ type ThinkingBudgets,
21
+ type ToolChoice,
22
+ type ToolResultMessage,
23
+ } from "@prometheus-ai/ai";
24
+ import { agentLoop, agentLoopContinue } from "./agent-loop";
25
+ import type { AppendOnlyContextManager } from "./append-only-context";
26
+ import type { HarmonyAuditEvent } from "./harmony-leak";
27
+ import type {
28
+ AgentContext,
29
+ AgentEvent,
30
+ AgentLoopConfig,
31
+ AgentMessage,
32
+ AgentState,
33
+ AgentTool,
34
+ AgentToolContext,
35
+ StreamFn,
36
+ ToolCallContext,
37
+ } from "./types";
38
+ import { EventLoopKeepalive } from "./utils/yield";
39
+
40
+ /**
41
+ * Default convertToLlm: Keep only LLM-compatible messages, convert attachments.
42
+ */
43
+ function defaultConvertToLlm(messages: AgentMessage[]): Message[] {
44
+ return messages.filter((m): m is Message => m.role === "user" || m.role === "assistant" || m.role === "toolResult");
45
+ }
46
+
47
+ const ANTHROPIC_OUTPUT_BLOCKED_PREFIX = "Output blocked by conten";
48
+
49
+ function isAnthropicOutputBlockedError(message: string): boolean {
50
+ return message.includes(ANTHROPIC_OUTPUT_BLOCKED_PREFIX);
51
+ }
52
+
53
+ function refreshToolChoiceForActiveTools(
54
+ toolChoice: ToolChoice | undefined,
55
+ tools: AgentContext["tools"] = [],
56
+ ): ToolChoice | undefined {
57
+ if (!toolChoice || typeof toolChoice === "string") {
58
+ return toolChoice;
59
+ }
60
+
61
+ const toolName =
62
+ toolChoice.type === "tool"
63
+ ? toolChoice.name
64
+ : "function" in toolChoice
65
+ ? toolChoice.function.name
66
+ : toolChoice.name;
67
+
68
+ return tools.some(tool => tool.name === toolName) ? toolChoice : undefined;
69
+ }
70
+
71
+ export class AgentBusyError extends Error {
72
+ constructor(
73
+ message: string = "Agent is already processing. Use steer() or followUp() to queue messages, or wait for completion.",
74
+ ) {
75
+ super(message);
76
+ this.name = "AgentBusyError";
77
+ }
78
+ }
79
+ export interface AgentOptions {
80
+ initialState?: Partial<AgentState>;
81
+
82
+ /**
83
+ * Converts AgentMessage[] to LLM-compatible Message[] before each LLM call.
84
+ * Default filters to user/assistant/toolResult and converts attachments.
85
+ */
86
+ convertToLlm?: (messages: AgentMessage[]) => Message[] | Promise<Message[]>;
87
+
88
+ /**
89
+ * Optional transform applied to context before convertToLlm.
90
+ * Use for context pruning, injecting external context, etc.
91
+ */
92
+ transformContext?: (messages: AgentMessage[], signal?: AbortSignal) => Promise<AgentMessage[]>;
93
+
94
+ /**
95
+ * Steering mode: "all" = send all steering messages at once, "one-at-a-time" = one per turn
96
+ */
97
+ steeringMode?: "all" | "one-at-a-time";
98
+
99
+ /**
100
+ * Follow-up mode: "all" = send all follow-up messages at once, "one-at-a-time" = one per turn
101
+ */
102
+ followUpMode?: "all" | "one-at-a-time";
103
+
104
+ /**
105
+ * When to interrupt tool execution for steering messages.
106
+ * - "immediate": check after each tool call (default)
107
+ * - "wait": defer steering until the current turn completes
108
+ */
109
+ interruptMode?: "immediate" | "wait";
110
+
111
+ /**
112
+ * Maximum completed tool calls to accept from one streamed assistant turn before
113
+ * executing the batch. Undefined disables batching.
114
+ */
115
+ maxToolCallsPerTurn?: number;
116
+
117
+ /**
118
+ * API format for Kimi Code provider: "openai" or "anthropic" (default: "anthropic")
119
+ */
120
+ kimiApiFormat?: "openai" | "anthropic";
121
+
122
+ /** Hint that websocket transport should be preferred when supported by the provider implementation. */
123
+ preferWebsockets?: boolean;
124
+
125
+ /**
126
+ * Custom stream function (for proxy backends, etc.). Default uses streamSimple.
127
+ */
128
+ streamFn?: StreamFn;
129
+
130
+ /**
131
+ * Optional session identifier forwarded to LLM providers.
132
+ * Used by providers that support session-based caching (e.g., OpenAI Codex).
133
+ */
134
+ sessionId?: string;
135
+ /**
136
+ * Shared provider state map for session-scoped transport/session caches.
137
+ */
138
+ providerSessionState?: Map<string, ProviderSessionState>;
139
+
140
+ /**
141
+ * Resolves an API key dynamically for each LLM call.
142
+ * Useful for expiring tokens (e.g., GitHub Copilot OAuth).
143
+ */
144
+ getApiKey?: (provider: string) => Promise<string | undefined> | string | undefined;
145
+
146
+ /**
147
+ * Inspect or replace provider payloads before they are sent.
148
+ */
149
+ onPayload?: SimpleStreamOptions["onPayload"];
150
+ /**
151
+ * Inspect provider response metadata after headers arrive and before streaming body consumption.
152
+ */
153
+ onResponse?: SimpleStreamOptions["onResponse"];
154
+ /**
155
+ * Inspect raw Server-Sent Events from HTTP streaming providers.
156
+ */
157
+ onSseEvent?: SimpleStreamOptions["onSseEvent"];
158
+ /**
159
+ * Inspect assistant streaming events before they are emitted to subscribers.
160
+ * Use this when abort decisions must happen before buffered events continue flowing.
161
+ */
162
+ onAssistantMessageEvent?: (message: AssistantMessage, event: AssistantMessageEvent) => void;
163
+
164
+ /**
165
+ * Called when GPT-5 Harmony protocol leakage is detected and mitigated.
166
+ */
167
+ onHarmonyLeak?: (event: HarmonyAuditEvent) => void | Promise<void>;
168
+ /**
169
+ * Custom token budgets for thinking levels (token-based providers only).
170
+ */
171
+ thinkingBudgets?: ThinkingBudgets;
172
+
173
+ /**
174
+ * Sampling temperature for LLM calls. `undefined` uses provider default.
175
+ */
176
+ temperature?: number;
177
+
178
+ /** Additional sampling controls for providers that support them. */
179
+ topP?: number;
180
+ topK?: number;
181
+ minP?: number;
182
+ presencePenalty?: number;
183
+ repetitionPenalty?: number;
184
+ serviceTier?: ServiceTier;
185
+ /**
186
+ * If true, request that the underlying provider omit reasoning/thinking summaries
187
+ * from the response. The model still reasons internally; only the human-readable
188
+ * summary stream is suppressed. Useful when the UI hides thinking blocks anyway.
189
+ */
190
+ hideThinkingSummary?: boolean;
191
+
192
+ /**
193
+ * Maximum delay in milliseconds to wait for a retry when the server requests a long wait.
194
+ * If the server's requested delay exceeds this value, the request fails immediately,
195
+ * allowing higher-level retry logic to handle it with user visibility.
196
+ * Default: 60000 (60 seconds). Set to 0 to disable the cap.
197
+ */
198
+ maxRetryDelayMs?: number;
199
+
200
+ /**
201
+ * Provides tool execution context, resolved per tool call.
202
+ * Use for late-bound UI or session state access.
203
+ */
204
+ getToolContext?: (toolCall?: ToolCallContext) => AgentToolContext | undefined;
205
+
206
+ /**
207
+ * Optional transform applied to tool call arguments before execution.
208
+ * Use for deobfuscating secrets or rewriting arguments.
209
+ */
210
+ transformToolCallArguments?: (args: Record<string, unknown>, toolName: string) => Record<string, unknown>;
211
+
212
+ /** Enable intent tracing schema injection/stripping in the harness. */
213
+ intentTracing?: boolean;
214
+ /** Dynamic tool choice override, resolved per LLM call. */
215
+ getToolChoice?: () => ToolChoice | undefined;
216
+
217
+ /**
218
+ * Cursor exec handlers for local tool execution.
219
+ */
220
+ cursorExecHandlers?: CursorExecHandlers;
221
+
222
+ /**
223
+ * Cursor tool result callback for exec tool responses.
224
+ */
225
+ cursorOnToolResult?: CursorToolResultHandler;
226
+
227
+ /**
228
+ * Called after a tool call has been validated and is about to execute.
229
+ * See {@link AgentLoopConfig.beforeToolCall} for full semantics.
230
+ */
231
+ beforeToolCall?: AgentLoopConfig["beforeToolCall"];
232
+
233
+ /**
234
+ * Called after a tool finishes executing, before `tool_execution_end` and the tool-result
235
+ * message are emitted. See {@link AgentLoopConfig.afterToolCall} for full semantics.
236
+ */
237
+ afterToolCall?: AgentLoopConfig["afterToolCall"];
238
+
239
+ /**
240
+ * Opt-in OpenTelemetry instrumentation. Passing `{}` enables the loop's
241
+ * GenAI-semantic-convention spans using the global tracer provider. See
242
+ * {@link AgentLoopConfig.telemetry} for the full surface.
243
+ */
244
+ telemetry?: AgentLoopConfig["telemetry"];
245
+ /**
246
+ * Immutable context mode — stabilizes system prompt + tool spec bytes
247
+ * across turns so DeepSeek/Anthropic prefix caches hit at maximum rate.
248
+ */
249
+ appendOnlyContext?: AppendOnlyContextManager;
250
+ }
251
+
252
+ export interface AgentPromptOptions {
253
+ toolChoice?: ToolChoice;
254
+ }
255
+
256
+ /** Buffered Cursor tool result with text position at time of call */
257
+ interface CursorToolResultEntry {
258
+ toolResult: ToolResultMessage;
259
+ textLengthAtCall: number;
260
+ }
261
+
262
+ export class Agent {
263
+ #state: AgentState = {
264
+ systemPrompt: [],
265
+ model: getBundledModel("google", "gemini-2.5-flash-lite-preview-06-17"),
266
+ thinkingLevel: undefined,
267
+ tools: [],
268
+ messages: [],
269
+ isStreaming: false,
270
+ streamMessage: null,
271
+ pendingToolCalls: new Set<string>(),
272
+ error: undefined,
273
+ };
274
+
275
+ #listeners = new Set<(e: AgentEvent) => void>();
276
+ #abortController?: AbortController;
277
+ #convertToLlm: (messages: AgentMessage[]) => Message[] | Promise<Message[]>;
278
+ #transformContext?: (messages: AgentMessage[], signal?: AbortSignal) => Promise<AgentMessage[]>;
279
+ #steeringQueue: AgentMessage[] = [];
280
+ #followUpQueue: AgentMessage[] = [];
281
+ #steeringMode: "all" | "one-at-a-time";
282
+ #followUpMode: "all" | "one-at-a-time";
283
+ #interruptMode: "immediate" | "wait";
284
+ #maxToolCallsPerTurn?: number;
285
+ #sessionId?: string;
286
+ #metadata?: Record<string, unknown>;
287
+ #metadataResolver?: (provider: string) => Record<string, unknown> | undefined;
288
+ #providerSessionState?: Map<string, ProviderSessionState>;
289
+ #thinkingBudgets?: ThinkingBudgets;
290
+ #temperature?: number;
291
+ #topP?: number;
292
+ #topK?: number;
293
+ #minP?: number;
294
+ #presencePenalty?: number;
295
+ #repetitionPenalty?: number;
296
+ #serviceTier?: ServiceTier;
297
+ #hideThinkingSummary?: boolean;
298
+ #maxRetryDelayMs?: number;
299
+ #getToolContext?: (toolCall?: ToolCallContext) => AgentToolContext | undefined;
300
+ #cursorExecHandlers?: CursorExecHandlers;
301
+ #cursorOnToolResult?: CursorToolResultHandler;
302
+ #runningPrompt?: Promise<void>;
303
+ #resolveRunningPrompt?: () => void;
304
+ #kimiApiFormat?: "openai" | "anthropic";
305
+ #preferWebsockets?: boolean;
306
+ #transformToolCallArguments?: (args: Record<string, unknown>, toolName: string) => Record<string, unknown>;
307
+ #intentTracing: boolean;
308
+ #getToolChoice?: () => ToolChoice | undefined;
309
+ #onPayload?: SimpleStreamOptions["onPayload"];
310
+ #onResponse?: SimpleStreamOptions["onResponse"];
311
+ #onSseEvent?: SimpleStreamOptions["onSseEvent"];
312
+ #onAssistantMessageEvent?: (message: AssistantMessage, event: AssistantMessageEvent) => void;
313
+ #onHarmonyLeak?: (event: HarmonyAuditEvent) => void | Promise<void>;
314
+ #onBeforeYield?: () => Promise<void> | void;
315
+ #telemetry?: AgentLoopConfig["telemetry"];
316
+ #appendOnlyContext?: AppendOnlyContextManager;
317
+
318
+ /** Buffered Cursor tool results with text length at time of call (for correct ordering) */
319
+ #cursorToolResultBuffer: CursorToolResultEntry[] = [];
320
+
321
+ streamFn: StreamFn;
322
+ getApiKey?: (provider: string) => Promise<string | undefined> | string | undefined;
323
+ /**
324
+ * Hook invoked after tool arguments are validated and before execution.
325
+ * Reassign at any time to swap the implementation (e.g. on extension reload).
326
+ */
327
+ beforeToolCall?: AgentLoopConfig["beforeToolCall"];
328
+ /**
329
+ * Hook invoked after tool execution and before `tool_execution_end` / tool-result
330
+ * message emission. Reassign at any time to swap the implementation.
331
+ */
332
+ afterToolCall?: AgentLoopConfig["afterToolCall"];
333
+
334
+ constructor(opts: AgentOptions = {}) {
335
+ this.#state = { ...this.#state, ...opts.initialState };
336
+ if (opts.initialState?.messages) this.#state.messages = opts.initialState.messages.slice();
337
+ if (opts.initialState?.pendingToolCalls)
338
+ this.#state.pendingToolCalls = new Set(opts.initialState.pendingToolCalls);
339
+ this.#convertToLlm = opts.convertToLlm || defaultConvertToLlm;
340
+ this.#transformContext = opts.transformContext;
341
+ this.#steeringMode = opts.steeringMode || "one-at-a-time";
342
+ this.#followUpMode = opts.followUpMode || "one-at-a-time";
343
+ this.#interruptMode = opts.interruptMode || "immediate";
344
+ this.#maxToolCallsPerTurn = opts.maxToolCallsPerTurn;
345
+ this.streamFn = opts.streamFn || streamSimple;
346
+ this.#sessionId = opts.sessionId;
347
+ this.#providerSessionState = opts.providerSessionState;
348
+ this.#thinkingBudgets = opts.thinkingBudgets;
349
+ this.#temperature = opts.temperature;
350
+ this.#topP = opts.topP;
351
+ this.#topK = opts.topK;
352
+ this.#minP = opts.minP;
353
+ this.#presencePenalty = opts.presencePenalty;
354
+ this.#repetitionPenalty = opts.repetitionPenalty;
355
+ this.#serviceTier = opts.serviceTier;
356
+ this.#hideThinkingSummary = opts.hideThinkingSummary;
357
+ this.#maxRetryDelayMs = opts.maxRetryDelayMs;
358
+ this.getApiKey = opts.getApiKey;
359
+ this.#onPayload = opts.onPayload;
360
+ this.#onResponse = opts.onResponse;
361
+ this.#onSseEvent = opts.onSseEvent;
362
+ this.#getToolContext = opts.getToolContext;
363
+ this.#cursorExecHandlers = opts.cursorExecHandlers;
364
+ this.#cursorOnToolResult = opts.cursorOnToolResult;
365
+ this.#kimiApiFormat = opts.kimiApiFormat;
366
+ this.#preferWebsockets = opts.preferWebsockets;
367
+ this.#transformToolCallArguments = opts.transformToolCallArguments;
368
+ this.#intentTracing = opts.intentTracing === true;
369
+ this.#getToolChoice = opts.getToolChoice;
370
+ this.#onAssistantMessageEvent = opts.onAssistantMessageEvent;
371
+ this.#onHarmonyLeak = opts.onHarmonyLeak;
372
+ this.beforeToolCall = opts.beforeToolCall;
373
+ this.afterToolCall = opts.afterToolCall;
374
+ this.#telemetry = opts.telemetry;
375
+ this.#appendOnlyContext = opts.appendOnlyContext;
376
+ }
377
+
378
+ /**
379
+ * Get the current session ID used for provider caching.
380
+ */
381
+ get sessionId(): string | undefined {
382
+ return this.#sessionId;
383
+ }
384
+
385
+ /**
386
+ * Set the session ID for provider caching.
387
+ * Call this when switching sessions (new session, branch, resume).
388
+ */
389
+ set sessionId(value: string | undefined) {
390
+ this.#sessionId = value;
391
+ }
392
+
393
+ /**
394
+ * Static metadata forwarded to every API request when no resolver is installed
395
+ * (e.g. `metadata.user_id` for Anthropic session attribution). Setting this
396
+ * clears any installed resolver.
397
+ *
398
+ * For live/provider-aware metadata (e.g. Anthropic OAuth `account_uuid` that
399
+ * must reflect the credential selected per-request), use
400
+ * {@link setMetadataResolver} and read via {@link metadataForProvider}.
401
+ */
402
+ get metadata(): Record<string, unknown> | undefined {
403
+ return this.#metadata;
404
+ }
405
+
406
+ set metadata(value: Record<string, unknown> | undefined) {
407
+ this.#metadata = value;
408
+ this.#metadataResolver = undefined;
409
+ }
410
+
411
+ /**
412
+ * Resolve request metadata for the given provider at call time. When a
413
+ * resolver is installed via {@link setMetadataResolver}, it is invoked with
414
+ * the provider string so the result can be scoped (e.g. `account_uuid` is
415
+ * only included for `"anthropic"` requests). Falls back to the static
416
+ * {@link metadata} value when no resolver is set.
417
+ */
418
+ metadataForProvider(provider: string): Record<string, unknown> | undefined {
419
+ if (this.#metadataResolver) return this.#metadataResolver(provider);
420
+ return this.#metadata;
421
+ }
422
+
423
+ /**
424
+ * Install a function that resolves request metadata at call time. The
425
+ * resolver receives the target provider string and can gate provider-specific
426
+ * fields (e.g. `account_uuid` only for `"anthropic"`). Invoked per LLM
427
+ * request by `agent-loop` after `getApiKey` selects the session-sticky
428
+ * credential. Pass `undefined` to clear and revert to the static
429
+ * {@link metadata} value.
430
+ */
431
+ setMetadataResolver(resolver: ((provider: string) => Record<string, unknown> | undefined) | undefined): void {
432
+ this.#metadataResolver = resolver;
433
+ }
434
+
435
+ /**
436
+ * Read the active OpenTelemetry configuration. Returns `undefined` when
437
+ * instrumentation is disabled. Callers spawning child runs (e.g. subagent
438
+ * dispatch) forward this to the child's loop so its spans appear under the
439
+ * parent's active context with the subagent's own identity stamped.
440
+ */
441
+ get telemetry(): AgentLoopConfig["telemetry"] | undefined {
442
+ return this.#telemetry;
443
+ }
444
+
445
+ /**
446
+ * Replace the active OpenTelemetry configuration. Pass `undefined` to
447
+ * disable instrumentation. Applies to the *next* `agentLoop` invocation —
448
+ * in-flight loops keep the configuration they started with.
449
+ */
450
+ setTelemetry(telemetry: AgentLoopConfig["telemetry"] | undefined): void {
451
+ this.#telemetry = telemetry;
452
+ }
453
+
454
+ /**
455
+ * Get provider-scoped mutable session state store.
456
+ */
457
+ get providerSessionState(): Map<string, ProviderSessionState> | undefined {
458
+ return this.#providerSessionState;
459
+ }
460
+
461
+ /**
462
+ * Set provider-scoped mutable session state store.
463
+ */
464
+ set providerSessionState(value: Map<string, ProviderSessionState> | undefined) {
465
+ this.#providerSessionState = value;
466
+ }
467
+
468
+ /**
469
+ * Get the current thinking budgets.
470
+ */
471
+ get thinkingBudgets(): ThinkingBudgets | undefined {
472
+ return this.#thinkingBudgets;
473
+ }
474
+
475
+ /**
476
+ * Set custom thinking budgets for token-based providers.
477
+ */
478
+ set thinkingBudgets(value: ThinkingBudgets | undefined) {
479
+ this.#thinkingBudgets = value;
480
+ }
481
+
482
+ /**
483
+ * Get the current sampling temperature.
484
+ */
485
+ get temperature(): number | undefined {
486
+ return this.#temperature;
487
+ }
488
+
489
+ /**
490
+ * Set sampling temperature for LLM calls. `undefined` uses provider default.
491
+ */
492
+ set temperature(value: number | undefined) {
493
+ this.#temperature = value;
494
+ }
495
+
496
+ get topP(): number | undefined {
497
+ return this.#topP;
498
+ }
499
+
500
+ set topP(value: number | undefined) {
501
+ this.#topP = value;
502
+ }
503
+
504
+ get topK(): number | undefined {
505
+ return this.#topK;
506
+ }
507
+
508
+ set topK(value: number | undefined) {
509
+ this.#topK = value;
510
+ }
511
+
512
+ get minP(): number | undefined {
513
+ return this.#minP;
514
+ }
515
+
516
+ set minP(value: number | undefined) {
517
+ this.#minP = value;
518
+ }
519
+
520
+ get presencePenalty(): number | undefined {
521
+ return this.#presencePenalty;
522
+ }
523
+
524
+ set presencePenalty(value: number | undefined) {
525
+ this.#presencePenalty = value;
526
+ }
527
+
528
+ get repetitionPenalty(): number | undefined {
529
+ return this.#repetitionPenalty;
530
+ }
531
+
532
+ set repetitionPenalty(value: number | undefined) {
533
+ this.#repetitionPenalty = value;
534
+ }
535
+
536
+ get serviceTier(): ServiceTier | undefined {
537
+ return this.#serviceTier;
538
+ }
539
+
540
+ set serviceTier(value: ServiceTier | undefined) {
541
+ this.#serviceTier = value;
542
+ }
543
+
544
+ get hideThinkingSummary(): boolean | undefined {
545
+ return this.#hideThinkingSummary;
546
+ }
547
+
548
+ set hideThinkingSummary(value: boolean | undefined) {
549
+ this.#hideThinkingSummary = value;
550
+ }
551
+
552
+ /**
553
+ * Get the current max retry delay in milliseconds.
554
+ */
555
+ get maxRetryDelayMs(): number | undefined {
556
+ return this.#maxRetryDelayMs;
557
+ }
558
+
559
+ /**
560
+ * Set the maximum delay to wait for server-requested retries.
561
+ * Set to 0 to disable the cap.
562
+ */
563
+ set maxRetryDelayMs(value: number | undefined) {
564
+ this.#maxRetryDelayMs = value;
565
+ }
566
+
567
+ get maxToolCallsPerTurn(): number | undefined {
568
+ return this.#maxToolCallsPerTurn;
569
+ }
570
+
571
+ set maxToolCallsPerTurn(value: number | undefined) {
572
+ this.#maxToolCallsPerTurn = value;
573
+ }
574
+
575
+ get state(): AgentState {
576
+ return this.#state;
577
+ }
578
+
579
+ get appendOnlyContext(): AppendOnlyContextManager | undefined {
580
+ return this.#appendOnlyContext;
581
+ }
582
+
583
+ setAppendOnlyContext(manager?: AppendOnlyContextManager): void {
584
+ this.#appendOnlyContext = manager;
585
+ }
586
+
587
+ subscribe(fn: (e: AgentEvent) => void): () => void {
588
+ this.#listeners.add(fn);
589
+ return () => this.#listeners.delete(fn);
590
+ }
591
+
592
+ setProviderResponseInterceptor(fn: SimpleStreamOptions["onResponse"] | undefined): void {
593
+ this.#onResponse = fn;
594
+ }
595
+
596
+ setRawSseEventInterceptor(fn: SimpleStreamOptions["onSseEvent"] | undefined): void {
597
+ this.#onSseEvent = fn;
598
+ }
599
+
600
+ setAssistantMessageEventInterceptor(
601
+ fn: ((message: AssistantMessage, event: AssistantMessageEvent) => void) | undefined,
602
+ ): void {
603
+ this.#onAssistantMessageEvent = fn;
604
+ }
605
+
606
+ setOnBeforeYield(fn: (() => Promise<void> | void) | undefined): void {
607
+ this.#onBeforeYield = fn;
608
+ }
609
+
610
+ emitExternalEvent(event: AgentEvent) {
611
+ switch (event.type) {
612
+ case "message_start":
613
+ case "message_update":
614
+ this.#state.streamMessage = event.message;
615
+ break;
616
+ case "message_end":
617
+ this.#state.streamMessage = null;
618
+ this.appendMessage(event.message);
619
+ break;
620
+ case "tool_execution_start":
621
+ this.#state.pendingToolCalls.add(event.toolCallId);
622
+ break;
623
+ case "tool_execution_end":
624
+ this.#state.pendingToolCalls.delete(event.toolCallId);
625
+ break;
626
+ }
627
+
628
+ this.#emit(event);
629
+ }
630
+
631
+ // State mutators
632
+ setSystemPrompt(v: string[]) {
633
+ this.#state.systemPrompt = v;
634
+ }
635
+
636
+ setModel(m: Model) {
637
+ this.#state.model = m;
638
+ }
639
+
640
+ setThinkingLevel(l: Effort | undefined) {
641
+ this.#state.thinkingLevel = l;
642
+ }
643
+
644
+ setSteeringMode(mode: "all" | "one-at-a-time") {
645
+ this.#steeringMode = mode;
646
+ }
647
+
648
+ getSteeringMode(): "all" | "one-at-a-time" {
649
+ return this.#steeringMode;
650
+ }
651
+
652
+ setFollowUpMode(mode: "all" | "one-at-a-time") {
653
+ this.#followUpMode = mode;
654
+ }
655
+
656
+ getFollowUpMode(): "all" | "one-at-a-time" {
657
+ return this.#followUpMode;
658
+ }
659
+
660
+ setInterruptMode(mode: "immediate" | "wait") {
661
+ this.#interruptMode = mode;
662
+ }
663
+
664
+ getInterruptMode(): "immediate" | "wait" {
665
+ return this.#interruptMode;
666
+ }
667
+
668
+ setTools(t: AgentTool<any>[]) {
669
+ this.#state.tools = t;
670
+ }
671
+
672
+ replaceMessages(ms: AgentMessage[]) {
673
+ // New array assignment is intentional: caller-owned `ms` may be mutated
674
+ // after handoff; snapshot it so external mutations cannot leak in.
675
+ this.#state.messages = ms.slice();
676
+ }
677
+
678
+ appendMessage(m: AgentMessage) {
679
+ this.#state.messages.push(m);
680
+ }
681
+
682
+ popMessage(): AgentMessage | undefined {
683
+ const removed = this.#state.messages.pop();
684
+ if (removed && this.#state.streamMessage === removed) {
685
+ this.#state.streamMessage = null;
686
+ }
687
+ return removed;
688
+ }
689
+
690
+ /**
691
+ * Queue a steering message to interrupt the agent mid-run.
692
+ * Delivered after current tool execution, skips remaining tools.
693
+ */
694
+ steer(m: AgentMessage) {
695
+ this.#steeringQueue.push(m);
696
+ }
697
+
698
+ /**
699
+ * Queue a follow-up message to be processed after the agent finishes.
700
+ * Delivered only when agent has no more tool calls or steering messages.
701
+ */
702
+ followUp(m: AgentMessage) {
703
+ this.#followUpQueue.push(m);
704
+ }
705
+
706
+ clearSteeringQueue() {
707
+ this.#steeringQueue = [];
708
+ }
709
+
710
+ clearFollowUpQueue() {
711
+ this.#followUpQueue = [];
712
+ }
713
+
714
+ clearAllQueues() {
715
+ this.#steeringQueue = [];
716
+ this.#followUpQueue = [];
717
+ }
718
+
719
+ hasQueuedMessages(): boolean {
720
+ return this.#steeringQueue.length > 0 || this.#followUpQueue.length > 0;
721
+ }
722
+
723
+ #dequeueSteeringMessages(): AgentMessage[] {
724
+ if (this.#steeringMode === "one-at-a-time") {
725
+ if (this.#steeringQueue.length > 0) {
726
+ const first = this.#steeringQueue[0];
727
+ this.#steeringQueue = this.#steeringQueue.slice(1);
728
+ return [first];
729
+ }
730
+ return [];
731
+ }
732
+ const steering = this.#steeringQueue.slice();
733
+ this.#steeringQueue = [];
734
+ return steering;
735
+ }
736
+
737
+ #dequeueFollowUpMessages(): AgentMessage[] {
738
+ if (this.#followUpMode === "one-at-a-time") {
739
+ if (this.#followUpQueue.length > 0) {
740
+ const first = this.#followUpQueue[0];
741
+ this.#followUpQueue = this.#followUpQueue.slice(1);
742
+ return [first];
743
+ }
744
+ return [];
745
+ }
746
+ const followUp = this.#followUpQueue.slice();
747
+ this.#followUpQueue = [];
748
+ return followUp;
749
+ }
750
+
751
+ /**
752
+ * Remove and return the last steering message from the queue (LIFO).
753
+ * Used by dequeue keybinding.
754
+ */
755
+ popLastSteer(): AgentMessage | undefined {
756
+ return this.#steeringQueue.pop();
757
+ }
758
+
759
+ /**
760
+ * Remove and return the last follow-up message from the queue (LIFO).
761
+ * Used by dequeue keybinding.
762
+ */
763
+ popLastFollowUp(): AgentMessage | undefined {
764
+ return this.#followUpQueue.pop();
765
+ }
766
+
767
+ clearMessages() {
768
+ this.#state.messages.length = 0;
769
+ }
770
+
771
+ abort() {
772
+ this.#abortController?.abort();
773
+ }
774
+
775
+ waitForIdle(): Promise<void> {
776
+ return this.#runningPrompt ?? Promise.resolve();
777
+ }
778
+
779
+ reset() {
780
+ this.#state.messages.length = 0;
781
+ this.#state.isStreaming = false;
782
+ this.#state.streamMessage = null;
783
+ this.#state.pendingToolCalls.clear();
784
+ this.#state.error = undefined;
785
+ this.#steeringQueue = [];
786
+ this.#followUpQueue = [];
787
+ }
788
+
789
+ /** Send a prompt with an AgentMessage */
790
+ async prompt(message: AgentMessage | AgentMessage[], options?: AgentPromptOptions): Promise<void>;
791
+ async prompt(input: string, options?: AgentPromptOptions): Promise<void>;
792
+ async prompt(input: string, images?: ImageContent[], options?: AgentPromptOptions): Promise<void>;
793
+ async prompt(
794
+ input: string | AgentMessage | AgentMessage[],
795
+ imagesOrOptions?: ImageContent[] | AgentPromptOptions,
796
+ options?: AgentPromptOptions,
797
+ ) {
798
+ if (this.#state.isStreaming) {
799
+ throw new AgentBusyError();
800
+ }
801
+
802
+ const model = this.#state.model;
803
+ if (!model) throw new Error("No model configured");
804
+
805
+ let msgs: AgentMessage[];
806
+ let promptOptions: AgentPromptOptions | undefined;
807
+ let images: ImageContent[] | undefined;
808
+
809
+ if (Array.isArray(input)) {
810
+ msgs = input;
811
+ promptOptions = imagesOrOptions as AgentPromptOptions | undefined;
812
+ } else if (typeof input === "string") {
813
+ if (Array.isArray(imagesOrOptions)) {
814
+ images = imagesOrOptions;
815
+ promptOptions = options;
816
+ } else {
817
+ promptOptions = imagesOrOptions;
818
+ }
819
+ const content: Array<TextContent | ImageContent> = [{ type: "text", text: input }];
820
+ if (images && images.length > 0) {
821
+ content.push(...images);
822
+ }
823
+ msgs = [
824
+ {
825
+ role: "user",
826
+ content,
827
+ timestamp: Date.now(),
828
+ },
829
+ ];
830
+ } else {
831
+ msgs = [input];
832
+ promptOptions = imagesOrOptions as AgentPromptOptions | undefined;
833
+ }
834
+
835
+ await this.#runLoop(msgs, promptOptions);
836
+ }
837
+
838
+ /**
839
+ * Continue from current context (used for retries and resuming queued messages).
840
+ */
841
+ async continue() {
842
+ if (this.#state.isStreaming) {
843
+ throw new AgentBusyError();
844
+ }
845
+
846
+ const messages = this.#state.messages;
847
+ if (messages.length === 0) {
848
+ throw new Error("No messages to continue from");
849
+ }
850
+ if (messages[messages.length - 1].role === "assistant") {
851
+ const queuedSteering = this.#dequeueSteeringMessages();
852
+ if (queuedSteering.length > 0) {
853
+ await this.#runLoop(queuedSteering, { skipInitialSteeringPoll: true });
854
+ return;
855
+ }
856
+
857
+ const queuedFollowUp = this.#dequeueFollowUpMessages();
858
+ if (queuedFollowUp.length > 0) {
859
+ await this.#runLoop(queuedFollowUp);
860
+ return;
861
+ }
862
+
863
+ throw new Error("Cannot continue from message role: assistant");
864
+ }
865
+
866
+ await this.#runLoop(undefined);
867
+ }
868
+
869
+ /**
870
+ * Run the agent loop.
871
+ * If messages are provided, starts a new conversation turn with those messages.
872
+ * Otherwise, continues from existing context.
873
+ */
874
+ async #runLoop(messages?: AgentMessage[], options?: AgentPromptOptions & { skipInitialSteeringPoll?: boolean }) {
875
+ const model = this.#state.model;
876
+ if (!model) throw new Error("No model configured");
877
+
878
+ let skipInitialSteeringPoll = options?.skipInitialSteeringPoll === true;
879
+ using _ = new EventLoopKeepalive();
880
+ const { promise, resolve } = Promise.withResolvers<void>();
881
+ this.#runningPrompt = promise;
882
+ this.#resolveRunningPrompt = resolve;
883
+
884
+ this.#abortController = new AbortController();
885
+ this.#state.isStreaming = true;
886
+ this.#state.streamMessage = null;
887
+ this.#state.error = undefined;
888
+
889
+ // Clear Cursor tool result buffer at start of each run
890
+ this.#cursorToolResultBuffer = [];
891
+
892
+ const reasoning = this.#state.thinkingLevel;
893
+
894
+ const context: AgentContext = {
895
+ systemPrompt: this.#state.systemPrompt,
896
+ messages: this.#state.messages.slice(),
897
+ tools: this.#state.tools,
898
+ };
899
+
900
+ const cursorOnToolResult =
901
+ this.#cursorExecHandlers || this.#cursorOnToolResult
902
+ ? async (message: ToolResultMessage) => {
903
+ let finalMessage = message;
904
+ if (this.#cursorOnToolResult) {
905
+ try {
906
+ const updated = await this.#cursorOnToolResult(message);
907
+ if (updated) {
908
+ finalMessage = updated;
909
+ }
910
+ } catch {}
911
+ }
912
+ // Buffer tool result with current text length for correct ordering later.
913
+ // Cursor executes tools server-side during streaming, so the assistant message
914
+ // already incorporates results. We buffer here and emit in correct order
915
+ // when the assistant message ends.
916
+ const textLength = this.#getAssistantTextLength(this.#state.streamMessage);
917
+ this.#cursorToolResultBuffer.push({ toolResult: finalMessage, textLengthAtCall: textLength });
918
+ return finalMessage;
919
+ }
920
+ : undefined;
921
+
922
+ const getToolChoice = () =>
923
+ this.#getToolChoice?.() ?? refreshToolChoiceForActiveTools(options?.toolChoice, this.#state.tools);
924
+
925
+ const config: AgentLoopConfig = {
926
+ model,
927
+ reasoning,
928
+ temperature: this.#temperature,
929
+ topP: this.#topP,
930
+ topK: this.#topK,
931
+ minP: this.#minP,
932
+ presencePenalty: this.#presencePenalty,
933
+ repetitionPenalty: this.#repetitionPenalty,
934
+ serviceTier: this.#serviceTier,
935
+ hideThinkingSummary: this.#hideThinkingSummary,
936
+ interruptMode: this.#interruptMode,
937
+ maxToolCallsPerTurn: this.#maxToolCallsPerTurn,
938
+ sessionId: this.#sessionId,
939
+ metadata: this.#metadataResolver ? undefined : this.#metadata,
940
+ metadataResolver: this.#metadataResolver,
941
+ providerSessionState: this.#providerSessionState,
942
+ thinkingBudgets: this.#thinkingBudgets,
943
+ maxRetryDelayMs: this.#maxRetryDelayMs,
944
+ kimiApiFormat: this.#kimiApiFormat,
945
+ preferWebsockets: this.#preferWebsockets,
946
+ convertToLlm: this.#convertToLlm,
947
+ transformContext: this.#transformContext,
948
+ onPayload: this.#onPayload,
949
+ onResponse: this.#onResponse,
950
+ onSseEvent: this.#onSseEvent,
951
+ getApiKey: this.getApiKey,
952
+ getToolContext: this.#getToolContext,
953
+ syncContextBeforeModelCall: async context => {
954
+ if (this.#listeners.size > 0) {
955
+ await Bun.sleep(0);
956
+ }
957
+ context.systemPrompt = this.#state.systemPrompt;
958
+ context.tools = this.#state.tools;
959
+ },
960
+ cursorExecHandlers: this.#cursorExecHandlers,
961
+ cursorOnToolResult,
962
+ transformToolCallArguments: this.#transformToolCallArguments,
963
+ intentTracing: this.#intentTracing,
964
+ appendOnlyContext: this.#appendOnlyContext,
965
+ beforeToolCall: this.beforeToolCall ? (ctx, signal) => this.beforeToolCall?.(ctx, signal) : undefined,
966
+ afterToolCall: this.afterToolCall ? (ctx, signal) => this.afterToolCall?.(ctx, signal) : undefined,
967
+ onAssistantMessageEvent: this.#onAssistantMessageEvent,
968
+ onHarmonyLeak: this.#onHarmonyLeak,
969
+ getToolChoice,
970
+ getReasoning: () => this.#state.thinkingLevel,
971
+ getSteeringMessages: async () => {
972
+ if (skipInitialSteeringPoll) {
973
+ skipInitialSteeringPoll = false;
974
+ return [];
975
+ }
976
+ return this.#dequeueSteeringMessages();
977
+ },
978
+ getFollowUpMessages: async () => this.#dequeueFollowUpMessages(),
979
+ onBeforeYield: () => this.#onBeforeYield?.(),
980
+ telemetry: this.#telemetry,
981
+ };
982
+
983
+ let partial: AgentMessage | null = null;
984
+
985
+ try {
986
+ const stream = messages
987
+ ? agentLoop(messages, context, config, this.#abortController.signal, this.streamFn)
988
+ : agentLoopContinue(context, config, this.#abortController.signal, this.streamFn);
989
+
990
+ for await (const event of stream) {
991
+ // Update internal state based on events
992
+ switch (event.type) {
993
+ case "message_start":
994
+ partial = event.message;
995
+ this.#state.streamMessage = event.message;
996
+ break;
997
+
998
+ case "message_update":
999
+ partial = event.message;
1000
+ this.#state.streamMessage = event.message;
1001
+ break;
1002
+
1003
+ case "message_end":
1004
+ partial = null;
1005
+ // Check if this is an assistant message with buffered Cursor tool results.
1006
+ // If so, split the message to emit tool results at the correct position.
1007
+ if (event.message.role === "assistant" && this.#cursorToolResultBuffer.length > 0) {
1008
+ this.#emitCursorSplitAssistantMessage(event.message as AssistantMessage);
1009
+ continue; // Skip default emit - split method handles everything
1010
+ }
1011
+ this.#state.streamMessage = null;
1012
+ this.appendMessage(event.message);
1013
+ break;
1014
+
1015
+ case "tool_execution_start":
1016
+ this.#state.pendingToolCalls.add(event.toolCallId);
1017
+ break;
1018
+
1019
+ case "tool_execution_end":
1020
+ this.#state.pendingToolCalls.delete(event.toolCallId);
1021
+ break;
1022
+
1023
+ case "turn_end":
1024
+ if (event.message.role === "assistant" && (event.message as any).errorMessage) {
1025
+ this.#state.error = (event.message as any).errorMessage;
1026
+ }
1027
+ break;
1028
+
1029
+ case "agent_end":
1030
+ this.#state.isStreaming = false;
1031
+ this.#state.streamMessage = null;
1032
+ break;
1033
+ }
1034
+
1035
+ // Emit to listeners
1036
+ this.#emit(event);
1037
+ }
1038
+
1039
+ // Handle any remaining partial message
1040
+ if (partial && partial.role === "assistant" && Array.isArray(partial.content) && partial.content.length > 0) {
1041
+ const onlyEmpty = !partial.content.some(
1042
+ c =>
1043
+ (c.type === "thinking" && c.thinking.trim().length > 0) ||
1044
+ (c.type === "text" && c.text.trim().length > 0) ||
1045
+ (c.type === "toolCall" && c.name.trim().length > 0),
1046
+ );
1047
+ if (!onlyEmpty) {
1048
+ this.appendMessage(partial);
1049
+ } else {
1050
+ if (this.#abortController?.signal.aborted) {
1051
+ throw new Error("Request was aborted");
1052
+ }
1053
+ }
1054
+ }
1055
+ } catch (err) {
1056
+ const errorMessage = err instanceof Error ? err.message : String(err);
1057
+ const stoppedForAbort = this.#abortController?.signal.aborted === true;
1058
+ const shouldEmitVisibleOutputBlockedError = !stoppedForAbort && isAnthropicOutputBlockedError(errorMessage);
1059
+ const assistantPartial = partial?.role === "assistant" ? partial : undefined;
1060
+ const hadAssistantStart = assistantPartial !== undefined;
1061
+ const errorMsg: AssistantMessage =
1062
+ shouldEmitVisibleOutputBlockedError && assistantPartial
1063
+ ? { ...assistantPartial, stopReason: "error", errorMessage }
1064
+ : {
1065
+ role: "assistant",
1066
+ content: [{ type: "text", text: "" }],
1067
+ api: model.api,
1068
+ provider: model.provider,
1069
+ model: model.id,
1070
+ usage: {
1071
+ input: 0,
1072
+ output: 0,
1073
+ cacheRead: 0,
1074
+ cacheWrite: 0,
1075
+ totalTokens: 0,
1076
+ cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0, total: 0 },
1077
+ },
1078
+ stopReason: stoppedForAbort ? "aborted" : "error",
1079
+ errorMessage,
1080
+ timestamp: Date.now(),
1081
+ };
1082
+
1083
+ if (shouldEmitVisibleOutputBlockedError) {
1084
+ if (!hadAssistantStart) {
1085
+ this.#state.streamMessage = errorMsg;
1086
+ this.#emit({ type: "message_start", message: errorMsg });
1087
+ }
1088
+ this.#state.streamMessage = null;
1089
+ this.appendMessage(errorMsg);
1090
+ this.#state.error = errorMessage;
1091
+ this.#emit({ type: "message_end", message: errorMsg });
1092
+ this.#emit({ type: "turn_end", message: errorMsg, toolResults: [] });
1093
+ this.#emit({ type: "agent_end", messages: [errorMsg] });
1094
+ } else {
1095
+ this.appendMessage(errorMsg);
1096
+ this.#state.error = errorMessage;
1097
+ this.#emit({ type: "agent_end", messages: [errorMsg] });
1098
+ }
1099
+ } finally {
1100
+ this.#state.isStreaming = false;
1101
+ this.#state.streamMessage = null;
1102
+ this.#state.pendingToolCalls.clear();
1103
+ this.#abortController = undefined;
1104
+ this.#resolveRunningPrompt?.();
1105
+ this.#runningPrompt = undefined;
1106
+ this.#resolveRunningPrompt = undefined;
1107
+ }
1108
+ }
1109
+
1110
+ #emit(e: AgentEvent) {
1111
+ for (const listener of this.#listeners) {
1112
+ try {
1113
+ const result = listener(e) as unknown;
1114
+ if (isPromise(result)) {
1115
+ result.catch(err => {
1116
+ console.error("Agent listener rejected:", err instanceof Error ? err.message : err);
1117
+ });
1118
+ }
1119
+ } catch (err) {
1120
+ console.error("Agent listener threw:", err instanceof Error ? err.message : err);
1121
+ }
1122
+ }
1123
+ }
1124
+
1125
+ /** Calculate total text length from an assistant message's content blocks */
1126
+ #getAssistantTextLength(message: AgentMessage | null): number {
1127
+ if (message?.role !== "assistant" || !Array.isArray(message.content)) {
1128
+ return 0;
1129
+ }
1130
+ let length = 0;
1131
+ for (const block of message.content) {
1132
+ if (block.type === "text") {
1133
+ length += (block as TextContent).text.length;
1134
+ }
1135
+ }
1136
+ return length;
1137
+ }
1138
+
1139
+ /**
1140
+ * Emit a Cursor assistant message split around tool results.
1141
+ * This fixes the ordering issue where tool results appear after the full explanation.
1142
+ *
1143
+ * Output order: Assistant(preamble) -> ToolResults -> Assistant(continuation)
1144
+ */
1145
+ #emitCursorSplitAssistantMessage(assistantMessage: AssistantMessage): void {
1146
+ const buffer = this.#cursorToolResultBuffer;
1147
+ this.#cursorToolResultBuffer = [];
1148
+
1149
+ if (buffer.length === 0) {
1150
+ // No tool results, emit normally
1151
+ this.#state.streamMessage = null;
1152
+ this.appendMessage(assistantMessage);
1153
+ this.#emit({ type: "message_end", message: assistantMessage });
1154
+ return;
1155
+ }
1156
+
1157
+ // Find the split point: minimum text length at first tool call
1158
+ const splitPoint = Math.min(...buffer.map(r => r.textLengthAtCall));
1159
+
1160
+ // Extract text content from assistant message
1161
+ const content = assistantMessage.content;
1162
+ let fullText = "";
1163
+ for (const block of content) {
1164
+ if (block.type === "text") {
1165
+ fullText += block.text;
1166
+ }
1167
+ }
1168
+
1169
+ // If no text or split point is 0 or at/past end, don't split
1170
+ if (fullText.length === 0 || splitPoint <= 0 || splitPoint >= fullText.length) {
1171
+ // Emit assistant message first, then tool results (original behavior but with buffered results)
1172
+ this.#state.streamMessage = null;
1173
+ this.appendMessage(assistantMessage);
1174
+ this.#emit({ type: "message_end", message: assistantMessage });
1175
+
1176
+ // Emit buffered tool results
1177
+ for (const { toolResult } of buffer) {
1178
+ this.#emit({ type: "message_start", message: toolResult });
1179
+ this.appendMessage(toolResult);
1180
+ this.#emit({ type: "message_end", message: toolResult });
1181
+ }
1182
+ return;
1183
+ }
1184
+
1185
+ // Split the text
1186
+ const preambleText = fullText.slice(0, splitPoint);
1187
+ const continuationText = fullText.slice(splitPoint);
1188
+
1189
+ // Create preamble message (text before tools)
1190
+ const preambleContent = content.map(block => {
1191
+ if (block.type === "text") {
1192
+ return { ...block, text: preambleText };
1193
+ }
1194
+ return block;
1195
+ });
1196
+ const preambleMessage: AssistantMessage = {
1197
+ ...assistantMessage,
1198
+ content: preambleContent,
1199
+ };
1200
+
1201
+ // Emit preamble
1202
+ this.#state.streamMessage = null;
1203
+ this.appendMessage(preambleMessage);
1204
+ this.#emit({ type: "message_end", message: preambleMessage });
1205
+
1206
+ // Emit buffered tool results
1207
+ for (const { toolResult } of buffer) {
1208
+ this.#emit({ type: "message_start", message: toolResult });
1209
+ this.appendMessage(toolResult);
1210
+ this.#emit({ type: "message_end", message: toolResult });
1211
+ }
1212
+
1213
+ // Emit continuation message (text after tools) if non-empty
1214
+ const trimmedContinuation = continuationText.trim();
1215
+ if (trimmedContinuation.length > 0) {
1216
+ // Create continuation message with only text content (no thinking/toolCalls)
1217
+ const continuationContent: TextContent[] = [{ type: "text", text: continuationText }];
1218
+ const continuationMessage: AssistantMessage = {
1219
+ ...assistantMessage,
1220
+ content: continuationContent,
1221
+ // Zero out usage for continuation since it's part of same response
1222
+ usage: {
1223
+ input: 0,
1224
+ output: 0,
1225
+ cacheRead: 0,
1226
+ cacheWrite: 0,
1227
+ totalTokens: 0,
1228
+ cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0, total: 0 },
1229
+ },
1230
+ };
1231
+ this.#emit({ type: "message_start", message: continuationMessage });
1232
+ this.appendMessage(continuationMessage);
1233
+ this.#emit({ type: "message_end", message: continuationMessage });
1234
+ }
1235
+ }
1236
+ }