import { tr } from '../lib/i18n'; /** * Settings -> Tools: what the assistant may use in chats, in plain words. * * Every row is a switch in the existing Field pattern. Nothing here is a new component: switches, selects, dialogs and buttons are * the ones the other Settings panels use. The host owns the truth (GET/PUT /api/agent/tools); a change applies from the next message. */ import { useEffect, useState } from 'react'; import { api } from '../lib/client'; import { pushToast } from '../lib/store'; import { Btn, Dialog, ErrorNotice, Field, Icon, Select } from './ui'; export interface ToolGroup { id: string; name: string; about: string; area: 'computer' | 'web' | 'together'; enabled: boolean; /** Manual mode asks before this tool changes anything. */ asks: boolean; } export interface CustomTool { id: string; name: string; about: string; kind: 'instructions' | 'command' | 'server'; enabled: boolean; /** Shown in the list: the command, the server address or the first words of the instructions. */ summary: string; instructions?: string; command?: string; address?: string; args?: string[]; } export interface ToolsState { groups: ToolGroup[]; search: { provider: 'builtin' | 'exa' | 'perplexity' | 'deepseek'; keySet: boolean }; /** Skip the once-per-chat question before the first web search or page in Manual mode. Off unless the person turned it on. */ webWithoutAsking: boolean; browser: { enabled: boolean; found: boolean; name: string | null; missing?: 'component' }; custom: CustomTool[]; } const AREAS: { id: ToolGroup['area']; title: string }[] = [ { id: 'computer', title: 'On this computer' }, { id: 'web', title: 'On the web' }, { id: 'together', title: 'Working on bigger jobs' }, ]; const SEARCH_CHOICES = [ { value: 'builtin', label: 'DuckDuckGo (no key)' }, { value: 'exa', label: 'Exa (your own key)' }, { value: 'perplexity', label: 'Perplexity (your own key)' }, { value: 'deepseek', label: 'DeepSeek (your own key)' }, ]; const KIND_CHOICES = [ { value: 'instructions', label: 'Follow my instructions' }, { value: 'command', label: 'Run a command' }, { value: 'server', label: 'Connect a tool server (MCP)' }, ]; const KIND_NAMES: Record = { instructions: 'Instructions', command: 'Command', server: 'Tool server' }; interface Draft { id?: string; name: string; about: string; kind: CustomTool['kind']; instructions: string; command: string; address: string; args: string; } const blank = (): Draft => ({ name: '', about: '', kind: 'instructions', instructions: '', command: '', address: '', args: '' }); export function SettingsTools({ preview }: { preview?: ToolsState }) { const [state, setState] = useState(preview ?? null); const [error, setError] = useState(''); const [key, setKey] = useState(''); const [draft, setDraft] = useState(null); const [formError, setFormError] = useState(''); const [remove, setRemove] = useState(null); const [busy, setBusy] = useState(false); async function load() { try { setState(await api.get('/api/agent/tools')); setError(''); } catch (e) { setError(e instanceof Error ? e.message : 'The tool list could not be loaded.'); } } useEffect(() => { if (!preview) void load(); }, []); async function save(change: Record) { if (preview) return; setBusy(true); try { setState(await api.put('/api/agent/tools', change)); setError(''); } catch (e) { setError(e instanceof Error ? e.message : 'The change could not be saved.'); void load(); } finally { setBusy(false); } } const toggle = (id: string, on: boolean) => { setState(s => s && { ...s, groups: s.groups.map(g => g.id === id ? { ...g, enabled: on } : g) }); void save({ groups: { [id]: on } }); }; async function submit() { if (!draft) return; if (!/^[A-Za-z][A-Za-z0-9 _-]{0,39}$/.test(draft.name.trim())) { setFormError('Give the tool a short name: letters, numbers, spaces or hyphens.'); return; } if (!draft.about.trim()) { setFormError('Say what the tool does, in a sentence. The assistant reads this to decide when to use it.'); return; } if (draft.kind === 'instructions' && !draft.instructions.trim()) { setFormError('Write the instructions the assistant should follow when it uses this tool.'); return; } if (draft.kind === 'command' && !draft.command.trim()) { setFormError('Enter the command to run.'); return; } if (draft.kind === 'server' && !draft.address.trim()) { setFormError('Enter the tool server’s web address, or the program that starts it.'); return; } setBusy(true); try { const body = { name: draft.name.trim(), about: draft.about.trim(), kind: draft.kind, instructions: draft.instructions, command: draft.command.trim(), address: draft.address.trim(), args: draft.args.split('\n').map(a => a.trim()).filter(Boolean) }; setState(draft.id ? await api.patch(`/api/agent/tools/custom/${encodeURIComponent(draft.id)}`, body) : await api.post('/api/agent/tools/custom', body)); setDraft(null); setFormError(''); } catch (e) { setFormError(e instanceof Error ? e.message : 'The tool could not be saved.'); } finally { setBusy(false); } } if (!state) return
{error ? void load()} /> :

{tr('Loading the tool list…')}

}
; const keyed = state.search.provider !== 'builtin'; return

{tr('Tools')}

{tr('Choose what the assistant may use in your chats. A change applies from your next message. In Manual mode, anything that changes your computer still asks you first.')}

{error ? void load()} /> : null} {AREAS.map(area =>

{tr(area.title)}

{state.groups.filter(g => g.area === area.id).map(g =>
)}

{tr('Web search')}

setKey(e.target.value)} /> { void save({ search: { provider: state.search.provider, key: key.trim() } }); setKey(''); pushToast('success', tr('Key saved')); }}>{tr('Save key')}
: null}

{tr('Web browser')}

{tr('Your own tools')}

{tr('Teach the assistant something new: give it instructions to follow, a command to run, or connect a tool server. Commands and servers ask first in Manual mode.')}

{state.custom.length ?
{state.custom.map(t => {tr(t.about)} · {tr(KIND_NAMES[t.kind])}}>
{ setFormError(''); setDraft({ id: t.id, name: t.name, about: t.about, kind: t.kind, instructions: t.instructions ?? '', command: t.command ?? '', address: t.address ?? '', args: (t.args ?? []).join('\n') }); }}>{tr('Edit')} setRemove(t)}>{tr('Remove')}
)}
:
{tr('No tools of your own yet')}

{tr('For example: “Write release notes in our house style”, or a command that checks your website is up.')}

}
{ setFormError(''); setDraft(blank()); }}>{tr('Add a tool')}
setDraft(null)} actions={<> setDraft(null)}>{tr('Cancel')} void submit()}>{tr('Save tool')}}> {draft ?
{tr('How it works')}