createJsonTransport
SUPPORT · function · 运行时值
Create a typed JSON transport without a capability-selection envelope.
The transport embeds the JSON Schema in the instruction and requires the response to be one complete JSON value. The returned response parser rejects native call events and invalid JSON, then validates the value through the supplied schema.
导入
import { createJsonTransport } from '@jangts/agent-core/support';import { createJsonTransport } from '@jangts/agent-core/support';其他公开入口:@jangts/agent-core、@jangts/agent-core/types、@jangts/agent-core/support、@jangts/agent-core/support/model。
定义
泛型参数
| 类型参数 | 约束 | 默认类型 |
|---|---|---|
Output | — | — |
Id | string | string |
调用
createJsonTransport<Output, const Id extends string = string>(definition: { readonly id: Id; readonly outputSchema: import("../../structs/common.js").SchemaAdapter<Output>; readonly options?: Pick<ModelOptions, "temperature" | "json">; }): ModelTransportDefinition<Output> & { readonly id: Id; }Create a typed JSON transport without a capability-selection envelope.
The transport embeds the JSON Schema in the instruction and requires the response to be one complete JSON value. The returned response parser rejects native call events and invalid JSON, then validates the value through the supplied schema.
| 类型参数 | 约束 | 默认类型 |
|---|---|---|
Output | — | — |
Id | string | string |
| 参数 | 类型 | 传递 | 说明 |
|---|---|---|---|
definition | { readonly id: Id; readonly outputSchema: import("../../structs/common.js").SchemaAdapter<Output>; readonly options?: Pick<ModelOptions, "temperature" | "json">; } | 必需 | — |
definition 的字段
| 字段 | 类型 | 必需 | 说明 |
|---|---|---|---|
id | Id | 必需 | 只读。见类型定义及调用场景。 |
outputSchema | SchemaAdapter<Output> | 必需 | 只读。见类型定义及调用场景。 |
options | Pick<ModelOptions, "json" | "temperature"> | undefined | 可省略 | 只读。见类型定义及调用场景。 |
返回: ModelTransportDefinition<Output> & { readonly id: Id; }。
返回值结构
| 字段 | 类型 | 必需 | 说明 |
|---|---|---|---|
id | Id | 必需 | 只读。Transport id used by call requests and reported in diagnostics. |
prepare | (stageId: string) => PreparedModelTransport<Output> | 必需 | Builds the prepared transport for stageId; must not spend a call by itself. |
使用示例
- JSON 值传输:直接校验结构化结果:从输出 Schema 生成 JSON 收集与解析实现,不增加 Tool 选择 envelope,返回统一的模型回执。
最小 JSON 摘要:Schema 和 parser 一次绑定
入门 · Node.js
import { ModelRunner, Support } from '@jangts/agent-core';
const summary = Support.createJsonTransport({ id: 'summary', outputSchema: {
jsonSchema: { type: 'object', properties: { text: { type: 'string' } }, required: ['text'], additionalProperties: false },
parse(value: unknown) {
if (!value || typeof value !== 'object' || !('text' in value) || typeof value.text !== 'string') throw new TypeError('需要摘要正文');
return { text: value.text };
},
} });
const provider = { async *stream() { yield '{"text":"服务运行正常。"}'; } };
const model = new ModelRunner({ provider, transports: [summary] });
const result = await model.invoke({ input: '概括服务状态', transport: summary });
console.log(result.value.text, result.invocation);import { ModelRunner, Support } from '@jangts/agent-core';
const summary = Support.createJsonTransport({ id: 'summary', outputSchema: {
jsonSchema: { type: 'object', properties: { text: { type: 'string' } }, required: ['text'], additionalProperties: false },
parse(value) {
if (!value || typeof value !== 'object' || !('text' in value) || typeof value.text !== 'string')
throw new TypeError('需要摘要正文');
return { text: value.text };
},
} });
const provider = { async *stream() { yield '{"text":"服务运行正常。"}'; } };
const model = new ModelRunner({ provider, transports: [summary] });
const result = await model.invoke({ input: '概括服务状态', transport: summary });
console.log(result.value.text, result.invocation);同一摘要合同增加温度、预算与自定义解析实现
扩展 · Node.js
两个示例均为离线 Provider。更换供应商时继续使用同一传输和调用合同。
import { ModelRunner, Support } from '@jangts/agent-core';
class SummarySchema {
readonly jsonSchema = { type: 'object', properties: { text: { type: 'string' } }, required: ['text'], additionalProperties: false };
#maxChars = 100;
parse(value: unknown) {
if (!value || typeof value !== 'object' || !('text' in value) || typeof value.text !== 'string'
|| !value.text.trim() || value.text.length > this.#maxChars) throw new TypeError('需要简短摘要');
return { text: value.text };
}
}
const summary = Support.createJsonTransport({ id: 'summary', outputSchema: new SummarySchema(), options: { temperature: 0.2 } });
const model = new ModelRunner({ provider: { async *stream() { yield '{"text":"服务运行正常。"}'; } },
transports: [summary], maxCalls: 2, maxOutputTokens: 256,
});
console.log((await model.invoke({ input: '概括服务状态', transport: 'summary' })).value.text);import { ModelRunner, Support } from '@jangts/agent-core';
class SummarySchema {
jsonSchema = { type: 'object', properties: { text: { type: 'string' } }, required: ['text'], additionalProperties: false };
#maxChars = 100;
parse(value) {
if (!value || typeof value !== 'object' || !('text' in value) || typeof value.text !== 'string'
|| !value.text.trim() || value.text.length > this.#maxChars)
throw new TypeError('需要简短摘要');
return { text: value.text };
}
}
const summary = Support.createJsonTransport({ id: 'summary', outputSchema: new SummarySchema(), options: { temperature: 0.2 } });
const model = new ModelRunner({ provider: { async *stream() { yield '{"text":"服务运行正常。"}'; } },
transports: [summary], maxCalls: 2, maxOutputTokens: 256,
});
console.log((await model.invoke({ input: '概括服务状态', transport: 'summary' })).value.text);