Skip to content
中文

ContextBuilder ​

SUPPORT · interface · Type contract

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

Import ​

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

Other public entry points:@jangts/agent-core, @jangts/agent-core/types, @jangts/agent-core/structs, @jangts/agent-core/support, @jangts/agent-core/support/context.

Definition ​

ts
interface ContextBuilder<Input>

Type parameters ​

Type parameterConstraintDefault type
Input——

Field structure ​

FieldTypeRequiredDescription
fragments(input: Input) => ContextFragment[]RequiredProduces the fragments for one input, after transforms.
build(request: { input: Input; maxInputChars?: number; } & Omit<ContextPlan, "fragments" | "maxInputChars">) => BuiltContextRequiredAssembles and validates the plan into messages with usage accounting.
plan(request: { input: Input; maxInputChars?: number; } & Omit<ContextPlan, "fragments" | "maxInputChars">) => ContextPlanRequiredProduces the unassembled plan without running the budget boundary.

Member index ​

MemberPurpose
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.

Member details ​

fragments ​

Produces the fragments for one input, after transforms.

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

Produces the fragments for one input, after transforms.

ParameterTypePassingDescription
inputInputRequired—

Returns: 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.

ParameterTypePassingDescription
request{ input: Input; maxInputChars?: number; } & Omit<ContextPlan, "maxInputChars" | "fragments">Required—
Fields of request
FieldTypeRequiredDescription
inputInputRequiredSee the type definition and usage context.
maxInputCharsnumber | undefinedOptionalSee the type definition and usage context.
additionalMessagesreadonly (ModelMessage & { role: "user"; })[] | undefinedOptionalProtected user messages (for example image inputs); text counts toward the same budget.
originalFragmentsreadonly ContextFragment[] | undefinedOptionalTrusted originals for structural mutation auditing; never sent to the model.
mutableFragmentIdsreadonly string[] | undefinedOptionalExplicit value-mutation allowance when originals are supplied.

Returns: BuiltContext.

Return value structure
FieldTypeRequiredDescription
messagesModelMessage[]RequiredMessages in final model order.
fragmentUsageContextFragmentUsage[]RequiredPer-fragment accounting in the same order as the fragments were merged.
inputCharsnumberRequiredTotal counted characters across messages and usage.
maxInputCharsnumberRequiredBudget 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.

ParameterTypePassingDescription
request{ input: Input; maxInputChars?: number; } & Omit<ContextPlan, "maxInputChars" | "fragments">Required—
Fields of request
FieldTypeRequiredDescription
inputInputRequiredSee the type definition and usage context.
maxInputCharsnumber | undefinedOptionalSee the type definition and usage context.
additionalMessagesreadonly (ModelMessage & { role: "user"; })[] | undefinedOptionalProtected user messages (for example image inputs); text counts toward the same budget.
originalFragmentsreadonly ContextFragment[] | undefinedOptionalTrusted originals for structural mutation auditing; never sent to the model.
mutableFragmentIdsreadonly string[] | undefinedOptionalExplicit value-mutation allowance when originals are supplied.

Returns: ContextPlan.

Return value structure
FieldTypeRequiredDescription
fragmentsreadonly ContextFragment[]RequiredRead-only. Fragments in the order supplied by the App; assembly preserves it.
maxInputCharsnumberRequiredCharacter budget Core validates after assembly.
additionalMessagesreadonly (ModelMessage & { role: "user"; })[] | undefinedOptionalProtected user messages (for example image inputs); text counts toward the same budget.
originalFragmentsreadonly ContextFragment[] | undefinedOptionalTrusted originals for structural mutation auditing; never sent to the model.
mutableFragmentIdsreadonly string[] | undefinedOptionalExplicit value-mutation allowance when originals are supplied.

Examples ​

Configure once, then supply only question and history ​

Beginner · 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);

The same builder produces a plan in advance ​

Configuration · 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);

API reference, usage examples and developer guides.