Skip to content
English

projectContinuousExcerpt ​

KIT · function · 运行时值

Project a continuous excerpt of a body by binary-searching the longest prefix that fits.

Binary-search a continuous prefix and keep an existing surrogate pair intact. render receives the prefix length, not the text, so the caller decides how the excerpt is framed.

导入 ​

ts
import { projectContinuousExcerpt } from '@jangts/agent-core/kits/projection';
js
import { projectContinuousExcerpt } from '@jangts/agent-core/kits/projection';

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

定义 ​

调用 ​

ts
projectContinuousExcerpt(body: string, limit: number, render: (length: number) => string): ContentProjection | undefined

Project a continuous excerpt of a body by binary-searching the longest prefix that fits.

Binary-search a continuous prefix and keep an existing surrogate pair intact. render receives the prefix length, not the text, so the caller decides how the excerpt is framed.

参数类型传递说明
bodystring必需—
limitnumber必需—
render(length: number) => string必需—

返回: ContentProjection \| undefined。

返回值的可选结构: 选择其中一种结构,各分支的字段要求分别列出。

分支 1

undefined

分支 2
字段类型必需说明
textstring必需只读。Rendered text that fits the requested budget.
truncatedboolean必需只读。True when anything was shortened or replaced by the fallback.

使用示例 ​

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

保留连续正文而非拆散段落 ​

扩展 · Node.js

render 的输出长度须随前缀长度单调增长,二分搜索才有意义。

ts
import { Projection } from '@jangts/agent-core/kits/projection';
const body = '一段连续的资料正文。'.repeat(100);
const excerpt = Projection.projectContinuousExcerpt(body, 120, length => JSON.stringify({ excerpt: body.slice(0, length) }));
if (excerpt) console.log(JSON.parse(excerpt.text));
js
import { Projection } from '@jangts/agent-core/kits/projection';
const body = '一段连续的资料正文。'.repeat(100);
const excerpt = Projection.projectContinuousExcerpt(body, 120, length => JSON.stringify({ excerpt: body.slice(0, length) }));
if (excerpt)
    console.log(JSON.parse(excerpt.text));

相关策略源码 ​

相关定义 ​

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