Skip to content
中文

InteractionProtocolDefinition ​

KIT · type · Type contract

Transfer envelope plus its registered command family; no change to HTTP or SSE framing.

Import ​

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

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 ​

Type: InteractionProtocolDefinition<Value, Result, Args, Id>

Type expression
ts
type InteractionProtocolDefinition<Value extends object, Result = Value, Args extends unknown[] = [], Id extends string = string> = Omit<
  Partial<CommandDefinition<Value, Result, Args>>,
  'discriminator' | 'modes' | 'ownedFields'
> &
  CommandCatalogOptions & {
    /** Field carrying the command id; also the canonical key in normalized output. */
    readonly commandField: string;
    /** Accepted only on input. Schema and normalized output always use commandField. */
    readonly commandAliases?: readonly string[];
    /** Rejection wording when a payload selects no command. */
    readonly selectionError?: ProtocolReason<Value, Args>;
    readonly commands: Readonly<Record<Id, CommandDefinition<Value, Result, Args>>>;
  };

Type parameters ​

Type parameterConstraintDefault type
Valueobject—
Result—Value
Argsunknown[][]
Idstringstring

Field structure ​

FieldTypeRequiredDescription
descriptionstring | undefinedOptionalRead-only. See the type definition and usage context.
tagsreadonly string[] | undefinedOptionalRead-only. Diagnostic tags attached to rejections from this command. Diagnostic tags attached to every rejection from this catalog.
errorProtocolReason<Value, Args> | undefinedOptionalRead-only. Rejection wording for the command as a whole.
fieldsreadonly string[] | Readonly<Record<string, ProtocolField>> | undefinedOptionalRead-only. Field declarations by name, or an ordered list of field names.
versionnumber | undefinedOptionalRead-only. Protocol version, used to reject incompatible registrations.
format((value: Value, ...args: Args) => Result) | undefinedOptionalRead-only. Formats the validated value into the command result.
categoryInteractionCategory | undefinedOptionalRead-only. Declared purpose; validated against the frozen category list.
terminalboolean | undefinedOptionalRead-only. Marks a command that ends the interaction.
additionalPropertiesboolean | undefinedOptionalRead-only. Whether fields outside the declaration are rejected; defaults to the compiler policy.
optionalPresence"own" | "defined" | undefinedOptionalRead-only. 'defined' rejects an explicitly undefined optional property; 'own' preserves its presence.
checksreadonly ProtocolCheck<Value, Args>[] | undefinedOptionalRead-only. Declarative rejection rules, evaluated in order after the structural checks.
refine((value: Value, ...args: Args) => void) | undefinedOptionalRead-only. Domain checks/bindings only; invoked after structural and declarative checks.
modeFieldstring | undefinedOptionalRead-only. Field carrying the mode selector, when modes are used.
unknownErrorstring | Error | undefinedOptionalRead-only. Rejection wording for an unknown command selector.
modeErrorstring | Error | undefinedOptionalRead-only. Rejection wording for a command used outside its declared modes.
ownershipErrorstring | Error | undefinedOptionalRead-only. Rejection wording for a model-supplied owned field.
commandFieldstringRequiredRead-only. Field carrying the command id; also the canonical key in normalized output.
commandAliasesreadonly string[] | undefinedOptionalRead-only. Accepted only on input. Schema and normalized output always use commandField.
selectionErrorProtocolReason<Value, Args> | undefinedOptionalRead-only. Rejection wording when a payload selects no command.
commandsReadonly<Record<Id, CommandDefinition<Value, Result, Args>>>RequiredRead-only. See the type definition and usage context.

Examples ​

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);

API reference, usage examples and developer guides.