createContextSelector
SUPPORT · function · Runtime value
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
import { createContextSelector } from '@jangts/agent-core/support';import { createContextSelector } from '@jangts/agent-core/support';Other public entry points:@jangts/agent-core/support, @jangts/agent-core/support/context.
Definition
Call
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.
| Parameter | Type | Passing | Description |
|---|---|---|---|
options | { id: string; transport: string; modelId?: string; maxOutputTokens: number; maxExcerptChars?: number; } & ({ instruction?: string; parse?: undefined; } | { instruction: string; parse(value: unknown): readonly string[]; }) | Required | — |
Optional shapes of Parameter options: choose one shape; required fields for each branch are listed separately.
Branch 1
| Field | Type | Required | Description |
|---|---|---|---|
id | string | Required | See the type definition and usage context. |
transport | string | Required | See the type definition and usage context. |
modelId | string | undefined | Optional | See the type definition and usage context. |
maxOutputTokens | number | Required | See the type definition and usage context. |
maxExcerptChars | number | undefined | Optional | Total excerpt budget across reducible fragments; default 0 sends metadata only. |
instruction | string | undefined | Optional | See the type definition and usage context. |
parse | undefined | Optional | See the type definition and usage context. |
Branch 2
| Field | Type | Required | Description |
|---|---|---|---|
id | string | Required | See the type definition and usage context. |
transport | string | Required | See the type definition and usage context. |
modelId | string | undefined | Optional | See the type definition and usage context. |
maxOutputTokens | number | Required | See the type definition and usage context. |
maxExcerptChars | number | undefined | Optional | Total excerpt budget across reducible fragments; default 0 sends metadata only. |
instruction | string | Required | See the type definition and usage context. |
parse | (value: unknown) => readonly string[] | Required | See the type definition and usage context. |
Returns: ContextSelector.
Return value structure
| Field | Type | Required | Description |
|---|---|---|---|
id | string | Required | See the type definition and usage context. |
maxCalls | number | undefined | Optional | Maximum model calls made through callModel, default 1; 0 permits a local selector. |
select | (call: ContextSelectionCall, context: ExecutionContext) => Promise<readonly string[]> | Required | Returns selected reduction ids; unknown ids are rejected. |
Examples
- Compressors and selectors: recommended implementations and custom algorithms: Bind prompts, output parsing, and algorithms as handlers; model I/O goes through callModel of the same request.
Bind a text compression handler
Beginner · 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);A selector explicitly controls the visible excerpt range
Configuration · Node.js
selector-json must be registered by the App and return value={stepIds:string[]}; an ordinary text transport must not be treated as already parsed JSON. This example only shows handler assembly and sends no request.
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);Implement the same selection contract with a local class
Extension · Node.js
This policy does not call a model. It only suggests registered candidates, and the node still performs execution and checks.
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 {};