/** * Admin > Routing > Routing Policies */ const { html } = window; const { useState, useEffect, useCallback } = hooks; const POLICY_TYPES = ['provider_prefer', 'team_route', 'cost_limit', 'model_alias', 'capability_match']; export default function RoutingSection() { const [policies, setPolicies] = useState([]); const [loading, setLoading] = useState(true); const [editing, setEditing] = useState(null); // null | 'new' | policy const [testModel, setTestModel] = useState(''); const [testUser, setTestUser] = useState(''); const [testResult, setTestResult] = useState(null); const load = useCallback(async () => { try { const data = await sw.api.admin.routing.policies(); setPolicies(data || []); } catch (e) { sw.toast(e.message, 'error'); } finally { setLoading(false); } }, []); useEffect(() => { load(); }, []); async function savePolicy(e) { e.preventDefault(); const form = e.target; const data = { name: form.pname.value.trim(), scope: form.scope.value, priority: parseInt(form.priority.value) || 0, policy_type: form.policy_type.value, config: JSON.parse(form.config.value || '{}'), is_active: form.is_active.checked, }; try { if (editing === 'new') { await sw.api.admin.routing.createPolicy(data); sw.toast('Policy created', 'success'); } else { await sw.api.admin.routing.updatePolicy(editing.id, data); sw.toast('Policy updated', 'success'); } setEditing(null); load(); } catch (e) { sw.toast(e.message, 'error'); } } async function deletePolicy(id) { const ok = await sw.confirm('Delete this routing policy?', true); if (!ok) return; try { await sw.api.admin.routing.deletePolicy(id); sw.toast('Policy deleted', 'success'); load(); } catch (e) { sw.toast(e.message, 'error'); } } async function runTest() { if (!testModel) return sw.toast('Model is required', 'error'); try { const result = await sw.api.admin.routing.test({ model: testModel, user_id: testUser || undefined, }); setTestResult(result); } catch (e) { sw.toast(e.message, 'error'); } } if (loading) return html`
Loading\u2026
`; if (editing) { const isNew = editing === 'new'; const p = isNew ? {} : editing; return html`

${isNew ? 'New Policy' : `Edit: ${p.name}`}

`; } return html`
${policies.length === 0 && html`
No routing policies
`} ${policies.map(p => html`
${p.name} ${p.policy_type} priority: ${p.priority}
${p.scope} ${p.is_active ? 'active' : 'off'}
`)}

Test Routing

setTestModel(e.target.value)} placeholder="gpt-4o" />
setTestUser(e.target.value)} placeholder="optional" />
${testResult && html`
${JSON.stringify(testResult, null, 2)}
`}
`; }