Skip to content
English

transformContextText ​

SUPPORT · function · 运行时值

Apply a text transform to a fragment value, including strings nested inside objects.

Structured values are walked through a JSON round trip, which is what the budget measurement serializes later; values that JSON cannot represent are therefore not preserved.

导入 ​

ts
import { transformContextText } from '@jangts/agent-core/support';
js
import { transformContextText } from '@jangts/agent-core/support';

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

定义 ​

调用 ​

ts
transformContextText(fragment: ContextFragment, transform: (text: string) => string): ContextFragment

Apply a text transform to a fragment value, including strings nested inside objects.

Structured values are walked through a JSON round trip, which is what the budget measurement serializes later; values that JSON cannot represent are therefore not preserved.

参数类型传递说明
fragmentContextFragment必需—
transform(text: string) => string必需—
fragment 的字段
字段类型必需说明
idstring必需Unique, stable id matching ^[a-z][a-zA-Z0-9:._-]*$; duplicates are rejected.
sourceIdstring必需Owning source (module or registration) used to explain where the content came from.
trustContextTrust必需Producer trust level. user and tool content cannot become a system message.
lifetimeContextLifetime必需Declared validity window of this content.
maxCharsnumber必需Hard upper bound in JSON characters; must be a positive safe integer.
role"system" | "user"必需Message slot the fragment occupies when assembled.
valuestring | Record<string, unknown>必需Rendered content. A system message accepts a string only, never a record.

返回: ContextFragment。

返回值结构
字段类型必需说明
idstring必需Unique, stable id matching ^[a-z][a-zA-Z0-9:._-]*$; duplicates are rejected.
sourceIdstring必需Owning source (module or registration) used to explain where the content came from.
trustContextTrust必需Producer trust level. user and tool content cannot become a system message.
lifetimeContextLifetime必需Declared validity window of this content.
maxCharsnumber必需Hard upper bound in JSON characters; must be a positive safe integer.
role"system" | "user"必需Message slot the fragment occupies when assembled.
valuestring | Record<string, unknown>必需Rendered content. A system message accepts a string only, never a record.

使用示例 ​

一次配置,以后只给问题和历史 ​

入门 · Node.js

ts
import { Support } from '@jangts/agent-core';
type Input = { question: string; history: readonly { role: string; content: string }[] };
const builder = Support.defineContextFragments<Input>([
  { id: 'question', sourceId: 'request', trust: 'user', lifetime: 'turn', maxChars: 1000,
    role: 'user', format: input => input.question },
  { id: 'history', sourceId: 'conversation', trust: 'user', lifetime: 'turn', maxChars: 10000,
    role: 'user', format: input => ({ messages: input.history }) },
], { maxInputChars: 1200 });
const context = builder.build({ input: { question: '服务状态?', history: [] } });
console.log(context.messages);
js
import { Support } from '@jangts/agent-core';
const builder = Support.defineContextFragments([
    { id: 'question', sourceId: 'request', trust: 'user', lifetime: 'turn', maxChars: 1000,
        role: 'user', format: input => input.question },
    { id: 'history', sourceId: 'conversation', trust: 'user', lifetime: 'turn', maxChars: 10000,
        role: 'user', format: input => ({ messages: input.history }) },
], { maxInputChars: 1200 });
const context = builder.build({ input: { question: '服务状态?', history: [] } });
console.log(context.messages);

同一个 builder 提前给出计划 ​

配置 · Node.js

ts
import { Support } from '@jangts/agent-core';
type Input = { question: string; history: readonly { role: string; content: string }[] };
const builder = Support.defineContextFragments<Input>([
  { id: 'question', sourceId: 'request', trust: 'user', lifetime: 'turn', maxChars: 1000,
    role: 'user', format: input => input.question },
  { id: 'history', sourceId: 'conversation', trust: 'user', lifetime: 'turn', maxChars: 10000,
    role: 'user', format: input => ({ messages: input.history }) },
], { maxInputChars: 1200 });
const input = { question: '服务状态?', history: [] };
const plan = builder.plan({ input });
const expanded = builder.fragments(input);
console.log(plan.maxInputChars, expanded.map(fragment => fragment.id));
const largerRequest = builder.build({ input, maxInputChars: 2000 });
console.log(largerRequest.maxInputChars);
js
import { Support } from '@jangts/agent-core';
const builder = Support.defineContextFragments([
    { id: 'question', sourceId: 'request', trust: 'user', lifetime: 'turn', maxChars: 1000,
        role: 'user', format: input => input.question },
    { id: 'history', sourceId: 'conversation', trust: 'user', lifetime: 'turn', maxChars: 10000,
        role: 'user', format: input => ({ messages: input.history }) },
], { maxInputChars: 1200 });
const input = { question: '服务状态?', history: [] };
const plan = builder.plan({ input });
const expanded = builder.fragments(input);
console.log(plan.maxInputChars, expanded.map(fragment => fragment.id));
const largerRequest = builder.build({ input, maxInputChars: 2000 });
console.log(largerRequest.maxInputChars);

复用元数据目录与请求贡献插槽 ​

扩展 · Node.js

ts
import { Support } from '@jangts/agent-core';
import type { ContextFragment } from '@jangts/agent-core/types';
type Input = { question: string; evidence: string };
const builder = Support.defineContextAssembly<Input, 'question', 'evidence'>([
  { kind: 'fragment', id: 'question', format: input => input.question },
  { kind: 'contributions', placement: 'evidence' },
], {
  maxInputChars: 2000,
  fragment(id, value): ContextFragment { return { id, sourceId: 'request', trust: 'user', lifetime: 'turn', role: 'user', maxChars: 1000, value }; },
  contributions: { evidence(input) { return [{ id: 'evidence', sourceId: 'service', trust: 'server', lifetime: 'turn', role: 'user', maxChars: 1000, value: input.evidence }]; } },
});
console.log(builder.build({ input: { question: '状态?', evidence: '最近检查:正常' } }).messages);
js
import { Support } from '@jangts/agent-core';
const builder = Support.defineContextAssembly([
    { kind: 'fragment', id: 'question', format: input => input.question },
    { kind: 'contributions', placement: 'evidence' },
], {
    maxInputChars: 2000,
    fragment(id, value) { return { id, sourceId: 'request', trust: 'user', lifetime: 'turn', role: 'user', maxChars: 1000, value }; },
    contributions: { evidence(input) { return [{ id: 'evidence', sourceId: 'service', trust: 'server', lifetime: 'turn', role: 'user', maxChars: 1000, value: input.evidence }]; } },
});
console.log(builder.build({ input: { question: '状态?', evidence: '最近检查:正常' } }).messages);

相关策略源码 ​

相关定义 ​

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