ContextPolicy
SUPPORT · type · 类型合同
Declared context policy for one stage.
导入
ts
import type { ContextPolicy } from '@jangts/agent-core/support';js
/** @typedef {import('@jangts/agent-core/support').ContextPolicy} ContextPolicy */其他公开入口:@jangts/agent-core/support、@jangts/agent-core/support/context。
定义
类型: ContextPolicy
类型表达式
ts
type ContextPolicy = | { mode: 'validate' }
/**
* Reduce content when a trigger is exceeded.
*
* Character and token triggers/targets may be combined; `targetInputChars`/`targetInputTokens`
* define the size the reduction must reach.
*/
| {
mode: 'reduce';
triggerChars?: number;
targetInputChars?: number;
triggerTokens?: number;
targetInputTokens?: number;
selection?: ContextSelection;
steps: readonly ContextReduction[];
}
/** Verify that only the listed fragments changed value, without reducing anything. */
| { mode: 'audit'; mutableFragmentIds: readonly string[] };联合分支结构
ContextPolicy的可选结构: 选择其中一种结构,各分支的字段要求分别列出。
分支 1
| 字段 | 类型 | 必需 | 说明 |
|---|---|---|---|
mode | "validate" | 必需 | 见类型定义及调用场景。 |
分支 2
| 字段 | 类型 | 必需 | 说明 |
|---|---|---|---|
mode | "reduce" | 必需 | 见类型定义及调用场景。 |
triggerChars | number | undefined | 可省略 | 见类型定义及调用场景。 |
targetInputChars | number | undefined | 可省略 | 见类型定义及调用场景。 |
triggerTokens | number | undefined | 可省略 | 见类型定义及调用场景。 |
targetInputTokens | number | undefined | 可省略 | 见类型定义及调用场景。 |
selection | ContextSelection | undefined | 可省略 | 见类型定义及调用场景。 |
steps | readonly ContextReduction[] | 必需 | 见类型定义及调用场景。 |
分支 3
| 字段 | 类型 | 必需 | 说明 |
|---|---|---|---|
mode | "audit" | 必需 | 见类型定义及调用场景。 |
mutableFragmentIds | readonly string[] | 必需 | 见类型定义及调用场景。 |
使用示例
- 上下文策略:验证、缩减与审计: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);