import { readFileSync } from "fs";
import { GraphAILogger } from "graphai";
import Replicate from "replicate";
import { provider2SoundEffectAgent } from "../types/provider2agent.js";
import { apiKeyMissingError, agentGenerationError, imageAction, movieFileTarget, hasCause } from "../utils/error_cause.js";
export const soundEffectReplicateAgent = async ({ namedInputs, params, config }) => {
    const { prompt, movieFile } = namedInputs;
    const apiKey = config?.apiKey;
    const model = params.model ?? provider2SoundEffectAgent.replicate.defaultModel;
    if (!apiKey) {
        throw new Error("Replicate API key is required (REPLICATE_API_TOKEN)", {
            cause: apiKeyMissingError("soundEffectReplicateAgent", imageAction, "REPLICATE_API_TOKEN"),
        });
    }
    const replicate = new Replicate({
        auth: apiKey,
    });
    const buffer = readFileSync(movieFile);
    const uri = `data:video/quicktime;base64,${buffer.toString("base64")}`;
    const input = {
        video: uri,
        prompt,
        duration: params.duration,
        // seed: -1,
        // num_steps: 25,
        // cfg_strength: 4.5,
        // negative_prompt: "music"
    };
    try {
        const model_identifier = provider2SoundEffectAgent.replicate.modelParams[model]?.identifier ?? model;
        const output = await replicate.run(model_identifier, {
            input,
        });
        if (output && typeof output === "object" && "url" in output) {
            const videoUrl = output.url();
            const videoResponse = await fetch(videoUrl);
            if (!videoResponse.ok) {
                throw new Error(`Error downloading video: ${videoResponse.status} - ${videoResponse.statusText}`, {
                    cause: agentGenerationError("soundEffectReplicateAgent", imageAction, movieFileTarget),
                });
            }
            const arrayBuffer = await videoResponse.arrayBuffer();
            return { buffer: Buffer.from(arrayBuffer) };
        }
        return undefined;
    }
    catch (error) {
        GraphAILogger.info("Failed to generate sound effect:", error.message);
        if (hasCause(error) && error.cause) {
            throw error;
        }
        throw new Error("Failed to generate sound effect with Replicate", {
            cause: agentGenerationError("soundEffectReplicateAgent", imageAction, movieFileTarget),
        });
    }
};
const soundEffectReplicateAgentInfo = {
    name: "soundEffectReplicateAgent",
    agent: soundEffectReplicateAgent,
    mock: soundEffectReplicateAgent,
    samples: [],
    description: "Replicate Sound Effect agent (movie to movie)",
    category: ["movie"],
    author: "Receptron Team",
    repository: "https://github.com/receptron/mulmocast-cli/",
    license: "MIT",
    environmentVariables: ["REPLICATE_API_TOKEN"],
};
export default soundEffectReplicateAgentInfo;
