Skip to content
中文

ContextPolicy ​

SUPPORT · type · Type contract

Declared context policy for one stage.

Import ​

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

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

Definition ​

Type: ContextPolicy

Type expression
ts
type ContextPolicy = | { mode: 'validate' }
  /**
   * Reduce content when a trigger is exceeded.
   *
   * Character and token triggers/targets may be combined; `targetInputChars`/`targetInputTokens`
   * define the size the reduction must reach.
   */
  | {
      mode: 'reduce';
      triggerChars?: number;
      targetInputChars?: number;
      triggerTokens?: number;
      targetInputTokens?: number;
      selection?: ContextSelection;
      steps: readonly ContextReduction[];
    }
  /** Verify that only the listed fragments changed value, without reducing anything. */
  | { mode: 'audit'; mutableFragmentIds: readonly string[] };

Union branch structure ​

Optional shapes of ContextPolicy: 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.

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);

API reference, usage examples and developer guides.