Skip to content
English

CommandCatalog ​

KIT · class · 运行时值

Mode and cross-protocol field ownership checks are shared by all receiving hosts.

The catalog seals itself as soon as construction finishes, so a run cannot register a new command later; set remains available only for the superclass installation pass.

导入 ​

ts
import { CommandCatalog } from '@jangts/agent-core/kits/interaction';
js
import { CommandCatalog } from '@jangts/agent-core/kits/interaction';

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

定义 ​

ts
class CommandCatalog<Value, Result, Args, Id>

继承或实现: ProtocolRegistry<Command<Value, Result, Args> & { readonly id: Id }>。

泛型参数 ​

类型参数约束默认类型
ValueobjectRecord<string, unknown>
Result—Value
Argsunknown[][]
Idstringstring

构造函数 ​

ts
new CommandCatalog<Value extends object = Record<string, unknown>, Result = Value, Args extends unknown[] = [], Id extends string = string>(definitions: Iterable<readonly [Id, Command<Value, Result, Args>]>, options?: CommandCatalogOptions): CommandCatalog<Value, Result, Args, Id>
参数类型传递说明
definitionsIterable<readonly [Id, Command<Value, Result, Args>]>必需—
optionsCommandCatalogOptions可省略—
options 的字段
字段类型必需说明
modeFieldstring | undefined可省略只读。Field carrying the mode selector, when modes are used.
unknownErrorstring | Error | undefined可省略只读。Rejection wording for an unknown command selector.
modeErrorstring | Error | undefined可省略只读。Rejection wording for a command used outside its declared modes.
ownershipErrorstring | Error | undefined可省略只读。Rejection wording for a model-supplied owned field.
tagsreadonly string[] | undefined可省略只读。Diagnostic tags attached to every rejection from this catalog.

返回: CommandCatalog<Value, Result, Args, Id>。

实例属性 ​

字段类型必需说明
definitionsreadonly Readonly<Command<Value, Result, Args> & { id: Id; }>[]必需只读。Compiled commands in registration order.

成员目录 ​

成员用途
define静态。Compile and construct a catalog from definitions in one call.
compile静态。Compile a set of definitions with shared envelope fields.
setInstall one compiled command during construction.
deleteRemoval is not supported.
clearClearing is not supported.
toRecordExplicit dictionary projection; definitions always remains an ordered array.
manifestThe same declarations drive purpose-specific menus, documentation and handler binding.
parseValidate and parse one payload as the named command. Checks run before the command's own parser: the id must exist, the mode (when configured) must be allowed, and no other command may claim a field that this payload sets. The last rule prevents one command from silently consuming another command's data.
ids完整输入输出见下方重载。
requireLook up a protocol that must exist.

成员详情 ​

define ​

静态成员。

Compile and construct a catalog from definitions in one call.

ts
CommandCatalog.define<Value extends object, Result = Value, Args extends unknown[] = [], Id extends string = string>(definitions: Readonly<Record<Id, CommandDefinition<Value, Result, Args>>>, defaults?: Partial<CommandDefinition<Value, Result, Args>>, options?: CommandCatalogOptions): CommandCatalog<Value, Result, Args, Id>

Compile definitions and wrap them in a sealed catalog.

