Skip to content
English

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.

导入 ​

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

其他公开入口:@jangts/agent-core/support、@jangts/agent-core/support/context。

定义 ​

调用 ​

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.

参数类型传递说明
optionsContextStrategyOptions | readonly ContextStrategyOptions[]必需—
settingsContextStrategySettings可省略—
settings 的字段
字段类型必需说明
selectionContextSelection | undefined可省略见类型定义及调用场景。
triggerCharsnumber | undefined可省略见类型定义及调用场景。
targetInputCharsnumber | undefined可省略见类型定义及调用场景。
targetInputTokensnumber | undefined可省略见类型定义及调用场景。
triggerTokensnumber | undefined可省略见类型定义及调用场景。
stepsreadonly ContextReduction[] | undefined可省略只读。见类型定义及调用场景。
compressorsreadonly ContextCompressor[] | undefined可省略只读。见类型定义及调用场景。
selectorsreadonly ContextSelector[] | undefined可省略只读。见类型定义及调用场景。

参数 options的可选结构: 选择其中一种结构,各分支的字段要求分别列出。

分支 1
字段类型必需说明
fragmentPick<ContextFragment, "id" | "maxChars">必需只读。Reuse the registered fragment identity and its existing hard size allowance.
fieldstring必需只读。见类型定义及调用场景。
preset"compact" | "balanced" | "extended" | undefined可省略只读。见类型定义及调用场景。
targetCharsnumber | undefined可省略只读。见类型定义及调用场景。
keepGroupsnumber | undefined可省略只读。见类型定义及调用场景。
groupingContextHistoryGrouping | undefined可省略只读。见类型定义及调用场景。
selectionContextSelection | undefined可省略见类型定义及调用场景。
triggerCharsnumber | undefined可省略见类型定义及调用场景。
targetInputCharsnumber | undefined可省略见类型定义及调用场景。
targetInputTokensnumber | undefined可省略见类型定义及调用场景。
triggerTokensnumber | undefined可省略见类型定义及调用场景。
stepsreadonly ContextReduction[] | undefined可省略只读。见类型定义及调用场景。
compressorsreadonly ContextCompressor[] | undefined可省略只读。见类型定义及调用场景。
selectorsreadonly ContextSelector[] | undefined可省略只读。见类型定义及调用场景。
kind"recent"必需只读。见类型定义及调用场景。
compressorundefined可省略只读。见类型定义及调用场景。
分支 2
字段类型必需说明
fragmentPick<ContextFragment, "id" | "maxChars">必需只读。Reuse the registered fragment identity and its existing hard size allowance.
fieldstring必需只读。见类型定义及调用场景。
preset"compact" | "balanced" | "extended" | undefined可省略只读。见类型定义及调用场景。
targetCharsnumber | undefined可省略只读。见类型定义及调用场景。
keepGroupsnumber | undefined可省略只读。见类型定义及调用场景。
groupingContextHistoryGrouping | undefined可省略只读。见类型定义及调用场景。
selectionContextSelection | undefined可省略见类型定义及调用场景。
triggerCharsnumber | undefined可省略见类型定义及调用场景。
targetInputCharsnumber | undefined可省略见类型定义及调用场景。
targetInputTokensnumber | undefined可省略见类型定义及调用场景。
triggerTokensnumber | undefined可省略见类型定义及调用场景。
stepsreadonly ContextReduction[] | undefined可省略只读。见类型定义及调用场景。
compressorsreadonly ContextCompressor[] | undefined可省略只读。见类型定义及调用场景。
selectorsreadonly ContextSelector[] | undefined可省略只读。见类型定义及调用场景。
kind"continuity"必需只读。见类型定义及调用场景。
compressorContextCompressor必需只读。见类型定义及调用场景。
分支 3
字段类型必需说明
selectionContextSelection | undefined可省略见类型定义及调用场景。
triggerCharsnumber | undefined可省略见类型定义及调用场景。
targetInputCharsnumber | undefined可省略见类型定义及调用场景。
targetInputTokensnumber | undefined可省略见类型定义及调用场景。
triggerTokensnumber | undefined可省略见类型定义及调用场景。
stepsreadonly ContextReduction[]必需只读。见类型定义及调用场景。
compressorsreadonly ContextCompressor[] | undefined可省略只读。见类型定义及调用场景。
selectorsreadonly ContextSelector[] | undefined可省略只读。见类型定义及调用场景。
fragmentPick<ContextFragment, "id" | "maxChars"> | undefined可省略只读。Reuse the registered fragment identity and its existing hard size allowance.
fieldstring | undefined可省略只读。见类型定义及调用场景。
preset"compact" | "balanced" | "extended" | undefined可省略只读。见类型定义及调用场景。
targetCharsnumber | undefined可省略只读。见类型定义及调用场景。
keepGroupsnumber | undefined可省略只读。见类型定义及调用场景。
groupingContextHistoryGrouping | undefined可省略只读。见类型定义及调用场景。
kind"recent" | "continuity" | undefined可省略只读。见类型定义及调用场景。
compressorContextCompressor | undefined可省略只读。见类型定义及调用场景。
分支 4

readonly ContextStrategyOptions[]

返回: ContextStrategy。

返回值结构
字段类型必需说明
policyContextPolicy必需只读。Reduction policy with the generated or explicit steps and selection.
compressorsreadonly ContextCompressor[]必需只读。Compressors referenced by generated compression steps, deduplicated by identity.
selectorsreadonly ContextSelector[]必需只读。Selectors available to model-driven selection.

使用示例 ​

复用已有片段生成最近历史策略 ​

入门 · 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);

同一配置增加保留组数与目标大小 ​

配置 · 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);

需要承接旧话题时明确提供压缩算法 ​

扩展 · Node.js

本例只装配策略。将 policy/compressors/selectors 作为原上下文节点配置,不在这里发新请求或创建额外额度。

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

相关策略源码 ​

相关定义 ​

接口参考、使用示例与开发指南。