@codebolt/agent 6.1.19 → 6.1.21
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/README.md +19 -10
- package/dist/processor-pieces/messageModifiers/argumentProcessorModifier.js +11 -15
- package/dist/processor-pieces/messageModifiers/atFileProcessorModifier.d.ts +0 -1
- package/dist/processor-pieces/messageModifiers/atFileProcessorModifier.js +16 -33
- package/dist/processor-pieces/messageModifiers/capabilityContextModifier.js +3 -2
- package/dist/processor-pieces/messageModifiers/chatHistoryMessageModifier.d.ts +7 -0
- package/dist/processor-pieces/messageModifiers/chatHistoryMessageModifier.js +135 -27
- package/dist/processor-pieces/messageModifiers/chatRecordingModifier.js +3 -3
- package/dist/processor-pieces/messageModifiers/contextAssemblyModifier.js +18 -12
- package/dist/processor-pieces/messageModifiers/directoryContextModifier.d.ts +1 -0
- package/dist/processor-pieces/messageModifiers/directoryContextModifier.js +15 -15
- package/dist/processor-pieces/messageModifiers/environmentContextModifier.d.ts +1 -0
- package/dist/processor-pieces/messageModifiers/environmentContextModifier.js +48 -2
- package/dist/processor-pieces/messageModifiers/ideContextModifier.js +3 -2
- package/dist/processor-pieces/messageModifiers/memoryImportModifier.js +9 -15
- package/dist/processor-pieces/messageModifiers/toolInjectionModifier.js +17 -20
- package/dist/processor-pieces/postInferenceProcessors/loopDetectionModifier.js +8 -19
- package/dist/processor-pieces/postToolCallProcessors/conversationCompactorModifier.d.ts +1 -1
- package/dist/processor-pieces/postToolCallProcessors/conversationCompactorModifier.js +15 -15
- package/dist/processor-pieces/postToolCallProcessors/shellProcessorModifier.js +3 -3
- package/dist/processor-pieces/preInferenceProcessors/chatCompressionModifier.js +2 -2
- package/dist/processor-pieces/utils/messageModifierHelper.js +3 -3
- package/dist/types/libFunctionTypes.d.ts +5 -0
- package/dist/unified/agent/agent.d.ts +69 -2
- package/dist/unified/agent/agent.js +370 -48
- package/dist/unified/agent/tools.d.ts +17 -3
- package/dist/unified/agent/tools.js +82 -51
- package/dist/unified/base/agentStep.d.ts +1 -0
- package/dist/unified/base/agentStep.js +39 -11
- package/dist/unified/base/initialPromptGenerator.d.ts +2 -0
- package/dist/unified/base/initialPromptGenerator.js +98 -20
- package/dist/unified/base/promptContext.d.ts +3 -0
- package/dist/unified/base/promptContext.js +193 -15
- package/dist/unified/base/responseExecutor.d.ts +9 -1
- package/dist/unified/base/responseExecutor.js +248 -68
- package/dist/unified/index.d.ts +1 -2
- package/dist/unified/index.js +2 -4
- package/dist/unified/services/CompressionCoordinator.js +9 -9
- package/dist/unified/services/compaction/autoCompact.js +5 -5
- package/dist/unified/services/compaction/contextCollapse.js +2 -2
- package/dist/unified/services/compaction/reactiveCompact.js +5 -5
- package/dist/unified/types/libTypes.d.ts +6 -0
- package/dist/unified/utils/agentToolLoader.d.ts +10 -0
- package/dist/unified/utils/agentToolLoader.js +90 -24
- package/package.json +5 -1
- package/dist/unified/agent/codeboltAgent.d.ts +0 -61
- package/dist/unified/agent/codeboltAgent.js +0 -334
|
@@ -7,6 +7,55 @@ exports.ResponseExecutor = void 0;
|
|
|
7
7
|
const codeboltjs_1 = __importDefault(require("@codebolt/codeboltjs"));
|
|
8
8
|
const promptContext_1 = require("./promptContext");
|
|
9
9
|
const agentToolLoader_1 = require("../utils/agentToolLoader");
|
|
10
|
+
function normalizeDiscoveredToolSchema(schema) {
|
|
11
|
+
const schemaFunction = schema['function'];
|
|
12
|
+
if (typeof (schemaFunction === null || schemaFunction === void 0 ? void 0 : schemaFunction.name) === 'string' && schemaFunction.name.length > 0) {
|
|
13
|
+
return schema;
|
|
14
|
+
}
|
|
15
|
+
if (schema['type'] !== 'function' || typeof schema['name'] !== 'string' || schema['name'].length === 0) {
|
|
16
|
+
return null;
|
|
17
|
+
}
|
|
18
|
+
return {
|
|
19
|
+
...schema,
|
|
20
|
+
function: {
|
|
21
|
+
name: schema['name'],
|
|
22
|
+
description: schema['description'],
|
|
23
|
+
parameters: schema['parameters'] || { type: 'object', properties: {} },
|
|
24
|
+
},
|
|
25
|
+
};
|
|
26
|
+
}
|
|
27
|
+
function extractDiscoveredToolSchemas(content) {
|
|
28
|
+
let parsedContent = content;
|
|
29
|
+
if (typeof content === 'string') {
|
|
30
|
+
try {
|
|
31
|
+
parsedContent = JSON.parse(content);
|
|
32
|
+
}
|
|
33
|
+
catch {
|
|
34
|
+
const jsonMatch = content.match(/\[[\s\S]*\]/);
|
|
35
|
+
if (!jsonMatch)
|
|
36
|
+
return [];
|
|
37
|
+
try {
|
|
38
|
+
parsedContent = JSON.parse(jsonMatch[0]);
|
|
39
|
+
}
|
|
40
|
+
catch {
|
|
41
|
+
return [];
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
const rawSchemas = Array.isArray(parsedContent)
|
|
46
|
+
? parsedContent
|
|
47
|
+
: (parsedContent &&
|
|
48
|
+
typeof parsedContent === 'object' &&
|
|
49
|
+
!Array.isArray(parsedContent) &&
|
|
50
|
+
parsedContent.type === 'tool_search_output' &&
|
|
51
|
+
Array.isArray(parsedContent.tools))
|
|
52
|
+
? parsedContent.tools
|
|
53
|
+
: [];
|
|
54
|
+
return rawSchemas
|
|
55
|
+
.filter((schema) => !!schema && typeof schema === 'object' && !Array.isArray(schema))
|
|
56
|
+
.map(normalizeDiscoveredToolSchema)
|
|
57
|
+
.filter((schema) => !!schema);
|
|
58
|
+
}
|
|
10
59
|
class ResponseExecutor {
|
|
11
60
|
constructor(options) {
|
|
12
61
|
this.preToolCallProcessors = [];
|
|
@@ -16,6 +65,7 @@ class ResponseExecutor {
|
|
|
16
65
|
this.preToolCallProcessors = options.preToolCallProcessors;
|
|
17
66
|
this.postToolCallProcessors = options.postToolCallProcessors;
|
|
18
67
|
this.loopDetectionService = options.loopDetectionService;
|
|
68
|
+
this.localToolsByExecutionName = options.localToolsByExecutionName || new Map();
|
|
19
69
|
}
|
|
20
70
|
async executeResponse(input) {
|
|
21
71
|
var _a, _b, _c;
|
|
@@ -40,25 +90,23 @@ class ResponseExecutor {
|
|
|
40
90
|
}
|
|
41
91
|
}
|
|
42
92
|
const compactionMessagePromise = this.runRequiredCompaction(input.rawLLMOutput);
|
|
43
|
-
const toolExecution = await this.executeTools(input
|
|
44
|
-
const
|
|
45
|
-
if (
|
|
93
|
+
const toolExecution = await this.executeTools(input);
|
|
94
|
+
const compactionOutcome = await compactionMessagePromise;
|
|
95
|
+
if (compactionOutcome.completed) {
|
|
46
96
|
await Promise.resolve(codeboltjs_1.default.chat.sendMessage('Conversation Compacted'));
|
|
47
97
|
}
|
|
48
98
|
this.completed = this.completed || toolExecution.completed;
|
|
49
99
|
this.finalMessage = (_a = toolExecution.finalMessage) !== null && _a !== void 0 ? _a : this.finalMessage;
|
|
50
100
|
await this.injectDiscoveredTools(input.rawLLMOutput, toolExecution.toolResults, nextMessage);
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
...toolExecution.followUpMessages,
|
|
61
|
-
]);
|
|
101
|
+
const toolResultMessages = this.buildToolResultMessages(toolExecution);
|
|
102
|
+
if (compactionOutcome.completed) {
|
|
103
|
+
const refreshedMessage = await this.refreshNextMessageFromCompactedContext(nextMessage, input.actualMessageSentToLLM, compactionOutcome, toolResultMessages);
|
|
104
|
+
nextMessage = refreshedMessage || (toolResultMessages.length > 0
|
|
105
|
+
? (0, promptContext_1.appendTranscriptMessages)(nextMessage, toolResultMessages)
|
|
106
|
+
: nextMessage);
|
|
107
|
+
}
|
|
108
|
+
else if (toolResultMessages.length > 0) {
|
|
109
|
+
nextMessage = (0, promptContext_1.appendTranscriptMessages)(nextMessage, toolResultMessages);
|
|
62
110
|
}
|
|
63
111
|
const transcriptLengthBeforePostToolProcessors = (0, promptContext_1.getTranscriptMessages)(nextMessage).length;
|
|
64
112
|
for (const postToolCallProcessor of this.postToolCallProcessors) {
|
|
@@ -87,6 +135,24 @@ class ResponseExecutor {
|
|
|
87
135
|
this.completed = false;
|
|
88
136
|
this.finalMessage = undefined;
|
|
89
137
|
}
|
|
138
|
+
if (this.completed) {
|
|
139
|
+
const pendingAsyncTasks = await this.getPendingAsyncTasks();
|
|
140
|
+
if (pendingAsyncTasks.length > 0) {
|
|
141
|
+
nextMessage = (0, promptContext_1.appendTranscriptMessages)(nextMessage, [{
|
|
142
|
+
role: 'user',
|
|
143
|
+
content: [
|
|
144
|
+
'<async_tasks_require_decision>',
|
|
145
|
+
'You attempted to complete while scoped async tasks owned by this run are still unresolved.',
|
|
146
|
+
'Before completing, use async_task_control on each task with action "wait", "stop", or "detach".',
|
|
147
|
+
'If a wait times out and the task is intentionally long-running, either stop it or detach it explicitly. background_detached tasks do not block completion and survive user force-stop.',
|
|
148
|
+
JSON.stringify(pendingAsyncTasks, null, 2),
|
|
149
|
+
'</async_tasks_require_decision>',
|
|
150
|
+
].join('\n'),
|
|
151
|
+
}]);
|
|
152
|
+
this.completed = false;
|
|
153
|
+
this.finalMessage = undefined;
|
|
154
|
+
}
|
|
155
|
+
}
|
|
90
156
|
const output = {
|
|
91
157
|
completed: this.completed,
|
|
92
158
|
nextMessage,
|
|
@@ -98,10 +164,12 @@ class ResponseExecutor {
|
|
|
98
164
|
return output;
|
|
99
165
|
}
|
|
100
166
|
parseToolCall(tool) {
|
|
167
|
+
var _a, _b, _c;
|
|
101
168
|
let toolInput = {};
|
|
102
|
-
|
|
169
|
+
const rawArguments = (_a = tool.arguments) !== null && _a !== void 0 ? _a : (_b = tool.function) === null || _b === void 0 ? void 0 : _b.arguments;
|
|
170
|
+
if (rawArguments) {
|
|
103
171
|
try {
|
|
104
|
-
const parsedArguments = JSON.parse(
|
|
172
|
+
const parsedArguments = typeof rawArguments === 'string' ? JSON.parse(rawArguments) : rawArguments;
|
|
105
173
|
if (parsedArguments && typeof parsedArguments === 'object' && !Array.isArray(parsedArguments)) {
|
|
106
174
|
toolInput = parsedArguments;
|
|
107
175
|
}
|
|
@@ -113,37 +181,32 @@ class ResponseExecutor {
|
|
|
113
181
|
return {
|
|
114
182
|
tool,
|
|
115
183
|
toolInput,
|
|
116
|
-
toolName: tool.function.name,
|
|
117
|
-
toolUseId: tool.id,
|
|
184
|
+
toolName: tool.name || ((_c = tool.function) === null || _c === void 0 ? void 0 : _c.name) || '',
|
|
185
|
+
toolUseId: tool.call_id || tool.id || '',
|
|
118
186
|
waitForPrevious: toolInput['waitForPreviousTools'] === true,
|
|
119
187
|
};
|
|
120
188
|
}
|
|
121
189
|
extractLastMessageContent(llmResponse) {
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
const reasoningContent = (_c = choice.message) === null || _c === void 0 ? void 0 : _c.reasoning_content;
|
|
128
|
-
if (reasoningContent) {
|
|
129
|
-
return reasoningContent;
|
|
130
|
-
}
|
|
190
|
+
if (llmResponse.output_text) {
|
|
191
|
+
return llmResponse.output_text;
|
|
192
|
+
}
|
|
193
|
+
if (llmResponse.content) {
|
|
194
|
+
return llmResponse.content;
|
|
131
195
|
}
|
|
132
196
|
return undefined;
|
|
133
197
|
}
|
|
134
198
|
getToolCalls(llmResponse) {
|
|
135
|
-
var _a, _b
|
|
199
|
+
var _a, _b;
|
|
136
200
|
const toolCallsById = new Map();
|
|
137
|
-
for (const
|
|
138
|
-
if (
|
|
139
|
-
toolCallsById.set(
|
|
201
|
+
for (const item of (_a = llmResponse.items) !== null && _a !== void 0 ? _a : []) {
|
|
202
|
+
if ((item === null || item === void 0 ? void 0 : item.type) === 'function_call' && item.call_id) {
|
|
203
|
+
toolCallsById.set(item.call_id, item);
|
|
140
204
|
}
|
|
141
205
|
}
|
|
142
|
-
for (const
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
}
|
|
206
|
+
for (const toolCall of (_b = llmResponse.tool_calls) !== null && _b !== void 0 ? _b : []) {
|
|
207
|
+
const id = (toolCall === null || toolCall === void 0 ? void 0 : toolCall.call_id) || (toolCall === null || toolCall === void 0 ? void 0 : toolCall.id);
|
|
208
|
+
if (id) {
|
|
209
|
+
toolCallsById.set(id, toolCall);
|
|
147
210
|
}
|
|
148
211
|
}
|
|
149
212
|
return Array.from(toolCallsById.values());
|
|
@@ -151,32 +214,124 @@ class ResponseExecutor {
|
|
|
151
214
|
hasExecutableToolCalls(llmResponse) {
|
|
152
215
|
return this.getToolCalls(llmResponse).some((toolCall) => {
|
|
153
216
|
var _a;
|
|
154
|
-
const toolName = ((_a = toolCall.function) === null || _a === void 0 ? void 0 : _a.name) || '';
|
|
217
|
+
const toolName = toolCall.name || ((_a = toolCall.function) === null || _a === void 0 ? void 0 : _a.name) || '';
|
|
155
218
|
return !toolName.includes('attempt_completion') &&
|
|
156
219
|
!toolName.includes('context_compaction') &&
|
|
157
220
|
!toolName.includes('contextCompaction');
|
|
158
221
|
});
|
|
159
222
|
}
|
|
160
223
|
async runRequiredCompaction(llmResponse) {
|
|
161
|
-
var _a;
|
|
224
|
+
var _a, _b;
|
|
162
225
|
const decision = llmResponse.contextCompaction;
|
|
163
226
|
if (!(decision === null || decision === void 0 ? void 0 : decision.required) || decision.hasToolCalls === false || !this.hasExecutableToolCalls(llmResponse)) {
|
|
164
|
-
return false;
|
|
227
|
+
return { completed: false };
|
|
165
228
|
}
|
|
166
229
|
try {
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
230
|
+
const runRequest = decision.runRequest || {};
|
|
231
|
+
const response = await codeboltjs_1.default.contextCompaction.run({
|
|
232
|
+
...runRequest,
|
|
233
|
+
reason: String(runRequest['reason'] || decision.reason || 'llm_response_tool_calls'),
|
|
170
234
|
});
|
|
171
|
-
|
|
235
|
+
const responseThreadId = (_b = (_a = response === null || response === void 0 ? void 0 : response.data) === null || _a === void 0 ? void 0 : _a.result) === null || _b === void 0 ? void 0 : _b.threadId;
|
|
236
|
+
const requestThreadId = runRequest['threadId'];
|
|
237
|
+
const threadId = typeof responseThreadId === 'string'
|
|
238
|
+
? responseThreadId
|
|
239
|
+
: typeof requestThreadId === 'string'
|
|
240
|
+
? requestThreadId
|
|
241
|
+
: undefined;
|
|
242
|
+
return {
|
|
243
|
+
completed: (response === null || response === void 0 ? void 0 : response.success) !== false,
|
|
244
|
+
...(threadId ? { threadId } : {}),
|
|
245
|
+
};
|
|
172
246
|
}
|
|
173
247
|
catch (error) {
|
|
174
248
|
console.error('[ResponseExecutor] Context compaction failed:', error);
|
|
249
|
+
return { completed: false };
|
|
250
|
+
}
|
|
251
|
+
}
|
|
252
|
+
buildToolResultMessages(toolExecution) {
|
|
253
|
+
return [
|
|
254
|
+
...toolExecution.toolResults.map((toolResult) => {
|
|
255
|
+
const content = toolResult.content;
|
|
256
|
+
if (content &&
|
|
257
|
+
typeof content === 'object' &&
|
|
258
|
+
!Array.isArray(content) &&
|
|
259
|
+
content.type === 'tool_search_output') {
|
|
260
|
+
return {
|
|
261
|
+
...content,
|
|
262
|
+
type: 'tool_search_output',
|
|
263
|
+
call_id: content.call_id || toolResult.tool_call_id,
|
|
264
|
+
status: content.status || 'completed',
|
|
265
|
+
};
|
|
266
|
+
}
|
|
267
|
+
return {
|
|
268
|
+
type: 'function_call_output',
|
|
269
|
+
call_id: toolResult.tool_call_id,
|
|
270
|
+
output: typeof content === 'string'
|
|
271
|
+
? content
|
|
272
|
+
: JSON.stringify(content),
|
|
273
|
+
status: 'completed',
|
|
274
|
+
};
|
|
275
|
+
}),
|
|
276
|
+
...toolExecution.followUpMessages,
|
|
277
|
+
];
|
|
278
|
+
}
|
|
279
|
+
async refreshNextMessageFromCompactedContext(nextMessage, actualMessageSentToLLM, compactionOutcome, toolResultMessages) {
|
|
280
|
+
if (!compactionOutcome.threadId) {
|
|
281
|
+
return null;
|
|
282
|
+
}
|
|
283
|
+
try {
|
|
284
|
+
const response = await codeboltjs_1.default.thread.getThreadContextCompacted({
|
|
285
|
+
threadId: compactionOutcome.threadId,
|
|
286
|
+
});
|
|
287
|
+
const compactedMessages = this.extractCompactedContextMessages(response === null || response === void 0 ? void 0 : response.compactedContext);
|
|
288
|
+
if (compactedMessages.length === 0) {
|
|
289
|
+
return null;
|
|
290
|
+
}
|
|
291
|
+
const currentTurnMessages = this.getMessagesAddedAfterInference(actualMessageSentToLLM, nextMessage);
|
|
292
|
+
return (0, promptContext_1.replaceTranscriptMessages)(nextMessage, [
|
|
293
|
+
...compactedMessages,
|
|
294
|
+
...currentTurnMessages,
|
|
295
|
+
...toolResultMessages,
|
|
296
|
+
]);
|
|
297
|
+
}
|
|
298
|
+
catch (error) {
|
|
299
|
+
console.error('[ResponseExecutor] Failed to refresh compacted context:', error);
|
|
300
|
+
return null;
|
|
301
|
+
}
|
|
302
|
+
}
|
|
303
|
+
extractCompactedContextMessages(compactedContext) {
|
|
304
|
+
const data = typeof compactedContext === 'object' && compactedContext !== null
|
|
305
|
+
? compactedContext.data
|
|
306
|
+
: undefined;
|
|
307
|
+
const messages = typeof data === 'object' && data !== null
|
|
308
|
+
? data.input
|
|
309
|
+
: undefined;
|
|
310
|
+
if (!Array.isArray(messages)) {
|
|
311
|
+
return [];
|
|
312
|
+
}
|
|
313
|
+
return messages
|
|
314
|
+
.filter((message) => this.isMessageObject(message))
|
|
315
|
+
.map((message) => ({ ...message }));
|
|
316
|
+
}
|
|
317
|
+
isMessageObject(value) {
|
|
318
|
+
if (!value || typeof value !== 'object') {
|
|
319
|
+
return false;
|
|
320
|
+
}
|
|
321
|
+
const message = value;
|
|
322
|
+
if (typeof message.role !== 'string') {
|
|
175
323
|
return false;
|
|
176
324
|
}
|
|
325
|
+
return message.content !== undefined;
|
|
177
326
|
}
|
|
178
|
-
|
|
327
|
+
getMessagesAddedAfterInference(actualMessageSentToLLM, nextMessage) {
|
|
328
|
+
const sentMessages = (0, promptContext_1.getTranscriptMessages)(actualMessageSentToLLM);
|
|
329
|
+
const nextMessages = (0, promptContext_1.getTranscriptMessages)(nextMessage);
|
|
330
|
+
return nextMessages.slice(sentMessages.length);
|
|
331
|
+
}
|
|
332
|
+
async executeTools(input) {
|
|
179
333
|
var _a;
|
|
334
|
+
const llmResponse = input.rawLLMOutput;
|
|
180
335
|
const lastMessageContent = this.extractLastMessageContent(llmResponse);
|
|
181
336
|
const toolCalls = this.getToolCalls(llmResponse);
|
|
182
337
|
if (toolCalls.length === 0) {
|
|
@@ -220,7 +375,7 @@ class ResponseExecutor {
|
|
|
220
375
|
toolResults.push(skippedResult);
|
|
221
376
|
continue;
|
|
222
377
|
}
|
|
223
|
-
const executionResult = await this.executeSingleToolCall(currentToolCall);
|
|
378
|
+
const executionResult = await this.executeSingleToolCall(currentToolCall, input);
|
|
224
379
|
toolResults.push(executionResult.toolResult);
|
|
225
380
|
followUpMessages.push(...executionResult.followUpMessages);
|
|
226
381
|
userRejectedToolUse = executionResult.didUserReject;
|
|
@@ -229,7 +384,7 @@ class ResponseExecutor {
|
|
|
229
384
|
const completionToolCall = completionToolCalls.at(-1);
|
|
230
385
|
if (completionToolCall) {
|
|
231
386
|
const completionArguments = completionToolCall.toolInput;
|
|
232
|
-
const [, completionResult] = await this.executeTool(completionToolCall.toolName, completionArguments);
|
|
387
|
+
const [, completionResult] = await this.executeTool(completionToolCall.toolName, completionArguments, input);
|
|
233
388
|
this.finalMessage = (_a = this.extractCompletionMessage(completionArguments)) !== null && _a !== void 0 ? _a : lastMessageContent;
|
|
234
389
|
await this.sendFinalMessageToChat(this.finalMessage);
|
|
235
390
|
const parsedCompletionResult = this.parseToolResult(completionToolCall.toolUseId, completionResult === '' ? 'The user is satisfied with the result.' : completionResult);
|
|
@@ -255,6 +410,21 @@ class ResponseExecutor {
|
|
|
255
410
|
console.error('[ResponseExecutor] Failed to send final chat message:', error);
|
|
256
411
|
}
|
|
257
412
|
}
|
|
413
|
+
async getPendingAsyncTasks() {
|
|
414
|
+
try {
|
|
415
|
+
const response = await Promise.resolve(codeboltjs_1.default.asyncTask.listTasks({
|
|
416
|
+
scope: 'current_run',
|
|
417
|
+
}));
|
|
418
|
+
const tasks = Array.isArray(response === null || response === void 0 ? void 0 : response.tasks) ? response.tasks : [];
|
|
419
|
+
return tasks.filter((task) => task &&
|
|
420
|
+
task.executionMode !== 'background_detached' &&
|
|
421
|
+
!['completed', 'failed', 'stopped'].includes(task.status));
|
|
422
|
+
}
|
|
423
|
+
catch (error) {
|
|
424
|
+
console.error('[ResponseExecutor] Failed to check async tasks before completion:', error);
|
|
425
|
+
return [];
|
|
426
|
+
}
|
|
427
|
+
}
|
|
258
428
|
extractCompletionMessage(toolInput) {
|
|
259
429
|
if ('result' in toolInput) {
|
|
260
430
|
return this.formatCompletionValue(toolInput['result']);
|
|
@@ -285,7 +455,7 @@ class ResponseExecutor {
|
|
|
285
455
|
return String(value);
|
|
286
456
|
}
|
|
287
457
|
}
|
|
288
|
-
async executeSingleToolCall(toolCall) {
|
|
458
|
+
async executeSingleToolCall(toolCall, input) {
|
|
289
459
|
try {
|
|
290
460
|
let resultTuple;
|
|
291
461
|
if (toolCall.toolName === 'codebolt--thread_management') {
|
|
@@ -309,7 +479,7 @@ class ResponseExecutor {
|
|
|
309
479
|
resultTuple = [false, 'tool result is successful'];
|
|
310
480
|
}
|
|
311
481
|
else {
|
|
312
|
-
resultTuple = await this.executeTool(toolCall.toolName, toolCall.toolInput);
|
|
482
|
+
resultTuple = await this.executeTool(toolCall.toolName, toolCall.toolInput, input);
|
|
313
483
|
}
|
|
314
484
|
const [didUserReject, result] = resultTuple;
|
|
315
485
|
const parsedResult = this.parseToolResult(toolCall.toolUseId, result);
|
|
@@ -334,12 +504,26 @@ class ResponseExecutor {
|
|
|
334
504
|
};
|
|
335
505
|
}
|
|
336
506
|
}
|
|
337
|
-
async executeTool(toolName, toolInput) {
|
|
338
|
-
var _a, _b, _c;
|
|
507
|
+
async executeTool(toolName, toolInput, input) {
|
|
508
|
+
var _a, _b, _c, _d;
|
|
339
509
|
const executionToolName = (0, agentToolLoader_1.resolveToolExecutionName)(toolName);
|
|
510
|
+
const localTool = this.localToolsByExecutionName.get(executionToolName);
|
|
511
|
+
if (localTool) {
|
|
512
|
+
const localResult = await localTool.execute(toolInput, {
|
|
513
|
+
initialUserMessage: input.initialUserMessage,
|
|
514
|
+
llmMessageSent: input.actualMessageSentToLLM,
|
|
515
|
+
rawLLMResponse: input.rawLLMOutput,
|
|
516
|
+
nextMessage: input.nextMessage,
|
|
517
|
+
toolName: executionToolName,
|
|
518
|
+
});
|
|
519
|
+
if (!localResult.success) {
|
|
520
|
+
return [false, localResult.error || `Local tool "${executionToolName}" failed.`];
|
|
521
|
+
}
|
|
522
|
+
return [false, (_a = localResult.result) !== null && _a !== void 0 ? _a : ''];
|
|
523
|
+
}
|
|
340
524
|
const parts = executionToolName.split('--');
|
|
341
|
-
const toolboxName = parts.length > 1 ? ((
|
|
342
|
-
const actualToolName = parts.length > 1 ? ((
|
|
525
|
+
const toolboxName = parts.length > 1 ? ((_b = parts[0]) !== null && _b !== void 0 ? _b : '') : 'codebolt';
|
|
526
|
+
const actualToolName = parts.length > 1 ? ((_c = parts[1]) !== null && _c !== void 0 ? _c : '') : ((_d = parts[0]) !== null && _d !== void 0 ? _d : '');
|
|
343
527
|
const { data } = await codeboltjs_1.default.mcp.executeTool(toolboxName, actualToolName, toolInput);
|
|
344
528
|
if (Array.isArray(data) && data.length >= 2) {
|
|
345
529
|
const [didUserReject, content] = data;
|
|
@@ -348,6 +532,7 @@ class ResponseExecutor {
|
|
|
348
532
|
return [false, data];
|
|
349
533
|
}
|
|
350
534
|
parseToolResult(tool_call_id, content) {
|
|
535
|
+
let parsedStructuredContent = content;
|
|
351
536
|
let serializedContent = typeof content === 'string'
|
|
352
537
|
? content
|
|
353
538
|
: JSON.stringify(content);
|
|
@@ -356,6 +541,7 @@ class ResponseExecutor {
|
|
|
356
541
|
const parsedContent = typeof serializedContent === 'string'
|
|
357
542
|
? JSON.parse(serializedContent)
|
|
358
543
|
: serializedContent;
|
|
544
|
+
parsedStructuredContent = parsedContent;
|
|
359
545
|
if (parsedContent &&
|
|
360
546
|
typeof parsedContent === 'object' &&
|
|
361
547
|
'payload' in parsedContent &&
|
|
@@ -370,10 +556,16 @@ class ResponseExecutor {
|
|
|
370
556
|
catch {
|
|
371
557
|
// Preserve the raw tool result when it is not JSON.
|
|
372
558
|
}
|
|
559
|
+
const contentForModel = (parsedStructuredContent &&
|
|
560
|
+
typeof parsedStructuredContent === 'object' &&
|
|
561
|
+
!Array.isArray(parsedStructuredContent) &&
|
|
562
|
+
parsedStructuredContent.type === 'tool_search_output')
|
|
563
|
+
? parsedStructuredContent
|
|
564
|
+
: serializedContent;
|
|
373
565
|
return {
|
|
374
566
|
role: 'tool',
|
|
375
567
|
tool_call_id,
|
|
376
|
-
content:
|
|
568
|
+
content: contentForModel,
|
|
377
569
|
userMessage,
|
|
378
570
|
};
|
|
379
571
|
}
|
|
@@ -398,7 +590,7 @@ class ResponseExecutor {
|
|
|
398
590
|
for (const toolCall of toolCalls) {
|
|
399
591
|
if (!toolCall)
|
|
400
592
|
continue;
|
|
401
|
-
const toolName = ((_b = toolCall.function) === null || _b === void 0 ? void 0 : _b.name) || '';
|
|
593
|
+
const toolName = toolCall.name || ((_b = toolCall.function) === null || _b === void 0 ? void 0 : _b.name) || '';
|
|
402
594
|
const isToolSearch = toolName === 'tool_search' ||
|
|
403
595
|
toolName === 'codebolt--tool_search' ||
|
|
404
596
|
toolName.endsWith('--tool_search') ||
|
|
@@ -415,20 +607,8 @@ class ResponseExecutor {
|
|
|
415
607
|
const toolResult = toolResults.find(r => r.tool_call_id === toolCallId);
|
|
416
608
|
if (!(toolResult === null || toolResult === void 0 ? void 0 : toolResult.content))
|
|
417
609
|
continue;
|
|
418
|
-
const
|
|
419
|
-
|
|
420
|
-
: JSON.stringify(toolResult.content);
|
|
421
|
-
const jsonMatch = content.match(/\[[\s\S]*\]/);
|
|
422
|
-
if (!jsonMatch)
|
|
423
|
-
continue;
|
|
424
|
-
let discoveredSchemas;
|
|
425
|
-
try {
|
|
426
|
-
discoveredSchemas = JSON.parse(jsonMatch[0]);
|
|
427
|
-
}
|
|
428
|
-
catch {
|
|
429
|
-
continue;
|
|
430
|
-
}
|
|
431
|
-
if (!Array.isArray(discoveredSchemas))
|
|
610
|
+
const discoveredSchemas = extractDiscoveredToolSchemas(toolResult.content);
|
|
611
|
+
if (!discoveredSchemas.length)
|
|
432
612
|
continue;
|
|
433
613
|
const existingToolNames = new Set(nextMessage.message.tools.map((tool) => { var _a; return (_a = tool.function) === null || _a === void 0 ? void 0 : _a.name; }));
|
|
434
614
|
for (const schema of discoveredSchemas) {
|
package/dist/unified/index.d.ts
CHANGED
|
@@ -16,8 +16,7 @@ export { AgentStep } from './base/agentStep';
|
|
|
16
16
|
export { ResponseExecutor } from './base/responseExecutor';
|
|
17
17
|
export { LoopDetectionService, LoopType } from './services/LoopDetectionService';
|
|
18
18
|
export { CompressionCoordinator, type CompressionCoordinatorOptions, type CompressionDecision, type CompressionMetadata, type CompressionRecoveryResult, type CompressionStage, } from './services/CompressionCoordinator';
|
|
19
|
-
export { Agent } from './agent/agent';
|
|
20
|
-
export { CodeboltAgent, createCodeboltAgent, type CodeboltAgentConfig } from './agent/codeboltAgent';
|
|
19
|
+
export { Agent, createAgent, type AgentOptions, type AgentRunResult, type AgentRunOptions, type AgentRunState, type CreateAgentOptions } from './agent/agent';
|
|
21
20
|
export { Tool, createTool } from './agent/tools';
|
|
22
21
|
export { Workflow } from './agent/workflow';
|
|
23
22
|
export { type OpenAIMessage, type OpenAITool, type ToolResult, type CodeboltAPI, type AgentExecutionResult, type StreamChunk, type StreamCallback } from './types/libTypes';
|
package/dist/unified/index.js
CHANGED
|
@@ -10,7 +10,7 @@
|
|
|
10
10
|
* The framework is designed to be modular, extensible, and easy to use.
|
|
11
11
|
*/
|
|
12
12
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
13
|
-
exports.TokenEstimator = exports.PostCompactCleanup = exports.ReactiveCompact = exports.AutoCompact = exports.ContextCollapse = exports.MicroCompact = exports.SnipCompact = exports.CompactionOrchestrator = exports.Workflow = exports.createTool = exports.Tool = exports.
|
|
13
|
+
exports.TokenEstimator = exports.PostCompactCleanup = exports.ReactiveCompact = exports.AutoCompact = exports.ContextCollapse = exports.MicroCompact = exports.SnipCompact = exports.CompactionOrchestrator = exports.Workflow = exports.createTool = exports.Tool = exports.createAgent = exports.Agent = exports.CompressionCoordinator = exports.LoopType = exports.LoopDetectionService = exports.ResponseExecutor = exports.AgentStep = exports.InitialPromptGenerator = exports.createDefaultMessageProcessor = exports.UnifiedToolExecutionError = exports.UnifiedResponseExecutionError = exports.UnifiedStepExecutionError = exports.UnifiedMessageProcessingError = exports.UnifiedAgentError = void 0;
|
|
14
14
|
// Error types
|
|
15
15
|
var types_1 = require("./types/types");
|
|
16
16
|
Object.defineProperty(exports, "UnifiedAgentError", { enumerable: true, get: function () { return types_1.UnifiedAgentError; } });
|
|
@@ -35,9 +35,7 @@ Object.defineProperty(exports, "CompressionCoordinator", { enumerable: true, get
|
|
|
35
35
|
// Agent framework components
|
|
36
36
|
var agent_1 = require("./agent/agent");
|
|
37
37
|
Object.defineProperty(exports, "Agent", { enumerable: true, get: function () { return agent_1.Agent; } });
|
|
38
|
-
|
|
39
|
-
Object.defineProperty(exports, "CodeboltAgent", { enumerable: true, get: function () { return codeboltAgent_1.CodeboltAgent; } });
|
|
40
|
-
Object.defineProperty(exports, "createCodeboltAgent", { enumerable: true, get: function () { return codeboltAgent_1.createCodeboltAgent; } });
|
|
38
|
+
Object.defineProperty(exports, "createAgent", { enumerable: true, get: function () { return agent_1.createAgent; } });
|
|
41
39
|
var tools_1 = require("./agent/tools");
|
|
42
40
|
Object.defineProperty(exports, "Tool", { enumerable: true, get: function () { return tools_1.Tool; } });
|
|
43
41
|
Object.defineProperty(exports, "createTool", { enumerable: true, get: function () { return tools_1.createTool; } });
|
|
@@ -37,7 +37,7 @@ class CompressionCoordinator {
|
|
|
37
37
|
this.conversationCompactor = new processor_pieces_1.ConversationCompactorModifier(conversationCompactorOptions);
|
|
38
38
|
}
|
|
39
39
|
shouldCompressBeforeInference(message) {
|
|
40
|
-
const estimatedTokens = this.countMessageTokens(message.message.
|
|
40
|
+
const estimatedTokens = this.countMessageTokens(message.message.input);
|
|
41
41
|
const threshold = this.options.proactiveThreshold * this.options.modelTokenLimit;
|
|
42
42
|
if (!this.options.enabled) {
|
|
43
43
|
return {
|
|
@@ -92,8 +92,8 @@ class CompressionCoordinator {
|
|
|
92
92
|
recoveredMessage: this.withCompressionMetadata(message, {
|
|
93
93
|
status: 'FAILED_REACTIVE_RETRY_LIMIT',
|
|
94
94
|
stage: 'reactive_force_compact',
|
|
95
|
-
originalTokenCount: this.countMessageTokens(message.message.
|
|
96
|
-
newTokenCount: this.countMessageTokens(message.message.
|
|
95
|
+
originalTokenCount: this.countMessageTokens(message.message.input),
|
|
96
|
+
newTokenCount: this.countMessageTokens(message.message.input),
|
|
97
97
|
timestamp: new Date().toISOString(),
|
|
98
98
|
strategy: this.options.compactStrategy,
|
|
99
99
|
reactiveRetryCount: this.reactiveRetryCount,
|
|
@@ -104,7 +104,7 @@ class CompressionCoordinator {
|
|
|
104
104
|
};
|
|
105
105
|
}
|
|
106
106
|
this.reactiveRetryCount++;
|
|
107
|
-
const proactiveCompression = await this.chatCompression.tryCompressChat(message.message.
|
|
107
|
+
const proactiveCompression = await this.chatCompression.tryCompressChat(message.message.input, true);
|
|
108
108
|
if (proactiveCompression.compressedMessages &&
|
|
109
109
|
proactiveCompression.compressionStatus === 1) {
|
|
110
110
|
return {
|
|
@@ -112,7 +112,7 @@ class CompressionCoordinator {
|
|
|
112
112
|
...message,
|
|
113
113
|
message: {
|
|
114
114
|
...message.message,
|
|
115
|
-
|
|
115
|
+
input: proactiveCompression.compressedMessages,
|
|
116
116
|
},
|
|
117
117
|
}, {
|
|
118
118
|
status: 'COMPRESSED',
|
|
@@ -128,19 +128,19 @@ class CompressionCoordinator {
|
|
|
128
128
|
reason: 'Reactive pre-inference compression applied',
|
|
129
129
|
};
|
|
130
130
|
}
|
|
131
|
-
const forceCompressed = await this.conversationCompactor.forceCompress(message.message.
|
|
131
|
+
const forceCompressed = await this.conversationCompactor.forceCompress(message.message.input);
|
|
132
132
|
return {
|
|
133
133
|
recoveredMessage: this.withCompressionMetadata({
|
|
134
134
|
...message,
|
|
135
135
|
message: {
|
|
136
136
|
...message.message,
|
|
137
|
-
|
|
137
|
+
input: forceCompressed.input,
|
|
138
138
|
},
|
|
139
139
|
}, {
|
|
140
140
|
status: 'COMPRESSED',
|
|
141
141
|
stage: 'reactive_force_compact',
|
|
142
|
-
originalTokenCount: this.countMessageTokens(message.message.
|
|
143
|
-
newTokenCount: this.countMessageTokens(forceCompressed.
|
|
142
|
+
originalTokenCount: this.countMessageTokens(message.message.input),
|
|
143
|
+
newTokenCount: this.countMessageTokens(forceCompressed.input),
|
|
144
144
|
timestamp: new Date().toISOString(),
|
|
145
145
|
strategy: this.options.compactStrategy,
|
|
146
146
|
...(forceCompressed.metadata.messagesCompressed !==
|
|
@@ -196,7 +196,7 @@ class AutoCompact {
|
|
|
196
196
|
return splitIndex;
|
|
197
197
|
}
|
|
198
198
|
async generateSummary(messages) {
|
|
199
|
-
var _a, _b
|
|
199
|
+
var _a, _b;
|
|
200
200
|
try {
|
|
201
201
|
// eslint-disable-next-line @typescript-eslint/no-require-imports
|
|
202
202
|
const codebolt = require('@codebolt/codeboltjs');
|
|
@@ -266,11 +266,11 @@ CRITICAL RULES:
|
|
|
266
266
|
});
|
|
267
267
|
// Extract summary from response
|
|
268
268
|
let summary;
|
|
269
|
-
if (typeof ((_a = response === null || response === void 0 ? void 0 : response.completion) === null || _a === void 0 ? void 0 : _a.
|
|
270
|
-
summary = response.completion.
|
|
269
|
+
if (typeof ((_a = response === null || response === void 0 ? void 0 : response.completion) === null || _a === void 0 ? void 0 : _a.output_text) === 'string') {
|
|
270
|
+
summary = response.completion.output_text;
|
|
271
271
|
}
|
|
272
|
-
else if (
|
|
273
|
-
summary = response.completion.
|
|
272
|
+
else if (typeof ((_b = response === null || response === void 0 ? void 0 : response.completion) === null || _b === void 0 ? void 0 : _b.content) === 'string') {
|
|
273
|
+
summary = response.completion.content;
|
|
274
274
|
}
|
|
275
275
|
if (summary && summary.trim().length > 0) {
|
|
276
276
|
return summary.trim();
|
|
@@ -213,7 +213,7 @@ class ContextCollapse {
|
|
|
213
213
|
];
|
|
214
214
|
}
|
|
215
215
|
async generateGranularSummary(messages) {
|
|
216
|
-
var _a, _b
|
|
216
|
+
var _a, _b;
|
|
217
217
|
const estimator = new types_1.TokenEstimator();
|
|
218
218
|
// For small ranges, create a structural summary without LLM
|
|
219
219
|
const tokenCount = estimator.estimateForMessages(messages);
|
|
@@ -244,7 +244,7 @@ ${historyText}`;
|
|
|
244
244
|
{ role: 'user', content: prompt },
|
|
245
245
|
],
|
|
246
246
|
});
|
|
247
|
-
const summary = ((_a = response === null || response === void 0 ? void 0 : response.completion) === null || _a === void 0 ? void 0 : _a.
|
|
247
|
+
const summary = ((_a = response === null || response === void 0 ? void 0 : response.completion) === null || _a === void 0 ? void 0 : _a.output_text) || ((_b = response === null || response === void 0 ? void 0 : response.completion) === null || _b === void 0 ? void 0 : _b.content);
|
|
248
248
|
if (summary && typeof summary === 'string' && summary.trim().length > 0) {
|
|
249
249
|
return summary.trim();
|
|
250
250
|
}
|
|
@@ -235,7 +235,7 @@ class ReactiveCompact {
|
|
|
235
235
|
];
|
|
236
236
|
}
|
|
237
237
|
async forceCompact(messages) {
|
|
238
|
-
var _a, _b
|
|
238
|
+
var _a, _b;
|
|
239
239
|
try {
|
|
240
240
|
// eslint-disable-next-line @typescript-eslint/no-require-imports
|
|
241
241
|
const codebolt = require('@codebolt/codeboltjs');
|
|
@@ -276,11 +276,11 @@ ${historyText}`;
|
|
|
276
276
|
],
|
|
277
277
|
});
|
|
278
278
|
let summary;
|
|
279
|
-
if (typeof ((_a = response === null || response === void 0 ? void 0 : response.completion) === null || _a === void 0 ? void 0 : _a.
|
|
280
|
-
summary = response.completion.
|
|
279
|
+
if (typeof ((_a = response === null || response === void 0 ? void 0 : response.completion) === null || _a === void 0 ? void 0 : _a.output_text) === 'string') {
|
|
280
|
+
summary = response.completion.output_text;
|
|
281
281
|
}
|
|
282
|
-
else if (
|
|
283
|
-
summary = response.completion.
|
|
282
|
+
else if (typeof ((_b = response === null || response === void 0 ? void 0 : response.completion) === null || _b === void 0 ? void 0 : _b.content) === 'string') {
|
|
283
|
+
summary = response.completion.content;
|
|
284
284
|
}
|
|
285
285
|
if (!summary || summary.trim().length === 0) {
|
|
286
286
|
return null;
|