The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
import { useEffect, useState } from 'react'
import { deleteEnvVar, getEnvVars, revealEnvVar, setEnvVar } from '@/hermes'
import { useI18n } from '@/i18n'
import { type IconComponent } from '@/lib/icons'
import { queryClient } from '@/lib/query-client'
import { confirm } from '@/store/confirm'
import { notify, notifyError } from '@/store/notifications'
import type { EnvVarInfo } from '@/types/hermes'
import { asText, includesQuery, redactedValue, withoutKey } from './helpers'
import { Pill } from './primitives'
import type { EnvRowProps } from './types'
// Shared filter used by every credential surface (Providers + Keys pages):
// category gate first, then a free-text match across key name + description.
export function filterEnv(info: EnvVarInfo, key: string, q: string, cat: string, extra?: string): boolean {
if (asText(info.category) !== cat) {
return false
}
if (!q) {
return true
}
return (
key.toLowerCase().includes(q) ||
includesQuery(info.description, q) ||
Boolean(extra && extra.toLowerCase().includes(q))
)
}
export function SettingsCategoryHeading({ count, icon: Icon, title }: CategoryHeadingProps) {
return (
<div className="mb-3 flex items-center gap-2 text-[length:var(--conversation-text-font-size)] font-medium">
<Icon className="size-4 text-muted-foreground" />
<span>{title}</span>
{count && <Pill>{count}</Pill>}
</div>
)
}
// Owns the env-var fetch + the edit/reveal/save/delete lifecycle so multiple
// credential pages (Providers, Keys) share one source of truth and one set of
// mutation handlers instead of duplicating the plumbing. An optional `profile`
// targets another profile's env store (the shared settings "Applies to"
// scope); undefined keeps the app-wide active profile. Request-shaped on
// purpose: the API helpers treat an explicit `null` as "target the
// primary/default backend", which is never what a settings page means.
export function useEnvCredentials(profile?: string): UseEnvCredentials {
const { t } = useI18n()
const credentials = t.settings.credentials
const toolsets = t.settings.toolsets
const [vars, setVars] = useState<Record<string, EnvVarInfo> | null>(null)
const [edits, setEdits] = useState<Record<string, string>>({})
const [revealed, setRevealed] = useState<Record<string, string>>({})
const [saving, setSaving] = useState<string | null>(null)
// Best-effort cleanup of a retired localStorage flag (global "Show
// advanced" toggle) — everything in these views is configuration-level.
useEffect(() => {
try {
window.localStorage.removeItem('desktop.settings.keys.show_advanced')
} catch {
// Ignore — old key cleanup is best-effort.
}
}, [])
useEffect(() => {
let cancelled = false
// Everything keyed by var name is dropped together with the reload: the
// cached `vars`, plus any in-flight edit or revealed value. Those maps are
// keyed by name alone, so leaving a draft behind after the target profile
// changed left its Save button live — writing the value into the profile
// now being targeted instead of the one it was typed for.
setVars(null)
setEdits({})
setRevealed({})
void (async () => {
try {
const next = await getEnvVars(profile)
if (!cancelled) {
setVars(next)
}
} catch (err) {
notifyError(err, t.settings.keys.failedLoad)
}
})()
return () => void (cancelled = true)
// eslint-disable-next-line react-hooks/exhaustive-deps -- reload per target profile; copy is stable
}, [profile])
function patchVar(key: string, patch: Partial<Pick<EnvVarInfo, 'is_set' | 'redacted_value'>>) {
setVars(c => (c ? { ...c, [key]: { ...c[key], ...patch } } : c))
}
function clearLocalState(key: string, editKey = key) {
setEdits(c => withoutKey(c, editKey))
setRevealed(c => withoutKey(c, key))
}
async function handleSave(key: string, editKey = key) {
const value = edits[editKey]
if (!value) {
return
}
setSaving(key)
try {
await setEnvVar(key, value, profile)
patchVar(key, { is_set: true, redacted_value: redactedValue(value) })
clearLocalState(key, editKey)
void queryClient.invalidateQueries({ queryKey: ['model-options'] })
notify({ kind: 'success', title: toolsets.savedTitle, message: toolsets.savedMessage(key) })
} catch (err) {
notifyError(err, toolsets.failedSave(key))
} finally {
setSaving(null)
}
}
// Direct save for a known value (no edit-state round-trip) — used by the
// onboarding-style key form, which owns its own input. Returns a result so
// the form can surface inline errors instead of only toasting.
async function saveValue(key: string, value: string): Promise<{ message?: string; ok: boolean }> {
const trimmed = value.trim()
if (!trimmed) {
return { message: credentials.enterValueFirst, ok: false }
}
setSaving(key)
try {
await setEnvVar(key, trimmed, profile)
patchVar(key, { is_set: true, redacted_value: redactedValue(trimmed) })
clearLocalState(key)
void queryClient.invalidateQueries({ queryKey: ['model-options'] })
notify({ kind: 'success', message: toolsets.savedMessage(key), title: toolsets.savedTitle })
return { ok: true }
} catch (err) {
notifyError(err, toolsets.failedSave(key))
return { message: err instanceof Error ? err.message : credentials.couldNotSave, ok: false }
} finally {
setSaving(null)
}
}
async function handleClear(key: string, editKey = key) {
if (!(await confirm({ destructive: true, title: toolsets.removeConfirm(key) }))) {
return
}
setSaving(key)
try {
await deleteEnvVar(key, profile)
patchVar(key, { is_set: false, redacted_value: null })
clearLocalState(key, editKey)
void queryClient.invalidateQueries({ queryKey: ['model-options'] })
notify({ kind: 'success', title: toolsets.removedTitle, message: toolsets.removedMessage(key) })
} catch (err) {
notifyError(err, toolsets.failedRemove(key))
} finally {
setSaving(null)
}
}
async function handleReveal(key: string) {
if (revealed[key]) {
setRevealed(c => withoutKey(c, key))
return
}
try {
const result = await revealEnvVar(key, profile)
setRevealed(c => ({ ...c, [key]: result.value }))
} catch (err) {
notifyError(err, toolsets.failedReveal(key))
}
}
return {
saveValue,
vars,
rowProps: {
edits,
revealed,
saving,
setEdits,
onSave: handleSave,
onClear: handleClear,
onReveal: handleReveal
}
}
}
interface CategoryHeadingProps {
count?: string
icon: IconComponent
title: string
}
interface UseEnvCredentials {
rowProps: Omit<EnvRowProps, 'varKey' | 'info'>
saveValue: (key: string, value: string) => Promise<{ message?: string; ok: boolean }>
vars: Record<string, EnvVarInfo> | null
}