Skip to content
中文

compileCommands ​

KIT · function · Runtime value

Compile a whole catalog with shared envelope fields.

Applications declare only their own data and handlers. Defaults are merged in this order: unrelated default keys, then the command's own keys, with fields, checks and tags concatenated so shared envelope checks always run before per-command checks.

Import ​

ts
import { compileCommands } from '@jangts/agent-core/kits/interaction';
js
import { compileCommands } from '@jangts/agent-core/kits/interaction';

Other public entry points:@jangts/agent-core/types, @jangts/agent-core/kits/interaction, @jangts/agent-core/kits.

Definition ​

Type parameters ​

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

Call ​

ts
compileCommands<Value extends object, Result = Value, Args extends unknown[] = [], Id extends string = string>(definitions: Readonly<Record<Id, CommandDefinition<Value, Result, Args>>>, defaults?: Partial<CommandDefinition<Value, Result, Args>>): Readonly<Record<Id, Command<Value, Result, Args>>>

Compile a whole catalog with shared envelope fields.

Applications declare only their own data and handlers. Defaults are merged in this order: unrelated default keys, then the command's own keys, with fields, checks and tags concatenated so shared envelope checks always run before per-command checks.

Type parameterConstraintDefault type
Valueobject—
Result—Value
Argsunknown[][]
Idstringstring
ParameterTypePassingDescription
definitionsReadonly<Record<Id, CommandDefinition<Value, Result, Args>>>Required—
defaultsPartial<CommandDefinition<Value, Result, Args>>Optional—
Fields of defaults
FieldTypeRequiredDescription
categoryInteractionCategory | undefinedOptionalRead-only. Declared purpose; validated against the frozen category list.
versionnumber | undefinedOptionalRead-only. Protocol version, used to reject incompatible registrations.
descriptionstring | undefinedOptionalRead-only. See the type definition and usage context.
terminalboolean | undefinedOptionalRead-only. Marks a command that ends the interaction.
discriminator{ readonly field: string; readonly value: string; } | undefinedOptionalRead-only. Constant field identifying the command; its value is forced to the command id.
fieldsreadonly string[] | Readonly<Record<string, ProtocolField>> | undefinedOptionalRead-only. Field declarations by name, or an ordered list of field names.
ownedFieldsreadonly string[] | undefinedOptionalRead-only. Fields the program writes; may name fields not declared in fields.
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.
modesreadonly string[] | undefinedOptionalRead-only. App-declared modes in which the command is available.
errorProtocolReason<Value, Args> | undefinedOptionalRead-only. Rejection wording for the command as a whole.
tagsreadonly string[] | undefinedOptionalRead-only. Diagnostic tags attached to rejections from this command.
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.
format((value: Value, ...args: Args) => Result) | undefinedOptionalRead-only. Formats the validated value into the command result.

Returns: Readonly<Record<Id, Command<Value, Result, Args>>>.

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

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

API reference, usage examples and developer guides.