import { injectCustomToolsPrompt } from "./custom-tool-prompt.js";
export function registerCodeModeEvents(pi, runtime) {
    pi.on("before_agent_start", (event, ctx) => {
        const activeProviders = runtime.activeProviders(ctx);
        if (activeProviders.length === 0)
            return undefined;
        void runtime.prepare(ctx)?.catch(() => undefined);
        const documentationPath = activeProviders.find((provider) => provider.documentationPath)?.documentationPath;
        if (!documentationPath)
            return undefined;
        const systemPrompt = injectCustomToolsPrompt(event.systemPrompt, runtime.collectTools(ctx), documentationPath);
        return systemPrompt === event.systemPrompt ? undefined : { systemPrompt };
    });
    pi.on("tool_result", (event) => {
        if ((event.toolName === "exec" || event.toolName === "wait") &&
            event.details &&
            typeof event.details === "object" &&
            "codeMode" in event.details &&
            event.details.codeMode === true &&
            "scriptError" in event.details &&
            typeof event.details.scriptError === "string")
            return { isError: true };
        return undefined;
    });
}
