Skip to content

A team of long-lived bots for DeepSeek Harness, with main bots, bot-to-bot messages and group chats.

Send system instructions and fill catalog defaults on custom provider routes

Fei2411dedebed7dbb1
diff --git a/ds-bot/src/host/providers.js b/ds-bot/src/host/providers.js
index 49814dd1..7e894eb8 100644
--- a/ds-bot/src/host/providers.js
+++ b/ds-bot/src/host/providers.js
@@ -8,6 +8,32 @@ 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
+// pi-ai guesses compatibility from the endpoint hostname and treats any unrecognized
+// gateway as OpenAI's own server (sending the `developer` role, `store: false`, and
+// `max_completion_tokens`). Every OpenAI-compatible server accepts `system` and
+// `max_tokens`, and reasoning models such as Kimi and DeepSeek require
+// `reasoning_content` on replayed assistant messages.
+const CUSTOM_COMPAT = {
+ 'openai-completions': {
+ supportsDeveloperRole: false,
+ supportsStore: false,
+ maxTokensField: 'max_tokens',
+ requiresReasoningContentOnAssistantMessages: true,
+ },
+ 'openai-responses': {
+ supportsDeveloperRole: false,
+ },
+}
+const RESPONSES_PROVIDERS = new Set(['openai', 'xai', 'meta'])
+const COMPLETIONS_PROVIDERS = new Set([
+ 'ant-ling', 'baseten', 'cerebras', 'cloudflare-ai-gateway', 'cloudflare-workers-ai',
+ 'deepseek', 'fireworks', 'github-copilot', 'groq', 'huggingface', 'kimi-coding',
+ 'moonshotai', 'moonshotai-cn', 'nvidia', 'opencode', 'opencode-go', 'openrouter',
+ 'qwen-token-plan', 'qwen-token-plan-cn', 'qwen-token-plan-individual', 'together',
+ 'xiaomi', 'xiaomi-token-plan-ams', 'xiaomi-token-plan-cn', 'xiaomi-token-plan-sgp',
+ 'zai', 'zai-coding-cn',
+])
+const catalogProtocol = id => (RESPONSES_PROVIDERS.has(id) ? 'openai-responses' : COMPLETIONS_PROVIDERS.has(id) ? 'openai-completions' : undefined)
// 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.
@@ -34,20 +60,50 @@ function checkEndpoint(value) {
return text
}
-// A model as the route stores it: what discovery told about it, under pi-ai's names.
-function modelEntry(model) {
+function lookupKnownModel(known, rawId) {
+ if (!known || known.size === 0 || typeof rawId !== 'string') return undefined
+ const id = rawId.trim().toLowerCase()
+ if (id === '') return undefined
+ const bare = id.includes('/') ? id.slice(id.lastIndexOf('/') + 1) : id
+ return known.get(id) ?? known.get(bare) ?? (bare.startsWith('kimi-') ? known.get(bare.slice(5)) : known.get(`kimi-${bare}`))
+}
+
+// A model as the route stores it: what discovery told about it, under pi-ai's names,
+// with any missing capacity or modalities filled from a known catalog model of the same id.
+function modelEntry(model, known) {
const id = typeof model === 'string' ? model.trim() : typeof model?.id === 'string' ? model.id.trim() : ''
if (id === '') throw invalid('Every model needs an id')
+ const match = lookupKnownModel(known, id)
const whole = value => (Number.isInteger(value) && value > 0 ? value : undefined)
+ const contextWindow = whole(model?.contextWindow) ?? whole(match?.contextWindow)
+ const maxTokens = whole(model?.maxTokens)
+ const rawInput = Array.isArray(model?.inputModalities) && model.inputModalities.length > 0
+ ? model.inputModalities
+ : Array.isArray(model?.input) && model.input.length > 0
+ ? model.input
+ : match?.inputModalities
+ const input = Array.isArray(rawInput) ? rawInput.filter(kind => typeof kind === 'string') : []
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') } : {}),
+ ...(contextWindow ? { contextWindow } : {}),
+ ...(maxTokens ? { maxTokens } : {}),
+ ...(input.length > 0 ? { input } : {}),
}
}
+function compatForRoute(id, profile) {
+ if (typeof profile !== 'object' || profile === null) return undefined
+ const api = typeof profile.api === 'string'
+ ? profile.api
+ : (typeof profile.baseURL === 'string' || Array.isArray(profile.models)) ? catalogProtocol(id) : undefined
+ const defaults = CUSTOM_COMPAT[api]
+ if (defaults === undefined) return undefined
+ const current = typeof profile.compat === 'object' && profile.compat !== null ? profile.compat : {}
+ if (Object.keys(defaults).every(key => current[key] !== undefined)) return undefined
+ return { ...defaults, ...current }
+}
+
export function install(rt) {
const { ctx } = rt
@@ -66,6 +122,57 @@ export function install(rt) {
const refOf = profile => (typeof profile?.apiKeyEnv === 'string' && profile.apiKeyEnv !== '' ? profile.apiKeyEnv : undefined)
const entryOf = provider => ctx.llm.listConfigurableProviders().find(entry => entry.provider === provider)
+ let knownCache
+ async function knownCatalogModels() {
+ if (knownCache !== undefined) return knownCache
+ const entries = (ctx.llm.listConfigurableProviders?.() ?? [])
+ .filter(entry => entry.settingsNs === PI_AI && entry.declared === false && (entry.nativeAuth !== undefined || entry.defaultBaseURL !== undefined))
+ if (entries.length === 0) return new Map()
+ const map = new Map()
+ const remember = (key, model) => {
+ if (key && !map.has(key)) map.set(key, model)
+ }
+ for (const entry of entries) {
+ let list = []
+ try { list = await ctx.llm.discoverModels(PI_AI, { provider: entry.provider }) } catch { continue }
+ for (const model of list) {
+ const id = typeof model?.id === 'string' ? model.id.trim().toLowerCase() : ''
+ if (!id) continue
+ remember(id, model)
+ const bare = id.includes('/') ? id.slice(id.lastIndexOf('/') + 1) : id
+ remember(bare, model)
+ if (bare.startsWith('kimi-')) remember(bare.slice(5), model)
+ }
+ }
+ knownCache = map
+ return map
+ }
+
+ // Backfill compatibility defaults onto custom routes (and catalog routes pointed at a
+ // custom gateway) that were saved before DS Bot wrote `compat`, or added through DSH's
+ // own Models page. Explicit user settings win per field.
+ let repairing = Promise.resolve()
+ async function ensureCustomCompat() {
+ const found = seams()
+ if (found === undefined) return
+ const section = sections(found.settings).get(PI_AI)
+ const userProviders = section?.user?.providers
+ if (!userProviders || typeof userProviders !== 'object') return
+ const ops = []
+ for (const [id, profile] of Object.entries(userProviders)) {
+ const nextCompat = compatForRoute(id, profile)
+ if (nextCompat !== undefined) ops.push({ op: 'set', path: ['providers', id, 'compat'], value: nextCompat })
+ }
+ if (ops.length === 0) return
+ await found.settings.mutate(PI_AI, ops, section.revision)
+ rt.dropCatalog()
+ }
+ function repairRoutes() {
+ repairing = repairing.then(ensureCustomCompat, ensureCustomCompat).catch(() => {})
+ return repairing
+ }
+ repairRoutes()
+
// Per provider route, the credential its DSH settings name as its API key. A
// provider with none authenticates some other way.
function keyRefs() {
@@ -81,6 +188,7 @@ export function install(rt) {
}
async function missingKeys(providers) {
+ await repairRoutes()
let refs
try { refs = keyRefs() } catch { return new Set() }
const missing = new Set()
@@ -95,6 +203,7 @@ export function install(rt) {
// can add. A provider with no key and no models (DeepSeek's account route before a
// sign-in) is left out.
async function providersView() {
+ await repairRoutes()
const found = seams()
if (found === undefined) return { available: false }
const map = sections(found.settings)
@@ -149,12 +258,14 @@ export function install(rt) {
// 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) {
+ await repairing
const { settings, credentials } = usable()
const provider = String(body.provider ?? '').trim()
+ const custom = body.custom === true
+ const known = custom ? await knownCatalogModels() : undefined
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`)
@@ -164,7 +275,7 @@ export function install(rt) {
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) : {}
+ const profile = custom ? customProfile(body, known) : {}
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)
@@ -172,13 +283,20 @@ export function install(rt) {
return providersView()
}
- function customProfile(body) {
+ function customProfile(body, known) {
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) : []
+ const models = Array.isArray(body.models) ? body.models.slice(0, MAX_MODELS).map(model => modelEntry(model, known)) : []
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 }
+ const compat = CUSTOM_COMPAT[body.api]
+ return {
+ ...(name ? { displayName: name } : {}),
+ api: body.api,
+ baseURL: checkEndpoint(body.baseURL),
+ ...(compat ? { compat: { ...compat } } : {}),
+ models,
+ }
}
// The models an endpoint (or a provider DSH ships) says it serves. The key typed for
@@ -194,18 +312,25 @@ export function install(rt) {
...(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] } : {}),
- }))
+ const known = request.provider === undefined ? await knownCatalogModels() : undefined
+ return models.slice(0, MAX_MODELS).map((model) => {
+ const match = lookupKnownModel(known, model.id)
+ const contextWindow = model.contextWindow ?? match?.contextWindow
+ const inputModalities = model.inputModalities ?? match?.inputModalities
+ return {
+ id: model.id,
+ ...(model.name ? { name: model.name } : {}),
+ ...(contextWindow ? { contextWindow } : {}),
+ ...(model.maxTokens ? { maxTokens: model.maxTokens } : {}),
+ ...(inputModalities ? { inputModalities: [...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) {
+ await repairing
const { settings, credentials } = usable()
const map = sections(settings)
const entry = entryOf(provider)
diff --git a/ds-bot/test/providers.test.js b/ds-bot/test/providers.test.js
index 53e76241..a0c0e37f 100644
--- a/ds-bot/test/providers.test.js
+++ b/ds-bot/test/providers.test.js
@@ -1,5 +1,6 @@
import assert from 'node:assert/strict'
import { test } from 'node:test'
+import { Config as PiAiConfig } from '@deepseek-ai/dsh-llm-pi-ai'
import { install as installModels } from '../src/host/models.js'
import { checkKey, install, keyRefFor } from '../src/host/providers.js'
@@ -140,7 +141,7 @@ test('a blank key keeps a key that is already there, else leaves the route witho
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 () => {
+test('a custom API goes in with its endpoint, protocol, compatibility switches 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',
@@ -148,11 +149,67 @@ test('a custom API goes in with its endpoint, protocol and the models picked', a
})
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',
+ compat: { supportsDeveloperRole: false, supportsStore: false, maxTokensField: 'max_tokens', requiresReasoningContentOnAssistantMessages: true },
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])
+ // OpenAI Responses takes supportsDeveloperRole alone; Anthropic Messages takes no OpenAI compat switch.
+ await rt.addProvider({ custom: true, provider: 'resp-gw', api: 'openai-responses', baseURL: 'https://resp.example/v1', models: ['m'] })
+ assert.deepEqual(writes.at(-1)[1][0].value.compat, { supportsDeveloperRole: false })
+ await rt.addProvider({ custom: true, provider: 'anth-gw', api: 'anthropic-messages', baseURL: 'https://anth.example', models: ['m'] })
+ assert.equal(writes.at(-1)[1][0].value.compat, undefined)
+})
+
+test('existing custom routes and custom-endpoint catalog routes backfill missing compatibility switches without overwriting explicit ones', async () => {
+ const user = {
+ providers: {
+ moonshotai: { apiKeyEnv: 'MOONSHOTAI_API_KEY' },
+ openai: { apiKeyEnv: 'OPENAI_API_KEY', baseURL: 'https://gateway.example/v1', models: [{ id: 'k3.1-pro' }] },
+ relay: { api: 'openai-completions', baseURL: 'https://gateway.example/v1', compat: { supportsStore: true }, models: [{ id: 'k3.1-pro' }] },
+ },
+ }
+ const { rt, writes } = dsh({ user, catalog: { moonshotai: ['kimi-k3'], openai: ['gpt-5'] } })
+ await rt.providersView()
+ assert.deepEqual(writes, [['llm-pi-ai', [
+ { op: 'set', path: ['providers', 'openai', 'compat'], value: { supportsDeveloperRole: false } },
+ { op: 'set', path: ['providers', 'relay', 'compat'], value: { supportsDeveloperRole: false, supportsStore: true, maxTokensField: 'max_tokens', requiresReasoningContentOnAssistantMessages: true } },
+ ]]])
+ // A second read sees every switch in place and writes nothing again.
+ writes.length = 0
+ await rt.providersView()
+ assert.deepEqual(writes, [])
+})
+
+test('a custom gateway model whose id matches a built-in catalog model inherits its context window and image support when the endpoint omits them', async () => {
+ const { rt, writes } = dsh({ catalog: { 'kimi-coding': ['k3.1-pro'] } })
+ const origList = rt.ctx.llm.listConfigurableProviders
+ rt.ctx.llm.listConfigurableProviders = () => origList().map(entry => (entry.provider === 'kimi-coding' ? { ...entry, defaultBaseURL: 'https://api.kimi.com/coding/v1' } : entry))
+ rt.ctx.llm.discoverModels = async (_ns, request) => {
+ if (request.provider === 'kimi-coding') {
+ return [{ id: 'k3.1-pro', name: 'Kimi K3.1 Pro', contextWindow: 1048576, maxTokens: 131072, inputModalities: ['text', 'image'] }]
+ }
+ return [{ id: 'k3.1-pro', name: 'k3.1-pro' }, { id: 'custom-only', name: 'custom-only' }]
+ }
+ const discovered = await rt.discoverModels({ baseURL: 'https://gateway.example/v1', api: 'openai-completions', key: 'sk-gw' })
+ assert.deepEqual(discovered, [
+ { id: 'k3.1-pro', name: 'k3.1-pro', contextWindow: 1048576, inputModalities: ['text', 'image'] },
+ { id: 'custom-only', name: 'custom-only' },
+ ])
+ await rt.addProvider({ custom: true, provider: 'my-gateway', api: 'openai-completions', baseURL: 'https://gateway.example/v1', models: ['moonshotai/k3.1-pro'] })
+ assert.deepEqual(writes[0][1][0].value.models, [
+ { id: 'moonshotai/k3.1-pro', contextWindow: 1048576, input: ['text', 'image'] },
+ ])
+ // Verify the written profile against the real dsh-llm-pi-ai schema.
+ const validated = PiAiConfig({ providers: { 'my-gateway': writes[0][1][0].value } }).providers.get()['my-gateway']
+ assert.equal(validated.models[0].id, 'moonshotai/k3.1-pro')
+ assert.equal(validated.models[0].contextWindow, 1048576)
+ assert.deepEqual(validated.models[0].input, ['text', 'image'])
+ assert.equal(validated.compat.supportsDeveloperRole, false)
+ assert.equal(validated.compat.supportsStore, false)
+ assert.equal(validated.compat.maxTokensField, 'max_tokens')
+ assert.equal(validated.compat.requiresReasoningContentOnAssistantMessages, true)
})
test('a custom API is refused before anything is written when it cannot serve', async () => {