A team of long-lived bots for DeepSeek Harness, with main bots, bot-to-bot messages and group chats.
Serve model providers and their keys to a window without DSH settings
Fei2411a6428142313b
diff --git a/ds-bot/index.js b/ds-bot/index.jsindex 59104ef5..b7dfb260 100644--- a/ds-bot/index.js+++ b/ds-bot/index.js@@ -5,6 +5,7 @@ import { join } from 'node:path'import * as diagnostics from './src/host/diagnostics.js'import * as store from './src/host/store.js'import * as models from './src/host/models.js'+import * as providers from './src/host/providers.js'import * as permissions from './src/host/permissions.js'import * as turns from './src/host/turns.js'import * as handoffs from './src/host/handoffs.js'@@ -42,7 +43,7 @@ export const inject = ['llm', 'tools', 'systemPrompt', 'sessionController', 'ses// the functions other modules call; they call them through `rt` at run time, never during// install. Listeners run and tools are listed in registration order, so this order holds:// usage meters model requests after every listener that rewrites them.-const MODULES = [diagnostics, store, models, permissions, turns, handoffs, delivery, roster, folders, agents, prompt, timeContext, tools, chatIndex, groups, approvals, resume, memory, parts, images, secrets, connectors, capabilities, schedules, browser, usage, update, dshUpdate, feed, channel]+const MODULES = [diagnostics, store, models, providers, permissions, turns, handoffs, delivery, roster, folders, agents, prompt, timeContext, tools, chatIndex, groups, approvals, resume, memory, parts, images, secrets, connectors, capabilities, schedules, browser, usage, update, dshUpdate, feed, channel]export function apply(ctx, config = {}) {createRuntime(ctx, config)diff --git a/ds-bot/src/host/channel.js b/ds-bot/src/host/channel.jsindex 167b3b57..44375a7f 100644--- a/ds-bot/src/host/channel.js+++ b/ds-bot/src/host/channel.js@@ -82,7 +82,7 @@ export function install(rt) {})// What a host that only reads the team may still answer.// Updates change the dsh profile, not the team, so a reading host may run them too.- const READS = new Set(['state', 'models', 'efforts', 'earlier', 'chat', 'run-stats', 'room-progress', 'exchange', 'exchange-text', 'file-text', 'usage', 'schedules', 'memory-view', 'browser-wait', 'browser-result', 'diagnostics', 'diagnostics-log', 'diagnostics-clear', 'update-check', 'update-install', 'dsh-update-install', 'repo-stars', 'approval-answer'])+ const READS = new Set(['state', 'models', 'efforts', 'providers', 'provider-models', 'earlier', 'chat', 'run-stats', 'room-progress', 'exchange', 'exchange-text', 'file-text', 'usage', 'schedules', 'memory-view', 'browser-wait', 'browser-result', 'diagnostics', 'diagnostics-log', 'diagnostics-clear', 'update-check', 'update-install', 'dsh-update-install', 'repo-stars', 'approval-answer'])function setPrefs(body) {if (body.theme !== undefined) {@@ -121,9 +121,19 @@ export function install(rt) {return ok({ modelsKey: modelsKey() })case 'efforts':return ok(await rt.reasoningOf(String(body.ref ?? '')) ?? { efforts: [] })- case 'model-key':- await rt.setModelKey(String(body.provider ?? ''), body.value)- return ok({ modelsKey: modelsKey() })+ // Model providers and their keys, for a window without DSH's own Models page.+ case 'providers':+ return ok(await rt.providersView())+ case 'provider-key':+ if (body.value === null) await rt.clearProviderKey(String(body.provider ?? ''))+ else await rt.setProviderKey(String(body.provider ?? ''), body.value)+ return ok(await rt.providersView())+ case 'provider-add':+ return ok(await rt.addProvider(body))+ case 'provider-models':+ return ok(await rt.discoverModels(body))+ case 'provider-remove':+ return ok(await rt.removeProvider(String(body.provider ?? '')))case 'duplicate-bot':await rt.load()return ok(await rt.duplicateBot(body.id))diff --git a/ds-bot/src/host/models.js b/ds-bot/src/host/models.jsindex 9513b78f..3bb8474a 100644--- a/ds-bot/src/host/models.js+++ b/ds-bot/src/host/models.js@@ -17,47 +17,6 @@ export function install(rt) {ctx.on?.('llm/adapters-updated', () => { catalogCache = { at: 0, value: [] }; reasoningCache.clear() })for (const name of ['credentials/reference-updated', 'settings/document-updated']) ctx.on?.(name, () => { catalogCache = { at: 0, value: [] } })- // Per served provider, the credential its DSH settings name as its API key- // (`apiKeyEnv`). A provider with none authenticates some other way.- async function keyRefs() {- const settings = ctx.get?.('settings')- if (settings === undefined || ctx.get?.('credentials') === undefined || ctx.llm.listConfigurableProviders === undefined) return new Map()- const [directory, sections] = await Promise.all([ctx.llm.listConfigurableProviders(), settings.describe({ redactSecrets: true })])- const values = new Map(sections.map(section => [section.ns, section.value]))- const refs = new Map()- for (const entry of directory) {- const ref = entry.settingsPath.reduce((value, key) => value?.[key], values.get(entry.settingsNs))?.apiKeyEnv- if (typeof ref === 'string' && ref !== '') refs.set(entry.provider, ref)- }- return refs- }-- // DSH serves a provider's models whether or not its key is set; without one, every- // turn on them fails.- async function missingKeys(providers) {- const refs = await keyRefs().catch(() => new Map())- const credentials = ctx.get?.('credentials')- const missing = new Set()- await Promise.all(providers.filter(id => refs.has(id)).map(async (id) => {- const info = await credentials.describe(refs.get(id)).catch(() => undefined)- if (info?.configured === false) missing.add(id)- }))- return missing- }-- // A key typed in a window that has no DSH settings of its own. It goes to DSH's- // credential store under the provider's reference and is never read back.- async function setModelKey(provider, value) {- const ref = (await keyRefs()).get(provider)- if (ref === undefined) throw new Error(`${provider} takes no API key here`)- const key = checkKey(value)- const credentials = ctx.get('credentials')- if ((await credentials.describe(ref)).writable === false) throw new Error(`The API key for ${provider} is set outside DS Bot and cannot be changed here`)- await credentials.set(ref, key)- catalogCache = { at: 0, value: [] }- await catalog({ fresh: true })- }-// The thinking levels a model serves: `{efforts: [{id, name, description?}],// defaultEffort?}`, or undefined when it has none or the provider cannot say.async function reasoningOf(ref) {@@ -93,7 +52,9 @@ export function install(rt) {})}}- const missing = await missingKeys([...new Set(entries.map(entry => entry.provider))])+ // DSH serves a provider's models whether or not its key is set; without one, every+ // turn on them fails.+ const missing = await rt.missingKeys?.([...new Set(entries.map(entry => entry.provider))]) ?? new Set()for (const entry of entries) if (missing.has(entry.provider)) entry.needsKey = trueconst used = new Set(Object.values(state.bots ?? {}).flatMap(bot => [bot.model, bot.appliedModel].filter(Boolean)))await Promise.all(entries.filter(entry => used.has(entry.ref)).map(async (entry) => {@@ -241,17 +202,7 @@ export function install(rt) {Object.assign(rt, {catalog, cachedCatalog: () => catalogCache.value, cachedDefault: () => pickDefault(catalogCache.value),defaultRef, defaultModel, resolveModel, desiredModel, reasoningOf, resolveEffort, desiredEffort,- selectSessionModel, applyModel, waitForModel, syncModels, setModelKey,+ selectSessionModel, applyModel, waitForModel, syncModels,+ dropCatalog: () => { catalogCache = { at: 0, value: [] } },})}--// What an HTTP header can carry, like DSH's own key check: a pasted `NAME=value` line-// or a quoted value would be stored as the key and refused by the provider.-export function checkKey(value) {- const key = typeof value === 'string' ? value.trim() : ''- if (key === '') throw new Error('Enter the API key')- if (/^[A-Za-z_][A-Za-z0-9_]*=[^=]/.test(key)) throw new Error('Paste only the key, not a NAME=value line')- if (/^(["']).*\1$/.test(key)) throw new Error('Paste the key without quotes')- if (!/^[\x21-\x7E]+$/.test(key)) throw new Error('An API key has no spaces or special characters')- return key-}diff --git a/ds-bot/src/host/providers.js b/ds-bot/src/host/providers.jsnew file mode 100644index 00000000..49814dd1--- /dev/null+++ b/ds-bot/src/host/providers.js@@ -0,0 +1,226 @@+// Model providers and their API keys, for a window that has no DSH Models page of its+// own. DSH keeps all of it: a provider route in the settings of the adapter that serves+// it, the route's key in DSH's credential store under the reference the route names.+// The providers to add are the ones DSH's directory of configurable providers offers.++const PI_AI = 'llm-pi-ai'+const ROUTE_ID = /^[a-z][a-z0-9]*(?:-[a-z0-9]+)*$/+// The wire protocols DSH's pi-ai adapter serves on a route it ships nothing about.+export const PROTOCOLS = ['openai-completions', 'openai-responses', 'anthropic-messages']+const MAX_MODELS = 500++// The reference DSH's own Models page derives for a route's key, so a provider added+// here and one added there read the same key.+export const keyRefFor = provider => `${provider.toUpperCase().replace(/[^A-Z0-9]+/g, '_')}_API_KEY`++const invalid = message => Object.assign(new Error(message), { code: 'bot/invalid' })++// What an HTTP header can carry, like DSH's own key check: a pasted `NAME=value` line+// or a quoted value would be stored as the key and refused by the provider.+export function checkKey(value) {+ const key = typeof value === 'string' ? value.trim() : ''+ if (key === '') throw invalid('Enter the API key')+ if (/^[A-Za-z_][A-Za-z0-9_]*=[^=]/.test(key)) throw invalid('Paste only the key, not a NAME=value line')+ if (/^(["']).*\1$/.test(key)) throw invalid('Paste the key without quotes')+ if (!/^[\x21-\x7E]+$/.test(key)) throw invalid('An API key has no spaces or special characters')+ return key+}++function checkEndpoint(value) {+ const text = typeof value === 'string' ? value.trim() : ''+ let url+ try { url = new URL(text) } catch { throw invalid('Enter the endpoint as an http or https address') }+ if (url.protocol !== 'http:' && url.protocol !== 'https:') throw invalid('Enter the endpoint as an http or https address')+ return text+}++// A model as the route stores it: what discovery told about it, under pi-ai's names.+function modelEntry(model) {+ const id = typeof model === 'string' ? model.trim() : typeof model?.id === 'string' ? model.id.trim() : ''+ if (id === '') throw invalid('Every model needs an id')+ const whole = value => (Number.isInteger(value) && value > 0 ? value : undefined)+ return {+ id,+ ...(typeof model?.name === 'string' && model.name.trim() !== '' ? { name: model.name.trim() } : {}),+ ...(whole(model?.contextWindow) ? { contextWindow: model.contextWindow } : {}),+ ...(whole(model?.maxTokens) ? { maxTokens: model.maxTokens } : {}),+ ...(Array.isArray(model?.inputModalities) && model.inputModalities.length > 0 ? { input: model.inputModalities.filter(kind => typeof kind === 'string') } : {}),+ }+}++export function install(rt) {+ const { ctx } = rt++ const seams = () => {+ const settings = ctx.get?.('settings')+ const credentials = ctx.get?.('credentials')+ return settings && credentials && ctx.llm.listConfigurableProviders ? { settings, credentials } : undefined+ }+ const usable = () => {+ const found = seams()+ if (found === undefined) throw invalid('This DSH cannot set up model providers')+ return found+ }+ const sections = settings => new Map(settings.describe({ redactSecrets: true }).map(section => [section.ns, section]))+ const profileOf = (entry, map, layer = 'value') => entry.settingsPath.reduce((value, key) => value?.[key], map.get(entry.settingsNs)?.[layer])+ const refOf = profile => (typeof profile?.apiKeyEnv === 'string' && profile.apiKeyEnv !== '' ? profile.apiKeyEnv : undefined)+ const entryOf = provider => ctx.llm.listConfigurableProviders().find(entry => entry.provider === provider)++ // Per provider route, the credential its DSH settings name as its API key. A+ // provider with none authenticates some other way.+ function keyRefs() {+ const found = seams()+ if (found === undefined) return new Map()+ const map = sections(found.settings)+ const refs = new Map()+ for (const entry of ctx.llm.listConfigurableProviders()) {+ const ref = refOf(profileOf(entry, map))+ if (ref !== undefined) refs.set(entry.provider, ref)+ }+ return refs+ }++ async function missingKeys(providers) {+ let refs+ try { refs = keyRefs() } catch { return new Set() }+ const missing = new Set()+ await Promise.all(providers.filter(id => refs.has(id)).map(async (id) => {+ const info = await seams().credentials.describe(refs.get(id)).catch(() => undefined)+ if (info?.configured === false) missing.add(id)+ }))+ return missing+ }++ // The providers set up in DSH, with their models and key state, and the ones DSH+ // can add. A provider with no key and no models (DeepSeek's account route before a+ // sign-in) is left out.+ async function providersView() {+ const found = seams()+ if (found === undefined) return { available: false }+ const map = sections(found.settings)+ const catalog = await rt.catalog({ fresh: true })+ const providers = []+ const addable = []+ for (const entry of ctx.llm.listConfigurableProviders()) {+ const profile = profileOf(entry, map)+ if (entry.settingsNs === PI_AI && profile === undefined) {+ if (entry.declared !== true) addable.push({ id: entry.provider, name: entry.displayName })+ continue+ }+ const ref = refOf(profile)+ const models = catalog.filter(model => model.provider === entry.provider).map(model => ({ ref: model.ref, name: model.name }))+ if (ref === undefined && models.length === 0 && entry.error === undefined) continue+ const info = ref === undefined ? undefined : await found.credentials.describe(ref).catch(() => undefined)+ providers.push({+ id: entry.provider,+ name: entry.displayName,+ models,+ key: ref === undefined ? null : { configured: info?.configured === true, writable: info?.writable !== false },+ removable: entry.settingsNs === PI_AI && profileOf(entry, map, 'user') !== undefined && profileOf(entry, map, 'base') === undefined,+ custom: entry.declared === true,+ ...(entry.error ? { error: entry.error } : {}),+ })+ }+ // The ones that come with the setup first, then the added ones as DSH lists them.+ providers.sort((a, b) => Number(a.removable) - Number(b.removable))+ return { available: true, providers, addable, protocols: PROTOCOLS }+ }++ async function writableRef(provider) {+ const ref = keyRefs().get(provider)+ if (ref === undefined) throw invalid(`${provider} takes no API key here`)+ if ((await usable().credentials.describe(ref)).writable === false) throw invalid(`The API key for ${provider} is set outside DS Bot and cannot be changed here`)+ return ref+ }++ // A key is only ever written; nothing here reads one back.+ async function setProviderKey(provider, value) {+ const key = checkKey(value)+ await usable().credentials.set(await writableRef(provider), key)+ rt.dropCatalog()+ }++ async function clearProviderKey(provider) {+ await usable().credentials.unset(await writableRef(provider))+ rt.dropCatalog()+ }++ // `provider` from DSH's directory, or a route the user declares (`custom`) for an+ // endpoint DSH ships nothing about. The route goes in first, then its key, as DSH's+ // own Models page does it: a failed key write leaves a provider that asks for one.+ async function addProvider(body) {+ const { settings, credentials } = usable()+ const provider = String(body.provider ?? '').trim()+ const map = sections(settings)+ const entry = entryOf(provider)+ if (entry !== undefined && (entry.settingsNs !== PI_AI || profileOf(entry, map) !== undefined)) throw invalid(`${provider} is already set up`)+ const custom = body.custom === true+ if (!custom && (entry === undefined || entry.declared === true)) throw invalid(`DSH offers no provider ${provider}`)+ if (custom && !ROUTE_ID.test(provider)) throw invalid('A provider id is lowercase letters, digits and dashes, starting with a letter')+ if (custom && entry !== undefined) throw invalid(`DSH already offers ${provider}; add it from its list`)+ const key = typeof body.key === 'string' && body.key.trim() !== '' ? checkKey(body.key) : undefined+ const ref = keyRefFor(provider)+ const info = await credentials.describe(ref)+ if (key !== undefined && info.writable === false) throw invalid(`The API key for ${provider} is set outside DS Bot and cannot be changed here`)+ // A blank key keeps a key that is already there, else the provider's own sign-in.+ const keyed = key !== undefined || info.configured === true+ const profile = custom ? customProfile(body) : {}+ if (keyed) profile.apiKeyEnv = ref+ await settings.mutate(PI_AI, [{ op: 'set', path: entry?.settingsPath ?? ['providers', provider], value: profile }], map.get(PI_AI)?.revision)+ if (key !== undefined) await credentials.set(ref, key)+ rt.dropCatalog()+ return providersView()+ }++ function customProfile(body) {+ if (!PROTOCOLS.includes(body.api)) throw invalid(`The protocol must be one of ${PROTOCOLS.join(', ')}`)+ const models = Array.isArray(body.models) ? body.models.slice(0, MAX_MODELS).map(modelEntry) : []+ if (models.length === 0) throw invalid('Add at least one model')+ if (new Set(models.map(model => model.id)).size !== models.length) throw invalid('Two models have the same id')+ const name = typeof body.name === 'string' ? body.name.trim().slice(0, 80) : ''+ return { ...(name ? { displayName: name } : {}), api: body.api, baseURL: checkEndpoint(body.baseURL), models }+ }++ // The models an endpoint (or a provider DSH ships) says it serves. The key typed for+ // the question goes with it and is not stored.+ async function discoverModels(body) {+ usable()+ const provider = typeof body.provider === 'string' && body.provider !== '' ? body.provider : undefined+ const request = provider !== undefined && entryOf(provider)?.declared === false+ ? { provider }+ : {+ baseURL: checkEndpoint(body.baseURL),+ ...(PROTOCOLS.includes(body.api) ? { api: body.api } : {}),+ ...(typeof body.key === 'string' && body.key.trim() !== '' ? { apiKey: checkKey(body.key) } : {}),+ }+ const models = await ctx.llm.discoverModels(PI_AI, request)+ return models.slice(0, MAX_MODELS).map(model => ({+ id: model.id,+ ...(model.name ? { name: model.name } : {}),+ ...(model.contextWindow ? { contextWindow: model.contextWindow } : {}),+ ...(model.maxTokens ? { maxTokens: model.maxTokens } : {}),+ ...(model.inputModalities ? { inputModalities: [...model.inputModalities] } : {}),+ }))+ }++ // Only a route the user added goes; its key goes with it when it is the one this page+ // derives and no other provider names it.+ async function removeProvider(provider) {+ const { settings, credentials } = usable()+ const map = sections(settings)+ const entry = entryOf(provider)+ if (entry === undefined || entry.settingsNs !== PI_AI || profileOf(entry, map) === undefined) throw invalid(`${provider} is not set up`)+ if (profileOf(entry, map, 'user') === undefined || profileOf(entry, map, 'base') !== undefined) throw invalid(`${provider} comes with this setup and cannot be removed`)+ const ref = refOf(profileOf(entry, map))+ const shared = [...keyRefs()].some(([other, otherRef]) => other !== provider && otherRef === ref)+ await settings.mutate(PI_AI, [{ op: 'unset', path: entry.settingsPath }], map.get(PI_AI)?.revision)+ if (ref === keyRefFor(provider) && !shared) {+ const info = await credentials.describe(ref)+ if (info.configured && info.writable) await credentials.unset(ref)+ }+ rt.dropCatalog()+ return providersView()+ }++ Object.assign(rt, { keyRefs, missingKeys, providersView, setProviderKey, clearProviderKey, addProvider, discoverModels, removeProvider })+}diff --git a/ds-bot/test/models.test.js b/ds-bot/test/models.test.jsindex 927468c6..248b3b6f 100644--- a/ds-bot/test/models.test.js+++ b/ds-bot/test/models.test.js@@ -1,6 +1,6 @@import assert from 'node:assert/strict'import { test } from 'node:test'-import { checkKey, install } from '../src/host/models.js'+import { install } from '../src/host/models.js'const PROVIDERS = {deepseek: [{ id: 'deepseek-v4.1-flash', name: 'DeepSeek V4.1 Flash' }, { id: 'deepseek-v4-pro' }],@@ -186,89 +186,3 @@ test('a Bot on the default model with its own thinking level leaves the defaultawait rt.applyModel({ id: 'b1', model: 'deepseek/v4-flash', effort: 'low' })assert.deepEqual(current(), fallback)})--// DSH's settings name each provider's key; the credential store says which are set.-function keyed({ keys = {}, writable = true } = {}) {- const store = { ...keys }- const writes = []- const listeners = new Map()- const services = {- settings: {- describe: async () => [- { ns: 'llm-deepseek', value: { apiKeyEnv: 'DEEPSEEK_API_KEY' } },- { ns: 'llm-pi-ai', value: { providers: { relay: { apiKeyEnv: 'RELAY_API_KEY' }, bedrock: {} } } },- ],- },- credentials: {- describe: async ref => ({ configured: ref in store, writable }),- set: async (ref, value) => { writes.push(ref); store[ref] = value },- },- }- const ctx = {- on(name, listener) { listeners.set(name, listener); return () => listeners.delete(name) },- get: name => services[name],- llm: {- listProviders: () => [{ id: 'deepseek-official', name: 'DeepSeek' }, { id: 'relay', name: 'Relay' }, { id: 'bedrock' }],- listModels: async id => [{ id: `${id}-model` }],- listConfigurableProviders: async () => [- { provider: 'deepseek-official', settingsNs: 'llm-deepseek', settingsPath: [] },- { provider: 'relay', settingsNs: 'llm-pi-ai', settingsPath: ['providers', 'relay'] },- { provider: 'bedrock', settingsNs: 'llm-pi-ai', settingsPath: ['providers', 'bedrock'] },- ],- },- }- const rt = { ctx, state: { bots: {} } }- install(rt)- return { rt, store, writes, listeners }-}--const needing = async rt => (await rt.catalog({ fresh: true })).filter(entry => entry.needsKey).map(entry => entry.provider)--test('models of a provider whose API key is set nowhere need the key', async () => {- assert.deepEqual(await needing(keyed().rt), ['deepseek-official', 'relay'])- assert.deepEqual(await needing(keyed({ keys: { DEEPSEEK_API_KEY: 'k' } }).rt), ['relay'])- // A provider without a key reference authenticates some other way.- assert.ok((await keyed().rt.catalog()).some(entry => entry.provider === 'bedrock' && !entry.needsKey))-})--test('without DSH settings or credentials no model needs a key', async () => {- const { rt } = keyed()- rt.ctx.get = () => undefined- assert.deepEqual(await needing(rt), [])-})--test('a saved API key goes to the provider\'s reference and frees its models', async () => {- const { rt, store, writes } = keyed()- await rt.catalog()- await rt.setModelKey('deepseek-official', ' sk-test ')- assert.deepEqual(writes, ['DEEPSEEK_API_KEY'])- assert.equal(store.DEEPSEEK_API_KEY, 'sk-test')- assert.deepEqual(rt.cachedCatalog().filter(entry => entry.needsKey).map(entry => entry.provider), ['relay'])- await assert.rejects(rt.setModelKey('bedrock', 'sk-test'), /bedrock takes no API key here/)-})--test('a key set outside DS Bot is not overwritten', async () => {- const { rt, writes } = keyed({ writable: false })- await assert.rejects(rt.setModelKey('deepseek-official', 'sk-test'), /set outside DS Bot/)- assert.deepEqual(writes, [])-})--test('a changed credential drops the cached catalog', async () => {- const { rt, store, listeners } = keyed()- assert.deepEqual(await needing(rt), ['deepseek-official', 'relay'])- store.RELAY_API_KEY = 'k'- assert.deepEqual((await rt.catalog()).filter(entry => entry.needsKey).map(entry => entry.provider), ['deepseek-official', 'relay'])- listeners.get('credentials/reference-updated')()- assert.deepEqual((await rt.catalog()).filter(entry => entry.needsKey).map(entry => entry.provider), ['deepseek-official'])-})--test('an API key is what an HTTP header carries, without a NAME= or quotes', () => {- assert.equal(checkKey(' sk-abc '), 'sk-abc')- assert.equal(checkKey('YWJj=='), 'YWJj==')- assert.throws(() => checkKey(' '), /Enter the API key/)- assert.throws(() => checkKey(undefined), /Enter the API key/)- assert.throws(() => checkKey('DEEPSEEK_API_KEY=sk-abc'), /not a NAME=value line/)- assert.throws(() => checkKey('"sk-abc"'), /without quotes/)- assert.throws(() => checkKey('sk abc'), /no spaces/)- assert.throws(() => checkKey('sk-密钥'), /no spaces/)-})diff --git a/ds-bot/test/providers.test.js b/ds-bot/test/providers.test.jsnew file mode 100644index 00000000..53e76241--- /dev/null+++ b/ds-bot/test/providers.test.js@@ -0,0 +1,224 @@+import assert from 'node:assert/strict'+import { test } from 'node:test'+import { install as installModels } from '../src/host/models.js'+import { checkKey, install, keyRefFor } from '../src/host/providers.js'++// A DSH with the official DeepSeek route, an account route, and the pi-ai adapter: its+// directory offers catalog providers, and its settings take path writes the way DSH's+// do, under a revision. Routes come and go with the pi-ai section.+function dsh({ keys = {}, readOnly = [], user = {}, base = {}, catalog = { anthropic: ['claude-a', 'claude-b'], moonshotai: ['kimi-k3'] } } = {}) {+ const store = { ...keys }+ const writes = []+ const listeners = new Map()+ let revision = 0+ let pi = { user: structuredClone(user), base: structuredClone(base) }+ const piValue = () => ({ providers: { ...pi.base.providers, ...pi.user.providers } })+ const routes = () => Object.keys(piValue().providers)+ const services = {+ settings: {+ describe: () => [+ { ns: 'llm-deepseek', revision: 0, value: { apiKeyEnv: 'DEEPSEEK_API_KEY' }, user: {}, base: { apiKeyEnv: 'DEEPSEEK_API_KEY' } },+ { ns: 'llm-deepseek-account', revision: 0, value: {}, user: {}, base: {} },+ { ns: 'llm-pi-ai', revision, value: piValue(), user: structuredClone(pi.user), base: structuredClone(pi.base) },+ ],+ mutate: async (ns, ops, expected) => {+ if (expected !== revision) throw new Error(`settings namespace "${ns}" changed since it was read`)+ const next = structuredClone(pi.user)+ for (const op of ops) {+ const parent = op.path.slice(0, -1).reduce((value, key) => (value[key] ??= {}), next)+ if (op.op === 'set') parent[op.path.at(-1)] = op.value+ else delete parent[op.path.at(-1)]+ }+ for (const [id, profile] of Object.entries(next.providers ?? {})) {+ if (!(id in catalog) && !(profile.models?.length > 0)) throw new Error(`llm-pi-ai: provider "${id}" resolves no models`)+ }+ writes.push([ns, ops])+ pi = { ...pi, user: next }+ revision += 1+ },+ },+ credentials: {+ describe: async ref => ({ configured: ref in store, writable: !readOnly.includes(ref) }),+ set: async (ref, value) => { writes.push(['set', ref]); store[ref] = value },+ unset: async (ref) => { writes.push(['unset', ref]); delete store[ref] },+ },+ }+ const discovered = []+ const ctx = {+ on(name, listener) { listeners.set(name, listener); return () => listeners.delete(name) },+ get: name => services[name],+ llm: {+ listProviders: () => [{ id: 'deepseek-official', name: 'DeepSeek' }, { id: 'deepseek-account', name: 'DeepSeek Account' }, ...routes().map(id => ({ id, name: piValue().providers[id].displayName ?? id }))],+ listModels: async (id) => {+ if (id === 'deepseek-official') return [{ id: 'deepseek-flash', name: 'DeepSeek Flash' }]+ if (id === 'deepseek-account') return []+ const profile = piValue().providers[id]+ return profile.models ?? (catalog[id] ?? []).map(model => ({ id: model }))+ },+ listConfigurableProviders: () => [+ { provider: 'deepseek-official', displayName: 'DeepSeek', settingsNs: 'llm-deepseek', settingsPath: [] },+ { provider: 'deepseek-account', displayName: 'DeepSeek Account', settingsNs: 'llm-deepseek-account', settingsPath: [] },+ ...Object.keys(catalog).map(id => ({ provider: id, displayName: id, settingsNs: 'llm-pi-ai', settingsPath: ['providers', id], declared: false })),+ ...routes().filter(id => !(id in catalog)).map(id => ({ provider: id, displayName: piValue().providers[id].displayName ?? id, settingsNs: 'llm-pi-ai', settingsPath: ['providers', id], declared: true })),+ ],+ discoverModels: async (ns, request) => { discovered.push([ns, request]); return [{ id: 'relay-1', name: 'Relay One', contextWindow: 128000, inputModalities: ['text'] }, { id: 'relay-2' }] },+ },+ }+ const rt = { ctx, state: { bots: {} } }+ installModels(rt)+ install(rt)+ return { rt, store, writes, listeners, discovered }+}++const needing = async rt => (await rt.catalog({ fresh: true })).filter(entry => entry.needsKey).map(entry => entry.provider)+const row = (view, id) => view.providers.find(provider => provider.id === id)++test('models of a provider whose API key is set nowhere need the key', async () => {+ const user = { providers: { moonshotai: { apiKeyEnv: 'MOONSHOTAI_API_KEY' }, bedrock: {} } }+ const catalog = { moonshotai: ['kimi-k3'], bedrock: ['nova'] }+ assert.deepEqual(await needing(dsh({ user, catalog }).rt), ['deepseek-official', 'moonshotai'])+ assert.deepEqual(await needing(dsh({ user, catalog, keys: { DEEPSEEK_API_KEY: 'k' } }).rt), ['moonshotai'])+ // A route without a key reference authenticates some other way.+ assert.ok((await dsh({ user, catalog }).rt.catalog()).some(entry => entry.provider === 'bedrock' && !entry.needsKey))+})++test('without DSH settings or credentials no model needs a key, and there is no Models page', async () => {+ const { rt } = dsh()+ rt.ctx.get = () => undefined+ assert.deepEqual(await needing(rt), [])+ assert.deepEqual(await rt.providersView(), { available: false })+})++test('the page lists the providers set up and the ones DSH can add', async () => {+ const { rt } = dsh({ keys: { DEEPSEEK_API_KEY: 'k' }, user: { providers: { moonshotai: { apiKeyEnv: 'MOONSHOTAI_API_KEY' } } } })+ const view = await rt.providersView()+ assert.deepEqual(view.providers.map(provider => [provider.id, provider.key, provider.removable, provider.models.length]), [+ ['deepseek-official', { configured: true, writable: true }, false, 1],+ ['moonshotai', { configured: false, writable: true }, true, 1],+ ])+ // The account route has no key and no models before a sign-in.+ assert.equal(row(view, 'deepseek-account'), undefined)+ assert.deepEqual(view.addable, [{ id: 'anthropic', name: 'anthropic' }])+ assert.deepEqual(view.protocols, ['openai-completions', 'openai-responses', 'anthropic-messages'])+})++test('a saved API key goes to the provider\'s reference and frees its models', async () => {+ const { rt, store, writes } = dsh()+ await rt.setProviderKey('deepseek-official', ' sk-test ')+ assert.deepEqual(writes, [['set', 'DEEPSEEK_API_KEY']])+ assert.equal(store.DEEPSEEK_API_KEY, 'sk-test')+ assert.deepEqual(await needing(rt), [])+ await rt.clearProviderKey('deepseek-official')+ assert.equal('DEEPSEEK_API_KEY' in store, false)+ await assert.rejects(rt.setProviderKey('deepseek-account', 'sk-test'), /deepseek-account takes no API key here/)+})++test('a key set outside DS Bot is not overwritten or removed', async () => {+ const { rt, writes } = dsh({ keys: { DEEPSEEK_API_KEY: 'k' }, readOnly: ['DEEPSEEK_API_KEY'] })+ await assert.rejects(rt.setProviderKey('deepseek-official', 'sk-test'), /set outside DS Bot/)+ await assert.rejects(rt.clearProviderKey('deepseek-official'), /set outside DS Bot/)+ assert.deepEqual(writes, [])+ assert.deepEqual(row(await rt.providersView(), 'deepseek-official').key, { configured: true, writable: false })+})++test('a provider from DSH\'s list goes in with the key DSH\'s own page would name', async () => {+ const { rt, store, writes } = dsh()+ const view = await rt.addProvider({ provider: 'moonshotai', key: 'sk-kimi' })+ assert.deepEqual(writes, [['llm-pi-ai', [{ op: 'set', path: ['providers', 'moonshotai'], value: { apiKeyEnv: 'MOONSHOTAI_API_KEY' } }]], ['set', 'MOONSHOTAI_API_KEY']])+ assert.equal(store.MOONSHOTAI_API_KEY, 'sk-kimi')+ assert.deepEqual(row(view, 'moonshotai').models.map(model => model.ref), ['moonshotai/kimi-k3'])+ assert.equal(view.addable.some(entry => entry.id === 'moonshotai'), false)+ await assert.rejects(rt.addProvider({ provider: 'moonshotai', key: 'sk-kimi' }), /moonshotai is already set up/)+ await assert.rejects(rt.addProvider({ provider: 'deepseek-official' }), /already set up/)+ await assert.rejects(rt.addProvider({ provider: 'nowhere' }), /DSH offers no provider nowhere/)+})++test('a blank key keeps a key that is already there, else leaves the route without one', async () => {+ const { rt, writes } = dsh({ keys: { ANTHROPIC_API_KEY: 'k' } })+ await rt.addProvider({ provider: 'anthropic', key: '' })+ await rt.addProvider({ provider: 'moonshotai' })+ assert.deepEqual(writes.map(([, ops]) => ops[0].value), [{ apiKeyEnv: 'ANTHROPIC_API_KEY' }, {}])+})++test('a custom API goes in with its endpoint, protocol and the models picked', async () => {+ const { rt, store, writes } = dsh()+ await rt.addProvider({+ custom: true, provider: 'relay', name: ' My relay ', api: 'openai-completions', baseURL: ' https://relay.example/v1 ', key: 'sk-relay',+ models: [{ id: 'relay-1', name: 'Relay One', contextWindow: 128000, inputModalities: ['text'] }, 'relay-2'],+ })+ assert.deepEqual(writes[0], ['llm-pi-ai', [{ op: 'set', path: ['providers', 'relay'], value: {+ displayName: 'My relay', api: 'openai-completions', baseURL: 'https://relay.example/v1', apiKeyEnv: 'RELAY_API_KEY',+ models: [{ id: 'relay-1', name: 'Relay One', contextWindow: 128000, input: ['text'] }, { id: 'relay-2' }],+ } }]])+ assert.equal(store.RELAY_API_KEY, 'sk-relay')+ const view = await rt.providersView()+ assert.deepEqual([row(view, 'relay').name, row(view, 'relay').custom, row(view, 'relay').removable], ['My relay', true, true])+})++test('a custom API is refused before anything is written when it cannot serve', async () => {+ const { rt, writes } = dsh()+ const custom = { custom: true, provider: 'relay', api: 'openai-completions', baseURL: 'https://relay.example/v1', models: ['m'] }+ await assert.rejects(rt.addProvider({ ...custom, provider: 'My Relay' }), /lowercase letters, digits and dashes/)+ await assert.rejects(rt.addProvider({ ...custom, provider: 'anthropic' }), /DSH already offers anthropic/)+ await assert.rejects(rt.addProvider({ ...custom, api: 'grpc' }), /protocol must be one of/)+ await assert.rejects(rt.addProvider({ ...custom, baseURL: 'relay.example' }), /http or https/)+ await assert.rejects(rt.addProvider({ ...custom, baseURL: 'ftp://relay.example' }), /http or https/)+ await assert.rejects(rt.addProvider({ ...custom, models: [] }), /at least one model/)+ await assert.rejects(rt.addProvider({ ...custom, models: ['m', ' m '] }), /same id/)+ await assert.rejects(rt.addProvider({ ...custom, key: 'KEY=sk' }), /NAME=value/)+ assert.deepEqual(writes, [])+})++test('fetching models asks DSH about the endpoint, with the typed key for that question only', async () => {+ const { rt, discovered, store } = dsh()+ const models = await rt.discoverModels({ baseURL: 'https://relay.example/v1', api: 'openai-completions', key: 'sk-relay' })+ assert.deepEqual(discovered, [['llm-pi-ai', { baseURL: 'https://relay.example/v1', api: 'openai-completions', apiKey: 'sk-relay' }]])+ assert.deepEqual(models, [{ id: 'relay-1', name: 'Relay One', contextWindow: 128000, inputModalities: ['text'] }, { id: 'relay-2' }])+ assert.deepEqual(store, {})+ // A provider DSH ships is answered from its own catalog.+ await rt.discoverModels({ provider: 'anthropic' })+ assert.deepEqual(discovered.at(-1), ['llm-pi-ai', { provider: 'anthropic' }])+})++test('removing a provider takes its key along unless another provider names it', async () => {+ const user = { providers: { moonshotai: { apiKeyEnv: 'MOONSHOTAI_API_KEY' }, kimi: { apiKeyEnv: 'MOONSHOTAI_API_KEY', api: 'openai-completions', baseURL: 'https://k.example/v1', models: [{ id: 'k' }] }, relay: { apiKeyEnv: 'RELAY_API_KEY', api: 'openai-completions', baseURL: 'https://r.example/v1', models: [{ id: 'r' }] } } }+ const { rt, store } = dsh({ user, keys: { MOONSHOTAI_API_KEY: 'a', RELAY_API_KEY: 'b' } })+ await rt.removeProvider('moonshotai')+ assert.equal(store.MOONSHOTAI_API_KEY, 'a')+ const view = await rt.removeProvider('relay')+ assert.equal('RELAY_API_KEY' in store, false)+ assert.deepEqual(view.providers.map(provider => provider.id), ['deepseek-official', 'kimi'])+ await assert.rejects(rt.removeProvider('relay'), /relay is not set up/)+ await assert.rejects(rt.removeProvider('deepseek-official'), /not set up/)+})++test('a route that comes with the setup stays', async () => {+ const { rt } = dsh({ base: { providers: { moonshotai: { apiKeyEnv: 'MOONSHOTAI_API_KEY' } } } })+ assert.equal(row(await rt.providersView(), 'moonshotai').removable, false)+ await assert.rejects(rt.removeProvider('moonshotai'), /comes with this setup/)+})++test('a changed credential drops the cached catalog', async () => {+ const { rt, store, listeners } = dsh()+ assert.deepEqual(await needing(rt), ['deepseek-official'])+ store.DEEPSEEK_API_KEY = 'k'+ assert.deepEqual((await rt.catalog()).filter(entry => entry.needsKey).map(entry => entry.provider), ['deepseek-official'])+ listeners.get('credentials/reference-updated')()+ assert.deepEqual((await rt.catalog()).filter(entry => entry.needsKey).map(entry => entry.provider), [])+})++test('an API key is what an HTTP header carries, without a NAME= or quotes', () => {+ assert.equal(checkKey(' sk-abc '), 'sk-abc')+ assert.equal(checkKey('YWJj=='), 'YWJj==')+ assert.throws(() => checkKey(' '), /Enter the API key/)+ assert.throws(() => checkKey(undefined), /Enter the API key/)+ assert.throws(() => checkKey('DEEPSEEK_API_KEY=sk-abc'), /not a NAME=value line/)+ assert.throws(() => checkKey('"sk-abc"'), /without quotes/)+ assert.throws(() => checkKey('sk abc'), /no spaces/)+ assert.throws(() => checkKey('sk-密钥'), /no spaces/)+})++test('a route\'s key reference follows DSH\'s own Models page', () => {+ assert.equal(keyRefFor('moonshotai'), 'MOONSHOTAI_API_KEY')+ assert.equal(keyRefFor('my-gateway2'), 'MY_GATEWAY2_API_KEY')+})