@slack/radar-mcp 1.21.0 → 1.23.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (87) hide show
  1. package/README.md +114 -18
  2. package/dist/mcp/api-paths.d.ts +2 -2
  3. package/dist/mcp/api-paths.js +4 -19
  4. package/dist/mcp/dashboard-identity.d.ts +6 -0
  5. package/dist/mcp/dashboard-identity.js +11 -0
  6. package/dist/mcp/index.js +210 -145
  7. package/dist/mcp/logs-result.d.ts +1 -0
  8. package/dist/mcp/logs-result.js +1 -0
  9. package/dist/mcp/radar-export-get.d.ts +30 -0
  10. package/dist/mcp/radar-export-get.js +49 -0
  11. package/dist/mcp/radar-http.d.ts +22 -0
  12. package/dist/mcp/radar-http.js +61 -0
  13. package/dist/mcp/radar-response.d.ts +5 -1
  14. package/dist/mcp/radar-response.js +8 -3
  15. package/dist/mcp/session-backfill.js +3 -4
  16. package/dist/mcp/session-enable.d.ts +15 -0
  17. package/dist/mcp/session-enable.js +268 -19
  18. package/dist/mcp/snapshot.js +42 -26
  19. package/dist/mcp/sources-gate.js +6 -0
  20. package/dist/mcp/stale-dashboard.d.ts +23 -0
  21. package/dist/mcp/stale-dashboard.js +122 -0
  22. package/dist/mcp/switchboard-spool.d.ts +12 -0
  23. package/dist/mcp/switchboard-spool.js +107 -0
  24. package/dist/mcp/switchboard-tools.d.ts +9 -0
  25. package/dist/mcp/switchboard-tools.js +144 -6
  26. package/dist/mcp/test-cases.d.ts +14 -0
  27. package/dist/mcp/test-cases.js +77 -0
  28. package/dist/mcp/test-runs-tools.d.ts +8 -0
  29. package/dist/mcp/test-runs-tools.js +95 -0
  30. package/dist/mcp/tools.d.ts +4 -1
  31. package/dist/mcp/tools.js +101 -21
  32. package/dist/mcp/web-child-env.d.ts +4 -1
  33. package/dist/mcp/web-child-env.js +4 -1
  34. package/dist/shared/android.d.ts +44 -0
  35. package/dist/shared/android.js +234 -24
  36. package/dist/shared/constants.d.ts +9 -4
  37. package/dist/shared/constants.js +9 -4
  38. package/dist/shared/feature-flags.d.ts +1 -0
  39. package/dist/shared/feature-flags.js +1 -0
  40. package/dist/shared/ios-logs.d.ts +10 -0
  41. package/dist/shared/ios-logs.js +91 -0
  42. package/dist/shared/ios.d.ts +25 -2
  43. package/dist/shared/ios.js +157 -3
  44. package/dist/{web → shared}/log-session.d.ts +45 -31
  45. package/dist/shared/log-session.js +300 -0
  46. package/dist/shared/logcat-capture.js +17 -1
  47. package/dist/shared/radar-request-raw.d.ts +7 -0
  48. package/dist/shared/radar-request-raw.js +89 -0
  49. package/dist/shared/radar-token.d.ts +25 -0
  50. package/dist/shared/radar-token.js +164 -0
  51. package/dist/shared/stream.d.ts +3 -0
  52. package/dist/shared/stream.js +107 -15
  53. package/dist/shared/switchboard-host-store.d.ts +21 -0
  54. package/dist/shared/switchboard-host-store.js +166 -0
  55. package/dist/shared/switchboard-note.d.ts +15 -0
  56. package/dist/shared/switchboard-note.js +92 -0
  57. package/dist/shared/switchboard-report-file.d.ts +5 -0
  58. package/dist/shared/switchboard-report-file.js +26 -0
  59. package/dist/shared/switchboard-report.d.ts +20 -0
  60. package/dist/shared/switchboard-report.js +406 -0
  61. package/dist/shared/switchboard.d.ts +111 -2
  62. package/dist/shared/switchboard.js +436 -3
  63. package/dist/shared/transport.d.ts +49 -3
  64. package/dist/shared/version.d.ts +2 -0
  65. package/dist/shared/version.js +18 -0
  66. package/dist/shared/wire.d.ts +14 -1
  67. package/dist/web/install-app.d.ts +75 -8
  68. package/dist/web/install-app.js +330 -67
  69. package/dist/web/macapp/RadarApp.swift +487 -23
  70. package/dist/web/public/index.html +51 -9
  71. package/dist/web/public/next/app.js +33 -18
  72. package/dist/web/public/next/feed.js +102 -15
  73. package/dist/web/public/next/flags.js +3 -0
  74. package/dist/web/public/next/inspector.js +69 -3
  75. package/dist/web/public/next/prefs.js +5 -2
  76. package/dist/web/public/next/registry.js +18 -5
  77. package/dist/web/public/next/store.js +455 -29
  78. package/dist/web/public/next/switchboard-note.js +93 -0
  79. package/dist/web/public/next/toast.js +9 -5
  80. package/dist/web/public/next/voice-model.js +215 -35
  81. package/dist/web/public/next/voice.js +173 -18
  82. package/dist/web/server.d.ts +15 -11
  83. package/dist/web/server.js +690 -168
  84. package/dist/web/spec.d.ts +2 -1
  85. package/dist/web/spec.js +2 -1
  86. package/package.json +2 -2
  87. package/dist/web/log-session.js +0 -166
