ContextProjection
SUPPORT · type · Type contract
One declared projection: a single fragment or a placement expanded into fragments.
Import
ts
import type { ContextProjection } from '@jangts/agent-core/support';js
/** @typedef {import('@jangts/agent-core/support').ContextProjection} ContextProjection */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
Type: ContextProjection<Input, Id, Placement>
Type expression
ts
type ContextProjection<Input, Id extends string = string, Placement extends string = string> = | {
readonly kind: 'fragment';
/** Fragment id; must be unique across all projections. */
readonly id: Id;
/** Produces the fragment value; must be synchronous. */
readonly format: (input: Input) => ContextFragment['value'];
}
/** Expands to the fragments supplied for this placement; ids use a separate prefix. */
| { readonly kind: 'contributions'; readonly placement: Placement };Type parameters
| Type parameter | Constraint | Default type |
|---|---|---|
Input | — | — |
Id | string | string |
Placement | string | string |
Union branch structure
Optional shapes of ContextProjection: choose one shape; required fields for each branch are listed separately.
Branch 1
| Field | Type | Required | Description |
|---|---|---|---|
kind | "fragment" | Required | Read-only. See the type definition and usage context. |
id | Id | Required | Read-only. Fragment id; must be unique across all projections. |
format | (input: Input) => ContextFragment["value"] | Required | Read-only. Produces the fragment value; must be synchronous. |
Branch 2
| Field | Type | Required | Description |
|---|---|---|---|
kind | "contributions" | Required | Read-only. See the type definition and usage context. |
placement | Placement | Required | Read-only. See the type definition and usage context. |
Examples
- Context assembly: configure stable metadata once: Support binds fragment metadata, formatting, and order in advance, and requests supply only the changing business input.
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);