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
import { createJsonTransport } from '@jangts/agent-core/support';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 parameter | Constraint | Default type |
|---|---|---|
Output | — | — |
Id | string | string |
Call
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 parameter | Constraint | Default type |
|---|---|---|
Output | — | — |
Id | string | string |
| Parameter | Type | Passing | Description |
|---|---|---|---|
definition | { readonly id: Id; readonly outputSchema: import("../../structs/common.js").SchemaAdapter<Output>; readonly options?: Pick<ModelOptions, "temperature" | "json">; } | Required | — |
Fields of definition
| Field | Type | Required | Description |
|---|---|---|---|
id | Id | Required | Read-only. See the type definition and usage context. |
outputSchema | SchemaAdapter<Output> | Required | Read-only. See the type definition and usage context. |
options | Pick<ModelOptions, "json" | "temperature"> | undefined | Optional | Read-only. See the type definition and usage context. |
Returns: ModelTransportDefinition<Output> & { readonly id: Id; }.
Return value structure
| Field | Type | Required | Description |
|---|---|---|---|
id | Id | Required | Read-only. Transport id used by call requests and reported in diagnostics. |
prepare | (stageId: string) => PreparedModelTransport<Output> | Required | Builds the prepared transport for stageId; must not spend a call by itself. |
Examples
- JSON value transport: validate structured results directly: Generate JSON collection and parsing from an output Schema without adding a tool-selection envelope, and return a unified model result.
Minimal JSON summary: bind Schema and parser in one step
Beginner · 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);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.
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);