fix(codex): accept prepared chat attachments with model overrides
This commit is contained in:
@@ -177,7 +177,7 @@ func prepareChatAttachments(sessionID string, attachments []protocol.ChatAttachm
|
||||
return nil, nil, fmt.Errorf("close attachment %q: %w", name, err)
|
||||
}
|
||||
uri := (&url.URL{Scheme: "file", Path: path}).String()
|
||||
files = append(files, opencode.PromptFile{URI: uri, Name: name})
|
||||
files = append(files, opencode.NewPreparedPromptFile(uri, name))
|
||||
parts = append(parts, protocol.ChatPart{Type: protocol.ChatPartFile, Name: name, Mime: mime, URI: uri})
|
||||
}
|
||||
return files, parts, nil
|
||||
|
||||
@@ -5,6 +5,8 @@ import (
|
||||
"path/filepath"
|
||||
"testing"
|
||||
|
||||
"git.a-wan.net/alan/pipelines/internal/codex"
|
||||
"git.a-wan.net/alan/pipelines/internal/opencode"
|
||||
"git.a-wan.net/alan/pipelines/internal/pipeline"
|
||||
"git.a-wan.net/alan/pipelines/internal/protocol"
|
||||
)
|
||||
@@ -13,6 +15,19 @@ type selectedModelChat struct {
|
||||
*fakeChat
|
||||
model, nativeID string
|
||||
overrides int
|
||||
files []opencode.PromptFile
|
||||
}
|
||||
|
||||
func TestCodexManagerAttachmentsRejectUnpreparedURI(t *testing.T) {
|
||||
// Invalid input must fail before any JSON-RPC/socket access, including when
|
||||
// no model override is saved.
|
||||
manager := &chatManager{codex: &codex.Chat{}, cache: map[string]string{"thread": "codex"}}
|
||||
if err := manager.CheckAttachments(context.Background(), "thread"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := manager.PromptWithMessageIDFiles(context.Background(), "thread", "inspect", "msg", []opencode.PromptFile{{URI: "file:///etc/passwd"}}); err == nil {
|
||||
t.Fatal("unprepared URI accepted")
|
||||
}
|
||||
}
|
||||
|
||||
func (c *selectedModelChat) PromptWithMessageIDModel(_ context.Context, _, _, nativeID, model string) error {
|
||||
@@ -21,7 +36,13 @@ func (c *selectedModelChat) PromptWithMessageIDModel(_ context.Context, _, _, na
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c *selectedModelChat) PromptWithMessageIDModelFiles(ctx context.Context, id, text, nativeID, model string, files []opencode.PromptFile) error {
|
||||
c.files = append([]opencode.PromptFile(nil), files...)
|
||||
return c.PromptWithMessageIDModel(ctx, id, text, nativeID, model)
|
||||
}
|
||||
|
||||
func TestCodexModelSelectionSurvivesServerRestart(t *testing.T) {
|
||||
t.Setenv("PIPELINES_CHAT_ATTACHMENT_DIR", filepath.Join(t.TempDir(), "attachments"))
|
||||
ctx := context.Background()
|
||||
path := filepath.Join(t.TempDir(), "chat.db")
|
||||
store, err := pipeline.Open(path)
|
||||
@@ -85,11 +106,25 @@ func TestCodexModelSelectionSurvivesServerRestart(t *testing.T) {
|
||||
if fake.overrides != 3 || fake.nativeID != "msg_empty" {
|
||||
t.Fatal("empty attachments bypassed saved choice")
|
||||
}
|
||||
files, _, err := prepareChatAttachments(id, []protocol.ChatAttachment{{Name: "report.txt", Data: "aGVsbG8="}, {Name: "image.png", Mime: "image/png", Data: "aW1hZ2UgYnl0ZXM="}})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer releaseChatAttachments(files)
|
||||
if err := manager.PromptWithMessageIDFiles(ctx, id, "with files", "msg_files", files); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if fake.overrides != 4 || fake.model != "fixture-model" || fake.nativeID != "msg_files" || len(fake.files) != 2 || !fake.files[0].ServerPrepared() {
|
||||
t.Fatalf("attachment model override lost: %+v", fake)
|
||||
}
|
||||
fake.sessionRecord.ModelProvider = "different-provider"
|
||||
if err := manager.Prompt(ctx, id, "must not send"); err == nil {
|
||||
t.Fatal("saved model sent to a different provider")
|
||||
}
|
||||
if fake.overrides != 3 {
|
||||
if err := manager.PromptWithMessageIDFiles(ctx, id, "must not send files", "msg_bad", files); err == nil {
|
||||
t.Fatal("saved model with files sent to a different provider")
|
||||
}
|
||||
if fake.overrides != 4 {
|
||||
t.Fatal("mismatched provider started a turn")
|
||||
}
|
||||
// A failed durable save must never acknowledge the next-turn selection.
|
||||
|
||||
@@ -128,18 +128,11 @@ func (m *chatManager) PromptWithMessageID(ctx context.Context, sessionID, text,
|
||||
return err
|
||||
}
|
||||
if m.backendFor(ctx, sessionID) == codex.Backend {
|
||||
provider, model, err := m.chatModelSelection(ctx, sessionID)
|
||||
model, err := m.codexPromptModel(ctx, adapter, sessionID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if model != "" {
|
||||
session, found, err := adapter.Session(ctx, sessionID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if !found || session.ModelProvider != provider {
|
||||
return errors.New("saved model provider no longer matches the Codex thread; select a model again")
|
||||
}
|
||||
withModel, ok := adapter.(interface {
|
||||
PromptWithMessageIDModel(context.Context, string, string, string, string) error
|
||||
})
|
||||
@@ -155,6 +148,21 @@ func (m *chatManager) PromptWithMessageID(ctx context.Context, sessionID, text,
|
||||
return adapter.Prompt(ctx, sessionID, text)
|
||||
}
|
||||
|
||||
func (m *chatManager) codexPromptModel(ctx context.Context, adapter chatBroker, sessionID string) (string, error) {
|
||||
provider, model, err := m.chatModelSelection(ctx, sessionID)
|
||||
if err != nil || model == "" {
|
||||
return model, err
|
||||
}
|
||||
session, found, err := adapter.Session(ctx, sessionID)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
if !found || session.ModelProvider != provider {
|
||||
return "", errors.New("saved model provider no longer matches the Codex thread; select a model again")
|
||||
}
|
||||
return model, nil
|
||||
}
|
||||
|
||||
// PromptWithMessageIDFiles forwards a prompt with the private local files the
|
||||
// server wrote for browser attachments. Never silently discard files.
|
||||
func (m *chatManager) PromptWithMessageIDFiles(ctx context.Context, sessionID, text, messageID string, files []opencode.PromptFile) error {
|
||||
@@ -165,6 +173,21 @@ func (m *chatManager) PromptWithMessageIDFiles(ctx context.Context, sessionID, t
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if m.backendFor(ctx, sessionID) == codex.Backend {
|
||||
model, err := m.codexPromptModel(ctx, adapter, sessionID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if model != "" {
|
||||
withModelFiles, ok := adapter.(interface {
|
||||
PromptWithMessageIDModelFiles(context.Context, string, string, string, string, []opencode.PromptFile) error
|
||||
})
|
||||
if !ok {
|
||||
return errors.New("this Codex adapter cannot apply the saved next-turn model with attachments")
|
||||
}
|
||||
return withModelFiles.PromptWithMessageIDModelFiles(ctx, sessionID, text, messageID, model, files)
|
||||
}
|
||||
}
|
||||
if withFiles, ok := adapter.(chatPromptWithFiles); ok {
|
||||
return withFiles.PromptWithMessageIDFiles(ctx, sessionID, text, messageID, files)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user