@codebolt/agent 6.1.20 → 6.1.22
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 +1 -0
- 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 +8 -0
- package/dist/processor-pieces/messageModifiers/chatHistoryMessageModifier.js +150 -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/index.d.ts +1 -0
- package/dist/processor-pieces/messageModifiers/index.js +3 -1
- package/dist/processor-pieces/messageModifiers/memoryImportModifier.js +9 -15
- package/dist/processor-pieces/messageModifiers/toolInjectionModifier.js +17 -20
- package/dist/processor-pieces/messageModifiers/toolManifestPromptModifier.d.ts +18 -0
- package/dist/processor-pieces/messageModifiers/toolManifestPromptModifier.js +57 -0
- 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 +28 -20
- 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 +139 -7
- package/dist/unified/agent/agent.d.ts +13 -0
- package/dist/unified/agent/agent.js +180 -19
- package/dist/unified/agent/tools.d.ts +14 -0
- package/dist/unified/agent/tools.js +82 -51
- package/dist/unified/base/agentStep.d.ts +2 -1
- package/dist/unified/base/agentStep.js +47 -16
- package/dist/unified/base/initialPromptGenerator.d.ts +2 -0
- package/dist/unified/base/initialPromptGenerator.js +42 -19
- package/dist/unified/base/promptContext.d.ts +3 -0
- package/dist/unified/base/promptContext.js +193 -15
- package/dist/unified/base/responseExecutor.d.ts +6 -0
- package/dist/unified/base/responseExecutor.js +256 -69
- package/dist/unified/index.d.ts +1 -0
- package/dist/unified/index.js +3 -1
- package/dist/unified/services/CompressionCoordinator.js +9 -9
- package/dist/unified/services/compaction/autoCompact.d.ts +2 -1
- package/dist/unified/services/compaction/autoCompact.js +19 -11
- package/dist/unified/services/compaction/compactionOrchestrator.d.ts +1 -0
- package/dist/unified/services/compaction/compactionOrchestrator.js +8 -0
- package/dist/unified/services/compaction/contextCollapse.d.ts +2 -1
- package/dist/unified/services/compaction/contextCollapse.js +17 -9
- package/dist/unified/services/compaction/reactiveCompact.d.ts +3 -0
- package/dist/unified/services/compaction/reactiveCompact.js +17 -8
- package/dist/unified/types/libTypes.d.ts +189 -10
- package/dist/unified/utils/agentToolLoader.d.ts +17 -1
- package/dist/unified/utils/agentToolLoader.js +182 -31
- package/package.json +5 -1
|
@@ -7,6 +7,75 @@ 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
|
+
}
|
|
59
|
+
function isToolSearchOutput(content) {
|
|
60
|
+
return !!content &&
|
|
61
|
+
typeof content === 'object' &&
|
|
62
|
+
!Array.isArray(content) &&
|
|
63
|
+
content.type === 'tool_search_output';
|
|
64
|
+
}
|
|
65
|
+
function normalizeToolSearchOutput(content, toolCallId) {
|
|
66
|
+
const contentCallId = content['call_id'];
|
|
67
|
+
const contentStatus = content['status'];
|
|
68
|
+
return {
|
|
69
|
+
...content,
|
|
70
|
+
type: 'tool_search_output',
|
|
71
|
+
call_id: typeof contentCallId === 'string' && contentCallId.length > 0
|
|
72
|
+
? contentCallId
|
|
73
|
+
: toolCallId,
|
|
74
|
+
status: typeof contentStatus === 'string' && contentStatus.length > 0
|
|
75
|
+
? contentStatus
|
|
76
|
+
: 'completed',
|
|
77
|
+
};
|
|
78
|
+
}
|
|
10
79
|
class ResponseExecutor {
|
|
11
80
|
constructor(options) {
|
|
12
81
|
this.preToolCallProcessors = [];
|
|
@@ -42,24 +111,22 @@ class ResponseExecutor {
|
|
|
42
111
|
}
|
|
43
112
|
const compactionMessagePromise = this.runRequiredCompaction(input.rawLLMOutput);
|
|
44
113
|
const toolExecution = await this.executeTools(input);
|
|
45
|
-
const
|
|
46
|
-
if (
|
|
114
|
+
const compactionOutcome = await compactionMessagePromise;
|
|
115
|
+
if (compactionOutcome.completed) {
|
|
47
116
|
await Promise.resolve(codeboltjs_1.default.chat.sendMessage('Conversation Compacted'));
|
|
48
117
|
}
|
|
49
118
|
this.completed = this.completed || toolExecution.completed;
|
|
50
119
|
this.finalMessage = (_a = toolExecution.finalMessage) !== null && _a !== void 0 ? _a : this.finalMessage;
|
|
51
120
|
await this.injectDiscoveredTools(input.rawLLMOutput, toolExecution.toolResults, nextMessage);
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
...toolExecution.followUpMessages,
|
|
62
|
-
]);
|
|
121
|
+
const toolResultMessages = this.buildToolResultMessages(toolExecution);
|
|
122
|
+
if (compactionOutcome.completed) {
|
|
123
|
+
const refreshedMessage = await this.refreshNextMessageFromCompactedContext(nextMessage, input.actualMessageSentToLLM, compactionOutcome, toolResultMessages);
|
|
124
|
+
nextMessage = refreshedMessage || (toolResultMessages.length > 0
|
|
125
|
+
? (0, promptContext_1.appendTranscriptMessages)(nextMessage, toolResultMessages)
|
|
126
|
+
: nextMessage);
|
|
127
|
+
}
|
|
128
|
+
else if (toolResultMessages.length > 0) {
|
|
129
|
+
nextMessage = (0, promptContext_1.appendTranscriptMessages)(nextMessage, toolResultMessages);
|
|
63
130
|
}
|
|
64
131
|
const transcriptLengthBeforePostToolProcessors = (0, promptContext_1.getTranscriptMessages)(nextMessage).length;
|
|
65
132
|
for (const postToolCallProcessor of this.postToolCallProcessors) {
|
|
@@ -88,6 +155,24 @@ class ResponseExecutor {
|
|
|
88
155
|
this.completed = false;
|
|
89
156
|
this.finalMessage = undefined;
|
|
90
157
|
}
|
|
158
|
+
if (this.completed) {
|
|
159
|
+
const pendingAsyncTasks = await this.getPendingAsyncTasks();
|
|
160
|
+
if (pendingAsyncTasks.length > 0) {
|
|
161
|
+
nextMessage = (0, promptContext_1.appendTranscriptMessages)(nextMessage, [{
|
|
162
|
+
role: 'user',
|
|
163
|
+
content: [
|
|
164
|
+
'<async_tasks_require_decision>',
|
|
165
|
+
'You attempted to complete while scoped async tasks owned by this run are still unresolved.',
|
|
166
|
+
'Before completing, use async_task_control on each task with action "wait", "stop", or "detach".',
|
|
167
|
+
'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.',
|
|
168
|
+
JSON.stringify(pendingAsyncTasks, null, 2),
|
|
169
|
+
'</async_tasks_require_decision>',
|
|
170
|
+
].join('\n'),
|
|
171
|
+
}]);
|
|
172
|
+
this.completed = false;
|
|
173
|
+
this.finalMessage = undefined;
|
|
174
|
+
}
|
|
175
|
+
}
|
|
91
176
|
const output = {
|
|
92
177
|
completed: this.completed,
|
|
93
178
|
nextMessage,
|
|
@@ -99,10 +184,12 @@ class ResponseExecutor {
|
|
|
99
184
|
return output;
|
|
100
185
|
}
|
|
101
186
|
parseToolCall(tool) {
|
|
187
|
+
var _a, _b, _c;
|
|
102
188
|
let toolInput = {};
|
|
103
|
-
|
|
189
|
+
const rawArguments = (_a = tool.arguments) !== null && _a !== void 0 ? _a : (_b = tool.function) === null || _b === void 0 ? void 0 : _b.arguments;
|
|
190
|
+
if (rawArguments) {
|
|
104
191
|
try {
|
|
105
|
-
const parsedArguments = JSON.parse(
|
|
192
|
+
const parsedArguments = typeof rawArguments === 'string' ? JSON.parse(rawArguments) : rawArguments;
|
|
106
193
|
if (parsedArguments && typeof parsedArguments === 'object' && !Array.isArray(parsedArguments)) {
|
|
107
194
|
toolInput = parsedArguments;
|
|
108
195
|
}
|
|
@@ -114,37 +201,32 @@ class ResponseExecutor {
|
|
|
114
201
|
return {
|
|
115
202
|
tool,
|
|
116
203
|
toolInput,
|
|
117
|
-
toolName: tool.function.name,
|
|
118
|
-
toolUseId: tool.id,
|
|
204
|
+
toolName: tool.name || ((_c = tool.function) === null || _c === void 0 ? void 0 : _c.name) || '',
|
|
205
|
+
toolUseId: tool.call_id || tool.id || '',
|
|
119
206
|
waitForPrevious: toolInput['waitForPreviousTools'] === true,
|
|
120
207
|
};
|
|
121
208
|
}
|
|
122
209
|
extractLastMessageContent(llmResponse) {
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
const reasoningContent = (_c = choice.message) === null || _c === void 0 ? void 0 : _c.reasoning_content;
|
|
129
|
-
if (reasoningContent) {
|
|
130
|
-
return reasoningContent;
|
|
131
|
-
}
|
|
210
|
+
if (llmResponse.output_text) {
|
|
211
|
+
return llmResponse.output_text;
|
|
212
|
+
}
|
|
213
|
+
if (llmResponse.content) {
|
|
214
|
+
return llmResponse.content;
|
|
132
215
|
}
|
|
133
216
|
return undefined;
|
|
134
217
|
}
|
|
135
218
|
getToolCalls(llmResponse) {
|
|
136
|
-
var _a, _b
|
|
219
|
+
var _a, _b;
|
|
137
220
|
const toolCallsById = new Map();
|
|
138
|
-
for (const
|
|
139
|
-
if (
|
|
140
|
-
toolCallsById.set(
|
|
221
|
+
for (const item of (_a = llmResponse.items) !== null && _a !== void 0 ? _a : []) {
|
|
222
|
+
if ((item === null || item === void 0 ? void 0 : item.type) === 'function_call' && item.call_id) {
|
|
223
|
+
toolCallsById.set(item.call_id, item);
|
|
141
224
|
}
|
|
142
225
|
}
|
|
143
|
-
for (const
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
}
|
|
226
|
+
for (const toolCall of (_b = llmResponse.tool_calls) !== null && _b !== void 0 ? _b : []) {
|
|
227
|
+
const id = (toolCall === null || toolCall === void 0 ? void 0 : toolCall.call_id) || (toolCall === null || toolCall === void 0 ? void 0 : toolCall.id);
|
|
228
|
+
if (id) {
|
|
229
|
+
toolCallsById.set(id, toolCall);
|
|
148
230
|
}
|
|
149
231
|
}
|
|
150
232
|
return Array.from(toolCallsById.values());
|
|
@@ -152,29 +234,112 @@ class ResponseExecutor {
|
|
|
152
234
|
hasExecutableToolCalls(llmResponse) {
|
|
153
235
|
return this.getToolCalls(llmResponse).some((toolCall) => {
|
|
154
236
|
var _a;
|
|
155
|
-
const toolName = ((_a = toolCall.function) === null || _a === void 0 ? void 0 : _a.name) || '';
|
|
237
|
+
const toolName = toolCall.name || ((_a = toolCall.function) === null || _a === void 0 ? void 0 : _a.name) || '';
|
|
156
238
|
return !toolName.includes('attempt_completion') &&
|
|
157
239
|
!toolName.includes('context_compaction') &&
|
|
158
240
|
!toolName.includes('contextCompaction');
|
|
159
241
|
});
|
|
160
242
|
}
|
|
161
243
|
async runRequiredCompaction(llmResponse) {
|
|
162
|
-
var _a;
|
|
244
|
+
var _a, _b;
|
|
163
245
|
const decision = llmResponse.contextCompaction;
|
|
164
246
|
if (!(decision === null || decision === void 0 ? void 0 : decision.required) || decision.hasToolCalls === false || !this.hasExecutableToolCalls(llmResponse)) {
|
|
165
|
-
return false;
|
|
247
|
+
return { completed: false };
|
|
166
248
|
}
|
|
167
249
|
try {
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
250
|
+
const runRequest = decision.runRequest || {};
|
|
251
|
+
const response = await codeboltjs_1.default.contextCompaction.run({
|
|
252
|
+
...runRequest,
|
|
253
|
+
reason: String(runRequest['reason'] || decision.reason || 'llm_response_tool_calls'),
|
|
171
254
|
});
|
|
172
|
-
|
|
255
|
+
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;
|
|
256
|
+
const requestThreadId = runRequest['threadId'];
|
|
257
|
+
const threadId = typeof responseThreadId === 'string'
|
|
258
|
+
? responseThreadId
|
|
259
|
+
: typeof requestThreadId === 'string'
|
|
260
|
+
? requestThreadId
|
|
261
|
+
: undefined;
|
|
262
|
+
return {
|
|
263
|
+
completed: (response === null || response === void 0 ? void 0 : response.success) !== false,
|
|
264
|
+
...(threadId ? { threadId } : {}),
|
|
265
|
+
};
|
|
173
266
|
}
|
|
174
267
|
catch (error) {
|
|
175
268
|
console.error('[ResponseExecutor] Context compaction failed:', error);
|
|
269
|
+
return { completed: false };
|
|
270
|
+
}
|
|
271
|
+
}
|
|
272
|
+
buildToolResultMessages(toolExecution) {
|
|
273
|
+
return [
|
|
274
|
+
...toolExecution.toolResults.map((toolResult) => {
|
|
275
|
+
const content = toolResult.content;
|
|
276
|
+
if (isToolSearchOutput(content)) {
|
|
277
|
+
return normalizeToolSearchOutput(content, toolResult.tool_call_id);
|
|
278
|
+
}
|
|
279
|
+
return {
|
|
280
|
+
type: 'function_call_output',
|
|
281
|
+
call_id: toolResult.tool_call_id,
|
|
282
|
+
output: typeof content === 'string'
|
|
283
|
+
? content
|
|
284
|
+
: JSON.stringify(content),
|
|
285
|
+
status: 'completed',
|
|
286
|
+
};
|
|
287
|
+
}),
|
|
288
|
+
...toolExecution.followUpMessages,
|
|
289
|
+
];
|
|
290
|
+
}
|
|
291
|
+
async refreshNextMessageFromCompactedContext(nextMessage, actualMessageSentToLLM, compactionOutcome, toolResultMessages) {
|
|
292
|
+
if (!compactionOutcome.threadId) {
|
|
293
|
+
return null;
|
|
294
|
+
}
|
|
295
|
+
try {
|
|
296
|
+
const response = await codeboltjs_1.default.thread.getThreadContextCompacted({
|
|
297
|
+
threadId: compactionOutcome.threadId,
|
|
298
|
+
});
|
|
299
|
+
const compactedMessages = this.extractCompactedContextMessages(response === null || response === void 0 ? void 0 : response.compactedContext);
|
|
300
|
+
if (compactedMessages.length === 0) {
|
|
301
|
+
return null;
|
|
302
|
+
}
|
|
303
|
+
const currentTurnMessages = this.getMessagesAddedAfterInference(actualMessageSentToLLM, nextMessage);
|
|
304
|
+
return (0, promptContext_1.replaceTranscriptMessages)(nextMessage, [
|
|
305
|
+
...compactedMessages,
|
|
306
|
+
...currentTurnMessages,
|
|
307
|
+
...toolResultMessages,
|
|
308
|
+
]);
|
|
309
|
+
}
|
|
310
|
+
catch (error) {
|
|
311
|
+
console.error('[ResponseExecutor] Failed to refresh compacted context:', error);
|
|
312
|
+
return null;
|
|
313
|
+
}
|
|
314
|
+
}
|
|
315
|
+
extractCompactedContextMessages(compactedContext) {
|
|
316
|
+
const data = typeof compactedContext === 'object' && compactedContext !== null
|
|
317
|
+
? compactedContext.data
|
|
318
|
+
: undefined;
|
|
319
|
+
const messages = typeof data === 'object' && data !== null
|
|
320
|
+
? data.input
|
|
321
|
+
: undefined;
|
|
322
|
+
if (!Array.isArray(messages)) {
|
|
323
|
+
return [];
|
|
324
|
+
}
|
|
325
|
+
return messages
|
|
326
|
+
.filter((message) => this.isMessageObject(message))
|
|
327
|
+
.map((message) => ({ ...message }));
|
|
328
|
+
}
|
|
329
|
+
isMessageObject(value) {
|
|
330
|
+
if (!value || typeof value !== 'object') {
|
|
176
331
|
return false;
|
|
177
332
|
}
|
|
333
|
+
const message = value;
|
|
334
|
+
if (typeof message.role !== 'string') {
|
|
335
|
+
return false;
|
|
336
|
+
}
|
|
337
|
+
return message.content !== undefined;
|
|
338
|
+
}
|
|
339
|
+
getMessagesAddedAfterInference(actualMessageSentToLLM, nextMessage) {
|
|
340
|
+
const sentMessages = (0, promptContext_1.getTranscriptMessages)(actualMessageSentToLLM);
|
|
341
|
+
const nextMessages = (0, promptContext_1.getTranscriptMessages)(nextMessage);
|
|
342
|
+
return nextMessages.slice(sentMessages.length);
|
|
178
343
|
}
|
|
179
344
|
async executeTools(input) {
|
|
180
345
|
var _a;
|
|
@@ -257,6 +422,21 @@ class ResponseExecutor {
|
|
|
257
422
|
console.error('[ResponseExecutor] Failed to send final chat message:', error);
|
|
258
423
|
}
|
|
259
424
|
}
|
|
425
|
+
async getPendingAsyncTasks() {
|
|
426
|
+
try {
|
|
427
|
+
const response = await Promise.resolve(codeboltjs_1.default.asyncTask.listTasks({
|
|
428
|
+
scope: 'current_run',
|
|
429
|
+
}));
|
|
430
|
+
const tasks = Array.isArray(response === null || response === void 0 ? void 0 : response.tasks) ? response.tasks : [];
|
|
431
|
+
return tasks.filter((task) => task &&
|
|
432
|
+
task.executionMode !== 'background_detached' &&
|
|
433
|
+
!['completed', 'failed', 'stopped'].includes(task.status));
|
|
434
|
+
}
|
|
435
|
+
catch (error) {
|
|
436
|
+
console.error('[ResponseExecutor] Failed to check async tasks before completion:', error);
|
|
437
|
+
return [];
|
|
438
|
+
}
|
|
439
|
+
}
|
|
260
440
|
extractCompletionMessage(toolInput) {
|
|
261
441
|
if ('result' in toolInput) {
|
|
262
442
|
return this.formatCompletionValue(toolInput['result']);
|
|
@@ -299,6 +479,9 @@ class ResponseExecutor {
|
|
|
299
479
|
userMessage: String(toolCall.toolInput['task'] || toolCall.toolInput['userMessage'] || ''),
|
|
300
480
|
selectedAgent: toolCall.toolInput['selectedAgent'],
|
|
301
481
|
isGrouped: Boolean(toolCall.toolInput['isGrouped']),
|
|
482
|
+
...(toolCall.toolInput['llm'] && typeof toolCall.toolInput['llm'] === 'object'
|
|
483
|
+
? { llm: toolCall.toolInput['llm'] }
|
|
484
|
+
: {}),
|
|
302
485
|
...(typeof toolCall.toolInput['groupId'] === 'string'
|
|
303
486
|
? { groupId: toolCall.toolInput['groupId'] }
|
|
304
487
|
: {}),
|
|
@@ -307,7 +490,9 @@ class ResponseExecutor {
|
|
|
307
490
|
}
|
|
308
491
|
else if (toolCall.toolName.startsWith('subagent--')) {
|
|
309
492
|
const task = toolCall.toolInput['task'];
|
|
310
|
-
await codeboltjs_1.default.agent.startAgent(toolCall.toolName.replace('subagent--', ''), typeof task === 'string' ? task : JSON.stringify(task)
|
|
493
|
+
await codeboltjs_1.default.agent.startAgent(toolCall.toolName.replace('subagent--', ''), typeof task === 'string' ? task : JSON.stringify(task), toolCall.toolInput['llm'] && typeof toolCall.toolInput['llm'] === 'object'
|
|
494
|
+
? { llm: toolCall.toolInput['llm'] }
|
|
495
|
+
: undefined);
|
|
311
496
|
resultTuple = [false, 'tool result is successful'];
|
|
312
497
|
}
|
|
313
498
|
else {
|
|
@@ -337,7 +522,7 @@ class ResponseExecutor {
|
|
|
337
522
|
}
|
|
338
523
|
}
|
|
339
524
|
async executeTool(toolName, toolInput, input) {
|
|
340
|
-
var _a, _b
|
|
525
|
+
var _a, _b;
|
|
341
526
|
const executionToolName = (0, agentToolLoader_1.resolveToolExecutionName)(toolName);
|
|
342
527
|
const localTool = this.localToolsByExecutionName.get(executionToolName);
|
|
343
528
|
if (localTool) {
|
|
@@ -353,17 +538,20 @@ class ResponseExecutor {
|
|
|
353
538
|
}
|
|
354
539
|
return [false, (_a = localResult.result) !== null && _a !== void 0 ? _a : ''];
|
|
355
540
|
}
|
|
356
|
-
const
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
541
|
+
const toolGateway = codeboltjs_1.default.tools;
|
|
542
|
+
if (typeof (toolGateway === null || toolGateway === void 0 ? void 0 : toolGateway.execute) === 'function') {
|
|
543
|
+
const response = await toolGateway.execute(executionToolName, toolInput);
|
|
544
|
+
const data = (_b = response.data) !== null && _b !== void 0 ? _b : response.result;
|
|
545
|
+
if (Array.isArray(data) && data.length >= 2) {
|
|
546
|
+
const [didUserReject, content] = data;
|
|
547
|
+
return [Boolean(didUserReject), content];
|
|
548
|
+
}
|
|
549
|
+
return [false, data];
|
|
363
550
|
}
|
|
364
|
-
|
|
551
|
+
throw new Error('codebolt.tools.execute is required for runtime tool execution.');
|
|
365
552
|
}
|
|
366
553
|
parseToolResult(tool_call_id, content) {
|
|
554
|
+
let parsedStructuredContent = content;
|
|
367
555
|
let serializedContent = typeof content === 'string'
|
|
368
556
|
? content
|
|
369
557
|
: JSON.stringify(content);
|
|
@@ -372,6 +560,7 @@ class ResponseExecutor {
|
|
|
372
560
|
const parsedContent = typeof serializedContent === 'string'
|
|
373
561
|
? JSON.parse(serializedContent)
|
|
374
562
|
: serializedContent;
|
|
563
|
+
parsedStructuredContent = parsedContent;
|
|
375
564
|
if (parsedContent &&
|
|
376
565
|
typeof parsedContent === 'object' &&
|
|
377
566
|
'payload' in parsedContent &&
|
|
@@ -386,10 +575,13 @@ class ResponseExecutor {
|
|
|
386
575
|
catch {
|
|
387
576
|
// Preserve the raw tool result when it is not JSON.
|
|
388
577
|
}
|
|
578
|
+
const contentForModel = isToolSearchOutput(parsedStructuredContent)
|
|
579
|
+
? normalizeToolSearchOutput(parsedStructuredContent, tool_call_id)
|
|
580
|
+
: serializedContent;
|
|
389
581
|
return {
|
|
390
582
|
role: 'tool',
|
|
391
583
|
tool_call_id,
|
|
392
|
-
content:
|
|
584
|
+
content: contentForModel,
|
|
393
585
|
userMessage,
|
|
394
586
|
};
|
|
395
587
|
}
|
|
@@ -414,10 +606,13 @@ class ResponseExecutor {
|
|
|
414
606
|
for (const toolCall of toolCalls) {
|
|
415
607
|
if (!toolCall)
|
|
416
608
|
continue;
|
|
417
|
-
const toolName = ((_b = toolCall.function) === null || _b === void 0 ? void 0 : _b.name) || '';
|
|
609
|
+
const toolName = toolCall.name || ((_b = toolCall.function) === null || _b === void 0 ? void 0 : _b.name) || '';
|
|
418
610
|
const isToolSearch = toolName === 'tool_search' ||
|
|
419
611
|
toolName === 'codebolt--tool_search' ||
|
|
420
612
|
toolName.endsWith('--tool_search') ||
|
|
613
|
+
toolName === 'get_available_tools_manifest' ||
|
|
614
|
+
toolName === 'codebolt--get_available_tools_manifest' ||
|
|
615
|
+
toolName.endsWith('--get_available_tools_manifest') ||
|
|
421
616
|
toolName === 'search_mcp_tool' ||
|
|
422
617
|
toolName === 'codebolt--search_mcp_tool' ||
|
|
423
618
|
toolName.endsWith('--search_mcp_tool') ||
|
|
@@ -431,20 +626,8 @@ class ResponseExecutor {
|
|
|
431
626
|
const toolResult = toolResults.find(r => r.tool_call_id === toolCallId);
|
|
432
627
|
if (!(toolResult === null || toolResult === void 0 ? void 0 : toolResult.content))
|
|
433
628
|
continue;
|
|
434
|
-
const
|
|
435
|
-
|
|
436
|
-
: JSON.stringify(toolResult.content);
|
|
437
|
-
const jsonMatch = content.match(/\[[\s\S]*\]/);
|
|
438
|
-
if (!jsonMatch)
|
|
439
|
-
continue;
|
|
440
|
-
let discoveredSchemas;
|
|
441
|
-
try {
|
|
442
|
-
discoveredSchemas = JSON.parse(jsonMatch[0]);
|
|
443
|
-
}
|
|
444
|
-
catch {
|
|
445
|
-
continue;
|
|
446
|
-
}
|
|
447
|
-
if (!Array.isArray(discoveredSchemas))
|
|
629
|
+
const discoveredSchemas = extractDiscoveredToolSchemas(toolResult.content);
|
|
630
|
+
if (!discoveredSchemas.length)
|
|
448
631
|
continue;
|
|
449
632
|
const existingToolNames = new Set(nextMessage.message.tools.map((tool) => { var _a; return (_a = tool.function) === null || _a === void 0 ? void 0 : _a.name; }));
|
|
450
633
|
for (const schema of discoveredSchemas) {
|
|
@@ -452,7 +635,11 @@ class ResponseExecutor {
|
|
|
452
635
|
const rawName = schemaFunction === null || schemaFunction === void 0 ? void 0 : schemaFunction.name;
|
|
453
636
|
if (!rawName)
|
|
454
637
|
continue;
|
|
455
|
-
const
|
|
638
|
+
const isLocalToolName = this.localToolsByExecutionName.has(rawName) ||
|
|
639
|
+
this.localToolsByExecutionName.has((0, agentToolLoader_1.resolveToolExecutionName)(rawName));
|
|
640
|
+
const prefixedName = rawName.includes('--') || isLocalToolName || existingToolNames.has(rawName)
|
|
641
|
+
? rawName
|
|
642
|
+
: `codebolt--${rawName}`;
|
|
456
643
|
if (existingToolNames.has(prefixedName))
|
|
457
644
|
continue;
|
|
458
645
|
const prefixedSchema = {
|
package/dist/unified/index.d.ts
CHANGED
|
@@ -19,6 +19,7 @@ export { CompressionCoordinator, type CompressionCoordinatorOptions, type Compre
|
|
|
19
19
|
export { Agent, createAgent, type AgentOptions, type AgentRunResult, type AgentRunOptions, type AgentRunState, type CreateAgentOptions } from './agent/agent';
|
|
20
20
|
export { Tool, createTool } from './agent/tools';
|
|
21
21
|
export { Workflow } from './agent/workflow';
|
|
22
|
+
export { listAgentRuntimeTools, type ToolSourceType, type RuntimeToolDescriptor, } from './utils/agentToolLoader';
|
|
22
23
|
export { type OpenAIMessage, type OpenAITool, type ToolResult, type CodeboltAPI, type AgentExecutionResult, type StreamChunk, type StreamCallback } from './types/libTypes';
|
|
23
24
|
export { type LLMConfig } from './types/libTypes';
|
|
24
25
|
export { CompactionOrchestrator, type CompactionPipelineResult, } from './services/compaction/compactionOrchestrator';
|
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.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;
|
|
13
|
+
exports.TokenEstimator = exports.PostCompactCleanup = exports.ReactiveCompact = exports.AutoCompact = exports.ContextCollapse = exports.MicroCompact = exports.SnipCompact = exports.CompactionOrchestrator = exports.listAgentRuntimeTools = 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; } });
|
|
@@ -41,6 +41,8 @@ Object.defineProperty(exports, "Tool", { enumerable: true, get: function () { re
|
|
|
41
41
|
Object.defineProperty(exports, "createTool", { enumerable: true, get: function () { return tools_1.createTool; } });
|
|
42
42
|
var workflow_1 = require("./agent/workflow");
|
|
43
43
|
Object.defineProperty(exports, "Workflow", { enumerable: true, get: function () { return workflow_1.Workflow; } });
|
|
44
|
+
var agentToolLoader_1 = require("./utils/agentToolLoader");
|
|
45
|
+
Object.defineProperty(exports, "listAgentRuntimeTools", { enumerable: true, get: function () { return agentToolLoader_1.listAgentRuntimeTools; } });
|
|
44
46
|
// Workflow step factories
|
|
45
47
|
// Multi-layer compaction system
|
|
46
48
|
var compactionOrchestrator_1 = require("./services/compaction/compactionOrchestrator");
|
|
@@ -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 !==
|
|
@@ -23,7 +23,7 @@ export interface AutoCompactOptions {
|
|
|
23
23
|
modelTokenLimit?: number;
|
|
24
24
|
/** Fraction of recent history to preserve after compression (default: 0.3) */
|
|
25
25
|
preserveThreshold?: number;
|
|
26
|
-
/** LLM role for summarization calls
|
|
26
|
+
/** LLM role for summarization calls */
|
|
27
27
|
llmRole?: string;
|
|
28
28
|
/** Enable logging (default: false) */
|
|
29
29
|
enableLogging?: boolean;
|
|
@@ -40,6 +40,7 @@ export declare class AutoCompact implements CompactionLayer {
|
|
|
40
40
|
private consecutiveFailures;
|
|
41
41
|
private tracking;
|
|
42
42
|
constructor(options?: AutoCompactOptions);
|
|
43
|
+
private normalizeLLMRole;
|
|
43
44
|
shouldApply(ctx: CompactionContext): boolean;
|
|
44
45
|
apply(ctx: CompactionContext): Promise<CompactionContext>;
|
|
45
46
|
reset(): void;
|
|
@@ -25,7 +25,7 @@ const DEFAULT_MAX_CONSECUTIVE_FAILURES = 3;
|
|
|
25
25
|
const DEFAULT_MODEL_TOKEN_LIMIT = 128000;
|
|
26
26
|
class AutoCompact {
|
|
27
27
|
constructor(options) {
|
|
28
|
-
var _a, _b, _c, _d, _e
|
|
28
|
+
var _a, _b, _c, _d, _e;
|
|
29
29
|
this.name = 'auto';
|
|
30
30
|
this.consecutiveFailures = 0;
|
|
31
31
|
this.options = {
|
|
@@ -33,10 +33,14 @@ class AutoCompact {
|
|
|
33
33
|
maxConsecutiveFailures: (_b = options === null || options === void 0 ? void 0 : options.maxConsecutiveFailures) !== null && _b !== void 0 ? _b : DEFAULT_MAX_CONSECUTIVE_FAILURES,
|
|
34
34
|
modelTokenLimit: (_c = options === null || options === void 0 ? void 0 : options.modelTokenLimit) !== null && _c !== void 0 ? _c : DEFAULT_MODEL_TOKEN_LIMIT,
|
|
35
35
|
preserveThreshold: (_d = options === null || options === void 0 ? void 0 : options.preserveThreshold) !== null && _d !== void 0 ? _d : 0.3,
|
|
36
|
-
llmRole: (
|
|
37
|
-
enableLogging: (
|
|
36
|
+
llmRole: this.normalizeLLMRole(options === null || options === void 0 ? void 0 : options.llmRole),
|
|
37
|
+
enableLogging: (_e = options === null || options === void 0 ? void 0 : options.enableLogging) !== null && _e !== void 0 ? _e : false,
|
|
38
38
|
};
|
|
39
39
|
}
|
|
40
|
+
normalizeLLMRole(llmRole) {
|
|
41
|
+
const normalized = llmRole === null || llmRole === void 0 ? void 0 : llmRole.trim();
|
|
42
|
+
return normalized ? normalized : undefined;
|
|
43
|
+
}
|
|
40
44
|
shouldApply(ctx) {
|
|
41
45
|
// Don't auto-compact if context collapse is handling it
|
|
42
46
|
if (ctx.contextCollapseEnabled) {
|
|
@@ -196,7 +200,7 @@ class AutoCompact {
|
|
|
196
200
|
return splitIndex;
|
|
197
201
|
}
|
|
198
202
|
async generateSummary(messages) {
|
|
199
|
-
var _a, _b
|
|
203
|
+
var _a, _b;
|
|
200
204
|
try {
|
|
201
205
|
// eslint-disable-next-line @typescript-eslint/no-require-imports
|
|
202
206
|
const codebolt = require('@codebolt/codeboltjs');
|
|
@@ -255,22 +259,26 @@ CRITICAL RULES:
|
|
|
255
259
|
- Include the FULL original user request, not a paraphrase
|
|
256
260
|
- Do not summarize away technical details
|
|
257
261
|
- Focus on facts and specifics`;
|
|
258
|
-
const
|
|
259
|
-
|
|
262
|
+
const inferencePayload = {
|
|
263
|
+
formatVersion: 'codebolt.llm.v2',
|
|
264
|
+
input: [
|
|
260
265
|
{
|
|
261
266
|
role: 'system',
|
|
262
267
|
content: 'You are a precise conversation compression assistant. Be comprehensive but concise.',
|
|
263
268
|
},
|
|
264
269
|
{ role: 'user', content: prompt },
|
|
265
270
|
],
|
|
266
|
-
|
|
271
|
+
...(this.options.llmRole ? { llmrole: this.options.llmRole } : {}),
|
|
272
|
+
};
|
|
273
|
+
console.log('[AutoCompact] llm.inference payload:', inferencePayload);
|
|
274
|
+
const response = await codebolt.llm.inference(inferencePayload);
|
|
267
275
|
// Extract summary from response
|
|
268
276
|
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.
|
|
277
|
+
if (typeof ((_a = response === null || response === void 0 ? void 0 : response.completion) === null || _a === void 0 ? void 0 : _a.output_text) === 'string') {
|
|
278
|
+
summary = response.completion.output_text;
|
|
271
279
|
}
|
|
272
|
-
else if (
|
|
273
|
-
summary = response.completion.
|
|
280
|
+
else if (typeof ((_b = response === null || response === void 0 ? void 0 : response.completion) === null || _b === void 0 ? void 0 : _b.content) === 'string') {
|
|
281
|
+
summary = response.completion.content;
|
|
274
282
|
}
|
|
275
283
|
if (summary && summary.trim().length > 0) {
|
|
276
284
|
return summary.trim();
|