release: 0.2.0 — custom models, gateway context caps, free daily keys

- Custom models: add a model by id (protocol, reasoning, images, context
  window, max output) next to the built-in catalog.
- Gateway context caps: read per-model context_window from each key's
  /v1/models and narrow the catalog model to the tightest window (free
  DeepSeek group: 200K, 32K output) so DSH compacts before the cap.
- Free daily keys: show each key's expiry, flag the renewal window and
  link to https://www.geek2api.com/free-ds; the no-usable-key error points
  there too.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
大阔andClaude Opus 5.5 committed 2026-11-16 21:44:12 +08:00
1 parent 93a5fe93b4
commit a8f689e39c
21 files changed
+2161 -427

No files matched your search

+94
View File
@@ -0,0 +1,94 @@
import assert from 'node:assert/strict'
import test from 'node:test'
import { cappedCatalogModel, GEEK2API_MODELS } from '../src/catalog.js'
import { createGroupReader, parseContextWindows, parseKeyLifetime } from '../src/groups.js'
import { createModelSelectionController } from '../src/model-selection.js'
import { fixedHostPool } from './helpers.mjs'
const DS = 'deepseek-v4.1-flash'
function jsonResponse(payload, status = 200) {
const body = JSON.stringify(payload)
return { ok: status >= 200 && status < 300, status, json: async () => JSON.parse(body), text: async () => body }
}
test('per-model context windows are read from the model list, ignoring noise', () => {
const windows = parseContextWindows([
{ id: DS, context_window: 200000 },
{ id: 'gpt-6.1-sol' },
{ id: 'tiny', context_window: 100 },
{ id: 'bad', context_window: '200000' },
])
assert.deepEqual([...windows], [[DS, 200000]])
assert.equal(parseContextWindows(undefined).size, 0)
})
test('the key lifetime marks a daily free key and where to renew it', () => {
const daily = parseKeyLifetime({ key: { expires_at: '2026-11-17T12:00:00Z', purpose: 'free_daily', renew_window_hours: 2, renew_path: '/free-ds' } })
assert.equal(daily.daily, true)
assert.equal(daily.expiresAt, Date.parse('2026-11-17T12:00:00Z'))
assert.equal(daily.renewWindowHours, 2)
assert.equal(daily.renewPath, '/free-ds')
const plain = parseKeyLifetime({ key: { expires_at: '2027-01-01T00:00:00Z' } })
assert.equal(plain.daily, false)
assert.equal(plain.renewPath, undefined)
assert.equal(parseKeyLifetime({ key: {} }), undefined)
assert.equal(parseKeyLifetime({ key: { expires_at: 'soon' } }), undefined)
assert.equal(parseKeyLifetime({ key: { expires_at: '2026-11-17T12:00:00Z', purpose: 'free_daily', renew_path: 'javascript:alert(1)' } }).renewPath, '/free-ds')
})
test('a capped model keeps a positive compaction budget', () => {
const ds = GEEK2API_MODELS.find(model => model.id === DS)
assert.equal(ds.contextWindow, 1_000_000)
const capped = cappedCatalogModel(ds, 200000)
assert.equal(capped.contextWindow, 200000)
assert.equal(capped.maxTokens, 32000)
// DSH compaction: window - max output - 64K headroom must stay positive.
assert.ok(capped.contextWindow - capped.maxTokens - 65536 > 90000)
assert.equal(cappedCatalogModel(ds, undefined), ds)
assert.equal(cappedCatalogModel(ds, 2_000_000), ds, 'a looser limit never widens the model')
})
test('the free key narrows the catalog model; a paid key alone leaves it at 1M', async () => {
const lists = {
'sk-free': [{ id: DS, context_window: 200000 }],
'sk-paid': [{ id: DS }, { id: 'claude-opus-5-5' }],
}
const fetchImpl = async (url, options) => {
const key = String(options.headers.Authorization).replace('Bearer ', '')
if (url.endsWith('/v1/models')) return jsonResponse({ object: 'list', data: lists[key] ?? [] })
if (url.endsWith('/v1/usage?lite=1')) {
return jsonResponse(key === 'sk-free'
? { mode: 'unrestricted', key: { name: 'd', expires_at: '2026-11-17T12:00:00Z', purpose: 'free_daily', renew_window_hours: 2, renew_path: '/free-ds' } }
: { mode: 'unrestricted', key: { name: 'p' } })
}
throw new Error(`unrouted ${url}`)
}
let keys = [{ id: 'p', label: 'paid', key: 'sk-paid', disabled: false }]
const reader = createGroupReader({ listKeys: async () => keys, hosts: await fixedHostPool(), fetchImpl })
const selection = createModelSelectionController({
readPreference: () => ({}),
writePreference: () => {},
isWritable: () => false,
contextLimitFor: id => reader.contextLimitFor(id),
})
const dsOf = () => selection.models().find(model => model.id === DS)
await reader.read()
assert.equal(reader.contextLimitFor(DS), undefined)
assert.equal(dsOf().contextWindow, 1_000_000)
const before = selection.models()
keys = [...keys, { id: 'f', label: 'free', key: 'sk-free', disabled: false }]
await reader.read({ force: true })
assert.equal(reader.contextLimitFor(DS), 200000)
assert.equal(dsOf().contextWindow, 200000)
assert.equal(dsOf().maxTokens, 32000)
assert.notEqual(selection.models(), before, 'a new cap is a new catalog, so the route rebuilds')
assert.equal(selection.models(), selection.models(), 'an unchanged cap keeps the catalog identity')
assert.equal(reader.lifetimeOf('f').daily, true)
assert.equal(reader.lifetimeOf('p'), undefined)
const claude = selection.models().find(model => model.id === 'claude-opus-5-5')
assert.equal(claude.contextWindow, 1_000_000, 'models without a cap are untouched')
})
+223
View File
@@ -0,0 +1,223 @@
import assert from 'node:assert/strict'
import test from 'node:test'
import { PiAiAdapter } from '@deepseek-ai/dsh-llm-pi-ai'
import { customCatalogModel, GEEK2API_MODEL_IDS } from '../src/catalog.js'
import { PROVIDER } from '../src/constants.js'
import {
DEFAULT_CUSTOM_CONTEXT_WINDOW,
DEFAULT_CUSTOM_MAX_TOKENS,
MAX_CUSTOM_MODELS,
parseCustomModel,
readCustomModels,
validateCustomModels,
} from '../src/custom-models.js'
import { createGroupReader } from '../src/groups.js'
import { createHostPool } from '../src/hosts.js'
import { Geek2APIKeyRing } from '../src/key-ring.js'
import { createModelSelectionController } from '../src/model-selection.js'
import { createRouteAdapter } from '../src/route.js'
import { fakeCredentials, fixedHostPool, stubHostPool } from './helpers.mjs'
test('a custom model needs only an id; everything else has a default', () => {
assert.deepEqual({ ...parseCustomModel({ id: ' qwen3-max ' }) }, {
id: 'qwen3-max',
name: '',
api: 'openai-completions',
reasoning: false,
image: false,
contextWindow: DEFAULT_CUSTOM_CONTEXT_WINDOW,
maxTokens: DEFAULT_CUSTOM_MAX_TOKENS,
})
// A small window caps the default output.
assert.equal(parseCustomModel({ id: 'tiny', contextWindow: 8000 }).maxTokens, 8000)
// The panel's number inputs may arrive as strings.
assert.equal(parseCustomModel({ id: 'm', contextWindow: '200000', maxTokens: '64000' }).maxTokens, 64_000)
assert.equal(parseCustomModel({ id: 'org/model:free@v2+x', name: ' My model ' }).name, 'My model')
})
test('a custom model that cannot be served is refused with a reason the panel may show', () => {
const refuses = (entry, pattern) => assert.throws(() => parseCustomModel(entry), error => {
assert.match(error.message, /^Geek2API /u, 'the RPC only lets messages it authored through')
assert.match(error.message, pattern)
return true
})
refuses({ id: '' }, /needs a model id/u)
refuses({ id: 'has space' }, /may only use/u)
refuses({ id: '-leading' }, /may only use/u)
refuses({ id: 'x'.repeat(129) }, /may only use/u)
refuses({ id: 'claude-opus-5-5' }, /already a built-in/u)
refuses({ id: 'm', api: 'google-generative-ai' }, /unsupported protocol/u)
refuses({ id: 'm', contextWindow: 10 }, /context window/u)
refuses({ id: 'm', contextWindow: 1.5e4 + 0.5 }, /context window/u)
refuses({ id: 'm', contextWindow: 16_000, maxTokens: 32_000 }, /max output/u)
refuses({ id: 'm', maxTokens: 0 }, /max output/u)
refuses('m', /must be objects/u)
assert.throws(() => validateCustomModels([{ id: 'a' }, { id: 'a' }]), /listed twice/u)
assert.throws(() => validateCustomModels(Array.from({ length: MAX_CUSTOM_MODELS + 1 }, (_, i) => ({ id: `m${i}` }))), /at most/u)
assert.throws(() => validateCustomModels('a'), /must be a list/u)
})
test('a hand-edited list is read leniently: only unservable entries are dropped', () => {
const read = readCustomModels([{ id: 'ok-1' }, { id: 'gpt-6.1-sol' }, { id: 'bad id' }, null, { id: 'ok-1', name: 'dup' }, { id: 'ok-2', api: 'anthropic-messages' }])
assert.deepEqual(read.map(entry => entry.id), ['ok-1', 'ok-2'])
assert.equal(read[0].name, '')
assert.deepEqual(readCustomModels(undefined), [])
assert.deepEqual(readCustomModels({ id: 'x' }), [])
})
test('each protocol gets the base URL and compat its built-in models use', () => {
const completions = customCatalogModel(parseCustomModel({ id: 'qwen3-max' }))
assert.equal(completions.provider, PROVIDER)
assert.equal(completions.name, 'qwen3-max', 'an empty name shows the id')
assert.equal(completions.baseUrl, 'https://hk.geek2api.com/v1')
assert.equal(completions.compat.maxTokensField, 'max_tokens')
assert.equal(completions.reasoning, false)
assert.deepEqual([...completions.input], ['text'])
const responses = customCatalogModel(parseCustomModel({ id: 'gpt-x', api: 'openai-responses', reasoning: true, image: true }))
assert.equal(responses.baseUrl, 'https://hk.geek2api.com/v1')
assert.equal(responses.compat, undefined)
assert.deepEqual([...responses.input], ['text', 'image'])
assert.equal(responses.thinkingLevelMap.minimal, null)
const messages = customCatalogModel(parseCustomModel({ id: 'claude-x', api: 'anthropic-messages', name: 'Claude X' }))
assert.equal(messages.baseUrl, 'https://hk.geek2api.com', 'the Anthropic client appends /v1/messages itself')
assert.equal(messages.name, 'Claude X')
})
/** The controller over an in-memory preference, as `apply()` composes it. */
function selection(initial = {}) {
let values = { ...initial }
const seen = { changes: 0, catalogChanges: 0, writes: [] }
const controller = createModelSelectionController({
readPreference: () => values,
writePreference: async patch => {
seen.writes.push(patch)
values = { ...values, ...patch }
},
isWritable: () => true,
onChange: () => { seen.changes += 1 },
onCatalogChange: () => { seen.catalogChanges += 1 },
})
return { controller, seen, set: patch => { values = { ...values, ...patch } } }
}
test('custom models join the catalog after the built-in ones, selected by default', async () => {
const { controller, seen } = selection()
const before = controller.models()
assert.equal(controller.models(), before, 'an unchanged catalog keeps its identity')
const status = await controller.updateCustom({ customModels: [{ id: 'qwen3-max', name: 'Qwen3 Max' }, { id: 'claude-x', api: 'anthropic-messages' }] })
assert.deepEqual(seen.writes.at(-1).customModels.map(entry => entry.id), ['qwen3-max', 'claude-x'])
assert.deepEqual(controller.ids(), [...GEEK2API_MODEL_IDS, 'qwen3-max', 'claude-x'])
assert.notEqual(controller.models(), before)
assert.equal(seen.changes, 1)
assert.equal(seen.catalogChanges, 1)
const qwen = status.models.find(model => model.id === 'qwen3-max')
assert.deepEqual(qwen, { id: 'qwen3-max', name: 'Qwen3 Max', selected: true, custom: true })
assert.equal(status.models[0].custom, undefined)
assert.deepEqual(status.customModels.map(entry => entry.api), ['openai-completions', 'anthropic-messages'])
assert.deepEqual(status.protocols, ['openai-completions', 'openai-responses', 'anthropic-messages'])
// A custom model can be hidden like any other.
await controller.update({ selectedIds: GEEK2API_MODEL_IDS })
assert.equal(controller.isVisible('qwen3-max'), false)
assert.equal(seen.catalogChanges, 1, 'hiding a model does not re-read coverage')
// An invalid list is refused without writing anything.
const writes = seen.writes.length
await assert.rejects(controller.updateCustom({ customModels: [{ id: 'gpt-6-luna' }] }), /already a built-in/u)
assert.equal(seen.writes.length, writes)
await controller.updateCustom({ customModels: [] })
assert.deepEqual(controller.ids(), [...GEEK2API_MODEL_IDS])
assert.equal(seen.catalogChanges, 2)
})
test('a hand edit of the custom list is followed on reconcile', () => {
const { controller, seen, set } = selection()
set({ customModels: [{ id: 'kimi-k4' }] })
controller.reconcile()
assert.equal(seen.changes, 1)
assert.equal(seen.catalogChanges, 1)
assert.ok(controller.ids().includes('kimi-k4'))
controller.reconcile()
assert.equal(seen.changes, 1, 'the same list is no change')
})
test('the route serves a custom model on its protocol and refreshes with the catalog', async t => {
const { controller } = selection()
const ring = new Geek2APIKeyRing(fakeCredentials())
await ring.add('sk-fixture-only-not-a-real-key', 'fixture')
const adapter = createRouteAdapter({
ring,
hosts: stubHostPool(createHostPool),
keys: { active: () => ring.activeKey(), forRequest: (_model, key) => key, onKeyFailure: () => {} },
fallbackSessionId: 'custom-fixture',
listModels: controller.models,
})
assert.ok(adapter instanceof PiAiAdapter)
await assert.rejects(adapter.resolveModel(PROVIDER, 'qwen3-max'), /no configured model/u)
await controller.updateCustom({ customModels: [{ id: 'qwen3-max', contextWindow: 256_000 }, { id: 'think-x', api: 'openai-responses', reasoning: true }] })
assert.deepEqual((await adapter.listModels(PROVIDER)).slice(-2).map(model => model.id), ['qwen3-max', 'think-x'])
const qwen = await adapter.resolveModel(PROVIDER, 'qwen3-max')
assert.equal(qwen.context.contextWindow, 256_000)
assert.equal(qwen.reasoning?.defaultEffort, undefined)
const think = await adapter.resolveModel(PROVIDER, 'think-x')
assert.deepEqual(think.reasoning.efforts.map(effort => effort.id), ['off', 'low', 'medium', 'high'])
const requests = []
t.mock.method(globalThis, 'fetch', async (input, init) => {
const request = new Request(input, init)
requests.push({ url: request.url, body: await request.json() })
const chunks = [
{ id: 'c1', object: 'chat.completion.chunk', created: 1, model: 'qwen3-max', choices: [{ index: 0, delta: { role: 'assistant', content: 'ok' }, finish_reason: null }] },
{ id: 'c1', object: 'chat.completion.chunk', created: 1, model: 'qwen3-max', choices: [{ index: 0, delta: {}, finish_reason: 'stop' }] },
]
return new Response(chunks.map(event => `data: ${JSON.stringify(event)}\n\n`).join('') + 'data: [DONE]\n\n', { headers: { 'Content-Type': 'text/event-stream' } })
})
const { createUserMessage } = await import('@deepseek-ai/dsh-llm')
const prepared = await adapter.prepareCall(PROVIDER, 'qwen3-max')
let last
for await (const chunk of prepared.stream({
provider: PROVIDER,
model: 'qwen3-max',
sessionId: 'custom-fixture',
maxTokens: 64,
// A non-reasoning custom model is asked for `off` like a built-in one.
reasoningEffort: 'high',
messages: [createUserMessage({ content: [{ type: 'text', text: 'hi' }], source: { kind: 'user' } })],
})) last = chunk
assert.equal(last.reason.kind, 'stop')
assert.equal(requests.length, 1)
assert.equal(requests[0].url, 'https://h1.example/v1/chat/completions')
assert.equal(requests[0].body.model, 'qwen3-max')
assert.equal(requests[0].body.reasoning_effort, undefined)
await controller.updateCustom({ customModels: [] })
await assert.rejects(adapter.resolveModel(PROVIDER, 'qwen3-max'), /no configured model/u, 'a removed model never falls back to another')
})
test('key coverage is measured for custom models too', async () => {
const { controller } = selection({ customModels: [{ id: 'qwen3-max' }] })
const fetchImpl = async url => {
const body = url.endsWith('/v1/models')
? { object: 'list', data: ['qwen3-max', 'claude-opus-5-5', 'other'].map(id => ({ id })) }
: { mode: 'unrestricted', balance: 1 }
return { ok: true, status: 200, json: async () => body, text: async () => JSON.stringify(body) }
}
const reader = createGroupReader({
listKeys: async () => [{ id: 'k', label: 'k', key: 'sk-fixture', disabled: false }],
hosts: await fixedHostPool(),
modelIds: () => controller.ids(),
fetchImpl,
})
const value = await reader.read()
assert.equal(value.keys[0].coverage['qwen3-max'], true)
assert.equal(value.keys[0].coverage['gpt-6.1-sol'], false)
assert.equal(value.keys[0].extraModels, 1, 'a custom model is no longer "outside the list"')
assert.equal(reader.covers('k', 'qwen3-max'), true)
})
+27 -1
View File
@@ -59,7 +59,9 @@ function host({ generation = '0.2', writable = true, failWrite = false, initial
},
effect(callback) { return callback() },
}
const config = generation === '0.2' ? { hiddenModels: { get: () => values.hiddenModels } } : {}
const config = generation === '0.2'
? { hiddenModels: { get: () => values.hiddenModels }, customModels: { get: () => values.customModels } }
: {}
apply(ctx, config)
return {
seen,
@@ -182,3 +184,27 @@ test('the actual DSH LLM registry refreshes discovery atomically and keeps a pre
assert.deepEqual((await llm.listModels('geek2api')).map(model => model.id), ['grok-4.7'])
registration()
})
for (const generation of ['0.1', '0.2']) {
test(`DSH ${generation}: a custom model added in the panel reaches the picker and survives reconstruction`, async () => {
const env = host({ generation })
const { seen } = env
const added = await seen.handler('models/custom/update', { customModels: [{ id: 'qwen3-max', name: 'Qwen3 Max', api: 'openai-completions' }] })
assert.equal(added.ok, true)
assert.deepEqual(seen.updates.at(-1).customModels.map(entry => entry.id), ['qwen3-max'])
assert.equal(seen.notifications, 1, 'one registry refresh for the changed catalog')
assert.equal(added.value.models.at(-1).id, 'qwen3-max')
assert.equal(added.value.models.at(-1).custom, true)
assert.equal((await seen.adapter.listModels('geek2api')).at(-1).id, 'qwen3-max')
assert.equal((await seen.adapter.resolveModel('geek2api', 'qwen3-max')).id, 'qwen3-max')
assert.equal((await env.reapply().seen.adapter.listModels('geek2api')).at(-1).id, 'qwen3-max')
const refused = await seen.handler('models/custom/update', { customModels: [{ id: 'claude-opus-5-5' }] })
assert.equal(refused.ok, false)
assert.match(refused.error.message, /already a built-in model/u, 'the reason reaches the panel')
await seen.handler('models/custom/update', { customModels: [] })
assert.equal(seen.notifications, 2)
assert.ok(!(await seen.adapter.listModels('geek2api')).some(model => model.id === 'qwen3-max'))
})
}
+1 -1
View File
@@ -9,7 +9,7 @@ import { fakeCredentials, stubHostPool } from './helpers.mjs'
import { Config } from '../src/index.js'
test('search settings declare volatile fields accepted by DSH 0.2', () => {
for (const field of ['searchProvider', 'searchBackend', 'hiddenModels', 'apiHosts', 'claudeRuntime']) {
for (const field of ['searchProvider', 'searchBackend', 'hiddenModels', 'customModels', 'apiHosts', 'claudeRuntime']) {
assert.equal(Config.dict[field].meta.volatile, true, `${field}: DSH 0.2 otherwise rejects settings.update with no volatile fields`)
}
})