Skip to content
English

createContextCompressor ​

SUPPORT · function · 运行时值

Build a compressor that asks a model to rewrite one fragment.

The request pairs a system instruction with a user message carrying the fragment ID, its current value and the target character count. Only the model's parsed value is returned; the caller decides whether the result fits the reduction contract.

导入 ​

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

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

定义 ​

调用 ​

ts
createContextCompressor(options: { id: string; instruction: string; modelId?: string; transport: string; maxOutputTokens: number; parse(value: unknown): ContextFragment["value"]; }): ContextCompressor

Build a compressor that asks a model to rewrite one fragment.

The request pairs a system instruction with a user message carrying the fragment ID, its current value and the target character count. Only the model's parsed value is returned; the caller decides whether the result fits the reduction contract.

参数类型传递说明
options{ id: string; instruction: string; modelId?: string; transport: string; maxOutputTokens: number; parse(value: unknown): ContextFragment["value"]; }必需—
options 的字段
字段类型必需说明
idstring必需见类型定义及调用场景。
instructionstring必需见类型定义及调用场景。
modelIdstring | undefined可省略见类型定义及调用场景。
transportstring必需见类型定义及调用场景。
maxOutputTokensnumber必需见类型定义及调用场景。
parse(value: unknown) => ContextFragment["value"]必需见类型定义及调用场景。

返回: ContextCompressor。

返回值结构
字段类型必需说明
idstring必需见类型定义及调用场景。
maxCallsnumber | undefined可省略Maximum model calls through callModel, default 1; declare 0 for local compression.
compress(call: ContextCompressionCall, context: ExecutionContext) => Promise<ContextFragment["value"]>必需Produces a replacement value no larger than the requested target where possible.

使用示例 ​

绑定一个文本压缩 handler ​

入门 · Node.js

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 value; },
});
console.log(compressor.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 value; },
});
console.log(compressor.id);

选择器明确控制可见摘录范围 ​

配置 · Node.js

selector-json 必须由 App 注册,返回 value={stepIds:string[]};不能把普通文本传输当成已解析 JSON。此例只展示 handler 装配,不发请求。

ts
import { Support } from '@jangts/agent-core';
const selector = Support.createContextSelector({ id: 'select-reduction',
  transport: 'selector-json', maxOutputTokens: 200, maxExcerptChars: 1000,
});
console.log(selector.id);
js
import { Support } from '@jangts/agent-core';
const selector = Support.createContextSelector({ id: 'select-reduction',
    transport: 'selector-json', maxOutputTokens: 200, maxExcerptChars: 1000,
});
console.log(selector.id);

用本地类实现同一选择合同 ​

扩展 · Node.js

这段策略不调用模型。它只建议已登记候选,仍由节点执行与检查。

ts
import type { ContextSelector, ContextSelectionCall } from '@jangts/agent-core/support/context';
class TrimFirst implements ContextSelector {
  readonly id = 'trim-first';
  readonly maxCalls = 0;
  async select(call: ContextSelectionCall) {
    const candidate = call.candidates.find(item => item.kind === 'trim');
    return candidate ? [candidate.id] : [];
  }
}
const selector = new TrimFirst();
console.log(selector.id, selector.maxCalls);
js
class TrimFirst {
    id = 'trim-first';
    maxCalls = 0;
    async select(call) {
        const candidate = call.candidates.find(item => item.kind === 'trim');
        return candidate ? [candidate.id] : [];
    }
}
const selector = new TrimFirst();
console.log(selector.id, selector.maxCalls);
export {};

相关策略源码 ​

相关定义 ​

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