import { spawnSync } from "node:child_process";
import path from "node:path";
import { SEED_DEFAULT_AGENT } from "./constants.js";
import { runOpenCodeSync } from "./opencode-command.js";
import { isSeedSupportedPlatform, unsupportedPlatformMessage } from "./platform.js";
export function buildOpenCodeRunArgs(options) {
    const args = ["run", "--command", "seed", "--agent", options.agent ?? process.env.OPENCODE_DEFAULT_AGENT ?? SEED_DEFAULT_AGENT];
    if (options.model)
        args.push("--model", options.model);
    if (options.variant)
        args.push("--variant", options.variant);
    if (options.directory)
        args.push("--dir", options.directory);
    if (options.json)
        args.push("--format", "json");
    if (options.passthrough?.length)
        args.push(...options.passthrough);
    args.push(options.message);
    return args;
}
export function runSeedAgent(options) {
    const platform = options.platform ?? process.platform;
    if (!isSeedSupportedPlatform(platform)) {
        console.error(unsupportedPlatformMessage(platform));
        return 1;
    }
    const spawn = options.spawn ?? spawnSync;
    const cwd = options.directory ? path.resolve(options.directory) : process.cwd();
    const env = options.env ?? process.env;
    const result = runOpenCodeSync(spawn, buildOpenCodeRunArgs(options), {
        cwd,
        env,
        encoding: "utf8",
        stdio: "inherit",
    }, { env, platform });
    if (typeof result.status === "number")
        return result.status;
    if (result.error) {
        console.error(`Failed to run OpenCode via ${result.command}: ${result.error.message}`);
    }
    return 1;
}
//# sourceMappingURL=run.js.map