fix(chat): settle authoritative turns and make capabilities explicit

This commit is contained in:
2026-10-20 15:45:09 +01:00
parent 016fcb552a
commit e504780976
16 changed files with 615 additions and 56 deletions
+98 -25
View File
@@ -3,16 +3,41 @@ package server
import (
"encoding/base64"
"fmt"
"io/fs"
"net/url"
"os"
"path/filepath"
"strings"
"sync"
"time"
"git.a-wan.net/alan/pipelines/internal/opencode"
"git.a-wan.net/alan/pipelines/internal/protocol"
)
// Files survive process restarts and asynchronous harness inbox admission.
// Only unleased files older than seven days are collected, on the next upload.
const chatAttachmentRetention = 7 * 24 * time.Hour
const chatAttachmentQuota = 64 << 20
const chatAttachmentFileQuota = 512
var chatAttachmentStore = struct {
sync.Mutex
leased map[string]bool
}{leased: make(map[string]bool)}
func releaseChatAttachments(files []opencode.PromptFile) {
chatAttachmentStore.Lock()
defer chatAttachmentStore.Unlock()
for _, file := range files {
u, err := url.Parse(file.URI)
if err != nil || !chatAttachmentStore.leased[u.Path] {
continue
}
delete(chatAttachmentStore.leased, u.Path)
}
}
const (
// maxChatAttachments bounds one prompt's browser files. OpenCode receives a
// small number of local files; more is not a useful chat message.
@@ -24,7 +49,7 @@ const (
)
// prepareChatAttachments writes browser attachment bytes into a private
// temporary directory and returns the file references OpenCode accepts plus the
// durable directory and returns the file references OpenCode accepts plus the
// display parts the user turn carries. A browser File reference is not readable
// by the agent service, so the bytes must cross the websocket.
func prepareChatAttachments(sessionID string, attachments []protocol.ChatAttachment) ([]opencode.PromptFile, []protocol.ChatPart, error) {
@@ -34,21 +59,74 @@ func prepareChatAttachments(sessionID string, attachments []protocol.ChatAttachm
if len(attachments) > maxChatAttachments {
return nil, nil, fmt.Errorf("at most %d attachments are allowed", maxChatAttachments)
}
dir := filepath.Join(os.TempDir(), "pipelines-chat-attachments", safeChatAttachmentSegment(sessionID))
if err := os.MkdirAll(dir, 0o700); err != nil {
dir := os.Getenv("PIPELINES_CHAT_ATTACHMENT_DIR")
if dir == "" {
home, err := os.UserHomeDir()
if err != nil {
return nil, nil, err
}
dir = filepath.Join(home, ".pipelines-chat-attachments")
}
if !filepath.IsAbs(dir) {
return nil, nil, fmt.Errorf("chat attachment storage must be an absolute path")
}
dir = filepath.Clean(dir)
chatAttachmentStore.Lock()
defer chatAttachmentStore.Unlock()
if err := os.Mkdir(dir, 0o700); err != nil && !os.IsExist(err) {
return nil, nil, fmt.Errorf("prepare attachment directory: %w", err)
}
info, err := os.Lstat(dir)
if err != nil || !info.IsDir() || info.Mode().Perm()&0o077 != 0 {
return nil, nil, fmt.Errorf("attachment storage must be a private directory, not a symlink")
}
root, err := os.OpenRoot(dir)
if err != nil {
return nil, nil, err
}
defer root.Close()
actual, err := root.Stat(".")
if err != nil || !os.SameFile(info, actual) {
return nil, nil, fmt.Errorf("chat attachment storage changed while opening")
}
entries, err := fs.ReadDir(root.FS(), ".")
if err != nil {
return nil, nil, err
}
var stored int64
var storedFiles int
for _, entry := range entries {
info, err := entry.Info()
if err != nil {
return nil, nil, err
}
if !info.Mode().IsRegular() {
continue
}
if !chatAttachmentStore.leased[filepath.Join(dir, entry.Name())] && time.Since(info.ModTime()) > chatAttachmentRetention {
if err := root.Remove(entry.Name()); err == nil {
continue
}
}
stored += info.Size()
storedFiles++
}
var files []opencode.PromptFile
var parts []protocol.ChatPart
var created []string
cleanup := func() {
for _, path := range created {
_ = os.Remove(path)
_ = root.Remove(filepath.Base(path))
delete(chatAttachmentStore.leased, path)
}
}
total := 0
for index, attachment := range attachments {
data := strings.TrimSpace(attachment.Data)
if len(data) > base64.StdEncoding.EncodedLen(maxChatAttachmentBytes) {
cleanup()
return nil, nil, fmt.Errorf("attachments exceed the 1 MiB limit")
}
if data == "" {
cleanup()
return nil, nil, fmt.Errorf("attachment %q has no data", attachment.Name)
@@ -63,27 +141,41 @@ func prepareChatAttachments(sessionID string, attachments []protocol.ChatAttachm
cleanup()
return nil, nil, fmt.Errorf("attachments exceed the %s limit", "1 MiB")
}
if stored+int64(total) > chatAttachmentQuota {
cleanup()
return nil, nil, fmt.Errorf("chat attachment storage quota exceeded (64 MiB); retry after retention cleanup")
}
if storedFiles+index+1 > chatAttachmentFileQuota {
cleanup()
return nil, nil, fmt.Errorf("chat attachment storage quota exceeded (512 files)")
}
name := safeChatAttachmentName(attachment.Name)
mime := strings.TrimSpace(attachment.Mime)
if mime == "" {
mime = "application/octet-stream"
}
path := filepath.Join(dir, fmt.Sprintf("%d-%02d-%s", time.Now().UnixNano(), index, name))
file, err := os.OpenFile(path, os.O_WRONLY|os.O_CREATE|os.O_EXCL, 0o600)
file, err := root.OpenFile(filepath.Base(path), os.O_WRONLY|os.O_CREATE|os.O_EXCL, 0o600)
if err != nil {
cleanup()
return nil, nil, fmt.Errorf("write attachment %q: %w", name, err)
}
created = append(created, path)
chatAttachmentStore.leased[path] = true
if _, err := file.Write(decoded); err != nil {
_ = file.Close()
cleanup()
return nil, nil, fmt.Errorf("write attachment %q: %w", name, err)
}
if err := file.Sync(); err != nil {
_ = file.Close()
cleanup()
return nil, nil, fmt.Errorf("sync attachment %q: %w", name, err)
}
if err := file.Close(); err != nil {
cleanup()
return nil, nil, fmt.Errorf("close attachment %q: %w", name, err)
}
created = append(created, path)
uri := (&url.URL{Scheme: "file", Path: path}).String()
files = append(files, opencode.PromptFile{URI: uri, Name: name})
parts = append(parts, protocol.ChatPart{Type: protocol.ChatPartFile, Name: name, Mime: mime, URI: uri})
@@ -91,25 +183,6 @@ func prepareChatAttachments(sessionID string, attachments []protocol.ChatAttachm
return files, parts, nil
}
func safeChatAttachmentSegment(value string) string {
value = strings.TrimSpace(value)
value = strings.Map(func(r rune) rune {
switch {
case r >= 'a' && r <= 'z', r >= 'A' && r <= 'Z', r >= '0' && r <= '9', r == '_', r == '-':
return r
default:
return '_'
}
}, value)
if value == "" {
return "session"
}
if len(value) > 80 {
value = value[:80]
}
return value
}
func safeChatAttachmentName(value string) string {
name := filepath.Base(strings.TrimSpace(value))
name = strings.Map(func(r rune) rune {
+62
View File
@@ -6,11 +6,13 @@ import (
"path/filepath"
"strings"
"testing"
"time"
"git.a-wan.net/alan/pipelines/internal/protocol"
)
func TestPrepareChatAttachmentsWritesPrivateFileAndPart(t *testing.T) {
t.Setenv("PIPELINES_CHAT_ATTACHMENT_DIR", filepath.Join(t.TempDir(), "uploads"))
data := base64.StdEncoding.EncodeToString([]byte("hello attachment"))
files, parts, err := prepareChatAttachments("ses_test", []protocol.ChatAttachment{{
Name: "../../note.txt", Mime: "text/plain", Data: data,
@@ -44,7 +46,67 @@ func TestPrepareChatAttachmentsWritesPrivateFileAndPart(t *testing.T) {
}
}
func TestChatAttachmentStorageRetentionLeaseAndSymlink(t *testing.T) {
dir := filepath.Join(t.TempDir(), "uploads")
t.Setenv("PIPELINES_CHAT_ATTACHMENT_DIR", dir)
attachment := []protocol.ChatAttachment{{Name: "x", Data: base64.StdEncoding.EncodeToString([]byte("private"))}}
files, _, err := prepareChatAttachments("ses_test", attachment)
if err != nil {
t.Fatal(err)
}
path := strings.TrimPrefix(files[0].URI, "file://")
old := time.Now().Add(-chatAttachmentRetention - time.Hour)
if err := os.Chtimes(path, old, old); err != nil {
t.Fatal(err)
}
more, _, err := prepareChatAttachments("ses_test", attachment)
if err != nil {
t.Fatal(err)
}
defer releaseChatAttachments(more)
if _, err := os.Stat(path); err != nil {
t.Fatal("queued/active file collected", err)
}
releaseChatAttachments(files)
last, _, err := prepareChatAttachments("ses_test", attachment)
if err != nil {
t.Fatal(err)
}
defer releaseChatAttachments(last)
if _, err := os.Stat(path); !os.IsNotExist(err) {
t.Fatal("expired unleased file not collected", err)
}
link := filepath.Join(t.TempDir(), "link")
if err := os.Symlink(dir, link); err != nil {
t.Fatal(err)
}
t.Setenv("PIPELINES_CHAT_ATTACHMENT_DIR", link)
if _, _, err := prepareChatAttachments("ses_test", attachment); err == nil {
t.Fatal("symlink storage accepted")
}
}
func TestChatAttachmentStorageQuota(t *testing.T) {
dir := t.TempDir()
if err := os.Chmod(dir, 0o700); err != nil {
t.Fatal(err)
}
t.Setenv("PIPELINES_CHAT_ATTACHMENT_DIR", dir)
file, err := os.Create(filepath.Join(dir, "quota"))
if err != nil {
t.Fatal(err)
}
if err := file.Truncate(chatAttachmentQuota); err != nil {
t.Fatal(err)
}
file.Close()
if _, _, err := prepareChatAttachments("ses_test", []protocol.ChatAttachment{{Name: "x", Data: "eA=="}}); err == nil || !strings.Contains(err.Error(), "quota") {
t.Fatalf("quota error = %v", err)
}
}
func TestPrepareChatAttachmentsRejectsInvalidAndOversized(t *testing.T) {
t.Setenv("PIPELINES_CHAT_ATTACHMENT_DIR", filepath.Join(t.TempDir(), "uploads"))
if _, _, err := prepareChatAttachments("ses_test", []protocol.ChatAttachment{{Name: "x", Data: "not base64"}}); err == nil {
t.Fatal("invalid base64 accepted")
}
+60
View File
@@ -0,0 +1,60 @@
package server
import (
"context"
"errors"
"os"
"path/filepath"
"testing"
"git.a-wan.net/alan/pipelines/internal/opencode"
"git.a-wan.net/alan/pipelines/internal/protocol"
)
func TestChatRejectsUnsupportedAttachmentsBeforeStorageOrQueue(t *testing.T) {
broker := newChannelChat() // deliberately has no file capability
manager := &chatManager{opencode: broker, cache: map[string]string{}}
dir := filepath.Join(t.TempDir(), "uploads")
t.Setenv("PIPELINES_CHAT_ATTACHMENT_DIR", dir)
var reply protocol.Message
handleChatPrompt(context.Background(), protocol.Message{ID: "p", ChatPrompt: &protocol.ChatPromptRequest{SessionID: "ses_1", Text: "hello", Attachments: []protocol.ChatAttachment{{Name: "x", Data: "eA=="}}}}, manager, newChatHub(), nil, "client", func(msg protocol.Message) error { reply = msg; return nil })
if reply.Type != protocol.Error || reply.Error != errChatAttachmentsUnsupported.Error() {
t.Fatalf("reply = %+v", reply)
}
if broker.promptCount() != 0 {
t.Fatal("unsupported prompt reached harness")
}
if _, err := os.Stat(dir); !os.IsNotExist(err) {
t.Fatal("unsupported upload wrote storage", err)
}
if err := manager.PromptWithMessageIDFiles(context.Background(), "ses_1", "hello", "", []opencode.PromptFile{{URI: "file:///x"}}); !errors.Is(err, errChatAttachmentsUnsupported) {
t.Fatalf("silent fallback: %v", err)
}
}
type failingChatInstructions struct{ *fakeChat }
func (f failingChatInstructions) SetInstruction(context.Context, string, string, any) error {
return errors.New("instruction service unavailable")
}
func TestChatCreateReportsInstructionCapabilities(t *testing.T) {
for _, status := range []string{"applied", "failed", "unsupported"} {
t.Run(status, func(t *testing.T) {
fake := scriptedChat()
fake.created = protocol.ChatSession{ID: "ses_new"}
var broker chatBroker = fake
if status == "failed" {
broker = failingChatInstructions{fake}
}
if status == "unsupported" {
broker = newChannelChat()
}
var reply protocol.Message
handleChatCreate(context.Background(), protocol.Message{ID: "create", ChatCreate: &protocol.ChatCreateRequest{Directory: t.TempDir(), Instructions: "be careful"}}, broker, newOperatorStore(t), func(msg protocol.Message) error { reply = msg; return nil })
if reply.ChatSession == nil || reply.ChatSession.InstructionStatus != status {
t.Fatalf("reply = %+v", reply)
}
})
}
}
+20 -4
View File
@@ -3,11 +3,11 @@ package server
import (
"context"
"errors"
"git.a-wan.net/alan/pipelines/internal/opencode"
"sort"
"sync"
"git.a-wan.net/alan/pipelines/internal/codex"
"git.a-wan.net/alan/pipelines/internal/opencode"
"git.a-wan.net/alan/pipelines/internal/protocol"
)
@@ -138,8 +138,7 @@ func (m *chatManager) PromptWithMessageID(ctx context.Context, sessionID, text,
}
// PromptWithMessageIDFiles forwards a prompt with the private local files the
// server wrote for browser attachments. A backend without file support falls
// back to the text-only path rather than dropping the prompt.
// server wrote for browser attachments. Never silently discard files.
func (m *chatManager) PromptWithMessageIDFiles(ctx context.Context, sessionID, text, messageID string, files []opencode.PromptFile) error {
adapter, err := m.brokerFor(ctx, sessionID)
if err != nil {
@@ -148,9 +147,26 @@ func (m *chatManager) PromptWithMessageIDFiles(ctx context.Context, sessionID, t
if withFiles, ok := adapter.(chatPromptWithFiles); ok {
return withFiles.PromptWithMessageIDFiles(ctx, sessionID, text, messageID, files)
}
if len(files) > 0 {
return errChatAttachmentsUnsupported
}
return m.PromptWithMessageID(ctx, sessionID, text, messageID)
}
var errChatAttachmentsUnsupported = errors.New("this chat harness does not support attachments")
var errChatInstructionsUnsupported = errors.New("this chat harness does not support session instructions")
func (m *chatManager) CheckAttachments(ctx context.Context, sessionID string) error {
adapter, err := m.brokerFor(ctx, sessionID)
if err != nil {
return err
}
if _, ok := adapter.(chatPromptWithFiles); !ok {
return errChatAttachmentsUnsupported
}
return nil
}
// SetInstruction forwards one preloaded behavior instruction to the session's
// harness when it supports durable instruction entries.
func (m *chatManager) SetInstruction(ctx context.Context, sessionID, key string, value any) error {
@@ -160,7 +176,7 @@ func (m *chatManager) SetInstruction(ctx context.Context, sessionID, key string,
}
instr, ok := adapter.(chatInstructioner)
if !ok {
return errors.New("this chat harness does not support session instructions")
return errChatInstructionsUnsupported
}
return instr.SetInstruction(ctx, sessionID, key, value)
}
+76 -1
View File
@@ -6,6 +6,7 @@ import (
"net/http/httptest"
"strings"
"sync"
"sync/atomic"
"testing"
"time"
@@ -16,6 +17,71 @@ import (
"git.a-wan.net/alan/pipelines/internal/protocol"
)
type reconnectingChat struct {
*channelChat
streams atomic.Int32
disconnect chan struct{}
reconnected chan struct{}
}
func (c *reconnectingChat) Stream(ctx context.Context, id string, ready func(), emit func(protocol.ChatEventFrame) error) error {
n := c.streams.Add(1)
if ready != nil {
ready()
}
if n == 1 {
select {
case <-c.disconnect:
return errors.New("test transport disconnect")
case <-ctx.Done():
return ctx.Err()
}
}
if n == 2 {
close(c.reconnected)
}
for {
select {
case <-ctx.Done():
return ctx.Err()
case frame := <-c.frames:
if err := emit(frame); err != nil {
return err
}
}
}
}
func TestChatStreamDisconnectPreservesQueuedTurn(t *testing.T) {
broker := &reconnectingChat{channelChat: newChannelChat(), disconnect: make(chan struct{}), reconnected: make(chan struct{})}
ctx, _, _, conns := dialChatServer(t, Options{Chat: broker}, 1)
conn := conns[0]
for _, text := range []string{"one", "two"} {
writeChat(t, ctx, conn, protocol.Message{Type: protocol.ChatPrompt, ID: text, ChatPrompt: &protocol.ChatPromptRequest{SessionID: "ses_1", Text: text}})
ack := readUntil(t, ctx, conn, protocol.ChatPrompt)
if ack.ChatQueued != (text == "two") {
t.Fatalf("ack = %+v", ack)
}
}
close(broker.disconnect)
select {
case <-broker.reconnected:
case <-ctx.Done():
t.Fatal("stream did not reconnect")
}
if broker.promptCount() != 1 {
t.Fatal("transport loss advanced queue")
}
broker.push(protocol.ChatEventFrame{SessionID: "ses_1", Kind: protocol.ChatEventStep, Step: &protocol.ChatStep{Status: protocol.ChatStepEnded}})
deadline := time.Now().Add(time.Second)
for broker.promptCount() < 2 && time.Now().Before(deadline) {
time.Sleep(5 * time.Millisecond)
}
if broker.promptCount() != 2 {
t.Fatal("authoritative completion did not advance queue")
}
}
// channelChat is a broker whose stream is driven frame-by-frame from a channel,
// so a test can hold a turn in flight and prove prompt serialisation, fan-out and
// exactly-once approval behaviour without a real OpenCode service.
@@ -286,7 +352,16 @@ func TestChatPromptSerialisedPerSession(t *testing.T) {
t.Fatalf("prompts forwarded = %d, want 1 while the first turn is in flight", broker.promptCount())
}
// End the first turn; the queued prompt is then posted.
// Provider/tool steps are not foreground-drain completion. Neither step
// boundary may start the queued prompt, including after a subscriber attach.
for i := 0; i < 2; i++ {
broker.push(protocol.ChatEventFrame{SessionID: "ses_1", MessageID: "msg_1", Kind: protocol.ChatEventStep, Step: &protocol.ChatStep{Status: protocol.ChatStepEnded, Index: i + 1, Intermediate: true}})
time.Sleep(30 * time.Millisecond)
if broker.promptCount() != 1 {
t.Fatalf("intermediate step advanced queue: %v", broker.promptTexts())
}
}
// End the first turn authoritatively; the queued prompt is then posted.
broker.push(protocol.ChatEventFrame{SessionID: "ses_1", MessageID: "msg_1", Kind: protocol.ChatEventStep, Step: &protocol.ChatStep{Status: protocol.ChatStepEnded, Index: 1}})
deadline := time.Now().Add(3 * time.Second)
for broker.promptCount() < 2 {
+24 -6
View File
@@ -358,8 +358,8 @@ func (h *chatHub) broadcastUser(frame protocol.ChatEventFrame, exceptClientID st
// emit folds one broker frame into the retained state and fans it out. While the
// first turn's ack is still pending the frames are buffered and flushed in order
// once it is written. A step-ended frame finishes the turn, letting the queue
// advance.
// once it is written. Only an authoritative (non-intermediate) ended frame
// finishes the turn and lets the queue advance.
func (h *chatHub) emit(frame protocol.ChatEventFrame) {
h.emitRoom(nil, frame)
}
@@ -384,7 +384,7 @@ func (h *chatHub) emitRoom(source *chatRoom, frame protocol.ChatEventFrame) {
}
}
}
if room != nil && frame.Kind == protocol.ChatEventStep && frame.Step != nil && frame.Step.Status == protocol.ChatStepEnded {
if room != nil && frame.Kind == protocol.ChatEventStep && frame.Step != nil && frame.Step.Status == protocol.ChatStepEnded && !frame.Step.Intermediate {
h.finishTurnLocked(room)
}
// Shell replay and live lifecycle delivery must enqueue in the same order
@@ -630,6 +630,13 @@ func (h *chatHub) pushPermission(sessionID string, frame protocol.ChatPermission
// posts each to the broker and waits for the turn to end, so two prompts of one
// session can never interleave.
func (h *chatHub) runRoom(room *chatRoom) {
defer func() {
h.mu.Lock()
defer h.mu.Unlock()
for _, p := range room.queue {
releaseChatAttachments(p.files)
}
}()
for {
p, done, ok := h.nextPrompt(room)
if !ok {
@@ -666,6 +673,7 @@ func (h *chatHub) nextPrompt(room *chatRoom) (*chatQueuedPrompt, chan struct{},
// runTurn subscribes to the broker stream when needed, posts one prompt, writes
// its ack and waits for the turn to end.
func (h *chatHub) runTurn(room *chatRoom, p *chatQueuedPrompt, done chan struct{}) {
defer releaseChatAttachments(p.files)
broker := h.roomBroker(room)
first := false
h.mu.Lock()
@@ -785,17 +793,27 @@ func (h *chatHub) startStream(room *chatRoom, broker chatBroker) error {
}
// drainStream reports the terminal error of a room's subscription and clears the
// streaming flag so a later turn re-subscribes; it also releases a turn still
// waiting on a stream that ended.
// streaming flag and reconnects. Transport loss never releases a turn slot.
func (h *chatHub) drainStream(room *chatRoom, result <-chan error) {
err := <-result
h.mu.Lock()
room.streaming = false
h.finishTurnLocked(room)
h.mu.Unlock()
if err != nil && room.ctx.Err() == nil {
log.Printf("chat stream session=%s: %v", room.sessionID, err)
}
// Losing the transport does not end the harness turn. Re-subscribe without
// releasing its queue slot; the harness must report authoritative completion.
for room.ctx.Err() == nil {
select {
case <-room.ctx.Done():
return
case <-time.After(time.Second):
}
if err := h.ensureObserving(room, h.roomBroker(room)); err == nil {
return
}
}
}
// pollRoom polls the broker's pending-approval list for the room on a fixed
+18 -11
View File
@@ -3,7 +3,6 @@ package server
import (
"context"
"errors"
"log"
"os"
"path/filepath"
"strings"
@@ -180,16 +179,6 @@ func handleChatCreate(ctx context.Context, request protocol.Message, broker chat
// Reserve the activation before calling the external service. If the
// process dies after the call but before the success checkpoint, a retry
// must report the ambiguous operation rather than append the skill twice.
// Preload the web chat's behavior brief before the first turn. This is
// best effort: an older OpenCode build or another harness without the
// experimental instruction surface still gets a usable chat.
if instructions != "" {
if instr, ok := broker.(chatInstructioner); ok {
if err := instr.SetInstruction(ctx, session.ID, "pipelines.behavior", instructions); err != nil {
log.Printf("chat %s preload instructions: %v", session.ID, err)
}
}
}
if err := registry.AdvanceChatCreate(ctx, operationID, "created", "activating"); err != nil {
chatCreateConflictError(request, err, write)
return
@@ -239,6 +228,24 @@ func handleChatCreate(ctx context.Context, request protocol.Message, broker chat
session.Directory = resolved
session.Tag = protocol.ChatSessionTagOperator
session.Backend = backend
// Setting a named instruction replaces it idempotently. Repeat on a retried
// create so the reply reports an observed result, never a guessed success.
session.InstructionStatus = "not_requested"
if instructions != "" {
session.InstructionStatus = "unsupported"
if instr, ok := broker.(chatInstructioner); ok {
err := instr.SetInstruction(ctx, session.ID, "pipelines.behavior", instructions)
switch {
case err == nil:
session.InstructionStatus = "applied"
case errors.Is(err, errChatInstructionsUnsupported):
session.InstructionError = err.Error()
default:
session.InstructionStatus = "failed"
session.InstructionError = err.Error()
}
}
}
response := protocol.Message{Type: protocol.ChatCreate, ID: request.ID, ChatSession: &session}
// The same registry row is echoed on the list field so a client can splice it
// into chat_sessions without a re-list. An ephemeral create has no listed row
+21
View File
@@ -45,11 +45,31 @@ 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
}
if len(prompt.Attachments) > 0 {
var err error
if checker, ok := broker.(interface {
CheckAttachments(context.Context, string) error
}); ok {
err = checker.CheckAttachments(ctx, prompt.SessionID)
} else if _, ok := broker.(chatPromptWithFiles); !ok {
err = errChatAttachmentsUnsupported
}
if err != nil {
_ = write(protocol.Message{Type: protocol.Error, ID: request.ID, Error: err.Error()})
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
}
accepted := false
defer func() {
if !accepted {
releaseChatAttachments(attachmentFiles)
}
}()
sessionID := prompt.SessionID
sub := hub.attach(sessionID, clientID, sanitizeChatClientLabel(prompt.Client), broker, write)
if sub == nil {
@@ -85,6 +105,7 @@ func handleChatPrompt(ctx context.Context, request protocol.Message, broker chat
_ = write(protocol.Message{Type: protocol.Error, ID: request.ID, Error: "session " + sessionID + " already has the maximum number of prompts queued"})
return
}
accepted = true
// Publish the user turn to every other client; the sender already has it.
hub.broadcastUser(userFrame, clientID)
if queued {
+1
View File
@@ -473,6 +473,7 @@ func TestChatModelsAndSwitch(t *testing.T) {
}
func TestChatPromptWritesAttachmentAndForwardsLocalFile(t *testing.T) {
t.Setenv("PIPELINES_CHAT_ATTACHMENT_DIR", filepath.Join(t.TempDir(), "uploads"))
broker := scriptedChat()
ctx, _, conn := dialChat(t, broker)
data := base64.StdEncoding.EncodeToString([]byte("hello attachment"))