@@ -0,0 +1,93 @@
1
+ // GENERATED by scripts/mirror-shared.mjs from src/shared/switchboard-note.ts. Do not edit; edit the source.
2
+ // Kept free of imports: scripts/mirror-shared.mjs mirrors it into the browser bundle, which could
3
+ // not resolve switchboard-report.ts's Node-side import chain.
4
+ // NOTE lines are lowercase inline in the report; a caller opening a sentence with one needs this.
5
+ export function capitalizeSwitchboardNote(note) {
6
+ return note ? note.charAt(0).toUpperCase() + note.slice(1) : note;
7
+ }
8
+ // A peak at or above this is clipping. Shared here so the report and the dashboard sparkline use
9
+ // the same threshold.
10
+ export const CLIP_PEAK = 0.99;
11
+ // A turn record's `clock` in words. Shared here so the feed row and the report read the same.
12
+ // An unknown value is the device's to add, so it shows as sent rather than throwing.
13
+ const TURN_CLOCK_LABELS = {
14
+ no_card: "no card",
15
+ resumed_from_tap: "after tap",
16
+ resumed_from_server_answer: "after server answer",
17
+ };
18
+ export function switchboardClockLabel(clock) {
19
+ return Object.hasOwn(TURN_CLOCK_LABELS, clock) ? TURN_CLOCK_LABELS[clock] : clock;
20
+ }
21
+ // The saved_turn outcomes, the one list the feed row, the panel and the report read. `applied`
22
+ // (a subtitle line still held the frame's item) and `no_line` (no line held it any more, a normal
23
+ // late save, still kept) keep the turn; the drop reasons lose it. The words are the row's and the
24
+ // panel's. A server resend counts again under `applied` while its line still holds the slot, then
25
+ // under `no_line` once the line has moved on; no counting code needs the resend as its own name.
26
+ const SAVED_TURN_KEPT = { applied: "saved", no_line: "saved (line moved on)" };
27
+ const SAVED_TURN_DROPS = { missing_field: "missing field", other_session: "other session" };
28
+ const DROP_PREFIX = "dropped";
29
+ const THREAD_FLAG = "thread";
30
+ const NO_THREAD_FLAG = "no thread";
31
+ export const SAVED_TURN_DROP_OUTCOMES = Object.keys(SAVED_TURN_DROPS);
32
+ // Only these keep the turn. The panel warns on anything else, so a drop reason the device adds
33
+ // later fails closed instead of reading as a clean session.
34
+ export const SAVED_TURN_KEPT_OUTCOMES = Object.keys(SAVED_TURN_KEPT);
35
+ // The four a revision 3 build always counts, in the order the panel and the report list them.
36
+ export const SAVED_TURN_OUTCOMES = [...Object.keys(SAVED_TURN_KEPT), ...SAVED_TURN_DROP_OUTCOMES];
37
+ // Code points a free-form device string could use to fake a line break, a report boundary, or a
38
+ // hidden direction override: controls, bidi marks, zero-width characters, U+2028/2029 (a newline
39
+ // to a browser), and the BOM. Here rather than in the report so the panel strips the same set.
40
+ export const SWITCHBOARD_CONTROL_CHARS = new RegExp("[\\u0000-\\u001f\\u007f-\\u009f\\u061c\\u200b-\\u200f\\u2028\\u2029\\u202a-\\u202e\\u2066-\\u2069\\ufeff]", "g");
41
+ export function stripSwitchboardControlChars(s) {
42
+ return s.replace(SWITCHBOARD_CONTROL_CHARS, "");
43
+ }
44
+ // The words a device name could be misread by: the kept marker "saved", the retired "replaced",
45
+ // the drop prefix, the thread flag, and the known outcome names. A name is matched on its letters
46
+ // alone, lowercased, so Saved, saved_again, savedAgain, saved2 and noLine all hit, while a name
47
+ // that only shares a filler word with a label (session_closed, line_busy) stays bare.
48
+ const letters = (s) => s.toLowerCase().replace(/[^a-z]/g, "");
49
+ const SAVED_TURN_MARKERS = [...SAVED_TURN_OUTCOMES, "saved", "replaced", DROP_PREFIX, THREAD_FLAG].map(letters);
50
+ // A device-added name (an outcome, a counts key) as the panel, the feed row and the report print
51
+ // it: bare only when it is a plain token with none of the marker words in it, quoted otherwise, so
52
+ // it cannot pass for a built-in word or a count of its own. The quoted form escapes the raw name,
53
+ // anything outside printable ASCII as \uXXXX, rather than stripping it first: the escape already
54
+ // makes a control or invisible character harmless, and stripping would print two names that differ
55
+ // only by one the same.
56
+ export function switchboardDeviceToken(name) {
57
+ if (/^[A-Za-z0-9_.-]+$/.test(name) && !SAVED_TURN_MARKERS.some((m) => letters(name).includes(m)))
58
+ return name;
59
+ return JSON.stringify(name).replace(/[^\x20-\x7e]/g, (c) => `\\u${c.charCodeAt(0).toString(16).padStart(4, "0")}`);
60
+ }
61
+ // An outcome in words: "saved", "saved (line moved on)", "dropped: missing field", decided on the
62
+ // raw outcome. An unknown outcome is the device's to add and prints as a device token (above).
63
+ export function savedTurnCountWord(outcome) {
64
+ if (Object.hasOwn(SAVED_TURN_KEPT, outcome))
65
+ return SAVED_TURN_KEPT[outcome];
66
+ return Object.hasOwn(SAVED_TURN_DROPS, outcome) ? `${DROP_PREFIX}: ${SAVED_TURN_DROPS[outcome]}` : switchboardDeviceToken(outcome);
67
+ }
68
+ // A saved_turn record in words, shared by the feed row and the report like the clock above. A kept
69
+ // turn reads with its thread flag; a drop reads its reason alone; an unknown outcome reads as its
70
+ // token and never gets a thread flag, so it cannot read as a kept turn. A non-string outcome reads "".
71
+ export function savedTurnLabel(outcome, hasThread) {
72
+ if (typeof outcome !== "string")
73
+ return "";
74
+ const word = savedTurnCountWord(outcome);
75
+ return Object.hasOwn(SAVED_TURN_KEPT, outcome) ? `${word}, ${hasThread === true ? THREAD_FLAG : NO_THREAD_FLAG}` : word;
76
+ }
77
+ // A summary's `saved_turn_counts` as ordered entries: the four known outcomes first, then any key
78
+ // the device added, as sent. A count reads null when nothing counted it: absent (a revision 2 build
79
+ // counts nothing, which is not zero), a known key missing from a present object (revision 3 always
80
+ // sends all four), or a value that is not a non-negative safe integer. The caller prints "?" rather
81
+ // than a count nobody sent.
82
+ export function savedTurnCountEntries(counts) {
83
+ const obj = counts !== null && typeof counts === "object" && !Array.isArray(counts) ? counts : {};
84
+ const read = (k) => {
85
+ const v = obj[k];
86
+ return typeof v === "number" && Number.isSafeInteger(v) && v >= 0 ? v : null;
87
+ };
88
+ const out = SAVED_TURN_OUTCOMES.map((k) => ({ outcome: k, count: read(k) }));
89
+ for (const k of Object.keys(obj))
90
+ if (!SAVED_TURN_OUTCOMES.includes(k))
91
+ out.push({ outcome: k, count: read(k) });
92
+ return out;
93
+ }
@@ -62,6 +62,7 @@ export function isCaptureDownloadMatch(downloadedPath, expectedFile) {
62
62
  const middle = base.slice(stem.length, base.length - ext.length);
63
63
  return /^-\d+$/.test(middle);
64
64
  }
