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.
createContextCompressor requires a business instruction, a registered transport ID, an output reservation, and result parse; Core does not decide how to summarize business material. createContextSelector has a built-in selection instruction and a default {stepIds} output protocol, defaults maxExcerptChars=0, and passes only candidate metadata; a custom parser must also provide a corresponding instruction.
A handler can also be implemented directly by an App object or class, and declaring maxCalls=0 means purely local processing. A model handler should use callModel from the call arguments rather than creating a new Runner that bypasses ancestor budgets, cancellation, and logging. Algorithms are replaceable, but protected ranges, output structures, and final budgets are not cancelled by custom implementations.
Creating a handler only forms configuration and does not automatically register a transport or enter a Workflow; a node's compressors/selectors are wired explicitly, and policy refers to them by handlerId.
Progressive examples for the same business case
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 {};