Dodaios is an open-source agent OS for your desktop: it runs your coding agents, browser and apps by voice or chat, on your own machine. Formerly Personal Jarvis.
Python agent-orchestrationai-agentai-coding-assistantclaude-codecoding-agentcomputer-usedesktop-applocal-firstmcppersonal-jarvisvoice-assistantvoice-coding
import { create, type StoreApi, type UseBoundStore } from "zustand";
import { bindComposerDraftTarget, captureComposerDraftTarget, resetBlankComposerDraftTarget, type ComposerDraftTarget } from "@/components/agentchat/composerDrafts";
import { discardHeldFiles, moveHeldFiles } from "@/components/agentchat/useChatAttachments";
import {
AgentChatApiError,
agentChatSocketUrl,
cancelAgentChatTurn,
createAgentChatSession,
deleteAgentChatSession,
fetchAgentChatCatalog,
fetchAgentChatSessions,
fetchAgentConnections,
fetchProviderHealth,
fetchProviderModels,
isApiRunner,
invalidateAgentChatSessions,
patchAgentChatSession,
resolveAgentChatApproval,
saveAgentChatApprovalRule,
answerAgentChatQuestion,
bindVoiceChat,
replyToAgentChatQuestion,
skipAgentChatQuestion,
submitAgentChatQuestion,
touchAgentChatQuestion,
type QuestionSubmitAnswer,
resolveAgentChatPlan,
type PlanDecision,
type QuestionAnswerInput,
sendAgentChatMessage,
saveChatSelection,
type AgentChatCatalog,
type AgentChatEvent,
type AgentChatProvider,
type AgentChatSession,
type AgentChatSurface,
type AgentConnectionRow,
type ApprovalDecision,
type ApprovalRuleChoice,
type ApprovalRuleRequest,
type ChatAttachment,
type CuratedModel,
type PatchSessionInput,
type ProviderHealth,
} from "@/lib/agentChatApi";
import { EMPTY_TIMELINE, markApprovalAnswering, noteApprovalRule, reduceEvent, reduceEvents, settleApproval, type Timeline } from "@/components/agentchat/reduce";
import { markAnswered } from "@/components/society/attention/attentionStore";
import { openQuestionOf } from "@/components/agentchat/questions/questionModel";
import { jitteredDelay, requestConnect } from "../lib/connectBudget";
import { reuseHistoryRows } from "@/lib/historyRequests";
/**
* The agent chat's store — one per SURFACE (`createAgentChatStore`).
*
* The front page's store (`useAgentChatStore`, surface `jarvis`) is Jarvis
* with a keyboard: what is typed there goes to the same assistant the
* microphone reaches, and its sessions are the front page's chats. An
* Agentic IDE store (surface `agent`) holds coding sessions instead. Each
* store asks the backend for its own list and catalog, stamps its surface on
* the sessions it creates, and keeps its own socket and draft.
*
* One active session at a time: its timeline is fed by one WebSocket
* (snapshot, then live events, reconnect from the last seq). The composer's
* choice of provider / model / effort / permission mode lives here as the
* DRAFT: for a fresh chat it is what the next session is created with; with
* a session open, changing a pick patches that session on the backend and
* the same draft mirrors it. The draft survives a reload (localStorage) so
* the composer opens on what you used last.
*
* The provider list is the backend catalog (`/api/agent-chat/catalog`)
* joined with the Agents tab's credential truth (`/api/jarvis-agent/status`):
* a provider is offered when it is connected — a key saved, a subscription
* logged in, or a local server — and shown greyed out with a "connect" hint
* otherwise. The one marked active on the Agents tab is the draft's default.
*/
/**
* Where the composer's draft survives a reload. The front page keeps the key
* it has always had, so picks made before the surface split still load; an
* IDE store gets its own so the two never overwrite each other's picks.
*/
export function draftKey(surface: AgentChatSurface): string {
// v2 for the front page (2026-08-25): the v1 draft was written while the
// IDE's chat and the front page shared one store, so it carried the IDE's
// last folder and a vendor permission mode the Jarvis ladder does not know.
// A new key starts the Jarvis chat clean — the active brain, the home
// folder, the ladder's default — instead of on a coding session's picks.
return surface === "jarvis" ? "jarvis.agentChat.draft.v2" : `jarvis.agentChat.draft.${surface}.v1`;
}
/** First retry delay; each further failure doubles it under full jitter. */
const RECONNECT_MS = 1500;
/** Ceiling on the retry wait — a flat 1.5 s forever is a spin, not a retry. */
const RECONNECT_MAX_MS = 30_000;
/** How many sessions the list holds — the "All chats" archive shows them all. */
const SESSION_LIST_LIMIT = 500;
export interface ComposerDraft {
provider: string;
accountId?: string;
model: string;
effort: string;
permissionMode: string;
/** The permission mode to go back to when Plan is switched off. */
buildMode: string;
cwd: string;
}
export interface ProviderOption extends AgentChatProvider {
/** Usable right now: credential present (or keyless) and, for a CLI, installed. */
connected: boolean;
/** The sub-agent marked active on the Agents tab. */
active: boolean;
/**
* Draw this row with a CODING AGENT's mark rather than a provider-family
* logo, naming the workspace entry whose mark it is ("opencode", "kimi").
*
* The catalog's rows are brands the provider table knows; the Agentic IDE's
* rows are the CLIs installed on this machine, and several of them —
* OpenCode, Kimi, GLM — are marks only `AgentMark` carries. Without this the
* IDE's picker would fall back to a letter in a box, which is the one thing
* a logo must never be here. Absent for every catalog row, which keeps the
* front page's picker exactly as it was.
*/
agentMark?: string;
/** A mark the entry brings itself — the file a user uploaded for their own CLI. */
logoUrl?: string;
}
export interface AgentChatStore {
/** Which list and catalog this store speaks for; fixed at creation. */
readonly surface: AgentChatSurface;
catalog: AgentChatCatalog | null;
connections: AgentConnectionRow[];
/**
* The catalog on hand is a copy from before (the last start, or the chat
* left behind), painted at once while a fresh read is on its way. The
* composer used to show "Provider" for seconds on every start and every new
* chat because the catalog route waits on CLI model lists; the picks now
* read from this copy and settle when the fresh one lands.
*/
catalogStale: boolean;
catalogError: string | null;
/**
* The catalog answered without the fields this bundle reads (no permission
* ladders): the backend process predates the app on disk — an update that
* was reloaded but not yet restarted. The composer says so instead of
* silently showing fewer picks.
*/
backendOutdated: boolean;
/** Live model lists per provider id (the API-backed rows). */
liveModels: Record<string, CuratedModel[]>;
/** Live per-provider state, keyed by provider id; empty until the sweep lands. */
health: Record<string, ProviderHealth>;
sessions: AgentChatSession[];
activeSessionId: string | null;
activeSession: AgentChatSession | null;
timeline: Timeline;
socketState: "idle" | "connecting" | "open" | "closed";
draft: ComposerDraft;
busy: boolean;
lastError: string | null;
/**
* The picks this chat cannot take, each with the sentence that says why —
* the composer disables that pill and shows the sentence on it. Absent (a
* session the chat runs itself) means every pick is live. A pane's store
* sets these: its CLI chose its provider the moment it started, and takes
* the other three only where it has a typed command for them
* (`store/paneChat`, `RuntimePickOffers`).
*/
locks?: Partial<Record<"provider" | "model" | "effort" | "permissionMode", string>>;
loadCatalog: () => Promise<void>;
loadSessions: () => Promise<void>;
loadModels: (providerId: string) => Promise<void>;
providerOptions: () => ProviderOption[];
/** Ask which seats actually answer. Never awaited by a render path. */
loadHealth: (refresh?: boolean) => Promise<void>;
providerById: (id: string) => ProviderOption | null;
setDraft: (patch: Partial<ComposerDraft>) => Promise<void>;
setPlan: (on: boolean) => Promise<void>;
/**
* A blank page. `voiceSessionId` = an archived voice chat goes on stage
* instead, and calls continue it rather than opening a new chat.
*/
newChat: (opts?: { voiceSessionId?: string }) => void;
openSession: (sessionId: string) => void;
removeSession: (sessionId: string) => Promise<void>;
/** Send the sentence, with whatever files the composer is holding for it. */
send: (text: string, attachments?: ChatAttachment[], toolChoices?: string[], onSessionCreated?: (session: AgentChatSession) => void) => Promise<void | "sent" | "failed" | "stale">;
/** Stop the running turn; `via` names the control that asked, for the log. */
cancel: (via?: string) => Promise<void>;
/**
* Answer an approval card in the active session. The card closes as soon as
* the backend took the answer, or said nothing waits on it any more; any
* other failure throws so the card can say why.
*/
decide: (approvalId: string, decision: ApprovalDecision, rule?: ApprovalRuleChoice) => Promise<void>;
/**
* Turn an approval card into a standing rule after the fact ("Allow every
* time", "Check with me every time"). The card shows it at once; throws so
* the card can say why when nothing was saved.
*/
saveApprovalRule: (approvalId: string, rule: ApprovalRuleRequest) => Promise<void>;
/** Answer question `index` of an agent's card in the active session; throws so the card can say why. */
answerQuestion: (questionId: string, index: number, answer: QuestionAnswerInput) => Promise<void>;
/**
* Close an agent's card so its recommendations apply; with `index`, skip
* only that question. Throws so the card can say why.
*/
skipQuestion: (questionId: string, index?: number) => Promise<void>;
/**
* Send the person's reviewed answers to the whole card at once. Optional:
* a store without it gets the answers one question at a time. Throws so the
* card can roll back; a card the server no longer holds resolves quietly.
*/
submitQuestion?: (questionId: string, answers: QuestionSubmitAnswer[]) => Promise<void>;
/** The person is still answering: the card's quiet window starts again. Never throws. */
touchQuestion?: (questionId: string) => Promise<void>;
/**
* A composer message while a card is open answers it. True when a running
* turn took the message as its answer, so nothing else is sent; false sends
* it as usual (an end-of-turn card is settled by the same call).
*/
answerByMessage?: (text: string) => Promise<boolean>;
/** Answer a coding agent's plan card in the active session; throws so the card can say why. */
resolvePlan: (turnId: string, decision: PlanDecision) => Promise<void>;
/** Tests and the socket: fold one event into the active timeline. */
ingest: (event: AgentChatEvent) => void;
disconnect: () => void;
}
function readDraft(key: string): ComposerDraft {
const empty: ComposerDraft = {
provider: "",
model: "",
effort: "",
permissionMode: "",
buildMode: "",
cwd: "",
};
try {
const raw = window.localStorage.getItem(key);
if (!raw) return empty;
const parsed = JSON.parse(raw) as Partial<ComposerDraft>;
return { ...empty, ...parsed };
} catch {
return empty;
}
}
function writeDraft(key: string, draft: ComposerDraft): void {
try {
window.localStorage.setItem(key, JSON.stringify(draft));
} catch {
/* storage blocked — the draft just does not survive a reload */
}
}
/** Where the last catalog a surface read is kept between starts. */
function catalogCacheKey(surface: AgentChatSurface): string {
return `jarvis.agentChat.catalog.${surface}.v1`;
}
interface CatalogSnapshot {
catalog: AgentChatCatalog;
connections: AgentConnectionRow[];
}
function readCatalogSnapshot(surface: AgentChatSurface): CatalogSnapshot | null {
try {
const raw = window.localStorage.getItem(catalogCacheKey(surface));
if (!raw) return null;
const parsed = JSON.parse(raw) as Partial<CatalogSnapshot>;
if (!parsed.catalog || !Array.isArray(parsed.catalog.providers)) return null;
return {
catalog: parsed.catalog,
connections: Array.isArray(parsed.connections) ? parsed.connections : [],
};
} catch {
return null; // storage blocked or a damaged copy — wait for the route
}
}
function writeCatalogSnapshot(surface: AgentChatSurface, snapshot: CatalogSnapshot): void {
try {
window.localStorage.setItem(catalogCacheKey(surface), JSON.stringify(snapshot));
} catch {
/* storage blocked or full — the next start just waits for the route */
}
}
function errorText(err: unknown): string {
return err instanceof Error ? err.message : String(err);
}
/** Which `/api/jarvis-agent/status` row speaks for a catalog provider. */
function connectionFor(
provider: AgentChatProvider,
rows: AgentConnectionRow[],
): AgentConnectionRow | undefined {
return rows.find((r) => r.jarvis === provider.id);
}
/**
* The catalog joined with the Agents tab's credential truth — one row per
* provider with `connected` decided here and nowhere else. Pure, so a surface
* without a chat store (the society's creator) joins the same way and the
* two pickers can never disagree about what is usable.
*/
export function joinProviderOptions(
providers: AgentChatProvider[],
connections: AgentConnectionRow[],
): ProviderOption[] {
return providers.map((p) => {
const row = connectionFor(p, connections);
// An API seat needs an API KEY, not just any credential. The Agents
// tab counts a subscription login as connected — right for a row a
// CLI runs — but the brain runner calls the provider's endpoint,
// where a Claude Code login buys nothing. `key_set` stands in when
// an older backend does not report the finer field.
// A coding CLI the Agents tab has no card for — OpenCode, Kimi,
// GLM Coding Plan, the DeepSeek harness — keeps its own login where
// this app has no verified reader; installed is all it can know,
// and the live sweep says the rest.
const credentialed = p.keyless
? true
: isApiRunner(p.runner)
? Boolean(row?.api_key_set ?? row?.key_set)
: row
? Boolean(row.key_set)
: true;
const installed = p.cli_installed === null ? true : p.cli_installed;
return {
...p,
connected: credentialed && installed,
active: Boolean(row?.is_active_brain),
// The IDE's mark for the CLI behind a CLI row; an API row keeps
// its provider-family logo.
agentMark: p.agent || undefined,
};
});
}
/** A session patch the composer's draft must mirror. */
function draftFromSession(session: AgentChatSession, prev: ComposerDraft): ComposerDraft {
const plan = session.permission_mode === "plan";
return {
provider: session.provider,
accountId: session.account_id ?? "",
model: session.model,
effort: session.effort,
permissionMode: session.permission_mode,
buildMode: plan ? prev.buildMode || prev.permissionMode : session.permission_mode,
cwd: session.cwd,
};
}
/**
* One store per surface. Each holds its own socket, its own draft key and
* its own session list, so the front page's chat and an IDE's coding
* sessions can be open at the same time without sharing a connection.
*/
/** `value` when the ladder offers it (or the ladder is unknown), else `fallback`. */
function onLadder(value: string, ladder: readonly string[], fallback: string): string {
if (!value) return fallback;
if (ladder.length === 0) return value;
return ladder.includes(value) ? value : fallback;
}
/** How many left-behind transcripts the store keeps for an instant switch-back. */
const TIMELINE_CACHE_LIMIT = 24;
export function createAgentChatStore(surface: AgentChatSurface, draftNamespace = ""): UseBoundStore<StoreApi<AgentChatStore>> {
const DRAFT_KEY = draftKey(surface) + (draftNamespace ? `:${draftNamespace}` : "");
/**
* Tell the backend which Jarvis chat is on stage, so a voice call continues
* THIS chat — its turns land here and it starts with this chat's history.
* `null` = a blank page: the next call opens a new chat. Only the front
* page's chat has a voice; the other surfaces never bind.
*/
const bindVoice = (sessionId: string | null, voiceSessionId: string | null = null) => {
if (surface !== "jarvis") return;
void bindVoiceChat(sessionId, voiceSessionId).catch((err: unknown) => {
// Not fatal: the composer's voice button binds again before a call.
console.info("Voice chat binding failed.", err);
});
};
let catalogRequest = 0;
const patchQueues = new Map<string, Promise<void>>();
const patchRevisions = new Map<string, number>();
type SendLane = { tail: Promise<unknown>; pending: number; sessionId: string | null; draftTarget?: ComposerDraftTarget };
const sendLanes = new Map<string, SendLane>();
const emptySendLane = (): SendLane => ({ tail: Promise.resolve(), pending: 0, sessionId: null });
let newChatLane = emptySendLane();
let selectionRevision = 0;
let selectionWrite: Promise<unknown> = Promise.resolve();
let socket: WebSocket | null = null;
let socketSession: string | null = null;
/** Cancels the reconnect waiting in the shared connect budget. */
let cancelReconnect: (() => void) | null = null;
/** Consecutive failed reconnects — the exponent under the jitter. */
let reconnectAttempt = 0;
/**
* Last-known transcript per session. Switching chats (or society agents)
* used to keep painting the previous session until the new socket's
* snapshot arrived; this cache is restored synchronously in `openSession`
* so the column never shows another conversation, and a return visit
* paints immediately while the socket refreshes.
*/
const timelineCache = new Map<string, Timeline>();
function rememberTimeline(sessionId: string, timeline: Timeline): void {
if (timeline.lastSeq <= 0 && timeline.items.length === 0) return;
timelineCache.delete(sessionId);
timelineCache.set(sessionId, timeline);
while (timelineCache.size > TIMELINE_CACHE_LIMIT) {
const oldest = timelineCache.keys().next().value;
if (oldest === undefined) break;
timelineCache.delete(oldest);
}
}
function closeSocket(): void {
cancelReconnect?.();
cancelReconnect = null;
const s = socket;
socket = null;
socketSession = null;
if (s) {
s.onclose = null;
s.onmessage = null;
s.onerror = null;
s.onopen = null;
try {
s.close();
} catch {
/* already closed */
}
}
}
const chatStore = create<AgentChatStore>((set, get) => {
function connect(sessionId: string, afterSeq: number): void {
if (typeof WebSocket === "undefined") return;
closeSocket();
socketSession = sessionId;
set({ socketState: "connecting" });
const ws = new WebSocket(agentChatSocketUrl(sessionId, afterSeq));
socket = ws;
ws.onopen = () => {
if (socket !== ws) return;
// A socket that opened is proof the backend is back: the next outage
// starts its backoff from the fast end again.
reconnectAttempt = 0;
set({ socketState: "open" });
};
ws.onmessage = (msg) => {
if (socket !== ws) return;
let frame: { type?: string; session?: AgentChatSession; events?: AgentChatEvent[]; event?: AgentChatEvent };
try {
frame = JSON.parse(String(msg.data));
} catch {
return;
}
if (frame.type === "snapshot" && frame.session) {
const prev = get();
// A snapshot after a reconnect only carries events past `after`;
// fold them onto what is already there, else start fresh.
const base = afterSeq > 0 && prev.activeSessionId === sessionId ? prev.timeline : EMPTY_TIMELINE;
const tl = reduceEvents(base, frame.events ?? []);
set({
activeSession: frame.session,
timeline: { ...tl, sessionPatch: null },
draft: draftFromSession(frame.session, prev.draft),
});
if (surface !== "jarvis") writeDraft(DRAFT_KEY, get().draft);
if (prev.activeSession?.account_id !== frame.session.account_id) void get().loadCatalog();
return;
}
if (frame.type === "event" && frame.event) get().ingest(frame.event);
};
ws.onclose = () => {
if (socket !== ws) return;
socket = null;
set({ socketState: "closed" });
// Reconnect while this session is still the open one; the backend
// replays what was missed from the last seq.
// Jittered, escalating and paid for out of the shared connect budget.
// It used to be a flat 1.5 s with no cap and no spread: every window
// holding this session retried in the same millisecond, forever, which
// is one of the loops that emptied the machine's socket pool when the
// backend went away for a while (BUG-215, AP-33).
reconnectAttempt += 1;
cancelReconnect = requestConnect(
() => {
cancelReconnect = null;
const st = get();
if (st.activeSessionId === sessionId && socketSession === sessionId) {
connect(sessionId, st.timeline.lastSeq);
}
},
jitteredDelay(reconnectAttempt - 1, RECONNECT_MS, RECONNECT_MAX_MS),
);
};
ws.onerror = () => {
/* onclose follows and schedules the reconnect */
};
}
const snapshot = readCatalogSnapshot(surface);
return {
surface,
catalog: snapshot?.catalog ?? null,
connections: snapshot?.connections ?? [],
catalogStale: snapshot !== null,
catalogError: null,
backendOutdated: false,
liveModels: {},
health: {},
sessions: [],
activeSessionId: null,
activeSession: null,
timeline: EMPTY_TIMELINE,
socketState: "idle",
draft: readDraft(DRAFT_KEY),
busy: false,
lastError: null,
loadCatalog: async () => {
const requestId = ++catalogRequest;
const revision = selectionRevision;
const sessionId = get().activeSessionId;
try {
if (surface === "jarvis" && !sessionId) await selectionWrite;
const [raw, connections] = await Promise.all([
fetchAgentChatCatalog(surface, { sessionId: sessionId ?? undefined, cwd: get().draft.cwd || undefined }),
fetchAgentConnections().catch(() => [] as AgentConnectionRow[]),
]);
if (requestId !== catalogRequest || sessionId !== get().activeSessionId) return;
// A backend older than this bundle (the app not yet restarted after
// an update) may lack the newer arrays; an empty ladder is honest,
// a crash is not — and the composer tells the person to restart.
// `permission_modes` is the tell: every row of the current route
// carries the array (possibly empty); only the older route omits it.
const providersRaw = raw.providers ?? [];
const backendOutdated =
providersRaw.length > 0 && providersRaw.some((p) => !Array.isArray(p.permission_modes));
const catalog: AgentChatCatalog = {
...raw,
providers: providersRaw.map((p) => ({
...p,
curated_models: Array.isArray(p.curated_models) ? p.curated_models : [],
effort_levels: Array.isArray(p.effort_levels) ? p.effort_levels : [],
permission_modes: Array.isArray(p.permission_modes) ? p.permission_modes : [],
default_permission_mode: p.default_permission_mode ?? "",
default_effort: p.default_effort ?? "",
default_model: p.default_model ?? "",
})),
};
set({ catalog, connections, catalogStale: false, catalogError: null, backendOutdated });
if (!backendOutdated) writeCatalogSnapshot(surface, { catalog, connections });
// Deliberately not awaited: the sweep reads CLI logins and can take
// a moment. It never sends a request to a provider — the dots show
// the outcome of each seat's last real call. The composer paints
// from the catalog now and the dots appear when the answer arrives.
// The society picker has no health dots, so it skips the sweep.
if (surface !== "society") void get().loadHealth();
// Settle the draft: an empty or unknown provider becomes the active
// sub-agent (else the first connected one); blank picks take the
// provider's defaults.
const st = get();
const options = st.providerOptions();
let draft = st.draft;
if (surface === "jarvis" && !sessionId && revision === selectionRevision && catalog.selection) {
draft = { ...draft, provider: catalog.selection.provider, model: catalog.selection.model, effort: catalog.selection.effort, accountId: catalog.selection.account_id ?? "" };
}
const known = options.find((o) => o.id === draft.provider);
if (!known) {
const pick = options.find((o) => o.active && o.connected) ?? options.find((o) => o.connected) ?? options[0];
if (pick) {
draft = {
provider: pick.id,
model: pick.default_model,
effort: pick.default_effort,
permissionMode: pick.default_permission_mode,
buildMode: pick.default_permission_mode,
cwd: draft.cwd || catalog.default_cwd,
};
}
} else {
draft = {
...draft,
effort: onLadder(draft.effort, known.effort_levels, known.default_effort),
// A stored mode the row's ladder does not offer (a vendor word
// from before the unified ladder, a pick made on another
// surface) snaps to the row's default rather than showing a
// label the person cannot choose.
permissionMode: onLadder(
draft.permissionMode,
known.permission_modes.map((m) => m.id),
known.default_permission_mode,
),
buildMode: onLadder(
draft.buildMode,
known.permission_modes.map((m) => m.id),
known.default_permission_mode,
),
cwd: draft.cwd || catalog.default_cwd,
};
}
if (draft !== st.draft) {
set({ draft });
if (surface !== "jarvis" || !sessionId) writeDraft(DRAFT_KEY, draft);
}
// Upgrade an existing local pick once, so voice can use it before
// the person sends another chat message.
if (surface === "jarvis" && !sessionId && !catalog.selection && known && revision === selectionRevision) {
const selection = { provider: draft.provider, model: draft.model, effort: draft.effort, ...(draft.accountId ? { account_id: draft.accountId } : {}) };
selectionWrite = selectionWrite.catch(() => undefined).then(() => saveChatSelection(selection));
try {
await selectionWrite;
} catch (err) {
set({ lastError: errorText(err) });
}
}
if (draft.provider) void get().loadModels(draft.provider);
} catch (err) {
if (requestId === catalogRequest && sessionId === get().activeSessionId) {
set({ catalogError: errorText(err), catalogStale: false });
}
}
},
loadSessions: async () => {
try {
const rows = await fetchAgentChatSessions(SESSION_LIST_LIMIT, surface);
// The backend filters by surface; a row that names another surface
// anyway is dropped here too. A row with no surface at all comes
// from a backend older than the split — those are kept, so an
// update never makes a person's chats vanish until the restart.
const sessions = rows.filter((row) => row.surface === undefined || row.surface === surface);
set((state) => {
const shared = reuseHistoryRows(state.sessions, sessions, (row) => row.session_id);
return shared === state.sessions ? state : { sessions: shared };
});
} catch {
/* offline / headless — keep the list as-is */
}
},
loadModels: async (providerId) => {
const provider = get().catalog?.providers.find((p) => p.id === providerId);
if (!provider || provider.models_source !== "live") return;
if (get().liveModels[providerId]) return;
try {
const models = await fetchProviderModels(providerId);
const rows: CuratedModel[] = models.map((m) => ({ id: m.id, label: m.label ?? m.name ?? m.id }));
set((s) => ({ liveModels: { ...s.liveModels, [providerId]: rows } }));
} catch {
/* the curated list stays */
}
},
loadHealth: async (refresh = false) => {
try {
const rows = await fetchProviderHealth(surface, refresh);
const health: Record<string, ProviderHealth> = {};
for (const row of rows) health[row.provider] = row;
set({ health });
} catch {
// A backend too old to know the route, or a sweep that failed: the
// composer simply shows no live state, which is what it did before.
// Never an error banner — nothing the person did went wrong.
}
},
providerOptions: () => {
const { catalog, connections } = get();
if (!catalog) return [];
return joinProviderOptions(catalog.providers, connections);
},
providerById: (id) => get().providerOptions().find((p) => p.id === id) ?? null,
setDraft: async (patch) => {
const st = get();
const changesSelection = patch.provider !== undefined || patch.model !== undefined || patch.effort !== undefined;
if (changesSelection) ++selectionRevision;
let draft: ComposerDraft = { ...st.draft, ...patch };
// A provider change re-seats model / effort / permission on the new
// provider's defaults unless the caller set them explicitly.
if (patch.provider && patch.provider !== st.draft.provider) {
const p = st.providerById(patch.provider);
if (p) {
draft = {
...draft,
model: patch.model ?? p.default_model,
accountId: patch.accountId ?? "",
effort: patch.effort ?? p.default_effort,
permissionMode: patch.permissionMode ?? p.default_permission_mode,
buildMode: patch.permissionMode ?? p.default_permission_mode,
};
}
void get().loadModels(patch.provider);
} else if (patch.permissionMode && patch.permissionMode !== "plan") {
draft = { ...draft, buildMode: patch.permissionMode };
}
set({ draft });
writeDraft(DRAFT_KEY, draft);
// With a session open, the pick applies to it right away.
const sid = st.activeSessionId;
if (surface === "jarvis" && changesSelection && !sid && draft.provider) {
const selection = { provider: draft.provider, model: draft.model, effort: draft.effort, ...(draft.accountId ? { account_id: draft.accountId } : {}) };
selectionWrite = selectionWrite.catch(() => undefined).then(() => saveChatSelection(selection));
try {
await selectionWrite;
} catch (err) {
set({ lastError: errorText(err) });
}
}
if (sid) {
const body: PatchSessionInput = {};
if (patch.provider !== undefined) body.provider = draft.provider;
if (patch.provider !== undefined || patch.model !== undefined) body.model = draft.model;
if (patch.provider !== undefined || patch.effort !== undefined) body.effort = draft.effort;
if (patch.provider !== undefined || patch.permissionMode !== undefined) {
body.permission_mode = draft.permissionMode;
}
if (patch.cwd !== undefined) body.cwd = draft.cwd;
const revision = (patchRevisions.get(sid) ?? 0) + 1;
patchRevisions.set(sid, revision);
const previous = surface === "jarvis"
? selectionWrite.catch(() => undefined)
: patchQueues.get(sid) ?? Promise.resolve();
const pending = previous.then(() => patchAgentChatSession(sid, body));
if (surface === "jarvis") selectionWrite = pending;
patchQueues.set(sid, pending.then(() => undefined, () => undefined));
try {
const session = await pending;
let reconciledDraft: ComposerDraft | null = null;
set((s) => {
const latest = patchRevisions.get(sid) === revision;
const active = s.activeSessionId === sid && latest;
if (active) reconciledDraft = draftFromSession(session, s.draft);
return {
activeSession: active ? session : s.activeSession,
draft: reconciledDraft ?? s.draft,
sessions: latest
? s.sessions.map((x) => (x.session_id === sid ? { ...x, ...session } : x))
: s.sessions,
};
});
if (reconciledDraft) writeDraft(DRAFT_KEY, reconciledDraft);
} catch (err) {
if (get().activeSessionId === sid && patchRevisions.get(sid) === revision) {
set({ lastError: errorText(err) });
}
} finally {
if (patchRevisions.get(sid) === revision) {
patchQueues.delete(sid);
patchRevisions.delete(sid);
}
}
}
},
setPlan: async (on) => {
const st = get();
if (on) {
await st.setDraft({ permissionMode: "plan" });
} else {
const p = st.providerById(st.draft.provider);
const back = st.draft.buildMode && st.draft.buildMode !== "plan" ? st.draft.buildMode : p?.default_permission_mode ?? "";
await st.setDraft({ permissionMode: back });
}
},
newChat: (opts) => {
closeSocket();
newChatLane = emptySendLane();
resetBlankComposerDraftTarget(chatStore);
discardHeldFiles(chatStore, "");
++catalogRequest;
set({
...(surface === "jarvis" ? { draft: readDraft(DRAFT_KEY) } : {}),
activeSessionId: null,
activeSession: null,
busy: false,
timeline: EMPTY_TIMELINE,
socketState: "idle",
lastError: null,
// The chat left behind keeps the picks painted while this one's
// fresh read runs; blanking the catalog showed "Provider" for
// seconds on every new chat.
catalogStale: get().catalog !== null,
});
bindVoice(null, opts?.voiceSessionId ?? null);
void get().loadCatalog();
},
openSession: (sessionId) => {
if (get().activeSessionId === sessionId && socket) return;
const prev = get();
if (prev.activeSessionId && prev.activeSessionId !== sessionId) {
rememberTimeline(prev.activeSessionId, prev.timeline);
}
set({
activeSessionId: sessionId,
activeSession: prev.sessions.find((s) => s.session_id === sessionId) ?? null,
timeline: timelineCache.get(sessionId) ?? EMPTY_TIMELINE,
busy: (sendLanes.get(sessionId)?.pending ?? 0) > 0,
lastError: null,
});
bindVoice(sessionId);
void get().loadCatalog();
connect(sessionId, 0);
},
removeSession: async (sessionId) => {
try {
await deleteAgentChatSession(sessionId);
} catch (err) {
set({ lastError: errorText(err) });
}
timelineCache.delete(sessionId);
patchQueues.delete(sessionId);
patchRevisions.delete(sessionId);
if (get().activeSessionId === sessionId) get().newChat();
set((s) => ({ sessions: s.sessions.filter((x) => x.session_id !== sessionId) }));
void get().loadSessions();
},
send: async (text, attachments = [], toolChoices = [], onSessionCreated) => {
const content = text.trim();
// A message may be files alone — dropping a screenshot and pressing
// Enter is a complete gesture — but never nothing at all.
if (!content && attachments.length === 0) return "failed";
const st = get();
const lane = st.activeSessionId
? sendLanes.get(st.activeSessionId) ?? { ...emptySendLane(), sessionId: st.activeSessionId }
: newChatLane;
lane.draftTarget ??= captureComposerDraftTarget(chatStore, st.activeSessionId);
if (lane.sessionId) sendLanes.set(lane.sessionId, lane);
const isVisible = () => lane.sessionId
? get().activeSessionId === lane.sessionId
: get().activeSessionId === null && newChatLane === lane;
const previous = lane.tail;
lane.pending += 1;
set({ busy: true, lastError: null });
// Serialize transport admission, not agent execution. A second send
// shares an unfinished session creation and reaches the server in order.
const submission = previous.catch(() => undefined).then(async () => {
try {
await selectionWrite;
if (!lane.sessionId) {
const d = st.draft;
const session = await createAgentChatSession({
provider: d.provider,
...(d.accountId ? { account_id: d.accountId } : {}),
model: d.model,
effort: d.effort,
cwd: d.cwd || null,
permission_mode: d.permissionMode,
surface,
});
const visible = isVisible();
if (bindComposerDraftTarget(chatStore, lane.draftTarget!, session.session_id)) {
moveHeldFiles(chatStore, "", session.session_id);
}
lane.sessionId = session.session_id;
sendLanes.set(session.session_id, lane);
set({ sessions: [session, ...get().sessions] });
onSessionCreated?.(session);
if (visible) {
set({ activeSessionId: session.session_id, activeSession: session, timeline: EMPTY_TIMELINE });
bindVoice(session.session_id);
connect(session.session_id, 0);
}
}
const sid = lane.sessionId;
await sendAgentChatMessage(sid, content, attachments, toolChoices);
if (surface === "jarvis") writeDraft(DRAFT_KEY, st.draft);
if (get().activeSessionId === sid) void get().loadSessions();
return isVisible() ? "sent" as const : "stale" as const;
} catch (err) {
if (isVisible()) set({ lastError: errorText(err) });
return "failed" as const;
} finally {
// A switch away from this session already cleared `busy`; do not
// unlock (or error) the chat that is on screen now.
lane.pending -= 1;
if (isVisible()) set({ busy: lane.pending > 0 });
if (lane.pending === 0 && lane.sessionId && sendLanes.get(lane.sessionId) === lane) {
sendLanes.delete(lane.sessionId);
}
}
});
lane.tail = submission;
return submission;
},
cancel: async (via) => {
const sid = get().activeSessionId;
if (!sid) return;
try {
await cancelAgentChatTurn(sid, via);
} catch (err) {
set({ lastError: errorText(err) });
}
},
decide: async (approvalId, decision, rule) => {
const sid = get().activeSessionId;
if (!sid) return;
let outcome: string = decision;
// The person has answered: "needs your OK" leaves the live line and
// the roster now, not when the backend's confirmation arrives.
set({ timeline: markApprovalAnswering(get().timeline, approvalId, true) });
try {
if (rule) await resolveAgentChatApproval(sid, approvalId, decision, rule);
else await resolveAgentChatApproval(sid, approvalId, decision);
markAnswered(approvalId);
} catch (err) {
// 410: the card outlived what it asked for (an app restart) and is
// closed now; 404: the backend never knew it. Either way nothing
// waits on it, and the next card the agent opened takes its place.
const gone = err instanceof AgentChatApiError && (err.status === 404 || err.status === 410);
if (!gone) {
if (get().activeSessionId === sid) set({ timeline: markApprovalAnswering(get().timeline, approvalId, false) });
throw err;
}
outcome = "expired";
}
if (get().activeSessionId === sid) {
const scope = outcome === "allow_always" ? rule?.scope : undefined;
set({ timeline: settleApproval(get().timeline, approvalId, outcome, "", scope) });
}
},
saveApprovalRule: async (approvalId, rule) => {
const sid = get().activeSessionId;
if (!sid) throw new Error("no open chat");
const saved = await saveAgentChatApprovalRule(sid, approvalId, rule);
if (get().activeSessionId !== sid) return;
// The socket's notice says the same; the card must not wait on it.
set({ timeline: noteApprovalRule(get().timeline, approvalId, {
kind: rule.kind, scope: rule.scope, pattern: saved.pattern,
app: saved.app ?? "", verb: saved.verb ?? "", stillAsks: saved.still_asks === true,
}) });
},
answerQuestion: async (questionId, index, answer) => {
const sid = get().activeSessionId;
if (!sid) throw new Error("no open chat");
await answerAgentChatQuestion(sid, questionId, index, answer);
},
skipQuestion: async (questionId, index) => {
const sid = get().activeSessionId;
if (!sid) throw new Error("no open chat");
await skipAgentChatQuestion(sid, questionId, index);
},
submitQuestion: async (questionId, answers) => {
const sid = get().activeSessionId;
if (!sid) throw new Error("no open chat");
try {
await submitAgentChatQuestion(sid, questionId, answers);
} catch (err) {
// 404: the card closed on the server meanwhile (its window ran out,
// or the turn ended). Its own closing event says what was chosen.
if (err instanceof AgentChatApiError && err.status === 404) return;
throw err;
}
},
touchQuestion: async (questionId) => {
const sid = get().activeSessionId;
if (!sid) return;
try {
await touchAgentChatQuestion(sid, questionId);
} catch {
/* a lost touch only means the card keeps its first deadline */
}
},
answerByMessage: async (text) => {
const sid = get().activeSessionId;
if (!sid || !text.trim() || !openQuestionOf(get().timeline)) return false;
try {
const outcome = await replyToAgentChatQuestion(sid, text);
return outcome.consumed;
} catch {
// The route failed: the message still goes out as an ordinary one.
return false;
}
},
resolvePlan: async (turnId, decision) => {
const sid = get().activeSessionId;
if (!sid) throw new Error("no open chat");
await resolveAgentChatPlan(sid, turnId, decision);
},
ingest: (event) => {
const st = get();
const tl = reduceEvent(st.timeline, event);
if (tl === st.timeline) return;
const patch: Partial<AgentChatStore> = { timeline: tl };
if (tl.sessionPatch && st.activeSession) {
const session = { ...st.activeSession, ...tl.sessionPatch } as AgentChatSession;
patch.activeSession = session;
patch.draft = draftFromSession(session, st.draft);
if (surface !== "jarvis") writeDraft(DRAFT_KEY, patch.draft);
}
set(patch);
if (event.kind === "turn_finished" || event.kind === "user_message") {
invalidateAgentChatSessions();
void st.loadSessions();
}
},
disconnect: () => {
closeSocket();
set({ socketState: "idle" });
},
};
});
return chatStore;
}
/** The front page's store — every existing call site reads this one. */
export const useAgentChatStore = createAgentChatStore("jarvis");
/**
* The Agentic IDE's chat store — surface `agent`: coding-agent sessions in a
* workspace's folder, with their own sessions, draft and socket. The shared
* chat components reach it through `AgentChatStoreProvider`
* (components/agentchat/AgentChatStoreContext); nothing on the front page
* ever reads it, so the IDE's last chat can never appear there.
*/
export const useAgentSessionStore = createAgentChatStore("agent");
/** The hook a `createAgentChatStore` call returns — what the context carries. */
export type AgentChatStoreHook = typeof useAgentChatStore;