@emesoft/tokenhub 0.8.6 → 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 CHANGED
@@ -69,10 +69,11 @@ Prefer not to install? Use `npx @emesoft/tokenhub connect <your tenant name>` in
69
69
  - Asks whether to install the **TokenHub model picker**, unless you pass `--no-model-picker` or there is no terminal
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
- opens a picker of TokenHub models above the prompt: providers side by side, ↑/↓ and Enter or `1`–`9` to pick,
73
- `,` / `.` for effort. `/model <id>` and other hosts keep Claude Code's own picker. The list is fixed in the plugin and
74
- does not follow what your key may use; the plugin relies on Claude Code's early-access plugin hooks, which can change
75
- between Claude Code releases.
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 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,6 +1,6 @@
1
1
  {
2
2
  "name": "tokenhub-models",
3
- "version": "0.1.0",
3
+ "version": "0.3.0",
4
4
  "description": "A bare /model opens a TokenHub model picker above the prompt when ANTHROPIC_BASE_URL points at TokenHub",
5
5
  "author": { "name": "Emesoft" },
6
6
  "license": "MIT",
@@ -1,5 +1,6 @@
1
- // Spike: a static copy of the gateway's declared catalogs (src/model-catalog.ts) with the
2
- // provider prefixes of ADR-0045. Antigravity has no prefix, so it is left out.
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
@@ -13,7 +15,10 @@ type Props = {
13
15
 
14
16
  const matches = (id: string, current: string) => id === current || id.endsWith('/' + current)
15
17
 
16
- const Picker: ClientModule<Props> = (props, s) => {
18
+ // Local to the drawing: the effort level under the mouse, which only highlights.
19
+ type State = { hoverEffort?: string }
20
+
21
+ const Picker: ClientModule<Props, State> = (props, s) => {
17
22
  const { Box, Text } = s.elements
18
23
  const flat = props.groups.flatMap(g => g.models)
19
24
  s.onKey(k => {
@@ -23,10 +28,67 @@ const Picker: ClientModule<Props> = (props, s) => {
23
28
  else if (k.key === 'left' || k.key === ',') s.post({ effort: -1 })
24
29
  else if (k.key === 'return') s.post({ pick: true })
25
30
  })
26
- 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)))
32
+ // Rows as drawn: 0 the provider names, 1..tallest the models, a blank, then the effort line.
33
+ const tallest = props.loading ? 5 : Math.max(0, ...props.groups.map(g => g.models.length))
34
+ const effortRow = tallest + 2
35
+ const EFFORT_START = '● Effort: '.length
36
+ /** The flat model index under a cell, or -1. */
37
+ const modelAt = (x: number, y: number) => {
38
+ const col = Math.floor(x / colWidth)
39
+ const group = props.groups[col]
40
+ if (!group || y < 1 || y > group.models.length) return -1
41
+ return props.groups.slice(0, col).reduce((n, g) => n + g.models.length, 0) + (y - 1)
42
+ }
43
+ /** The effort level under a column of the effort line, or undefined. */
44
+ const effortAt = (x: number) => {
45
+ let start = EFFORT_START
46
+ for (const e of props.efforts) {
47
+ const width = (e === props.effort ? `[${e}]` : e).length
48
+ if (x >= start && x < start + width) return e
49
+ start += width + 2
50
+ }
51
+ return undefined
52
+ }
53
+ const hoverEffort = s.state?.hoverEffort
54
+ const setHover = (level: string | undefined) => {
55
+ if (level !== hoverEffort) s.setState({ hoverEffort: level })
56
+ }
57
+ // The mouse as Claude Code's own picker: hovering a model moves the highlight, a click picks it;
58
+ // hovering an effort level highlights it, a click sets it.
59
+ s.onPointer(p => {
60
+ if (p.type === 'leave') {
61
+ setHover(undefined)
62
+ } else if (p.type === 'move' && !p.button) {
63
+ const at = modelAt(p.x, p.y)
64
+ if (at >= 0 && at !== props.cursor) s.post({ cursorTo: at })
65
+ setHover(p.y === effortRow ? effortAt(p.x) : undefined)
66
+ } else if (p.type === 'up' && p.button === 'left') {
67
+ const at = modelAt(p.x, p.y)
68
+ if (at >= 0) s.post({ pickAt: at })
69
+ else if (p.y === effortRow) {
70
+ const level = effortAt(p.x)
71
+ if (level && level !== props.effort) s.post({ effortTo: level })
72
+ }
73
+ }
74
+ })
27
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
+ )
28
89
  return (
29
90
  <Box flexDirection="column">
91
+ {props.loading ? skeleton : (
30
92
  <Box flexDirection="row">
31
93
  {props.groups.map(g => (
32
94
  <Box flexDirection="column" width={colWidth}>
@@ -45,16 +107,23 @@ const Picker: ClientModule<Props> = (props, s) => {
45
107
  </Box>
46
108
  ))}
47
109
  </Box>
110
+ )}
48
111
  <Box marginTop={1}>
49
112
  <Text>
50
113
  <Text color="warning">● </Text>
51
114
  {'Effort: '}
52
- {props.efforts.map(e => (e === props.effort ? `[${e}]` : e)).join(' ')}
53
- <Text dimColor> , / . to adjust</Text>
115
+ {props.efforts.flatMap((e, j) => [
116
+ ...(j > 0 ? [<Text>{' '}</Text>] : []),
117
+ <Text color={e === hoverEffort ? 'suggestion' : undefined}
118
+ dimColor={e !== props.effort && e !== hoverEffort}>
119
+ {e === props.effort ? `[${e}]` : e}
120
+ </Text>,
121
+ ])}
122
+ <Text dimColor> , / . or click to adjust</Text>
54
123
  </Text>
55
124
  </Box>
56
125
  <Text dimColor>
57
- {flat[props.cursor]?.id ?? ''} · ↑/↓ select · Enter 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`}
58
127
  </Text>
59
128
  </Box>
60
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 = FLAT[index]
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, FLAT.findIndex(m => matches(m.id, current)))
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
- const at = await cursorOf($)
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,29 +207,48 @@ 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: GROUPS, current, cursor, effort, efforts: EFFORTS }} />
225
+ props={{ groups, loading: groups.length === 0, current, cursor, effort, efforts: EFFORTS }} />
141
226
  </Box>
142
227
  )
143
228
  })
144
229
 
145
230
  on('ui.message', async ($, e, next) => {
146
231
  if (e.element !== 'picker') return next(e)
147
- const data = e.data as { move?: number; effort?: number; pick?: boolean }
148
- if (data.move) {
149
- const at = await cursorOf($)
150
- await update($, cursorAt, () => (at + data.move! + FLAT.length) % FLAT.length)
232
+ const data = e.data as {
233
+ move?: number
234
+ effort?: number
235
+ pick?: boolean
236
+ cursorTo?: number
237
+ pickAt?: number
238
+ effortTo?: string
239
+ }
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
242
+ if (isIndex(data.pickAt)) {
243
+ await pick($, data.pickAt)
244
+ } else if (isIndex(data.cursorTo)) {
245
+ const to = data.cursorTo
246
+ await update($, cursorAt, () => to)
247
+ } else if (typeof data.effortTo === 'string' && EFFORTS.includes(data.effortTo)) {
248
+ const to = data.effortTo
249
+ await update($, effortPick, () => to)
250
+ } else if (data.move) {
251
+ await moveCursor($, data.move)
151
252
  } else if (data.effort) {
152
253
  await stepEffort($, data.effort)
153
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': { isOpen: boolean; cursor: number | null; effort: string | null }
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
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@emesoft/tokenhub",
3
- "version": "0.8.6",
3
+ "version": "0.8.8",
4
4
  "description": "Connect Claude Code to a TokenHub pool in one command, and track your spend from the terminal",
5
5
  "keywords": [
6
6
  "claude",