import { classifyDataSurface } from "../data-surface"; import { useCallback, useEffect, useMemo, useReducer, useRef, useState } from "react"; import { useKeyedClientResource } from "../client-resource"; import { replaceHash } from "../hash-routing"; import { useI18n } from "../i18n/shared"; import { openDesktopUpdatePage } from "../lib/desktop-shell"; import { clearSessionListCache, readSessionListCache, writeSessionListCache } from "../session-list-cache"; import { PROJECT_CONFIG_DIAGNOSTICS_POLL_MS, STARTUP_HEALTH_STALE_RETRY_MS, probeNeedsFastRetry, mergeStartupProbe, seedStartupHealthFromSettings, startupDesktopContext, type StartupHealthStatus, } from "../startup-health-ui"; import { fetchDashboardMaMode, fetchDashboardModels, fetchDashboardMultiAgent, fetchDashboardOverview, fetchDashboardSettings, fetchDashboardSidecars, fetchDashboardUsage, fetchProjectConfigDiagnostics, fetchStartupHealth, normalizeInjectionSelection, type DashboardEpochRefs, } from "./dashboard-core-poll"; import { usageSummary30dResourceKey } from "../usage-summary-resource"; import { confirmsPlaintextV2Mode } from "../subagent-surface"; import type { SubagentSurfaceAdvisory, SubagentSurfaceMode } from "../subagent-surface"; import { type DashboardSection, type HealthData, type ModelInfo, type ProjectCodexConfigGroup, type ProviderInfo, type SettingsData, type ShadowCallData, type SidecarData, type SidecarPatch, type SyncResult, type UpdateChannel, type UpdateCheckData, type UpdateJob, type UsageSummary30d, type SidecarCodexApply, UPDATE_CHECK_MAX_AUTO_RETRIES, UPDATE_CHECK_RETRY_BASE_MS, defaultUpdateChannel, hashRequestsUpdateDialog, mergeSidecarSetting, sidecarPatchForSave, nextSidecarCodexApply, readDashboardSectionFromHash, requireJson, webSearchModelOptionsForPicker, visionModelOptions, useModalDialog, } from "./dashboard-shared"; import { shadowCallSaveErrorKey } from "./models-shared"; const CONTROLS_CACHE_PREFIX = "ocx.dash.controls.v1:"; const OVERVIEW_CACHE_PREFIX = "ocx.dash.overview.v1:"; const USAGE_CACHE_PREFIX = "ocx.dash.usage30d.v1:"; const STARTUP_CACHE_PREFIX = "ocx.dash.startup.v3:"; const MA_MODE_CACHE_PREFIX = "ocx.dash.maMode.v1:"; type CachedControls = { settings?: SettingsData | null; sidecar?: SidecarData | null; shadowCall?: ShadowCallData | null; }; type CachedOverview = { health: HealthData; providers: ProviderInfo[]; }; type MaMode = SubagentSurfaceMode; type CodexPreference = "codexAutoStart" | "codexDesktopAuthless" | "codexClientCompaction"; type DashboardSettingsState = { settings: SettingsData | null; beforeSave: SettingsData | null; }; type DashboardSettingsAction = | { type: "polled"; settings: SettingsData } | { type: "save-started"; key: CodexPreference; value: boolean } | { type: "save-succeeded"; key: CodexPreference; settings: SettingsData } | { type: "save-failed" } | { type: "save-finished" } | { type: "applied" }; // Own both server snapshots and the local save/apply transaction. A poll has no // application receipt and must not overwrite a preference while it is being saved. function dashboardSettingsReducer(state: DashboardSettingsState, action: DashboardSettingsAction): DashboardSettingsState { switch (action.type) { case "polled": if (state.beforeSave) return state; return { ...state, settings: { ...action.settings, catalogRefreshPending: state.settings?.catalogRefreshPending === true || action.settings.catalogRefreshPending, }, }; case "save-started": if (!state.settings || state.beforeSave) return state; return { beforeSave: state.settings, settings: { ...state.settings, [action.key]: action.value } }; case "save-succeeded": if (!state.settings || !state.beforeSave) return state; return { ...state, settings: { ...state.settings, [action.key]: action.settings[action.key], catalogRefreshPending: action.key === "codexDesktopAuthless" || action.key === "codexClientCompaction" ? true : state.settings.catalogRefreshPending, startupHealth: action.settings.startupHealth ?? state.settings.startupHealth, }, }; case "save-failed": return state.beforeSave ? { ...state, settings: state.beforeSave } : state; case "save-finished": return { ...state, beforeSave: null }; case "applied": return state.settings ? { ...state, settings: { ...state.settings, catalogRefreshPending: false } } : state; } } export function groupDashboardModels(models: ModelInfo[]): Array<[string, ModelInfo[]]> { const groups = new Map(); for (const model of models) { const rows = groups.get(model.provider); if (rows) rows.push(model); else groups.set(model.provider, [model]); } return [...groups.entries()].sort(([a], [b]) => a.localeCompare(b)); } function controlsCacheKey(apiBase: string): string { return `${CONTROLS_CACHE_PREFIX}${apiBase}`; } export function useDashboardData(apiBase: string, refreshEpoch = 0, targetKey?: string) { const { locale, t } = useI18n(); // The hash is the source of truth for the active section (#dashboard, …). const [selectedSection, setSelectedSection] = useState(readDashboardSectionFromHash); const [modelQuery, setModelQuery] = useState(""); const [expandedProviders, setExpandedProviders] = useState>(new Set()); const cachedControls = useMemo( () => readSessionListCache(controlsCacheKey(apiBase)), [apiBase], ); const cachedOverview = useMemo( () => readSessionListCache(`${OVERVIEW_CACHE_PREFIX}${apiBase}`), [apiBase], ); const cachedUsage = useMemo( () => readSessionListCache(`${USAGE_CACHE_PREFIX}${apiBase}`), [apiBase], ); const cachedStartup = useMemo(() => { const cached = readSessionListCache<{ status: StartupHealthStatus; desktopManaged: boolean }>(`${STARTUP_CACHE_PREFIX}${apiBase}`); return cached && ["native", "protected", "unverified"].includes(cached.status) && typeof cached.desktopManaged === "boolean" ? cached : null; }, [apiBase]); const cachedMaMode = useMemo( () => readSessionListCache(`${MA_MODE_CACHE_PREFIX}${apiBase}`), [apiBase], ); const [health, setHealth] = useState(() => cachedOverview?.health ?? null); const [startupHealth, setStartupHealth] = useState(() => cachedStartup?.status ?? null); const [providers, setProviders] = useState(() => cachedOverview?.providers ?? []); const [models, setModels] = useState([]); const [settingsState, dispatchSettings] = useReducer(dashboardSettingsReducer, { settings: cachedControls?.settings ?? null, beforeSave: null, }); const { settings } = settingsState; const settingsSaving = settingsState.beforeSave !== null; const [sidecar, setSidecar] = useState(() => cachedControls?.sidecar ?? null); const [shadowCall, setShadowCall] = useState(() => cachedControls?.shadowCall ?? null); const [usage30d, setUsage30d] = useState(() => cachedUsage); const [sidecarSaving, setSidecarSaving] = useState(false); const [sidecarCodexApply, setSidecarCodexApply] = useState(); const [shadowCallSaving, setShadowCallSaving] = useState(false); const [shadowCallError, setShadowCallError] = useState(null); const [modelsLoading, setModelsLoading] = useState(false); const [syncing, setSyncing] = useState(false); const [maMode, setMaMode] = useState(() => cachedMaMode ?? "default"); const [maBusy, setMaBusy] = useState(false); const [maError, setMaError] = useState(null); /** The runtime's one-time advisory, and whether this page load has answered it. */ /** * The runtime's one-time advisory and any staged base/v2 selection, each tagged with the * endpoint it came from. This hook stays mounted across an endpoint switch, and clearing the * state from an effect would be a cascading render, so the tag is what scopes them. */ const [maAdvisoryState, setMaAdvisoryState] = useState<{ advisory: SubagentSurfaceAdvisory | null; apiBase: string } | null>(null); const [maAdvisoryAnsweredFor, setMaAdvisoryAnsweredFor] = useState(null); const [pendingMaModeState, setPendingMaModeState] = useState<{ mode: Exclude; apiBase: string } | null>(null); const maAdvisory = maAdvisoryState?.apiBase === apiBase ? maAdvisoryState.advisory : null; const maAdvisoryAnswered = maAdvisoryAnsweredFor === apiBase; const pendingMaMode = pendingMaModeState?.apiBase === apiBase ? pendingMaModeState.mode : null; // Same endpoint tag: a capability read on one server must not offer the fourth option on another. const [maPlaintextSupportState, setMaPlaintextSupportState] = useState<{ supports: boolean; apiBase: string } | null>(null); const [maHelpOpen, setMaHelpOpen] = useState(false); const [effortCapHelpOpen, setEffortCapHelpOpen] = useState(false); const [shadowCallHelpOpen, setShadowCallHelpOpen] = useState(false); const [injectionModel, setInjectionModel] = useState(""); const [injectionEffort, setInjectionEffort] = useState(""); const [injectionEfforts, setInjectionEfforts] = useState([]); const [injectionAvailable, setInjectionAvailable] = useState>([]); const [injectionSaving, setInjectionSaving] = useState(false); const [multiAgentGuidanceEnabled, setMultiAgentGuidanceEnabled] = useState(true); const [syncCodexSubagentDefaults, setSyncCodexSubagentDefaults] = useState(false); const [effortCap, setEffortCap] = useState(""); const [subagentEffortCap, setSubagentEffortCap] = useState(""); const [effortCapSaving, setEffortCapSaving] = useState(false); const [syncResult, setSyncResult] = useState(null); const [syncError, setSyncError] = useState(null); const [projectConfigWarnings, setProjectConfigWarnings] = useState([]); const [updateOpen, setUpdateOpen] = useState(false); const [updateChannel, setUpdateChannel] = useState("latest"); const [updateRestart, setUpdateRestart] = useState(true); const [updateLoading, setUpdateLoading] = useState(false); const updateRetryRef = useRef(0); const updateRetryTimerRef = useRef(null); const updateRequestEpochRef = useRef(0); const settingsRequestEpochRef = useRef(0); const settingsMutationEpochRef = useRef(0); const settingsMutationInFlightRef = useRef(false); const shadowCallRequestEpochRef = useRef(0); const shadowCallMutationEpochRef = useRef(0); const shadowCallMutationInFlightRef = useRef(false); const [updateCheck, setUpdateCheck] = useState(null); const [updateError, setUpdateError] = useState(null); const [updateJob, setUpdateJob] = useState(null); const [reconnecting, setReconnecting] = useState(false); const [error, setError] = useState(false); const effortCapHelpTriggerRef = useRef(null); const updateTriggerRef = useRef(null); const maHelpTriggerRef = useRef(null); const shadowCallHelpTriggerRef = useRef(null); const effortCapHelpDialogRef = useModalDialog(effortCapHelpOpen, effortCapHelpTriggerRef); const updateDialogRef = useModalDialog(updateOpen, updateTriggerRef); const maHelpDialogRef = useModalDialog(maHelpOpen, maHelpTriggerRef); const shadowCallHelpDialogRef = useModalDialog(shadowCallHelpOpen, shadowCallHelpTriggerRef); useEffect(() => { const onHash = () => setSelectedSection(readDashboardSectionFromHash()); window.addEventListener("hashchange", onHash); return () => window.removeEventListener("hashchange", onHash); }, []); useEffect(() => () => { updateRequestEpochRef.current += 1; if (updateRetryTimerRef.current !== null) { window.clearTimeout(updateRetryTimerRef.current); updateRetryTimerRef.current = null; } }, []); const startupHealthRef = useRef(cachedStartup?.status ?? null); const startupProbeAuthoritativeRef = useRef(false); const startupDesktopRef = useRef(cachedStartup?.desktopManaged === true); const startupTargetRef = useRef(apiBase); /** Bumped whenever the dedicated startup-health poll commits; core polls ignore older generations. */ const startupHealthGenerationRef = useRef(0); const epochRefs = useRef({ settingsRequestEpochRef, settingsMutationEpochRef, settingsMutationInFlightRef, shadowCallRequestEpochRef, shadowCallMutationEpochRef, shadowCallMutationInFlightRef, }).current; const startupHealthPoll = useKeyedClientResource( `dashboard-startup-health:${apiBase}`, [apiBase, refreshEpoch], (signal) => fetchStartupHealth(apiBase, signal), { pollMs: 30_000 }, ); useEffect(() => { if (startupTargetRef.current === apiBase) return; startupTargetRef.current = apiBase; startupHealthGenerationRef.current += 1; startupHealthRef.current = cachedStartup?.status ?? null; startupProbeAuthoritativeRef.current = false; startupDesktopRef.current = cachedStartup?.desktopManaged === true; setStartupHealth(startupHealthRef.current); }, [apiBase, cachedStartup]); /* * `/api/startup-health` answers instantly from a 30s cache and resolves the real probe in the * background, so a cold answer is a conservative placeholder. Waiting for the next 30s tick is * what made the chip look stuck until an unrelated action (refresh quota, tab hop) remounted it. * Re-ask in ~2s while the server says it is still working. */ const startupHealthStale = probeNeedsFastRetry(startupHealthPoll.data); const refreshStartupHealth = startupHealthPoll.refresh; useEffect(() => { if (!startupHealthStale) return; const timer = window.setTimeout(() => { void refreshStartupHealth(); }, STARTUP_HEALTH_STALE_RETRY_MS); return () => window.clearTimeout(timer); }, [startupHealthStale, refreshStartupHealth]); // Wave 1: status/uptime/providers must not wait on injection-model / usage. const overviewPoll = useKeyedClientResource( `dashboard-overview:${apiBase}`, [apiBase, refreshEpoch], (signal) => fetchDashboardOverview(apiBase, signal), { pollMs: 5000 }, ); const overviewSurface = classifyDataSurface(overviewPoll, data => data.health === null, true); const overviewReady = health !== null || overviewPoll.data !== undefined; // Preferences that are just config — never gate on overview or injection. const maModePoll = useKeyedClientResource( `dashboard-ma-mode:${apiBase}`, [apiBase, refreshEpoch], (signal) => fetchDashboardMaMode(apiBase, signal), { pollMs: 5000 }, ); const sidecarPoll = useKeyedClientResource( `dashboard-sidecars:${apiBase}`, [apiBase, refreshEpoch], async (signal) => { const startupHealthGeneration = startupHealthGenerationRef.current; const data = await fetchDashboardSidecars(apiBase, signal, epochRefs); return { ...data, startupHealthGeneration }; }, { pollMs: 5000 }, ); const settingsPoll = useKeyedClientResource( `dashboard-settings:${apiBase}`, [apiBase, refreshEpoch], async (signal) => { const startupHealthGeneration = startupHealthGenerationRef.current; const data = await fetchDashboardSettings(apiBase, signal, epochRefs); return { ...data, startupHealthGeneration }; }, { pollMs: 5000 }, ); // Wave 2: heavier peers start after overview commits (or session seed) to cut contention. const multiAgentPoll = useKeyedClientResource( `dashboard-multi-agent:${apiBase}`, [apiBase, refreshEpoch], (signal) => fetchDashboardMultiAgent(apiBase, signal), { pollMs: 5000, enabled: overviewReady }, ); const usagePoll = useKeyedClientResource( usageSummary30dResourceKey(apiBase, "all", targetKey), [apiBase, refreshEpoch, targetKey], (signal) => fetchDashboardUsage(apiBase, signal), // 30d usage is documented ~5s cold; this shared key has four subscribers, so // every one of them carries the same raised deadline (mount-order independent). { enabled: overviewReady, pollMs: 60_000, deadlineMs: 60_000 }, ); const diagnosticsPoll = useKeyedClientResource( `dashboard-diagnostics:${apiBase}`, [apiBase, refreshEpoch], (signal) => fetchProjectConfigDiagnostics(apiBase, signal), { pollMs: PROJECT_CONFIG_DIAGNOSTICS_POLL_MS, enabled: overviewReady }, ); const modelsPoll = useKeyedClientResource( `dashboard-models:${apiBase}`, [apiBase, error, refreshEpoch], (signal) => fetchDashboardModels(apiBase, signal), { enabled: overviewReady && !error }, ); /* oxlint-disable react/react-compiler -- mirror client-resource snapshots into mutable dashboard UI state that handlers also update */ /* eslint-disable react-hooks/set-state-in-effect -- mirror client-resource snapshots into mutable dashboard UI state that handlers also update */ useEffect(() => { if (startupHealthPoll.data !== undefined) { const probe = startupHealthPoll.data; startupHealthGenerationRef.current += 1; const mapped = mergeStartupProbe(probe, startupDesktopRef.current); setStartupHealth(mapped.status); startupHealthRef.current = mapped.status; startupProbeAuthoritativeRef.current = probe.status !== "error"; startupDesktopRef.current = mapped.desktopManaged; // Never persist hard errors — a cold SWR miss used to poison revisits. // A stale answer is a placeholder too: caching it makes the next visit start from // the server's guess instead of asking again. if (probe.status !== "error" && probe.status !== "at-risk" && !probe.stale) { writeSessionListCache(`${STARTUP_CACHE_PREFIX}${apiBase}`, mapped); } else clearSessionListCache(`${STARTUP_CACHE_PREFIX}${apiBase}`); } }, [startupHealthPoll.data, apiBase]); useEffect(() => { const data = overviewPoll.data; if (!data) return; if (data.health) { setHealth(data.health); setProviders(data.providers); writeSessionListCache(`${OVERVIEW_CACHE_PREFIX}${apiBase}`, { health: data.health, providers: data.providers, }); } setError(data.error); }, [overviewPoll.data, apiBase]); useEffect(() => { if (maModePoll.data === undefined) return; setMaMode(maModePoll.data.maMode); setMaAdvisoryState({ advisory: maModePoll.data.advisory ?? null, apiBase }); setMaPlaintextSupportState({ supports: maModePoll.data.supportsPlaintextV2, apiBase }); writeSessionListCache(`${MA_MODE_CACHE_PREFIX}${apiBase}`, maModePoll.data.maMode); }, [maModePoll.data, apiBase]); // The runtime advertises the fourth mode with supportsPlaintextV2Mode, proven per // apiBase by the live poll. A cached 'v2-plaintext' still renders as the stored // selection, but it cannot prove this server accepts the write — the same apiBase // may have been downgraded or replaced — so the option follows support only. const maSupportsPlaintextV2 = maPlaintextSupportState?.apiBase === apiBase && maPlaintextSupportState.supports === true; // Derived — avoids setState-on-prop-change for the resolved flag. Cache / poll / optimistic // save (which writes the same cache key) all count as resolved for MA UI. const maModeResolved = maModePoll.data !== undefined || cachedMaMode !== null; useEffect(() => { const data = multiAgentPoll.data; if (!data) return; if (data.injection) { setMultiAgentGuidanceEnabled(data.injection.multiAgentGuidanceEnabled); setSyncCodexSubagentDefaults(data.injection.syncCodexSubagentDefaults); setInjectionModel(data.injection.injectionModel); setInjectionEffort(data.injection.injectionEffort); setInjectionEfforts(data.injection.injectionEfforts); setInjectionAvailable(data.injection.injectionAvailable); } if (data.effortCaps) { setEffortCap(data.effortCaps.effortCap); setSubagentEffortCap(data.effortCaps.subagentEffortCap); } }, [multiAgentPoll.data]); useEffect(() => { const data = sidecarPoll.data; if (!data) return; setSidecar(data.sidecar); if (data.shadowCall !== undefined) setShadowCall(data.shadowCall); const prev = readSessionListCache(controlsCacheKey(apiBase)) ?? {}; writeSessionListCache(controlsCacheKey(apiBase), { ...prev, sidecar: data.sidecar, ...(data.shadowCall !== undefined ? { shadowCall: data.shadowCall } : {}), }); }, [sidecarPoll.data, apiBase]); useEffect(() => { const data = settingsPoll.data; if (!data) return; if (data.settings !== undefined) { dispatchSettings({ type: "polled", settings: data.settings }); } // Latest-wins: only seed from settings when no newer dedicated probe has committed // while this settings poll was in flight. Always merge against the live ref. if ( data.startupHealthSeed !== undefined && data.startupHealthGeneration === startupHealthGenerationRef.current ) { const previous = startupHealthRef.current; const merged = seedStartupHealthFromSettings(previous, data.startupHealthSeed, startupProbeAuthoritativeRef.current); setStartupHealth(merged); startupHealthRef.current = merged; if (data.startupHealthSeed && !startupProbeAuthoritativeRef.current && (!data.startupHealthSeed.diagnosticStale || data.startupHealthSeed.routingKind === "opencodex-local")) startupDesktopRef.current = startupDesktopContext(data.startupHealthSeed); if (merged && merged !== "at-risk" && merged !== "error" && !data.startupHealthSeed?.diagnosticStale) writeSessionListCache(`${STARTUP_CACHE_PREFIX}${apiBase}`, { status: merged, desktopManaged: startupDesktopRef.current }); else if (merged === "at-risk" || merged === "error") clearSessionListCache(`${STARTUP_CACHE_PREFIX}${apiBase}`); } }, [settingsPoll.data, apiBase]); // Cache the merged UI state, including preference saves and successful applies. // Raw GET settings cannot replace the local application receipt on a revisit. useEffect(() => { if (!settings) return; const prev = readSessionListCache(controlsCacheKey(apiBase)) ?? {}; writeSessionListCache(controlsCacheKey(apiBase), { ...prev, settings }); }, [settings, apiBase]); useEffect(() => { if (usagePoll.data !== undefined) { setUsage30d(usagePoll.data); writeSessionListCache(`${USAGE_CACHE_PREFIX}${apiBase}`, usagePoll.data); } }, [usagePoll.data, apiBase]); useEffect(() => { if (diagnosticsPoll.data) setProjectConfigWarnings(diagnosticsPoll.data); }, [diagnosticsPoll.data]); useEffect(() => { if (modelsPoll.data) setModels(modelsPoll.data); setModelsLoading(modelsPoll.loading); }, [modelsPoll.data, modelsPoll.loading]); /* eslint-enable react-hooks/set-state-in-effect */ /* oxlint-enable react/react-compiler */ useEffect(() => () => { settingsRequestEpochRef.current += 1; shadowCallRequestEpochRef.current += 1; }, []); const updatePoll = useKeyedClientResource( updateJob?.id && updateJob.restart ? `update-job:${apiBase}:${updateJob.id}` : `update-job:idle:${apiBase}`, [apiBase, updateJob?.id, updateJob?.restart, updateJob?.latestVersion], async (signal) => { if (!updateJob?.id || !updateJob.restart) return { reconnecting: false as const }; const targetVersion = updateJob.latestVersion; try { const res = await fetch(`${apiBase}/api/update/status?jobId=${encodeURIComponent(updateJob.id)}`, { signal }); const statusData = await requireJson<{ job?: UpdateJob }>(res); if (statusData.job) { if (statusData.job.status === "failed") return { job: statusData.job, reconnecting: false as const }; if (targetVersion) { try { // The management health route answers on every dashboard ingress; a hub's management // ingress denies /healthz (#8506). A 401 after the restart is renewed by api.ts. const healthRes = await fetch(`${apiBase}/api/system/health`, { cache: "no-store", signal }); const healthData = await requireJson(healthRes); if (healthData.version === targetVersion) { return { job: statusData.job, reconnecting: false as const, reload: true as const }; } } catch { return { job: statusData.job, reconnecting: true as const }; } } return { job: statusData.job, reconnecting: false as const }; } } catch { return { reconnecting: true as const }; } return { reconnecting: false as const }; }, { pollMs: 1500, enabled: !!(updateJob?.id && updateJob.restart), // This poll exists to notice a restarted server coming back. Pausing it while the // tab is hidden is exactly when it would be missed, so it opts out of the gate. pauseWhenHidden: false, }, ); /* oxlint-disable react/react-compiler -- mirror update poll snapshot into mutable dashboard UI state */ useEffect(() => { const data = updatePoll.data; if (!data) return; if ("job" in data && data.job) setUpdateJob(data.job); setReconnecting(data.reconnecting); if ("reload" in data && data.reload) window.location.reload(); }, [updatePoll.data]); /* oxlint-enable react/react-compiler */ const grouped = useMemo(() => groupDashboardModels(models), [models]); const filteredGroups = useMemo(() => { const q = modelQuery.trim().toLowerCase(); if (!q) return grouped; const out: Array<[string, ModelInfo[]]> = []; for (const [provider, rows] of grouped) { const hits = rows.filter(m => m.id.toLowerCase().includes(q) || provider.toLowerCase().includes(q)); if (hits.length > 0) out.push([provider, hits]); } return out; }, [grouped, modelQuery]); const sidecarModels = useMemo(() => { // Server-computed runnable set when present (#2188); legacy union otherwise. // The shared SidecarSetting type admits vision's "routed", which the // web-search picker cannot carry — narrow it away for this card. const webBackend = sidecar?.webSearch?.backend; return webSearchModelOptionsForPicker( sidecar?.webSearchModels, models, sidecar?.webSearch?.model, webBackend === "routed" ? undefined : webBackend, ); }, [models, sidecar?.webSearchModels, sidecar?.webSearch]); const visionModels = useMemo( () => visionModelOptions(sidecar?.visionModels, models, sidecar?.vision?.model, sidecar?.vision?.backend), [sidecar?.visionModels, models, sidecar?.vision], ); const saveSidecar = async (patch: SidecarPatch) => { if (!sidecar || sidecarSaving) return; patch = sidecarPatchForSave(sidecar, patch); const previous = sidecar; const next = { webSearch: mergeSidecarSetting(sidecar.webSearch, patch.webSearch), vision: mergeSidecarSetting(sidecar.vision, patch.vision), ...(sidecar.visionModels ? { visionModels: sidecar.visionModels } : {}), ...(sidecar.webSearchModels ? { webSearchModels: sidecar.webSearchModels } : {}), }; setSidecarSaving(true); setSidecar(next); try { const res = await fetch(`${apiBase}/api/sidecar-settings`, { method: "PUT", headers: { "Content-Type": "application/json" }, body: JSON.stringify(patch), }); const data = await requireJson(res, "save failed"); // The Codex-side write is a separate outcome from the stored switch: it can be refused // while the setting is saved, and the card has to say so instead of implying it happened. // A save that did not move this switch answers `not_requested` about a file it never // touched, so it must not clear an earlier failure. setSidecarCodexApply(previousReport => nextSidecarCodexApply(previousReport, data.codexWebSearch)); setSidecar({ webSearch: data.webSearch, vision: data.vision, ...(data.visionModels ? { visionModels: data.visionModels } : {}), ...(data.webSearchModels ? { webSearchModels: data.webSearchModels } : {}), }); const prev = readSessionListCache(controlsCacheKey(apiBase)) ?? {}; writeSessionListCache(controlsCacheKey(apiBase), { ...prev, sidecar: { webSearch: data.webSearch, vision: data.vision, ...(data.visionModels ? { visionModels: data.visionModels } : {}), ...(data.webSearchModels ? { webSearchModels: data.webSearchModels } : {}), }, }); } catch { setSidecar(previous); // The request failed before any answer existed, so it says nothing about the Codex file: // an outstanding report stays until a write that ran or a successful sync settles it. } finally { setSidecarSaving(false); } }; async function saveShadowCall(patch: Partial) { if (!shadowCall || shadowCallSaving) return; const previous = shadowCall; const updated = { ...shadowCall, ...patch }; setShadowCallSaving(true); setShadowCallError(null); shadowCallMutationInFlightRef.current = true; setShadowCall(updated); try { const res = await fetch(`${apiBase}/api/shadow-call-settings`, { method: "PUT", headers: { "Content-Type": "application/json" }, body: JSON.stringify(patch), }); if (!res.ok) { setShadowCall(previous); const body = await res.json().catch(() => null) as { code?: unknown } | null; setShadowCallError(t(shadowCallSaveErrorKey(body?.code))); return; } shadowCallMutationEpochRef.current += 1; } catch { setShadowCall(previous); setShadowCallError(t("models.networkError")); } finally { shadowCallMutationInFlightRef.current = false; setShadowCallSaving(false); } } const writeMaMode = async ( mode: MaMode, acknowledgeAdvisory = false, acknowledgePlaintext = false, ) => { setMaBusy(true); setMaError(null); try { const payload: { multiAgentMode: MaMode; multiAgentSurfaceAdvisoryAcknowledged?: true; plaintextAgentMessagesAcknowledged?: true; } = { multiAgentMode: mode }; // One request, so the recommended answer cannot leave the notice raised on a mode it applied. if (acknowledgeAdvisory) payload.multiAgentSurfaceAdvisoryAcknowledged = true; // 'v2-plaintext' is rejected without the dedicated consent; the checkbox in // PlaintextV2ConsentModal is what proved it before this write ran. if (acknowledgePlaintext) payload.plaintextAgentMessagesAcknowledged = true; const r = await fetch(`${apiBase}/api/v2`, { method: "PUT", headers: { "Content-Type": "application/json" }, body: JSON.stringify(payload), }); if (r.ok) { if (mode === "v2-plaintext") { // Claim the mode only when the receipt proves it landed: the catalog pin is // V2 while the global override must stay OFF. A 200 without those fields // means an older or replaced proxy answered — keep the stored selection. let confirmed = false; try { confirmed = confirmsPlaintextV2Mode(await r.json()); } catch { /* unreadable receipt */ } if (!confirmed) { setMaError(t("models.saveFailed")); return; } // A confirmed write proves this apiBase supports the mode until the next poll. setMaPlaintextSupportState({ supports: true, apiBase }); } setMaMode(mode); if (acknowledgeAdvisory) setMaAdvisoryAnsweredFor(apiBase); writeSessionListCache(`${MA_MODE_CACHE_PREFIX}${apiBase}`, mode); } else { let message = t("dash.maSwitchFailed", { status: String(r.status) }); try { const body = await r.json() as { error?: string; message?: string }; message = (typeof body.error === "string" && body.error) || (typeof body.message === "string" && body.message) || message; } catch { /* non-JSON error body */ } setMaError(message); } } catch (e) { setMaError(e instanceof Error ? e.message : t("dash.maNetworkError")); } finally { setMaBusy(false); } }; const switchMaMode = async (mode: MaMode) => { if (maBusy || maMode === mode) return; // v1 applies immediately: confirming a move toward the safe default would be noise. base // and v2 both put ChatGPT-native parents on the surface whose task a routed child cannot // read, so they wait for an answer; 'v2-plaintext' waits for its dedicated consent modal. if (mode !== "v1") { setPendingMaModeState({ mode, apiBase }); return; } await writeMaMode("v1"); }; /** Answer the advisory without moving the mode. Failure just means it asks again. */ const acknowledgeMaAdvisory = async () => { // Answered for this page load either way: the operator did answer. If the write did not // land the runtime raises the notice again next load, so a failure costs one more prompt // rather than a lost setting — but say so instead of swallowing it. setMaAdvisoryAnsweredFor(apiBase); try { const r = await fetch(`${apiBase}/api/v2`, { method: "PUT", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ multiAgentSurfaceAdvisoryAcknowledged: true }), }); if (!r.ok) setMaError(t("dash.maSwitchFailed", { status: String(r.status) })); } catch (e) { setMaError(e instanceof Error ? e.message : t("dash.maNetworkError")); } }; /** The ghost button: apply the mode that was selected, or keep the stored one. */ const keepMaMode = async () => { const pending = pendingMaMode; setPendingMaModeState(null); // 'v2-plaintext' writes with its dedicated acknowledgement in a single PUT — the mode // itself records the consent, and the runtime rejects the write without the field. if (pending === "v2-plaintext") { await writeMaMode(pending, false, true); return; } // A selection answers the advisory too. Without that, continuing to base or v2 leaves the // notice raised and the next poll asks the same question the operator just answered. // Unconditionally, not gated on the poll's current `required`: that projection goes false // while the mode is v1 without the version having been stored, so a later confirmed base or // v2 would skip the acknowledgement and be asked all over again. if (pending) { await writeMaMode(pending, true); return; } await acknowledgeMaAdvisory(); }; /** The primary button: v1, and the advisory answered in the same request when it is raised. */ const chooseMaV1 = async () => { setPendingMaModeState(null); if (maMode === "v1") { await acknowledgeMaAdvisory(); return; } await writeMaMode("v1", true); }; /** Escape or backdrop: abandon a selection, or leave the advisory unanswered for next load. */ const dismissMaSurfaceDialog = () => { if (pendingMaMode) { setPendingMaModeState(null); return; } setMaAdvisoryAnsweredFor(apiBase); }; const maAdvisoryOpen = !pendingMaMode && !maAdvisoryAnswered && maAdvisory?.required === true; const saveInjection = async (patch: { multiAgentGuidanceEnabled?: boolean; syncCodexSubagentDefaults?: boolean; model?: string | null; effort?: string | null; }) => { if (injectionSaving) return; setInjectionSaving(true); try { const res = await fetch(`${apiBase}/api/injection-model`, { method: "PUT", headers: { "Content-Type": "application/json" }, body: JSON.stringify(patch), }); if (!res.ok) throw new Error("injection save failed"); const getRes = await fetch(`${apiBase}/api/injection-model`); const data = await requireJson<{ multiAgentGuidanceEnabled?: boolean; syncCodexSubagentDefaults?: boolean; model?: string | null; effort?: string | null; efforts?: string[]; available?: Array<{ provider: string; model: string; namespaced: string }>; }>(getRes); const normalized = normalizeInjectionSelection(data); setMultiAgentGuidanceEnabled(normalized.multiAgentGuidanceEnabled); setSyncCodexSubagentDefaults(normalized.syncCodexSubagentDefaults); setInjectionModel(normalized.injectionModel); setInjectionEffort(normalized.injectionEffort); if (Array.isArray(data.efforts)) setInjectionEfforts(data.efforts); if (Array.isArray(data.available)) setInjectionAvailable(data.available); } catch { /* keep the last committed UI state */ } finally { setInjectionSaving(false); } }; const toggleCodexSetting = async (key: CodexPreference) => { if (!settings || settingsSaving || syncing) return; const next = !(settings[key] ?? (key === "codexAutoStart")); settingsMutationInFlightRef.current = true; dispatchSettings({ type: "save-started", key, value: next }); try { const res = await fetch(`${apiBase}/api/settings`, { method: "PUT", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ [key]: next }), }); const data = await requireJson(res, "save failed"); settingsMutationEpochRef.current += 1; dispatchSettings({ type: "save-succeeded", key, settings: data }); if (key === "codexDesktopAuthless" || key === "codexClientCompaction") await runSync(); } catch { dispatchSettings({ type: "save-failed" }); setError(true); } finally { settingsMutationInFlightRef.current = false; dispatchSettings({ type: "save-finished" }); } }; const toggleCodexAutoStart = () => toggleCodexSetting("codexAutoStart"); const toggleCodexDesktopAuthless = () => toggleCodexSetting("codexDesktopAuthless"); const toggleCodexClientCompaction = () => toggleCodexSetting("codexClientCompaction"); // Clears the sync result/error in this hook. The dashboard toast owns its own dismissal // timer but must publish the dismissal here: syncResult/syncError live above the dashboard // tabs, so a component-local flag alone would let a stale result remount as a fresh toast // after the Overview panel unmounts and comes back. const clearSyncFeedback = useCallback(() => { setSyncResult(null); setSyncError(null); }, []); const runSync = async () => { if (syncing) return; setSyncing(true); setSyncResult(null); setSyncError(null); try { const res = await fetch(`${apiBase}/api/sync`, { method: "POST" }); const data = await requireJson(res, "sync failed"); setSyncResult(data); if (data.ok && data.status === "applied") { dispatchSettings({ type: "applied" }); // A successful sync rewrites the Codex config from the stored settings, which is exactly // the write the sidecar card was still warning about. setSidecarCodexApply(undefined); } if (data.projectConfigGrouped) setProjectConfigWarnings(data.projectConfigGrouped); } catch (err) { setSyncError(err instanceof Error ? err.message : String(err)); } finally { setSyncing(false); } }; const fetchUpdateCheck = async (channel: UpdateChannel, resetRetry = false) => { if (resetRetry) updateRetryRef.current = 0; if (updateRetryTimerRef.current !== null) { window.clearTimeout(updateRetryTimerRef.current); updateRetryTimerRef.current = null; } const requestEpoch = ++updateRequestEpochRef.current; setUpdateLoading(true); setUpdateError(null); setUpdateCheck(null); try { const res = await fetch(`${apiBase}/api/update/check?tag=${channel}`); const check = await requireJson(res, "update check failed"); if (requestEpoch !== updateRequestEpochRef.current) return; setUpdateCheck(check); if ( check.reason === "latest_unavailable" && updateRetryRef.current < UPDATE_CHECK_MAX_AUTO_RETRIES ) { const retry = ++updateRetryRef.current; // Keep loading through scheduled retries — do not clear here. updateRetryTimerRef.current = window.setTimeout(() => { if (requestEpoch !== updateRequestEpochRef.current) return; updateRetryTimerRef.current = null; void fetchUpdateCheck(channel); }, UPDATE_CHECK_RETRY_BASE_MS * retry); return; } if (check.reason !== "latest_unavailable") updateRetryRef.current = 0; setUpdateLoading(false); } catch (err) { if (requestEpoch !== updateRequestEpochRef.current) return; setUpdateError(err instanceof Error ? err.message : String(err)); setUpdateLoading(false); } }; const closeUpdateDialog = () => { updateRequestEpochRef.current += 1; if (updateRetryTimerRef.current !== null) { window.clearTimeout(updateRetryTimerRef.current); updateRetryTimerRef.current = null; } setUpdateLoading(false); setUpdateOpen(false); }; const openUpdateDialog = () => { if (openDesktopUpdatePage()) return; const channel = defaultUpdateChannel(health?.version); setUpdateChannel(channel); setUpdateRestart(true); setUpdateOpen(true); void fetchUpdateCheck(channel, true); }; const changeUpdateChannel = (channel: UpdateChannel) => { setUpdateChannel(channel); void fetchUpdateCheck(channel, true); }; /** * Sidebar update button deep link (`#dashboard/update`). Opening happens straight from * the hashchange listener — an external event, not a render-time effect — so no * intermediate state or ref hand-off is needed. The hash is normalized back to * `#dashboard` before opening, so Back never re-triggers the dialog. * * `openUpdateDialogRef` keeps the listener registration stable while still calling the * latest handler; it is only ever written inside an effect. */ const openUpdateDialogRef = useRef(openUpdateDialog); useEffect(() => { openUpdateDialogRef.current = openUpdateDialog; }); useEffect(() => { const consume = () => { if (!hashRequestsUpdateDialog()) return; replaceHash("dashboard"); openUpdateDialogRef.current(); }; // A cold load straight onto the deep link: defer past mount so the open is not a // render-phase side effect. const initial = hashRequestsUpdateDialog() ? window.setTimeout(consume, 0) : null; window.addEventListener("hashchange", consume); return () => { if (initial !== null) window.clearTimeout(initial); window.removeEventListener("hashchange", consume); }; }, []); const runUpdate = async () => { if (!updateCheck?.canUpdate) return; setUpdateError(null); try { const res = await fetch(`${apiBase}/api/update/run`, { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ tag: updateChannel, restart: updateRestart }), }); const data = await requireJson<{ job?: UpdateJob }>(res, "update failed to start"); if (!data.job) throw new Error("update failed to start"); setUpdateJob(data.job); setReconnecting(false); closeUpdateDialog(); } catch (err) { setUpdateError(err instanceof Error ? err.message : String(err)); } }; return { apiBase, locale, t, selectedSection, setSelectedSection, modelQuery, setModelQuery, expandedProviders, setExpandedProviders, health, startupHealth, providers, models, settings, sidecar, shadowCall, usage30d, usageLoading: usagePoll.loading && !usage30d, healthLoading: overviewPoll.loading && !health, sidecarSaving, shadowCallSaving, shadowCallError, modelsLoading, settingsSaving, syncing, maMode, maModeResolved, maBusy, setMaHelpOpen, maHelpOpen, maError, maSupportsPlaintextV2, maAdvisory, maAdvisoryOpen, pendingMaMode, keepMaMode, chooseMaV1, dismissMaSurfaceDialog, effortCapHelpOpen, setEffortCapHelpOpen, shadowCallHelpOpen, setShadowCallHelpOpen, injectionModel, injectionEffort, injectionEfforts, injectionAvailable, injectionSaving, multiAgentGuidanceEnabled, syncCodexSubagentDefaults, saveInjection, effortCap, subagentEffortCap, effortCapSaving, setEffortCap, setSubagentEffortCap, setEffortCapSaving, syncResult, syncError, projectConfigWarnings, updateOpen, updateChannel, setUpdateRestart, updateRestart, updateLoading, updateCheck, updateError, updateJob, reconnecting, error: error || overviewSurface.showError, connectionFailure: overviewPoll.data?.failure ?? (overviewSurface.showError ? "unavailable" : undefined), refreshDashboard: overviewPoll.refresh, effortCapHelpTriggerRef, updateTriggerRef, maHelpTriggerRef, shadowCallHelpTriggerRef, effortCapHelpDialogRef, updateDialogRef, maHelpDialogRef, shadowCallHelpDialogRef, filteredGroups, sidecarModels, visionModels, sidecarCodexApply, saveSidecar, saveShadowCall, switchMaMode, toggleCodexAutoStart, toggleCodexDesktopAuthless, toggleCodexClientCompaction, runSync, clearSyncFeedback, fetchUpdateCheck, closeUpdateDialog, openUpdateDialog, changeUpdateChannel, runUpdate, }; }