wave-agent-sdk 0.0.7 → 0.0.10

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 (240) hide show
  1. package/dist/agent.d.ts +105 -24
  2. package/dist/agent.d.ts.map +1 -1
  3. package/dist/agent.js +438 -53
  4. package/dist/index.d.ts +4 -0
  5. package/dist/index.d.ts.map +1 -1
  6. package/dist/index.js +4 -0
  7. package/dist/managers/aiManager.d.ts +18 -7
  8. package/dist/managers/aiManager.d.ts.map +1 -1
  9. package/dist/managers/aiManager.js +254 -142
  10. package/dist/managers/backgroundBashManager.d.ts.map +1 -1
  11. package/dist/managers/backgroundBashManager.js +11 -9
  12. package/dist/managers/hookManager.d.ts +6 -6
  13. package/dist/managers/hookManager.d.ts.map +1 -1
  14. package/dist/managers/hookManager.js +81 -39
  15. package/dist/managers/liveConfigManager.d.ts +95 -0
  16. package/dist/managers/liveConfigManager.d.ts.map +1 -0
  17. package/dist/managers/liveConfigManager.js +442 -0
  18. package/dist/managers/lspManager.d.ts +43 -0
  19. package/dist/managers/lspManager.d.ts.map +1 -0
  20. package/dist/managers/lspManager.js +326 -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 +184 -73
  24. package/dist/managers/permissionManager.d.ts +66 -0
  25. package/dist/managers/permissionManager.d.ts.map +1 -0
  26. package/dist/managers/permissionManager.js +208 -0
  27. package/dist/managers/skillManager.d.ts +1 -0
  28. package/dist/managers/skillManager.d.ts.map +1 -1
  29. package/dist/managers/skillManager.js +2 -1
  30. package/dist/managers/slashCommandManager.d.ts.map +1 -1
  31. package/dist/managers/slashCommandManager.js +4 -2
  32. package/dist/managers/subagentManager.d.ts +42 -6
  33. package/dist/managers/subagentManager.d.ts.map +1 -1
  34. package/dist/managers/subagentManager.js +213 -62
  35. package/dist/managers/toolManager.d.ts +38 -1
  36. package/dist/managers/toolManager.d.ts.map +1 -1
  37. package/dist/managers/toolManager.js +66 -2
  38. package/dist/services/aiService.d.ts +15 -5
  39. package/dist/services/aiService.d.ts.map +1 -1
  40. package/dist/services/aiService.js +446 -77
  41. package/dist/services/configurationService.d.ts +116 -0
  42. package/dist/services/configurationService.d.ts.map +1 -0
  43. package/dist/services/configurationService.js +585 -0
  44. package/dist/services/fileWatcher.d.ts +69 -0
  45. package/dist/services/fileWatcher.d.ts.map +1 -0
  46. package/dist/services/fileWatcher.js +212 -0
  47. package/dist/services/hook.d.ts +5 -40
  48. package/dist/services/hook.d.ts.map +1 -1
  49. package/dist/services/hook.js +47 -109
  50. package/dist/services/jsonlHandler.d.ts +71 -0
  51. package/dist/services/jsonlHandler.d.ts.map +1 -0
  52. package/dist/services/jsonlHandler.js +236 -0
  53. package/dist/services/memory.d.ts.map +1 -1
  54. package/dist/services/memory.js +33 -11
  55. package/dist/services/session.d.ts +116 -52
  56. package/dist/services/session.d.ts.map +1 -1
  57. package/dist/services/session.js +415 -143
  58. package/dist/tools/bashTool.d.ts.map +1 -1
  59. package/dist/tools/bashTool.js +77 -17
  60. package/dist/tools/deleteFileTool.d.ts.map +1 -1
  61. package/dist/tools/deleteFileTool.js +27 -1
  62. package/dist/tools/editTool.d.ts.map +1 -1
  63. package/dist/tools/editTool.js +33 -8
  64. package/dist/tools/lspTool.d.ts +6 -0
  65. package/dist/tools/lspTool.d.ts.map +1 -0
  66. package/dist/tools/lspTool.js +589 -0
  67. package/dist/tools/multiEditTool.d.ts.map +1 -1
  68. package/dist/tools/multiEditTool.js +30 -10
  69. package/dist/tools/readTool.d.ts.map +1 -1
  70. package/dist/tools/readTool.js +113 -3
  71. package/dist/tools/skillTool.js +2 -2
  72. package/dist/tools/todoWriteTool.d.ts.map +1 -1
  73. package/dist/tools/todoWriteTool.js +23 -0
  74. package/dist/tools/types.d.ts +11 -8
  75. package/dist/tools/types.d.ts.map +1 -1
  76. package/dist/tools/writeTool.d.ts.map +1 -1
  77. package/dist/tools/writeTool.js +30 -15
  78. package/dist/types/commands.d.ts +4 -1
  79. package/dist/types/commands.d.ts.map +1 -1
  80. package/dist/types/config.d.ts +4 -0
  81. package/dist/types/config.d.ts.map +1 -1
  82. package/dist/types/configuration.d.ts +69 -0
  83. package/dist/types/configuration.d.ts.map +1 -0
  84. package/dist/types/configuration.js +8 -0
  85. package/dist/types/core.d.ts +45 -0
  86. package/dist/types/core.d.ts.map +1 -1
  87. package/dist/types/environment.d.ts +83 -0
  88. package/dist/types/environment.d.ts.map +1 -0
  89. package/dist/types/environment.js +21 -0
  90. package/dist/types/fileSearch.d.ts +5 -0
  91. package/dist/types/fileSearch.d.ts.map +1 -0
  92. package/dist/types/fileSearch.js +1 -0
  93. package/dist/types/hooks.d.ts +18 -3
  94. package/dist/types/hooks.d.ts.map +1 -1
  95. package/dist/types/hooks.js +8 -8
  96. package/dist/types/index.d.ts +7 -0
  97. package/dist/types/index.d.ts.map +1 -1
  98. package/dist/types/index.js +7 -0
  99. package/dist/types/lsp.d.ts +90 -0
  100. package/dist/types/lsp.d.ts.map +1 -0
  101. package/dist/types/lsp.js +4 -0
  102. package/dist/types/messaging.d.ts +19 -12
  103. package/dist/types/messaging.d.ts.map +1 -1
  104. package/dist/types/permissions.d.ts +35 -0
  105. package/dist/types/permissions.d.ts.map +1 -0
  106. package/dist/types/permissions.js +12 -0
  107. package/dist/types/session.d.ts +15 -0
  108. package/dist/types/session.d.ts.map +1 -0
  109. package/dist/types/session.js +7 -0
  110. package/dist/types/skills.d.ts +1 -0
  111. package/dist/types/skills.d.ts.map +1 -1
  112. package/dist/types/tools.d.ts +35 -0
  113. package/dist/types/tools.d.ts.map +1 -0
  114. package/dist/types/tools.js +4 -0
  115. package/dist/utils/abortUtils.d.ts +34 -0
  116. package/dist/utils/abortUtils.d.ts.map +1 -0
  117. package/dist/utils/abortUtils.js +92 -0
  118. package/dist/utils/bashHistory.d.ts +4 -0
  119. package/dist/utils/bashHistory.d.ts.map +1 -1
  120. package/dist/utils/bashHistory.js +48 -30
  121. package/dist/utils/builtinSubagents.d.ts +7 -0
  122. package/dist/utils/builtinSubagents.d.ts.map +1 -0
  123. package/dist/utils/builtinSubagents.js +65 -0
  124. package/dist/utils/cacheControlUtils.d.ts +96 -0
  125. package/dist/utils/cacheControlUtils.d.ts.map +1 -0
  126. package/dist/utils/cacheControlUtils.js +324 -0
  127. package/dist/utils/commandPathResolver.d.ts +52 -0
  128. package/dist/utils/commandPathResolver.d.ts.map +1 -0
  129. package/dist/utils/commandPathResolver.js +145 -0
  130. package/dist/utils/configPaths.d.ts +85 -0
  131. package/dist/utils/configPaths.d.ts.map +1 -0
  132. package/dist/utils/configPaths.js +121 -0
  133. package/dist/utils/constants.d.ts +1 -13
  134. package/dist/utils/constants.d.ts.map +1 -1
  135. package/dist/utils/constants.js +2 -14
  136. package/dist/utils/convertMessagesForAPI.d.ts +2 -1
  137. package/dist/utils/convertMessagesForAPI.d.ts.map +1 -1
  138. package/dist/utils/convertMessagesForAPI.js +39 -18
  139. package/dist/utils/customCommands.d.ts.map +1 -1
  140. package/dist/utils/customCommands.js +66 -21
  141. package/dist/utils/fileSearch.d.ts +14 -0
  142. package/dist/utils/fileSearch.d.ts.map +1 -0
  143. package/dist/utils/fileSearch.js +88 -0
  144. package/dist/utils/fileUtils.d.ts +27 -0
  145. package/dist/utils/fileUtils.d.ts.map +1 -0
  146. package/dist/utils/fileUtils.js +145 -0
  147. package/dist/utils/globalLogger.d.ts +88 -0
  148. package/dist/utils/globalLogger.d.ts.map +1 -0
  149. package/dist/utils/globalLogger.js +120 -0
  150. package/dist/utils/largeOutputHandler.d.ts +15 -0
  151. package/dist/utils/largeOutputHandler.d.ts.map +1 -0
  152. package/dist/utils/largeOutputHandler.js +40 -0
  153. package/dist/utils/markdownParser.d.ts.map +1 -1
  154. package/dist/utils/markdownParser.js +1 -17
  155. package/dist/utils/mcpUtils.d.ts.map +1 -1
  156. package/dist/utils/mcpUtils.js +25 -3
  157. package/dist/utils/messageOperations.d.ts +20 -18
  158. package/dist/utils/messageOperations.d.ts.map +1 -1
  159. package/dist/utils/messageOperations.js +30 -38
  160. package/dist/utils/pathEncoder.d.ts +108 -0
  161. package/dist/utils/pathEncoder.d.ts.map +1 -0
  162. package/dist/utils/pathEncoder.js +279 -0
  163. package/dist/utils/subagentParser.d.ts +2 -2
  164. package/dist/utils/subagentParser.d.ts.map +1 -1
  165. package/dist/utils/subagentParser.js +12 -8
  166. package/dist/utils/tokenCalculation.d.ts +26 -0
  167. package/dist/utils/tokenCalculation.d.ts.map +1 -0
  168. package/dist/utils/tokenCalculation.js +36 -0
  169. package/dist/utils/tokenEstimator.d.ts +39 -0
  170. package/dist/utils/tokenEstimator.d.ts.map +1 -0
  171. package/dist/utils/tokenEstimator.js +55 -0
  172. package/package.json +6 -6
  173. package/src/agent.ts +586 -78
  174. package/src/index.ts +4 -0
  175. package/src/managers/aiManager.ts +341 -192
  176. package/src/managers/backgroundBashManager.ts +11 -9
  177. package/src/managers/hookManager.ts +102 -54
  178. package/src/managers/liveConfigManager.ts +634 -0
  179. package/src/managers/lspManager.ts +434 -0
  180. package/src/managers/messageManager.ts +258 -121
  181. package/src/managers/permissionManager.ts +276 -0
  182. package/src/managers/skillManager.ts +3 -1
  183. package/src/managers/slashCommandManager.ts +5 -3
  184. package/src/managers/subagentManager.ts +295 -76
  185. package/src/managers/toolManager.ts +95 -3
  186. package/src/services/aiService.ts +656 -84
  187. package/src/services/configurationService.ts +762 -0
  188. package/src/services/fileWatcher.ts +300 -0
  189. package/src/services/hook.ts +54 -144
  190. package/src/services/jsonlHandler.ts +303 -0
  191. package/src/services/memory.ts +34 -11
  192. package/src/services/session.ts +522 -173
  193. package/src/tools/bashTool.ts +94 -20
  194. package/src/tools/deleteFileTool.ts +38 -1
  195. package/src/tools/editTool.ts +44 -9
  196. package/src/tools/lspTool.ts +760 -0
  197. package/src/tools/multiEditTool.ts +41 -11
  198. package/src/tools/readTool.ts +127 -3
  199. package/src/tools/skillTool.ts +2 -2
  200. package/src/tools/todoWriteTool.ts +33 -1
  201. package/src/tools/types.ts +15 -9
  202. package/src/tools/writeTool.ts +43 -16
  203. package/src/types/commands.ts +6 -1
  204. package/src/types/config.ts +5 -0
  205. package/src/types/configuration.ts +73 -0
  206. package/src/types/core.ts +55 -0
  207. package/src/types/environment.ts +104 -0
  208. package/src/types/fileSearch.ts +4 -0
  209. package/src/types/hooks.ts +32 -16
  210. package/src/types/index.ts +7 -0
  211. package/src/types/lsp.ts +96 -0
  212. package/src/types/messaging.ts +21 -14
  213. package/src/types/permissions.ts +48 -0
  214. package/src/types/session.ts +20 -0
  215. package/src/types/skills.ts +1 -0
  216. package/src/types/tools.ts +38 -0
  217. package/src/utils/abortUtils.ts +118 -0
  218. package/src/utils/bashHistory.ts +55 -31
  219. package/src/utils/builtinSubagents.ts +71 -0
  220. package/src/utils/cacheControlUtils.ts +475 -0
  221. package/src/utils/commandPathResolver.ts +189 -0
  222. package/src/utils/configPaths.ts +163 -0
  223. package/src/utils/constants.ts +2 -17
  224. package/src/utils/convertMessagesForAPI.ts +44 -18
  225. package/src/utils/customCommands.ts +90 -22
  226. package/src/utils/fileSearch.ts +107 -0
  227. package/src/utils/fileUtils.ts +160 -0
  228. package/src/utils/globalLogger.ts +128 -0
  229. package/src/utils/largeOutputHandler.ts +55 -0
  230. package/src/utils/markdownParser.ts +1 -19
  231. package/src/utils/mcpUtils.ts +34 -3
  232. package/src/utils/messageOperations.ts +47 -53
  233. package/src/utils/pathEncoder.ts +394 -0
  234. package/src/utils/subagentParser.ts +13 -9
  235. package/src/utils/tokenCalculation.ts +43 -0
  236. package/src/utils/tokenEstimator.ts +68 -0
  237. package/dist/utils/configResolver.d.ts +0 -38
  238. package/dist/utils/configResolver.d.ts.map +0 -1
  239. package/dist/utils/configResolver.js +0 -106
  240. package/src/utils/configResolver.ts +0 -142
