Skip to content
English

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
字段类型必需说明
idstring必需见类型定义及调用场景。
kind"trim"必需见类型定义及调用场景。
fragmentIdstring必需见类型定义及调用场景。
fieldstring必需见类型定义及调用场景。
targetCharsnumber必需见类型定义及调用场景。
groupingContextHistoryGrouping必需见类型定义及调用场景。
keepGroupsnumber必需见类型定义及调用场景。
分支 2
字段类型必需说明
idstring必需见类型定义及调用场景。
kind"compress"必需见类型定义及调用场景。
fragmentIdstring必需见类型定义及调用场景。
targetCharsnumber必需见类型定义及调用场景。
preserveContextRecentHistory | undefined可省略Recent history the compressor must preserve verbatim.
handlerIdstring必需只读。Registered handler id; an unknown id is rejected before execution.

使用示例 ​

显式使用只校验策略 ​

入门 · 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);

相关策略源码 ​

相关定义 ​

接口参考、使用示例与开发指南。