2026-04-02 15:36:32 +08:00
|
|
|
|
import type { Command, CommandSchema } from './types';
|
2026-04-03 14:10:42 +08:00
|
|
|
|
import { parseCommand } from './command-parse';
|
|
|
|
|
|
import { applyCommandSchema } from './command-validate';
|
2026-04-02 08:58:11 +08:00
|
|
|
|
|
2026-04-02 09:05:47 +08:00
|
|
|
|
export type PromptEvent = {
|
|
|
|
|
|
schema: CommandSchema;
|
2026-04-03 14:10:42 +08:00
|
|
|
|
/**
|
2026-04-02 19:08:14 +08:00
|
|
|
|
* 尝试提交命令
|
2026-04-03 14:10:42 +08:00
|
|
|
|
* @param commandOrInput Command 对象或命令字符串
|
2026-04-02 19:08:14 +08:00
|
|
|
|
* @returns null - 验证成功,Promise 已 resolve
|
|
|
|
|
|
* @returns string - 验证失败,返回错误消息,Promise 未 resolve
|
|
|
|
|
|
*/
|
2026-04-03 14:10:42 +08:00
|
|
|
|
tryCommit: (commandOrInput: Command | string) => string | null;
|
2026-04-02 19:08:14 +08:00
|
|
|
|
/** 取消 prompt,Promise 被 reject */
|
|
|
|
|
|
cancel: (reason?: string) => void;
|
2026-04-02 09:05:47 +08:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
export type CommandRunnerEvents = {
|
|
|
|
|
|
prompt: PromptEvent;
|
|
|
|
|
|
};
|
|
|
|
|
|
|
2026-04-02 10:38:31 +08:00
|
|
|
|
export type CommandResult<T=unknown> = {
|
|
|
|
|
|
success: true;
|
|
|
|
|
|
result: T;
|
|
|
|
|
|
} | {
|
|
|
|
|
|
success: false;
|
|
|
|
|
|
error: string;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-04-02 08:58:11 +08:00
|
|
|
|
export type CommandRunnerContext<TContext> = {
|
|
|
|
|
|
context: TContext;
|
2026-04-02 10:38:31 +08:00
|
|
|
|
run: <T=unknown>(input: string) => Promise<CommandResult<T>>;
|
2026-04-02 08:58:11 +08:00
|
|
|
|
runParsed: (command: Command) => Promise<{ success: true; result: unknown } | { success: false; error: string }>;
|
2026-04-02 19:08:14 +08:00
|
|
|
|
prompt: (schema: CommandSchema | string, validator?: (command: Command) => string | null) => Promise<Command>;
|
2026-04-02 09:05:47 +08:00
|
|
|
|
on: <T extends keyof CommandRunnerEvents>(event: T, listener: (e: CommandRunnerEvents[T]) => void) => void;
|
|
|
|
|
|
off: <T extends keyof CommandRunnerEvents>(event: T, listener: (e: CommandRunnerEvents[T]) => void) => void;
|
2026-04-02 08:58:11 +08:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
export type CommandRunnerHandler<TContext, TResult> = (
|
|
|
|
|
|
this: CommandRunnerContext<TContext>,
|
|
|
|
|
|
command: Command
|
|
|
|
|
|
) => Promise<TResult>;
|
|
|
|
|
|
|
|
|
|
|
|
export type CommandRunner<TContext, TResult = unknown> = {
|
|
|
|
|
|
schema: CommandSchema;
|
|
|
|
|
|
run: CommandRunnerHandler<TContext, TResult>;
|
|
|
|
|
|
};
|