"use strict";
/**
 * Helper functions for the extract pipeline stage.
 *
 * Parsing utilities for MCP activity types, tool arguments,
 * LLM messages, and default prompt construction.
 */
Object.defineProperty(exports, "__esModule", { value: true });
exports.parseMcpActivityType = parseMcpActivityType;
exports.extractToolArgs = extractToolArgs;
exports.extractLlmMessages = extractLlmMessages;
exports.buildDefaultPrompt = buildDefaultPrompt;
const prompts_1 = require("./prompts");
/**
 * Parse an MCP activity type name back into server name + tool name.
 * Activity names follow: mcp_{serverName}_{toolName}
 */
function parseMcpActivityType(activityType) {
    const rest = activityType.slice(4);
    const firstUnderscore = rest.indexOf('_');
    if (firstUnderscore === -1) {
        return { serverName: rest, toolName: rest };
    }
    return {
        serverName: rest.slice(0, firstUnderscore),
        toolName: rest.slice(firstUnderscore + 1),
    };
}
/**
 * Extract the tool arguments from an enriched activity event's input.
 *
 * The enriched `input` field contains the raw arguments passed to the
 * activity function. The shape depends on the activity:
 * - callMcpTool(qualifiedName, args) → [qualifiedName, args]
 * - callDbTool(name, args) → [name, args]
 * - callLLM(messages, tools?) → [messages, tools?]
 * - mcp_* activities(args) → [args]
 */
function extractToolArgs(attrs) {
    const input = attrs.input;
    if (!input || !Array.isArray(input))
        return {};
    // callMcpTool / callTriageTool / callDbTool / callVisionTool: [name, args]
    if (['callMcpTool', 'callTriageTool', 'callDbTool', 'callVisionTool'].includes(attrs.activity_type)) {
        const args = input[1];
        return (args && typeof args === 'object' && !Array.isArray(args))
            ? args
            : {};
    }
    // mcp_* activities: [args] (single argument — the tool parameters)
    if (attrs.activity_type?.startsWith('mcp_')) {
        const args = input[0];
        return (args && typeof args === 'object' && !Array.isArray(args))
            ? args
            : {};
    }
    return {};
}
/**
 * Extract the LLM prompt messages from an enriched callLLM event's input.
 * callLLM(messages, tools?) → input = [messages, tools?]
 */
function extractLlmMessages(attrs) {
    const input = attrs.input;
    if (!input || !Array.isArray(input) || input.length === 0)
        return undefined;
    const messages = input[0];
    if (!Array.isArray(messages))
        return undefined;
    return messages.filter((m) => m?.role && m?.content);
}
/**
 * Build a default prompt template referencing the data available from prior tool steps.
 * The prompt uses {field} placeholders that map to input_maps at runtime.
 */
function buildDefaultPrompt(priorSteps) {
    const lastToolStep = [...priorSteps].reverse().find((s) => s.kind === 'tool');
    const fields = lastToolStep?.result && typeof lastToolStep.result === 'object' && !Array.isArray(lastToolStep.result)
        ? Object.keys(lastToolStep.result)
        : [];
    const dataRef = fields.length > 0
        ? `The data includes the following fields: ${fields.join(', ')}.`
        : 'Analyze the provided data.';
    return [
        { role: 'system', content: prompts_1.EXTRACT_DEFAULT_SYSTEM_PROMPT },
        { role: 'user', content: prompts_1.EXTRACT_DEFAULT_USER_TEMPLATE.replace('{dataRef}', dataRef) },
    ];
}
