Skip to content
English

ContextBuilder ​

SUPPORT · interface · 类型合同

Compiled builder. It holds no request state and may be shared across invocations.

导入 ​

ts
import type { ContextBuilder } from '@jangts/agent-core/support';
js
/** @typedef {import('@jangts/agent-core/support').ContextBuilder} ContextBuilder */

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

定义 ​

ts
interface ContextBuilder<Input>

泛型参数 ​

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

字段结构 ​

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

成员目录 ​

成员用途
fragmentsProduces the fragments for one input, after transforms.
buildAssembles and validates the plan into messages with usage accounting.
planProduces the unassembled plan without running the budget boundary.

成员详情 ​

fragments ​

Produces the fragments for one input, after transforms.

ts
ContextBuilder.fragments(input: Input): ContextFragment[]

Produces the fragments for one input, after transforms.

参数类型传递说明
inputInput必需—

返回: ContextFragment[]。

build ​

Assembles and validates the plan into messages with usage accounting.

ts
ContextBuilder.build(request: { input: Input; maxInputChars?: number; } & Omit<ContextPlan, "fragments" | "maxInputChars">): BuiltContext

Assembles and validates the plan into messages with usage accounting.

参数类型传递说明
request{ input: Input; maxInputChars?: number; } & Omit<ContextPlan, "maxInputChars" | "fragments">必需—
request 的字段
字段类型必需说明
inputInput必需见类型定义及调用场景。
maxInputCharsnumber | undefined可省略见类型定义及调用场景。
additionalMessagesreadonly (ModelMessage & { role: "user"; })[] | undefined可省略Protected user messages (for example image inputs); text counts toward the same budget.
originalFragmentsreadonly ContextFragment[] | undefined可省略Trusted originals for structural mutation auditing; never sent to the model.
mutableFragmentIdsreadonly string[] | undefined可省略Explicit value-mutation allowance when originals are supplied.

返回: BuiltContext。

返回值结构
字段类型必需说明
messagesModelMessage[]必需Messages in final model order.
fragmentUsageContextFragmentUsage[]必需Per-fragment accounting in the same order as the fragments were merged.
inputCharsnumber必需Total counted characters across messages and usage.
maxInputCharsnumber必需Budget the assembly was validated against; Infinity means unbounded.

plan ​

Produces the unassembled plan without running the budget boundary.

ts
ContextBuilder.plan(request: { input: Input; maxInputChars?: number; } & Omit<ContextPlan, "fragments" | "maxInputChars">): ContextPlan

Produces the unassembled plan without running the budget boundary.

参数类型传递说明
request{ input: Input; maxInputChars?: number; } & Omit<ContextPlan, "maxInputChars" | "fragments">必需—
request 的字段
字段类型必需说明
inputInput必需见类型定义及调用场景。
maxInputCharsnumber | undefined可省略见类型定义及调用场景。
additionalMessagesreadonly (ModelMessage & { role: "user"; })[] | undefined可省略Protected user messages (for example image inputs); text counts toward the same budget.
originalFragmentsreadonly ContextFragment[] | undefined可省略Trusted originals for structural mutation auditing; never sent to the model.
mutableFragmentIdsreadonly string[] | undefined可省略Explicit value-mutation allowance when originals are supplied.

返回: ContextPlan。

返回值结构
字段类型必需说明
fragmentsreadonly ContextFragment[]必需只读。Fragments in the order supplied by the App; assembly preserves it.
maxInputCharsnumber必需Character budget Core validates after assembly.
additionalMessagesreadonly (ModelMessage & { role: "user"; })[] | undefined可省略Protected user messages (for example image inputs); text counts toward the same budget.
originalFragmentsreadonly ContextFragment[] | undefined可省略Trusted originals for structural mutation auditing; never sent to the model.
mutableFragmentIdsreadonly string[] | undefined可省略Explicit value-mutation allowance when originals are supplied.

使用示例 ​

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

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

相关策略源码 ​

相关定义 ​

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