From bf526d4d13908dd700a3ca455b85c50b88c6661c Mon Sep 17 00:00:00 2001 From: K <61059522+kahme247@users.noreply.github.com> Date: Mon, 23 Nov 2026 11:03:29 +0200 Subject: [PATCH 1/2] feat(ui): show the active toolset badge and restricted-session banner (refs #356) --- components/ChatInput.test.mjs | 94 ++++++++++++++++++++++++++ components/ChatInput.tsx | 100 +++++++++++++++++++++++++++- components/ChatWindow.tsx | 4 +- hooks/useAgentSession.ts | 30 +++++++-- lib/i18n/locales/en.json | 5 ++ lib/tool-preset-preference.test.mjs | 30 ++++++++- lib/tool-preset-preference.ts | 25 +++++++ lib/tool-presets.ts | 13 ++++ 8 files changed, 293 insertions(+), 8 deletions(-) diff --git a/components/ChatInput.test.mjs b/components/ChatInput.test.mjs index 29998b77e..f5847700c 100644 --- a/components/ChatInput.test.mjs +++ b/components/ChatInput.test.mjs @@ -447,3 +447,97 @@ test("renders retry banner with countdown when delayMs and startedAt are provide assert.match(html, /Retrying \(2\/5\) in 10s…/); }); + +/** The active-toolset badge (#356 item 2): plain text, like the plus menu's raw preset values. */ +function renderToolset(props = {}) { + const html = renderToStaticMarkup( + React.createElement(ChatInput, { + async onSend() { return { status: "accepted" }; }, + onToolPresetChange() {}, + isStreaming: false, + ...props, + }), + ); + const dom = new JSDOM(html); + const buttons = [...dom.window.document.querySelectorAll("button")]; + const badge = buttons.find((button) => button.textContent.startsWith("tools:")); + const statuses = [...dom.window.document.querySelectorAll('[role="status"]')]; + const banner = statuses.find((node) => /only \d+ tools|no tools enabled|restrictedToolsBanner/.test(node.textContent)); + return { dom, badge, banner }; +} + +test("toolset badge shows the preset and its tool count next to the context gauge", () => { + for (const [preset, label] of [["full", "tools: full"], ["default", "tools: default (4)"], ["none", "tools: none (0)"]]) { + const { dom, badge, banner } = renderToolset({ toolPreset: preset }); + try { + assert.equal(badge?.textContent, label); + assert.equal(banner, undefined); + } finally { + dom.window.close(); + } + } +}); + +test("toolset badge shows the session's spawn preset, not the preference for new sessions", () => { + const { dom, badge } = renderToolset({ toolPreset: "default", sessionToolPreset: "full" }); + try { + assert.equal(badge?.textContent, "tools: full"); + } finally { + dom.window.close(); + } + const recorded = renderToolset({ toolPreset: "full", sessionToolPreset: "default" }); + try { + assert.equal(recorded.badge?.textContent, "tools: default (4)"); + } finally { + recorded.dom.window.close(); + } +}); + +test("no toolset badge without a preset change handler", () => { + const { dom, badge } = renderToolset({ toolPreset: "default", onToolPresetChange: undefined }); + try { + assert.equal(badge, undefined); + } finally { + dom.window.close(); + } +}); + +test("restricted sessions get a persistent banner with a restore action", () => { + const { dom, banner } = renderToolset({ sessionToolPreset: "default", onToolPresetChange() {} }); + try { + assert.ok(banner, "a session spawned with the default preset must show the banner"); + assert.match(banner.textContent, /(This session runs with only 4 tools|chatInput\.restrictedToolsBanner)/); + assert.match(banner.textContent, /(Restore full tools|chatInput\.restoreFullTools)/); + } finally { + dom.window.close(); + } + const none = renderToolset({ sessionToolPreset: "none", onToolPresetChange() {} }); + try { + assert.ok(none.banner, "a session spawned with no tools must show the banner"); + assert.match(none.banner.textContent, /(no tools enabled|chatInput\.restrictedToolsBannerNone)/); + } finally { + none.dom.window.close(); + } +}); + +test("no restricted banner for full sessions, unknown sessions, or a new full chat", () => { + for (const props of [ + { sessionToolPreset: "full" }, + // Unknown existing session: the preference is for new sessions only, so no claim is made about this one. + { toolPreset: "default" }, + { isNewSession: true, toolPreset: "full" }, + ]) { + const { dom, banner } = renderToolset(props); + try { + assert.equal(banner, undefined, JSON.stringify(props)); + } finally { + dom.window.close(); + } + } + const fresh = renderToolset({ isNewSession: true, toolPreset: "default" }); + try { + assert.ok(fresh.banner, "a new chat that would spawn restricted must show the banner"); + } finally { + fresh.dom.window.close(); + } +}); diff --git a/components/ChatInput.tsx b/components/ChatInput.tsx index fbec985f1..12fa48e51 100644 --- a/components/ChatInput.tsx +++ b/components/ChatInput.tsx @@ -90,6 +90,7 @@ import { GhostMirror } from "@/components/GhostMirror"; import { useI18n } from "@/lib/i18n"; import { selectableThinkingLevels, DEFAULT_THINKING_LEVELS } from "@/lib/thinking-levels"; import type { ToolPreset } from "@/lib/tool-presets"; +import { getToolPresetToolCount } from "@/lib/tool-presets"; function ThinkingGlyph({ level }: { level: string }) { if (!DEFAULT_THINKING_LEVELS.includes(level)) return null; @@ -221,6 +222,13 @@ interface Props { /** Browser-side tool preset, applied when spawning NEW sessions. */ toolPreset?: ToolPreset; onToolPresetChange?: (preset: ToolPreset) => void; + /** Tool preset the current session was spawned with (#356 item 2): null + * when unknown — a session from before this was recorded, or from + * another browser or process. The badge then shows the preset for new + * sessions instead, and no banner is shown. */ + sessionToolPreset?: ToolPreset | null; + /** True for a brand-new chat: the preset shown is the one the next spawn will use. */ + isNewSession?: boolean; /** Display name for the current model when the catalog does not know it. */ modelNameOverride?: string | null; retryInfo?: { attempt: number; maxAttempts: number; delayMs?: number; startedAt?: number; errorMessage?: string } | null; @@ -366,7 +374,7 @@ export const ChatInput = memo(forwardRef(function ChatIn onSend, onPredictWord, onPredictWordFeedback, onAbort, onSteer, onFollowUp, isStreaming, model, isAutoModelSelection, modelNames, modelList, modelError, modelsLoading, onModelChange, fastModeEnabled, fastModeActive, usageLimit, fastModeSupported, onFastModeChange, slowModeSupported, slowModeEnabled, slowModeScope, onSlowModeChange, onAbortCompaction, isCompacting, compactResult, thinkingLevel, onThinkingLevelChange, availableThinkingLevels, thinkingLevelMap, modelNameOverride, - toolPreset, onToolPresetChange, + toolPreset, onToolPresetChange, sessionToolPreset, isNewSession, retryInfo, queuedMessages, inputHistory = [], onAbortRetry, slashCommands, slashCommandsLoading, onLoadSlashCommands, onBuiltinCommand, @@ -2281,6 +2289,15 @@ export const ChatInput = memo(forwardRef(function ChatIn t("chatInput.compactContext"), ].filter(Boolean).join(" · ") : t("chatInput.compactContext"); + // The session's active toolset (#356 item 2): what this session was spawned + // with when known, else the preset a new session would spawn with. A + // restricted toolset is loud: a badge by the context gauge, and a persistent + // banner while the session is known to run restricted. + const activeToolPreset = sessionToolPreset ?? toolPreset ?? "full"; + const toolPresetKnown = sessionToolPreset != null || isNewSession === true; + const activeToolCount = getToolPresetToolCount(activeToolPreset); + const toolsetBadgeLabel = `tools: ${activeToolPreset}${activeToolCount === null ? "" : ` (${activeToolCount})`}`; + const restrictedToolPreset = toolPresetKnown && activeToolPreset !== "full" ? activeToolPreset : null; const thinkingDisplayLabel = (() => { const lvl = thinkingLevel ?? "auto"; if (lvl === "auto" || !thinkingLevelMap) return lvl; @@ -3208,6 +3225,51 @@ export const ChatInput = memo(forwardRef(function ChatIn )} )} + {/* Restricted toolset banner (#356 item 2): persistent while this + session is known to run restricted. Changing the preset only + affects new sessions (see the toolPresetNotice); this session + keeps its tools, so the banner stays until a new session starts. */} + {restrictedToolPreset && onToolPresetChange && ( +
+ + {restrictedToolPreset === "none" + ? t("chatInput.restrictedToolsBannerNone") + : t("chatInput.restrictedToolsBanner", { count: getToolPresetToolCount(restrictedToolPreset) ?? 0 })} + + +
+ )} {/* Live agent status bar — attached to composer's top edge */} {statusText && (
(function ChatIn )} + {/* Active toolset badge (#356 item 2): what this session runs + with, next to the model/context gauge. Select it to change + the preset (applies to new sessions). */} + {onToolPresetChange && ( + + )} + {/* Desktop keeps the control in the composer; mobile uses the header mount. */} {onCompact && (
diff --git a/components/ChatWindow.tsx b/components/ChatWindow.tsx index 763c6e851..78ad8bd7c 100644 --- a/components/ChatWindow.tsx +++ b/components/ChatWindow.tsx @@ -448,7 +448,7 @@ export function ChatWindow({ session, newSessionCwd, newSessionHostId, newSessio loading, error, messages, entryIds, showPreCompactionHistory, streamState, agentRunning, bashRunning, pendingBash, deferredBash, modelNames, modelList, modelsLoading, modelError, modelThinkingLevels, modelThinkingLevelMaps, thinkingLevel, fastModeEnabled, fastModeActive, slowModeSupported, slowModeEnabled, slowModeScope, usageLimit, externalRunActive, - toolPreset, + toolPreset, sessionToolPreset, liveModelMeta, retryInfo, contextUsage, forkingEntryId, editingEntryId, isCompacting, compactResult, tokensPerSecond, displayModel: displayModelValue, sessionStats, @@ -1043,6 +1043,8 @@ export function ChatWindow({ session, newSessionCwd, newSessionHostId, newSessio thinkingLevel={thinkingLevel} onThinkingLevelChange={session || isNew ? handleThinkingLevelChange : undefined} toolPreset={toolPreset} + sessionToolPreset={sessionToolPreset} + isNewSession={isNew} onToolPresetChange={handleToolPresetChange} fastModeEnabled={fastModeEnabled} fastModeActive={fastModeActive} diff --git a/hooks/useAgentSession.ts b/hooks/useAgentSession.ts index 0d3a9ed84..17e664cc8 100644 --- a/hooks/useAgentSession.ts +++ b/hooks/useAgentSession.ts @@ -37,8 +37,8 @@ import { displayMessage } from "@/lib/message-delta"; import { looksLikeRunningTurn } from "@/lib/chat-transcript-plan"; import { createReconcileGuard, type ReconcileGuard } from "@/lib/reconcile-guard"; import type { BashResultInfo, ClientMessageStatus } from "@/lib/pi-types"; -import { getToolNamesForPreset, type ToolPreset } from "@/lib/tool-presets"; -import { getPreferredToolPreset, setPreferredToolPreset } from "@/lib/tool-preset-preference"; +import { getPresetForToolNames, getToolNamesForPreset, type ToolPreset } from "@/lib/tool-presets"; +import { getPreferredToolPreset, getSessionToolPreset, setPreferredToolPreset, setSessionToolPreset } from "@/lib/tool-preset-preference"; import { toast } from "@/components/ui/toast"; import { expandWebSlashCommand } from "@/lib/web-slash-commands"; import { validateOutgoingPrompt } from "@/lib/image-attachments"; @@ -451,6 +451,11 @@ export function useAgentSession(opts: UseAgentSessionOptions) { // to avoid a server/client mismatch when the user stored a different preset. const [toolPreset, setToolPreset] = useState("full"); useEffect(() => { setToolPreset(getPreferredToolPreset()); }, []); + // The preset the current session was spawned with (#356 item 2): null while + // the chat is new (nothing spawned yet) or the session's spawn was never + // recorded in this browser. A running session keeps its tools when the + // preference changes, so this never follows toolPreset. + const [sessionToolPreset, setSessionToolPresetState] = useState(null); const [thinkingLevel, setThinkingLevel] = useState("auto"); const [fastModeEnabled, setFastModeEnabled] = useState(false); const [fastModeActive, setFastModeActive] = useState(undefined); @@ -942,6 +947,7 @@ export function useAgentSession(opts: UseAgentSessionOptions) { const sid = session?.id; if (!sid) { setAdvisorEnabled(false); + setSessionToolPresetState(null); return; } let stored = false; @@ -954,6 +960,8 @@ export function useAgentSession(opts: UseAgentSessionOptions) { // The spawn registry must match before any command POST can lazily start // the omp process; localStorage alone would leave resumed sessions dark. setSessionAdvisorSpawn(sid, stored); + // The preset this session was spawned with, if this browser recorded it. + setSessionToolPresetState(getSessionToolPreset(sid)); }, [session?.id]); const handleAdvisorChange = useCallback((enabled: boolean) => { @@ -1399,6 +1407,18 @@ export function useAgentSession(opts: UseAgentSessionOptions) { // Best-effort: the spawned process already has --advisor. } } + // Record the preset this spawn used, so the badge and banner can show + // the session's actual toolset (#356 item 2). A pooled creation reuses + // its original body's toolNames, which may differ from the current + // preference, so read the effective body. + const spawnedToolNames = pooled + ? (JSON.parse(creationBodyRef.current!.json) as { toolNames?: string[] }).toolNames + : request.toolNames; + const spawnedPreset = getPresetForToolNames(spawnedToolNames); + if (spawnedPreset !== null) { + setSessionToolPreset(realId, spawnedPreset); + setSessionToolPresetState(spawnedPreset); + } // A prompt or side question that created this runtime attaches its own stream, // which would only replace an observer attached here. if (hookAliveRef.current && !promptDispatchPendingRef.current && !btwAskPendingRef.current) { @@ -3933,6 +3953,8 @@ export function useAgentSession(opts: UseAgentSessionOptions) { // In-memory registry still applies for this page load. } } + // Same for the spawn toolset: the fork keeps the parent's tools. + if (sessionToolPreset) setSessionToolPreset(newSessionId, sessionToolPreset); onSessionForked?.(newSessionId); } } catch (e) { @@ -3941,7 +3963,7 @@ export function useAgentSession(opts: UseAgentSessionOptions) { forkInFlightRef.current = false; setForkingEntryId(null); } - }, [advisorEnabled, onSessionForked]); + }, [advisorEnabled, sessionToolPreset, onSessionForked]); const handleLeafChange = useCallback(async (leafId: string | null) => { if (bashRunningRef.current || agentRunningRef.current || editInFlightRef.current) return; @@ -4916,7 +4938,7 @@ export function useAgentSession(opts: UseAgentSessionOptions) { return { // State data, loading, error, activeLeafId, messages, entryIds, showPreCompactionHistory, streamState, - agentRunning, modelNames, modelList, modelsLoading, modelError, modelThinkingLevels, modelThinkingLevelMaps, newSessionModel, toolPreset, thinkingLevel, fastModeEnabled, fastModeActive, slowModeSupported, slowModeEnabled, slowModeScope, usageLimit, autoRetryEnabled, interruptMode, autoCompactionEnabled, steeringMode, followUpMode, + agentRunning, modelNames, modelList, modelsLoading, modelError, modelThinkingLevels, modelThinkingLevelMaps, newSessionModel, toolPreset, sessionToolPreset, thinkingLevel, fastModeEnabled, fastModeActive, slowModeSupported, slowModeEnabled, slowModeScope, usageLimit, autoRetryEnabled, interruptMode, autoCompactionEnabled, steeringMode, followUpMode, liveModelMeta, retryInfo, contextUsage, systemPrompt, forkingEntryId, editingEntryId, isCompacting, compactError, compactResult, tokensPerSecond, currentModel, displayModel, isAutoModelSelection: !displayModel, sessionStats, agentPhase, diff --git a/lib/i18n/locales/en.json b/lib/i18n/locales/en.json index b7bda9c71..a6dd651dc 100644 --- a/lib/i18n/locales/en.json +++ b/lib/i18n/locales/en.json @@ -473,6 +473,11 @@ "chatInput.toolPresetNone": "No tools", "chatInput.toolPresetDefault": "Read, bash, edit, write", "chatInput.toolPresetFull": "All tools, including subagents", + "chatInput.toolsetBadgeTitle": "This session runs with the {preset} tool preset. Select to change the preset for new sessions.", + "chatInput.toolsetBadgeTitleUnknown": "Tool preset for new sessions: {preset}. This session's tools were not recorded.", + "chatInput.restrictedToolsBanner": "This session runs with only {count} tools — subagents, search, eval and more are disabled.", + "chatInput.restrictedToolsBannerNone": "This session runs with no tools enabled — the agent cannot read, run, or edit anything.", + "chatInput.restoreFullTools": "Restore full tools", "chatInput.cmdCompact": "Compress context, optionally with instructions", "chatInput.commandUsageTitle": "Command usage", "chatInput.cmdGoal": "Set the goal for this session", diff --git a/lib/tool-preset-preference.test.mjs b/lib/tool-preset-preference.test.mjs index 68789d771..d176f853c 100644 --- a/lib/tool-preset-preference.test.mjs +++ b/lib/tool-preset-preference.test.mjs @@ -1,8 +1,8 @@ import assert from "node:assert/strict"; import test from "node:test"; -const { getPreferredToolPreset, setPreferredToolPreset } = await import("./tool-preset-preference.ts"); -const { getToolNamesForPreset } = await import("./tool-presets.ts"); +import { getPreferredToolPreset, getSessionToolPreset, setPreferredToolPreset, setSessionToolPreset } from "./tool-preset-preference.ts"; +import { getPresetForToolNames, getToolNamesForPreset, getToolPresetToolCount } from "./tool-presets.ts"; function createStorage(values = {}) { const entries = new Map(Object.entries(values)); @@ -28,3 +28,29 @@ test("full preset leaves OMP's native toolset unrestricted", () => { assert.deepEqual(getToolNamesForPreset("default"), ["read", "bash", "edit", "write"]); assert.deepEqual(getToolNamesForPreset("none"), []); }); + +test("a session's spawn preset is recorded per session and unknown when never recorded (#356)", () => { + const storage = createStorage(); + assert.equal(getSessionToolPreset("s1", storage), null); + assert.equal(getSessionToolPreset(null, storage), null); + + setSessionToolPreset("s1", "default", storage); + assert.equal(getSessionToolPreset("s1", storage), "default"); + assert.equal(getSessionToolPreset("s2", storage), null, "sessions do not share a record"); + + setSessionToolPreset("s1", "full", storage); + assert.equal(getSessionToolPreset("s1", storage), "full"); + + const invalid = createStorage({ "omp-web:tool-preset:s1": "unknown" }); + assert.equal(getSessionToolPreset("s1", invalid), null); +}); + +test("tool counts and the toolNames round-trip name the preset (#356)", () => { + assert.equal(getToolPresetToolCount("full"), null); + assert.equal(getToolPresetToolCount("default"), 4); + assert.equal(getToolPresetToolCount("none"), 0); + assert.equal(getPresetForToolNames(undefined), "full"); + assert.equal(getPresetForToolNames([]), "none"); + assert.equal(getPresetForToolNames(["write", "read", "edit", "bash"]), "default"); + assert.equal(getPresetForToolNames(["read", "bash"]), null, "a custom toolset is no preset"); +}); diff --git a/lib/tool-preset-preference.ts b/lib/tool-preset-preference.ts index c7066db7e..b44814796 100644 --- a/lib/tool-preset-preference.ts +++ b/lib/tool-preset-preference.ts @@ -34,3 +34,28 @@ export function setPreferredToolPreset(preset: ToolPreset, storage: StorageLike // Preferences remain optional when storage is unavailable. } } + +const SESSION_STORAGE_KEY_PREFIX = "omp-web:tool-preset:"; + +/** The preset a session was spawned with, recorded at spawn time (#356). + * Null when unknown: a session from before this was recorded, or from + * another browser or process. The server exposes no per-session toolset, + * so only sessions this browser spawned can be known. */ +export function getSessionToolPreset(sessionId: string | null, storage: StorageLike | null = browserStorage()): ToolPreset | null { + if (!storage || !sessionId) return null; + try { + const value = storage.getItem(`${SESSION_STORAGE_KEY_PREFIX}${sessionId}`); + return isToolPreset(value) ? value : null; + } catch { + return null; + } +} + +export function setSessionToolPreset(sessionId: string, preset: ToolPreset, storage: StorageLike | null = browserStorage()): void { + if (!storage || !sessionId) return; + try { + storage.setItem(`${SESSION_STORAGE_KEY_PREFIX}${sessionId}`, preset); + } catch { + // The record is optional when storage is unavailable. + } +} diff --git a/lib/tool-presets.ts b/lib/tool-presets.ts index 2f7c23e2a..6aac3e626 100644 --- a/lib/tool-presets.ts +++ b/lib/tool-presets.ts @@ -20,3 +20,16 @@ export function getToolNamesForPreset(preset: ToolPreset): string[] | undefined if (preset === "full") return undefined; return [...PRESET_DEFAULT]; } + +export function getPresetForToolNames(toolNames: string[] | undefined): ToolPreset | null { + if (toolNames === undefined) return "full"; + if (toolNames.length === 0) return "none"; + const key = [...toolNames].map((name) => name.toLowerCase()).sort().join(","); + return key === [...PRESET_DEFAULT].sort().join(",") ? "default" : null; +} + +/** How many tools a preset grants: null for "full" (unrestricted — whatever omp ships). */ +export function getToolPresetToolCount(preset: ToolPreset): number | null { + const names = getToolNamesForPreset(preset); + return names === undefined ? null : names.length; +} From 6310c11fa1a519ae436e934b0f4fd2bd31fd10a0 Mon Sep 17 00:00:00 2001 From: K <61059522+kahme247@users.noreply.github.com> Date: Mon, 23 Nov 2026 11:25:53 +0200 Subject: [PATCH 2/2] Add zh-TW translations for toolset badge and restricted-tools banner (fixes #566 i18n failure) --- lib/i18n/locales/zh-TW.json | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/lib/i18n/locales/zh-TW.json b/lib/i18n/locales/zh-TW.json index 530707dd6..11df8f27e 100644 --- a/lib/i18n/locales/zh-TW.json +++ b/lib/i18n/locales/zh-TW.json @@ -473,6 +473,11 @@ "chatInput.toolPresetNone": "不使用工具", "chatInput.toolPresetDefault": "讀取、Bash、編輯、寫入", "chatInput.toolPresetFull": "所有工具(包含子代理)", + "chatInput.toolsetBadgeTitle": "此對話使用 {preset} 工具組合執行。點選可變更新對話的工具組合。", + "chatInput.toolsetBadgeTitleUnknown": "新對話的工具組合:{preset}。此對話的工具未被記錄。", + "chatInput.restrictedToolsBanner": "此對話僅使用 {count} 個工具執行 — 子代理、搜尋、評估等功能均已停用。", + "chatInput.restrictedToolsBannerNone": "此對話未啟用任何工具 — 代理無法讀取、執行或編輯任何內容。", + "chatInput.restoreFullTools": "恢復完整工具", "chatInput.cmdCompact": "壓縮上下文,可選擇附加指示", "chatInput.commandUsageTitle": "指令用法", "chatInput.cmdGoal": "設定此對話的目標",