@emesoft/tokenhub 0.8.8 → 0.8.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/README.md CHANGED
@@ -69,8 +69,8 @@ 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, 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
72
+ opens a picker of TokenHub models above the prompt: providers side by side, ↑/↓ and Enter or a click to pick (rows are numbered by position),
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
74
  (`/member/api/models`: the providers your key may use, with a skeleton while it loads); when the pool does not
75
75
  answer within a few seconds it falls back to a built-in list, marked "offline list". The plugin relies on Claude
76
76
  Code's early-access plugin hooks, which can change between Claude Code releases.
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "tokenhub-models",
3
- "version": "0.3.0",
3
+ "version": "0.4.2",
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,19 +1,19 @@
1
1
  // The built-in list: a static copy of the gateway's declared catalogs (src/model-catalog.ts) with the provider
2
2
  // prefixes of ADR-0045, shown only when the gateway's own list (`GET /member/api/models`, #737) cannot be read.
3
3
  // Antigravity has no prefix, so it is left out.
4
- export type Model = { id: string; name: string }
4
+ export type Model = { id: string; name: string; contextWindow?: number | null }
5
5
  export type Group = { provider: string; models: Model[] }
6
6
 
7
7
  export const GROUPS: Group[] = [
8
8
  {
9
9
  provider: 'Claude Code',
10
10
  models: [
11
- { id: 'cc/claude-fable-5-1', name: 'Fable 5.1' },
12
- { id: 'cc/claude-opus-5-5', name: 'Opus 5.5' },
13
- { id: 'cc/claude-opus-5', name: 'Opus 5' },
14
- { id: 'cc/claude-sonnet-5-5', name: 'Sonnet 5.5' },
15
- { id: 'cc/claude-sonnet-5', name: 'Sonnet 5' },
16
- { 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' },
17
17
  ],
18
18
  },
19
19
  {
@@ -1,22 +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 effort handed in as
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
8
  /** True while the gateway's list is being read for the first time: the columns draw as a skeleton. */
9
9
  loading?: boolean
10
10
  current: string
11
11
  cursor: number
12
12
  effort: string
13
13
  efforts: string[]
14
+ /** Whether a pick of a 1M model asks Claude Code for its 1M window (`[1m]`, #746). */
15
+ use1m?: boolean
14
16
  }
15
17
 
16
- const matches = (id: string, current: string) => id === current || id.endsWith('/' + current)
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`
17
25
 
18
- // Local to the drawing: the effort level under the mouse, which only highlights.
19
- 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 }
20
28
 
21
29
  const Picker: ClientModule<Props, State> = (props, s) => {
22
30
  const { Box, Text } = s.elements
@@ -26,13 +34,23 @@ const Picker: ClientModule<Props, State> = (props, s) => {
26
34
  else if (k.key === 'up') s.post({ move: -1 })
27
35
  else if (k.key === 'right' || k.key === '.') s.post({ effort: 1 })
28
36
  else if (k.key === 'left' || k.key === ',') s.post({ effort: -1 })
37
+ else if (k.key === '[') s.post({ toggle1m: true })
29
38
  else if (k.key === 'return') s.post({ pick: true })
30
39
  })
31
40
  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.
41
+ // Rows as drawn: 0 the provider names, 1..tallest the models, a blank, the effort line, then the context line.
33
42
  const tallest = props.loading ? 5 : Math.max(0, ...props.groups.map(g => g.models.length))
34
43
  const effortRow = tallest + 2
44
+ const contextRow = effortRow + 1
35
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
36
54
  /** The flat model index under a cell, or -1. */
37
55
  const modelAt = (x: number, y: number) => {
38
56
  const col = Math.floor(x / colWidth)
@@ -40,39 +58,56 @@ const Picker: ClientModule<Props, State> = (props, s) => {
40
58
  if (!group || y < 1 || y > group.models.length) return -1
41
59
  return props.groups.slice(0, col).reduce((n, g) => n + g.models.length, 0) + (y - 1)
42
60
  }
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
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
50
68
  }
51
69
  return undefined
52
70
  }
71
+ const effortChoices = props.efforts.map(e => ({ key: e, label: e }))
53
72
  const hoverEffort = s.state?.hoverEffort
54
- const setHover = (level: string | undefined) => {
55
- if (level !== hoverEffort) s.setState({ hoverEffort: level })
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 })
56
76
  }
57
77
  // 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.
78
+ // hovering an effort level or a context choice highlights it, a click sets it.
59
79
  s.onPointer(p => {
60
80
  if (p.type === 'leave') {
61
- setHover(undefined)
81
+ setHover(undefined, undefined)
62
82
  } else if (p.type === 'move' && !p.button) {
63
83
  const at = modelAt(p.x, p.y)
64
84
  if (at >= 0 && at !== props.cursor) s.post({ cursorTo: at })
65
- setHover(p.y === effortRow ? effortAt(p.x) : undefined)
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
+ )
66
89
  } else if (p.type === 'up' && p.button === 'left') {
67
90
  const at = modelAt(p.x, p.y)
68
91
  if (at >= 0) s.post({ pickAt: at })
69
92
  else if (p.y === effortRow) {
70
- const level = effortAt(p.x)
93
+ const level = choiceAt(p.x, EFFORT_START, effortChoices, props.effort)
71
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 })
72
98
  }
73
99
  }
74
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
+ ])
75
109
  let i = 0
110
+ const numberWidth = String(flat.length).length
76
111
  // Skeleton: three columns of placeholder bars where the providers and their models will be.
77
112
  const skeleton = (
78
113
  <Box flexDirection="row">
@@ -86,6 +121,11 @@ const Picker: ClientModule<Props, State> = (props, s) => {
86
121
  ))}
87
122
  </Box>
88
123
  )
124
+ const contextNote = props.loading
125
+ ? ''
126
+ : cursorTakes1m
127
+ ? ' [ or click to switch'
128
+ : ` ${windowLabel(cursorModel?.contextWindow) || 'window unknown'} · no 1M for this model`
89
129
  return (
90
130
  <Box flexDirection="column">
91
131
  {props.loading ? skeleton : (
@@ -97,10 +137,13 @@ const Picker: ClientModule<Props, State> = (props, s) => {
97
137
  const at = i++
98
138
  const isCursor = at === props.cursor
99
139
  const isCurrent = matches(m.id, props.current)
100
- const digit = at < 9 ? `${at + 1}.` : ' '
140
+ // Every row's position, padded so the names line up; a number is not a key (#751).
141
+ const digit = `${String(at + 1).padStart(numberWidth)}.`
142
+ const label = windowLabel(m.contextWindow)
101
143
  return (
102
144
  <Text wrap="truncate" color={isCursor ? 'suggestion' : undefined} dimColor={!isCursor && !isCurrent}>
103
145
  {isCursor ? '› ' : ' '}{digit} {m.name}{isCurrent ? ' ✔' : ''}
146
+ {label ? <Text dimColor>{` ${label}`}</Text> : ''}
104
147
  </Text>
105
148
  )
106
149
  })}
@@ -112,18 +155,18 @@ const Picker: ClientModule<Props, State> = (props, s) => {
112
155
  <Text>
113
156
  <Text color="warning">● </Text>
114
157
  {'Effort: '}
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
- ])}
158
+ {choiceLine(effortChoices, props.effort, hoverEffort)}
122
159
  <Text dimColor> , / . or click to adjust</Text>
123
160
  </Text>
124
161
  </Box>
162
+ <Text>
163
+ <Text color={props.use1m ? 'suggestion' : 'warning'}>● </Text>
164
+ {'Context: '}
165
+ {choiceLine(contextChoices, contextOn, hoverContext)}
166
+ <Text dimColor>{contextNote}</Text>
167
+ </Text>
125
168
  <Text dimColor>
126
- {props.loading ? 'Loading your TokenHub models…' : `${flat[props.cursor]?.id ?? ''} · ↑/↓ or hover · Enter, click or 1-9 picks · type anything to close`}
169
+ {props.loading ? 'Loading your TokenHub models…' : `${cursorModel?.id ?? ''}${props.use1m && cursorTakes1m ? '[1m]' : ''} · ↑/↓ or hover · Enter or click picks · type anything to close`}
127
170
  </Text>
128
171
  </Box>
129
172
  )
@@ -12,12 +12,17 @@ const effortPick = atom({ plugin: 'tokenhub-models', key: 'effort' } as const, n
12
12
  // comes from now: `loading` before the first answer, `live` once one came, `offline` when it could not be read.
13
13
  const liveGroups = atom({ plugin: 'tokenhub-models', key: 'groups' } as const, null)
14
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)
15
17
 
16
18
  const MODELS_PATH = '/member/api/models'
17
19
  /** How long the picker waits for the gateway before it falls back to the built-in list. */
18
20
  const FETCH_TIMEOUT_MS = 3000
19
21
 
20
- const matches = (id: string, current: string) => id === current || id.endsWith('/' + current)
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))
21
26
 
22
27
  /** TokenHub hosts: `<slug>.aimpact.io.vn`, or a local gateway on 8787-8789. */
23
28
  function isTokenHub(base: string | undefined): boolean {
@@ -47,8 +52,8 @@ function parseGroups(text: string): Group[] | null {
47
52
  const models = (group as { models?: unknown })?.models
48
53
  if (typeof provider !== 'string' || !Array.isArray(models)) continue
49
54
  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 }))
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 }))
52
57
  if (list.length > 0) out.push({ provider, models: list })
53
58
  }
54
59
  return out.length > 0 ? out : null
@@ -115,7 +120,10 @@ async function pick($: EngineInterface, index: number) {
115
120
  const saved = await savedEffort($)
116
121
  const effort = (await read($, effortPick)) ?? saved
117
122
  if (effort !== saved) void $.command.run({ command: 'effort', args: effort })
118
- void $.command.run({ command: 'model', args: model.id })
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 })
119
127
  }
120
128
 
121
129
  async function cursorOf($: EngineInterface): Promise<number> {
@@ -148,6 +156,9 @@ export const register: Register = on => {
148
156
  // Start from the current model and the saved effort; a failed reset is not fatal.
149
157
  await update($, cursorAt, () => null).catch(err => $.ui.toast(`tokenhub-models cursor: ${String(err)}`))
150
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)
151
162
  // A list read earlier this session shows at once and is refreshed behind it; the first open shows the skeleton.
152
163
  if ((await read($, liveGroups).catch(() => null)) === null) await update($, listState, () => 'loading').catch(() => undefined)
153
164
  void loadModels($)
@@ -171,7 +182,8 @@ export const register: Register = on => {
171
182
  })
172
183
 
173
184
  // While the picker is open and the prompt is empty, the prompt drives it: up/down move the
174
- // cursor, `,` / `.` step the effort, 1-9 pick a model. Anything else types as usual.
185
+ // cursor, `,` / `.` step the effort, `[` switches 1M (the row numbers are positions, not keys). Anything else typed closes the
186
+ // picker and types as usual.
175
187
  on('prompt.edit', async ($, e, next) => {
176
188
  const arrow = e.key?.key === 'up' || e.key?.key === 'down' ? e.key.key : undefined
177
189
  if (arrow && e.text === '') {
@@ -187,7 +199,12 @@ export const register: Register = on => {
187
199
  return { ...r, text: '', cursor: 0 }
188
200
  }
189
201
  const typed = e.inputText
190
- if (e.text !== '' || !(typed === ',' || typed === '.' || /^[1-9]$/.test(typed))) return next(e)
202
+ const isPickerKey = e.text === '' && (typed === ',' || typed === '.' || typed === '[')
203
+ if (!isPickerKey) {
204
+ // Anything else typed while the picker is open closes it, as its hint says; the text stays in the prompt.
205
+ if (typed !== '' && (await read($, isOpen).catch(() => false))) await update($, isOpen, () => false).catch(() => undefined)
206
+ return next(e)
207
+ }
191
208
  let open = false
192
209
  try {
193
210
  open = await read($, isOpen)
@@ -197,7 +214,7 @@ export const register: Register = on => {
197
214
  if (!open) return next(e)
198
215
  const r = await next(e)
199
216
  if (typed === ',' || typed === '.') await stepEffort($, typed === '.' ? 1 : -1).catch(() => undefined)
200
- else await pick($, Number(typed) - 1).catch(() => undefined)
217
+ else await update($, use1m, isOn => !isOn).catch(() => undefined)
201
218
  return { ...r, text: '', cursor: 0 }
202
219
  })
203
220
 
@@ -222,7 +239,7 @@ export const register: Register = on => {
222
239
  <Button key="cancel" plain dimColor onPress={close}>Cancel</Button>
223
240
  </Box>
224
241
  <Client key="picker" module="./picker.tsx"
225
- props={{ groups, loading: groups.length === 0, current, cursor, effort, efforts: EFFORTS }} />
242
+ props={{ groups, loading: groups.length === 0, current, cursor, effort, efforts: EFFORTS, use1m: await read($, use1m) }} />
226
243
  </Box>
227
244
  )
228
245
  })
@@ -236,6 +253,7 @@ export const register: Register = on => {
236
253
  cursorTo?: number
237
254
  pickAt?: number
238
255
  effortTo?: string
256
+ toggle1m?: boolean
239
257
  }
240
258
  const length = (await flatOf($)).length
241
259
  const isIndex = (n: unknown): n is number => Number.isInteger(n) && (n as number) >= 0 && (n as number) < length
@@ -247,6 +265,8 @@ export const register: Register = on => {
247
265
  } else if (typeof data.effortTo === 'string' && EFFORTS.includes(data.effortTo)) {
248
266
  const to = data.effortTo
249
267
  await update($, effortPick, () => to)
268
+ } else if (data.toggle1m) {
269
+ await update($, use1m, isOn => !isOn)
250
270
  } else if (data.move) {
251
271
  await moveCursor($, data.move)
252
272
  } else if (data.effort) {
@@ -1,4 +1,4 @@
1
- export type PickerModel = { id: string; name: string }
1
+ export type PickerModel = { id: string; name: string; contextWindow?: number | null }
2
2
  export type PickerGroup = { provider: string; models: PickerModel[] }
3
3
  declare module 'claude-code' {
4
4
  interface PluginState {
@@ -8,6 +8,7 @@ declare module 'claude-code' {
8
8
  effort: string | null
9
9
  groups: PickerGroup[] | null
10
10
  listState: string
11
+ use1m: boolean
11
12
  }
12
13
  }
13
14
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@emesoft/tokenhub",
3
- "version": "0.8.8",
3
+ "version": "0.8.10",
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",