/**
 * Handler for command_cancel messages.
 *
 * Kills a running command by its requestId. The original command_exec
 * handler will send the command_exit event when the process terminates.
 */
import { ErrorCode } from "@pocketcoder/shared/constants";
import { cancelCommand } from "../services/command-runner.js";
import { logger } from "../utils/logger.js";
export async function handleCommandCancel(message, send) {
    const msg = message;
    const { requestId, payload } = msg;
    const targetRequestId = payload.requestId;
    logger.info(`Cancel requested for command: ${targetRequestId}`);
    const found = cancelCommand(targetRequestId);
    if (!found) {
        logger.warn(`Command not found for cancellation: ${targetRequestId}`);
        send({
            type: "error",
            requestId,
            error: {
                code: ErrorCode.INVALID_REQUEST,
                message: `No running command found with requestId: ${targetRequestId}`,
            },
        });
        return;
    }
    // Acknowledge the cancellation. The command_exit event will be sent
    // by the original command_exec handler when the process terminates.
    logger.info(`Command ${targetRequestId} cancelled successfully`);
}
//# sourceMappingURL=command-cancel.js.map