chat: add model switching and preloaded instructions

Expose the OpenCode model list and session model switch over the chat protocol,
carry a per-session model provider on chat rows, and apply a preloaded behavior
brief as a durable session instruction entry when a chat is created.
This commit is contained in:
2026-10-20 14:58:20 +01:00
parent f982c0b712
commit 6bfe4c8921
12 changed files with 468 additions and 17 deletions
+61 -1
View File
@@ -48,6 +48,9 @@ type fakeChat struct {
replyRequest protocol.ChatReplyRequest
replyErr error
messageCalls int
models []protocol.ChatModel
modelSwitch string
modelSession string
messageSessionID string
messageLimit int
messageCursor string
@@ -86,6 +89,9 @@ type fakeChat struct {
activateSessionID string
activateSkill string
activateErr error
instructionSession string
instructionKey string
instructionValue any
sessionByIDCalls int
sessionByID string
sessionRecord protocol.ChatSession
@@ -109,6 +115,17 @@ func (f *fakeChat) Prompt(context.Context, string, string) error {
return nil
}
func (f *fakeChat) Models(context.Context, string) ([]protocol.ChatModel, error) {
return append([]protocol.ChatModel(nil), f.models...), nil
}
func (f *fakeChat) SwitchModel(_ context.Context, sessionID, provider, model string) error {
f.mu.Lock()
f.modelSession = sessionID
f.modelSwitch = provider + "/" + model
f.mu.Unlock()
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...)
@@ -280,6 +297,15 @@ func (f *fakeChat) ActivateSkill(_ context.Context, sessionID, skill string) err
return f.activateErr
}
func (f *fakeChat) SetInstruction(_ context.Context, sessionID, key string, value any) error {
f.mu.Lock()
defer f.mu.Unlock()
f.instructionSession = sessionID
f.instructionKey = key
f.instructionValue = value
return nil
}
func (f *fakeChat) Create(_ context.Context, title, directory string) (protocol.ChatSession, error) {
f.mu.Lock()
f.createCalls++
@@ -418,6 +444,34 @@ func readChatTurn(t *testing.T, ctx context.Context, conn *websocket.Conn) []pro
}
}
func TestChatModelsAndSwitch(t *testing.T) {
broker := scriptedChat()
broker.models = []protocol.ChatModel{{ID: "m1", Provider: "p1", Name: "Model One"}}
ctx, _, conn := dialChat(t, broker)
if err := wsjson.Write(ctx, conn, protocol.Message{Type: protocol.ChatModels, ID: "r1", ChatQuery: &protocol.ChatQuery{SessionID: "ses_1"}}); err != nil {
t.Fatal(err)
}
var reply protocol.Message
if err := wsjson.Read(ctx, conn, &reply); err != nil {
t.Fatal(err)
}
if reply.Type != protocol.ChatModels || len(reply.ChatModels) != 1 || reply.ChatModels[0].ID != "m1" {
t.Fatalf("models reply = %+v", reply)
}
if err := wsjson.Write(ctx, conn, protocol.Message{Type: protocol.ChatModelSelect, ID: "r2", ModelRequest: &protocol.ChatModelRequest{SessionID: "ses_1", Provider: "p1", Model: "m1"}}); err != nil {
t.Fatal(err)
}
if err := wsjson.Read(ctx, conn, &reply); err != nil {
t.Fatal(err)
}
if reply.Type != protocol.ChatModelSelect || reply.ModelRequest == nil || reply.ModelRequest.Model != "m1" {
t.Fatalf("switch reply = %+v", reply)
}
if broker.modelSession != "ses_1" || broker.modelSwitch != "p1/m1" {
t.Fatalf("switched %q %q", broker.modelSession, broker.modelSwitch)
}
}
func TestChatPromptWritesAttachmentAndForwardsLocalFile(t *testing.T) {
broker := scriptedChat()
ctx, _, conn := dialChat(t, broker)
@@ -1463,7 +1517,7 @@ func TestChatCreateRecordsOperatorSession(t *testing.T) {
broker.created = protocol.ChatSession{ID: "ses_new", Title: "demo"}
dir := t.TempDir()
ctx, _, conn := dialChatOptions(t, Options{Chat: broker, OperatorRegistry: store})
if err := wsjson.Write(ctx, conn, protocol.Message{Type: protocol.ChatCreate, ID: "c1", ChatCreate: &protocol.ChatCreateRequest{Title: "demo", Directory: dir}}); err != nil {
if err := wsjson.Write(ctx, conn, protocol.Message{Type: protocol.ChatCreate, ID: "c1", ChatCreate: &protocol.ChatCreateRequest{Title: "demo", Directory: dir, Instructions: "be careful"}}); err != nil {
t.Fatal(err)
}
var response protocol.Message
@@ -1488,6 +1542,12 @@ func TestChatCreateRecordsOperatorSession(t *testing.T) {
if activateCalls != 1 || activateSession != "ses_new" || activateSkill != "pipelines-manager" {
t.Fatalf("manager bootstrap = %d/%q/%q", activateCalls, activateSession, activateSkill)
}
broker.mu.Lock()
instructionSession, instructionKey, instructionValue := broker.instructionSession, broker.instructionKey, broker.instructionValue
broker.mu.Unlock()
if instructionSession != "ses_new" || instructionKey != "pipelines.behavior" || instructionValue != "be careful" {
t.Fatalf("preloaded instruction = %q/%q/%v", instructionSession, instructionKey, instructionValue)
}
recorded, err := store.OperatorSessions(ctx)
if err != nil {
t.Fatal(err)