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
+28 -7
View File
@@ -88,6 +88,7 @@ const (
ChatPartText = "text"
ChatPartReasoning = "reasoning"
ChatPartTool = "tool"
ChatPartFile = "file"
)
// ChatEvent.Kind values pushed on the chat_event stream. Step and usage carry
@@ -273,6 +274,21 @@ type ChatPromptRequest struct {
// sent with the prompt so history can be reconciled without guessing from
// text, time, order or page boundaries.
NativeMessageID string `json:"native_message_id,omitempty"`
// Attachments carries the bytes of files picked or pasted in the browser.
// Files are local to the browser, so a reference string alone is not enough
// for a server on another machine; Data is standard base64 and the server
// writes it to a private temporary file before prompting the agent.
Attachments []ChatAttachment `json:"attachments,omitempty"`
}
// ChatAttachment is one browser attachment carried by a chat prompt. Data is
// standard base64 (no data: prefix); Name is a display name and Mime is the
// browser's content type. The server derives the on-disk name and never trusts
// either for path construction.
type ChatAttachment struct {
Name string `json:"name"`
Mime string `json:"mime,omitempty"`
Data string `json:"data"`
}
// ChatMessage is one stored turn returned by chat_messages. A user turn is a
@@ -313,6 +329,8 @@ type ChatPart struct {
ID string `json:"id,omitempty"`
Text string `json:"text,omitempty"`
Name string `json:"name,omitempty"`
URI string `json:"uri,omitempty"`
Mime string `json:"mime,omitempty"`
Executed bool `json:"executed,omitempty"`
State *ChatToolState `json:"state,omitempty"`
Time *ChatTime `json:"time,omitempty"`
@@ -365,13 +383,16 @@ type ChatTime struct {
// tool carries a full Part with its state, step marks turn progress and usage
// reports the turn's totals.
type ChatEventFrame struct {
SessionID string `json:"session_id"`
MessageID string `json:"message_id,omitempty"`
Kind string `json:"kind"`
Delta string `json:"delta,omitempty"`
Part *ChatPart `json:"part,omitempty"`
Step *ChatStep `json:"step,omitempty"`
Usage *ChatUsage `json:"usage,omitempty"`
SessionID string `json:"session_id"`
MessageID string `json:"message_id,omitempty"`
Kind string `json:"kind"`
Delta string `json:"delta,omitempty"`
Part *ChatPart `json:"part,omitempty"`
// Attachments carries the file parts of a user turn another client sent.
// A frame has one Part, so the prompt's files travel alongside its text.
Attachments []ChatPart `json:"attachments,omitempty"`
Step *ChatStep `json:"step,omitempty"`
Usage *ChatUsage `json:"usage,omitempty"`
// Shell is the payload of a ChatEventShell frame: the shell's id, its
// status, the command it is running and (once it has exited) its exit code.
// It is nil on every other kind.
+1
View File
@@ -24,6 +24,7 @@ const (
RunStatusQueued = "queued"
RunStatusWaiting = "waiting"
RunStatusRunning = "running"
RunStatusCanceled = "canceled"
StepStatusRunning = "running"
StepStatusWaiting = "waiting"
// NotifyWaiting is the notification event a human stage emits when it parks