wave-agent-sdk 0.0.6 → 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 +209 -24
- 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 +248 -132
- package/dist/managers/backgroundBashManager.d.ts.map +1 -1
- package/dist/managers/backgroundBashManager.js +7 -6
- package/dist/managers/hookManager.d.ts +13 -16
- package/dist/managers/hookManager.d.ts.map +1 -1
- package/dist/managers/hookManager.js +81 -44
- 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 +41 -24
- package/dist/managers/messageManager.d.ts.map +1 -1
- package/dist/managers/messageManager.js +168 -49
- package/dist/managers/slashCommandManager.d.ts.map +1 -1
- package/dist/managers/slashCommandManager.js +9 -3
- package/dist/managers/subagentManager.d.ts +51 -0
- package/dist/managers/subagentManager.d.ts.map +1 -1
- package/dist/managers/subagentManager.js +190 -19
- 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/todoWriteTool.d.ts.map +1 -1
- package/dist/tools/todoWriteTool.js +3 -10
- 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 +21 -9
- package/dist/types/messaging.d.ts.map +1 -1
- package/dist/types/messaging.js +5 -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 +8 -13
- 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/hookMatcher.d.ts +1 -6
- package/dist/utils/hookMatcher.d.ts.map +1 -1
- package/dist/utils/mcpUtils.d.ts.map +1 -1
- package/dist/utils/mcpUtils.js +25 -3
- package/dist/utils/messageOperations.d.ts +27 -27
- package/dist/utils/messageOperations.d.ts.map +1 -1
- package/dist/utils/messageOperations.js +46 -36
- 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 +301 -37
- package/src/constants/events.ts +38 -0
- package/src/index.ts +2 -0
- package/src/managers/aiManager.ts +325 -173
- package/src/managers/backgroundBashManager.ts +7 -6
- package/src/managers/hookManager.ts +106 -84
- package/src/managers/liveConfigManager.ts +248 -0
- package/src/managers/messageManager.ts +237 -100
- package/src/managers/slashCommandManager.ts +9 -7
- package/src/managers/subagentManager.ts +284 -22
- 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/todoWriteTool.ts +3 -11
- 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 +21 -10
- 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 +8 -14
- package/src/utils/customCommands.ts +90 -22
- package/src/utils/fileUtils.ts +65 -0
- package/src/utils/globalLogger.ts +145 -0
- package/src/utils/hookMatcher.ts +1 -12
- package/src/utils/mcpUtils.ts +34 -3
- package/src/utils/messageOperations.ts +77 -60
- package/src/utils/pathEncoder.ts +379 -0
- package/src/utils/subagentParser.ts +2 -1
- package/src/utils/tokenCalculation.ts +43 -0
- package/src/types/index.ts.backup +0 -357
|
@@ -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,7 +50,7 @@ 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, } = options;
|
|
48
54
|
try {
|
|
49
55
|
// Create OpenAI client with injected configuration
|
|
50
56
|
const openai = new OpenAI({
|
|
@@ -83,55 +89,273 @@ Today's date: ${new Date().toISOString().split("T")[0]}
|
|
|
83
89
|
content: systemContent,
|
|
84
90
|
};
|
|
85
91
|
// ChatCompletionMessageParam[] is already in OpenAI format, add system prompt to the beginning
|
|
86
|
-
|
|
92
|
+
let openaiMessages = [
|
|
93
|
+
systemMessage,
|
|
94
|
+
...messages,
|
|
95
|
+
];
|
|
96
|
+
// Apply cache control for Claude models
|
|
97
|
+
const currentModel = model || modelConfig.agentModel;
|
|
98
|
+
if (isClaudeModel(currentModel)) {
|
|
99
|
+
openaiMessages = transformMessagesForClaudeCache(openaiMessages, currentModel);
|
|
100
|
+
}
|
|
87
101
|
// Get model configuration - use injected modelConfig with optional override
|
|
88
102
|
const openaiModelConfig = getModelConfig(model || modelConfig.agentModel, {
|
|
89
103
|
temperature: 0,
|
|
90
|
-
max_completion_tokens: 32768,
|
|
91
104
|
});
|
|
105
|
+
// Determine if streaming is needed
|
|
106
|
+
const isStreaming = !!(onContentUpdate || onToolUpdate);
|
|
92
107
|
// Prepare API call parameters
|
|
93
108
|
const createParams = {
|
|
94
109
|
...openaiModelConfig,
|
|
95
110
|
messages: openaiMessages,
|
|
111
|
+
stream: isStreaming,
|
|
96
112
|
};
|
|
97
113
|
// Only add tools if they exist
|
|
98
114
|
if (tools && tools.length > 0) {
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
const finalMessage = response.choices[0]?.message;
|
|
106
|
-
const totalUsage = response.usage
|
|
107
|
-
? {
|
|
108
|
-
prompt_tokens: response.usage.prompt_tokens,
|
|
109
|
-
completion_tokens: response.usage.completion_tokens,
|
|
110
|
-
total_tokens: response.usage.total_tokens,
|
|
115
|
+
// Apply cache control to tools for Claude models
|
|
116
|
+
if (isClaudeModel(currentModel)) {
|
|
117
|
+
createParams.tools = addCacheControlToLastTool(tools);
|
|
118
|
+
}
|
|
119
|
+
else {
|
|
120
|
+
createParams.tools = tools;
|
|
111
121
|
}
|
|
112
|
-
: undefined;
|
|
113
|
-
const result = {};
|
|
114
|
-
// Return content
|
|
115
|
-
if (finalMessage?.content) {
|
|
116
|
-
result.content = finalMessage.content;
|
|
117
122
|
}
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
123
|
+
if (isStreaming) {
|
|
124
|
+
// Handle streaming response
|
|
125
|
+
const { data: stream, response } = await openai.chat.completions
|
|
126
|
+
.create(createParams, {
|
|
127
|
+
signal: abortSignal,
|
|
128
|
+
})
|
|
129
|
+
.withResponse();
|
|
130
|
+
// Extract response headers
|
|
131
|
+
const responseHeaders = {};
|
|
132
|
+
response.headers.forEach((value, key) => {
|
|
133
|
+
responseHeaders[key] = value;
|
|
134
|
+
});
|
|
135
|
+
return await processStreamingResponse(stream, onContentUpdate, onToolUpdate, abortSignal, responseHeaders, currentModel);
|
|
121
136
|
}
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
137
|
+
else {
|
|
138
|
+
// Handle non-streaming response
|
|
139
|
+
const { data: response, response: rawResponse } = await openai.chat.completions
|
|
140
|
+
.create(createParams, {
|
|
141
|
+
signal: abortSignal,
|
|
142
|
+
})
|
|
143
|
+
.withResponse();
|
|
144
|
+
// Extract response headers
|
|
145
|
+
const responseHeaders = {};
|
|
146
|
+
rawResponse.headers.forEach((value, key) => {
|
|
147
|
+
responseHeaders[key] = value;
|
|
148
|
+
});
|
|
149
|
+
const finalMessage = response.choices[0]?.message;
|
|
150
|
+
const finishReason = response.choices[0]?.finish_reason || null;
|
|
151
|
+
let totalUsage = response.usage
|
|
152
|
+
? {
|
|
153
|
+
prompt_tokens: response.usage.prompt_tokens,
|
|
154
|
+
completion_tokens: response.usage.completion_tokens,
|
|
155
|
+
total_tokens: response.usage.total_tokens,
|
|
156
|
+
}
|
|
157
|
+
: undefined;
|
|
158
|
+
// Extend usage with cache metrics for Claude models
|
|
159
|
+
if (totalUsage && isClaudeModel(currentModel) && response.usage) {
|
|
160
|
+
totalUsage = extendUsageWithCacheMetrics(totalUsage, response.usage);
|
|
161
|
+
}
|
|
162
|
+
const result = {};
|
|
163
|
+
if (finalMessage) {
|
|
164
|
+
const { content: finalContent, tool_calls: finalToolCalls, ...otherFields } = finalMessage;
|
|
165
|
+
if (typeof finalContent === "string" && finalContent.length > 0) {
|
|
166
|
+
result.content = finalContent;
|
|
167
|
+
}
|
|
168
|
+
if (Array.isArray(finalToolCalls) && finalToolCalls.length > 0) {
|
|
169
|
+
result.tool_calls = finalToolCalls;
|
|
170
|
+
}
|
|
171
|
+
if (Object.keys(otherFields).length > 0) {
|
|
172
|
+
const metadata = {};
|
|
173
|
+
for (const [key, value] of Object.entries(otherFields)) {
|
|
174
|
+
if (value !== undefined) {
|
|
175
|
+
metadata[key] = value;
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
if (Object.keys(metadata).length > 0) {
|
|
179
|
+
result.metadata = metadata;
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
if (totalUsage) {
|
|
184
|
+
result.usage = totalUsage;
|
|
185
|
+
}
|
|
186
|
+
if (finishReason) {
|
|
187
|
+
result.finish_reason = finishReason;
|
|
188
|
+
}
|
|
189
|
+
if (Object.keys(responseHeaders).length > 0) {
|
|
190
|
+
result.response_headers = responseHeaders;
|
|
191
|
+
}
|
|
192
|
+
return result;
|
|
125
193
|
}
|
|
126
|
-
return result;
|
|
127
194
|
}
|
|
128
195
|
catch (error) {
|
|
129
196
|
if (error.name === "AbortError") {
|
|
130
197
|
throw new Error("Request was aborted");
|
|
131
198
|
}
|
|
132
|
-
|
|
199
|
+
logger.error("Failed to call OpenAI:", error);
|
|
200
|
+
throw error;
|
|
201
|
+
}
|
|
202
|
+
}
|
|
203
|
+
/**
|
|
204
|
+
* Process streaming response from OpenAI API
|
|
205
|
+
* @param stream Async iterator of chat completion chunks
|
|
206
|
+
* @param onContentUpdate Callback for content updates
|
|
207
|
+
* @param onToolUpdate Callback for tool updates
|
|
208
|
+
* @param abortSignal Optional abort signal
|
|
209
|
+
* @param responseHeaders Response headers from the initial request
|
|
210
|
+
* @param modelName Model name for cache control processing
|
|
211
|
+
* @returns Final result with accumulated content and tool calls
|
|
212
|
+
*/
|
|
213
|
+
async function processStreamingResponse(stream, onContentUpdate, onToolUpdate, abortSignal, responseHeaders, modelName) {
|
|
214
|
+
let accumulatedContent = "";
|
|
215
|
+
const toolCalls = [];
|
|
216
|
+
const additionalDeltaFields = {};
|
|
217
|
+
let usage = undefined;
|
|
218
|
+
let finishReason = null;
|
|
219
|
+
try {
|
|
220
|
+
for await (const chunk of stream) {
|
|
221
|
+
// Check for abort signal
|
|
222
|
+
if (abortSignal?.aborted) {
|
|
223
|
+
throw new Error("Request was aborted");
|
|
224
|
+
}
|
|
225
|
+
// Check for usage information in any chunk
|
|
226
|
+
if (chunk.usage) {
|
|
227
|
+
let chunkUsage = {
|
|
228
|
+
prompt_tokens: chunk.usage.prompt_tokens,
|
|
229
|
+
completion_tokens: chunk.usage.completion_tokens,
|
|
230
|
+
total_tokens: chunk.usage.total_tokens,
|
|
231
|
+
};
|
|
232
|
+
// Extend usage with cache metrics for Claude models
|
|
233
|
+
if (modelName && isClaudeModel(modelName)) {
|
|
234
|
+
chunkUsage = extendUsageWithCacheMetrics(chunkUsage, chunk.usage);
|
|
235
|
+
}
|
|
236
|
+
usage = chunkUsage;
|
|
237
|
+
}
|
|
238
|
+
// Check for finish_reason in the choice
|
|
239
|
+
const choice = chunk.choices?.[0];
|
|
240
|
+
if (choice?.finish_reason) {
|
|
241
|
+
finishReason = choice.finish_reason;
|
|
242
|
+
}
|
|
243
|
+
const delta = choice?.delta;
|
|
244
|
+
if (!delta) {
|
|
245
|
+
continue;
|
|
246
|
+
}
|
|
247
|
+
const { content, tool_calls: toolCallUpdates, ...deltaMetadata } = delta;
|
|
248
|
+
if (Object.keys(deltaMetadata).length > 0) {
|
|
249
|
+
Object.assign(additionalDeltaFields, deltaMetadata);
|
|
250
|
+
}
|
|
251
|
+
if (typeof content === "string" && content.length > 0) {
|
|
252
|
+
// Note: OpenAI API already handles UTF-8 character boundaries correctly in streaming,
|
|
253
|
+
// ensuring that delta.content always contains complete UTF-8 strings
|
|
254
|
+
accumulatedContent += content;
|
|
255
|
+
if (onContentUpdate) {
|
|
256
|
+
onContentUpdate(accumulatedContent);
|
|
257
|
+
}
|
|
258
|
+
}
|
|
259
|
+
if (Array.isArray(toolCallUpdates)) {
|
|
260
|
+
for (const rawToolCall of toolCallUpdates) {
|
|
261
|
+
const toolCallDelta = rawToolCall;
|
|
262
|
+
if (!toolCallDelta.function) {
|
|
263
|
+
continue;
|
|
264
|
+
}
|
|
265
|
+
const functionDelta = toolCallDelta.function;
|
|
266
|
+
let existingCall;
|
|
267
|
+
let isNew = false;
|
|
268
|
+
if (toolCallDelta.id) {
|
|
269
|
+
existingCall = toolCalls.find((t) => t.id === toolCallDelta.id);
|
|
270
|
+
if (!existingCall) {
|
|
271
|
+
existingCall = {
|
|
272
|
+
id: toolCallDelta.id,
|
|
273
|
+
type: "function",
|
|
274
|
+
function: {
|
|
275
|
+
name: functionDelta.name || "",
|
|
276
|
+
arguments: "",
|
|
277
|
+
},
|
|
278
|
+
};
|
|
279
|
+
toolCalls.push(existingCall);
|
|
280
|
+
isNew = true;
|
|
281
|
+
}
|
|
282
|
+
}
|
|
283
|
+
else {
|
|
284
|
+
existingCall = toolCalls[toolCalls.length - 1];
|
|
285
|
+
}
|
|
286
|
+
if (!existingCall) {
|
|
287
|
+
continue;
|
|
288
|
+
}
|
|
289
|
+
if (functionDelta.name) {
|
|
290
|
+
existingCall.function.name = functionDelta.name;
|
|
291
|
+
}
|
|
292
|
+
// Emit start stage when a new tool call is created and we have the tool name
|
|
293
|
+
if (onToolUpdate && isNew && existingCall.function.name) {
|
|
294
|
+
onToolUpdate({
|
|
295
|
+
id: existingCall.id,
|
|
296
|
+
name: existingCall.function.name,
|
|
297
|
+
parameters: "", // Empty parameters for start stage
|
|
298
|
+
parametersChunk: "", // Empty chunk for start stage
|
|
299
|
+
stage: "start", // New tool call triggers start stage
|
|
300
|
+
});
|
|
301
|
+
isNew = false; // Prevent duplicate start emissions
|
|
302
|
+
}
|
|
303
|
+
if (functionDelta.arguments) {
|
|
304
|
+
existingCall.function.arguments += functionDelta.arguments;
|
|
305
|
+
}
|
|
306
|
+
// Emit streaming updates for all chunks with actual content (including first chunk)
|
|
307
|
+
if (onToolUpdate &&
|
|
308
|
+
existingCall.function.name &&
|
|
309
|
+
functionDelta.arguments &&
|
|
310
|
+
functionDelta.arguments.length > 0 // Only emit streaming for chunks with actual content
|
|
311
|
+
) {
|
|
312
|
+
onToolUpdate({
|
|
313
|
+
id: existingCall.id,
|
|
314
|
+
name: existingCall.function.name,
|
|
315
|
+
parameters: existingCall.function.arguments,
|
|
316
|
+
parametersChunk: functionDelta.arguments,
|
|
317
|
+
stage: "streaming",
|
|
318
|
+
});
|
|
319
|
+
}
|
|
320
|
+
}
|
|
321
|
+
}
|
|
322
|
+
}
|
|
323
|
+
}
|
|
324
|
+
catch (error) {
|
|
325
|
+
if (error.message === "Request was aborted") {
|
|
326
|
+
throw error;
|
|
327
|
+
}
|
|
133
328
|
throw error;
|
|
134
329
|
}
|
|
330
|
+
// Prepare final result
|
|
331
|
+
const result = {};
|
|
332
|
+
if (accumulatedContent) {
|
|
333
|
+
result.content = accumulatedContent;
|
|
334
|
+
}
|
|
335
|
+
if (toolCalls.length > 0) {
|
|
336
|
+
result.tool_calls = toolCalls;
|
|
337
|
+
}
|
|
338
|
+
if (usage) {
|
|
339
|
+
result.usage = usage;
|
|
340
|
+
}
|
|
341
|
+
if (finishReason) {
|
|
342
|
+
result.finish_reason = finishReason;
|
|
343
|
+
}
|
|
344
|
+
if (responseHeaders && Object.keys(responseHeaders).length > 0) {
|
|
345
|
+
result.response_headers = responseHeaders;
|
|
346
|
+
}
|
|
347
|
+
if (Object.keys(additionalDeltaFields).length > 0) {
|
|
348
|
+
result.metadata = {};
|
|
349
|
+
for (const [key, value] of Object.entries(additionalDeltaFields)) {
|
|
350
|
+
if (value !== undefined) {
|
|
351
|
+
result.metadata[key] = value;
|
|
352
|
+
}
|
|
353
|
+
}
|
|
354
|
+
if (Object.keys(result.metadata).length === 0) {
|
|
355
|
+
delete result.metadata;
|
|
356
|
+
}
|
|
357
|
+
}
|
|
358
|
+
return result;
|
|
135
359
|
}
|
|
136
360
|
export async function compressMessages(options) {
|
|
137
361
|
const { gatewayConfig, modelConfig, messages, abortSignal } = options;
|
|
@@ -143,7 +367,7 @@ export async function compressMessages(options) {
|
|
|
143
367
|
// Get model configuration - use injected fast model
|
|
144
368
|
const openaiModelConfig = getModelConfig(modelConfig.fastModel, {
|
|
145
369
|
temperature: 0.1,
|
|
146
|
-
max_tokens:
|
|
370
|
+
max_tokens: 2048,
|
|
147
371
|
});
|
|
148
372
|
try {
|
|
149
373
|
const response = await openai.chat.completions.create({
|
|
@@ -151,54 +375,106 @@ export async function compressMessages(options) {
|
|
|
151
375
|
messages: [
|
|
152
376
|
{
|
|
153
377
|
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
|
-
|
|
378
|
+
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.
|
|
379
|
+
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.
|
|
380
|
+
|
|
381
|
+
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:
|
|
382
|
+
|
|
383
|
+
1. Chronologically analyze each message and section of the conversation. For each section thoroughly identify:
|
|
384
|
+
- The user's explicit requests and intents
|
|
385
|
+
- Your approach to addressing the user's requests
|
|
386
|
+
- Key decisions, technical concepts and code patterns
|
|
387
|
+
- Specific details like:
|
|
388
|
+
- file names
|
|
389
|
+
- full code snippets
|
|
390
|
+
- function signatures
|
|
391
|
+
- file edits
|
|
392
|
+
- Errors that you ran into and how you fixed them
|
|
393
|
+
- Pay special attention to specific user feedback that you received, especially if the user told you to do something differently.
|
|
394
|
+
2. Double-check for technical accuracy and completeness, addressing each required element thoroughly.
|
|
395
|
+
|
|
396
|
+
Your summary should include the following sections:
|
|
397
|
+
|
|
398
|
+
1. Primary Request and Intent: Capture all of the user's explicit requests and intents in detail
|
|
399
|
+
2. Key Technical Concepts: List all important technical concepts, technologies, and frameworks discussed.
|
|
400
|
+
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.
|
|
401
|
+
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.
|
|
402
|
+
5. Problem Solving: Document problems solved and any ongoing troubleshooting efforts.
|
|
403
|
+
6. All user messages: List ALL user messages that are not tool results. These are critical for understanding the users' feedback and changing intent.
|
|
404
|
+
6. Pending Tasks: Outline any pending tasks that you have explicitly been asked to work on.
|
|
405
|
+
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.
|
|
406
|
+
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.
|
|
407
|
+
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.
|
|
408
|
+
|
|
409
|
+
Here's an example of how your output should be structured:
|
|
410
|
+
|
|
411
|
+
<example>
|
|
412
|
+
<analysis>
|
|
413
|
+
[Your thought process, ensuring all points are covered thoroughly and accurately]
|
|
414
|
+
</analysis>
|
|
415
|
+
|
|
416
|
+
<summary>
|
|
417
|
+
1. Primary Request and Intent:
|
|
418
|
+
[Detailed description]
|
|
419
|
+
|
|
420
|
+
2. Key Technical Concepts:
|
|
421
|
+
- [Concept 1]
|
|
422
|
+
- [Concept 2]
|
|
423
|
+
- [...]
|
|
424
|
+
|
|
425
|
+
3. Files and Code Sections:
|
|
426
|
+
- [File Name 1]
|
|
427
|
+
- [Summary of why this file is important]
|
|
428
|
+
- [Summary of the changes made to this file, if any]
|
|
429
|
+
- [Important Code Snippet]
|
|
430
|
+
- [File Name 2]
|
|
431
|
+
- [Important Code Snippet]
|
|
432
|
+
- [...]
|
|
433
|
+
|
|
434
|
+
4. Errors and fixes:
|
|
435
|
+
- [Detailed description of error 1]:
|
|
436
|
+
- [How you fixed the error]
|
|
437
|
+
- [User feedback on the error if any]
|
|
438
|
+
- [...]
|
|
439
|
+
|
|
440
|
+
5. Problem Solving:
|
|
441
|
+
[Description of solved problems and ongoing troubleshooting]
|
|
442
|
+
|
|
443
|
+
6. All user messages:
|
|
444
|
+
- [Detailed non tool use user message]
|
|
445
|
+
- [...]
|
|
446
|
+
|
|
447
|
+
7. Pending Tasks:
|
|
448
|
+
- [Task 1]
|
|
449
|
+
- [Task 2]
|
|
450
|
+
- [...]
|
|
451
|
+
|
|
452
|
+
8. Current Work:
|
|
453
|
+
[Precise description of current work]
|
|
454
|
+
|
|
455
|
+
9. Optional Next Step:
|
|
456
|
+
[Optional Next step to take]
|
|
457
|
+
|
|
458
|
+
</summary>
|
|
459
|
+
</example>
|
|
460
|
+
|
|
461
|
+
Please provide your summary based on the conversation so far, following this structure and ensuring precision and thoroughness in your response.
|
|
462
|
+
|
|
463
|
+
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:
|
|
464
|
+
<example>
|
|
465
|
+
## Compact Instructions
|
|
466
|
+
When summarizing the conversation focus on typescript code changes and also remember the mistakes you made and how you fixed them.
|
|
467
|
+
</example>
|
|
468
|
+
|
|
469
|
+
<example>
|
|
470
|
+
# Summary instructions
|
|
471
|
+
When you are using compact - please focus on test output and code changes. Include file reads verbatim.
|
|
472
|
+
</example>`,
|
|
197
473
|
},
|
|
198
474
|
...messages,
|
|
199
475
|
{
|
|
200
476
|
role: "user",
|
|
201
|
-
content: `Please
|
|
477
|
+
content: `Please create a detailed summary of the conversation so far.`,
|
|
202
478
|
},
|
|
203
479
|
],
|
|
204
480
|
}, {
|
|
@@ -222,7 +498,7 @@ For technical conversations, structure as:
|
|
|
222
498
|
if (error.name === "AbortError") {
|
|
223
499
|
throw new Error("Compression request was aborted");
|
|
224
500
|
}
|
|
225
|
-
|
|
501
|
+
logger.error("Failed to compress messages:", error);
|
|
226
502
|
return {
|
|
227
503
|
content: "Failed to compress conversation history",
|
|
228
504
|
usage: undefined,
|
|
@@ -0,0 +1,120 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Configuration Watcher Service
|
|
3
|
+
*
|
|
4
|
+
* Orchestrates live configuration reload functionality by coordinating file watching,
|
|
5
|
+
* configuration validation, and error recovery. Provides automatic reload of settings.json
|
|
6
|
+
* changes without restart.
|
|
7
|
+
*/
|
|
8
|
+
import { EventEmitter } from "events";
|
|
9
|
+
import { type FileWatchEvent } from "./fileWatcher.js";
|
|
10
|
+
import type { WaveConfiguration } from "../types/hooks.js";
|
|
11
|
+
import type { Logger } from "../types/index.js";
|
|
12
|
+
export interface ConfigurationChangeEvent {
|
|
13
|
+
type: "settings_changed" | "memory_changed" | "env_changed";
|
|
14
|
+
path: string;
|
|
15
|
+
timestamp: number;
|
|
16
|
+
changes: {
|
|
17
|
+
added: string[];
|
|
18
|
+
modified: string[];
|
|
19
|
+
removed: string[];
|
|
20
|
+
};
|
|
21
|
+
isValid: boolean;
|
|
22
|
+
errorMessage?: string;
|
|
23
|
+
}
|
|
24
|
+
export interface ConfigurationReloadService {
|
|
25
|
+
initializeWatching(userPaths: string[], projectPaths?: string[]): Promise<void>;
|
|
26
|
+
reloadConfiguration(): Promise<WaveConfiguration>;
|
|
27
|
+
getCurrentConfiguration(): WaveConfiguration | null;
|
|
28
|
+
validateEnvironmentVariables(env: Record<string, string>): {
|
|
29
|
+
isValid: boolean;
|
|
30
|
+
errors: string[];
|
|
31
|
+
warnings: string[];
|
|
32
|
+
};
|
|
33
|
+
shutdown(): Promise<void>;
|
|
34
|
+
}
|
|
35
|
+
export declare class ConfigurationWatcher extends EventEmitter implements ConfigurationReloadService {
|
|
36
|
+
private fileWatcher;
|
|
37
|
+
private logger?;
|
|
38
|
+
private currentConfiguration;
|
|
39
|
+
private lastValidConfiguration;
|
|
40
|
+
private userConfigPaths?;
|
|
41
|
+
private projectConfigPaths?;
|
|
42
|
+
private workdir;
|
|
43
|
+
private isWatching;
|
|
44
|
+
private reloadInProgress;
|
|
45
|
+
constructor(workdir: string, logger?: Logger);
|
|
46
|
+
/**
|
|
47
|
+
* Initialize configuration watching
|
|
48
|
+
* Maps to FR-004: System MUST watch settings.json files
|
|
49
|
+
* Supports watching multiple file paths (e.g., settings.local.json and settings.json)
|
|
50
|
+
*/
|
|
51
|
+
initializeWatching(userPaths: string[], projectPaths?: string[]): Promise<void>;
|
|
52
|
+
/**
|
|
53
|
+
* Reload configuration from files
|
|
54
|
+
* Maps to FR-008: Continue with previous valid configuration on errors
|
|
55
|
+
*/
|
|
56
|
+
reloadConfiguration(): Promise<WaveConfiguration>;
|
|
57
|
+
/**
|
|
58
|
+
* Get current effective configuration
|
|
59
|
+
* Maps to FR-002: Merged configuration with project precedence
|
|
60
|
+
*/
|
|
61
|
+
getCurrentConfiguration(): WaveConfiguration | null;
|
|
62
|
+
/**
|
|
63
|
+
* Validate environment variables
|
|
64
|
+
* Maps to FR-003: Validate env field format
|
|
65
|
+
*/
|
|
66
|
+
validateEnvironmentVariables(env: Record<string, string>): {
|
|
67
|
+
isValid: boolean;
|
|
68
|
+
errors: string[];
|
|
69
|
+
warnings: string[];
|
|
70
|
+
};
|
|
71
|
+
/**
|
|
72
|
+
* Stop watching and cleanup resources
|
|
73
|
+
* Maps to cleanup requirements
|
|
74
|
+
*/
|
|
75
|
+
shutdown(): Promise<void>;
|
|
76
|
+
/**
|
|
77
|
+
* Watch an additional file (like AGENTS.md) for changes
|
|
78
|
+
* Maps to T033: Add AGENTS.md file watching to HookManager
|
|
79
|
+
*/
|
|
80
|
+
watchAdditionalFile(filePath: string, callback: (event: FileWatchEvent) => void): Promise<void>;
|
|
81
|
+
/**
|
|
82
|
+
* Stop watching an additional file
|
|
83
|
+
*/
|
|
84
|
+
unwatchAdditionalFile(filePath: string): Promise<void>;
|
|
85
|
+
/**
|
|
86
|
+
* Check if watching is active
|
|
87
|
+
*/
|
|
88
|
+
isWatchingActive(): boolean;
|
|
89
|
+
/**
|
|
90
|
+
* Get watcher status for monitoring
|
|
91
|
+
*/
|
|
92
|
+
getWatcherStatus(): {
|
|
93
|
+
isActive: boolean;
|
|
94
|
+
configurationLoaded: boolean;
|
|
95
|
+
hasValidConfiguration: boolean;
|
|
96
|
+
reloadInProgress: boolean;
|
|
97
|
+
watchedFiles: {
|
|
98
|
+
path: string;
|
|
99
|
+
isActive: boolean;
|
|
100
|
+
method: "native" | "polling" | "failed";
|
|
101
|
+
errorCount: number;
|
|
102
|
+
}[];
|
|
103
|
+
};
|
|
104
|
+
private setupFileWatcherEvents;
|
|
105
|
+
private handleFileChange;
|
|
106
|
+
/**
|
|
107
|
+
* Validate configuration structure and content
|
|
108
|
+
*/
|
|
109
|
+
private validateConfiguration;
|
|
110
|
+
private detectChanges;
|
|
111
|
+
/**
|
|
112
|
+
* Get the first existing user config path for error reporting
|
|
113
|
+
*/
|
|
114
|
+
private getFirstExistingUserPath;
|
|
115
|
+
/**
|
|
116
|
+
* Get the first existing project config path for error reporting
|
|
117
|
+
*/
|
|
118
|
+
private getFirstExistingProjectPath;
|
|
119
|
+
}
|
|
120
|
+
//# sourceMappingURL=configurationWatcher.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"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