// src/runtime/sandbox-events.ts
function extractLlmCallEvent(event, agentRunName) {
  if (!event || typeof event !== "object") return void 0;
  const type = String(event.type ?? "");
  const data = event.data && typeof event.data === "object" ? event.data : {};
  if (type === "llm_call" || type === "cost.usage" || type === "usage") {
    return buildLlmCall(data, agentRunName);
  }
  if (type === "message.completed" || type === "result" || type === "final") {
    const usage = data.usage;
    if (!usage || typeof usage !== "object") return void 0;
    return buildLlmCall({ ...usage, model: data.model ?? usage.model }, agentRunName);
  }
  if (type === "done") {
    const usage = data.tokenUsage;
    if (!usage || typeof usage !== "object") return void 0;
    const out = pickFiniteNumber(usage, ["outputTokens", "completion_tokens", "tokensOut"]);
    const reasoning = pickFiniteNumber(usage, ["reasoningTokens"]);
    const mergedOut = out !== void 0 || reasoning !== void 0 ? (out ?? 0) + (reasoning ?? 0) : void 0;
    return buildLlmCall(
      {
        inputTokens: usage.inputTokens,
        outputTokens: mergedOut,
        totalCostUsd: data.totalCostUsd,
        model: data.model ?? usage.model
      },
      agentRunName
    );
  }
  return void 0;
}
function sumSandboxUsage(events, agentRunName = "agent") {
  let input = 0;
  let output = 0;
  let costUsd = 0;
  for (const ev of events) {
    const call = extractLlmCallEvent(ev, agentRunName);
    if (!call) continue;
    input += call.tokensIn ?? 0;
    output += call.tokensOut ?? 0;
    costUsd += call.costUsd ?? 0;
  }
  return { input, output, costUsd };
}
function buildLlmCall(data, agentRunName) {
  const tokensIn = pickFiniteNumber(data, ["tokensIn", "inputTokens", "prompt_tokens"]);
  const tokensOut = pickFiniteNumber(data, ["tokensOut", "outputTokens", "completion_tokens"]);
  const costUsd = pickFiniteNumber(data, ["costUsd", "totalCostUsd", "cost_usd", "cost"]);
  if (tokensIn === void 0 && tokensOut === void 0 && costUsd === void 0) {
    return void 0;
  }
  const model = typeof data.model === "string" && data.model.length > 0 ? data.model : agentRunName;
  const event = {
    type: "llm_call",
    model
  };
  if (tokensIn !== void 0) event.tokensIn = tokensIn;
  if (tokensOut !== void 0) event.tokensOut = tokensOut;
  if (costUsd !== void 0) event.costUsd = costUsd;
  return event;
}
function pickFiniteNumber(data, keys) {
  for (const key of keys) {
    const value = data[key];
    if (typeof value === "number" && Number.isFinite(value)) return value;
  }
  return void 0;
}
function mapSandboxEvent(event, opts = {}) {
  if (!event || typeof event !== "object") return void 0;
  const type = String(event.type ?? "");
  const data = event.data && typeof event.data === "object" ? event.data : {};
  if (type === "message.part.updated") {
    const part = data.part && typeof data.part === "object" ? data.part : {};
    const partType = String(part.type ?? "");
    const delta = typeof data.delta === "string" ? data.delta : void 0;
    const text = delta ?? (typeof part.text === "string" ? part.text : void 0);
    if (text === void 0) return void 0;
    if (partType === "text") return { type: "text_delta", text };
    if (partType === "reasoning" || partType === "thinking")
      return { type: "reasoning_delta", text };
    return void 0;
  }
  return extractLlmCallEvent(event, opts.agentRunName ?? "agent");
}

export {
  extractLlmCallEvent,
  sumSandboxUsage,
  mapSandboxEvent
};
//# sourceMappingURL=chunk-4J6RBI3K.js.map