ContextAssemblyOptions
SUPPORT · interface · 类型合同
Assembly options for placement-based projections.
导入
ts
import type { ContextAssemblyOptions } from '@jangts/agent-core/support';1
js
/** @typedef {import('@jangts/agent-core/support').ContextAssemblyOptions} ContextAssemblyOptions */1
其他公开入口:@jangts/agent-core、@jangts/agent-core/types、@jangts/agent-core/structs、@jangts/agent-core/support、@jangts/agent-core/support/context。
定义
ts
interface ContextAssemblyOptions<Input, Id, Placement>1
继承或实现: ContextBuilderOptions<Input>。
泛型参数
| 类型参数 | 约束 | 默认类型 |
|---|---|---|
Input | — | — |
Id | string | — |
Placement | string | — |
字段结构
| 字段 | 类型 | 必需 | 说明 |
|---|---|---|---|
fragment | (id: Id, value: ContextFragment["value"]) => ContextFragment | 必需 | 只读。Builds a fragment for a single-fragment projection. |
contributions | Readonly<Record<Placement, (input: Input) => readonly ContextFragment[]>> | undefined | 可省略 | 只读。Contribution handlers keyed by placement; each expands to zero or more fragments. |
maxInputChars | number | ((input: Input) => number) | undefined | 可省略 | 只读。Character budget, constant or derived from the request; at least one of these is required. |
transform | ((fragment: ContextFragment, input: Input) => ContextFragment) | undefined | 可省略 | 只读。Applied to every produced fragment before assembly; must be synchronous. |
成员目录
| 成员 | 用途 |
|---|---|
fragment | Builds a fragment for a single-fragment projection. |
成员详情
fragment
Builds a fragment for a single-fragment projection.
ts
ContextAssemblyOptions.fragment(id: Id, value: ContextFragment["value"]): ContextFragment1
| 参数 | 类型 | 传递 | 说明 |
|---|---|---|---|
id | Id | 必需 | — |
value | string | Record<string, unknown> | 必需 | — |
返回: ContextFragment。
返回值结构
| 字段 | 类型 | 必需 | 说明 |
|---|---|---|---|
id | string | 必需 | Unique, stable id matching ^[a-z][a-zA-Z0-9:._-]*$; duplicates are rejected. |
sourceId | string | 必需 | Owning source (module or registration) used to explain where the content came from. |
trust | ContextTrust | 必需 | Producer trust level. user and tool content cannot become a system message. |
lifetime | ContextLifetime | 必需 | Declared validity window of this content. |
maxChars | number | 必需 | Hard upper bound in JSON characters; must be a positive safe integer. |
role | "system" | "user" | 必需 | Message slot the fragment occupies when assembled. |
value | string | Record<string, unknown> | 必需 | Rendered content. A system message accepts a string only, never a record. |
使用示例
- 上下文装配:稳定元数据配置一次:Support 提前绑定片段元数据、格式化和顺序,请求只提供变化的业务输入。
一次配置,以后只给问题和历史
入门 · Node.js
ts
import { Support } from '@jangts/agent-core';
type Input = { question: string; history: readonly { role: string; content: string }[] };
const builder = Support.defineContextFragments<Input>([
{ id: 'question', sourceId: 'request', trust: 'user', lifetime: 'turn', maxChars: 1000,
role: 'user', format: input => input.question },
{ id: 'history', sourceId: 'conversation', trust: 'user', lifetime: 'turn', maxChars: 10000,
role: 'user', format: input => ({ messages: input.history }) },
], { maxInputChars: 1200 });
const context = builder.build({ input: { question: '服务状态?', history: [] } });
console.log(context.messages);1
2
3
4
5
6
7
8
9
10
2
3
4
5
6
7
8
9
10
js
import { Support } from '@jangts/agent-core';
const builder = Support.defineContextFragments([
{ id: 'question', sourceId: 'request', trust: 'user', lifetime: 'turn', maxChars: 1000,
role: 'user', format: input => input.question },
{ id: 'history', sourceId: 'conversation', trust: 'user', lifetime: 'turn', maxChars: 10000,
role: 'user', format: input => ({ messages: input.history }) },
], { maxInputChars: 1200 });
const context = builder.build({ input: { question: '服务状态?', history: [] } });
console.log(context.messages);1
2
3
4
5
6
7
8
9
2
3
4
5
6
7
8
9
同一个 builder 提前给出计划
配置 · Node.js
ts
import { Support } from '@jangts/agent-core';
type Input = { question: string; history: readonly { role: string; content: string }[] };
const builder = Support.defineContextFragments<Input>([
{ id: 'question', sourceId: 'request', trust: 'user', lifetime: 'turn', maxChars: 1000,
role: 'user', format: input => input.question },
{ id: 'history', sourceId: 'conversation', trust: 'user', lifetime: 'turn', maxChars: 10000,
role: 'user', format: input => ({ messages: input.history }) },
], { maxInputChars: 1200 });
const input = { question: '服务状态?', history: [] };
const plan = builder.plan({ input });
const expanded = builder.fragments(input);
console.log(plan.maxInputChars, expanded.map(fragment => fragment.id));
const largerRequest = builder.build({ input, maxInputChars: 2000 });
console.log(largerRequest.maxInputChars);1
2
3
4
5
6
7
8
9
10
11
12
13
14
2
3
4
5
6
7
8
9
10
11
12
13
14
js
import { Support } from '@jangts/agent-core';
const builder = Support.defineContextFragments([
{ id: 'question', sourceId: 'request', trust: 'user', lifetime: 'turn', maxChars: 1000,
role: 'user', format: input => input.question },
{ id: 'history', sourceId: 'conversation', trust: 'user', lifetime: 'turn', maxChars: 10000,
role: 'user', format: input => ({ messages: input.history }) },
], { maxInputChars: 1200 });
const input = { question: '服务状态?', history: [] };
const plan = builder.plan({ input });
const expanded = builder.fragments(input);
console.log(plan.maxInputChars, expanded.map(fragment => fragment.id));
const largerRequest = builder.build({ input, maxInputChars: 2000 });
console.log(largerRequest.maxInputChars);1
2
3
4
5
6
7
8
9
10
11
12
13
2
3
4
5
6
7
8
9
10
11
12
13