Skip to content
中文

createContextStrategy ​

SUPPORT · function · Runtime value

Build a reduction policy from one or more history declarations.

recent keeps a window of recent groups; continuity summarizes older groups while preserving the most recent originals. Neither preset claims semantic quality, only bounded input size.

Import ​

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

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

Definition ​

Call ​

ts
createContextStrategy(options: ContextStrategyOptions | readonly ContextStrategyOptions[], settings?: ContextStrategySettings): ContextStrategy

Build a reduction policy from one or more history declarations.

recent keeps a window of recent groups; continuity summarizes older groups while preserving the most recent originals. Neither preset claims semantic quality, only bounded input size.

ParameterTypePassingDescription
optionsContextStrategyOptions | readonly ContextStrategyOptions[]Required—
settingsContextStrategySettingsOptional—
Fields of settings
FieldTypeRequiredDescription
selectionContextSelection | undefinedOptionalSee the type definition and usage context.
triggerCharsnumber | undefinedOptionalSee the type definition and usage context.
targetInputCharsnumber | undefinedOptionalSee the type definition and usage context.
targetInputTokensnumber | undefinedOptionalSee the type definition and usage context.
triggerTokensnumber | undefinedOptionalSee the type definition and usage context.
stepsreadonly ContextReduction[] | undefinedOptionalRead-only. See the type definition and usage context.
compressorsreadonly ContextCompressor[] | undefinedOptionalRead-only. See the type definition and usage context.
selectorsreadonly ContextSelector[] | undefinedOptionalRead-only. See the type definition and usage context.

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

Branch 1
FieldTypeRequiredDescription
fragmentPick<ContextFragment, "id" | "maxChars">RequiredRead-only. Reuse the registered fragment identity and its existing hard size allowance.
fieldstringRequiredRead-only. See the type definition and usage context.
preset"compact" | "balanced" | "extended" | undefinedOptionalRead-only. See the type definition and usage context.
targetCharsnumber | undefinedOptionalRead-only. See the type definition and usage context.
keepGroupsnumber | undefinedOptionalRead-only. See the type definition and usage context.
groupingContextHistoryGrouping | undefinedOptionalRead-only. See the type definition and usage context.
selectionContextSelection | undefinedOptionalSee the type definition and usage context.
triggerCharsnumber | undefinedOptionalSee the type definition and usage context.
targetInputCharsnumber | undefinedOptionalSee the type definition and usage context.
targetInputTokensnumber | undefinedOptionalSee the type definition and usage context.
triggerTokensnumber | undefinedOptionalSee the type definition and usage context.
stepsreadonly ContextReduction[] | undefinedOptionalRead-only. See the type definition and usage context.
compressorsreadonly ContextCompressor[] | undefinedOptionalRead-only. See the type definition and usage context.
selectorsreadonly ContextSelector[] | undefinedOptionalRead-only. See the type definition and usage context.
kind"recent"RequiredRead-only. See the type definition and usage context.
compressorundefinedOptionalRead-only. See the type definition and usage context.
Branch 2
FieldTypeRequiredDescription
fragmentPick<ContextFragment, "id" | "maxChars">RequiredRead-only. Reuse the registered fragment identity and its existing hard size allowance.
fieldstringRequiredRead-only. See the type definition and usage context.
preset"compact" | "balanced" | "extended" | undefinedOptionalRead-only. See the type definition and usage context.
targetCharsnumber | undefinedOptionalRead-only. See the type definition and usage context.
keepGroupsnumber | undefinedOptionalRead-only. See the type definition and usage context.
groupingContextHistoryGrouping | undefinedOptionalRead-only. See the type definition and usage context.
selectionContextSelection | undefinedOptionalSee the type definition and usage context.
triggerCharsnumber | undefinedOptionalSee the type definition and usage context.
targetInputCharsnumber | undefinedOptionalSee the type definition and usage context.
targetInputTokensnumber | undefinedOptionalSee the type definition and usage context.
triggerTokensnumber | undefinedOptionalSee the type definition and usage context.
stepsreadonly ContextReduction[] | undefinedOptionalRead-only. See the type definition and usage context.
compressorsreadonly ContextCompressor[] | undefinedOptionalRead-only. See the type definition and usage context.
selectorsreadonly ContextSelector[] | undefinedOptionalRead-only. See the type definition and usage context.
kind"continuity"RequiredRead-only. See the type definition and usage context.
compressorContextCompressorRequiredRead-only. See the type definition and usage context.
Branch 3
FieldTypeRequiredDescription
selectionContextSelection | undefinedOptionalSee the type definition and usage context.
triggerCharsnumber | undefinedOptionalSee the type definition and usage context.
targetInputCharsnumber | undefinedOptionalSee the type definition and usage context.
targetInputTokensnumber | undefinedOptionalSee the type definition and usage context.
triggerTokensnumber | undefinedOptionalSee the type definition and usage context.
stepsreadonly ContextReduction[]RequiredRead-only. See the type definition and usage context.
compressorsreadonly ContextCompressor[] | undefinedOptionalRead-only. See the type definition and usage context.
selectorsreadonly ContextSelector[] | undefinedOptionalRead-only. See the type definition and usage context.
fragmentPick<ContextFragment, "id" | "maxChars"> | undefinedOptionalRead-only. Reuse the registered fragment identity and its existing hard size allowance.
fieldstring | undefinedOptionalRead-only. See the type definition and usage context.
preset"compact" | "balanced" | "extended" | undefinedOptionalRead-only. See the type definition and usage context.
targetCharsnumber | undefinedOptionalRead-only. See the type definition and usage context.
keepGroupsnumber | undefinedOptionalRead-only. See the type definition and usage context.
groupingContextHistoryGrouping | undefinedOptionalRead-only. See the type definition and usage context.
kind"recent" | "continuity" | undefinedOptionalRead-only. See the type definition and usage context.
compressorContextCompressor | undefinedOptionalRead-only. See the type definition and usage context.
Branch 4

