wave-agent-sdk 0.0.6 → 0.0.8

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 (180) hide show
  1. package/dist/agent.d.ts +32 -20
  2. package/dist/agent.d.ts.map +1 -1
  3. package/dist/agent.js +209 -24
  4. package/dist/constants/events.d.ts +28 -0
  5. package/dist/constants/events.d.ts.map +1 -0
  6. package/dist/constants/events.js +27 -0
  7. package/dist/index.d.ts +2 -0
  8. package/dist/index.d.ts.map +1 -1
  9. package/dist/index.js +2 -0
  10. package/dist/managers/aiManager.d.ts +34 -1
  11. package/dist/managers/aiManager.d.ts.map +1 -1
  12. package/dist/managers/aiManager.js +248 -132
  13. package/dist/managers/backgroundBashManager.d.ts.map +1 -1
  14. package/dist/managers/backgroundBashManager.js +7 -6
  15. package/dist/managers/hookManager.d.ts +13 -16
  16. package/dist/managers/hookManager.d.ts.map +1 -1
  17. package/dist/managers/hookManager.js +81 -44
  18. package/dist/managers/liveConfigManager.d.ts +58 -0
  19. package/dist/managers/liveConfigManager.d.ts.map +1 -0
  20. package/dist/managers/liveConfigManager.js +160 -0
  21. package/dist/managers/messageManager.d.ts +41 -24
  22. package/dist/managers/messageManager.d.ts.map +1 -1
  23. package/dist/managers/messageManager.js +168 -49
  24. package/dist/managers/slashCommandManager.d.ts.map +1 -1
  25. package/dist/managers/slashCommandManager.js +9 -3
  26. package/dist/managers/subagentManager.d.ts +51 -0
  27. package/dist/managers/subagentManager.d.ts.map +1 -1
  28. package/dist/managers/subagentManager.js +190 -19
  29. package/dist/services/aiService.d.ts +13 -5
  30. package/dist/services/aiService.d.ts.map +1 -1
  31. package/dist/services/aiService.js +350 -74
  32. package/dist/services/configurationWatcher.d.ts +120 -0
  33. package/dist/services/configurationWatcher.d.ts.map +1 -0
  34. package/dist/services/configurationWatcher.js +439 -0
  35. package/dist/services/fileWatcher.d.ts +69 -0
  36. package/dist/services/fileWatcher.d.ts.map +1 -0
  37. package/dist/services/fileWatcher.js +213 -0
  38. package/dist/services/hook.d.ts +91 -9
  39. package/dist/services/hook.d.ts.map +1 -1
  40. package/dist/services/hook.js +393 -43
  41. package/dist/services/jsonlHandler.d.ts +62 -0
  42. package/dist/services/jsonlHandler.d.ts.map +1 -0
  43. package/dist/services/jsonlHandler.js +257 -0
  44. package/dist/services/memory.d.ts +9 -0
  45. package/dist/services/memory.d.ts.map +1 -1
  46. package/dist/services/memory.js +81 -12
  47. package/dist/services/memoryStore.d.ts +81 -0
  48. package/dist/services/memoryStore.d.ts.map +1 -0
  49. package/dist/services/memoryStore.js +200 -0
  50. package/dist/services/session.d.ts +64 -49
  51. package/dist/services/session.d.ts.map +1 -1
  52. package/dist/services/session.js +310 -132
  53. package/dist/tools/bashTool.d.ts.map +1 -1
  54. package/dist/tools/bashTool.js +5 -4
  55. package/dist/tools/deleteFileTool.d.ts.map +1 -1
  56. package/dist/tools/deleteFileTool.js +2 -1
  57. package/dist/tools/editTool.d.ts.map +1 -1
  58. package/dist/tools/editTool.js +3 -2
  59. package/dist/tools/multiEditTool.d.ts.map +1 -1
  60. package/dist/tools/multiEditTool.js +4 -3
  61. package/dist/tools/readTool.d.ts.map +1 -1
  62. package/dist/tools/readTool.js +2 -1
  63. package/dist/tools/todoWriteTool.d.ts.map +1 -1
  64. package/dist/tools/todoWriteTool.js +3 -10
  65. package/dist/tools/writeTool.d.ts.map +1 -1
  66. package/dist/tools/writeTool.js +5 -6
  67. package/dist/types/commands.d.ts +4 -0
  68. package/dist/types/commands.d.ts.map +1 -1
  69. package/dist/types/core.d.ts +35 -0
  70. package/dist/types/core.d.ts.map +1 -1
  71. package/dist/types/environment.d.ts +42 -0
  72. package/dist/types/environment.d.ts.map +1 -0
  73. package/dist/types/environment.js +21 -0
  74. package/dist/types/hooks.d.ts +8 -2
  75. package/dist/types/hooks.d.ts.map +1 -1
  76. package/dist/types/hooks.js +8 -2
  77. package/dist/types/index.d.ts +2 -0
  78. package/dist/types/index.d.ts.map +1 -1
  79. package/dist/types/index.js +2 -0
  80. package/dist/types/memoryStore.d.ts +82 -0
  81. package/dist/types/memoryStore.d.ts.map +1 -0
  82. package/dist/types/memoryStore.js +7 -0
  83. package/dist/types/messaging.d.ts +21 -9
  84. package/dist/types/messaging.d.ts.map +1 -1
  85. package/dist/types/messaging.js +5 -1
  86. package/dist/types/session.d.ts +20 -0
  87. package/dist/types/session.d.ts.map +1 -0
  88. package/dist/types/session.js +7 -0
  89. package/dist/utils/bashHistory.d.ts.map +1 -1
  90. package/dist/utils/bashHistory.js +27 -26
  91. package/dist/utils/cacheControlUtils.d.ts +121 -0
  92. package/dist/utils/cacheControlUtils.d.ts.map +1 -0
  93. package/dist/utils/cacheControlUtils.js +367 -0
  94. package/dist/utils/commandPathResolver.d.ts +52 -0
  95. package/dist/utils/commandPathResolver.d.ts.map +1 -0
  96. package/dist/utils/commandPathResolver.js +145 -0
  97. package/dist/utils/configPaths.d.ts +85 -0
  98. package/dist/utils/configPaths.d.ts.map +1 -0
  99. package/dist/utils/configPaths.js +121 -0
  100. package/dist/utils/configResolver.d.ts +37 -10
  101. package/dist/utils/configResolver.d.ts.map +1 -1
  102. package/dist/utils/configResolver.js +127 -23
  103. package/dist/utils/constants.d.ts +1 -1
  104. package/dist/utils/constants.js +1 -1
  105. package/dist/utils/convertMessagesForAPI.d.ts.map +1 -1
  106. package/dist/utils/convertMessagesForAPI.js +8 -13
  107. package/dist/utils/customCommands.d.ts.map +1 -1
  108. package/dist/utils/customCommands.js +66 -21
  109. package/dist/utils/fileUtils.d.ts +15 -0
  110. package/dist/utils/fileUtils.d.ts.map +1 -0
  111. package/dist/utils/fileUtils.js +61 -0
  112. package/dist/utils/globalLogger.d.ts +102 -0
  113. package/dist/utils/globalLogger.d.ts.map +1 -0
  114. package/dist/utils/globalLogger.js +136 -0
  115. package/dist/utils/hookMatcher.d.ts +1 -6
  116. package/dist/utils/hookMatcher.d.ts.map +1 -1
  117. package/dist/utils/mcpUtils.d.ts.map +1 -1
  118. package/dist/utils/mcpUtils.js +25 -3
  119. package/dist/utils/messageOperations.d.ts +27 -27
  120. package/dist/utils/messageOperations.d.ts.map +1 -1
  121. package/dist/utils/messageOperations.js +46 -36
  122. package/dist/utils/pathEncoder.d.ts +104 -0
  123. package/dist/utils/pathEncoder.d.ts.map +1 -0
  124. package/dist/utils/pathEncoder.js +272 -0
  125. package/dist/utils/subagentParser.d.ts.map +1 -1
  126. package/dist/utils/subagentParser.js +2 -1
  127. package/dist/utils/tokenCalculation.d.ts +26 -0
  128. package/dist/utils/tokenCalculation.d.ts.map +1 -0
  129. package/dist/utils/tokenCalculation.js +36 -0
  130. package/package.json +6 -3
  131. package/src/agent.ts +301 -37
  132. package/src/constants/events.ts +38 -0
  133. package/src/index.ts +2 -0
  134. package/src/managers/aiManager.ts +325 -173
  135. package/src/managers/backgroundBashManager.ts +7 -6
  136. package/src/managers/hookManager.ts +106 -84
  137. package/src/managers/liveConfigManager.ts +248 -0
  138. package/src/managers/messageManager.ts +237 -100
  139. package/src/managers/slashCommandManager.ts +9 -7
  140. package/src/managers/subagentManager.ts +284 -22
  141. package/src/services/aiService.ts +474 -83
  142. package/src/services/configurationWatcher.ts +622 -0
  143. package/src/services/fileWatcher.ts +301 -0
  144. package/src/services/hook.ts +538 -47
  145. package/src/services/jsonlHandler.ts +319 -0
  146. package/src/services/memory.ts +92 -12
  147. package/src/services/memoryStore.ts +279 -0
  148. package/src/services/session.ts +381 -157
  149. package/src/tools/bashTool.ts +5 -4
  150. package/src/tools/deleteFileTool.ts +2 -1
  151. package/src/tools/editTool.ts +3 -2
  152. package/src/tools/multiEditTool.ts +4 -3
  153. package/src/tools/readTool.ts +2 -1
  154. package/src/tools/todoWriteTool.ts +3 -11
  155. package/src/tools/writeTool.ts +7 -6
  156. package/src/types/commands.ts +6 -0
  157. package/src/types/core.ts +44 -0
  158. package/src/types/environment.ts +60 -0
  159. package/src/types/hooks.ts +21 -8
  160. package/src/types/index.ts +2 -0
  161. package/src/types/memoryStore.ts +94 -0
  162. package/src/types/messaging.ts +21 -10
  163. package/src/types/session.ts +25 -0
  164. package/src/utils/bashHistory.ts +27 -27
  165. package/src/utils/cacheControlUtils.ts +540 -0
  166. package/src/utils/commandPathResolver.ts +189 -0
  167. package/src/utils/configPaths.ts +163 -0
  168. package/src/utils/configResolver.ts +182 -22
  169. package/src/utils/constants.ts +1 -1
  170. package/src/utils/convertMessagesForAPI.ts +8 -14
  171. package/src/utils/customCommands.ts +90 -22
  172. package/src/utils/fileUtils.ts +65 -0
  173. package/src/utils/globalLogger.ts +145 -0
  174. package/src/utils/hookMatcher.ts +1 -12
  175. package/src/utils/mcpUtils.ts +34 -3
  176. package/src/utils/messageOperations.ts +77 -60
  177. package/src/utils/pathEncoder.ts +379 -0
  178. package/src/utils/subagentParser.ts +2 -1
  179. package/src/utils/tokenCalculation.ts +43 -0
  180. package/src/types/index.ts.backup +0 -357
