@@ -28,6 +28,19 @@ import (
2828 "infini.sh/framework/core/util"
2929)
3030
31+ // getCancelledMessage returns a localized "task cancelled" message based on
32+ // the CancelLang stored in the inflight task. The cancel API writes the user's
33+ // UI language into the task right before triggering cancellation, so we can
34+ // read it here and produce an appropriate message without a full i18n system.
35+ func getCancelledMessage (taskID string ) string {
36+ if v , ok := InflightMessages .Load (taskID ); ok {
37+ if task , ok := v .(common2.MessageTask ); ok && strings .HasPrefix (task .CancelLang , "zh" ) {
38+ return "~~该任务已被取消。~~"
39+ }
40+ }
41+ return "~~This task has been cancelled.~~"
42+ }
43+
3144func CreateAssistantReplyMessage (sessionID , assistantID , requestMessageID string ) * core.ChatMessage {
3245 msg := & core.ChatMessage {
3346 SessionID : sessionID ,
@@ -71,6 +84,8 @@ func ProcessMessageAsync(ctx context.Context, userID string, reqMsg, replyMsg *c
7184 )
7285
7386 defer func () {
87+ taskID := GetReplyMessageTaskID (params .SessionID , reqMsg .ID )
88+
7489 if ! global .Env ().IsDebug {
7590 if r := recover (); r != nil {
7691 var v string
@@ -85,8 +100,11 @@ func ProcessMessageAsync(ctx context.Context, userID string, reqMsg, replyMsg *c
85100
86101 // If the context was cancelled (user switched chat or clicked cancel),
87102 // treat it as a graceful stop — no error message to the user.
88- if ctx .Err () != nil {
103+ if ctx .Err () != nil || strings . Contains ( v , "context canceled" ) || strings . Contains ( v , "context deadline exceeded" ) {
89104 log .Infof ("async processing cancelled by user: %v" , v )
105+ if replyMsg .Message == "" {
106+ replyMsg .Message = getCancelledMessage (taskID )
107+ }
90108 } else {
91109 msg := fmt .Sprintf ("⚠️ error in async processing message reply, %v" , v )
92110 if replyMsg .Message != "" {
@@ -102,13 +120,19 @@ func ProcessMessageAsync(ctx context.Context, userID string, reqMsg, replyMsg *c
102120 }
103121
104122 if err != nil {
105- log .Errorf ("Failed to process message reply: %v" , err )
106- replyMsg .Message += err .Error ()
123+ if ctx .Err () != nil || strings .Contains (err .Error (), "context canceled" ) || strings .Contains (err .Error (), "context deadline exceeded" ) {
124+ log .Infof ("async processing cancelled: %v" , err )
125+ if replyMsg .Message == "" {
126+ replyMsg .Message = getCancelledMessage (taskID )
127+ }
128+ } else {
129+ log .Errorf ("Failed to process message reply: %v" , err )
130+ replyMsg .Message += err .Error ()
131+ }
107132 }
108133
109134 finalizeProcessing (ctx , params .SessionID , replyMsg , sender )
110135 // clear the inflight message task
111- taskID := GetReplyMessageTaskID (params .SessionID , reqMsg .ID )
112136 InflightMessages .Delete (taskID )
113137
114138 log .Info ("finished async processing message" )
0 commit comments