ContentProjectionOptions
KIT · interface · Type contract
Candidate strategy for content projection.
Import
ts
import type { ContentProjectionOptions } from '@jangts/agent-core/kits/projection';1
js
/** @typedef {import('@jangts/agent-core/kits/projection').ContentProjectionOptions} ContentProjectionOptions */1
Other public entry points:@jangts/agent-core/types, @jangts/agent-core/kits/projection, @jangts/agent-core/kits.
Definition
ts
interface ContentProjectionOptions1
Field structure
| Field | Type | Required | Description |
|---|---|---|---|
counts | readonly number[] | Required | Read-only. Maximum array items to keep, tried in order before moving to the next length. |
lengths | readonly number[] | Required | Read-only. Maximum characters per string, tried in order inside each count candidate. |
render | (excerpt: unknown) => string | Required | Read-only. Renders a shortened value; must be synchronous. |
fallback | (limit: number) => string | Required | Read-only. Builds fallback text for a limit, used when no candidate fits; must be synchronous. |
Member index
| Member | Purpose |
|---|---|
render | Renders a shortened value; must be synchronous. |
fallback | Builds fallback text for a limit, used when no candidate fits; must be synchronous. |
Member details
render
Renders a shortened value; must be synchronous.
ts
ContentProjectionOptions.render(excerpt: unknown): string1
| Parameter | Type | Passing | Description |
|---|---|---|---|
excerpt | unknown | Required | — |
Returns: string.
fallback
Builds fallback text for a limit, used when no candidate fits; must be synchronous.
ts
ContentProjectionOptions.fallback(limit: number): string1
| Parameter | Type | Passing | Description |
|---|---|---|---|
limit | number | Required | — |
Returns: string.
Examples
- Projection: project content under a budget: Shorten text first, then reduce entries; source data, projection result, and budget are explicitly separated by the App.
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);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
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 }));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