defineRegistrationCatalog
SUPPORT · function · 运行时值
Compile an ordered installation plan.
Predicates observe the effects of preceding installations at request time; a predicate that throws rejects the install instead of being treated as false. Callbacks are bound to their declaring definition so implementations may rely on this.
导入
import { defineRegistrationCatalog } from '@jangts/agent-core/support';import { defineRegistrationCatalog } from '@jangts/agent-core/support';其他公开入口:@jangts/agent-core、@jangts/agent-core/types、@jangts/agent-core/support。
定义
泛型参数
| 类型参数 | 约束 | 默认类型 |
|---|---|---|
Context | — | — |
调用
defineRegistrationCatalog<Context>(definitions: readonly RegistrationDefinition<Context>[]): Readonly<{ install(context: Context): void; definitions: readonly Readonly<{ id: string; when: ((context: Context) => boolean) | undefined; register: (context: Context) => void; }>[]; ids(): readonly string[]; values(): IterableIterator<Readonly<{ id: string; when: ((context: Context) => boolean) | undefined; register: (context: Context) => void; }>>; entries(): IterableIterator<readonly [string, Readonly<{ id: string; when: ((context: Context) => boolean) | undefined; register: (context: Context) => void; }>]>; has(id: string): boolean; get(id: string): Readonly<{ id: string; when: ((context: Context) => boolean) | undefined; register: (context: Context) => void; }> | undefined; require(id: string): Readonly<{ id: string; when: ((context: Context) => boolean) | undefined; register: (context: Context) => void; }>; }>Compile an ordered installation plan.
Predicates observe the effects of preceding installations at request time; a predicate that throws rejects the install instead of being treated as false. Callbacks are bound to their declaring definition so implementations may rely on this.
| 类型参数 | 约束 | 默认类型 |
|---|---|---|
Context | — | — |
| 参数 | 类型 | 传递 | 说明 |
|---|---|---|---|
definitions | readonly RegistrationDefinition<Context>[] | 必需 | — |
返回: Readonly<{ install(context: Context): void; definitions: readonly Readonly<{ id: string; when: ((context: Context) => boolean) \| undefined; register: (context: Context) => void; }>[]; ids(): readonly string[]; values(): IterableIterator<Readonly<{ id: string; when: ((context: Context) => boolean) \| undefined; register: (context: Context) => void; }>>; entries(): IterableIterator<readonly [string, Readonly<{ id: string; when: ((context: Context) => boolean) \| undefined; register: (context: Context) => void; }>]>; has(id: string): boolean; get(id: string): Readonly<{ id: string; when: ((context: Context) => boolean) \| undefined; register: (context: Context) => void; }> \| undefined; require(id: string): Readonly<{ id: string; when: ((context: Context) => boolean) \| undefined; register: (context: Context) => void; }>; }>。
返回值结构
| 字段 | 类型 | 必需 | 说明 |
|---|---|---|---|
install | (context: Context) => void | 必需 | Run every enabled step in declaration order against context. Stops at the first failing predicate or handler; earlier installations are not rolled back. |
definitions | readonly Readonly<{ id: string; when: ((context: Context) => boolean) | undefined; register: (context: Context) => void; }>[] | 必需 | 只读。Definitions in registration order; each entry is frozen by the implementation. |
ids | () => readonly string[] | 必需 | Ids in the same order as definitions. |
values | () => IterableIterator<Readonly<{ id: string; when: ((context: Context) => boolean) | undefined; register: (context: Context) => void; }>> | 必需 | 见类型定义及调用场景。 |
entries | () => IterableIterator<readonly [string, Readonly<{ id: string; when: ((context: Context) => boolean) | undefined; register: (context: Context) => void; }>]> | 必需 | 见类型定义及调用场景。 |
has | (id: string) => boolean | 必需 | 见类型定义及调用场景。 |
get | (id: string) => Readonly<{ id: string; when: ((context: Context) => boolean) | undefined; register: (context: Context) => void; }> | undefined | 必需 | Returns the definition or undefined; the caller decides whether absence is an error. |
require | (id: string) => Readonly<{ id: string; when: ((context: Context) => boolean) | undefined; register: (context: Context) => void; }> | 必需 | Returns the definition or throws; use when the id is a required reference. |
使用示例
- 能力组合与声明式安装:集中列出模块、工具引用和注册顺序;通用循环只消费这份实际清单。
直接复用现有模块声明
入门 · Node.js
import { Support } from '@jangts/agent-core';
const modules = [{ id: 'service', description: '服务查询能力', tools: [{ id: 'get_status' }] }];
const catalog = Support.defineCapabilityCatalog(modules);
console.log(catalog.ids(), catalog.findToolOwner('get_status')?.id);import { Support } from '@jangts/agent-core';
const modules = [{ id: 'service', description: '服务查询能力', tools: [{ id: 'get_status' }] }];
const catalog = Support.defineCapabilityCatalog(modules);
console.log(catalog.ids(), catalog.findToolOwner('get_status')?.id);增加工具引用和有序安装
配置 · Node.js
import { Support } from '@jangts/agent-core';
const modules = [{ id: 'service', description: '服务查询', tools: [{ id: 'get_status' }],
references: [{ id: 'status-skill', requiredToolIds: ['get_status'], optionalToolIds: [] }] }];
const moduleCatalog = Support.defineCapabilityCatalog(modules, { toolReferences: module => module.references });
const registration = Support.defineRegistrationCatalog<{ installed: string[] }>([
{ id: 'service', register: app => { app.installed.push(moduleCatalog.require('service').id); } },
{ id: 'status-view', when: app => app.installed.includes('service'), register: app => { app.installed.push('status-view'); } },
]);
const app = { installed: [] as string[] };
registration.install(app);
console.log(app.installed);import { Support } from '@jangts/agent-core';
const modules = [{ id: 'service', description: '服务查询', tools: [{ id: 'get_status' }],
references: [{ id: 'status-skill', requiredToolIds: ['get_status'], optionalToolIds: [] }] }];
const moduleCatalog = Support.defineCapabilityCatalog(modules, { toolReferences: module => module.references });
const registration = Support.defineRegistrationCatalog([
{ id: 'service', register: app => { app.installed.push(moduleCatalog.require('service').id); } },
{ id: 'status-view', when: app => app.installed.includes('service'), register: app => { app.installed.push('status-view'); } },
]);
const app = { installed: [] };
registration.install(app);
console.log(app.installed);