@action-state-group/capsule-viewer 0.1.0 → 0.3.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 (90) hide show
  1. package/README.md +73 -2
  2. package/dist/alias-interpreters.d.ts +18 -0
  3. package/dist/alias-interpreters.js +50 -0
  4. package/dist/alias-interpreters.js.map +1 -0
  5. package/dist/browser.d.ts +5 -2
  6. package/dist/browser.js +19 -5
  7. package/dist/browser.js.map +1 -1
  8. package/dist/builtin-manifests.d.ts +51 -0
  9. package/dist/builtin-manifests.js +196 -0
  10. package/dist/builtin-manifests.js.map +1 -0
  11. package/dist/compliance-styles.d.ts +1 -8
  12. package/dist/compliance-styles.js +41 -3
  13. package/dist/compliance-styles.js.map +1 -1
  14. package/dist/compliance-view.d.ts +41 -1
  15. package/dist/compliance-view.js +737 -303
  16. package/dist/compliance-view.js.map +1 -1
  17. package/dist/compliance.d.ts +33 -4
  18. package/dist/compliance.js +92 -13
  19. package/dist/compliance.js.map +1 -1
  20. package/dist/composed-styles.d.ts +12 -0
  21. package/dist/composed-styles.js +20 -0
  22. package/dist/composed-styles.js.map +1 -0
  23. package/dist/composed-view.d.ts +1 -1
  24. package/dist/composed-view.js +9 -2
  25. package/dist/composed-view.js.map +1 -1
  26. package/dist/contract-report-wording.d.ts +195 -0
  27. package/dist/contract-report-wording.js +1141 -0
  28. package/dist/contract-report-wording.js.map +1 -0
  29. package/dist/contract-report.d.ts +92 -0
  30. package/dist/contract-report.js +63 -0
  31. package/dist/contract-report.js.map +1 -0
  32. package/dist/emitter-shell.html +176 -2
  33. package/dist/emitter.d.ts +36 -0
  34. package/dist/emitter.js +64 -1
  35. package/dist/emitter.js.map +1 -1
  36. package/dist/evidence-graph-view.d.ts +60 -7
  37. package/dist/evidence-graph-view.js +1344 -106
  38. package/dist/evidence-graph-view.js.map +1 -1
  39. package/dist/evidence-graph.iife.js +12410 -5624
  40. package/dist/frame-tokens.generated.d.ts +3 -0
  41. package/dist/frame-tokens.generated.js +5 -0
  42. package/dist/frame-tokens.generated.js.map +1 -0
  43. package/dist/index.d.ts +3 -0
  44. package/dist/index.js +5 -0
  45. package/dist/index.js.map +1 -1
  46. package/dist/outcome-report-presentation.d.ts +48 -0
  47. package/dist/outcome-report-presentation.js +90 -0
  48. package/dist/outcome-report-presentation.js.map +1 -0
  49. package/dist/outcome-report-styles.d.ts +1 -16
  50. package/dist/outcome-report-styles.js +48 -45
  51. package/dist/outcome-report-styles.js.map +1 -1
  52. package/dist/outcome-report-view.d.ts +75 -2
  53. package/dist/outcome-report-view.js +1039 -436
  54. package/dist/outcome-report-view.js.map +1 -1
  55. package/dist/presentation-builder.d.ts +55 -6
  56. package/dist/presentation-builder.js +163 -69
  57. package/dist/presentation-builder.js.map +1 -1
  58. package/dist/presentation-fragment.d.ts +129 -0
  59. package/dist/presentation-fragment.js +213 -0
  60. package/dist/presentation-fragment.js.map +1 -0
  61. package/dist/presentation-mount.d.ts +2 -2
  62. package/dist/presentation-mount.js +1 -1
  63. package/dist/presentation-mount.js.map +1 -1
  64. package/dist/presentation-registry.d.ts +277 -0
  65. package/dist/presentation-registry.js +416 -0
  66. package/dist/presentation-registry.js.map +1 -0
  67. package/dist/record-content.d.ts +68 -0
  68. package/dist/record-content.js +153 -0
  69. package/dist/record-content.js.map +1 -0
  70. package/dist/record-dating.d.ts +10 -0
  71. package/dist/record-dating.js +76 -0
  72. package/dist/record-dating.js.map +1 -0
  73. package/dist/report-front-styles.d.ts +9 -0
  74. package/dist/report-front-styles.js +55 -0
  75. package/dist/report-front-styles.js.map +1 -0
  76. package/dist/report-front.d.ts +78 -0
  77. package/dist/report-front.js +274 -0
  78. package/dist/report-front.js.map +1 -0
  79. package/dist/report-levels.d.ts +206 -0
  80. package/dist/report-levels.js +739 -0
  81. package/dist/report-levels.js.map +1 -0
  82. package/dist/report-provenance.d.ts +111 -0
  83. package/dist/report-provenance.js +535 -0
  84. package/dist/report-provenance.js.map +1 -0
  85. package/dist/viewer-build.d.ts +37 -0
  86. package/dist/viewer-build.js +29 -0
  87. package/dist/viewer-build.js.map +1 -0
  88. package/dist/wording/contract-report-eu-ai-act.en.json +295 -0
  89. package/dist/wording/contract-report-outcomes.en.json +394 -0
  90. package/package.json +3 -2
