Skip to content
中文

Projection ​

KIT · constant · Runtime value

Import ​

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

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

Definition ​

Member index ​

MemberPurpose
defineContentProjectionSee the overload below for full inputs and outputs.
projectContinuousExcerptSee the overload below for full inputs and outputs.
defineTreeProjectionSee the overload below for full inputs and outputs.

Member details ​

defineContentProjection ​

ts
Projection.defineContentProjection(options: ContentProjectionOptions): Readonly<{ project: ({ value, maxChars: limit, }: { value: unknown; maxChars: number; }) => ContentProjection; }>

Compile a JSON-preserving projector.

Keep valid JSON while reducing prose before dropping records; budgets are caller policy. The candidate list is tried in order (outer counts, inner lengths), so callers control whether trimming text or dropping items is preferred, and the configured fallback is used only when no candidate fits.

ParameterTypePassingDescription
optionsContentProjectionOptionsRequired—
Fields of options
FieldTypeRequiredDescription
countsreadonly number[]RequiredRead-only. Maximum array items to keep, tried in order before moving to the next length.
lengthsreadonly number[]RequiredRead-only. Maximum characters per string, tried in order inside each count candidate.
render(excerpt: unknown) => stringRequiredRead-only. Renders a shortened value; must be synchronous.
fallback(limit: number) => stringRequiredRead-only. Builds fallback text for a limit, used when no candidate fits; must be synchronous.

Returns: Readonly<{ project: ({ value, maxChars: limit, }: { value: unknown; maxChars: number; }) => ContentProjection; }>.

Return value structure
FieldTypeRequiredDescription
project({ value, maxChars: limit, }: { value: unknown; maxChars: number; }) => ContentProjectionRequiredSee the type definition and usage context.

projectContinuousExcerpt ​

ts
Projection.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.

ParameterTypePassingDescription
bodystringRequired—
limitnumberRequired—
render(length: number) => stringRequired—

Returns: ContentProjection \| undefined.

Optional shapes of Return value: choose one shape; required fields for each branch are listed separately.

Branch 1

undefined

Branch 2
FieldTypeRequiredDescription
textstringRequiredRead-only. Rendered text that fits the requested budget.
truncatedbooleanRequiredRead-only. True when anything was shortened or replaced by the fallback.

defineTreeProjection ​

ts
Projection.defineTreeProjection<Context>(options: TreeProjectionOptions<Context>): Readonly<{ visit: (value: unknown, context: Context) => unknown; }>

Compile a depth-limited tree transform.

Traverse synchronously in property/array order; only undefined omits a result. Because a visitor returns a new value instead of mutating, an omitted branch disappears from arrays and objects the same way, and maxDepth bounds both cycles and cost.

Type parameterConstraintDefault type
Context——
ParameterTypePassingDescription
optionsTreeProjectionOptions<Context>Required—
Fields of options
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.

Returns: Readonly<{ visit: (value: unknown, context: Context) => unknown; }>.

Return value structure
FieldTypeRequiredDescription
visit(value: unknown, context: Context) => unknownRequiredSee the type definition and usage context.

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

Keep continuous body text instead of splitting paragraphs ​

Extension · Node.js

The output length of render must grow monotonically with the prefix length for the binary search to be meaningful.

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

API reference, usage examples and developer guides.