2026-07-15 23:11:25 -07:00
|
|
|
|
# 系统提示词组装
|
|
|
|
|
|
|
|
|
|
|
|
[English](system-prompt.md) | 中文
|
|
|
|
|
|
|
2026-08-04 17:36:14 +08:00
|
|
|
|
[system-prompt 包](../../packages/core/system-prompt)负责管理提示词贡献者与一次组装调用之间交换的数据。该包的 [README](../../packages/core/system-prompt/README.md) 记录了注册、排序、作用域与渲染行为;本页固定各插件实现或传递的跨包字面形状。
|
2026-07-15 23:11:25 -07:00
|
|
|
|
|
|
|
|
|
|
源码:[`packages/core/system-prompt/src/index.ts`](../../packages/core/system-prompt/src/index.ts)。
|
|
|
|
|
|
|
|
|
|
|
|
## 组装上下文
|
|
|
|
|
|
|
2026-08-04 17:36:14 +08:00
|
|
|
|
`AssembleContext` 标识一次组装所解析的作用域层,并可携带该请求的显式控制信号。它可合并扩展:`dsh-agent` 添加可选字段 `agent`,用于携带当前的 agent(智能体)实例;`assembleContextFor(agent, signal)` 则一起设置这些显式字段。裸组装既没有作用域,也没有信号。
|
2026-07-15 23:11:25 -07:00
|
|
|
|
|
|
|
|
|
|
```ts type-equiv
|
2026-07-22 22:58:05 +08:00
|
|
|
|
/** Merge-extensible context for one prompt assembly. */
|
2026-07-15 23:11:25 -07:00
|
|
|
|
interface AssembleContext {
|
2026-07-22 22:58:05 +08:00
|
|
|
|
/**
|
|
|
|
|
|
* Scope whose providers and waterfall listeners participate. When absent,
|
|
|
|
|
|
* only global providers and subject-less listeners participate.
|
|
|
|
|
|
*/
|
2026-07-15 23:11:25 -07:00
|
|
|
|
scope?: ScopeKey
|
2026-07-22 22:58:05 +08:00
|
|
|
|
/** Explicit control signal for the turn that requested this assembly, when any. */
|
|
|
|
|
|
signal?: AbortSignal
|
2026-07-15 23:11:25 -07:00
|
|
|
|
}
|
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
|
|
## 工具提供方结果
|
|
|
|
|
|
|
2026-08-04 17:36:14 +08:00
|
|
|
|
`ToolProviderResult.schemas` 是当前组装中对模型可见的工具 schema 集合。`knownNames` 是提供方在限制前的名称全集,用于区分「配置名拼写错误」与「已知工具在此作用域中被有意隐藏」。
|
2026-07-15 23:11:25 -07:00
|
|
|
|
|
|
|
|
|
|
```ts type-equiv
|
2026-07-22 22:58:05 +08:00
|
|
|
|
/** Tool schemas visible in one assembly and their pre-restriction name set. */
|
2026-07-15 23:11:25 -07:00
|
|
|
|
interface ToolProviderResult {
|
2026-07-22 22:58:05 +08:00
|
|
|
|
/** The schemas this provider contributes to THIS assembly. */
|
2026-07-15 23:11:25 -07:00
|
|
|
|
readonly schemas: readonly ToolSchema[]
|
2026-07-22 22:58:05 +08:00
|
|
|
|
/** The pre-restriction name universe for config validation (defaults to `schemas`' names). */
|
2026-07-15 23:11:25 -07:00
|
|
|
|
readonly knownNames?: readonly string[]
|
|
|
|
|
|
}
|
|
|
|
|
|
```
|
|
|
|
|
|
|
2026-07-23 01:05:50 +08:00
|
|
|
|
## 提示词段落
|
2026-07-15 23:11:25 -07:00
|
|
|
|
|
2026-07-22 03:06:01 -07:00
|
|
|
|
`PromptSection` 是一份只读的同进程注册契约。其文本可以是静态的,也可以从当前组装上下文动态解析。
|
2026-07-15 23:11:25 -07:00
|
|
|
|
|
|
|
|
|
|
```ts type-equiv
|
2026-07-22 22:58:05 +08:00
|
|
|
|
/** One contributed section of the system prompt (registry input). */
|
2026-07-15 23:11:25 -07:00
|
|
|
|
interface PromptSection {
|
2026-07-22 22:58:05 +08:00
|
|
|
|
/** Unique name — a duplicate registration throws (see {@link SystemPrompt.section}). */
|
2026-07-15 23:11:25 -07:00
|
|
|
|
readonly name: string
|
2026-07-22 22:58:05 +08:00
|
|
|
|
/**
|
|
|
|
|
|
* Sections are concatenated in ascending order. Convention: `-100` is the
|
|
|
|
|
|
* harness identity, `0` the deployment persona, tool guidance uses 100–199;
|
|
|
|
|
|
* other negative orders also render before the persona.
|
|
|
|
|
|
*/
|
2026-07-15 23:11:25 -07:00
|
|
|
|
readonly order: number
|
2026-07-22 22:58:05 +08:00
|
|
|
|
/**
|
|
|
|
|
|
* Static text or a provider evaluated at each assembly with that assembly's
|
|
|
|
|
|
* {@link AssembleContext}. The text may reference `{{variable}}`s — they are
|
|
|
|
|
|
* interpolated later, by {@link renderPrompt}.
|
|
|
|
|
|
*/
|
2026-07-15 23:11:25 -07:00
|
|
|
|
readonly text: string | ((context: AssembleContext) => string)
|
|
|
|
|
|
}
|
|
|
|
|
|
```
|
2026-07-30 22:09:15 +08:00
|
|
|
|
|
|
|
|
|
|
## 动态提示词上下文
|
|
|
|
|
|
|
|
|
|
|
|
`PromptContext` 是与 `PromptSection` 对应的缓存安全结构。组装会解析这些贡献并排序;agent loop(智能体循环)仅在完整当前快照发生变化或被压缩(compaction)移除时,才会将其记录在保留的模型历史之后。
|
|
|
|
|
|
|
|
|
|
|
|
```ts type-equiv
|
2026-08-03 12:25:33 +08:00
|
|
|
|
/** Dynamic model context materialized as a durable user-role snapshot. */
|
2026-07-30 22:09:15 +08:00
|
|
|
|
interface PromptContext {
|
|
|
|
|
|
/** Unique name — a duplicate registration throws (see {@link SystemPrompt.context}). */
|
|
|
|
|
|
readonly name: string
|
2026-08-03 12:25:33 +08:00
|
|
|
|
/** Contexts are joined in ascending order. */
|
2026-07-30 22:09:15 +08:00
|
|
|
|
readonly order: number
|
|
|
|
|
|
/** Static text or a provider evaluated for each assembly. Empty text contributes nothing. */
|
|
|
|
|
|
readonly text: string | ((context: AssembleContext) => string)
|
|
|
|
|
|
}
|
|
|
|
|
|
```
|