import { useState } from 'react' import { Plus, Trash2, Play, Loader2, Save } from 'lucide-react' import { useStore, useActiveProject } from '../../store/useStore' import { BUILTIN_TOOLS, executeToolCall, resolveTools } from '../../lib/tools' import { Btn, Field, Section, inputCls } from '../ui' const SAMPLE_ARGS = { web_search: { query: '大型語言模型' }, wikipedia_article: { title: 'Taipei', lang: 'en' }, fetch_url: { url: 'https://example.com' }, get_weather: { city: 'Taipei', days: 2 }, currency_convert: { amount: 100, from: 'USD', to: 'TWD' }, calculate: { expression: '(2+3)^2 * sqrt(16)' }, get_current_time: { timezone: 'Asia/Taipei' }, search_github: { query: 'llm agent', limit: 3 }, search_hackernews: { query: 'LLM', recent: true }, search_papers: { query: 'retrieval augmented generation' }, search_books: { query: '三體' }, save_note: { title: '測試筆記', content: '由工具測試寫入' }, read_notes: {}, } function Tester({ name, defaultArgs }) { const project = useActiveProject() const customTools = useStore((s) => s.customTools) const [args, setArgs] = useState(JSON.stringify(defaultArgs || {}, null, 0)) const [out, setOut] = useState('') const [busy, setBusy] = useState(false) const run = async () => { setBusy(true); setOut('') const st = useStore.getState() const tools = resolveTools([name], customTools) const res = await executeToolCall( { function: { name, arguments: args } }, tools, { settings: st.settings, project, addNote: st.addNote, signal: undefined }, ) setOut(res); setBusy(false) } return (
setArgs(e.target.value)} /> {busy ? : }
{out &&
{out}
}
) } const EMPTY = { name: '', description: '', method: 'GET', endpoint: '', headers: '', parameters: '{"type":"object","properties":{"q":{"type":"string","description":"查詢字串"}},"required":["q"]}' } function CustomToolForm({ initial, onDone }) { const [d, setD] = useState(initial ? { ...initial, parameters: JSON.stringify(initial.parameters, null, 2) } : EMPTY) const [err, setErr] = useState('') const save = () => { const name = d.name.trim() if (!/^[a-zA-Z0-9_-]{1,64}$/.test(name)) return setErr('名稱只能用英數字、底線、連字號') if (!initial && [...BUILTIN_TOOLS, ...useStore.getState().customTools].some((t) => t.name === name)) return setErr('名稱重複') let parameters try { parameters = JSON.parse(d.parameters || '{}') } catch { return setErr('parameters 不是合法 JSON') } if (d.headers?.trim()) { try { JSON.parse(d.headers) } catch { return setErr('headers 不是合法 JSON') } } useStore.getState().saveCustomTool({ ...d, name, parameters }) onDone() } return (
setD({ ...d, name: e.target.value })} /> setD({ ...d, description: e.target.value })} />
setD({ ...d, endpoint: e.target.value })} />