validateContextPolicy
SUPPORT · function · 运行时值
Validate a complete context policy and every handler it can reference.
Handler IDs must be unique and each handler must declare a non-negative integer call ceiling. Reduction targets and selection modes are checked by their own catalog entries.
导入
ts
import { validateContextPolicy } from '@jangts/agent-core/support';js
import { validateContextPolicy } from '@jangts/agent-core/support';其他公开入口:@jangts/agent-core/support、@jangts/agent-core/support/context。
定义
调用
ts
validateContextPolicy(policy: ContextPolicy, compressors: readonly ContextCompressor[], selectors?: readonly ContextSelector[]): voidValidate a complete context policy and every handler it can reference.
Handler IDs must be unique and each handler must declare a non-negative integer call ceiling. Reduction targets and selection modes are checked by their own catalog entries.
| 参数 | 类型 | 传递 | 说明 |
|---|---|---|---|
policy | ContextPolicy | 必需 | — |
compressors | readonly ContextCompressor[] | 必需 | — |
selectors | readonly ContextSelector[] | 可省略 | — |
参数 policy的可选结构: 选择其中一种结构,各分支的字段要求分别列出。
分支 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[] | 必需 | 见类型定义及调用场景。 |
返回: void。
使用示例
- 上下文策略:验证、缩减与审计: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);对 App 自己的处理限定可变范围
扩展 · Node.js
审计还需要 ContextPlan 携带原片段;仅列 ID 不会自行修改或发布资料。
ts
import { Support } from '@jangts/agent-core';
import type { ContextPolicy } from '@jangts/agent-core/support/context';
const policy: ContextPolicy = { mode: 'audit', mutableFragmentIds: ['history'] };
Support.validateContextPolicy(policy, [], []);
console.log(policy.mutableFragmentIds);js
import { Support } from '@jangts/agent-core';
const policy = { mode: 'audit', mutableFragmentIds: ['history'] };
Support.validateContextPolicy(policy, [], []);
console.log(policy.mutableFragmentIds);