1+ import { PromptBuilder , LLMOutputHandler , FollowUpQuestionBuilder } from '../src/agentlib/promptbuilder' ;
2+ import llm from '../src/modules/llm' ;
3+ import type { CodeboltAPI } from '../src/types/libFunctionTypes' ;
4+
5+ /**
6+ * Example demonstrating the agent workflow using the new classes
7+ */
8+ async function runAgentWorkflow ( userMessage : any , codebolt : CodeboltAPI ) {
9+ try {
10+ // Step 1: Build the initial prompt
11+ const promptBuilderObject = new PromptBuilder ( userMessage , codebolt ) ;
12+ const userPrompt = await promptBuilderObject
13+ . addMCPTools ( )
14+ . addAgentTools ( )
15+ . addEnvironmentDetails ( )
16+ . addSystemPrompt ( 'agent.yaml' , 'test' , 'example.md' )
17+ . addTaskInstruction ( 'task.yaml' , 'main_task' )
18+ . buildInferenceParams ( ) ; // Use buildInferenceParams for LLM inference
19+
20+ // Step 2: Get initial LLM response
21+ let llmOutput = llm . inference ( userPrompt ) ;
22+ let llmOutputObject = new LLMOutputHandler ( llmOutput , codebolt ) ;
23+
24+ // Step 3: Main conversation loop
25+ while ( ! llmOutputObject . isCompleted ( ) ) {
26+ // Send the assistant's message to the user
27+ await llmOutputObject . sendMessageToUser ( ) ;
28+
29+ // Execute any tool calls in the response
30+ const toolCallResult = await llmOutputObject . runTools ( ) ;
31+
32+ // Step 4: Build follow-up prompt with tool results
33+ const followUpQuestionObject = new FollowUpQuestionBuilder ( codebolt ) ;
34+ const nextUserPrompt = await followUpQuestionObject
35+ . addPreviousConversation ( userPrompt )
36+ . addToolResult ( toolCallResult )
37+ . checkAndSummarizeConversationIfLong ( )
38+ . buildInferenceParams ( ) ; // Use buildInferenceParams for LLM inference
39+
40+ // Step 5: Get next LLM response
41+ llmOutput = llm . inference ( nextUserPrompt ) ;
42+ llmOutputObject = new LLMOutputHandler ( llmOutput , codebolt ) ;
43+
44+ // Update userPrompt for next iteration
45+ userPrompt . messages = nextUserPrompt . messages ;
46+ }
47+
48+ console . log ( "Agent workflow completed successfully!" ) ;
49+
50+ } catch ( error ) {
51+ console . error ( "Error in agent workflow:" , error ) ;
52+ throw error ;
53+ }
54+ }
55+
56+ /**
57+ * Alternative example with more detailed control
58+ */
59+ async function runDetailedAgentWorkflow ( userMessage : any , codebolt : CodeboltAPI ) {
60+ try {
61+ // Step 1: Build the initial prompt with more control
62+ const promptBuilder = new PromptBuilder ( userMessage , codebolt ) ;
63+
64+ // Add components step by step
65+ promptBuilder
66+ . addMCPTools ( [ 'codebolt' , 'filesystem' , 'browser' ] )
67+ . addAgentTools ( )
68+ . addEnvironmentDetails ( )
69+ . addSystemPrompt ( 'agent.yaml' , 'test' )
70+ . addTaskInstruction ( 'task.yaml' , 'main_task' )
71+ . addCustomSection ( 'Additional Context' , 'This is a complex task requiring multiple steps' ) ;
72+
73+ const initialPrompt = await promptBuilder . buildInferenceParams ( ) ;
74+
75+ // Step 2: Start the conversation
76+ let currentPrompt = initialPrompt ;
77+ let conversationTurn = 0 ;
78+ const maxTurns = 20 ; // Prevent infinite loops
79+
80+ while ( conversationTurn < maxTurns ) {
81+ console . log ( `\n--- Conversation Turn ${ conversationTurn + 1 } ---` ) ;
82+
83+ // Get LLM response
84+ const llmResponse = llm . inference ( currentPrompt ) ;
85+ const outputHandler = new LLMOutputHandler ( llmResponse , codebolt ) ;
86+
87+ // Send message to user
88+ await outputHandler . sendMessageToUser ( ) ;
89+
90+ // Check if completed
91+ if ( outputHandler . isCompleted ( ) ) {
92+ console . log ( "Task completed successfully!" ) ;
93+ break ;
94+ }
95+
96+ // Execute tools
97+ const toolResults = await outputHandler . runTools ( ) ;
98+
99+ // Build next prompt
100+ const followUpBuilder = new FollowUpQuestionBuilder ( codebolt ) ;
101+ followUpBuilder
102+ . addPreviousConversation ( currentPrompt )
103+ . addToolResult ( toolResults )
104+ . setMaxConversationLength ( 30 ) ; // Summarize after 30 messages
105+
106+ // Check if we need to summarize
107+ if ( followUpBuilder . shouldSummarize ( ) ) {
108+ console . log ( "Summarizing conversation due to length..." ) ;
109+ followUpBuilder . checkAndSummarizeConversationIfLong ( ) ;
110+ }
111+
112+ currentPrompt = await followUpBuilder . buildInferenceParams ( ) ;
113+ conversationTurn ++ ;
114+ }
115+
116+ if ( conversationTurn >= maxTurns ) {
117+ console . log ( "Maximum conversation turns reached. Stopping." ) ;
118+ }
119+
120+ } catch ( error ) {
121+ console . error ( "Error in detailed agent workflow:" , error ) ;
122+ throw error ;
123+ }
124+ }
125+
126+ /**
127+ * Example with error handling and recovery
128+ */
129+ async function runRobustAgentWorkflow ( userMessage : any , codebolt : CodeboltAPI ) {
130+ const maxRetries = 3 ;
131+ let retryCount = 0 ;
132+
133+ while ( retryCount < maxRetries ) {
134+ try {
135+ // Build initial prompt
136+ const promptBuilder = new PromptBuilder ( userMessage , codebolt ) ;
137+ const userPrompt = await promptBuilder
138+ . addAllAutomatic ( ) // Add all tools and environment automatically
139+ . addSystemPrompt ( 'agent.yaml' , 'robust_agent' )
140+ . buildInferenceParams ( ) ;
141+
142+ let currentPrompt = userPrompt ;
143+ let conversationActive = true ;
144+
145+ while ( conversationActive ) {
146+ try {
147+ // Get LLM response with timeout handling
148+ const llmResponse = llm . inference ( currentPrompt ) ;
149+ const outputHandler = new LLMOutputHandler ( llmResponse , codebolt ) ;
150+
151+ // Process response
152+ await outputHandler . sendMessageToUser ( ) ;
153+
154+ if ( outputHandler . isCompleted ( ) ) {
155+ conversationActive = false ;
156+ console . log ( "Workflow completed successfully!" ) ;
157+ return ; // Success - exit all retry loops
158+ }
159+
160+ // Execute tools with error handling
161+ const toolResults = await outputHandler . runTools ( ) ;
162+
163+ // Build next prompt
164+ const followUpBuilder = new FollowUpQuestionBuilder ( codebolt ) ;
165+ currentPrompt = await followUpBuilder
166+ . addPreviousConversation ( currentPrompt )
167+ . addToolResult ( toolResults )
168+ . checkAndSummarizeConversationIfLong ( )
169+ . buildInferenceParams ( ) ;
170+
171+ } catch ( stepError ) {
172+ console . error ( "Error in conversation step:" , stepError ) ;
173+
174+ // Add error recovery message
175+ const followUpBuilder = new FollowUpQuestionBuilder ( codebolt ) ;
176+ currentPrompt = await followUpBuilder
177+ . addPreviousConversation ( currentPrompt )
178+ . addUserMessage ( `An error occurred: ${ stepError } . Please try a different approach.` )
179+ . buildInferenceParams ( ) ;
180+ }
181+ }
182+
183+ return ; // Success
184+
185+ } catch ( error ) {
186+ retryCount ++ ;
187+ console . error ( `Workflow attempt ${ retryCount } failed:` , error ) ;
188+
189+ if ( retryCount >= maxRetries ) {
190+ console . error ( "Maximum retries reached. Workflow failed." ) ;
191+ throw error ;
192+ }
193+
194+ console . log ( `Retrying... (${ retryCount } /${ maxRetries } )` ) ;
195+ await new Promise ( resolve => setTimeout ( resolve , 1000 * retryCount ) ) ; // Exponential backoff
196+ }
197+ }
198+ }
199+
200+ export {
201+ runAgentWorkflow ,
202+ runDetailedAgentWorkflow ,
203+ runRobustAgentWorkflow
204+ } ;
0 commit comments