类型参数约束默认类型
Valueobject—
Result—Value
Argsunknown[][]
Idstringstring
参数类型传递说明
definitionsReadonly<Record<Id, CommandDefinition<Value, Result, Args>>>必需—
defaultsPartial<CommandDefinition<Value, Result, Args>>可省略—
optionsCommandCatalogOptions可省略—
defaults 的字段
字段类型必需说明
categoryInteractionCategory | undefined可省略只读。Declared purpose; validated against the frozen category list.
versionnumber | undefined可省略只读。Protocol version, used to reject incompatible registrations.
descriptionstring | undefined可省略只读。见类型定义及调用场景。
terminalboolean | undefined可省略只读。Marks a command that ends the interaction.
discriminator{ readonly field: string; readonly value: string; } | undefined可省略只读。Constant field identifying the command; its value is forced to the command id.
fieldsreadonly string[] | Readonly<Record<string, ProtocolField>> | undefined可省略只读。Field declarations by name, or an ordered list of field names.
ownedFieldsreadonly string[] | undefined可省略只读。Fields the program writes; may name fields not declared in fields.
additionalPropertiesboolean | undefined可省略只读。Whether fields outside the declaration are rejected; defaults to the compiler policy.
optionalPresence"own" | "defined" | undefined可省略只读。'defined' rejects an explicitly undefined optional property; 'own' preserves its presence.
modesreadonly string[] | undefined可省略只读。App-declared modes in which the command is available.
errorProtocolReason<Value, Args> | undefined可省略只读。Rejection wording for the command as a whole.
tagsreadonly string[] | undefined可省略只读。Diagnostic tags attached to rejections from this command.
checksreadonly ProtocolCheck<Value, Args>[] | undefined可省略只读。Declarative rejection rules, evaluated in order after the structural checks.
refine((value: Value, ...args: Args) => void) | undefined可省略只读。Domain checks/bindings only; invoked after structural and declarative checks.
format((value: Value, ...args: Args) => Result) | undefined可省略只读。Formats the validated value into the command result.
options 的字段
字段类型必需说明
modeFieldstring | undefined可省略只读。Field carrying the mode selector, when modes are used.
unknownErrorstring | Error | undefined可省略只读。Rejection wording for an unknown command selector.
modeErrorstring | Error | undefined可省略只读。Rejection wording for a command used outside its declared modes.
ownershipErrorstring | Error | undefined可省略只读。Rejection wording for a model-supplied owned field.
tagsreadonly string[] | undefined可省略只读。Diagnostic tags attached to every rejection from this catalog.

返回: CommandCatalog<Value, Result, Args, Id>。

compile ​

静态成员。

Compile a set of definitions with shared envelope fields.

ts
CommandCatalog.compile<Value extends object, Result = Value, Args extends unknown[] = [], Id extends string = string>(definitions: Readonly<Record<Id, CommandDefinition<Value, Result, Args>>>, defaults?: Partial<CommandDefinition<Value, Result, Args>>): Readonly<Record<Id, Command<Value, Result, Args>>>

Compile a whole catalog with shared envelope fields.

Applications declare only their own data and handlers. Defaults are merged in this order: unrelated default keys, then the command's own keys, with fields, checks and tags concatenated so shared envelope checks always run before per-command checks.

类型参数约束默认类型
Valueobject—
Result—Value
Argsunknown[][]
Idstringstring
参数类型传递说明
definitionsReadonly<Record<Id, CommandDefinition<Value, Result, Args>>>必需—
defaultsPartial<CommandDefinition<Value, Result, Args>>可省略—
defaults 的字段
字段类型必需说明
categoryInteractionCategory | undefined可省略只读。Declared purpose; validated against the frozen category list.
versionnumber | undefined可省略只读。Protocol version, used to reject incompatible registrations.
descriptionstring | undefined可省略只读。见类型定义及调用场景。
terminalboolean | undefined可省略只读。Marks a command that ends the interaction.
discriminator{ readonly field: string; readonly value: string; } | undefined可省略只读。Constant field identifying the command; its value is forced to the command id.
fieldsreadonly string[] | Readonly<Record<string, ProtocolField>> | undefined可省略只读。Field declarations by name, or an ordered list of field names.
ownedFieldsreadonly string[] | undefined可省略只读。Fields the program writes; may name fields not declared in fields.
additionalPropertiesboolean | undefined可省略只读。Whether fields outside the declaration are rejected; defaults to the compiler policy.
optionalPresence"own" | "defined" | undefined可省略只读。'defined' rejects an explicitly undefined optional property; 'own' preserves its presence.
modesreadonly string[] | undefined可省略只读。App-declared modes in which the command is available.
errorProtocolReason<Value, Args> | undefined可省略只读。Rejection wording for the command as a whole.
tagsreadonly string[] | undefined可省略只读。Diagnostic tags attached to rejections from this command.
checksreadonly ProtocolCheck<Value, Args>[] | undefined可省略只读。Declarative rejection rules, evaluated in order after the structural checks.
refine((value: Value, ...args: Args) => void) | undefined可省略只读。Domain checks/bindings only; invoked after structural and declarative checks.
format((value: Value, ...args: Args) => Result) | undefined可省略只读。Formats the validated value into the command result.

返回: Readonly<Record<Id, Command<Value, Result, Args>>>。

set ​

实例方法。

Install one compiled command during construction.

