feat(web): add shell-first runbook UI and mock demo (#2)
Add notebook parameters, literal highlighted scripts, copy helpers, streamed runs, host-key confirmation, artifacts, conditional notes, login and optimistic Markdown editing. Keep the temporary core renderer isolated to mock mode. Use React and Vite for the UI, react-markdown for safe Markdown rendering, highlight.js for shell syntax, and CodeMirror 6 for Markdown editing. Add Testing Library, jsdom and Vitest for component and event-state coverage. Validate pnpm install, root typecheck and test (29 web tests, 1 server test), plus normal and mock web builds. Core contracts remain unchanged; real backend execution awaits the parallel core/server implementations.
This commit is contained in:
@@ -0,0 +1,108 @@
|
||||
import type { ServerEvent, StartRunRequest } from "@shellbook/core";
|
||||
|
||||
export interface RunState {
|
||||
runId: string;
|
||||
notebookPath: string;
|
||||
stepId: string;
|
||||
status: "running" | "success" | "failure" | "hostKey";
|
||||
output: { stream: "stdout" | "stderr"; data: string }[];
|
||||
exitCode?: number;
|
||||
durationMs?: number;
|
||||
artifacts: string[];
|
||||
error?: string;
|
||||
hostKey?: Extract<ServerEvent, { type: "run.hostKeyUnknown" }>;
|
||||
}
|
||||
export interface Session {
|
||||
runs: Record<string, RunState>;
|
||||
latest: Record<string, string>;
|
||||
runtime: Record<string, Record<string, string>>;
|
||||
artifactSources: Record<string, Record<string, string>>;
|
||||
}
|
||||
export const emptySession: Session = {
|
||||
runs: {},
|
||||
latest: {},
|
||||
runtime: {},
|
||||
artifactSources: {},
|
||||
};
|
||||
export const stepKey = (path: string, id: string) => JSON.stringify([path, id]);
|
||||
export type Action =
|
||||
| ServerEvent
|
||||
| { type: "requested"; runId: string; request: StartRunRequest };
|
||||
export function eventReducer(state: Session, event: Action): Session {
|
||||
if (event.type === "file.changed" || event.type === "file.deleted")
|
||||
return state;
|
||||
if (event.type === "requested" || event.type === "run.started") {
|
||||
const info = event.type === "requested" ? event.request : event;
|
||||
const previous = state.runs[event.runId];
|
||||
return {
|
||||
...state,
|
||||
runs: {
|
||||
...state.runs,
|
||||
[event.runId]: previous ?? {
|
||||
runId: event.runId,
|
||||
notebookPath: info.notebookPath,
|
||||
stepId: info.stepId,
|
||||
status: "running",
|
||||
output: [],
|
||||
artifacts: [],
|
||||
},
|
||||
},
|
||||
latest: {
|
||||
...state.latest,
|
||||
[stepKey(info.notebookPath, info.stepId)]: event.runId,
|
||||
},
|
||||
};
|
||||
}
|
||||
const run = state.runs[event.runId];
|
||||
if (!run) return state;
|
||||
let updated: RunState = run;
|
||||
let next = state;
|
||||
switch (event.type) {
|
||||
case "run.output":
|
||||
updated = {
|
||||
...run,
|
||||
output: [...run.output, { stream: event.stream, data: event.data }],
|
||||
};
|
||||
break;
|
||||
case "run.error":
|
||||
updated = { ...run, status: "failure", error: event.message };
|
||||
break;
|
||||
case "run.hostKeyUnknown":
|
||||
updated = { ...run, status: "hostKey", hostKey: event };
|
||||
break;
|
||||
case "run.finished": {
|
||||
updated = {
|
||||
...run,
|
||||
status: event.exitCode === 0 ? "success" : "failure",
|
||||
exitCode: event.exitCode,
|
||||
durationMs: event.durationMs,
|
||||
artifacts: event.artifacts,
|
||||
};
|
||||
if (event.exitCode === 0) {
|
||||
next = {
|
||||
...state,
|
||||
runtime: {
|
||||
...state.runtime,
|
||||
[run.notebookPath]: {
|
||||
...state.runtime[run.notebookPath],
|
||||
...(event.captured
|
||||
? { [event.captured.name]: event.captured.value }
|
||||
: {}),
|
||||
},
|
||||
},
|
||||
artifactSources: {
|
||||
...state.artifactSources,
|
||||
[run.notebookPath]: {
|
||||
...state.artifactSources[run.notebookPath],
|
||||
...Object.fromEntries(
|
||||
event.artifacts.map((name) => [name, event.runId]),
|
||||
),
|
||||
},
|
||||
},
|
||||
};
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
return { ...next, runs: { ...state.runs, [event.runId]: updated } };
|
||||
}
|
||||
Reference in New Issue
Block a user