The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
import { atom, computed, onStop, type ReadableAtom } from 'nanostores'
import { persistStore } from '@/lib/persist-store'
import { persistBoolean, storedBoolean } from '@/lib/storage'
import { modeBound } from '@/store/interface-mode'
export type ToolViewMode = 'product' | 'technical'
type ToolDisclosureStates = Record<string, boolean>
const TOOL_VIEW_TECHNICAL_STORAGE_KEY = 'hermes.desktop.toolView.technical'
const HIDE_CODE_DIFFS_STORAGE_KEY = 'hermes.desktop.toolView.hideCodeDiffs'
const TOOL_DISCLOSURE_STORAGE_KEY = 'hermes.desktop.toolDisclosure.v1'
const MAX_DISCLOSURE_STATES = 240
// Simple mode rests on product summaries, diffs folded, without touching either
// preference.
const $toolViewModePref = atom<ToolViewMode>(
storedBoolean(TOOL_VIEW_TECHNICAL_STORAGE_KEY, false) ? 'technical' : 'product'
)
const $hideCodeDiffsPref = atom(storedBoolean(HIDE_CODE_DIFFS_STORAGE_KEY, false))
export const $toolViewMode = modeBound('toolViewMode', $toolViewModePref, mode => $toolViewModePref.set(mode))
export const $hideCodeDiffs = modeBound('hideCodeDiffs', $hideCodeDiffsPref, hidden => $hideCodeDiffsPref.set(hidden))
export const $toolDisclosureStates = atom<ToolDisclosureStates>(loadToolDisclosureStates())
const disclosureOpenCache = new Map<string, ReadableAtom<boolean | undefined>>()
const anyDisclosureOpenCache = new Map<string, ReadableAtom<boolean>>()
persistStore(
$toolViewModePref,
(mode, options) => persistBoolean(TOOL_VIEW_TECHNICAL_STORAGE_KEY, mode === 'technical', options),
'product'
)
persistStore(
$hideCodeDiffsPref,
(hidden, options) => persistBoolean(HIDE_CODE_DIFFS_STORAGE_KEY, hidden, options),
false
)
$toolDisclosureStates.subscribe(persistToolDisclosureStates)
export function setToolViewMode(mode: ToolViewMode) {
$toolViewMode.set(mode)
}
export function setHideCodeDiffs(hidden: boolean) {
$hideCodeDiffs.set(hidden)
}
export function $toolDisclosureOpen(id: string): ReadableAtom<boolean | undefined> {
let cached = disclosureOpenCache.get(id)
if (!cached) {
cached = computed($toolDisclosureStates, states => states[id])
onStop(cached, () => {
if (disclosureOpenCache.get(id) === cached) {
disclosureOpenCache.delete(id)
}
})
disclosureOpenCache.set(id, cached)
}
return cached
}
/**
* Whether any of a set of disclosures is open — a run asking about its rows.
*
* Computed rather than reading the whole map so a toggle anywhere in the
* transcript only re-renders the runs whose own answer changed.
*/
export function $anyToolDisclosureOpen(ids: readonly string[]): ReadableAtom<boolean> {
const key = ids.join('|')
let cached = anyDisclosureOpenCache.get(key)
if (!cached) {
cached = computed($toolDisclosureStates, states => ids.some(id => Boolean(states[id])))
onStop(cached, () => {
if (anyDisclosureOpenCache.get(key) === cached) {
anyDisclosureOpenCache.delete(key)
}
})
anyDisclosureOpenCache.set(key, cached)
}
return cached
}
function loadToolDisclosureStates(): ToolDisclosureStates {
if (typeof window === 'undefined') {
return {}
}
try {
const raw = window.localStorage.getItem(TOOL_DISCLOSURE_STORAGE_KEY)
if (!raw) {
return {}
}
const parsed = JSON.parse(raw) as unknown
if (!parsed || typeof parsed !== 'object' || Array.isArray(parsed)) {
return {}
}
return Object.fromEntries(
Object.entries(parsed as Record<string, unknown>)
.filter((entry): entry is [string, boolean] => typeof entry[0] === 'string' && typeof entry[1] === 'boolean')
.slice(-MAX_DISCLOSURE_STATES)
)
} catch {
return {}
}
}
function persistToolDisclosureStates(states: ToolDisclosureStates) {
if (typeof window === 'undefined') {
return
}
try {
const entries = Object.entries(states).slice(-MAX_DISCLOSURE_STATES)
window.localStorage.setItem(TOOL_DISCLOSURE_STORAGE_KEY, JSON.stringify(Object.fromEntries(entries)))
} catch {
// Tool disclosure is a local UI preference; ignore storage failures.
}
}
export function setToolDisclosureOpen(id: string, open: boolean) {
if (!id) {
return
}
const current = $toolDisclosureStates.get()
if (current[id] === open) {
return
}
$toolDisclosureStates.set({ ...current, [id]: open })
}