Chat: draw agent follow-ups and automatic continues apart from the operator
The server now names a user turn's origin (followup, auto_continue, auto_switch, scheduled) on chat messages, user frames and queued prompts, from the prompt's operation id while it holds the prompt and from its own text prefixes in history read later. Origin is a new field, so older clients keep treating these as ordinary user turns. The app draws an agent follow-up, auto-continue and account-switch prompt as a quiet outlined card on the agent's side with an icon, label and the schedule facts from the preamble; the operator's own scheduled message keeps its bubble with a small Scheduled mark. Find searches the drawn text. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,76 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"strings"
|
||||
|
||||
"git.a-wan.net/alan/pipelines/internal/protocol"
|
||||
)
|
||||
|
||||
// A user turn the operator did not type just now carries an origin
|
||||
// (protocol.ChatOrigin*), so clients can draw an agent's follow-up or the
|
||||
// server's automatic continue apart from the operator's own messages. While
|
||||
// the server holds the prompt (queued, or retained after delivery) the origin
|
||||
// comes from its operation id; a history page read later, after the server
|
||||
// forgot the prompt, recognises the server-written text prefixes instead.
|
||||
|
||||
const (
|
||||
// chatFollowupTextPrefix starts the text of an agent's follow-up.
|
||||
chatFollowupTextPrefix = "[Follow-up you scheduled at "
|
||||
// chatAutoSwitchTextPrefix starts the text of the auto-switch continue.
|
||||
chatAutoSwitchTextPrefix = "[Sent automatically by Pipelines]"
|
||||
)
|
||||
|
||||
// chatPromptOrigin is the origin of a prompt sent under operationID;
|
||||
// scheduled says it waits for a send_at (the operator's "send later").
|
||||
func chatPromptOrigin(operationID string, scheduled bool) string {
|
||||
switch {
|
||||
case strings.HasPrefix(operationID, chatFollowupPrefix):
|
||||
return protocol.ChatOriginFollowup
|
||||
case strings.HasPrefix(operationID, chatAutoContinuePrefix):
|
||||
return protocol.ChatOriginAutoContinue
|
||||
case strings.HasPrefix(operationID, chatAutoSwitchPrefix):
|
||||
return protocol.ChatOriginAutoSwitch
|
||||
case scheduled:
|
||||
return protocol.ChatOriginScheduled
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
// chatTextOrigin is the origin a user turn's text shows on its own: only the
|
||||
// server's own prefixes are recognised, so an operator's message never is.
|
||||
func chatTextOrigin(message protocol.ChatMessage) string {
|
||||
for _, part := range message.Parts {
|
||||
if part.Type != protocol.ChatPartText && part.Type != "" {
|
||||
continue
|
||||
}
|
||||
switch text := strings.TrimSpace(part.Text); {
|
||||
case strings.HasPrefix(text, chatFollowupTextPrefix):
|
||||
return protocol.ChatOriginFollowup
|
||||
case strings.HasPrefix(text, chatAutoSwitchTextPrefix):
|
||||
return protocol.ChatOriginAutoSwitch
|
||||
}
|
||||
return ""
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
// markChatOrigins gives each user turn of a history page that has no origin
|
||||
// the one its text shows. The page is copied before a change, since a
|
||||
// harness may keep the slice it returned.
|
||||
func markChatOrigins(messages []protocol.ChatMessage) []protocol.ChatMessage {
|
||||
out := messages
|
||||
for i := range messages {
|
||||
if messages[i].Role != protocol.ChatRoleUser || messages[i].Origin != "" {
|
||||
continue
|
||||
}
|
||||
origin := chatTextOrigin(messages[i])
|
||||
if origin == "" {
|
||||
continue
|
||||
}
|
||||
if &out[0] == &messages[0] {
|
||||
out = append([]protocol.ChatMessage(nil), messages...)
|
||||
}
|
||||
out[i].Origin = origin
|
||||
}
|
||||
return out
|
||||
}
|
||||
Reference in New Issue
Block a user