| import type { ActivityEvent } from "./types"; |
|
|
| export type Cat = "reasoning" | "tool" | "generating" | "idle"; |
|
|
| export interface OverviewTurn { |
| start: number; |
| end: number; |
| counts: { reasoning: number; tool: number; generating: number }; |
| |
| |
| |
| |
| seq: Exclude<Cat, "idle">[]; |
| |
| |
| |
| |
| |
| |
| |
| times: number[]; |
| |
| |
| |
| timed: boolean; |
| task: string; |
| } |
|
|
| export const MIN_TURN_SEC = 1; |
|
|
| export function tsSec(s: string): number { |
| const t = Date.parse(s); |
| return Number.isNaN(t) ? NaN : t / 1000; |
| } |
|
|
| function eventTimeBounds(events: ActivityEvent[]): { min: number; max: number } { |
| let min = Infinity; |
| let max = -Infinity; |
| for (const e of events) { |
| const t = tsSec(e.timestamp); |
| if (Number.isFinite(t)) { |
| min = Math.min(min, t); |
| max = Math.max(max, t); |
| } |
| } |
| return { min, max }; |
| } |
|
|
| export function eventCat(ev: ActivityEvent): Exclude<Cat, "idle"> { |
| if (ev.event_type === "tool_result") return "tool"; |
| if (ev.event_type === "thinking_start") return "reasoning"; |
| return "generating"; |
| } |
|
|
| function emptyCounts() { |
| return { reasoning: 0, tool: 0, generating: 0 }; |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| function resolveTurnTimes( |
| start: number, |
| end: number, |
| raw: { ts: number; exact: boolean }[], |
| trustDb = false, |
| ): { times: number[]; timed: boolean } { |
| const n = raw.length; |
| if (!n) return { times: [], timed: false }; |
| const clamp = (t: number) => Math.min(end, Math.max(start, t)); |
| const times = raw.map((r) => ((r.exact || trustDb) && Number.isFinite(r.ts) ? clamp(r.ts) : NaN)); |
| if (!times.some((t) => Number.isFinite(t))) return { times: [], timed: false }; |
|
|
| |
| const anchors: { i: number; t: number }[] = [{ i: -1, t: start }]; |
| times.forEach((t, i) => { if (Number.isFinite(t)) anchors.push({ i, t }); }); |
| anchors.push({ i: n, t: end }); |
| for (let a = 0; a < anchors.length - 1; a++) { |
| const lo = anchors[a]; |
| const hi = anchors[a + 1]; |
| const gap = hi.i - lo.i; |
| if (gap <= 1) continue; |
| for (let i = lo.i + 1; i < hi.i; i++) { |
| times[i] = lo.t + (hi.t - lo.t) * ((i - lo.i) / gap); |
| } |
| } |
|
|
| |
| |
| |
| let last = start; |
| for (let i = 0; i < n; i++) { |
| const t = Number.isFinite(times[i]) ? times[i] : last; |
| times[i] = Math.min(end, Math.max(last, t)); |
| last = times[i]; |
| } |
| return { times, timed: times[n - 1] - times[0] > MIN_TURN_SEC }; |
| } |
|
|
| function syntheticUserEvent(text: string, startSec: number): ActivityEvent { |
| return { |
| timestamp: new Date(startSec * 1000).toISOString(), |
| event_type: "user_message", |
| icon: "👤", |
| title: "User", |
| detail: text, |
| tool_name: "", |
| is_error: false, |
| files_touched: [], |
| }; |
| } |
|
|
| |
| export function buildOverviewTurns( |
| events: ActivityEvent[], |
| opts: { |
| endTime?: number; |
| startTime?: string; |
| /** Latest desk activity (ISO) from the overview API — not session.ended_at. */ |
| deskEndTime?: string; |
| taskContent?: string | null; |
| liveEvents?: ActivityEvent[]; |
| } = {}, |
| ): OverviewTurn[] { |
| const { endTime, startTime, deskEndTime, taskContent, liveEvents = [] } = opts; |
| const merged = [...events, ...liveEvents]; |
| const bounds = eventTimeBounds(merged); |
|
|
| const hasUser = merged.some((e) => e.event_type === "user_message"); |
| const deskStartSec = tsSec(startTime ?? ""); |
| const eventMin = Number.isFinite(bounds.min) ? bounds.min : NaN; |
| |
| const spanStart = Number.isFinite(eventMin) && Number.isFinite(deskStartSec) |
| ? Math.min(eventMin, deskStartSec) |
| : Number.isFinite(eventMin) |
| ? eventMin |
| : Number.isFinite(deskStartSec) |
| ? deskStartSec |
| : NaN; |
| const fallbackStart = Number.isFinite(spanStart) ? spanStart : Date.now() / 1000 - 60; |
|
|
| const feed: ActivityEvent[] = [...merged]; |
| if (!hasUser && taskContent?.trim()) { |
| feed.unshift(syntheticUserEvent(taskContent.trim(), fallbackStart)); |
| } |
|
|
| |
| |
| const out: OverviewTurn[] = []; |
| const rawByTurn: { ts: number; exact: boolean }[][] = []; |
| let cur: OverviewTurn | null = null; |
| let curRaw: { ts: number; exact: boolean }[] | null = null; |
| for (const e of feed) { |
| if (e.event_type === "user_message") { |
| let t = tsSec(e.timestamp); |
| if (!Number.isFinite(t)) t = cur?.end ?? fallbackStart; |
| if (cur) cur.end = Math.max(cur.end, t, cur.start + MIN_TURN_SEC); |
| cur = { |
| start: t, |
| end: t, |
| counts: emptyCounts(), |
| seq: [], |
| times: [], |
| timed: false, |
| task: e.detail.trim().slice(0, 80) || "(task)", |
| }; |
| out.push(cur); |
| curRaw = []; |
| rawByTurn.push(curRaw); |
| } else if (cur && curRaw) { |
| const c = eventCat(e); |
| cur.counts[c] += 1; |
| cur.seq.push(c); |
| curRaw.push({ ts: tsSec(e.timestamp), exact: e.time_exact !== false }); |
| } |
| } |
|
|
| |
| if (!out.length) { |
| const agentish = feed.filter((e) => e.event_type !== "user_message"); |
| if (agentish.length > 0 || taskContent?.trim()) { |
| const counts = emptyCounts(); |
| const seq: Exclude<Cat, "idle">[] = []; |
| const raw: { ts: number; exact: boolean }[] = []; |
| for (const e of agentish) { |
| const c = eventCat(e); |
| counts[c] += 1; |
| seq.push(c); |
| raw.push({ ts: tsSec(e.timestamp), exact: e.time_exact !== false }); |
| } |
| out.push({ |
| start: fallbackStart, |
| end: fallbackStart, |
| counts, |
| seq, |
| times: [], |
| timed: false, |
| task: taskContent?.trim().slice(0, 80) || "(task)", |
| }); |
| rawByTurn.push(raw); |
| } |
| } |
|
|
| const now = Date.now() / 1000; |
| |
| |
| |
| |
| |
| const liveEndSec = tsSec(deskEndTime ?? ""); |
| const end = typeof endTime === "number" && Number.isFinite(endTime) |
| ? (Number.isFinite(bounds.max) ? Math.max(endTime, bounds.max) : endTime) |
| : Number.isFinite(liveEndSec) |
| ? Math.max(liveEndSec, now) |
| : Number.isFinite(bounds.max) |
| ? Math.max(bounds.max, now) |
| : now; |
| if (out.length) { |
| const last = out[out.length - 1]; |
| last.end = Math.max(last.end, end, last.start + MIN_TURN_SEC); |
| } |
|
|
| |
| for (let i = 0; i < out.length - 1; i++) { |
| if (out[i].end <= out[i].start) { |
| out[i].end = Math.max(out[i].start + MIN_TURN_SEC, out[i + 1].start); |
| } |
| } |
|
|
| const needsEvenSpread = |
| out.length > 0 && |
| (out.some((t) => t.end <= t.start) || |
| (out.length > 1 && out.every((t) => t.start === out[0].start))); |
|
|
| const eventSpan = |
| Number.isFinite(bounds.min) && Number.isFinite(bounds.max) ? bounds.max - bounds.min : 0; |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| const noneExact = merged.length > 0 && merged.every((e) => e.time_exact === false); |
| const distinctTs = new Set( |
| merged.map((e) => tsSec(e.timestamp)).filter((t) => Number.isFinite(t)).map((t) => Math.round(t)), |
| ).size; |
| const trustDbTimes = noneExact && eventSpan > MIN_TURN_SEC && distinctTs > 2; |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| const hasExactTimes = merged.some((e) => e.time_exact === true); |
| const deskSpan = Number.isFinite(spanStart) ? end - spanStart : 0; |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| const timestampsClustered = |
| !trustDbTimes && !hasExactTimes && |
| out.length > 1 && Number.isFinite(spanStart) && deskSpan > 600 && eventSpan < deskSpan * 0.15; |
|
|
| |
| |
| |
| |
| |
| |
| |
| const redistributed = |
| (needsEvenSpread && !hasExactTimes && !trustDbTimes) || timestampsClustered; |
| if (redistributed) { |
| const t0 = Number.isFinite(spanStart) ? spanStart : out[0].start; |
| const t1 = Math.max(end, t0 + out.length * MIN_TURN_SEC); |
| const slice = (t1 - t0) / out.length; |
| out.forEach((t, i) => { |
| t.start = t0 + i * slice; |
| t.end = t0 + (i + 1) * slice; |
| }); |
| out[out.length - 1].end = t1; |
| } |
|
|
| |
| |
| |
| |
| if (!redistributed) { |
| out.forEach((turn, i) => { |
| const { times, timed } = resolveTurnTimes(turn.start, turn.end, rawByTurn[i] ?? [], trustDbTimes); |
| turn.times = times; |
| turn.timed = timed; |
| }); |
| } |
|
|
| return out.filter((t) => t.end > t.start); |
| } |
|
|