Skip to content
English

ContentProjectionOptions ​

KIT · interface · 类型合同

Candidate strategy for content projection.

导入 ​

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

其他公开入口:@jangts/agent-core/types、@jangts/agent-core/kits/projection、@jangts/agent-core/kits。

定义 ​

ts
interface ContentProjectionOptions

字段结构 ​

字段类型必需说明
countsreadonly number[]必需只读。Maximum array items to keep, tried in order before moving to the next length.
lengthsreadonly number[]必需只读。Maximum characters per string, tried in order inside each count candidate.
render(excerpt: unknown) => string必需只读。Renders a shortened value; must be synchronous.
fallback(limit: number) => string必需只读。Builds fallback text for a limit, used when no candidate fits; must be synchronous.

成员目录 ​

成员用途
renderRenders a shortened value; must be synchronous.
fallbackBuilds fallback text for a limit, used when no candidate fits; must be synchronous.

成员详情 ​

render ​

Renders a shortened value; must be synchronous.

ts
ContentProjectionOptions.render(excerpt: unknown): string
参数类型传递说明
excerptunknown必需—

返回: string。

fallback ​

Builds fallback text for a limit, used when no candidate fits; must be synchronous.

ts
ContentProjectionOptions.fallback(limit: number): string
参数类型传递说明
limitnumber必需—

返回: string。

使用示例 ​

为同一份查询结果准备紧凑 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);
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);

相同数据先移除私有字段,再按预算投影 ​

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

相关策略源码 ​

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