@codebolt/agent 6.1.20 → 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/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 +10 -0
- package/dist/unified/agent/agent.js +166 -15
- package/dist/unified/agent/tools.d.ts +14 -0
- 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 +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 +222 -58
- 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.js +29 -24
- package/package.json +5 -1
|
@@ -1,7 +1,12 @@
|
|
|
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;
|
|
4
7
|
exports.createAgent = createAgent;
|
|
8
|
+
const codeboltjs_1 = __importDefault(require("@codebolt/codeboltjs"));
|
|
9
|
+
const crypto_1 = require("crypto");
|
|
5
10
|
const base_1 = require("../base");
|
|
6
11
|
const agentStep_1 = require("../base/agentStep");
|
|
7
12
|
const responseExecutor_1 = require("../base/responseExecutor");
|
|
@@ -118,17 +123,24 @@ class Agent {
|
|
|
118
123
|
this.maxTurns = (_l = (_k = config.maxTurns) !== null && _k !== void 0 ? _k : config.maxIterations) !== null && _l !== void 0 ? _l : 25;
|
|
119
124
|
this.localToolSchemas = localToolRegistry.schemas;
|
|
120
125
|
this.localToolsByExecutionName = localToolRegistry.byExecutionName;
|
|
126
|
+
this.runtimeToolSetId = `agent-${(0, crypto_1.randomUUID)()}`;
|
|
121
127
|
}
|
|
122
128
|
async run(message, options) {
|
|
123
129
|
var _a, _b;
|
|
124
130
|
try {
|
|
131
|
+
await this.registerRuntimeToolsForSearch();
|
|
125
132
|
const reqMessage = typeof message === 'string'
|
|
126
133
|
? createDefaultUserMessage(message)
|
|
127
134
|
: message;
|
|
128
135
|
let prompt;
|
|
129
136
|
const contextToUse = this.resolveRunContext(options);
|
|
130
137
|
if (contextToUse) {
|
|
131
|
-
|
|
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);
|
|
132
144
|
}
|
|
133
145
|
else {
|
|
134
146
|
const promptGenerator = new base_1.InitialPromptGenerator({
|
|
@@ -138,6 +150,7 @@ class Agent {
|
|
|
138
150
|
});
|
|
139
151
|
prompt = await promptGenerator.processMessage(reqMessage);
|
|
140
152
|
}
|
|
153
|
+
prompt = await this.hydratePromptFromServerCompaction(reqMessage, prompt);
|
|
141
154
|
let completed = false;
|
|
142
155
|
let turnNumber = 0;
|
|
143
156
|
let finalMessage;
|
|
@@ -224,6 +237,37 @@ class Agent {
|
|
|
224
237
|
error: errorMessage
|
|
225
238
|
};
|
|
226
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
|
+
}
|
|
227
271
|
}
|
|
228
272
|
async processMessage(message, options) {
|
|
229
273
|
return this.run(message, options);
|
|
@@ -259,6 +303,9 @@ class Agent {
|
|
|
259
303
|
}
|
|
260
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;
|
|
261
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
|
+
}
|
|
262
309
|
isProcessedMessage(value) {
|
|
263
310
|
return 'message' in value && 'metadata' in value;
|
|
264
311
|
}
|
|
@@ -280,6 +327,111 @@ class Agent {
|
|
|
280
327
|
},
|
|
281
328
|
};
|
|
282
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
|
+
}
|
|
283
435
|
async tryRecoverPrompt(prompt, error) {
|
|
284
436
|
const errorMessage = error instanceof Error ? error.message : String(error);
|
|
285
437
|
if (!this.compactionOrchestrator.getReactiveLayer().isRecoverableError(errorMessage)) {
|
|
@@ -370,35 +522,34 @@ class Agent {
|
|
|
370
522
|
return this.allowedTools;
|
|
371
523
|
}
|
|
372
524
|
getRecoverableResponseError(response) {
|
|
373
|
-
var _a
|
|
525
|
+
var _a;
|
|
374
526
|
const reactiveLayer = this.compactionOrchestrator.getReactiveLayer();
|
|
375
527
|
const candidateMessages = this.collectResponseMessages(response);
|
|
376
528
|
const recoverableMessage = candidateMessages.find((message) => reactiveLayer.isRecoverableError(message));
|
|
377
529
|
if (recoverableMessage) {
|
|
378
530
|
return recoverableMessage;
|
|
379
531
|
}
|
|
380
|
-
const finishReasons = [
|
|
381
|
-
response.finish_reason,
|
|
382
|
-
...((_a = response.choices) !== null && _a !== void 0 ? _a : []).map((choice) => choice.finish_reason),
|
|
383
|
-
].filter((reason) => typeof reason === 'string');
|
|
532
|
+
const finishReasons = [response.finish_reason].filter((reason) => typeof reason === 'string');
|
|
384
533
|
const hasLengthFinishReason = finishReasons.some((reason) => reason.toLowerCase() === 'length');
|
|
385
|
-
const hasToolCalls = ((
|
|
386
|
-
((_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');
|
|
387
535
|
if (hasLengthFinishReason && candidateMessages.length === 0 && !hasToolCalls) {
|
|
388
536
|
return 'Too many tokens or token limit reached before producing usable output.';
|
|
389
537
|
}
|
|
390
538
|
return null;
|
|
391
539
|
}
|
|
392
540
|
collectResponseMessages(response) {
|
|
393
|
-
var _a
|
|
541
|
+
var _a;
|
|
394
542
|
const messages = [];
|
|
395
|
-
|
|
396
|
-
|
|
543
|
+
const outputText = response.output_text || response.content;
|
|
544
|
+
if (typeof outputText === 'string' && outputText.trim().length > 0) {
|
|
545
|
+
messages.push(outputText.trim());
|
|
397
546
|
}
|
|
398
|
-
for (const
|
|
399
|
-
if (
|
|
400
|
-
|
|
401
|
-
|
|
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
|
+
}
|
|
402
553
|
}
|
|
403
554
|
}
|
|
404
555
|
return messages;
|
|
@@ -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
|
|
@@ -114,14 +114,54 @@ class Tool {
|
|
|
114
114
|
}
|
|
115
115
|
};
|
|
116
116
|
}
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
117
|
+
getZodDef(zodType) {
|
|
118
|
+
return (zodType._def || {});
|
|
119
|
+
}
|
|
120
|
+
getZodTypeName(zodType) {
|
|
121
|
+
const def = this.getZodDef(zodType);
|
|
122
|
+
return typeof def['typeName'] === 'string' ? def['typeName'] : undefined;
|
|
123
|
+
}
|
|
124
|
+
getZodDescription(zodType) {
|
|
125
|
+
const def = this.getZodDef(zodType);
|
|
126
|
+
return typeof def['description'] === 'string' ? def['description'] : undefined;
|
|
127
|
+
}
|
|
128
|
+
applyDescription(jsonSchema, zodType) {
|
|
129
|
+
const description = this.getZodDescription(zodType);
|
|
130
|
+
if (description) {
|
|
131
|
+
jsonSchema['description'] = description;
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
withDescriptionFrom(jsonSchema, zodType) {
|
|
135
|
+
if (jsonSchema && typeof jsonSchema === 'object' && !Array.isArray(jsonSchema)) {
|
|
136
|
+
this.applyDescription(jsonSchema, zodType);
|
|
137
|
+
}
|
|
138
|
+
return jsonSchema;
|
|
139
|
+
}
|
|
140
|
+
getObjectShape(zodType) {
|
|
141
|
+
const directShape = zodType.shape;
|
|
142
|
+
if (directShape && typeof directShape === 'object') {
|
|
143
|
+
return directShape;
|
|
144
|
+
}
|
|
145
|
+
const defShape = this.getZodDef(zodType)['shape'];
|
|
146
|
+
if (typeof defShape === 'function') {
|
|
147
|
+
return defShape();
|
|
148
|
+
}
|
|
149
|
+
if (defShape && typeof defShape === 'object') {
|
|
150
|
+
return defShape;
|
|
151
|
+
}
|
|
152
|
+
return {};
|
|
153
|
+
}
|
|
154
|
+
/**
|
|
155
|
+
* Converts a Zod schema to JSON Schema format for OpenAI functions.
|
|
156
|
+
*
|
|
157
|
+
* The converter intentionally checks Zod's schema metadata instead of
|
|
158
|
+
* relying only on instanceof. Local tools are often created in another
|
|
159
|
+
* workspace package with its own zod module instance, which makes
|
|
160
|
+
* instanceof checks fail even though the schema is valid.
|
|
161
|
+
*/
|
|
120
162
|
zodSchemaToJsonSchema(schema) {
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
if (schema instanceof zod_1.z.ZodObject) {
|
|
124
|
-
const shape = schema.shape;
|
|
163
|
+
if (schema instanceof zod_1.z.ZodObject || this.getZodTypeName(schema) === 'ZodObject') {
|
|
164
|
+
const shape = this.getObjectShape(schema);
|
|
125
165
|
const properties = {};
|
|
126
166
|
const required = [];
|
|
127
167
|
for (const [key, value] of Object.entries(shape)) {
|
|
@@ -131,12 +171,14 @@ class Tool {
|
|
|
131
171
|
required.push(key);
|
|
132
172
|
}
|
|
133
173
|
}
|
|
134
|
-
|
|
174
|
+
const result = {
|
|
135
175
|
type: 'object',
|
|
136
176
|
properties,
|
|
137
177
|
required: required.length > 0 ? required : undefined,
|
|
138
178
|
additionalProperties: false
|
|
139
179
|
};
|
|
180
|
+
this.applyDescription(result, schema);
|
|
181
|
+
return result;
|
|
140
182
|
}
|
|
141
183
|
return this.zodTypeToJsonSchema(schema);
|
|
142
184
|
}
|
|
@@ -144,14 +186,12 @@ class Tool {
|
|
|
144
186
|
* Converts individual Zod types to JSON Schema
|
|
145
187
|
*/
|
|
146
188
|
zodTypeToJsonSchema(zodType) {
|
|
147
|
-
|
|
189
|
+
const typeName = this.getZodTypeName(zodType);
|
|
190
|
+
if (zodType instanceof zod_1.z.ZodString || typeName === 'ZodString') {
|
|
148
191
|
const result = { type: 'string' };
|
|
149
|
-
|
|
150
|
-
if (zodType._def.description) {
|
|
151
|
-
result.description = zodType._def.description;
|
|
152
|
-
}
|
|
192
|
+
this.applyDescription(result, zodType);
|
|
153
193
|
// Add constraints
|
|
154
|
-
const checks = zodType
|
|
194
|
+
const checks = this.getZodDef(zodType)['checks'] || [];
|
|
155
195
|
for (const check of checks) {
|
|
156
196
|
switch (check.kind) {
|
|
157
197
|
case 'min':
|
|
@@ -173,12 +213,10 @@ class Tool {
|
|
|
173
213
|
}
|
|
174
214
|
return result;
|
|
175
215
|
}
|
|
176
|
-
if (zodType instanceof zod_1.z.ZodNumber) {
|
|
216
|
+
if (zodType instanceof zod_1.z.ZodNumber || typeName === 'ZodNumber') {
|
|
177
217
|
const result = { type: 'number' };
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
}
|
|
181
|
-
const checks = zodType._def.checks || [];
|
|
218
|
+
this.applyDescription(result, zodType);
|
|
219
|
+
const checks = this.getZodDef(zodType)['checks'] || [];
|
|
182
220
|
for (const check of checks) {
|
|
183
221
|
switch (check.kind) {
|
|
184
222
|
case 'min':
|
|
@@ -194,22 +232,18 @@ class Tool {
|
|
|
194
232
|
}
|
|
195
233
|
return result;
|
|
196
234
|
}
|
|
197
|
-
if (zodType instanceof zod_1.z.ZodBoolean) {
|
|
235
|
+
if (zodType instanceof zod_1.z.ZodBoolean || typeName === 'ZodBoolean') {
|
|
198
236
|
const result = { type: 'boolean' };
|
|
199
|
-
|
|
200
|
-
result.description = zodType._def.description;
|
|
201
|
-
}
|
|
237
|
+
this.applyDescription(result, zodType);
|
|
202
238
|
return result;
|
|
203
239
|
}
|
|
204
|
-
if (zodType instanceof zod_1.z.ZodArray) {
|
|
240
|
+
if (zodType instanceof zod_1.z.ZodArray || typeName === 'ZodArray') {
|
|
205
241
|
const result = {
|
|
206
242
|
type: 'array',
|
|
207
|
-
items: this.zodTypeToJsonSchema(zodType
|
|
243
|
+
items: this.zodTypeToJsonSchema(this.getZodDef(zodType)['type'])
|
|
208
244
|
};
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
}
|
|
212
|
-
const checks = zodType._def.checks || [];
|
|
245
|
+
this.applyDescription(result, zodType);
|
|
246
|
+
const checks = this.getZodDef(zodType)['checks'] || [];
|
|
213
247
|
for (const check of checks) {
|
|
214
248
|
switch (check.kind) {
|
|
215
249
|
case 'min':
|
|
@@ -222,48 +256,45 @@ class Tool {
|
|
|
222
256
|
}
|
|
223
257
|
return result;
|
|
224
258
|
}
|
|
225
|
-
if (zodType instanceof zod_1.z.ZodEnum) {
|
|
259
|
+
if (zodType instanceof zod_1.z.ZodEnum || typeName === 'ZodEnum') {
|
|
226
260
|
const result = {
|
|
227
261
|
type: 'string',
|
|
228
|
-
enum: zodType
|
|
262
|
+
enum: this.getZodDef(zodType)['values']
|
|
229
263
|
};
|
|
230
|
-
|
|
231
|
-
result.description = zodType._def.description;
|
|
232
|
-
}
|
|
264
|
+
this.applyDescription(result, zodType);
|
|
233
265
|
return result;
|
|
234
266
|
}
|
|
235
|
-
if (zodType instanceof zod_1.z.ZodLiteral) {
|
|
267
|
+
if (zodType instanceof zod_1.z.ZodLiteral || typeName === 'ZodLiteral') {
|
|
268
|
+
const value = this.getZodDef(zodType)['value'];
|
|
236
269
|
const result = {
|
|
237
|
-
type: typeof
|
|
238
|
-
enum: [
|
|
270
|
+
type: typeof value,
|
|
271
|
+
enum: [value]
|
|
239
272
|
};
|
|
240
|
-
|
|
241
|
-
result.description = zodType._def.description;
|
|
242
|
-
}
|
|
273
|
+
this.applyDescription(result, zodType);
|
|
243
274
|
return result;
|
|
244
275
|
}
|
|
245
|
-
if (zodType instanceof zod_1.z.ZodUnion) {
|
|
246
|
-
const options = zodType
|
|
276
|
+
if (zodType instanceof zod_1.z.ZodUnion || typeName === 'ZodUnion') {
|
|
277
|
+
const options = this.getZodDef(zodType)['options'] || [];
|
|
247
278
|
const anyOf = options.map((option) => this.zodTypeToJsonSchema(option));
|
|
248
279
|
const result = { anyOf };
|
|
249
|
-
|
|
250
|
-
result.description = zodType._def.description;
|
|
251
|
-
}
|
|
280
|
+
this.applyDescription(result, zodType);
|
|
252
281
|
return result;
|
|
253
282
|
}
|
|
254
|
-
if (zodType instanceof zod_1.z.ZodOptional) {
|
|
255
|
-
return this.zodTypeToJsonSchema(zodType
|
|
283
|
+
if (zodType instanceof zod_1.z.ZodOptional || typeName === 'ZodOptional') {
|
|
284
|
+
return this.withDescriptionFrom(this.zodTypeToJsonSchema(this.getZodDef(zodType)['innerType']), zodType);
|
|
256
285
|
}
|
|
257
|
-
if (zodType instanceof zod_1.z.ZodNullable) {
|
|
258
|
-
const innerSchema = this.zodTypeToJsonSchema(zodType
|
|
259
|
-
|
|
286
|
+
if (zodType instanceof zod_1.z.ZodNullable || typeName === 'ZodNullable') {
|
|
287
|
+
const innerSchema = this.zodTypeToJsonSchema(this.getZodDef(zodType)['innerType']);
|
|
288
|
+
const result = {
|
|
260
289
|
anyOf: [
|
|
261
290
|
innerSchema,
|
|
262
291
|
{ type: 'null' }
|
|
263
292
|
]
|
|
264
293
|
};
|
|
294
|
+
this.applyDescription(result, zodType);
|
|
295
|
+
return result;
|
|
265
296
|
}
|
|
266
|
-
if (zodType instanceof zod_1.z.ZodObject) {
|
|
297
|
+
if (zodType instanceof zod_1.z.ZodObject || typeName === 'ZodObject') {
|
|
267
298
|
return this.zodSchemaToJsonSchema(zodType);
|
|
268
299
|
}
|
|
269
300
|
// Fallback for unknown types
|
|
@@ -17,6 +17,7 @@ export declare class AgentStep implements AgentStepInterface {
|
|
|
17
17
|
*/
|
|
18
18
|
executeStep(originalRequest: FlatUserMessage, createdMessage: ProcessedMessage): Promise<AgentStepOutput>;
|
|
19
19
|
private generateResponse;
|
|
20
|
+
private extractCompletion;
|
|
20
21
|
/**
|
|
21
22
|
* Update LLM configuration
|
|
22
23
|
*/
|
|
@@ -36,11 +36,11 @@ class AgentStep {
|
|
|
36
36
|
message: (0, promptContext_1.buildInferenceParams)(preparedMessage),
|
|
37
37
|
};
|
|
38
38
|
const rawLLMResponse = await this.generateResponse(actualMessageSentToLLM.message);
|
|
39
|
-
const
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
let modifiedMessage = (0, promptContext_1.appendTranscriptMessages)(preparedMessage,
|
|
39
|
+
const assistantResponseItems = ((_a = rawLLMResponse.items) !== null && _a !== void 0 ? _a : [])
|
|
40
|
+
.filter((item) => (((item === null || item === void 0 ? void 0 : item.type) === 'message' && item.role === 'assistant') ||
|
|
41
|
+
(item === null || item === void 0 ? void 0 : item.type) === 'function_call' ||
|
|
42
|
+
(item === null || item === void 0 ? void 0 : item.type) === 'tool_search_call'));
|
|
43
|
+
let modifiedMessage = (0, promptContext_1.appendTranscriptMessages)(preparedMessage, assistantResponseItems);
|
|
44
44
|
for (const postInferenceProcessor of this.postInferenceProcessors) {
|
|
45
45
|
try {
|
|
46
46
|
modifiedMessage = await postInferenceProcessor.modify(actualMessageSentToLLM, rawLLMResponse, modifiedMessage);
|
|
@@ -63,9 +63,14 @@ class AgentStep {
|
|
|
63
63
|
}
|
|
64
64
|
}
|
|
65
65
|
async generateResponse(messageForLLM) {
|
|
66
|
-
var _a, _b, _c, _d;
|
|
66
|
+
var _a, _b, _c, _d, _e;
|
|
67
67
|
const response = await codeboltjs_1.default.llm.inference(messageForLLM);
|
|
68
|
-
const completion = response
|
|
68
|
+
const completion = this.extractCompletion(response);
|
|
69
|
+
if (!completion) {
|
|
70
|
+
const responseType = (response === null || response === void 0 ? void 0 : response.type) || 'unknown';
|
|
71
|
+
const responseMessage = (response === null || response === void 0 ? void 0 : response.message) || ((_a = response === null || response === void 0 ? void 0 : response.error) === null || _a === void 0 ? void 0 : _a.message) || 'No completion returned from LLM service';
|
|
72
|
+
throw new Error(`LLM response did not include completion data (${responseType}): ${responseMessage}`);
|
|
73
|
+
}
|
|
69
74
|
// Add tokenLimit and maxOutputTokens to completion object if available in response
|
|
70
75
|
if (completion) {
|
|
71
76
|
// Check if tokenLimit exists at response level or in completion
|
|
@@ -82,21 +87,44 @@ class AgentStep {
|
|
|
82
87
|
completion.compactionRequired = response.compactionRequired;
|
|
83
88
|
}
|
|
84
89
|
// Also check inside completion object itself (from LLM provider response)
|
|
85
|
-
if (((
|
|
90
|
+
if (((_b = completion.completion) === null || _b === void 0 ? void 0 : _b.tokenLimit) !== undefined) {
|
|
86
91
|
completion.tokenLimit = completion.completion.tokenLimit;
|
|
87
92
|
}
|
|
88
|
-
if (((
|
|
93
|
+
if (((_c = completion.completion) === null || _c === void 0 ? void 0 : _c.maxOutputTokens) !== undefined) {
|
|
89
94
|
completion.maxOutputTokens = completion.completion.maxOutputTokens;
|
|
90
95
|
}
|
|
91
|
-
if (((
|
|
96
|
+
if (((_d = completion.completion) === null || _d === void 0 ? void 0 : _d.contextCompaction) !== undefined) {
|
|
92
97
|
completion.contextCompaction = completion.completion.contextCompaction;
|
|
93
98
|
}
|
|
94
|
-
if (((
|
|
99
|
+
if (((_e = completion.completion) === null || _e === void 0 ? void 0 : _e.compactionRequired) !== undefined) {
|
|
95
100
|
completion.compactionRequired = completion.completion.compactionRequired;
|
|
96
101
|
}
|
|
97
102
|
}
|
|
98
103
|
return completion;
|
|
99
104
|
}
|
|
105
|
+
extractCompletion(response) {
|
|
106
|
+
if (!response || typeof response !== 'object') {
|
|
107
|
+
return undefined;
|
|
108
|
+
}
|
|
109
|
+
if (response.completion && typeof response.completion === 'object') {
|
|
110
|
+
return response.completion;
|
|
111
|
+
}
|
|
112
|
+
if (Array.isArray(response.items) || typeof response.output_text === 'string') {
|
|
113
|
+
return {
|
|
114
|
+
...response,
|
|
115
|
+
items: Array.isArray(response.items) ? response.items : [],
|
|
116
|
+
output_text: response.output_text || response.message || response.content || '',
|
|
117
|
+
};
|
|
118
|
+
}
|
|
119
|
+
if (typeof response.message === 'string' || typeof response.content === 'string') {
|
|
120
|
+
return {
|
|
121
|
+
...response,
|
|
122
|
+
items: [],
|
|
123
|
+
output_text: response.message || response.content || '',
|
|
124
|
+
};
|
|
125
|
+
}
|
|
126
|
+
return undefined;
|
|
127
|
+
}
|
|
100
128
|
/**
|
|
101
129
|
* Update LLM configuration
|
|
102
130
|
*/
|
|
@@ -8,9 +8,11 @@ export declare class InitialPromptGenerator implements InitialPromptGeneratorInt
|
|
|
8
8
|
private metaData;
|
|
9
9
|
private enableLogging;
|
|
10
10
|
private baseSystemPrompt?;
|
|
11
|
+
private initialPrompt?;
|
|
11
12
|
constructor(options?: {
|
|
12
13
|
processors?: MessageModifier[];
|
|
13
14
|
baseSystemPrompt?: string;
|
|
15
|
+
initialPrompt?: ProcessedMessage;
|
|
14
16
|
metaData?: Record<string, unknown>;
|
|
15
17
|
enableLogging?: boolean;
|
|
16
18
|
templating?: boolean;
|