Skip to content
中文

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 ​

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

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

Definition ​

Call ​

ts
createContextCompressor(options: { id: string; instruction: string; modelId?: string; transport: string; maxOutputTokens: number; parse(value: unknown): ContextFragment["value"]; }): ContextCompressor

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.

ParameterTypePassingDescription
options{ id: string; instruction: string; modelId?: string; transport: string; maxOutputTokens: number; parse(value: unknown): ContextFragment["value"]; }Required—
Fields of options
FieldTypeRequiredDescription
idstringRequiredSee the type definition and usage context.
instructionstringRequiredSee the type definition and usage context.
modelIdstring | undefinedOptionalSee the type definition and usage context.
transportstringRequiredSee the type definition and usage context.
maxOutputTokensnumberRequiredSee the type definition and usage context.
parse(value: unknown) => ContextFragment["value"]RequiredSee the type definition and usage context.

Returns: ContextCompressor.

Return value structure
FieldTypeRequiredDescription
idstringRequiredSee the type definition and usage context.
maxCallsnumber | undefinedOptionalMaximum model calls through callModel, default 1; declare 0 for local compression.
compress(call: ContextCompressionCall, context: ExecutionContext) => Promise<ContextFragment["value"]>RequiredProduces a replacement value no larger than the requested target where possible.

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.