Skip to content
中文

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 ​

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

Other public entry points:@jangts/agent-core/support, @jangts/agent-core/support/context.

Definition ​

Call ​

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.

ParameterTypePassingDescription
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
FieldTypeRequiredDescription
idstringRequiredSee the type definition and usage context.
transportstringRequiredSee the type definition and usage context.
modelIdstring | undefinedOptionalSee the type definition and usage context.
maxOutputTokensnumberRequiredSee the type definition and usage context.
maxExcerptCharsnumber | undefinedOptionalTotal excerpt budget across reducible fragments; default 0 sends metadata only.
instructionstring | undefinedOptionalSee the type definition and usage context.
parseundefinedOptionalSee the type definition and usage context.
Branch 2
FieldTypeRequiredDescription
idstringRequiredSee the type definition and usage context.
transportstringRequiredSee the type definition and usage context.
modelIdstring | undefinedOptionalSee the type definition and usage context.
maxOutputTokensnumberRequiredSee the type definition and usage context.
maxExcerptCharsnumber | undefinedOptionalTotal excerpt budget across reducible fragments; default 0 sends metadata only.
instructionstringRequiredSee the type definition and usage context.
parse(value: unknown) => readonly string[]RequiredSee the type definition and usage context.

Returns: ContextSelector.

Return value structure
FieldTypeRequiredDescription
idstringRequiredSee the type definition and usage context.
maxCallsnumber | undefinedOptionalMaximum model calls made through callModel, default 1; 0 permits a local selector.
select(call: ContextSelectionCall, context: ExecutionContext) => Promise<readonly string[]>RequiredReturns selected reduction ids; unknown ids are rejected.

Examples ​

Bind a text compression handler ​

Beginner · 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);

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.

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);

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.

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 {};

API reference, usage examples and developer guides.