chat: deliver attachments and cancel runs immediately

Carry browser files as base64 prompt attachments, write them to private
temporary files and pass file:// references to OpenCode, and map the user
turn's top-level files back into history. Replay a running step on subscribe,
forward only a caller-supplied msg_ id, and persist cancel on the run and live
step before interrupting the runner.
This commit is contained in:
2026-10-20 14:30:53 +01:00
parent 7758d20feb
commit f5ccec8e1f
17 changed files with 570 additions and 29 deletions
+24 -3
View File
@@ -45,6 +45,11 @@ func handleChatPrompt(ctx context.Context, request protocol.Message, broker chat
_ = write(protocol.Message{Type: protocol.Error, ID: request.ID, Error: "chat prompt exceeds 64 KiB"})
return
}
attachmentFiles, attachmentParts, err := prepareChatAttachments(prompt.SessionID, prompt.Attachments)
if err != nil {
_ = write(protocol.Message{Type: protocol.Error, ID: request.ID, Error: err.Error()})
return
}
sessionID := prompt.SessionID
sub := hub.attach(sessionID, clientID, sanitizeChatClientLabel(prompt.Client), broker, write)
if sub == nil {
@@ -58,6 +63,7 @@ func handleChatPrompt(ctx context.Context, request protocol.Message, broker chat
MessageID: prompt.NativeMessageID,
Kind: protocol.ChatEventUser,
Part: &protocol.ChatPart{Type: protocol.ChatPartText, ID: "usr", Text: prompt.Text},
Attachments: attachmentParts,
Client: sub.label,
OperationID: prompt.OperationID,
}
@@ -66,10 +72,14 @@ func handleChatPrompt(ctx context.Context, request protocol.Message, broker chat
}
queued, refused := hub.submitPrompt(sessionID, &chatQueuedPrompt{
text: prompt.Text,
files: attachmentFiles,
operationID: prompt.OperationID,
nativeID: userFrame.MessageID,
requestID: request.ID,
write: write,
// Only the caller's real OpenCode message id is forwarded. The hub's own
// `usr_` display id is not a valid OpenCode `msg_` id, so a client that
// omitted native_message_id lets OpenCode generate one instead.
nativeID: prompt.NativeMessageID,
requestID: request.ID,
write: write,
})
if refused {
_ = write(protocol.Message{Type: protocol.Error, ID: request.ID, Error: "session " + sessionID + " already has the maximum number of prompts queued"})
@@ -125,6 +135,17 @@ func handleChatSubscribe(request protocol.Message, broker chatBroker, hub *chatH
_ = write(protocol.Message{Type: protocol.Error, ID: request.ID, Error: "could not observe session " + query.SessionID})
return
}
// A turn already in flight when this client attaches has no `step started`
// frame in its future, so replay the live marker. The composer's Stop control
// and the transcript's one activity row read it; the later `step ended` frame
// settles them normally.
if hub.turnRunning(query.SessionID) {
sub.send(protocol.Message{Type: protocol.ChatEvent, ChatEvent: &protocol.ChatEventFrame{
SessionID: query.SessionID,
Kind: protocol.ChatEventStep,
Step: &protocol.ChatStep{Status: protocol.ChatStepStarted},
}})
}
// Replay every still-unresolved approval request the room holds, not only the
// most recent one, so a client that attaches after two requests were raised
// still sees both cards.