2026-04-02 08:58:11 +08:00
|
|
|
import type { Command, CommandSchema } from './types.js';
|
|
|
|
|
|
2026-04-02 09:05:47 +08:00
|
|
|
export type PromptEvent = {
|
|
|
|
|
schema: CommandSchema;
|
|
|
|
|
resolve: (command: Command) => void;
|
|
|
|
|
reject: (error: Error) => void;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export type CommandRunnerEvents = {
|
|
|
|
|
prompt: PromptEvent;
|
|
|
|
|
};
|
|
|
|
|
|
2026-04-02 08:58:11 +08:00
|
|
|
export type CommandRunnerContext<TContext> = {
|
|
|
|
|
context: TContext;
|
|
|
|
|
run: (input: string) => Promise<{ success: true; result: unknown } | { success: false; error: string }>;
|
|
|
|
|
runParsed: (command: Command) => Promise<{ success: true; result: unknown } | { success: false; error: string }>;
|
2026-04-02 09:05:47 +08:00
|
|
|
prompt: (schema: CommandSchema | string) => Promise<Command>;
|
|
|
|
|
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>;
|
|
|
|
|
};
|