Skip to content
English

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.

导入 ​

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

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

定义 ​

泛型参数 ​

类型参数约束默认类型
Output——
Idstringstring

调用 ​

ts
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——
Idstringstring
参数类型传递说明
definition{ readonly id: Id; readonly outputSchema: import("../../structs/common.js").SchemaAdapter<Output>; readonly options?: Pick<ModelOptions, "temperature" | "json">; }必需—
definition 的字段
字段类型必需说明
idId必需只读。见类型定义及调用场景。
outputSchemaSchemaAdapter<Output>必需只读。见类型定义及调用场景。
optionsPick<ModelOptions, "json" | "temperature"> | undefined可省略只读。见类型定义及调用场景。

返回: ModelTransportDefinition<Output> & { readonly id: Id; }。

返回值结构
字段类型必需说明
idId必需只读。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 和 parser 一次绑定 ​

入门 · Node.js

ts
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);
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) {
            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。更换供应商时继续使用同一传输和调用合同。

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

相关策略源码 ​

相关定义 ​

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