@@ -0,0 +1,739 @@
1
+ // SPDX-License-Identifier: BSD-3-Clause
2
+ import { parseJudgeProbabilities, } from "@action-state-group/agent-action-capsule";
3
+ /**
4
+ * The two levels every human-facing report card has, and nothing deeper:
5
+ *
6
+ * summary -- what was checked and the result per rule, in plain words
7
+ * level 1 -- toggle: the conversations behind a line, listed in place,
8
+ * each one readable as it stands (its result, what the user
9
+ * and the agent said, which tools it called); a conversation
10
+ * is never a toggle of its own
11
+ * level 2 -- toggle: the proofs for one conversation (record ids,
12
+ * digests, judge pin, how to recompute)
13
+ *
14
+ * There is no third level: a reader toggles once to see the conversations
15
+ * and the tools, toggles again for one conversation's proofs, and that is
16
+ * all. No <details> ever sits inside more than one other <details>, and a
17
+ * level-1 toggle never holds another level-1 toggle.
18
+ *
19
+ * Shared by both groupings of the contract-report module (contract-report.ts:
20
+ * outcome-report-view.ts and compliance-view.ts). Everything here draws only
21
+ * from values the callers already read from the verified bundle; it reads
22
+ * nothing itself. Its words come from the card's wording pack (`Words`).
23
+ * Toggles are native <details> (keyboard-accessible, no script-driven modal);
24
+ * a toggle's body is built the first time it is opened, and inside an open
25
+ * level-1 toggle each conversation's body is built when it scrolls near the
26
+ * view (`lazyBody`), so a month of conversations does not multiply the
27
+ * page's DOM for content nobody read. A page that runs no script has every
28
+ * body built before it is written (`fillForNoScript`).
29
+ */
30
+ function element(tag, text) {
31
+ const value = document.createElement(tag);
32
+ if (text !== undefined)
33
+ value.textContent = text;
34
+ return value;
35
+ }
36
+ function el(tag, className, text) {
37
+ const value = element(tag, text);
38
+ value.className = className;
39
+ return value;
40
+ }
41
+ // ---------------------------------------------------------------------------
42
+ // Toggles
43
+ // ---------------------------------------------------------------------------
44
+ /**
45
+ * A level-1 or level-2 toggle. `summary` is what the closed toggle says;
46
+ * `build` makes the body on first open (or at once with `eager`).
47
+ */
48
+ export function levelToggle(level, summary, build, options = {}) {
49
+ const details = document.createElement("details");
50
+ details.className = `lv lv${level}${options.className === undefined ? "" : ` ${options.className}`}`;
51
+ details.dataset.level = String(level);
52
+ const head = document.createElement("summary");
53
+ if (typeof summary === "string")
54
+ head.textContent = summary;
55
+ else
56
+ head.append(summary);
57
+ details.append(head);
58
+ deferBody(details, build, options.eager === true);
59
+ return details;
60
+ }
61
+ /**
62
+ * A <details> whose body is built the first time it opens, or at once with
63
+ * `eager`, into the toggle itself or into `into` (a table row's detail cell). Until then it is marked `data-body="pending"`, and
64
+ * `fillPendingBodies` can build it without opening it.
65
+ */
66
+ export function deferBody(details, build, eager = false, into = details) {
67
+ const fill = () => {
68
+ if (details.dataset.body !== "pending")
69
+ return;
70
+ pendingToggles.delete(details);
71
+ details.dataset.body = "built";
72
+ into.append(build());
73
+ openBuiltToDepth(into);
74
+ };
75
+ details.dataset.body = "pending";
76
+ if (eager) {
77
+ fill();
78
+ return;
79
+ }
80
+ pendingToggles.set(details, fill);
81
+ details.addEventListener("toggle", () => {
82
+ if (details.open)
83
+ fill();
84
+ });
85
+ }
86
+ /** Each toggle's pending fill, keyed weakly by the toggle. */
87
+ const pendingToggles = new WeakMap();
88
+ /**
89
+ * Builds every body under `root` that `selector` names and is still
90
+ * pending -- toggle bodies and conversation bodies alike, and the ones those
91
+ * bodies bring -- leaving every toggle open or closed as it was. The static
92
+ * package builds every body this way (`fillForNoScript`), so a toggle a
93
+ * reader opens on a page that runs no script is not empty. Returns how
94
+ * many bodies it built.
95
+ */
96
+ export function fillPendingBodies(root, selector = '[data-body="pending"]') {
97
+ let built = 0;
98
+ for (;;) {
99
+ const pending = [...root.querySelectorAll(selector)];
100
+ const fills = pending
101
+ .map((host) => host.localName === "details"
102
+ ? pendingToggles.get(host)
103
+ : pendingBodies.get(host))
104
+ .filter((fill) => fill !== undefined);
105
+ if (fills.length === 0)
106
+ return built;
107
+ for (const fill of fills)
108
+ fill();
109
+ built += fills.length;
110
+ }
111
+ }
112
+ /**
113
+ * For a page that runs no script (the static package,
114
+ * presentation-builder.ts): builds every body still pending under `root`,
115
+ * and the ones those bodies bring, so opening any toggle shows what it
116
+ * shows where script runs (capsule-viewer#67). Each conversation is kept
117
+ * once, in the core's records section: a card's conversation becomes a
118
+ * link to it (`linkConversations`), including the conversations a body
119
+ * brings when it is built. Every toggle stays open or closed as it was,
120
+ * except where the host opened a region to L2, as on the scripted page.
121
+ * Returns how many bodies it built; a body with no fill registered here
122
+ * stays pending, for the caller to report.
123
+ */
124
+ export function fillForNoScript(root) {
125
+ let built = 0;
126
+ for (;;) {
127
+ linkConversations(root);
128
+ const more = fillPendingBodies(root, '[data-body="pending"]:not([data-section="conversation"])');
129
+ if (more === 0)
130
+ return built;
131
+ built += more;
132
+ }
133
+ }
134
+ /**
135
+ * The one nesting rule for every report page (SEAMS ruling 11): a level-2
136
+ * toggle may sit inside a level-1 toggle, and nothing deeper. Returns every
137
+ * breach in `root`, by the summary text of the offending toggle: a toggle
138
+ * inside two others, a level-1 toggle inside another toggle, or a level-2
139
+ * toggle inside anything but a level-1 toggle (another level 2, say).
140
+ * Empty when the page keeps the rule. Run it over a page with every toggle
141
+ * opened, so lazily built bodies are counted.
142
+ */
143
+ export function nestingProblems(root) {
144
+ const problems = [];
145
+ for (const details of root.querySelectorAll("details")) {
146
+ const outer = [];
147
+ for (let at = details.parentElement?.closest("details"); at !== null && at !== undefined; at = at.parentElement?.closest("details"))
148
+ outer.push(at);
149
+ const name = JSON.stringify(details.querySelector(":scope > summary")?.textContent ?? "");
150
+ if (outer.length > 1)
151
+ problems.push(`${name} is nested ${outer.length + 1} toggles deep`);
152
+ else if (outer.length === 1 && outer[0].dataset.level !== undefined) {
153
+ if (details.dataset.level !== "2")
154
+ problems.push(`${name} is not level 2 but sits inside a toggle`);
155
+ else if (outer[0].dataset.level !== "1")
156
+ problems.push(`${name} is level 2 but sits inside a level-${outer[0].dataset.level} toggle, not a level-1 one`);
157
+ }
158
+ }
159
+ return problems;
160
+ }
161
+ /**
162
+ * At depth L2 everything is open (presentation contract section 9.1),
163
+ * including the toggles a body holds when it is built after the page was
164
+ * drawn: inside a region the host opened to L2 (`data-open-all`), a newly
165
+ * built body's toggles open too.
166
+ */
167
+ export function openBuiltToDepth(body) {
168
+ if (body.closest("[data-open-all]") === null)
169
+ return;
170
+ for (const toggle of body.querySelectorAll("details.lv"))
171
+ if (!toggle.open)
172
+ openToggle(toggle);
173
+ }
174
+ /** Opens a toggle and builds its body now (tests, print). */
175
+ export function openToggle(details) {
176
+ details.open = true;
177
+ // The event of the element's own window: a static page is drawn into a
178
+ // document that is not the global one.
179
+ const view = details.ownerDocument.defaultView;
180
+ details.dispatchEvent(new (view === null ? Event : view.Event)("toggle"));
181
+ }
182
+ /**
183
+ * A conversation's body inside an open level-1 toggle, in place (never a
184
+ * toggle). Built when it comes within a screen or so of the view; where the
185
+ * browser has no IntersectionObserver it is built at once. `beforeprint`
186
+ * builds every pending body so a printed page is whole.
187
+ */
188
+ export function lazyBody(host, build) {
189
+ host.dataset.body = "pending";
190
+ const Observer = globalThis.IntersectionObserver;
191
+ let observer;
192
+ const fill = () => {
193
+ if (host.dataset.body !== "pending")
194
+ return;
195
+ observer?.disconnect();
196
+ pendingBodies.delete(host);
197
+ host.dataset.body = "built";
198
+ host.append(build());
199
+ openBuiltToDepth(host);
200
+ };
201
+ if (Observer === undefined) {
202
+ fill();
203
+ return;
204
+ }
205
+ observer = new Observer((entries) => {
206
+ if (entries.some((e) => e.isIntersecting))
207
+ fill();
208
+ }, { rootMargin: "1200px 0px" });
209
+ observer.observe(host);
210
+ // No listener per body: one `beforeprint` listener for the whole page
211
+ // fills the bodies still pending in the document. A body that was built,
212
+ // or whose card a host has since unmounted, holds no listener, so nothing
213
+ // keeps an old card's model alive.
214
+ pendingBodies.set(host, fill);
215
+ watchPrint(host.ownerDocument);
216
+ }
217
+ /** Each pending body's fill, keyed weakly by its host, so a host dropped from the page is collected with its model. */
218
+ const pendingBodies = new WeakMap();
219
+ const printWatched = new WeakSet();
220
+ /** One `beforeprint` listener per document: builds every body still pending in it, and only those still in the page. */
221
+ function watchPrint(doc) {
222
+ const view = doc.defaultView;
223
+ if (view === null || printWatched.has(doc))
224
+ return;
225
+ printWatched.add(doc);
226
+ view.addEventListener("beforeprint", () => {
227
+ for (const host of doc.querySelectorAll('[data-body="pending"]'))
228
+ pendingBodies.get(host)?.();
229
+ });
230
+ }
231
+ export function judgedOf(reportState, judgePinDigest) {
232
+ if (reportState === "withheld")
233
+ return "withheld";
234
+ if (reportState !== "disclosed")
235
+ return "unknown";
236
+ return judgePinDigest === undefined ? "no" : "yes";
237
+ }
238
+ /**
239
+ * One rule's result in words, from the card's own grouping prefix
240
+ * (`lines.verdict.*` or `groups.verdict.*`: the bare `verdict.*` namespace
241
+ * stays contract-report/v1 §3's). `judged` picks among the pack's four
242
+ * words for a rule nobody could decide: a judge ran and could not decide
243
+ * (`.not_evaluable.judged`), none ran (`.unjudged`), the bundle
244
+ * withholds the report that would say which (`.withheld`), or a judge's
245
+ * report is cited but this bundle does not carry a readable copy of it --
246
+ * mismatched, unreadable, invalid, or simply not carried (`.unavailable`),
247
+ * never `.withheld`, which would claim more than those states show. A
248
+ * failed or unsupported claim failed verification, or its evidence is
249
+ * missing; it is never "not checked".
250
+ */
251
+ export function ruleWord(verdict, judged, w) {
252
+ const v = (key) => w.t(`${w.grouping}.verdict.${key}`);
253
+ switch (verdict) {
254
+ case "failed":
255
+ return { word: v("failed"), tone: "ne" };
256
+ case "unsupported":
257
+ return { word: v("unsupported"), tone: "ne" };
258
+ case "met":
259
+ return { word: v("met"), tone: "ok" };
260
+ case "not_met":
261
+ return { word: v("not_met"), tone: "bad" };
262
+ case "not_applicable":
263
+ return { word: v("not_applicable"), tone: "na" };
264
+ case "out_of_scope":
265
+ return { word: v("out_of_scope"), tone: "na" };
266
+ case "not_yet_in_force":
267
+ return { word: v("not_yet_in_force"), tone: "na" };
268
+ case "not_evaluable":
269
+ return {
270
+ word: v(judged === "yes"
271
+ ? "not_evaluable.judged"
272
+ : judged === "no"
273
+ ? "not_evaluable.unjudged"
274
+ : judged === "withheld"
275
+ ? "not_evaluable.withheld"
276
+ : "not_evaluable.unavailable"),
277
+ tone: "ne",
278
+ };
279
+ }
280
+ }
281
+ /**
282
+ * The judge's note in plain words. A sealed rationale that is the judge
283
+ * backend's own synthesized line ("jev choice: not_met (probabilities={...},
284
+ * confidence=...)") reads "The judge was 78% sure." (`judge.note.sure`); any
285
+ * other rationale (a recomputed check's own explanation) is shown as sealed.
286
+ * Display only: the proofs always carry the rationale exactly as sealed.
287
+ */
288
+ export function judgeNote(rationale, verdict, w) {
289
+ if (!/^jev choice:/u.test(rationale))
290
+ return rationale;
291
+ // The same parse the model uses for P (anchored on the probabilities
292
+ // block, a value only in [0, 1]), so the note and the proofs agree.
293
+ const probabilities = parseJudgeProbabilities(rationale).probabilities ?? {};
294
+ const p = Object.hasOwn(probabilities, verdict)
295
+ ? probabilities[verdict]
296
+ : undefined;
297
+ return p === undefined || !Number.isFinite(p) || p < 0 || p > 1
298
+ ? rationale
299
+ : w.t("judge.note.sure", { p: Math.round(p * 100) });
300
+ }
301
+ export function wordPill(worded, key) {
302
+ const pill = el("span", `pill ${worded.tone}`, worded.word);
303
+ pill.dataset.pill = key;
304
+ return pill;
305
+ }
306
+ /**
307
+ * `tau2:airline:<model>:task-20:trial-1` reads "task 20, trial 1" with the
308
+ * model beside it; the older `tau2:airline:task-20:trial-0` reads "task 20,
309
+ * trial 0"; any other id is shown whole.
310
+ */
311
+ export function caseParts(conversationId, w) {
312
+ const parts = conversationId.split(":");
313
+ const at = parts.findIndex((p) => /^task-[0-9]+$/u.test(p));
314
+ if (at === -1)
315
+ return { label: conversationId };
316
+ const task = parts[at].slice("task-".length);
317
+ const trial = /^trial-([0-9]+)$/u.exec(parts[at + 1] ?? "")?.[1];
318
+ const model = at > 2 ? parts.slice(2, at).join(":") : undefined;
319
+ return {
320
+ label: trial === undefined
321
+ ? w.t("case.label", { task })
322
+ : w.t("case.label.trial", { task, trial }),
323
+ ...(model === undefined || model.length === 0 ? {} : { model }),
324
+ };
325
+ }
326
+ // ---------------------------------------------------------------------------
327
+ // The conversation, readable
328
+ // ---------------------------------------------------------------------------
329
+ /** The pack's label for a transcript role; an unknown role is shown as the transcript names it. */
330
+ function roleLabel(role, w) {
331
+ return role === "user" ||
332
+ role === "assistant" ||
333
+ role === "tool" ||
334
+ role === "system"
335
+ ? w.t(`chat.role.${role}`)
336
+ : role;
337
+ }
338
+ /** The text with every whitespace character outside JSON strings removed. */
339
+ function jsonWithoutSpace(text) {
340
+ let out = "";
341
+ let inString = false;
342
+ let escaped = false;
343
+ for (const ch of text) {
344
+ if (inString) {
345
+ out += ch;
346
+ if (escaped)
347
+ escaped = false;
348
+ else if (ch === "\\")
349
+ escaped = true;
350
+ else if (ch === '"')
351
+ inString = false;
352
+ }
353
+ else if (ch === '"') {
354
+ inString = true;
355
+ out += ch;
356
+ }
357
+ else if (!/\s/u.test(ch))
358
+ out += ch;
359
+ }
360
+ return out;
361
+ }
362
+ /**
363
+ * A sealed tool result, shown verbatim. It is indented for
364
+ * reading only when doing so changes nothing but whitespace: JSON.parse then
365
+ * JSON.stringify must give back exactly the original once whitespace outside
366
+ * strings is set aside. A number JSON cannot hold exactly
367
+ * (`{"id":12345678901234567890}`), a duplicate key, an escape written
368
+ * differently: any of these, and the original text is shown as it is.
369
+ */
370
+ export function exactText(text) {
371
+ const trimmed = text.trim();
372
+ if (!trimmed.startsWith("{") && !trimmed.startsWith("[")) {
373
+ return { text, indented: false };
374
+ }
375
+ let value;
376
+ try {
377
+ value = JSON.parse(trimmed);
378
+ }
379
+ catch {
380
+ return { text, indented: false };
381
+ }
382
+ if (JSON.stringify(value) !== jsonWithoutSpace(trimmed))
383
+ return { text, indented: false };
384
+ return { text: JSON.stringify(value, null, 2), indented: true };
385
+ }
386
+ /**
387
+ * The conversation as a chat: who said what, and each tool the agent called
388
+ * with its arguments. The words come from the transcript the caller has
389
+ * already tied to the judged case by digest (`verified`), or to the case
390
+ * every claim cites when the reports are withheld (`cited`, said as such);
391
+ * nothing is reworded. Returns the "not in this bundle" note otherwise.
392
+ */
393
+ export function renderChat(transcript, caseRecord, w) {
394
+ const wrap = el("div", "chat");
395
+ wrap.dataset.transcript = transcript.state;
396
+ if (transcript.state !== "verified" && transcript.state !== "cited") {
397
+ wrap.append(el("p", "absent", transcript.state === "absent"
398
+ ? w.t("chat.absent", {
399
+ record: caseRecord ?? w.t("chat.record.not-stated"),
400
+ })
401
+ : w.t("chat.not-shown", {
402
+ reason: transcript.reason ?? w.t("chat.not-shown.default-reason"),
403
+ })));
404
+ return wrap;
405
+ }
406
+ const turns = transcript.turns ?? [];
407
+ const calls = turns.reduce((n, t) => n + t.toolCalls.length, 0);
408
+ wrap.append(el("p", "chat-k", w.t("chat.lede", {
409
+ messages: String(turns.length),
410
+ calls: w.p("chat.calls", calls, { n: String(calls) }),
411
+ state: w.t(transcript.state === "cited"
412
+ ? "chat.state.cited"
413
+ : "chat.state.verified"),
414
+ })));
415
+ wrap.append(renderTurns(turns, w));
416
+ return wrap;
417
+ }
418
+ /**
419
+ * The turns of a conversation as a chat, and nothing else: who said what,
420
+ * each tool call with its arguments, each tool result as sealed. It states
421
+ * nothing about where the turns came from or what was checked; its caller
422
+ * does (renderChat for a card, the core's record entry for a record).
423
+ */
424
+ export function renderTurns(turns, w) {
425
+ const list = el("ol", "turns");
426
+ turns.forEach((turn, index) => {
427
+ const role = turn.role === "user" ||
428
+ turn.role === "assistant" ||
429
+ turn.role === "tool" ||
430
+ turn.role === "system"
431
+ ? turn.role
432
+ : "other";
433
+ const item = el("li", `turn ${role}`);
434
+ item.dataset.turn = String(index);
435
+ item.dataset.role = turn.role;
436
+ item.append(el("div", "who", roleLabel(turn.role, w)));
437
+ if (turn.content !== undefined && turn.content.length > 0) {
438
+ if (role === "tool") {
439
+ const shown = exactText(turn.content);
440
+ const pre = el("pre", "say", shown.text);
441
+ pre.dataset.exact = shown.indented ? "indented" : "verbatim";
442
+ item.append(pre);
443
+ }
444
+ else
445
+ item.append(el("div", "say", turn.content));
446
+ }
447
+ for (const call of turn.toolCalls) {
448
+ const line = el("div", "call");
449
+ line.dataset.toolCall = call.name;
450
+ line.append(el("span", "call-k", w.t("chat.called")));
451
+ line.append(el("b", "", call.name));
452
+ // The arguments as the outcome report model hands them over. At
453
+ // agent-action-capsule 0.5.0 `transcriptTurns` re-serialises them
454
+ // with JSON.stringify, so they are not the sealed bytes: a number
455
+ // JSON cannot hold exactly is already rounded there. Never claimed
456
+ // verbatim; this page rewords nothing further.
457
+ if (call.arguments.length > 0) {
458
+ const args = el("code", "call-a", call.arguments);
459
+ args.dataset.exact = "as-modelled";
460
+ line.append(" ", args);
461
+ }
462
+ item.append(line);
463
+ }
464
+ list.append(item);
465
+ });
466
+ return list;
467
+ }
468
+ /** The address of a record in the core's records section (evidence-graph-view.ts). */
469
+ export function recordAddress(recordId) {
470
+ return `record-${recordId}`;
471
+ }
472
+ /**
473
+ * A conversation in place in its card: a level-2 toggle whose body is the
474
+ * chat, built when it opens, with a close control and a link to the record
475
+ * in the core's records section above and below it. The in-place copy
476
+ * carries no address; only the records section's entry does.
477
+ *
478
+ * Beside the toggle sits the same link worded for a page that runs no
479
+ * script, hidden: the static package (presentation-builder.ts) keeps one
480
+ * copy of each conversation, in the records section, and swaps the toggle
481
+ * for that link when the record is there (`linkConversations`).
482
+ */
483
+ export function conversationFold(transcript, caseRecord, w) {
484
+ const wrap = el("div", "chat-fold");
485
+ const record = (transcript.state === "verified" || transcript.state === "cited") &&
486
+ transcript.transcriptRecordId !== undefined
487
+ ? transcript.transcriptRecordId
488
+ : undefined;
489
+ let toggle;
490
+ const bar = () => {
491
+ const line = el("div", "chat-bar");
492
+ // A link, not a button: the card stays a page of links and native
493
+ // toggles. With script it closes the conversation; a page that runs no
494
+ // script drops it (linkConversations).
495
+ const close = el("a", "chat-close", w.t("chat.close"));
496
+ close.setAttribute("href", "#");
497
+ close.setAttribute("role", "button");
498
+ close.addEventListener("click", (event) => {
499
+ event.preventDefault();
500
+ toggle.open = false;
501
+ toggle.querySelector(":scope > summary")?.scrollIntoView?.({
502
+ block: "nearest",
503
+ });
504
+ });
505
+ line.append(close);
506
+ if (record !== undefined) {
507
+ const link = el("a", "chat-records", w.t("chat.in-records"));
508
+ link.setAttribute("href", `#${recordAddress(record)}`);
509
+ link.addEventListener("click", () => revealRecord(link, record));
510
+ line.append(link);
511
+ }
512
+ return line;
513
+ };
514
+ toggle = levelToggle(2, w.t("card.h.conversation"), () => {
515
+ const body = el("div", "chat-body");
516
+ body.append(bar(), renderChat(transcript, caseRecord, w), bar());
517
+ return body;
518
+ });
519
+ toggle.dataset.section = "conversation";
520
+ if (record !== undefined) {
521
+ toggle.dataset.record = record;
522
+ const link = el("a", "chat-static", w.t("chat.read"));
523
+ link.setAttribute("href", `#${recordAddress(record)}`);
524
+ link.hidden = true;
525
+ wrap.append(link);
526
+ }
527
+ wrap.append(toggle);
528
+ return wrap;
529
+ }
530
+ /**
531
+ * Opens a record's entry in the core's records section, and every toggle
532
+ * around it, so the link's target is in view when the browser scrolls to
533
+ * it. A browser that opens a closed <details> for a fragment does the same
534
+ * on a page that runs no script.
535
+ */
536
+ export function revealRecord(from, recordId) {
537
+ const doc = from.nodeType === 9 ? from : from.ownerDocument;
538
+ const target = doc.getElementById(recordAddress(recordId));
539
+ if (target === null)
540
+ return;
541
+ for (let at = target.parentElement?.closest("details") ?? null; at !== null; at = at.parentElement?.closest("details") ?? null)
542
+ if (!at.open)
543
+ openToggle(at);
544
+ target.scrollIntoView?.();
545
+ }
546
+ const hashWatched = new WeakSet();
547
+ /**
548
+ * Where script runs: a page opened at a record's address (`#record-<id>`),
549
+ * or whose address changes to one, opens that record's entry and every
550
+ * fold around it, as a link to it does (`revealRecord`). One listener per
551
+ * document.
552
+ */
553
+ export function revealRecordsByHash(doc) {
554
+ const view = doc.defaultView;
555
+ if (view === null)
556
+ return;
557
+ const go = () => {
558
+ const hash = view.location.hash;
559
+ if (!hash.startsWith("#record-"))
560
+ return;
561
+ let id;
562
+ try {
563
+ id = decodeURIComponent(hash.slice("#record-".length));
564
+ }
565
+ catch {
566
+ // A fragment that does not decode names no record.
567
+ return;
568
+ }
569
+ revealRecord(doc, id);
570
+ };
571
+ go();
572
+ if (hashWatched.has(doc))
573
+ return;
574
+ hashWatched.add(doc);
575
+ view.addEventListener("hashchange", go);
576
+ }
577
+ /**
578
+ * For a page that runs no script: every in-place conversation whose record
579
+ * the core's records section holds becomes a link to it, so the page keeps
580
+ * one copy of each conversation however many cards cite it, built or not.
581
+ * A conversation whose record is not there (none is stated, or its
582
+ * transcript is absent or does not check) keeps its toggle and its state
583
+ * note. Returns how many it linked.
584
+ */
585
+ export function linkConversations(root) {
586
+ let linked = 0;
587
+ const ids = new Set([
588
+ ...root.querySelectorAll("details.cv-record > .cv-record-anchor[id]"),
589
+ ].map((e) => e.id));
590
+ for (const toggle of root.querySelectorAll('details[data-section="conversation"][data-record]')) {
591
+ const link = toggle.parentElement?.querySelector(":scope > a.chat-static");
592
+ if (!ids.has(recordAddress(toggle.dataset.record)) || link == null)
593
+ continue;
594
+ link.hidden = false;
595
+ toggle.remove();
596
+ linked++;
597
+ }
598
+ // A conversation kept in place (no record to link to) shows its state
599
+ // note: the page runs no script, so its body is built now.
600
+ for (const toggle of root.querySelectorAll('details[data-section="conversation"][data-body="pending"]'))
601
+ pendingToggles.get(toggle)?.();
602
+ // A page that runs no script cannot close a toggle from a control.
603
+ for (const close of root.querySelectorAll(".chat-close"))
604
+ close.remove();
605
+ return linked;
606
+ }
607
+ // ---------------------------------------------------------------------------
608
+ // contract-report/v1 section 3 words shared by both groupings
609
+ // ---------------------------------------------------------------------------
610
+ /**
611
+ * The pack's `report.summary` line under the card's title, or nothing: the
612
+ * module has no summary of its own, so a pack that sets none shows none.
613
+ */
614
+ export function reportSummary(w) {
615
+ const text = w.section3("report.summary");
616
+ if (text === undefined)
617
+ return undefined;
618
+ const p = el("p", "rsum", text);
619
+ p.dataset.reportSummary = "";
620
+ return p;
621
+ }
622
+ /**
623
+ * One claim's assurance, as sealed on it (contract-report/v1 section 2:
624
+ * assurance stays on the claim): its tier, grade, sufficiency and carrier
625
+ * status, each in the layered section 3 word, or its raw value when no layer
626
+ * has one. The raw values ride on the element as data.
627
+ */
628
+ export function assuranceRow(claim, w) {
629
+ return [
630
+ w.t("card.pf.assurance"),
631
+ w.t("card.pf.assurance.line", {
632
+ tier: w.enumWord("tier", claim.tier),
633
+ grade: w.enumWord("grade", claim.grade),
634
+ sufficiency: w.enumWord("sufficiency", claim.sufficiency),
635
+ status: w.enumWord("status", claim.presentation.status),
636
+ }),
637
+ {
638
+ tier: claim.tier,
639
+ grade: claim.grade,
640
+ sufficiency: claim.sufficiency,
641
+ status: claim.presentation.status,
642
+ },
643
+ ];
644
+ }
645
+ /**
646
+ * A wording layer the host supplied was refused (contract-report/v1 section
647
+ * 3): one line at the top of the card, so a reader never takes the default
648
+ * words for the ones the page was built to show.
649
+ */
650
+ export function refusedWordingNotice(w) {
651
+ if (w.source.refused === undefined)
652
+ return undefined;
653
+ const p = el("p", "wref", w.t("card.wording.refused"));
654
+ p.dataset.wording = "refused";
655
+ p.dataset.refusal = w.source.refused.reason;
656
+ return p;
657
+ }
658
+ // ---------------------------------------------------------------------------
659
+ // Proofs: one definition list, ids in monospace
660
+ // ---------------------------------------------------------------------------
661
+ export function proofList(rows) {
662
+ const dl = el("dl", "pf");
663
+ for (const [k, v, data] of rows) {
664
+ const dd = element("dd", v);
665
+ for (const [key, value] of Object.entries(data ?? {}))
666
+ dd.dataset[key] = value;
667
+ dl.append(element("dt", k), dd);
668
+ }
669
+ return dl;
670
+ }
671
+ /** The toggles', chat's and proofs' styles, scoped under the card's class. */
672
+ export function levelCss(scope) {
673
+ const s = `.${scope}`;
674
+ return `
675
+ ${s} details.lv{border:1px solid var(--line);border-radius:10px;background:#fff;margin-top:8px;padding:0}
676
+ ${s} details.lv > summary{cursor:pointer;list-style:none;padding:8px 12px;font-size:13.5px;font-weight:600;color:var(--navy)}
677
+ ${s} details.lv > summary::-webkit-details-marker{display:none}
678
+ ${s} details.lv > summary:before{content:"";display:inline-block;width:0;height:0;border-left:5px solid var(--navy);border-top:4px solid transparent;border-bottom:4px solid transparent;margin-right:8px;vertical-align:1px;transition:transform .1s}
679
+ ${s} details.lv[open] > summary:before{transform:rotate(90deg)}
680
+ ${s} details.lv1.line{border:0;background:transparent;margin-top:4px}
681
+ ${s} details.lv1.line > summary{padding:2px 0;font-size:13px}
682
+ ${s} details.lv1.line > .convs,${s} details.lv1.line > .why-missed{margin-top:8px}
683
+ ${s} tr.drill-row > td{padding:0 10px 12px;border-bottom:1px solid var(--line)}
684
+ ${s} details.lv2{background:#FAF8F4;border-style:dashed}
685
+ ${s} details.lv2 > summary{color:var(--slate);font-weight:600}
686
+ ${s} details.lv2 > .pf-b{padding:4px 14px 14px;font-size:12.5px}
687
+ ${s} .convs{display:grid;gap:12px}
688
+ ${s} .conv{border:1px solid var(--line);border-radius:10px;background:#fff}
689
+ ${s} .conv-h{display:grid;grid-template-columns:110px auto 1fr auto;gap:10px;align-items:center;padding:8px 12px;font-size:13.5px;background:#FAF8F4;border-bottom:1px solid var(--line);border-radius:10px 10px 0 0}
690
+ ${s} .conv-h .cid{font-weight:700;color:var(--navy)}
691
+ ${s} .conv-h .cwhy{color:var(--slate)}
692
+ ${s} .conv-h .cmodel{color:var(--slate);font-family:ui-monospace,Menlo,monospace;font-size:11.5px;white-space:nowrap}
693
+ ${s} .conv > [data-body="pending"]{min-height:160px}
694
+ ${s} .conv-b{padding:4px 16px 16px;font-size:13.5px;font-weight:400;color:var(--ink)}
695
+ ${s} .conv-b h4{font-size:11.5px;letter-spacing:.06em;text-transform:uppercase;color:var(--slate);margin:16px 0 6px}
696
+ ${s} .conv-b .small{font-size:12.5px;color:var(--slate);margin:6px 0}
697
+ ${s} .conv-b .muted{color:var(--slate)}
698
+ ${s} .why-missed h5{font-size:11px;letter-spacing:.06em;text-transform:uppercase;color:var(--slate);margin:14px 0 6px}
699
+ ${s} .why-missed table{border-collapse:collapse;font-size:13px;margin:0 0 8px}
700
+ ${s} .why-missed td{padding:3px 12px 3px 0;border-bottom:1px solid var(--line)}
701
+ ${s} .why-missed td.n{text-align:right;font-variant-numeric:tabular-nums;font-weight:700;color:var(--bad)}
702
+ ${s} .why-missed .rsn-h{margin:16px 0 6px;font-size:13.5px;font-weight:700;color:var(--navy)}
703
+ ${s} .chat .absent{margin:0;padding:10px 12px;border:1px dashed #BFC4CC;border-radius:10px;background:#FAF8F4;color:var(--slate);word-break:break-word;font-size:13px}
704
+ ${s} .chat .chat-k{font-size:12.5px;color:var(--slate);margin:4px 0 6px}
705
+ ${s} .chat ol.turns{list-style:none;margin:0;padding:10px;display:grid;gap:6px;max-height:620px;overflow:auto;border:1px solid var(--line);border-radius:10px;background:#FAF8F4}
706
+ ${s} .chat li.turn{border-radius:10px;padding:6px 10px;max-width:86%;background:#fff;border:1px solid var(--line);font-size:13.5px}
707
+ ${s} .chat li.turn.user{justify-self:end;background:var(--person-l);border-color:transparent}
708
+ ${s} .chat li.turn.assistant{justify-self:start}
709
+ ${s} .chat li.turn.tool,${s} .chat li.turn.system{justify-self:center;max-width:96%;border-style:dashed;background:#fff}
710
+ ${s} .chat li.turn .who{font-size:10.5px;letter-spacing:.06em;text-transform:uppercase;color:var(--slate);margin-bottom:2px}
711
+ ${s} .chat-body{padding:4px 12px 12px}
712
+ ${s} .chat-bar{display:flex;align-items:center;gap:12px;margin:6px 0;font-size:13px}
713
+ ${s} .chat-bar .chat-close{font-weight:600}
714
+ ${s} .chat-bar .chat-records{margin-left:auto}
715
+ ${s} a.chat-static:not([hidden]){display:inline-block;margin-top:8px;font-size:13.5px;font-weight:600}
716
+ ${s} .chat li.turn .say{white-space:pre-wrap;word-break:break-word}
717
+ ${s} .chat li.turn pre.say{margin:0;font-family:ui-monospace,Menlo,monospace;font-size:11.5px;max-height:180px;overflow:auto;color:var(--slate)}
718
+ ${s} .chat li.turn.system .say{max-height:120px;overflow:auto;font-size:12px;color:var(--slate)}
719
+ ${s} .chat .call{margin-top:4px;font-size:12.5px;background:var(--gold-l);border-radius:6px;padding:3px 8px;word-break:break-word}
720
+ ${s} .chat .call .call-k{color:var(--slate)}
721
+ ${s} .chat .call b{font-family:ui-monospace,Menlo,monospace;color:var(--navy);font-size:12px}
722
+ ${s} .chat .call .call-a{font-family:ui-monospace,Menlo,monospace;font-size:11.5px;color:var(--ink);white-space:pre-wrap;word-break:break-all}
723
+ ${s} dl.pf{display:grid;grid-template-columns:200px 1fr;gap:4px 12px;margin:6px 0;font-size:12px}
724
+ ${s} dl.pf dt{color:var(--slate)}
725
+ ${s} dl.pf dd{margin:0;font-family:ui-monospace,Menlo,monospace;word-break:break-all;color:var(--navy)}
726
+ ${s} .pf-b p{margin:8px 0;color:var(--slate)}
727
+ ${s} .pf-b h5{font-size:11px;letter-spacing:.06em;text-transform:uppercase;color:var(--slate);margin:14px 0 4px}
728
+ ${s} .pill{display:inline-block;padding:1px 9px;border-radius:99px;font-size:12px;font-weight:700;white-space:nowrap;text-align:center}
729
+ ${s} .pill.ok{background:var(--ok-l);color:var(--ok)}
730
+ ${s} .pill.bad{background:var(--bad-l);color:var(--bad)}
731
+ ${s} .pill.na{background:#EEF0F3;color:var(--slate);border:1px dashed #BFC4CC}
732
+ ${s} .pill.ne{background:var(--gold-l);color:#8A5A12}
733
+ ${s} dl.prov{display:grid;grid-template-columns:110px 1fr;gap:4px 12px;margin:14px 0 0;padding:10px 12px;border:1px solid var(--line);border-radius:10px;background:#fff;font-size:12.5px}
734
+ ${s} dl.prov dt{color:var(--slate);font-weight:600}
735
+ ${s} dl.prov dd{margin:0;color:var(--ink);word-break:break-word}
736
+ @media (max-width:760px){${s} dl.pf,${s} dl.prov{grid-template-columns:1fr}${s} .chat li.turn{max-width:100%}${s} .conv-h{grid-template-columns:1fr auto}${s} .conv-h .cmodel{display:none}}
737
+ `;
738
+ }
739
+ //# sourceMappingURL=report-levels.js.map