ModelToolSource
CORE · interface · 类型合同
Minimal source a stage needs: enumerate tools and resolve one selection.
导入
ts
import type { ModelToolSource } from '@jangts/agent-core';js
/** @typedef {import('@jangts/agent-core').ModelToolSource} ModelToolSource */其他公开入口:@jangts/agent-core、@jangts/agent-core/types、@jangts/agent-core/structs。
定义
ts
interface ModelToolSource<Output>泛型参数
| 类型参数 | 约束 | 默认类型 |
|---|---|---|
Output | — | unknown |
字段结构
| 字段 | 类型 | 必需 | 说明 |
|---|---|---|---|
values | () => IterableIterator<RegisteredModelTool<Output>> | 必需 | 见类型定义及调用场景。 |
resolve | (selection: ModelSelection, context: Readonly<{ stageId: string; }>) => Output | 必需 | Resolves one selection; throws when the capability is absent from this source. |
成员目录
| 成员 | 用途 |
|---|---|
values | 完整输入输出见下方重载。 |
resolve | Resolves one selection; throws when the capability is absent from this source. |
成员详情
values
ts
ModelToolSource.values(): IterableIterator<RegisteredModelTool<Output>>无调用参数。
返回: IterableIterator<Readonly<ModelTool<unknown, Output, any>>>。
resolve
Resolves one selection; throws when the capability is absent from this source.
ts
ModelToolSource.resolve(selection: ModelSelection, context: Readonly<{ stageId: string; }>): OutputResolves one selection; throws when the capability is absent from this source.
| 参数 | 类型 | 传递 | 说明 |
|---|---|---|---|
selection | ModelSelection | 必需 | — |
context | Readonly<{ stageId: string; }> | 必需 | — |
selection 的字段
| 字段 | 类型 | 必需 | 说明 |
|---|---|---|---|
input | Record<string, unknown> | 必需 | 只读。Arguments exactly as produced by the model, before schema parsing. |
call | { id: string; name: string; argumentChars: number; } | undefined | 可省略 | 只读。Native function call that carried the selection, when the provider used one. |
toolId | string | 必需 | 只读。Capability the selection resolved to. |
context 的字段
| 字段 | 类型 | 必需 | 说明 |
|---|---|---|---|
stageId | string | 必需 | 见类型定义及调用场景。 |
返回: Output。
等待后的值: Awaited<Output>。
使用示例
- 模型 Tool 与传输:完整校验后才得到类型化选择:Tool 定义提供 Schema 与 resolver;传输将 wire 输出翻译为统一的模型选择合同。
声明两个可选择 Tool
入门 · Node.js
ts
import { ModelToolRegistry } from '@jangts/agent-core';
const inputSchema = { jsonSchema: { type: 'object', properties: {}, additionalProperties: false },
parse(value: unknown) {
if (!value || typeof value !== 'object' || Array.isArray(value) || Object.keys(value).length) throw new Error('需要空对象');
return {};
} };
const tools = new ModelToolRegistry([
{ id: 'greet', description: '回答问候', inputSchema, resolve: () => 'greet' },
{ id: 'status', description: '查询状态', inputSchema, resolve: () => 'status' },
]).seal();
console.log(tools.resolve({ toolId: 'status', input: {} }, { stageId: 'recognize' }));js
import { ModelToolRegistry } from '@jangts/agent-core';
const inputSchema = { jsonSchema: { type: 'object', properties: {}, additionalProperties: false },
parse(value) {
if (!value || typeof value !== 'object' || Array.isArray(value) || Object.keys(value).length)
throw new Error('需要空对象');
return {};
} };
const tools = new ModelToolRegistry([
{ id: 'greet', description: '回答问候', inputSchema, resolve: () => 'greet' },
{ id: 'status', description: '查询状态', inputSchema, resolve: () => 'status' },
]).seal();
console.log(tools.resolve({ toolId: 'status', input: {} }, { stageId: 'recognize' }));仅向一个阶段投影允许的子集
配置 · Node.js
ts
import { ModelToolRegistry, Support } from '@jangts/agent-core';
const schema = Support.defineSchema({ jsonSchema: { type: 'object', properties: {} },
parse(value: unknown) { if (!value || typeof value !== 'object' || Array.isArray(value)) throw new TypeError('需要对象'); return {}; } });
const registry = new ModelToolRegistry([
{ id: 'greet', description: '问候', inputSchema: schema, resolve: () => 'greet' },
{ id: 'status', description: '状态', inputSchema: schema, resolve: () => 'status' },
]).seal();
const readOnly = registry.select(['status']);
const transport = Support.createModelTransport({ id: 'recognize', tools: readOnly });
console.log(transport.id, readOnly.ids());js
import { ModelToolRegistry, Support } from '@jangts/agent-core';
const schema = Support.defineSchema({ jsonSchema: { type: 'object', properties: {} },
parse(value) { if (!value || typeof value !== 'object' || Array.isArray(value))
throw new TypeError('需要对象'); return {}; } });
const registry = new ModelToolRegistry([
{ id: 'greet', description: '问候', inputSchema: schema, resolve: () => 'greet' },
{ id: 'status', description: '状态', inputSchema: schema, resolve: () => 'status' },
]).seal();
const readOnly = registry.select(['status']);
const transport = Support.createModelTransport({ id: 'recognize', tools: readOnly });
console.log(transport.id, readOnly.ids());