Reloading MCP servers rebuilds the tool set for the active session, which invalidates the provider prompt cache (tool schemas are baked into the system prompt). The next message re-sends full input tokens — can be expensive on long-context or high-reasoning models. To surface that cost, /reload-mcp now routes through a new slash-confirm primitive with three options: Approve Once / Always Approve / Cancel. 'Always Approve' persists approvals.mcp_reload_confirm: false so future reloads run silently. Coverage: * Classic CLI (cli.py) — interactive numbered prompt. * TUI (tui_gateway + Ink ops.ts) — text warning on first call; `now` / `always` args skip the gate; `always` also persists the opt-out. * Messenger gateway — button UI on Telegram (inline keyboard), Discord (discord.ui.View), Slack (Block Kit actions); text fallback on every other platform via /approve /always /cancel replies intercepted in gateway/run.py _handle_message. * Config key: approvals.mcp_reload_confirm (default true). * Auto-reload paths (CLI file watcher, TUI config-sync mtime poll) pass confirm=true so they do NOT prompt. Implementation: * tools/slash_confirm.py — module-level pending-state store used by all adapters and by the CLI prompt. Thread-safe register/resolve/clear. * gateway/platforms/base.py — send_slash_confirm hook (default 'Not supported' → text fallback). * gateway/run.py — _request_slash_confirm helper + text intercept in _handle_message (yields to in-progress tool-exec approvals so dangerous-command /approve still unblocks the tool thread first). Tests: * tests/tools/test_slash_confirm.py — primitive lifecycle + async resolution + double-click atomicity (16 tests). * tests/hermes_cli/test_mcp_reload_confirm_gate.py — default-config shape + deep-merge preserves user opt-out (5 tests). Targeted runs (hermetic): 89 passed (slash-confirm, config gate, existing agent cache, existing telegram approval buttons).
170 lines
5.3 KiB
TypeScript
170 lines
5.3 KiB
TypeScript
import { useEffect, useRef } from 'react'
|
|
|
|
import { resolveDetailsMode, resolveSections } from '../domain/details.js'
|
|
import type { GatewayClient } from '../gatewayClient.js'
|
|
import type {
|
|
ConfigFullResponse,
|
|
ConfigMtimeResponse,
|
|
ReloadMcpResponse
|
|
} from '../gatewayTypes.js'
|
|
import { asRpcResult } from '../lib/rpc.js'
|
|
|
|
import {
|
|
type BusyInputMode,
|
|
DEFAULT_INDICATOR_STYLE,
|
|
INDICATOR_STYLES,
|
|
type IndicatorStyle,
|
|
type StatusBarMode
|
|
} from './interfaces.js'
|
|
import { turnController } from './turnController.js'
|
|
import { patchUiState } from './uiStore.js'
|
|
|
|
const STATUSBAR_ALIAS: Record<string, StatusBarMode> = {
|
|
bottom: 'bottom',
|
|
off: 'off',
|
|
on: 'top',
|
|
top: 'top'
|
|
}
|
|
|
|
export const normalizeStatusBar = (raw: unknown): StatusBarMode =>
|
|
raw === false ? 'off' : typeof raw === 'string' ? (STATUSBAR_ALIAS[raw.trim().toLowerCase()] ?? 'top') : 'top'
|
|
|
|
const BUSY_MODES = new Set<BusyInputMode>(['interrupt', 'queue', 'steer'])
|
|
|
|
// TUI defaults to `queue` even though the framework default
|
|
// (`hermes_cli/config.py`) is `interrupt`. Rationale: in a full-screen
|
|
// TUI you're typically authoring the next prompt while the agent is
|
|
// still streaming, and an unintended interrupt loses work. Set
|
|
// `display.busy_input_mode: interrupt` (or `steer`) explicitly to
|
|
// opt out per-config; CLI / messaging adapters keep their `interrupt`
|
|
// default unchanged.
|
|
const TUI_BUSY_DEFAULT: BusyInputMode = 'queue'
|
|
|
|
export const normalizeBusyInputMode = (raw: unknown): BusyInputMode => {
|
|
if (typeof raw !== 'string') {
|
|
return TUI_BUSY_DEFAULT
|
|
}
|
|
|
|
const v = raw.trim().toLowerCase() as BusyInputMode
|
|
|
|
return BUSY_MODES.has(v) ? v : TUI_BUSY_DEFAULT
|
|
}
|
|
|
|
const INDICATOR_STYLE_SET: ReadonlySet<IndicatorStyle> = new Set(INDICATOR_STYLES)
|
|
|
|
export const normalizeIndicatorStyle = (raw: unknown): IndicatorStyle => {
|
|
if (typeof raw !== 'string') {
|
|
return DEFAULT_INDICATOR_STYLE
|
|
}
|
|
|
|
const v = raw.trim().toLowerCase() as IndicatorStyle
|
|
|
|
return INDICATOR_STYLE_SET.has(v) ? v : DEFAULT_INDICATOR_STYLE
|
|
}
|
|
|
|
const FALSEY_MOUSE = new Set(['0', 'false', 'no', 'off'])
|
|
const hasOwn = (obj: object, key: PropertyKey) => Object.prototype.hasOwnProperty.call(obj, key)
|
|
|
|
export const normalizeMouseTracking = (display: { mouse_tracking?: unknown; tui_mouse?: unknown }): boolean => {
|
|
const raw = hasOwn(display, 'mouse_tracking') ? display.mouse_tracking : display.tui_mouse
|
|
|
|
if (raw === false || raw === 0) {
|
|
return false
|
|
}
|
|
|
|
return typeof raw === 'string' ? !FALSEY_MOUSE.has(raw.trim().toLowerCase()) : true
|
|
}
|
|
|
|
const MTIME_POLL_MS = 5000
|
|
|
|
const quietRpc = async <T extends Record<string, any> = Record<string, any>>(
|
|
gw: GatewayClient,
|
|
method: string,
|
|
params: Record<string, unknown> = {}
|
|
): Promise<null | T> => {
|
|
try {
|
|
return asRpcResult<T>(await gw.request<T>(method, params))
|
|
} catch {
|
|
return null
|
|
}
|
|
}
|
|
|
|
export const applyDisplay = (cfg: ConfigFullResponse | null, setBell: (v: boolean) => void) => {
|
|
const d = cfg?.config?.display ?? {}
|
|
|
|
setBell(!!d.bell_on_complete)
|
|
patchUiState({
|
|
busyInputMode: normalizeBusyInputMode(d.busy_input_mode),
|
|
compact: !!d.tui_compact,
|
|
detailsMode: resolveDetailsMode(d),
|
|
detailsModeCommandOverride: false,
|
|
indicatorStyle: normalizeIndicatorStyle(d.tui_status_indicator),
|
|
inlineDiffs: d.inline_diffs !== false,
|
|
mouseTracking: normalizeMouseTracking(d),
|
|
sections: resolveSections(d.sections),
|
|
showCost: !!d.show_cost,
|
|
showReasoning: !!d.show_reasoning,
|
|
statusBar: normalizeStatusBar(d.tui_statusbar),
|
|
streaming: d.streaming !== false
|
|
})
|
|
}
|
|
|
|
export function useConfigSync({ gw, setBellOnComplete, setVoiceEnabled, sid }: UseConfigSyncOptions) {
|
|
const mtimeRef = useRef(0)
|
|
|
|
useEffect(() => {
|
|
if (!sid) {
|
|
return
|
|
}
|
|
|
|
// Keep startup cheap: voice.toggle status probes optional audio/STT deps and
|
|
// can run long enough to delay prompt.submit on the single stdio RPC pipe.
|
|
// Environment flags are enough to initialize the UI bit; the heavier status
|
|
// check still runs when the user opens /voice.
|
|
setVoiceEnabled(process.env.HERMES_VOICE === '1')
|
|
quietRpc<ConfigMtimeResponse>(gw, 'config.get', { key: 'mtime' }).then(r => {
|
|
mtimeRef.current = Number(r?.mtime ?? 0)
|
|
})
|
|
quietRpc<ConfigFullResponse>(gw, 'config.get', { key: 'full' }).then(r => applyDisplay(r, setBellOnComplete))
|
|
}, [gw, setBellOnComplete, setVoiceEnabled, sid])
|
|
|
|
useEffect(() => {
|
|
if (!sid) {
|
|
return
|
|
}
|
|
|
|
const id = setInterval(() => {
|
|
quietRpc<ConfigMtimeResponse>(gw, 'config.get', { key: 'mtime' }).then(r => {
|
|
const next = Number(r?.mtime ?? 0)
|
|
|
|
if (!mtimeRef.current) {
|
|
if (next) {
|
|
mtimeRef.current = next
|
|
}
|
|
|
|
return
|
|
}
|
|
|
|
if (!next || next === mtimeRef.current) {
|
|
return
|
|
}
|
|
|
|
mtimeRef.current = next
|
|
|
|
quietRpc<ReloadMcpResponse>(gw, 'reload.mcp', { session_id: sid, confirm: true }).then(
|
|
r => r && turnController.pushActivity('MCP reloaded after config change')
|
|
)
|
|
quietRpc<ConfigFullResponse>(gw, 'config.get', { key: 'full' }).then(r => applyDisplay(r, setBellOnComplete))
|
|
})
|
|
}, MTIME_POLL_MS)
|
|
|
|
return () => clearInterval(id)
|
|
}, [gw, setBellOnComplete, sid])
|
|
}
|
|
|
|
export interface UseConfigSyncOptions {
|
|
gw: GatewayClient
|
|
setBellOnComplete: (v: boolean) => void
|
|
setVoiceEnabled: (v: boolean) => void
|
|
sid: null | string
|
|
}
|