import { RECOMMENDED_MODELS, } from '@bubblelab/shared-schemas';
import { getCapability, } from '../../capabilities/index.js';
export async function applyCapabilityPreprocessing(params, bubbleContext, resolveCapabilityCredentials) {
    const caps = params.capabilities ?? [];
    if (caps.length > 1) {
        // Multi-capability delegator: use Gemini 3 Pro for reliable tool-calling / routing.
        // Sub-agents apply their own modelConfigOverride in single-cap mode.
        params.model.model = RECOMMENDED_MODELS.BEST;
        params.model.reasoningEffort = undefined;
    }
    else {
        // Single-cap: apply that capability's model override directly
        for (const capConfig of caps) {
            const capDef = getCapability(capConfig.id);
            const override = capDef?.metadata.modelConfigOverride;
            if (!override)
                continue;
            if (override.model)
                params.model.model = override.model;
            if (override.reasoningEffort)
                params.model.reasoningEffort = override.reasoningEffort;
            if (override.maxTokens)
                params.model.maxTokens = Math.max(params.model.maxTokens ?? 0, override.maxTokens);
        }
    }
    // Inject capability system prompts
    if (caps.length > 1) {
        // Multi-capability: summaries with tool names + delegation hints — sub-agents get full prompts
        const summaries = caps
            .map((c, idx) => {
            const def = getCapability(c.id);
            if (!def)
                return null;
            const toolNames = def.metadata.tools.map((t) => t.name).join(', ');
            let summary = `${idx + 1}. "${def.metadata.name}" (id: ${c.id})\n   Purpose: ${def.metadata.description}`;
            if (toolNames)
                summary += `\n   Tools: ${toolNames}`;
            if (def.metadata.delegationHint)
                summary += `\n   When to use: ${def.metadata.delegationHint}`;
            return summary;
        })
            .filter((summary) => Boolean(summary));
        params.systemPrompt += `\n\n---\nSYSTEM CAPABILITY EXTENSIONS:\nMultiple specialized capabilities are available. You MUST delegate to them using the 'use-capability' tool.\n\nAvailable Capabilities:\n${summaries.join('\n\n')}\n\nDELEGATION RULES:\n- Use 'use-capability' tool to delegate tasks to the appropriate capability\n- Do NOT attempt to handle capability tasks yourself\n- Include full context when delegating, including all known user details and preferences from context (especially timezone)\n- Can chain multiple capabilities if needed\n- Only respond directly for: greetings, clarifications, or tasks outside all capabilities\n---\n\nYour role is to understand the user's request and delegate to the appropriate capability or respond directly when appropriate.`;
    }
    else {
        // Single or zero capabilities: eager load as before
        for (const capConfig of caps) {
            const capDef = getCapability(capConfig.id);
            if (!capDef)
                continue;
            const ctx = {
                credentials: resolveCapabilityCredentials(capDef, capConfig),
                inputs: capConfig.inputs ?? {},
                bubbleContext,
            };
            const addition = (await capDef.createSystemPrompt?.(ctx)) ??
                capDef.metadata.systemPromptAddition;
            if (addition) {
                params.systemPrompt = `${params.systemPrompt}\n\n---\nSYSTEM CAPABILITY EXTENSION:\nThe following capability has been added to enhance your functionality:\n\n[${capDef.metadata.name}]\n${addition}\n---\n\nYour primary objective is to fulfill the user's request using both your base capabilities and the extended capability above.`;
            }
        }
    }
    return params;
}
export async function applyCapabilityPostprocessing(result, params, bubbleContext, resolveCapabilityCredentials) {
    let updated = result;
    updated = await applyCapabilityResponseAppend(updated, params, bubbleContext, resolveCapabilityCredentials);
    updated = applyConversationHistoryNotice(updated, params);
    return updated;
}
async function applyCapabilityResponseAppend(result, params, bubbleContext, resolveCapabilityCredentials) {
    const caps = params.capabilities ?? [];
    // Multi-cap: sub-agents handle their own responseAppend
    if (caps.length > 1)
        return result;
    const appendParts = [];
    for (const capConfig of caps) {
        const capDef = getCapability(capConfig.id);
        if (!capDef?.createResponseAppend)
            continue;
        const ctx = {
            credentials: resolveCapabilityCredentials(capDef, capConfig),
            inputs: capConfig.inputs ?? {},
            bubbleContext,
        };
        const text = await capDef.createResponseAppend(ctx);
        if (text)
            appendParts.push(text);
    }
    if (appendParts.length > 0) {
        return {
            ...result,
            response: `${result.response}\n\n${appendParts.join('\n\n')}`,
        };
    }
    return result;
}
/**
 * Detects conversation history that wasn't properly enhanced (e.g. Slack thread
 * history fell back to raw user IDs instead of display names due to rate limits).
 * Pattern: first user message starts with a Slack user ID like "U0831C0BXEX:"
 */
function hasUnenhancedConversationHistory(history) {
    if (!history || history.length === 0)
        return false;
    const firstUserMsg = history.find((m) => m.role === 'user');
    if (!firstUserMsg)
        return false;
    const name = firstUserMsg.content.split(':')[0]?.trim();
    if (!name)
        return false;
    return /^U[A-Z0-9]{8,12}$/.test(name);
}
function applyConversationHistoryNotice(result, params) {
    const caps = params.capabilities ?? [];
    // Show for any capability-enabled main agent, not delegated sub-agents
    if (caps.length === 0 || params.name?.startsWith('Capability Agent: ')) {
        return result;
    }
    const hasHistory = params.conversationHistory && params.conversationHistory.length > 0;
    let notice = null;
    if (hasHistory &&
        hasUnenhancedConversationHistory(params.conversationHistory)) {
        notice =
            '---\n⚠️ **Conversation History Temporarily Unavailable**\n\n' +
                "I couldn't remember your past conversation at the moment due to a rate limit. " +
                'Please contact the Bubble Lab team if this issue persists.';
    }
    else if (!hasHistory) {
        notice =
            '---\n💡 **Conversation History Not Available**\n\n' +
                "I don't have access to our previous conversation or know what time it is for you, so I might ask you to repeat some information. " +
                'To enable conversation history and make our chat more seamless, you can ask Pearl in Bubble Studio to add conversation history support to this bubble.';
    }
    if (!notice)
        return result;
    const separator = result.response?.trim().length ? '\n\n' : '';
    return {
        ...result,
        response: `${result.response}${separator}${notice}`,
    };
}
//# sourceMappingURL=capability-pipeline.js.map