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 }