// src/shared/stats.ts
var roundTo1 = (n) => Math.round(n * 10) / 10;
function hasValidStats(stats) {
  return typeof stats.totalTime === "number" && !Number.isNaN(stats.totalTime);
}
function getStatsIdentity(stats) {
  return {
    providerId: stats.providerId,
    providerName: stats.providerName,
    backend: stats.backend,
    model: stats.model
  };
}
function getModelGroupKey(identity) {
  return `${identity.providerId}::${identity.model}`;
}
function getModelGroupLabel(identity) {
  return `${identity.providerName} > ${identity.model}`;
}
function buildSessionStats(messagesWithStats) {
  let totalTime = 0;
  let toolTime = 0;
  let prefillTokens = 0;
  let generationTokens = 0;
  let totalPrefillTime = 0;
  let totalGenTime = 0;
  const dataPoints = [];
  const callDataPoints = [];
  let sessionCallIndex = 0;
  for (const [index, msg] of messagesWithStats.entries()) {
    const stats = msg.stats;
    const identity = getStatsIdentity(stats);
    totalTime += stats.totalTime;
    toolTime += stats.toolTime;
    prefillTokens += stats.prefillTokens;
    generationTokens += stats.generationTokens;
    const prefillTime = stats.prefillSpeed > 0 ? stats.prefillTokens / stats.prefillSpeed : 0;
    const genTime = stats.generationSpeed > 0 ? stats.generationTokens / stats.generationSpeed : 0;
    totalPrefillTime += prefillTime;
    totalGenTime += genTime;
    dataPoints.push({
      messageId: msg.id,
      timestamp: msg.timestamp,
      ...identity,
      mode: stats.mode,
      responseIndex: index + 1,
      prefillTokens: stats.prefillTokens,
      generationTokens: stats.generationTokens,
      prefillSpeed: stats.prefillSpeed,
      generationSpeed: stats.generationSpeed,
      totalTime: stats.totalTime,
      aiTime: stats.totalTime - stats.toolTime,
      toolTime: stats.toolTime
    });
    const llmCalls = stats.llmCalls ?? [];
    for (const call of llmCalls) {
      sessionCallIndex += 1;
      callDataPoints.push({
        messageId: msg.id,
        timestamp: call.timestamp ?? msg.timestamp,
        providerId: call.providerId,
        providerName: call.providerName,
        backend: call.backend,
        model: call.model,
        mode: stats.mode,
        responseIndex: index + 1,
        sessionCallIndex,
        callIndex: call.callIndex,
        promptTokens: call.promptTokens,
        completionTokens: call.completionTokens,
        ttft: call.ttft,
        completionTime: call.completionTime,
        prefillSpeed: call.prefillSpeed,
        generationSpeed: call.generationSpeed,
        totalTime: call.totalTime,
        ...call.temperature !== void 0 && { temperature: call.temperature },
        ...call.topP !== void 0 && { topP: call.topP },
        ...call.topK !== void 0 && { topK: call.topK },
        ...call.maxTokens !== void 0 && { maxTokens: call.maxTokens }
      });
    }
  }
  const avgPrefillSpeed = totalPrefillTime > 0 ? roundTo1(prefillTokens / totalPrefillTime) : 0;
  const avgGenerationSpeed = totalGenTime > 0 ? roundTo1(generationTokens / totalGenTime) : 0;
  return {
    totalTime: roundTo1(totalTime),
    aiTime: roundTo1(totalTime - toolTime),
    toolTime: roundTo1(toolTime),
    prefillTokens,
    generationTokens,
    avgPrefillSpeed,
    avgGenerationSpeed,
    responseCount: messagesWithStats.length,
    llmCallCount: callDataPoints.length,
    dataPoints,
    callDataPoints
  };
}
function computeSessionStats(messages) {
  const messagesWithStats = messages.filter((msg) => msg.stats !== void 0 && msg.stats !== null && hasValidStats(msg.stats)).sort((a, b) => new Date(a.timestamp).getTime() - new Date(b.timestamp).getTime());
  if (messagesWithStats.length === 0) {
    return null;
  }
  const modelBuckets = /* @__PURE__ */ new Map();
  for (const message of messagesWithStats) {
    const key = getModelGroupKey(getStatsIdentity(message.stats));
    const existing = modelBuckets.get(key) ?? [];
    modelBuckets.set(key, [...existing, message]);
  }
  const modelGroups = Array.from(modelBuckets.entries()).map(([key, groupMessages]) => {
    const identity = getStatsIdentity(groupMessages[0].stats);
    const groupStats = buildSessionStats(groupMessages);
    return {
      ...identity,
      key,
      label: getModelGroupLabel(identity),
      ...groupStats
    };
  });
  return {
    ...buildSessionStats(messagesWithStats),
    modelGroups
  };
}

export {
  computeSessionStats
};
//# sourceMappingURL=chunk-VUQCQXXJ.js.map