createContextSelector
SUPPORT · function · 运行时值
Build a selector that asks a model to pick reduction steps.
Without a custom parser the model must answer with a JSON object containing a stepIds array; with a custom parser the application owns that contract but must still return step IDs.
导入
import { createContextSelector } from '@jangts/agent-core/support';import { createContextSelector } from '@jangts/agent-core/support';其他公开入口:@jangts/agent-core/support、@jangts/agent-core/support/context。
定义
调用
createContextSelector(options: { id: string; transport: string; modelId?: string; maxOutputTokens: number; maxExcerptChars?: number; } & ({ instruction?: string; parse?: undefined; } | { instruction: string; parse(value: unknown): readonly string[]; })): ContextSelectorBuild a selector that asks a model to pick reduction steps.
Without a custom parser the model must answer with a JSON object containing a stepIds array; with a custom parser the application owns that contract but must still return step IDs.
| 参数 | 类型 | 传递 | 说明 |
|---|---|---|---|
options | { id: string; transport: string; modelId?: string; maxOutputTokens: number; maxExcerptChars?: number; } & ({ instruction?: string; parse?: undefined; } | { instruction: string; parse(value: unknown): readonly string[]; }) | 必需 | — |
参数 options的可选结构: 选择其中一种结构,各分支的字段要求分别列出。
分支 1
| 字段 | 类型 | 必需 | 说明 |
|---|---|---|---|
id | string | 必需 | 见类型定义及调用场景。 |
transport | string | 必需 | 见类型定义及调用场景。 |
modelId | string | undefined | 可省略 | 见类型定义及调用场景。 |
maxOutputTokens | number | 必需 | 见类型定义及调用场景。 |
maxExcerptChars | number | undefined | 可省略 | Total excerpt budget across reducible fragments; default 0 sends metadata only. |
instruction | string | undefined | 可省略 | 见类型定义及调用场景。 |
parse | undefined | 可省略 | 见类型定义及调用场景。 |
分支 2
| 字段 | 类型 | 必需 | 说明 |
|---|---|---|---|
id | string | 必需 | 见类型定义及调用场景。 |
transport | string | 必需 | 见类型定义及调用场景。 |
modelId | string | undefined | 可省略 | 见类型定义及调用场景。 |
maxOutputTokens | number | 必需 | 见类型定义及调用场景。 |
maxExcerptChars | number | undefined | 可省略 | Total excerpt budget across reducible fragments; default 0 sends metadata only. |
instruction | string | 必需 | 见类型定义及调用场景。 |
parse | (value: unknown) => readonly string[] | 必需 | 见类型定义及调用场景。 |
返回: ContextSelector。
返回值结构
| 字段 | 类型 | 必需 | 说明 |
|---|---|---|---|
id | string | 必需 | 见类型定义及调用场景。 |
maxCalls | number | undefined | 可省略 | Maximum model calls made through callModel, default 1; 0 permits a local selector. |
select | (call: ContextSelectionCall, context: ExecutionContext) => Promise<readonly string[]> | 必需 | Returns selected reduction ids; unknown ids are rejected. |
使用示例
- 压缩器与选择器:推荐实现和自定义算法:将提示词、输出解析和算法绑定为 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 {};