Registry
CORE · interface · 类型合同
Mutable catalog. Registrations append; seal freezes the set against later writes.
导入
import type { Registry } from '@jangts/agent-core';/** @typedef {import('@jangts/agent-core').Registry} Registry */其他公开入口:@jangts/agent-core、@jangts/agent-core/types、@jangts/agent-core/structs。
定义
interface Registry<Definition>继承或实现: ReadonlyCatalog<Definition>。
泛型参数
| 类型参数 | 约束 | 默认类型 |
|---|---|---|
Definition | { readonly id: string } | — |
字段结构
| 字段 | 类型 | 必需 | 说明 |
|---|---|---|---|
register | (definition: Definition) => Registry<Definition> | 必需 | Adds one definition; implementations reject blank or duplicate ids. |
registerAll | (definitions: Iterable<Definition>) => Registry<Definition> | 必需 | 见类型定义及调用场景。 |
seal | () => Registry<Definition> | 必需 | Rejects every later registration on this instance. |
definitions | readonly Readonly<Definition>[] | 必需 | 只读。Definitions in registration order; each entry is frozen by the implementation. |
ids | () => readonly string[] | 必需 | Ids in the same order as definitions. |
values | () => IterableIterator<Readonly<Definition>> | 必需 | 见类型定义及调用场景。 |
entries | () => IterableIterator<readonly [string, Readonly<Definition>]> | 必需 | 见类型定义及调用场景。 |
has | (id: string) => boolean | 必需 | 见类型定义及调用场景。 |
get | (id: string) => Readonly<Definition> | undefined | 必需 | Returns the definition or undefined; the caller decides whether absence is an error. |
require | (id: string) => Readonly<Definition> | 必需 | Returns the definition or throws; use when the id is a required reference. |
成员目录
| 成员 | 用途 |
|---|---|
register | Adds one definition; implementations reject blank or duplicate ids. |
registerAll | 完整输入输出见下方重载。 |
seal | Rejects every later registration on this instance. |
ids | Ids in the same order as definitions. |
values | 完整输入输出见下方重载。 |
entries | 完整输入输出见下方重载。 |
has | 完整输入输出见下方重载。 |
get | Returns the definition or undefined; the caller decides whether absence is an error. |
require | Returns the definition or throws; use when the id is a required reference. |
成员详情
register
Adds one definition; implementations reject blank or duplicate ids.
Registry.register(definition: Definition): Registry<Definition>Adds one definition; implementations reject blank or duplicate ids.
| 参数 | 类型 | 传递 | 说明 |
|---|---|---|---|
definition | Definition | 必需 | — |
返回: Registry<Definition>。
返回值结构
| 字段 | 类型 | 必需 | 说明 |
|---|---|---|---|
register | (definition: Definition) => Registry<Definition> | 必需 | Adds one definition; implementations reject blank or duplicate ids. |
registerAll | (definitions: Iterable<Definition>) => Registry<Definition> | 必需 | 见类型定义及调用场景。 |
seal | () => Registry<Definition> | 必需 | Rejects every later registration on this instance. |
definitions | readonly Readonly<Definition>[] | 必需 | 只读。Definitions in registration order; each entry is frozen by the implementation. |
ids | () => readonly string[] | 必需 | Ids in the same order as definitions. |
values | () => IterableIterator<Readonly<Definition>> | 必需 | 见类型定义及调用场景。 |
entries | () => IterableIterator<readonly [string, Readonly<Definition>]> | 必需 | 见类型定义及调用场景。 |
has | (id: string) => boolean | 必需 | 见类型定义及调用场景。 |
get | (id: string) => Readonly<Definition> | undefined | 必需 | Returns the definition or undefined; the caller decides whether absence is an error. |
require | (id: string) => Readonly<Definition> | 必需 | Returns the definition or throws; use when the id is a required reference. |
registerAll
Registry.registerAll(definitions: Iterable<Definition>): Registry<Definition>| 参数 | 类型 | 传递 | 说明 |
|---|---|---|---|
definitions | Iterable<Definition> | 必需 | — |
返回: Registry<Definition>。
返回值结构
| 字段 | 类型 | 必需 | 说明 |
|---|---|---|---|
register | (definition: Definition) => Registry<Definition> | 必需 | Adds one definition; implementations reject blank or duplicate ids. |
registerAll | (definitions: Iterable<Definition>) => Registry<Definition> | 必需 | 见类型定义及调用场景。 |
seal | () => Registry<Definition> | 必需 | Rejects every later registration on this instance. |
definitions | readonly Readonly<Definition>[] | 必需 | 只读。Definitions in registration order; each entry is frozen by the implementation. |
ids | () => readonly string[] | 必需 | Ids in the same order as definitions. |
values | () => IterableIterator<Readonly<Definition>> | 必需 | 见类型定义及调用场景。 |
entries | () => IterableIterator<readonly [string, Readonly<Definition>]> | 必需 | 见类型定义及调用场景。 |
has | (id: string) => boolean | 必需 | 见类型定义及调用场景。 |
get | (id: string) => Readonly<Definition> | undefined | 必需 | Returns the definition or undefined; the caller decides whether absence is an error. |
require | (id: string) => Readonly<Definition> | 必需 | Returns the definition or throws; use when the id is a required reference. |
seal
Rejects every later registration on this instance.
Registry.seal(): Registry<Definition>Rejects every later registration on this instance.
无调用参数。
返回: Registry<Definition>。
返回值结构
| 字段 | 类型 | 必需 | 说明 |
|---|---|---|---|
register | (definition: Definition) => Registry<Definition> | 必需 | Adds one definition; implementations reject blank or duplicate ids. |
registerAll | (definitions: Iterable<Definition>) => Registry<Definition> | 必需 | 见类型定义及调用场景。 |
seal | () => Registry<Definition> | 必需 | Rejects every later registration on this instance. |
definitions | readonly Readonly<Definition>[] | 必需 | 只读。Definitions in registration order; each entry is frozen by the implementation. |
ids | () => readonly string[] | 必需 | Ids in the same order as definitions. |
values | () => IterableIterator<Readonly<Definition>> | 必需 | 见类型定义及调用场景。 |
entries | () => IterableIterator<readonly [string, Readonly<Definition>]> | 必需 | 见类型定义及调用场景。 |
has | (id: string) => boolean | 必需 | 见类型定义及调用场景。 |
get | (id: string) => Readonly<Definition> | undefined | 必需 | Returns the definition or undefined; the caller decides whether absence is an error. |
require | (id: string) => Readonly<Definition> | 必需 | Returns the definition or throws; use when the id is a required reference. |
ids
Ids in the same order as definitions.
Registry.ids(): readonly string[]Ids in the same order as definitions.
无调用参数。
返回: readonly string[]。
values
Registry.values(): IterableIterator<Readonly<Definition>>无调用参数。
返回: IterableIterator<Readonly<Definition>>。
entries
Registry.entries(): IterableIterator<readonly [string, Readonly<Definition>]>无调用参数。
返回: IterableIterator<readonly [string, Readonly<Definition>]>。
has
Registry.has(id: string): boolean| 参数 | 类型 | 传递 | 说明 |
|---|---|---|---|
id | string | 必需 | — |
返回: boolean。
get
Returns the definition or undefined; the caller decides whether absence is an error.
Registry.get(id: string): Readonly<Definition> | undefinedReturns the definition or undefined; the caller decides whether absence is an error.
| 参数 | 类型 | 传递 | 说明 |
|---|---|---|---|
id | string | 必需 | — |
返回: Readonly<Definition> \| undefined。
返回值的可选结构: 选择其中一种结构,各分支的字段要求分别列出。
分支 1
undefined
分支 2
| 字段 | 类型 | 必需 | 说明 |
|---|---|---|---|
id | Definition["id"] | 必需 | 只读。见类型定义及调用场景。 |
require
Returns the definition or throws; use when the id is a required reference.
Registry.require(id: string): Readonly<Definition>Returns the definition or throws; use when the id is a required reference.
| 参数 | 类型 | 传递 | 说明 |
|---|---|---|---|
id | string | 必需 | — |
返回: Readonly<Definition>。
返回值结构
| 字段 | 类型 | 必需 | 说明 |
|---|---|---|---|
id | Definition["id"] | 必需 | 只读。见类型定义及调用场景。 |
使用示例
- 能力组合与声明式安装:集中列出模块、工具引用和注册顺序;通用循环只消费这份实际清单。
直接复用现有模块声明
入门 · 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);