@gajae-code/agent-core 0.1.1

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