ts
set(id: string, command: Command<Value, Result, Args>): CommandCatalog<Value, Result, Args, Id>

Install one compiled command during construction.

参数类型传递说明
idstring必需—
commandCommand<Value, Result, Args>必需—
command 的字段
字段类型必需说明
categoryInteractionCategory | undefined可省略只读。见类型定义及调用场景。
versionnumber | undefined可省略只读。见类型定义及调用场景。
descriptionstring | undefined可省略只读。见类型定义及调用场景。
terminalboolean | undefined可省略只读。见类型定义及调用场景。
fieldSchemasRecord<string, unknown>必需只读。Per-field schemas, including the forced discriminator field.
modelJsonSchemaRecord<string, unknown>必需只读。Schema for model output; only modelRequired fields are required here.
inputJsonSchemaRecord<string, unknown>必需只读。Schema for runtime input; every required field is required here.
modesreadonly string[] | undefined可省略只读。见类型定义及调用场景。
ownedFieldsreadonly string[]必需只读。Field names the program owns; the model must not supply them.
keys(value: Record<string, unknown>) => string[]必需Selector values carried by value, used to resolve the command before parsing.
assert(value: Value, ...args: Args) => void必需Validates without formatting; throws on the first failed check.
parse(value: Value, ...args: Args) => Result必需Validates and returns the formatted result.

返回: CommandCatalog<Value, Result, Args, Id>。

delete ​

实例方法。

Removal is not supported.

ts
delete(_id: string): boolean

Removal is not supported.

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

返回: boolean。

clear ​

实例方法。

Clearing is not supported.

ts
clear(): void

Clearing is not supported.

无调用参数。

返回: void。

toRecord ​

实例方法。

Explicit dictionary projection; definitions always remains an ordered array.

ts
toRecord(): Readonly<Record<Id, Command<Value, Result, Args>>>

Explicit dictionary projection; definitions always remains an ordered array.

无调用参数。

返回: Readonly<Record<Id, Command<Value, Result, Args>>>。

manifest ​

实例方法。

The same declarations drive purpose-specific menus, documentation and handler binding.

ts
manifest(category?: InteractionCategory): readonly InteractionDescriptor[]

The same declarations drive purpose-specific menus, documentation and handler binding.

参数类型传递说明
categoryInteractionCategory | undefined可省略—

返回: readonly InteractionDescriptor[]。

parse ​

实例方法。

Validate and parse one payload as the named command.

Checks run before the command's own parser: the id must exist, the mode (when configured) must be allowed, and no other command may claim a field that this payload sets. The last rule prevents one command from silently consuming another command's data.

ts
parse(id: string, value: Value, ...args: Args): Result

Validate and parse one payload as the named command.

Checks run before the command's own parser: the id must exist, the mode (when configured) must be allowed, and no other command may claim a field that this payload sets. The last rule prevents one command from silently consuming another command's data.

参数类型传递说明
idstring必需—
valueValue必需—
argsArgs剩余参数—

返回: Result。

等待后的值: Awaited<Result>。

ids ​

实例方法。

ts
ids(): readonly string[]

无调用参数。

返回: readonly string[]。

require ​

实例方法。

Look up a protocol that must exist.

ts
require(id: string): Command<Value, Result, Args> & { readonly id: Id; }

Look up a protocol that must exist.

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

返回: Command<Value, Result, Args> & { readonly id: Id; }。

返回值结构
字段类型必需说明
categoryInteractionCategory | undefined可省略只读。见类型定义及调用场景。
versionnumber | undefined可省略只读。见类型定义及调用场景。
descriptionstring | undefined可省略只读。见类型定义及调用场景。
terminalboolean | undefined可省略只读。见类型定义及调用场景。
fieldSchemasRecord<string, unknown>必需只读。Per-field schemas, including the forced discriminator field.
modelJsonSchemaRecord<string, unknown>必需只读。Schema for model output; only modelRequired fields are required here.
inputJsonSchemaRecord<string, unknown>必需只读。Schema for runtime input; every required field is required here.
modesreadonly string[] | undefined可省略只读。见类型定义及调用场景。
ownedFieldsreadonly string[]必需只读。Field names the program owns; the model must not supply them.
keys(value: Record<string, unknown>) => string[]必需Selector values carried by value, used to resolve the command before parsing.
assert(value: Value, ...args: Args) => void必需Validates without formatting; throws on the first failed check.
parse(value: Value, ...args: Args) => Result必需Validates and returns the formatted result.
idId必需只读。见类型定义及调用场景。

