@emesoft/tokenhub 0.8.7 → 0.8.9
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/README.md +4 -3
- package/claude-plugin/plugins/tokenhub-models/.claude-plugin/plugin.json +1 -1
- package/claude-plugin/plugins/tokenhub-models/hooks/catalog.ts +10 -9
- package/claude-plugin/plugins/tokenhub-models/hooks/picker.tsx +88 -30
- package/claude-plugin/plugins/tokenhub-models/hooks/register.tsx +116 -16
- package/claude-plugin/plugins/tokenhub-models/types/index.d.ts +10 -2
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -70,9 +70,10 @@ Prefer not to install? Use `npx @emesoft/tokenhub connect <your tenant name>` in
|
|
|
70
70
|
to ask on. On yes it copies a Claude Code plugin to `~/.tokenhub/claude-plugin` and registers it as
|
|
71
71
|
`extraKnownMarketplaces.tokenhub` and `enabledPlugins["tokenhub-models@tokenhub"]`. After a restart, a bare `/model`
|
|
72
72
|
opens a picker of TokenHub models above the prompt: providers side by side, ↑/↓ and Enter, a click or `1`–`9` to pick,
|
|
73
|
-
`,` / `.` or a click for effort (hover highlights). `/model <id>` and other hosts keep Claude Code's own picker. The list is
|
|
74
|
-
|
|
75
|
-
|
|
73
|
+
`,` / `.` or a click for effort (hover highlights), and `[` or a click for **1M**: each model shows its context window, and with 1M on, a model whose window is 1M is picked as `<id>[1m]` so Claude Code compacts at 1M instead of 200K (only for models whose window TokenHub knows to be 1M). `/model <id>` and other hosts keep Claude Code's own picker. The list is read from your tenant
|
|
74
|
+
(`/member/api/models`: the providers your key may use, with a skeleton while it loads); when the pool does not
|
|
75
|
+
answer within a few seconds it falls back to a built-in list, marked "offline list". The plugin relies on Claude
|
|
76
|
+
Code's early-access plugin hooks, which can change between Claude Code releases.
|
|
76
77
|
- Keeps every other setting, and saves the previous file as `settings.json.bak`.
|
|
77
78
|
|
|
78
79
|
Each machine gets its own key, which you can revoke from the pool's **Keys** page. `disconnect` removes exactly these entries (and the model picker's copy) and nothing else.
|
|
@@ -1,18 +1,19 @@
|
|
|
1
|
-
//
|
|
2
|
-
//
|
|
3
|
-
|
|
1
|
+
// The built-in list: a static copy of the gateway's declared catalogs (src/model-catalog.ts) with the provider
|
|
2
|
+
// prefixes of ADR-0045, shown only when the gateway's own list (`GET /member/api/models`, #737) cannot be read.
|
|
3
|
+
// Antigravity has no prefix, so it is left out.
|
|
4
|
+
export type Model = { id: string; name: string; contextWindow?: number | null }
|
|
4
5
|
export type Group = { provider: string; models: Model[] }
|
|
5
6
|
|
|
6
7
|
export const GROUPS: Group[] = [
|
|
7
8
|
{
|
|
8
9
|
provider: 'Claude Code',
|
|
9
10
|
models: [
|
|
10
|
-
{ id: 'cc/claude-fable-5-1', name: 'Fable 5.1' },
|
|
11
|
-
{ id: 'cc/claude-opus-5-5', name: 'Opus 5.5' },
|
|
12
|
-
{ id: 'cc/claude-opus-5', name: 'Opus 5' },
|
|
13
|
-
{ id: 'cc/claude-sonnet-5-5', name: 'Sonnet 5.5' },
|
|
14
|
-
{ id: 'cc/claude-sonnet-5', name: 'Sonnet 5' },
|
|
15
|
-
{ id: 'cc/claude-haiku-4-5', name: 'Haiku 4.5' },
|
|
11
|
+
{ id: 'cc/claude-fable-5-1', contextWindow: 1_000_000, name: 'Fable 5.1' },
|
|
12
|
+
{ id: 'cc/claude-opus-5-5', contextWindow: 1_000_000, name: 'Opus 5.5' },
|
|
13
|
+
{ id: 'cc/claude-opus-5', contextWindow: 1_000_000, name: 'Opus 5' },
|
|
14
|
+
{ id: 'cc/claude-sonnet-5-5', contextWindow: 1_000_000, name: 'Sonnet 5.5' },
|
|
15
|
+
{ id: 'cc/claude-sonnet-5', contextWindow: 1_000_000, name: 'Sonnet 5' },
|
|
16
|
+
{ id: 'cc/claude-haiku-4-5', contextWindow: 200_000, name: 'Haiku 4.5' },
|
|
16
17
|
],
|
|
17
18
|
},
|
|
18
19
|
{
|
|
@@ -1,20 +1,30 @@
|
|
|
1
1
|
import type { ClientModule } from 'claude-code'
|
|
2
2
|
|
|
3
|
-
// The picker's drawing: provider columns side by side, the cursor and
|
|
3
|
+
// The picker's drawing: provider columns side by side, the cursor, effort and 1M switch handed in as
|
|
4
4
|
// props (the hooks module owns them). While it has the focus, up/down post a move, left/right
|
|
5
|
-
// or ,/. an effort step, Enter a pick; the hooks module answers with the next props.
|
|
5
|
+
// or ,/. an effort step, [ the 1M switch, Enter a pick; the hooks module answers with the next props.
|
|
6
6
|
type Props = {
|
|
7
|
-
groups: { provider: string; models: { id: string; name: string }[] }[]
|
|
7
|
+
groups: { provider: string; models: { id: string; name: string; contextWindow?: number | null }[] }[]
|
|
8
|
+
/** True while the gateway's list is being read for the first time: the columns draw as a skeleton. */
|
|
9
|
+
loading?: boolean
|
|
8
10
|
current: string
|
|
9
11
|
cursor: number
|
|
10
12
|
effort: string
|
|
11
13
|
efforts: string[]
|
|
14
|
+
/** Whether a pick of a 1M model asks Claude Code for its 1M window (`[1m]`, #746). */
|
|
15
|
+
use1m?: boolean
|
|
12
16
|
}
|
|
13
17
|
|
|
14
|
-
|
|
18
|
+
/** A model with a window of at least this many tokens can be picked with `[1m]`. */
|
|
19
|
+
const ONE_MILLION = 1_000_000
|
|
20
|
+
const bare = (id: string) => id.replace(/\[1m\]$/i, '')
|
|
21
|
+
const matches = (id: string, current: string) => id === bare(current) || id.endsWith('/' + bare(current))
|
|
22
|
+
/** @returns A context window as the picker labels it: `1M`, `200K`, `131K`; empty when unknown. */
|
|
23
|
+
const windowLabel = (tokens: number | null | undefined): string =>
|
|
24
|
+
!tokens ? '' : tokens >= ONE_MILLION ? `${Math.round((tokens / ONE_MILLION) * 10) / 10}M` : `${Math.round(tokens / 1000)}K`
|
|
15
25
|
|
|
16
|
-
// Local to the drawing: the effort level under the mouse, which only highlights.
|
|
17
|
-
type State = { hoverEffort?: string }
|
|
26
|
+
// Local to the drawing: the effort level or context choice under the mouse, which only highlights.
|
|
27
|
+
type State = { hoverEffort?: string; hoverContext?: string }
|
|
18
28
|
|
|
19
29
|
const Picker: ClientModule<Props, State> = (props, s) => {
|
|
20
30
|
const { Box, Text } = s.elements
|
|
@@ -24,13 +34,23 @@ const Picker: ClientModule<Props, State> = (props, s) => {
|
|
|
24
34
|
else if (k.key === 'up') s.post({ move: -1 })
|
|
25
35
|
else if (k.key === 'right' || k.key === '.') s.post({ effort: 1 })
|
|
26
36
|
else if (k.key === 'left' || k.key === ',') s.post({ effort: -1 })
|
|
37
|
+
else if (k.key === '[') s.post({ toggle1m: true })
|
|
27
38
|
else if (k.key === 'return') s.post({ pick: true })
|
|
28
39
|
})
|
|
29
|
-
const colWidth = Math.max(20, Math.floor((s.columns || 90) / Math.max(1, props.groups.length)))
|
|
30
|
-
// Rows as drawn: 0 the provider names, 1..tallest the models, a blank, then the
|
|
31
|
-
const tallest = Math.max(0, ...props.groups.map(g => g.models.length))
|
|
40
|
+
const colWidth = Math.max(20, Math.floor((s.columns || 90) / Math.max(1, props.loading ? 3 : props.groups.length)))
|
|
41
|
+
// Rows as drawn: 0 the provider names, 1..tallest the models, a blank, the effort line, then the context line.
|
|
42
|
+
const tallest = props.loading ? 5 : Math.max(0, ...props.groups.map(g => g.models.length))
|
|
32
43
|
const effortRow = tallest + 2
|
|
44
|
+
const contextRow = effortRow + 1
|
|
33
45
|
const EFFORT_START = '● Effort: '.length
|
|
46
|
+
const CONTEXT_START = '● Context: '.length
|
|
47
|
+
const contextChoices = [
|
|
48
|
+
{ key: 'standard', label: 'Standard' },
|
|
49
|
+
{ key: '1m', label: '1M' },
|
|
50
|
+
]
|
|
51
|
+
const contextOn = props.use1m ? '1m' : 'standard'
|
|
52
|
+
const cursorModel = flat[props.cursor]
|
|
53
|
+
const cursorTakes1m = (cursorModel?.contextWindow ?? 0) >= ONE_MILLION
|
|
34
54
|
/** The flat model index under a cell, or -1. */
|
|
35
55
|
const modelAt = (x: number, y: number) => {
|
|
36
56
|
const col = Math.floor(x / colWidth)
|
|
@@ -38,41 +58,76 @@ const Picker: ClientModule<Props, State> = (props, s) => {
|
|
|
38
58
|
if (!group || y < 1 || y > group.models.length) return -1
|
|
39
59
|
return props.groups.slice(0, col).reduce((n, g) => n + g.models.length, 0) + (y - 1)
|
|
40
60
|
}
|
|
41
|
-
/** The
|
|
42
|
-
const
|
|
43
|
-
let
|
|
44
|
-
for (const
|
|
45
|
-
const width = (
|
|
46
|
-
if (x >=
|
|
47
|
-
|
|
61
|
+
/** The choice under a column of a line drawn as `<start>a b c`, the chosen one in brackets. */
|
|
62
|
+
const choiceAt = (x: number, start: number, keys: { key: string; label: string }[], chosen: string) => {
|
|
63
|
+
let at = start
|
|
64
|
+
for (const choice of keys) {
|
|
65
|
+
const width = (choice.key === chosen ? `[${choice.label}]` : choice.label).length
|
|
66
|
+
if (x >= at && x < at + width) return choice.key
|
|
67
|
+
at += width + 2
|
|
48
68
|
}
|
|
49
69
|
return undefined
|
|
50
70
|
}
|
|
71
|
+
const effortChoices = props.efforts.map(e => ({ key: e, label: e }))
|
|
51
72
|
const hoverEffort = s.state?.hoverEffort
|
|
52
|
-
const
|
|
53
|
-
|
|
73
|
+
const hoverContext = s.state?.hoverContext
|
|
74
|
+
const setHover = (effort: string | undefined, context: string | undefined) => {
|
|
75
|
+
if (effort !== hoverEffort || context !== hoverContext) s.setState({ hoverEffort: effort, hoverContext: context })
|
|
54
76
|
}
|
|
55
77
|
// The mouse as Claude Code's own picker: hovering a model moves the highlight, a click picks it;
|
|
56
|
-
// hovering an effort level highlights it, a click sets it.
|
|
78
|
+
// hovering an effort level or a context choice highlights it, a click sets it.
|
|
57
79
|
s.onPointer(p => {
|
|
58
80
|
if (p.type === 'leave') {
|
|
59
|
-
setHover(undefined)
|
|
81
|
+
setHover(undefined, undefined)
|
|
60
82
|
} else if (p.type === 'move' && !p.button) {
|
|
61
83
|
const at = modelAt(p.x, p.y)
|
|
62
84
|
if (at >= 0 && at !== props.cursor) s.post({ cursorTo: at })
|
|
63
|
-
setHover(
|
|
85
|
+
setHover(
|
|
86
|
+
p.y === effortRow ? choiceAt(p.x, EFFORT_START, effortChoices, props.effort) : undefined,
|
|
87
|
+
p.y === contextRow ? choiceAt(p.x, CONTEXT_START, contextChoices, contextOn) : undefined,
|
|
88
|
+
)
|
|
64
89
|
} else if (p.type === 'up' && p.button === 'left') {
|
|
65
90
|
const at = modelAt(p.x, p.y)
|
|
66
91
|
if (at >= 0) s.post({ pickAt: at })
|
|
67
92
|
else if (p.y === effortRow) {
|
|
68
|
-
const level =
|
|
93
|
+
const level = choiceAt(p.x, EFFORT_START, effortChoices, props.effort)
|
|
69
94
|
if (level && level !== props.effort) s.post({ effortTo: level })
|
|
95
|
+
} else if (p.y === contextRow) {
|
|
96
|
+
const choice = choiceAt(p.x, CONTEXT_START, contextChoices, contextOn)
|
|
97
|
+
if (choice && choice !== contextOn) s.post({ toggle1m: true })
|
|
70
98
|
}
|
|
71
99
|
}
|
|
72
100
|
})
|
|
101
|
+
/** One line of choices, the chosen one in brackets, the hovered one in the accent colour. */
|
|
102
|
+
const choiceLine = (choices: { key: string; label: string }[], chosen: string, hovered: string | undefined) =>
|
|
103
|
+
choices.flatMap((choice, j) => [
|
|
104
|
+
...(j > 0 ? [<Text>{' '}</Text>] : []),
|
|
105
|
+
<Text color={choice.key === hovered ? 'suggestion' : undefined} dimColor={choice.key !== chosen && choice.key !== hovered}>
|
|
106
|
+
{choice.key === chosen ? `[${choice.label}]` : choice.label}
|
|
107
|
+
</Text>,
|
|
108
|
+
])
|
|
73
109
|
let i = 0
|
|
110
|
+
// Skeleton: three columns of placeholder bars where the providers and their models will be.
|
|
111
|
+
const skeleton = (
|
|
112
|
+
<Box flexDirection="row">
|
|
113
|
+
{[0, 1, 2].map(col => (
|
|
114
|
+
<Box flexDirection="column" width={colWidth}>
|
|
115
|
+
<Text dimColor>{'▒'.repeat(8 + col * 2)}</Text>
|
|
116
|
+
{[14, 11, 16, 9, 13].map((width, row) => (
|
|
117
|
+
<Text dimColor wrap="truncate">{' '}{'░'.repeat(Math.min(colWidth - 6, width + ((row + col) % 3)))}</Text>
|
|
118
|
+
))}
|
|
119
|
+
</Box>
|
|
120
|
+
))}
|
|
121
|
+
</Box>
|
|
122
|
+
)
|
|
123
|
+
const contextNote = props.loading
|
|
124
|
+
? ''
|
|
125
|
+
: cursorTakes1m
|
|
126
|
+
? ' [ or click to switch'
|
|
127
|
+
: ` ${windowLabel(cursorModel?.contextWindow) || 'window unknown'} · no 1M for this model`
|
|
74
128
|
return (
|
|
75
129
|
<Box flexDirection="column">
|
|
130
|
+
{props.loading ? skeleton : (
|
|
76
131
|
<Box flexDirection="row">
|
|
77
132
|
{props.groups.map(g => (
|
|
78
133
|
<Box flexDirection="column" width={colWidth}>
|
|
@@ -82,31 +137,34 @@ const Picker: ClientModule<Props, State> = (props, s) => {
|
|
|
82
137
|
const isCursor = at === props.cursor
|
|
83
138
|
const isCurrent = matches(m.id, props.current)
|
|
84
139
|
const digit = at < 9 ? `${at + 1}.` : ' '
|
|
140
|
+
const label = windowLabel(m.contextWindow)
|
|
85
141
|
return (
|
|
86
142
|
<Text wrap="truncate" color={isCursor ? 'suggestion' : undefined} dimColor={!isCursor && !isCurrent}>
|
|
87
143
|
{isCursor ? '› ' : ' '}{digit} {m.name}{isCurrent ? ' ✔' : ''}
|
|
144
|
+
{label ? <Text dimColor> {label}</Text> : ''}
|
|
88
145
|
</Text>
|
|
89
146
|
)
|
|
90
147
|
})}
|
|
91
148
|
</Box>
|
|
92
149
|
))}
|
|
93
150
|
</Box>
|
|
151
|
+
)}
|
|
94
152
|
<Box marginTop={1}>
|
|
95
153
|
<Text>
|
|
96
154
|
<Text color="warning">● </Text>
|
|
97
155
|
{'Effort: '}
|
|
98
|
-
{props.
|
|
99
|
-
...(j > 0 ? [<Text>{' '}</Text>] : []),
|
|
100
|
-
<Text color={e === hoverEffort ? 'suggestion' : undefined}
|
|
101
|
-
dimColor={e !== props.effort && e !== hoverEffort}>
|
|
102
|
-
{e === props.effort ? `[${e}]` : e}
|
|
103
|
-
</Text>,
|
|
104
|
-
])}
|
|
156
|
+
{choiceLine(effortChoices, props.effort, hoverEffort)}
|
|
105
157
|
<Text dimColor> , / . or click to adjust</Text>
|
|
106
158
|
</Text>
|
|
107
159
|
</Box>
|
|
160
|
+
<Text>
|
|
161
|
+
<Text color={props.use1m ? 'suggestion' : 'warning'}>● </Text>
|
|
162
|
+
{'Context: '}
|
|
163
|
+
{choiceLine(contextChoices, contextOn, hoverContext)}
|
|
164
|
+
<Text dimColor>{contextNote}</Text>
|
|
165
|
+
</Text>
|
|
108
166
|
<Text dimColor>
|
|
109
|
-
{
|
|
167
|
+
{props.loading ? 'Loading your TokenHub models…' : `${cursorModel?.id ?? ''}${props.use1m && cursorTakes1m ? '[1m]' : ''} · ↑/↓ or hover · Enter, click or 1-9 picks · type anything to close`}
|
|
110
168
|
</Text>
|
|
111
169
|
</Box>
|
|
112
170
|
)
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { atom, read, update } from 'claude-code'
|
|
2
2
|
import type { EngineInterface, Register } from 'claude-code'
|
|
3
|
+
import type { Group } from './catalog'
|
|
3
4
|
import { EFFORTS, GROUPS } from './catalog'
|
|
4
5
|
|
|
5
6
|
const SELF = 'tokenhub-models'
|
|
@@ -7,9 +8,21 @@ const isOpen = atom({ plugin: 'tokenhub-models', key: 'isOpen' } as const, false
|
|
|
7
8
|
// The picker's cursor (index over every model) and effort; null until moved.
|
|
8
9
|
const cursorAt = atom({ plugin: 'tokenhub-models', key: 'cursor' } as const, null)
|
|
9
10
|
const effortPick = atom({ plugin: 'tokenhub-models', key: 'effort' } as const, null)
|
|
11
|
+
// The list the gateway answered (`GET /member/api/models`, #737), kept for the session, and where the picker's list
|
|
12
|
+
// comes from now: `loading` before the first answer, `live` once one came, `offline` when it could not be read.
|
|
13
|
+
const liveGroups = atom({ plugin: 'tokenhub-models', key: 'groups' } as const, null)
|
|
14
|
+
const listState = atom({ plugin: 'tokenhub-models', key: 'listState' } as const, 'loading')
|
|
15
|
+
// Whether a pick of a model with a 1M window asks Claude Code for that window (`<id>[1m]`, #746).
|
|
16
|
+
const use1m = atom({ plugin: 'tokenhub-models', key: 'use1m' } as const, false)
|
|
10
17
|
|
|
11
|
-
const
|
|
12
|
-
|
|
18
|
+
const MODELS_PATH = '/member/api/models'
|
|
19
|
+
/** How long the picker waits for the gateway before it falls back to the built-in list. */
|
|
20
|
+
const FETCH_TIMEOUT_MS = 3000
|
|
21
|
+
|
|
22
|
+
/** A model with a window of at least this many tokens can be picked with `[1m]`. */
|
|
23
|
+
const ONE_MILLION = 1_000_000
|
|
24
|
+
const bare = (id: string) => id.replace(/\[1m\]$/i, '')
|
|
25
|
+
const matches = (id: string, current: string) => id === bare(current) || id.endsWith('/' + bare(current))
|
|
13
26
|
|
|
14
27
|
/** TokenHub hosts: `<slug>.aimpact.io.vn`, or a local gateway on 8787-8789. */
|
|
15
28
|
function isTokenHub(base: string | undefined): boolean {
|
|
@@ -23,6 +36,71 @@ function isTokenHub(base: string | undefined): boolean {
|
|
|
23
36
|
}
|
|
24
37
|
}
|
|
25
38
|
|
|
39
|
+
/** @returns The groups of an answer when it has the expected shape and at least one model, else null. */
|
|
40
|
+
function parseGroups(text: string): Group[] | null {
|
|
41
|
+
let body: unknown
|
|
42
|
+
try {
|
|
43
|
+
body = JSON.parse(text)
|
|
44
|
+
} catch {
|
|
45
|
+
return null
|
|
46
|
+
}
|
|
47
|
+
const groups = (body as { groups?: unknown })?.groups
|
|
48
|
+
if (!Array.isArray(groups)) return null
|
|
49
|
+
const out: Group[] = []
|
|
50
|
+
for (const group of groups) {
|
|
51
|
+
const provider = (group as { provider?: unknown })?.provider
|
|
52
|
+
const models = (group as { models?: unknown })?.models
|
|
53
|
+
if (typeof provider !== 'string' || !Array.isArray(models)) continue
|
|
54
|
+
const list = models
|
|
55
|
+
.filter((m): m is { id: string; name: string; contextWindow?: unknown } => typeof m?.id === 'string' && typeof m?.name === 'string')
|
|
56
|
+
.map(m => ({ id: m.id, name: m.name, contextWindow: typeof m.contextWindow === 'number' && m.contextWindow > 0 ? m.contextWindow : null }))
|
|
57
|
+
if (list.length > 0) out.push({ provider, models: list })
|
|
58
|
+
}
|
|
59
|
+
return out.length > 0 ? out : null
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
/**
|
|
63
|
+
* Reads the key's models from the gateway and stores them; a failure, an old gateway, an empty answer or no answer
|
|
64
|
+
* within `FETCH_TIMEOUT_MS` leaves the last good list, or marks the list offline when there is none.
|
|
65
|
+
*/
|
|
66
|
+
async function loadModels($: EngineInterface): Promise<void> {
|
|
67
|
+
let groups: Group[] | null = null
|
|
68
|
+
try {
|
|
69
|
+
const base = await $.env.get('ANTHROPIC_BASE_URL')
|
|
70
|
+
const key = await $.env.get('ANTHROPIC_AUTH_TOKEN')
|
|
71
|
+
if (base && key) {
|
|
72
|
+
const stop = new AbortController()
|
|
73
|
+
// A sleep that cannot run (refused, or cut short by the abort below) never counts as a timeout.
|
|
74
|
+
const timeout = $.clock.sleep(FETCH_TIMEOUT_MS, { signal: stop.signal }).then(() => null, () => new Promise<null>(() => {}))
|
|
75
|
+
const answer = await Promise.race([
|
|
76
|
+
$.http.fetch(`${base.replace(/\/+$/, '')}${MODELS_PATH}`, { headers: { 'x-api-key': key } }).catch(() => null),
|
|
77
|
+
timeout,
|
|
78
|
+
])
|
|
79
|
+
stop.abort()
|
|
80
|
+
if (answer?.ok) groups = parseGroups(answer.text)
|
|
81
|
+
}
|
|
82
|
+
} catch {
|
|
83
|
+
groups = null
|
|
84
|
+
}
|
|
85
|
+
if (groups) {
|
|
86
|
+
const fresh = groups
|
|
87
|
+
await update($, liveGroups, () => fresh)
|
|
88
|
+
await update($, listState, () => 'live')
|
|
89
|
+
} else if ((await read($, liveGroups)) === null) {
|
|
90
|
+
await update($, listState, () => 'offline')
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
/** @returns The list the picker shows now: the gateway's once read, else the built-in one (empty while loading). */
|
|
95
|
+
async function groupsOf($: EngineInterface): Promise<Group[]> {
|
|
96
|
+
const live = await read($, liveGroups)
|
|
97
|
+
if (live) return live
|
|
98
|
+
return (await read($, listState)) === 'loading' ? [] : GROUPS
|
|
99
|
+
}
|
|
100
|
+
async function flatOf($: EngineInterface) {
|
|
101
|
+
return (await groupsOf($)).flatMap(g => g.models)
|
|
102
|
+
}
|
|
103
|
+
|
|
26
104
|
async function savedEffort($: EngineInterface): Promise<string> {
|
|
27
105
|
return String((await $.settings.read({ key: 'effortLevel' })) ?? 'high')
|
|
28
106
|
}
|
|
@@ -36,20 +114,31 @@ async function stepEffort($: EngineInterface, by: number) {
|
|
|
36
114
|
|
|
37
115
|
/** Closes the picker and switches: `/effort` first when it changed, then `/model`. */
|
|
38
116
|
async function pick($: EngineInterface, index: number) {
|
|
39
|
-
const model =
|
|
117
|
+
const model = (await flatOf($))[index]
|
|
40
118
|
if (!model) return
|
|
41
119
|
await update($, isOpen, () => false)
|
|
42
120
|
const saved = await savedEffort($)
|
|
43
121
|
const effort = (await read($, effortPick)) ?? saved
|
|
44
122
|
if (effort !== saved) void $.command.run({ command: 'effort', args: effort })
|
|
45
|
-
|
|
123
|
+
// `[1m]` tells Claude Code the model's window is 1M, so it compacts there instead of at 200K; only for a model whose
|
|
124
|
+
// catalog says it takes that much.
|
|
125
|
+
const wants1m = (await read($, use1m)) && (model.contextWindow ?? 0) >= ONE_MILLION
|
|
126
|
+
void $.command.run({ command: 'model', args: wants1m ? `${model.id}[1m]` : model.id })
|
|
46
127
|
}
|
|
47
128
|
|
|
48
129
|
async function cursorOf($: EngineInterface): Promise<number> {
|
|
130
|
+
const flat = await flatOf($)
|
|
49
131
|
const at = await read($, cursorAt)
|
|
50
|
-
if (at !== null) return at
|
|
132
|
+
if (at !== null && at < flat.length) return at
|
|
51
133
|
const current = await $.session.model()
|
|
52
|
-
return Math.max(0,
|
|
134
|
+
return Math.max(0, flat.findIndex(m => matches(m.id, current)))
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
async function moveCursor($: EngineInterface, by: number) {
|
|
138
|
+
const length = (await flatOf($)).length
|
|
139
|
+
if (length === 0) return
|
|
140
|
+
const at = await cursorOf($)
|
|
141
|
+
await update($, cursorAt, () => (at + by + length) % length)
|
|
53
142
|
}
|
|
54
143
|
|
|
55
144
|
export const register: Register = on => {
|
|
@@ -67,6 +156,12 @@ export const register: Register = on => {
|
|
|
67
156
|
// Start from the current model and the saved effort; a failed reset is not fatal.
|
|
68
157
|
await update($, cursorAt, () => null).catch(err => $.ui.toast(`tokenhub-models cursor: ${String(err)}`))
|
|
69
158
|
await update($, effortPick, () => null).catch(err => $.ui.toast(`tokenhub-models effort: ${String(err)}`))
|
|
159
|
+
// The 1M switch starts where the session is: on when the current model was picked with `[1m]`.
|
|
160
|
+
const current = await $.session.model().catch(() => '')
|
|
161
|
+
await update($, use1m, () => /\[1m\]$/i.test(current)).catch(() => undefined)
|
|
162
|
+
// A list read earlier this session shows at once and is refreshed behind it; the first open shows the skeleton.
|
|
163
|
+
if ((await read($, liveGroups).catch(() => null)) === null) await update($, listState, () => 'loading').catch(() => undefined)
|
|
164
|
+
void loadModels($)
|
|
70
165
|
return { text: 'TokenHub model picker opened above the prompt.' }
|
|
71
166
|
})
|
|
72
167
|
|
|
@@ -87,7 +182,7 @@ export const register: Register = on => {
|
|
|
87
182
|
})
|
|
88
183
|
|
|
89
184
|
// While the picker is open and the prompt is empty, the prompt drives it: up/down move the
|
|
90
|
-
// cursor, `,` / `.` step the effort, 1-9 pick a model. Anything else types as usual.
|
|
185
|
+
// cursor, `,` / `.` step the effort, `[` switches 1M, 1-9 pick a model. Anything else types as usual.
|
|
91
186
|
on('prompt.edit', async ($, e, next) => {
|
|
92
187
|
const arrow = e.key?.key === 'up' || e.key?.key === 'down' ? e.key.key : undefined
|
|
93
188
|
if (arrow && e.text === '') {
|
|
@@ -99,13 +194,11 @@ export const register: Register = on => {
|
|
|
99
194
|
}
|
|
100
195
|
if (!open) return next(e)
|
|
101
196
|
const r = await next(e)
|
|
102
|
-
|
|
103
|
-
const by = arrow === 'down' ? 1 : -1
|
|
104
|
-
await update($, cursorAt, () => (at + by + FLAT.length) % FLAT.length).catch(() => undefined)
|
|
197
|
+
await moveCursor($, arrow === 'down' ? 1 : -1).catch(() => undefined)
|
|
105
198
|
return { ...r, text: '', cursor: 0 }
|
|
106
199
|
}
|
|
107
200
|
const typed = e.inputText
|
|
108
|
-
if (e.text !== '' || !(typed === ',' || typed === '.' || /^[1-9]$/.test(typed))) return next(e)
|
|
201
|
+
if (e.text !== '' || !(typed === ',' || typed === '.' || typed === '[' || /^[1-9]$/.test(typed))) return next(e)
|
|
109
202
|
let open = false
|
|
110
203
|
try {
|
|
111
204
|
open = await read($, isOpen)
|
|
@@ -115,6 +208,7 @@ export const register: Register = on => {
|
|
|
115
208
|
if (!open) return next(e)
|
|
116
209
|
const r = await next(e)
|
|
117
210
|
if (typed === ',' || typed === '.') await stepEffort($, typed === '.' ? 1 : -1).catch(() => undefined)
|
|
211
|
+
else if (typed === '[') await update($, use1m, isOn => !isOn).catch(() => undefined)
|
|
118
212
|
else await pick($, Number(typed) - 1).catch(() => undefined)
|
|
119
213
|
return { ...r, text: '', cursor: 0 }
|
|
120
214
|
})
|
|
@@ -125,19 +219,22 @@ export const register: Register = on => {
|
|
|
125
219
|
const { Box, Text, Button, Client } = $.ui.resolve(e)
|
|
126
220
|
const current = await $.session.model()
|
|
127
221
|
const effort = (await read($, effortPick)) ?? (await savedEffort($))
|
|
222
|
+
const state = await read($, listState)
|
|
223
|
+
const groups = await groupsOf($)
|
|
128
224
|
const cursor = await cursorOf($)
|
|
129
225
|
const close = () => void update($, isOpen, () => false)
|
|
226
|
+
const note = state === 'offline' ? 'offline list · TokenHub did not answer' : ''
|
|
130
227
|
return (
|
|
131
228
|
<Box flexDirection="column" borderStyle="round" paddingX={1}>
|
|
132
229
|
<Box flexDirection="row">
|
|
133
230
|
<Text bold>Select model </Text>
|
|
134
|
-
<Text dimColor>TokenHub · current {current} </Text>
|
|
231
|
+
<Text dimColor>TokenHub · current {current}{note ? ` · ${note}` : ''} </Text>
|
|
135
232
|
<Button key="builtin" plain dimColor onPress={() => { close(); void $.command.run({ command: 'model' }) }}>Built-in picker</Button>
|
|
136
233
|
<Text> </Text>
|
|
137
234
|
<Button key="cancel" plain dimColor onPress={close}>Cancel</Button>
|
|
138
235
|
</Box>
|
|
139
236
|
<Client key="picker" module="./picker.tsx"
|
|
140
|
-
props={{ groups:
|
|
237
|
+
props={{ groups, loading: groups.length === 0, current, cursor, effort, efforts: EFFORTS, use1m: await read($, use1m) }} />
|
|
141
238
|
</Box>
|
|
142
239
|
)
|
|
143
240
|
})
|
|
@@ -151,8 +248,10 @@ export const register: Register = on => {
|
|
|
151
248
|
cursorTo?: number
|
|
152
249
|
pickAt?: number
|
|
153
250
|
effortTo?: string
|
|
251
|
+
toggle1m?: boolean
|
|
154
252
|
}
|
|
155
|
-
const
|
|
253
|
+
const length = (await flatOf($)).length
|
|
254
|
+
const isIndex = (n: unknown): n is number => Number.isInteger(n) && (n as number) >= 0 && (n as number) < length
|
|
156
255
|
if (isIndex(data.pickAt)) {
|
|
157
256
|
await pick($, data.pickAt)
|
|
158
257
|
} else if (isIndex(data.cursorTo)) {
|
|
@@ -161,9 +260,10 @@ export const register: Register = on => {
|
|
|
161
260
|
} else if (typeof data.effortTo === 'string' && EFFORTS.includes(data.effortTo)) {
|
|
162
261
|
const to = data.effortTo
|
|
163
262
|
await update($, effortPick, () => to)
|
|
263
|
+
} else if (data.toggle1m) {
|
|
264
|
+
await update($, use1m, isOn => !isOn)
|
|
164
265
|
} else if (data.move) {
|
|
165
|
-
|
|
166
|
-
await update($, cursorAt, () => (at + data.move! + FLAT.length) % FLAT.length)
|
|
266
|
+
await moveCursor($, data.move)
|
|
167
267
|
} else if (data.effort) {
|
|
168
268
|
await stepEffort($, data.effort)
|
|
169
269
|
} else if (data.pick) {
|
|
@@ -1,6 +1,14 @@
|
|
|
1
|
+
export type PickerModel = { id: string; name: string; contextWindow?: number | null }
|
|
2
|
+
export type PickerGroup = { provider: string; models: PickerModel[] }
|
|
1
3
|
declare module 'claude-code' {
|
|
2
4
|
interface PluginState {
|
|
3
|
-
'tokenhub-models': {
|
|
5
|
+
'tokenhub-models': {
|
|
6
|
+
isOpen: boolean
|
|
7
|
+
cursor: number | null
|
|
8
|
+
effort: string | null
|
|
9
|
+
groups: PickerGroup[] | null
|
|
10
|
+
listState: string
|
|
11
|
+
use1m: boolean
|
|
12
|
+
}
|
|
4
13
|
}
|
|
5
14
|
}
|
|
6
|
-
export type IsOpen = boolean
|