Skip to content
English

Command ​

KIT · interface · 类型合同

Compiled command.

导入 ​

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

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

定义 ​

ts
interface Command<Value, Result, Args>

泛型参数 ​

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

字段结构 ​

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

成员目录 ​

成员用途
keysSelector values carried by value, used to resolve the command before parsing.
assertValidates without formatting; throws on the first failed check.
parseValidates and returns the formatted result.

成员详情 ​

keys ​

Selector values carried by value, used to resolve the command before parsing.

ts
Command.keys(value: Record<string, unknown>): string[]

Selector values carried by value, used to resolve the command before parsing.

参数类型传递说明
valueRecord<string, unknown>必需—

返回: string[]。

assert ​

Validates without formatting; throws on the first failed check.

ts
Command.assert(value: Value, ...args: Args): void

Validates without formatting; throws on the first failed check.

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

返回: void。

parse ​

Validates and returns the formatted result.

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

Validates and returns the formatted result.

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

返回: Result。

等待后的值: Awaited<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);

相关策略源码 ​

相关定义 ​

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