Skip to content
中文

TreeProjectionOptions ​

KIT · interface · Type contract

Depth- and record-level traversal options for tree projection.

Import ​

ts
import type { TreeProjectionOptions } from '@jangts/agent-core/kits/projection';
js
/** @typedef {import('@jangts/agent-core/kits/projection').TreeProjectionOptions} TreeProjectionOptions */

Other public entry points:@jangts/agent-core/types, @jangts/agent-core/kits/projection, @jangts/agent-core/kits.

Definition ​

ts
interface TreeProjectionOptions<Context>

Type parameters ​

Type parameterConstraintDefault type
Context——

Field structure ​

FieldTypeRequiredDescription
maxDepthnumberRequiredRead-only. Maximum included depth; deeper values are omitted. Must be a non-negative integer.
omit((value: unknown, depth: number, context: Context) => boolean) | undefinedOptionalRead-only. Drops a value when it returns true; must be synchronous boolean.
visitRecord((value: Record<string, unknown>, depth: number, context: Context) => void) | undefinedOptionalRead-only. Observes every retained record, for example to record which fields were kept.

Examples ​

Prepare compact JSON for the same query result ​

Beginner · 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);
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);

Remove private fields from the same data, then project under budget ​

Configuration · 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 }));
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 }));

API reference, usage examples and developer guides.