65
+ const TOAST_DEFAULT_MS = 4000;
65
66
  let toastTimer = null;
66
67
  // The capture the toast on screen is waiting on, or null when it is not waiting on one. Holding
67
68
  // the FILE rather than a boolean is what lets the listener below tell "this save's download
@@ -72,8 +73,8 @@ let awaitedFile = null;
72
73
  * toast unclickable and shuts the reveal gate, so a late download-finished path from an earlier
73
74
  * save cannot attach itself to this message.
74
75
  */
75
- export function showToast(message) {
76
- paint(message);
76
+ export function showToast(message, durationMs = TOAST_DEFAULT_MS) {
77
+ paint(message, durationMs);
77
78
  awaitedFile = null;
78
79
  }
79
80
  /**
@@ -81,14 +82,17 @@ export function showToast(message) {
81
82
  * server reported for THIS save.
82
83
  */
83
84
  export function showSaveToast(message, file) {
84
- paint(message);
85
+ paint(message, TOAST_DEFAULT_MS);
85
86
  awaitedFile = file;
86
87
  }
87
- function paint(message) {
88
+ function paint(message, durationMs) {
88
89
  let el = document.getElementById("radartoast");
89
90
  if (!el) {
90
91
  el = document.createElement("div");
91
92
  el.id = "radartoast";
93
+ // polite: a confirmation should wait for a pause, not interrupt the screen reader.
94
+ el.setAttribute("role", "status");
95
+ el.setAttribute("aria-live", "polite");
92
96
  document.body.appendChild(el);
93
97
  }
94
98
  el.textContent = message;
@@ -108,7 +112,7 @@ function paint(message) {
108
112
  // A toast that has faded is no longer waiting on anything, so a path arriving after it must
109
113
  // not re-arm a toast a later message has since taken over.
110
114
  awaitedFile = null;
111
- }, 4000);
115
+ }, durationMs);
112
116
  }
