@emesoft/tokenhub 0.8.7 → 0.8.8
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 +3 -2
- package/claude-plugin/plugins/tokenhub-models/hooks/picker.tsx +20 -3
- package/claude-plugin/plugins/tokenhub-models/hooks/register.tsx +97 -12
- package/claude-plugin/plugins/tokenhub-models/types/index.d.ts +9 -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). `/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,5 +1,6 @@
|
|
|
1
|
-
//
|
|
2
|
-
//
|
|
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.
|
|
3
4
|
export type Model = { id: string; name: string }
|
|
4
5
|
export type Group = { provider: string; models: Model[] }
|
|
5
6
|
|
|
@@ -5,6 +5,8 @@ import type { ClientModule } from 'claude-code'
|
|
|
5
5
|
// or ,/. an effort step, Enter a pick; the hooks module answers with the next props.
|
|
6
6
|
type Props = {
|
|
7
7
|
groups: { provider: string; models: { id: string; name: string }[] }[]
|
|
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
|
|
@@ -26,9 +28,9 @@ const Picker: ClientModule<Props, State> = (props, s) => {
|
|
|
26
28
|
else if (k.key === 'left' || k.key === ',') s.post({ effort: -1 })
|
|
27
29
|
else if (k.key === 'return') s.post({ pick: true })
|
|
28
30
|
})
|
|
29
|
-
const colWidth = Math.max(20, Math.floor((s.columns || 90) / Math.max(1, props.groups.length)))
|
|
31
|
+
const colWidth = Math.max(20, Math.floor((s.columns || 90) / Math.max(1, props.loading ? 3 : props.groups.length)))
|
|
30
32
|
// Rows as drawn: 0 the provider names, 1..tallest the models, a blank, then the effort line.
|
|
31
|
-
const tallest = Math.max(0, ...props.groups.map(g => g.models.length))
|
|
33
|
+
const tallest = props.loading ? 5 : Math.max(0, ...props.groups.map(g => g.models.length))
|
|
32
34
|
const effortRow = tallest + 2
|
|
33
35
|
const EFFORT_START = '● Effort: '.length
|
|
34
36
|
/** The flat model index under a cell, or -1. */
|
|
@@ -71,8 +73,22 @@ const Picker: ClientModule<Props, State> = (props, s) => {
|
|
|
71
73
|
}
|
|
72
74
|
})
|
|
73
75
|
let i = 0
|
|
76
|
+
// Skeleton: three columns of placeholder bars where the providers and their models will be.
|
|
77
|
+
const skeleton = (
|
|
78
|
+
<Box flexDirection="row">
|
|
79
|
+
{[0, 1, 2].map(col => (
|
|
80
|
+
<Box flexDirection="column" width={colWidth}>
|
|
81
|
+
<Text dimColor>{'▒'.repeat(8 + col * 2)}</Text>
|
|
82
|
+
{[14, 11, 16, 9, 13].map((width, row) => (
|
|
83
|
+
<Text dimColor wrap="truncate">{' '}{'░'.repeat(Math.min(colWidth - 6, width + ((row + col) % 3)))}</Text>
|
|
84
|
+
))}
|
|
85
|
+
</Box>
|
|
86
|
+
))}
|
|
87
|
+
</Box>
|
|
88
|
+
)
|
|
74
89
|
return (
|
|
75
90
|
<Box flexDirection="column">
|
|
91
|
+
{props.loading ? skeleton : (
|
|
76
92
|
<Box flexDirection="row">
|
|
77
93
|
{props.groups.map(g => (
|
|
78
94
|
<Box flexDirection="column" width={colWidth}>
|
|
@@ -91,6 +107,7 @@ const Picker: ClientModule<Props, State> = (props, s) => {
|
|
|
91
107
|
</Box>
|
|
92
108
|
))}
|
|
93
109
|
</Box>
|
|
110
|
+
)}
|
|
94
111
|
<Box marginTop={1}>
|
|
95
112
|
<Text>
|
|
96
113
|
<Text color="warning">● </Text>
|
|
@@ -106,7 +123,7 @@ const Picker: ClientModule<Props, State> = (props, s) => {
|
|
|
106
123
|
</Text>
|
|
107
124
|
</Box>
|
|
108
125
|
<Text dimColor>
|
|
109
|
-
{flat[props.cursor]?.id ?? ''} · ↑/↓ or hover · Enter, click or 1-9 picks · type anything to close
|
|
126
|
+
{props.loading ? 'Loading your TokenHub models…' : `${flat[props.cursor]?.id ?? ''} · ↑/↓ or hover · Enter, click or 1-9 picks · type anything to close`}
|
|
110
127
|
</Text>
|
|
111
128
|
</Box>
|
|
112
129
|
)
|
|
@@ -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,8 +8,15 @@ 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
|
+
|
|
16
|
+
const MODELS_PATH = '/member/api/models'
|
|
17
|
+
/** How long the picker waits for the gateway before it falls back to the built-in list. */
|
|
18
|
+
const FETCH_TIMEOUT_MS = 3000
|
|
10
19
|
|
|
11
|
-
const FLAT = GROUPS.flatMap(g => g.models)
|
|
12
20
|
const matches = (id: string, current: string) => id === current || id.endsWith('/' + current)
|
|
13
21
|
|
|
14
22
|
/** TokenHub hosts: `<slug>.aimpact.io.vn`, or a local gateway on 8787-8789. */
|
|
@@ -23,6 +31,71 @@ function isTokenHub(base: string | undefined): boolean {
|
|
|
23
31
|
}
|
|
24
32
|
}
|
|
25
33
|
|
|
34
|
+
/** @returns The groups of an answer when it has the expected shape and at least one model, else null. */
|
|
35
|
+
function parseGroups(text: string): Group[] | null {
|
|
36
|
+
let body: unknown
|
|
37
|
+
try {
|
|
38
|
+
body = JSON.parse(text)
|
|
39
|
+
} catch {
|
|
40
|
+
return null
|
|
41
|
+
}
|
|
42
|
+
const groups = (body as { groups?: unknown })?.groups
|
|
43
|
+
if (!Array.isArray(groups)) return null
|
|
44
|
+
const out: Group[] = []
|
|
45
|
+
for (const group of groups) {
|
|
46
|
+
const provider = (group as { provider?: unknown })?.provider
|
|
47
|
+
const models = (group as { models?: unknown })?.models
|
|
48
|
+
if (typeof provider !== 'string' || !Array.isArray(models)) continue
|
|
49
|
+
const list = models
|
|
50
|
+
.filter((m): m is { id: string; name: string } => typeof m?.id === 'string' && typeof m?.name === 'string')
|
|
51
|
+
.map(m => ({ id: m.id, name: m.name }))
|
|
52
|
+
if (list.length > 0) out.push({ provider, models: list })
|
|
53
|
+
}
|
|
54
|
+
return out.length > 0 ? out : null
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
/**
|
|
58
|
+
* Reads the key's models from the gateway and stores them; a failure, an old gateway, an empty answer or no answer
|
|
59
|
+
* within `FETCH_TIMEOUT_MS` leaves the last good list, or marks the list offline when there is none.
|
|
60
|
+
*/
|
|
61
|
+
async function loadModels($: EngineInterface): Promise<void> {
|
|
62
|
+
let groups: Group[] | null = null
|
|
63
|
+
try {
|
|
64
|
+
const base = await $.env.get('ANTHROPIC_BASE_URL')
|
|
65
|
+
const key = await $.env.get('ANTHROPIC_AUTH_TOKEN')
|
|
66
|
+
if (base && key) {
|
|
67
|
+
const stop = new AbortController()
|
|
68
|
+
// A sleep that cannot run (refused, or cut short by the abort below) never counts as a timeout.
|
|
69
|
+
const timeout = $.clock.sleep(FETCH_TIMEOUT_MS, { signal: stop.signal }).then(() => null, () => new Promise<null>(() => {}))
|
|
70
|
+
const answer = await Promise.race([
|
|
71
|
+
$.http.fetch(`${base.replace(/\/+$/, '')}${MODELS_PATH}`, { headers: { 'x-api-key': key } }).catch(() => null),
|
|
72
|
+
timeout,
|
|
73
|
+
])
|
|
74
|
+
stop.abort()
|
|
75
|
+
if (answer?.ok) groups = parseGroups(answer.text)
|
|
76
|
+
}
|
|
77
|
+
} catch {
|
|
78
|
+
groups = null
|
|
79
|
+
}
|
|
80
|
+
if (groups) {
|
|
81
|
+
const fresh = groups
|
|
82
|
+
await update($, liveGroups, () => fresh)
|
|
83
|
+
await update($, listState, () => 'live')
|
|
84
|
+
} else if ((await read($, liveGroups)) === null) {
|
|
85
|
+
await update($, listState, () => 'offline')
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
/** @returns The list the picker shows now: the gateway's once read, else the built-in one (empty while loading). */
|
|
90
|
+
async function groupsOf($: EngineInterface): Promise<Group[]> {
|
|
91
|
+
const live = await read($, liveGroups)
|
|
92
|
+
if (live) return live
|
|
93
|
+
return (await read($, listState)) === 'loading' ? [] : GROUPS
|
|
94
|
+
}
|
|
95
|
+
async function flatOf($: EngineInterface) {
|
|
96
|
+
return (await groupsOf($)).flatMap(g => g.models)
|
|
97
|
+
}
|
|
98
|
+
|
|
26
99
|
async function savedEffort($: EngineInterface): Promise<string> {
|
|
27
100
|
return String((await $.settings.read({ key: 'effortLevel' })) ?? 'high')
|
|
28
101
|
}
|
|
@@ -36,7 +109,7 @@ async function stepEffort($: EngineInterface, by: number) {
|
|
|
36
109
|
|
|
37
110
|
/** Closes the picker and switches: `/effort` first when it changed, then `/model`. */
|
|
38
111
|
async function pick($: EngineInterface, index: number) {
|
|
39
|
-
const model =
|
|
112
|
+
const model = (await flatOf($))[index]
|
|
40
113
|
if (!model) return
|
|
41
114
|
await update($, isOpen, () => false)
|
|
42
115
|
const saved = await savedEffort($)
|
|
@@ -46,10 +119,18 @@ async function pick($: EngineInterface, index: number) {
|
|
|
46
119
|
}
|
|
47
120
|
|
|
48
121
|
async function cursorOf($: EngineInterface): Promise<number> {
|
|
122
|
+
const flat = await flatOf($)
|
|
49
123
|
const at = await read($, cursorAt)
|
|
50
|
-
if (at !== null) return at
|
|
124
|
+
if (at !== null && at < flat.length) return at
|
|
51
125
|
const current = await $.session.model()
|
|
52
|
-
return Math.max(0,
|
|
126
|
+
return Math.max(0, flat.findIndex(m => matches(m.id, current)))
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
async function moveCursor($: EngineInterface, by: number) {
|
|
130
|
+
const length = (await flatOf($)).length
|
|
131
|
+
if (length === 0) return
|
|
132
|
+
const at = await cursorOf($)
|
|
133
|
+
await update($, cursorAt, () => (at + by + length) % length)
|
|
53
134
|
}
|
|
54
135
|
|
|
55
136
|
export const register: Register = on => {
|
|
@@ -67,6 +148,9 @@ export const register: Register = on => {
|
|
|
67
148
|
// Start from the current model and the saved effort; a failed reset is not fatal.
|
|
68
149
|
await update($, cursorAt, () => null).catch(err => $.ui.toast(`tokenhub-models cursor: ${String(err)}`))
|
|
69
150
|
await update($, effortPick, () => null).catch(err => $.ui.toast(`tokenhub-models effort: ${String(err)}`))
|
|
151
|
+
// A list read earlier this session shows at once and is refreshed behind it; the first open shows the skeleton.
|
|
152
|
+
if ((await read($, liveGroups).catch(() => null)) === null) await update($, listState, () => 'loading').catch(() => undefined)
|
|
153
|
+
void loadModels($)
|
|
70
154
|
return { text: 'TokenHub model picker opened above the prompt.' }
|
|
71
155
|
})
|
|
72
156
|
|
|
@@ -99,9 +183,7 @@ export const register: Register = on => {
|
|
|
99
183
|
}
|
|
100
184
|
if (!open) return next(e)
|
|
101
185
|
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)
|
|
186
|
+
await moveCursor($, arrow === 'down' ? 1 : -1).catch(() => undefined)
|
|
105
187
|
return { ...r, text: '', cursor: 0 }
|
|
106
188
|
}
|
|
107
189
|
const typed = e.inputText
|
|
@@ -125,19 +207,22 @@ export const register: Register = on => {
|
|
|
125
207
|
const { Box, Text, Button, Client } = $.ui.resolve(e)
|
|
126
208
|
const current = await $.session.model()
|
|
127
209
|
const effort = (await read($, effortPick)) ?? (await savedEffort($))
|
|
210
|
+
const state = await read($, listState)
|
|
211
|
+
const groups = await groupsOf($)
|
|
128
212
|
const cursor = await cursorOf($)
|
|
129
213
|
const close = () => void update($, isOpen, () => false)
|
|
214
|
+
const note = state === 'offline' ? 'offline list · TokenHub did not answer' : ''
|
|
130
215
|
return (
|
|
131
216
|
<Box flexDirection="column" borderStyle="round" paddingX={1}>
|
|
132
217
|
<Box flexDirection="row">
|
|
133
218
|
<Text bold>Select model </Text>
|
|
134
|
-
<Text dimColor>TokenHub · current {current} </Text>
|
|
219
|
+
<Text dimColor>TokenHub · current {current}{note ? ` · ${note}` : ''} </Text>
|
|
135
220
|
<Button key="builtin" plain dimColor onPress={() => { close(); void $.command.run({ command: 'model' }) }}>Built-in picker</Button>
|
|
136
221
|
<Text> </Text>
|
|
137
222
|
<Button key="cancel" plain dimColor onPress={close}>Cancel</Button>
|
|
138
223
|
</Box>
|
|
139
224
|
<Client key="picker" module="./picker.tsx"
|
|
140
|
-
props={{ groups:
|
|
225
|
+
props={{ groups, loading: groups.length === 0, current, cursor, effort, efforts: EFFORTS }} />
|
|
141
226
|
</Box>
|
|
142
227
|
)
|
|
143
228
|
})
|
|
@@ -152,7 +237,8 @@ export const register: Register = on => {
|
|
|
152
237
|
pickAt?: number
|
|
153
238
|
effortTo?: string
|
|
154
239
|
}
|
|
155
|
-
const
|
|
240
|
+
const length = (await flatOf($)).length
|
|
241
|
+
const isIndex = (n: unknown): n is number => Number.isInteger(n) && (n as number) >= 0 && (n as number) < length
|
|
156
242
|
if (isIndex(data.pickAt)) {
|
|
157
243
|
await pick($, data.pickAt)
|
|
158
244
|
} else if (isIndex(data.cursorTo)) {
|
|
@@ -162,8 +248,7 @@ export const register: Register = on => {
|
|
|
162
248
|
const to = data.effortTo
|
|
163
249
|
await update($, effortPick, () => to)
|
|
164
250
|
} else if (data.move) {
|
|
165
|
-
|
|
166
|
-
await update($, cursorAt, () => (at + data.move! + FLAT.length) % FLAT.length)
|
|
251
|
+
await moveCursor($, data.move)
|
|
167
252
|
} else if (data.effort) {
|
|
168
253
|
await stepEffort($, data.effort)
|
|
169
254
|
} else if (data.pick) {
|
|
@@ -1,6 +1,13 @@
|
|
|
1
|
+
export type PickerModel = { id: string; name: string }
|
|
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
|
+
}
|
|
4
12
|
}
|
|
5
13
|
}
|
|
6
|
-
export type IsOpen = boolean
|