defineInteractionProtocol
KIT · function · Runtime value
Construct a protocol from one definition.
Import
ts
import { defineInteractionProtocol } from '@jangts/agent-core/kits/interaction';js
import { defineInteractionProtocol } from '@jangts/agent-core/kits/interaction';Other public entry points:@jangts/agent-core, @jangts/agent-core/types, @jangts/agent-core/kits/interaction, @jangts/agent-core/kits.
Definition
Type parameters
| Type parameter | Constraint | Default type |
|---|---|---|
Value | object | Record<string, unknown> |
Result | — | Value |
Args | unknown[] | [] |
Id | string | string |
Call
ts
defineInteractionProtocol<Value extends object = Record<string, unknown>, Result = Value, Args extends unknown[] = [], Id extends string = string>(definition: InteractionProtocolDefinition<Value, Result, Args, Id>): InteractionProtocol<Value, Result, Args, Id>Construct a protocol from one definition.
| Type parameter | Constraint | Default type |
|---|---|---|
Value | object | Record<string, unknown> |
Result | — | Value |
Args | unknown[] | [] |
Id | string | string |
| Parameter | Type | Passing | Description |
|---|---|---|---|
definition | InteractionProtocolDefinition<Value, Result, Args, Id> | Required | — |
Fields of definition
| Field | Type | Required | Description |
|---|---|---|---|
description | string | undefined | Optional | Read-only. See the type definition and usage context. |
tags | readonly string[] | undefined | Optional | Read-only. Diagnostic tags attached to rejections from this command. Diagnostic tags attached to every rejection from this catalog. |
error | ProtocolReason<Value, Args> | undefined | Optional | Read-only. Rejection wording for the command as a whole. |
fields | readonly string[] | Readonly<Record<string, ProtocolField>> | undefined | Optional | Read-only. Field declarations by name, or an ordered list of field names. |
version | number | undefined | Optional | Read-only. Protocol version, used to reject incompatible registrations. |
format | ((value: Value, ...args: Args) => Result) | undefined | Optional | Read-only. Formats the validated value into the command result. |
category | InteractionCategory | undefined | Optional | Read-only. Declared purpose; validated against the frozen category list. |
terminal | boolean | undefined | Optional | Read-only. Marks a command that ends the interaction. |
additionalProperties | boolean | undefined | Optional | Read-only. Whether fields outside the declaration are rejected; defaults to the compiler policy. |
optionalPresence | "own" | "defined" | undefined | Optional | Read-only. 'defined' rejects an explicitly undefined optional property; 'own' preserves its presence. |
checks | readonly ProtocolCheck<Value, Args>[] | undefined | Optional | Read-only. Declarative rejection rules, evaluated in order after the structural checks. |
refine | ((value: Value, ...args: Args) => void) | undefined | Optional | Read-only. Domain checks/bindings only; invoked after structural and declarative checks. |
modeField | string | undefined | Optional | Read-only. Field carrying the mode selector, when modes are used. |
unknownError | string | Error | undefined | Optional | Read-only. Rejection wording for an unknown command selector. |
modeError | string | Error | undefined | Optional | Read-only. Rejection wording for a command used outside its declared modes. |
ownershipError | string | Error | undefined | Optional | Read-only. Rejection wording for a model-supplied owned field. |
commandField | string | Required | Read-only. Field carrying the command id; also the canonical key in normalized output. |
commandAliases | readonly string[] | undefined | Optional | Read-only. Accepted only on input. Schema and normalized output always use commandField. |
selectionError | ProtocolReason<Value, Args> | undefined | Optional | Read-only. Rejection wording when a payload selects no command. |
commands | Readonly<Record<Id, CommandDefinition<Value, Result, Args>>> | Required | Read-only. See the type definition and usage context. |
Returns: InteractionProtocol<Value, Result, Args, Id>.
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);Reuse the single-command and batch compilation entries directly
Extension · Node.js
When you use Interaction the selector is generated for you; when you use defineCommand yourself you may also supply discriminator explicitly. This example does not execute any tool.
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());Related policy source
- Fields, checks, and the model Schema
- Selectors and command receiving
- Bind local handlers by validated command