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