A team of long-lived bots for DeepSeek Harness, with main bots, bot-to-bot messages and group chats.
Mark the models whose provider has no API key, and take one
Fei2411e5d747d56d82
diff --git a/ds-bot/src/host/channel.js b/ds-bot/src/host/channel.jsindex 6168b5ee..167b3b57 100644--- a/ds-bot/src/host/channel.js+++ b/ds-bot/src/host/channel.js@@ -28,7 +28,7 @@ export function install(rt) {const fail = (message, code = 'bot/error') => ({ ok: false, error: { code, message, details: {} } })// Models come from DSH, not state.json, so a provider added in Harness settings// changes the view without a new revision.- const modelsKey = () => `${rt.cachedCatalog().map(entry => entry.reasoning ? `${entry.ref}:${JSON.stringify(entry.reasoning)}` : entry.ref).join(',')}|${rt.cachedDefault() ?? ''}`+ const modelsKey = () => `${rt.cachedCatalog().map(entry => `${entry.ref}${entry.needsKey ? '!' : ''}${entry.reasoning ? `:${JSON.stringify(entry.reasoning)}` : ''}`).join(',')}|${rt.cachedDefault() ?? ''}`const view = () => ({revision: state.revision,workspaceId: state.workspaceId ?? null,@@ -121,6 +121,9 @@ 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() })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 598f38cb..9513b78f 100644--- a/ds-bot/src/host/models.js+++ b/ds-bot/src/host/models.js@@ -15,6 +15,48 @@ export function install(rt) {// only with the provider, which also drops the catalog cache.const reasoningCache = new Map()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.@@ -51,6 +93,8 @@ export function install(rt) {})}}+ const missing = await missingKeys([...new Set(entries.map(entry => entry.provider))])+ 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) => {const reasoning = await reasoningOf(entry.ref)@@ -197,6 +241,17 @@ 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,+ selectSessionModel, applyModel, waitForModel, syncModels, setModelKey,})}++// 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/test/install.test.js b/ds-bot/test/install.test.jsindex 430f154f..ca96813f 100644--- a/ds-bot/test/install.test.js+++ b/ds-bot/test/install.test.js@@ -37,6 +37,8 @@ test('apply registers listeners, tools, and effects in a fixed order', (t) => {'effect ds-bot: diagnostics log','effect ds-bot: state lock','on llm/adapters-updated',+ 'on credentials/reference-updated',+ 'on settings/document-updated','on session/event','effect ds-bot: agent tool restrict retry','on agent/created',diff --git a/ds-bot/test/models.test.js b/ds-bot/test/models.test.jsindex 248b3b6f..927468c6 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 { install } from '../src/host/models.js'+import { checkKey, install } from '../src/host/models.js'const PROVIDERS = {deepseek: [{ id: 'deepseek-v4.1-flash', name: 'DeepSeek V4.1 Flash' }, { id: 'deepseek-v4-pro' }],@@ -186,3 +186,89 @@ 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/)+})