The first real LlmAdapter implementations, shipped as a deliberate pair:
same models and wire protocol, completely different internals, so the
StreamChunk protocol is verified across independent implementations.
- dsh-llm-deepseek: hand-rolled fetch + SSE parser + chunk-translation
state machine against the official chat-completions format (thinking
mode via top-level thinking/reasoning_effort; the empty-string
reasoning_content first chunk; usage attached to the finish chunk or
trailing; reasoning_content passback on tool-call turns; disjoint
cache-token accounting).
- dsh-llm-pi-ai: the same endpoint through @earendil-works/pi-ai,
mapping its event vocabulary (parsed tool arguments, in-stream error
events, folded reasoning tokens) onto the same chunks.
The agent loop now honors the in-band error path: an adapter that ends
its stream with finish {kind:error|aborted} (the only option for
adapters that can't throw mid-stream, like pi-ai) is translated into a
step error, so the turn ends error/aborted with a logged error event
instead of a normal completed assistant message. This makes the
StreamChunk error contract real for both adapters; docs/architecture.md
and the StreamChunk doc are updated accordingly.
New yarn test:e2e (vitest.e2e.config.ts, *.e2e.ts) runs key-gated
real-API matrices for both adapters across V4 Flash/Pro and all
thinking/effort levels; it self-skips without DEEPSEEK_API_KEY. Unit
suites run against local node:http mock SSE servers at 100% per-file
coverage.
79 lines
2.8 KiB
TypeScript
79 lines
2.8 KiB
TypeScript
/**
|
|
* DeepSeek LLM adapter plugin: registers a {@link DeepSeekAdapter} for the
|
|
* configured model names on `ctx.llm`.
|
|
*
|
|
* Config is cordis-native (schemastery). Secrets flow per the repo policy:
|
|
* `apiKey` from cordis.yml via the `!!js` tag (`!!js process.env.DEEPSEEK_API_KEY`)
|
|
* or from the environment directly; never from ad-hoc files.
|
|
*
|
|
* ```yaml
|
|
* - id: llm-deepseek
|
|
* name: '@deepseek-ai/dsh-llm-deepseek'
|
|
* config:
|
|
* apiKey: !!js process.env.DEEPSEEK_API_KEY
|
|
* baseURL: !!js process.env.DEEPSEEK_BASE_URL
|
|
* models: [deepseek-v4-flash, deepseek-v4-pro]
|
|
* ```
|
|
*
|
|
* @module @deepseek-ai/dsh-llm-deepseek
|
|
*/
|
|
|
|
import type { Context } from 'cordis'
|
|
import z from 'schemastery'
|
|
import type {} from '@deepseek-ai/dsh-llm'
|
|
import { DeepSeekAdapter } from './adapter.ts'
|
|
|
|
export { DeepSeekAdapter, httpErrorCode } from './adapter.ts'
|
|
export type { DeepSeekAdapterOptions } from './adapter.ts'
|
|
export { serializeMessages, serializeRequest } from './serialize.ts'
|
|
export type { RequestDefaults } from './serialize.ts'
|
|
export { DONE, parseSse } from './sse.ts'
|
|
export { mapFinishReason, mapUsage, translate } from './translate.ts'
|
|
export type * from './types.ts'
|
|
|
|
export const name = 'llm-deepseek'
|
|
export const inject = ['llm']
|
|
|
|
export interface Config {
|
|
/** API key; falls back to $DEEPSEEK_API_KEY. Required one way or the other. */
|
|
apiKey?: string
|
|
/** Endpoint base; falls back to $DEEPSEEK_BASE_URL, then the public API. */
|
|
baseURL?: string
|
|
/** Model names to register (sent verbatim on the wire). */
|
|
models?: string[]
|
|
/** Thinking-mode default for every request (provider default: enabled). */
|
|
thinking?: 'enabled' | 'disabled'
|
|
/** Thinking effort (only meaningful with thinking enabled). */
|
|
reasoningEffort?: 'high' | 'max'
|
|
}
|
|
|
|
export const Config: z<Config> = z.object({
|
|
apiKey: z.string(),
|
|
baseURL: z.string(),
|
|
models: z.array(z.string()).default(['deepseek-v4-flash', 'deepseek-v4-pro']),
|
|
thinking: z.union(['enabled', 'disabled']),
|
|
reasoningEffort: z.union(['high', 'max']),
|
|
})
|
|
|
|
/** Public API default; the internal endpoint comes from $DEEPSEEK_BASE_URL. */
|
|
export const PUBLIC_BASE_URL = 'https://api.deepseek.com'
|
|
|
|
export function apply(ctx: Context, config: Config): void {
|
|
const apiKey = config.apiKey ?? process.env.DEEPSEEK_API_KEY
|
|
if (apiKey === undefined || apiKey.length === 0) {
|
|
throw new Error('llm-deepseek: an API key is required (Config.apiKey or $DEEPSEEK_API_KEY)')
|
|
}
|
|
const baseURL = config.baseURL ?? process.env.DEEPSEEK_BASE_URL ?? PUBLIC_BASE_URL
|
|
// schemastery's .default() guarantees models is set after validation.
|
|
const models = config.models as string[]
|
|
|
|
ctx.llm.registerAdapter(models, new DeepSeekAdapter({
|
|
apiKey,
|
|
baseURL,
|
|
defaults: {
|
|
thinking: config.thinking,
|
|
reasoningEffort: config.reasoningEffort,
|
|
},
|
|
}))
|
|
}
|