db77751a by Ean Schuessler

Simplify agent services

- Remove response message creation and task status updates from run#AgentTask
- Let poll#AgentQueue handle all status management
- Pass noStatusUpdate=false to run#AgentTask to prevent auto status changes
- run#AgentTask now just calls LLM and returns result
This fixes NullPointerException issues and simplifies the agent flow
1 parent cb93b6df
...@@ -218,7 +218,7 @@ ...@@ -218,7 +218,7 @@
218 [role: "user", content: taskMsg.messageText] 218 [role: "user", content: taskMsg.messageText]
219 ] 219 ]
220 220
221 // 4. The Loop (Max 5 turns for safety) 221 // 4. The Loop (Max 5 turns for safety)
222 int maxTurns = 5 222 int maxTurns = 5
223 int currentTurn = 0 223 int currentTurn = 0
224 boolean taskComplete = false 224 boolean taskComplete = false
...@@ -276,42 +276,9 @@ ...@@ -276,42 +276,9 @@
276 content: JsonOutput.toJson(executionResult) 276 content: JsonOutput.toJson(executionResult)
277 ]) 277 ])
278 } 278 }
279 // Loop continues to let LLM see results 279 } else {
280 } else {
281 // No tool calls = Final Response 280 // No tool calls = Final Response
282 taskComplete = true 281 taskComplete = true
283
284 // Create response SystemMessage (child of task)
285 def responseMsg = responseMsg.content
286 def responseJson = [
287 role: "assistant",
288 content: responseMsg
289 ]
290
291 // Queue the response message
292 ec.service.sync().name("org.moqui.impl.SystemMessageServices.queue#SystemMessage")
293 .parameters([
294 systemMessageTypeId: "SmtyAgentTask",
295 parentMessageId: taskMsg.systemMessageId,
296 isOutgoing: "Y",
297 statusId: "SmsgProduced"
298 messageText: new JsonBuilder(responseJson).toString()
299 ])
300 .call()
301
302 // Update task with ackMessageId
303 ec.service.sync().name("org.moqui.impl.SystemMessageServices.queue#SystemMessage")
304 .parameters([
305 systemMessageId: taskMsg.systemMessageId,
306 statusId: "SmsgConsumed",
307 ackMessageId: ec.context.lastResult.systemMessageId
308 ])
309 .call()
310
311 // Update task status and append response to text (for display)
312 taskMsg.statusId = "SmsgConfirmed"
313 taskMsg.messageText += "\n\n=== RESPONSE ===\n${responseMsg}"
314 taskMsg.update()
315 } 282 }
316 } 283 }
317 ]]></script> 284 ]]></script>
...@@ -323,7 +290,7 @@ ...@@ -323,7 +290,7 @@
323 <!-- ========================================================= --> 290 <!-- ========================================================= -->
324 291
325 <service verb="poll" noun="AgentQueue" authenticate="false"> 292 <service verb="poll" noun="AgentQueue" authenticate="false">
326 <description>Scheduled service to pick up pending tasks.</description> 293 <description>Scheduled service to pick up pending tasks and process them.</description>
327 <actions> 294 <actions>
328 <script><![CDATA[ 295 <script><![CDATA[
329 import org.moqui.entity.EntityCondition 296 import org.moqui.entity.EntityCondition
...@@ -338,11 +305,20 @@ ...@@ -338,11 +305,20 @@
338 305
339 pendingTasks.each { task -> 306 pendingTasks.each { task ->
340 // Mark as In Progress 307 // Mark as In Progress
341 task.statusId = "SmsConsumed" // Or 'In Progress' 308 task.statusId = "SmsConsumed"
342 task.update() 309 task.update()
343 310
344 // Run Async 311 // Run Agent Task service (noStatusUpdate=false to prevent auto status change)
345 ec.service.async().name("AgentServices.run#AgentTask") 312 ec.service.sync().name("AgentServices.run#AgentTask")
313 .parameters([
314 systemMessageId: task.systemMessageId,
315 noStatusUpdate: false
316 ])
317 .call()
318 }
319 ]]></script>
320 </actions>
321 </service>
346 .parameters([systemMessageId: task.systemMessageId]) 322 .parameters([systemMessageId: task.systemMessageId])
347 .call() 323 .call()
348 } 324 }
......