Skip to content
中文

createJsonTransport ​

SUPPORT · function · Runtime value

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 ​

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

Other public entry points:@jangts/agent-core, @jangts/agent-core/types, @jangts/agent-core/support, @jangts/agent-core/support/model.

Definition ​

Type parameters ​

Type parameterConstraintDefault type
Output——
Idstringstring

Call ​

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.

Type parameterConstraintDefault type
Output——
Idstringstring
ParameterTypePassingDescription
definition{ readonly id: Id; readonly outputSchema: import("../../structs/common.js").SchemaAdapter<Output>; readonly options?: Pick<ModelOptions, "temperature" | "json">; }Required—
Fields of definition
FieldTypeRequiredDescription
idIdRequiredRead-only. See the type definition and usage context.
outputSchemaSchemaAdapter<Output>RequiredRead-only. See the type definition and usage context.
optionsPick<ModelOptions, "json" | "temperature"> | undefinedOptionalRead-only. See the type definition and usage context.

Returns: ModelTransportDefinition<Output> & { readonly id: Id; }.

Return value structure
FieldTypeRequiredDescription
idIdRequiredRead-only. Transport id used by call requests and reported in diagnostics.
prepare(stageId: string) => PreparedModelTransport<Output>RequiredBuilds the prepared transport for stageId; must not spend a call by itself.

Examples ​

Minimal JSON summary: bind Schema and parser in one step ​

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

Add temperature, budget, and a custom parser to the same summary contract ​

Extension · Node.js

Both examples use offline Providers. When switching providers, the same transport and call contract continue to apply.

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

API reference, usage examples and developer guides.