createContextCompressor
SUPPORT · function · Runtime value
Build a compressor that asks a model to rewrite one fragment.
The request pairs a system instruction with a user message carrying the fragment ID, its current value and the target character count. Only the model's parsed value is returned; the caller decides whether the result fits the reduction contract.
Import
import { createContextCompressor } from '@jangts/agent-core/support';import { createContextCompressor } from '@jangts/agent-core/support';Other public entry points:@jangts/agent-core/support, @jangts/agent-core/support/context.
Definition
Call
createContextCompressor(options: { id: string; instruction: string; modelId?: string; transport: string; maxOutputTokens: number; parse(value: unknown): ContextFragment["value"]; }): ContextCompressorBuild a compressor that asks a model to rewrite one fragment.
The request pairs a system instruction with a user message carrying the fragment ID, its current value and the target character count. Only the model's parsed value is returned; the caller decides whether the result fits the reduction contract.
| Parameter | Type | Passing | Description |
|---|---|---|---|
options | { id: string; instruction: string; modelId?: string; transport: string; maxOutputTokens: number; parse(value: unknown): ContextFragment["value"]; } | Required | — |
Fields of options
| Field | Type | Required | Description |
|---|---|---|---|
id | string | Required | See the type definition and usage context. |
instruction | string | Required | See the type definition and usage context. |
modelId | string | undefined | Optional | See the type definition and usage context. |
transport | string | Required | See the type definition and usage context. |
maxOutputTokens | number | Required | See the type definition and usage context. |
parse | (value: unknown) => ContextFragment["value"] | Required | See the type definition and usage context. |
Returns: ContextCompressor.
Return value structure
| Field | Type | Required | Description |
|---|---|---|---|
id | string | Required | See the type definition and usage context. |
maxCalls | number | undefined | Optional | Maximum model calls through callModel, default 1; declare 0 for local compression. |
compress | (call: ContextCompressionCall, context: ExecutionContext) => Promise<ContextFragment["value"]> | Required | Produces a replacement value no larger than the requested target where possible. |
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 {};