2a182d7f17
Keep Esc and /stop scoped to the foreground turn while exposing an explicit stop-tasks operation for supported chat backends. Add the protocol, server routing, app command, and coverage for task stopping and unsupported harnesses.
40 lines
1.2 KiB
Go
40 lines
1.2 KiB
Go
package server
|
|
|
|
import (
|
|
"context"
|
|
|
|
"git.a-wan.net/alan/pipelines/internal/protocol"
|
|
)
|
|
|
|
func handleChatStopTasks(ctx context.Context, request protocol.Message, broker chatBroker, write func(protocol.Message) error) {
|
|
query := request.ChatQuery
|
|
if query == nil || !validChatSessionID(query.SessionID) {
|
|
_ = write(protocol.Message{Type: protocol.Error, ID: request.ID, Error: "a valid chat session id is required"})
|
|
return
|
|
}
|
|
if broker == nil {
|
|
_ = write(protocol.Message{Type: protocol.Error, ID: request.ID, Error: "chat is not configured on this server"})
|
|
return
|
|
}
|
|
stopper, ok := broker.(chatTaskStopper)
|
|
if !ok {
|
|
_ = write(protocol.Message{Type: protocol.Error, ID: request.ID, Error: "this chat backend cannot stop background tasks separately"})
|
|
return
|
|
}
|
|
stopped, err := stopper.StopTasks(ctx, query.SessionID)
|
|
if err != nil {
|
|
_ = write(protocol.Message{Type: protocol.Error, ID: request.ID, Error: err.Error()})
|
|
return
|
|
}
|
|
message := "No background tasks are running in this chat."
|
|
if stopped > 0 {
|
|
message = "Stopped background tasks."
|
|
}
|
|
_ = write(protocol.Message{
|
|
Type: protocol.ChatStopTasks, ID: request.ID,
|
|
ChatStopTasks: &protocol.ChatStopTasksResult{
|
|
SessionID: query.SessionID, Stopped: stopped, Message: message,
|
|
},
|
|
})
|
|
}
|