@codebolt/agent 5.0.1 → 5.0.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/agent.d.ts +86 -0
- package/dist/agent.js +324 -0
- package/dist/builderpattern/agent.js +2 -2
- package/dist/builderpattern/followupquestionbuilder.js +1 -1
- package/dist/builderpattern/llmoutputhandler.js +7 -7
- package/dist/builderpattern/usermessage.d.ts +1 -6
- package/dist/composablepattern/document.js +35 -2
- package/dist/followupquestionbuilder.d.ts +75 -0
- package/dist/followupquestionbuilder.js +197 -0
- package/dist/index.d.ts +14 -0
- package/dist/index.js +23 -0
- package/dist/llmoutputhandler.d.ts +102 -0
- package/dist/llmoutputhandler.js +452 -0
- package/dist/processor/types/interfaces.d.ts +21 -44
- package/dist/processor-pieces/index.d.ts +3 -0
- package/dist/processor-pieces/index.js +3 -0
- package/dist/processor-pieces/messageModifiers/addToolsListMessageModifier.d.ts +5 -14
- package/dist/processor-pieces/messageModifiers/addToolsListMessageModifier.js +40 -41
- package/dist/processor-pieces/messageModifiers/baseContextMessageModifier.d.ts +13 -5
- package/dist/processor-pieces/messageModifiers/baseContextMessageModifier.js +58 -50
- package/dist/processor-pieces/messageModifiers/baseSystemInstructionMessageModifier.d.ts +8 -6
- package/dist/processor-pieces/messageModifiers/baseSystemInstructionMessageModifier.js +22 -66
- package/dist/processor-pieces/messageModifiers/handleUrlMessageModifier.d.ts +2 -3
- package/dist/processor-pieces/messageModifiers/handleUrlMessageModifier.js +2 -1
- package/dist/processor-pieces/messageModifiers/imageAttachmentMessageModifier.d.ts +2 -3
- package/dist/processor-pieces/messageModifiers/imageAttachmentMessageModifier.js +2 -1
- package/dist/processor-pieces/messageModifiers/toolInjectionModifier.js +4 -1
- package/dist/processor-pieces/messageModifiers/workingDirectoryMessageModifier.d.ts +2 -18
- package/dist/processor-pieces/messageModifiers/workingDirectoryMessageModifier.js +93 -291
- package/dist/processor-pieces/postToolCallProcessors/conversationCompactorModifier.d.ts +18 -0
- package/dist/processor-pieces/postToolCallProcessors/conversationCompactorModifier.js +41 -0
- package/dist/processor-pieces/postToolCallProcessors/index.d.ts +2 -1
- package/dist/processor-pieces/postToolCallProcessors/index.js +5 -0
- package/dist/processor-pieces/preInferenceProcessors/index.d.ts +1 -1
- package/dist/processor-pieces/preInferenceProcessors/index.js +3 -0
- package/dist/processor-pieces/pretoolCallProcessors/index.d.ts +2 -1
- package/dist/processor-pieces/pretoolCallProcessors/index.js +5 -0
- package/dist/processor-pieces/pretoolCallProcessors/toolParameterModifier.d.ts +21 -0
- package/dist/processor-pieces/pretoolCallProcessors/toolParameterModifier.js +32 -0
- package/dist/processor-pieces/pretoolCallProcessors/toolValidationModifier.d.ts +16 -0
- package/dist/processor-pieces/pretoolCallProcessors/toolValidationModifier.js +37 -0
- package/dist/promptbuilder.d.ts +382 -0
- package/dist/promptbuilder.js +805 -0
- package/dist/systemprompt.d.ts +20 -0
- package/dist/systemprompt.js +48 -0
- package/dist/taskInstruction.d.ts +37 -0
- package/dist/taskInstruction.js +57 -0
- package/dist/unified/agent/agent.js +2 -2
- package/dist/unified/agent/codeboltAgent.d.ts +122 -0
- package/dist/unified/agent/codeboltAgent.js +210 -0
- package/dist/unified/base/initialPromptGenerator.js +18 -4
- package/dist/unified/base/responseExecutor.d.ts +3 -2
- package/dist/unified/base/responseExecutor.js +100 -56
- package/dist/unified/index.d.ts +4 -0
- package/dist/unified/index.js +12 -1
- package/dist/usermessage.d.ts +70 -0
- package/dist/usermessage.js +123 -0
- package/package.json +5 -5
- package/dist/local-tools/fileTools.d.ts +0 -26
- package/dist/local-tools/fileTools.js +0 -162
- package/dist/processor-pieces/additionalModifiers/argumentProcessorModifier.d.ts +0 -13
- package/dist/processor-pieces/additionalModifiers/argumentProcessorModifier.js +0 -68
- package/dist/processor-pieces/additionalModifiers/atFileProcessorModifier.d.ts +0 -15
- package/dist/processor-pieces/additionalModifiers/atFileProcessorModifier.js +0 -212
- package/dist/processor-pieces/additionalModifiers/chatCompressionModifier.d.ts +0 -19
- package/dist/processor-pieces/additionalModifiers/chatCompressionModifier.js +0 -110
- package/dist/processor-pieces/additionalModifiers/chatRecordingModifier.d.ts +0 -23
- package/dist/processor-pieces/additionalModifiers/chatRecordingModifier.js +0 -173
- package/dist/processor-pieces/additionalModifiers/coreSystemPromptModifier.d.ts +0 -14
- package/dist/processor-pieces/additionalModifiers/coreSystemPromptModifier.js +0 -93
- package/dist/processor-pieces/additionalModifiers/directoryContextModifier.d.ts +0 -30
- package/dist/processor-pieces/additionalModifiers/directoryContextModifier.js +0 -330
- package/dist/processor-pieces/additionalModifiers/environmentContextModifier.d.ts +0 -26
- package/dist/processor-pieces/additionalModifiers/environmentContextModifier.js +0 -269
- package/dist/processor-pieces/additionalModifiers/fallbackHandlerModifier.d.ts +0 -38
- package/dist/processor-pieces/additionalModifiers/fallbackHandlerModifier.js +0 -217
- package/dist/processor-pieces/additionalModifiers/ideContextModifier.d.ts +0 -34
- package/dist/processor-pieces/additionalModifiers/ideContextModifier.js +0 -147
- package/dist/processor-pieces/additionalModifiers/index.d.ts +0 -12
- package/dist/processor-pieces/additionalModifiers/index.js +0 -28
- package/dist/processor-pieces/additionalModifiers/loopDetectionModifier.d.ts +0 -29
- package/dist/processor-pieces/additionalModifiers/loopDetectionModifier.js +0 -155
- package/dist/processor-pieces/additionalModifiers/memoryImportModifier.d.ts +0 -15
- package/dist/processor-pieces/additionalModifiers/memoryImportModifier.js +0 -129
- package/dist/processor-pieces/additionalModifiers/shellProcessorModifier.d.ts +0 -25
- package/dist/processor-pieces/additionalModifiers/shellProcessorModifier.js +0 -169
- package/dist/processor-pieces/additionalModifiers/toolInjectionModifier.d.ts +0 -20
- package/dist/processor-pieces/additionalModifiers/toolInjectionModifier.js +0 -152
- package/dist/processor-pieces/messageModifiers/addCurrentDirectoryRootFilesModifier.d.ts +0 -7
- package/dist/processor-pieces/messageModifiers/addCurrentDirectoryRootFilesModifier.js +0 -93
- package/dist/processor-pieces/messageModifiers/advancedSystemInstructionMessageModifier.d.ts +0 -9
- package/dist/processor-pieces/messageModifiers/advancedSystemInstructionMessageModifier.js +0 -77
- package/dist/processor-pieces/messageModifiers/chatCompressionModifier.d.ts +0 -19
- package/dist/processor-pieces/messageModifiers/chatCompressionModifier.js +0 -110
- package/dist/processor-pieces/messageModifiers/chatHistoryMessageModifier.example.d.ts +0 -20
- package/dist/processor-pieces/messageModifiers/chatHistoryMessageModifier.example.js +0 -124
- package/dist/processor-pieces/messageModifiers/environmentContext.d.ts +0 -41
- package/dist/processor-pieces/messageModifiers/environmentContext.js +0 -355
- package/dist/processor-pieces/messageModifiers/mentionedFilesModifier.d.ts +0 -23
- package/dist/processor-pieces/messageModifiers/mentionedFilesModifier.js +0 -207
- package/dist/processor-pieces/messageModifiers/simpleMessageModifier.d.ts +0 -7
- package/dist/processor-pieces/messageModifiers/simpleMessageModifier.js +0 -26
- package/dist/processor-pieces/postInference/llmOutputValidityProcessor.d.ts +0 -1
- package/dist/processor-pieces/postInference/llmOutputValidityProcessor.js +0 -2
- package/dist/processor-pieces/postInferenceProcessors/checkForNoToolCall.d.ts +0 -7
- package/dist/processor-pieces/postInferenceProcessors/checkForNoToolCall.js +0 -24
- package/dist/processor-pieces/postToolCall/advancedLoopDetectionProcessor.d.ts +0 -41
- package/dist/processor-pieces/postToolCall/advancedLoopDetectionProcessor.js +0 -197
- package/dist/processor-pieces/postToolCall/chatCompressionProcessor.d.ts +0 -26
- package/dist/processor-pieces/postToolCall/chatCompressionProcessor.js +0 -90
- package/dist/processor-pieces/postToolCall/chatRecordingProcessor.d.ts +0 -81
- package/dist/processor-pieces/postToolCall/chatRecordingProcessor.js +0 -329
- package/dist/processor-pieces/postToolCall/contextManagementProcessor.d.ts +0 -40
- package/dist/processor-pieces/postToolCall/contextManagementProcessor.js +0 -272
- package/dist/processor-pieces/postToolCall/conversationCompactorProcessor.d.ts +0 -49
- package/dist/processor-pieces/postToolCall/conversationCompactorProcessor.js +0 -291
- package/dist/processor-pieces/postToolCall/conversationContinuityProcessor.d.ts +0 -49
- package/dist/processor-pieces/postToolCall/conversationContinuityProcessor.js +0 -293
- package/dist/processor-pieces/postToolCall/followUpConversationProcessor.d.ts +0 -47
- package/dist/processor-pieces/postToolCall/followUpConversationProcessor.js +0 -249
- package/dist/processor-pieces/postToolCall/loopDetectionProcessor.d.ts +0 -30
- package/dist/processor-pieces/postToolCall/loopDetectionProcessor.js +0 -137
- package/dist/processor-pieces/postToolCall/responseValidationProcessor.d.ts +0 -30
- package/dist/processor-pieces/postToolCall/responseValidationProcessor.js +0 -228
- package/dist/processor-pieces/postToolCall/telemetryProcessor.d.ts +0 -99
- package/dist/processor-pieces/postToolCall/telemetryProcessor.js +0 -278
- package/dist/processor-pieces/postToolCall/tokenManagementProcessor.d.ts +0 -37
- package/dist/processor-pieces/postToolCall/tokenManagementProcessor.js +0 -178
- package/dist/processor-pieces/postToolCall/toolExecutionProcessor.d.ts +0 -43
- package/dist/processor-pieces/postToolCall/toolExecutionProcessor.js +0 -207
- package/dist/processor-pieces/preInference/conversationCompactorProcessor.d.ts +0 -52
- package/dist/processor-pieces/preInference/conversationCompactorProcessor.js +0 -264
- package/dist/processor-pieces/preInference/conversationContinuityProcessor.d.ts +0 -49
- package/dist/processor-pieces/preInference/conversationContinuityProcessor.js +0 -293
- package/dist/processor-pieces/preInferenceProcessors/conversationCompaction.d.ts +0 -7
- package/dist/processor-pieces/preInferenceProcessors/conversationCompaction.js +0 -31
- package/dist/processor-pieces/preToolCall/index.d.ts +0 -9
- package/dist/processor-pieces/preToolCall/index.js +0 -18
- package/dist/processor-pieces/preToolCall/localToolInterceptorProcessor.d.ts +0 -58
- package/dist/processor-pieces/preToolCall/localToolInterceptorProcessor.js +0 -444
- package/dist/processor-pieces/preToolCall/toolParameterModifierProcessor.d.ts +0 -44
- package/dist/processor-pieces/preToolCall/toolParameterModifierProcessor.js +0 -367
- package/dist/processor-pieces/preToolCall/toolValidationProcessor.d.ts +0 -58
- package/dist/processor-pieces/preToolCall/toolValidationProcessor.js +0 -391
- package/dist/unified/agent/tool.d.ts +0 -99
- package/dist/unified/agent/tool.js +0 -440
- package/dist/unified/examples/agentExample.d.ts +0 -46
- package/dist/unified/examples/agentExample.js +0 -464
- package/dist/unified/examples/documentationPatternExample.d.ts +0 -10
- package/dist/unified/examples/documentationPatternExample.js +0 -385
- package/dist/unified/examples/followUpProcessorsExample.d.ts +0 -45
- package/dist/unified/examples/followUpProcessorsExample.js +0 -311
- package/dist/unified/examples/orchestratorExample.d.ts +0 -16
- package/dist/unified/examples/orchestratorExample.js +0 -415
- package/dist/unified/examples/preToolCallProcessorsExample.d.ts +0 -36
- package/dist/unified/examples/preToolCallProcessorsExample.js +0 -458
- package/dist/unified/examples/workflowExample.d.ts +0 -12
- package/dist/unified/examples/workflowExample.js +0 -497
- package/dist/unified/orchestrator/orchestrator.d.ts +0 -260
- package/dist/unified/orchestrator/orchestrator.js +0 -519
package/dist/agent.d.ts
ADDED
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
import { SystemPrompt } from "./systemprompt";
|
|
2
|
+
import { TaskInstruction } from "./taskInstruction";
|
|
3
|
+
/**
|
|
4
|
+
* Agent class that manages conversations with LLMs and tool executions.
|
|
5
|
+
* Handles the conversation flow, tool calls, and task completions.
|
|
6
|
+
*/
|
|
7
|
+
declare class Agent {
|
|
8
|
+
/** Available tools for the agent to use */
|
|
9
|
+
private tools;
|
|
10
|
+
/** Full conversation history for API calls */
|
|
11
|
+
private apiConversationHistory;
|
|
12
|
+
/** Maximum number of conversation turns (0 means unlimited) */
|
|
13
|
+
private maxRun;
|
|
14
|
+
/** System prompt that provides instructions to the model */
|
|
15
|
+
private systemPrompt;
|
|
16
|
+
/** Messages from the user */
|
|
17
|
+
private userMessage;
|
|
18
|
+
/** The next user message to be added to the conversation */
|
|
19
|
+
private nextUserMessage;
|
|
20
|
+
/**
|
|
21
|
+
* Creates a new Agent instance.
|
|
22
|
+
*
|
|
23
|
+
* @param tools - The tools available to the agent
|
|
24
|
+
* @param systemPrompt - The system prompt providing instructions to the LLM
|
|
25
|
+
* @param maxRun - Maximum number of conversation turns (0 means unlimited)
|
|
26
|
+
*/
|
|
27
|
+
constructor(tools: any | undefined, systemPrompt: SystemPrompt, maxRun?: number);
|
|
28
|
+
/**
|
|
29
|
+
* Runs the agent on a specific task until completion or max runs reached.
|
|
30
|
+
*
|
|
31
|
+
* @param task - The task instruction to be executed
|
|
32
|
+
* @param successCondition - Optional function to determine if the task is successful
|
|
33
|
+
* @returns Promise with success status, error (if any), and the last assistant message
|
|
34
|
+
*/
|
|
35
|
+
run(task: TaskInstruction, successCondition?: () => boolean): Promise<{
|
|
36
|
+
success: boolean;
|
|
37
|
+
error: string | null;
|
|
38
|
+
message: string | null;
|
|
39
|
+
}>;
|
|
40
|
+
/**
|
|
41
|
+
* Attempts to make a request to the LLM with conversation history and tools.
|
|
42
|
+
*
|
|
43
|
+
* @param apiConversationHistory - The current conversation history
|
|
44
|
+
* @param tools - The tools available to the LLM
|
|
45
|
+
* @returns Promise with the LLM response
|
|
46
|
+
*/
|
|
47
|
+
private attemptLlmRequest;
|
|
48
|
+
/**
|
|
49
|
+
* Executes a tool with given name and input.
|
|
50
|
+
*
|
|
51
|
+
* @param toolName - The name of the tool to execute
|
|
52
|
+
* @param toolInput - The input parameters for the tool
|
|
53
|
+
* @returns Promise with tuple [userRejected, result]
|
|
54
|
+
*/
|
|
55
|
+
private executeTool;
|
|
56
|
+
/**
|
|
57
|
+
* Starts a sub-agent to handle a specific task.
|
|
58
|
+
*
|
|
59
|
+
* @param agentName - The name of the sub-agent to start
|
|
60
|
+
* @param params - Parameters for the sub-agent
|
|
61
|
+
* @returns Promise with tuple [userRejected, result]
|
|
62
|
+
*/
|
|
63
|
+
private startSubAgent;
|
|
64
|
+
/**
|
|
65
|
+
* Extracts tool details from a tool call object.
|
|
66
|
+
*
|
|
67
|
+
* @param tool - The tool call object from the LLM response
|
|
68
|
+
* @returns ToolDetails object with name, input, and ID
|
|
69
|
+
*/
|
|
70
|
+
private getToolDetail;
|
|
71
|
+
/**
|
|
72
|
+
* Creates a tool result object from the tool execution response.
|
|
73
|
+
*
|
|
74
|
+
* @param tool_call_id - The ID of the tool call
|
|
75
|
+
* @param content - The content returned by the tool
|
|
76
|
+
* @returns ToolResult object
|
|
77
|
+
*/
|
|
78
|
+
private getToolResult;
|
|
79
|
+
/**
|
|
80
|
+
* Fallback method for API requests in case of failures.
|
|
81
|
+
*
|
|
82
|
+
* @throws Error API request fallback not implemented
|
|
83
|
+
*/
|
|
84
|
+
private attemptApiRequest;
|
|
85
|
+
}
|
|
86
|
+
export { Agent };
|
package/dist/agent.js
ADDED
|
@@ -0,0 +1,324 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.Agent = void 0;
|
|
7
|
+
const codeboltjs_1 = __importDefault(require("@codebolt/codeboltjs"));
|
|
8
|
+
const { chat, mcp, llm, agent: codeboltAgent } = codeboltjs_1.default;
|
|
9
|
+
// All interfaces moved to libFunctionTypes.ts
|
|
10
|
+
/**
|
|
11
|
+
* Agent class that manages conversations with LLMs and tool executions.
|
|
12
|
+
* Handles the conversation flow, tool calls, and task completions.
|
|
13
|
+
*/
|
|
14
|
+
class Agent {
|
|
15
|
+
/**
|
|
16
|
+
* Creates a new Agent instance.
|
|
17
|
+
*
|
|
18
|
+
* @param tools - The tools available to the agent
|
|
19
|
+
* @param systemPrompt - The system prompt providing instructions to the LLM
|
|
20
|
+
* @param maxRun - Maximum number of conversation turns (0 means unlimited)
|
|
21
|
+
*/
|
|
22
|
+
constructor(tools = [], systemPrompt, maxRun = 0) {
|
|
23
|
+
this.tools = tools;
|
|
24
|
+
this.userMessage = [];
|
|
25
|
+
this.apiConversationHistory = [];
|
|
26
|
+
this.maxRun = maxRun;
|
|
27
|
+
this.systemPrompt = systemPrompt;
|
|
28
|
+
}
|
|
29
|
+
/**
|
|
30
|
+
* Runs the agent on a specific task until completion or max runs reached.
|
|
31
|
+
*
|
|
32
|
+
* @param task - The task instruction to be executed
|
|
33
|
+
* @param successCondition - Optional function to determine if the task is successful
|
|
34
|
+
* @returns Promise with success status, error (if any), and the last assistant message
|
|
35
|
+
*/
|
|
36
|
+
async run(task, successCondition = () => true) {
|
|
37
|
+
var _a, _b;
|
|
38
|
+
let mentaionedMCPSTool = await task.userMessage.getMentionedMcpsTools();
|
|
39
|
+
this.tools = [
|
|
40
|
+
...this.tools,
|
|
41
|
+
...mentaionedMCPSTool || [],
|
|
42
|
+
];
|
|
43
|
+
let mentionedAgents = await task.userMessage.getMentionedAgents();
|
|
44
|
+
// Transform agents into tool format
|
|
45
|
+
const agentTools = mentionedAgents.map(agent => {
|
|
46
|
+
return {
|
|
47
|
+
type: "function",
|
|
48
|
+
function: {
|
|
49
|
+
name: `subagent--${agent.unique_id}`,
|
|
50
|
+
description: agent.longDescription || agent.description,
|
|
51
|
+
parameters: {
|
|
52
|
+
type: "object",
|
|
53
|
+
properties: {
|
|
54
|
+
task: {
|
|
55
|
+
type: "string",
|
|
56
|
+
description: "The task to be executed by the tool."
|
|
57
|
+
}
|
|
58
|
+
},
|
|
59
|
+
required: ["task"]
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
};
|
|
63
|
+
});
|
|
64
|
+
this.tools = this.tools.concat(agentTools);
|
|
65
|
+
let completed = false;
|
|
66
|
+
let userMessages = await task.toPrompt();
|
|
67
|
+
this.apiConversationHistory.push({ role: "user", content: userMessages });
|
|
68
|
+
let runcomplete = 0;
|
|
69
|
+
while (!completed && (runcomplete <= this.maxRun || this.maxRun === 0)) {
|
|
70
|
+
try {
|
|
71
|
+
runcomplete++;
|
|
72
|
+
const response = await this.attemptLlmRequest(this.apiConversationHistory, this.tools);
|
|
73
|
+
let isMessagePresentinReply = false;
|
|
74
|
+
for (const contentBlock of response.choices) {
|
|
75
|
+
if (contentBlock.message) {
|
|
76
|
+
isMessagePresentinReply = true;
|
|
77
|
+
this.apiConversationHistory.push(contentBlock.message);
|
|
78
|
+
if (contentBlock.message.content != null) {
|
|
79
|
+
await chat.sendMessage(contentBlock.message.content, {});
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
if (!isMessagePresentinReply) {
|
|
84
|
+
this.apiConversationHistory.push({
|
|
85
|
+
role: "assistant",
|
|
86
|
+
content: [{ type: "text", text: "Failure: I did not provide a response." }],
|
|
87
|
+
});
|
|
88
|
+
}
|
|
89
|
+
try {
|
|
90
|
+
let toolResults = [];
|
|
91
|
+
let taskCompletedBlock;
|
|
92
|
+
let userRejectedToolUse = false;
|
|
93
|
+
const contentBlock = response.choices[0];
|
|
94
|
+
if ((_a = contentBlock.message) === null || _a === void 0 ? void 0 : _a.tool_calls) {
|
|
95
|
+
for (const tool of contentBlock.message.tool_calls) {
|
|
96
|
+
try {
|
|
97
|
+
const { toolInput, toolName, toolUseId } = this.getToolDetail(tool);
|
|
98
|
+
if (!userRejectedToolUse) {
|
|
99
|
+
if (toolName.includes("attempt_completion")) {
|
|
100
|
+
taskCompletedBlock = tool;
|
|
101
|
+
}
|
|
102
|
+
else {
|
|
103
|
+
let [serverName] = toolName.replace('--', ':').split(':');
|
|
104
|
+
if (serverName == 'subagent') {
|
|
105
|
+
const agentResponse = await codeboltAgent.startAgent(toolName.replace("subagent--", ''), toolInput.task);
|
|
106
|
+
const [didUserReject, result] = [false, "tool result is successful"];
|
|
107
|
+
let toolResult = this.getToolResult(toolUseId, result);
|
|
108
|
+
toolResults.push({
|
|
109
|
+
role: "tool",
|
|
110
|
+
tool_call_id: toolResult.tool_call_id,
|
|
111
|
+
content: toolResult.content,
|
|
112
|
+
});
|
|
113
|
+
if (toolResult.userMessage) {
|
|
114
|
+
this.nextUserMessage = {
|
|
115
|
+
role: "user",
|
|
116
|
+
content: toolResult.userMessage
|
|
117
|
+
};
|
|
118
|
+
}
|
|
119
|
+
if (didUserReject) {
|
|
120
|
+
userRejectedToolUse = true;
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
else {
|
|
124
|
+
const [didUserReject, result] = await this.executeTool(toolName, toolInput);
|
|
125
|
+
// toolResults.push(this.getToolResult(toolUseId, result));
|
|
126
|
+
let toolResult = this.getToolResult(toolUseId, result);
|
|
127
|
+
toolResults.push({
|
|
128
|
+
role: "tool",
|
|
129
|
+
tool_call_id: toolResult.tool_call_id,
|
|
130
|
+
content: toolResult.content,
|
|
131
|
+
});
|
|
132
|
+
if (toolResult.userMessage) {
|
|
133
|
+
this.nextUserMessage = {
|
|
134
|
+
role: "user",
|
|
135
|
+
content: toolResult.userMessage
|
|
136
|
+
};
|
|
137
|
+
}
|
|
138
|
+
if (didUserReject) {
|
|
139
|
+
userRejectedToolUse = true;
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
else {
|
|
145
|
+
let toolResult = this.getToolResult(toolUseId, "Skipping tool execution due to previous tool user rejection.");
|
|
146
|
+
toolResults.push({
|
|
147
|
+
role: "tool",
|
|
148
|
+
tool_call_id: toolResult.tool_call_id,
|
|
149
|
+
content: toolResult.content,
|
|
150
|
+
});
|
|
151
|
+
if (toolResult.userMessage) {
|
|
152
|
+
this.nextUserMessage = {
|
|
153
|
+
role: "user",
|
|
154
|
+
content: toolResult.userMessage
|
|
155
|
+
};
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
catch (error) {
|
|
160
|
+
toolResults.push({
|
|
161
|
+
role: "tool",
|
|
162
|
+
tool_call_id: tool.id,
|
|
163
|
+
content: `please provide valid json string for tool.function.arguments String(error)`,
|
|
164
|
+
});
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
if (taskCompletedBlock) {
|
|
169
|
+
let [_, result] = await this.executeTool(taskCompletedBlock.function.name, JSON.parse(taskCompletedBlock.function.arguments || "{}"));
|
|
170
|
+
if (result === "") {
|
|
171
|
+
completed = true;
|
|
172
|
+
result = "The user is satisfied with the result.";
|
|
173
|
+
}
|
|
174
|
+
let toolResult = this.getToolResult(taskCompletedBlock.id, result);
|
|
175
|
+
toolResults.push({
|
|
176
|
+
role: "tool",
|
|
177
|
+
tool_call_id: toolResult.tool_call_id,
|
|
178
|
+
content: toolResult.content,
|
|
179
|
+
});
|
|
180
|
+
if (toolResult.userMessage) {
|
|
181
|
+
this.nextUserMessage = {
|
|
182
|
+
role: "user",
|
|
183
|
+
content: toolResult.userMessage
|
|
184
|
+
};
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
this.apiConversationHistory.push(...toolResults);
|
|
188
|
+
if (this.nextUserMessage) {
|
|
189
|
+
this.apiConversationHistory.push(this.nextUserMessage);
|
|
190
|
+
}
|
|
191
|
+
let nextUserMessage = toolResults;
|
|
192
|
+
if (toolResults.length === 0) {
|
|
193
|
+
nextUserMessage = [{
|
|
194
|
+
role: "user",
|
|
195
|
+
content: [{
|
|
196
|
+
type: "text",
|
|
197
|
+
text: "If you have completed the user's task, use the attempt_completion tool. If you require additional information from the user, use the ask_followup_question tool. Otherwise, if you have not completed the task and do not need additional information, then proceed with the next step of the task. (This is an automated message, so do not respond to it conversationally.)"
|
|
198
|
+
}]
|
|
199
|
+
}];
|
|
200
|
+
if (nextUserMessage) {
|
|
201
|
+
this.apiConversationHistory.push(nextUserMessage[0]);
|
|
202
|
+
}
|
|
203
|
+
}
|
|
204
|
+
}
|
|
205
|
+
catch (error) {
|
|
206
|
+
console.error("Error in agent tool call:", error);
|
|
207
|
+
return { success: false, error: error instanceof Error ? error.message : String(error), message: null };
|
|
208
|
+
}
|
|
209
|
+
}
|
|
210
|
+
catch (error) {
|
|
211
|
+
console.error("Error in agent tool call:", error);
|
|
212
|
+
return { success: false, error: error instanceof Error ? error.message : String(error), message: null };
|
|
213
|
+
}
|
|
214
|
+
}
|
|
215
|
+
return {
|
|
216
|
+
success: completed,
|
|
217
|
+
error: null,
|
|
218
|
+
message: ((_b = this.apiConversationHistory
|
|
219
|
+
.filter(msg => msg.role === 'assistant')
|
|
220
|
+
.pop()) === null || _b === void 0 ? void 0 : _b.content) || ''
|
|
221
|
+
};
|
|
222
|
+
}
|
|
223
|
+
/**
|
|
224
|
+
* Attempts to make a request to the LLM with conversation history and tools.
|
|
225
|
+
*
|
|
226
|
+
* @param apiConversationHistory - The current conversation history
|
|
227
|
+
* @param tools - The tools available to the LLM
|
|
228
|
+
* @returns Promise with the LLM response
|
|
229
|
+
*/
|
|
230
|
+
async attemptLlmRequest(apiConversationHistory, tools) {
|
|
231
|
+
try {
|
|
232
|
+
let systemPrompt = await this.systemPrompt.toPromptText();
|
|
233
|
+
const aiMessages = [
|
|
234
|
+
{ role: "system", content: systemPrompt },
|
|
235
|
+
...apiConversationHistory,
|
|
236
|
+
];
|
|
237
|
+
const createParams = {
|
|
238
|
+
full: true,
|
|
239
|
+
messages: aiMessages,
|
|
240
|
+
tools: tools,
|
|
241
|
+
tool_choice: "auto",
|
|
242
|
+
};
|
|
243
|
+
//@ts-ignore
|
|
244
|
+
const { completion } = await llm.inference(createParams);
|
|
245
|
+
return completion;
|
|
246
|
+
}
|
|
247
|
+
catch (error) {
|
|
248
|
+
return this.attemptApiRequest();
|
|
249
|
+
}
|
|
250
|
+
}
|
|
251
|
+
/**
|
|
252
|
+
* Executes a tool with given name and input.
|
|
253
|
+
*
|
|
254
|
+
* @param toolName - The name of the tool to execute
|
|
255
|
+
* @param toolInput - The input parameters for the tool
|
|
256
|
+
* @returns Promise with tuple [userRejected, result]
|
|
257
|
+
*/
|
|
258
|
+
async executeTool(toolName, toolInput) {
|
|
259
|
+
//codebolttools--readfile
|
|
260
|
+
const [toolboxName, actualToolName] = toolName.split('--');
|
|
261
|
+
console.log("Toolbox name: ", toolboxName, "Actual tool name: ", actualToolName);
|
|
262
|
+
const { data } = await mcp.executeTool(toolboxName, actualToolName, toolInput);
|
|
263
|
+
console.log("Tool result: ", data);
|
|
264
|
+
return data;
|
|
265
|
+
}
|
|
266
|
+
/**
|
|
267
|
+
* Starts a sub-agent to handle a specific task.
|
|
268
|
+
*
|
|
269
|
+
* @param agentName - The name of the sub-agent to start
|
|
270
|
+
* @param params - Parameters for the sub-agent
|
|
271
|
+
* @returns Promise with tuple [userRejected, result]
|
|
272
|
+
*/
|
|
273
|
+
async startSubAgent(agentName, params) {
|
|
274
|
+
return [false, await codeboltAgent.startAgent(agentName, params.task)];
|
|
275
|
+
}
|
|
276
|
+
/**
|
|
277
|
+
* Extracts tool details from a tool call object.
|
|
278
|
+
*
|
|
279
|
+
* @param tool - The tool call object from the LLM response
|
|
280
|
+
* @returns ToolDetails object with name, input, and ID
|
|
281
|
+
*/
|
|
282
|
+
getToolDetail(tool) {
|
|
283
|
+
return {
|
|
284
|
+
toolName: tool.function.name,
|
|
285
|
+
toolInput: JSON.parse(tool.function.arguments || "{}"),
|
|
286
|
+
toolUseId: tool.id
|
|
287
|
+
};
|
|
288
|
+
}
|
|
289
|
+
/**
|
|
290
|
+
* Creates a tool result object from the tool execution response.
|
|
291
|
+
*
|
|
292
|
+
* @param tool_call_id - The ID of the tool call
|
|
293
|
+
* @param content - The content returned by the tool
|
|
294
|
+
* @returns ToolResult object
|
|
295
|
+
*/
|
|
296
|
+
getToolResult(tool_call_id, content) {
|
|
297
|
+
let userMessage = undefined;
|
|
298
|
+
try {
|
|
299
|
+
let parsed = JSON.parse(content);
|
|
300
|
+
if (parsed.payload && parsed.payload.content) {
|
|
301
|
+
content = `The browser action has been executed. The screenshot have been captured for your analysis. The tool response is provided in the next user message`;
|
|
302
|
+
// this.apiConversationHistory.push()
|
|
303
|
+
userMessage = parsed.payload.content;
|
|
304
|
+
}
|
|
305
|
+
}
|
|
306
|
+
catch (error) {
|
|
307
|
+
}
|
|
308
|
+
return {
|
|
309
|
+
role: "tool",
|
|
310
|
+
tool_call_id,
|
|
311
|
+
content,
|
|
312
|
+
userMessage
|
|
313
|
+
};
|
|
314
|
+
}
|
|
315
|
+
/**
|
|
316
|
+
* Fallback method for API requests in case of failures.
|
|
317
|
+
*
|
|
318
|
+
* @throws Error API request fallback not implemented
|
|
319
|
+
*/
|
|
320
|
+
attemptApiRequest() {
|
|
321
|
+
throw new Error("API request fallback not implemented");
|
|
322
|
+
}
|
|
323
|
+
}
|
|
324
|
+
exports.Agent = Agent;
|
|
@@ -258,9 +258,9 @@ class Agent {
|
|
|
258
258
|
async executeTool(toolName, toolInput) {
|
|
259
259
|
//codebolttools--readfile
|
|
260
260
|
const [toolboxName, actualToolName] = toolName.split('--');
|
|
261
|
-
|
|
261
|
+
console.log("Toolbox name: ", toolboxName, "Actual tool name: ", actualToolName);
|
|
262
262
|
const { data } = await mcp.executeTool(toolboxName, actualToolName, toolInput);
|
|
263
|
-
|
|
263
|
+
console.log("Tool result: ", data);
|
|
264
264
|
return data;
|
|
265
265
|
}
|
|
266
266
|
/**
|
|
@@ -143,7 +143,7 @@ class FollowUpPromptBuilder {
|
|
|
143
143
|
return this.previousConversation;
|
|
144
144
|
}
|
|
145
145
|
try {
|
|
146
|
-
|
|
146
|
+
console.log("Summarizing conversation due to length:", this.previousConversation.length);
|
|
147
147
|
// Convert OpenAI messages to the format expected by chatSummary
|
|
148
148
|
const messagesToSummarize = this.previousConversation.map(msg => ({
|
|
149
149
|
role: msg.role,
|
|
@@ -137,7 +137,7 @@ class LLMOutputHandler {
|
|
|
137
137
|
let toolResults = [];
|
|
138
138
|
let taskCompletedBlock;
|
|
139
139
|
let userRejectedToolUse = false;
|
|
140
|
-
|
|
140
|
+
console.log("Calling run tool: ", JSON.stringify(this.llmResponse.completion));
|
|
141
141
|
const contentBlock = this.llmResponse.completion.choices[0];
|
|
142
142
|
// Validate contentBlock structure
|
|
143
143
|
if (!contentBlock || !contentBlock.message) {
|
|
@@ -174,12 +174,12 @@ class LLMOutputHandler {
|
|
|
174
174
|
}
|
|
175
175
|
}
|
|
176
176
|
else {
|
|
177
|
-
|
|
177
|
+
console.log("Executing tool: ", toolName, toolInput);
|
|
178
178
|
const [didUserReject, result] = await this.executeTool(toolName, toolInput);
|
|
179
|
-
|
|
179
|
+
console.log("Tool result: ", result);
|
|
180
180
|
// toolResults.push(this.getToolResult(toolUseId, result));
|
|
181
181
|
let toolResult = this.getToolResult(toolUseId, result);
|
|
182
|
-
|
|
182
|
+
console.log("tool result after parsing", toolResult);
|
|
183
183
|
this.toolResults.push({
|
|
184
184
|
role: "tool",
|
|
185
185
|
tool_call_id: toolResult.tool_call_id,
|
|
@@ -367,9 +367,9 @@ class LLMOutputHandler {
|
|
|
367
367
|
var _a;
|
|
368
368
|
//codeboltmcp--readfile
|
|
369
369
|
const [toolboxName, actualToolName] = toolName.split('--');
|
|
370
|
-
|
|
370
|
+
console.log("Toolbox name: ", toolboxName, "Actual tool name: ", actualToolName);
|
|
371
371
|
const data = await ((_a = this.codebolt) === null || _a === void 0 ? void 0 : _a.mcp.executeTool(toolboxName, actualToolName, toolInput));
|
|
372
|
-
|
|
372
|
+
console.log("Tool result: ", data);
|
|
373
373
|
return data;
|
|
374
374
|
}
|
|
375
375
|
/**
|
|
@@ -389,7 +389,7 @@ class LLMOutputHandler {
|
|
|
389
389
|
// Only attempt to parse if content looks like JSON
|
|
390
390
|
if (content.trim().startsWith('{') || content.trim().startsWith('[')) {
|
|
391
391
|
const parsed = JSON.parse(content);
|
|
392
|
-
|
|
392
|
+
console.log("Parsed Content: ", parsed);
|
|
393
393
|
if (parsed && typeof parsed === 'object' && parsed.payload && parsed.payload.content) {
|
|
394
394
|
content = `The browser action has been executed. The screenshot have been captured for your analysis. The tool response is provided in the next user message`;
|
|
395
395
|
userMessage = parsed.payload.content;
|
|
@@ -58,12 +58,7 @@ declare class UserMessage {
|
|
|
58
58
|
*
|
|
59
59
|
* @returns Promise with an array of MCP tools
|
|
60
60
|
*/
|
|
61
|
-
getMentionedMcpsTools(): Promise<
|
|
62
|
-
toolbox: string;
|
|
63
|
-
toolName: string;
|
|
64
|
-
description?: string;
|
|
65
|
-
parameters?: Record<string, unknown>;
|
|
66
|
-
}[] | undefined>;
|
|
61
|
+
getMentionedMcpsTools(): Promise<any>;
|
|
67
62
|
/**
|
|
68
63
|
* Gets environment details for the current working directory.
|
|
69
64
|
*
|
|
@@ -3,6 +3,39 @@
|
|
|
3
3
|
* @fileoverview Document handling for Composable Agent Pattern
|
|
4
4
|
* @description Provides document processing, chunking, and embedding capabilities
|
|
5
5
|
*/
|
|
6
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
7
|
+
if (k2 === undefined) k2 = k;
|
|
8
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
9
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
10
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
11
|
+
}
|
|
12
|
+
Object.defineProperty(o, k2, desc);
|
|
13
|
+
}) : (function(o, m, k, k2) {
|
|
14
|
+
if (k2 === undefined) k2 = k;
|
|
15
|
+
o[k2] = m[k];
|
|
16
|
+
}));
|
|
17
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
18
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
19
|
+
}) : function(o, v) {
|
|
20
|
+
o["default"] = v;
|
|
21
|
+
});
|
|
22
|
+
var __importStar = (this && this.__importStar) || (function () {
|
|
23
|
+
var ownKeys = function(o) {
|
|
24
|
+
ownKeys = Object.getOwnPropertyNames || function (o) {
|
|
25
|
+
var ar = [];
|
|
26
|
+
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
|
|
27
|
+
return ar;
|
|
28
|
+
};
|
|
29
|
+
return ownKeys(o);
|
|
30
|
+
};
|
|
31
|
+
return function (mod) {
|
|
32
|
+
if (mod && mod.__esModule) return mod;
|
|
33
|
+
var result = {};
|
|
34
|
+
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
|
|
35
|
+
__setModuleDefault(result, mod);
|
|
36
|
+
return result;
|
|
37
|
+
};
|
|
38
|
+
})();
|
|
6
39
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
7
40
|
exports.MDocument = void 0;
|
|
8
41
|
/**
|
|
@@ -58,8 +91,8 @@ class MDocument {
|
|
|
58
91
|
* @returns Promise<MDocument> instance
|
|
59
92
|
*/
|
|
60
93
|
static async fromFile(filePath, metadata) {
|
|
61
|
-
const fs = await
|
|
62
|
-
const path = await
|
|
94
|
+
const fs = await Promise.resolve().then(() => __importStar(require('fs/promises')));
|
|
95
|
+
const path = await Promise.resolve().then(() => __importStar(require('path')));
|
|
63
96
|
const content = await fs.readFile(filePath, 'utf-8');
|
|
64
97
|
const extension = path.extname(filePath).toLowerCase();
|
|
65
98
|
let type = 'text';
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
import type { OpenAIMessage, OpenAITool, ToolResult, CodeboltAPI } from "./types/libFunctionTypes";
|
|
2
|
+
/**
|
|
3
|
+
* Builds follow-up prompts for continuing conversations with tool results.
|
|
4
|
+
* Manages conversation history and summarization when conversations get too long.
|
|
5
|
+
*/
|
|
6
|
+
declare class FollowUpPromptBuilder {
|
|
7
|
+
/** Previous conversation messages */
|
|
8
|
+
private previousConversation;
|
|
9
|
+
/** Tool results to add to the conversation */
|
|
10
|
+
private toolResults;
|
|
11
|
+
/** Available tools for the conversation */
|
|
12
|
+
private tools;
|
|
13
|
+
/** The last LLM response, if available */
|
|
14
|
+
private llmResponse?;
|
|
15
|
+
/** Maximum conversation length before summarization */
|
|
16
|
+
private maxConversationLength;
|
|
17
|
+
/** Whether to force summarization */
|
|
18
|
+
private forceSummarization;
|
|
19
|
+
/** Codebolt API instance */
|
|
20
|
+
private codebolt?;
|
|
21
|
+
/**
|
|
22
|
+
* Creates a new FollowUpQuestionBuilder instance.
|
|
23
|
+
*
|
|
24
|
+
* @param codebolt - Optional codebolt API instance
|
|
25
|
+
*/
|
|
26
|
+
constructor(codebolt?: CodeboltAPI);
|
|
27
|
+
/**
|
|
28
|
+
* Adds the previous conversation to the builder.
|
|
29
|
+
*
|
|
30
|
+
* @param previousPrompt - The previous prompt object containing messages and tools
|
|
31
|
+
* @returns The FollowUpQuestionBuilder instance for chaining
|
|
32
|
+
*/
|
|
33
|
+
addPreviousConversation(previousPrompt: {
|
|
34
|
+
messages: OpenAIMessage[];
|
|
35
|
+
tools: OpenAITool[];
|
|
36
|
+
tool_choice?: string;
|
|
37
|
+
}, llmResponse: {
|
|
38
|
+
completion: any;
|
|
39
|
+
}): this;
|
|
40
|
+
addLLMResponseToConverstaion(llmResponse: {
|
|
41
|
+
completion: any;
|
|
42
|
+
}): this;
|
|
43
|
+
/**
|
|
44
|
+
* Adds tool execution results to the conversation.
|
|
45
|
+
*
|
|
46
|
+
* @param toolResults - Array of tool execution results
|
|
47
|
+
* @returns The FollowUpQuestionBuilder instance for chaining
|
|
48
|
+
*/
|
|
49
|
+
addToolResult(toolResults: ToolResult[]): this;
|
|
50
|
+
/**
|
|
51
|
+
* Checks if the conversation is too long and sets up summarization with custom max length.
|
|
52
|
+
*
|
|
53
|
+
* @param maxLength - Maximum number of messages before summarization
|
|
54
|
+
* @returns The FollowUpQuestionBuilder instance for chaining
|
|
55
|
+
*/
|
|
56
|
+
checkAndSummarizeConversationIfLong(maxLength: number): this;
|
|
57
|
+
/**
|
|
58
|
+
* Performs conversation summarization if needed.
|
|
59
|
+
*
|
|
60
|
+
* @returns Promise that resolves to the summarized messages
|
|
61
|
+
*/
|
|
62
|
+
private performSummarization;
|
|
63
|
+
/**
|
|
64
|
+
* Builds the follow-up conversation prompt with tool results.
|
|
65
|
+
*
|
|
66
|
+
* @returns Promise that resolves to the conversation prompt object
|
|
67
|
+
*/
|
|
68
|
+
build(): Promise<{
|
|
69
|
+
messages: OpenAIMessage[];
|
|
70
|
+
tools: OpenAITool[];
|
|
71
|
+
tool_choice: "auto";
|
|
72
|
+
full: boolean;
|
|
73
|
+
}>;
|
|
74
|
+
}
|
|
75
|
+
export { FollowUpPromptBuilder };
|