TreeProjectionOptions
KIT · interface · 类型合同
Depth- and record-level traversal options for tree projection.
导入
ts
import type { TreeProjectionOptions } from '@jangts/agent-core/kits/projection';1
js
/** @typedef {import('@jangts/agent-core/kits/projection').TreeProjectionOptions} TreeProjectionOptions */1
其他公开入口:@jangts/agent-core/types、@jangts/agent-core/kits/projection、@jangts/agent-core/kits。
定义
ts
interface TreeProjectionOptions<Context>1
泛型参数
| 类型参数 | 约束 | 默认类型 |
|---|---|---|
Context | — | — |
字段结构
| 字段 | 类型 | 必需 | 说明 |
|---|---|---|---|
maxDepth | number | 必需 | 只读。Maximum included depth; deeper values are omitted. Must be a non-negative integer. |
omit | ((value: unknown, depth: number, context: Context) => boolean) | undefined | 可省略 | 只读。Drops a value when it returns true; must be synchronous boolean. |
visitRecord | ((value: Record<string, unknown>, depth: number, context: Context) => void) | undefined | 可省略 | 只读。Observes every retained record, for example to record which fields were kept. |
使用示例
- Projection:按预算投影内容:先缩短文本,再减少条目;源数据、投影结果和预算由 App 显式区分。
为同一份查询结果准备紧凑 JSON
入门 · Node.js
ts
import { Projection } from '@jangts/agent-core/kits/projection';
const projection = Projection.defineContentProjection({
counts: [10, 3, 1], lengths: [200, 80, 20],
render: value => JSON.stringify(value), fallback: () => '{}',
});
const result = projection.project({ value: { items: [{ title: '配置说明', body: '正文'.repeat(100) }] }, maxChars: 120 });
console.log(result.text, result.truncated);1
2
3
4
5
6
7
2
3
4
5
6
7
js
import { Projection } from '@jangts/agent-core/kits/projection';
const projection = Projection.defineContentProjection({
counts: [10, 3, 1], lengths: [200, 80, 20],
render: value => JSON.stringify(value), fallback: () => '{}',
});
const result = projection.project({ value: { items: [{ title: '配置说明', body: '正文'.repeat(100) }] }, maxChars: 120 });
console.log(result.text, result.truncated);1
2
3
4
5
6
7
2
3
4
5
6
7
相同数据先移除私有字段,再按预算投影
配置 · Node.js
ts
import { Projection } from '@jangts/agent-core/kits/projection';
const tree = Projection.defineTreeProjection({
maxDepth: 4,
visitRecord(record) { delete record.privateNote; },
});
const source = { items: [{ title: '配置说明', body: '正文'.repeat(100), privateNote: '不向模型提供' }] };
// 调用者明确选择副本所有权;visitor 不会碰原记录。
const publicData = tree.visit(structuredClone(source), undefined);
const compact = Projection.defineContentProjection({ counts: [10, 3, 1], lengths: [200, 80, 20], render: JSON.stringify, fallback: () => '{}' });
console.log(compact.project({ value: publicData, maxChars: 120 }));1
2
3
4
5
6
7
8
9
10
2
3
4
5
6
7
8
9
10
js
import { Projection } from '@jangts/agent-core/kits/projection';
const tree = Projection.defineTreeProjection({
maxDepth: 4,
visitRecord(record) { delete record.privateNote; },
});
const source = { items: [{ title: '配置说明', body: '正文'.repeat(100), privateNote: '不向模型提供' }] };
// 调用者明确选择副本所有权;visitor 不会碰原记录。
const publicData = tree.visit(structuredClone(source), undefined);
const compact = Projection.defineContentProjection({ counts: [10, 3, 1], lengths: [200, 80, 20], render: JSON.stringify, fallback: () => '{}' });
console.log(compact.project({ value: publicData, maxChars: 120 }));1
2
3
4
5
6
7
8
9
10
2
3
4
5
6
7
8
9
10