359912f822
The full-screen viewer now opens markdown/text artifacts (text/markdown, text/plain, .md/.markdown/.txt) through the resolved artifact URL and renders them with the page's own DOM markdown renderer, with an honest failure message and an external fallback. ArrowLeft/ArrowRight page the shared media+document list, and a backdrop click closes the viewer while panel clicks do not. The run gallery and per-visit artifacts present input.md, memory.md and output.md as ordinary Document tiles with a drawn glyph and open the viewer. Adds isMarkdownArtifact/isViewerArtifact with unit coverage, deterministic viewer fixtures, and ui-smoke assertions/captures for rendered markdown, keyboard paging, the honest 404 and backdrop dismissal.
980 lines
34 KiB
JavaScript
980 lines
34 KiB
JavaScript
// The run detail: Summary / Graph / Input / Output / Timeline, the per-visit
|
||
// stage pane, the decision card and run actions, the Summary title/rename row and
|
||
// the run's artifact gallery. Pure models live in runDetail.js, changes.js,
|
||
// artifacts.js and actions.js; dialogs are delegated to dialogs.js.
|
||
|
||
import { h, kvMono, renderLiveBadge, canCopyLink, canShare, copyArtifactLink, shareArtifact } from "../dom.js";
|
||
import { isExecuting, changeKindTone, describeError } from "../format.js";
|
||
import { liveBadgeState } from "../connection.js";
|
||
import { resolveArtifactUrl } from "../serverUrl.js";
|
||
import { runStateOf } from "../runs.js";
|
||
import { PIPELINE_GRAPH_EMPTY, RUN_GRAPH_MISSING, mount as mountGraph } from "../graph.js";
|
||
import {
|
||
RENDERED_RUN_TABS,
|
||
EMPTY_INPUT_MESSAGE,
|
||
EMPTY_OUTPUT_MESSAGE,
|
||
EMPTY_TIMELINE_MESSAGE,
|
||
NO_STAGE_DETAILS_MESSAGE,
|
||
fieldSections,
|
||
summaryRows,
|
||
runIdentityLine,
|
||
runLabelFor,
|
||
runDurationText,
|
||
runTimeline,
|
||
runGraphState,
|
||
nodeLabel,
|
||
headlineFields,
|
||
runStepFields,
|
||
stageSectionSpecs,
|
||
clampVisitIndex,
|
||
referencedVerdictArtifacts,
|
||
verdictReasonSegments,
|
||
outcomeColorToken,
|
||
stateGlyph,
|
||
} from "../runDetail.js";
|
||
import {
|
||
ArtifactKind,
|
||
artifactCaptureNotes,
|
||
artifactGroups,
|
||
artifactKind,
|
||
artifactKindLabel,
|
||
artifactsHeader,
|
||
artifactsSummary,
|
||
artifactViewerPositionLabel,
|
||
displayVisit,
|
||
humanBytes,
|
||
isViewerArtifact,
|
||
pagedArtifacts,
|
||
presentedArtifacts,
|
||
resolvedArtifactUrl,
|
||
sessionExportArtifact,
|
||
viewerIndexByUrl,
|
||
} from "../artifacts.js";
|
||
import {
|
||
BINARY_DIFF_MESSAGE,
|
||
NO_LINE_CHANGES_MESSAGE,
|
||
TRUNCATED_PATCH_NOTE,
|
||
ChangeKind,
|
||
changeKindLabel,
|
||
changeKindTitle,
|
||
changePositionLabel,
|
||
changeRowMeta,
|
||
changeStat,
|
||
changesListSummary,
|
||
diffLineClass,
|
||
diffLineSymbol,
|
||
entryHasPatch,
|
||
gutterText,
|
||
gutterWidth,
|
||
noChangeDetail,
|
||
parseUnifiedPatch,
|
||
patchUnavailableReason,
|
||
runChanges,
|
||
runChangesHeader,
|
||
runChangesNote,
|
||
showRunChangesSummary,
|
||
} from "../changes.js";
|
||
import {
|
||
DECISION_ACTION,
|
||
DECISION_CARD_TITLE,
|
||
RENAME_ACTION,
|
||
RENAME_CANCEL,
|
||
RENAME_FIELD_LABEL,
|
||
RENAME_SAVE,
|
||
canCancel,
|
||
canRetry,
|
||
decisionCardMeta,
|
||
pendingDecision,
|
||
} from "../actions.js";
|
||
|
||
/** Shown where tiles exist but the configured server URL cannot resolve them. */
|
||
const ARTIFACT_SERVER_HINT =
|
||
"Set the server URL in Settings to open or copy artifact links.";
|
||
|
||
export function createRunDetailView(rt) {
|
||
const { state, repo, modal } = rt;
|
||
const renderContent = (...args) => rt.renderContent(...args);
|
||
const selectRun = (...args) => rt.selectRun(...args);
|
||
const pushRoute = (...args) => rt.pushRoute(...args);
|
||
const openChangeViewer = (...args) => rt.views.dialogs.openChangeViewer(...args);
|
||
const openDecisionSheet = (...args) => rt.views.dialogs.openDecisionSheet(...args);
|
||
const openConfirm = (...args) => rt.views.dialogs.openConfirm(...args);
|
||
const renderDecisionSheet = (...args) => rt.views.dialogs.renderDecisionSheet(...args);
|
||
const renderConfirmDialog = (...args) => rt.views.dialogs.renderConfirmDialog(...args);
|
||
const renderActionFailedDialog = (...args) => rt.views.dialogs.renderActionFailedDialog(...args);
|
||
const openArtifactViewer = (...args) => rt.views.dialogs.openArtifactViewer(...args);
|
||
|
||
/**
|
||
* The run detail: a tab strip (Summary / Graph / Input / Output / Timeline) over
|
||
* the selected tab's body, wired from the pure helpers in runDetail.js. Summary
|
||
* is the default and carries the run's identity, key/value rows and diagnostic;
|
||
* the Graph tab mounts the shared workflow graph in run mode; Input/Output are
|
||
* one collapsed section per recorded field; the Timeline is one compact row per
|
||
* visit that opens the stage pane. Every server string is set with textContent,
|
||
* never innerHTML.
|
||
*/
|
||
function renderRunDetail(now = Date.now()) {
|
||
const d = state.runDetail;
|
||
const pane = h("div", { class: "detail run-detail" });
|
||
if (!d) {
|
||
pane.append(h("div", { class: "muted pad", text: "Select a run." }));
|
||
return pane;
|
||
}
|
||
// A run parked on a person surfaces its decision above the tabs, exactly where
|
||
// the Android run detail does, so opening the run is enough — no tab hunt.
|
||
const decision = pendingDecision(d);
|
||
if (decision) pane.append(renderDecisionCard(decision));
|
||
pane.append(
|
||
h("h1", {
|
||
class: "run-detail-title",
|
||
id: "detailTitle",
|
||
tabindex: "-1",
|
||
text: runLabelFor(d),
|
||
}),
|
||
);
|
||
const actions = renderRunActions(d);
|
||
if (actions) pane.append(actions);
|
||
if (state.runTab === "graph") {
|
||
pane.append(renderRunGraphTab(d));
|
||
} else if (state.runTab === "input") {
|
||
pane.append(renderRunFieldTab(d.input, "input", EMPTY_INPUT_MESSAGE));
|
||
} else if (state.runTab === "output") {
|
||
pane.append(renderRunFieldTab(d.output, "output", EMPTY_OUTPUT_MESSAGE));
|
||
} else if (state.runTab === "steps") {
|
||
pane.append(renderRunTimelineTab(d));
|
||
} else {
|
||
pane.append(renderRunSummaryTab(d, now));
|
||
}
|
||
if (state.runStagePane) pane.append(renderStagePane(d));
|
||
if (state.decisionOpen && state.runDecision) pane.append(renderDecisionSheet(state.runDecision));
|
||
const confirm = renderConfirmDialog(d);
|
||
if (confirm) pane.append(confirm);
|
||
if (state.actionError) pane.append(renderActionFailedDialog());
|
||
return pane;
|
||
}
|
||
|
||
/**
|
||
* The Graph tab (R5): the run's own guidance note plus the shared workflow graph
|
||
* mounted in run mode, or the app's exact missing-graph sentence when the server
|
||
* sent no stages. The active stage pulses with its elapsed time while the run
|
||
* executes; clicking a stage opens the existing visit pane at that stage's most
|
||
* recent visit. The mount handle is tracked in `state.runGraph` so a re-render
|
||
* (or a tab/run change) always unmounts the previous wheel/pointer/resize
|
||
* listeners and the elapsed timer.
|
||
*/
|
||
function renderRunGraphTab(d) {
|
||
const wrap = h("div", { class: "run-tab-body run-graph" });
|
||
const stages = d.graph?.stages ?? [];
|
||
if (!stages.length) {
|
||
wrap.append(h("div", { class: "run-empty muted", text: RUN_GRAPH_MISSING }));
|
||
return wrap;
|
||
}
|
||
const host = h("div", { class: "graph-host" });
|
||
wrap.append(host);
|
||
state.runGraph = mountGraph(host, d.graph, {
|
||
runState: runGraphState(d),
|
||
onStageTap: (key) => openStagePane(key, latestVisitIndex(d, key)),
|
||
});
|
||
return wrap;
|
||
}
|
||
|
||
/**
|
||
* The visit index the graph opens for a stage: its most recent recorded visit
|
||
* (`visits.size - 1`, never negative), exactly as RunGraphTab computes it.
|
||
*/
|
||
function latestVisitIndex(d, key) {
|
||
const visits = runTimeline(d.steps ?? []).visitsByStage.get(key) ?? [];
|
||
return Math.max(0, visits.length - 1);
|
||
}
|
||
|
||
/** The Summary tab: identity, chip row, key/value rows, diagnostic and gallery. */
|
||
function renderRunSummaryTab(d, now) {
|
||
const wrap = h("div", { class: "run-tab-body" });
|
||
wrap.append(renderRunTitleRow(d));
|
||
const status = runStateOf(d.status);
|
||
const chips = h("div", { class: "run-chip-row" });
|
||
chips.append(h("span", { class: `chip chip-${status}`, text: d.status || status }));
|
||
if (String(d.outcome ?? "").trim()) chips.append(h("span", { class: "muted", text: `outcome ${d.outcome}` }));
|
||
if (d.executing) chips.append(h("span", { class: "muted", text: "executing" }));
|
||
chips.append(renderLiveBadge(liveBadgeState(state.runStream)));
|
||
wrap.append(chips);
|
||
wrap.append(h("div", { class: "run-identity muted", text: runIdentityLine(d) }));
|
||
|
||
const rows = h("div", { class: "kv" });
|
||
for (const row of summaryRows(d, now)) {
|
||
if (row.kind === "wait") rows.append(h("div", { class: "run-wait", text: row.value }));
|
||
else if (row.kind === "duration") rows.append(renderDurationRow(d, row));
|
||
else if (row.kind === "block") rows.append(renderCodeBlock(row.key, row.value));
|
||
else rows.append(renderKvRow(row));
|
||
}
|
||
wrap.append(rows);
|
||
|
||
// The run-level artifacts gallery (parity R8), collapsed by default like the
|
||
// Android summary. Its header alone says what was captured until expanded.
|
||
wrap.append(
|
||
renderCollapsible("summary-artifacts", artifactsHeader(d.steps ?? [], isExecuting(d)), () =>
|
||
renderArtifactGallery(d.steps ?? []),
|
||
),
|
||
);
|
||
|
||
// The run-level Changes (parity R4), collapsed by default like the Android
|
||
// summary: the observed final-state workspace diff. Only a succeeded run has a
|
||
// settled final state to present; a failed run keeps its evidence in each
|
||
// visit's own capture (a deliberate divergence from Android, see PARITY R4).
|
||
if (showRunChangesSummary(d.status)) {
|
||
const changes = runChanges(d.steps ?? []);
|
||
wrap.append(
|
||
renderCollapsible("summary-changes", runChangesHeader(changes), () => renderChangesBody(changes)),
|
||
);
|
||
}
|
||
return wrap;
|
||
}
|
||
|
||
/** The Duration row, tagged so the detail clock can update it in place. */
|
||
function renderDurationRow(d, row) {
|
||
return h(
|
||
"div",
|
||
{ class: "kv-row" },
|
||
h("span", { class: "kv-key", text: row.key }),
|
||
h("span", { class: "kv-val run-duration", "data-run-id": String(d.id), text: row.value }),
|
||
);
|
||
}
|
||
|
||
/** One key/value row from a summary row model. */
|
||
function renderKvRow(row) {
|
||
return h(
|
||
"div",
|
||
{ class: "kv-row" },
|
||
h("span", { class: "kv-key", text: row.key }),
|
||
h("span", { class: `kv-val${row.monospace ? " monospace" : ""}`, text: row.value }),
|
||
);
|
||
}
|
||
|
||
/** A themed monospace block with a label header. */
|
||
function renderCodeBlock(label, text) {
|
||
return h(
|
||
"div",
|
||
{ class: "codeblock" },
|
||
h("div", { class: "codeblock-label", text: label }),
|
||
h("pre", { class: "codeblock-body", text }),
|
||
);
|
||
}
|
||
|
||
/** One stage-pane field as either the code card (blocks) or a key/value row. */
|
||
function renderStageField(field) {
|
||
if (field.block) return renderCodeBlock(field.label, field.value);
|
||
return h(
|
||
"div",
|
||
{ class: "kv-row" },
|
||
h("span", { class: "kv-key", text: field.label }),
|
||
h("span", { class: `kv-val${field.mono ? " monospace" : ""}`, text: field.value }),
|
||
);
|
||
}
|
||
|
||
/** One collapsible input/output field section (expanded body is a code block). */
|
||
function renderRunFieldTab(map, keyPrefix, emptyMessage) {
|
||
const wrap = h("div", { class: "run-tab-body" });
|
||
const sections = fieldSections(map, keyPrefix);
|
||
if (!sections.length) {
|
||
wrap.append(h("div", { class: "run-empty muted", text: emptyMessage }));
|
||
return wrap;
|
||
}
|
||
for (const section of sections) {
|
||
const key = `${keyPrefix}-${section.key}`;
|
||
wrap.append(
|
||
renderCollapsible(key, section.title, () =>
|
||
h("pre", { class: "field-block" }, h("code", { text: section.value })),
|
||
),
|
||
);
|
||
}
|
||
return wrap;
|
||
}
|
||
|
||
/**
|
||
* One collapsible section of the run detail's field/summary body. Expanded state
|
||
* is keyed in `state.runSections`, so the operator's choices survive a re-render.
|
||
*/
|
||
function renderCollapsible(key, title, bodyFn) {
|
||
const expanded = state.runSections.has(key);
|
||
const wrap = h("div", { class: "collapsible" });
|
||
wrap.append(
|
||
h(
|
||
"button",
|
||
{
|
||
class: "section-toggle",
|
||
type: "button",
|
||
"aria-expanded": expanded ? "true" : "false",
|
||
onclick: () => {
|
||
state.runSections = new Set(state.runSections);
|
||
if (state.runSections.has(key)) state.runSections.delete(key);
|
||
else state.runSections.add(key);
|
||
renderContent();
|
||
},
|
||
},
|
||
h("span", { class: "chevron", text: expanded ? "\u25be" : "\u25b8" }),
|
||
h("span", { class: "section-title", text: title }),
|
||
),
|
||
);
|
||
if (expanded) wrap.append(h("div", { class: "section-body" }, bodyFn()));
|
||
return wrap;
|
||
}
|
||
|
||
// --- Changes (parity R4) ---------------------------------------------------
|
||
|
||
/**
|
||
* The Changes section body: the file-list summary line, the honest capture note
|
||
* (when the capture was not complete) and then either the no-change detail or
|
||
* one clickable row per changed file in first-seen order. Every server string is
|
||
* set as text, never as markup.
|
||
*/
|
||
function renderChangesBody(changes) {
|
||
const body = h("div", { class: "changes" });
|
||
body.append(h("div", { class: "changes-summary", text: changesListSummary(changes) }));
|
||
const note = runChangesNote(changes);
|
||
if (note.trim() !== "") body.append(h("div", { class: "changes-note muted", text: note }));
|
||
if (changes.files.length === 0) {
|
||
body.append(h("div", { class: "changes-empty muted", text: noChangeDetail(changes) }));
|
||
return body;
|
||
}
|
||
const list = h("div", { class: "changes-list" });
|
||
changes.files.forEach((entry, index) => list.append(renderChangeRow(entry, index, changes.files)));
|
||
body.append(list);
|
||
return body;
|
||
}
|
||
|
||
/**
|
||
* One changed-file row: the kind word in its hue, the path in monospace, the
|
||
* `+N -M` stat and the `changed by …` meta line. Clicking (or Enter/Space) opens
|
||
* the diff viewer on this file, whose position there confirms which row it is.
|
||
*/
|
||
function renderChangeRow(entry, index, files) {
|
||
const row = h("div", {
|
||
class: "change-row",
|
||
role: "button",
|
||
tabindex: "0",
|
||
"data-change-index": String(index),
|
||
"aria-label": `Open ${entry.path}`,
|
||
});
|
||
const open = () => openChangeViewer(files, index, row);
|
||
row.addEventListener("click", open);
|
||
row.addEventListener("keydown", (event) => {
|
||
if (event.key === "Enter" || event.key === " ") {
|
||
event.preventDefault();
|
||
open();
|
||
}
|
||
});
|
||
row.append(h("span", { class: `change-kind ${changeKindTone(entry.kind)}`, text: changeKindLabel(entry.kind) }));
|
||
const main = h("div", { class: "change-main" });
|
||
main.append(h("div", { class: "change-path monospace", text: entry.path }));
|
||
main.append(h("div", { class: "change-meta muted", text: changeRowMeta(entry) }));
|
||
row.append(main);
|
||
const stat = h("div", { class: "change-stat" });
|
||
if (entry.added > 0) stat.append(h("span", { class: "stat-add", text: `+${entry.added}` }));
|
||
if (entry.removed > 0) stat.append(h("span", { class: "stat-del", text: `-${entry.removed}` }));
|
||
row.append(stat);
|
||
return row;
|
||
}
|
||
|
||
|
||
function renderRunTimelineTab(d) {
|
||
const wrap = h("div", { class: "run-tab-body" });
|
||
const timeline = runTimeline(d.steps ?? []);
|
||
if (!timeline.entries.length) {
|
||
wrap.append(h("div", { class: "run-empty muted", text: EMPTY_TIMELINE_MESSAGE }));
|
||
return wrap;
|
||
}
|
||
const list = h("div", { class: "timeline" });
|
||
for (const entry of timeline.entries) list.append(renderTimelineRow(entry));
|
||
wrap.append(list);
|
||
return wrap;
|
||
}
|
||
|
||
/**
|
||
* One timeline row: a stage-outcome glyph, the `{stage} · visit {n}` line with
|
||
* the outcome chip, a paperclip when the visit recorded artifacts, the duration,
|
||
* and the one-line description. Clicking (or Enter/Space) opens the stage pane.
|
||
*/
|
||
function renderTimelineRow(entry) {
|
||
const state_ = runStateOf(entry.outcomeLabel);
|
||
const chipLabel = entry.outcomeLabel || state_.label;
|
||
const tone = outcomeClass(entry.step.outcome, entry.step.status);
|
||
const open = () => openStagePane(entry.stageKey, entry.visitIndex);
|
||
const row = h("div", {
|
||
class: "timeline-row",
|
||
role: "button",
|
||
tabindex: "0",
|
||
"data-stage-key": entry.stageKey,
|
||
"data-visit-index": String(entry.visitIndex),
|
||
});
|
||
row.addEventListener("click", open);
|
||
row.addEventListener("keydown", (event) => {
|
||
if (event.key === "Enter" || event.key === " ") {
|
||
event.preventDefault();
|
||
open();
|
||
}
|
||
});
|
||
row.append(h("span", { class: `timeline-glyph ${tone}`, text: stateGlyph(state_) }));
|
||
const head = h("div", { class: "timeline-head" });
|
||
head.append(h("span", { class: "timeline-title", text: `${entry.stageKey} · visit ${entry.visit}` }));
|
||
head.append(h("span", { class: `outcome-chip ${tone}`, text: chipLabel }));
|
||
if (entry.hasArtifacts) {
|
||
head.append(
|
||
h("span", { class: "timeline-paperclip", title: "Recorded artifacts", "aria-label": "has recorded artifacts", text: "\u{1F4CE}" }),
|
||
);
|
||
}
|
||
if (entry.duration) head.append(h("span", { class: "timeline-duration muted", text: entry.duration }));
|
||
const body = h("div", { class: "timeline-main" }, head);
|
||
if (entry.description) body.append(h("span", { class: "timeline-desc muted", text: entry.description }));
|
||
row.append(body);
|
||
return row;
|
||
}
|
||
|
||
/** A CSS class naming a stage-outcome colour (status palette, else flow). */
|
||
function outcomeClass(outcome, status) {
|
||
const [kind, value] = outcomeColorToken(outcome, status).split(" ");
|
||
return `outcome-${kind}-${value}`;
|
||
}
|
||
|
||
// --- Stage pane ------------------------------------------------------------
|
||
|
||
/**
|
||
* The stage pane: a closable dialog for one visit, opened from a timeline row.
|
||
* It leads with the verdict block (when the server recorded one), then the
|
||
* always-visible headline rows, then the visit's collapsible sections in the
|
||
* Android order (Artifacts, Session, Output, Input, This run, Stage definition).
|
||
* Escape, the Back button and a backdrop click close it.
|
||
*/
|
||
function renderStagePane(d) {
|
||
const stageKey = state.runStagePane.stageKey;
|
||
const timeline = runTimeline(d.steps ?? []);
|
||
const runSteps = timeline.visitsByStage.get(stageKey) ?? [];
|
||
const visitIndex = clampVisitIndex(state.runStagePane.visitIndex, runSteps.length);
|
||
const step = runSteps[visitIndex] ?? null;
|
||
const graph = d.graph ?? { stages: [] };
|
||
const specs = stageSectionSpecs(stageKey, graph, runSteps, visitIndex, true);
|
||
const fields = step ? runStepFields(step) : [];
|
||
const headline = headlineFields(fields);
|
||
|
||
const overlay = h("div", {
|
||
class: "stage-pane",
|
||
role: "dialog",
|
||
"aria-modal": "true",
|
||
"aria-label": nodeLabel(stageKey),
|
||
});
|
||
overlay.addEventListener("click", (event) => {
|
||
if (event.target === overlay) closeStagePane();
|
||
});
|
||
const bar = h(
|
||
"div",
|
||
{ class: "stage-pane-bar" },
|
||
h("button", {
|
||
class: "stage-pane-back",
|
||
type: "button",
|
||
"aria-label": "Back",
|
||
text: "\u2190 Back",
|
||
onclick: closeStagePane,
|
||
}),
|
||
h(
|
||
"div",
|
||
{ class: "stage-pane-titles" },
|
||
h("span", { class: "stage-pane-title", text: nodeLabel(stageKey) }),
|
||
h("span", { class: "stage-pane-run muted", text: runLabelFor(d) }),
|
||
),
|
||
);
|
||
|
||
const body = h("div", { class: "stage-pane-body" });
|
||
if (step) {
|
||
const verdict = renderVerdictBlock(step);
|
||
if (verdict) body.append(verdict);
|
||
}
|
||
for (const field of headline) body.append(renderStageField(field));
|
||
if (headline.length === 0 && specs.length === 0) {
|
||
body.append(h("div", { class: "run-empty muted", text: NO_STAGE_DETAILS_MESSAGE }));
|
||
}
|
||
for (const spec of specs) body.append(renderStageSection(spec, step));
|
||
overlay.append(h("div", { class: "stage-pane-sheet" }, bar, body));
|
||
modal.stage("stage-pane", overlay, {
|
||
onClose: () => closeStagePane(),
|
||
fallbackSelectors: ["#detailTitle", "#pane-main"],
|
||
// A pane opened from the Timeline returns to its row; one opened from the
|
||
// Graph returns to the stage node (there is no timeline row then).
|
||
restore: () => {
|
||
const row = [...document.querySelectorAll(".timeline-row")].find(
|
||
(node) =>
|
||
node.dataset.stageKey === stageKey && node.dataset.visitIndex === String(visitIndex),
|
||
);
|
||
return (
|
||
row ??
|
||
[...document.querySelectorAll(".graph-node")].find((node) => node.dataset.key === stageKey) ??
|
||
null
|
||
);
|
||
},
|
||
});
|
||
return overlay;
|
||
}
|
||
|
||
/** One stage-pane section, using the pane's own expansion state. */
|
||
function renderStageSection(spec, step) {
|
||
const expanded = state.runStageSections.has(spec.key);
|
||
const wrap = h("div", { class: "collapsible stage-section" });
|
||
wrap.append(
|
||
h(
|
||
"button",
|
||
{
|
||
class: "section-toggle",
|
||
type: "button",
|
||
"aria-expanded": expanded ? "true" : "false",
|
||
onclick: () => {
|
||
state.runStageSections = new Set(state.runStageSections);
|
||
if (state.runStageSections.has(spec.key)) state.runStageSections.delete(spec.key);
|
||
else state.runStageSections.add(spec.key);
|
||
renderContent();
|
||
},
|
||
},
|
||
h("span", { class: "chevron", text: expanded ? "\u25be" : "\u25b8" }),
|
||
h("span", { class: "section-title", text: spec.title }),
|
||
),
|
||
);
|
||
if (!expanded) return wrap;
|
||
const body = h("div", { class: "section-body" });
|
||
if (spec.artifacts && step) body.append(renderVisitArtifacts(step));
|
||
else if (spec.session && step) body.append(renderSessionExport(step));
|
||
else for (const field of spec.fields) body.append(renderStageField(field));
|
||
wrap.append(body);
|
||
return wrap;
|
||
}
|
||
|
||
/**
|
||
* The verdict block: heading "Verdict", the option as an outcome-coloured chip,
|
||
* the reason as body text with inline artifact links, and the referenced
|
||
* artifacts as compact chips. Nothing renders when the server recorded no
|
||
* option and no reason.
|
||
*/
|
||
function renderVerdictBlock(step) {
|
||
const verdict = step.verdict;
|
||
if (!verdict) return null;
|
||
const option = String(verdict.option ?? "");
|
||
const reason = String(verdict.reason ?? "");
|
||
if (option.trim() === "" && reason.trim() === "") return null;
|
||
|
||
const wrap = h("div", { class: "verdict" });
|
||
const head = h("div", { class: "verdict-head" }, h("span", { class: "verdict-title", text: "Verdict" }));
|
||
if (option.trim() !== "") {
|
||
head.append(h("span", { class: `outcome-chip ${outcomeClass(step.outcome, step.status)}`, text: option }));
|
||
}
|
||
wrap.append(head);
|
||
|
||
const references = referencedVerdictArtifacts(verdict, presentedArtifacts(step));
|
||
if (reason.trim() !== "") {
|
||
const body = h("p", { class: "verdict-reason" });
|
||
for (const segment of verdictReasonSegments(reason, references)) {
|
||
if (!segment.artifact) body.append(document.createTextNode(segment.text));
|
||
else body.append(renderVerdictLink(segment.text, segment.artifact, step));
|
||
}
|
||
wrap.append(body);
|
||
}
|
||
if (references.length) {
|
||
const refs = h("div", { class: "verdict-refs" });
|
||
for (const artifact of references) refs.append(renderVerdictRef(artifact, step));
|
||
wrap.append(refs);
|
||
}
|
||
return wrap;
|
||
}
|
||
|
||
/** One inline artifact reference in a verdict reason (viewer files open the viewer). */
|
||
function renderVerdictLink(text, artifact, step) {
|
||
const resolved = resolvedArtifactUrl(artifact, state.url);
|
||
if (!resolved) return h("span", { class: "verdict-link disabled", text });
|
||
const link = h("a", { class: "verdict-link", href: resolved, target: "_blank", rel: "noreferrer", text });
|
||
link.addEventListener("click", (event) => {
|
||
event.preventDefault();
|
||
openArtifactFromVerdict(artifact, step, resolved, link);
|
||
});
|
||
return link;
|
||
}
|
||
|
||
/** One compact chip naming a referenced artifact. */
|
||
function renderVerdictRef(artifact, step) {
|
||
const resolved = resolvedArtifactUrl(artifact, state.url);
|
||
const chip = h("button", {
|
||
class: "verdict-ref",
|
||
type: "button",
|
||
text: artifact.name || "(unnamed artifact)",
|
||
});
|
||
chip.addEventListener("click", () => openArtifactFromVerdict(artifact, step, resolved, chip));
|
||
return chip;
|
||
}
|
||
|
||
/** Open a referenced artifact: viewer-selectable files in the viewer, others externally. */
|
||
function openArtifactFromVerdict(artifact, step, resolved, trigger = null) {
|
||
if (!resolved) return;
|
||
const group = { stageKey: step.stage, visit: displayVisit(step, 0), artifacts: presentedArtifacts(step) };
|
||
const viewable = pagedArtifacts([group]);
|
||
const viewerIndexOf = viewerIndexByUrl([group]);
|
||
const at = viewerIndexOf.get(artifact.url);
|
||
if (isViewerArtifact(artifact.content_type, artifact.name) && at != null) {
|
||
openArtifactViewer(viewable, at, trigger);
|
||
} else {
|
||
window.open(resolved, "_blank", "noopener,noreferrer");
|
||
}
|
||
}
|
||
|
||
/** Open the stage pane for one visit of a stage; the controller moves focus in. */
|
||
function openStagePane(stageKey, visitIndex, { updateRoute = true } = {}) {
|
||
state.runStagePane = { stageKey, visitIndex };
|
||
state.runStageSections = new Set();
|
||
if (updateRoute && state.selectedRunId) {
|
||
pushRoute({
|
||
kind: "runStage",
|
||
id: state.selectedRunId,
|
||
stageKey,
|
||
visit: Number.isSafeInteger(visitIndex) && visitIndex >= 0 ? visitIndex : null,
|
||
});
|
||
}
|
||
renderContent();
|
||
}
|
||
|
||
/** Close the stage pane; the controller returns focus to the row or graph node. */
|
||
function closeStagePane({ updateRoute = true } = {}) {
|
||
if (!state.runStagePane) return;
|
||
state.runStagePane = null;
|
||
state.runStageSections = new Set();
|
||
if (updateRoute && state.selectedRunId) {
|
||
pushRoute({ kind: "run", id: state.selectedRunId });
|
||
}
|
||
renderContent();
|
||
}
|
||
|
||
// --- Run-level operator actions (R11–R14) ----------------------------------
|
||
|
||
|
||
/**
|
||
* The decision card (R11): a prominent "Waiting for your decision" banner with
|
||
* the stage/run identity and a deliberate Decide action. Only rendered when the
|
||
* loaded run is genuinely waiting on a person.
|
||
*/
|
||
function renderDecisionCard(decision) {
|
||
const card = h("div", { class: "decision-card" });
|
||
card.append(h("span", { class: "decision-card-icon", "aria-hidden": "true", text: "\u270B" }));
|
||
const text = h("div", { class: "decision-card-text" });
|
||
text.append(h("div", { class: "decision-card-title", text: DECISION_CARD_TITLE }));
|
||
text.append(h("div", { class: "decision-card-meta muted", text: decisionCardMeta(decision) }));
|
||
card.append(text);
|
||
card.append(
|
||
h("button", {
|
||
class: "decision-open",
|
||
type: "button",
|
||
text: DECISION_ACTION,
|
||
onclick: () => openDecisionSheet(decision),
|
||
}),
|
||
);
|
||
return card;
|
||
}
|
||
|
||
/** Cancel/Retry actions, shown exactly per the app's eligibility rules. */
|
||
function renderRunActions(d) {
|
||
const buttons = [];
|
||
if (canCancel(d)) {
|
||
buttons.push(
|
||
h("button", {
|
||
class: "run-action cancel-run",
|
||
type: "button",
|
||
text: "Cancel run",
|
||
onclick: () => openConfirm({ kind: "cancel", runId: d.id }),
|
||
}),
|
||
);
|
||
}
|
||
if (canRetry(d)) {
|
||
buttons.push(
|
||
h("button", {
|
||
class: "run-action retry-run",
|
||
type: "button",
|
||
text: "Retry",
|
||
onclick: () => openConfirm({ kind: "retry", runId: d.id }),
|
||
}),
|
||
);
|
||
}
|
||
if (!buttons.length) return null;
|
||
return h("div", { class: "run-actions" }, ...buttons);
|
||
}
|
||
|
||
function renderRunTitleRow(d) {
|
||
const row = h("div", { class: "run-title-row" });
|
||
if (state.renameValue == null) {
|
||
row.append(
|
||
h("button", {
|
||
class: "ghost rename-open",
|
||
type: "button",
|
||
text: RENAME_ACTION,
|
||
onclick: () => {
|
||
state.renameValue = String(d.title ?? "");
|
||
renderContent();
|
||
},
|
||
}),
|
||
);
|
||
return row;
|
||
}
|
||
const field = h("div", { class: "rename-field" });
|
||
field.append(
|
||
h("input", {
|
||
class: "rename-input",
|
||
type: "text",
|
||
value: state.renameValue,
|
||
"aria-label": RENAME_FIELD_LABEL,
|
||
oninput: (event) => {
|
||
state.renameValue = event.target.value;
|
||
},
|
||
}),
|
||
);
|
||
field.append(
|
||
h("button", {
|
||
class: "rename-save",
|
||
type: "button",
|
||
text: RENAME_SAVE,
|
||
onclick: () => saveRunTitle(d.id),
|
||
}),
|
||
);
|
||
field.append(
|
||
h("button", {
|
||
class: "ghost rename-cancel",
|
||
type: "button",
|
||
text: RENAME_CANCEL,
|
||
onclick: () => {
|
||
state.renameValue = null;
|
||
renderContent();
|
||
},
|
||
}),
|
||
);
|
||
row.append(field);
|
||
return row;
|
||
}
|
||
|
||
/**
|
||
* Rename the run: send `set_run_title`, then update the run detail and the runs
|
||
* list row. A refusal (for example a blank title) keeps the field open and shows
|
||
* the server's exact reason through the shared Action-failed dialog.
|
||
*/
|
||
async function saveRunTitle(id) {
|
||
if (state.renameBusy) return;
|
||
state.renameBusy = true;
|
||
try {
|
||
const detail = await repo.setRunTitle(id, String(state.renameValue ?? ""));
|
||
state.renameValue = null;
|
||
applyRunTitle(id, detail);
|
||
await selectRun(id, { silent: true });
|
||
} catch (error) {
|
||
state.actionError = describeError(error);
|
||
renderContent();
|
||
} finally {
|
||
state.renameBusy = false;
|
||
}
|
||
}
|
||
|
||
/** Apply a renamed run to the loaded detail and the runs-list row. */
|
||
function applyRunTitle(id, detail) {
|
||
if (state.runDetail && state.runDetail.id === id && detail) state.runDetail = detail;
|
||
const title = String(detail?.title ?? "");
|
||
state.runs = state.runs.map((run) => (run.id === id ? { ...run, title } : run));
|
||
}
|
||
|
||
/** Whether the configured server URL can resolve a server-relative artifact. */
|
||
function artifactsResolvable() {
|
||
return resolveArtifactUrl(state.url, "/artifacts/0/0/probe") != null;
|
||
}
|
||
|
||
/**
|
||
* The run-level gallery: one header per (stage, visit) and one grid of tiles per
|
||
* visit, in the gallery order (stages first-appearance, visits by sequence). A
|
||
* run with no artifacts shows the header (which already says why) and nothing
|
||
* else, so the section is never blank.
|
||
*/
|
||
function renderArtifactGallery(steps) {
|
||
const wrap = h("div", { class: "artifact-gallery" });
|
||
const groups = artifactGroups(steps);
|
||
if (!groups.length) return wrap;
|
||
if (!artifactsResolvable()) {
|
||
wrap.append(h("div", { class: "muted artifact-hint", text: ARTIFACT_SERVER_HINT }));
|
||
}
|
||
const viewable = pagedArtifacts(groups);
|
||
const viewerIndexOf = viewerIndexByUrl(groups);
|
||
for (const group of groups) {
|
||
wrap.append(
|
||
h("div", { class: "artifact-group-head", text: `${group.stageKey} · visit ${group.visit}` }),
|
||
renderArtifactGrid(group.artifacts, viewable, viewerIndexOf),
|
||
);
|
||
}
|
||
return wrap;
|
||
}
|
||
|
||
/**
|
||
* The per-visit artifacts section shown inside one step: the summary line, the
|
||
* capture notes, the no-server-URL hint and a headerless grid of that visit's
|
||
* tiles (matching the Android ArtifactsSection). Its viewer pages only through
|
||
* that visit's viewable artifacts (media and markdown), exactly as the Android
|
||
* section does.
|
||
*/
|
||
function renderVisitArtifacts(step) {
|
||
const wrap = h("div", { class: "artifacts" });
|
||
const artifacts = presentedArtifacts(step);
|
||
// The section title already carries the count, so the body repeats it only when
|
||
// there is nothing to show and the reason itself is the useful part.
|
||
if (artifacts.length === 0) wrap.append(h("div", { class: "artifacts-summary", text: artifactsSummary(step) }));
|
||
for (const note of artifactCaptureNotes(step)) {
|
||
wrap.append(h("div", { class: "artifacts-note muted", text: note }));
|
||
}
|
||
if (artifacts.length) {
|
||
if (!artifactsResolvable()) {
|
||
wrap.append(h("div", { class: "muted artifact-hint", text: ARTIFACT_SERVER_HINT }));
|
||
}
|
||
const group = { stageKey: step.stage, visit: displayVisit(step, 0), artifacts };
|
||
wrap.append(
|
||
renderArtifactGrid(artifacts, pagedArtifacts([group]), viewerIndexByUrl([group])),
|
||
);
|
||
}
|
||
return wrap;
|
||
}
|
||
|
||
/** One visit's tiles in a CSS grid. */
|
||
function renderArtifactGrid(artifacts, viewable, viewerIndexOf) {
|
||
const grid = h("div", { class: "artifact-grid" });
|
||
for (const artifact of artifacts) {
|
||
grid.append(renderArtifactTile(artifact, viewable, viewerIndexOf));
|
||
}
|
||
return grid;
|
||
}
|
||
|
||
/**
|
||
* One gallery tile: an image thumbnail, a video poster with a play marker (never
|
||
* a <video> element or autoplay), or a kind/name/size label. Markdown and text
|
||
* documents get a paper glyph and open in the viewer like media. Every server
|
||
* string is set as text, never as markup.
|
||
*/
|
||
function renderArtifactTile(artifact, viewable, viewerIndexOf) {
|
||
const resolved = resolvedArtifactUrl(artifact, state.url);
|
||
const kind = artifactKind(artifact.content_type, artifact.name);
|
||
const media = kind === ArtifactKind.Image || kind === ArtifactKind.Video;
|
||
const opensViewer = isViewerArtifact(artifact.content_type, artifact.name);
|
||
const name = artifact.name || "(unnamed artifact)";
|
||
// The tile is the native control (M5): Enter/Space activate it for free, and
|
||
// the Copy link / Share buttons are siblings in the wrapper, never children.
|
||
const wrap = h("div", { class: "artifact-tile-wrap" });
|
||
const tile = h("button", {
|
||
class: "artifact-tile clickable",
|
||
type: "button",
|
||
"aria-label": `Open ${name}`,
|
||
"data-artifact-name": artifact.name ?? "",
|
||
"data-artifact-kind": kind,
|
||
"data-artifact-url": artifact.url ?? "",
|
||
});
|
||
const openViewer = () => {
|
||
const at = viewerIndexOf.get(artifact.url);
|
||
if (at != null) openArtifactViewer(viewable, at, tile);
|
||
};
|
||
const openTab = () => {
|
||
if (resolved) window.open(resolved, "_blank", "noopener,noreferrer");
|
||
};
|
||
if (kind === ArtifactKind.Image) {
|
||
const img = h("img", { class: "artifact-thumb", alt: name, loading: "lazy" });
|
||
if (resolved) img.src = resolved;
|
||
tile.append(img);
|
||
tile.addEventListener("click", openViewer);
|
||
} else if (kind === ArtifactKind.Video) {
|
||
tile.append(
|
||
h(
|
||
"div",
|
||
{ class: "artifact-poster" },
|
||
h("span", { class: "artifact-play", "aria-hidden": "true", text: "▶" }),
|
||
),
|
||
);
|
||
tile.append(h("div", { class: "artifact-name", text: name }));
|
||
tile.addEventListener("click", openViewer);
|
||
} else {
|
||
if (opensViewer) {
|
||
tile.classList.add("artifact-document-tile");
|
||
tile.append(h("span", { class: "artifact-doc-glyph", "aria-hidden": "true" }));
|
||
}
|
||
tile.append(
|
||
h("div", { class: "artifact-kind", text: artifactKindLabel(kind) }),
|
||
h("div", { class: "artifact-name", text: name }),
|
||
h("div", { class: "artifact-size", text: humanBytes(artifact.size_bytes) }),
|
||
);
|
||
tile.addEventListener("click", opensViewer ? openViewer : openTab);
|
||
}
|
||
wrap.append(tile, renderTileActions(resolved, media));
|
||
return wrap;
|
||
}
|
||
|
||
/** The compact per-tile actions: copy-link (non-media tiles) and share. */
|
||
function renderTileActions(resolved, media) {
|
||
const actions = h("div", { class: "artifact-actions" });
|
||
const stop = (fn) => (event) => {
|
||
event.stopPropagation();
|
||
fn();
|
||
};
|
||
if (!media) {
|
||
actions.append(
|
||
h("button", {
|
||
class: "artifact-action",
|
||
type: "button",
|
||
text: "Copy link",
|
||
disabled: !resolved || !canCopyLink(),
|
||
onclick: stop(() => copyArtifactLink(resolved)),
|
||
}),
|
||
);
|
||
}
|
||
actions.append(
|
||
h("button", {
|
||
class: "artifact-action",
|
||
type: "button",
|
||
text: "Share",
|
||
disabled: !resolved || !canShare(),
|
||
onclick: stop(() => shareArtifact(resolved)),
|
||
}),
|
||
);
|
||
return actions;
|
||
}
|
||
|
||
/**
|
||
* The step's session export: a link opening the read-only session.html in a new
|
||
* tab, or the plain notice when the visit exported nothing (parity R9).
|
||
*/
|
||
function renderSessionExport(step) {
|
||
const file = sessionExportArtifact(step);
|
||
const wrap = h("div", { class: "session-export" });
|
||
if (!file) {
|
||
wrap.append(h("span", { class: "muted", text: "No session was exported for this visit." }));
|
||
return wrap;
|
||
}
|
||
const href = resolvedArtifactUrl(file, state.url);
|
||
if (href) {
|
||
wrap.append(
|
||
h("a", {
|
||
class: "session-link",
|
||
href,
|
||
target: "_blank",
|
||
rel: "noreferrer",
|
||
text: `Open ${file.name || "session.html"}`,
|
||
}),
|
||
);
|
||
} else {
|
||
wrap.append(
|
||
h("span", { class: "muted", text: "Set the server URL in Settings to open the session." }),
|
||
);
|
||
}
|
||
return wrap;
|
||
}
|
||
|
||
return {
|
||
renderRunDetail,
|
||
renderRunGraphTab,
|
||
renderRunSummaryTab,
|
||
renderStagePane,
|
||
openStagePane,
|
||
closeStagePane,
|
||
renderArtifactGallery,
|
||
renderVisitArtifacts,
|
||
renderSessionExport,
|
||
latestVisitIndex,
|
||
};
|
||
}
|