@@ -278,58 +278,64 @@ func (al *AgentLoop) Run(ctx context.Context) error {
278278 return nil
279279 }
280280 // Process message
281- // TODO: Re-enable media cleanup after inbound media is properly consumed by the agent.
282- // Currently disabled because files are deleted before the LLM can access their content.
283- // defer func() {
284- // if al.mediaStore != nil && msg.MediaScope != "" {
285- // if releaseErr := al.mediaStore.ReleaseAll(msg.MediaScope); releaseErr != nil {
286- // logger.WarnCF("agent", "Failed to release media", map[string]any{
287- // "scope": msg.MediaScope,
288- // "error": releaseErr.Error(),
289- // })
290- // }
291- // }
292- // }()
293-
294- response , err := al .processMessage (ctx , msg )
295- if err != nil {
296- response = fmt .Sprintf ("Error processing message: %v" , err )
297- }
298-
299- if response != "" {
300- // Check if the message tool already sent a response during this round.
301- // If so, skip publishing to avoid duplicate messages to the user.
302- // Use default agent's tools to check (message tool is shared).
303- alreadySent := false
304- defaultAgent := al .GetRegistry ().GetDefaultAgent ()
305- if defaultAgent != nil {
306- if tool , ok := defaultAgent .Tools .Get ("message" ); ok {
307- if mt , ok := tool .(* tools.MessageTool ); ok {
308- alreadySent = mt .HasSentInRound ()
309- }
281+ func () {
282+ defer func () {
283+ if al .channelManager != nil {
284+ al .channelManager .InvokeTypingStop (msg .Channel , msg .ChatID )
310285 }
286+ }()
287+ // TODO: Re-enable media cleanup after inbound media is properly consumed by the agent.
288+ // Currently disabled because files are deleted before the LLM can access their content.
289+ // defer func() {
290+ // if al.mediaStore != nil && msg.MediaScope != "" {
291+ // if releaseErr := al.mediaStore.ReleaseAll(msg.MediaScope); releaseErr != nil {
292+ // logger.WarnCF("agent", "Failed to release media", map[string]any{
293+ // "scope": msg.MediaScope,
294+ // "error": releaseErr.Error(),
295+ // })
296+ // }
297+ // }
298+ // }()
299+
300+ response , err := al .processMessage (ctx , msg )
301+ if err != nil {
302+ response = fmt .Sprintf ("Error processing message: %v" , err )
311303 }
312304
313- if ! alreadySent {
314- al .bus .PublishOutbound (ctx , bus.OutboundMessage {
315- Channel : msg .Channel ,
316- ChatID : msg .ChatID ,
317- Content : response ,
318- })
319- logger .InfoCF ("agent" , "Published outbound response" ,
320- map [string ]any {
321- "channel" : msg .Channel ,
322- "chat_id" : msg .ChatID ,
323- "content_len" : len (response ),
305+ if response != "" {
306+ // Check if the message tool already sent a response during this round.
307+ // If so, skip publishing to avoid duplicate messages to the user.
308+ // Use default agent's tools to check (message tool is shared).
309+ alreadySent := false
310+ defaultAgent := al .GetRegistry ().GetDefaultAgent ()
311+ if defaultAgent != nil {
312+ if tool , ok := defaultAgent .Tools .Get ("message" ); ok {
313+ if mt , ok := tool .(* tools.MessageTool ); ok {
314+ alreadySent = mt .HasSentInRound ()
315+ }
316+ }
317+ }
318+ if ! alreadySent {
319+ al .bus .PublishOutbound (ctx , bus.OutboundMessage {
320+ Channel : msg .Channel ,
321+ ChatID : msg .ChatID ,
322+ Content : response ,
324323 })
325- } else {
326- logger .DebugCF (
327- "agent" ,
328- "Skipped outbound (message tool already sent)" ,
329- map [string ]any {"channel" : msg .Channel },
330- )
324+ logger .InfoCF ("agent" , "Published outbound response" ,
325+ map [string ]any {
326+ "channel" : msg .Channel ,
327+ "chat_id" : msg .ChatID ,
328+ "content_len" : len (response ),
329+ })
330+ } else {
331+ logger .DebugCF (
332+ "agent" ,
333+ "Skipped outbound (message tool already sent)" ,
334+ map [string ]any {"channel" : msg .Channel },
335+ )
336+ }
331337 }
332- }
338+ }()
333339 default :
334340 time .Sleep (time .Microsecond * 200 )
335341 }
0 commit comments