Skip to content

Commit 977c1c3

Browse files
changes
1 parent 0c6f21b commit 977c1c3

15 files changed

+709
-44
lines changed

modules/agent.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ const codeboltAgent = {
6363
}));
6464
websocket_1.default.getWebsocket.on('message', (data) => {
6565
const response = JSON.parse(data);
66-
if (response.type === "taskCompletionResponse") {
66+
if (response.type === "taskCompletionResponse" && response.agentId === agentId) {
6767
resolve(response); // Resolve the Promise when the agent has been successfully started
6868
}
6969
});

modules/agentlib/agent.d.ts

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,86 @@
11
import { SystemPrompt } from "./systemprompt";
22
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+
*/
37
declare class Agent {
8+
/** Available tools for the agent to use */
49
private tools;
10+
/** Full conversation history for API calls */
511
private apiConversationHistory;
12+
/** Maximum number of conversation turns (0 means unlimited) */
613
private maxRun;
14+
/** System prompt that provides instructions to the model */
715
private systemPrompt;
16+
/** Messages from the user */
817
private userMessage;
18+
/** The next user message to be added to the conversation */
919
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+
*/
1027
constructor(tools: any, 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+
*/
1135
run(task: TaskInstruction, successCondition?: () => boolean): Promise<{
1236
success: boolean;
1337
error: string | null;
1438
message: string | null;
1539
}>;
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+
*/
1647
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+
*/
1755
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+
*/
1863
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+
*/
1970
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+
*/
2078
private getToolResult;
79+
/**
80+
* Fallback method for API requests in case of failures.
81+
*
82+
* @throws Error API request fallback not implemented
83+
*/
2184
private attemptApiRequest;
2285
}
2386
export { Agent };

modules/agentlib/agent.js

Lines changed: 57 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,32 @@ const chat_1 = __importDefault(require("./../chat"));
88
const tools_1 = __importDefault(require("./../tools"));
99
const llm_1 = __importDefault(require("./../llm"));
1010
const agent_1 = __importDefault(require("./../agent"));
11+
/**
12+
* Agent class that manages conversations with LLMs and tool executions.
13+
* Handles the conversation flow, tool calls, and task completions.
14+
*/
1115
class Agent {
16+
/**
17+
* Creates a new Agent instance.
18+
*
19+
* @param tools - The tools available to the agent
20+
* @param systemPrompt - The system prompt providing instructions to the LLM
21+
* @param maxRun - Maximum number of conversation turns (0 means unlimited)
22+
*/
1223
constructor(tools = [], systemPrompt, maxRun = 0) {
1324
this.tools = tools;
1425
this.userMessage = [];
1526
this.apiConversationHistory = [];
1627
this.maxRun = maxRun;
1728
this.systemPrompt = systemPrompt;
1829
}
30+
/**
31+
* Runs the agent on a specific task until completion or max runs reached.
32+
*
33+
* @param task - The task instruction to be executed
34+
* @param successCondition - Optional function to determine if the task is successful
35+
* @returns Promise with success status, error (if any), and the last assistant message
36+
*/
1937
async run(task, successCondition = () => true) {
2038
var _a, _b;
2139
let mentaionedMCPSTool = await task.userMessage.getMentionedMcpsTools();
@@ -199,6 +217,13 @@ class Agent {
199217
.pop()) === null || _b === void 0 ? void 0 : _b.content) || ''
200218
};
201219
}
220+
/**
221+
* Attempts to make a request to the LLM with conversation history and tools.
222+
*
223+
* @param apiConversationHistory - The current conversation history
224+
* @param tools - The tools available to the LLM
225+
* @returns Promise with the LLM response
226+
*/
202227
async attemptLlmRequest(apiConversationHistory, tools) {
203228
try {
204229
let systemPrompt = await this.systemPrompt.toPromptText();
@@ -221,21 +246,48 @@ class Agent {
221246
return this.attemptApiRequest();
222247
}
223248
}
249+
/**
250+
* Executes a tool with given name and input.
251+
*
252+
* @param toolName - The name of the tool to execute
253+
* @param toolInput - The input parameters for the tool
254+
* @returns Promise with tuple [userRejected, result]
255+
*/
224256
async executeTool(toolName, toolInput) {
225257
//codebolttools--readfile
226258
const [toolboxName, actualToolName] = toolName.split('--');
227259
return tools_1.default.executeTool(toolboxName, actualToolName, toolInput);
228260
}
261+
/**
262+
* Starts a sub-agent to handle a specific task.
263+
*
264+
* @param agentName - The name of the sub-agent to start
265+
* @param params - Parameters for the sub-agent
266+
* @returns Promise with tuple [userRejected, result]
267+
*/
229268
async startSubAgent(agentName, params) {
230269
return agent_1.default.startAgent(agentName, params.task);
231270
}
271+
/**
272+
* Extracts tool details from a tool call object.
273+
*
274+
* @param tool - The tool call object from the LLM response
275+
* @returns ToolDetails object with name, input, and ID
276+
*/
232277
getToolDetail(tool) {
233278
return {
234279
toolName: tool.function.name,
235280
toolInput: JSON.parse(tool.function.arguments || "{}"),
236281
toolUseId: tool.id
237282
};
238283
}
284+
/**
285+
* Creates a tool result object from the tool execution response.
286+
*
287+
* @param tool_call_id - The ID of the tool call
288+
* @param content - The content returned by the tool
289+
* @returns ToolResult object
290+
*/
239291
getToolResult(tool_call_id, content) {
240292
let userMessage = undefined;
241293
try {
@@ -255,7 +307,11 @@ class Agent {
255307
userMessage
256308
};
257309
}
258-
// Placeholder for error fallback method
310+
/**
311+
* Fallback method for API requests in case of failures.
312+
*
313+
* @throws Error API request fallback not implemented
314+
*/
259315
attemptApiRequest() {
260316
throw new Error("API request fallback not implemented");
261317
}

modules/agentlib/taskInstruction.d.ts

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,59 @@
11
import { UserMessage, UserMessageContent } from "./usermessage";
2+
/**
3+
* Interface for tools that can be used within tasks.
4+
* Each tool has a description and usage example.
5+
*/
26
interface Tools {
37
[key: string]: {
8+
/** Description of what the tool does */
49
description: string;
10+
/** How to use the tool correctly */
511
usage: string;
12+
/** Optional example demonstrating tool usage */
613
example?: string;
714
};
815
}
16+
/**
17+
* Interface for user message structure.
18+
* Contains message type and text content.
19+
*/
920
interface UserMessages {
21+
/** The type of user message */
1022
type: string;
23+
/** The text content of the message */
1124
text: string;
1225
}
26+
/**
27+
* Class representing a task instruction.
28+
* Handles loading task data and converting it to prompts.
29+
*/
1330
declare class TaskInstruction {
31+
/** Available tools for the task */
1432
tools: Tools;
33+
/** Messages from the user for this task */
1534
userMessages: UserMessageContent[];
35+
/** The user message object containing input */
1636
userMessage: UserMessage;
37+
/** Path to the YAML file with task instructions */
1738
filepath: string;
39+
/** The section reference within the YAML file */
1840
refsection: string;
41+
/**
42+
* Creates a new TaskInstruction instance.
43+
*
44+
* @param tools - Tools available for this task
45+
* @param userMessage - User message containing task instructions
46+
* @param filepath - Path to the YAML file with task data
47+
* @param refsection - Section name within the YAML file
48+
*/
1949
constructor(tools: Tools | undefined, userMessage: UserMessage, filepath?: string, refsection?: string);
50+
/**
51+
* Converts the task instruction to a prompt format.
52+
* Loads data from YAML file and combines with user message.
53+
*
54+
* @returns Promise with an array of user message content blocks
55+
* @throws Error if there's an issue processing the task instruction
56+
*/
2057
toPrompt(): Promise<UserMessages[]>;
2158
}
2259
export { TaskInstruction };

modules/agentlib/taskInstruction.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,34 @@ exports.TaskInstruction = void 0;
88
const yaml = require('js-yaml');
99
const fs = require('fs');
1010
const path = require('path');
11+
/**
12+
* Class representing a task instruction.
13+
* Handles loading task data and converting it to prompts.
14+
*/
1115
class TaskInstruction {
16+
/**
17+
* Creates a new TaskInstruction instance.
18+
*
19+
* @param tools - Tools available for this task
20+
* @param userMessage - User message containing task instructions
21+
* @param filepath - Path to the YAML file with task data
22+
* @param refsection - Section name within the YAML file
23+
*/
1224
constructor(tools = {}, userMessage, filepath = "", refsection = "") {
25+
/** Messages from the user for this task */
1326
this.userMessages = [];
1427
this.tools = tools;
1528
this.userMessage = userMessage;
1629
this.filepath = filepath;
1730
this.refsection = refsection;
1831
}
32+
/**
33+
* Converts the task instruction to a prompt format.
34+
* Loads data from YAML file and combines with user message.
35+
*
36+
* @returns Promise with an array of user message content blocks
37+
* @throws Error if there's an issue processing the task instruction
38+
*/
1939
async toPrompt() {
2040
try {
2141
this.userMessages = await this.userMessage.toPrompt();

0 commit comments

Comments
 (0)