ProtocolCheck
KIT · interface · Type contract
One declarative rejection rule.
Import
ts
import type { ProtocolCheck } from '@jangts/agent-core/kits/interaction';js
/** @typedef {import('@jangts/agent-core/kits/interaction').ProtocolCheck} ProtocolCheck */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 ProtocolCheck<Value, Args>Type parameters
| Type parameter | Constraint | Default type |
|---|---|---|
Value | — | — |
Args | unknown[] | — |
Field structure
| Field | Type | Required | Description |
|---|---|---|---|
when | ((value: Value, ...args: Args) => boolean) | undefined | Optional | Read-only. Runs the check only when it returns true; must be synchronous. |
occur | ProtocolPredicate<Value, Args> | readonly ProtocolPredicate<Value, Args>[] | Required | Read-only. Failing predicate or ordered OR-list; evaluation stops at the first failure. |
error | ProtocolReason<Value, Args> | undefined | Optional | Read-only. Rejection wording; omitted means a default TypeError. |
tags | readonly string[] | undefined | Optional | Read-only. Diagnostic tags attached to the rejection. |
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