Skip to content
中文

defineContextAssembly ​

SUPPORT · function · Runtime value

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.

Import ​

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

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

Definition ​

Type parameters ​

Type parameterConstraintDefault type
Input——
Idstringstring
Placementstringstring

Call ​

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.

Type parameterConstraintDefault type
Input——
Idstringstring
Placementstringstring
ParameterTypePassingDescription
definitionsreadonly ContextProjection<Input, Id, Placement>[]Required—
optionsContextAssemblyOptions<Input, Id, Placement>Required—
Fields of options
FieldTypeRequiredDescription
fragment(id: Id, value: ContextFragment["value"]) => ContextFragmentRequiredRead-only. Builds a fragment for a single-fragment projection.
contributionsReadonly<Record<Placement, (input: Input) => readonly ContextFragment[]>> | undefinedOptionalRead-only. Contribution handlers keyed by placement; each expands to zero or more fragments.
maxInputCharsnumber | ((input: Input) => number) | undefinedOptionalRead-only. Character budget, constant or derived from the request; at least one of these is required.
transform((fragment: ContextFragment, input: Input) => ContextFragment) | undefinedOptionalRead-only. Applied to every produced fragment before assembly; must be synchronous.

Returns: ContextBuilder<Input>.

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

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

Reuse a metadata catalog and request contribution slots ​

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

API reference, usage examples and developer guides.