feat: add minimum API and web GUI
This commit is contained in:
43
apps/web/src/api.ts
Normal file
43
apps/web/src/api.ts
Normal file
@@ -0,0 +1,43 @@
|
||||
import { normalizeApiBase } from "./view-model.js";
|
||||
|
||||
export interface ApiClient {
|
||||
get<T>(path: string): Promise<T>;
|
||||
post<T>(path: string, body?: unknown): Promise<T>;
|
||||
sseUrl(path: string): string;
|
||||
}
|
||||
|
||||
export function createApiClient(baseValue: string | undefined): ApiClient {
|
||||
const base = normalizeApiBase(baseValue);
|
||||
return {
|
||||
get: (path) => request("GET", `${base}${path}`),
|
||||
post: (path, body) => request("POST", `${base}${path}`, body),
|
||||
sseUrl: (path) => `${base}${path}`,
|
||||
};
|
||||
}
|
||||
|
||||
async function request<T>(method: string, url: string, body?: unknown): Promise<T> {
|
||||
const init: RequestInit = { method };
|
||||
if (body !== undefined) {
|
||||
init.headers = { "content-type": "application/json" };
|
||||
init.body = JSON.stringify(body);
|
||||
}
|
||||
const response = await fetch(url, init);
|
||||
const payload = (await response.json().catch(() => ({}))) as unknown;
|
||||
if (!response.ok) {
|
||||
throw new Error(errorMessage(payload, response.status));
|
||||
}
|
||||
return payload as T;
|
||||
}
|
||||
|
||||
function errorMessage(payload: unknown, status: number): string {
|
||||
if (payload !== null && typeof payload === "object") {
|
||||
const error = (payload as Record<string, unknown>).error;
|
||||
if (error !== null && typeof error === "object") {
|
||||
const message = (error as Record<string, unknown>).message;
|
||||
if (typeof message === "string") {
|
||||
return message;
|
||||
}
|
||||
}
|
||||
}
|
||||
return `HTTP ${status}`;
|
||||
}
|
||||
661
apps/web/src/main.ts
Normal file
661
apps/web/src/main.ts
Normal file
@@ -0,0 +1,661 @@
|
||||
import "./styles.css";
|
||||
|
||||
import { createApiClient } from "./api.js";
|
||||
import {
|
||||
type ApprovalDecisionAction,
|
||||
type DoctorCheck,
|
||||
type RunSummary,
|
||||
approvalDecisionActions,
|
||||
compactPath,
|
||||
formatDateTime,
|
||||
highestDoctorStatus,
|
||||
screenFromHash,
|
||||
screens,
|
||||
stateTone,
|
||||
} from "./view-model.js";
|
||||
|
||||
interface TemplateSummary {
|
||||
id: string;
|
||||
name: string;
|
||||
version: number;
|
||||
hash: string;
|
||||
definition: { phases?: unknown[]; roles?: unknown[] };
|
||||
}
|
||||
|
||||
interface PersonaSummary {
|
||||
id: string;
|
||||
name: string;
|
||||
version: number;
|
||||
hash: string;
|
||||
definition: { backend?: string; capabilities?: unknown[]; maxRiskLevel?: string };
|
||||
}
|
||||
|
||||
interface RunStatus {
|
||||
run: RunSummary & {
|
||||
worktreeRoot: string;
|
||||
finalReportPath: string | null;
|
||||
};
|
||||
phases: Array<{ id: string; phaseKey: string; state: string; attempts: number }>;
|
||||
approvals: Array<{ id: string; gateKey: string; state: string; phaseId: string | null }>;
|
||||
eventsTail: Array<{ seq: string; type: string; ts: string; payload: unknown }>;
|
||||
}
|
||||
|
||||
interface SessionSummary {
|
||||
id: string;
|
||||
roleId: string;
|
||||
backend: string;
|
||||
cwd: string;
|
||||
state: string;
|
||||
expectedArtifactPath: string | null;
|
||||
expectedSchema: string | null;
|
||||
lastPromptAt: string | null;
|
||||
recoveryAttempts: number;
|
||||
tmuxSession: string | null;
|
||||
tmuxWindow: string | null;
|
||||
}
|
||||
|
||||
const api = createApiClient(import.meta.env.VITE_API_BASE);
|
||||
const appRoot = document.querySelector<HTMLDivElement>("#app");
|
||||
|
||||
if (appRoot === null) {
|
||||
throw new Error("Missing #app root");
|
||||
}
|
||||
const app = appRoot;
|
||||
|
||||
let activeRunId: string | undefined;
|
||||
let activeRunStatus: RunStatus | undefined;
|
||||
let activeRunSessions: SessionSummary[] = [];
|
||||
let connectedRunId: string | undefined;
|
||||
let doctorChecks: DoctorCheck[] = [];
|
||||
let eventSource: EventSource | undefined;
|
||||
let liveEvents: string[] = [];
|
||||
const approvalErrors = new Map<string, string>();
|
||||
const approvalTokens = new Map<string, string>();
|
||||
|
||||
window.addEventListener("hashchange", () => {
|
||||
void render();
|
||||
});
|
||||
|
||||
void render();
|
||||
|
||||
async function render(): Promise<void> {
|
||||
const screen = screenFromHash(window.location.hash);
|
||||
app.innerHTML = shell(screen, "Loading");
|
||||
try {
|
||||
doctorChecks = await loadDoctorChecks();
|
||||
if (screen === "dashboard") {
|
||||
await renderDashboard();
|
||||
} else if (screen === "run-detail") {
|
||||
await renderRunDetailScreen();
|
||||
} else if (screen === "approvals") {
|
||||
await renderApprovalsScreen();
|
||||
} else if (screen === "sessions") {
|
||||
await renderSessionsScreen();
|
||||
} else if (screen === "templates") {
|
||||
await renderTemplates();
|
||||
} else if (screen === "personas") {
|
||||
await renderPersonas();
|
||||
} else {
|
||||
await renderNewRun();
|
||||
}
|
||||
} catch (error) {
|
||||
app.innerHTML = shell(screen, errorBanner(error));
|
||||
}
|
||||
}
|
||||
|
||||
async function renderDashboard(): Promise<void> {
|
||||
const { runs, sessions, status } = await loadActiveRunData();
|
||||
app.innerHTML = shell(
|
||||
"dashboard",
|
||||
`
|
||||
${doctorPanel()}
|
||||
<section class="band split">
|
||||
<div>
|
||||
<div class="section-title">
|
||||
<h2>Runs</h2>
|
||||
<button class="small" data-action="refresh">Refresh</button>
|
||||
</div>
|
||||
${runsTable(runs)}
|
||||
</div>
|
||||
<div>
|
||||
<div class="section-title">
|
||||
<h2>Run Detail</h2>
|
||||
${activeRunId === undefined ? "" : `<span class="mono">${escapeHtml(activeRunId)}</span>`}
|
||||
</div>
|
||||
${status === undefined ? empty("No run selected") : runDetail(status, sessions)}
|
||||
</div>
|
||||
</section>
|
||||
`,
|
||||
);
|
||||
bindDashboardActions();
|
||||
connectRunStream(activeRunId);
|
||||
}
|
||||
|
||||
async function renderRunDetailScreen(): Promise<void> {
|
||||
const { runs, sessions, status } = await loadActiveRunData();
|
||||
app.innerHTML = shell(
|
||||
"run-detail",
|
||||
`
|
||||
<section class="band split">
|
||||
<div>
|
||||
<div class="section-title"><h2>Runs</h2><button class="small" data-action="refresh">Refresh</button></div>
|
||||
${runsTable(runs)}
|
||||
</div>
|
||||
<div>
|
||||
<div class="section-title"><h2>Run Detail</h2>${activeRunId === undefined ? "" : `<span class="mono">${escapeHtml(activeRunId)}</span>`}</div>
|
||||
${status === undefined ? empty("No run selected") : runDetail(status, sessions)}
|
||||
</div>
|
||||
</section>
|
||||
`,
|
||||
);
|
||||
bindDashboardActions();
|
||||
connectRunStream(activeRunId);
|
||||
}
|
||||
|
||||
async function renderApprovalsScreen(): Promise<void> {
|
||||
const { runs, status } = await loadActiveRunData();
|
||||
app.innerHTML = shell(
|
||||
"approvals",
|
||||
`
|
||||
<section class="band split">
|
||||
<div>
|
||||
<div class="section-title"><h2>Runs</h2><button class="small" data-action="refresh">Refresh</button></div>
|
||||
${runsTable(runs)}
|
||||
</div>
|
||||
<div>
|
||||
<div class="section-title"><h2>Approvals</h2>${activeRunId === undefined ? "" : `<span class="mono">${escapeHtml(activeRunId)}</span>`}</div>
|
||||
${
|
||||
status === undefined || status.approvals.length === 0
|
||||
? empty("No approvals")
|
||||
: `<table><tbody>${status.approvals.map(approvalRow).join("")}</tbody></table>`
|
||||
}
|
||||
</div>
|
||||
</section>
|
||||
`,
|
||||
);
|
||||
bindDashboardActions();
|
||||
connectRunStream(activeRunId);
|
||||
}
|
||||
|
||||
async function renderSessionsScreen(): Promise<void> {
|
||||
const { runs, sessions } = await loadActiveRunData();
|
||||
app.innerHTML = shell(
|
||||
"sessions",
|
||||
`
|
||||
<section class="band split">
|
||||
<div>
|
||||
<div class="section-title"><h2>Runs</h2><button class="small" data-action="refresh">Refresh</button></div>
|
||||
${runsTable(runs)}
|
||||
</div>
|
||||
<div>
|
||||
<div class="section-title"><h2>TUI Sessions</h2>${activeRunId === undefined ? "" : `<span class="mono">${escapeHtml(activeRunId)}</span>`}</div>
|
||||
${sessions.length === 0 ? empty("No sessions") : sessionsTable(sessions)}
|
||||
</div>
|
||||
</section>
|
||||
`,
|
||||
);
|
||||
bindDashboardActions();
|
||||
connectRunStream(activeRunId);
|
||||
}
|
||||
|
||||
async function renderTemplates(): Promise<void> {
|
||||
const { templates } = await api.get<{ templates: TemplateSummary[] }>("/api/templates");
|
||||
app.innerHTML = shell(
|
||||
"templates",
|
||||
`
|
||||
<section class="band">
|
||||
<div class="section-title"><h2>Templates</h2><span>${templates.length} loaded</span></div>
|
||||
<table>
|
||||
<thead><tr><th>Name</th><th>Version</th><th>Roles</th><th>Phases</th><th>Hash</th></tr></thead>
|
||||
<tbody>
|
||||
${templates
|
||||
.map(
|
||||
(template) => `
|
||||
<tr>
|
||||
<td>${escapeHtml(template.name)}</td>
|
||||
<td>${template.version}</td>
|
||||
<td>${arrayLength(template.definition.roles)}</td>
|
||||
<td>${arrayLength(template.definition.phases)}</td>
|
||||
<td class="mono">${escapeHtml(template.hash.slice(0, 16))}</td>
|
||||
</tr>
|
||||
`,
|
||||
)
|
||||
.join("")}
|
||||
</tbody>
|
||||
</table>
|
||||
</section>
|
||||
`,
|
||||
);
|
||||
}
|
||||
|
||||
async function renderPersonas(): Promise<void> {
|
||||
const { personas } = await api.get<{ personas: PersonaSummary[] }>("/api/personas");
|
||||
app.innerHTML = shell(
|
||||
"personas",
|
||||
`
|
||||
<section class="band">
|
||||
<div class="section-title"><h2>Personas</h2><span>${personas.length} loaded</span></div>
|
||||
<table>
|
||||
<thead><tr><th>Name</th><th>Version</th><th>Backend</th><th>Risk</th><th>Capabilities</th></tr></thead>
|
||||
<tbody>
|
||||
${personas
|
||||
.map(
|
||||
(persona) => `
|
||||
<tr>
|
||||
<td>${escapeHtml(persona.name)}</td>
|
||||
<td>${persona.version}</td>
|
||||
<td>${escapeHtml(persona.definition.backend ?? "")}</td>
|
||||
<td>${escapeHtml(persona.definition.maxRiskLevel ?? "")}</td>
|
||||
<td>${arrayLength(persona.definition.capabilities)}</td>
|
||||
</tr>
|
||||
`,
|
||||
)
|
||||
.join("")}
|
||||
</tbody>
|
||||
</table>
|
||||
</section>
|
||||
`,
|
||||
);
|
||||
}
|
||||
|
||||
async function renderNewRun(): Promise<void> {
|
||||
const [{ templates }, { personas }] = await Promise.all([
|
||||
api.get<{ templates: TemplateSummary[] }>("/api/templates"),
|
||||
api.get<{ personas: PersonaSummary[] }>("/api/personas"),
|
||||
]);
|
||||
app.innerHTML = shell(
|
||||
"new-run",
|
||||
`
|
||||
<section class="band new-run">
|
||||
<div class="section-title">
|
||||
<h2>New Run</h2>
|
||||
<span>${templates.length} templates · ${personas.length} personas</span>
|
||||
</div>
|
||||
<form id="new-run-form">
|
||||
<label>Requirements<textarea name="requirementsMd" rows="8" required></textarea></label>
|
||||
<div class="form-grid">
|
||||
<label>Repository path<input name="repoPath" required /></label>
|
||||
<label>Base branch<input name="baseBranch" value="main" required /></label>
|
||||
<label>Template name<input name="templateName" value="development" /></label>
|
||||
<label>Template version<input name="templateVersion" type="number" value="1" min="1" /></label>
|
||||
</div>
|
||||
<label>Scenarios JSON<textarea name="scenarios" rows="5" placeholder='{"spec":"ok","phase_plan":"ok"}'></textarea></label>
|
||||
<div class="actions"><button type="submit">Start Run</button><span id="form-status"></span></div>
|
||||
</form>
|
||||
</section>
|
||||
`,
|
||||
);
|
||||
bindNewRunForm();
|
||||
}
|
||||
|
||||
function shell(active: string, content: string): string {
|
||||
return `
|
||||
<header>
|
||||
<div>
|
||||
<h1>Devflow</h1>
|
||||
<p>Local workflow control plane</p>
|
||||
</div>
|
||||
<nav>
|
||||
${screens
|
||||
.map(
|
||||
(screen) =>
|
||||
`<a class="${screen === active ? "active" : ""}" href="#/${screen}">${screenLabel(screen)}</a>`,
|
||||
)
|
||||
.join("")}
|
||||
</nav>
|
||||
</header>
|
||||
<main>${content}</main>
|
||||
`;
|
||||
}
|
||||
|
||||
function doctorPanel(): string {
|
||||
const status = highestDoctorStatus(doctorChecks);
|
||||
const visible = doctorChecks.filter((check) => check.status !== "pass");
|
||||
return `
|
||||
<section class="doctor ${status}">
|
||||
<strong>Doctor</strong>
|
||||
${
|
||||
visible.length === 0
|
||||
? "<span>All visible checks pass</span>"
|
||||
: visible
|
||||
.map((check) => `<span>${escapeHtml(check.name)}: ${escapeHtml(check.detail)}</span>`)
|
||||
.join("")
|
||||
}
|
||||
</section>
|
||||
`;
|
||||
}
|
||||
|
||||
function runsTable(runs: RunSummary[]): string {
|
||||
if (runs.length === 0) {
|
||||
return empty("No runs yet");
|
||||
}
|
||||
return `
|
||||
<table>
|
||||
<thead><tr><th>State</th><th>Repo</th><th>Branch</th><th>Created</th><th></th></tr></thead>
|
||||
<tbody>
|
||||
${runs
|
||||
.map(
|
||||
(run) => `
|
||||
<tr class="${run.id === activeRunId ? "selected" : ""}">
|
||||
<td><span class="pill ${stateTone(run.state)}">${escapeHtml(run.state)}</span></td>
|
||||
<td title="${escapeHtml(run.repoPath)}">${escapeHtml(compactPath(run.repoPath))}</td>
|
||||
<td>${escapeHtml(run.baseBranch)}</td>
|
||||
<td>${escapeHtml(formatDateTime(run.createdAt))}</td>
|
||||
<td><button class="small" data-run-id="${escapeHtml(run.id)}">Open</button></td>
|
||||
</tr>
|
||||
`,
|
||||
)
|
||||
.join("")}
|
||||
</tbody>
|
||||
</table>
|
||||
`;
|
||||
}
|
||||
|
||||
function runDetail(status: RunStatus, sessions: SessionSummary[]): string {
|
||||
return `
|
||||
<div class="detail-grid">
|
||||
<div><span>State</span><strong class="pill ${stateTone(status.run.state)}">${escapeHtml(status.run.state)}</strong></div>
|
||||
<div><span>Worktree</span><strong title="${escapeHtml(status.run.worktreeRoot)}">${escapeHtml(compactPath(status.run.worktreeRoot))}</strong></div>
|
||||
<div><span>Final report</span><strong>${escapeHtml(status.run.finalReportPath ?? "")}</strong></div>
|
||||
</div>
|
||||
<h3>Phases</h3>
|
||||
<table>
|
||||
<thead><tr><th>Phase</th><th>State</th><th>Attempts</th></tr></thead>
|
||||
<tbody>
|
||||
${status.phases
|
||||
.map(
|
||||
(phase) =>
|
||||
`<tr><td>${escapeHtml(phase.phaseKey)}</td><td>${escapeHtml(phase.state)}</td><td>${phase.attempts}</td></tr>`,
|
||||
)
|
||||
.join("")}
|
||||
</tbody>
|
||||
</table>
|
||||
<h3>Approvals</h3>
|
||||
${
|
||||
status.approvals.length === 0
|
||||
? empty("No approvals")
|
||||
: `<table><tbody>${status.approvals.map(approvalRow).join("")}</tbody></table>`
|
||||
}
|
||||
<h3>TUI Sessions</h3>
|
||||
${sessions.length === 0 ? empty("No sessions") : sessionsTable(sessions)}
|
||||
<h3>Event Tail</h3>
|
||||
<pre>${escapeHtml([...status.eventsTail.map((event) => `${event.seq} ${event.type}`), ...liveEvents].slice(-40).join("\n"))}</pre>
|
||||
`;
|
||||
}
|
||||
|
||||
function sessionsTable(sessions: SessionSummary[]): string {
|
||||
return `<table>
|
||||
<thead><tr><th>Role</th><th>Backend</th><th>State</th><th>CWD</th><th>Artifact</th><th>Recovery</th></tr></thead>
|
||||
<tbody>${sessions.map(sessionRow).join("")}</tbody>
|
||||
</table>`;
|
||||
}
|
||||
|
||||
function sessionRow(session: SessionSummary): string {
|
||||
const artifact =
|
||||
session.expectedArtifactPath === null
|
||||
? ""
|
||||
: `${session.expectedSchema ?? ""} ${compactPath(session.expectedArtifactPath)}`;
|
||||
return `
|
||||
<tr>
|
||||
<td>${escapeHtml(session.roleId)}</td>
|
||||
<td>${escapeHtml(session.backend)}</td>
|
||||
<td><span class="pill ${stateTone(session.state)}">${escapeHtml(session.state)}</span></td>
|
||||
<td title="${escapeHtml(session.cwd)}">${escapeHtml(compactPath(session.cwd))}</td>
|
||||
<td title="${escapeHtml(session.expectedArtifactPath ?? "")}">${escapeHtml(artifact)}</td>
|
||||
<td>${session.recoveryAttempts}</td>
|
||||
</tr>
|
||||
`;
|
||||
}
|
||||
|
||||
function approvalRow(approval: RunStatus["approvals"][number]): string {
|
||||
const disabled = approval.state !== "pending" ? "disabled" : "";
|
||||
const actionCells = approvalDecisionActions
|
||||
.map((action) => approvalButton(approval.id, action, disabled))
|
||||
.join("");
|
||||
const error = approvalDecisionActions
|
||||
.map((action) => approvalErrors.get(approvalActionKey(approval.id, action)))
|
||||
.find((message) => message !== undefined);
|
||||
return `
|
||||
<tr>
|
||||
<td>${escapeHtml(approval.gateKey)}</td>
|
||||
<td>${escapeHtml(approval.state)}</td>
|
||||
<td class="approval-actions">
|
||||
${actionCells}
|
||||
</td>
|
||||
</tr>
|
||||
${error === undefined ? "" : `<tr><td colspan="3" class="error">${escapeHtml(error)}</td></tr>`}
|
||||
`;
|
||||
}
|
||||
|
||||
function approvalButton(
|
||||
approvalId: string,
|
||||
action: ApprovalDecisionAction,
|
||||
disabled: string,
|
||||
): string {
|
||||
const danger = action === "reject" || action === "abort" ? " danger" : "";
|
||||
return `<button class="small${danger}" data-approval="${escapeHtml(approvalId)}" data-action="${action}" ${disabled}>${approvalActionLabel(action)}</button>`;
|
||||
}
|
||||
|
||||
function approvalActionLabel(action: ApprovalDecisionAction): string {
|
||||
if (action === "request_changes") {
|
||||
return "Request Changes";
|
||||
}
|
||||
return action.slice(0, 1).toUpperCase() + action.slice(1);
|
||||
}
|
||||
|
||||
function bindDashboardActions(): void {
|
||||
document.querySelector('[data-action="refresh"]')?.addEventListener("click", () => {
|
||||
void render();
|
||||
});
|
||||
for (const button of document.querySelectorAll<HTMLButtonElement>("[data-run-id]")) {
|
||||
button.addEventListener("click", () => {
|
||||
activeRunId = button.dataset.runId;
|
||||
liveEvents = [];
|
||||
void render();
|
||||
});
|
||||
}
|
||||
for (const button of document.querySelectorAll<HTMLButtonElement>("[data-approval]")) {
|
||||
button.addEventListener("click", async () => {
|
||||
const action = parseApprovalAction(button.dataset.action);
|
||||
if (activeRunId === undefined || button.dataset.approval === undefined || action === null) {
|
||||
return;
|
||||
}
|
||||
const key = approvalActionKey(button.dataset.approval, action);
|
||||
const clientToken = approvalTokens.get(key) ?? crypto.randomUUID();
|
||||
approvalTokens.set(key, clientToken);
|
||||
disableApprovalButtons(button.dataset.approval);
|
||||
try {
|
||||
await api.post(`/api/runs/${activeRunId}/approvals/${button.dataset.approval}`, {
|
||||
action,
|
||||
clientToken,
|
||||
});
|
||||
approvalTokens.delete(key);
|
||||
approvalErrors.delete(key);
|
||||
await render();
|
||||
} catch (error) {
|
||||
approvalErrors.set(key, error instanceof Error ? error.message : String(error));
|
||||
await render();
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
function parseApprovalAction(value: string | undefined): ApprovalDecisionAction | null {
|
||||
return approvalDecisionActions.includes(value as ApprovalDecisionAction)
|
||||
? (value as ApprovalDecisionAction)
|
||||
: null;
|
||||
}
|
||||
|
||||
function approvalActionKey(approvalId: string, action: ApprovalDecisionAction): string {
|
||||
return `${approvalId}:${action}`;
|
||||
}
|
||||
|
||||
function disableApprovalButtons(approvalId: string): void {
|
||||
for (const button of document.querySelectorAll<HTMLButtonElement>("[data-approval]")) {
|
||||
if (button.dataset.approval === approvalId) {
|
||||
button.disabled = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function bindNewRunForm(): void {
|
||||
document
|
||||
.querySelector<HTMLFormElement>("#new-run-form")
|
||||
?.addEventListener("submit", async (event) => {
|
||||
event.preventDefault();
|
||||
const form = event.currentTarget as HTMLFormElement;
|
||||
const data = new FormData(form);
|
||||
const status = document.querySelector("#form-status");
|
||||
try {
|
||||
const scenariosText = stringFormValue(data, "scenarios");
|
||||
const body = {
|
||||
baseBranch: stringFormValue(data, "baseBranch"),
|
||||
repoPath: stringFormValue(data, "repoPath"),
|
||||
requirementsMd: stringFormValue(data, "requirementsMd"),
|
||||
templateName: stringFormValue(data, "templateName"),
|
||||
templateVersion: Number(stringFormValue(data, "templateVersion")),
|
||||
...(scenariosText.length === 0
|
||||
? {}
|
||||
: { scenarios: JSON.parse(scenariosText) as unknown }),
|
||||
};
|
||||
const result = await api.post<{ runId: string }>("/api/runs", body);
|
||||
activeRunId = result.runId;
|
||||
window.location.hash = "#/dashboard";
|
||||
} catch (error) {
|
||||
if (status !== null) {
|
||||
status.textContent = error instanceof Error ? error.message : String(error);
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
function connectRunStream(runId: string | undefined): void {
|
||||
if (eventSource !== undefined && connectedRunId === runId) {
|
||||
return;
|
||||
}
|
||||
eventSource?.close();
|
||||
eventSource = undefined;
|
||||
connectedRunId = undefined;
|
||||
if (runId === undefined) {
|
||||
return;
|
||||
}
|
||||
const source = new EventSource(api.sseUrl(`/sse/runs/${runId}`));
|
||||
source.addEventListener("run.event_appended", (event) => {
|
||||
const payload = JSON.parse(String((event as MessageEvent).data)) as { type?: string };
|
||||
liveEvents.push(`live ${payload.type ?? "event"}`);
|
||||
if (liveEvents.length > 50) {
|
||||
liveEvents = liveEvents.slice(-50);
|
||||
}
|
||||
void Promise.all([loadRunStatus(runId), loadRunSessions(runId)]).then(([status, sessions]) => {
|
||||
activeRunStatus = status;
|
||||
activeRunSessions = sessions;
|
||||
const main = document.querySelector("main");
|
||||
const screen = screenFromHash(window.location.hash);
|
||||
if (
|
||||
main !== null &&
|
||||
(screen === "dashboard" ||
|
||||
screen === "run-detail" ||
|
||||
screen === "approvals" ||
|
||||
screen === "sessions")
|
||||
) {
|
||||
void render();
|
||||
}
|
||||
});
|
||||
});
|
||||
source.addEventListener("transcript.chunk_appended", (event) => {
|
||||
const payload = JSON.parse(String((event as MessageEvent).data)) as { content?: string };
|
||||
liveEvents.push(`transcript ${payload.content ?? ""}`);
|
||||
});
|
||||
eventSource = source;
|
||||
connectedRunId = runId;
|
||||
}
|
||||
|
||||
async function loadDoctorChecks(): Promise<DoctorCheck[]> {
|
||||
try {
|
||||
const response = await api.get<{ checks: DoctorCheck[] }>("/api/doctor");
|
||||
return response.checks;
|
||||
} catch {
|
||||
return [
|
||||
{
|
||||
detail: "API doctor endpoint unavailable",
|
||||
name: "api",
|
||||
remediation: "Start apps/api or configure VITE_API_BASE.",
|
||||
status: "warn",
|
||||
},
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
async function loadRunStatus(runId: string): Promise<RunStatus> {
|
||||
return api.get<RunStatus>(`/api/runs/${runId}`);
|
||||
}
|
||||
|
||||
async function loadActiveRunData(): Promise<{
|
||||
runs: RunSummary[];
|
||||
sessions: SessionSummary[];
|
||||
status: RunStatus | undefined;
|
||||
}> {
|
||||
const { runs } = await api.get<{ runs: RunSummary[] }>("/api/runs");
|
||||
if (activeRunId === undefined && runs[0] !== undefined) {
|
||||
activeRunId = runs[0].id;
|
||||
}
|
||||
if (activeRunId === undefined) {
|
||||
activeRunStatus = undefined;
|
||||
activeRunSessions = [];
|
||||
return { runs, sessions: [], status: undefined };
|
||||
}
|
||||
const [status, sessions] = await Promise.all([
|
||||
loadRunStatus(activeRunId),
|
||||
loadRunSessions(activeRunId),
|
||||
]);
|
||||
activeRunStatus = status;
|
||||
activeRunSessions = sessions;
|
||||
return { runs, sessions, status };
|
||||
}
|
||||
|
||||
async function loadRunSessions(runId: string): Promise<SessionSummary[]> {
|
||||
const response = await api.get<{ sessions: SessionSummary[] }>(`/api/runs/${runId}/sessions`);
|
||||
return response.sessions;
|
||||
}
|
||||
|
||||
function screenLabel(screen: string): string {
|
||||
if (screen === "new-run") {
|
||||
return "New Run";
|
||||
}
|
||||
if (screen === "run-detail") {
|
||||
return "Run Detail";
|
||||
}
|
||||
if (screen === "sessions") {
|
||||
return "TUI Sessions";
|
||||
}
|
||||
return screen
|
||||
.split("-")
|
||||
.map((part) => `${part[0]?.toUpperCase() ?? ""}${part.slice(1)}`)
|
||||
.join(" ");
|
||||
}
|
||||
|
||||
function stringFormValue(data: FormData, key: string): string {
|
||||
const value = data.get(key);
|
||||
return typeof value === "string" ? value : "";
|
||||
}
|
||||
|
||||
function arrayLength(value: unknown): number {
|
||||
return Array.isArray(value) ? value.length : 0;
|
||||
}
|
||||
|
||||
function empty(text: string): string {
|
||||
return `<div class="empty">${escapeHtml(text)}</div>`;
|
||||
}
|
||||
|
||||
function errorBanner(error: unknown): string {
|
||||
return `<section class="doctor fail"><strong>Error</strong><span>${escapeHtml(error instanceof Error ? error.message : String(error))}</span></section>`;
|
||||
}
|
||||
|
||||
function escapeHtml(value: string): string {
|
||||
return value
|
||||
.replaceAll("&", "&")
|
||||
.replaceAll("<", "<")
|
||||
.replaceAll(">", ">")
|
||||
.replaceAll('"', """)
|
||||
.replaceAll("'", "'");
|
||||
}
|
||||
317
apps/web/src/styles.css
Normal file
317
apps/web/src/styles.css
Normal file
@@ -0,0 +1,317 @@
|
||||
:root {
|
||||
color: #182026;
|
||||
background: #f5f7f8;
|
||||
font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI",
|
||||
sans-serif;
|
||||
font-size: 14px;
|
||||
letter-spacing: 0;
|
||||
}
|
||||
|
||||
* {
|
||||
box-sizing: border-box;
|
||||
}
|
||||
|
||||
body {
|
||||
margin: 0;
|
||||
}
|
||||
|
||||
button,
|
||||
input,
|
||||
textarea {
|
||||
font: inherit;
|
||||
}
|
||||
|
||||
header {
|
||||
align-items: center;
|
||||
background: #ffffff;
|
||||
border-bottom: 1px solid #d9e0e4;
|
||||
display: flex;
|
||||
justify-content: space-between;
|
||||
min-height: 72px;
|
||||
padding: 14px 24px;
|
||||
}
|
||||
|
||||
h1,
|
||||
h2,
|
||||
h3,
|
||||
p {
|
||||
margin: 0;
|
||||
}
|
||||
|
||||
h1 {
|
||||
font-size: 20px;
|
||||
line-height: 1.2;
|
||||
}
|
||||
|
||||
h2 {
|
||||
font-size: 16px;
|
||||
}
|
||||
|
||||
h3 {
|
||||
font-size: 13px;
|
||||
margin: 18px 0 8px;
|
||||
text-transform: uppercase;
|
||||
}
|
||||
|
||||
header p,
|
||||
.section-title span,
|
||||
.empty,
|
||||
.detail-grid span {
|
||||
color: #61717c;
|
||||
}
|
||||
|
||||
nav {
|
||||
display: flex;
|
||||
gap: 4px;
|
||||
}
|
||||
|
||||
nav a,
|
||||
button {
|
||||
border-radius: 6px;
|
||||
text-decoration: none;
|
||||
}
|
||||
|
||||
nav a {
|
||||
color: #36464f;
|
||||
padding: 8px 10px;
|
||||
}
|
||||
|
||||
nav a.active {
|
||||
background: #17324d;
|
||||
color: #ffffff;
|
||||
}
|
||||
|
||||
main {
|
||||
margin: 0 auto;
|
||||
max-width: 1280px;
|
||||
padding: 20px 24px 48px;
|
||||
}
|
||||
|
||||
.band {
|
||||
background: #ffffff;
|
||||
border: 1px solid #d9e0e4;
|
||||
border-radius: 8px;
|
||||
margin-top: 14px;
|
||||
padding: 16px;
|
||||
}
|
||||
|
||||
.split {
|
||||
display: grid;
|
||||
gap: 16px;
|
||||
grid-template-columns: minmax(360px, 0.9fr) minmax(420px, 1.1fr);
|
||||
}
|
||||
|
||||
.section-title,
|
||||
.actions {
|
||||
align-items: center;
|
||||
display: flex;
|
||||
gap: 10px;
|
||||
justify-content: space-between;
|
||||
margin-bottom: 12px;
|
||||
}
|
||||
|
||||
.doctor {
|
||||
align-items: center;
|
||||
border: 1px solid #d9e0e4;
|
||||
border-radius: 8px;
|
||||
display: flex;
|
||||
gap: 12px;
|
||||
padding: 10px 12px;
|
||||
}
|
||||
|
||||
.doctor.pass {
|
||||
background: #eff8f2;
|
||||
border-color: #b9dfc6;
|
||||
}
|
||||
|
||||
.doctor.warn {
|
||||
background: #fff7e8;
|
||||
border-color: #efcf91;
|
||||
}
|
||||
|
||||
.doctor.fail {
|
||||
background: #fff0f0;
|
||||
border-color: #e2a4a4;
|
||||
}
|
||||
|
||||
table {
|
||||
border-collapse: collapse;
|
||||
table-layout: fixed;
|
||||
width: 100%;
|
||||
}
|
||||
|
||||
th,
|
||||
td {
|
||||
border-bottom: 1px solid #e5eaed;
|
||||
overflow-wrap: anywhere;
|
||||
padding: 9px 8px;
|
||||
text-align: left;
|
||||
vertical-align: middle;
|
||||
}
|
||||
|
||||
th {
|
||||
color: #5e6d77;
|
||||
font-size: 12px;
|
||||
font-weight: 600;
|
||||
}
|
||||
|
||||
tr.selected {
|
||||
background: #f0f5fa;
|
||||
}
|
||||
|
||||
button {
|
||||
background: #17324d;
|
||||
border: 1px solid #17324d;
|
||||
color: #ffffff;
|
||||
cursor: pointer;
|
||||
min-height: 34px;
|
||||
padding: 7px 12px;
|
||||
}
|
||||
|
||||
button.small {
|
||||
min-height: 28px;
|
||||
padding: 4px 8px;
|
||||
}
|
||||
|
||||
button.danger {
|
||||
background: #7b2d2d;
|
||||
border-color: #7b2d2d;
|
||||
}
|
||||
|
||||
button:disabled {
|
||||
background: #c9d1d6;
|
||||
border-color: #c9d1d6;
|
||||
cursor: not-allowed;
|
||||
}
|
||||
|
||||
.pill {
|
||||
border-radius: 999px;
|
||||
display: inline-block;
|
||||
font-size: 12px;
|
||||
font-weight: 600;
|
||||
min-width: 76px;
|
||||
padding: 3px 8px;
|
||||
text-align: center;
|
||||
}
|
||||
|
||||
.pill.active {
|
||||
background: #e7f1fb;
|
||||
color: #205a8c;
|
||||
}
|
||||
|
||||
.pill.blocked {
|
||||
background: #fff0cc;
|
||||
color: #735100;
|
||||
}
|
||||
|
||||
.pill.done {
|
||||
background: #ddf2e3;
|
||||
color: #236b38;
|
||||
}
|
||||
|
||||
.pill.failed {
|
||||
background: #f8dddd;
|
||||
color: #8d2b2b;
|
||||
}
|
||||
|
||||
.pill.neutral {
|
||||
background: #e8edf0;
|
||||
color: #4e5c65;
|
||||
}
|
||||
|
||||
.detail-grid {
|
||||
display: grid;
|
||||
gap: 10px;
|
||||
grid-template-columns: repeat(3, minmax(0, 1fr));
|
||||
}
|
||||
|
||||
.detail-grid div {
|
||||
border-bottom: 1px solid #e5eaed;
|
||||
min-height: 58px;
|
||||
padding: 4px 0 10px;
|
||||
}
|
||||
|
||||
.detail-grid span,
|
||||
.detail-grid strong {
|
||||
display: block;
|
||||
}
|
||||
|
||||
.mono,
|
||||
pre {
|
||||
font-family: "SFMono-Regular", Consolas, "Liberation Mono", monospace;
|
||||
}
|
||||
|
||||
pre {
|
||||
background: #11191f;
|
||||
border-radius: 6px;
|
||||
color: #d9e8ef;
|
||||
min-height: 160px;
|
||||
overflow: auto;
|
||||
padding: 12px;
|
||||
white-space: pre-wrap;
|
||||
}
|
||||
|
||||
.approval-actions {
|
||||
display: flex;
|
||||
flex-wrap: wrap;
|
||||
gap: 6px;
|
||||
}
|
||||
|
||||
.error {
|
||||
color: #8d2b2b;
|
||||
font-weight: 600;
|
||||
}
|
||||
|
||||
form {
|
||||
display: grid;
|
||||
gap: 14px;
|
||||
}
|
||||
|
||||
label {
|
||||
color: #4c5b64;
|
||||
display: grid;
|
||||
gap: 6px;
|
||||
font-weight: 600;
|
||||
}
|
||||
|
||||
input,
|
||||
textarea {
|
||||
border: 1px solid #cbd5da;
|
||||
border-radius: 6px;
|
||||
color: #17232b;
|
||||
min-width: 0;
|
||||
padding: 9px 10px;
|
||||
}
|
||||
|
||||
textarea {
|
||||
resize: vertical;
|
||||
}
|
||||
|
||||
.form-grid {
|
||||
display: grid;
|
||||
gap: 12px;
|
||||
grid-template-columns: repeat(2, minmax(0, 1fr));
|
||||
}
|
||||
|
||||
#form-status {
|
||||
color: #8d2b2b;
|
||||
}
|
||||
|
||||
@media (max-width: 900px) {
|
||||
header,
|
||||
.split,
|
||||
.detail-grid,
|
||||
.form-grid {
|
||||
grid-template-columns: 1fr;
|
||||
}
|
||||
|
||||
header {
|
||||
align-items: flex-start;
|
||||
display: grid;
|
||||
gap: 12px;
|
||||
}
|
||||
|
||||
nav {
|
||||
flex-wrap: wrap;
|
||||
}
|
||||
}
|
||||
46
apps/web/src/view-model.test.ts
Normal file
46
apps/web/src/view-model.test.ts
Normal file
@@ -0,0 +1,46 @@
|
||||
import { describe, expect, it } from "vitest";
|
||||
|
||||
import {
|
||||
approvalDecisionActions,
|
||||
compactPath,
|
||||
highestDoctorStatus,
|
||||
normalizeApiBase,
|
||||
screenFromHash,
|
||||
stateTone,
|
||||
} from "./view-model.js";
|
||||
|
||||
describe("web view model", () => {
|
||||
it("normalizes navigation and API base values", () => {
|
||||
expect(screenFromHash("#/templates")).toBe("templates");
|
||||
expect(screenFromHash("#/run-detail")).toBe("run-detail");
|
||||
expect(screenFromHash("#/approvals")).toBe("approvals");
|
||||
expect(screenFromHash("#/sessions")).toBe("sessions");
|
||||
expect(screenFromHash("#missing")).toBe("dashboard");
|
||||
expect(normalizeApiBase("http://127.0.0.1:3000///")).toBe("http://127.0.0.1:3000");
|
||||
expect(normalizeApiBase(undefined)).toBe("");
|
||||
});
|
||||
|
||||
it("maps operational state tones", () => {
|
||||
expect(stateTone("executing")).toBe("active");
|
||||
expect(stateTone("awaiting_approval")).toBe("blocked");
|
||||
expect(stateTone("completed")).toBe("done");
|
||||
expect(stateTone("failed")).toBe("failed");
|
||||
expect(stateTone("created")).toBe("neutral");
|
||||
});
|
||||
|
||||
it("summarizes doctor severity and long paths", () => {
|
||||
expect(
|
||||
highestDoctorStatus([
|
||||
{ name: "config", status: "pass", detail: "", remediation: "" },
|
||||
{ name: "backend.codex", status: "warn", detail: "", remediation: "" },
|
||||
]),
|
||||
).toBe("warn");
|
||||
expect(compactPath("/a/very/long/path/that/should/be/shortened/for/the/dashboard", 24)).toBe(
|
||||
"/a/very/lon…e/dashboard",
|
||||
);
|
||||
});
|
||||
|
||||
it("exposes every approval decision action required by the API contract", () => {
|
||||
expect([...approvalDecisionActions]).toEqual(["approve", "request_changes", "reject", "abort"]);
|
||||
});
|
||||
});
|
||||
108
apps/web/src/view-model.ts
Normal file
108
apps/web/src/view-model.ts
Normal file
@@ -0,0 +1,108 @@
|
||||
export const screens = [
|
||||
"dashboard",
|
||||
"run-detail",
|
||||
"approvals",
|
||||
"sessions",
|
||||
"templates",
|
||||
"personas",
|
||||
"new-run",
|
||||
] as const;
|
||||
export type Screen = (typeof screens)[number];
|
||||
|
||||
export const approvalDecisionActions = ["approve", "request_changes", "reject", "abort"] as const;
|
||||
export type ApprovalDecisionAction = (typeof approvalDecisionActions)[number];
|
||||
|
||||
export interface RunSummary {
|
||||
id: string;
|
||||
state: string;
|
||||
repoPath: string;
|
||||
baseBranch: string;
|
||||
currentPhaseId: string | null;
|
||||
createdAt: string;
|
||||
startedAt: string | null;
|
||||
endedAt: string | null;
|
||||
}
|
||||
|
||||
export interface DoctorCheck {
|
||||
name: string;
|
||||
status: "pass" | "warn" | "fail";
|
||||
detail: string;
|
||||
remediation: string;
|
||||
}
|
||||
|
||||
export function screenFromHash(hash: string): Screen {
|
||||
const candidate = hash.replace(/^#\/?/, "");
|
||||
return isScreen(candidate) ? candidate : "dashboard";
|
||||
}
|
||||
|
||||
export function normalizeApiBase(value: string | undefined): string {
|
||||
if (value === undefined || value.trim().length === 0) {
|
||||
return "";
|
||||
}
|
||||
return value.replace(/\/+$/, "");
|
||||
}
|
||||
|
||||
export function stateTone(state: string): "active" | "blocked" | "done" | "failed" | "neutral" {
|
||||
if (state === "completed") {
|
||||
return "done";
|
||||
}
|
||||
if (state === "failed" || state === "aborted" || state === "FAILED_NEEDS_HUMAN") {
|
||||
return "failed";
|
||||
}
|
||||
if (
|
||||
state === "paused" ||
|
||||
state === "awaiting_approval" ||
|
||||
state === "WAITING_FOR_APPROVAL" ||
|
||||
state === "ARTIFACT_TIMEOUT" ||
|
||||
state === "HUNG" ||
|
||||
state === "CRASHED"
|
||||
) {
|
||||
return "blocked";
|
||||
}
|
||||
if (
|
||||
state === "executing" ||
|
||||
state === "planning" ||
|
||||
state === "bound" ||
|
||||
state === "READY" ||
|
||||
state === "BUSY" ||
|
||||
state === "BOOTSTRAPPING" ||
|
||||
state === "RESUMING" ||
|
||||
state === "REBOOTSTRAPPED"
|
||||
) {
|
||||
return "active";
|
||||
}
|
||||
return "neutral";
|
||||
}
|
||||
|
||||
export function compactPath(path: string, maxLength = 54): string {
|
||||
if (path.length <= maxLength) {
|
||||
return path;
|
||||
}
|
||||
const keep = Math.max(8, Math.floor((maxLength - 1) / 2));
|
||||
return `${path.slice(0, keep)}…${path.slice(-keep)}`;
|
||||
}
|
||||
|
||||
export function formatDateTime(value: string | null): string {
|
||||
if (value === null) {
|
||||
return "";
|
||||
}
|
||||
const date = new Date(value);
|
||||
if (Number.isNaN(date.getTime())) {
|
||||
return value;
|
||||
}
|
||||
return date.toLocaleString();
|
||||
}
|
||||
|
||||
export function highestDoctorStatus(checks: readonly DoctorCheck[]): DoctorCheck["status"] {
|
||||
if (checks.some((check) => check.status === "fail")) {
|
||||
return "fail";
|
||||
}
|
||||
if (checks.some((check) => check.status === "warn")) {
|
||||
return "warn";
|
||||
}
|
||||
return "pass";
|
||||
}
|
||||
|
||||
function isScreen(value: string): value is Screen {
|
||||
return screens.includes(value as Screen);
|
||||
}
|
||||
Reference in New Issue
Block a user