使用示例 ​

先定义一种说明命令并校验 ​

入门 · Node.js

ts
import { Interaction } from '@jangts/agent-core/kits/interaction';
const protocol = Interaction.define({
  commandField: 'type', commands: {
    reply: { category: 'presentation', description: '展示已确认说明', fields: { text: { required: true, jsonSchema: { type: 'string' } } },
      checks: [{ occur: value => typeof value.text !== 'string', error: 'text 必须是字符串' }] },
  },
});
console.log(protocol.receive({ type: 'reply', text: '普通对象也能实现接口。' }));
console.log(protocol.modelJsonSchema);
js
import { Interaction } from '@jangts/agent-core/kits/interaction';
const protocol = Interaction.define({
    commandField: 'type', commands: {
        reply: { category: 'presentation', description: '展示已确认说明', fields: { text: { required: true, jsonSchema: { type: 'string' } } },
            checks: [{ occur: value => typeof value.text !== 'string', error: 'text 必须是字符串' }] },
    },
});
console.log(protocol.receive({ type: 'reply', text: '普通对象也能实现接口。' }));
console.log(protocol.modelJsonSchema);

保留同一协议,增加完整 handler 绑定 ​

配置 · Node.js

ts
import { Interaction } from '@jangts/agent-core/kits/interaction';
const protocol = Interaction.define({
  commandField: 'type', commands: {
    reply: { category: 'presentation', description: '展示已确认说明', fields: { text: { required: true, jsonSchema: { type: 'string' } } }, checks: [{ occur: value => typeof value.text !== 'string', error: 'text 必须是字符串' }] },
    unavailable: { category: 'receipt', description: '说明资料不可用', fields: {} },
  },
});
const dispatcher = Interaction.bind(protocol, {
  reply: value => '说明:' + String(value.text),
  unavailable: () => '资料暂不可用',
});
console.log(dispatcher.dispatch({ type: 'reply', text: '普通对象也能实现接口。' }, undefined));
console.log(dispatcher.catalog);
js
import { Interaction } from '@jangts/agent-core/kits/interaction';
const protocol = Interaction.define({
    commandField: 'type', commands: {
        reply: { category: 'presentation', description: '展示已确认说明', fields: { text: { required: true, jsonSchema: { type: 'string' } } }, checks: [{ occur: value => typeof value.text !== 'string', error: 'text 必须是字符串' }] },
        unavailable: { category: 'receipt', description: '说明资料不可用', fields: {} },
    },
});
const dispatcher = Interaction.bind(protocol, {
    reply: value => '说明:' + String(value.text),
    unavailable: () => '资料暂不可用',
});
console.log(dispatcher.dispatch({ type: 'reply', text: '普通对象也能实现接口。' }, undefined));
console.log(dispatcher.catalog);

直接复用单命令与批量编译入口 ​

扩展 · Node.js

使用 Interaction 时选择器由它生成;自行用 defineCommand 时也可以显式提供 discriminator。此例没有执行任何工具。

ts
import { defineCommand, compileCommands, commandSchema, CommandCatalog } from '@jangts/agent-core/kits/interaction';
const reply = defineCommand({ fields: { text: { required: true } }, checks: [{ occur: value => typeof value.text !== 'string', error: '需要文本' }] });
console.log(reply.parse({ text: '接口说明' }));
const compiled = compileCommands({ reply: { fields: { text: { required: true } } } });
const commands = new CommandCatalog(Object.entries(compiled));
console.log(commandSchema('reply', commands.require('reply')), commands.manifest());
js
import { defineCommand, compileCommands, commandSchema, CommandCatalog } from '@jangts/agent-core/kits/interaction';
const reply = defineCommand({ fields: { text: { required: true } }, checks: [{ occur: value => typeof value.text !== 'string', error: '需要文本' }] });
console.log(reply.parse({ text: '接口说明' }));
const compiled = compileCommands({ reply: { fields: { text: { required: true } } } });
const commands = new CommandCatalog(Object.entries(compiled));
console.log(commandSchema('reply', commands.require('reply')), commands.manifest());

相关策略源码 ​

相关定义 ​

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