import { join } from "node:path";
import { writeFile } from "node:fs/promises";
import { startAgent } from "ccapi-agent";
export async function start(config) {
    startAgent({
        ...config,
        maxTurns: config.maxTurns ?? 15,
        timeoutMs: config.timeoutMs ?? 300_000,
        name: "remotion-ai",
    }, async (ctx) => {
        const compositionPath = join(ctx.projectDir, "composition.jsx");
        let claudePrompt = ctx.prompt;
        try {
            const parsed = JSON.parse(ctx.prompt);
            if (parsed.currentCode) {
                await writeFile(compositionPath, parsed.currentCode, "utf-8");
            }
            if (ctx.sessionOpts.resume) {
                const lastUserMsg = [...(parsed.messages || [])]
                    .reverse()
                    .find((m) => m.role === "user");
                claudePrompt = lastUserMsg?.content || ctx.prompt;
            }
            else {
                claudePrompt = JSON.stringify({ messages: parsed.messages || [] });
            }
        }
        catch {
            // prompt is not JSON — pass through as-is
        }
        const contentBefore = await ctx.readFileSafe(compositionPath);
        const result = await ctx.invokeClaude(claudePrompt);
        const contentAfter = await ctx.readFileSafe(compositionPath);
        if (contentAfter && contentAfter !== contentBefore) {
            return contentAfter;
        }
        return result.result;
    });
}
//# sourceMappingURL=index.js.map