Skip to content
English

CommandDefinition ​

KIT · interface · 类型合同

Declarative command shape before compilation.

导入 ​

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

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

定义 ​

ts
interface CommandDefinition<Value, Result, Args>

泛型参数 ​

类型参数约束默认类型
Valueobject—
Result—Value
Argsunknown[][]

字段结构 ​

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

使用示例 ​

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

入门 · 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);

相关策略源码 ​

相关定义 ​

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