Send mid-turn chat prompts to the harness and queue the rest durably on the server
Claude Code (stream-json stdin) and Codex (turn/steer pinned to the active turn) receive a prompt sent while a turn runs immediately. Other harnesses (OpenCode) get a durable chat_prompt_queue row owned by the session: it survives the last client detaching and a server restart, is delivered in order when the turn ends with or without a client, and is deduplicated by operation id. chat_subscribe carries the queue, chat_queue frames report delivered/removed/failed changes, and chat_unqueue removes a waiting prompt.
This commit is contained in:
@@ -35,6 +35,14 @@ type transcriptEntry struct {
|
||||
} `json:"message"`
|
||||
ToolUseResult json.RawMessage `json:"toolUseResult"`
|
||||
Compact json.RawMessage `json:"compactMetadata"`
|
||||
// Attachment is set on "attachment" entries; a queued_command with
|
||||
// commandMode "prompt" is a user message the CLI read mid-turn. Prompt is
|
||||
// either a string or an array of content blocks.
|
||||
Attachment struct {
|
||||
Type string `json:"type"`
|
||||
Prompt json.RawMessage `json:"prompt"`
|
||||
CommandMode string `json:"commandMode"`
|
||||
} `json:"attachment"`
|
||||
}
|
||||
|
||||
func readTranscript(path string) ([]transcriptEntry, error) {
|
||||
@@ -150,6 +158,22 @@ func (s *session) transcriptMessages(limit int, cursor string) (protocol.ChatHis
|
||||
messages := []protocol.ChatMessage{}
|
||||
messageIndex := map[string]int{}
|
||||
toolIndex := map[string][2]int{} // tool id -> message/part index
|
||||
addUser := func(entry transcriptEntry, text string, files []protocol.ChatPart) {
|
||||
id := takePromptID(text)
|
||||
if id == "" {
|
||||
id = entry.UUID
|
||||
}
|
||||
if id == "" {
|
||||
return
|
||||
}
|
||||
created, _ := transcriptTime(entry.Timestamp)
|
||||
parts := []protocol.ChatPart{}
|
||||
if text != "" {
|
||||
parts = append(parts, protocol.ChatPart{Type: protocol.ChatPartText, ID: id + "#text", Text: text})
|
||||
}
|
||||
parts = append(parts, files...)
|
||||
messages = append(messages, protocol.ChatMessage{ID: id, SessionID: s.id, Role: protocol.ChatRoleUser, CreatedAt: created, Parts: parts})
|
||||
}
|
||||
for _, entry := range entries {
|
||||
if entry.IsSidechain {
|
||||
continue
|
||||
@@ -239,20 +263,29 @@ func (s *session) transcriptMessages(limit int, cursor string) (protocol.ChatHis
|
||||
if hasResult || (text == "" && len(files) == 0) {
|
||||
continue
|
||||
}
|
||||
id := takePromptID(text)
|
||||
if id == "" {
|
||||
id = entry.UUID
|
||||
}
|
||||
if id == "" {
|
||||
addUser(entry, text, files)
|
||||
case "attachment":
|
||||
// A message the operator sent while the turn was running is recorded
|
||||
// as a queued_command attachment where the CLI surfaced it to the
|
||||
// model, so it is the user's turn at that point of the transcript.
|
||||
// Other queued commands (task notifications, agent messages) are
|
||||
// harness input, not the operator's.
|
||||
if entry.Attachment.Type != "queued_command" || entry.Attachment.CommandMode != "prompt" {
|
||||
continue
|
||||
}
|
||||
created, _ := transcriptTime(entry.Timestamp)
|
||||
parts := []protocol.ChatPart{}
|
||||
if text != "" {
|
||||
parts = append(parts, protocol.ChatPart{Type: protocol.ChatPartText, ID: id + "#text", Text: text})
|
||||
var text string
|
||||
for _, block := range decodeBlocks(entry.Attachment.Prompt) {
|
||||
if block.Type == "text" && strings.TrimSpace(block.Text) != "" {
|
||||
if text != "" {
|
||||
text += "\n"
|
||||
}
|
||||
text += block.Text
|
||||
}
|
||||
}
|
||||
parts = append(parts, files...)
|
||||
messages = append(messages, protocol.ChatMessage{ID: id, SessionID: s.id, Role: protocol.ChatRoleUser, CreatedAt: created, Parts: parts})
|
||||
if strings.TrimSpace(text) == "" {
|
||||
continue
|
||||
}
|
||||
addUser(entry, text, []protocol.ChatPart{})
|
||||
}
|
||||
}
|
||||
// Drop assistant messages that ended with no renderable part (for example a
|
||||
|
||||
Reference in New Issue
Block a user