Skip to content

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.

8.7 KB
Raw Blame
import type { TrustBranch } from '../trust/types';
import { TrustLevelControl } from '../trust/TrustLevelControl';
import { DecisionSelector } from './DecisionSelector';
import type { OpenDecisionConnection } from './presentation';
import { useState, type ReactNode } from 'react';
import { Disclosure, UiIcon } from '../../ui';
import type { Selection } from '../../plan-editor';
import { isCustomExtension, newBranch, type BranchRouting, type DecisionService, type RouteBranch } from './types';
import { JudgmentFields, judgmentSummary } from './JudgmentSettings';

export type DecisionModelGroup = (title: string, subtitle: string, values: Selection[], replace: (values: Selection[]) => void, options: { groupId: string; preferred?: 'low' | 'high' }) => ReactNode;
export function BranchRoutingEditor({ routing, services, language, onChange, onOpenServices, group, errorGroup, error, evidence, evidenceError, newPlan, replayPlan }: {
  replayPlan?: { planId: string; revision: number; savedThresholds: Record<string, number>; active: boolean }; evidence?: TrustBranch[]; evidenceError?: boolean; newPlan?: boolean;
  routing: BranchRouting; services: DecisionService[]; language: 'zh' | 'en';
  onChange(value: BranchRouting): void; onOpenServices?: OpenDecisionConnection; errorGroup?: string; error?: string; group: DecisionModelGroup;
}) {
  const t = (zh: string, en: string) => language === 'zh' ? zh : en;
  const [primaryEditors, setPrimaryEditors] = useState<string[]>([]);
  const extension = isCustomExtension(routing.classifier);
  const patch = (value: Partial<BranchRouting>) => onChange({ ...routing, ...value });
  const put = (branch: RouteBranch, value: Partial<RouteBranch>) => patch({ branches: routing.branches.map(b => b.id === branch.id ? { ...b, ...value } : b) });
  return <>
    <DecisionSelector classifier={routing.classifier} smart={false} services={services} language={language} onChange={classifier => patch({ classifier })} onOpenServices={onOpenServices} error={errorGroup === 'classifier' ? error : undefined} />
    {routing.branches.map((branch, index) => {
      const judgment = branch.judgment ?? routing.judgment, dual = branch.primary_candidates.length > 0;
      return <section className="editor-section branch-routing-card" data-branch-id={branch.id} key={branch.id}>
        <div className="editor-section-heading"><div><h3>{branch.name || t('未命名分支', 'Untitled branch')}</h3><p>{dual ? t('先判断任务类别,再在本分支选择常规或主力模型。', 'Classify the task, then choose regular or primary models within this branch.') : t('匹配此类任务后使用常规模型。', 'Use regular models for tasks in this category.')}</p></div>{routing.branches.length > 2 && <button className="icon-btn" type="button" aria-label={t('删除分支', 'Remove branch')} onClick={() => { const branches = routing.branches.filter(b => b.id !== branch.id); patch({ branches, default_branch_id: routing.default_branch_id === branch.id ? branches[0].id : routing.default_branch_id }); }}><UiIcon name="trash" /></button>}</div>
        <div data-route-group={branch.id}><label className="field"><span className="field-label">{t('分支名称', 'Branch name')}</span><input className="input" maxLength={128} data-decision-field={`branch-${index}-name`} value={branch.name} onChange={e => put(branch, { name: e.target.value })} /></label><label className="field"><span className="field-label">{t('任务条件', 'Task condition')}</span><textarea className="input" rows={3} placeholder={t('例如:根据资料起草或改写文章', 'For example: draft or rewrite an article from supplied materials')} data-decision-field={`branch-${index}-condition`} value={branch.condition} onChange={e => put(branch, { condition: e.target.value })} /><span className="field-help">{extension ? t('扩展根据此条件选择任务类别和模型组。', 'The extension uses this condition to choose the category and group.') : t('描述任务类别。简单与复杂的判断在下方“判断设置”中调整。', 'Describe the task category. Adjust simple and complex criteria in Judgment settings below.')}</span></label></div>
        {group(t('常规模型', 'Regular models'), extension ? t('扩展选择常规组时使用,按顺序尝试。', 'Used when the extension selects regular, tried in this order.') : t('处理常规任务,按顺序尝试。', 'Handle regular tasks, tried in this order.'), branch.candidates, candidates => put(branch, { candidates }), { groupId: branch.id, preferred: 'low' })}
        {(dual || primaryEditors.includes(branch.id)) ? <>
          {group(t('主力模型', 'Primary models'), extension ? t('扩展选择主力组时使用,按顺序尝试。', 'Used when the extension selects primary, tried in this order.') : t('任务复杂或上一阶段不胜任时使用,按顺序尝试。', 'Used for complex tasks or low competence, tried in this order.'), branch.primary_candidates, primary_candidates => put(branch, { primary_candidates }), { groupId: branch.id + '-primary', preferred: 'high' })}
          <button className="btn btn-quiet" type="button" onClick={() => { put(branch, { primary_candidates: [] }); setPrimaryEditors(ids => ids.filter(id => id !== branch.id)); }}>{t('仅使用常规模型', 'Use regular models only')}</button>
        </> : <button className="btn" type="button" onClick={() => setPrimaryEditors(ids => [...ids, branch.id])}><UiIcon name="plus" />{t('添加主力模型(可选)', 'Add primary models (optional)')}</button>}
        {dual && !extension && <TrustLevelControl classifier={routing.classifier} value={judgment} onChange={judgment => put(branch, { judgment })} id={`branch-${index}`} language={language} replayScope={replayPlan ? { ...replayPlan, branchId: branch.id, savedThreshold: replayPlan.savedThresholds[branch.id] ?? judgment.degree.simple_threshold_millis } : undefined} complete={newPlan ? 0 : evidence?.find(item => item.branch_id === branch.id)?.full_history.complete} evidenceError={evidenceError} />}
        {!extension && <Disclosure label={t(`判断设置 · ${branch.judgment ? '单独调整' : '使用计划默认'} · ${judgmentSummary(judgment, dual, language)}`, `Judgment settings · ${branch.judgment ? 'Customized' : 'Plan defaults'} · ${judgmentSummary(judgment, dual, language)}`)} language={language}>
          <div className="field-actions"><p className="field-help">{branch.judgment ? t('本分支使用独立的完整判断设置。', 'This branch uses its own complete judgment settings.') : t('跟随计划默认,单独调整后整套设置独立生效。', 'Follows plan defaults. Customize to make all settings independent.')}</p><button className="btn" type="button" onClick={() => put(branch, { judgment: branch.judgment ? null : structuredClone(routing.judgment) })}>{branch.judgment ? t('恢复计划默认', 'Restore plan defaults') : t('单独调整', 'Customize')}</button></div>
          <JudgmentFields native={routing.classifier.kind === 'decision_service' && routing.classifier.service.connection.kind === 'system_one'} value={judgment} onChange={judgment => put(branch, { judgment })} id={`branch-${index}`} dual={dual} hideThreshold={dual} disabled={!branch.judgment} language={language} />
        </Disclosure>}
      </section>;
    })}
    <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>
    {!extension && <section className="editor-section"><Disclosure label={t(`计划默认判断设置 · ${judgmentSummary(routing.judgment, true, language)}`, `Plan default judgment · ${judgmentSummary(routing.judgment, true, language)}`)} language={language}><JudgmentFields native={routing.classifier.kind === 'decision_service' && routing.classifier.service.connection.kind === 'system_one'} value={routing.judgment} onChange={judgment => patch({ judgment })} id="global" language={language} /></Disclosure></section>}
    <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>
  </>;
}