import { getModel } from "../core/model-registry.js";
import { PROMPT_COMMANDS } from "../core/prompt-commands.js";
import { log } from "../core/logger.js";
import { buildUserContentWithAttachments, routePromptCommandInput } from "./prompt-routing.js";
import { toErrorItem } from "./error-item.js";
export async function submitPromptCommand({ trimmed, inputImages, currentModel, customCommands, setLastUserMessage, setDoneStatus, finalizeSubmittedUserItem, runAgent, setLiveItems, getId, reloadCustomCommands, }) {
    const promptCommandRoute = routePromptCommandInput(trimmed, PROMPT_COMMANDS, customCommands);
    if (!promptCommandRoute)
        return false;
    const { cmdName, cmdArgs, fullPrompt } = promptCommandRoute;
    log("INFO", "command", `Prompt command: /${cmdName}${cmdArgs ? ` (args: ${cmdArgs})` : ""}`);
    const imageCount = inputImages.filter((img) => img.kind === "image").length;
    const videoCount = inputImages.filter((img) => img.kind === "video").length;
    const modelInfo = getModel(currentModel);
    const modelSupportsImages = modelInfo?.supportsImages ?? true;
    const modelSupportsVideo = modelInfo?.supportsVideo ?? false;
    const userContent = buildUserContentWithAttachments(fullPrompt, inputImages, modelSupportsImages, modelSupportsVideo);
    const userItem = {
        kind: "user",
        text: trimmed,
        imageCount: imageCount > 0 ? imageCount : undefined,
        videoCount: videoCount > 0 ? videoCount : undefined,
        id: getId(),
    };
    setLastUserMessage(trimmed);
    setDoneStatus(null);
    finalizeSubmittedUserItem(userItem);
    try {
        await runAgent(userContent);
    }
    catch (err) {
        const msg = err instanceof Error ? err.message : String(err);
        log("ERROR", "error", msg);
        const isAbort = msg.includes("aborted") || msg.includes("abort");
        setLiveItems((prev) => [
            ...prev,
            isAbort
                ? { kind: "stopped", text: "Request was stopped.", id: getId() }
                : toErrorItem(err, getId()),
        ]);
    }
    reloadCustomCommands();
    return true;
}
//# sourceMappingURL=submit-prompt-command.js.map