import { redirectOversizedToolResult } from "./tools/oversized-tool-result.js";
export function _installAgentToolHooks() {
    this.agent.beforeToolCall = async ({ toolCall, args }) => {
        const runner = this._extensionRunner;
        if (!runner.hasHandlers("tool_call")) {
            return undefined;
        }
        await this._agentEventQueue;
        try {
            return await runner.emitToolCall({
                type: "tool_call",
                toolName: toolCall.name,
                toolCallId: toolCall.id,
                input: args,
            });
        }
        catch (err) {
            if (err instanceof Error) {
                throw err;
            }
            throw new Error(`Extension failed, blocking execution: ${String(err)}`);
        }
    };
    this.agent.afterToolCall = async ({ toolCall, args, result, isError }) => {
        const runner = this._extensionRunner;
        const hookResult = runner.hasHandlers("tool_result")
            ? await runner.emitToolResult({
                type: "tool_result",
                toolName: toolCall.name,
                toolCallId: toolCall.id,
                input: args,
                content: result.content,
                details: result.details,
                isError,
            })
            : undefined;
        const extensionReplacement = hookResult
            ? {
                content: hookResult.content,
                details: hookResult.details,
                isError: hookResult.isError ?? isError,
            }
            : undefined;
        const finalResult = hookResult
            ? {
                content: hookResult.content ?? result.content,
                // Preserve original details when an extension hook rewrites only content;
                // the redirect check only replaces model-visible content blocks.
                details: hookResult.details ?? result.details,
            }
            : result;
        const finalIsError = hookResult?.isError ?? isError;
        const redirectReplacement = await redirectOversizedToolResult({
            toolName: toolCall.name,
            toolCallId: toolCall.id,
            result: finalResult,
            isError: finalIsError,
            sessionId: this.sessionManager.getSessionId(),
            sessionDir: this.sessionManager.getSessionDir() || undefined,
            maxResultSizeChars: this.getToolDefinition(toolCall.name)?.maxResultSizeChars,
        });
        return redirectReplacement ?? extensionReplacement;
    };
}
/**
 * Install a prepareNextTurnWithContext hook so that extension tool changes
 * (e.g. setActiveTools) and before_agent_start systemPrompt overrides are
 * applied to the next provider request within the same run.
 */
export function _installAgentNextTurnRefresh() {
    const previousPrepareNextTurnWithContext = this.agent.prepareNextTurnWithContext ??
        (this.agent.prepareNextTurn
            ? async (_turn, signal) => await this.agent.prepareNextTurn?.(signal)
            : undefined);
    this.agent.prepareNextTurnWithContext = async (turn, signal) => {
        const previousSnapshot = await previousPrepareNextTurnWithContext?.(turn, signal);
        const previousContext = previousSnapshot?.context ?? turn.context;
        return {
            ...previousSnapshot,
            context: {
                ...previousContext,
                systemPrompt: this._systemPromptOverride ?? this._baseSystemPrompt,
                tools: this.agent.state.tools.slice(),
            },
            model: this.agent.state.model,
            thinkingLevel: this.agent.state.thinkingLevel,
        };
    };
}
export const agentSessionToolHooksMethods = {
    _installAgentToolHooks,
    _installAgentNextTurnRefresh,
};
//# sourceMappingURL=agent-session-tool-hooks.js.map