Command
KIT · interface · Type contract
Compiled command.
Import
ts
import type { Command } from '@jangts/agent-core/kits/interaction';js
/** @typedef {import('@jangts/agent-core/kits/interaction').Command} Command */Other public entry points:@jangts/agent-core, @jangts/agent-core/types, @jangts/agent-core/structs, @jangts/agent-core/kits/interaction, @jangts/agent-core/kits.
Definition
ts
interface Command<Value, Result, Args>Type parameters
| Type parameter | Constraint | Default type |
|---|---|---|
Value | object | — |
Result | — | Value |
Args | unknown[] | [] |
Field structure
| Field | Type | Required | Description |
|---|---|---|---|
category | InteractionCategory | undefined | Optional | Read-only. See the type definition and usage context. |
version | number | undefined | Optional | Read-only. See the type definition and usage context. |
description | string | undefined | Optional | Read-only. See the type definition and usage context. |
terminal | boolean | undefined | Optional | Read-only. See the type definition and usage context. |
fieldSchemas | Record<string, unknown> | Required | Read-only. Per-field schemas, including the forced discriminator field. |
modelJsonSchema | Record<string, unknown> | Required | Read-only. Schema for model output; only modelRequired fields are required here. |
inputJsonSchema | Record<string, unknown> | Required | Read-only. Schema for runtime input; every required field is required here. |
modes | readonly string[] | undefined | Optional | Read-only. See the type definition and usage context. |
ownedFields | readonly string[] | Required | Read-only. Field names the program owns; the model must not supply them. |
keys | (value: Record<string, unknown>) => string[] | Required | Selector values carried by value, used to resolve the command before parsing. |
assert | (value: Value, ...args: Args) => void | Required | Validates without formatting; throws on the first failed check. |
parse | (value: Value, ...args: Args) => Result | Required | Validates and returns the formatted result. |
Member index
| Member | Purpose |
|---|---|
keys | Selector values carried by value, used to resolve the command before parsing. |
assert | Validates without formatting; throws on the first failed check. |
parse | Validates and returns the formatted result. |
Member details
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.
| Parameter | Type | Passing | Description |
|---|---|---|---|
value | Record<string, unknown> | Required | — |
Returns: string[].
assert
Validates without formatting; throws on the first failed check.
ts
Command.assert(value: Value, ...args: Args): voidValidates without formatting; throws on the first failed check.
| Parameter | Type | Passing | Description |
|---|---|---|---|
value | Value | Required | — |
args | Args | Rest parameter | — |
Returns: void.
parse
Validates and returns the formatted result.
ts
Command.parse(value: Value, ...args: Args): ResultValidates and returns the formatted result.
| Parameter | Type | Passing | Description |
|---|---|---|---|
value | Value | Required | — |
args | Args | Rest parameter | — |
Returns: Result.
Awaited value: Awaited<Result>.
Examples
- Interaction: declare commands and bind local handlers: The fields, purpose, selector, checks, and handler of a command come from one declaration; the model cannot add executable handlers.
First define one explanatory command and validate it
Beginner · 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);Keep the same protocol and add complete handler binding
Configuration · 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);Related policy source
- Fields, checks, and the model Schema
- Selectors and command receiving
- Bind local handlers by validated command