Skip to content
中文

Context fragments: explicit source, trust, and budget ​

Core recognizes fragments and the assembly contract, validates source metadata and budgets, and has no built-in business compression algorithm.

A fragment contains id/sourceId/trust/lifetime/maxChars/role/value. trust describes source semantics and does not grant system permissions to data; external raw text is still data. Assembly outputs BuiltContext, containing messages, fragmentUsage, and character usage.

assembleFragments validates and assembles in the registered order and does not silently truncate over-limit fragments. assembleContextPlan audits the original fragments and the mutable range before assembling; it does not call a model selector or compressor. The recommended selection and compression algorithms are provided by ordinary Support nodes, and custom nodes can also implement the same contract.

ContextPlan is a candidate fragment and budget plan, not a model request that has already been sent. fragmentUsage is usage metadata after assembly and must not be treated as the original fragments.

Progressive examples for the same business case ​

Assemble one input from bare configuration ​

Beginner · Node.js

ts
import { assembleFragments } from '@jangts/agent-core/context';
const input = assembleFragments([{ id: 'question', sourceId: 'request', trust: 'user',
  lifetime: 'turn', maxChars: 1000, role: 'user', value: '现在服务状态怎样?' }], 2000);
console.log(input.messages, input.fragmentUsage);
js
import { assembleFragments } from '@jangts/agent-core/context';
const input = assembleFragments([{ id: 'question', sourceId: 'request', trust: 'user',
        lifetime: 'turn', maxChars: 1000, role: 'user', value: '现在服务状态怎样?' }], 2000);
console.log(input.messages, input.fragmentUsage);

Add a candidate plan and final audit ​

Configuration · Node.js

An empty mutable fragment list means the original fragments may not be modified; no compression step is authorized automatically.

ts
import { assembleContextPlan } from '@jangts/agent-core/context';
import type { ContextFragment } from '@jangts/agent-core/types';
const fragments: ContextFragment[] = [{ id: 'question', sourceId: 'request', trust: 'user',
  lifetime: 'turn', maxChars: 1000, role: 'user', value: '现在服务状态怎样?' }];
const input = assembleContextPlan({ fragments, originalFragments: fragments, mutableFragmentIds: [], maxInputChars: 2000 });
console.log(input.inputChars);
js
import { assembleContextPlan } from '@jangts/agent-core/context';
const fragments = [{ id: 'question', sourceId: 'request', trust: 'user',
        lifetime: 'turn', maxChars: 1000, role: 'user', value: '现在服务状态怎样?' }];
const input = assembleContextPlan({ fragments, originalFragments: fragments, mutableFragmentIds: [], maxInputChars: 2000 });
console.log(input.inputChars);

Policies & implementations ​

API reference, usage examples and developer guides.