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:
@@ -0,0 +1,55 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"encoding/base64"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"git.a-wan.net/alan/pipelines/internal/protocol"
|
||||
)
|
||||
|
||||
func TestPrepareChatAttachmentsWritesPrivateFileAndPart(t *testing.T) {
|
||||
data := base64.StdEncoding.EncodeToString([]byte("hello attachment"))
|
||||
files, parts, err := prepareChatAttachments("ses_test", []protocol.ChatAttachment{{
|
||||
Name: "../../note.txt", Mime: "text/plain", Data: data,
|
||||
}})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if len(files) != 1 || len(parts) != 1 {
|
||||
t.Fatalf("files=%d parts=%d", len(files), len(parts))
|
||||
}
|
||||
if files[0].Name != "note.txt" || parts[0].Name != "note.txt" || parts[0].Mime != "text/plain" {
|
||||
t.Fatalf("attachment = %+v / %+v", files[0], parts[0])
|
||||
}
|
||||
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("body = %q", body)
|
||||
}
|
||||
info, err := os.Stat(path)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if info.Mode().Perm() != 0o600 {
|
||||
t.Fatalf("mode = %o, want 0600", info.Mode().Perm())
|
||||
}
|
||||
if filepath.Dir(path) == filepath.Dir("/tmp") {
|
||||
t.Fatalf("attachment escaped its private directory: %s", path)
|
||||
}
|
||||
}
|
||||
|
||||
func TestPrepareChatAttachmentsRejectsInvalidAndOversized(t *testing.T) {
|
||||
if _, _, err := prepareChatAttachments("ses_test", []protocol.ChatAttachment{{Name: "x", Data: "not base64"}}); err == nil {
|
||||
t.Fatal("invalid base64 accepted")
|
||||
}
|
||||
oversized := base64.StdEncoding.EncodeToString(make([]byte, maxChatAttachmentBytes+1))
|
||||
if _, _, err := prepareChatAttachments("ses_test", []protocol.ChatAttachment{{Name: "big", Data: oversized}}); err == nil {
|
||||
t.Fatal("oversized attachment accepted")
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user