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.
- package/dist/agent.d.ts +105 -24
- package/dist/agent.d.ts.map +1 -1
- package/dist/agent.js +438 -53
- package/dist/index.d.ts +4 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +4 -0
- package/dist/managers/aiManager.d.ts +18 -7
- package/dist/managers/aiManager.d.ts.map +1 -1
- package/dist/managers/aiManager.js +254 -142
- package/dist/managers/backgroundBashManager.d.ts.map +1 -1
- package/dist/managers/backgroundBashManager.js +11 -9
- package/dist/managers/hookManager.d.ts +6 -6
- package/dist/managers/hookManager.d.ts.map +1 -1
- package/dist/managers/hookManager.js +81 -39
- package/dist/managers/liveConfigManager.d.ts +95 -0
- package/dist/managers/liveConfigManager.d.ts.map +1 -0
- package/dist/managers/liveConfigManager.js +442 -0
- 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 +41 -24
- package/dist/managers/messageManager.d.ts.map +1 -1
- package/dist/managers/messageManager.js +184 -73
- package/dist/managers/permissionManager.d.ts +66 -0
- package/dist/managers/permissionManager.d.ts.map +1 -0
- package/dist/managers/permissionManager.js +208 -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 +4 -2
- package/dist/managers/subagentManager.d.ts +42 -6
- package/dist/managers/subagentManager.d.ts.map +1 -1
- package/dist/managers/subagentManager.js +213 -62
- 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 +15 -5
- package/dist/services/aiService.d.ts.map +1 -1
- package/dist/services/aiService.js +446 -77
- 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 +69 -0
- package/dist/services/fileWatcher.d.ts.map +1 -0
- package/dist/services/fileWatcher.js +212 -0
- package/dist/services/hook.d.ts +5 -40
- package/dist/services/hook.d.ts.map +1 -1
- package/dist/services/hook.js +47 -109
- package/dist/services/jsonlHandler.d.ts +71 -0
- package/dist/services/jsonlHandler.d.ts.map +1 -0
- package/dist/services/jsonlHandler.js +236 -0
- package/dist/services/memory.d.ts.map +1 -1
- package/dist/services/memory.js +33 -11
- package/dist/services/session.d.ts +116 -52
- package/dist/services/session.d.ts.map +1 -1
- package/dist/services/session.js +415 -143
- package/dist/tools/bashTool.d.ts.map +1 -1
- package/dist/tools/bashTool.js +77 -17
- package/dist/tools/deleteFileTool.d.ts.map +1 -1
- package/dist/tools/deleteFileTool.js +27 -1
- package/dist/tools/editTool.d.ts.map +1 -1
- package/dist/tools/editTool.js +33 -8
- 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 +30 -10
- package/dist/tools/readTool.d.ts.map +1 -1
- package/dist/tools/readTool.js +113 -3
- 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 +30 -15
- package/dist/types/commands.d.ts +4 -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 +45 -0
- package/dist/types/core.d.ts.map +1 -1
- package/dist/types/environment.d.ts +83 -0
- package/dist/types/environment.d.ts.map +1 -0
- package/dist/types/environment.js +21 -0
- 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 +18 -3
- package/dist/types/hooks.d.ts.map +1 -1
- package/dist/types/hooks.js +8 -8
- package/dist/types/index.d.ts +7 -0
- package/dist/types/index.d.ts.map +1 -1
- package/dist/types/index.js +7 -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 +19 -12
- package/dist/types/messaging.d.ts.map +1 -1
- package/dist/types/permissions.d.ts +35 -0
- package/dist/types/permissions.d.ts.map +1 -0
- package/dist/types/permissions.js +12 -0
- package/dist/types/session.d.ts +15 -0
- package/dist/types/session.d.ts.map +1 -0
- package/dist/types/session.js +7 -0
- 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 +48 -30
- 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 +96 -0
- package/dist/utils/cacheControlUtils.d.ts.map +1 -0
- package/dist/utils/cacheControlUtils.js +324 -0
- package/dist/utils/commandPathResolver.d.ts +52 -0
- package/dist/utils/commandPathResolver.d.ts.map +1 -0
- package/dist/utils/commandPathResolver.js +145 -0
- package/dist/utils/configPaths.d.ts +85 -0
- package/dist/utils/configPaths.d.ts.map +1 -0
- package/dist/utils/configPaths.js +121 -0
- package/dist/utils/constants.d.ts +1 -13
- package/dist/utils/constants.d.ts.map +1 -1
- package/dist/utils/constants.js +2 -14
- package/dist/utils/convertMessagesForAPI.d.ts +2 -1
- package/dist/utils/convertMessagesForAPI.d.ts.map +1 -1
- package/dist/utils/convertMessagesForAPI.js +39 -18
- package/dist/utils/customCommands.d.ts.map +1 -1
- package/dist/utils/customCommands.js +66 -21
- 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 +27 -0
- package/dist/utils/fileUtils.d.ts.map +1 -0
- package/dist/utils/fileUtils.js +145 -0
- package/dist/utils/globalLogger.d.ts +88 -0
- package/dist/utils/globalLogger.d.ts.map +1 -0
- package/dist/utils/globalLogger.js +120 -0
- package/dist/utils/largeOutputHandler.d.ts +15 -0
- package/dist/utils/largeOutputHandler.d.ts.map +1 -0
- package/dist/utils/largeOutputHandler.js +40 -0
- package/dist/utils/markdownParser.d.ts.map +1 -1
- package/dist/utils/markdownParser.js +1 -17
- package/dist/utils/mcpUtils.d.ts.map +1 -1
- package/dist/utils/mcpUtils.js +25 -3
- package/dist/utils/messageOperations.d.ts +20 -18
- package/dist/utils/messageOperations.d.ts.map +1 -1
- package/dist/utils/messageOperations.js +30 -38
- package/dist/utils/pathEncoder.d.ts +108 -0
- package/dist/utils/pathEncoder.d.ts.map +1 -0
- package/dist/utils/pathEncoder.js +279 -0
- package/dist/utils/subagentParser.d.ts +2 -2
- package/dist/utils/subagentParser.d.ts.map +1 -1
- package/dist/utils/subagentParser.js +12 -8
- package/dist/utils/tokenCalculation.d.ts +26 -0
- package/dist/utils/tokenCalculation.d.ts.map +1 -0
- package/dist/utils/tokenCalculation.js +36 -0
- package/dist/utils/tokenEstimator.d.ts +39 -0
- package/dist/utils/tokenEstimator.d.ts.map +1 -0
- package/dist/utils/tokenEstimator.js +55 -0
- package/package.json +6 -6
- package/src/agent.ts +586 -78
- package/src/index.ts +4 -0
- package/src/managers/aiManager.ts +341 -192
- package/src/managers/backgroundBashManager.ts +11 -9
- package/src/managers/hookManager.ts +102 -54
- package/src/managers/liveConfigManager.ts +634 -0
- package/src/managers/lspManager.ts +434 -0
- package/src/managers/messageManager.ts +258 -121
- package/src/managers/permissionManager.ts +276 -0
- package/src/managers/skillManager.ts +3 -1
- package/src/managers/slashCommandManager.ts +5 -3
- package/src/managers/subagentManager.ts +295 -76
- package/src/managers/toolManager.ts +95 -3
- package/src/services/aiService.ts +656 -84
- package/src/services/configurationService.ts +762 -0
- package/src/services/fileWatcher.ts +300 -0
- package/src/services/hook.ts +54 -144
- package/src/services/jsonlHandler.ts +303 -0
- package/src/services/memory.ts +34 -11
- package/src/services/session.ts +522 -173
- package/src/tools/bashTool.ts +94 -20
- package/src/tools/deleteFileTool.ts +38 -1
- package/src/tools/editTool.ts +44 -9
- package/src/tools/lspTool.ts +760 -0
- package/src/tools/multiEditTool.ts +41 -11
- package/src/tools/readTool.ts +127 -3
- 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 +43 -16
- package/src/types/commands.ts +6 -1
- package/src/types/config.ts +5 -0
- package/src/types/configuration.ts +73 -0
- package/src/types/core.ts +55 -0
- package/src/types/environment.ts +104 -0
- package/src/types/fileSearch.ts +4 -0
- package/src/types/hooks.ts +32 -16
- package/src/types/index.ts +7 -0
- package/src/types/lsp.ts +96 -0
- package/src/types/messaging.ts +21 -14
- package/src/types/permissions.ts +48 -0
- package/src/types/session.ts +20 -0
- 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 +55 -31
- package/src/utils/builtinSubagents.ts +71 -0
- package/src/utils/cacheControlUtils.ts +475 -0
- package/src/utils/commandPathResolver.ts +189 -0
- package/src/utils/configPaths.ts +163 -0
- package/src/utils/constants.ts +2 -17
- package/src/utils/convertMessagesForAPI.ts +44 -18
- package/src/utils/customCommands.ts +90 -22
- package/src/utils/fileSearch.ts +107 -0
- package/src/utils/fileUtils.ts +160 -0
- package/src/utils/globalLogger.ts +128 -0
- package/src/utils/largeOutputHandler.ts +55 -0
- package/src/utils/markdownParser.ts +1 -19
- package/src/utils/mcpUtils.ts +34 -3
- package/src/utils/messageOperations.ts +47 -53
- package/src/utils/pathEncoder.ts +394 -0
- package/src/utils/subagentParser.ts +13 -9
- package/src/utils/tokenCalculation.ts +43 -0
- package/src/utils/tokenEstimator.ts +68 -0
- package/dist/utils/configResolver.d.ts +0 -38
- package/dist/utils/configResolver.d.ts.map +0 -1
- package/dist/utils/configResolver.js +0 -106
- package/src/utils/configResolver.ts +0 -142
|
@@ -1,7 +1,13 @@
|
|
|
1
1
|
import OpenAI from "openai";
|
|
2
|
+
import { logger } from "../utils/globalLogger.js";
|
|
3
|
+
import { transformMessagesForClaudeCache, addCacheControlToLastTool, isClaudeModel, extendUsageWithCacheMetrics, } from "../utils/cacheControlUtils.js";
|
|
2
4
|
import * as os from "os";
|
|
3
5
|
import * as fs from "fs";
|
|
4
6
|
import * as path from "path";
|
|
7
|
+
/**
|
|
8
|
+
* Use parametersChunk as compact param for better performance
|
|
9
|
+
* Instead of parsing JSON, we use the raw chunk for efficient streaming
|
|
10
|
+
*/
|
|
5
11
|
/**
|
|
6
12
|
* Check if a directory is a git repository
|
|
7
13
|
* @param dirPath Directory path to check
|
|
@@ -44,12 +50,19 @@ function getModelConfig(modelName, baseConfig = {}) {
|
|
|
44
50
|
return config;
|
|
45
51
|
}
|
|
46
52
|
export async function callAgent(options) {
|
|
47
|
-
const { gatewayConfig, modelConfig, messages, abortSignal, memory, workdir, tools, model, systemPrompt, } = options;
|
|
53
|
+
const { gatewayConfig, modelConfig, messages, abortSignal, memory, workdir, tools, model, systemPrompt, onContentUpdate, onToolUpdate, onReasoningUpdate, } = options;
|
|
54
|
+
// Declare variables outside try block for error handling access
|
|
55
|
+
let openaiMessages;
|
|
56
|
+
let createParams;
|
|
57
|
+
let processedTools;
|
|
48
58
|
try {
|
|
49
59
|
// Create OpenAI client with injected configuration
|
|
50
60
|
const openai = new OpenAI({
|
|
51
61
|
apiKey: gatewayConfig.apiKey,
|
|
52
62
|
baseURL: gatewayConfig.baseURL,
|
|
63
|
+
defaultHeaders: gatewayConfig.defaultHeaders,
|
|
64
|
+
fetchOptions: gatewayConfig.fetchOptions,
|
|
65
|
+
fetch: gatewayConfig.fetch,
|
|
53
66
|
});
|
|
54
67
|
// Build system prompt content
|
|
55
68
|
let systemContent = systemPrompt ||
|
|
@@ -83,55 +96,356 @@ Today's date: ${new Date().toISOString().split("T")[0]}
|
|
|
83
96
|
content: systemContent,
|
|
84
97
|
};
|
|
85
98
|
// ChatCompletionMessageParam[] is already in OpenAI format, add system prompt to the beginning
|
|
86
|
-
|
|
99
|
+
openaiMessages = [systemMessage, ...messages];
|
|
100
|
+
// Apply cache control for Claude models
|
|
101
|
+
const currentModel = model || modelConfig.agentModel;
|
|
102
|
+
processedTools = tools;
|
|
103
|
+
if (isClaudeModel(currentModel)) {
|
|
104
|
+
openaiMessages = transformMessagesForClaudeCache(openaiMessages, currentModel);
|
|
105
|
+
// Apply cache control to tools separately
|
|
106
|
+
if (tools && tools.length > 0) {
|
|
107
|
+
processedTools = addCacheControlToLastTool(tools);
|
|
108
|
+
}
|
|
109
|
+
}
|
|
87
110
|
// Get model configuration - use injected modelConfig with optional override
|
|
88
111
|
const openaiModelConfig = getModelConfig(model || modelConfig.agentModel, {
|
|
89
112
|
temperature: 0,
|
|
90
|
-
max_completion_tokens: 32768,
|
|
91
113
|
});
|
|
114
|
+
// Determine if streaming is needed
|
|
115
|
+
const isStreaming = !!(onContentUpdate ||
|
|
116
|
+
onToolUpdate ||
|
|
117
|
+
onReasoningUpdate);
|
|
92
118
|
// Prepare API call parameters
|
|
93
|
-
|
|
119
|
+
createParams = {
|
|
94
120
|
...openaiModelConfig,
|
|
95
121
|
messages: openaiMessages,
|
|
122
|
+
stream: isStreaming,
|
|
96
123
|
};
|
|
97
124
|
// Only add tools if they exist
|
|
98
|
-
if (
|
|
99
|
-
createParams.tools =
|
|
125
|
+
if (processedTools && processedTools.length > 0) {
|
|
126
|
+
createParams.tools = processedTools;
|
|
100
127
|
}
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
// Return content
|
|
115
|
-
if (finalMessage?.content) {
|
|
116
|
-
result.content = finalMessage.content;
|
|
117
|
-
}
|
|
118
|
-
// Return tool call
|
|
119
|
-
if (finalMessage?.tool_calls && finalMessage.tool_calls.length > 0) {
|
|
120
|
-
result.tool_calls = finalMessage.tool_calls;
|
|
128
|
+
if (isStreaming) {
|
|
129
|
+
// Handle streaming response
|
|
130
|
+
const { data: stream, response } = await openai.chat.completions
|
|
131
|
+
.create(createParams, {
|
|
132
|
+
signal: abortSignal,
|
|
133
|
+
})
|
|
134
|
+
.withResponse();
|
|
135
|
+
// Extract response headers
|
|
136
|
+
const responseHeaders = {};
|
|
137
|
+
response.headers.forEach((value, key) => {
|
|
138
|
+
responseHeaders[key] = value;
|
|
139
|
+
});
|
|
140
|
+
return await processStreamingResponse(stream, onContentUpdate, onToolUpdate, onReasoningUpdate, abortSignal, responseHeaders, currentModel);
|
|
121
141
|
}
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
142
|
+
else {
|
|
143
|
+
// Handle non-streaming response
|
|
144
|
+
const { data: response, response: rawResponse } = await openai.chat.completions
|
|
145
|
+
.create(createParams, {
|
|
146
|
+
signal: abortSignal,
|
|
147
|
+
})
|
|
148
|
+
.withResponse();
|
|
149
|
+
// Extract response headers
|
|
150
|
+
const responseHeaders = {};
|
|
151
|
+
rawResponse.headers.forEach((value, key) => {
|
|
152
|
+
responseHeaders[key] = value;
|
|
153
|
+
});
|
|
154
|
+
const finalMessage = response.choices[0]?.message;
|
|
155
|
+
const finishReason = response.choices[0]?.finish_reason || null;
|
|
156
|
+
let totalUsage = response.usage
|
|
157
|
+
? {
|
|
158
|
+
prompt_tokens: response.usage.prompt_tokens,
|
|
159
|
+
completion_tokens: response.usage.completion_tokens,
|
|
160
|
+
total_tokens: response.usage.total_tokens,
|
|
161
|
+
}
|
|
162
|
+
: undefined;
|
|
163
|
+
// Extend usage with cache metrics for Claude models
|
|
164
|
+
if (totalUsage && isClaudeModel(currentModel) && response.usage) {
|
|
165
|
+
totalUsage = extendUsageWithCacheMetrics(totalUsage, response.usage);
|
|
166
|
+
}
|
|
167
|
+
const result = {};
|
|
168
|
+
if (finalMessage) {
|
|
169
|
+
const { content: finalContent, tool_calls: finalToolCalls, reasoning_content: finalReasoningContent, ...otherFields } = finalMessage;
|
|
170
|
+
if (typeof finalContent === "string" && finalContent.length > 0) {
|
|
171
|
+
result.content = finalContent;
|
|
172
|
+
}
|
|
173
|
+
if (typeof finalReasoningContent === "string" &&
|
|
174
|
+
finalReasoningContent.length > 0) {
|
|
175
|
+
result.reasoning_content = finalReasoningContent;
|
|
176
|
+
}
|
|
177
|
+
if (Array.isArray(finalToolCalls) && finalToolCalls.length > 0) {
|
|
178
|
+
result.tool_calls = finalToolCalls;
|
|
179
|
+
}
|
|
180
|
+
if (Object.keys(otherFields).length > 0) {
|
|
181
|
+
const additionalFields = {};
|
|
182
|
+
for (const [key, value] of Object.entries(otherFields)) {
|
|
183
|
+
if (value !== undefined && key !== "role") {
|
|
184
|
+
additionalFields[key] = value;
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
if (Object.keys(additionalFields).length > 0) {
|
|
188
|
+
result.additionalFields = additionalFields;
|
|
189
|
+
}
|
|
190
|
+
}
|
|
191
|
+
}
|
|
192
|
+
if (totalUsage) {
|
|
193
|
+
result.usage = totalUsage;
|
|
194
|
+
}
|
|
195
|
+
if (finishReason) {
|
|
196
|
+
result.finish_reason = finishReason;
|
|
197
|
+
}
|
|
198
|
+
if (Object.keys(responseHeaders).length > 0) {
|
|
199
|
+
result.response_headers = responseHeaders;
|
|
200
|
+
}
|
|
201
|
+
return result;
|
|
125
202
|
}
|
|
126
|
-
return result;
|
|
127
203
|
}
|
|
128
204
|
catch (error) {
|
|
129
205
|
if (error.name === "AbortError") {
|
|
130
206
|
throw new Error("Request was aborted");
|
|
131
207
|
}
|
|
132
|
-
//
|
|
208
|
+
// Check if it's a 400 error and save messages to temp directory
|
|
209
|
+
if (error &&
|
|
210
|
+
typeof error === "object" &&
|
|
211
|
+
"status" in error &&
|
|
212
|
+
error.status === 400) {
|
|
213
|
+
try {
|
|
214
|
+
// Create temp directory for error debugging
|
|
215
|
+
const tempDir = fs.mkdtempSync(path.join(os.tmpdir(), "callAgent-400-error-"));
|
|
216
|
+
const messagesFile = path.join(tempDir, "messages.json");
|
|
217
|
+
const errorFile = path.join(tempDir, "error.json");
|
|
218
|
+
// Save complete messages to temp file
|
|
219
|
+
const debugData = {
|
|
220
|
+
originalMessages: messages,
|
|
221
|
+
timestamp: new Date().toISOString(),
|
|
222
|
+
model: model || modelConfig.agentModel,
|
|
223
|
+
workdir,
|
|
224
|
+
sessionId: options.sessionId,
|
|
225
|
+
gatewayConfig: {
|
|
226
|
+
baseURL: gatewayConfig.baseURL,
|
|
227
|
+
// Don't include apiKey for security
|
|
228
|
+
defaultHeaders: gatewayConfig.defaultHeaders,
|
|
229
|
+
},
|
|
230
|
+
};
|
|
231
|
+
// Add processed messages if they exist
|
|
232
|
+
if (typeof openaiMessages !== "undefined") {
|
|
233
|
+
debugData.processedMessages = openaiMessages;
|
|
234
|
+
}
|
|
235
|
+
// Add create params if they exist
|
|
236
|
+
if (typeof createParams !== "undefined") {
|
|
237
|
+
debugData.createParams = createParams;
|
|
238
|
+
}
|
|
239
|
+
// Add tools if they exist
|
|
240
|
+
if (processedTools) {
|
|
241
|
+
debugData.tools = processedTools;
|
|
242
|
+
}
|
|
243
|
+
fs.writeFileSync(messagesFile, JSON.stringify(debugData, null, 2));
|
|
244
|
+
// Save error details
|
|
245
|
+
const errorData = {
|
|
246
|
+
error: {
|
|
247
|
+
message: error && typeof error === "object" && "message" in error
|
|
248
|
+
? String(error.message)
|
|
249
|
+
: undefined,
|
|
250
|
+
status: error && typeof error === "object" && "status" in error
|
|
251
|
+
? Number(error.status)
|
|
252
|
+
: undefined,
|
|
253
|
+
type: error && typeof error === "object" && "type" in error
|
|
254
|
+
? String(error.type)
|
|
255
|
+
: undefined,
|
|
256
|
+
code: error && typeof error === "object" && "code" in error
|
|
257
|
+
? String(error.code)
|
|
258
|
+
: undefined,
|
|
259
|
+
body: error && typeof error === "object" && "body" in error
|
|
260
|
+
? error.body
|
|
261
|
+
: undefined,
|
|
262
|
+
stack: error && typeof error === "object" && "stack" in error
|
|
263
|
+
? String(error.stack)
|
|
264
|
+
: undefined,
|
|
265
|
+
},
|
|
266
|
+
timestamp: new Date().toISOString(),
|
|
267
|
+
};
|
|
268
|
+
fs.writeFileSync(errorFile, JSON.stringify(errorData, null, 2));
|
|
269
|
+
logger.error("callAgent 400 error occurred. Debug files saved to:", tempDir);
|
|
270
|
+
logger.error("Messages file:", messagesFile);
|
|
271
|
+
logger.error("Error file:", errorFile);
|
|
272
|
+
logger.error("Error details:", error);
|
|
273
|
+
}
|
|
274
|
+
catch (saveError) {
|
|
275
|
+
logger.error("Failed to save 400 error debug files:", saveError);
|
|
276
|
+
}
|
|
277
|
+
}
|
|
278
|
+
logger.error("Failed to call OpenAI:", error);
|
|
279
|
+
throw error;
|
|
280
|
+
}
|
|
281
|
+
}
|
|
282
|
+
/**
|
|
283
|
+
* Process streaming response from OpenAI API
|
|
284
|
+
* @param stream Async iterator of chat completion chunks
|
|
285
|
+
* @param onContentUpdate Callback for content updates
|
|
286
|
+
* @param onToolUpdate Callback for tool updates
|
|
287
|
+
* @param abortSignal Optional abort signal
|
|
288
|
+
* @param responseHeaders Response headers from the initial request
|
|
289
|
+
* @param modelName Model name for cache control processing
|
|
290
|
+
* @returns Final result with accumulated content and tool calls
|
|
291
|
+
*/
|
|
292
|
+
async function processStreamingResponse(stream, onContentUpdate, onToolUpdate, onReasoningUpdate, abortSignal, responseHeaders, modelName) {
|
|
293
|
+
let accumulatedContent = "";
|
|
294
|
+
let accumulatedReasoningContent = "";
|
|
295
|
+
const toolCalls = [];
|
|
296
|
+
const additionalDeltaFields = {};
|
|
297
|
+
let usage = undefined;
|
|
298
|
+
let finishReason = null;
|
|
299
|
+
try {
|
|
300
|
+
for await (const chunk of stream) {
|
|
301
|
+
// Check for abort signal
|
|
302
|
+
if (abortSignal?.aborted) {
|
|
303
|
+
throw new Error("Request was aborted");
|
|
304
|
+
}
|
|
305
|
+
// Check for usage information in any chunk
|
|
306
|
+
if (chunk.usage) {
|
|
307
|
+
let chunkUsage = {
|
|
308
|
+
prompt_tokens: chunk.usage.prompt_tokens,
|
|
309
|
+
completion_tokens: chunk.usage.completion_tokens,
|
|
310
|
+
total_tokens: chunk.usage.total_tokens,
|
|
311
|
+
};
|
|
312
|
+
// Extend usage with cache metrics for Claude models
|
|
313
|
+
if (modelName && isClaudeModel(modelName)) {
|
|
314
|
+
chunkUsage = extendUsageWithCacheMetrics(chunkUsage, chunk.usage);
|
|
315
|
+
}
|
|
316
|
+
usage = chunkUsage;
|
|
317
|
+
}
|
|
318
|
+
// Check for finish_reason in the choice
|
|
319
|
+
const choice = chunk.choices?.[0];
|
|
320
|
+
if (choice?.finish_reason) {
|
|
321
|
+
finishReason = choice.finish_reason;
|
|
322
|
+
}
|
|
323
|
+
const delta = choice?.delta;
|
|
324
|
+
if (!delta) {
|
|
325
|
+
continue;
|
|
326
|
+
}
|
|
327
|
+
const { content, tool_calls: toolCallUpdates, reasoning_content, ...deltaMetadata } = delta;
|
|
328
|
+
if (Object.keys(deltaMetadata).length > 0) {
|
|
329
|
+
Object.assign(additionalDeltaFields, deltaMetadata);
|
|
330
|
+
}
|
|
331
|
+
if (typeof content === "string" && content.length > 0) {
|
|
332
|
+
// Note: OpenAI API already handles UTF-8 character boundaries correctly in streaming,
|
|
333
|
+
// ensuring that delta.content always contains complete UTF-8 strings
|
|
334
|
+
accumulatedContent += content;
|
|
335
|
+
if (onContentUpdate) {
|
|
336
|
+
onContentUpdate(accumulatedContent);
|
|
337
|
+
}
|
|
338
|
+
}
|
|
339
|
+
if (typeof reasoning_content === "string" &&
|
|
340
|
+
reasoning_content.length > 0) {
|
|
341
|
+
accumulatedReasoningContent += reasoning_content;
|
|
342
|
+
if (onReasoningUpdate) {
|
|
343
|
+
onReasoningUpdate(accumulatedReasoningContent);
|
|
344
|
+
}
|
|
345
|
+
}
|
|
346
|
+
if (Array.isArray(toolCallUpdates)) {
|
|
347
|
+
for (const rawToolCall of toolCallUpdates) {
|
|
348
|
+
const toolCallDelta = rawToolCall;
|
|
349
|
+
if (!toolCallDelta.function) {
|
|
350
|
+
continue;
|
|
351
|
+
}
|
|
352
|
+
const functionDelta = toolCallDelta.function;
|
|
353
|
+
let existingCall;
|
|
354
|
+
let isNew = false;
|
|
355
|
+
if (toolCallDelta.id) {
|
|
356
|
+
existingCall = toolCalls.find((t) => t.id === toolCallDelta.id);
|
|
357
|
+
if (!existingCall) {
|
|
358
|
+
existingCall = {
|
|
359
|
+
id: toolCallDelta.id,
|
|
360
|
+
type: "function",
|
|
361
|
+
function: {
|
|
362
|
+
name: functionDelta.name || "",
|
|
363
|
+
arguments: "",
|
|
364
|
+
},
|
|
365
|
+
};
|
|
366
|
+
toolCalls.push(existingCall);
|
|
367
|
+
isNew = true;
|
|
368
|
+
}
|
|
369
|
+
}
|
|
370
|
+
else {
|
|
371
|
+
existingCall = toolCalls[toolCalls.length - 1];
|
|
372
|
+
}
|
|
373
|
+
if (!existingCall) {
|
|
374
|
+
continue;
|
|
375
|
+
}
|
|
376
|
+
if (functionDelta.name) {
|
|
377
|
+
existingCall.function.name = functionDelta.name;
|
|
378
|
+
}
|
|
379
|
+
// Emit start stage when a new tool call is created and we have the tool name
|
|
380
|
+
if (onToolUpdate && isNew && existingCall.function.name) {
|
|
381
|
+
onToolUpdate({
|
|
382
|
+
id: existingCall.id,
|
|
383
|
+
name: existingCall.function.name,
|
|
384
|
+
parameters: "", // Empty parameters for start stage
|
|
385
|
+
parametersChunk: "", // Empty chunk for start stage
|
|
386
|
+
stage: "start", // New tool call triggers start stage
|
|
387
|
+
});
|
|
388
|
+
isNew = false; // Prevent duplicate start emissions
|
|
389
|
+
}
|
|
390
|
+
if (functionDelta.arguments) {
|
|
391
|
+
existingCall.function.arguments += functionDelta.arguments;
|
|
392
|
+
}
|
|
393
|
+
// Emit streaming updates for all chunks with actual content (including first chunk)
|
|
394
|
+
if (onToolUpdate &&
|
|
395
|
+
existingCall.function.name &&
|
|
396
|
+
functionDelta.arguments &&
|
|
397
|
+
functionDelta.arguments.length > 0 // Only emit streaming for chunks with actual content
|
|
398
|
+
) {
|
|
399
|
+
onToolUpdate({
|
|
400
|
+
id: existingCall.id,
|
|
401
|
+
name: existingCall.function.name,
|
|
402
|
+
parameters: existingCall.function.arguments,
|
|
403
|
+
parametersChunk: functionDelta.arguments,
|
|
404
|
+
stage: "streaming",
|
|
405
|
+
});
|
|
406
|
+
}
|
|
407
|
+
}
|
|
408
|
+
}
|
|
409
|
+
}
|
|
410
|
+
}
|
|
411
|
+
catch (error) {
|
|
412
|
+
if (error.message === "Request was aborted") {
|
|
413
|
+
throw error;
|
|
414
|
+
}
|
|
133
415
|
throw error;
|
|
134
416
|
}
|
|
417
|
+
// Prepare final result
|
|
418
|
+
const result = {};
|
|
419
|
+
if (accumulatedContent) {
|
|
420
|
+
result.content = accumulatedContent.trim();
|
|
421
|
+
}
|
|
422
|
+
if (accumulatedReasoningContent) {
|
|
423
|
+
result.reasoning_content = accumulatedReasoningContent.trim();
|
|
424
|
+
}
|
|
425
|
+
if (toolCalls.length > 0) {
|
|
426
|
+
result.tool_calls = toolCalls;
|
|
427
|
+
}
|
|
428
|
+
if (usage) {
|
|
429
|
+
result.usage = usage;
|
|
430
|
+
}
|
|
431
|
+
if (finishReason) {
|
|
432
|
+
result.finish_reason = finishReason;
|
|
433
|
+
}
|
|
434
|
+
if (responseHeaders && Object.keys(responseHeaders).length > 0) {
|
|
435
|
+
result.response_headers = responseHeaders;
|
|
436
|
+
}
|
|
437
|
+
if (Object.keys(additionalDeltaFields).length > 0) {
|
|
438
|
+
result.additionalFields = {};
|
|
439
|
+
for (const [key, value] of Object.entries(additionalDeltaFields)) {
|
|
440
|
+
if (value !== undefined && key !== "role") {
|
|
441
|
+
result.additionalFields[key] = value;
|
|
442
|
+
}
|
|
443
|
+
}
|
|
444
|
+
if (Object.keys(result.additionalFields).length === 0) {
|
|
445
|
+
delete result.additionalFields;
|
|
446
|
+
}
|
|
447
|
+
}
|
|
448
|
+
return result;
|
|
135
449
|
}
|
|
136
450
|
export async function compressMessages(options) {
|
|
137
451
|
const { gatewayConfig, modelConfig, messages, abortSignal } = options;
|
|
@@ -139,11 +453,14 @@ export async function compressMessages(options) {
|
|
|
139
453
|
const openai = new OpenAI({
|
|
140
454
|
apiKey: gatewayConfig.apiKey,
|
|
141
455
|
baseURL: gatewayConfig.baseURL,
|
|
456
|
+
defaultHeaders: gatewayConfig.defaultHeaders,
|
|
457
|
+
fetchOptions: gatewayConfig.fetchOptions,
|
|
458
|
+
fetch: gatewayConfig.fetch,
|
|
142
459
|
});
|
|
143
460
|
// Get model configuration - use injected fast model
|
|
144
461
|
const openaiModelConfig = getModelConfig(modelConfig.fastModel, {
|
|
145
462
|
temperature: 0.1,
|
|
146
|
-
max_tokens:
|
|
463
|
+
max_tokens: 2048,
|
|
147
464
|
});
|
|
148
465
|
try {
|
|
149
466
|
const response = await openai.chat.completions.create({
|
|
@@ -151,54 +468,106 @@ export async function compressMessages(options) {
|
|
|
151
468
|
messages: [
|
|
152
469
|
{
|
|
153
470
|
role: "system",
|
|
154
|
-
content: `
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
-
|
|
161
|
-
-
|
|
162
|
-
-
|
|
163
|
-
-
|
|
164
|
-
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
471
|
+
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.
|
|
472
|
+
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.
|
|
473
|
+
|
|
474
|
+
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:
|
|
475
|
+
|
|
476
|
+
1. Chronologically analyze each message and section of the conversation. For each section thoroughly identify:
|
|
477
|
+
- The user's explicit requests and intents
|
|
478
|
+
- Your approach to addressing the user's requests
|
|
479
|
+
- Key decisions, technical concepts and code patterns
|
|
480
|
+
- Specific details like:
|
|
481
|
+
- file names
|
|
482
|
+
- full code snippets
|
|
483
|
+
- function signatures
|
|
484
|
+
- file edits
|
|
485
|
+
- Errors that you ran into and how you fixed them
|
|
486
|
+
- Pay special attention to specific user feedback that you received, especially if the user told you to do something differently.
|
|
487
|
+
2. Double-check for technical accuracy and completeness, addressing each required element thoroughly.
|
|
488
|
+
|
|
489
|
+
Your summary should include the following sections:
|
|
490
|
+
|
|
491
|
+
1. Primary Request and Intent: Capture all of the user's explicit requests and intents in detail
|
|
492
|
+
2. Key Technical Concepts: List all important technical concepts, technologies, and frameworks discussed.
|
|
493
|
+
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.
|
|
494
|
+
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.
|
|
495
|
+
5. Problem Solving: Document problems solved and any ongoing troubleshooting efforts.
|
|
496
|
+
6. All user messages: List ALL user messages that are not tool results. These are critical for understanding the users' feedback and changing intent.
|
|
497
|
+
6. Pending Tasks: Outline any pending tasks that you have explicitly been asked to work on.
|
|
498
|
+
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.
|
|
499
|
+
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.
|
|
500
|
+
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.
|
|
501
|
+
|
|
502
|
+
Here's an example of how your output should be structured:
|
|
503
|
+
|
|
504
|
+
<example>
|
|
505
|
+
<analysis>
|
|
506
|
+
[Your thought process, ensuring all points are covered thoroughly and accurately]
|
|
507
|
+
</analysis>
|
|
508
|
+
|
|
509
|
+
<summary>
|
|
510
|
+
1. Primary Request and Intent:
|
|
511
|
+
[Detailed description]
|
|
512
|
+
|
|
513
|
+
2. Key Technical Concepts:
|
|
514
|
+
- [Concept 1]
|
|
515
|
+
- [Concept 2]
|
|
516
|
+
- [...]
|
|
517
|
+
|
|
518
|
+
3. Files and Code Sections:
|
|
519
|
+
- [File Name 1]
|
|
520
|
+
- [Summary of why this file is important]
|
|
521
|
+
- [Summary of the changes made to this file, if any]
|
|
522
|
+
- [Important Code Snippet]
|
|
523
|
+
- [File Name 2]
|
|
524
|
+
- [Important Code Snippet]
|
|
525
|
+
- [...]
|
|
526
|
+
|
|
527
|
+
4. Errors and fixes:
|
|
528
|
+
- [Detailed description of error 1]:
|
|
529
|
+
- [How you fixed the error]
|
|
530
|
+
- [User feedback on the error if any]
|
|
531
|
+
- [...]
|
|
532
|
+
|
|
533
|
+
5. Problem Solving:
|
|
534
|
+
[Description of solved problems and ongoing troubleshooting]
|
|
535
|
+
|
|
536
|
+
6. All user messages:
|
|
537
|
+
- [Detailed non tool use user message]
|
|
538
|
+
- [...]
|
|
539
|
+
|
|
540
|
+
7. Pending Tasks:
|
|
541
|
+
- [Task 1]
|
|
542
|
+
- [Task 2]
|
|
543
|
+
- [...]
|
|
544
|
+
|
|
545
|
+
8. Current Work:
|
|
546
|
+
[Precise description of current work]
|
|
547
|
+
|
|
548
|
+
9. Optional Next Step:
|
|
549
|
+
[Optional Next step to take]
|
|
550
|
+
|
|
551
|
+
</summary>
|
|
552
|
+
</example>
|
|
553
|
+
|
|
554
|
+
Please provide your summary based on the conversation so far, following this structure and ensuring precision and thoroughness in your response.
|
|
555
|
+
|
|
556
|
+
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:
|
|
557
|
+
<example>
|
|
558
|
+
## Compact Instructions
|
|
559
|
+
When summarizing the conversation focus on typescript code changes and also remember the mistakes you made and how you fixed them.
|
|
560
|
+
</example>
|
|
561
|
+
|
|
562
|
+
<example>
|
|
563
|
+
# Summary instructions
|
|
564
|
+
When you are using compact - please focus on test output and code changes. Include file reads verbatim.
|
|
565
|
+
</example>`,
|
|
197
566
|
},
|
|
198
567
|
...messages,
|
|
199
568
|
{
|
|
200
569
|
role: "user",
|
|
201
|
-
content: `Please
|
|
570
|
+
content: `Please create a detailed summary of the conversation so far.`,
|
|
202
571
|
},
|
|
203
572
|
],
|
|
204
573
|
}, {
|
|
@@ -222,7 +591,7 @@ For technical conversations, structure as:
|
|
|
222
591
|
if (error.name === "AbortError") {
|
|
223
592
|
throw new Error("Compression request was aborted");
|
|
224
593
|
}
|
|
225
|
-
|
|
594
|
+
logger.error("Failed to compress messages:", error);
|
|
226
595
|
return {
|
|
227
596
|
content: "Failed to compress conversation history",
|
|
228
597
|
usage: undefined,
|