Skip to content
English

ReadonlyCatalog ​

CORE · interface · 类型合同

Read-only projection of a definition set; the implementation owns ordering and validation.

导入 ​

ts
import type { ReadonlyCatalog } from '@jangts/agent-core';
js
/** @typedef {import('@jangts/agent-core').ReadonlyCatalog} ReadonlyCatalog */

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

定义 ​

ts
interface ReadonlyCatalog<Definition>

泛型参数 ​

类型参数约束默认类型
Definition{ readonly id: string }—

字段结构 ​

字段类型必需说明
definitionsreadonly 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.

成员目录 ​

成员用途
idsIds in the same order as definitions.
values完整输入输出见下方重载。
entries完整输入输出见下方重载。
has完整输入输出见下方重载。
getReturns the definition or undefined; the caller decides whether absence is an error.
requireReturns the definition or throws; use when the id is a required reference.

成员详情 ​

ids ​

Ids in the same order as definitions.

ts
ReadonlyCatalog.ids(): readonly string[]

Ids in the same order as definitions.

无调用参数。

返回: readonly string[]。

values ​

ts
ReadonlyCatalog.values(): IterableIterator<Readonly<Definition>>

无调用参数。

返回: IterableIterator<Readonly<Definition>>。

entries ​

ts
ReadonlyCatalog.entries(): IterableIterator<readonly [string, Readonly<Definition>]>

无调用参数。

返回: IterableIterator<readonly [string, Readonly<Definition>]>。

has ​

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

返回: boolean。

get ​

Returns the definition or undefined; the caller decides whether absence is an error.

ts
ReadonlyCatalog.get(id: string): Readonly<Definition> | undefined

Returns the definition or undefined; the caller decides whether absence is an error.

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

返回: Readonly<Definition> \| undefined。

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

分支 1

undefined

分支 2
字段类型必需说明
idDefinition["id"]必需只读。见类型定义及调用场景。

require ​

Returns the definition or throws; use when the id is a required reference.

ts
ReadonlyCatalog.require(id: string): Readonly<Definition>

Returns the definition or throws; use when the id is a required reference.

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

返回: Readonly<Definition>。

返回值结构
字段类型必需说明
idDefinition["id"]必需只读。见类型定义及调用场景。

使用示例 ​

直接复用现有模块声明 ​

入门 · Node.js

ts
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);
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);

增加工具引用和有序安装 ​

配置 · Node.js

ts
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);
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([
    { 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);

相关策略源码 ​

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