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
+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];