@@ -2,14 +2,65 @@ 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
+ * Interface for debug data saved during 400 errors
26
+ */
27
+ interface DebugData {
28
+ originalMessages: ChatCompletionMessageParam[];
29
+ timestamp: string;
30
+ model: string;
31
+ workdir: string;
32
+ sessionId?: string;
33
+ gatewayConfig: {
34
+ baseURL?: string;
35
+ defaultHeaders?: Record<string, string>;
36
+ };
37
+ processedMessages?: OpenAI.Chat.Completions.ChatCompletionMessageParam[];
38
+ createParams?:
39
+ | ChatCompletionCreateParamsNonStreaming
40
+ | ChatCompletionCreateParamsStreaming;
41
+ tools?: ChatCompletionFunctionTool[];
42
+ }
43
+
44
+ /**
45
+ * Interface for error data saved during 400 errors
46
+ */
47
+ interface ErrorData {
48
+ error: {
49
+ message?: string;
50
+ status?: number;
51
+ type?: string;
52
+ code?: string;
53
+ body?: unknown;
54
+ stack?: string;
55
+ };
56
+ timestamp: string;
57
+ }
58
+
59
+ /**
60
+ * Use parametersChunk as compact param for better performance
61
+ * Instead of parsing JSON, we use the raw chunk for efficient streaming
62
+ */
63
+
13
64
  /**
14
65
  * Check if a directory is a git repository
15
66
  * @param dirPath Directory path to check
@@ -79,16 +130,33 @@ export interface CallAgentOptions {
79
130
  tools?: ChatCompletionFunctionTool[]; // Tool configuration
80
131
  model?: string; // Custom model
81
132
  systemPrompt?: string; // Custom system prompt
133
+
134
+ // NEW: Streaming callbacks
135
+ onContentUpdate?: (content: string) => void;
136
+ onToolUpdate?: (toolCall: {
137
+ id: string;
138
+ name: string;
139
+ parameters: string;
140
+ parametersChunk?: string;
141
+ stage?: "start" | "streaming" | "running" | "end";
142
+ }) => void;
143
+ onReasoningUpdate?: (content: string) => void;
82
144
  }
83
145
 
84
146
  export interface CallAgentResult {
85
147
  content?: string;
86
148
  tool_calls?: ChatCompletionMessageToolCall[];
87
- usage?: {
88
- prompt_tokens: number;
89
- completion_tokens: number;
90
- total_tokens: number;
91
- };
149
+ reasoning_content?: string;
150
+ usage?: ClaudeUsage;
151
+ finish_reason?:
152
+ | "stop"
153
+ | "length"
154
+ | "tool_calls"
155
+ | "content_filter"
156
+ | "function_call"
157
+ | null;
158
+ response_headers?: Record<string, string>;
159
+ additionalFields?: Record<string, unknown>;
92
160
  }
93
161
 
94
162
  export async function callAgent(
@@ -104,13 +172,29 @@ export async function callAgent(
104
172
  tools,
105
173
  model,
106
174
  systemPrompt,
175
+ onContentUpdate,
176
+ onToolUpdate,
177
+ onReasoningUpdate,
107
178
  } = options;
108
179
 
180
+ // Declare variables outside try block for error handling access
181
+ let openaiMessages:
182
+ | OpenAI.Chat.Completions.ChatCompletionMessageParam[]
183
+ | undefined;
184
+ let createParams:
185
+ | ChatCompletionCreateParamsNonStreaming
186
+ | ChatCompletionCreateParamsStreaming
187
+ | undefined;
188
+ let processedTools: ChatCompletionFunctionTool[] | undefined;
189
+
109
190
  try {
110
191
  // Create OpenAI client with injected configuration
111
192
  const openai = new OpenAI({
112
193
  apiKey: gatewayConfig.apiKey,
113
194
  baseURL: gatewayConfig.baseURL,
195
+ defaultHeaders: gatewayConfig.defaultHeaders,
196
+ fetchOptions: gatewayConfig.fetchOptions,
197
+ fetch: gatewayConfig.fetch,
114
198
  });
115
199
 
116
200
  // Build system prompt content
@@ -150,65 +234,498 @@ Today's date: ${new Date().toISOString().split("T")[0]}
150
234
  };
151
235
 
152
236
  // ChatCompletionMessageParam[] is already in OpenAI format, add system prompt to the beginning
153
- const openaiMessages: OpenAI.Chat.Completions.ChatCompletionMessageParam[] =
154
- [systemMessage, ...messages];
237
+ openaiMessages = [systemMessage, ...messages];
238
+
239
+ // Apply cache control for Claude models
240
+ const currentModel = model || modelConfig.agentModel;
241
+
242
+ processedTools = tools;
243
+
244
+ if (isClaudeModel(currentModel)) {
245
+ openaiMessages = transformMessagesForClaudeCache(
246
+ openaiMessages,
247
+ currentModel,
248
+ );
249
+
250
+ // Apply cache control to tools separately
251
+ if (tools && tools.length > 0) {
252
+ processedTools = addCacheControlToLastTool(tools);
253
+ }
254
+ }
155
255
 
156
256
  // Get model configuration - use injected modelConfig with optional override
157
257
  const openaiModelConfig = getModelConfig(model || modelConfig.agentModel, {
158
258
  temperature: 0,
159
- max_completion_tokens: 32768,
160
259
  });
161
260
 
261
+ // Determine if streaming is needed
262
+ const isStreaming = !!(
263
+ onContentUpdate ||
264
+ onToolUpdate ||
265
+ onReasoningUpdate
266
+ );
267
+
162
268
  // Prepare API call parameters
163
- const createParams: ChatCompletionCreateParamsNonStreaming = {
269
+ createParams = {
164
270
  ...openaiModelConfig,
165
271
  messages: openaiMessages,
166
- };
272
+ stream: isStreaming,
273
+ } as
274
+ | ChatCompletionCreateParamsNonStreaming
275
+ | ChatCompletionCreateParamsStreaming;
167
276
 
168
277
  // Only add tools if they exist
169
- if (tools && tools.length > 0) {
170
- createParams.tools = tools;
278
+ if (processedTools && processedTools.length > 0) {
279
+ createParams.tools = processedTools;
171
280
  }
172
281
 
173
- // Call OpenAI API (non-streaming)
174
- const response = await openai.chat.completions.create(createParams, {
175
- signal: abortSignal,
176
- });
282
+ if (isStreaming) {
283
+ // Handle streaming response
284
+ const { data: stream, response } = await openai.chat.completions
285
+ .create(createParams as ChatCompletionCreateParamsStreaming, {
286
+ signal: abortSignal,
287
+ })
288
+ .withResponse();
289
+
290
+ // Extract response headers
291
+ const responseHeaders: Record<string, string> = {};
292
+ response.headers.forEach((value, key) => {
293
+ responseHeaders[key] = value;
294
+ });
295
+
296
+ return await processStreamingResponse(
297
+ stream,
298
+ onContentUpdate,
299
+ onToolUpdate,
300
+ onReasoningUpdate,
301
+ abortSignal,
302
+ responseHeaders,
303
+ currentModel,
304
+ );
305
+ } else {
306
+ // Handle non-streaming response
307
+ const { data: response, response: rawResponse } =
308
+ await openai.chat.completions
309
+ .create(createParams as ChatCompletionCreateParamsNonStreaming, {
310
+ signal: abortSignal,
311
+ })
312
+ .withResponse();
313
+
314
+ // Extract response headers
315
+ const responseHeaders: Record<string, string> = {};
316
+ rawResponse.headers.forEach((value, key) => {
317
+ responseHeaders[key] = value;
318
+ });
319
+
320
+ const finalMessage = response.choices[0]?.message;
321
+ const finishReason = response.choices[0]?.finish_reason || null;
322
+
323
+ let totalUsage = response.usage
324
+ ? {
325
+ prompt_tokens: response.usage.prompt_tokens,
326
+ completion_tokens: response.usage.completion_tokens,
327
+ total_tokens: response.usage.total_tokens,
328
+ }
329
+ : undefined;
330
+
331
+ // Extend usage with cache metrics for Claude models
332
+ if (totalUsage && isClaudeModel(currentModel) && response.usage) {
333
+ totalUsage = extendUsageWithCacheMetrics(
334
+ totalUsage,
335
+ response.usage as Partial<ClaudeUsage>,
336
+ );
337
+ }
177
338
 
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,
339
+ const result: CallAgentResult = {};
340
+
341
+ if (finalMessage) {
342
+ const {
343
+ content: finalContent,
344
+ tool_calls: finalToolCalls,
345
+ reasoning_content: finalReasoningContent,
346
+ ...otherFields
347
+ } = finalMessage as unknown as {
348
+ content?: string;
349
+ tool_calls?: ChatCompletionMessageToolCall[];
350
+ reasoning_content?: string;
351
+ [key: string]: unknown;
352
+ };
353
+
354
+ if (typeof finalContent === "string" && finalContent.length > 0) {
355
+ result.content = finalContent;
184
356
  }
185
- : undefined;
186
357
 
187
- const result: CallAgentResult = {};
358
+ if (
359
+ typeof finalReasoningContent === "string" &&
360
+ finalReasoningContent.length > 0
361
+ ) {
362
+ result.reasoning_content = finalReasoningContent;
363
+ }
188
364
 
189
- // Return content
190
- if (finalMessage?.content) {
191
- result.content = finalMessage.content;
192
- }
365
+ if (Array.isArray(finalToolCalls) && finalToolCalls.length > 0) {
366
+ result.tool_calls = finalToolCalls;
367
+ }
193
368
 
194
- // Return tool call
195
- if (finalMessage?.tool_calls && finalMessage.tool_calls.length > 0) {
196
- result.tool_calls = finalMessage.tool_calls;
197
- }
369
+ if (Object.keys(otherFields).length > 0) {
370
+ const additionalFields: Record<string, unknown> = {};
371
+ for (const [key, value] of Object.entries(otherFields)) {
372
+ if (value !== undefined && key !== "role") {
373
+ additionalFields[key] = value;
374
+ }
375
+ }
376
+ if (Object.keys(additionalFields).length > 0) {
377
+ result.additionalFields = additionalFields;
378
+ }
379
+ }
380
+ }
198
381
 
199
- // Return token usage information
200
- if (totalUsage) {
201
- result.usage = totalUsage;
202
- }
382
+ if (totalUsage) {
383
+ result.usage = totalUsage;
384
+ }
385
+
386
+ if (finishReason) {
387
+ result.finish_reason = finishReason;
388
+ }
203
389
 
204
- return result;
390
+ if (Object.keys(responseHeaders).length > 0) {
391
+ result.response_headers = responseHeaders;
392
+ }
393
+
394
+ return result;
395
+ }
205
396
  } catch (error) {
206
397
  if ((error as Error).name === "AbortError") {
207
398
  throw new Error("Request was aborted");
208
399
  }
209
- // // logger.error("Failed to call OpenAI:", error);
400
+
401
+ // Check if it's a 400 error and save messages to temp directory
402
+ if (
403
+ error &&
404
+ typeof error === "object" &&
405
+ "status" in error &&
406
+ error.status === 400
407
+ ) {
408
+ try {
409
+ // Create temp directory for error debugging
410
+ const tempDir = fs.mkdtempSync(
411
+ path.join(os.tmpdir(), "callAgent-400-error-"),
412
+ );
413
+ const messagesFile = path.join(tempDir, "messages.json");
414
+ const errorFile = path.join(tempDir, "error.json");
415
+
416
+ // Save complete messages to temp file
417
+ const debugData: DebugData = {
418
+ originalMessages: messages,
419
+ timestamp: new Date().toISOString(),
420
+ model: model || modelConfig.agentModel,
421
+ workdir,
422
+ sessionId: options.sessionId,
423
+ gatewayConfig: {
424
+ baseURL: gatewayConfig.baseURL,
425
+ // Don't include apiKey for security
426
+ defaultHeaders: gatewayConfig.defaultHeaders,
427
+ },
428
+ };
429
+
430
+ // Add processed messages if they exist
431
+ if (typeof openaiMessages !== "undefined") {
432
+ debugData.processedMessages = openaiMessages;
433
+ }
434
+
435
+ // Add create params if they exist
436
+ if (typeof createParams !== "undefined") {
437
+ debugData.createParams = createParams;
438
+ }
439
+
440
+ // Add tools if they exist
441
+ if (processedTools) {
442
+ debugData.tools = processedTools;
443
+ }
444
+
445
+ fs.writeFileSync(messagesFile, JSON.stringify(debugData, null, 2));
446
+
447
+ // Save error details
448
+ const errorData: ErrorData = {
449
+ error: {
450
+ message:
451
+ error && typeof error === "object" && "message" in error
452
+ ? String(error.message)
453
+ : undefined,
454
+ status:
455
+ error && typeof error === "object" && "status" in error
456
+ ? Number(error.status)
457
+ : undefined,
458
+ type:
459
+ error && typeof error === "object" && "type" in error
460
+ ? String(error.type)
461
+ : undefined,
462
+ code:
463
+ error && typeof error === "object" && "code" in error
464
+ ? String(error.code)
465
+ : undefined,
466
+ body:
467
+ error && typeof error === "object" && "body" in error
468
+ ? error.body
469
+ : undefined,
470
+ stack:
471
+ error && typeof error === "object" && "stack" in error
472
+ ? String(error.stack)
473
+ : undefined,
474
+ },
475
+ timestamp: new Date().toISOString(),
476
+ };
477
+
478
+ fs.writeFileSync(errorFile, JSON.stringify(errorData, null, 2));
479
+
480
+ logger.error(
481
+ "callAgent 400 error occurred. Debug files saved to:",
482
+ tempDir,
483
+ );
484
+ logger.error("Messages file:", messagesFile);
485
+ logger.error("Error file:", errorFile);
486
+ logger.error("Error details:", error);
487
+ } catch (saveError) {
488
+ logger.error("Failed to save 400 error debug files:", saveError);
489
+ }
490
+ }
491
+
492
+ logger.error("Failed to call OpenAI:", error);
493
+ throw error;
494
+ }
495
+ }
496
+
497
+ /**
498
+ * Process streaming response from OpenAI API
499
+ * @param stream Async iterator of chat completion chunks
500
+ * @param onContentUpdate Callback for content updates
501
+ * @param onToolUpdate Callback for tool updates
502
+ * @param abortSignal Optional abort signal
503
+ * @param responseHeaders Response headers from the initial request
504
+ * @param modelName Model name for cache control processing
505
+ * @returns Final result with accumulated content and tool calls
506
+ */
507
+ async function processStreamingResponse(
508
+ stream: AsyncIterable<ChatCompletionChunk>,
509
+ onContentUpdate?: (content: string) => void,
510
+ onToolUpdate?: (toolCall: {
511
+ id: string;
512
+ name: string;
513
+ parameters: string;
514
+ parametersChunk?: string;
515
+ stage?: "start" | "streaming" | "running" | "end";
516
+ }) => void,
517
+ onReasoningUpdate?: (content: string) => void,
518
+ abortSignal?: AbortSignal,
519
+ responseHeaders?: Record<string, string>,
520
+ modelName?: string,
521
+ ): Promise<CallAgentResult> {
522
+ let accumulatedContent = "";
523
+ let accumulatedReasoningContent = "";
524
+ const toolCalls: {
525
+ id: string;
526
+ type: "function";
527
+ function: {
528
+ name: string;
529
+ arguments: string;
530
+ };
531
+ }[] = [];
532
+ const additionalDeltaFields: Record<string, unknown> = {};
533
+ let usage: CallAgentResult["usage"] = undefined;
534
+ let finishReason: CallAgentResult["finish_reason"] = null;
535
+
536
+ try {
537
+ for await (const chunk of stream) {
538
+ // Check for abort signal
539
+ if (abortSignal?.aborted) {
540
+ throw new Error("Request was aborted");
541
+ }
542
+
543
+ // Check for usage information in any chunk
544
+ if (chunk.usage) {
545
+ let chunkUsage = {
546
+ prompt_tokens: chunk.usage.prompt_tokens,
547
+ completion_tokens: chunk.usage.completion_tokens,
548
+ total_tokens: chunk.usage.total_tokens,
549
+ };
550
+
551
+ // Extend usage with cache metrics for Claude models
552
+ if (modelName && isClaudeModel(modelName)) {
553
+ chunkUsage = extendUsageWithCacheMetrics(
554
+ chunkUsage,
555
+ chunk.usage as Partial<ClaudeUsage>,
556
+ );
557
+ }
558
+
559
+ usage = chunkUsage;
560
+ }
561
+
562
+ // Check for finish_reason in the choice
563
+ const choice = chunk.choices?.[0];
564
+ if (choice?.finish_reason) {
565
+ finishReason = choice.finish_reason;
566
+ }
567
+
568
+ const delta = choice?.delta;
569
+ if (!delta) {
570
+ continue;
571
+ }
572
+
573
+ const {
574
+ content,
575
+ tool_calls: toolCallUpdates,
576
+ reasoning_content,
577
+ ...deltaMetadata
578
+ } = delta as unknown as {
579
+ content?: string;
580
+ tool_calls?: ChatCompletionChunk.Choice.Delta.ToolCall[];
581
+ reasoning_content?: string;
582
+ [key: string]: unknown;
583
+ };
584
+
585
+ if (Object.keys(deltaMetadata).length > 0) {
586
+ Object.assign(additionalDeltaFields, deltaMetadata);
587
+ }
588
+
589
+ if (typeof content === "string" && content.length > 0) {
590
+ // Note: OpenAI API already handles UTF-8 character boundaries correctly in streaming,
591
+ // ensuring that delta.content always contains complete UTF-8 strings
592
+ accumulatedContent += content;
593
+ if (onContentUpdate) {
594
+ onContentUpdate(accumulatedContent);
595
+ }
596
+ }
597
+
598
+ if (
599
+ typeof reasoning_content === "string" &&
600
+ reasoning_content.length > 0
601
+ ) {
602
+ accumulatedReasoningContent += reasoning_content;
603
+ if (onReasoningUpdate) {
604
+ onReasoningUpdate(accumulatedReasoningContent);
605
+ }
606
+ }
607
+
608
+ if (Array.isArray(toolCallUpdates)) {
609
+ for (const rawToolCall of toolCallUpdates) {
610
+ const toolCallDelta =
611
+ rawToolCall as ChatCompletionChunk.Choice.Delta.ToolCall;
612
+
613
+ if (!toolCallDelta.function) {
614
+ continue;
615
+ }
616
+
617
+ const functionDelta = toolCallDelta.function;
618
+
619
+ let existingCall;
620
+ let isNew = false;
621
+
622
+ if (toolCallDelta.id) {
623
+ existingCall = toolCalls.find((t) => t.id === toolCallDelta.id);
624
+ if (!existingCall) {
625
+ existingCall = {
626
+ id: toolCallDelta.id,
627
+ type: "function" as const,
628
+ function: {
629
+ name: functionDelta.name || "",
630
+ arguments: "",
631
+ },
632
+ };
633
+ toolCalls.push(existingCall);
634
+ isNew = true;
635
+ }
636
+ } else {
637
+ existingCall = toolCalls[toolCalls.length - 1];
638
+ }
639
+
640
+ if (!existingCall) {
641
+ continue;
642
+ }
643
+
644
+ if (functionDelta.name) {
645
+ existingCall.function.name = functionDelta.name;
646
+ }
647
+
648
+ // Emit start stage when a new tool call is created and we have the tool name
649
+ if (onToolUpdate && isNew && existingCall.function.name) {
650
+ onToolUpdate({
651
+ id: existingCall.id,
652
+ name: existingCall.function.name,
653
+ parameters: "", // Empty parameters for start stage
654
+ parametersChunk: "", // Empty chunk for start stage
655
+ stage: "start", // New tool call triggers start stage
656
+ });
657
+ isNew = false; // Prevent duplicate start emissions
658
+ }
659
+
660
+ if (functionDelta.arguments) {
661
+ existingCall.function.arguments += functionDelta.arguments;
662
+ }
663
+
664
+ // Emit streaming updates for all chunks with actual content (including first chunk)
665
+ if (
666
+ onToolUpdate &&
667
+ existingCall.function.name &&
668
+ functionDelta.arguments &&
669
+ functionDelta.arguments.length > 0 // Only emit streaming for chunks with actual content
670
+ ) {
671
+ onToolUpdate({
672
+ id: existingCall.id,
673
+ name: existingCall.function.name,
674
+ parameters: existingCall.function.arguments,
675
+ parametersChunk: functionDelta.arguments,
676
+ stage: "streaming",
677
+ });
678
+ }
679
+ }
680
+ }
681
+ }
682
+ } catch (error) {
683
+ if ((error as Error).message === "Request was aborted") {
684
+ throw error;
685
+ }
210
686
  throw error;
211
687
  }
688
+
689
+ // Prepare final result
690
+ const result: CallAgentResult = {};
691
+
692
+ if (accumulatedContent) {
693
+ result.content = accumulatedContent.trim();
694
+ }
695
+
696
+ if (accumulatedReasoningContent) {
697
+ result.reasoning_content = accumulatedReasoningContent.trim();
698
+ }
699
+
700
+ if (toolCalls.length > 0) {
701
+ result.tool_calls = toolCalls;
702
+ }
703
+
704
+ if (usage) {
705
+ result.usage = usage;
706
+ }
707
+
708
+ if (finishReason) {
709
+ result.finish_reason = finishReason;
710
+ }
711
+
712
+ if (responseHeaders && Object.keys(responseHeaders).length > 0) {
713
+ result.response_headers = responseHeaders;
714
+ }
715
+
716
+ if (Object.keys(additionalDeltaFields).length > 0) {
717
+ result.additionalFields = {};
718
+ for (const [key, value] of Object.entries(additionalDeltaFields)) {
719
+ if (value !== undefined && key !== "role") {
720
+ result.additionalFields[key] = value;
721
+ }
722
+ }
723
+ if (Object.keys(result.additionalFields).length === 0) {
724
+ delete result.additionalFields;
725
+ }
726
+ }
727
+
728
+ return result;
212
729
  }
