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
+43
View File
@@ -105,6 +105,49 @@ func handleChatPrompt(ctx context.Context, request protocol.Message, broker chat
}
}
// handleChatModels lists the models a session's harness can switch to.
func handleChatModels(ctx context.Context, request protocol.Message, broker chatBroker, write func(protocol.Message) error) {
query := request.ChatQuery
if query == nil || !validChatSessionID(query.SessionID) {
_ = write(protocol.Message{Type: protocol.Error, ID: request.ID, Error: "a valid OpenCode session id is required"})
return
}
modeler, ok := broker.(chatModeler)
if !ok {
_ = write(protocol.Message{Type: protocol.Error, ID: request.ID, Error: "this chat harness does not support changing models"})
return
}
models, err := modeler.Models(ctx, query.SessionID)
if err != nil {
_ = write(protocol.Message{Type: protocol.Error, ID: request.ID, Error: err.Error()})
return
}
_ = write(protocol.Message{Type: protocol.ChatModels, ID: request.ID, ChatModels: models})
}
// handleChatModelSwitch changes one session's model for subsequent turns.
func handleChatModelSwitch(ctx context.Context, request protocol.Message, broker chatBroker, write func(protocol.Message) error) {
body := request.ModelRequest
if body == nil || !validChatSessionID(body.SessionID) {
_ = write(protocol.Message{Type: protocol.Error, ID: request.ID, Error: "a valid OpenCode session id is required"})
return
}
if strings.TrimSpace(body.Provider) == "" || strings.TrimSpace(body.Model) == "" {
_ = write(protocol.Message{Type: protocol.Error, ID: request.ID, Error: "a model and provider are required"})
return
}
modeler, ok := broker.(chatModeler)
if !ok {
_ = write(protocol.Message{Type: protocol.Error, ID: request.ID, Error: "this chat harness does not support changing models"})
return
}
if err := modeler.SwitchModel(ctx, body.SessionID, body.Provider, body.Model); err != nil {
_ = write(protocol.Message{Type: protocol.Error, ID: request.ID, Error: err.Error()})
return
}
_ = write(protocol.Message{Type: protocol.ChatModelSelect, ID: request.ID, ModelRequest: body})
}
// handleChatSubscribe attaches one connection to a session's shared stream so it
// receives every frame any client's turn produces, its own or another device's.
// It replays the session's still-pending approval requests, if the room holds