Files
ShellBook/apps/web/src/events.ts
T
Florian Schmidt b224fff7f3 feat(web): handle one-shot sensitive artifact downloads (#4)
Track sensitive download state per run in the session reducer. Fetch each artifact once into a blob so expired or consumed downloads show HTTP 410 without a separate preflight request. Verify the SSH-key notebook, capture substitution, failure sections and public-key upload provenance through App. Document demo and real terminal workflows.
2026-11-16 00:21:24 +01:00

143 lines
4.0 KiB
TypeScript

import type { ServerEvent, StartRunRequest } from "@shellbook/core";
export type DownloadState =
| "downloading"
| "downloaded"
| "unavailable"
| "error";
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[];
sensitiveArtifacts?: string[];
artifactDownloads?: Record<string, DownloadState>;
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 =
| {
type: "artifact.download";
runId: string;
name: string;
state: DownloadState;
}
| 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 "artifact.download":
updated = {
...run,
artifactDownloads: {
...run.artifactDownloads,
[event.name]: event.state,
},
};
break;
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": {
// An attempt aborted for host key confirmation also finishes (exit -1);
// keep the prompt open until the user accepts or rejects.
if (run.status === "hostKey") {
updated = {
...run,
exitCode: event.exitCode,
durationMs: event.durationMs,
};
break;
}
updated = {
...run,
status: event.exitCode === 0 ? "success" : "failure",
exitCode: event.exitCode,
durationMs: event.durationMs,
artifacts: event.artifacts,
sensitiveArtifacts: event.sensitiveArtifacts ?? [],
};
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 } };
}