processContext
SUPPORT · function · Runtime value
Run a complete context policy in one call: snapshot, execute and finalize.
Import
import { processContext } from '@jangts/agent-core/support';import { processContext } from '@jangts/agent-core/support';Other public entry points:@jangts/agent-core/support, @jangts/agent-core/support/context.
Definition
Call
processContext(plan: ContextPlan, policy: ContextPolicy, ports: ProcessingPorts): Promise<{ context: BuiltContext; report: ContextProcessingReport; }>Run a complete context policy in one call: snapshot, execute and finalize.
| Parameter | Type | Passing | Description |
|---|---|---|---|
plan | ContextPlan | Required | — |
policy | ContextPolicy | Required | — |
ports | ContextProcessingOptions | Required | — |
Fields of plan
| Field | Type | Required | Description |
|---|---|---|---|
fragments | readonly ContextFragment[] | Required | Read-only. Fragments in the order supplied by the App; assembly preserves it. |
maxInputChars | number | Required | Character budget Core validates after assembly. |
additionalMessages | readonly (ModelMessage & { role: "user"; })[] | undefined | Optional | Protected user messages (for example image inputs); text counts toward the same budget. |
originalFragments | readonly ContextFragment[] | undefined | Optional | Trusted originals for structural mutation auditing; never sent to the model. |
mutableFragmentIds | readonly string[] | undefined | Optional | Explicit value-mutation allowance when originals are supplied. |
Fields of ports
| Field | Type | Required | Description |
|---|---|---|---|
stageId | string | Required | Read-only. Stage the processing belongs to; reported in diagnostics and selection calls. |
compressors | readonly ContextCompressor[] | Required | Read-only. Compressors available by handler id. |
selectors | readonly ContextSelector[] | undefined | Optional | Read-only. Selectors available by handler id; required only for model selection. |
callModel | ContextModelCall | Required | Read-only. See the type definition and usage context. |
remainingCalls | (() => number) | undefined | Optional | Read-only. Remaining model calls; used to reject unaffordable selections. |
tokenBudget | ContextTokenMeasurement | undefined | Optional | Read-only. Token measurement; without it, token triggers and targets cannot apply. |
logger | SubFlowLog | undefined | Optional | Read-only. Explicit optional recorder for this operation; never inferred from diagnostics. |
signal | AbortSignal | Required | Read-only. Non-optional cancellation signal; never undefined once execution has started. |
diagnostics | DiagnosticContext | undefined | Optional | Read-only. Diagnostic scope of the enclosing call, when recording is enabled. |
Optional shapes of Parameter policy: choose one shape; required fields for each branch are listed separately.
Branch 1
| Field | Type | Required | Description |
|---|---|---|---|
mode | "validate" | Required | See the type definition and usage context. |
Branch 2
| Field | Type | Required | Description |
|---|---|---|---|
mode | "reduce" | Required | See the type definition and usage context. |
triggerChars | number | undefined | Optional | See the type definition and usage context. |
targetInputChars | number | undefined | Optional | See the type definition and usage context. |
triggerTokens | number | undefined | Optional | See the type definition and usage context. |
targetInputTokens | number | undefined | Optional | See the type definition and usage context. |
selection | ContextSelection | undefined | Optional | See the type definition and usage context. |
steps | readonly ContextReduction[] | Required | See the type definition and usage context. |
Branch 3
| Field | Type | Required | Description |
|---|---|---|---|
mode | "audit" | Required | See the type definition and usage context. |
mutableFragmentIds | readonly string[] | Required | See the type definition and usage context. |
Returns: Promise<{ context: BuiltContext; report: ContextProcessingReport; }>.
Awaited value: { context: BuiltContext; report: ContextProcessingReport; }.
Return value structure
| Field | Type | Required | Description |
|---|---|---|---|
context | BuiltContext | Required | See the type definition and usage context. |
report | ContextProcessingReport | Required | See the type definition and usage context. |
Examples
- Context policy: validate, reduce, and audit: Support provides replaceable policy implementations while Core keeps the final input contract and budget checks.
Explicitly use a validation-only policy
Beginner · Node.js
import { Support } from '@jangts/agent-core';
import type { ContextPolicy } from '@jangts/agent-core/support/context';
const policy: ContextPolicy = { mode: 'validate' };
Support.validateContextPolicy(policy, [], []);
console.log(policy.mode);import { Support } from '@jangts/agent-core';
const policy = { mode: 'validate' };
Support.validateContextPolicy(policy, [], []);
console.log(policy.mode);Configure reduction order from real business fragments
Configuration · Node.js
These numbers are explicit choices for the example business, not SDK recommendations for capacities that suit all models.
import { Support } from '@jangts/agent-core';
import type { ContextPolicy } from '@jangts/agent-core/support/context';
const policy: ContextPolicy = { mode: 'reduce', triggerChars: 1800, targetInputChars: 1500,
selection: { mode: 'sequence' },
steps: [{ id: 'recent-history', kind: 'trim', fragmentId: 'history', field: 'messages',
grouping: { kind: 'user-turn' }, keepGroups: 2, targetChars: 1000 }],
};
Support.validateContextPolicy(policy, [], []);
console.log(policy.steps);import { Support } from '@jangts/agent-core';
const policy = { mode: 'reduce', triggerChars: 1800, targetInputChars: 1500,
selection: { mode: 'sequence' },
steps: [{ id: 'recent-history', kind: 'trim', fragmentId: 'history', field: 'messages',
grouping: { kind: 'user-turn' }, keepGroups: 2, targetChars: 1000 }],
};
Support.validateContextPolicy(policy, [], []);
console.log(policy.steps);Limit the mutable range for the App's own processing
Extension · Node.js
Auditing also requires the ContextPlan to carry the original fragments; listing IDs alone does not modify or publish material by itself.
import { Support } from '@jangts/agent-core';
import type { ContextPolicy } from '@jangts/agent-core/support/context';
const policy: ContextPolicy = { mode: 'audit', mutableFragmentIds: ['history'] };
Support.validateContextPolicy(policy, [], []);
console.log(policy.mutableFragmentIds);import { Support } from '@jangts/agent-core';
const policy = { mode: 'audit', mutableFragmentIds: ['history'] };
Support.validateContextPolicy(policy, [], []);
console.log(policy.mutableFragmentIds);