import { l as store_get, h as head, e as ensure_array_like, a as attr_class, u as unsubscribe_stores, c as bind_props } from './index2-CzosXNJA.js';
import { Y as fallback, X as escape_html, _ as getContext } from './context-BZ_07CFR.js';
import './utils-KcIDVAAe.js';
import './state.svelte-Yu4RD6aK.js';
import { C as CollapseExpand, m as mockGenerator, T as TelescopeClient } from './client-DtDK0-V3.js';
import './html-FW6Ia4bL.js';
import 'zod';

const getStores = () => {
  const stores$1 = getContext("__svelte__");
  return {
    /** @type {typeof page} */
    page: {
      subscribe: stores$1.page.subscribe
    },
    /** @type {typeof navigating} */
    navigating: {
      subscribe: stores$1.navigating.subscribe
    },
    /** @type {typeof updated} */
    updated: stores$1.updated
  };
};
const page = {
  subscribe(fn) {
    const store = getStores().page;
    return store.subscribe(fn);
  }
};
function _page($$renderer, $$props) {
  $$renderer.component(($$renderer2) => {
    var $$store_subs;
    let useMock = fallback($$props["useMock"], false);
    const client = new TelescopeClient(fetch);
    let stepTraces = [];
    let tracesLoading = false;
    let tracesError = null;
    let expandedTraceId = null;
    let lastLoadedCallId = null;
    function fmtDate(ms) {
      return new Date(ms).toLocaleString();
    }
    async function loadStepTraces(callId) {
      if (!callId || callId === lastLoadedCallId) return;
      lastLoadedCallId = callId;
      tracesLoading = true;
      tracesError = null;
      expandedTraceId = null;
      try {
        let items = [];
        if (useMock) {
          items = await mockGenerator.fetchStepTraces(callId);
        } else {
          const resp = await client.getStepTraces(callId, 50);
          items = resp.items ?? [];
          console.log("Step Traces Response:", resp);
          if (resp.items && resp.items.length > 0) {
            console.log("One step trace, first:", resp.items[0]);
            console.log("One step trace, second:", resp.items[1]);
          }
        }
        stepTraces = items.slice().sort((a, b) => a.createdAt - b.createdAt);
      } catch (e) {
        tracesError = e.message;
        stepTraces = [];
      } finally {
        tracesLoading = false;
      }
    }
    {
      const cid = store_get($$store_subs ??= {}, "$page", page).params.callId;
      if (cid && cid !== lastLoadedCallId) {
        loadStepTraces(cid);
      }
    }
    head($$renderer2, ($$renderer3) => {
      $$renderer3.title(($$renderer4) => {
        $$renderer4.push(`<title>Bosun: Telescope</title>`);
      });
    });
    $$renderer2.push(`<div class="flex h-full w-full flex-col bg-rc-gray-700"><div class="flex-none border-b border-rc-gray-300/40 p-4"><h2 class="text-lg font-semibold text-rc-gray-100">Step Traces `);
    if (store_get($$store_subs ??= {}, "$page", page).params.callId) {
      $$renderer2.push("<!--[-->");
      $$renderer2.push(`<span class="text-rc-gray-100">for call Id:</span> <span class="text-rc-green-200">${escape_html(store_get($$store_subs ??= {}, "$page", page).params.callId)}</span>`);
    } else {
      $$renderer2.push("<!--[!-->");
    }
    $$renderer2.push(`<!--]--></h2></div> <div class="flex-1 overflow-auto p-4">`);
    if (!store_get($$store_subs ??= {}, "$page", page).params.callId) {
      $$renderer2.push("<!--[-->");
      $$renderer2.push(`<div class="py-8 text-center text-rc-gray-300">No call selected</div>`);
    } else {
      $$renderer2.push("<!--[!-->");
      if (tracesLoading) {
        $$renderer2.push("<!--[-->");
        $$renderer2.push(`<div class="py-8 text-center text-rc-gray-200">Loading traces...</div>`);
      } else {
        $$renderer2.push("<!--[!-->");
        if (tracesError) {
          $$renderer2.push("<!--[-->");
          $$renderer2.push(`<div class="py-8 text-center text-red-400">Error: ${escape_html(tracesError)}</div>`);
        } else {
          $$renderer2.push("<!--[!-->");
          if (!stepTraces.length) {
            $$renderer2.push("<!--[-->");
            $$renderer2.push(`<div class="py-8 text-center text-rc-gray-300">No step traces found</div>`);
          } else {
            $$renderer2.push("<!--[!-->");
            $$renderer2.push(`<ul class="space-y-3"><!--[-->`);
            const each_array = ensure_array_like(stepTraces);
            for (let $$index = 0, $$length = each_array.length; $$index < $$length; $$index++) {
              let trace = each_array[$$index];
              $$renderer2.push(`<li class="rounded-sm border border-rc-gray-300 bg-rc-gray-700"><button class="w-full p-4 text-left hover:bg-rc-gray-600"><div class="flex items-start justify-between"><h3 class="text-lg font-semibold text-rc-gray-100">Trace Id: <span class="text-rc-green-200">${escape_html(trace.traceId)}</span></h3> <div class="flex items-center gap-3"><div class="text-sm text-rc-gray-200">${escape_html(fmtDate(trace.createdAt))}</div> <svg${attr_class("h-5 w-5 text-rc-gray-200 transition-transform", void 0, { "rotate-180": expandedTraceId === trace.traceId })} fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M19 9l-7 7-7-7"></path></svg></div></div> <div class="mt-2 text-sm text-rc-gray-200">Model: ${escape_html(trace.model)}</div> <div class="mt-2 flex gap-6 text-sm text-rc-gray-300"><div>Scope Id: ${escape_html(trace.scopeId)}</div> <div>Workflow Id: ${escape_html(trace.workflowId)}</div> <div>WorkflowRun Id: ${escape_html(trace.workflowRunId)}</div></div></button> `);
              if (expandedTraceId === trace.traceId) {
                $$renderer2.push("<!--[-->");
                $$renderer2.push(`<div class="border-t border-rc-gray-300 p-4">`);
                if (trace.renderedPrompt) {
                  $$renderer2.push("<!--[-->");
                  $$renderer2.push(`<div class="mb-4"><div class="mb-1 font-semibold text-rc-gray-100">Rendered Prompt:</div> `);
                  CollapseExpand($$renderer2, {
                    value: trace.renderedPrompt,
                    maxLines: 2,
                    highlightPrompt: true
                  });
                  $$renderer2.push(`<!----></div>`);
                } else {
                  $$renderer2.push("<!--[!-->");
                }
                $$renderer2.push(`<!--]--> `);
                if (trace.receivedPrompt) {
                  $$renderer2.push("<!--[-->");
                  $$renderer2.push(`<div class="mb-4"><div class="mb-1 font-semibold text-rc-gray-100">Received Prompt:</div> `);
                  CollapseExpand($$renderer2, {
                    value: trace.receivedPrompt,
                    maxLines: 2,
                    highlightPrompt: true
                  });
                  $$renderer2.push(`<!----></div>`);
                } else {
                  $$renderer2.push("<!--[!-->");
                }
                $$renderer2.push(`<!--]--> `);
                if (trace.receivedContext) {
                  $$renderer2.push("<!--[-->");
                  $$renderer2.push(`<div class="mb-4"><div class="mb-1 font-semibold text-rc-gray-100">Received Context:</div> `);
                  CollapseExpand($$renderer2, {
                    value: typeof trace.receivedContext === "string" ? trace.receivedContext : JSON.stringify(trace.receivedContext, null, 2),
                    maxLines: 2
                  });
                  $$renderer2.push(`<!----></div>`);
                } else {
                  $$renderer2.push("<!--[!-->");
                }
                $$renderer2.push(`<!--]--> `);
                if (trace.stringifiedConversation) {
                  $$renderer2.push("<!--[-->");
                  $$renderer2.push(`<div class="mb-4"><div class="mb-1 font-semibold text-rc-gray-100">Conversation:</div> `);
                  CollapseExpand($$renderer2, {
                    value: trace.stringifiedConversation,
                    limit: 50,
                    treatPlainText: true
                  });
                  $$renderer2.push(`<!----></div>`);
                } else {
                  $$renderer2.push("<!--[!-->");
                }
                $$renderer2.push(`<!--]--> `);
                if (trace.response) {
                  $$renderer2.push("<!--[-->");
                  $$renderer2.push(`<div class="mb-4"><div class="mb-1 font-semibold text-rc-gray-100">Response:</div> `);
                  CollapseExpand($$renderer2, { value: trace.response, limit: 50, treatPlainText: true });
                  $$renderer2.push(`<!----></div>`);
                } else {
                  $$renderer2.push("<!--[!-->");
                }
                $$renderer2.push(`<!--]--></div>`);
              } else {
                $$renderer2.push("<!--[!-->");
              }
              $$renderer2.push(`<!--]--></li>`);
            }
            $$renderer2.push(`<!--]--></ul>`);
          }
          $$renderer2.push(`<!--]-->`);
        }
        $$renderer2.push(`<!--]-->`);
      }
      $$renderer2.push(`<!--]-->`);
    }
    $$renderer2.push(`<!--]--></div></div>`);
    if ($$store_subs) unsubscribe_stores($$store_subs);
    bind_props($$props, { useMock });
  });
}

export { _page as default };
//# sourceMappingURL=_page.svelte-Dxa9NmSL.js.map
