Skip to content
English

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.

导入 ​

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

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

定义 ​

调用 ​

ts
createContextSelector(options: { id: string; transport: string; modelId?: string; maxOutputTokens: number; maxExcerptChars?: number; } & ({ instruction?: string; parse?: undefined; } | { instruction: string; parse(value: unknown): readonly string[]; })): ContextSelector

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.

参数类型传递说明
options{ id: string; transport: string; modelId?: string; maxOutputTokens: number; maxExcerptChars?: number; } & ({ instruction?: string; parse?: undefined; } | { instruction: string; parse(value: unknown): readonly string[]; })必需—

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

分支 1
字段类型必需说明
idstring必需见类型定义及调用场景。
transportstring必需见类型定义及调用场景。
modelIdstring | undefined可省略见类型定义及调用场景。
maxOutputTokensnumber必需见类型定义及调用场景。
maxExcerptCharsnumber | undefined可省略Total excerpt budget across reducible fragments; default 0 sends metadata only.
instructionstring | undefined可省略见类型定义及调用场景。
parseundefined可省略见类型定义及调用场景。
分支 2
字段类型必需说明
idstring必需见类型定义及调用场景。
transportstring必需见类型定义及调用场景。
modelIdstring | undefined可省略见类型定义及调用场景。
maxOutputTokensnumber必需见类型定义及调用场景。
maxExcerptCharsnumber | undefined可省略Total excerpt budget across reducible fragments; default 0 sends metadata only.
instructionstring必需见类型定义及调用场景。
parse(value: unknown) => readonly string[]必需见类型定义及调用场景。

返回: ContextSelector。

返回值结构
字段类型必需说明
idstring必需见类型定义及调用场景。
maxCallsnumber | 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 ​

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

相关策略源码 ​

相关定义 ​

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