Skip to content
English

defineContextAssembly ​

SUPPORT · function · 运行时值

Compile ordered context projections into a builder.

A projection is either a single fragment (with its own formatter) or a placement whose contribution handler expands to any number of fragments at request time.

导入 ​

ts
import { defineContextAssembly } from '@jangts/agent-core/support';
js
import { defineContextAssembly } from '@jangts/agent-core/support';

其他公开入口:@jangts/agent-core、@jangts/agent-core/types、@jangts/agent-core/support、@jangts/agent-core/support/context。

定义 ​

泛型参数 ​

类型参数约束默认类型
Input——
Idstringstring
Placementstringstring

调用 ​

ts
defineContextAssembly<Input, Id extends string = string, Placement extends string = string>(definitions: readonly ContextProjection<Input, Id, Placement>[], options: ContextAssemblyOptions<Input, Id, Placement>): ContextBuilder<Input>

Compile ordered context projections into a builder.

A projection is either a single fragment (with its own formatter) or a placement whose contribution handler expands to any number of fragments at request time.

类型参数约束默认类型
Input——
Idstringstring
Placementstringstring
参数类型传递说明
definitionsreadonly ContextProjection<Input, Id, Placement>[]必需—
optionsContextAssemblyOptions<Input, Id, Placement>必需—
options 的字段
字段类型必需说明
fragment(id: Id, value: ContextFragment["value"]) => ContextFragment必需只读。Builds a fragment for a single-fragment projection.
contributionsReadonly<Record<Placement, (input: Input) => readonly ContextFragment[]>> | undefined可省略只读。Contribution handlers keyed by placement; each expands to zero or more fragments.
maxInputCharsnumber | ((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.

返回: ContextBuilder<Input>。

返回值结构
字段类型必需说明
fragments(input: Input) => ContextFragment[]必需Produces the fragments for one input, after transforms.
build(request: { input: Input; maxInputChars?: number; } & Omit<ContextPlan, "maxInputChars" | "fragments">) => BuiltContext必需Assembles and validates the plan into messages with usage accounting.
plan(request: { input: Input; maxInputChars?: number; } & Omit<ContextPlan, "maxInputChars" | "fragments">) => ContextPlan必需Produces the unassembled plan without running the budget boundary.

使用示例 ​

一次配置,以后只给问题和历史 ​

入门 · 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);
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);

同一个 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);
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);

复用元数据目录与请求贡献插槽 ​

扩展 · Node.js

ts
import { Support } from '@jangts/agent-core';
import type { ContextFragment } from '@jangts/agent-core/types';
type Input = { question: string; evidence: string };
const builder = Support.defineContextAssembly<Input, 'question', 'evidence'>([
  { kind: 'fragment', id: 'question', format: input => input.question },
  { kind: 'contributions', placement: 'evidence' },
], {
  maxInputChars: 2000,
  fragment(id, value): ContextFragment { return { id, sourceId: 'request', trust: 'user', lifetime: 'turn', role: 'user', maxChars: 1000, value }; },
  contributions: { evidence(input) { return [{ id: 'evidence', sourceId: 'service', trust: 'server', lifetime: 'turn', role: 'user', maxChars: 1000, value: input.evidence }]; } },
});
console.log(builder.build({ input: { question: '状态?', evidence: '最近检查:正常' } }).messages);
js
import { Support } from '@jangts/agent-core';
const builder = Support.defineContextAssembly([
    { kind: 'fragment', id: 'question', format: input => input.question },
    { kind: 'contributions', placement: 'evidence' },
], {
    maxInputChars: 2000,
    fragment(id, value) { return { id, sourceId: 'request', trust: 'user', lifetime: 'turn', role: 'user', maxChars: 1000, value }; },
    contributions: { evidence(input) { return [{ id: 'evidence', sourceId: 'service', trust: 'server', lifetime: 'turn', role: 'user', maxChars: 1000, value: input.evidence }]; } },
});
console.log(builder.build({ input: { question: '状态?', evidence: '最近检查:正常' } }).messages);

相关策略源码 ​

相关定义 ​

接口参考、使用示例与开发指南。