jeopi-agent-core 16.2.13

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