@@ -30,7 +30,7 @@ import type {
3030 Task ,
3131 AgentPlaybackControl ,
3232} from "../types/agentTypes" ;
33- import { useAgentStore } from "./stores" ;
33+ import { useAgentStore , useMessageStore } from "./stores" ;
3434import { i18n } from "next-i18next" ;
3535
3636const TIMEOUT_LONG = 1000 ;
@@ -50,9 +50,8 @@ class AutonomousAgent {
5050 mode : AgentMode ;
5151 playbackControl : AgentPlaybackControl ;
5252
53- tasks : Task [ ] = [ ] ;
5453 completedTasks : string [ ] = [ ] ;
55- isRunning = true ;
54+ isRunning = false ;
5655 numLoops = 0 ;
5756 currentTask ?: Task ;
5857
@@ -88,6 +87,18 @@ class AutonomousAgent {
8887 }
8988
9089 async run ( ) {
90+ if ( ! this . isRunning ) {
91+ this . isRunning = true ;
92+ await this . startGoal ( ) ;
93+ }
94+
95+ await this . loop ( ) ;
96+ if ( this . mode === PAUSE_MODE && ! this . isRunning ) {
97+ this . handlePause ( { agentPlaybackControl : this . playbackControl } ) ;
98+ }
99+ }
100+
101+ async startGoal ( ) {
91102 const { isGuestMode, isValidGuest } = this . guestSettings ;
92103 if ( isGuestMode && ! isValidGuest && ! this . modelSettings . customApiKey ) {
93104 this . sendErrorMessage (
@@ -96,49 +107,38 @@ class AutonomousAgent {
96107 this . stopAgent ( ) ;
97108 return ;
98109 }
99- if ( this . tasks . length === 0 ) {
100- this . sendGoalMessage ( ) ;
101- this . sendThinkingMessage ( ) ;
102-
103- // Initialize by getting tasks
104- try {
105- const taskValues = await this . getInitialTasks ( ) ;
106- for ( const value of taskValues ) {
107- await new Promise ( ( r ) => setTimeout ( r , TIMOUT_SHORT ) ) ;
108- const task : Task = {
109- taskId : v1 ( ) . toString ( ) ,
110- value,
111- status : TASK_STATUS_STARTED ,
112- type : MESSAGE_TYPE_TASK ,
113- } ;
114- this . sendMessage ( task ) ;
115- this . tasks . push ( task ) ;
116- }
117- } catch ( e ) {
118- console . log ( e ) ;
119- this . sendErrorMessage ( getMessageFromError ( e ) ) ;
120- this . shutdown ( ) ;
121- return ;
122- }
123- }
110+ this . sendGoalMessage ( ) ;
111+ this . sendThinkingMessage ( ) ;
124112
125- await this . loop ( ) ;
126- if ( this . mode === PAUSE_MODE && ! this . isRunning ) {
127- this . handlePause ( { agentPlaybackControl : this . playbackControl } ) ;
113+ // Initialize by getting taskValues
114+ try {
115+ const taskValues = await this . getInitialTasks ( ) ;
116+ for ( const value of taskValues ) {
117+ await new Promise ( ( r ) => setTimeout ( r , TIMOUT_SHORT ) ) ;
118+ const task : Task = {
119+ taskId : v1 ( ) . toString ( ) ,
120+ value,
121+ status : TASK_STATUS_STARTED ,
122+ type : MESSAGE_TYPE_TASK ,
123+ } ;
124+ this . sendMessage ( task ) ;
125+ }
126+ } catch ( e ) {
127+ console . log ( e ) ;
128+ this . sendErrorMessage ( getMessageFromError ( e ) ) ;
129+ this . shutdown ( ) ;
130+ return ;
128131 }
129132 }
130133
131134 async loop ( ) {
132- console . log ( `Loop ${ this . numLoops } ` ) ;
133- console . log ( this . tasks ) ;
134-
135135 this . conditionalPause ( ) ;
136136
137137 if ( ! this . isRunning ) {
138138 return ;
139139 }
140140
141- if ( this . tasks . length === 0 ) {
141+ if ( this . getRemainingTasks ( ) . length === 0 ) {
142142 this . sendCompletedMessage ( ) ;
143143 this . shutdown ( ) ;
144144 return ;
@@ -155,7 +155,9 @@ class AutonomousAgent {
155155 // Wait before starting
156156 await new Promise ( ( r ) => setTimeout ( r , TIMEOUT_LONG ) ) ;
157157
158- const currentTask = this . tasks . shift ( ) as Task ;
158+ // Start with first task
159+ const currentTask = this . getRemainingTasks ( ) [ 0 ] as Task ;
160+ this . sendMessage ( { ...currentTask , status : TASK_STATUS_EXECUTING } ) ;
159161
160162 this . currentTask = currentTask ;
161163
@@ -171,19 +173,15 @@ class AutonomousAgent {
171173 this . sendAnalysisMessage ( analysis , currentTask . taskId ) ;
172174 }
173175
174- // Execute first task
175- // Get and remove first task
176- this . completedTasks . push ( this . tasks [ 0 ] ?. value || "" ) ;
177-
178- this . sendMessage ( { ...currentTask , status : TASK_STATUS_EXECUTING } ) ;
179-
180176 const result = await this . executeTask ( currentTask . value , analysis ) ;
181177 this . sendMessage ( {
182178 ...currentTask ,
183179 info : result ,
184180 status : TASK_STATUS_COMPLETED ,
185181 } ) ;
186182
183+ this . completedTasks . push ( currentTask . value || "" ) ;
184+
187185 // Wait before adding tasks
188186 await new Promise ( ( r ) => setTimeout ( r , TIMEOUT_LONG ) ) ;
189187 this . sendThinkingMessage ( currentTask . taskId ) ;
@@ -202,10 +200,9 @@ class AutonomousAgent {
202200 } ;
203201 return task ;
204202 } ) ;
205- this . tasks = newTasks . concat ( this . tasks ) ;
203+
206204 for ( const task of newTasks ) {
207205 await new Promise ( ( r ) => setTimeout ( r , TIMOUT_SHORT ) ) ;
208- // this.tasks.push(task);
209206 this . sendMessage ( task ) ;
210207 }
211208
@@ -223,8 +220,13 @@ class AutonomousAgent {
223220 await this . loop ( ) ;
224221 }
225222
223+ getRemainingTasks ( ) {
224+ const tasks = useMessageStore . getState ( ) . tasks ;
225+ return tasks . filter ( ( task : Task ) => task . status === TASK_STATUS_STARTED ) ;
226+ }
227+
226228 private conditionalPause ( ) {
227- if ( this . mode !== PAUSE_MODE ) {
229+ if ( this . mode != PAUSE_MODE ) {
228230 return ;
229231 }
230232
@@ -275,7 +277,7 @@ class AutonomousAgent {
275277 currentTask : string ,
276278 result : string
277279 ) : Promise < string [ ] > {
278- const taskValues = this . tasks . map ( ( task ) => task . value ) ;
280+ const taskValues = this . getRemainingTasks ( ) . map ( ( task ) => task . value ) ;
279281
280282 if ( this . shouldRunClientSide ( ) ) {
281283 return await AgentService . createTasksAgent (
0 commit comments