chat: add the Codex backend

Store a chat's harness with its operator session, expose chat_backends and a
backend choice in New chat, and route each session through a manager. The Codex
adapter uses the App Server's Unix WebSocket for threads, turns, history,
questions and interrupt; OpenCode remains the default and Claude is not
advertised until its sidecar and login are wired.
This commit is contained in:
2026-10-20 12:10:39 +01:00
parent 74b74adee7
commit f7a3a49f94
25 changed files with 1287 additions and 32 deletions
+2 -1
View File
@@ -336,6 +336,7 @@ const state = {
chatDirectoriesLoaded: false,
chatDirectoriesError: "",
chatCreate: null,
chatBackends: ["opencode"],
};
// The app-wide run-event watch (R10), owned by the connection rather than any
@@ -1692,7 +1693,6 @@ async function applyRoute(route) {
if (!state.chatLoaded) await loadCurrentView();
if (r.kind === "chat") {
state.chatActiveId = null;
state.chatCreate = null;
renderContent();
return;
}
@@ -1710,6 +1710,7 @@ async function applyRoute(route) {
renderContent();
return;
}
state.chatCreate = null;
state.chatActiveId = r.id;
renderContent();
await openChatSession(r.id, { updateRoute: false });
+8
View File
@@ -936,6 +936,7 @@ export function defaultDirectoryChoice(choices) {
export function newChatCreateState(operationId) {
return {
directory: HOME_DIRECTORY,
backend: "opencode",
operationId: str(operationId),
creating: false,
error: "",
@@ -954,6 +955,13 @@ export function selectChatDirectory(create, choice) {
return { ...create, directory, error: "" };
}
/** Choose the harness for a new chat. */
export function selectChatBackend(create, backend) {
const value = str(backend);
if (value === "") return { ...create };
return { ...create, backend: value, error: "" };
}
/** The create failed: stop sending and keep the server's own reason in the pane. */
export function chatCreateFailed(create, message) {
return { ...create, creating: false, error: str(message) };
+2
View File
@@ -62,6 +62,7 @@ export const Protocol = Object.freeze({
CHAT_REOPEN: "chat_reopen",
CHAT_STAR: "chat_star",
CHAT_RENAME: "chat_rename",
CHAT_BACKENDS: "chat_backends",
// 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
@@ -116,6 +117,7 @@ export const ChatKeys = Object.freeze({
EVENT: "chat_event",
ANSWER: "chat_answer",
RENAME: "chat_rename",
BACKENDS: "chat_backends",
// 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
+13 -2
View File
@@ -234,6 +234,11 @@ export class PipelinesRepository {
return (await this.#client.call({ type: Protocol.CHAT_SESSIONS }))[ChatKeys.SESSIONS] ?? [];
}
/** The installed chat harnesses a new session can use. */
async chatBackends() {
return (await this.#client.call({ type: Protocol.CHAT_BACKENDS }))[ChatKeys.BACKENDS] ?? ["opencode"];
}
/**
* Loads a bounded chronological history page. `cursor` is the opaque
* continuation the server returned as `next_cursor`; it is passed back
@@ -469,10 +474,16 @@ 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(), { ephemeral = false } = {}) {
async chatCreate(title, directory, operationId = newChatOperationId(), { backend = "opencode", ephemeral = false } = {}) {
const response = await this.#client.call({
type: Protocol.CHAT_CREATE,
[ChatKeys.CREATE]: { title, directory, operation_id: operationId, ...(ephemeral ? { ephemeral: true } : {}) },
[ChatKeys.CREATE]: {
title,
directory,
operation_id: operationId,
...(backend && backend !== "opencode" ? { backend } : {}),
...(ephemeral ? { ephemeral: true } : {}),
},
});
const session = response[ChatKeys.SESSION];
if (!session) {
+52 -1
View File
@@ -135,6 +135,7 @@ import {
seedChatSeen,
sessionActivity,
sessionActivityBadge,
selectChatBackend as selectChatBackendModel,
selectChatDirectory as selectChatDirectoryModel,
sessionTitle,
sessionWorkLabel,
@@ -396,6 +397,22 @@ function currentChatPosition(el) {
return { index, offset };
}
/** The installed chat harnesses, loaded when the create pane opens. */
async function loadChatBackends() {
try {
const backends = await repo.chatBackends();
if (Array.isArray(backends) && backends.length) state.chatBackends = backends;
} catch {
// Keep the OpenCode default; create reports an unavailable backend clearly.
}
renderContent();
}
function selectChatBackend(value) {
state.chatCreate = selectChatBackendModel(state.chatCreate, value);
renderContent();
}
/** The list of the server's operator sessions, narrowed by the search field. */
async function loadChatSessions(force = false) {
if (state.connection.kind !== "connected") {
@@ -490,6 +507,7 @@ function startNewChat() {
renderContent();
focusDetailHeading();
loadChatDirectories();
loadChatBackends();
}
/**
@@ -543,7 +561,9 @@ async function createNewChat() {
try {
// Not ephemeral: this is the operator's own create path (the UI smoke uses the
// same path for its one real session and closes it afterwards).
const created = await repo.chatCreate("New chat", create.directory, create.operationId);
const created = await repo.chatCreate("New chat", create.directory, create.operationId, {
backend: create.backend,
});
state.chatCreate = null;
state.chatSessions = chatCreateSucceeded(state.chatSessions, created);
await openChatSession(created.id);
@@ -1906,6 +1926,11 @@ function renderChatRow(session, duplicateTitles = new Set()) {
});
const head = h("span", { class: "row-head" });
head.append(h("span", { class: "row-title chat-row-title", text: sessionTitle(session) }));
if (session.backend && session.backend !== "opencode") {
head.append(
h("span", { class: "chat-backend-chip", text: chatBackendLabel(session.backend) }),
);
}
// Only the status word, coloured, with no dot and no operator chip. A closed
// session says so here instead of carrying a second chip.
const statusWord = session.closed ? "Closed" : badge;
@@ -1932,6 +1957,15 @@ function renderChatRow(session, duplicateTitles = new Set()) {
* order, the model note and the footer naming the value that will be sent. Every
* server string is set with textContent.
*/
function chatBackendLabel(backend) {
switch (backend) {
case "codex": return "Codex";
case "claude": return "Claude";
case "opencode": return "OpenCode";
default: return String(backend ?? "");
}
}
function renderChatCreate() {
const create = state.chatCreate;
const pane = h("div", { class: "detail chat-detail chat-create" });
@@ -1976,6 +2010,23 @@ function renderChatCreate() {
} else {
pane.append(h("div", { class: "chat-create-prompt muted", text: CHAT_CREATE_PROMPT }));
if ((state.chatBackends ?? []).length > 1) {
const harness = h("div", { class: "chat-create-backends", role: "group", "aria-label": "Chat harness" });
for (const backend of state.chatBackends) {
const active = (create.backend ?? "opencode") === backend;
harness.append(
h("button", {
class: `ghost chat-create-backend${active ? " selected" : ""}`,
type: "button",
"aria-pressed": active ? "true" : "false",
text: chatBackendLabel(backend),
onclick: () => selectChatBackend(backend),
}),
);
}
pane.append(harness);
}
const choices = directoryChoices(state.chatDirectories);
const selected =
choices.find((choice) => choice.createDirectory === create.directory) ??