Files
deepseek-harness/website/zh-CN/develop/basic/config.md
T
lintianle 6ce9f16030 website: wire the site into the repo gates; make every tutorial example compile
- website joins the pnpm workspace; root scripts website:dev/website:build;
  run-gates gains a website-build gate (ci-primary + ci-static) — the
  VitePress build doubles as the site's dead-link check; AGENTS.md documents
  the commands.
- doc-typecheck + verify-type-equiv now scan website/zh-CN/**/*.md; every
  ```typescript fence converted to ```ts and made standalone-compilable
  (55 compiled, 1 ignore-check). Phantom APIs the compiler caught are fixed:
  invented event names (agent/turn-end, tool/call, llm/pre-request, ready,
  dispose) replaced with real catalog events or per-plugin declare-module
  merges; presentCall/inject/Config claims corrected to the real shapes.
- guide/config.md entry-fields table completed against loader EntryOptions;
  its coding-agent example brought in line with examples/coding-agent.
2026-07-16 18:12:22 +08:00

2.9 KiB
Raw Blame History

插件配置

让你的插件接受用户在 cordis.yml 中传入的配置。

定义 Config 类型

在插件中导出一个 Config 类型,apply 的第二个参数就是用户配置:

import type { Context } from 'cordis'

export const name = 'my-plugin'

export interface Config {
  greeting?: string
  maxRetries?: number
  verbose?: boolean
}

export function apply(ctx: Context, config: Config) {
  console.log(config.greeting ?? 'Hello')  // 用户配置或默认值
}

用户在 cordis.yml 中这样使用:

- name: './src/my-plugin.ts'
  config:
    greeting: 'Hi there'
    maxRetries: 5

只导出类型时,配置原样传入,默认值由代码自己兜底(如上面的 ??)。想让框架代管默认值和校验,导出一个 schema(见下节)。

Schema 校验

对于需要默认值和严格校验的场景,额外导出一个 Schemastery schema(仓库约定以 z 引入)。加载时框架先用它校验并填充默认值,再把结果传给 apply

import type { Context } from 'cordis'
import z from 'schemastery'

export const name = 'validated-plugin'

export interface Config {
  apiKey: string
  timeout?: number
  mode?: 'fast' | 'accurate'
}

export const Config: z<Config> = z.object({
  apiKey: z.string().required(),
  timeout: z.number().default(30000),
  mode: z.union(['fast', 'accurate'] as const).default('fast'),
})

export function apply(ctx: Context, config: Config) {
  // config 已经过校验,类型安全,默认值已填充
}

Schema 在插件加载时执行校验。如果配置不合法,插件会加载失败并给出明确错误信息。

设计原则

无硬编码可调参数

Harness 的约定:任何两个部署可能想要不同值的东西,都应该是配置字段

// 错误 — 硬编码超时时间
const TIMEOUT = 30000

// 正确 — 可配置
export interface Config {
  /** 默认 30000 */
  timeoutMs?: number
}

检验标准:能否在 cordis.yml 中改变这个值,而不需要修改代码?

配置错误要响亮

如果配置引用了不存在的东西(比如一个不存在的模型名),应该尽早报错,而不是静默跳过:

import type { Context } from 'cordis'
import type {} from '@deepseek-ai/dsh-llm'

export interface Config {
  model: string
}

export function apply(ctx: Context, config: Config) {
  if (!ctx.llm.models().includes(config.model)) {
    throw new Error(`Model "${config.model}" is not registered by any LLM adapter`)
  }
}

配合 HMR

配置变更会触发插件热替换:修改 cordis.yml 中某个插件的 config,框架会卸载旧实例、加载新实例。由于注册都是效果(自动清理),这个过程是安全的。

下一步