| 41 | } |
| 42 | |
| 43 | function groupTurns(items: LiveRecent[]): TurnGroup[] { |
| 44 | const buckets = new Map<string, TurnGroup>(); |
| 45 | const order: string[] = []; |
| 46 | for (const r of items) { |
| 47 | const key = r.turn_id || `_solo:${r.request_id}`; |
| 48 | let g = buckets.get(key); |
| 49 | if (!g) { |
| 50 | g = { |
| 51 | key, |
| 52 | representative: r, |
| 53 | entries: [], |
| 54 | tierCounts: [], |
| 55 | dominantTier: "", |
| 56 | totalCost: 0, |
| 57 | hasPending: false, |
| 58 | hasRouted: false, |
| 59 | }; |
| 60 | buckets.set(key, g); |
| 61 | order.push(key); |
| 62 | } |
| 63 | g.entries.push(r); |
| 64 | if (r.prompt_preview && !g.representative.prompt_preview) g.representative = r; |
| 65 | g.totalCost += r.cost ?? 0; |
| 66 | if (r.state === "pending") g.hasPending = true; |
| 67 | if (r.state === "routed") g.hasRouted = true; |
| 68 | } |
| 69 | for (const g of buckets.values()) { |
| 70 | const counts = new Map<string, number>(); |
| 71 | for (const r of g.entries) { |
| 72 | const t = (r.tier || "").toUpperCase(); |
| 73 | if (!t) continue; |
| 74 | counts.set(t, (counts.get(t) || 0) + 1); |
| 75 | } |
| 76 | g.tierCounts = Array.from(counts.entries()).sort( |
| 77 | (a, b) => (TIER_RANK[b[0]] ?? 0) - (TIER_RANK[a[0]] ?? 0), |
| 78 | ); |
| 79 | g.dominantTier = g.tierCounts[0]?.[0] ?? ""; |
| 80 | } |
| 81 | return order.map((k) => buckets.get(k)!); |
| 82 | } |
| 83 | |
| 84 | function turnDotClass(g: TurnGroup): string { |
| 85 | if (g.hasPending) return "bg-n-interactive animate-dotPulse"; |