contextHistoryStrategies
SUPPORT · constant · Runtime value
Import
ts
import { contextHistoryStrategies } from '@jangts/agent-core/support';js
import { contextHistoryStrategies } from '@jangts/agent-core/support';Other public entry points:@jangts/agent-core/support, @jangts/agent-core/support/context.
Definition
ts
const contextHistoryStrategies: Readonly<{ compact: Readonly<{ targetRatio: 0.25; keepGroups: 1; }>; balanced: Readonly<{ targetRatio: 0.5; keepGroups: 3; }>; extended: Readonly<{ targetRatio: 0.75; keepGroups: 6; }>; }>Field structure
| Field | Type | Required | Description |
|---|---|---|---|
compact | Readonly<{ targetRatio: 0.25; keepGroups: 1; }> | Required | See the type definition and usage context. |
balanced | Readonly<{ targetRatio: 0.5; keepGroups: 3; }> | Required | See the type definition and usage context. |
extended | Readonly<{ targetRatio: 0.75; keepGroups: 6; }> | Required | See the type definition and usage context. |
Examples
- Generate processing strategies from history semantics: Reuse fragment identity and hard budgets to generate complete steps and references for recent or continuity.
Reuse an existing fragment to generate a recent-history strategy
Beginner · Node.js
ts
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);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);Add kept groups and target size to the same configuration
Configuration · Node.js
ts
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);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);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.
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 { 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));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 { 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));