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
import { createContextStrategy } from '@jangts/agent-core/support';import { createContextStrategy } from '@jangts/agent-core/support';Other public entry points:@jangts/agent-core/support, @jangts/agent-core/support/context.
Definition
Call
createContextStrategy(options: ContextStrategyOptions | readonly ContextStrategyOptions[], settings?: ContextStrategySettings): ContextStrategyBuild 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.
| Parameter | Type | Passing | Description |
|---|---|---|---|
options | ContextStrategyOptions | readonly ContextStrategyOptions[] | Required | — |
settings | ContextStrategySettings | Optional | — |
Fields of settings
| Field | Type | Required | Description |
|---|---|---|---|
selection | ContextSelection | undefined | Optional | 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. |
targetInputTokens | number | undefined | Optional | See the type definition and usage context. |
triggerTokens | number | undefined | Optional | See the type definition and usage context. |
steps | readonly ContextReduction[] | undefined | Optional | Read-only. See the type definition and usage context. |
compressors | readonly ContextCompressor[] | undefined | Optional | Read-only. See the type definition and usage context. |
selectors | readonly ContextSelector[] | undefined | Optional | Read-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
| Field | Type | Required | Description |
|---|---|---|---|
fragment | Pick<ContextFragment, "id" | "maxChars"> | Required | Read-only. Reuse the registered fragment identity and its existing hard size allowance. |
field | string | Required | Read-only. See the type definition and usage context. |
preset | "compact" | "balanced" | "extended" | undefined | Optional | Read-only. See the type definition and usage context. |
targetChars | number | undefined | Optional | Read-only. See the type definition and usage context. |
keepGroups | number | undefined | Optional | Read-only. See the type definition and usage context. |
grouping | ContextHistoryGrouping | undefined | Optional | Read-only. See the type definition and usage context. |
selection | ContextSelection | undefined | Optional | 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. |
targetInputTokens | number | undefined | Optional | See the type definition and usage context. |
triggerTokens | number | undefined | Optional | See the type definition and usage context. |
steps | readonly ContextReduction[] | undefined | Optional | Read-only. See the type definition and usage context. |
compressors | readonly ContextCompressor[] | undefined | Optional | Read-only. See the type definition and usage context. |
selectors | readonly ContextSelector[] | undefined | Optional | Read-only. See the type definition and usage context. |
kind | "recent" | Required | Read-only. See the type definition and usage context. |
compressor | undefined | Optional | Read-only. See the type definition and usage context. |
Branch 2
| Field | Type | Required | Description |
|---|---|---|---|
fragment | Pick<ContextFragment, "id" | "maxChars"> | Required | Read-only. Reuse the registered fragment identity and its existing hard size allowance. |
field | string | Required | Read-only. See the type definition and usage context. |
preset | "compact" | "balanced" | "extended" | undefined | Optional | Read-only. See the type definition and usage context. |
targetChars | number | undefined | Optional | Read-only. See the type definition and usage context. |
keepGroups | number | undefined | Optional | Read-only. See the type definition and usage context. |
grouping | ContextHistoryGrouping | undefined | Optional | Read-only. See the type definition and usage context. |
selection | ContextSelection | undefined | Optional | 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. |
targetInputTokens | number | undefined | Optional | See the type definition and usage context. |
triggerTokens | number | undefined | Optional | See the type definition and usage context. |
steps | readonly ContextReduction[] | undefined | Optional | Read-only. See the type definition and usage context. |
compressors | readonly ContextCompressor[] | undefined | Optional | Read-only. See the type definition and usage context. |
selectors | readonly ContextSelector[] | undefined | Optional | Read-only. See the type definition and usage context. |
kind | "continuity" | Required | Read-only. See the type definition and usage context. |
compressor | ContextCompressor | Required | Read-only. See the type definition and usage context. |
Branch 3
| Field | Type | Required | Description |
|---|---|---|---|
selection | ContextSelection | undefined | Optional | 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. |
targetInputTokens | number | undefined | Optional | See the type definition and usage context. |
triggerTokens | number | undefined | Optional | See the type definition and usage context. |
steps | readonly ContextReduction[] | Required | Read-only. See the type definition and usage context. |
compressors | readonly ContextCompressor[] | undefined | Optional | Read-only. See the type definition and usage context. |
selectors | readonly ContextSelector[] | undefined | Optional | Read-only. See the type definition and usage context. |
fragment | Pick<ContextFragment, "id" | "maxChars"> | undefined | Optional | Read-only. Reuse the registered fragment identity and its existing hard size allowance. |
field | string | undefined | Optional | Read-only. See the type definition and usage context. |
preset | "compact" | "balanced" | "extended" | undefined | Optional | Read-only. See the type definition and usage context. |
targetChars | number | undefined | Optional | Read-only. See the type definition and usage context. |
keepGroups | number | undefined | Optional | Read-only. See the type definition and usage context. |
grouping | ContextHistoryGrouping | undefined | Optional | Read-only. See the type definition and usage context. |
kind | "recent" | "continuity" | undefined | Optional | Read-only. See the type definition and usage context. |
compressor | ContextCompressor | undefined | Optional | Read-only. See the type definition and usage context. |
Branch 4
readonly ContextStrategyOptions[]
Returns: ContextStrategy.
Return value structure
| Field | Type | Required | Description |
|---|---|---|---|
policy | ContextPolicy | Required | Read-only. Reduction policy with the generated or explicit steps and selection. |
compressors | readonly ContextCompressor[] | Required | Read-only. Compressors referenced by generated compression steps, deduplicated by identity. |
selectors | readonly ContextSelector[] | Required | Read-only. Selectors available to model-driven selection. |
Examples
- Generate processing strategies from history semantics: Reuse fragment identity and hard budgets to generate complete steps and references for recent or continuity.
Reuse an existing fragment to generate a recent-history strategy
Beginner · Node.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);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
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);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.
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));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));