wave-agent-sdk 0.0.8 → 0.0.11
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/dist/agent.d.ts +92 -23
- package/dist/agent.d.ts.map +1 -1
- package/dist/agent.js +351 -137
- package/dist/index.d.ts +3 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +3 -0
- package/dist/managers/aiManager.d.ts +14 -36
- package/dist/managers/aiManager.d.ts.map +1 -1
- package/dist/managers/aiManager.js +74 -77
- package/dist/managers/backgroundBashManager.d.ts.map +1 -1
- package/dist/managers/backgroundBashManager.js +4 -3
- package/dist/managers/hookManager.d.ts +3 -8
- package/dist/managers/hookManager.d.ts.map +1 -1
- package/dist/managers/hookManager.js +39 -29
- package/dist/managers/liveConfigManager.d.ts +55 -18
- package/dist/managers/liveConfigManager.d.ts.map +1 -1
- package/dist/managers/liveConfigManager.js +372 -90
- package/dist/managers/lspManager.d.ts +43 -0
- package/dist/managers/lspManager.d.ts.map +1 -0
- package/dist/managers/lspManager.js +326 -0
- package/dist/managers/messageManager.d.ts +8 -16
- package/dist/managers/messageManager.d.ts.map +1 -1
- package/dist/managers/messageManager.js +52 -74
- package/dist/managers/permissionManager.d.ts +75 -0
- package/dist/managers/permissionManager.d.ts.map +1 -0
- package/dist/managers/permissionManager.js +368 -0
- package/dist/managers/skillManager.d.ts +1 -0
- package/dist/managers/skillManager.d.ts.map +1 -1
- package/dist/managers/skillManager.js +2 -1
- package/dist/managers/slashCommandManager.d.ts.map +1 -1
- package/dist/managers/slashCommandManager.js +0 -1
- package/dist/managers/subagentManager.d.ts +8 -23
- package/dist/managers/subagentManager.d.ts.map +1 -1
- package/dist/managers/subagentManager.js +97 -117
- package/dist/managers/toolManager.d.ts +38 -1
- package/dist/managers/toolManager.d.ts.map +1 -1
- package/dist/managers/toolManager.js +66 -2
- package/dist/services/aiService.d.ts +3 -1
- package/dist/services/aiService.d.ts.map +1 -1
- package/dist/services/aiService.js +123 -30
- package/dist/services/configurationService.d.ts +116 -0
- package/dist/services/configurationService.d.ts.map +1 -0
- package/dist/services/configurationService.js +585 -0
- package/dist/services/fileWatcher.d.ts.map +1 -1
- package/dist/services/fileWatcher.js +5 -6
- package/dist/services/hook.d.ts +7 -124
- package/dist/services/hook.d.ts.map +1 -1
- package/dist/services/hook.js +46 -458
- package/dist/services/jsonlHandler.d.ts +24 -15
- package/dist/services/jsonlHandler.d.ts.map +1 -1
- package/dist/services/jsonlHandler.js +67 -88
- package/dist/services/memory.d.ts +0 -9
- package/dist/services/memory.d.ts.map +1 -1
- package/dist/services/memory.js +2 -49
- package/dist/services/session.d.ts +82 -33
- package/dist/services/session.d.ts.map +1 -1
- package/dist/services/session.js +275 -181
- package/dist/tools/bashTool.d.ts.map +1 -1
- package/dist/tools/bashTool.js +109 -11
- package/dist/tools/deleteFileTool.d.ts.map +1 -1
- package/dist/tools/deleteFileTool.js +25 -0
- package/dist/tools/editTool.d.ts.map +1 -1
- package/dist/tools/editTool.js +30 -6
- package/dist/tools/lspTool.d.ts +6 -0
- package/dist/tools/lspTool.d.ts.map +1 -0
- package/dist/tools/lspTool.js +589 -0
- package/dist/tools/multiEditTool.d.ts.map +1 -1
- package/dist/tools/multiEditTool.js +26 -7
- package/dist/tools/readTool.d.ts.map +1 -1
- package/dist/tools/readTool.js +111 -2
- package/dist/tools/skillTool.js +2 -2
- package/dist/tools/todoWriteTool.d.ts.map +1 -1
- package/dist/tools/todoWriteTool.js +23 -0
- package/dist/tools/types.d.ts +11 -8
- package/dist/tools/types.d.ts.map +1 -1
- package/dist/tools/writeTool.d.ts.map +1 -1
- package/dist/tools/writeTool.js +25 -9
- package/dist/types/commands.d.ts +0 -1
- package/dist/types/commands.d.ts.map +1 -1
- package/dist/types/config.d.ts +4 -0
- package/dist/types/config.d.ts.map +1 -1
- package/dist/types/configuration.d.ts +69 -0
- package/dist/types/configuration.d.ts.map +1 -0
- package/dist/types/configuration.js +8 -0
- package/dist/types/core.d.ts +10 -0
- package/dist/types/core.d.ts.map +1 -1
- package/dist/types/environment.d.ts +41 -0
- package/dist/types/environment.d.ts.map +1 -1
- package/dist/types/fileSearch.d.ts +5 -0
- package/dist/types/fileSearch.d.ts.map +1 -0
- package/dist/types/fileSearch.js +1 -0
- package/dist/types/hooks.d.ts +11 -2
- package/dist/types/hooks.d.ts.map +1 -1
- package/dist/types/hooks.js +1 -7
- package/dist/types/index.d.ts +5 -0
- package/dist/types/index.d.ts.map +1 -1
- package/dist/types/index.js +5 -0
- package/dist/types/lsp.d.ts +90 -0
- package/dist/types/lsp.d.ts.map +1 -0
- package/dist/types/lsp.js +4 -0
- package/dist/types/messaging.d.ts +6 -11
- package/dist/types/messaging.d.ts.map +1 -1
- package/dist/types/permissions.d.ts +39 -0
- package/dist/types/permissions.d.ts.map +1 -0
- package/dist/types/permissions.js +12 -0
- package/dist/types/session.d.ts +1 -6
- package/dist/types/session.d.ts.map +1 -1
- package/dist/types/skills.d.ts +1 -0
- package/dist/types/skills.d.ts.map +1 -1
- package/dist/types/tools.d.ts +35 -0
- package/dist/types/tools.d.ts.map +1 -0
- package/dist/types/tools.js +4 -0
- package/dist/utils/abortUtils.d.ts +34 -0
- package/dist/utils/abortUtils.d.ts.map +1 -0
- package/dist/utils/abortUtils.js +92 -0
- package/dist/utils/bashHistory.d.ts +4 -0
- package/dist/utils/bashHistory.d.ts.map +1 -1
- package/dist/utils/bashHistory.js +21 -4
- package/dist/utils/bashParser.d.ts +24 -0
- package/dist/utils/bashParser.d.ts.map +1 -0
- package/dist/utils/bashParser.js +413 -0
- package/dist/utils/builtinSubagents.d.ts +7 -0
- package/dist/utils/builtinSubagents.d.ts.map +1 -0
- package/dist/utils/builtinSubagents.js +65 -0
- package/dist/utils/cacheControlUtils.d.ts +8 -33
- package/dist/utils/cacheControlUtils.d.ts.map +1 -1
- package/dist/utils/cacheControlUtils.js +83 -126
- package/dist/utils/constants.d.ts +0 -12
- package/dist/utils/constants.d.ts.map +1 -1
- package/dist/utils/constants.js +1 -13
- package/dist/utils/convertMessagesForAPI.d.ts +2 -1
- package/dist/utils/convertMessagesForAPI.d.ts.map +1 -1
- package/dist/utils/convertMessagesForAPI.js +33 -14
- package/dist/utils/fileSearch.d.ts +14 -0
- package/dist/utils/fileSearch.d.ts.map +1 -0
- package/dist/utils/fileSearch.js +88 -0
- package/dist/utils/fileUtils.d.ts +14 -2
- package/dist/utils/fileUtils.d.ts.map +1 -1
- package/dist/utils/fileUtils.js +101 -17
- package/dist/utils/globalLogger.d.ts +0 -14
- package/dist/utils/globalLogger.d.ts.map +1 -1
- package/dist/utils/globalLogger.js +0 -16
- package/dist/utils/markdownParser.d.ts.map +1 -1
- package/dist/utils/markdownParser.js +1 -17
- package/dist/utils/messageOperations.d.ts +1 -11
- package/dist/utils/messageOperations.d.ts.map +1 -1
- package/dist/utils/messageOperations.js +7 -24
- package/dist/utils/pathEncoder.d.ts +4 -0
- package/dist/utils/pathEncoder.d.ts.map +1 -1
- package/dist/utils/pathEncoder.js +16 -9
- package/dist/utils/pathSafety.d.ts +10 -0
- package/dist/utils/pathSafety.d.ts.map +1 -0
- package/dist/utils/pathSafety.js +23 -0
- package/dist/utils/subagentParser.d.ts +2 -2
- package/dist/utils/subagentParser.d.ts.map +1 -1
- package/dist/utils/subagentParser.js +10 -7
- package/package.json +9 -9
- package/src/agent.ts +475 -216
- package/src/index.ts +3 -0
- package/src/managers/aiManager.ts +107 -111
- package/src/managers/backgroundBashManager.ts +4 -3
- package/src/managers/hookManager.ts +44 -39
- package/src/managers/liveConfigManager.ts +524 -138
- package/src/managers/lspManager.ts +434 -0
- package/src/managers/messageManager.ts +73 -103
- package/src/managers/permissionManager.ts +480 -0
- package/src/managers/skillManager.ts +3 -1
- package/src/managers/slashCommandManager.ts +1 -2
- package/src/managers/subagentManager.ts +116 -159
- package/src/managers/toolManager.ts +95 -3
- package/src/services/aiService.ts +207 -26
- package/src/services/configurationService.ts +762 -0
- package/src/services/fileWatcher.ts +5 -6
- package/src/services/hook.ts +50 -631
- package/src/services/jsonlHandler.ts +84 -100
- package/src/services/memory.ts +2 -59
- package/src/services/session.ts +338 -213
- package/src/tools/bashTool.ts +126 -13
- package/src/tools/deleteFileTool.ts +36 -0
- package/src/tools/editTool.ts +41 -7
- package/src/tools/lspTool.ts +760 -0
- package/src/tools/multiEditTool.ts +37 -8
- package/src/tools/readTool.ts +125 -2
- package/src/tools/skillTool.ts +2 -2
- package/src/tools/todoWriteTool.ts +33 -1
- package/src/tools/types.ts +15 -9
- package/src/tools/writeTool.ts +36 -10
- package/src/types/commands.ts +0 -1
- package/src/types/config.ts +5 -0
- package/src/types/configuration.ts +73 -0
- package/src/types/core.ts +11 -0
- package/src/types/environment.ts +44 -0
- package/src/types/fileSearch.ts +4 -0
- package/src/types/hooks.ts +14 -11
- package/src/types/index.ts +5 -0
- package/src/types/lsp.ts +96 -0
- package/src/types/messaging.ts +8 -13
- package/src/types/permissions.ts +52 -0
- package/src/types/session.ts +3 -8
- package/src/types/skills.ts +1 -0
- package/src/types/tools.ts +38 -0
- package/src/utils/abortUtils.ts +118 -0
- package/src/utils/bashHistory.ts +28 -4
- package/src/utils/bashParser.ts +444 -0
- package/src/utils/builtinSubagents.ts +71 -0
- package/src/utils/cacheControlUtils.ts +106 -171
- package/src/utils/constants.ts +1 -16
- package/src/utils/convertMessagesForAPI.ts +38 -14
- package/src/utils/fileSearch.ts +107 -0
- package/src/utils/fileUtils.ts +114 -19
- package/src/utils/globalLogger.ts +0 -17
- package/src/utils/markdownParser.ts +1 -19
- package/src/utils/messageOperations.ts +7 -35
- package/src/utils/pathEncoder.ts +24 -9
- package/src/utils/pathSafety.ts +26 -0
- package/src/utils/subagentParser.ts +11 -8
- package/dist/constants/events.d.ts +0 -28
- package/dist/constants/events.d.ts.map +0 -1
- package/dist/constants/events.js +0 -27
- package/dist/services/configurationWatcher.d.ts +0 -120
- package/dist/services/configurationWatcher.d.ts.map +0 -1
- package/dist/services/configurationWatcher.js +0 -439
- package/dist/services/memoryStore.d.ts +0 -81
- package/dist/services/memoryStore.d.ts.map +0 -1
- package/dist/services/memoryStore.js +0 -200
- package/dist/types/memoryStore.d.ts +0 -82
- package/dist/types/memoryStore.d.ts.map +0 -1
- package/dist/types/memoryStore.js +0 -7
- package/dist/utils/configResolver.d.ts +0 -65
- package/dist/utils/configResolver.d.ts.map +0 -1
- package/dist/utils/configResolver.js +0 -210
- package/src/constants/events.ts +0 -38
- package/src/services/configurationWatcher.ts +0 -622
- package/src/services/memoryStore.ts +0 -279
- package/src/types/memoryStore.ts +0 -94
- package/src/utils/configResolver.ts +0 -302
|
@@ -21,6 +21,41 @@ import * as os from "os";
|
|
|
21
21
|
import * as fs from "fs";
|
|
22
22
|
import * as path from "path";
|
|
23
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
|
+
|
|
24
59
|
/**
|
|
25
60
|
* Use parametersChunk as compact param for better performance
|
|
26
61
|
* Instead of parsing JSON, we use the raw chunk for efficient streaming
|
|
@@ -105,11 +140,13 @@ export interface CallAgentOptions {
|
|
|
105
140
|
parametersChunk?: string;
|
|
106
141
|
stage?: "start" | "streaming" | "running" | "end";
|
|
107
142
|
}) => void;
|
|
143
|
+
onReasoningUpdate?: (content: string) => void;
|
|
108
144
|
}
|
|
109
145
|
|
|
110
146
|
export interface CallAgentResult {
|
|
111
147
|
content?: string;
|
|
112
148
|
tool_calls?: ChatCompletionMessageToolCall[];
|
|
149
|
+
reasoning_content?: string;
|
|
113
150
|
usage?: ClaudeUsage;
|
|
114
151
|
finish_reason?:
|
|
115
152
|
| "stop"
|
|
@@ -119,7 +156,7 @@ export interface CallAgentResult {
|
|
|
119
156
|
| "function_call"
|
|
120
157
|
| null;
|
|
121
158
|
response_headers?: Record<string, string>;
|
|
122
|
-
|
|
159
|
+
additionalFields?: Record<string, unknown>;
|
|
123
160
|
}
|
|
124
161
|
|
|
125
162
|
export async function callAgent(
|
|
@@ -137,13 +174,27 @@ export async function callAgent(
|
|
|
137
174
|
systemPrompt,
|
|
138
175
|
onContentUpdate,
|
|
139
176
|
onToolUpdate,
|
|
177
|
+
onReasoningUpdate,
|
|
140
178
|
} = options;
|
|
141
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
|
+
|
|
142
190
|
try {
|
|
143
191
|
// Create OpenAI client with injected configuration
|
|
144
192
|
const openai = new OpenAI({
|
|
145
193
|
apiKey: gatewayConfig.apiKey,
|
|
146
194
|
baseURL: gatewayConfig.baseURL,
|
|
195
|
+
defaultHeaders: gatewayConfig.defaultHeaders,
|
|
196
|
+
fetchOptions: gatewayConfig.fetchOptions,
|
|
197
|
+
fetch: gatewayConfig.fetch,
|
|
147
198
|
});
|
|
148
199
|
|
|
149
200
|
// Build system prompt content
|
|
@@ -183,19 +234,23 @@ Today's date: ${new Date().toISOString().split("T")[0]}
|
|
|
183
234
|
};
|
|
184
235
|
|
|
185
236
|
// ChatCompletionMessageParam[] is already in OpenAI format, add system prompt to the beginning
|
|
186
|
-
|
|
187
|
-
systemMessage,
|
|
188
|
-
...messages,
|
|
189
|
-
];
|
|
237
|
+
openaiMessages = [systemMessage, ...messages];
|
|
190
238
|
|
|
191
239
|
// Apply cache control for Claude models
|
|
192
240
|
const currentModel = model || modelConfig.agentModel;
|
|
193
241
|
|
|
242
|
+
processedTools = tools;
|
|
243
|
+
|
|
194
244
|
if (isClaudeModel(currentModel)) {
|
|
195
245
|
openaiMessages = transformMessagesForClaudeCache(
|
|
196
246
|
openaiMessages,
|
|
197
247
|
currentModel,
|
|
198
248
|
);
|
|
249
|
+
|
|
250
|
+
// Apply cache control to tools separately
|
|
251
|
+
if (tools && tools.length > 0) {
|
|
252
|
+
processedTools = addCacheControlToLastTool(tools);
|
|
253
|
+
}
|
|
199
254
|
}
|
|
200
255
|
|
|
201
256
|
// Get model configuration - use injected modelConfig with optional override
|
|
@@ -204,10 +259,14 @@ Today's date: ${new Date().toISOString().split("T")[0]}
|
|
|
204
259
|
});
|
|
205
260
|
|
|
206
261
|
// Determine if streaming is needed
|
|
207
|
-
const isStreaming = !!(
|
|
262
|
+
const isStreaming = !!(
|
|
263
|
+
onContentUpdate ||
|
|
264
|
+
onToolUpdate ||
|
|
265
|
+
onReasoningUpdate
|
|
266
|
+
);
|
|
208
267
|
|
|
209
268
|
// Prepare API call parameters
|
|
210
|
-
|
|
269
|
+
createParams = {
|
|
211
270
|
...openaiModelConfig,
|
|
212
271
|
messages: openaiMessages,
|
|
213
272
|
stream: isStreaming,
|
|
@@ -216,13 +275,8 @@ Today's date: ${new Date().toISOString().split("T")[0]}
|
|
|
216
275
|
| ChatCompletionCreateParamsStreaming;
|
|
217
276
|
|
|
218
277
|
// Only add tools if they exist
|
|
219
|
-
if (
|
|
220
|
-
|
|
221
|
-
if (isClaudeModel(currentModel)) {
|
|
222
|
-
createParams.tools = addCacheControlToLastTool(tools);
|
|
223
|
-
} else {
|
|
224
|
-
createParams.tools = tools;
|
|
225
|
-
}
|
|
278
|
+
if (processedTools && processedTools.length > 0) {
|
|
279
|
+
createParams.tools = processedTools;
|
|
226
280
|
}
|
|
227
281
|
|
|
228
282
|
if (isStreaming) {
|
|
@@ -243,6 +297,7 @@ Today's date: ${new Date().toISOString().split("T")[0]}
|
|
|
243
297
|
stream,
|
|
244
298
|
onContentUpdate,
|
|
245
299
|
onToolUpdate,
|
|
300
|
+
onReasoningUpdate,
|
|
246
301
|
abortSignal,
|
|
247
302
|
responseHeaders,
|
|
248
303
|
currentModel,
|
|
@@ -287,26 +342,39 @@ Today's date: ${new Date().toISOString().split("T")[0]}
|
|
|
287
342
|
const {
|
|
288
343
|
content: finalContent,
|
|
289
344
|
tool_calls: finalToolCalls,
|
|
345
|
+
reasoning_content: finalReasoningContent,
|
|
290
346
|
...otherFields
|
|
291
|
-
} = finalMessage
|
|
347
|
+
} = finalMessage as unknown as {
|
|
348
|
+
content?: string;
|
|
349
|
+
tool_calls?: ChatCompletionMessageToolCall[];
|
|
350
|
+
reasoning_content?: string;
|
|
351
|
+
[key: string]: unknown;
|
|
352
|
+
};
|
|
292
353
|
|
|
293
354
|
if (typeof finalContent === "string" && finalContent.length > 0) {
|
|
294
355
|
result.content = finalContent;
|
|
295
356
|
}
|
|
296
357
|
|
|
358
|
+
if (
|
|
359
|
+
typeof finalReasoningContent === "string" &&
|
|
360
|
+
finalReasoningContent.length > 0
|
|
361
|
+
) {
|
|
362
|
+
result.reasoning_content = finalReasoningContent;
|
|
363
|
+
}
|
|
364
|
+
|
|
297
365
|
if (Array.isArray(finalToolCalls) && finalToolCalls.length > 0) {
|
|
298
366
|
result.tool_calls = finalToolCalls;
|
|
299
367
|
}
|
|
300
368
|
|
|
301
369
|
if (Object.keys(otherFields).length > 0) {
|
|
302
|
-
const
|
|
370
|
+
const additionalFields: Record<string, unknown> = {};
|
|
303
371
|
for (const [key, value] of Object.entries(otherFields)) {
|
|
304
|
-
if (value !== undefined) {
|
|
305
|
-
|
|
372
|
+
if (value !== undefined && key !== "role") {
|
|
373
|
+
additionalFields[key] = value;
|
|
306
374
|
}
|
|
307
375
|
}
|
|
308
|
-
if (Object.keys(
|
|
309
|
-
result.
|
|
376
|
+
if (Object.keys(additionalFields).length > 0) {
|
|
377
|
+
result.additionalFields = additionalFields;
|
|
310
378
|
}
|
|
311
379
|
}
|
|
312
380
|
}
|
|
@@ -329,6 +397,98 @@ Today's date: ${new Date().toISOString().split("T")[0]}
|
|
|
329
397
|
if ((error as Error).name === "AbortError") {
|
|
330
398
|
throw new Error("Request was aborted");
|
|
331
399
|
}
|
|
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
|
+
|
|
332
492
|
logger.error("Failed to call OpenAI:", error);
|
|
333
493
|
throw error;
|
|
334
494
|
}
|
|
@@ -354,11 +514,13 @@ async function processStreamingResponse(
|
|
|
354
514
|
parametersChunk?: string;
|
|
355
515
|
stage?: "start" | "streaming" | "running" | "end";
|
|
356
516
|
}) => void,
|
|
517
|
+
onReasoningUpdate?: (content: string) => void,
|
|
357
518
|
abortSignal?: AbortSignal,
|
|
358
519
|
responseHeaders?: Record<string, string>,
|
|
359
520
|
modelName?: string,
|
|
360
521
|
): Promise<CallAgentResult> {
|
|
361
522
|
let accumulatedContent = "";
|
|
523
|
+
let accumulatedReasoningContent = "";
|
|
362
524
|
const toolCalls: {
|
|
363
525
|
id: string;
|
|
364
526
|
type: "function";
|
|
@@ -411,10 +573,12 @@ async function processStreamingResponse(
|
|
|
411
573
|
const {
|
|
412
574
|
content,
|
|
413
575
|
tool_calls: toolCallUpdates,
|
|
576
|
+
reasoning_content,
|
|
414
577
|
...deltaMetadata
|
|
415
578
|
} = delta as unknown as {
|
|
416
579
|
content?: string;
|
|
417
580
|
tool_calls?: ChatCompletionChunk.Choice.Delta.ToolCall[];
|
|
581
|
+
reasoning_content?: string;
|
|
418
582
|
[key: string]: unknown;
|
|
419
583
|
};
|
|
420
584
|
|
|
@@ -431,6 +595,16 @@ async function processStreamingResponse(
|
|
|
431
595
|
}
|
|
432
596
|
}
|
|
433
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
|
+
|
|
434
608
|
if (Array.isArray(toolCallUpdates)) {
|
|
435
609
|
for (const rawToolCall of toolCallUpdates) {
|
|
436
610
|
const toolCallDelta =
|
|
@@ -516,7 +690,11 @@ async function processStreamingResponse(
|
|
|
516
690
|
const result: CallAgentResult = {};
|
|
517
691
|
|
|
518
692
|
if (accumulatedContent) {
|
|
519
|
-
result.content = accumulatedContent;
|
|
693
|
+
result.content = accumulatedContent.trim();
|
|
694
|
+
}
|
|
695
|
+
|
|
696
|
+
if (accumulatedReasoningContent) {
|
|
697
|
+
result.reasoning_content = accumulatedReasoningContent.trim();
|
|
520
698
|
}
|
|
521
699
|
|
|
522
700
|
if (toolCalls.length > 0) {
|
|
@@ -536,14 +714,14 @@ async function processStreamingResponse(
|
|
|
536
714
|
}
|
|
537
715
|
|
|
538
716
|
if (Object.keys(additionalDeltaFields).length > 0) {
|
|
539
|
-
result.
|
|
717
|
+
result.additionalFields = {};
|
|
540
718
|
for (const [key, value] of Object.entries(additionalDeltaFields)) {
|
|
541
|
-
if (value !== undefined) {
|
|
542
|
-
result.
|
|
719
|
+
if (value !== undefined && key !== "role") {
|
|
720
|
+
result.additionalFields[key] = value;
|
|
543
721
|
}
|
|
544
722
|
}
|
|
545
|
-
if (Object.keys(result.
|
|
546
|
-
delete result.
|
|
723
|
+
if (Object.keys(result.additionalFields).length === 0) {
|
|
724
|
+
delete result.additionalFields;
|
|
547
725
|
}
|
|
548
726
|
}
|
|
549
727
|
|
|
@@ -578,6 +756,9 @@ export async function compressMessages(
|
|
|
578
756
|
const openai = new OpenAI({
|
|
579
757
|
apiKey: gatewayConfig.apiKey,
|
|
580
758
|
baseURL: gatewayConfig.baseURL,
|
|
759
|
+
defaultHeaders: gatewayConfig.defaultHeaders,
|
|
760
|
+
fetchOptions: gatewayConfig.fetchOptions,
|
|
761
|
+
fetch: gatewayConfig.fetch,
|
|
581
762
|
});
|
|
582
763
|
|
|
583
764
|
// Get model configuration - use injected fast model
|