113
117
  // Single persistent listener: the toast on screen becomes clickable once the download IT is
114
118
  // waiting on actually finishes. Registered once at module load rather than per paint() call, since
@@ -1,6 +1,8 @@
1
1
  import { CACHE_TTL_MS } from "./registry.js";
2
+ import { CLIP_PEAK, SAVED_TURN_OUTCOMES, savedTurnCountEntries } from "./switchboard-note.js";
2
3
  // Re-exported so a model test can assert the clamp without importing the registry module itself.
3
4
  export { CACHE_TTL_MS };
5
+ export { CLIP_PEAK };
4
6
  // The handshake phases in the order the device runs them. A phase record is drawn in this order,
5
7
  // not in arrival order, so a slow late phase still sits at the right end of the waterfall.
6
8
  export const PHASE_ORDER = ["url_lookup", "prepare", "audio_claim", "mic_open", "socket", "session_ready"];
@@ -43,47 +45,75 @@ export function recordTime(r) {
43
45
  function newestTs(rows) {
44
46
  return rows.length ? recordTime(rows[rows.length - 1]) : null;
45
47
  }
46
- // The session to draw: the id on the newest `session begin` record, the larger id breaking a
47
- // timestamp tie (a restarted process's id can be smaller than the session it replaces). An
48
- // `event: "end"` record never votes, on either pass. When no begin record is present at all,
49
- // falls back to the newest non-end record's id, so a session missing its begin (evicted, or an
50
- // older build that never sent one) can still be picked up. Records without a usable session_id
51
- // or timestamp cannot vote, so a lone odd record yields null.
52
- export function newestSessionId(rows) {
48
+ export function sessionVote(r) {
49
+ if (!isRecord(r))
50
+ return null;
51
+ const id = num(r.session_id);
52
+ const t = recordTime(r);
53
+ if (id === null || t === null)
54
+ return null;
55
+ return { id, t, begin: r.kind === "session" && r.event === "begin", end: r.kind === "session" && r.event === "end" };
56
+ }
57
+ // Whether `a` beats `b` for the same slot: the later timestamp, the larger id breaking a tie (a
58
+ // restarted process's id can be smaller than the session it replaces).
59
+ export function outranks(a, b) {
60
+ return !b || a.t > b.t || (a.t === b.t && a.id > b.id);
61
+ }
62
+ // The cut from the two winning votes: the newest `session begin` names the session, else (no
63
+ // begin present at all: evicted, or an older build that never sent one) the newest non-end
64
+ // record's id does, with no begin to cut at. An `event: "end"` record never votes.
65
+ export function cutFromVotes(bestBegin, bestAny) {
66
+ if (bestBegin)
67
+ return { sessionId: bestBegin.id, beganAt: bestBegin.t };
68
+ return bestAny ? { sessionId: bestAny.id, beganAt: null } : null;
69
+ }
70
+ export function sameSessionCut(a, b) {
71
+ return a === b || (a !== null && b !== null && a.sessionId === b.sessionId && a.beganAt === b.beganAt);
72
+ }
73
+ export function newestSessionCut(rows) {
53
74
  let bestBegin = null;
54
75
  let bestAny = null;
55
76
  for (const r of rows) {
56
- if (!isRecord(r))
57
- continue;
58
- const id = num(r.session_id);
59
- const t = recordTime(r);
60
- if (id === null || t === null)
77
+ const v = sessionVote(r);
78
+ if (!v)
61
79
  continue;
62
- const isBegin = r.kind === "session" && r.event === "begin";
63
- const isEnd = r.kind === "session" && r.event === "end";
64
- if (isBegin && (!bestBegin || t > bestBegin.t || (t === bestBegin.t && id > bestBegin.id)))
65
- bestBegin = { id, t };
66
- if (!isEnd && (!bestAny || t > bestAny.t || (t === bestAny.t && id > bestAny.id)))
67
- bestAny = { id, t };
80
+ if (v.begin && outranks(v, bestBegin))
81
+ bestBegin = v;
82
+ if (!v.end && outranks(v, bestAny))
83
+ bestAny = v;
68
84
  }
69
- const best = bestBegin ?? bestAny;
70
- return best ? best.id : null;
71
- }
72
- // The records of one session, oldest first, from its LAST `session begin` record onward (a
73
- // session_id restarts with the app process, so an old process's rows under the same id must not
74
- // bleed into the new one). All of the id's rows when no begin record is present. Only object
75
- // rows with a numeric timestamp take part; anything else is dropped rather than sorted somewhere
76
- // arbitrary.
85
+ return cutFromVotes(bestBegin, bestAny);
86
+ }
87
+ export function newestSessionId(rows) {
88
+ return newestSessionCut(rows)?.sessionId ?? null;
89
+ }
90
+ // Whether a record belongs to the cut's run: its session id, timestamped at or after the run's
91
+ // begin. The one membership rule for both the panel (sessionRows) and the Switchboard feed.
92
+ export function inSessionCut(r, cut) {
93
+ if (!isRecord(r) || num(r.session_id) !== cut.sessionId)
94
+ return false;
95
+ const t = recordTime(r);
96
+ return t !== null && (cut.beganAt === null || t >= cut.beganAt);
97
+ }
98
+ // The records of one session's current run, oldest first (see SessionCut for where the run
99
+ // starts). Only object rows with a numeric timestamp take part; anything else is dropped rather
100
+ // than sorted somewhere arbitrary.
77
101
  export function sessionRows(rows, sessionId) {
78
- const all = rows
79
- .filter((r) => isRecord(r) && num(r.session_id) === sessionId && recordTime(r) !== null)
80
- .sort((a, b) => recordTime(a) - recordTime(b) || (num(a.id) ?? 0) - (num(b.id) ?? 0));
81
- let lastBeginAt = null;
82
- for (const r of all) {
83
- if (r.kind === "session" && r.event === "begin")
84
- lastBeginAt = recordTime(r);
102
+ let beganAt = null;
103
+ for (const r of rows) {
104
+ const v = sessionVote(r);
105
+ if (v && v.begin && v.id === sessionId && (beganAt === null || v.t > beganAt))
106
+ beganAt = v.t;
85
107
  }
86
- return lastBeginAt === null ? all : all.filter((r) => recordTime(r) >= lastBeginAt);
108
+ return runRows(rows, { sessionId, beganAt });
109
+ }
110
+ // The rows in `cut`'s run, oldest first. The same order whether the caller found the cut here
111
+ // (sessionRows) or kept it up to date itself (the store).
112
+ export function runRows(rows, cut) {
113
+ return rows.filter((r) => inSessionCut(r, cut)).sort(byRecordTime);
114
+ }
115
+ export function byRecordTime(a, b) {
116
+ return recordTime(a) - recordTime(b) || (num(a.id) ?? 0) - (num(b.id) ?? 0);
87
117
  }
88
118
  // The last `session end` record's timestamp, or null when the session has not ended.
89
119
  export function sessionEndAt(rows) {
@@ -246,6 +276,147 @@ export function markers(rows, window) {
246
276
  }
247
277
  return out;
248
278
  }
279
+ // `pending` requires evidence the build reports ids at all (a ready or levels record, or the
280
+ // summary key present). A ready record with no id settles it as "no id"; a null summary does
281
+ // not, since the summary is a snapshot rather than the one-shot event.
282
+ export function serverSessionId(rows, ended, summaryServerSessionId) {
283
+ let id = null;
284
+ let knownNewBuild = false;
285
+ let seenReady = false;
286
+ for (const r of rows) {
287
+ if (r.kind === "levels")
288
+ knownNewBuild = true;
289
+ if (r.kind !== "session" || r.event !== "ready")
290
+ continue;
291
+ knownNewBuild = true;
292
+ seenReady = true;
293
+ const v = str(r.server_session_id);
294
+ if (v !== null)
295
+ id = v;
296
+ }
297
+ if (summaryServerSessionId !== undefined) {
298
+ knownNewBuild = true;
299
+ if (id === null && summaryServerSessionId !== null)
300
+ id = summaryServerSessionId;
301
+ }
302
+ return { id, pending: knownNewBuild && id === null && !ended && !seenReady };
303
+ }
304
+ // Whether the rows prove the device had no server id: a held ready record, and none of them named
305
+ // one. A null summary id is not proof, since a refill taken at begin reads null before the ready
306
+ // lands; and with no ready held, the id may simply have aged out with it.
307
+ export function readyNamedNoServerId(rows) {
308
+ let seenReady = false;
309
+ for (const r of rows) {
310
+ if (r.kind !== "session" || r.event !== "ready")
311
+ continue;
312
+ if (str(r.server_session_id) !== null)
313
+ return false;
314
+ seenReady = true;
315
+ }
316
+ return seenReady;
317
+ }
318
+ // The session's saved_turn counts by outcome, not the zoom window's: the question is whether every
319
+ // turn got saved. What the tab saw is the rows plus `seen`, the outcomes the store learned on
320
+ // ingest (by record id, so a row that aged out of the cache still counts and a row held in both
321
+ // counts once). That can still miss the start of the call (opened mid-call), so the device
322
+ // summary's counts from the session refill, when there is one, are the base: each outcome reads
323
+ // that count plus what the tab saw newer than the refill, or what the tab saw when that is more.
324
+ // The four known outcomes always list, in the summary's order, then any outcome the device
325
+ // added, as sent. Null with neither a saved_turn row nor a summary count object, so an older
326
+ // build draws no counts rather than a row of zeros.
327
+ export function savedTurnCounts(rows, summary, seenOutcomes) {
328
+ const held = new Map(SAVED_TURN_OUTCOMES.map((k) => [k, 0]));
329
+ const newer = new Map();
330
+ const summaryCounts = summary?.counts;
331
+ const throughId = summary?.throughId ?? Infinity;
332
+ const byId = new Map(seenOutcomes ?? []);
333
+ const noId = [];
334
+ for (const r of rows) {
335
+ if (r.kind !== "saved_turn" || typeof r.outcome !== "string")
336
+ continue;
337
+ const id = num(r.id);
338
+ if (id === null)
339
+ noId.push(r.outcome);
340
+ else if (!byId.has(id))
341
+ byId.set(id, r.outcome);
342
+ }
343
+ let seen = byId.size > 0 || noId.length > 0;
344
+ const add = (outcome, id) => {
345
+ held.set(outcome, (held.get(outcome) ?? 0) + 1);
346
+ if (id !== null && id > throughId)
347
+ newer.set(outcome, (newer.get(outcome) ?? 0) + 1);
348
+ };
349
+ for (const [id, outcome] of byId)
350
+ add(outcome, id);
351
+ for (const outcome of noId)
352
+ add(outcome, null);
353
+ const counts = new Map(held);
354
+ if (summaryCounts !== null && typeof summaryCounts === "object" && !Array.isArray(summaryCounts)) {
355
+ seen = true;
356
+ for (const { outcome, count } of savedTurnCountEntries(summaryCounts)) {
357
+ if (count !== null)
358
+ counts.set(outcome, Math.max(held.get(outcome) ?? 0, count + (newer.get(outcome) ?? 0)));
359
+ }
360
+ }
361
+ return seen ? [...counts].map(([outcome, count]) => ({ outcome, count })) : null;
362
+ }
363
+ const DBFS_FLOOR = -60;
364
+ // Log scale: normal speech sits under 0.2 linear and would read as nearly flat on a linear axis.
365
+ export function dbfsHeight(linear) {
366
+ const v = Math.max(0, Math.min(1, linear));
367
+ if (v <= 0)
368
+ return 0;
369
+ const db = 20 * Math.log10(v);
370
+ return Math.max(0, Math.min(1, (db - DBFS_FLOOR) / -DBFS_FLOOR));
371
+ }
372
+ // Walks backward from the newest batch and stops at the first batch whose LAST sample is before
373
+ // `from`, so a batch straddling the window's near edge is not cut off.
374
+ function levelBatchPoints(rows, from, to, rmsKey, peakKey) {
375
+ const out = [];
376
+ for (let i = rows.length - 1; i >= 0; i--) {
377
+ const r = rows[i];
378
+ if (r.kind !== "levels")
379
+ continue;
380
+ const startedAt = num(r.started_at) ?? recordTime(r);
381
+ if (startedAt === null)
382
+ continue;
383
+ const offsets = Array.isArray(r.offsets_ms) ? r.offsets_ms : [];
384
+ const lastOffset = offsets.length ? num(offsets[offsets.length - 1]) ?? 0 : 0;
385
+ if (startedAt + lastOffset < from)
386
+ break;
387
+ // Skip, not stop: older batches can still be in the window.
388
+ if (startedAt > to)
389
+ continue;
390
+ const rmsArr = Array.isArray(r[rmsKey]) ? r[rmsKey] : [];
391
+ const peakArr = Array.isArray(r[peakKey]) ? r[peakKey] : [];
392
+ // Newest-to-oldest like the outer loop, so the single out.reverse() below orders both.
393
+ for (let j = offsets.length - 1; j >= 0; j--) {
394
+ const off = num(offsets[j]);
395
+ if (off === null)
396
+ continue;
397
+ const t = startedAt + off;
398
+ if (t < from || t > to)
399
+ continue;
400
+ const rms = num(rmsArr[j]);
401
+ if (rms === null)
402
+ continue;
403
+ const peak = num(peakArr[j]);
404
+ out.push({ t, rms, peak: peak === null ? rms : peak });
405
+ }
406
+ }
407
+ out.reverse();
408
+ return out;
409
+ }
410
+ // No levels records means no sparkline rows, not a flat line that would read as silence.
411
+ export function hasLevelRecords(rows) {
412
+ return rows.some((r) => r.kind === "levels");
413
+ }
414
+ export function micLevelPoints(rows, from, to) {
415
+ return levelBatchPoints(rows, from, to, "mic_rms", "mic_peak");
416
+ }
417
+ export function playbackLevelPoints(rows, from, to) {
418
+ return levelBatchPoints(rows, from, to, "playback_rms", "playback_peak");
419
+ }
249
420
  // `sessionBeginAt`, when given, lets a session younger than the window divide by its own age
250
421
  // (floored at 1 s) instead of the full window, so a 2 s old session with 10 frames reads 5.0/s
251
422
  // rather than 1.0/s. Shared by uplinkRate and downlinkRate, which differ only in which frame
@@ -415,7 +586,9 @@ export function tickStepMs(spanMs) {
415
586
  }
416
587
  // Everything the panel draws for the newest session at `now`, or null when no record can name a
417
588
  // session (no switchboard record at all, or only ones missing session_id / timestamp).
418
- export function buildVoiceModel(rows, zoom, now) {
589
+ // The caller resolves `summaryServerSessionId`, `summarySavedTurnCounts` and `savedTurnOutcomes`
590
+ // so this module stays store-free.
591
+ export function buildVoiceModel(rows, zoom, now, summaryServerSessionId, summarySavedTurnCounts, savedTurnOutcomes) {
419
592
  const sessionId = newestSessionId(rows);
420
593
  if (sessionId === null)
421
594
  return null;
@@ -501,6 +674,7 @@ export function buildVoiceModel(rows, zoom, now) {
501
674
  }
502
675
  const beginAt = beginRec ? recordTime(beginRec) : null;
503
676
  const whole = sessionSpan(modelRows, closeAt);
677
+ const hasLevels = hasLevelRecords(modelRows);
504
678
  return {
505
679
  sessionId,
506
680
  connection,
@@ -528,5 +702,11 @@ export function buildVoiceModel(rows, zoom, now) {
528
702
  // real to have been clamped away from, whatever its earliest surviving record's own age is.
529
703
  wholeSpanClamped: hasBegin && whole.clamped,
530
704
  ended: endedAt !== null,
705
+ serverSession: serverSessionId(modelRows, endedAt !== null, summaryServerSessionId),
706
+ serverSessionNone: readyNamedNoServerId(modelRows),
707
+ savedTurns: savedTurnCounts(modelRows, summarySavedTurnCounts, savedTurnOutcomes),
708
+ hasLevels,
709
+ micLevels: hasLevels ? micLevelPoints(modelRows, window.from, window.to) : [],
710
+ playbackLevels: hasLevels ? playbackLevelPoints(modelRows, window.from, window.to) : [],
531
711
  };
532
712
  }