import { parseAgentSpecifier } from "@poe-code/agent-defs";
import { agent as defaultAgent, compactionPlugin, environmentPlugin, filesPlugin, openaiChatCompletionsPlugin, openaiResponsesPlugin, policyPlugin, shellPlugin, skillsPlugin, systemPromptPlugin, webPlugin } from "@poe-code/poe-agent";
export async function executePoeAgent(agentSpec, input, createAgent = defaultAgent) {
    const { model } = parseAgentSpecifier(agentSpec);
    if (!model) {
        throw new Error(`poe-agent requires a model in the agent specifier (e.g. "poe-agent:openai/gpt-5.4"); got "${agentSpec}".`);
    }
    return createAgent()
        .model(model)
        .use(openaiResponsesPlugin())
        .use(openaiChatCompletionsPlugin())
        .use(systemPromptPlugin())
        .use(environmentPlugin(input.cwd))
        .use(filesPlugin({ cwd: input.cwd }))
        .use(shellPlugin({ cwd: input.cwd }))
        .use(webPlugin())
        .use(compactionPlugin())
        .use(skillsPlugin({ definitions: {} }))
        .use(policyPlugin({ mode: readPolicyMode(input.mode) }))
        .mcp(input.mcpServers ?? {})
        .run(input.prompt, {
        cwd: input.cwd,
        signal: input.signal,
        onStdout: input.onStdout,
        logPath: input.logPath
    });
}
function readPolicyMode(mode) {
    if (mode === undefined) {
        return undefined;
    }
    if (mode === "read" || mode === "edit" || mode === "yolo") {
        return mode;
    }
    throw new Error(`poe-agent does not support mode "${mode}". Supported modes: read, edit, yolo.`);
}
