import * as fsPromises from "node:fs/promises";
import path from "node:path";
import { resolveWorkflowPath } from "@poe-code/agent-harness-tools";
import { ExperimentJournal, runExperimentLoop as runWorkspaceExperimentLoop } from "@poe-code/experiment-loop";
import { spawn as sdkSpawn } from "./spawn.js";
function createDefaultFs() {
    return {
        readFile: fsPromises.readFile,
        writeFile: async (filePath, content, options) => {
            await fsPromises.writeFile(filePath, content, options ?? { encoding: "utf8" });
        },
        readdir: fsPromises.readdir,
        stat: async (filePath) => {
            const stat = await fsPromises.stat(filePath);
            return {
                isFile: () => stat.isFile(),
                isDirectory: () => stat.isDirectory(),
                mtimeMs: stat.mtimeMs
            };
        },
        lstat: async (filePath) => {
            const stat = await fsPromises.lstat(filePath);
            return { isSymbolicLink: () => stat.isSymbolicLink() };
        },
        mkdir: async (filePath, options) => {
            await fsPromises.mkdir(filePath, options);
        },
        rmdir: async (filePath) => {
            await fsPromises.rmdir(filePath);
        },
        appendFile: async (filePath, content) => {
            await fsPromises.appendFile(filePath, content, "utf8");
        },
        rename: async (oldPath, newPath) => {
            await fsPromises.rename(oldPath, newPath);
        },
        unlink: async (filePath) => {
            await fsPromises.unlink(filePath);
        }
    };
}
function resolveJournalPath(docPath) {
    return path.join(path.dirname(docPath), `${path.basename(docPath, path.extname(docPath))}.journal.jsonl`);
}
export async function runExperiment(options) {
    return runWorkspaceExperimentLoop({
        ...options,
        runAgent: options.runAgent ?? (async (input) => {
            return await sdkSpawn.autonomous(input.agent, {
                prompt: input.prompt,
                cwd: input.cwd,
                model: input.model,
                mode: "yolo",
                ...(input.logDir ? { logDir: input.logDir } : {}),
                ...(input.logFileName ? { logFileName: input.logFileName } : {}),
                ...(options.runtime ? { runtime: options.runtime } : {}),
                ...(options.runtimeImage ? { runtimeImage: options.runtimeImage } : {}),
                ...(options.runtimeTemplate ? { runtimeTemplate: options.runtimeTemplate } : {}),
                ...(options.detach ? { detach: options.detach } : {}),
                ...(options.mountPoeCode ? { mountPoeCode: options.mountPoeCode } : {}),
                ...(options.runnerSync ? { runnerSync: options.runnerSync } : {}),
                ...(input.signal ? { signal: input.signal } : {})
            });
        })
    });
}
export async function readExperimentJournal(options) {
    const fs = options.fs ?? createDefaultFs();
    const absoluteDocPath = resolveWorkflowPath(options.docPath, options.cwd, options.homeDir);
    const journal = new ExperimentJournal(resolveJournalPath(absoluteDocPath), fs);
    return await journal.readAll();
}
export async function appendExperimentJournalEntry(options) {
    const fs = options.fs ?? createDefaultFs();
    const absoluteDocPath = resolveWorkflowPath(options.docPath, options.cwd, options.homeDir);
    const journal = new ExperimentJournal(resolveJournalPath(absoluteDocPath), fs);
    await journal.init();
    await journal.log(options.entry);
}
//# sourceMappingURL=experiment.js.map