@@ -5,36 +5,71 @@ import codeboltAgent from "./../agent"
5
5
import { SystemPrompt } from "./systemprompt" ;
6
6
import { TaskInstruction } from "./taskInstruction" ;
7
7
8
+ /**
9
+ * Represents a message in the conversation with roles and content.
10
+ */
8
11
interface Message {
12
+ /** The role of the message sender: user, assistant, tool, or system */
9
13
role : 'user' | 'assistant' | 'tool' | 'system' ;
14
+ /** The content of the message, can be an array of content blocks or a string */
10
15
content : any [ ] | string ;
16
+ /** Optional ID for tool calls */
11
17
tool_call_id ?: string ;
18
+ /** Additional properties that might be present */
12
19
[ key : string ] : any ;
13
20
}
14
21
22
+ /**
23
+ * Represents the result from a tool execution.
24
+ */
15
25
interface ToolResult {
26
+ /** Always 'tool' for tool execution results */
16
27
role : 'tool' ;
28
+ /** ID that links this result to the original tool call */
17
29
tool_call_id : string ;
30
+ /** The content returned by the tool */
18
31
content : any ;
32
+ /** Optional user message to be added after tool execution */
19
33
userMessage ?: any ;
20
34
}
21
35
36
+ /**
37
+ * Details about a tool to be executed.
38
+ */
22
39
interface ToolDetails {
40
+ /** The name of the tool to execute */
23
41
toolName : string ;
42
+ /** Input parameters for the tool */
24
43
toolInput : any ;
44
+ /** Unique ID for this tool use instance */
25
45
toolUseId : string ;
26
46
}
27
47
48
+ /**
49
+ * Agent class that manages conversations with LLMs and tool executions.
50
+ * Handles the conversation flow, tool calls, and task completions.
51
+ */
28
52
class Agent {
53
+ /** Available tools for the agent to use */
29
54
private tools : any [ ] ;
30
- // private subAgents: any[];
55
+ /** Full conversation history for API calls */
31
56
private apiConversationHistory : Message [ ] ;
57
+ /** Maximum number of conversation turns (0 means unlimited) */
32
58
private maxRun : number ;
59
+ /** System prompt that provides instructions to the model */
33
60
private systemPrompt : SystemPrompt ;
61
+ /** Messages from the user */
34
62
private userMessage : Message [ ] ;
63
+ /** The next user message to be added to the conversation */
35
64
private nextUserMessage :any ;
36
65
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
+ */
38
73
constructor ( tools : any = [ ] , systemPrompt : SystemPrompt , maxRun : number = 0 ) {
39
74
this . tools = tools ;
40
75
this . userMessage = [ ] ;
@@ -44,6 +79,13 @@ class Agent {
44
79
45
80
}
46
81
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
+ */
47
89
async run ( task : TaskInstruction , successCondition : ( ) => boolean = ( ) => true ) : Promise < { success : boolean ; error : string | null , message : string | null } > {
48
90
49
91
@@ -259,6 +301,13 @@ class Agent {
259
301
} ;
260
302
}
261
303
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
+ */
262
311
private async attemptLlmRequest ( apiConversationHistory : Message [ ] , tools : Record < string , any > ) : Promise < any > {
263
312
264
313
@@ -284,15 +333,36 @@ class Agent {
284
333
}
285
334
}
286
335
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
+ */
287
343
private async executeTool ( toolName : string , toolInput : any ) : Promise < [ boolean , any ] > {
288
344
//codebolttools--readfile
289
345
const [ toolboxName , actualToolName ] = toolName . split ( '--' ) ;
290
346
return tools . executeTool ( toolboxName , actualToolName , toolInput ) ;
291
347
}
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
+ */
292
356
private async startSubAgent ( agentName : string , params : any ) : Promise < [ boolean , any ] > {
293
357
return codeboltAgent . startAgent ( agentName , params . task ) ;
294
358
}
295
359
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
+ */
296
366
private getToolDetail ( tool : any ) : ToolDetails {
297
367
return {
298
368
toolName : tool . function . name ,
@@ -301,6 +371,13 @@ class Agent {
301
371
} ;
302
372
}
303
373
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
+ */
304
381
private getToolResult ( tool_call_id : string , content : string ) : ToolResult {
305
382
let userMessage = undefined
306
383
try {
@@ -322,7 +399,11 @@ class Agent {
322
399
} ;
323
400
}
324
401
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
+ */
326
407
private attemptApiRequest ( ) : any {
327
408
throw new Error ( "API request fallback not implemented" ) ;
328
409
}
0 commit comments