@@ -2,14 +2,30 @@ import OpenAI from "openai";
2
2
  import { ChatCompletionMessageToolCall } from "openai/resources";
3
3
  import {
4
4
  ChatCompletionCreateParamsNonStreaming,
5
+ ChatCompletionCreateParamsStreaming,
5
6
  ChatCompletionMessageParam,
6
7
  ChatCompletionFunctionTool,
8
+ ChatCompletionChunk,
7
9
  } from "openai/resources.js";
10
+ import { logger } from "../utils/globalLogger.js";
8
11
  import type { GatewayConfig, ModelConfig } from "../types/index.js";
12
+ import {
13
+ transformMessagesForClaudeCache,
14
+ addCacheControlToLastTool,
15
+ isClaudeModel,
16
+ extendUsageWithCacheMetrics,
17
+ type ClaudeUsage,
18
+ } from "../utils/cacheControlUtils.js";
19
+
9
20
  import * as os from "os";
10
21
  import * as fs from "fs";
11
22
  import * as path from "path";
12
23
 
24
+ /**
25
+ * Use parametersChunk as compact param for better performance
26
+ * Instead of parsing JSON, we use the raw chunk for efficient streaming
27
+ */
28
+
13
29
  /**
14
30
  * Check if a directory is a git repository
15
31
  * @param dirPath Directory path to check
@@ -79,16 +95,31 @@ export interface CallAgentOptions {
79
95
  tools?: ChatCompletionFunctionTool[]; // Tool configuration
80
96
  model?: string; // Custom model
81
97
  systemPrompt?: string; // Custom system prompt
98
+
99
+ // NEW: Streaming callbacks
100
+ onContentUpdate?: (content: string) => void;
101
+ onToolUpdate?: (toolCall: {
102
+ id: string;
103
+ name: string;
104
+ parameters: string;
105
+ parametersChunk?: string;
106
+ stage?: "start" | "streaming" | "running" | "end";
107
+ }) => void;
82
108
  }
83
109
 
84
110
  export interface CallAgentResult {
85
111
  content?: string;
86
112
  tool_calls?: ChatCompletionMessageToolCall[];
87
- usage?: {
88
- prompt_tokens: number;
89
- completion_tokens: number;
90
- total_tokens: number;
91
- };
113
+ usage?: ClaudeUsage;
114
+ finish_reason?:
115
+ | "stop"
116
+ | "length"
117
+ | "tool_calls"
118
+ | "content_filter"
119
+ | "function_call"
120
+ | null;
121
+ response_headers?: Record<string, string>;
122
+ metadata?: Record<string, unknown>;
92
123
  }
93
124
 
94
125
  export async function callAgent(
@@ -104,6 +135,8 @@ export async function callAgent(
104
135
  tools,
105
136
  model,
106
137
  systemPrompt,
138
+ onContentUpdate,
139
+ onToolUpdate,
107
140
  } = options;
108
141
 
109
142
  try {
@@ -150,65 +183,371 @@ Today's date: ${new Date().toISOString().split("T")[0]}
150
183
  };
151
184
 
152
185
  // ChatCompletionMessageParam[] is already in OpenAI format, add system prompt to the beginning
153
- const openaiMessages: OpenAI.Chat.Completions.ChatCompletionMessageParam[] =
154
- [systemMessage, ...messages];
186
+ let openaiMessages: OpenAI.Chat.Completions.ChatCompletionMessageParam[] = [
187
+ systemMessage,
188
+ ...messages,
189
+ ];
190
+
191
+ // Apply cache control for Claude models
192
+ const currentModel = model || modelConfig.agentModel;
193
+
194
+ if (isClaudeModel(currentModel)) {
195
+ openaiMessages = transformMessagesForClaudeCache(
196
+ openaiMessages,
197
+ currentModel,
198
+ );
199
+ }
155
200
 
156
201
  // Get model configuration - use injected modelConfig with optional override
157
202
  const openaiModelConfig = getModelConfig(model || modelConfig.agentModel, {
158
203
  temperature: 0,
159
- max_completion_tokens: 32768,
160
204
  });
161
205
 
206
+ // Determine if streaming is needed
207
+ const isStreaming = !!(onContentUpdate || onToolUpdate);
208
+
162
209
  // Prepare API call parameters
163
- const createParams: ChatCompletionCreateParamsNonStreaming = {
210
+ const createParams = {
164
211
  ...openaiModelConfig,
165
212
  messages: openaiMessages,
166
- };
213
+ stream: isStreaming,
214
+ } as
215
+ | ChatCompletionCreateParamsNonStreaming
216
+ | ChatCompletionCreateParamsStreaming;
167
217
 
168
218
  // Only add tools if they exist
169
219
  if (tools && tools.length > 0) {
170
- createParams.tools = tools;
220
+ // Apply cache control to tools for Claude models
221
+ if (isClaudeModel(currentModel)) {
222
+ createParams.tools = addCacheControlToLastTool(tools);
223
+ } else {
224
+ createParams.tools = tools;
225
+ }
171
226
  }
172
227
 
173
- // Call OpenAI API (non-streaming)
174
- const response = await openai.chat.completions.create(createParams, {
175
- signal: abortSignal,
176
- });
228
+ if (isStreaming) {
229
+ // Handle streaming response
230
+ const { data: stream, response } = await openai.chat.completions
231
+ .create(createParams as ChatCompletionCreateParamsStreaming, {
232
+ signal: abortSignal,
233
+ })
234
+ .withResponse();
235
+
236
+ // Extract response headers
237
+ const responseHeaders: Record<string, string> = {};
238
+ response.headers.forEach((value, key) => {
239
+ responseHeaders[key] = value;
240
+ });
241
+
242
+ return await processStreamingResponse(
243
+ stream,
244
+ onContentUpdate,
245
+ onToolUpdate,
246
+ abortSignal,
247
+ responseHeaders,
248
+ currentModel,
249
+ );
250
+ } else {
251
+ // Handle non-streaming response
252
+ const { data: response, response: rawResponse } =
253
+ await openai.chat.completions
254
+ .create(createParams as ChatCompletionCreateParamsNonStreaming, {
255
+ signal: abortSignal,
256
+ })
257
+ .withResponse();
258
+
259
+ // Extract response headers
260
+ const responseHeaders: Record<string, string> = {};
261
+ rawResponse.headers.forEach((value, key) => {
262
+ responseHeaders[key] = value;
263
+ });
264
+
265
+ const finalMessage = response.choices[0]?.message;
266
+ const finishReason = response.choices[0]?.finish_reason || null;
267
+
268
+ let totalUsage = response.usage
269
+ ? {
270
+ prompt_tokens: response.usage.prompt_tokens,
271
+ completion_tokens: response.usage.completion_tokens,
272
+ total_tokens: response.usage.total_tokens,
273
+ }
274
+ : undefined;
275
+
276
+ // Extend usage with cache metrics for Claude models
277
+ if (totalUsage && isClaudeModel(currentModel) && response.usage) {
278
+ totalUsage = extendUsageWithCacheMetrics(
279
+ totalUsage,
280
+ response.usage as Partial<ClaudeUsage>,
281
+ );
282
+ }
177
283
 
178
- const finalMessage = response.choices[0]?.message;
179
- const totalUsage = response.usage
180
- ? {
181
- prompt_tokens: response.usage.prompt_tokens,
182
- completion_tokens: response.usage.completion_tokens,
183
- total_tokens: response.usage.total_tokens,
284
+ const result: CallAgentResult = {};
285
+
286
+ if (finalMessage) {
287
+ const {
288
+ content: finalContent,
289
+ tool_calls: finalToolCalls,
290
+ ...otherFields
291
+ } = finalMessage;
292
+
293
+ if (typeof finalContent === "string" && finalContent.length > 0) {
294
+ result.content = finalContent;
184
295
  }
185
- : undefined;
186
296
 
187
- const result: CallAgentResult = {};
297
+ if (Array.isArray(finalToolCalls) && finalToolCalls.length > 0) {
298
+ result.tool_calls = finalToolCalls;
299
+ }
188
300
 
189
- // Return content
190
- if (finalMessage?.content) {
191
- result.content = finalMessage.content;
192
- }
301
+ if (Object.keys(otherFields).length > 0) {
302
+ const metadata: Record<string, unknown> = {};
303
+ for (const [key, value] of Object.entries(otherFields)) {
304
+ if (value !== undefined) {
305
+ metadata[key] = value;
306
+ }
307
+ }
308
+ if (Object.keys(metadata).length > 0) {
309
+ result.metadata = metadata;
310
+ }
311
+ }
312
+ }
193
313
 
194
- // Return tool call
195
- if (finalMessage?.tool_calls && finalMessage.tool_calls.length > 0) {
196
- result.tool_calls = finalMessage.tool_calls;
197
- }
314
+ if (totalUsage) {
315
+ result.usage = totalUsage;
316
+ }
198
317
 
199
- // Return token usage information
200
- if (totalUsage) {
201
- result.usage = totalUsage;
202
- }
318
+ if (finishReason) {
319
+ result.finish_reason = finishReason;
320
+ }
321
+
322
+ if (Object.keys(responseHeaders).length > 0) {
323
+ result.response_headers = responseHeaders;
324
+ }
203
325
 
204
- return result;
326
+ return result;
327
+ }
205
328
  } catch (error) {
206
329
  if ((error as Error).name === "AbortError") {
207
330
  throw new Error("Request was aborted");
208
331
  }
209
- // // logger.error("Failed to call OpenAI:", error);
332
+ logger.error("Failed to call OpenAI:", error);
333
+ throw error;
334
+ }
335
+ }
336
+
337
+ /**
338
+ * Process streaming response from OpenAI API
339
+ * @param stream Async iterator of chat completion chunks
340
+ * @param onContentUpdate Callback for content updates
341
+ * @param onToolUpdate Callback for tool updates
342
+ * @param abortSignal Optional abort signal
343
+ * @param responseHeaders Response headers from the initial request
344
+ * @param modelName Model name for cache control processing
345
+ * @returns Final result with accumulated content and tool calls
346
+ */
347
+ async function processStreamingResponse(
348
+ stream: AsyncIterable<ChatCompletionChunk>,
349
+ onContentUpdate?: (content: string) => void,
350
+ onToolUpdate?: (toolCall: {
351
+ id: string;
352
+ name: string;
353
+ parameters: string;
354
+ parametersChunk?: string;
355
+ stage?: "start" | "streaming" | "running" | "end";
356
+ }) => void,
357
+ abortSignal?: AbortSignal,
358
+ responseHeaders?: Record<string, string>,
359
+ modelName?: string,
360
+ ): Promise<CallAgentResult> {
361
+ let accumulatedContent = "";
362
+ const toolCalls: {
363
+ id: string;
364
+ type: "function";
365
+ function: {
366
+ name: string;
367
+ arguments: string;
368
+ };
369
+ }[] = [];
370
+ const additionalDeltaFields: Record<string, unknown> = {};
371
+ let usage: CallAgentResult["usage"] = undefined;
372
+ let finishReason: CallAgentResult["finish_reason"] = null;
373
+
374
+ try {
375
+ for await (const chunk of stream) {
376
+ // Check for abort signal
377
+ if (abortSignal?.aborted) {
378
+ throw new Error("Request was aborted");
379
+ }
380
+
381
+ // Check for usage information in any chunk
382
+ if (chunk.usage) {
383
+ let chunkUsage = {
384
+ prompt_tokens: chunk.usage.prompt_tokens,
385
+ completion_tokens: chunk.usage.completion_tokens,
386
+ total_tokens: chunk.usage.total_tokens,
387
+ };
388
+
389
+ // Extend usage with cache metrics for Claude models
390
+ if (modelName && isClaudeModel(modelName)) {
391
+ chunkUsage = extendUsageWithCacheMetrics(
392
+ chunkUsage,
393
+ chunk.usage as Partial<ClaudeUsage>,
394
+ );
395
+ }
396
+
397
+ usage = chunkUsage;
398
+ }
399
+
400
+ // Check for finish_reason in the choice
401
+ const choice = chunk.choices?.[0];
402
+ if (choice?.finish_reason) {
403
+ finishReason = choice.finish_reason;
404
+ }
405
+
406
+ const delta = choice?.delta;
407
+ if (!delta) {
408
+ continue;
409
+ }
410
+
411
+ const {
412
+ content,
413
+ tool_calls: toolCallUpdates,
414
+ ...deltaMetadata
415
+ } = delta as unknown as {
416
+ content?: string;
417
+ tool_calls?: ChatCompletionChunk.Choice.Delta.ToolCall[];
418
+ [key: string]: unknown;
419
+ };
420
+
421
+ if (Object.keys(deltaMetadata).length > 0) {
422
+ Object.assign(additionalDeltaFields, deltaMetadata);
423
+ }
424
+
425
+ if (typeof content === "string" && content.length > 0) {
426
+ // Note: OpenAI API already handles UTF-8 character boundaries correctly in streaming,
427
+ // ensuring that delta.content always contains complete UTF-8 strings
428
+ accumulatedContent += content;
429
+ if (onContentUpdate) {
430
+ onContentUpdate(accumulatedContent);
431
+ }
432
+ }
433
+
434
+ if (Array.isArray(toolCallUpdates)) {
435
+ for (const rawToolCall of toolCallUpdates) {
436
+ const toolCallDelta =
437
+ rawToolCall as ChatCompletionChunk.Choice.Delta.ToolCall;
438
+
439
+ if (!toolCallDelta.function) {
440
+ continue;
441
+ }
442
+
443
+ const functionDelta = toolCallDelta.function;
444
+
445
+ let existingCall;
446
+ let isNew = false;
447
+
448
+ if (toolCallDelta.id) {
449
+ existingCall = toolCalls.find((t) => t.id === toolCallDelta.id);
450
+ if (!existingCall) {
451
+ existingCall = {
452
+ id: toolCallDelta.id,
453
+ type: "function" as const,
454
+ function: {
455
+ name: functionDelta.name || "",
456
+ arguments: "",
457
+ },
458
+ };
459
+ toolCalls.push(existingCall);
460
+ isNew = true;
461
+ }
462
+ } else {
463
+ existingCall = toolCalls[toolCalls.length - 1];
464
+ }
465
+
466
+ if (!existingCall) {
467
+ continue;
468
+ }
469
+
470
+ if (functionDelta.name) {
471
+ existingCall.function.name = functionDelta.name;
472
+ }
473
+
474
+ // Emit start stage when a new tool call is created and we have the tool name
475
+ if (onToolUpdate && isNew && existingCall.function.name) {
476
+ onToolUpdate({
477
+ id: existingCall.id,
478
+ name: existingCall.function.name,
479
+ parameters: "", // Empty parameters for start stage
480
+ parametersChunk: "", // Empty chunk for start stage
481
+ stage: "start", // New tool call triggers start stage
482
+ });
483
+ isNew = false; // Prevent duplicate start emissions
484
+ }
485
+
486
+ if (functionDelta.arguments) {
487
+ existingCall.function.arguments += functionDelta.arguments;
488
+ }
489
+
490
+ // Emit streaming updates for all chunks with actual content (including first chunk)
491
+ if (
492
+ onToolUpdate &&
493
+ existingCall.function.name &&
494
+ functionDelta.arguments &&
495
+ functionDelta.arguments.length > 0 // Only emit streaming for chunks with actual content
496
+ ) {
497
+ onToolUpdate({
498
+ id: existingCall.id,
499
+ name: existingCall.function.name,
500
+ parameters: existingCall.function.arguments,
501
+ parametersChunk: functionDelta.arguments,
502
+ stage: "streaming",
503
+ });
504
+ }
505
+ }
506
+ }
507
+ }
508
+ } catch (error) {
509
+ if ((error as Error).message === "Request was aborted") {
510
+ throw error;
511
+ }
210
512
  throw error;
211
513
  }
514
+
515
+ // Prepare final result
516
+ const result: CallAgentResult = {};
517
+
518
+ if (accumulatedContent) {
519
+ result.content = accumulatedContent;
520
+ }
521
+
522
+ if (toolCalls.length > 0) {
523
+ result.tool_calls = toolCalls;
524
+ }
525
+
526
+ if (usage) {
527
+ result.usage = usage;
528
+ }
529
+
530
+ if (finishReason) {
531
+ result.finish_reason = finishReason;
532
+ }
533
+
534
+ if (responseHeaders && Object.keys(responseHeaders).length > 0) {
535
+ result.response_headers = responseHeaders;
536
+ }
537
+
538
+ if (Object.keys(additionalDeltaFields).length > 0) {
539
+ result.metadata = {};
540
+ for (const [key, value] of Object.entries(additionalDeltaFields)) {
541
+ if (value !== undefined) {
542
+ result.metadata[key] = value;
543
+ }
544
+ }
545
+ if (Object.keys(result.metadata).length === 0) {
546
+ delete result.metadata;
547
+ }
548
+ }
549
+
550
+ return result;
212
551
  }
