Generate processing strategies from history semantics
Reuse fragment identity and hard budgets to generate complete steps and references for recent or continuity.
recent generates a whole-round history window step; continuity uses the supplied compressor to generate a compression step that combines an old-round summary with recent original text. It returns {policy, compressors, selectors}, which can be configured directly into context Workflow nodes.
The optional preset is compact, balanced, or extended: target character counts are 25%, 50%, and 75% of fragment.maxChars respectively, keeping 1, 3, and 6 groups of full original text; these ratios express character space, not semantic fidelity. Explicit targetChars and keepGroups take precedence. When no preset is selected, the default targetChars is the existing fragment.maxChars, grouping uses user-turn, and keepGroups is 1; the selection default is fixed to the trim or compress that corresponds to the scenario. When more precise constraints are needed, override them in the same options rather than returning to another configuration style. Strategy generation produces .window/.summary IDs, fragmentId, and handlerId so these relationships do not need to be hand-configured repeatedly.
An array of strategies can also be passed, with shared selection, selectors, compressors, steps, and triggerChars/targetInputChars/triggerTokens/targetInputTokens configured in the second argument. selection supports model and custom selectors; explicit steps fully replace the generated steps and their original dependencies, and generated fields that are no longer used, such as kind/fragment/field/compressor, can be omitted. By default the strategy is selected by final steps: fixed for a single step and sequence for multiple steps.
These are Support scenario completions, not Core rules for all history, and they do not guarantee the semantic quality of summaries. The fragment must come from a real registration in the App, and budgets and retention of recent original text still go through final checks.
Progressive examples for the same business case
Reuse an existing fragment to generate a recent-history strategy
Beginner · Node.js
import { Support } from '@jangts/agent-core';
const fragment = { id: 'history', maxChars: 4000 };
const strategy = Support.createContextStrategy({ kind: 'recent', fragment, field: 'messages' });
console.log(strategy.policy, strategy.compressors);import { Support } from '@jangts/agent-core';
const fragment = { id: 'history', maxChars: 4000 };
const strategy = Support.createContextStrategy({ kind: 'recent', fragment, field: 'messages' });
console.log(strategy.policy, strategy.compressors);Add kept groups and target size to the same configuration
Configuration · Node.js
import { Support } from '@jangts/agent-core';
const fragment = { id: 'history', maxChars: 4000 };
const strategy = Support.createContextStrategy({ kind: 'recent', fragment, field: 'messages',
keepGroups: 3, targetChars: 2500, grouping: { kind: 'user-turn' },
});
console.log(strategy.policy);import { Support } from '@jangts/agent-core';
const fragment = { id: 'history', maxChars: 4000 };
const strategy = Support.createContextStrategy({ kind: 'recent', fragment, field: 'messages',
keepGroups: 3, targetChars: 2500, grouping: { kind: 'user-turn' },
});
console.log(strategy.policy);Supply a compression algorithm explicitly when old topics must be carried forward
Extension · Node.js
This example only assembles the strategy. The policy/compressors/selectors are used as configuration for existing context nodes; no new request is sent and no extra budget is created here.
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 { messages: [{ role: 'user', content: value }] }; },
});
const strategy = Support.createContextStrategy({ kind: 'continuity',
fragment: { id: 'history', maxChars: 4000 }, field: 'messages', compressor,
targetChars: 2500, keepGroups: 2,
});
console.log(strategy.policy, strategy.compressors.map(item => item.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 { messages: [{ role: 'user', content: value }] }; },
});
const strategy = Support.createContextStrategy({ kind: 'continuity',
fragment: { id: 'history', maxChars: 4000 }, field: 'messages', compressor,
targetChars: 2500, keepGroups: 2,
});
console.log(strategy.policy, strategy.compressors.map(item => item.id));