defineModelTools
SUPPORT · function · 运行时值
Build a sealed capability catalog from typed declarations.
导入
ts
import { defineModelTools } from '@jangts/agent-core/support';js
import { defineModelTools } from '@jangts/agent-core/support';其他公开入口:@jangts/agent-core/support、@jangts/agent-core/support/model。
定义
泛型参数
| 类型参数 | 约束 | 默认类型 |
|---|---|---|
Definitions | readonly ModelToolRegistration[] | — |
调用
ts
defineModelTools<const Definitions extends readonly ModelToolRegistration[]>(definitions: Definitions): ModelToolCatalog<CapabilityOutput<Definitions[number]>>Build a sealed capability catalog from typed declarations.
| 类型参数 | 约束 | 默认类型 |
|---|---|---|
Definitions | readonly ModelToolRegistration[] | — |
| 参数 | 类型 | 传递 | 说明 |
|---|---|---|---|
definitions | Definitions | 必需 | — |
返回: ModelToolCatalog<CapabilityOutput<Definitions[number]>>。
返回值结构
| 字段 | 类型 | 必需 | 说明 |
|---|---|---|---|
definitions | readonly Readonly<Readonly<ModelTool<unknown, CapabilityOutput<Definitions[number]>, any>>>[] | 必需 | 只读。Definitions in registration order; each entry is frozen by the implementation. |
ids | () => readonly string[] | 必需 | Ids in the same order as definitions. |
values | (() => IterableIterator<Readonly<Readonly<ModelTool<unknown, CapabilityOutput<Definitions[number]>, any>>>>) & (() => IterableIterator<Readonly<ModelTool<unknown, CapabilityOutput<Definitions[number]>, any>>>) | 必需 | 见类型定义及调用场景。 |
entries | () => IterableIterator<readonly [string, Readonly<Readonly<ModelTool<unknown, CapabilityOutput<Definitions[number]>, any>>>]> | 必需 | 见类型定义及调用场景。 |
has | (id: string) => boolean | 必需 | 见类型定义及调用场景。 |
get | (id: string) => Readonly<Readonly<ModelTool<unknown, CapabilityOutput<Definitions[number]>, any>>> | undefined | 必需 | Returns the definition or undefined; the caller decides whether absence is an error. |
require | (id: string) => Readonly<Readonly<ModelTool<unknown, CapabilityOutput<Definitions[number]>, any>>> | 必需 | Returns the definition or throws; use when the id is a required reference. |
resolve | (selection: ModelSelection, context: Readonly<{ stageId: string; }>) => CapabilityOutput<Definitions[number]> | 必需 | Resolves one selection; throws when the capability is absent from this source. |
select | (ids: readonly string[], context?: unknown) => ModelToolCatalog<CapabilityOutput<Definitions[number]>> | 必需 | Explicit selection, optionally filtered against a host context. |
withIntent | (context: unknown) => ModelToolCatalog<CapabilityOutput<Definitions[number]>> | 必需 | Demand-facing active tools; no workflow node or invocation is implied. |
使用示例
- 模型传输与阶段投影:从 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);