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,80 @@
|
||||
import type {
|
||||
NotebookListResponse,
|
||||
HostsResponse,
|
||||
NotebookResponse,
|
||||
SaveNotebookResponse,
|
||||
StartRunRequest,
|
||||
StartRunResponse,
|
||||
ServerEvent,
|
||||
} from "@shellbook/core";
|
||||
|
||||
export class HttpError extends Error {
|
||||
constructor(public status: number) {
|
||||
super(`Request failed (${status})`);
|
||||
}
|
||||
}
|
||||
async function json<T>(
|
||||
path: string,
|
||||
method = "GET",
|
||||
body?: unknown,
|
||||
): Promise<T> {
|
||||
const response = await fetch(`/api${path}`, {
|
||||
method,
|
||||
credentials: "same-origin",
|
||||
headers: body === undefined ? {} : { "Content-Type": "application/json" },
|
||||
body: body === undefined ? undefined : JSON.stringify(body),
|
||||
});
|
||||
if (!response.ok) throw new HttpError(response.status);
|
||||
return response.status === 204
|
||||
? (undefined as T)
|
||||
: (response.json() as Promise<T>);
|
||||
}
|
||||
export interface Api {
|
||||
me(): Promise<unknown>;
|
||||
login(password: string): Promise<unknown>;
|
||||
list(): Promise<NotebookListResponse>;
|
||||
hosts(): Promise<HostsResponse>;
|
||||
notebook(path: string): Promise<NotebookResponse>;
|
||||
save(
|
||||
path: string,
|
||||
source: string,
|
||||
baseVersion: string,
|
||||
): Promise<SaveNotebookResponse>;
|
||||
run(request: StartRunRequest): Promise<StartRunResponse>;
|
||||
events(
|
||||
receive: (event: ServerEvent) => void,
|
||||
connection: (connected: boolean) => void,
|
||||
): () => void;
|
||||
artifact(runId: string, name: string): string;
|
||||
}
|
||||
export const liveApi: Api = {
|
||||
me: () => json("/auth/me"),
|
||||
login: (password) => json("/auth/login", "POST", { password }),
|
||||
list: () => json("/notebooks"),
|
||||
hosts: () => json("/hosts"),
|
||||
notebook: (path) => json(`/notebooks/${encodeURIComponent(path)}`),
|
||||
save: (path, source, baseVersion) =>
|
||||
json(`/notebooks/${encodeURIComponent(path)}`, "PUT", {
|
||||
source,
|
||||
baseVersion,
|
||||
}),
|
||||
run: (request) => json("/runs", "POST", request),
|
||||
artifact: (id, name) =>
|
||||
`/api/runs/${encodeURIComponent(id)}/artifacts/${encodeURIComponent(name)}`,
|
||||
events(receive, connection) {
|
||||
const ws = new WebSocket(
|
||||
`${location.protocol === "https:" ? "wss:" : "ws:"}//${location.host}/api/events`,
|
||||
);
|
||||
ws.onopen = () => connection(true);
|
||||
ws.onclose = () => connection(false);
|
||||
ws.onerror = () => connection(false);
|
||||
ws.onmessage = (message) => {
|
||||
try {
|
||||
receive(JSON.parse(String(message.data)) as ServerEvent);
|
||||
} catch {
|
||||
connection(false);
|
||||
}
|
||||
};
|
||||
return () => ws.close();
|
||||
},
|
||||
};
|
||||
Reference in New Issue
Block a user