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.
导入
import { createContextCompressor } from '@jangts/agent-core/support';import { createContextCompressor } from '@jangts/agent-core/support';其他公开入口:@jangts/agent-core/support、@jangts/agent-core/support/context。
定义
调用
createContextCompressor(options: { id: string; instruction: string; modelId?: string; transport: string; maxOutputTokens: number; parse(value: unknown): ContextFragment["value"]; }): ContextCompressorBuild 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 的字段
| 字段 | 类型 | 必需 | 说明 |
|---|---|---|---|
id | string | 必需 | 见类型定义及调用场景。 |
instruction | string | 必需 | 见类型定义及调用场景。 |
modelId | string | undefined | 可省略 | 见类型定义及调用场景。 |
transport | string | 必需 | 见类型定义及调用场景。 |
maxOutputTokens | number | 必需 | 见类型定义及调用场景。 |
parse | (value: unknown) => ContextFragment["value"] | 必需 | 见类型定义及调用场景。 |
返回: ContextCompressor。
返回值结构
| 字段 | 类型 | 必需 | 说明 |
|---|---|---|---|
id | string | 必需 | 见类型定义及调用场景。 |
maxCalls | number | 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;模型 I/O 通过同一请求的 callModel。
绑定一个文本压缩 handler
入门 · Node.js
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);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 装配,不发请求。
import { Support } from '@jangts/agent-core';
const selector = Support.createContextSelector({ id: 'select-reduction',
transport: 'selector-json', maxOutputTokens: 200, maxExcerptChars: 1000,
});
console.log(selector.id);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
这段策略不调用模型。它只建议已登记候选,仍由节点执行与检查。
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);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 {};