2026-07-09 16:07:58 +08:00
|
|
|
|
# 插件配置
|
|
|
|
|
|
|
2026-07-15 18:08:28 +08:00
|
|
|
|
[English](config.md) | 中文
|
|
|
|
|
|
|
2026-07-09 16:07:58 +08:00
|
|
|
|
让你的插件接受用户在 `cordis.yml` 中传入的配置。
|
|
|
|
|
|
|
|
|
|
|
|
## 定义 Config 类型
|
|
|
|
|
|
|
2026-07-13 17:47:42 +08:00
|
|
|
|
在插件中导出一个 `Config` 类型和同名的 Schemastery schema;默认值直接写在 schema 中:
|
2026-07-09 16:07:58 +08:00
|
|
|
|
|
2026-07-15 18:08:28 +08:00
|
|
|
|
```ts
|
build(vendor): rescope the vendored Cordis packages into @deepseek-ai
Machine-produced by `pnpm run rescope-vendor --apply` plus the regeneration it
prints: `pnpm install` for the lockfile, `pnpm run gen-third-party-notices`,
`verify-translation-pairing --write` for the touched bilingual pairs,
`gen-doc-graphs`, and one typert snapshot whose ids embed character offsets.
`pnpm run rescope-vendor --check` verifies the result.
Renames nine vendored packages (cordis, cosmokit, schemastery and the six
@cordisjs plugins) and every reference that resolves them: manifest names and
dependency keys, module specifiers including declare-module merges, cordis.yml
plugin names, tsconfig paths, every Markdown fence, and `docs/` prose.
Directory names, upstream versions, and dependency ranges are unchanged, so
vendor/README.md still reads as an upstream snapshot; its manifest table gains
an upstream-name column so THIRD_PARTY_NOTICES keeps MIT attribution pointed
at each fork's origin.
The tutorial tier follows the rename end to end: its yaml fences named plugins
the Loader can no longer resolve, its `ts ignore-check` fences disagreed with
the compiled fences beside them, and its prose quoted both. The contracts that
told readers to keep upstream names — the root convention and the vendoring
cookbook's tree comment and manifest invariant — now say to rescope instead.
Two rules read `@deepseek-ai/` as "another workspace plugin": the client bundle
purity gate now names the vendored libraries a browser bundle inlines, and the
files where a bare `cordis` is an agent-preset id keep that product data.
2026-08-10 22:04:06 +08:00
|
|
|
|
import type { Context } from '@deepseek-ai/cordis'
|
|
|
|
|
|
import Schema from '@deepseek-ai/schemastery'
|
2026-07-09 16:07:58 +08:00
|
|
|
|
|
|
|
|
|
|
export const name = 'my-plugin'
|
|
|
|
|
|
|
|
|
|
|
|
export interface Config {
|
|
|
|
|
|
greeting: string
|
|
|
|
|
|
maxRetries: number
|
|
|
|
|
|
verbose?: boolean
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-07-13 17:47:42 +08:00
|
|
|
|
export const Config: Schema<Config> = Schema.object({
|
|
|
|
|
|
greeting: Schema.string().default('Hello'),
|
|
|
|
|
|
maxRetries: Schema.number().default(3),
|
|
|
|
|
|
verbose: Schema.boolean().default(false),
|
|
|
|
|
|
})
|
2026-07-09 16:07:58 +08:00
|
|
|
|
|
|
|
|
|
|
export function apply(ctx: Context, config: Config) {
|
2026-07-15 18:08:28 +08:00
|
|
|
|
console.log(config.greeting) // User value or schema default.
|
2026-07-09 16:07:58 +08:00
|
|
|
|
}
|
|
|
|
|
|
```
|
|
|
|
|
|
|
2026-08-05 12:46:38 +08:00
|
|
|
|
在 `scratch-plugin/cordis.yml` 新插入的本地插件行中添加配置:
|
2026-07-09 16:07:58 +08:00
|
|
|
|
|
|
|
|
|
|
```yaml
|
2026-08-05 12:46:38 +08:00
|
|
|
|
- insert:
|
|
|
|
|
|
- id: hello
|
|
|
|
|
|
name: './src/my-plugin.ts'
|
|
|
|
|
|
config:
|
|
|
|
|
|
greeting: 'Hi there'
|
|
|
|
|
|
maxRetries: 5
|
2026-07-09 16:07:58 +08:00
|
|
|
|
```
|
|
|
|
|
|
|
2026-07-13 17:47:42 +08:00
|
|
|
|
插件加载时,Cordis 会通过导出的 schema 校验配置,并填充未提供字段的默认值。不要导出普通对象作为 `Config`,因为它不满足 Cordis 要求的 Standard Schema 接口。
|
2026-07-09 16:07:58 +08:00
|
|
|
|
|
|
|
|
|
|
## Schema 校验
|
|
|
|
|
|
|
|
|
|
|
|
对于需要严格校验的场景,使用 Schemastery 定义 schema:
|
|
|
|
|
|
|
2026-07-15 18:08:28 +08:00
|
|
|
|
```ts
|
build(vendor): rescope the vendored Cordis packages into @deepseek-ai
Machine-produced by `pnpm run rescope-vendor --apply` plus the regeneration it
prints: `pnpm install` for the lockfile, `pnpm run gen-third-party-notices`,
`verify-translation-pairing --write` for the touched bilingual pairs,
`gen-doc-graphs`, and one typert snapshot whose ids embed character offsets.
`pnpm run rescope-vendor --check` verifies the result.
Renames nine vendored packages (cordis, cosmokit, schemastery and the six
@cordisjs plugins) and every reference that resolves them: manifest names and
dependency keys, module specifiers including declare-module merges, cordis.yml
plugin names, tsconfig paths, every Markdown fence, and `docs/` prose.
Directory names, upstream versions, and dependency ranges are unchanged, so
vendor/README.md still reads as an upstream snapshot; its manifest table gains
an upstream-name column so THIRD_PARTY_NOTICES keeps MIT attribution pointed
at each fork's origin.
The tutorial tier follows the rename end to end: its yaml fences named plugins
the Loader can no longer resolve, its `ts ignore-check` fences disagreed with
the compiled fences beside them, and its prose quoted both. The contracts that
told readers to keep upstream names — the root convention and the vendoring
cookbook's tree comment and manifest invariant — now say to rescope instead.
Two rules read `@deepseek-ai/` as "another workspace plugin": the client bundle
purity gate now names the vendored libraries a browser bundle inlines, and the
files where a bare `cordis` is an agent-preset id keep that product data.
2026-08-10 22:04:06 +08:00
|
|
|
|
import type { Context } from '@deepseek-ai/cordis'
|
|
|
|
|
|
import Schema from '@deepseek-ai/schemastery'
|
2026-07-09 16:07:58 +08:00
|
|
|
|
|
|
|
|
|
|
export const name = 'validated-plugin'
|
|
|
|
|
|
|
|
|
|
|
|
export interface Config {
|
|
|
|
|
|
apiKey: string
|
|
|
|
|
|
timeout: number
|
|
|
|
|
|
mode: 'fast' | 'accurate'
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
export const Config = Schema.object({
|
|
|
|
|
|
apiKey: Schema.string().required(),
|
|
|
|
|
|
timeout: Schema.number().default(30000),
|
|
|
|
|
|
mode: Schema.union(['fast', 'accurate']).default('fast'),
|
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
|
|
export function apply(ctx: Context, config: Config) {
|
2026-07-15 18:08:28 +08:00
|
|
|
|
// config is validated and type-safe.
|
2026-07-09 16:07:58 +08:00
|
|
|
|
}
|
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
|
|
Schema 在插件加载时执行校验。如果配置不合法,插件会加载失败并给出明确错误信息。
|
|
|
|
|
|
|
|
|
|
|
|
## 设计原则
|
|
|
|
|
|
|
|
|
|
|
|
### 无硬编码可调参数
|
|
|
|
|
|
|
2026-08-04 17:36:14 +08:00
|
|
|
|
Harness 的约定:**凡是不同部署可能需要采用不同值的参数,都必须定义为配置字段**。
|
2026-07-09 16:07:58 +08:00
|
|
|
|
|
2026-07-15 18:08:28 +08:00
|
|
|
|
```ts
|
|
|
|
|
|
// Wrong: hardcoded timeout.
|
2026-07-09 16:07:58 +08:00
|
|
|
|
const TIMEOUT = 30000
|
|
|
|
|
|
|
2026-07-15 18:08:28 +08:00
|
|
|
|
// Correct: configurable.
|
2026-07-09 16:07:58 +08:00
|
|
|
|
export interface Config {
|
2026-07-15 18:08:28 +08:00
|
|
|
|
timeoutMs: number // Defaults to 30000.
|
2026-07-09 16:07:58 +08:00
|
|
|
|
}
|
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
|
|
检验标准:能否在 `cordis.yml` 中改变这个值,而不需要修改代码?
|
|
|
|
|
|
|
|
|
|
|
|
### 配置错误要响亮
|
|
|
|
|
|
|
2026-08-18 19:00:37 +08:00
|
|
|
|
在 schema 中表达自身完备的约束,使无效配置在插件加载时失败。对服务或已注册资源的引用需要依赖注入;[服务教程](../framework/service.zh.md) 会介绍这项约定。
|
2026-07-09 16:07:58 +08:00
|
|
|
|
|
|
|
|
|
|
## 配合 HMR
|
|
|
|
|
|
|
2026-08-04 17:36:14 +08:00
|
|
|
|
配置变更会触发插件热替换:修改 `cordis.yml` 中某个插件的 `config` 后,框架会卸载旧实例并加载新实例。由于注册都属于 effect 并会自动清理,替换后不会保留旧实例的注册。
|
2026-07-09 16:07:58 +08:00
|
|
|
|
|
|
|
|
|
|
## 下一步
|
|
|
|
|
|
|
2026-08-18 19:00:37 +08:00
|
|
|
|
- [打包与安装插件](./publish.zh.md) — 把插件以可安装包的形式交付
|
|
|
|
|
|
- [插件与生命周期](../framework/index.zh.md) — 深入了解插件的完整生命周期
|
|
|
|
|
|
- [服务与依赖](../framework/service.zh.md) — 让你的插件对外提供服务
|