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];
+30
View File
@@ -367,6 +367,29 @@ test("chatPrompt includes files when present and reads chat_queued", async () =>
assert.equal(queued, true);
});
test("chatModels and switchChatModel use the session-scoped wire shapes", async () => {
const { repo, ws } = connected();
ws.reply = (frame) => {
if (frame.type === "chat_models") {
return { id: frame.id, type: "chat_models", chat_models: [{ id: "m1", provider: "p1", name: "Model One" }] };
}
return { id: frame.id, type: "chat_model", chat_model: frame.chat_model };
};
const models = await repo.chatModels("s1");
assert.deepEqual(lastFrame(ws), {
type: "chat_models",
chat_query: { session_id: "s1" },
});
assert.deepEqual(models, [{ id: "m1", provider: "p1", name: "Model One" }]);
const selected = await repo.switchChatModel("s1", "p1", "m1");
assert.deepEqual(lastFrame(ws), {
type: "chat_model",
chat_model: { session_id: "s1", provider: "p1", model: "m1" },
});
assert.equal(selected.model, "m1");
});
test("chatPrompt sends browser attachment bytes with their name and mime", async () => {
const { repo, ws } = connected();
ws.reply = (frame) => ({ id: frame.id, type: "chat_prompt" });
@@ -441,6 +464,13 @@ test("chatCreate sends title/directory/operation_id and returns chat_session", a
assert.deepEqual(session, created);
});
test("chatCreate sends a preloaded behavior brief when provided", async () => {
const { repo, ws } = connected();
ws.reply = (frame) => ({ id: frame.id, type: "chat_create", chat_session: { id: "s1" } });
await repo.chatCreate("New chat", "/repo", "op-5", { instructions: "be careful" });
assert.equal(lastFrame(ws).chat_create.instructions, "be careful");
});
test("chatCreate throws when the reply carries no chat_session", async () => {
const { repo, ws } = connected();
ws.reply = (frame) => ({ id: frame.id, type: "chat_create" });