Skip to content
English

ModelToolRegistry ​

CORE · class · 运行时值

A request/stage projection is a sealed registry. Hosts filter availability before projection.

导入 ​

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

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

定义 ​

ts
class ModelToolRegistry

构造函数 ​

ts
new ModelToolRegistry(definitions?: Iterable<ModelToolRegistration>): ModelToolRegistry
参数类型传递说明
definitionsIterable<ModelToolRegistration>可省略—

返回: ModelToolRegistry。

实例属性 ​

字段类型必需说明
definitionsreadonly Readonly<Readonly<ModelTool<unknown, unknown, any>>>[]必需只读。Frozen snapshot of the registered definitions, in insertion order.

成员目录 ​

成员用途
registerRegister one capability.
idsRegistered ids in declaration order.
entriesLive iterator over [id, definition] pairs; it reflects only entries added before sealing.
registerAllRegister several tools.
require完整输入输出见下方重载。
has完整输入输出见下方重载。
get完整输入输出见下方重载。
valuesLive iterator over the registered definitions.
sealClose the registry to further registration. Sealing is idempotent and does not affect existing entries.
selectDerive a sealed sub-catalog for one stage.
withIntentDiscover active demand-facing tools without tying the projection to a workflow entry.
resolveParse model-supplied arguments and run the selected capability.

成员详情 ​

register ​

实例方法。

Register one capability.

ts
register<Result>(capability: ModelToolRegistration<Result>): ModelToolRegistry

Register one capability.

类型参数约束默认类型
Result——
参数类型传递说明
capabilityModelToolRegistration<Result>必需—
capability 的字段
字段类型必需说明
idstring必需只读。Unique id within its registry; never blank.
descriptionstring必需只读。Model-visible description explaining when the capability applies.
inputSchemaSchemaAdapter<any>必需只读。Schema describing the arguments; must be an object schema.
activeToolCondition<any> | undefined可省略只读。Omitted means active. Authorization remains a separate execution boundary.
asIntentAsIntent<any> | undefined可省略只读。Omitted means this tool is not advertised by withIntent().
resolve((input: any, context: Readonly<{ stageId: string; }>) => Result) | undefined可省略只读。见类型定义及调用场景。

返回: ModelToolRegistry。

ids ​

实例方法。

Registered ids in declaration order.

ts
ids(): readonly string[]

Registered ids in declaration order.

无调用参数。

返回: readonly string[]。

entries ​

实例方法。

Live iterator over [id, definition] pairs; it reflects only entries added before sealing.

ts
entries(): IterableIterator<readonly [string, RegisteredModelTool]>

Live iterator over [id, definition] pairs; it reflects only entries added before sealing.

无调用参数。

返回: IterableIterator<readonly [string, Readonly<ModelTool<unknown, unknown, any>>]>。

registerAll ​

实例方法。

Register several tools.

ts
registerAll(definitions: Iterable<ModelToolRegistration>): ModelToolRegistry

Register several tools.

参数类型传递说明
definitionsIterable<ModelToolRegistration>必需—

返回: ModelToolRegistry。

require ​

实例方法。

ts
require(id: string): RegisteredModelTool
参数类型传递说明
idstring必需—

返回: Readonly<ModelTool<unknown, unknown, any>>。

返回值结构
字段类型必需说明
idstring必需只读。Unique id within its registry; never blank.
descriptionstring必需只读。Model-visible description explaining when the capability applies.
inputSchemaSchemaAdapter<unknown>必需只读。Schema describing the arguments; must be an object schema.
activeToolCondition<any> | undefined可省略只读。Omitted means active. Authorization remains a separate execution boundary.
asIntentAsIntent<any> | undefined可省略只读。Omitted means this tool is not advertised by withIntent().
resolve(input: unknown, context: Readonly<{ stageId: string; }>) => unknown必需Converts validated arguments into the value handed back to the caller. Runs inside the model call, so it must return synchronously; throwing rejects the selection.

has ​

实例方法。

ts
has(id: string): boolean
参数类型传递说明
idstring必需—

返回: boolean。

get ​

实例方法。

ts
get(id: string): RegisteredModelTool | undefined
参数类型传递说明
idstring必需—

返回: Readonly<ModelTool<unknown, unknown, any>> \| undefined。

返回值的可选结构: 选择其中一种结构,各分支的字段要求分别列出。

分支 1

undefined

分支 2
字段类型必需说明
idstring必需只读。Unique id within its registry; never blank.
descriptionstring必需只读。Model-visible description explaining when the capability applies.
inputSchemaSchemaAdapter<unknown>必需只读。Schema describing the arguments; must be an object schema.
activeToolCondition<any> | undefined可省略只读。Omitted means active. Authorization remains a separate execution boundary.
asIntentAsIntent<any> | undefined可省略只读。Omitted means this tool is not advertised by withIntent().
resolve(input: unknown, context: Readonly<{ stageId: string; }>) => unknown必需Converts validated arguments into the value handed back to the caller. Runs inside the model call, so it must return synchronously; throwing rejects the selection.

values ​

实例方法。

Live iterator over the registered definitions.

ts
values(): IterableIterator<RegisteredModelTool>

Live iterator over the registered definitions.

无调用参数。

返回: IterableIterator<Readonly<ModelTool<unknown, unknown, any>>>。

seal ​

实例方法。

Close the registry to further registration.

Sealing is idempotent and does not affect existing entries.

ts
seal(): ModelToolRegistry

Close the registry to further registration.

Sealing is idempotent and does not affect existing entries.

无调用参数。

返回: ModelToolRegistry。

select ​

实例方法。

Derive a sealed sub-catalog for one stage.

ts
select(ids: readonly string[], context?: unknown): ModelToolRegistry

Derive a sealed sub-catalog for one stage.

参数类型传递说明
idsreadonly string[]必需—
contextunknown可省略—

返回: ModelToolRegistry。

withIntent ​

实例方法。

Discover active demand-facing tools without tying the projection to a workflow entry.

ts
withIntent(context: unknown): ModelToolRegistry

Discover active demand-facing tools without tying the projection to a workflow entry.

参数类型传递说明
contextunknown必需—

返回: ModelToolRegistry。

resolve ​

实例方法。

Parse model-supplied arguments and run the selected capability.

ts
resolve(selection: ModelSelection, context: Readonly<{ stageId: string; }>): unknown

Parse model-supplied arguments and run the selected capability.

参数类型传递说明
selectionModelSelection必需—
contextReadonly<{ stageId: string; }>必需—
selection 的字段
字段类型必需说明
inputRecord<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.
toolIdstring必需只读。Capability the selection resolved to.
context 的字段
字段类型必需说明
stageIdstring必需见类型定义及调用场景。

返回: unknown。

使用示例 ​

声明两个可选择 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());

相关定义 ​

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