(() => {
  const AGENT_META = {
    rose: { emoji: "\u{1F339}", name: "Rose", tier: 1 },
    lotus: { emoji: "\u{1FAB7}", name: "Lotus", tier: 2 },
    "forget-me-not": { emoji: "\u{1F33C}", name: "Forget-me-not", tier: 2 },
    sunflower: { emoji: "\u{1F33B}", name: "Sunflower", tier: 2 },
    iris: { emoji: "\u269C\uFE0F", name: "Iris", tier: 2 },
    ivy: { emoji: "\u{1F33F}", name: "Ivy", tier: 2 },
    dandelion: { emoji: "\u{1F6E1}", name: "Dandelion", tier: 2 },
    root: { emoji: "\u{1F333}", name: "Root", tier: 2 }
  };
  let _nodeCounter = 0;
  function _nextNodeId(agentId) {
    return `${agentId}-${++_nodeCounter}`;
  }
  function initActivityTree() {
    const tree = {
      version: 1,
      rootId: "rose",
      nodes: {
        rose: {
          id: "rose",
          parentId: null,
          agentId: "rose",
          agentEmoji: "\u{1F339}",
          agentName: "Rose",
          tier: 1,
          status: "running",
          task: "Orchestrating",
          toolCalls: [],
          startedAt: Date.now(),
          endedAt: null,
          duration: null,
          children: [],
          collapsed: false,
          metadata: {}
        }
      },
      stats: _emptyStats()
    };
    S.activityTree = tree;
    S.mcFilter = {};
    S.mcSort = "runtime";
    return tree;
  }
  function _emptyStats() {
    return {
      totalAgents: 0,
      runningAgents: 0,
      pendingAgents: 0,
      doneAgents: 0,
      errorAgents: 0,
      totalTools: 0,
      doneTools: 0,
      runningTools: 0,
      avgResponseTime: 0,
      totalElapsed: 0
    };
  }
  function atAddNode(opts) {
    const tree = S.activityTree;
    if (!tree) return null;
    const parentId = opts.parentId || tree.rootId;
    const parent = tree.nodes[parentId];
    if (!parent) return null;
    const meta = AGENT_META[opts.agentId];
    const tier = opts.tier || (meta ? meta.tier : 3);
    const id = _nextNodeId(opts.agentId);
    const node = {
      id,
      parentId,
      agentId: opts.agentId,
      agentEmoji: meta?.emoji || "\u2699\uFE0F",
      agentName: meta?.name || opts.agentId,
      tier,
      status: opts.status || "pending",
      task: opts.task,
      toolCalls: [],
      startedAt: opts.status === "running" ? Date.now() : null,
      endedAt: null,
      duration: null,
      children: [],
      collapsed: false,
      metadata: {}
    };
    tree.nodes[id] = node;
    parent.children.push(id);
    _recalcStats(tree);
    return node;
  }
  function atUpdateNode(nodeId, updates) {
    const tree = S.activityTree;
    if (!tree) return;
    const node = tree.nodes[nodeId];
    if (!node) return;
    Object.assign(node, updates);
    _recalcStats(tree);
  }
  function atGetNode(nodeId) {
    return S.activityTree?.nodes[nodeId] || null;
  }
  function atAddToolCall(nodeId, tc) {
    const tree = S.activityTree;
    if (!tree) return null;
    const node = tree.nodes[nodeId];
    if (!node) return null;
    if (node.status === "pending") {
      node.status = "running";
      node.startedAt = node.startedAt || Date.now();
    }
    const toolCall = {
      id: `tc-${++_nodeCounter}`,
      name: tc.name,
      status: tc.status || "running",
      args: tc.args || {},
      startedAt: Date.now(),
      endedAt: null
    };
    node.toolCalls.push(toolCall);
    _recalcStats(tree);
    return toolCall;
  }
  function atUpdateToolCall(nodeId, toolId, updates) {
    const tree = S.activityTree;
    if (!tree) return;
    const node = tree.nodes[nodeId];
    if (!node) return;
    const tc = node.toolCalls.find((t) => t.id === toolId);
    if (!tc) return;
    Object.assign(tc, updates);
    if (updates.status === "done" || updates.status === "error") {
      tc.endedAt = tc.endedAt || Date.now();
      tc.duration = tc.startedAt ? (tc.endedAt - tc.startedAt) / 1e3 : null;
    }
    _recalcStats(tree);
  }
  function atFinalizeNode(nodeId, status) {
    const tree = S.activityTree;
    if (!tree) return;
    const node = tree.nodes[nodeId];
    if (!node) return;
    node.status = status;
    node.endedAt = Date.now();
    node.duration = node.startedAt ? (node.endedAt - node.startedAt) / 1e3 : null;
    for (const tc of node.toolCalls) {
      if (tc.status === "running" || tc.status === "pending") {
        tc.status = status === "error" ? "error" : "done";
        tc.endedAt = node.endedAt;
        tc.duration = tc.startedAt ? (tc.endedAt - tc.startedAt) / 1e3 : null;
      }
    }
    for (const childId of node.children) {
      const child = tree.nodes[childId];
      if (child && (child.status === "running" || child.status === "thinking" || child.status === "pending")) {
        atFinalizeNode(childId, status);
      }
    }
    _recalcStats(tree);
  }
  function _recalcStats(tree) {
    const nodes = Object.values(tree.nodes).filter((n) => n.id !== tree.rootId);
    const tools = nodes.flatMap((n) => n.toolCalls);
    const doneDurations = nodes.filter((n) => n.duration !== null).map((n) => n.duration);
    const runningNodes = nodes.filter((n) => n.status === "running" || n.status === "thinking");
    const maxElapsed = runningNodes.reduce((max, n) => {
      const elapsed = n.startedAt ? (Date.now() - n.startedAt) / 1e3 : 0;
      return Math.max(max, elapsed);
    }, 0);
    tree.stats = {
      totalAgents: nodes.length,
      runningAgents: runningNodes.length,
      pendingAgents: nodes.filter((n) => n.status === "pending").length,
      doneAgents: nodes.filter((n) => n.status === "done").length,
      errorAgents: nodes.filter((n) => n.status === "error").length,
      totalTools: tools.length,
      doneTools: tools.filter((t) => t.status === "done").length,
      runningTools: tools.filter((t) => t.status === "running").length,
      avgResponseTime: doneDurations.length ? doneDurations.reduce((a, b) => a + b, 0) / doneDurations.length : 0,
      totalElapsed: maxElapsed
    };
  }
  function atGetStats() {
    return S.activityTree?.stats || _emptyStats();
  }
  function atReset() {
    _nodeCounter = 0;
    initActivityTree();
  }
  function formatElapsed(ms) {
    if (ms < 1e3) return `${Math.round(ms)}ms`;
    const s = Math.floor(ms / 1e3);
    if (s < 60) return `${s}s`;
    const m = Math.floor(s / 60);
    const rs = s % 60;
    if (m < 60) return `${m}m ${rs}s`;
    const h = Math.floor(m / 60);
    const rm = m % 60;
    return `${h}h ${rm}m`;
  }
  function formatDuration(seconds) {
    if (seconds === null) return "\u2014";
    if (seconds < 0.01) return "<0.01s";
    if (seconds < 1) return `${seconds.toFixed(2)}s`;
    if (seconds < 60) return `${seconds.toFixed(1)}s`;
    const m = Math.floor(seconds / 60);
    const rs = Math.round(seconds % 60);
    return `${m}m ${rs}s`;
  }
  function createMockActivityTree() {
    initActivityTree();
    const tree = S.activityTree;
    const now = Date.now();
    const lotus = atAddNode({ agentId: "lotus", task: "Analysiere Gesundheitsdaten", status: "running" });
    lotus.startedAt = now - 12e3;
    atAddToolCall(lotus.id, { name: "search_files", args: { pattern: "health*", path: "/data" } });
    atUpdateToolCall(lotus.id, lotus.toolCalls[0].id, { status: "done", result: "12 matches", duration: 0.3 });
    lotus.toolCalls[0].startedAt = now - 11e3;
    lotus.toolCalls[0].endedAt = now - 10997;
    atAddToolCall(lotus.id, { name: "read_file", args: { path: "/data/health-log.md" } });
    const sunflower = atAddNode({ agentId: "sunflower", task: "Portfolio-Analyse Q1 2026", status: "running" });
    sunflower.startedAt = now - 8e3;
    atAddToolCall(sunflower.id, { name: "browser_navigate", args: { url: "https://finance.example.com" } });
    atUpdateToolCall(sunflower.id, sunflower.toolCalls[0].id, { status: "done", result: "Page loaded", duration: 2.1 });
    sunflower.toolCalls[0].startedAt = now - 7500;
    sunflower.toolCalls[0].endedAt = now - 5400;
    atAddToolCall(sunflower.id, { name: "terminal", args: { command: "python3 analyse.py" } });
    const dandelion = atAddNode({ agentId: "dandelion", task: "Triaging unread messages", status: "pending" });
    const researcher = atAddNode({ parentId: lotus.id, agentId: "researcher", task: "Looking up nutrition data", status: "running", tier: 3 });
    researcher.agentEmoji = "\u{1F50D}";
    researcher.agentName = "Researcher";
    researcher.startedAt = now - 3e3;
    atAddToolCall(researcher.id, { name: "web_search", args: { query: "nutrition database API" } });
    _recalcStats(tree);
    return tree;
  }
  window.initActivityTree = initActivityTree;
  window.createMockActivityTree = createMockActivityTree;
  window.formatElapsed = formatElapsed;
  window.formatDuration = formatDuration;
  window.atAddNode = atAddNode;
  window.atUpdateNode = atUpdateNode;
  window.atGetNode = atGetNode;
  window.atAddToolCall = atAddToolCall;
  window.atUpdateToolCall = atUpdateToolCall;
  window.atFinalizeNode = atFinalizeNode;
  window.atGetStats = atGetStats;
  window.atReset = atReset;
  window._atTrackTool = _atTrackTool;
  window._atTrackToolComplete = _atTrackToolComplete;
  window._atTrackDone = _atTrackDone;
  window._atTrackSubagent = _atTrackSubagent;
  window._at = {
    init: initActivityTree,
    addNode: atAddNode,
    updateNode: atUpdateNode,
    getNode: atGetNode,
    addToolCall: atAddToolCall,
    updateToolCall: atUpdateToolCall,
    finalize: atFinalizeNode,
    stats: atGetStats,
    reset: atReset,
    mock: createMockActivityTree,
    formatElapsed,
    formatDuration,
    AGENT_META,
    trackSubagent: _atTrackSubagent,
  };
  let _activeAgentNodeId = null;
  let _lastToolId = null;
  function _atTrackTool(d) {
    if (!S.activityTree) initActivityTree();
    const tree = S.activityTree;
    if (d.name === "delegate_task") {
      const agentId = d.args?.agentId || d.args?.agent || d.args?.name || "unknown";
      const task = d.args?.goal || d.args?.prompt || d.args?.task || d.preview || "Delegated task";
      const node = atAddNode({ agentId, task, status: "running" });
      if (node) {
        _activeAgentNodeId = node.id;
      }
      return;
    }
    let targetNodeId = _activeAgentNodeId || tree.rootId;
    const inflight = window.INFLIGHT?.[S.session?.session_id];
    if (inflight?.thisTurnAgent) {
      const agentKey = inflight.thisTurnAgent;
      const existingNode = Object.values(tree.nodes).find(
        (n) => n.agentId === agentKey && (n.status === "running" || n.status === "thinking")
      );
      if (existingNode) {
        targetNodeId = existingNode.id;
        _activeAgentNodeId = existingNode.id;
      } else if (AGENT_META[agentKey]) {
        const task = inflight.thisTurnModel ? `${inflight.thisTurnModel}` : "Working...";
        const node = atAddNode({ agentId: agentKey, task, status: "running" });
        if (node) {
          _activeAgentNodeId = node.id;
          targetNodeId = node.id;
        }
      }
    }
    const tc = atAddToolCall(targetNodeId, {
      name: d.name,
      args: d.args || {},
      status: "running"
    });
    if (tc) {
      _lastToolId = tc.id;
    }
  }
  function _atTrackToolComplete(d) {
    if (!S.activityTree) return;
    const tree = S.activityTree;
    const activeNodes = Object.values(tree.nodes).filter(
      (n) => n.status === "running" || n.status === "thinking"
    );
    for (const node of activeNodes) {
      for (const tc of node.toolCalls) {
        if (tc.status === "running" && tc.name === d.name) {
          atUpdateToolCall(node.id, tc.id, {
            status: d.is_error ? "error" : "done",
            result: d.preview || d.result,
            duration: d.duration
          });
          return;
        }
      }
    }
  }
  function _atTrackSubagent(d) {
    if (!S.activityTree) initActivityTree();
    const tree = S.activityTree;

    if (d.event_type === 'subagent.start') {
      // Derive agentId from subagent_id if available, otherwise from goal hint.
      // subagent_id format: "lotus-1" → agentId = "lotus"
      let agentId = 'unknown';
      if (d.subagent_id) {
        // Strip numeric suffix e.g. "lotus-1" → "lotus"
        agentId = d.subagent_id.replace(/-\d+$/, '');
      }
      const task = d.goal || d.preview || `Subagent ${d.task_index ?? ''}`.trim();
      const node = atAddNode({ agentId, task, status: 'running' });
      if (node) {
        _activeAgentNodeId = node.id;
      }
      return;
    }

    if (d.event_type === 'subagent.complete' || d.event_type === 'subagent.done') {
      // Find the running node for this subagent and finalize it.
      // Use subagent_id to locate the right node.
      let targetNodeId = null;
      if (d.subagent_id) {
        const agentId = d.subagent_id.replace(/-\d+$/, '');
        const found = Object.values(tree.nodes).find(
          (n) => n.agentId === agentId && n.status === 'running'
        );
        if (found) targetNodeId = found.id;
      }
      // Fall back to _activeAgentNodeId if no subagent_id match
      if (!targetNodeId) targetNodeId = _activeAgentNodeId;
      if (targetNodeId && tree.nodes[targetNodeId]) {
        const status = d.status === 'timeout' || d.status === 'error' ? 'error' : 'done';
        atFinalizeNode(targetNodeId, status);
      }
    }
  }
  function _atTrackDone() {
    if (!S.activityTree) return;
    if (_activeAgentNodeId) {
      atFinalizeNode(_activeAgentNodeId, "done");
      _activeAgentNodeId = null;
    }
    const root = S.activityTree.nodes[S.activityTree.rootId];
    if (root) {
      root.status = "running";
    }
  }
})();
//# sourceMappingURL=data:application/json;base64,[encoded data, 36060 characters]
