@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
|
@@ -1,45 +1,160 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
2
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
6
|
exports.Agent = void 0;
|
|
7
|
+
exports.createAgent = createAgent;
|
|
8
|
+
const codeboltjs_1 = __importDefault(require("@codebolt/codeboltjs"));
|
|
9
|
+
const crypto_1 = require("crypto");
|
|
4
10
|
const base_1 = require("../base");
|
|
5
11
|
const agentStep_1 = require("../base/agentStep");
|
|
6
12
|
const responseExecutor_1 = require("../base/responseExecutor");
|
|
7
13
|
const promptContext_1 = require("../base/promptContext");
|
|
8
14
|
const compactionOrchestrator_1 = require("../services/compaction/compactionOrchestrator");
|
|
9
15
|
const agentToolLoader_1 = require("../utils/agentToolLoader");
|
|
16
|
+
const processor_pieces_1 = require("../../processor-pieces");
|
|
17
|
+
const DEFAULT_SYSTEM_PROMPT = 'Based on User Message send reply';
|
|
18
|
+
function getProcessorKey(processor) {
|
|
19
|
+
var _a;
|
|
20
|
+
const candidate = processor;
|
|
21
|
+
if (typeof candidate.id === 'string' && candidate.id.length > 0) {
|
|
22
|
+
return candidate.id;
|
|
23
|
+
}
|
|
24
|
+
if (typeof candidate.name === 'string' && candidate.name.length > 0) {
|
|
25
|
+
return candidate.name;
|
|
26
|
+
}
|
|
27
|
+
if ((_a = candidate.constructor) === null || _a === void 0 ? void 0 : _a.name) {
|
|
28
|
+
return candidate.constructor.name;
|
|
29
|
+
}
|
|
30
|
+
return String(processor);
|
|
31
|
+
}
|
|
32
|
+
function mergeProcessors(defaults, custom) {
|
|
33
|
+
const merged = new Map();
|
|
34
|
+
for (const processor of defaults) {
|
|
35
|
+
merged.set(getProcessorKey(processor), processor);
|
|
36
|
+
}
|
|
37
|
+
for (const processor of custom) {
|
|
38
|
+
merged.set(getProcessorKey(processor), processor);
|
|
39
|
+
}
|
|
40
|
+
return Array.from(merged.values());
|
|
41
|
+
}
|
|
42
|
+
function createDefaultMessageModifiers(systemPrompt, allowedTools) {
|
|
43
|
+
return [
|
|
44
|
+
new processor_pieces_1.ChatHistoryMessageModifier({
|
|
45
|
+
enableChatHistory: true,
|
|
46
|
+
includeSystemMessages: false,
|
|
47
|
+
}),
|
|
48
|
+
new processor_pieces_1.EnvironmentContextModifier({ enableFullContext: true }),
|
|
49
|
+
new processor_pieces_1.DirectoryContextModifier(),
|
|
50
|
+
new processor_pieces_1.IdeContextModifier({
|
|
51
|
+
includeActiveFile: true,
|
|
52
|
+
includeOpenFiles: true,
|
|
53
|
+
includeCursorPosition: true,
|
|
54
|
+
includeSelectedText: true
|
|
55
|
+
}),
|
|
56
|
+
new processor_pieces_1.CoreSystemPromptModifier({ customSystemPrompt: systemPrompt }),
|
|
57
|
+
new processor_pieces_1.ToolInjectionModifier({
|
|
58
|
+
includeToolDescriptions: true,
|
|
59
|
+
...(allowedTools ? { allowedTools } : {})
|
|
60
|
+
}),
|
|
61
|
+
new processor_pieces_1.AtFileProcessorModifier({ enableRecursiveSearch: true })
|
|
62
|
+
];
|
|
63
|
+
}
|
|
64
|
+
function createDefaultPreInferenceProcessors() {
|
|
65
|
+
return [];
|
|
66
|
+
}
|
|
67
|
+
function createDefaultPostInferenceProcessors() {
|
|
68
|
+
return [];
|
|
69
|
+
}
|
|
70
|
+
function createDefaultPreToolCallProcessors() {
|
|
71
|
+
return [];
|
|
72
|
+
}
|
|
73
|
+
function createDefaultPostToolCallProcessors() {
|
|
74
|
+
return [];
|
|
75
|
+
}
|
|
76
|
+
function createDefaultUserMessage(message) {
|
|
77
|
+
const timestamp = Date.now();
|
|
78
|
+
return {
|
|
79
|
+
userMessage: message,
|
|
80
|
+
selectedAgent: {
|
|
81
|
+
id: 'codebolt-agent',
|
|
82
|
+
name: 'Codebolt Agent'
|
|
83
|
+
},
|
|
84
|
+
mentionedFiles: [],
|
|
85
|
+
mentionedFullPaths: [],
|
|
86
|
+
mentionedFolders: [],
|
|
87
|
+
mentionedMCPs: [],
|
|
88
|
+
uploadedImages: [],
|
|
89
|
+
mentionedAgents: [],
|
|
90
|
+
mentionedEnvironments: [],
|
|
91
|
+
messageId: `msg-${timestamp}`,
|
|
92
|
+
threadId: `thread-${timestamp}`
|
|
93
|
+
};
|
|
94
|
+
}
|
|
95
|
+
function collectProcessors(fromNested, fromTopLevel) {
|
|
96
|
+
return [
|
|
97
|
+
...(fromNested !== null && fromNested !== void 0 ? fromNested : []),
|
|
98
|
+
...(fromTopLevel !== null && fromTopLevel !== void 0 ? fromTopLevel : []),
|
|
99
|
+
];
|
|
100
|
+
}
|
|
10
101
|
class Agent {
|
|
11
102
|
constructor(config) {
|
|
12
|
-
var _a, _b, _c, _d, _e, _f;
|
|
13
|
-
const
|
|
103
|
+
var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l;
|
|
104
|
+
const localToolRegistry = (0, agentToolLoader_1.createAgentLocalToolRegistry)(config.tools || []);
|
|
105
|
+
const includeDefaultModifiers = (_b = (_a = config.includeDefaultModifiers) !== null && _a !== void 0 ? _a : config.defaultProcessors) !== null && _b !== void 0 ? _b : true;
|
|
106
|
+
const includeDefaultProcessors = (_d = (_c = config.includeDefaultProcessors) !== null && _c !== void 0 ? _c : config.defaultProcessors) !== null && _d !== void 0 ? _d : true;
|
|
107
|
+
const customMessageModifiers = collectProcessors((_e = config.processors) === null || _e === void 0 ? void 0 : _e.messageModifiers, config.messageModifiers);
|
|
108
|
+
const defaultMessageModifiers = includeDefaultModifiers
|
|
109
|
+
? createDefaultMessageModifiers(config.instructions || DEFAULT_SYSTEM_PROMPT, config.allowedTools)
|
|
110
|
+
: [];
|
|
14
111
|
this.config = { ...config };
|
|
15
|
-
this.messageModifiers = ((_a = config.processors) === null || _a === void 0 ? void 0 : _a.messageModifiers) || [];
|
|
16
|
-
this.preInferenceProcessors = ((_b = config.processors) === null || _b === void 0 ? void 0 : _b.preInferenceProcessors) || [];
|
|
17
|
-
this.postInferenceProcessors = ((_c = config.processors) === null || _c === void 0 ? void 0 : _c.postInferenceProcessors) || [];
|
|
18
|
-
this.preToolCallProcessors = ((_d = config.processors) === null || _d === void 0 ? void 0 : _d.preToolCallProcessors) || [];
|
|
19
|
-
this.postToolCallProcessors = ((_e = config.processors) === null || _e === void 0 ? void 0 : _e.postToolCallProcessors) || [];
|
|
20
112
|
this.enableLogging = config.enableLogging !== false;
|
|
21
|
-
this.
|
|
22
|
-
this.
|
|
23
|
-
this.
|
|
113
|
+
this.baseSystemPrompt = config.instructions || DEFAULT_SYSTEM_PROMPT;
|
|
114
|
+
this.context = config.context;
|
|
115
|
+
this.allowedTools = config.allowedTools;
|
|
116
|
+
this.messageModifiers = mergeProcessors(defaultMessageModifiers, customMessageModifiers);
|
|
117
|
+
this.preInferenceProcessors = mergeProcessors(includeDefaultProcessors ? createDefaultPreInferenceProcessors() : [], collectProcessors((_f = config.processors) === null || _f === void 0 ? void 0 : _f.preInferenceProcessors, config.preInferenceProcessors));
|
|
118
|
+
this.postInferenceProcessors = mergeProcessors(includeDefaultProcessors ? createDefaultPostInferenceProcessors() : [], collectProcessors((_g = config.processors) === null || _g === void 0 ? void 0 : _g.postInferenceProcessors, config.postInferenceProcessors));
|
|
119
|
+
this.preToolCallProcessors = mergeProcessors(includeDefaultProcessors ? createDefaultPreToolCallProcessors() : [], collectProcessors((_h = config.processors) === null || _h === void 0 ? void 0 : _h.preToolCallProcessors, config.preToolCallProcessors));
|
|
120
|
+
this.postToolCallProcessors = mergeProcessors(includeDefaultProcessors ? createDefaultPostToolCallProcessors() : [], collectProcessors((_j = config.processors) === null || _j === void 0 ? void 0 : _j.postToolCallProcessors, config.postToolCallProcessors));
|
|
121
|
+
this.compactionOrchestrator = new compactionOrchestrator_1.CompactionOrchestrator(config.compaction);
|
|
122
|
+
this.loopDetectionService = config.loopDetectionService;
|
|
123
|
+
this.maxTurns = (_l = (_k = config.maxTurns) !== null && _k !== void 0 ? _k : config.maxIterations) !== null && _l !== void 0 ? _l : 25;
|
|
124
|
+
this.localToolSchemas = localToolRegistry.schemas;
|
|
125
|
+
this.localToolsByExecutionName = localToolRegistry.byExecutionName;
|
|
126
|
+
this.runtimeToolSetId = `agent-${(0, crypto_1.randomUUID)()}`;
|
|
24
127
|
}
|
|
25
|
-
async
|
|
26
|
-
var _a;
|
|
27
|
-
if (!reqMessage) {
|
|
28
|
-
return {
|
|
29
|
-
success: false,
|
|
30
|
-
result: null,
|
|
31
|
-
error: 'Request message is required'
|
|
32
|
-
};
|
|
33
|
-
}
|
|
128
|
+
async run(message, options) {
|
|
129
|
+
var _a, _b;
|
|
34
130
|
try {
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
131
|
+
await this.registerRuntimeToolsForSearch();
|
|
132
|
+
const reqMessage = typeof message === 'string'
|
|
133
|
+
? createDefaultUserMessage(message)
|
|
134
|
+
: message;
|
|
135
|
+
let prompt;
|
|
136
|
+
const contextToUse = this.resolveRunContext(options);
|
|
137
|
+
if (contextToUse) {
|
|
138
|
+
const promptGenerator = new base_1.InitialPromptGenerator({
|
|
139
|
+
processors: this.getResumeMessageModifiers(),
|
|
140
|
+
initialPrompt: contextToUse,
|
|
141
|
+
enableLogging: this.enableLogging
|
|
142
|
+
});
|
|
143
|
+
prompt = await promptGenerator.processMessage(reqMessage);
|
|
144
|
+
}
|
|
145
|
+
else {
|
|
146
|
+
const promptGenerator = new base_1.InitialPromptGenerator({
|
|
147
|
+
processors: this.messageModifiers,
|
|
148
|
+
baseSystemPrompt: this.baseSystemPrompt,
|
|
149
|
+
enableLogging: this.enableLogging
|
|
150
|
+
});
|
|
151
|
+
prompt = await promptGenerator.processMessage(reqMessage);
|
|
152
|
+
}
|
|
153
|
+
prompt = await this.hydratePromptFromServerCompaction(reqMessage, prompt);
|
|
41
154
|
let completed = false;
|
|
42
155
|
let turnNumber = 0;
|
|
156
|
+
let finalMessage;
|
|
157
|
+
const toolResults = [];
|
|
43
158
|
while (!completed) {
|
|
44
159
|
turnNumber += 1;
|
|
45
160
|
if (turnNumber > this.maxTurns) {
|
|
@@ -82,6 +197,7 @@ class Agent {
|
|
|
82
197
|
const responseExecutor = new responseExecutor_1.ResponseExecutor({
|
|
83
198
|
preToolCallProcessors: this.preToolCallProcessors,
|
|
84
199
|
postToolCallProcessors: this.postToolCallProcessors,
|
|
200
|
+
localToolsByExecutionName: this.localToolsByExecutionName,
|
|
85
201
|
...(this.loopDetectionService
|
|
86
202
|
? { loopDetectionService: this.loopDetectionService }
|
|
87
203
|
: {}),
|
|
@@ -94,10 +210,17 @@ class Agent {
|
|
|
94
210
|
});
|
|
95
211
|
completed = executionResult.completed;
|
|
96
212
|
prompt = executionResult.nextMessage;
|
|
213
|
+
finalMessage = executionResult.finalMessage;
|
|
214
|
+
toolResults.push(...((_b = executionResult.toolResults) !== null && _b !== void 0 ? _b : []));
|
|
97
215
|
}
|
|
216
|
+
const state = { prompt };
|
|
98
217
|
return {
|
|
99
218
|
success: true,
|
|
100
|
-
|
|
219
|
+
state,
|
|
220
|
+
toolResults,
|
|
221
|
+
...(finalMessage !== undefined ? { finalMessage } : {}),
|
|
222
|
+
result: prompt,
|
|
223
|
+
context: prompt,
|
|
101
224
|
};
|
|
102
225
|
}
|
|
103
226
|
catch (error) {
|
|
@@ -107,10 +230,84 @@ class Agent {
|
|
|
107
230
|
}
|
|
108
231
|
return {
|
|
109
232
|
success: false,
|
|
233
|
+
state: null,
|
|
234
|
+
toolResults: [],
|
|
110
235
|
result: null,
|
|
236
|
+
context: null,
|
|
111
237
|
error: errorMessage
|
|
112
238
|
};
|
|
113
239
|
}
|
|
240
|
+
finally {
|
|
241
|
+
await this.unregisterRuntimeToolsForSearch();
|
|
242
|
+
}
|
|
243
|
+
}
|
|
244
|
+
async registerRuntimeToolsForSearch() {
|
|
245
|
+
var _a, _b;
|
|
246
|
+
if (this.localToolSchemas.length === 0) {
|
|
247
|
+
return;
|
|
248
|
+
}
|
|
249
|
+
try {
|
|
250
|
+
await ((_b = (_a = codeboltjs_1.default.searchableAssets) === null || _a === void 0 ? void 0 : _a.registerRuntimeTools) === null || _b === void 0 ? void 0 : _b.call(_a, this.runtimeToolSetId, this.localToolSchemas));
|
|
251
|
+
}
|
|
252
|
+
catch (error) {
|
|
253
|
+
if (this.enableLogging) {
|
|
254
|
+
console.error('[Agent] Failed to register runtime tools for search:', error);
|
|
255
|
+
}
|
|
256
|
+
}
|
|
257
|
+
}
|
|
258
|
+
async unregisterRuntimeToolsForSearch() {
|
|
259
|
+
var _a, _b;
|
|
260
|
+
if (this.localToolSchemas.length === 0) {
|
|
261
|
+
return;
|
|
262
|
+
}
|
|
263
|
+
try {
|
|
264
|
+
await ((_b = (_a = codeboltjs_1.default.searchableAssets) === null || _a === void 0 ? void 0 : _a.unregisterRuntimeTools) === null || _b === void 0 ? void 0 : _b.call(_a, this.runtimeToolSetId));
|
|
265
|
+
}
|
|
266
|
+
catch (error) {
|
|
267
|
+
if (this.enableLogging) {
|
|
268
|
+
console.error('[Agent] Failed to unregister runtime tools for search:', error);
|
|
269
|
+
}
|
|
270
|
+
}
|
|
271
|
+
}
|
|
272
|
+
async processMessage(message, options) {
|
|
273
|
+
return this.run(message, options);
|
|
274
|
+
}
|
|
275
|
+
async execute(reqMessage) {
|
|
276
|
+
return this.run(reqMessage);
|
|
277
|
+
}
|
|
278
|
+
getConfig() {
|
|
279
|
+
return { ...this.config };
|
|
280
|
+
}
|
|
281
|
+
getMessageModifiers() {
|
|
282
|
+
return [...this.messageModifiers];
|
|
283
|
+
}
|
|
284
|
+
getPreInferenceProcessors() {
|
|
285
|
+
return [...this.preInferenceProcessors];
|
|
286
|
+
}
|
|
287
|
+
getPostInferenceProcessors() {
|
|
288
|
+
return [...this.postInferenceProcessors];
|
|
289
|
+
}
|
|
290
|
+
getPreToolCallProcessors() {
|
|
291
|
+
return [...this.preToolCallProcessors];
|
|
292
|
+
}
|
|
293
|
+
getPostToolCallProcessors() {
|
|
294
|
+
return [...this.postToolCallProcessors];
|
|
295
|
+
}
|
|
296
|
+
resolveRunContext(options) {
|
|
297
|
+
var _a, _b, _c;
|
|
298
|
+
if (!options) {
|
|
299
|
+
return this.context;
|
|
300
|
+
}
|
|
301
|
+
if (this.isProcessedMessage(options)) {
|
|
302
|
+
return options;
|
|
303
|
+
}
|
|
304
|
+
return (_c = (_b = (_a = options.state) === null || _a === void 0 ? void 0 : _a.prompt) !== null && _b !== void 0 ? _b : options.context) !== null && _c !== void 0 ? _c : this.context;
|
|
305
|
+
}
|
|
306
|
+
getResumeMessageModifiers() {
|
|
307
|
+
return this.messageModifiers.filter((modifier) => { var _a; return ((_a = modifier.constructor) === null || _a === void 0 ? void 0 : _a.name) !== 'ChatHistoryMessageModifier'; });
|
|
308
|
+
}
|
|
309
|
+
isProcessedMessage(value) {
|
|
310
|
+
return 'message' in value && 'metadata' in value;
|
|
114
311
|
}
|
|
115
312
|
async applyCompaction(prompt) {
|
|
116
313
|
const result = await this.compactionOrchestrator.compact((0, promptContext_1.getTranscriptMessages)(prompt));
|
|
@@ -130,6 +327,111 @@ class Agent {
|
|
|
130
327
|
},
|
|
131
328
|
};
|
|
132
329
|
}
|
|
330
|
+
async hydratePromptFromServerCompaction(requestMessage, prompt) {
|
|
331
|
+
if (!requestMessage.threadId) {
|
|
332
|
+
return prompt;
|
|
333
|
+
}
|
|
334
|
+
try {
|
|
335
|
+
const response = await codeboltjs_1.default.thread.getThreadContextCompacted({
|
|
336
|
+
threadId: requestMessage.threadId,
|
|
337
|
+
});
|
|
338
|
+
if (!this.isServerCompactionCurrent(response === null || response === void 0 ? void 0 : response.context)) {
|
|
339
|
+
return prompt;
|
|
340
|
+
}
|
|
341
|
+
const compactedMessages = this.extractServerCompactedMessages(response === null || response === void 0 ? void 0 : response.compactedContext);
|
|
342
|
+
if (compactedMessages.length === 0) {
|
|
343
|
+
return prompt;
|
|
344
|
+
}
|
|
345
|
+
const currentRunMessages = this.extractCurrentRunMessages(requestMessage, prompt);
|
|
346
|
+
return {
|
|
347
|
+
...(0, promptContext_1.replaceTranscriptMessages)(prompt, [
|
|
348
|
+
...compactedMessages,
|
|
349
|
+
...currentRunMessages,
|
|
350
|
+
]),
|
|
351
|
+
metadata: {
|
|
352
|
+
...prompt.metadata,
|
|
353
|
+
serverCompaction: {
|
|
354
|
+
threadId: requestMessage.threadId,
|
|
355
|
+
timestamp: new Date().toISOString(),
|
|
356
|
+
messageCount: compactedMessages.length,
|
|
357
|
+
currentRunMessageCount: currentRunMessages.length,
|
|
358
|
+
},
|
|
359
|
+
},
|
|
360
|
+
};
|
|
361
|
+
}
|
|
362
|
+
catch (error) {
|
|
363
|
+
if (this.enableLogging) {
|
|
364
|
+
console.error('[Agent] Failed to hydrate prompt from server compaction:', error);
|
|
365
|
+
}
|
|
366
|
+
return prompt;
|
|
367
|
+
}
|
|
368
|
+
}
|
|
369
|
+
isServerCompactionCurrent(context) {
|
|
370
|
+
if (!context || typeof context !== 'object') {
|
|
371
|
+
return true;
|
|
372
|
+
}
|
|
373
|
+
const metadata = context.metadata;
|
|
374
|
+
if (!metadata || typeof metadata !== 'object') {
|
|
375
|
+
return true;
|
|
376
|
+
}
|
|
377
|
+
const messageCount = Number(metadata.messageCount);
|
|
378
|
+
const lastCompactionMessageCount = Number(metadata.lastCompactionMessageCount);
|
|
379
|
+
if (!Number.isFinite(messageCount) || !Number.isFinite(lastCompactionMessageCount)) {
|
|
380
|
+
return true;
|
|
381
|
+
}
|
|
382
|
+
return messageCount <= lastCompactionMessageCount;
|
|
383
|
+
}
|
|
384
|
+
extractCurrentRunMessages(requestMessage, prompt) {
|
|
385
|
+
const transcriptMessages = (0, promptContext_1.getTranscriptMessages)(prompt);
|
|
386
|
+
const lastMessage = transcriptMessages[transcriptMessages.length - 1];
|
|
387
|
+
if (!lastMessage || lastMessage.role !== 'user') {
|
|
388
|
+
return [];
|
|
389
|
+
}
|
|
390
|
+
const requestedContent = requestMessage.userMessage || '';
|
|
391
|
+
if (!this.isCurrentRunUserMessage(lastMessage, requestedContent)) {
|
|
392
|
+
return [];
|
|
393
|
+
}
|
|
394
|
+
return [{ ...lastMessage }];
|
|
395
|
+
}
|
|
396
|
+
isCurrentRunUserMessage(message, requestedContent) {
|
|
397
|
+
if (typeof message.content === 'string') {
|
|
398
|
+
return message.content === requestedContent ||
|
|
399
|
+
message.content.startsWith(`${requestedContent}\n\n`);
|
|
400
|
+
}
|
|
401
|
+
if (!Array.isArray(message.content)) {
|
|
402
|
+
return false;
|
|
403
|
+
}
|
|
404
|
+
const firstTextPart = message.content.find((part) => !!part &&
|
|
405
|
+
typeof part === 'object' &&
|
|
406
|
+
!Array.isArray(part) &&
|
|
407
|
+
part.type === 'text' &&
|
|
408
|
+
typeof part.text === 'string');
|
|
409
|
+
return (firstTextPart === null || firstTextPart === void 0 ? void 0 : firstTextPart.text) === requestedContent ||
|
|
410
|
+
(firstTextPart === null || firstTextPart === void 0 ? void 0 : firstTextPart.text.startsWith(`${requestedContent}\n\n`)) ||
|
|
411
|
+
(requestedContent.length === 0 && (firstTextPart === null || firstTextPart === void 0 ? void 0 : firstTextPart.text) === 'Please use the attached image.');
|
|
412
|
+
}
|
|
413
|
+
extractServerCompactedMessages(compactedContext) {
|
|
414
|
+
var _a;
|
|
415
|
+
const data = typeof compactedContext === 'object' && compactedContext !== null
|
|
416
|
+
? compactedContext.data
|
|
417
|
+
: undefined;
|
|
418
|
+
const messages = typeof data === 'object' && data !== null
|
|
419
|
+
? ((_a = data.input) !== null && _a !== void 0 ? _a : data.messages)
|
|
420
|
+
: undefined;
|
|
421
|
+
if (!Array.isArray(messages)) {
|
|
422
|
+
return [];
|
|
423
|
+
}
|
|
424
|
+
return messages
|
|
425
|
+
.filter((message) => this.isServerCompactedMessage(message))
|
|
426
|
+
.map((message) => ({ ...message }));
|
|
427
|
+
}
|
|
428
|
+
isServerCompactedMessage(value) {
|
|
429
|
+
if (!value || typeof value !== 'object') {
|
|
430
|
+
return false;
|
|
431
|
+
}
|
|
432
|
+
const message = value;
|
|
433
|
+
return typeof message.role === 'string' && message.content !== undefined;
|
|
434
|
+
}
|
|
133
435
|
async tryRecoverPrompt(prompt, error) {
|
|
134
436
|
const errorMessage = error instanceof Error ? error.message : String(error);
|
|
135
437
|
if (!this.compactionOrchestrator.getReactiveLayer().isRecoverableError(errorMessage)) {
|
|
@@ -155,8 +457,9 @@ class Agent {
|
|
|
155
457
|
}
|
|
156
458
|
async refreshAvailableTools(originalRequest, prompt) {
|
|
157
459
|
var _a, _b, _c;
|
|
158
|
-
|
|
159
|
-
((_b = prompt.metadata) === null || _b === void 0 ? void 0 : _b['toolsLocation'])
|
|
460
|
+
const shouldRefreshDiscoveredTools = ((_a = prompt.metadata) === null || _a === void 0 ? void 0 : _a['toolsInjected']) === true &&
|
|
461
|
+
((_b = prompt.metadata) === null || _b === void 0 ? void 0 : _b['toolsLocation']) === 'Tool';
|
|
462
|
+
if (!shouldRefreshDiscoveredTools && this.localToolSchemas.length === 0) {
|
|
160
463
|
return prompt;
|
|
161
464
|
}
|
|
162
465
|
const existingTools = Array.isArray(prompt.message.tools)
|
|
@@ -166,13 +469,21 @@ class Agent {
|
|
|
166
469
|
const mentionedMCPs = Array.isArray(originalRequest.mentionedMCPs)
|
|
167
470
|
? originalRequest.mentionedMCPs
|
|
168
471
|
: [];
|
|
169
|
-
let refreshedTools =
|
|
472
|
+
let refreshedTools = shouldRefreshDiscoveredTools
|
|
473
|
+
? await (0, agentToolLoader_1.listAgentAvailableTools)(mentionedMCPs)
|
|
474
|
+
: [];
|
|
475
|
+
(0, agentToolLoader_1.assertNoLocalToolSchemaCollisions)(this.localToolSchemas, refreshedTools);
|
|
170
476
|
const allowedToolNames = this.getAllowedToolNames(prompt);
|
|
171
477
|
if (allowedToolNames && allowedToolNames.length > 0) {
|
|
172
478
|
const allowed = new Set(allowedToolNames);
|
|
173
479
|
refreshedTools = refreshedTools.filter((tool) => { var _a; return !!((_a = tool.function) === null || _a === void 0 ? void 0 : _a.name) && allowed.has(tool.function.name); });
|
|
174
480
|
}
|
|
175
|
-
|
|
481
|
+
let localToolSchemas = this.localToolSchemas;
|
|
482
|
+
if (allowedToolNames && allowedToolNames.length > 0) {
|
|
483
|
+
const allowed = new Set(allowedToolNames);
|
|
484
|
+
localToolSchemas = localToolSchemas.filter((tool) => { var _a; return !!((_a = tool.function) === null || _a === void 0 ? void 0 : _a.name) && allowed.has(tool.function.name); });
|
|
485
|
+
}
|
|
486
|
+
const mergedTools = (0, agentToolLoader_1.mergeTools)(localToolSchemas, refreshedTools, existingTools);
|
|
176
487
|
return {
|
|
177
488
|
...prompt,
|
|
178
489
|
message: {
|
|
@@ -190,6 +501,9 @@ class Agent {
|
|
|
190
501
|
};
|
|
191
502
|
}
|
|
192
503
|
catch (error) {
|
|
504
|
+
if (error instanceof agentToolLoader_1.LocalToolConfigurationError) {
|
|
505
|
+
throw error;
|
|
506
|
+
}
|
|
193
507
|
if (this.enableLogging) {
|
|
194
508
|
console.error('[Agent] Failed to refresh tools:', error);
|
|
195
509
|
}
|
|
@@ -199,45 +513,53 @@ class Agent {
|
|
|
199
513
|
getAllowedToolNames(prompt) {
|
|
200
514
|
var _a;
|
|
201
515
|
const metadataAllowedTools = (_a = prompt.metadata) === null || _a === void 0 ? void 0 : _a['allowedTools'];
|
|
202
|
-
if (
|
|
203
|
-
|
|
516
|
+
if (Array.isArray(metadataAllowedTools)) {
|
|
517
|
+
const allowedToolNames = metadataAllowedTools.filter((toolName) => typeof toolName === 'string' && toolName.length > 0);
|
|
518
|
+
if (allowedToolNames.length > 0) {
|
|
519
|
+
return allowedToolNames;
|
|
520
|
+
}
|
|
204
521
|
}
|
|
205
|
-
|
|
206
|
-
return allowedToolNames.length > 0 ? allowedToolNames : undefined;
|
|
522
|
+
return this.allowedTools;
|
|
207
523
|
}
|
|
208
524
|
getRecoverableResponseError(response) {
|
|
209
|
-
var _a
|
|
525
|
+
var _a;
|
|
210
526
|
const reactiveLayer = this.compactionOrchestrator.getReactiveLayer();
|
|
211
527
|
const candidateMessages = this.collectResponseMessages(response);
|
|
212
528
|
const recoverableMessage = candidateMessages.find((message) => reactiveLayer.isRecoverableError(message));
|
|
213
529
|
if (recoverableMessage) {
|
|
214
530
|
return recoverableMessage;
|
|
215
531
|
}
|
|
216
|
-
const finishReasons = [
|
|
217
|
-
response.finish_reason,
|
|
218
|
-
...((_a = response.choices) !== null && _a !== void 0 ? _a : []).map((choice) => choice.finish_reason),
|
|
219
|
-
].filter((reason) => typeof reason === 'string');
|
|
532
|
+
const finishReasons = [response.finish_reason].filter((reason) => typeof reason === 'string');
|
|
220
533
|
const hasLengthFinishReason = finishReasons.some((reason) => reason.toLowerCase() === 'length');
|
|
221
|
-
const hasToolCalls = ((
|
|
222
|
-
((_d = response.choices) !== null && _d !== void 0 ? _d : []).some((choice) => { var _a, _b, _c; return ((_c = (_b = (_a = choice.message) === null || _a === void 0 ? void 0 : _a.tool_calls) === null || _b === void 0 ? void 0 : _b.length) !== null && _c !== void 0 ? _c : 0) > 0; });
|
|
534
|
+
const hasToolCalls = ((_a = response.items) !== null && _a !== void 0 ? _a : []).some((item) => (item === null || item === void 0 ? void 0 : item.type) === 'function_call');
|
|
223
535
|
if (hasLengthFinishReason && candidateMessages.length === 0 && !hasToolCalls) {
|
|
224
536
|
return 'Too many tokens or token limit reached before producing usable output.';
|
|
225
537
|
}
|
|
226
538
|
return null;
|
|
227
539
|
}
|
|
228
540
|
collectResponseMessages(response) {
|
|
229
|
-
var _a
|
|
541
|
+
var _a;
|
|
230
542
|
const messages = [];
|
|
231
|
-
|
|
232
|
-
|
|
543
|
+
const outputText = response.output_text || response.content;
|
|
544
|
+
if (typeof outputText === 'string' && outputText.trim().length > 0) {
|
|
545
|
+
messages.push(outputText.trim());
|
|
233
546
|
}
|
|
234
|
-
for (const
|
|
235
|
-
if (
|
|
236
|
-
|
|
237
|
-
|
|
547
|
+
for (const item of (_a = response.items) !== null && _a !== void 0 ? _a : []) {
|
|
548
|
+
if ((item === null || item === void 0 ? void 0 : item.type) === 'message' && item.role === 'assistant') {
|
|
549
|
+
const content = item.content;
|
|
550
|
+
if (typeof content === 'string' && content.trim().length > 0) {
|
|
551
|
+
messages.push(content.trim());
|
|
552
|
+
}
|
|
238
553
|
}
|
|
239
554
|
}
|
|
240
555
|
return messages;
|
|
241
556
|
}
|
|
242
557
|
}
|
|
243
558
|
exports.Agent = Agent;
|
|
559
|
+
function createAgent(options) {
|
|
560
|
+
const normalizedOptions = { ...options };
|
|
561
|
+
if (normalizedOptions.instructions === undefined && options.systemPrompt !== undefined) {
|
|
562
|
+
normalizedOptions.instructions = options.systemPrompt;
|
|
563
|
+
}
|
|
564
|
+
return new Agent(normalizedOptions);
|
|
565
|
+
}
|
|
@@ -2,14 +2,14 @@
|
|
|
2
2
|
* Tool creation utilities for the Unified Agent Framework
|
|
3
3
|
*/
|
|
4
4
|
import { z, ZodType } from 'zod';
|
|
5
|
-
import type { OpenAITool } from '
|
|
6
|
-
import { ToolConfig, ToolInterface } from '@codebolt/types/agent';
|
|
5
|
+
import type { Tool as OpenAITool } from '@codebolt/types/sdk';
|
|
6
|
+
import { AgentLocalToolExecutionContext, ToolConfig, ToolInterface } from '@codebolt/types/agent';
|
|
7
7
|
export declare class Tool implements ToolInterface {
|
|
8
8
|
id: string;
|
|
9
9
|
description: string;
|
|
10
10
|
inputSchema: ZodType;
|
|
11
11
|
outputSchema?: ZodType | undefined;
|
|
12
|
-
executionFunction: (context:
|
|
12
|
+
executionFunction: (context: AgentLocalToolExecutionContext) => unknown;
|
|
13
13
|
constructor(config: ToolConfig);
|
|
14
14
|
execute(input: unknown, context: unknown): Promise<{
|
|
15
15
|
success: boolean;
|
|
@@ -31,6 +31,20 @@ export declare class Tool implements ToolInterface {
|
|
|
31
31
|
* @returns OpenAI function specification
|
|
32
32
|
*/
|
|
33
33
|
toOpenAITool(): OpenAITool;
|
|
34
|
+
private getZodDef;
|
|
35
|
+
private getZodTypeName;
|
|
36
|
+
private getZodDescription;
|
|
37
|
+
private applyDescription;
|
|
38
|
+
private withDescriptionFrom;
|
|
39
|
+
private getObjectShape;
|
|
40
|
+
/**
|
|
41
|
+
* Converts a Zod schema to JSON Schema format for OpenAI functions.
|
|
42
|
+
*
|
|
43
|
+
* The converter intentionally checks Zod's schema metadata instead of
|
|
44
|
+
* relying only on instanceof. Local tools are often created in another
|
|
45
|
+
* workspace package with its own zod module instance, which makes
|
|
46
|
+
* instanceof checks fail even though the schema is valid.
|
|
47
|
+
*/
|
|
34
48
|
private zodSchemaToJsonSchema;
|
|
35
49
|
/**
|
|
36
50
|
* Converts individual Zod types to JSON Schema
|