@@ -5,36 +5,71 @@ import codeboltAgent from "./../agent"
55import { SystemPrompt } from "./systemprompt" ;
66import { TaskInstruction } from "./taskInstruction" ;
77
8+ /**
9+ * Represents a message in the conversation with roles and content.
10+ */
811interface Message {
12+ /** The role of the message sender: user, assistant, tool, or system */
913 role : 'user' | 'assistant' | 'tool' | 'system' ;
14+ /** The content of the message, can be an array of content blocks or a string */
1015 content : any [ ] | string ;
16+ /** Optional ID for tool calls */
1117 tool_call_id ?: string ;
18+ /** Additional properties that might be present */
1219 [ key : string ] : any ;
1320}
1421
22+ /**
23+ * Represents the result from a tool execution.
24+ */
1525interface ToolResult {
26+ /** Always 'tool' for tool execution results */
1627 role : 'tool' ;
28+ /** ID that links this result to the original tool call */
1729 tool_call_id : string ;
30+ /** The content returned by the tool */
1831 content : any ;
32+ /** Optional user message to be added after tool execution */
1933 userMessage ?: any ;
2034}
2135
36+ /**
37+ * Details about a tool to be executed.
38+ */
2239interface ToolDetails {
40+ /** The name of the tool to execute */
2341 toolName : string ;
42+ /** Input parameters for the tool */
2443 toolInput : any ;
44+ /** Unique ID for this tool use instance */
2545 toolUseId : string ;
2646}
2747
48+ /**
49+ * Agent class that manages conversations with LLMs and tool executions.
50+ * Handles the conversation flow, tool calls, and task completions.
51+ */
2852class Agent {
53+ /** Available tools for the agent to use */
2954 private tools : any [ ] ;
30- // private subAgents: any[];
55+ /** Full conversation history for API calls */
3156 private apiConversationHistory : Message [ ] ;
57+ /** Maximum number of conversation turns (0 means unlimited) */
3258 private maxRun : number ;
59+ /** System prompt that provides instructions to the model */
3360 private systemPrompt : SystemPrompt ;
61+ /** Messages from the user */
3462 private userMessage : Message [ ] ;
63+ /** The next user message to be added to the conversation */
3564 private nextUserMessage :any ;
3665
37-
66+ /**
67+ * Creates a new Agent instance.
68+ *
69+ * @param tools - The tools available to the agent
70+ * @param systemPrompt - The system prompt providing instructions to the LLM
71+ * @param maxRun - Maximum number of conversation turns (0 means unlimited)
72+ */
3873 constructor ( tools : any = [ ] , systemPrompt : SystemPrompt , maxRun : number = 0 ) {
3974 this . tools = tools ;
4075 this . userMessage = [ ] ;
@@ -44,6 +79,13 @@ class Agent {
4479
4580 }
4681
82+ /**
83+ * Runs the agent on a specific task until completion or max runs reached.
84+ *
85+ * @param task - The task instruction to be executed
86+ * @param successCondition - Optional function to determine if the task is successful
87+ * @returns Promise with success status, error (if any), and the last assistant message
88+ */
4789 async run ( task : TaskInstruction , successCondition : ( ) => boolean = ( ) => true ) : Promise < { success : boolean ; error : string | null , message : string | null } > {
4890
4991
@@ -259,6 +301,13 @@ class Agent {
259301 } ;
260302 }
261303
304+ /**
305+ * Attempts to make a request to the LLM with conversation history and tools.
306+ *
307+ * @param apiConversationHistory - The current conversation history
308+ * @param tools - The tools available to the LLM
309+ * @returns Promise with the LLM response
310+ */
262311 private async attemptLlmRequest ( apiConversationHistory : Message [ ] , tools : Record < string , any > ) : Promise < any > {
263312
264313
@@ -284,15 +333,36 @@ class Agent {
284333 }
285334 }
286335
336+ /**
337+ * Executes a tool with given name and input.
338+ *
339+ * @param toolName - The name of the tool to execute
340+ * @param toolInput - The input parameters for the tool
341+ * @returns Promise with tuple [userRejected, result]
342+ */
287343 private async executeTool ( toolName : string , toolInput : any ) : Promise < [ boolean , any ] > {
288344 //codebolttools--readfile
289345 const [ toolboxName , actualToolName ] = toolName . split ( '--' ) ;
290346 return tools . executeTool ( toolboxName , actualToolName , toolInput ) ;
291347 }
348+
349+ /**
350+ * Starts a sub-agent to handle a specific task.
351+ *
352+ * @param agentName - The name of the sub-agent to start
353+ * @param params - Parameters for the sub-agent
354+ * @returns Promise with tuple [userRejected, result]
355+ */
292356 private async startSubAgent ( agentName : string , params : any ) : Promise < [ boolean , any ] > {
293357 return codeboltAgent . startAgent ( agentName , params . task ) ;
294358 }
295359
360+ /**
361+ * Extracts tool details from a tool call object.
362+ *
363+ * @param tool - The tool call object from the LLM response
364+ * @returns ToolDetails object with name, input, and ID
365+ */
296366 private getToolDetail ( tool : any ) : ToolDetails {
297367 return {
298368 toolName : tool . function . name ,
@@ -301,6 +371,13 @@ class Agent {
301371 } ;
302372 }
303373
374+ /**
375+ * Creates a tool result object from the tool execution response.
376+ *
377+ * @param tool_call_id - The ID of the tool call
378+ * @param content - The content returned by the tool
379+ * @returns ToolResult object
380+ */
304381 private getToolResult ( tool_call_id : string , content : string ) : ToolResult {
305382 let userMessage = undefined
306383 try {
@@ -322,7 +399,11 @@ class Agent {
322399 } ;
323400 }
324401
325- // Placeholder for error fallback method
402+ /**
403+ * Fallback method for API requests in case of failures.
404+ *
405+ * @throws Error API request fallback not implemented
406+ */
326407 private attemptApiRequest ( ) : any {
327408 throw new Error ( "API request fallback not implemented" ) ;
328409 }
0 commit comments