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
+46
View File
@@ -2,6 +2,7 @@ package server
import (
"context"
"encoding/base64"
"encoding/json"
"errors"
"fmt"
@@ -41,6 +42,7 @@ func newOperatorStore(t *testing.T) *pipeline.Store {
type fakeChat struct {
mu sync.Mutex
promptCalls int
promptFiles []opencode.PromptFile
interrupts int
replies int
replyRequest protocol.ChatReplyRequest
@@ -107,6 +109,13 @@ func (f *fakeChat) Prompt(context.Context, string, string) error {
return nil
}
func (f *fakeChat) PromptWithMessageIDFiles(ctx context.Context, sessionID, text, _ string, files []opencode.PromptFile) error {
f.mu.Lock()
f.promptFiles = append([]opencode.PromptFile(nil), files...)
f.mu.Unlock()
return f.Prompt(ctx, sessionID, text)
}
func (f *fakeChat) Stream(ctx context.Context, _ string, ready func(), emit func(protocol.ChatEventFrame) error) error {
f.mu.Lock()
f.subscribed = true
@@ -409,6 +418,43 @@ func readChatTurn(t *testing.T, ctx context.Context, conn *websocket.Conn) []pro
}
}
func TestChatPromptWritesAttachmentAndForwardsLocalFile(t *testing.T) {
broker := scriptedChat()
ctx, _, conn := dialChat(t, broker)
data := base64.StdEncoding.EncodeToString([]byte("hello attachment"))
if err := wsjson.Write(ctx, conn, protocol.Message{Type: protocol.ChatPrompt, ID: "r1", ChatPrompt: &protocol.ChatPromptRequest{
SessionID: "ses_1",
Text: "read the attached note",
Attachments: []protocol.ChatAttachment{{
Name: "note.txt", Mime: "text/plain", Data: data,
}},
}}); err != nil {
t.Fatal(err)
}
var ack protocol.Message
if err := wsjson.Read(ctx, conn, &ack); err != nil {
t.Fatal(err)
}
if ack.Type != protocol.ChatPrompt || ack.Error != "" {
t.Fatalf("ack = %+v", ack)
}
_ = readChatTurn(t, ctx, conn)
broker.mu.Lock()
files := append([]opencode.PromptFile(nil), broker.promptFiles...)
broker.mu.Unlock()
if len(files) != 1 || files[0].Name != "note.txt" {
t.Fatalf("prompt files = %+v", files)
}
path := strings.TrimPrefix(files[0].URI, "file://")
body, err := os.ReadFile(path)
if err != nil {
t.Fatal(err)
}
if string(body) != "hello attachment" {
t.Fatalf("attachment body = %q", body)
}
}
func TestChatPromptStreamsEvents(t *testing.T) {
broker := scriptedChat()
ctx, _, conn := dialChat(t, broker)