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
|
@@ -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
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
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
|
-
|
|
154
|
-
|
|
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
|
-
|
|
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 (
|
|
170
|
-
createParams.tools =
|
|
278
|
+
if (processedTools && processedTools.length > 0) {
|
|
279
|
+
createParams.tools = processedTools;
|
|
171
280
|
}
|
|
172
281
|
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
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
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
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
|
-
|
|
358
|
+
if (
|
|
359
|
+
typeof finalReasoningContent === "string" &&
|
|
360
|
+
finalReasoningContent.length > 0
|
|
361
|
+
) {
|
|
362
|
+
result.reasoning_content = finalReasoningContent;
|
|
363
|
+
}
|
|
188
364
|
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
}
|
|
365
|
+
if (Array.isArray(finalToolCalls) && finalToolCalls.length > 0) {
|
|
366
|
+
result.tool_calls = finalToolCalls;
|
|
367
|
+
}
|
|
193
368
|
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
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
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
382
|
+
if (totalUsage) {
|
|
383
|
+
result.usage = totalUsage;
|
|
384
|
+
}
|
|
385
|
+
|
|
386
|
+
if (finishReason) {
|
|
387
|
+
result.finish_reason = finishReason;
|
|
388
|
+
}
|
|
203
389
|
|
|
204
|
-
|
|
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
|
-
|
|
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:
|
|
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: `
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
-
|
|
264
|
-
-
|
|
265
|
-
-
|
|
266
|
-
-
|
|
267
|
-
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
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
|
|
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
|
-
|
|
904
|
+
logger.error("Failed to compress messages:", error);
|
|
333
905
|
return {
|
|
334
906
|
content: "Failed to compress conversation history",
|
|
335
907
|
usage: undefined,
|