import { h3 as requireUtils, h4 as mapConnectionsByDestination, h5 as getParentNodes, by as emptyTokenUsageData, bx as addTokenUsageData, h6 as libExports, h7 as splitTextBySearch, d as defineComponent, c9 as useClipboard, a as useToast, x as computed, h as createElementBlock, g as openBlock, n as normalizeClass, F as Fragment, A as renderList, e as createBlock, f as createCommentVNode, l as unref, gW as __unplugin_components_0, i as createVNode, aa as _sfc_main$1, c as useI18n, _ as _export_sfc } from "./index-CeNA_ukL.js";
import { V as VueMarkdown, H as HighlightJS } from "./core-CZWe7osv.js";
var utilsExports = requireUtils();
function createNode(parent, nodeName, currentDepth, runIndex, r, children = []) {
  return {
    parent,
    node: nodeName,
    id: `${nodeName}:${runIndex}`,
    depth: currentDepth,
    startTime: r?.data?.metadata?.startTime ?? 0,
    runIndex,
    children,
    consumedTokens: getConsumedTokens(r?.data)
  };
}
function getTreeNodeData(nodeName, connectionsBySourceNode, aiData, runIndex) {
  return getTreeNodeDataRec(void 0, nodeName, 0, connectionsBySourceNode, aiData, runIndex);
}
function getTreeNodeDataRec(parent, nodeName, currentDepth, connectionsBySourceNode, aiData, runIndex) {
  const connectionsByDestinationNode = mapConnectionsByDestination(connectionsBySourceNode);
  const nodeConnections = connectionsByDestinationNode[nodeName];
  const resultData = aiData?.filter((data) => data.node === nodeName && runIndex === data.runIndex) ?? [];
  if (!nodeConnections) {
    return resultData.map((d) => createNode(parent, nodeName, currentDepth, d.runIndex, d));
  }
  const filteredAiData = aiData?.filter(({ data }) => {
    if (!data?.source || data.source.every((source) => source === null)) {
      return true;
    }
    return data.source.some(
      (source) => source?.previousNode === nodeName && source.previousNodeRun === runIndex
    );
  });
  const connectedSubNodes = getParentNodes(
    connectionsByDestinationNode,
    nodeName,
    "ALL_NON_MAIN",
    1
  );
  const treeNode = createNode(parent, nodeName, currentDepth, runIndex);
  const children = (filteredAiData ?? []).flatMap(
    (data) => connectedSubNodes.includes(data.node) ? getTreeNodeDataRec(
      treeNode,
      data.node,
      currentDepth + 1,
      connectionsBySourceNode,
      aiData,
      data.runIndex
    ) : []
  );
  treeNode.children = children;
  if (resultData.length) {
    return resultData.map(
      (r) => createNode(parent, nodeName, currentDepth, r.runIndex, r, children)
    );
  }
  return [treeNode];
}
function createAiData(nodeName, connectionsBySourceNode, getWorkflowResultDataByNodeName) {
  const connectionsByDestinationNode = mapConnectionsByDestination(connectionsBySourceNode);
  return getParentNodes(connectionsByDestinationNode, nodeName, "ALL_NON_MAIN").flatMap(
    (node) => (getWorkflowResultDataByNodeName(node) ?? []).map((task, index) => ({ node, task, index }))
  ).sort((a, b) => {
    if (a.task.executionIndex !== void 0 && b.task.executionIndex !== void 0) {
      return a.task.executionIndex - b.task.executionIndex;
    }
    const aTime = a.task.startTime ?? 0;
    const bTime = b.task.startTime ?? 0;
    return aTime - bTime;
  }).map(({ node, task, index }) => ({
    data: getReferencedData(task, false)[0],
    node,
    runIndex: index
  }));
}
function getReferencedData(taskData, withInput, withOutput) {
  if (!taskData) {
    return [];
  }
  const returnData = [];
  function addFunction(data, inOut) {
    if (!data) {
      return;
    }
    Object.keys(data).map((type) => {
      returnData.push({
        data: data[type][0],
        inOut,
        type,
        // Include source information in AI content to track which node triggered the execution
        // This enables filtering in the UI to show only relevant executions
        source: taskData.source,
        metadata: {
          executionTime: taskData.executionTime,
          startTime: taskData.startTime,
          subExecution: taskData.metadata?.subExecution
        }
      });
    });
  }
  if (withInput) {
    addFunction(taskData.inputOverride, "input");
  }
  {
    addFunction(taskData.data, "output");
  }
  return returnData;
}
function getConsumedTokens(outputRun) {
  if (!outputRun?.data) {
    return emptyTokenUsageData;
  }
  const tokenUsage = outputRun.data.reduce(
    (acc, curr) => {
      const tokenUsageData = curr.json?.tokenUsage ?? curr.json?.tokenUsageEstimate;
      if (!tokenUsageData) return acc;
      return addTokenUsageData(acc, {
        ...tokenUsageData,
        isEstimate: !!curr.json.tokenUsageEstimate
      });
    },
    emptyTokenUsageData
  );
  return tokenUsage;
}
function createHtmlFragmentWithSearchHighlight(text, search) {
  const escaped = libExports.escapeHtml(text);
  return search ? splitTextBySearch(escaped, search).map((part) => part.isMatched ? `${part.content}` : part.content).join("") : escaped;
}
function createSearchHighlightPlugin(search) {
  return (md) => {
    md.renderer.rules.text = (tokens, idx) => createHtmlFragmentWithSearchHighlight(tokens[idx].content, search);
    md.renderer.rules.code_inline = (tokens, idx, _, __, slf) => `${createHtmlFragmentWithSearchHighlight(tokens[idx].content, search)}`;
    md.renderer.rules.code_block = (tokens, idx, _, __, slf) => `
${createHtmlFragmentWithSearchHighlight(tokens[idx].content, search)}${highlighted}