Make Codex and Claude Code subscriptions go further: a local model router that gives routine agent work to economical models and keeps your strongest model for the hard parts.
Rust ai-agentsai-gatewayclaude-codecodexcoding-agentcost-optimizationdeepseekllm-gatewayllm-routerlocal-firstmodel-routingrusttask-routingtauri
fix(desktop): simplify follow-up settings and allow switching by default (#59)
澄潭048c5987ea86
diff --git a/apps/desktop/src/features/decision-services/BranchRoutingEditor.tsx b/apps/desktop/src/features/decision-services/BranchRoutingEditor.tsxindex 1c97b360..6813fbe1 100644--- a/apps/desktop/src/features/decision-services/BranchRoutingEditor.tsx+++ b/apps/desktop/src/features/decision-services/BranchRoutingEditor.tsx@@ -35,13 +35,14 @@ export function BranchRoutingEditor({ routing, services, language, onChange, onO})}<section className="editor-section"><button className="btn" type="button" disabled={routing.branches.length >= 16} onClick={() => patch({ branches: [...routing.branches, newBranch(t(`分支 ${routing.branches.length + 1}`, `Branch ${routing.branches.length + 1}`))] })}><UiIcon name="plus" />{t('添加分支', 'Add branch')}</button></section><section className="editor-section"><Disclosure label={t(`计划默认判断设置 · ${judgmentSummary(routing.judgment, true, language)}`, `Plan default judgment · ${judgmentSummary(routing.judgment, true, language)}`)} language={language}><JudgmentFields value={routing.judgment} onChange={judgment => patch({ judgment })} id="global" language={language} /></Disclosure></section>- <section className="editor-section"><Disclosure label={t('追问偏好与失败处理', 'Follow-up preference and failure handling')} language={language}>+ <section className="editor-section"><Disclosure label={t('默认分支', 'Default branch')} language={language}><label className="field"><span className="field-label">{t('默认分支', 'Default branch')}</span><select className="input" value={routing.default_branch_id} onChange={e => patch({ default_branch_id: e.target.value })}>{routing.branches.map(b => <option key={b.id} value={b.id}>{b.name}</option>)}</select><span className="field-help">{t('没有匹配的任务条件时使用。整体决策失败时也使用此分支,有主力则使用主力。', 'Used when no task condition matches. Decision failures also use this branch, preferring its primary group when present.')}</span></label>+ </Disclosure></section>+ <section className="editor-section"><Disclosure label={t('追问设置', 'Follow-up settings')} language={language}><FollowUpPreference value={routing.reselect_on_user_message} onChange={reselect_on_user_message => patch({ reselect_on_user_message })} language={language} />- <p className="field-help">{t('模型不可用时依次尝试本组候选,常规组耗尽后尝试本分支主力组。主力组耗尽后停止并提示。', 'Try candidates in order on failure; exhausted regular groups continue to this branch’s primary group. Stop when primary candidates are exhausted.')}</p></Disclosure></section></>;}export function FollowUpPreference({ value, onChange, language }: { value: boolean; onChange(value: boolean): void; language: 'zh' | 'en' }) {- return <label className="field"><span><input type="checkbox" checked={!value} onChange={e => onChange(!e.target.checked)} />{language === 'zh' ? '优先保持当前模型' : 'Prefer the current model'}</span><span className="field-help">{language === 'zh' ? '每条新消息都会重新决策,此偏好只在本次选中的模型组内生效。' : 'Every new message gets a fresh decision. This preference applies only within the selected model group.'}</span></label>;+ return <label className="field"><span><input type="checkbox" checked={value} onChange={e => onChange(e.target.checked)} />{language === 'zh' ? '追问时允许换模型' : 'Allow model switching on follow-up'}</span><span className="field-help">{language === 'zh' ? '关闭后,追问优先沿用当前模型;模型不可用时仍会自动切换。' : 'When off, follow-ups prefer the current model; unavailable models still trigger automatic switching.'}</span></label>;}diff --git a/apps/desktop/src/features/decision-services/types.ts b/apps/desktop/src/features/decision-services/types.tsindex 1847e5bd..a30a4946 100644--- a/apps/desktop/src/features/decision-services/types.ts+++ b/apps/desktop/src/features/decision-services/types.ts@@ -35,7 +35,7 @@ export function newBranch(name: string, condition = ''): RouteBranch {export function branchRouting(service?: DecisionService): BranchRouting {const branches = [newBranch('分支 1'), newBranch('分支 2')];return { classifier: { kind: 'decision_service', service: service ?? emptyService() }, branches,- default_branch_id: branches[1].id, judgment: structuredClone(defaultJudgment), reselect_on_user_message: false };+ default_branch_id: branches[1].id, judgment: structuredClone(defaultJudgment), reselect_on_user_message: true };}export const providers = [{ id: 'bailian-token-plan', name: '百炼 Token Plan', endpoint: 'https://token-plan.cn-beijing.maas.aliyuncs.com/compatible-mode/v1/systemone', model: 'decision-model-preview' },diff --git a/apps/desktop/src/plan-editor.tsx b/apps/desktop/src/plan-editor.tsxindex 92309ae1..267d890a 100644--- a/apps/desktop/src/plan-editor.tsx+++ b/apps/desktop/src/plan-editor.tsx@@ -65,7 +65,7 @@ function defaultReasoning(native: Native | undefined, preferred: 'low' | 'high'const profile = preferred === 'low' ? profiles[0] : profiles.at(-1);return profile ? { kind: 'profile', profile } : undefined;}-export function emptyEditor(): Editor { return { schema: 'hiroute.plan-editor/v2', display_name: '', purpose: '', mode: 'fixed_model', candidates: [], smart: { economy: [], primary: [], judgment: structuredClone(defaultJudgment), reselect_on_user_message: false, classifier: { kind: 'decision_service', service: emptyService() }, complex_keywords: [] }, free: { candidates: [], primary: [], primary_fallback: false }, delegation_enabled: false, requirements: {}, limits: { maximum_attempts: 6, request_timeout_ms: DEFAULT_REQUEST_TIMEOUT_MS, attempt_timeout_ms: 30000 } }; }+export function emptyEditor(): Editor { return { schema: 'hiroute.plan-editor/v2', display_name: '', purpose: '', mode: 'fixed_model', candidates: [], smart: { economy: [], primary: [], judgment: structuredClone(defaultJudgment), reselect_on_user_message: true, classifier: { kind: 'decision_service', service: emptyService() }, complex_keywords: [] }, free: { candidates: [], primary: [], primary_fallback: false }, delegation_enabled: false, requirements: {}, limits: { maximum_attempts: 6, request_timeout_ms: DEFAULT_REQUEST_TIMEOUT_MS, attempt_timeout_ms: 30000 } }; }export function reopen(plan: Plan): Editor {const e = { ...emptyEditor(), display_name: plan.desired.display_name, purpose: plan.desired.purpose, custom_alias: plan.model_alias, mode: plan.desired.mode, delegation_enabled: plan.desired.delegation_enabled, work: plan.desired.work, requirements: plan.desired.requirements, limits: plan.desired.limits };const s = plan.desired.strategy;@@ -409,9 +409,8 @@ export const PlanEditor = forwardRef<PlanEditorHandle, { plan?: Plan; draft?: Dr<section className="editor-section">{group(text('省钱模型', 'Economy models'), text('处理简单任务,按顺序尝试。', 'For simple tasks, tried in this order.'), editor.smart.economy, economy => update({ smart: { ...editor.smart, economy } }), { groupId: 'economy', preferred: 'low' })}</section><section className="editor-section">{group(text('主力模型', 'Primary models'), text('任务复杂或上一阶段不胜任时使用,按顺序尝试。', 'For complex tasks or low competence, tried in this order.'), editor.smart.primary, primary => update({ smart: { ...editor.smart, primary } }), { groupId: 'primary', preferred: 'high' })}</section>{editor.smart.classifier.kind !== 'local_rules' && <section className="editor-section"><Disclosure label={text(`判断设置 · ${judgmentSummary(editor.smart.judgment, true, language)}`, `Judgment settings · ${judgmentSummary(editor.smart.judgment, true, language)}`)} language={language}><JudgmentFields value={editor.smart.judgment} onChange={judgment => update({ smart: { ...editor.smart, judgment } })} id="smart" language={language} /></Disclosure></section>}- <section className="editor-section"><Disclosure label={text('追问偏好与失败处理', 'Follow-up preference and failure handling')} language={language}>+ <section className="editor-section"><Disclosure label={text('追问设置', 'Follow-up settings')} language={language}><FollowUpPreference value={editor.smart.reselect_on_user_message} onChange={reselect_on_user_message => update({ smart: { ...editor.smart, reselect_on_user_message } })} language={language} />- <p className="field-help">{text('每条新消息重新判断。决策调用失败时使用启发式规则;省钱模型均不可用时尝试主力模型,主力模型耗尽后停止并提示。', 'Each new message is judged again. Failed decision calls use heuristic rules. Exhausted economy candidates continue to primary models; stop when primary candidates are exhausted.')}</p></Disclosure></section></>}{editor.branch_routing && editor.mode === 'custom_branches' && <BranchRoutingEditor routing={editor.branch_routing} services={services} language={language} onChange={branch_routing => update({ branch_routing })} onOpenServices={onOpenServices} group={group} errorGroup={validationIssue?.group} error={validationIssue?.message} />}diff --git a/apps/desktop/tests/v3/browser/product-shell-scenarios.mjs b/apps/desktop/tests/v3/browser/product-shell-scenarios.mjsindex c69469fc..641358d0 100644--- a/apps/desktop/tests/v3/browser/product-shell-scenarios.mjs+++ b/apps/desktop/tests/v3/browser/product-shell-scenarios.mjs@@ -160,8 +160,43 @@ const scenarios = [assert(calls('preview_compute_save').at(-1).payload.change.expected_revisions.target === c().management.revisions.target, 'Credential change reused the revision from before the decision save');await until(() => text().includes('接入凭据已保存'), 'credential save completed');}),+ scenario('desktop.routing.follow-up-preference', ['routing-editor', 'decision-services'], 'New routes allow model switching; each mode retains the choice in its saved draft', async () => {+ await fresh(); await routing(); await click('新建智能路由');+ const fields = all('.plan-identity-fields input');+ setInput(fields[0], 'Follow-up preference'); setInput(fields[1], 'Check new route defaults');+ const mode = async label => {+ all('.mode-card').find(item => item.querySelector('strong')?.textContent === label).click();+ await pause(40);+ const details = [...document.querySelectorAll('.plan-editor details')].find(item => item.querySelector('summary')?.textContent.includes('追问设置'));+ assert(details, 'Follow-up settings are missing');+ if (!details.open) { details.querySelector('summary').click(); await pause(40); }+ assert(details.textContent.includes('模型不可用时仍会自动切换'), 'Automatic switching caveat is missing');+ assert(!details.querySelector('select'), 'Default branch selection is mixed into follow-up settings');+ return details.querySelector('input[type="checkbox"]');+ };+ const smart = await mode('智能省钱');+ assert(smart.checked && smart.closest('label').textContent.includes('追问时允许换模型'), 'New smart route does not enable the positive switching preference');+ smart.click(); await pause(40);+ const branch = await mode('自定义分支');+ assert(branch.checked, 'New custom routing does not allow model switching');+ branch.click(); await pause(40);+ assert(!(await mode('智能省钱')).checked, 'Changing modes lost the smart preference');+ await mode('自定义分支'); await click('保存草稿');+ await until(() => calls('preview_plan_editor').length > 0, 'follow-up draft request');+ const editor = calls('preview_plan_editor').at(-1).payload.input.editor;+ assert(editor.smart.reselect_on_user_message === false && editor.branch_routing.reselect_on_user_message === false, 'Unchecked preferences were inverted or reset when saving');+ await until(() => !button('保存草稿').disabled, 'follow-up save finished');+ (await mode('智能省钱')).click(); await pause(40);+ (await mode('自定义分支')).click(); await pause(40); await click('保存草稿');+ await until(() => calls('preview_plan_editor').length === 2, 'enabled follow-up draft request');+ const enabled = calls('preview_plan_editor').at(-1).payload.input.editor;+ assert(enabled.smart.reselect_on_user_message === true && enabled.branch_routing.reselect_on_user_message === true, 'Checked preferences were inverted when saving');+ }),scenario('desktop.routing.judgment-settings', ['routing-editor', 'decision-services'], 'Advanced judgment starts collapsed; branch copy/reset stays independent and draft payload retains settings', async () => {await fresh(); await routing();+ const followUp = [...document.querySelectorAll('.plan-editor details')].find(item => item.querySelector('summary')?.textContent.includes('追问设置'));+ followUp.querySelector('summary').click(); await pause(40);+ assert(!followUp.querySelector('input').checked, 'Opening a saved false preference applied the new default');await click('决策模型');const field = id => document.querySelector(`[data-decision-field="${id}"]`);const smartDetails = field('smart-simple-threshold').closest('details');@@ -173,6 +208,7 @@ const scenarios = [await click('保存草稿');await until(() => calls('preview_plan_editor').length > 0 && !button('保存草稿').disabled, 'smart draft request');const smart = calls('preview_plan_editor').at(-1).payload.input.editor.smart;+ assert(smart.reselect_on_user_message === false, 'Saving unrelated edits changed the saved follow-up preference');assert(smart.judgment.degree.simple_threshold_millis === 700 && !('branch_routing' in calls('preview_plan_editor').at(-1).payload.input.editor), 'Smart saving was serialized as task branches');all('.mode-card').find(item => item.querySelector('strong')?.textContent === '自定义分支').click();await until(() => all('.branch-routing-card').length === 2, 'custom branch editor');diff --git a/apps/desktop/tests/v3/browser/scenario-requirements.mjs b/apps/desktop/tests/v3/browser/scenario-requirements.mjsindex 765de10d..e212375a 100644--- a/apps/desktop/tests/v3/browser/scenario-requirements.mjs+++ b/apps/desktop/tests/v3/browser/scenario-requirements.mjs@@ -55,6 +55,7 @@ export const productShellRequired = ['desktop.quality.evidence-return','desktop.models.return-from-decisions','desktop.routing.judgment-settings',+ 'desktop.routing.follow-up-preference','desktop.models.branch-route-references','desktop.decisions.hidden-invalid-field','desktop.decisions.save-test-revision',