Skip to content
English

ContextFragmentConfig ​

SUPPORT · interface · 类型合同

Self-describing fragment declaration: metadata plus a formatter.

导入 ​

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

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

定义 ​

ts
interface ContextFragmentConfig<Input>

继承或实现: Omit<ContextFragment, 'value'>。

泛型参数 ​

类型参数约束默认类型
Input——

字段结构 ​

字段类型必需说明
descriptionstring | undefined可省略只读。Documentation only; not sent to the model.
format(input: Input) => ContextFragment["value"]必需只读。Produces the value; must be synchronous.
idstring必需Unique, stable id matching ^[a-z][a-zA-Z0-9:._-]*$; duplicates are rejected.
role"system" | "user"必需Message slot the fragment occupies when assembled.
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.

成员目录 ​

成员用途
formatProduces the value; must be synchronous.

成员详情 ​

format ​

Produces the value; must be synchronous.

ts
ContextFragmentConfig.format(input: Input): ContextFragment["value"]
参数类型传递说明
inputInput必需—

返回: string \| Record<string, unknown>。

使用示例 ​

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

入门 · 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);

相关策略源码 ​

相关定义 ​

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