Repository navigation
fix(settings): expose the Advisor default setting and wire it to new chats (fixes #323) #571
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,69 @@ | ||
| import "../tests/setup-dom.mjs"; | ||
| import assert from "node:assert/strict"; | ||
| import test from "node:test"; | ||
| import { act, createElement } from "react"; | ||
| import { createRoot } from "react-dom/client"; | ||
| import { SettingsConfig } from "./SettingsConfig"; | ||
|
|
||
| const noop = () => {}; | ||
|
|
||
| /** Render the real Agent & Intelligence panel with a stubbed settings API. */ | ||
| async function renderIntelligenceTab(t, { advisorEnabled = true } = {}) { | ||
| const puts = []; | ||
| const originalFetch = globalThis.fetch; | ||
| globalThis.fetch = async (url, options = {}) => { | ||
| if (String(url) === "/api/omp-settings" && (options.method ?? "GET") === "PUT") { | ||
| const settings = JSON.parse(options.body).settings; | ||
| puts.push(settings); | ||
| return Response.json({ success: true, settings }); | ||
| } | ||
| if (String(url) === "/api/omp-settings") return Response.json({ settings: { advisor: { enabled: advisorEnabled } } }); | ||
| return Response.json({}); | ||
| }; | ||
| const container = document.createElement("div"); | ||
| document.body.append(container); | ||
| const root = createRoot(container); | ||
| t.after(() => { | ||
| act(() => root.unmount()); | ||
| container.remove(); | ||
| globalThis.fetch = originalFetch; | ||
| }); | ||
| await act(async () => { | ||
| root.render(createElement(SettingsConfig, { | ||
| activeTab: "intelligence", | ||
| toolCallsDefaultCollapsed: false, onToolCallsDefaultCollapsedChange: noop, | ||
| hideToolActivity: false, onHideToolActivityChange: noop, | ||
| expandThinkingByDefault: false, onExpandThinkingByDefaultChange: noop, onHideThinkingBlockChange: noop, | ||
| providerUsageVisible: false, onProviderUsageVisibleChange: noop, | ||
| scopeNativeSelectAll: false, onScopeNativeSelectAllChange: noop, | ||
| openUrlAutomatically: false, onOpenUrlAutomaticallyChange: noop, | ||
| cwd: null, remoteHost: false, sessionId: null, | ||
| onModelsSaved: noop, onPluginsReloaded: noop, | ||
| appUpdate: null, onRefreshAppUpdate: async () => null, | ||
| onRequestAppUpdate: noop, onSelectTab: noop, onClose: noop, | ||
| })); | ||
| }); | ||
| await act(() => new Promise((resolve) => setTimeout(resolve, 20))); | ||
| return { container, puts }; | ||
| } | ||
|
|
||
| const advisorSwitch = (container) => container.querySelector('[data-search-id="advisor-enabled"] button[role="switch"]'); | ||
|
|
||
| test("Agent & Intelligence exposes the Advisor default and persists it", async (t) => { | ||
| const { container, puts } = await renderIntelligenceTab(t, { advisorEnabled: true }); | ||
| assert.ok(container.textContent.includes("Advisor"), "no Advisor section in the intelligence tab"); | ||
| const toggle = advisorSwitch(container); | ||
| assert.ok(toggle, "no Advisor toggle"); | ||
| assert.equal(toggle.getAttribute("aria-checked"), "true"); | ||
| act(() => toggle.click()); | ||
| await act(() => new Promise((resolve) => setTimeout(resolve, 50))); | ||
| assert.ok(puts.length >= 1, "toggling Advisor sent no PUT"); | ||
| assert.equal(puts.at(-1).advisor.enabled, false); | ||
| }); | ||
|
|
||
| test("the Advisor toggle reflects a disabled global default", async (t) => { | ||
| const { container } = await renderIntelligenceTab(t, { advisorEnabled: false }); | ||
| const toggle = advisorSwitch(container); | ||
| assert.ok(toggle, "no Advisor toggle"); | ||
| assert.equal(toggle.getAttribute("aria-checked"), "false"); | ||
| }); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -235,7 +235,10 @@ export interface UseAgentSessionOptions { | |
| /** Frozen ensure_session configuration retained by the existing navigation view. */ | ||
| newSessionInitialization?: string; | ||
| onNewSessionInitialization?: (body: string) => void; | ||
| advisorEnabled?: boolean; | ||
| /** Global omp default (`advisor.enabled` in settings): brand-new chats start | ||
| * with this until the user toggles the per-chat Advisor switch. Existing | ||
| * sessions keep their own stored choice. */ | ||
| advisorDefault?: boolean; | ||
| onAgentEnd?: () => void; | ||
| onSessionCreated?: (session: SessionInfo) => void; | ||
| onSessionForked?: (newSessionId: string) => void; | ||
|
|
@@ -322,7 +325,7 @@ function reconcileConfirmedLocalOutbox(sessionId: string, localEntries: LocalOut | |
|
|
||
| export function useAgentSession(opts: UseAgentSessionOptions) { | ||
| const { | ||
| session, newSessionCwd, newSessionHostId, newSessionCreationId, newSessionPoolId, newSessionProjectId, newSessionInitialization, onNewSessionInitialization, onAgentEnd, onSessionCreated, onSessionForked, onSessionMoved, | ||
| session, newSessionCwd, newSessionHostId, newSessionCreationId, newSessionPoolId, newSessionProjectId, newSessionInitialization, onNewSessionInitialization, advisorDefault, onAgentEnd, onSessionCreated, onSessionForked, onSessionMoved, | ||
| modelsRefreshKey, onBranchDataChange, onSystemPromptChange, onSystemPromptLoaderChange, onSessionStatsPanelOpen, | ||
| onOpenFile, onOpenUrl, | ||
| } = opts; | ||
|
|
@@ -590,9 +593,14 @@ export function useAgentSession(opts: UseAgentSessionOptions) { | |
| const [activeGoal, setActiveGoal] = useState<ActiveGoal | null>(null); | ||
| const [activePlan, setActivePlan] = useState<ActivePlan | null>(null); | ||
| const [advisorActiveAt, setAdvisorActiveAt] = useState(0); | ||
| // Advisor is a per-chat toggle (composer Sparkles + /advisor command), not a | ||
| // global setting: each session remembers its own choice in localStorage. | ||
| const [advisorEnabled, setAdvisorEnabled] = useState(false); | ||
| // Advisor is a per-chat toggle (composer Sparkles + /advisor command): each | ||
| // session remembers its own choice in localStorage. Brand-new chats start | ||
| // from the global `advisor.enabled` default until the user toggles it. | ||
| const [advisorEnabled, setAdvisorEnabled] = useState(advisorDefault ?? false); | ||
| // A toggle flipped on a brand-new chat (no id yet, so nothing persisted) | ||
| // must survive the global default arriving late; the load effect below | ||
| // only applies the default while this is false. | ||
| const advisorTouchedForNewRef = useRef(false); | ||
| const activeSubagentCount = subagents.filter((subagent) => subagent.source !== "history" && subagent.status === "started").length; | ||
|
|
||
| const eventSourceRef = useRef<EventStream | null>(null); | ||
|
|
@@ -942,14 +950,15 @@ export function useAgentSession(opts: UseAgentSessionOptions) { | |
| return null; | ||
| }, [todoPhases]); | ||
| // Load the per-session advisor choice on session switch; brand-new chats | ||
| // start disabled until the user toggles the composer icon. | ||
| // start from the global default until the user toggles the composer icon. | ||
| useEffect(() => { | ||
| const sid = session?.id; | ||
| if (!sid) { | ||
| setAdvisorEnabled(false); | ||
| setAdvisorEnabled((prev) => (advisorTouchedForNewRef.current ? prev : (advisorDefault ?? false))); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Stop applying defaults once session initialization is captured. If Freeze default application when initialization starts, not only when Based on learnings, related asynchronous state can update in separate renders.
|
||
| setSessionToolPresetState(null); | ||
| return; | ||
| } | ||
| advisorTouchedForNewRef.current = false; | ||
| let stored = false; | ||
| try { | ||
| stored = localStorage.getItem(`omp-advisor-enabled:${sid}`) === "true"; | ||
|
|
@@ -962,12 +971,15 @@ export function useAgentSession(opts: UseAgentSessionOptions) { | |
| setSessionAdvisorSpawn(sid, stored); | ||
| // The preset this session was spawned with, if this browser recorded it. | ||
| setSessionToolPresetState(getSessionToolPreset(sid)); | ||
| }, [session?.id]); | ||
| }, [session?.id, advisorDefault]); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Separate default synchronization from existing-session hydration. If an existing session stores Load stored choices only when the session ID changes. Use a separate effect for untouched new-chat defaults.
|
||
|
|
||
| const handleAdvisorChange = useCallback((enabled: boolean) => { | ||
| setAdvisorEnabled(enabled); | ||
| const sid = sessionIdRef.current; | ||
| if (!sid) return; | ||
| if (!sid) { | ||
| advisorTouchedForNewRef.current = true; | ||
| return; | ||
| } | ||
| setSessionAdvisorSpawn(sid, enabled); | ||
| try { | ||
| localStorage.setItem(`omp-advisor-enabled:${sid}`, String(enabled)); | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Refresh
advisorDefaultafter Settings saves it.If the user enables Advisor in Settings and returns to chat,
advisorDefaultretains its previous value. This loader runs only on mount or window focus. Settings saves do not update this state, and closing Settings does not reload it. New chats therefore receive the old default until the window regains focus or the page reloads.Update this state after a successful settings save, or reload it when Settings closes. Preserve stored choices for existing sessions.