Skip to content
中文

ContextAssemblyOptions ​

SUPPORT · interface · Type contract

Assembly options for placement-based projections.

Import ​

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

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 ContextAssemblyOptions<Input, Id, Placement>

Extends or implements: ContextBuilderOptions<Input>.

Type parameters ​

Type parameterConstraintDefault type
Input——
Idstring—
Placementstring—

Field structure ​

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.

Member index ​

MemberPurpose
fragmentBuilds a fragment for a single-fragment projection.

Member details ​

fragment ​

Builds a fragment for a single-fragment projection.

ts
ContextAssemblyOptions.fragment(id: Id, value: ContextFragment["value"]): ContextFragment
ParameterTypePassingDescription
idIdRequired—
valuestring | Record<string, unknown>Required—

Returns: ContextFragment.

Return value structure
FieldTypeRequiredDescription
idstringRequiredUnique, stable id matching ^[a-z][a-zA-Z0-9:._-]*$; duplicates are rejected.
sourceIdstringRequiredOwning source (module or registration) used to explain where the content came from.
trustContextTrustRequiredProducer trust level. user and tool content cannot become a system message.
lifetimeContextLifetimeRequiredDeclared validity window of this content.
maxCharsnumberRequiredHard upper bound in JSON characters; must be a positive safe integer.
role"system" | "user"RequiredMessage slot the fragment occupies when assembled.
valuestring | Record<string, unknown>RequiredRendered content. A system message accepts a string only, never a record.

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.