createContextStrategy
SUPPORT · function · 运行时值
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 { createContextStrategy } from '@jangts/agent-core/support';import { createContextStrategy } from '@jangts/agent-core/support';其他公开入口:@jangts/agent-core/support、@jangts/agent-core/support/context。
定义
调用
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.
| 参数 | 类型 | 传递 | 说明 |
|---|---|---|---|
options | ContextStrategyOptions | readonly ContextStrategyOptions[] | 必需 | — |
settings | ContextStrategySettings | 可省略 | — |
settings 的字段
| 字段 | 类型 | 必需 | 说明 |
|---|---|---|---|
selection | ContextSelection | undefined | 可省略 | 见类型定义及调用场景。 |
triggerChars | number | undefined | 可省略 | 见类型定义及调用场景。 |
targetInputChars | number | undefined | 可省略 | 见类型定义及调用场景。 |
targetInputTokens | number | undefined | 可省略 | 见类型定义及调用场景。 |
triggerTokens | number | undefined | 可省略 | 见类型定义及调用场景。 |
steps | readonly ContextReduction[] | undefined | 可省略 | 只读。见类型定义及调用场景。 |
compressors | readonly ContextCompressor[] | undefined | 可省略 | 只读。见类型定义及调用场景。 |
selectors | readonly ContextSelector[] | undefined | 可省略 | 只读。见类型定义及调用场景。 |
参数 options的可选结构: 选择其中一种结构,各分支的字段要求分别列出。
分支 1
| 字段 | 类型 | 必需 | 说明 |
|---|---|---|---|
fragment | Pick<ContextFragment, "id" | "maxChars"> | 必需 | 只读。Reuse the registered fragment identity and its existing hard size allowance. |
field | string | 必需 | 只读。见类型定义及调用场景。 |
preset | "compact" | "balanced" | "extended" | undefined | 可省略 | 只读。见类型定义及调用场景。 |
targetChars | number | undefined | 可省略 | 只读。见类型定义及调用场景。 |
keepGroups | number | undefined | 可省略 | 只读。见类型定义及调用场景。 |
grouping | ContextHistoryGrouping | undefined | 可省略 | 只读。见类型定义及调用场景。 |
selection | ContextSelection | undefined | 可省略 | 见类型定义及调用场景。 |
triggerChars | number | undefined | 可省略 | 见类型定义及调用场景。 |
targetInputChars | number | undefined | 可省略 | 见类型定义及调用场景。 |
targetInputTokens | number | undefined | 可省略 | 见类型定义及调用场景。 |
triggerTokens | number | undefined | 可省略 | 见类型定义及调用场景。 |
steps | readonly ContextReduction[] | undefined | 可省略 | 只读。见类型定义及调用场景。 |
compressors | readonly ContextCompressor[] | undefined | 可省略 | 只读。见类型定义及调用场景。 |
selectors | readonly ContextSelector[] | undefined | 可省略 | 只读。见类型定义及调用场景。 |
kind | "recent" | 必需 | 只读。见类型定义及调用场景。 |
compressor | undefined | 可省略 | 只读。见类型定义及调用场景。 |
分支 2
| 字段 | 类型 | 必需 | 说明 |
|---|---|---|---|
fragment | Pick<ContextFragment, "id" | "maxChars"> | 必需 | 只读。Reuse the registered fragment identity and its existing hard size allowance. |
field | string | 必需 | 只读。见类型定义及调用场景。 |
preset | "compact" | "balanced" | "extended" | undefined | 可省略 | 只读。见类型定义及调用场景。 |
targetChars | number | undefined | 可省略 | 只读。见类型定义及调用场景。 |
keepGroups | number | undefined | 可省略 | 只读。见类型定义及调用场景。 |
grouping | ContextHistoryGrouping | undefined | 可省略 | 只读。见类型定义及调用场景。 |
selection | ContextSelection | undefined | 可省略 | 见类型定义及调用场景。 |
triggerChars | number | undefined | 可省略 | 见类型定义及调用场景。 |
targetInputChars | number | undefined | 可省略 | 见类型定义及调用场景。 |
targetInputTokens | number | undefined | 可省略 | 见类型定义及调用场景。 |
triggerTokens | number | undefined | 可省略 | 见类型定义及调用场景。 |
steps | readonly ContextReduction[] | undefined | 可省略 | 只读。见类型定义及调用场景。 |
compressors | readonly ContextCompressor[] | undefined | 可省略 | 只读。见类型定义及调用场景。 |
selectors | readonly ContextSelector[] | undefined | 可省略 | 只读。见类型定义及调用场景。 |
kind | "continuity" | 必需 | 只读。见类型定义及调用场景。 |
compressor | ContextCompressor | 必需 | 只读。见类型定义及调用场景。 |
分支 3
| 字段 | 类型 | 必需 | 说明 |
|---|---|---|---|
selection | ContextSelection | undefined | 可省略 | 见类型定义及调用场景。 |
triggerChars | number | undefined | 可省略 | 见类型定义及调用场景。 |
targetInputChars | number | undefined | 可省略 | 见类型定义及调用场景。 |
targetInputTokens | number | undefined | 可省略 | 见类型定义及调用场景。 |
triggerTokens | number | undefined | 可省略 | 见类型定义及调用场景。 |
steps | readonly ContextReduction[] | 必需 | 只读。见类型定义及调用场景。 |
compressors | readonly ContextCompressor[] | undefined | 可省略 | 只读。见类型定义及调用场景。 |
selectors | readonly ContextSelector[] | undefined | 可省略 | 只读。见类型定义及调用场景。 |
fragment | Pick<ContextFragment, "id" | "maxChars"> | undefined | 可省略 | 只读。Reuse the registered fragment identity and its existing hard size allowance. |
field | string | undefined | 可省略 | 只读。见类型定义及调用场景。 |
preset | "compact" | "balanced" | "extended" | undefined | 可省略 | 只读。见类型定义及调用场景。 |
targetChars | number | undefined | 可省略 | 只读。见类型定义及调用场景。 |
keepGroups | number | undefined | 可省略 | 只读。见类型定义及调用场景。 |
grouping | ContextHistoryGrouping | undefined | 可省略 | 只读。见类型定义及调用场景。 |
kind | "recent" | "continuity" | undefined | 可省略 | 只读。见类型定义及调用场景。 |
compressor | ContextCompressor | undefined | 可省略 | 只读。见类型定义及调用场景。 |
分支 4
readonly ContextStrategyOptions[]
返回: ContextStrategy。
返回值结构
| 字段 | 类型 | 必需 | 说明 |
|---|---|---|---|
policy | ContextPolicy | 必需 | 只读。Reduction policy with the generated or explicit steps and selection. |
compressors | readonly ContextCompressor[] | 必需 | 只读。Compressors referenced by generated compression steps, deduplicated by identity. |
selectors | readonly ContextSelector[] | 必需 | 只读。Selectors available to model-driven selection. |
使用示例
- 从历史语义生成处理策略:复用片段身份与硬预算,生成 recent 或 continuity 的完整步骤和引用。
复用已有片段生成最近历史策略
入门 · 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);同一配置增加保留组数与目标大小
配置 · 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);需要承接旧话题时明确提供压缩算法
扩展 · Node.js
本例只装配策略。将 policy/compressors/selectors 作为原上下文节点配置,不在这里发新请求或创建额外额度。
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));