import type { Command, CommandSchema } from './types'; import { parseCommand } from './command-parse'; import { applyCommandSchema } from './command-validate'; export type PromptEvent = { schema: CommandSchema; /** 当前等待输入的玩家 */ currentPlayer: string | null; /** * 尝试提交命令 * @param commandOrInput Command 对象或命令字符串 * @returns null - 验证成功,Promise 已 resolve * @returns string - 验证失败,返回错误消息,Promise 未 resolve */ tryCommit: (commandOrInput: Command | string) => string | null; /** 取消 prompt,Promise 被 reject */ cancel: (reason?: string) => void; }; export type CommandRunnerEvents = { prompt: PromptEvent; /** 当 prompt 结束(tryCommit 成功或 cancel)时触发 */ promptEnd: void; }; export type CommandResult = { success: true; result: T; } | { success: false; error: string; } export type PromptValidator = (...params: TArgs) => TResult; export type CommandRunnerContext = { context: TContext; run: (input: string) => Promise>; runParsed: (command: Command) => Promise>; prompt: (schema: CommandSchema | string, validator: PromptValidator, currentPlayer?: string | null) => Promise; on: (event: T, listener: (e: CommandRunnerEvents[T]) => void) => void; off: (event: T, listener: (e: CommandRunnerEvents[T]) => void) => void; }; export type CommandRunnerHandler = ( this: CommandRunnerContext, command: Command ) => Promise; export type CommandRunner = { schema: CommandSchema; run: CommandRunnerHandler; }; export type CommandFunction = (ctx: TContext, ...args: any[]) => Promise; export type CommandDef> = { schema: string | CommandSchema, run: TFunc; }