213
730
 
214
731
  export interface CompressMessagesOptions {
@@ -239,12 +756,15 @@ export async function compressMessages(
239
756
  const openai = new OpenAI({
240
757
  apiKey: gatewayConfig.apiKey,
241
758
  baseURL: gatewayConfig.baseURL,
759
+ defaultHeaders: gatewayConfig.defaultHeaders,
760
+ fetchOptions: gatewayConfig.fetchOptions,
761
+ fetch: gatewayConfig.fetch,
242
762
  });
243
763
 
244
764
  // Get model configuration - use injected fast model
245
765
  const openaiModelConfig = getModelConfig(modelConfig.fastModel, {
246
766
  temperature: 0.1,
247
- max_tokens: 1500,
767
+ max_tokens: 2048,
248
768
  });
249
769
 
250
770
  try {
@@ -254,54 +774,106 @@ export async function compressMessages(
254
774
  messages: [
255
775
  {
256
776
  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`,
777
+ 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.
778
+ 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.
779
+
780
+ 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:
781
+
782
+ 1. Chronologically analyze each message and section of the conversation. For each section thoroughly identify:
783
+ - The user's explicit requests and intents
784
+ - Your approach to addressing the user's requests
785
+ - Key decisions, technical concepts and code patterns
786
+ - Specific details like:
787
+ - file names
788
+ - full code snippets
789
+ - function signatures
790
+ - file edits
791
+ - Errors that you ran into and how you fixed them
792
+ - Pay special attention to specific user feedback that you received, especially if the user told you to do something differently.
793
+ 2. Double-check for technical accuracy and completeness, addressing each required element thoroughly.
794
+
795
+ Your summary should include the following sections:
796
+
797
+ 1. Primary Request and Intent: Capture all of the user's explicit requests and intents in detail
798
+ 2. Key Technical Concepts: List all important technical concepts, technologies, and frameworks discussed.
799
+ 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.
800
+ 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.
801
+ 5. Problem Solving: Document problems solved and any ongoing troubleshooting efforts.
802
+ 6. All user messages: List ALL user messages that are not tool results. These are critical for understanding the users' feedback and changing intent.
803
+ 6. Pending Tasks: Outline any pending tasks that you have explicitly been asked to work on.
804
+ 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.
805
+ 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.
806
+ 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.
807
+
808
+ Here's an example of how your output should be structured:
809
+
810
+ <example>
811
+ <analysis>
812
+ [Your thought process, ensuring all points are covered thoroughly and accurately]
813
+ </analysis>
814
+
815
+ <summary>
816
+ 1. Primary Request and Intent:
817
+ [Detailed description]
818
+
819
+ 2. Key Technical Concepts:
820
+ - [Concept 1]
821
+ - [Concept 2]
822
+ - [...]
823
+
824
+ 3. Files and Code Sections:
825
+ - [File Name 1]
826
+ - [Summary of why this file is important]
827
+ - [Summary of the changes made to this file, if any]
828
+ - [Important Code Snippet]
829
+ - [File Name 2]
830
+ - [Important Code Snippet]
831
+ - [...]
832
+
833
+ 4. Errors and fixes:
834
+ - [Detailed description of error 1]:
835
+ - [How you fixed the error]
836
+ - [User feedback on the error if any]
837
+ - [...]
838
+
839
+ 5. Problem Solving:
840
+ [Description of solved problems and ongoing troubleshooting]
841
+
842
+ 6. All user messages:
843
+ - [Detailed non tool use user message]
844
+ - [...]
845
+
846
+ 7. Pending Tasks:
847
+ - [Task 1]
848
+ - [Task 2]
849
+ - [...]
850
+
851
+ 8. Current Work:
852
+ [Precise description of current work]
853
+
854
+ 9. Optional Next Step:
855
+ [Optional Next step to take]
856
+
857
+ </summary>
858
+ </example>
859
+
860
+ Please provide your summary based on the conversation so far, following this structure and ensuring precision and thoroughness in your response.
861
+
862
+ 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:
863
+ <example>
864
+ ## Compact Instructions
865
+ When summarizing the conversation focus on typescript code changes and also remember the mistakes you made and how you fixed them.
866
+ </example>
867
+
868
+ <example>
869
+ # Summary instructions
870
+ When you are using compact - please focus on test output and code changes. Include file reads verbatim.
871
+ </example>`,
300
872
  },
301
873
  ...messages,
302
874
  {
303
875
  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.`,
876
+ content: `Please create a detailed summary of the conversation so far.`,
305
877
  },
306
878
  ],
307
879
  },
@@ -329,7 +901,7 @@ For technical conversations, structure as:
329
901
  if ((error as Error).name === "AbortError") {
330
902
  throw new Error("Compression request was aborted");
331
903
  }
332
- // // logger.error("Failed to compress messages:", error);
904
+ logger.error("Failed to compress messages:", error);
333
905
  return {
334
906
  content: "Failed to compress conversation history",
335
907
  usage: undefined,