ContextReduction
SUPPORT · type · 类型合同
One declared reduction step.
导入
ts
import type { ContextReduction } from '@jangts/agent-core/support';js
/** @typedef {import('@jangts/agent-core/support').ContextReduction} ContextReduction */其他公开入口:@jangts/agent-core/support、@jangts/agent-core/support/context。
定义
类型: ContextReduction
类型表达式
ts
type ContextReduction = | {
id: string;
kind: 'trim';
fragmentId: string;
field: string;
targetChars: number;
grouping: ContextHistoryGrouping;
keepGroups: number;
}
/** Rewrites one fragment through a registered compressor down to `targetChars`. */
| ({
id: string;
kind: 'compress';
fragmentId: string;
targetChars: number;
/** Recent history the compressor must preserve verbatim. */
preserve?: ContextRecentHistory;
} & ContextHandlerReference);联合分支结构
ContextReduction的可选结构: 选择其中一种结构,各分支的字段要求分别列出。
分支 1
| 字段 | 类型 | 必需 | 说明 |
|---|---|---|---|
id | string | 必需 | 见类型定义及调用场景。 |
kind | "trim" | 必需 | 见类型定义及调用场景。 |
fragmentId | string | 必需 | 见类型定义及调用场景。 |
field | string | 必需 | 见类型定义及调用场景。 |
targetChars | number | 必需 | 见类型定义及调用场景。 |
grouping | ContextHistoryGrouping | 必需 | 见类型定义及调用场景。 |
keepGroups | number | 必需 | 见类型定义及调用场景。 |
分支 2
| 字段 | 类型 | 必需 | 说明 |
|---|---|---|---|
id | string | 必需 | 见类型定义及调用场景。 |
kind | "compress" | 必需 | 见类型定义及调用场景。 |
fragmentId | string | 必需 | 见类型定义及调用场景。 |
targetChars | number | 必需 | 见类型定义及调用场景。 |
preserve | ContextRecentHistory | undefined | 可省略 | Recent history the compressor must preserve verbatim. |
handlerId | string | 必需 | 只读。Registered handler id; an unknown id is rejected before execution. |
使用示例
- 上下文策略:验证、缩减与审计:Support 给出可替换的策略实现,Core 保留最终输入合同和预算检查。
显式使用只校验策略
入门 · Node.js
ts
import { Support } from '@jangts/agent-core';
import type { ContextPolicy } from '@jangts/agent-core/support/context';
const policy: ContextPolicy = { mode: 'validate' };
Support.validateContextPolicy(policy, [], []);
console.log(policy.mode);js
import { Support } from '@jangts/agent-core';
const policy = { mode: 'validate' };
Support.validateContextPolicy(policy, [], []);
console.log(policy.mode);从真实业务片段配置缩减顺序
配置 · Node.js
这些数字是示例业务的显式选择,不是 SDK 推荐适用于所有模型的容量。
ts
import { Support } from '@jangts/agent-core';
import type { ContextPolicy } from '@jangts/agent-core/support/context';
const policy: ContextPolicy = { mode: 'reduce', triggerChars: 1800, targetInputChars: 1500,
selection: { mode: 'sequence' },
steps: [{ id: 'recent-history', kind: 'trim', fragmentId: 'history', field: 'messages',
grouping: { kind: 'user-turn' }, keepGroups: 2, targetChars: 1000 }],
};
Support.validateContextPolicy(policy, [], []);
console.log(policy.steps);js
import { Support } from '@jangts/agent-core';
const policy = { mode: 'reduce', triggerChars: 1800, targetInputChars: 1500,
selection: { mode: 'sequence' },
steps: [{ id: 'recent-history', kind: 'trim', fragmentId: 'history', field: 'messages',
grouping: { kind: 'user-turn' }, keepGroups: 2, targetChars: 1000 }],
};
Support.validateContextPolicy(policy, [], []);
console.log(policy.steps);