Skip to content
中文

processContext ​

SUPPORT · function · Runtime value

Run a complete context policy in one call: snapshot, execute and finalize.

Import ​

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

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

Definition ​

Call ​

ts
processContext(plan: ContextPlan, policy: ContextPolicy, ports: ProcessingPorts): Promise<{ context: BuiltContext; report: ContextProcessingReport; }>

Run a complete context policy in one call: snapshot, execute and finalize.

ParameterTypePassingDescription
planContextPlanRequired—
policyContextPolicyRequired—
portsContextProcessingOptionsRequired—
Fields of plan
FieldTypeRequiredDescription
fragmentsreadonly ContextFragment[]RequiredRead-only. Fragments in the order supplied by the App; assembly preserves it.
maxInputCharsnumberRequiredCharacter budget Core validates after assembly.
additionalMessagesreadonly (ModelMessage & { role: "user"; })[] | undefinedOptionalProtected user messages (for example image inputs); text counts toward the same budget.
originalFragmentsreadonly ContextFragment[] | undefinedOptionalTrusted originals for structural mutation auditing; never sent to the model.
mutableFragmentIdsreadonly string[] | undefinedOptionalExplicit value-mutation allowance when originals are supplied.
Fields of ports
FieldTypeRequiredDescription
stageIdstringRequiredRead-only. Stage the processing belongs to; reported in diagnostics and selection calls.
compressorsreadonly ContextCompressor[]RequiredRead-only. Compressors available by handler id.
selectorsreadonly ContextSelector[] | undefinedOptionalRead-only. Selectors available by handler id; required only for model selection.
callModelContextModelCallRequiredRead-only. See the type definition and usage context.
remainingCalls(() => number) | undefinedOptionalRead-only. Remaining model calls; used to reject unaffordable selections.
tokenBudgetContextTokenMeasurement | undefinedOptionalRead-only. Token measurement; without it, token triggers and targets cannot apply.
loggerSubFlowLog | undefinedOptionalRead-only. Explicit optional recorder for this operation; never inferred from diagnostics.
signalAbortSignalRequiredRead-only. Non-optional cancellation signal; never undefined once execution has started.
diagnosticsDiagnosticContext | undefinedOptionalRead-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
FieldTypeRequiredDescription
mode"validate"RequiredSee the type definition and usage context.
Branch 2
FieldTypeRequiredDescription
mode"reduce"RequiredSee the type definition and usage context.
triggerCharsnumber | undefinedOptionalSee the type definition and usage context.
targetInputCharsnumber | undefinedOptionalSee the type definition and usage context.
triggerTokensnumber | undefinedOptionalSee the type definition and usage context.
targetInputTokensnumber | undefinedOptionalSee the type definition and usage context.
selectionContextSelection | undefinedOptionalSee the type definition and usage context.
stepsreadonly ContextReduction[]RequiredSee the type definition and usage context.
Branch 3
FieldTypeRequiredDescription
mode"audit"RequiredSee the type definition and usage context.
mutableFragmentIdsreadonly string[]RequiredSee the type definition and usage context.

Returns: Promise<{ context: BuiltContext; report: ContextProcessingReport; }>.

Awaited value: { context: BuiltContext; report: ContextProcessingReport; }.

Return value structure
FieldTypeRequiredDescription
contextBuiltContextRequiredSee the type definition and usage context.
reportContextProcessingReportRequiredSee the type definition and usage context.

Examples ​

Explicitly use a validation-only policy ​

Beginner · Node.js

ts
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);
js
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.

ts
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);
js
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.

ts
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);
js
import { Support } from '@jangts/agent-core';
const policy = { mode: 'audit', mutableFragmentIds: ['history'] };
Support.validateContextPolicy(policy, [], []);
console.log(policy.mutableFragmentIds);

API reference, usage examples and developer guides.