213
552
 
214
553
  export interface CompressMessagesOptions {
@@ -244,7 +583,7 @@ export async function compressMessages(
244
583
  // Get model configuration - use injected fast model
245
584
  const openaiModelConfig = getModelConfig(modelConfig.fastModel, {
246
585
  temperature: 0.1,
247
- max_tokens: 1500,
586
+ max_tokens: 2048,
248
587
  });
249
588
 
250
589
  try {
@@ -254,54 +593,106 @@ export async function compressMessages(
254
593
  messages: [
255
594
  {
256
595
  role: "system",
257
- content: `You are an expert conversation history compression specialist. Your task is to create comprehensive yet concise summaries that preserve critical development context.
258
-
259
- ## Primary Request and Intent
260
- Compress conversation history while maintaining all essential technical and procedural information.
261
-
262
- ## Key Technical Concepts
263
- - Code modifications and file operations
264
- - Tool executions and their results
265
- - Error handling and debugging processes
266
- - User requirements and assistant solutions
267
- - Technical discussions and decisions
268
-
269
- ## Compression Strategy
270
- 1. **Preserve Critical Information**:
271
- - All file paths, function names, and code examples
272
- - Tool execution results and outcomes
273
- - Error messages and resolution steps
274
- - User requirements and implementation approaches
275
- - Technical decisions and their reasoning
276
-
277
- 2. **Structure Organization**:
278
- - Group related actions and discussions
279
- - Maintain chronological flow for complex operations
280
- - Separate different technical topics clearly
281
-
282
- 3. **Context Preservation**:
283
- - Keep enough detail for future reference
284
- - Maintain relationships between requests and solutions
285
- - Preserve debugging context and error resolution paths
286
-
287
- ## Output Requirements:
288
- - Use third-person narrative format
289
- - Target 300-800 words (scale based on complexity)
290
- - Maintain the original conversation language
291
- - Structure with clear sections for multi-topic conversations
292
- - Focus on actionable information and outcomes
293
-
294
- ## Format Template:
295
- For technical conversations, structure as:
296
- - **User Requests**: Key requirements and goals
297
- - **Technical Implementation**: Code changes, file operations, tool usage
298
- - **Problem Resolution**: Errors encountered and solutions applied
299
- - **Outcomes**: Final results and current state`,
596
+ content: `Your task is to create a detailed summary of the conversation so far, paying close attention to the user's explicit requests and your previous actions.
597
+ This summary should be thorough in capturing technical details, code patterns, and architectural decisions that would be essential for continuing development work without losing context.
598
+
599
+ Before providing your final summary, wrap your analysis in <analysis> tags to organize your thoughts and ensure you've covered all necessary points. In your analysis process:
600
+
601
+ 1. Chronologically analyze each message and section of the conversation. For each section thoroughly identify:
602
+ - The user's explicit requests and intents
603
+ - Your approach to addressing the user's requests
604
+ - Key decisions, technical concepts and code patterns
605
+ - Specific details like:
606
+ - file names
607
+ - full code snippets
608
+ - function signatures
609
+ - file edits
610
+ - Errors that you ran into and how you fixed them
611
+ - Pay special attention to specific user feedback that you received, especially if the user told you to do something differently.
612
+ 2. Double-check for technical accuracy and completeness, addressing each required element thoroughly.
613
+
614
+ Your summary should include the following sections:
615
+
616
+ 1. Primary Request and Intent: Capture all of the user's explicit requests and intents in detail
617
+ 2. Key Technical Concepts: List all important technical concepts, technologies, and frameworks discussed.
618
+ 3. Files and Code Sections: Enumerate specific files and code sections examined, modified, or created. Pay special attention to the most recent messages and include full code snippets where applicable and include a summary of why this file read or edit is important.
619
+ 4. Errors and fixes: List all errors that you ran into, and how you fixed them. Pay special attention to specific user feedback that you received, especially if the user told you to do something differently.
620
+ 5. Problem Solving: Document problems solved and any ongoing troubleshooting efforts.
621
+ 6. All user messages: List ALL user messages that are not tool results. These are critical for understanding the users' feedback and changing intent.
622
+ 6. Pending Tasks: Outline any pending tasks that you have explicitly been asked to work on.
623
+ 7. Current Work: Describe in detail precisely what was being worked on immediately before this summary request, paying special attention to the most recent messages from both user and assistant. Include file names and code snippets where applicable.
624
+ 8. Optional Next Step: List the next step that you will take that is related to the most recent work you were doing. IMPORTANT: ensure that this step is DIRECTLY in line with the user's most recent explicit requests, and the task you were working on immediately before this summary request. If your last task was concluded, then only list next steps if they are explicitly in line with the users request. Do not start on tangential requests or really old requests that were already completed without confirming with the user first.
625
+ If there is a next step, include direct quotes from the most recent conversation showing exactly what task you were working on and where you left off. This should be verbatim to ensure there's no drift in task interpretation.
626
+
627
+ Here's an example of how your output should be structured:
628
+
629
+ <example>
630
+ <analysis>
631
+ [Your thought process, ensuring all points are covered thoroughly and accurately]
632
+ </analysis>
633
+
634
+ <summary>
635
+ 1. Primary Request and Intent:
636
+ [Detailed description]
637
+
638
+ 2. Key Technical Concepts:
639
+ - [Concept 1]
640
+ - [Concept 2]
641
+ - [...]
642
+
643
+ 3. Files and Code Sections:
644
+ - [File Name 1]
645
+ - [Summary of why this file is important]
646
+ - [Summary of the changes made to this file, if any]
647
+ - [Important Code Snippet]
648
+ - [File Name 2]
649
+ - [Important Code Snippet]
650
+ - [...]
651
+
652
+ 4. Errors and fixes:
653
+ - [Detailed description of error 1]:
654
+ - [How you fixed the error]
655
+ - [User feedback on the error if any]
656
+ - [...]
657
+
658
+ 5. Problem Solving:
659
+ [Description of solved problems and ongoing troubleshooting]
660
+
661
+ 6. All user messages:
662
+ - [Detailed non tool use user message]
663
+ - [...]
664
+
665
+ 7. Pending Tasks:
666
+ - [Task 1]
667
+ - [Task 2]
668
+ - [...]
669
+
670
+ 8. Current Work:
671
+ [Precise description of current work]
672
+
673
+ 9. Optional Next Step:
674
+ [Optional Next step to take]
675
+
676
+ </summary>
677
+ </example>
678
+
679
+ Please provide your summary based on the conversation so far, following this structure and ensuring precision and thoroughness in your response.
680
+
681
+ There may be additional summarization instructions provided in the included context. If so, remember to follow these instructions when creating the above summary. Examples of instructions include:
682
+ <example>
683
+ ## Compact Instructions
684
+ When summarizing the conversation focus on typescript code changes and also remember the mistakes you made and how you fixed them.
685
+ </example>
686
+
687
+ <example>
688
+ # Summary instructions
689
+ When you are using compact - please focus on test output and code changes. Include file reads verbatim.
690
+ </example>`,
300
691
  },
301
692
  ...messages,
302
693
  {
303
694
  role: "user",
304
- content: `Please compress this conversation following the structured approach. Focus on preserving all technical details, file operations, and problem-solving context while creating a concise summary.`,
695
+ content: `Please create a detailed summary of the conversation so far.`,
305
696
  },
306
697
  ],
307
698
  },
@@ -329,7 +720,7 @@ For technical conversations, structure as:
329
720
  if ((error as Error).name === "AbortError") {
330
721
  throw new Error("Compression request was aborted");
331
722
  }
332
- // // logger.error("Failed to compress messages:", error);
723
+ logger.error("Failed to compress messages:", error);
333
724
  return {
334
725
  content: "Failed to compress conversation history",
335
726
  usage: undefined,