@f5xc-salesdemos/pi-agent-core 14.0.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +319 -0
- package/README.md +375 -0
- package/package.json +63 -0
- package/src/agent-loop.ts +690 -0
- package/src/agent.ts +1019 -0
- package/src/index.ts +10 -0
- package/src/proxy.ts +322 -0
- package/src/thinking.ts +19 -0
- package/src/types.ts +292 -0
package/src/agent.ts
ADDED
|
@@ -0,0 +1,1019 @@
|
|
|
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 "@f5xc-salesdemos/pi-ai";
|
|
23
|
+
import { agentLoop, agentLoopContinue } from "./agent-loop";
|
|
24
|
+
import type {
|
|
25
|
+
AgentContext,
|
|
26
|
+
AgentEvent,
|
|
27
|
+
AgentLoopConfig,
|
|
28
|
+
AgentMessage,
|
|
29
|
+
AgentState,
|
|
30
|
+
AgentTool,
|
|
31
|
+
AgentToolContext,
|
|
32
|
+
StreamFn,
|
|
33
|
+
ToolCallContext,
|
|
34
|
+
} from "./types";
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* Default convertToLlm: Keep only LLM-compatible messages, convert attachments.
|
|
38
|
+
*/
|
|
39
|
+
function defaultConvertToLlm(messages: AgentMessage[]): Message[] {
|
|
40
|
+
return messages.filter(m => m.role === "user" || m.role === "assistant" || m.role === "toolResult");
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
function refreshToolChoiceForActiveTools(
|
|
44
|
+
toolChoice: ToolChoice | undefined,
|
|
45
|
+
tools: AgentContext["tools"] = [],
|
|
46
|
+
): ToolChoice | undefined {
|
|
47
|
+
if (!toolChoice || typeof toolChoice === "string") {
|
|
48
|
+
return toolChoice;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
const toolName =
|
|
52
|
+
toolChoice.type === "tool"
|
|
53
|
+
? toolChoice.name
|
|
54
|
+
: "function" in toolChoice
|
|
55
|
+
? toolChoice.function.name
|
|
56
|
+
: toolChoice.name;
|
|
57
|
+
|
|
58
|
+
return tools.some(tool => tool.name === toolName) ? toolChoice : undefined;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
export class AgentBusyError extends Error {
|
|
62
|
+
constructor(
|
|
63
|
+
message: string = "Agent is already processing. Use steer() or followUp() to queue messages, or wait for completion.",
|
|
64
|
+
) {
|
|
65
|
+
super(message);
|
|
66
|
+
this.name = "AgentBusyError";
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
export interface AgentOptions {
|
|
70
|
+
initialState?: Partial<AgentState>;
|
|
71
|
+
|
|
72
|
+
/**
|
|
73
|
+
* Converts AgentMessage[] to LLM-compatible Message[] before each LLM call.
|
|
74
|
+
* Default filters to user/assistant/toolResult and converts attachments.
|
|
75
|
+
*/
|
|
76
|
+
convertToLlm?: (messages: AgentMessage[]) => Message[] | Promise<Message[]>;
|
|
77
|
+
|
|
78
|
+
/**
|
|
79
|
+
* Optional transform applied to context before convertToLlm.
|
|
80
|
+
* Use for context pruning, injecting external context, etc.
|
|
81
|
+
*/
|
|
82
|
+
transformContext?: (messages: AgentMessage[], signal?: AbortSignal) => Promise<AgentMessage[]>;
|
|
83
|
+
|
|
84
|
+
/**
|
|
85
|
+
* Steering mode: "all" = send all steering messages at once, "one-at-a-time" = one per turn
|
|
86
|
+
*/
|
|
87
|
+
steeringMode?: "all" | "one-at-a-time";
|
|
88
|
+
|
|
89
|
+
/**
|
|
90
|
+
* Follow-up mode: "all" = send all follow-up messages at once, "one-at-a-time" = one per turn
|
|
91
|
+
*/
|
|
92
|
+
followUpMode?: "all" | "one-at-a-time";
|
|
93
|
+
|
|
94
|
+
/**
|
|
95
|
+
* When to interrupt tool execution for steering messages.
|
|
96
|
+
* - "immediate": check after each tool call (default)
|
|
97
|
+
* - "wait": defer steering until the current turn completes
|
|
98
|
+
*/
|
|
99
|
+
interruptMode?: "immediate" | "wait";
|
|
100
|
+
|
|
101
|
+
/**
|
|
102
|
+
* API format for Kimi Code provider: "openai" or "anthropic" (default: "anthropic")
|
|
103
|
+
*/
|
|
104
|
+
kimiApiFormat?: "openai" | "anthropic";
|
|
105
|
+
|
|
106
|
+
/** Hint that websocket transport should be preferred when supported by the provider implementation. */
|
|
107
|
+
preferWebsockets?: boolean;
|
|
108
|
+
|
|
109
|
+
/**
|
|
110
|
+
* Custom stream function (for proxy backends, etc.). Default uses streamSimple.
|
|
111
|
+
*/
|
|
112
|
+
streamFn?: StreamFn;
|
|
113
|
+
|
|
114
|
+
/**
|
|
115
|
+
* Optional session identifier forwarded to LLM providers.
|
|
116
|
+
* Used by providers that support session-based caching (e.g., OpenAI Codex).
|
|
117
|
+
*/
|
|
118
|
+
sessionId?: string;
|
|
119
|
+
/**
|
|
120
|
+
* Shared provider state map for session-scoped transport/session caches.
|
|
121
|
+
*/
|
|
122
|
+
providerSessionState?: Map<string, ProviderSessionState>;
|
|
123
|
+
|
|
124
|
+
/**
|
|
125
|
+
* Resolves an API key dynamically for each LLM call.
|
|
126
|
+
* Useful for expiring tokens (e.g., GitHub Copilot OAuth).
|
|
127
|
+
*/
|
|
128
|
+
getApiKey?: (provider: string) => Promise<string | undefined> | string | undefined;
|
|
129
|
+
|
|
130
|
+
/**
|
|
131
|
+
* Inspect or replace provider payloads before they are sent.
|
|
132
|
+
*/
|
|
133
|
+
onPayload?: SimpleStreamOptions["onPayload"];
|
|
134
|
+
/**
|
|
135
|
+
* Inspect assistant streaming events before they are emitted to subscribers.
|
|
136
|
+
* Use this when abort decisions must happen before buffered events continue flowing.
|
|
137
|
+
*/
|
|
138
|
+
onAssistantMessageEvent?: (message: AssistantMessage, event: AssistantMessageEvent) => void;
|
|
139
|
+
/**
|
|
140
|
+
* Custom token budgets for thinking levels (token-based providers only).
|
|
141
|
+
*/
|
|
142
|
+
thinkingBudgets?: ThinkingBudgets;
|
|
143
|
+
|
|
144
|
+
/**
|
|
145
|
+
* Sampling temperature for LLM calls. `undefined` uses provider default.
|
|
146
|
+
*/
|
|
147
|
+
temperature?: number;
|
|
148
|
+
|
|
149
|
+
/** Additional sampling controls for providers that support them. */
|
|
150
|
+
topP?: number;
|
|
151
|
+
topK?: number;
|
|
152
|
+
minP?: number;
|
|
153
|
+
presencePenalty?: number;
|
|
154
|
+
repetitionPenalty?: number;
|
|
155
|
+
serviceTier?: ServiceTier;
|
|
156
|
+
|
|
157
|
+
/**
|
|
158
|
+
* Maximum delay in milliseconds to wait for a retry when the server requests a long wait.
|
|
159
|
+
* If the server's requested delay exceeds this value, the request fails immediately,
|
|
160
|
+
* allowing higher-level retry logic to handle it with user visibility.
|
|
161
|
+
* Default: 60000 (60 seconds). Set to 0 to disable the cap.
|
|
162
|
+
*/
|
|
163
|
+
maxRetryDelayMs?: number;
|
|
164
|
+
|
|
165
|
+
/**
|
|
166
|
+
* Provides tool execution context, resolved per tool call.
|
|
167
|
+
* Use for late-bound UI or session state access.
|
|
168
|
+
*/
|
|
169
|
+
getToolContext?: (toolCall?: ToolCallContext) => AgentToolContext | undefined;
|
|
170
|
+
|
|
171
|
+
/**
|
|
172
|
+
* Optional transform applied to tool call arguments before execution.
|
|
173
|
+
* Use for deobfuscating secrets or rewriting arguments.
|
|
174
|
+
*/
|
|
175
|
+
transformToolCallArguments?: (args: Record<string, unknown>, toolName: string) => Record<string, unknown>;
|
|
176
|
+
|
|
177
|
+
/** Enable intent tracing schema injection/stripping in the harness. */
|
|
178
|
+
intentTracing?: boolean;
|
|
179
|
+
/** Dynamic tool choice override, resolved per LLM call. */
|
|
180
|
+
getToolChoice?: () => ToolChoice | undefined;
|
|
181
|
+
|
|
182
|
+
/**
|
|
183
|
+
* Cursor exec handlers for local tool execution.
|
|
184
|
+
*/
|
|
185
|
+
cursorExecHandlers?: CursorExecHandlers;
|
|
186
|
+
|
|
187
|
+
/**
|
|
188
|
+
* Cursor tool result callback for exec tool responses.
|
|
189
|
+
*/
|
|
190
|
+
cursorOnToolResult?: CursorToolResultHandler;
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
export interface AgentPromptOptions {
|
|
194
|
+
toolChoice?: ToolChoice;
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
/** Buffered Cursor tool result with text position at time of call */
|
|
198
|
+
interface CursorToolResultEntry {
|
|
199
|
+
toolResult: ToolResultMessage;
|
|
200
|
+
textLengthAtCall: number;
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
export class Agent {
|
|
204
|
+
#state: AgentState = {
|
|
205
|
+
systemPrompt: "",
|
|
206
|
+
model: getBundledModel("google", "gemini-2.5-flash-lite-preview-06-17"),
|
|
207
|
+
thinkingLevel: undefined,
|
|
208
|
+
tools: [],
|
|
209
|
+
messages: [],
|
|
210
|
+
isStreaming: false,
|
|
211
|
+
streamMessage: null,
|
|
212
|
+
pendingToolCalls: new Set<string>(),
|
|
213
|
+
error: undefined,
|
|
214
|
+
};
|
|
215
|
+
|
|
216
|
+
#listeners = new Set<(e: AgentEvent) => void>();
|
|
217
|
+
#abortController?: AbortController;
|
|
218
|
+
#convertToLlm: (messages: AgentMessage[]) => Message[] | Promise<Message[]>;
|
|
219
|
+
#transformContext?: (messages: AgentMessage[], signal?: AbortSignal) => Promise<AgentMessage[]>;
|
|
220
|
+
#steeringQueue: AgentMessage[] = [];
|
|
221
|
+
#followUpQueue: AgentMessage[] = [];
|
|
222
|
+
#steeringMode: "all" | "one-at-a-time";
|
|
223
|
+
#followUpMode: "all" | "one-at-a-time";
|
|
224
|
+
#interruptMode: "immediate" | "wait";
|
|
225
|
+
#sessionId?: string;
|
|
226
|
+
#providerSessionState?: Map<string, ProviderSessionState>;
|
|
227
|
+
#thinkingBudgets?: ThinkingBudgets;
|
|
228
|
+
#temperature?: number;
|
|
229
|
+
#topP?: number;
|
|
230
|
+
#topK?: number;
|
|
231
|
+
#minP?: number;
|
|
232
|
+
#presencePenalty?: number;
|
|
233
|
+
#repetitionPenalty?: number;
|
|
234
|
+
#serviceTier?: ServiceTier;
|
|
235
|
+
#maxRetryDelayMs?: number;
|
|
236
|
+
#getToolContext?: (toolCall?: ToolCallContext) => AgentToolContext | undefined;
|
|
237
|
+
#cursorExecHandlers?: CursorExecHandlers;
|
|
238
|
+
#cursorOnToolResult?: CursorToolResultHandler;
|
|
239
|
+
#runningPrompt?: Promise<void>;
|
|
240
|
+
#resolveRunningPrompt?: () => void;
|
|
241
|
+
#kimiApiFormat?: "openai" | "anthropic";
|
|
242
|
+
#preferWebsockets?: boolean;
|
|
243
|
+
#transformToolCallArguments?: (args: Record<string, unknown>, toolName: string) => Record<string, unknown>;
|
|
244
|
+
#intentTracing: boolean;
|
|
245
|
+
#getToolChoice?: () => ToolChoice | undefined;
|
|
246
|
+
#onPayload?: SimpleStreamOptions["onPayload"];
|
|
247
|
+
#onAssistantMessageEvent?: (message: AssistantMessage, event: AssistantMessageEvent) => void;
|
|
248
|
+
|
|
249
|
+
/** Buffered Cursor tool results with text length at time of call (for correct ordering) */
|
|
250
|
+
#cursorToolResultBuffer: CursorToolResultEntry[] = [];
|
|
251
|
+
|
|
252
|
+
streamFn: StreamFn;
|
|
253
|
+
getApiKey?: (provider: string) => Promise<string | undefined> | string | undefined;
|
|
254
|
+
|
|
255
|
+
constructor(opts: AgentOptions = {}) {
|
|
256
|
+
this.#state = { ...this.#state, ...opts.initialState };
|
|
257
|
+
this.#convertToLlm = opts.convertToLlm || defaultConvertToLlm;
|
|
258
|
+
this.#transformContext = opts.transformContext;
|
|
259
|
+
this.#steeringMode = opts.steeringMode || "one-at-a-time";
|
|
260
|
+
this.#followUpMode = opts.followUpMode || "one-at-a-time";
|
|
261
|
+
this.#interruptMode = opts.interruptMode || "immediate";
|
|
262
|
+
this.streamFn = opts.streamFn || streamSimple;
|
|
263
|
+
this.#sessionId = opts.sessionId;
|
|
264
|
+
this.#providerSessionState = opts.providerSessionState;
|
|
265
|
+
this.#thinkingBudgets = opts.thinkingBudgets;
|
|
266
|
+
this.#temperature = opts.temperature;
|
|
267
|
+
this.#topP = opts.topP;
|
|
268
|
+
this.#topK = opts.topK;
|
|
269
|
+
this.#minP = opts.minP;
|
|
270
|
+
this.#presencePenalty = opts.presencePenalty;
|
|
271
|
+
this.#repetitionPenalty = opts.repetitionPenalty;
|
|
272
|
+
this.#serviceTier = opts.serviceTier;
|
|
273
|
+
this.#maxRetryDelayMs = opts.maxRetryDelayMs;
|
|
274
|
+
this.getApiKey = opts.getApiKey;
|
|
275
|
+
this.#onPayload = opts.onPayload;
|
|
276
|
+
this.#getToolContext = opts.getToolContext;
|
|
277
|
+
this.#cursorExecHandlers = opts.cursorExecHandlers;
|
|
278
|
+
this.#cursorOnToolResult = opts.cursorOnToolResult;
|
|
279
|
+
this.#kimiApiFormat = opts.kimiApiFormat;
|
|
280
|
+
this.#preferWebsockets = opts.preferWebsockets;
|
|
281
|
+
this.#transformToolCallArguments = opts.transformToolCallArguments;
|
|
282
|
+
this.#intentTracing = opts.intentTracing === true;
|
|
283
|
+
this.#getToolChoice = opts.getToolChoice;
|
|
284
|
+
this.#onAssistantMessageEvent = opts.onAssistantMessageEvent;
|
|
285
|
+
}
|
|
286
|
+
|
|
287
|
+
/**
|
|
288
|
+
* Get the current session ID used for provider caching.
|
|
289
|
+
*/
|
|
290
|
+
get sessionId(): string | undefined {
|
|
291
|
+
return this.#sessionId;
|
|
292
|
+
}
|
|
293
|
+
|
|
294
|
+
/**
|
|
295
|
+
* Set the session ID for provider caching.
|
|
296
|
+
* Call this when switching sessions (new session, branch, resume).
|
|
297
|
+
*/
|
|
298
|
+
set sessionId(value: string | undefined) {
|
|
299
|
+
this.#sessionId = value;
|
|
300
|
+
}
|
|
301
|
+
|
|
302
|
+
/**
|
|
303
|
+
* Get provider-scoped mutable session state store.
|
|
304
|
+
*/
|
|
305
|
+
get providerSessionState(): Map<string, ProviderSessionState> | undefined {
|
|
306
|
+
return this.#providerSessionState;
|
|
307
|
+
}
|
|
308
|
+
|
|
309
|
+
/**
|
|
310
|
+
* Set provider-scoped mutable session state store.
|
|
311
|
+
*/
|
|
312
|
+
set providerSessionState(value: Map<string, ProviderSessionState> | undefined) {
|
|
313
|
+
this.#providerSessionState = value;
|
|
314
|
+
}
|
|
315
|
+
|
|
316
|
+
/**
|
|
317
|
+
* Get the current thinking budgets.
|
|
318
|
+
*/
|
|
319
|
+
get thinkingBudgets(): ThinkingBudgets | undefined {
|
|
320
|
+
return this.#thinkingBudgets;
|
|
321
|
+
}
|
|
322
|
+
|
|
323
|
+
/**
|
|
324
|
+
* Set custom thinking budgets for token-based providers.
|
|
325
|
+
*/
|
|
326
|
+
set thinkingBudgets(value: ThinkingBudgets | undefined) {
|
|
327
|
+
this.#thinkingBudgets = value;
|
|
328
|
+
}
|
|
329
|
+
|
|
330
|
+
/**
|
|
331
|
+
* Get the current sampling temperature.
|
|
332
|
+
*/
|
|
333
|
+
get temperature(): number | undefined {
|
|
334
|
+
return this.#temperature;
|
|
335
|
+
}
|
|
336
|
+
|
|
337
|
+
/**
|
|
338
|
+
* Set sampling temperature for LLM calls. `undefined` uses provider default.
|
|
339
|
+
*/
|
|
340
|
+
set temperature(value: number | undefined) {
|
|
341
|
+
this.#temperature = value;
|
|
342
|
+
}
|
|
343
|
+
|
|
344
|
+
get topP(): number | undefined {
|
|
345
|
+
return this.#topP;
|
|
346
|
+
}
|
|
347
|
+
|
|
348
|
+
set topP(value: number | undefined) {
|
|
349
|
+
this.#topP = value;
|
|
350
|
+
}
|
|
351
|
+
|
|
352
|
+
get topK(): number | undefined {
|
|
353
|
+
return this.#topK;
|
|
354
|
+
}
|
|
355
|
+
|
|
356
|
+
set topK(value: number | undefined) {
|
|
357
|
+
this.#topK = value;
|
|
358
|
+
}
|
|
359
|
+
|
|
360
|
+
get minP(): number | undefined {
|
|
361
|
+
return this.#minP;
|
|
362
|
+
}
|
|
363
|
+
|
|
364
|
+
set minP(value: number | undefined) {
|
|
365
|
+
this.#minP = value;
|
|
366
|
+
}
|
|
367
|
+
|
|
368
|
+
get presencePenalty(): number | undefined {
|
|
369
|
+
return this.#presencePenalty;
|
|
370
|
+
}
|
|
371
|
+
|
|
372
|
+
set presencePenalty(value: number | undefined) {
|
|
373
|
+
this.#presencePenalty = value;
|
|
374
|
+
}
|
|
375
|
+
|
|
376
|
+
get repetitionPenalty(): number | undefined {
|
|
377
|
+
return this.#repetitionPenalty;
|
|
378
|
+
}
|
|
379
|
+
|
|
380
|
+
set repetitionPenalty(value: number | undefined) {
|
|
381
|
+
this.#repetitionPenalty = value;
|
|
382
|
+
}
|
|
383
|
+
|
|
384
|
+
get serviceTier(): ServiceTier | undefined {
|
|
385
|
+
return this.#serviceTier;
|
|
386
|
+
}
|
|
387
|
+
|
|
388
|
+
set serviceTier(value: ServiceTier | undefined) {
|
|
389
|
+
this.#serviceTier = value;
|
|
390
|
+
}
|
|
391
|
+
|
|
392
|
+
/**
|
|
393
|
+
* Get the current max retry delay in milliseconds.
|
|
394
|
+
*/
|
|
395
|
+
get maxRetryDelayMs(): number | undefined {
|
|
396
|
+
return this.#maxRetryDelayMs;
|
|
397
|
+
}
|
|
398
|
+
|
|
399
|
+
/**
|
|
400
|
+
* Set the maximum delay to wait for server-requested retries.
|
|
401
|
+
* Set to 0 to disable the cap.
|
|
402
|
+
*/
|
|
403
|
+
set maxRetryDelayMs(value: number | undefined) {
|
|
404
|
+
this.#maxRetryDelayMs = value;
|
|
405
|
+
}
|
|
406
|
+
|
|
407
|
+
get state(): AgentState {
|
|
408
|
+
return this.#state;
|
|
409
|
+
}
|
|
410
|
+
|
|
411
|
+
subscribe(fn: (e: AgentEvent) => void): () => void {
|
|
412
|
+
this.#listeners.add(fn);
|
|
413
|
+
return () => this.#listeners.delete(fn);
|
|
414
|
+
}
|
|
415
|
+
|
|
416
|
+
setAssistantMessageEventInterceptor(
|
|
417
|
+
fn: ((message: AssistantMessage, event: AssistantMessageEvent) => void) | undefined,
|
|
418
|
+
): void {
|
|
419
|
+
this.#onAssistantMessageEvent = fn;
|
|
420
|
+
}
|
|
421
|
+
|
|
422
|
+
emitExternalEvent(event: AgentEvent) {
|
|
423
|
+
switch (event.type) {
|
|
424
|
+
case "message_start":
|
|
425
|
+
case "message_update":
|
|
426
|
+
this.#state.streamMessage = event.message;
|
|
427
|
+
break;
|
|
428
|
+
case "message_end":
|
|
429
|
+
this.#state.streamMessage = null;
|
|
430
|
+
this.appendMessage(event.message);
|
|
431
|
+
break;
|
|
432
|
+
case "tool_execution_start": {
|
|
433
|
+
const pending = new Set(this.#state.pendingToolCalls);
|
|
434
|
+
pending.add(event.toolCallId);
|
|
435
|
+
this.#state.pendingToolCalls = pending;
|
|
436
|
+
break;
|
|
437
|
+
}
|
|
438
|
+
case "tool_execution_end": {
|
|
439
|
+
const pending = new Set(this.#state.pendingToolCalls);
|
|
440
|
+
pending.delete(event.toolCallId);
|
|
441
|
+
this.#state.pendingToolCalls = pending;
|
|
442
|
+
break;
|
|
443
|
+
}
|
|
444
|
+
}
|
|
445
|
+
|
|
446
|
+
this.#emit(event);
|
|
447
|
+
}
|
|
448
|
+
|
|
449
|
+
// State mutators
|
|
450
|
+
setSystemPrompt(v: string) {
|
|
451
|
+
this.#state.systemPrompt = v;
|
|
452
|
+
}
|
|
453
|
+
|
|
454
|
+
setModel(m: Model) {
|
|
455
|
+
this.#state.model = m;
|
|
456
|
+
}
|
|
457
|
+
|
|
458
|
+
setThinkingLevel(l: Effort | undefined) {
|
|
459
|
+
this.#state.thinkingLevel = l;
|
|
460
|
+
}
|
|
461
|
+
|
|
462
|
+
setSteeringMode(mode: "all" | "one-at-a-time") {
|
|
463
|
+
this.#steeringMode = mode;
|
|
464
|
+
}
|
|
465
|
+
|
|
466
|
+
getSteeringMode(): "all" | "one-at-a-time" {
|
|
467
|
+
return this.#steeringMode;
|
|
468
|
+
}
|
|
469
|
+
|
|
470
|
+
setFollowUpMode(mode: "all" | "one-at-a-time") {
|
|
471
|
+
this.#followUpMode = mode;
|
|
472
|
+
}
|
|
473
|
+
|
|
474
|
+
getFollowUpMode(): "all" | "one-at-a-time" {
|
|
475
|
+
return this.#followUpMode;
|
|
476
|
+
}
|
|
477
|
+
|
|
478
|
+
setInterruptMode(mode: "immediate" | "wait") {
|
|
479
|
+
this.#interruptMode = mode;
|
|
480
|
+
}
|
|
481
|
+
|
|
482
|
+
getInterruptMode(): "immediate" | "wait" {
|
|
483
|
+
return this.#interruptMode;
|
|
484
|
+
}
|
|
485
|
+
|
|
486
|
+
setTools(t: AgentTool<any>[]) {
|
|
487
|
+
this.#state.tools = t;
|
|
488
|
+
}
|
|
489
|
+
|
|
490
|
+
replaceMessages(ms: AgentMessage[]) {
|
|
491
|
+
this.#state.messages = ms.slice();
|
|
492
|
+
}
|
|
493
|
+
|
|
494
|
+
appendMessage(m: AgentMessage) {
|
|
495
|
+
this.#state.messages = [...this.#state.messages, m];
|
|
496
|
+
}
|
|
497
|
+
|
|
498
|
+
popMessage(): AgentMessage | undefined {
|
|
499
|
+
const messages = this.#state.messages.slice(0, -1);
|
|
500
|
+
const removed = this.#state.messages.at(-1);
|
|
501
|
+
this.#state.messages = messages;
|
|
502
|
+
|
|
503
|
+
if (removed && this.#state.streamMessage === removed) {
|
|
504
|
+
this.#state.streamMessage = null;
|
|
505
|
+
}
|
|
506
|
+
|
|
507
|
+
return removed;
|
|
508
|
+
}
|
|
509
|
+
|
|
510
|
+
/**
|
|
511
|
+
* Queue a steering message to interrupt the agent mid-run.
|
|
512
|
+
* Delivered after current tool execution, skips remaining tools.
|
|
513
|
+
*/
|
|
514
|
+
steer(m: AgentMessage) {
|
|
515
|
+
this.#steeringQueue.push(m);
|
|
516
|
+
}
|
|
517
|
+
|
|
518
|
+
/**
|
|
519
|
+
* Queue a follow-up message to be processed after the agent finishes.
|
|
520
|
+
* Delivered only when agent has no more tool calls or steering messages.
|
|
521
|
+
*/
|
|
522
|
+
followUp(m: AgentMessage) {
|
|
523
|
+
this.#followUpQueue.push(m);
|
|
524
|
+
}
|
|
525
|
+
|
|
526
|
+
clearSteeringQueue() {
|
|
527
|
+
this.#steeringQueue = [];
|
|
528
|
+
}
|
|
529
|
+
|
|
530
|
+
clearFollowUpQueue() {
|
|
531
|
+
this.#followUpQueue = [];
|
|
532
|
+
}
|
|
533
|
+
|
|
534
|
+
clearAllQueues() {
|
|
535
|
+
this.#steeringQueue = [];
|
|
536
|
+
this.#followUpQueue = [];
|
|
537
|
+
}
|
|
538
|
+
|
|
539
|
+
hasQueuedMessages(): boolean {
|
|
540
|
+
return this.#steeringQueue.length > 0 || this.#followUpQueue.length > 0;
|
|
541
|
+
}
|
|
542
|
+
|
|
543
|
+
#dequeueSteeringMessages(): AgentMessage[] {
|
|
544
|
+
if (this.#steeringMode === "one-at-a-time") {
|
|
545
|
+
if (this.#steeringQueue.length > 0) {
|
|
546
|
+
const first = this.#steeringQueue[0];
|
|
547
|
+
this.#steeringQueue = this.#steeringQueue.slice(1);
|
|
548
|
+
return [first];
|
|
549
|
+
}
|
|
550
|
+
return [];
|
|
551
|
+
}
|
|
552
|
+
const steering = this.#steeringQueue.slice();
|
|
553
|
+
this.#steeringQueue = [];
|
|
554
|
+
return steering;
|
|
555
|
+
}
|
|
556
|
+
|
|
557
|
+
#dequeueFollowUpMessages(): AgentMessage[] {
|
|
558
|
+
if (this.#followUpMode === "one-at-a-time") {
|
|
559
|
+
if (this.#followUpQueue.length > 0) {
|
|
560
|
+
const first = this.#followUpQueue[0];
|
|
561
|
+
this.#followUpQueue = this.#followUpQueue.slice(1);
|
|
562
|
+
return [first];
|
|
563
|
+
}
|
|
564
|
+
return [];
|
|
565
|
+
}
|
|
566
|
+
const followUp = this.#followUpQueue.slice();
|
|
567
|
+
this.#followUpQueue = [];
|
|
568
|
+
return followUp;
|
|
569
|
+
}
|
|
570
|
+
|
|
571
|
+
/**
|
|
572
|
+
* Remove and return the last steering message from the queue (LIFO).
|
|
573
|
+
* Used by dequeue keybinding.
|
|
574
|
+
*/
|
|
575
|
+
popLastSteer(): AgentMessage | undefined {
|
|
576
|
+
return this.#steeringQueue.pop();
|
|
577
|
+
}
|
|
578
|
+
|
|
579
|
+
/**
|
|
580
|
+
* Remove and return the last follow-up message from the queue (LIFO).
|
|
581
|
+
* Used by dequeue keybinding.
|
|
582
|
+
*/
|
|
583
|
+
popLastFollowUp(): AgentMessage | undefined {
|
|
584
|
+
return this.#followUpQueue.pop();
|
|
585
|
+
}
|
|
586
|
+
|
|
587
|
+
clearMessages() {
|
|
588
|
+
this.#state.messages = [];
|
|
589
|
+
}
|
|
590
|
+
|
|
591
|
+
abort() {
|
|
592
|
+
this.#abortController?.abort();
|
|
593
|
+
}
|
|
594
|
+
|
|
595
|
+
waitForIdle(): Promise<void> {
|
|
596
|
+
return this.#runningPrompt ?? Promise.resolve();
|
|
597
|
+
}
|
|
598
|
+
|
|
599
|
+
reset() {
|
|
600
|
+
this.#state.messages = [];
|
|
601
|
+
this.#state.isStreaming = false;
|
|
602
|
+
this.#state.streamMessage = null;
|
|
603
|
+
this.#state.pendingToolCalls = new Set<string>();
|
|
604
|
+
this.#state.error = undefined;
|
|
605
|
+
this.#steeringQueue = [];
|
|
606
|
+
this.#followUpQueue = [];
|
|
607
|
+
}
|
|
608
|
+
|
|
609
|
+
/** Send a prompt with an AgentMessage */
|
|
610
|
+
async prompt(message: AgentMessage | AgentMessage[], options?: AgentPromptOptions): Promise<void>;
|
|
611
|
+
async prompt(input: string, options?: AgentPromptOptions): Promise<void>;
|
|
612
|
+
async prompt(input: string, images?: ImageContent[], options?: AgentPromptOptions): Promise<void>;
|
|
613
|
+
async prompt(
|
|
614
|
+
input: string | AgentMessage | AgentMessage[],
|
|
615
|
+
imagesOrOptions?: ImageContent[] | AgentPromptOptions,
|
|
616
|
+
options?: AgentPromptOptions,
|
|
617
|
+
) {
|
|
618
|
+
if (this.#state.isStreaming) {
|
|
619
|
+
throw new AgentBusyError();
|
|
620
|
+
}
|
|
621
|
+
|
|
622
|
+
const model = this.#state.model;
|
|
623
|
+
if (!model) throw new Error("No model configured");
|
|
624
|
+
|
|
625
|
+
let msgs: AgentMessage[];
|
|
626
|
+
let promptOptions: AgentPromptOptions | undefined;
|
|
627
|
+
let images: ImageContent[] | undefined;
|
|
628
|
+
|
|
629
|
+
if (Array.isArray(input)) {
|
|
630
|
+
msgs = input;
|
|
631
|
+
promptOptions = imagesOrOptions as AgentPromptOptions | undefined;
|
|
632
|
+
} else if (typeof input === "string") {
|
|
633
|
+
if (Array.isArray(imagesOrOptions)) {
|
|
634
|
+
images = imagesOrOptions;
|
|
635
|
+
promptOptions = options;
|
|
636
|
+
} else {
|
|
637
|
+
promptOptions = imagesOrOptions;
|
|
638
|
+
}
|
|
639
|
+
const content: Array<TextContent | ImageContent> = [{ type: "text", text: input }];
|
|
640
|
+
if (images && images.length > 0) {
|
|
641
|
+
content.push(...images);
|
|
642
|
+
}
|
|
643
|
+
msgs = [
|
|
644
|
+
{
|
|
645
|
+
role: "user",
|
|
646
|
+
content,
|
|
647
|
+
timestamp: Date.now(),
|
|
648
|
+
},
|
|
649
|
+
];
|
|
650
|
+
} else {
|
|
651
|
+
msgs = [input];
|
|
652
|
+
promptOptions = imagesOrOptions as AgentPromptOptions | undefined;
|
|
653
|
+
}
|
|
654
|
+
|
|
655
|
+
await this.#runLoop(msgs, promptOptions);
|
|
656
|
+
}
|
|
657
|
+
|
|
658
|
+
/**
|
|
659
|
+
* Continue from current context (used for retries and resuming queued messages).
|
|
660
|
+
*/
|
|
661
|
+
async continue() {
|
|
662
|
+
if (this.#state.isStreaming) {
|
|
663
|
+
throw new AgentBusyError();
|
|
664
|
+
}
|
|
665
|
+
|
|
666
|
+
const messages = this.#state.messages;
|
|
667
|
+
if (messages.length === 0) {
|
|
668
|
+
throw new Error("No messages to continue from");
|
|
669
|
+
}
|
|
670
|
+
if (messages[messages.length - 1].role === "assistant") {
|
|
671
|
+
const queuedSteering = this.#dequeueSteeringMessages();
|
|
672
|
+
if (queuedSteering.length > 0) {
|
|
673
|
+
await this.#runLoop(queuedSteering, { skipInitialSteeringPoll: true });
|
|
674
|
+
return;
|
|
675
|
+
}
|
|
676
|
+
|
|
677
|
+
const queuedFollowUp = this.#dequeueFollowUpMessages();
|
|
678
|
+
if (queuedFollowUp.length > 0) {
|
|
679
|
+
await this.#runLoop(queuedFollowUp);
|
|
680
|
+
return;
|
|
681
|
+
}
|
|
682
|
+
|
|
683
|
+
throw new Error("Cannot continue from message role: assistant");
|
|
684
|
+
}
|
|
685
|
+
|
|
686
|
+
await this.#runLoop(undefined);
|
|
687
|
+
}
|
|
688
|
+
|
|
689
|
+
/**
|
|
690
|
+
* Run the agent loop.
|
|
691
|
+
* If messages are provided, starts a new conversation turn with those messages.
|
|
692
|
+
* Otherwise, continues from existing context.
|
|
693
|
+
*/
|
|
694
|
+
async #runLoop(messages?: AgentMessage[], options?: AgentPromptOptions & { skipInitialSteeringPoll?: boolean }) {
|
|
695
|
+
const model = this.#state.model;
|
|
696
|
+
if (!model) throw new Error("No model configured");
|
|
697
|
+
|
|
698
|
+
let skipInitialSteeringPoll = options?.skipInitialSteeringPoll === true;
|
|
699
|
+
|
|
700
|
+
const { promise, resolve } = Promise.withResolvers<void>();
|
|
701
|
+
this.#runningPrompt = promise;
|
|
702
|
+
this.#resolveRunningPrompt = resolve;
|
|
703
|
+
|
|
704
|
+
this.#abortController = new AbortController();
|
|
705
|
+
this.#state.isStreaming = true;
|
|
706
|
+
this.#state.streamMessage = null;
|
|
707
|
+
this.#state.error = undefined;
|
|
708
|
+
|
|
709
|
+
// Clear Cursor tool result buffer at start of each run
|
|
710
|
+
this.#cursorToolResultBuffer = [];
|
|
711
|
+
|
|
712
|
+
const reasoning = this.#state.thinkingLevel;
|
|
713
|
+
|
|
714
|
+
const context: AgentContext = {
|
|
715
|
+
systemPrompt: this.#state.systemPrompt,
|
|
716
|
+
messages: this.#state.messages.slice(),
|
|
717
|
+
tools: this.#state.tools,
|
|
718
|
+
};
|
|
719
|
+
|
|
720
|
+
const cursorOnToolResult =
|
|
721
|
+
this.#cursorExecHandlers || this.#cursorOnToolResult
|
|
722
|
+
? async (message: ToolResultMessage) => {
|
|
723
|
+
let finalMessage = message;
|
|
724
|
+
if (this.#cursorOnToolResult) {
|
|
725
|
+
try {
|
|
726
|
+
const updated = await this.#cursorOnToolResult(message);
|
|
727
|
+
if (updated) {
|
|
728
|
+
finalMessage = updated;
|
|
729
|
+
}
|
|
730
|
+
} catch {}
|
|
731
|
+
}
|
|
732
|
+
// Buffer tool result with current text length for correct ordering later.
|
|
733
|
+
// Cursor executes tools server-side during streaming, so the assistant message
|
|
734
|
+
// already incorporates results. We buffer here and emit in correct order
|
|
735
|
+
// when the assistant message ends.
|
|
736
|
+
const textLength = this.#getAssistantTextLength(this.#state.streamMessage);
|
|
737
|
+
this.#cursorToolResultBuffer.push({ toolResult: finalMessage, textLengthAtCall: textLength });
|
|
738
|
+
return finalMessage;
|
|
739
|
+
}
|
|
740
|
+
: undefined;
|
|
741
|
+
|
|
742
|
+
const getToolChoice = () =>
|
|
743
|
+
this.#getToolChoice?.() ?? refreshToolChoiceForActiveTools(options?.toolChoice, this.#state.tools);
|
|
744
|
+
|
|
745
|
+
const config: AgentLoopConfig = {
|
|
746
|
+
model,
|
|
747
|
+
reasoning,
|
|
748
|
+
temperature: this.#temperature,
|
|
749
|
+
topP: this.#topP,
|
|
750
|
+
topK: this.#topK,
|
|
751
|
+
minP: this.#minP,
|
|
752
|
+
presencePenalty: this.#presencePenalty,
|
|
753
|
+
repetitionPenalty: this.#repetitionPenalty,
|
|
754
|
+
serviceTier: this.#serviceTier,
|
|
755
|
+
interruptMode: this.#interruptMode,
|
|
756
|
+
sessionId: this.#sessionId,
|
|
757
|
+
providerSessionState: this.#providerSessionState,
|
|
758
|
+
thinkingBudgets: this.#thinkingBudgets,
|
|
759
|
+
maxRetryDelayMs: this.#maxRetryDelayMs,
|
|
760
|
+
kimiApiFormat: this.#kimiApiFormat,
|
|
761
|
+
preferWebsockets: this.#preferWebsockets,
|
|
762
|
+
convertToLlm: this.#convertToLlm,
|
|
763
|
+
transformContext: this.#transformContext,
|
|
764
|
+
onPayload: this.#onPayload,
|
|
765
|
+
getApiKey: this.getApiKey,
|
|
766
|
+
getToolContext: this.#getToolContext,
|
|
767
|
+
syncContextBeforeModelCall: async context => {
|
|
768
|
+
if (this.#listeners.size > 0) {
|
|
769
|
+
await Bun.sleep(0);
|
|
770
|
+
}
|
|
771
|
+
context.systemPrompt = this.#state.systemPrompt;
|
|
772
|
+
context.tools = this.#state.tools;
|
|
773
|
+
},
|
|
774
|
+
cursorExecHandlers: this.#cursorExecHandlers,
|
|
775
|
+
cursorOnToolResult,
|
|
776
|
+
transformToolCallArguments: this.#transformToolCallArguments,
|
|
777
|
+
intentTracing: this.#intentTracing,
|
|
778
|
+
onAssistantMessageEvent: this.#onAssistantMessageEvent,
|
|
779
|
+
getToolChoice,
|
|
780
|
+
getSteeringMessages: async () => {
|
|
781
|
+
if (skipInitialSteeringPoll) {
|
|
782
|
+
skipInitialSteeringPoll = false;
|
|
783
|
+
return [];
|
|
784
|
+
}
|
|
785
|
+
return this.#dequeueSteeringMessages();
|
|
786
|
+
},
|
|
787
|
+
getFollowUpMessages: async () => this.#dequeueFollowUpMessages(),
|
|
788
|
+
};
|
|
789
|
+
|
|
790
|
+
let partial: AgentMessage | null = null;
|
|
791
|
+
|
|
792
|
+
try {
|
|
793
|
+
const stream = messages
|
|
794
|
+
? agentLoop(messages, context, config, this.#abortController.signal, this.streamFn)
|
|
795
|
+
: agentLoopContinue(context, config, this.#abortController.signal, this.streamFn);
|
|
796
|
+
|
|
797
|
+
for await (const event of stream) {
|
|
798
|
+
// Update internal state based on events
|
|
799
|
+
switch (event.type) {
|
|
800
|
+
case "message_start":
|
|
801
|
+
partial = event.message;
|
|
802
|
+
this.#state.streamMessage = event.message;
|
|
803
|
+
break;
|
|
804
|
+
|
|
805
|
+
case "message_update":
|
|
806
|
+
partial = event.message;
|
|
807
|
+
this.#state.streamMessage = event.message;
|
|
808
|
+
break;
|
|
809
|
+
|
|
810
|
+
case "message_end":
|
|
811
|
+
partial = null;
|
|
812
|
+
// Check if this is an assistant message with buffered Cursor tool results.
|
|
813
|
+
// If so, split the message to emit tool results at the correct position.
|
|
814
|
+
if (event.message.role === "assistant" && this.#cursorToolResultBuffer.length > 0) {
|
|
815
|
+
this.#emitCursorSplitAssistantMessage(event.message as AssistantMessage);
|
|
816
|
+
continue; // Skip default emit - split method handles everything
|
|
817
|
+
}
|
|
818
|
+
this.#state.streamMessage = null;
|
|
819
|
+
this.appendMessage(event.message);
|
|
820
|
+
break;
|
|
821
|
+
|
|
822
|
+
case "tool_execution_start": {
|
|
823
|
+
const s = new Set(this.#state.pendingToolCalls);
|
|
824
|
+
s.add(event.toolCallId);
|
|
825
|
+
this.#state.pendingToolCalls = s;
|
|
826
|
+
break;
|
|
827
|
+
}
|
|
828
|
+
|
|
829
|
+
case "tool_execution_end": {
|
|
830
|
+
const s = new Set(this.#state.pendingToolCalls);
|
|
831
|
+
s.delete(event.toolCallId);
|
|
832
|
+
this.#state.pendingToolCalls = s;
|
|
833
|
+
break;
|
|
834
|
+
}
|
|
835
|
+
|
|
836
|
+
case "turn_end":
|
|
837
|
+
if (event.message.role === "assistant" && (event.message as any).errorMessage) {
|
|
838
|
+
this.#state.error = (event.message as any).errorMessage;
|
|
839
|
+
}
|
|
840
|
+
break;
|
|
841
|
+
|
|
842
|
+
case "agent_end":
|
|
843
|
+
this.#state.isStreaming = false;
|
|
844
|
+
this.#state.streamMessage = null;
|
|
845
|
+
break;
|
|
846
|
+
}
|
|
847
|
+
|
|
848
|
+
// Emit to listeners
|
|
849
|
+
this.#emit(event);
|
|
850
|
+
}
|
|
851
|
+
|
|
852
|
+
// Handle any remaining partial message
|
|
853
|
+
if (partial && partial.role === "assistant" && partial.content.length > 0) {
|
|
854
|
+
const onlyEmpty = !partial.content.some(
|
|
855
|
+
c =>
|
|
856
|
+
(c.type === "thinking" && c.thinking.trim().length > 0) ||
|
|
857
|
+
(c.type === "text" && c.text.trim().length > 0) ||
|
|
858
|
+
(c.type === "toolCall" && c.name.trim().length > 0),
|
|
859
|
+
);
|
|
860
|
+
if (!onlyEmpty) {
|
|
861
|
+
this.appendMessage(partial);
|
|
862
|
+
} else {
|
|
863
|
+
if (this.#abortController?.signal.aborted) {
|
|
864
|
+
throw new Error("Request was aborted");
|
|
865
|
+
}
|
|
866
|
+
}
|
|
867
|
+
}
|
|
868
|
+
} catch (err: any) {
|
|
869
|
+
const errorMsg: AgentMessage = {
|
|
870
|
+
role: "assistant",
|
|
871
|
+
content: [{ type: "text", text: "" }],
|
|
872
|
+
api: model.api,
|
|
873
|
+
provider: model.provider,
|
|
874
|
+
model: model.id,
|
|
875
|
+
usage: {
|
|
876
|
+
input: 0,
|
|
877
|
+
output: 0,
|
|
878
|
+
cacheRead: 0,
|
|
879
|
+
cacheWrite: 0,
|
|
880
|
+
totalTokens: 0,
|
|
881
|
+
cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0, total: 0 },
|
|
882
|
+
},
|
|
883
|
+
stopReason: this.#abortController?.signal.aborted ? "aborted" : "error",
|
|
884
|
+
errorMessage: err?.message || String(err),
|
|
885
|
+
timestamp: Date.now(),
|
|
886
|
+
} as AgentMessage;
|
|
887
|
+
|
|
888
|
+
this.appendMessage(errorMsg);
|
|
889
|
+
this.#state.error = err?.message || String(err);
|
|
890
|
+
this.#emit({ type: "agent_end", messages: [errorMsg] });
|
|
891
|
+
} finally {
|
|
892
|
+
this.#state.isStreaming = false;
|
|
893
|
+
this.#state.streamMessage = null;
|
|
894
|
+
this.#state.pendingToolCalls = new Set<string>();
|
|
895
|
+
this.#abortController = undefined;
|
|
896
|
+
this.#resolveRunningPrompt?.();
|
|
897
|
+
this.#runningPrompt = undefined;
|
|
898
|
+
this.#resolveRunningPrompt = undefined;
|
|
899
|
+
}
|
|
900
|
+
}
|
|
901
|
+
|
|
902
|
+
#emit(e: AgentEvent) {
|
|
903
|
+
for (const listener of this.#listeners) {
|
|
904
|
+
listener(e);
|
|
905
|
+
}
|
|
906
|
+
}
|
|
907
|
+
|
|
908
|
+
/** Calculate total text length from an assistant message's content blocks */
|
|
909
|
+
#getAssistantTextLength(message: AgentMessage | null): number {
|
|
910
|
+
if (!message || message.role !== "assistant" || !Array.isArray(message.content)) {
|
|
911
|
+
return 0;
|
|
912
|
+
}
|
|
913
|
+
let length = 0;
|
|
914
|
+
for (const block of message.content) {
|
|
915
|
+
if (block.type === "text") {
|
|
916
|
+
length += (block as TextContent).text.length;
|
|
917
|
+
}
|
|
918
|
+
}
|
|
919
|
+
return length;
|
|
920
|
+
}
|
|
921
|
+
|
|
922
|
+
/**
|
|
923
|
+
* Emit a Cursor assistant message split around tool results.
|
|
924
|
+
* This fixes the ordering issue where tool results appear after the full explanation.
|
|
925
|
+
*
|
|
926
|
+
* Output order: Assistant(preamble) -> ToolResults -> Assistant(continuation)
|
|
927
|
+
*/
|
|
928
|
+
#emitCursorSplitAssistantMessage(assistantMessage: AssistantMessage): void {
|
|
929
|
+
const buffer = this.#cursorToolResultBuffer;
|
|
930
|
+
this.#cursorToolResultBuffer = [];
|
|
931
|
+
|
|
932
|
+
if (buffer.length === 0) {
|
|
933
|
+
// No tool results, emit normally
|
|
934
|
+
this.#state.streamMessage = null;
|
|
935
|
+
this.appendMessage(assistantMessage);
|
|
936
|
+
this.#emit({ type: "message_end", message: assistantMessage });
|
|
937
|
+
return;
|
|
938
|
+
}
|
|
939
|
+
|
|
940
|
+
// Find the split point: minimum text length at first tool call
|
|
941
|
+
const splitPoint = Math.min(...buffer.map(r => r.textLengthAtCall));
|
|
942
|
+
|
|
943
|
+
// Extract text content from assistant message
|
|
944
|
+
const content = assistantMessage.content;
|
|
945
|
+
let fullText = "";
|
|
946
|
+
for (const block of content) {
|
|
947
|
+
if (block.type === "text") {
|
|
948
|
+
fullText += block.text;
|
|
949
|
+
}
|
|
950
|
+
}
|
|
951
|
+
|
|
952
|
+
// If no text or split point is 0 or at/past end, don't split
|
|
953
|
+
if (fullText.length === 0 || splitPoint <= 0 || splitPoint >= fullText.length) {
|
|
954
|
+
// Emit assistant message first, then tool results (original behavior but with buffered results)
|
|
955
|
+
this.#state.streamMessage = null;
|
|
956
|
+
this.appendMessage(assistantMessage);
|
|
957
|
+
this.#emit({ type: "message_end", message: assistantMessage });
|
|
958
|
+
|
|
959
|
+
// Emit buffered tool results
|
|
960
|
+
for (const { toolResult } of buffer) {
|
|
961
|
+
this.#emit({ type: "message_start", message: toolResult });
|
|
962
|
+
this.appendMessage(toolResult);
|
|
963
|
+
this.#emit({ type: "message_end", message: toolResult });
|
|
964
|
+
}
|
|
965
|
+
return;
|
|
966
|
+
}
|
|
967
|
+
|
|
968
|
+
// Split the text
|
|
969
|
+
const preambleText = fullText.slice(0, splitPoint);
|
|
970
|
+
const continuationText = fullText.slice(splitPoint);
|
|
971
|
+
|
|
972
|
+
// Create preamble message (text before tools)
|
|
973
|
+
const preambleContent = content.map(block => {
|
|
974
|
+
if (block.type === "text") {
|
|
975
|
+
return { ...block, text: preambleText };
|
|
976
|
+
}
|
|
977
|
+
return block;
|
|
978
|
+
});
|
|
979
|
+
const preambleMessage: AssistantMessage = {
|
|
980
|
+
...assistantMessage,
|
|
981
|
+
content: preambleContent,
|
|
982
|
+
};
|
|
983
|
+
|
|
984
|
+
// Emit preamble
|
|
985
|
+
this.#state.streamMessage = null;
|
|
986
|
+
this.appendMessage(preambleMessage);
|
|
987
|
+
this.#emit({ type: "message_end", message: preambleMessage });
|
|
988
|
+
|
|
989
|
+
// Emit buffered tool results
|
|
990
|
+
for (const { toolResult } of buffer) {
|
|
991
|
+
this.#emit({ type: "message_start", message: toolResult });
|
|
992
|
+
this.appendMessage(toolResult);
|
|
993
|
+
this.#emit({ type: "message_end", message: toolResult });
|
|
994
|
+
}
|
|
995
|
+
|
|
996
|
+
// Emit continuation message (text after tools) if non-empty
|
|
997
|
+
const trimmedContinuation = continuationText.trim();
|
|
998
|
+
if (trimmedContinuation.length > 0) {
|
|
999
|
+
// Create continuation message with only text content (no thinking/toolCalls)
|
|
1000
|
+
const continuationContent: TextContent[] = [{ type: "text", text: continuationText }];
|
|
1001
|
+
const continuationMessage: AssistantMessage = {
|
|
1002
|
+
...assistantMessage,
|
|
1003
|
+
content: continuationContent,
|
|
1004
|
+
// Zero out usage for continuation since it's part of same response
|
|
1005
|
+
usage: {
|
|
1006
|
+
input: 0,
|
|
1007
|
+
output: 0,
|
|
1008
|
+
cacheRead: 0,
|
|
1009
|
+
cacheWrite: 0,
|
|
1010
|
+
totalTokens: 0,
|
|
1011
|
+
cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0, total: 0 },
|
|
1012
|
+
},
|
|
1013
|
+
};
|
|
1014
|
+
this.#emit({ type: "message_start", message: continuationMessage });
|
|
1015
|
+
this.appendMessage(continuationMessage);
|
|
1016
|
+
this.#emit({ type: "message_end", message: continuationMessage });
|
|
1017
|
+
}
|
|
1018
|
+
}
|
|
1019
|
+
}
|