Skip to content
中文

ContextReduction ​

SUPPORT · type · Type contract

One declared reduction step.

Import ​

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

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

Definition ​

Type: ContextReduction

Type expression
ts
type ContextReduction = | {
      id: string;
      kind: 'trim';
      fragmentId: string;
      field: string;
      targetChars: number;
      grouping: ContextHistoryGrouping;
      keepGroups: number;
    }
  /** Rewrites one fragment through a registered compressor down to `targetChars`. */
  | ({
      id: string;
      kind: 'compress';
      fragmentId: string;
      targetChars: number;
      /** Recent history the compressor must preserve verbatim. */
      preserve?: ContextRecentHistory;
    } & ContextHandlerReference);

Union branch structure ​

Optional shapes of ContextReduction: choose one shape; required fields for each branch are listed separately.

Branch 1
FieldTypeRequiredDescription
idstringRequiredSee the type definition and usage context.
kind"trim"RequiredSee the type definition and usage context.
fragmentIdstringRequiredSee the type definition and usage context.
fieldstringRequiredSee the type definition and usage context.
targetCharsnumberRequiredSee the type definition and usage context.
groupingContextHistoryGroupingRequiredSee the type definition and usage context.
keepGroupsnumberRequiredSee the type definition and usage context.
Branch 2
FieldTypeRequiredDescription
idstringRequiredSee the type definition and usage context.
kind"compress"RequiredSee the type definition and usage context.
fragmentIdstringRequiredSee the type definition and usage context.
targetCharsnumberRequiredSee the type definition and usage context.
preserveContextRecentHistory | undefinedOptionalRecent history the compressor must preserve verbatim.
handlerIdstringRequiredRead-only. Registered handler id; an unknown id is rejected before execution.

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.