ContextSelector
SUPPORT · interface · 类型合同
Model or local selector that chooses which reductions to apply.
导入
ts
import type { ContextSelector } from '@jangts/agent-core/support';js
/** @typedef {import('@jangts/agent-core/support').ContextSelector} ContextSelector */其他公开入口:@jangts/agent-core/support、@jangts/agent-core/support/context。
定义
ts
interface 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. |
成员目录
| 成员 | 用途 |
|---|---|
select | Returns selected reduction ids; unknown ids are rejected. |
成员详情
select
Returns selected reduction ids; unknown ids are rejected.
ts
ContextSelector.select(call: ContextSelectionCall, context: ExecutionContext): Promise<readonly string[]>Returns selected reduction ids; unknown ids are rejected.
| 参数 | 类型 | 传递 | 说明 |
|---|---|---|---|
call | ContextSelectionCall | 必需 | — |
context | ExecutionContext | 必需 | — |
call 的字段
| 字段 | 类型 | 必需 | 说明 |
|---|---|---|---|
stageId | string | 必需 | 只读。见类型定义及调用场景。 |
candidates | readonly ContextSelectionCandidate[] | 必需 | Reducible candidates, already filtered and measured. |
fragments | readonly ContextFragment[] | 必需 | Only reducible fragments; never protected request or system fragments. |
currentTokens | number | undefined | 可省略 | Current input tokens, when a token budget applies. |
requiredReductionTokens | number | undefined | 可省略 | Tokens that must be removed to satisfy the token budget. |
currentChars | number | 必需 | 见类型定义及调用场景。 |
requiredReductionChars | number | 必需 | Characters that must be removed to satisfy the character budget. |
remainingCalls | number | 必需 | Calls the selector may still spend through callModel. |
callModel | ContextModelCall | 必需 | 见类型定义及调用场景。 |
context 的字段
| 字段 | 类型 | 必需 | 说明 |
|---|---|---|---|
logger | SubFlowLog | undefined | 可省略 | 只读。Explicit optional recorder for this operation; never inferred from diagnostics. |
signal | AbortSignal | 必需 | 只读。Non-optional cancellation signal; never undefined once execution has started. |
diagnostics | DiagnosticContext | undefined | 可省略 | 只读。Diagnostic scope of the enclosing call, when recording is enabled. |
返回: Promise<readonly string[]>。
等待后的值: readonly string[]。
使用示例
- 压缩器与选择器:推荐实现和自定义算法:将提示词、输出解析和算法绑定为 handler;模型 I/O 通过同一请求的 callModel。
绑定一个文本压缩 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);