Skip to content
English

createModelTransport ​

CORE · function · 运行时值

Compose a registered selection transport.

The capability source may be a static catalog or a function of the stage id; the resulting definitions are captured when prepare runs and are used both to build the instruction and to reject unknown capability ids at finish time.

requireStrictArguments is a registration-time assertion: a transport cannot demand strict arguments from an adapter that does not support them.

导入 ​

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

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

定义 ​

泛型参数 ​

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

调用 ​

ts
createModelTransport<Result, const Id extends string = string>(registration: ModelTransportRegistration<Result> & { readonly id: Id; }): ModelTransportDefinition<Result> & { readonly id: Id; }

Compose a registered selection transport.

The capability source may be a static catalog or a function of the stage id; the resulting definitions are captured when prepare runs and are used both to build the instruction and to reject unknown capability ids at finish time.

requireStrictArguments is a registration-time assertion: a transport cannot demand strict arguments from an adapter that does not support them.

类型参数约束默认类型
Result——
Idstringstring
参数类型传递说明
registrationModelTransportRegistration<Result> & { readonly id: Id; }必需—
registration 的字段
字段类型必需说明
idId必需Transport id used by call requests and reported in diagnostic events.
toolsModelToolSource<Result> | ((stage: string) => ModelToolSource<Result>)必需Static capability source, or a factory that selects tools per stage.
adapterModelInteractionAdapter | undefined可省略Wire-format adapter; defaults to native function calling.
optionsPick<ModelOptions, "temperature"> | undefined可省略Sampling defaults this transport contributes to every call.
requireStrictArgumentsboolean | undefined可省略Reject the registration when the chosen adapter cannot enforce strict arguments.

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

返回值结构
字段类型必需说明
idId必需只读。Transport id used by call requests and reported in diagnostics.
prepare(stageId: string) => PreparedModelTransport<Result>必需Builds the prepared transport for stageId; must not spend a call by itself.

使用示例 ​

  • 模型传输与阶段投影:从 Tool 定义生成封存目录与阶段投影,原生函数和 JSON 适配复用同一 Tool 合同。

创建具名文本传输 ​

入门 · Node.js

ts
import { ModelRunner, Support } from '@jangts/agent-core';
const text = Support.createTextTransport({ id: 'brief', options: { temperature: 0.2 } });
const runner = new ModelRunner({ provider: { async *stream() { yield '服务正常'; } }, transports: [text] });
console.log((await runner.invoke({ input: '简答服务状态', transport: text })).value);
js
import { ModelRunner, Support } from '@jangts/agent-core';
const text = Support.createTextTransport({ id: 'brief', options: { temperature: 0.2 } });
const runner = new ModelRunner({ provider: { async *stream() { yield '服务正常'; } }, transports: [text] });
console.log((await runner.invoke({ input: '简答服务状态', transport: text })).value);

从同源 Tool 生成两个阶段的传输 ​

配置 · Node.js

ts
import { Support } from '@jangts/agent-core';
const inputSchema = Support.defineSchema({
  jsonSchema: { type: 'object', properties: {}, additionalProperties: false },
  parse(value: unknown) {
    if (!value || typeof value !== 'object' || Array.isArray(value) || Object.keys(value).length) throw new TypeError('需要空对象');
    return {};
  },
});
const tools = Support.defineModelTools([
  { id: 'greet', description: '问候', inputSchema, resolve: () => 'greet' },
  { id: 'status', description: '状态查询', inputSchema, resolve: () => 'status' },
]);
const transport = Support.createStageModelTransport({ id: 'intent', tools,
  stages: [{ id: 'recognize', toolIds: ['greet', 'status'] }, { id: 'status-only', toolIds: ['status'] }],
});
console.log(transport.prepare('status-only').schemaChars);
js
import { Support } from '@jangts/agent-core';
const inputSchema = Support.defineSchema({
    jsonSchema: { type: 'object', properties: {}, additionalProperties: false },
    parse(value) {
        if (!value || typeof value !== 'object' || Array.isArray(value) || Object.keys(value).length)
            throw new TypeError('需要空对象');
        return {};
    },
});
const tools = Support.defineModelTools([
    { id: 'greet', description: '问候', inputSchema, resolve: () => 'greet' },
    { id: 'status', description: '状态查询', inputSchema, resolve: () => 'status' },
]);
const transport = Support.createStageModelTransport({ id: 'intent', tools,
    stages: [{ id: 'recognize', toolIds: ['greet', 'status'] }, { id: 'status-only', toolIds: ['status'] }],
});
console.log(transport.prepare('status-only').schemaChars);

保留 Tool,只替换明确的交互适配 ​

扩展 · Node.js

模型调用仍是 invoke({input,transport}) 并返回 ModelResult;切换交互不重新设计业务结果。

ts
import { Support } from '@jangts/agent-core';
const inputSchema = Support.defineSchema({
  jsonSchema: { type: 'object', properties: {}, additionalProperties: false },
  parse(value: unknown) {
    if (!value || typeof value !== 'object' || Array.isArray(value) || Object.keys(value).length) throw new TypeError('需要空对象');
    return {};
  },
});
const tools = Support.defineModelTools([
  { id: 'status', description: '查询服务状态', inputSchema, resolve: () => 'status' },
]);
const native = Support.createModelTransport({ id: 'native', tools });
const adapterId: keyof typeof Support.modelInteractionCatalog = 'json-object';
const json = Support.createModelTransport({ id: 'json', tools, adapter: Support.modelInteractionCatalog[adapterId] });
console.log(native.id, json.id);
js
import { Support } from '@jangts/agent-core';
const inputSchema = Support.defineSchema({
    jsonSchema: { type: 'object', properties: {}, additionalProperties: false },
    parse(value) {
        if (!value || typeof value !== 'object' || Array.isArray(value) || Object.keys(value).length)
            throw new TypeError('需要空对象');
        return {};
    },
});
const tools = Support.defineModelTools([
    { id: 'status', description: '查询服务状态', inputSchema, resolve: () => 'status' },
]);
const native = Support.createModelTransport({ id: 'native', tools });
const adapterId = 'json-object';
const json = Support.createModelTransport({ id: 'json', tools, adapter: Support.modelInteractionCatalog[adapterId] });
console.log(native.id, json.id);

相关策略源码 ​

相关定义 ​

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