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
+7
View File
@@ -64,6 +64,11 @@ export const Protocol = Object.freeze({
CHAT_STAR: "chat_star",
CHAT_RENAME: "chat_rename",
CHAT_BACKENDS: "chat_backends",
// Model selection for one chat. `chat_models` uses the usual `chat_query`
// session body and replies in `chat_models`; `chat_model` carries
// `{session_id,provider,model}` in `chat_model` and echoes it.
CHAT_MODELS: "chat_models",
CHAT_MODEL: "chat_model",
// The shared live stream (C5). `chat_subscribe` attaches this connection to one
// session's stream (body: the session id); the server then pushes `chat_event`
// frames without a matching request id, so every client watching a session sees
@@ -119,6 +124,8 @@ export const ChatKeys = Object.freeze({
ANSWER: "chat_answer",
RENAME: "chat_rename",
BACKENDS: "chat_backends",
MODELS: "chat_models",
MODEL: "chat_model",
// The composer's payloads (C8/C9). `chat_interrupt` deliberately has no key of
// its own: its body is the same `chat_query` the other session-scoped commands
// carry (internal/protocol/chat.go ChatQuery, Chat.kt interruptRequest), so a
+28 -1
View File
@@ -262,6 +262,32 @@ export class PipelinesRepository {
return (await this.#client.call({ type: Protocol.CHAT_BACKENDS }))[ChatKeys.BACKENDS] ?? ["opencode"];
}
/** The models the session's harness offers for switching. */
async chatModels(sessionId) {
if (!String(sessionId ?? "").trim()) {
throw new ProtocolError("Select a chat session first.");
}
const response = await this.#client.call({
type: Protocol.CHAT_MODELS,
[ChatKeys.QUERY]: { session_id: sessionId },
});
return response[ChatKeys.MODELS] ?? [];
}
/** Switch one session's model for subsequent turns. */
async switchChatModel(sessionId, provider, model) {
const session = String(sessionId ?? "").trim();
const providerId = String(provider ?? "").trim();
const modelId = String(model ?? "").trim();
if (session === "") throw new ProtocolError("Select a chat session first.");
if (providerId === "" || modelId === "") throw new ProtocolError("Choose a model first.");
const response = await this.#client.call({
type: Protocol.CHAT_MODEL,
[ChatKeys.MODEL]: { session_id: session, provider: providerId, model: modelId },
});
return response[ChatKeys.MODEL] ?? { session_id: session, provider: providerId, model: modelId };
}
/**
* Loads a bounded chronological history page. `cursor` is the opaque
* continuation the server returned as `next_cursor`; it is passed back
@@ -497,7 +523,7 @@ export class PipelinesRepository {
* `chat_sessions` for the operator. Automated checks that drive this real page
* set it, so a run can never leave a stray "New chat" in the operator's list.
*/
async chatCreate(title, directory, operationId = newChatOperationId(), { backend = "opencode", ephemeral = false } = {}) {
async chatCreate(title, directory, operationId = newChatOperationId(), { backend = "opencode", ephemeral = false, instructions = "" } = {}) {
const response = await this.#client.call({
type: Protocol.CHAT_CREATE,
[ChatKeys.CREATE]: {
@@ -506,6 +532,7 @@ export class PipelinesRepository {
operation_id: operationId,
...(backend && backend !== "opencode" ? { backend } : {}),
...(ephemeral ? { ephemeral: true } : {}),
...(String(instructions ?? "").trim() !== "" ? { instructions: String(instructions).trim() } : {}),
},
});
const session = response[ChatKeys.SESSION];