const CODEX_COMPAT_SUBAGENTS = ["default", "explorer", "worker"];
function currentDate() {
    return new Date().toISOString().slice(0, 10);
}
function currentTimezone() {
    return Intl.DateTimeFormat().resolvedOptions().timeZone || "UTC";
}
export function buildCodexCompatSystemPrompt(options) {
    const childInstructions = options.isChildSession
        ? [
            "## Delegated Child Agent",
            "You are a child agent working as part of a team. Complete the delegated task, continue the thread when the parent sends more input, and return a concise final result for the parent agent.",
        ].join("\n")
        : "";
    const compatibilityInstructions = [
        "# Codex-Compatible Runtime",
        "You are running in Pibo through the codex-compat profile. Match Codex-style tool use where the exposed Pibo tools support it, while staying truthful about implemented behavior.",
        "Use Pibo's pibo_run_* tools and generated pibo_subagent_* tools for parallel work, yielded runs, and child-agent lifecycle management.",
        "Use direct execution for normal coding tasks. If a structured planning or user-input tool is not present, ask concise questions in normal chat.",
        "When web_search is selected by the profile, use it for current or externally sourced information.",
        childInstructions,
        "<environment_context>",
        `  <cwd>${options.cwd}</cwd>`,
        `  <shell>${options.shell}</shell>`,
        `  <current_date>${options.currentDate ?? currentDate()}</current_date>`,
        `  <timezone>${options.timezone ?? currentTimezone()}</timezone>`,
        `  <subagents>${CODEX_COMPAT_SUBAGENTS.join(", ")}</subagents>`,
        "</environment_context>",
        options.baseSystemPrompt,
    ];
    return compatibilityInstructions.filter((section) => section.trim().length > 0).join("\n\n");
}
export function createCodexCompatExtension(options = {}) {
    return (pi) => {
        pi.on("before_agent_start", (event, ctx) => ({
            systemPrompt: buildCodexCompatSystemPrompt({
                baseSystemPrompt: event.systemPrompt,
                cwd: ctx.cwd,
                shell: options.shell ?? process.env.SHELL ?? "bash",
                isChildSession: options.isChildSession,
            }),
        }));
    };
}