readonly ContextStrategyOptions[]

Returns: ContextStrategy.

Return value structure
FieldTypeRequiredDescription
policyContextPolicyRequiredRead-only. Reduction policy with the generated or explicit steps and selection.
compressorsreadonly ContextCompressor[]RequiredRead-only. Compressors referenced by generated compression steps, deduplicated by identity.
selectorsreadonly ContextSelector[]RequiredRead-only. Selectors available to model-driven selection.

Examples ​

Reuse an existing fragment to generate a recent-history strategy ​

Beginner · Node.js

ts
import { Support } from '@jangts/agent-core';
const fragment = { id: 'history', maxChars: 4000 };
const strategy = Support.createContextStrategy({ kind: 'recent', fragment, field: 'messages' });
console.log(strategy.policy, strategy.compressors);
js
import { Support } from '@jangts/agent-core';
const fragment = { id: 'history', maxChars: 4000 };
const strategy = Support.createContextStrategy({ kind: 'recent', fragment, field: 'messages' });
console.log(strategy.policy, strategy.compressors);

Add kept groups and target size to the same configuration ​

Configuration · Node.js

ts
import { Support } from '@jangts/agent-core';
const fragment = { id: 'history', maxChars: 4000 };
const strategy = Support.createContextStrategy({ kind: 'recent', fragment, field: 'messages',
  keepGroups: 3, targetChars: 2500, grouping: { kind: 'user-turn' },
});
console.log(strategy.policy);
js
import { Support } from '@jangts/agent-core';
const fragment = { id: 'history', maxChars: 4000 };
const strategy = Support.createContextStrategy({ kind: 'recent', fragment, field: 'messages',
    keepGroups: 3, targetChars: 2500, grouping: { kind: 'user-turn' },
});
console.log(strategy.policy);

Supply a compression algorithm explicitly when old topics must be carried forward ​

Extension · Node.js

This example only assembles the strategy. The policy/compressors/selectors are used as configuration for existing context nodes; no new request is sent and no extra budget is created here.

ts
import { Support } from '@jangts/agent-core';
const compressor = Support.createContextCompressor({ id: 'history-summary',
  instruction: '按资料原意概括旧历史,保留事实、时间和未完成事项。',
  transport: 'core:plain-text', maxOutputTokens: 500,
  parse(value: unknown) { if (typeof value !== 'string' || !value.trim()) throw new TypeError('需要摘要'); return { messages: [{ role: 'user', content: value }] }; },
});
const strategy = Support.createContextStrategy({ kind: 'continuity',
  fragment: { id: 'history', maxChars: 4000 }, field: 'messages', compressor,
  targetChars: 2500, keepGroups: 2,
});
console.log(strategy.policy, strategy.compressors.map(item => item.id));
js
import { Support } from '@jangts/agent-core';
const compressor = Support.createContextCompressor({ id: 'history-summary',
    instruction: '按资料原意概括旧历史,保留事实、时间和未完成事项。',
    transport: 'core:plain-text', maxOutputTokens: 500,
    parse(value) { if (typeof value !== 'string' || !value.trim())
        throw new TypeError('需要摘要'); return { messages: [{ role: 'user', content: value }] }; },
});
const strategy = Support.createContextStrategy({ kind: 'continuity',
    fragment: { id: 'history', maxChars: 4000 }, field: 'messages', compressor,
    targetChars: 2500, keepGroups: 2,
});
console.log(strategy.policy, strategy.compressors.map(item => item.id));

API reference, usage examples and developer guides.