cc-switchboard 0.1.0 → 0.2.10
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.claude-plugin/plugin.json +4 -3
- package/README.md +39 -37
- package/hooks/config.ts +79 -0
- package/hooks/hooks.json +1 -50
- package/hooks/switchboard.tsx +417 -0
- package/package.json +5 -5
- package/src/cli.js +1 -6
- package/src/core/classify.js +16 -9
- package/types/index.d.ts +42 -0
- package/agents/router-chores.md +0 -6
- package/agents/router-code.md +0 -6
- package/agents/router-hard.md +0 -6
- package/agents/router-plan.md +0 -7
- package/agents/router-review.md +0 -7
- package/agents/router-write.md +0 -6
- package/commands/router.md +0 -8
- package/hooks/on-agent-post.js +0 -3
- package/hooks/on-agent-pre.js +0 -3
- package/hooks/on-prompt.js +0 -3
- package/hooks/on-session-start.js +0 -3
- package/src/core/hooks.js +0 -92
- package/src/core/status.js +0 -11
- package/src/core/transcript.js +0 -34
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "switchboard",
|
|
3
|
-
"description": "
|
|
4
|
-
"version": "0.
|
|
3
|
+
"description": "Per-turn model routing for Claude Code: each prompt runs on the right model and effort (Fable plans and reviews, Opus does hard code and UI, Sonnet does regular code and writing, Haiku does chores), with a band above the prompt and a /router pane.",
|
|
4
|
+
"version": "0.2.10",
|
|
5
5
|
"author": {
|
|
6
6
|
"name": "Siamak Rostami"
|
|
7
7
|
},
|
|
@@ -16,5 +16,6 @@
|
|
|
16
16
|
"sonnet",
|
|
17
17
|
"haiku"
|
|
18
18
|
],
|
|
19
|
-
"homepage": "https://github.com/siamakrostami/ClaudeModelRouter"
|
|
19
|
+
"homepage": "https://github.com/siamakrostami/ClaudeModelRouter",
|
|
20
|
+
"types": "./types/index.d.ts"
|
|
20
21
|
}
|
package/README.md
CHANGED
|
@@ -4,27 +4,37 @@
|
|
|
4
4
|
[](https://nodejs.org)
|
|
5
5
|
[](LICENSE)
|
|
6
6
|
|
|
7
|
-
|
|
8
|
-
|
|
7
|
+
Per-turn model routing for Claude Code. You type your request as usual, and Switchboard
|
|
8
|
+
sends that turn to the model and effort that suit it:
|
|
9
9
|
|
|
10
10
|
| Role | Handles | Default |
|
|
11
11
|
|---|---|---|
|
|
12
|
-
| plan | planning, architecture, big decisions
|
|
12
|
+
| plan | planning, architecture, big decisions | Fable · xhigh |
|
|
13
13
|
| review | code review | Fable · high |
|
|
14
14
|
| hard | hard code, UI, debugging | Opus · high |
|
|
15
15
|
| code | regular code, tests, refactors | Sonnet · medium |
|
|
16
16
|
| write | PR descriptions, Slack, docs, tickets | Sonnet · medium |
|
|
17
17
|
| chores | git, renames, formatting, lookups, commit messages | Haiku · low |
|
|
18
18
|
|
|
19
|
+
A prompt that matches no role stays on your main model.
|
|
20
|
+
|
|
19
21
|
## How it works
|
|
20
22
|
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
-
|
|
25
|
-
|
|
26
|
-
- **
|
|
27
|
-
|
|
23
|
+
Switchboard is a Claude Code mod:
|
|
24
|
+
|
|
25
|
+
- **`prompt.submit`** sorts the prompt into a role with keyword rules.
|
|
26
|
+
- **`turn.step`** sends every main-chat request of that turn with the role's model and
|
|
27
|
+
effort. Nothing goes through a proxy or a helper agent, and subagents are left alone.
|
|
28
|
+
- **The band above the prompt** shows the model, an effort meter, the role, and why it was
|
|
29
|
+
picked:
|
|
30
|
+
|
|
31
|
+
```
|
|
32
|
+
▮▮▮▯▯ Opus · high hard ← debug default Router
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
- **`/router`** opens a pane:
|
|
36
|
+
- **Now**: this turn's route, a pin, and a setup picker.
|
|
37
|
+
- **Setups**: the model and effort for each role, plus save as.
|
|
28
38
|
|
|
29
39
|
## Install
|
|
30
40
|
|
|
@@ -35,48 +45,40 @@ claude plugin install switchboard@switchboard
|
|
|
35
45
|
```
|
|
36
46
|
|
|
37
47
|
From a clone instead: `npm install && npm link`, then `claude plugin marketplace add "$PWD"`
|
|
38
|
-
and the same `claude plugin install` line.
|
|
48
|
+
and the same `claude plugin install` line. Needs Claude Code 2.1.330 or newer.
|
|
39
49
|
|
|
40
|
-
Set
|
|
41
|
-
segment in your status line, add this to your status line script:
|
|
42
|
-
|
|
43
|
-
```bash
|
|
44
|
-
seg=$(switchboard status-segment "$session_id")
|
|
45
|
-
```
|
|
50
|
+
Set your main model to Fable (`"model": "fable"` in `~/.claude/settings.json`).
|
|
46
51
|
|
|
47
52
|
## Use
|
|
48
53
|
|
|
49
|
-
|
|
50
|
-
applies only to the prompt it appears in:
|
|
54
|
+
Just type. To steer one prompt, include one of these:
|
|
51
55
|
|
|
52
56
|
- `#plan` `#review` `#hard` `#code` `#write` `#chores`: force a role
|
|
53
|
-
- `@fable` `@opus` `@sonnet` `@haiku
|
|
54
|
-
- `#self
|
|
57
|
+
- `@fable` `@opus` `@sonnet` `@haiku`, or "use haiku for this": force a model
|
|
58
|
+
- `#self` or "do it yourself": stay on the main model
|
|
55
59
|
|
|
56
|
-
|
|
60
|
+
In a session:
|
|
61
|
+
|
|
62
|
+
- `/router`: open the pane
|
|
63
|
+
- `/router hard`: pin every turn to one role; `/router auto` unpins
|
|
64
|
+
- `/router off` / `/router on`: pause or resume this session
|
|
65
|
+
|
|
66
|
+
If you switch models yourself with `/model`, Switchboard steps aside until you switch back
|
|
67
|
+
to the main model (set `main_model` in `setups.json` if yours is not Fable).
|
|
68
|
+
|
|
69
|
+
From a terminal:
|
|
57
70
|
|
|
58
71
|
```bash
|
|
59
|
-
switchboard #
|
|
72
|
+
switchboard # full-screen setup picker
|
|
60
73
|
switchboard show | list
|
|
61
74
|
switchboard set code opus high
|
|
62
75
|
switchboard save fast # save the current setup under a name
|
|
63
76
|
switchboard use default
|
|
64
|
-
switchboard off | on #
|
|
77
|
+
switchboard off | on # everywhere
|
|
65
78
|
```
|
|
66
79
|
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
Data lives in `~/.claude/switchboard/`: `setups.json` and `rules.json` (keyword regexes,
|
|
70
|
-
which you can edit) and `sessions/` (per-session state). Errors go to `error.log`.
|
|
71
|
-
|
|
72
|
-
## Switching models yourself
|
|
73
|
-
|
|
74
|
-
If you change the main chat's model with `/model`, Switchboard steps aside for that
|
|
75
|
-
session: no routing notes, and helpers are left alone. The status line shows
|
|
76
|
-
`⇢ manual (opus)`. Switch back to Fable (the configured main model) and routing resumes
|
|
77
|
-
on your next prompt. To turn routing off or on explicitly, use `/router off` or
|
|
78
|
-
`switchboard off` (and `on`). If your main model is not Fable, set `main_model` in
|
|
79
|
-
`setups.json` (or `SWITCHBOARD_MAIN_MODEL`).
|
|
80
|
+
Data lives in `~/.claude/switchboard/`: `setups.json`, and `rules.json` (keyword regexes,
|
|
81
|
+
which you can edit).
|
|
80
82
|
|
|
81
83
|
## Test
|
|
82
84
|
|
package/hooks/config.ts
ADDED
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
import { DEFAULT_RULES, DEFAULT_SETUP, EFFORTS, MODELS, ROLES } from '../src/core/defaults.js'
|
|
2
|
+
|
|
3
|
+
import type { Config, Setup } from '../types'
|
|
4
|
+
|
|
5
|
+
export const MODEL_COLOR: Record<string, string> = {
|
|
6
|
+
fable: '#C084FC',
|
|
7
|
+
opus: '#F59E0B',
|
|
8
|
+
sonnet: '#22D3EE',
|
|
9
|
+
haiku: '#4ADE80',
|
|
10
|
+
}
|
|
11
|
+
export const MODEL_NAME: Record<string, string> = { fable: 'Fable', opus: 'Opus', sonnet: 'Sonnet', haiku: 'Haiku' }
|
|
12
|
+
export const ROLE_LIST: readonly string[] = ROLES
|
|
13
|
+
export const MODEL_LIST: readonly string[] = MODELS
|
|
14
|
+
export const EFFORT_LIST: readonly string[] = EFFORTS
|
|
15
|
+
|
|
16
|
+
export function family(model: string | undefined) {
|
|
17
|
+
return /(fable|opus|sonnet|haiku)/i.exec(model ?? '')?.[1]?.toLowerCase() ?? ''
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
export function meter(effort: string) {
|
|
21
|
+
const level = Math.max(0, EFFORT_LIST.indexOf(effort)) + 1
|
|
22
|
+
return { on: '●'.repeat(level), off: '○'.repeat(EFFORT_LIST.length - level) }
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
function fill(setup: any): Setup {
|
|
26
|
+
const out: Setup = {}
|
|
27
|
+
for (const r of ROLE_LIST) {
|
|
28
|
+
const s = setup && typeof setup[r] === 'object' ? setup[r] : {}
|
|
29
|
+
out[r] = {
|
|
30
|
+
model: MODEL_LIST.includes(s.model) ? s.model : (DEFAULT_SETUP as any)[r].model,
|
|
31
|
+
effort: EFFORT_LIST.includes(s.effort) ? s.effort : (DEFAULT_SETUP as any)[r].effort,
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
return out
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
export async function home($: any) {
|
|
38
|
+
const custom = await $.env.get('SWITCHBOARD_HOME')
|
|
39
|
+
return custom || `${await $.env.get('HOME')}/.claude/switchboard`
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
export async function readJson($: any, path: string) {
|
|
43
|
+
try {
|
|
44
|
+
return JSON.parse(await $.fs.read(path))
|
|
45
|
+
} catch {
|
|
46
|
+
return null
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
/** Read setups.json and rules.json (same files the switchboard CLI edits), filling defaults. */
|
|
51
|
+
export async function loadConfig($: any): Promise<Config> {
|
|
52
|
+
const dir = await home($)
|
|
53
|
+
const raw = (await readJson($, `${dir}/setups.json`)) || {}
|
|
54
|
+
const setups: Record<string, Setup> = {}
|
|
55
|
+
for (const [name, s] of Object.entries<any>(raw.setups || {})) setups[name] = fill(s)
|
|
56
|
+
if (!Object.keys(setups).length) setups.default = fill(null)
|
|
57
|
+
const active = typeof raw.active === 'string' && setups[raw.active] ? raw.active : Object.keys(setups)[0]
|
|
58
|
+
const rulesRaw = (await readJson($, `${dir}/rules.json`)) || {}
|
|
59
|
+
const rules: Record<string, string[]> = {}
|
|
60
|
+
for (const r of ROLE_LIST) {
|
|
61
|
+
const v = rulesRaw[r]
|
|
62
|
+
rules[r] = Array.isArray(v) && v.every((x: unknown) => typeof x === 'string') ? v : (DEFAULT_RULES as any)[r]
|
|
63
|
+
}
|
|
64
|
+
return {
|
|
65
|
+
active,
|
|
66
|
+
enabled: raw.enabled !== false,
|
|
67
|
+
mainModel: MODEL_LIST.includes(raw.main_model) ? raw.main_model : 'fable',
|
|
68
|
+
setups,
|
|
69
|
+
rules,
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
/** Write setups.json back in the CLI's format, keeping keys the mod does not know. */
|
|
74
|
+
export async function saveConfig($: any, cfg: Config) {
|
|
75
|
+
const dir = await home($)
|
|
76
|
+
const raw = (await readJson($, `${dir}/setups.json`)) || {}
|
|
77
|
+
const out = { ...raw, active: cfg.active, enabled: cfg.enabled, main_model: cfg.mainModel, setups: cfg.setups }
|
|
78
|
+
await $.fs.write(`${dir}/setups.json`, JSON.stringify(out, null, 2) + '\n')
|
|
79
|
+
}
|
package/hooks/hooks.json
CHANGED
|
@@ -1,50 +1 @@
|
|
|
1
|
-
{
|
|
2
|
-
"hooks": {
|
|
3
|
-
"SessionStart": [
|
|
4
|
-
{
|
|
5
|
-
"hooks": [
|
|
6
|
-
{
|
|
7
|
-
"type": "command",
|
|
8
|
-
"command": "node \"${CLAUDE_PLUGIN_ROOT}/hooks/on-session-start.js\"",
|
|
9
|
-
"timeout": 5
|
|
10
|
-
}
|
|
11
|
-
]
|
|
12
|
-
}
|
|
13
|
-
],
|
|
14
|
-
"UserPromptSubmit": [
|
|
15
|
-
{
|
|
16
|
-
"hooks": [
|
|
17
|
-
{
|
|
18
|
-
"type": "command",
|
|
19
|
-
"command": "node \"${CLAUDE_PLUGIN_ROOT}/hooks/on-prompt.js\"",
|
|
20
|
-
"timeout": 5
|
|
21
|
-
}
|
|
22
|
-
]
|
|
23
|
-
}
|
|
24
|
-
],
|
|
25
|
-
"PreToolUse": [
|
|
26
|
-
{
|
|
27
|
-
"matcher": "Agent|Task",
|
|
28
|
-
"hooks": [
|
|
29
|
-
{
|
|
30
|
-
"type": "command",
|
|
31
|
-
"command": "node \"${CLAUDE_PLUGIN_ROOT}/hooks/on-agent-pre.js\"",
|
|
32
|
-
"timeout": 5
|
|
33
|
-
}
|
|
34
|
-
]
|
|
35
|
-
}
|
|
36
|
-
],
|
|
37
|
-
"PostToolUse": [
|
|
38
|
-
{
|
|
39
|
-
"matcher": "Agent|Task",
|
|
40
|
-
"hooks": [
|
|
41
|
-
{
|
|
42
|
-
"type": "command",
|
|
43
|
-
"command": "node \"${CLAUDE_PLUGIN_ROOT}/hooks/on-agent-post.js\"",
|
|
44
|
-
"timeout": 5
|
|
45
|
-
}
|
|
46
|
-
]
|
|
47
|
-
}
|
|
48
|
-
]
|
|
49
|
-
}
|
|
50
|
-
}
|
|
1
|
+
{ "modules": ["./switchboard.tsx"] }
|
|
@@ -0,0 +1,417 @@
|
|
|
1
|
+
import { atom, read, update } from 'claude-code'
|
|
2
|
+
import type { Register } from 'claude-code'
|
|
3
|
+
|
|
4
|
+
import { classify } from '../src/core/classify.js'
|
|
5
|
+
import type { Config, Live, Route, Setup } from '../types'
|
|
6
|
+
import {
|
|
7
|
+
EFFORT_LIST,
|
|
8
|
+
MODEL_COLOR,
|
|
9
|
+
MODEL_LIST,
|
|
10
|
+
MODEL_NAME,
|
|
11
|
+
ROLE_LIST,
|
|
12
|
+
family,
|
|
13
|
+
loadConfig,
|
|
14
|
+
meter,
|
|
15
|
+
saveConfig,
|
|
16
|
+
} from './config'
|
|
17
|
+
|
|
18
|
+
const PANE = 'switchboard'
|
|
19
|
+
|
|
20
|
+
const config = atom({ plugin: 'switchboard', key: 'config' } as const, null, { shape: 'config-1' })
|
|
21
|
+
const route = atom({ plugin: 'switchboard', key: 'route' } as const, null, { shape: 'route-1' })
|
|
22
|
+
const live = atom({ plugin: 'switchboard', key: 'live' } as const, null, { shape: 'live-1' })
|
|
23
|
+
const paused = atom({ plugin: 'switchboard', key: 'paused' } as const, false)
|
|
24
|
+
const manual = atom({ plugin: 'switchboard', key: 'manual' } as const, null)
|
|
25
|
+
const pin = atom({ plugin: 'switchboard', key: 'pin' } as const, null)
|
|
26
|
+
const place = atom({ plugin: 'switchboard', key: 'place' } as const, null)
|
|
27
|
+
const base = atom({ plugin: 'switchboard', key: 'base' } as const, null)
|
|
28
|
+
const tab = atom({ plugin: 'switchboard', key: 'tab' } as const, 'now')
|
|
29
|
+
|
|
30
|
+
async function reload($: any) {
|
|
31
|
+
const cfg = await loadConfig($)
|
|
32
|
+
await update($, config, () => cfg)
|
|
33
|
+
return cfg
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/** Change the config, save it to setups.json and keep the atom in step. */
|
|
37
|
+
async function edit($: any, fn: (cfg: Config) => Config) {
|
|
38
|
+
const cfg = fn(structuredClone((await read($, config)) ?? (await loadConfig($))))
|
|
39
|
+
await saveConfig($, cfg)
|
|
40
|
+
await update($, config, () => cfg)
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/** Decide this turn's route from the prompt, a pin, or nothing (stay on the session's model). */
|
|
44
|
+
async function decide($: any, text: string) {
|
|
45
|
+
const cfg = await reload($)
|
|
46
|
+
const sessionModel = family(String(await $.session.model()))
|
|
47
|
+
// Manual only when the model was changed with /model after the session started.
|
|
48
|
+
const started = await read($, base)
|
|
49
|
+
if (!started && sessionModel) await update($, base, () => sessionModel)
|
|
50
|
+
const isManual = sessionModel !== '' && !!started && sessionModel !== started
|
|
51
|
+
await update($, manual, () => (isManual ? sessionModel : null))
|
|
52
|
+
if (!cfg.enabled || isManual || (await read($, paused))) return null
|
|
53
|
+
const setup = cfg.setups[cfg.active]
|
|
54
|
+
const pinned = await read($, pin)
|
|
55
|
+
if (pinned && MODEL_LIST.includes(pinned)) {
|
|
56
|
+
// Pinned to a model: use the effort of the first task that runs on it.
|
|
57
|
+
const role = ROLE_LIST.find(r => setup[r]?.model === pinned) ?? 'code'
|
|
58
|
+
const effort = setup[role]?.model === pinned ? setup[role].effort : 'high'
|
|
59
|
+
return { role, model: pinned, effort, why: '', source: 'pin' } as Route
|
|
60
|
+
}
|
|
61
|
+
const r = classify(text, cfg.rules, setup)
|
|
62
|
+
if (!r) return null
|
|
63
|
+
return { role: r.role, model: r.model, effort: r.effort, why: r.why, source: r.forced ? 'override' : 'auto' } as Route
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
/** Turn routing on or off from the pane. On also clears a saved "enabled: false". */
|
|
67
|
+
async function power($: any, turnOn: boolean) {
|
|
68
|
+
await update($, paused, () => !turnOn)
|
|
69
|
+
if (turnOn && (await read($, config))?.enabled === false) await edit($, c => ({ ...c, enabled: true }))
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
/** Set one task's model or effort in the active setup. */
|
|
73
|
+
async function setSlot($: any, role: string, field: 'model' | 'effort', value: string) {
|
|
74
|
+
await edit($, c => {
|
|
75
|
+
const setup = c.setups[c.active]
|
|
76
|
+
const slot = setup?.[role]
|
|
77
|
+
if (!setup || !slot) return c
|
|
78
|
+
return { ...c, setups: { ...c.setups, [c.active]: { ...setup, [role]: { ...slot, [field]: value } } } }
|
|
79
|
+
})
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
type Mode = 'auto' | 'pinned' | 'main' | 'manual' | 'off'
|
|
83
|
+
|
|
84
|
+
/** What the band and the pane both need: what this turn runs on, and in which mode. */
|
|
85
|
+
function status(cfg: Config, r: Route | null, l: Live | null, isPaused: boolean, man: string | null) {
|
|
86
|
+
const mode: Mode = isPaused || !cfg.enabled ? 'off' : man ? 'manual' : r ? (r.source === 'pin' ? 'pinned' : 'auto') : 'main'
|
|
87
|
+
const routed = mode === 'auto' || mode === 'pinned'
|
|
88
|
+
const idle = l ?? { model: cfg.mainModel, effort: cfg.setups[cfg.active]?.plan?.effort ?? '' }
|
|
89
|
+
const shown = mode === 'manual' ? { model: man as string, effort: l?.effort ?? '' } : routed && r ? { model: r.model, effort: r.effort } : idle
|
|
90
|
+
return {
|
|
91
|
+
mode,
|
|
92
|
+
model: shown.model,
|
|
93
|
+
effort: shown.effort,
|
|
94
|
+
role: routed && r ? r.role : '',
|
|
95
|
+
why: mode === 'auto' && r?.why ? r.why : '',
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
/** Display form of a task, mode or effort word: Plan, Manual, High. */
|
|
100
|
+
const cap = (w: string) => (w ? w[0].toUpperCase() + w.slice(1) : w)
|
|
101
|
+
const EFFORT_LABEL: Record<string, string> = { low: 'Low', medium: 'Medium', high: 'High', xhigh: 'X-High', max: 'Max' }
|
|
102
|
+
const effortLabel = (w: string) => EFFORT_LABEL[w] ?? cap(w)
|
|
103
|
+
|
|
104
|
+
/** The one-line route: meter, model, effort, then the task or the mode. Never wraps. */
|
|
105
|
+
function routeLine(Text: any, s: ReturnType<typeof status>, showEffort = true) {
|
|
106
|
+
const off = s.mode === 'off'
|
|
107
|
+
const color = off ? undefined : MODEL_COLOR[s.model] ?? 'gray'
|
|
108
|
+
const m = meter(s.effort)
|
|
109
|
+
return (
|
|
110
|
+
<Text wrap="truncate-end">
|
|
111
|
+
<Text color={color} dimColor={off}>{m.on}</Text>
|
|
112
|
+
<Text dimColor>{m.off}</Text>
|
|
113
|
+
<Text color={color} dimColor={off} bold={!off}> {MODEL_NAME[s.model] ?? s.model}</Text>
|
|
114
|
+
{s.effort && showEffort ? <Text dimColor> · {effortLabel(s.effort)}</Text> : null}
|
|
115
|
+
{s.mode === 'pinned' ? <Text dimColor>{' '}Pinned</Text> : null}
|
|
116
|
+
{s.mode === 'manual' ? <Text color="warning">{' '}Manual</Text> : null}
|
|
117
|
+
{off ? <Text dimColor>{' '}Off</Text> : null}
|
|
118
|
+
</Text>
|
|
119
|
+
)
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
/** Context use as a bar, coloured green → amber → red, always shown with its percentage. */
|
|
123
|
+
function gauge(pct: number, width: number) {
|
|
124
|
+
const filled = Math.max(0, Math.min(width, Math.round((pct / 100) * width)))
|
|
125
|
+
const color = pct >= 80 ? '#F87171' : pct >= 60 ? '#F59E0B' : '#4ADE80'
|
|
126
|
+
return { on: '▰'.repeat(filled), off: '▱'.repeat(width - filled), color }
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
/** Context use as a circle that fills by quarters, coloured by level; always shown with its percentage. */
|
|
130
|
+
function ring(pct: number) {
|
|
131
|
+
const glyph = pct < 13 ? '○' : pct < 38 ? '◔' : pct < 63 ? '◑' : pct < 88 ? '◕' : '●'
|
|
132
|
+
return { glyph, color: gauge(pct, 1).color }
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
/** Open the pane on one of its tabs. */
|
|
136
|
+
async function openPane($: any, which: 'now' | 'routes') {
|
|
137
|
+
await update($, tab, () => which)
|
|
138
|
+
await $.ui.open({ id: PANE, title: 'Switchboard' })
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
/**
|
|
142
|
+
* Pad a dropdown label so every dropdown in a column is the same width (Select has no width
|
|
143
|
+
* prop). Terminal: plain spaces to the longest label. Desktop: figure spaces, tuned to the
|
|
144
|
+
* app's proportional font.
|
|
145
|
+
*/
|
|
146
|
+
const DESKTOP_PAD: Record<string, number> = { Fable: 2, Opus: 2, Sonnet: 0, Haiku: 2, Low: 3, Medium: 0, High: 3, 'X-High': 0, Max: 3 }
|
|
147
|
+
function padLabel(label: string, all: readonly string[], surface: string) {
|
|
148
|
+
if (surface === 'terminal') return label.padEnd(Math.max(...all.map(x => x.length)), ' ')
|
|
149
|
+
return label + '\u2007'.repeat(DESKTOP_PAD[label] ?? 0)
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
/** Project, branch and context percentage for the band. Each part is best effort. */
|
|
153
|
+
async function where($: any) {
|
|
154
|
+
const root = String(await $.session.root())
|
|
155
|
+
const project = root.split('/').filter(Boolean).pop() ?? root
|
|
156
|
+
const copy = await $.vcs.detect({ path: root }).catch(() => null)
|
|
157
|
+
const usage = await $.session.usage().catch(() => null)
|
|
158
|
+
const pct = usage?.context?.percent
|
|
159
|
+
const value: Place = { project, branch: copy?.branch ?? null, ctx: typeof pct === 'number' ? pct : null }
|
|
160
|
+
await update($, place, () => value)
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
export const register: Register = on => {
|
|
164
|
+
on('session.start', async ($, e, next) => {
|
|
165
|
+
await $.command.register({ name: 'router', description: 'Switchboard settings: models per task, setups, pause (also: /router off|on|auto|<task>)' })
|
|
166
|
+
await update($, route, () => null)
|
|
167
|
+
await reload($).catch(() => {})
|
|
168
|
+
await where($).catch(() => {})
|
|
169
|
+
const startModel = family(String(await $.session.model().catch(() => '')))
|
|
170
|
+
await update($, base, () => startModel || null)
|
|
171
|
+
await update($, manual, () => null)
|
|
172
|
+
return next(e)
|
|
173
|
+
})
|
|
174
|
+
|
|
175
|
+
on('command.run', { command: 'router' }, async ($, e) => {
|
|
176
|
+
const arg = e.args.trim().toLowerCase()
|
|
177
|
+
if (arg === 'off' || arg === 'pause') {
|
|
178
|
+
await update($, paused, () => true)
|
|
179
|
+
return { text: 'Switchboard paused for this session.' }
|
|
180
|
+
}
|
|
181
|
+
if (arg === 'on' || arg === 'resume') {
|
|
182
|
+
await update($, paused, () => false)
|
|
183
|
+
return { text: 'Switchboard routing on.' }
|
|
184
|
+
}
|
|
185
|
+
if (arg === 'auto') {
|
|
186
|
+
await update($, pin, () => null)
|
|
187
|
+
return { text: 'Switchboard: back to automatic routing.' }
|
|
188
|
+
}
|
|
189
|
+
if (MODEL_LIST.includes(arg)) {
|
|
190
|
+
await update($, pin, () => arg)
|
|
191
|
+
return { text: `Switchboard: every turn uses ${MODEL_NAME[arg]}. /router auto to undo.` }
|
|
192
|
+
}
|
|
193
|
+
await reload($).catch(() => {})
|
|
194
|
+
await $.ui.open({ id: PANE, title: 'Switchboard' })
|
|
195
|
+
return { text: 'Switchboard pane opened.' }
|
|
196
|
+
})
|
|
197
|
+
|
|
198
|
+
on('turn.complete', async ($, e, next) => {
|
|
199
|
+
const r = await next(e)
|
|
200
|
+
if (!e.agentId) await where($).catch(() => {})
|
|
201
|
+
return r
|
|
202
|
+
})
|
|
203
|
+
|
|
204
|
+
on('prompt.submit', async ($, e, next) => {
|
|
205
|
+
if (!e.agentId) await where($).catch(() => {})
|
|
206
|
+
if (e.agentId || e.command) return next(e)
|
|
207
|
+
const r = await decide($, e.text).catch(() => null)
|
|
208
|
+
await update($, route, () => r)
|
|
209
|
+
return next(e)
|
|
210
|
+
})
|
|
211
|
+
|
|
212
|
+
on('turn.step', async function* ($, e, next) {
|
|
213
|
+
if (e.agentId) return yield* next(e)
|
|
214
|
+
const r = await read($, route)
|
|
215
|
+
if (!r) {
|
|
216
|
+
const sent: Live = { model: family(e.model), effort: typeof e.effort === 'string' ? e.effort : '' }
|
|
217
|
+
await update($, live, () => sent)
|
|
218
|
+
return yield* next(e)
|
|
219
|
+
}
|
|
220
|
+
await update($, live, () => ({ model: r.model, effort: r.effort }))
|
|
221
|
+
return yield* next({ ...e, model: r.model, effort: r.effort as any })
|
|
222
|
+
})
|
|
223
|
+
|
|
224
|
+
// ---- the band above the prompt: one row, route on the left, controls on the right ----
|
|
225
|
+
on('ui.render', { component: 'AbovePrompt' }, async ($, e, next) => {
|
|
226
|
+
if (e.props.hasSurvey) return next(e)
|
|
227
|
+
const cfg = await read($, config)
|
|
228
|
+
if (!cfg) return next(e)
|
|
229
|
+
const { Box, Text, Button } = $.ui.resolve(e)
|
|
230
|
+
const s = status(cfg, await read($, route), await read($, live), await read($, paused), await read($, manual))
|
|
231
|
+
const pinned = await read($, pin)
|
|
232
|
+
const cols = e.props.bodyColumns ?? 80
|
|
233
|
+
const pl = await read($, place)
|
|
234
|
+
|
|
235
|
+
const row = (
|
|
236
|
+
<Box paddingX={1} justifyContent="space-between" gap={2}>
|
|
237
|
+
<Box gap={2} flexShrink={1} overflow="hidden">
|
|
238
|
+
<Box flexShrink={0}>{routeLine(Text, s, cols >= 48)}</Box>
|
|
239
|
+
</Box>
|
|
240
|
+
<Box gap={1} flexShrink={0}>
|
|
241
|
+
{pl && cols >= 90 ? (
|
|
242
|
+
<Text dimColor wrap="truncate-end">
|
|
243
|
+
{pl.project}
|
|
244
|
+
{pl.branch ? ` ⎇ ${pl.branch}` : ''}
|
|
245
|
+
</Text>
|
|
246
|
+
) : null}
|
|
247
|
+
{pl?.ctx != null ? (
|
|
248
|
+
<Text>
|
|
249
|
+
<Text color={ring(pl.ctx).color}>{ring(pl.ctx).glyph}</Text>
|
|
250
|
+
<Text dimColor={pl.ctx < 60} color={pl.ctx >= 60 ? ring(pl.ctx).color : undefined}> {pl.ctx}%</Text>
|
|
251
|
+
</Text>
|
|
252
|
+
) : null}
|
|
253
|
+
<Button key="pin" label={pinned ? `${MODEL_NAME[pinned] ?? cap(pinned)} ▾` : 'Auto ▾'} variant="secondary" onPress={() => openPane($, 'now')} />
|
|
254
|
+
<Button key="settings" label="⚙️" variant="secondary" onPress={() => openPane($, 'routes')} />
|
|
255
|
+
</Box>
|
|
256
|
+
</Box>
|
|
257
|
+
)
|
|
258
|
+
return row
|
|
259
|
+
})
|
|
260
|
+
|
|
261
|
+
// ---- the /router pane: tabs and on/off on top, one aligned row per task below ----
|
|
262
|
+
on('ui.render', { component: 'Pane', requestId: PANE }, async ($, e) => {
|
|
263
|
+
const { Box, Text, Button, Select } = $.ui.resolve(e)
|
|
264
|
+
const cfg = await read($, config)
|
|
265
|
+
if (!cfg) return <Text dimColor>Loading…</Text>
|
|
266
|
+
const current = (await read($, tab)) === 'routes' ? 'routes' : 'now'
|
|
267
|
+
const s = status(cfg, await read($, route), await read($, live), await read($, paused), await read($, manual))
|
|
268
|
+
const pinned = await read($, pin)
|
|
269
|
+
const pl = await read($, place)
|
|
270
|
+
const isOff = s.mode === 'off'
|
|
271
|
+
const setup: Setup = cfg.setups[cfg.active] ?? {}
|
|
272
|
+
const slotOf = (role: string) => setup[role] ?? { model: cfg.mainModel, effort: '' }
|
|
273
|
+
const startedOn = await read($, base)
|
|
274
|
+
const cols = e.props.bodyColumns ?? 60
|
|
275
|
+
|
|
276
|
+
// Minimized on the desktop: the route alone.
|
|
277
|
+
if (e.props.placement === 'widget') return routeLine(Text, s)
|
|
278
|
+
|
|
279
|
+
// A small section header, like a grouped list's.
|
|
280
|
+
const section = (title: string) => (
|
|
281
|
+
<Box marginTop={1} marginBottom={e.surface === 'terminal' ? 0 : 1}>
|
|
282
|
+
<Text dimColor size="caption" weight="semibold">{title.toUpperCase()}</Text>
|
|
283
|
+
</Box>
|
|
284
|
+
)
|
|
285
|
+
const accent = isOff ? undefined : MODEL_COLOR[s.model] ?? 'gray'
|
|
286
|
+
const m = meter(s.effort)
|
|
287
|
+
|
|
288
|
+
const header = (
|
|
289
|
+
<Box justifyContent="space-between" alignItems="center">
|
|
290
|
+
<Box gap={1}>
|
|
291
|
+
<Button key="tab-now" label="Overview" variant={current === 'now' ? 'primary' : 'secondary'} onPress={() => update($, tab, () => 'now')} />
|
|
292
|
+
<Button key="tab-routes" label="Models" variant={current === 'routes' ? 'primary' : 'secondary'} onPress={() => update($, tab, () => 'routes')} />
|
|
293
|
+
</Box>
|
|
294
|
+
<Box gap={1} alignItems="center">
|
|
295
|
+
<Text dimColor size="footnote">Routing</Text>
|
|
296
|
+
<Button key="power" label={isOff ? 'Off' : 'On'} variant={isOff ? 'secondary' : 'primary'} onPress={() => power($, isOff)} />
|
|
297
|
+
</Box>
|
|
298
|
+
</Box>
|
|
299
|
+
)
|
|
300
|
+
|
|
301
|
+
if (current === 'routes') {
|
|
302
|
+
return (
|
|
303
|
+
<Box flexDirection="column">
|
|
304
|
+
{header}
|
|
305
|
+
{section(`Models · ${cfg.active}`)}
|
|
306
|
+
<Box flexDirection="column" borderStyle="round" borderDimColor paddingX={1}>
|
|
307
|
+
{ROLE_LIST.map(role => {
|
|
308
|
+
const slot = slotOf(role)
|
|
309
|
+
return (
|
|
310
|
+
<Box key={`edit-${role}`} alignItems="center">
|
|
311
|
+
<Box width={10} flexShrink={0}>
|
|
312
|
+
<Text>{cap(role)}</Text>
|
|
313
|
+
</Box>
|
|
314
|
+
<Box width={14} flexShrink={0} flexDirection="column" marginRight={1}>
|
|
315
|
+
<Select key={`model-${role}`} options={MODEL_LIST.map(v => ({ value: v, label: padLabel(MODEL_NAME[v], MODEL_LIST.map(x => MODEL_NAME[x]), e.surface) }))} value={slot.model} onSelect={v => setSlot($, role, 'model', v)} />
|
|
316
|
+
</Box>
|
|
317
|
+
<Box width={14} flexShrink={0} flexDirection="column" marginRight={1}>
|
|
318
|
+
<Select key={`effort-${role}`} options={EFFORT_LIST.map(v => ({ value: v, label: padLabel(effortLabel(v), EFFORT_LIST.map(effortLabel), e.surface) }))} value={slot.effort} onSelect={v => setSlot($, role, 'effort', v)} />
|
|
319
|
+
</Box>
|
|
320
|
+
<Text color={MODEL_COLOR[slot.model]}>{meter(slot.effort).on}</Text>
|
|
321
|
+
<Text dimColor>{meter(slot.effort).off}</Text>
|
|
322
|
+
</Box>
|
|
323
|
+
)
|
|
324
|
+
})}
|
|
325
|
+
</Box>
|
|
326
|
+
{Object.keys(cfg.setups).length > 1 ? (
|
|
327
|
+
<Box flexDirection="column">
|
|
328
|
+
{section('Setup')}
|
|
329
|
+
<Box borderStyle="round" borderDimColor paddingX={1}>
|
|
330
|
+
<Select key="setup" options={Object.keys(cfg.setups).map(v => ({ value: v }))} value={cfg.active} onSelect={v => edit($, c => ({ ...c, active: v }))} />
|
|
331
|
+
</Box>
|
|
332
|
+
</Box>
|
|
333
|
+
) : null}
|
|
334
|
+
</Box>
|
|
335
|
+
)
|
|
336
|
+
}
|
|
337
|
+
|
|
338
|
+
const modeWord = s.mode === 'main' ? 'Auto' : cap(s.mode)
|
|
339
|
+
const ctx = pl?.ctx
|
|
340
|
+
|
|
341
|
+
return (
|
|
342
|
+
<Box flexDirection="column">
|
|
343
|
+
{header}
|
|
344
|
+
|
|
345
|
+
{section('Now')}
|
|
346
|
+
<Box flexDirection="column" borderStyle="round" borderColor={accent} borderDimColor={isOff} paddingX={1}>
|
|
347
|
+
<Box justifyContent="space-between" gap={2}>
|
|
348
|
+
<Text color={accent} dimColor={isOff} bold size="title" weight="bold">{MODEL_NAME[s.model] ?? s.model}</Text>
|
|
349
|
+
<Text color={s.mode === 'manual' ? 'warning' : accent} dimColor={s.mode === 'main' || isOff} weight="semibold">{modeWord}</Text>
|
|
350
|
+
</Box>
|
|
351
|
+
<Text wrap="truncate-end">
|
|
352
|
+
<Text color={accent} dimColor={isOff}>{m.on}</Text>
|
|
353
|
+
<Text dimColor>{m.off}</Text>
|
|
354
|
+
<Text dimColor size="footnote">{s.effort ? ` ${effortLabel(s.effort)} effort` : ''}{s.why ? ` ← ${s.why}` : ''}</Text>
|
|
355
|
+
</Text>
|
|
356
|
+
{s.mode === 'manual' ? (
|
|
357
|
+
<Text color="warning" size="footnote" wrap="truncate-end">/model {startedOn ?? cfg.mainModel} to resume routing</Text>
|
|
358
|
+
) : null}
|
|
359
|
+
</Box>
|
|
360
|
+
|
|
361
|
+
{section('Use')}
|
|
362
|
+
<Box borderStyle="round" borderDimColor paddingX={1} flexDirection="column">
|
|
363
|
+
<Box width={20} flexDirection="column">
|
|
364
|
+
<Select
|
|
365
|
+
key="use"
|
|
366
|
+
options={[{ value: 'auto', label: 'Auto' }, ...MODEL_LIST.map(v => ({ value: v, label: MODEL_NAME[v] }))]}
|
|
367
|
+
value={pinned ?? 'auto'}
|
|
368
|
+
onSelect={v => update($, pin, () => (v === 'auto' ? null : v))}
|
|
369
|
+
/>
|
|
370
|
+
</Box>
|
|
371
|
+
</Box>
|
|
372
|
+
|
|
373
|
+
{ctx != null ? (
|
|
374
|
+
<Box flexDirection="column">
|
|
375
|
+
{section('Context')}
|
|
376
|
+
<Box borderStyle="round" borderDimColor paddingX={1} gap={1} alignItems="center">
|
|
377
|
+
<Text color={ring(ctx).color} size="title">{ring(ctx).glyph}</Text>
|
|
378
|
+
<Text color={ctx >= 60 ? ring(ctx).color : undefined} weight="semibold">{ctx}%</Text>
|
|
379
|
+
<Text dimColor>used</Text>
|
|
380
|
+
</Box>
|
|
381
|
+
</Box>
|
|
382
|
+
) : null}
|
|
383
|
+
|
|
384
|
+
{section('Tasks')}
|
|
385
|
+
<Box flexDirection="column" borderStyle="round" borderDimColor paddingX={1}>
|
|
386
|
+
{ROLE_LIST.map(role => {
|
|
387
|
+
const slot = slotOf(role)
|
|
388
|
+
const isNow = !isOff && s.role === role
|
|
389
|
+
const mm = meter(slot.effort)
|
|
390
|
+
return (
|
|
391
|
+
<Box key={`row-${role}`} justifyContent="space-between" gap={1}>
|
|
392
|
+
<Box flexShrink={1} gap={1}>
|
|
393
|
+
<Box width={1} flexShrink={0}>
|
|
394
|
+
<Text color={MODEL_COLOR[slot.model]}>{isNow ? '▸' : ' '}</Text>
|
|
395
|
+
</Box>
|
|
396
|
+
<Box width={9} flexShrink={0}>
|
|
397
|
+
<Text bold={isNow}>{cap(role)}</Text>
|
|
398
|
+
</Box>
|
|
399
|
+
<Box width={8} flexShrink={0}>
|
|
400
|
+
<Text color={isOff ? undefined : MODEL_COLOR[slot.model]} dimColor={isOff}>{MODEL_NAME[slot.model] ?? slot.model}</Text>
|
|
401
|
+
</Box>
|
|
402
|
+
<Box width={8} flexShrink={0}>
|
|
403
|
+
<Text>
|
|
404
|
+
<Text color={isOff ? undefined : MODEL_COLOR[slot.model]} dimColor={isOff}>{mm.on}</Text>
|
|
405
|
+
<Text dimColor>{mm.off}</Text>
|
|
406
|
+
</Text>
|
|
407
|
+
</Box>
|
|
408
|
+
<Text dimColor wrap="truncate-end">{effortLabel(slot.effort)}</Text>
|
|
409
|
+
</Box>
|
|
410
|
+
</Box>
|
|
411
|
+
)
|
|
412
|
+
})}
|
|
413
|
+
</Box>
|
|
414
|
+
</Box>
|
|
415
|
+
)
|
|
416
|
+
})
|
|
417
|
+
}
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "cc-switchboard",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"description": "
|
|
3
|
+
"version": "0.2.10",
|
|
4
|
+
"description": "Per-turn model routing for Claude Code: each prompt runs on the right model and effort, with a band above the prompt and a /router pane.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"claude-code",
|
|
7
7
|
"claude",
|
|
@@ -30,11 +30,11 @@
|
|
|
30
30
|
"bin",
|
|
31
31
|
"src",
|
|
32
32
|
"hooks",
|
|
33
|
-
"
|
|
34
|
-
"commands",
|
|
33
|
+
"types",
|
|
35
34
|
".claude-plugin",
|
|
36
35
|
"README.md",
|
|
37
|
-
"LICENSE"
|
|
36
|
+
"LICENSE",
|
|
37
|
+
"!hooks/*.test.tsx"
|
|
38
38
|
],
|
|
39
39
|
"engines": {
|
|
40
40
|
"node": ">=20"
|
package/src/cli.js
CHANGED
|
@@ -1,6 +1,5 @@
|
|
|
1
1
|
import { ROLES, ROLE_INFO } from './core/defaults.js';
|
|
2
2
|
import * as store from './core/store.js';
|
|
3
|
-
import { statusSegment } from './core/status.js';
|
|
4
3
|
|
|
5
4
|
const USAGE = `Usage: switchboard [command]
|
|
6
5
|
|
|
@@ -11,8 +10,7 @@ const USAGE = `Usage: switchboard [command]
|
|
|
11
10
|
set <role> <model> [effort] change one role in the active setup
|
|
12
11
|
save <name> save the active setup under a new name and switch to it
|
|
13
12
|
delete <setup> delete a saved setup
|
|
14
|
-
on | off turn routing on/off (
|
|
15
|
-
status-segment [session] print the status line segment
|
|
13
|
+
on | off turn routing on/off everywhere (in a session: /router off)
|
|
16
14
|
|
|
17
15
|
Roles: ${ROLES.join(', ')}
|
|
18
16
|
Models: fable, opus, sonnet, haiku Efforts: low, medium, high, xhigh, max
|
|
@@ -91,9 +89,6 @@ export async function main(argv, io = { stdout: process.stdout, stderr: process.
|
|
|
91
89
|
store.setEnabled(cmd === 'on', session || undefined);
|
|
92
90
|
out(`Routing ${cmd.toUpperCase()}${session ? ' for this session' : ''}.\n`);
|
|
93
91
|
return 0;
|
|
94
|
-
case 'status-segment':
|
|
95
|
-
out(statusSegment(rest[0] || session || null));
|
|
96
|
-
return 0;
|
|
97
92
|
case 'help':
|
|
98
93
|
case '--help':
|
|
99
94
|
case '-h':
|
package/src/core/classify.js
CHANGED
|
@@ -21,15 +21,15 @@ function compile(rules) {
|
|
|
21
21
|
return c;
|
|
22
22
|
}
|
|
23
23
|
|
|
24
|
-
function route(role, setup, forced, modelOverride) {
|
|
24
|
+
function route(role, setup, forced, modelOverride, why = '') {
|
|
25
25
|
const s = setup[role] || DEFAULT_SETUP[role];
|
|
26
|
-
return { role, model: modelOverride || s.model, effort: s.effort, agent: agentName(role), forced };
|
|
26
|
+
return { role, model: modelOverride || s.model, effort: s.effort, agent: agentName(role), forced, why };
|
|
27
27
|
}
|
|
28
28
|
|
|
29
29
|
/**
|
|
30
30
|
* Decide which role a prompt belongs to.
|
|
31
31
|
* Overrides: #self (no routing), #<role>, @<model>. Otherwise keyword scoring; ties go to ROLES order.
|
|
32
|
-
* Returns {role, model, effort, agent, forced} or null for "no routing".
|
|
32
|
+
* Returns {role, model, effort, agent, forced, why} or null for "no routing".
|
|
33
33
|
*/
|
|
34
34
|
export function classify(prompt, rules, setup) {
|
|
35
35
|
if (typeof prompt !== 'string' || !prompt.trim()) return null;
|
|
@@ -38,25 +38,32 @@ export function classify(prompt, rules, setup) {
|
|
|
38
38
|
if (/(^|\s)#self\b/i.test(text) || SELF_RE.test(text)) return null;
|
|
39
39
|
|
|
40
40
|
const tag = /(?:^|\s)#(plan|review|hard|code|write|chores)\b/i.exec(text);
|
|
41
|
-
if (tag) return route(tag[1].toLowerCase(), setup, true);
|
|
41
|
+
if (tag) return route(tag[1].toLowerCase(), setup, true, undefined, tag[0].trim());
|
|
42
42
|
|
|
43
43
|
const at = /(?:^|\s)@(fable|opus|sonnet|haiku)\b/i.exec(text) || PLAIN_MODEL_RE.exec(text);
|
|
44
44
|
if (at) {
|
|
45
45
|
const model = at[1].toLowerCase();
|
|
46
46
|
const byActive = ROLES.find(r => setup[r]?.model === model);
|
|
47
|
-
|
|
47
|
+
const why = at[0].trim();
|
|
48
|
+
if (byActive) return route(byActive, setup, true, undefined, why);
|
|
48
49
|
const byDefault = ROLES.find(r => DEFAULT_SETUP[r].model === model) || 'code';
|
|
49
|
-
return route(byDefault, setup, true, model);
|
|
50
|
+
return route(byDefault, setup, true, model, why);
|
|
50
51
|
}
|
|
51
52
|
|
|
52
53
|
const res = compile(rules);
|
|
53
54
|
let best = null;
|
|
54
55
|
let bestScore = 0;
|
|
56
|
+
let bestWhy = '';
|
|
55
57
|
for (const role of ROLES) {
|
|
56
|
-
|
|
57
|
-
|
|
58
|
+
let score = 0;
|
|
59
|
+
let why = '';
|
|
60
|
+
for (const re of res[role]) {
|
|
61
|
+
const m = re.exec(text);
|
|
62
|
+
if (m) { score += 1; if (!why) why = m[0].trim(); }
|
|
63
|
+
}
|
|
64
|
+
if (score > bestScore) { best = role; bestScore = score; bestWhy = why; }
|
|
58
65
|
}
|
|
59
|
-
return best ? route(best, setup, false) : null;
|
|
66
|
+
return best ? route(best, setup, false, undefined, bestWhy) : null;
|
|
60
67
|
}
|
|
61
68
|
|
|
62
69
|
/** One-line note injected into the main chat. */
|
package/types/index.d.ts
ADDED
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
export type RoleSetting = { model: string; effort: string }
|
|
2
|
+
export type Setup = Record<string, RoleSetting>
|
|
3
|
+
|
|
4
|
+
/** setups.json and rules.json as the mod reads them, defaults filled in. */
|
|
5
|
+
export type Config = {
|
|
6
|
+
active: string
|
|
7
|
+
enabled: boolean
|
|
8
|
+
mainModel: string
|
|
9
|
+
setups: Record<string, Setup>
|
|
10
|
+
rules: Record<string, string[]>
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
/** Where this turn is going and why. */
|
|
14
|
+
export type Route = {
|
|
15
|
+
role: string
|
|
16
|
+
model: string
|
|
17
|
+
effort: string
|
|
18
|
+
why: string
|
|
19
|
+
source: 'auto' | 'pin' | 'override'
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
/** The model and effort the last main-chat request actually went out with. */
|
|
23
|
+
export type Live = { model: string; effort: string }
|
|
24
|
+
|
|
25
|
+
/** Where the session is: project folder name, git branch, context window used. */
|
|
26
|
+
export type Place = { project: string; branch: string | null; ctx: number | null }
|
|
27
|
+
|
|
28
|
+
declare module 'claude-code' {
|
|
29
|
+
interface PluginState {
|
|
30
|
+
switchboard: {
|
|
31
|
+
config: Shaped<Config | null>
|
|
32
|
+
route: Shaped<Route | null>
|
|
33
|
+
live: Shaped<Live | null>
|
|
34
|
+
paused: boolean
|
|
35
|
+
manual: string | null
|
|
36
|
+
pin: string | null
|
|
37
|
+
tab: string
|
|
38
|
+
place: Place | null
|
|
39
|
+
base: string | null
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
}
|
package/agents/router-chores.md
DELETED
|
@@ -1,6 +0,0 @@
|
|
|
1
|
-
---
|
|
2
|
-
name: router-chores
|
|
3
|
-
description: Switchboard assistant for mechanical work. Use for git operations, renames, formatting, lint fixes, moving files, version bumps, quick lookups and commit messages.
|
|
4
|
-
model: haiku
|
|
5
|
-
---
|
|
6
|
-
You do mechanical tasks quickly and exactly. Do only what was asked, verify the result (run the formatter, linter or git status), and report in one or two lines. Never push, delete branches or rewrite history unless the request says so explicitly.
|
package/agents/router-code.md
DELETED
|
@@ -1,6 +0,0 @@
|
|
|
1
|
-
---
|
|
2
|
-
name: router-code
|
|
3
|
-
description: Switchboard engineer. Use for regular coding: implementing features, endpoints, tests, refactors and ordinary bug fixes.
|
|
4
|
-
model: sonnet
|
|
5
|
-
---
|
|
6
|
-
You write everyday production code. Follow the project's existing patterns, keep the change focused, write or update tests, and run them. Report what changed and what you verified.
|
package/agents/router-hard.md
DELETED
|
@@ -1,6 +0,0 @@
|
|
|
1
|
-
---
|
|
2
|
-
name: router-hard
|
|
3
|
-
description: Switchboard senior engineer. Use for hard code, UI work (SwiftUI, Compose, web UI, layout, animation) and debugging crashes, flaky tests, races and regressions.
|
|
4
|
-
model: opus
|
|
5
|
-
---
|
|
6
|
-
You handle the hard engineering. For bugs: reproduce, find the root cause, then fix it; never patch symptoms. For UI: match the platform's conventions and the existing design system. Run the build and tests that prove the change, and report what you verified and what you could not.
|
package/agents/router-plan.md
DELETED
|
@@ -1,7 +0,0 @@
|
|
|
1
|
-
---
|
|
2
|
-
name: router-plan
|
|
3
|
-
description: Switchboard planner. Use for planning, architecture, design docs, trade-off analysis and big decisions when Switchboard routes "plan" work to a helper.
|
|
4
|
-
model: fable
|
|
5
|
-
tools: Read, Grep, Glob, Bash, WebSearch, WebFetch
|
|
6
|
-
---
|
|
7
|
-
You plan. Read what exists before proposing anything. Give one recommended approach, the alternatives you rejected and why, the risks, and concrete next steps in order. Do not edit files; return the plan as text.
|
package/agents/router-review.md
DELETED
|
@@ -1,7 +0,0 @@
|
|
|
1
|
-
---
|
|
2
|
-
name: router-review
|
|
3
|
-
description: Switchboard reviewer. Use for code review: diffs, pull requests, branches or files that need a careful second read.
|
|
4
|
-
model: fable
|
|
5
|
-
tools: Read, Grep, Glob, Bash, WebSearch, WebFetch
|
|
6
|
-
---
|
|
7
|
-
You review code. Hunt for real bugs first (logic, edge cases, concurrency, error handling, security), then risky design choices. For each finding give file:line, what goes wrong and when, and a fix. Skip style nits unless asked. Do not edit files.
|
package/agents/router-write.md
DELETED
|
@@ -1,6 +0,0 @@
|
|
|
1
|
-
---
|
|
2
|
-
name: router-write
|
|
3
|
-
description: Switchboard writer. Use for prose other people will read: PR descriptions, Slack messages, emails, Jira tickets, Confluence pages, READMEs and docs.
|
|
4
|
-
model: sonnet
|
|
5
|
-
---
|
|
6
|
-
You write text people will read. If the matt3r-writing skill is available, follow it. Be short, plain and specific; lead with what matters; no filler or hype. Return the draft; never send or post anything yourself.
|
package/commands/router.md
DELETED
|
@@ -1,8 +0,0 @@
|
|
|
1
|
-
---
|
|
2
|
-
description: Show or change Switchboard model routing (setups, on/off)
|
|
3
|
-
argument-hint: "[show|list|use <setup>|set <role> <model> [effort]|save <name>|on|off]"
|
|
4
|
-
allowed-tools: Bash(node:*)
|
|
5
|
-
---
|
|
6
|
-
!`node "${CLAUDE_PLUGIN_ROOT}/bin/switchboard.js" $ARGUMENTS --session "${CLAUDE_SESSION_ID}"`
|
|
7
|
-
|
|
8
|
-
Show the output above to the user as is, in a code block. Do not add commentary unless it shows an error.
|
package/hooks/on-agent-post.js
DELETED
package/hooks/on-agent-pre.js
DELETED
package/hooks/on-prompt.js
DELETED
package/src/core/hooks.js
DELETED
|
@@ -1,92 +0,0 @@
|
|
|
1
|
-
import fs from 'node:fs';
|
|
2
|
-
import path from 'node:path';
|
|
3
|
-
import { roleFromAgent } from './defaults.js';
|
|
4
|
-
import { classify, note } from './classify.js';
|
|
5
|
-
import * as store from './store.js';
|
|
6
|
-
import { detectMainModel } from './transcript.js';
|
|
7
|
-
|
|
8
|
-
export const SESSION_RULE =
|
|
9
|
-
'Switchboard (installed by the user) routes work by model. When a prompt carries a "Switchboard:" routing note, ' +
|
|
10
|
-
'that note is the user\'s standing instruction: hand the work to the named switchboard:router-* agent with the Agent tool, ' +
|
|
11
|
-
'even for small or quick tasks, and relay its result. Do it yourself only when the note says so, when the user writes #self, ' +
|
|
12
|
-
'or when the user explicitly asks you to. Never pass model or effort to these agents; Switchboard sets them.';
|
|
13
|
-
|
|
14
|
-
/** SessionStart: one standing rule so routing notes are treated as the user's instruction. */
|
|
15
|
-
export function handleSessionStart(payload) {
|
|
16
|
-
try { store.clearRunning(payload?.session_id); } catch { /* best effort */ }
|
|
17
|
-
if (!store.sessionEnabled(payload?.session_id)) return null;
|
|
18
|
-
return { hookSpecificOutput: { hookEventName: 'SessionStart', additionalContext: SESSION_RULE } };
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
/** UserPromptSubmit: classify and inject a one-line routing note. */
|
|
22
|
-
export function handlePrompt(payload) {
|
|
23
|
-
const sid = payload?.session_id;
|
|
24
|
-
if (!store.sessionEnabled(sid)) return null;
|
|
25
|
-
// The user switched the main chat's model by hand (/model): step aside until they switch back.
|
|
26
|
-
const detected = detectMainModel(payload?.transcript_path);
|
|
27
|
-
if (detected && detected !== store.mainModel()) {
|
|
28
|
-
store.updateState(sid, s => { s.manual = detected; s.last_route = null; s.forced = null; });
|
|
29
|
-
return null;
|
|
30
|
-
}
|
|
31
|
-
const setup = store.activeSetup();
|
|
32
|
-
const r = classify(payload?.prompt, store.loadRules(), setup);
|
|
33
|
-
store.updateState(sid, s => {
|
|
34
|
-
s.manual = null;
|
|
35
|
-
s.last_route = r ? r.role : null;
|
|
36
|
-
// A forced model outside the setup is honoured for the next helper of that role only.
|
|
37
|
-
s.forced = r && r.model !== setup[r.role].model ? { role: r.role, model: r.model } : null;
|
|
38
|
-
});
|
|
39
|
-
if (!r) return null;
|
|
40
|
-
return { hookSpecificOutput: { hookEventName: 'UserPromptSubmit', additionalContext: note(r, store.mainModel()) } };
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
/** PreToolUse(Agent|Task): force router helpers onto the active setup's model and effort. */
|
|
44
|
-
export function handleAgentPre(payload) {
|
|
45
|
-
const input = payload?.tool_input;
|
|
46
|
-
const role = roleFromAgent(input?.subagent_type);
|
|
47
|
-
if (!role) return null;
|
|
48
|
-
const sid = payload.session_id;
|
|
49
|
-
if (store.readState(sid).manual) return null;
|
|
50
|
-
let { model, effort } = store.activeSetup()[role];
|
|
51
|
-
const forced = store.readState(sid).forced;
|
|
52
|
-
if (forced?.role === role) {
|
|
53
|
-
model = forced.model;
|
|
54
|
-
store.updateState(sid, s => { s.forced = null; });
|
|
55
|
-
}
|
|
56
|
-
store.addRunning(sid, payload.tool_use_id || `t${Date.now()}`, `${role}→${model}`);
|
|
57
|
-
return {
|
|
58
|
-
hookSpecificOutput: {
|
|
59
|
-
hookEventName: 'PreToolUse',
|
|
60
|
-
permissionDecision: 'allow',
|
|
61
|
-
permissionDecisionReason: `Switchboard: ${role} → ${model} (${effort})`,
|
|
62
|
-
updatedInput: { ...input, model, effort },
|
|
63
|
-
},
|
|
64
|
-
};
|
|
65
|
-
}
|
|
66
|
-
|
|
67
|
-
/** PostToolUse(Agent|Task): the helper is done. */
|
|
68
|
-
export function handleAgentPost(payload) {
|
|
69
|
-
if (!roleFromAgent(payload?.tool_input?.subagent_type) || !payload?.tool_use_id) return null;
|
|
70
|
-
store.removeRunning(payload.session_id, payload.tool_use_id);
|
|
71
|
-
return null;
|
|
72
|
-
}
|
|
73
|
-
|
|
74
|
-
/** Read hook JSON from stdin, run the handler, print its output. Never throws, always exits 0. */
|
|
75
|
-
export function runHook(handler) {
|
|
76
|
-
let raw = '';
|
|
77
|
-
process.stdin.setEncoding('utf8');
|
|
78
|
-
process.stdin.on('data', d => { raw += d; });
|
|
79
|
-
process.stdin.on('end', () => {
|
|
80
|
-
try {
|
|
81
|
-
const out = handler(JSON.parse(raw));
|
|
82
|
-
if (out) process.stdout.write(JSON.stringify(out));
|
|
83
|
-
} catch (e) {
|
|
84
|
-
try {
|
|
85
|
-
fs.mkdirSync(store.home(), { recursive: true });
|
|
86
|
-
fs.appendFileSync(path.join(store.home(), 'error.log'),
|
|
87
|
-
`${new Date().toISOString()} ${handler.name}: ${e?.stack || e}\n`);
|
|
88
|
-
} catch { /* nothing else we can do */ }
|
|
89
|
-
}
|
|
90
|
-
process.exitCode = 0;
|
|
91
|
-
});
|
|
92
|
-
}
|
package/src/core/status.js
DELETED
|
@@ -1,11 +0,0 @@
|
|
|
1
|
-
import * as store from './store.js';
|
|
2
|
-
|
|
3
|
-
/** Status line segment: "⇢ <setup>" plus running helpers, or "⇢ off". */
|
|
4
|
-
export function statusSegment(sessionId) {
|
|
5
|
-
if (!store.sessionEnabled(sessionId)) return '⇢ off';
|
|
6
|
-
const manual = store.readState(sessionId).manual;
|
|
7
|
-
if (manual) return `⇢ manual (${manual})`;
|
|
8
|
-
const base = `⇢ ${store.activeName()}`;
|
|
9
|
-
const running = Object.values(store.readState(sessionId).running || {});
|
|
10
|
-
return running.length ? `${base} · ${running.join(', ')}` : base;
|
|
11
|
-
}
|
package/src/core/transcript.js
DELETED
|
@@ -1,34 +0,0 @@
|
|
|
1
|
-
import fs from 'node:fs';
|
|
2
|
-
|
|
3
|
-
const TAIL_BYTES = 256 * 1024;
|
|
4
|
-
const COMMAND_RE = /(?:Set|Kept) model (?:to|as) `(Fable|Opus|Sonnet|Haiku)\b/gi;
|
|
5
|
-
const ASSISTANT_RE = /"model":"claude-(fable|opus|sonnet|haiku)-/g;
|
|
6
|
-
|
|
7
|
-
function lastMatch(re, text) {
|
|
8
|
-
let last = null;
|
|
9
|
-
re.lastIndex = 0;
|
|
10
|
-
for (let m = re.exec(text); m; m = re.exec(text)) last = m;
|
|
11
|
-
return last;
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
/** Which model family is the main chat on? Reads only the tail of the transcript; null if unknown. */
|
|
15
|
-
export function detectMainModel(transcriptPath) {
|
|
16
|
-
if (typeof transcriptPath !== 'string' || !transcriptPath) return null;
|
|
17
|
-
let fd;
|
|
18
|
-
try {
|
|
19
|
-
fd = fs.openSync(transcriptPath, 'r');
|
|
20
|
-
const size = fs.fstatSync(fd).size;
|
|
21
|
-
const len = Math.min(size, TAIL_BYTES);
|
|
22
|
-
const buf = Buffer.alloc(len);
|
|
23
|
-
fs.readSync(fd, buf, 0, len, size - len);
|
|
24
|
-
const text = buf.toString('utf8');
|
|
25
|
-
const a = lastMatch(COMMAND_RE, text);
|
|
26
|
-
const b = lastMatch(ASSISTANT_RE, text);
|
|
27
|
-
const pick = a && b ? (a.index > b.index ? a : b) : a || b;
|
|
28
|
-
return pick ? pick[1].toLowerCase() : null;
|
|
29
|
-
} catch {
|
|
30
|
-
return null;
|
|
31
|
-
} finally {
|
|
32
|
-
if (fd !== undefined) try { fs.closeSync(fd); } catch { /* ignore */ }
|
|
33
|
-
}
|
|
34
|
-
}
|