feat: chained notebooks with when sections and host blocks (#11)
A notebook can include another one and continue with what it produced:
- core: parse :::when{param=a,b}, ::use{notebook= as= with=} and
::host{name= address= user= port= capture=}; composeNotebook namespaces
an included notebook's params, steps, captures and templates by alias
(cycles, missing files, depth > 4 and child errors are reported on the
use line). Defaults may reference captures and builtins
({{shellbook.public_key}}); required params and secrets gate only steps
of their own scope; secretEnv passes a scope's secrets under local names.
- protocol: NotebookResponse gains includes and builtins.
- server: compose includes on load, inject builtins (client values are
ignored), refuse steps in inactive when sections.
- web: render when/use/host blocks, group included params in the form,
add a created machine to Hosts and capture its name, reload when an
included notebook changes, run all checks only for active steps,
authoring snippets for when, use and host.
This commit is contained in:
+98
-24
@@ -13,10 +13,14 @@ import {
|
||||
} from "react";
|
||||
import ReactMarkdown from "react-markdown";
|
||||
import {
|
||||
blockActive,
|
||||
notebookIncludes,
|
||||
parseNotebook,
|
||||
renderHost,
|
||||
renderStep,
|
||||
renderText,
|
||||
resolveParams,
|
||||
scopeActive,
|
||||
} from "@shellbook/core";
|
||||
import type {
|
||||
Block,
|
||||
@@ -34,7 +38,8 @@ import { HostsPage } from "./HostsPage";
|
||||
import { HttpError } from "./api";
|
||||
import type { Api } from "./api";
|
||||
import { emptySession, eventReducer, stepKey } from "./events";
|
||||
import { ParamsForm } from "./ParamsForm";
|
||||
import { ParamsForm, type ParamGroup } from "./ParamsForm";
|
||||
import { HostBlockCard } from "./HostBlockCard";
|
||||
import { StepCard } from "./StepCard";
|
||||
import { ChatPanel } from "./ChatPanel";
|
||||
const TerminalSlot = lazy(() =>
|
||||
@@ -48,8 +53,18 @@ function steps(blocks: Block[]): Step[] {
|
||||
return blocks.flatMap((block) =>
|
||||
block.type === "step"
|
||||
? [block.step]
|
||||
: block.type === "after"
|
||||
? steps(block.blocks)
|
||||
: block.type === "markdown" || block.type === "host"
|
||||
? []
|
||||
: steps(block.blocks),
|
||||
);
|
||||
}
|
||||
/** Included notebooks (`::use`, nested too) as parameter groups. */
|
||||
function includedGroups(blocks: Block[]): ParamGroup[] {
|
||||
return blocks.flatMap((block): ParamGroup[] =>
|
||||
block.type === "use"
|
||||
? [{ scope: block.scope, title: `${block.title ?? block.path} (${block.scope})` }, ...includedGroups(block.blocks)]
|
||||
: block.type === "after" || block.type === "when"
|
||||
? includedGroups(block.blocks)
|
||||
: [],
|
||||
);
|
||||
}
|
||||
@@ -146,7 +161,10 @@ export function App({ api }: { api: Api }) {
|
||||
})
|
||||
.catch((e) => setError(String(e)));
|
||||
}
|
||||
if (event.type === "file.changed" && event.path === open.doc?.path) {
|
||||
// A changed included notebook changes the composed one; a draft keeps its own copy.
|
||||
if (event.type === "file.changed" && event.path !== open.doc?.path && open.doc?.includes.includes(event.path)) {
|
||||
if (!open.dirty) void load(open.doc.path);
|
||||
} else if (event.type === "file.changed" && event.path === open.doc?.path) {
|
||||
if (open.dirty) setConflict(true);
|
||||
else void load(event.path);
|
||||
} else if (
|
||||
@@ -173,20 +191,31 @@ export function App({ api }: { api: Api }) {
|
||||
return () => window.removeEventListener("beforeunload", guard);
|
||||
}, [dirty]);
|
||||
|
||||
// The saved notebook comes composed from the server; a draft shows its includes after saving.
|
||||
const notebook = doc
|
||||
? edit
|
||||
? edit && source !== doc.source
|
||||
? parseNotebook(source)
|
||||
: doc.notebook
|
||||
: undefined;
|
||||
const resolved = notebook
|
||||
? resolveParams(
|
||||
notebook,
|
||||
values,
|
||||
hosts.map((h) => h.name),
|
||||
)
|
||||
: { values: {}, errors: {} };
|
||||
const path = doc?.path ?? "";
|
||||
const runtime = session.runtime[path] ?? {};
|
||||
const hostNames = hosts.map((h) => h.name);
|
||||
const sessionRuntime = session.runtime[path] ?? {};
|
||||
// Builtins ({{shellbook.public_key}}) come from the server, which also sets them on runs.
|
||||
const runtime = { ...sessionRuntime, ...doc?.builtins };
|
||||
const resolved = notebook
|
||||
? resolveParams(notebook, values, hostNames, runtime)
|
||||
: { values: {}, errors: {} };
|
||||
const active = (id: string) => !!notebook && blockActive(notebook, id, resolved.values);
|
||||
const groups = notebook
|
||||
? includedGroups(notebook.blocks).filter((group) => scopeActive(notebook, group.scope, resolved.values))
|
||||
: [];
|
||||
const activeChecks = notebook
|
||||
? steps(notebook.blocks).filter((s) => s.kind === "check" && active(s.attrs.id))
|
||||
: [];
|
||||
const runnable = (step: Step) => {
|
||||
const rendered = renderStep(notebook!, step, resolved.values, runtime, hostNames);
|
||||
return !rendered.missing.length && !Object.keys(rendered.invalid).length;
|
||||
};
|
||||
const runFor = (id: string) =>
|
||||
session.runs[session.latest[stepKey(path, id)] ?? ""];
|
||||
async function start(request: StartRunRequest) {
|
||||
@@ -207,13 +236,13 @@ export function App({ api }: { api: Api }) {
|
||||
}
|
||||
}
|
||||
function requestRun(step: Step) {
|
||||
if (!notebook || !doc || dirty) return;
|
||||
if (!notebook || !doc || dirty || !active(step.attrs.id)) return;
|
||||
const rendered = renderStep(
|
||||
notebook,
|
||||
step,
|
||||
resolved.values,
|
||||
runtime,
|
||||
hosts.map((h) => h.name),
|
||||
hostNames,
|
||||
);
|
||||
if (
|
||||
rendered.missing.length ||
|
||||
@@ -225,7 +254,7 @@ export function App({ api }: { api: Api }) {
|
||||
notebookPath: path,
|
||||
stepId: step.attrs.id,
|
||||
params: { ...resolved.values },
|
||||
runtime: { ...runtime },
|
||||
runtime: { ...sessionRuntime },
|
||||
artifactSources: { ...session.artifactSources[path] },
|
||||
};
|
||||
const host = hosts.find((h) => h.name === rendered.host);
|
||||
@@ -251,6 +280,49 @@ export function App({ api }: { api: Api }) {
|
||||
</ReactMarkdown>
|
||||
</div>
|
||||
);
|
||||
if (block.type === "when")
|
||||
return block.values.includes(resolved.values[block.param] ?? "") ? (
|
||||
<div className="when" key={i}>
|
||||
{blocks(block.blocks)}
|
||||
</div>
|
||||
) : null;
|
||||
if (block.type === "use")
|
||||
return (
|
||||
<details className="included" open key={i}>
|
||||
<summary>
|
||||
{block.title ?? block.path}
|
||||
<small>
|
||||
{block.path} · as {block.scope}
|
||||
</small>
|
||||
</summary>
|
||||
{block.blocks.length ? (
|
||||
blocks(block.blocks)
|
||||
) : (
|
||||
<p className="muted">Save the notebook to load {block.path}.</p>
|
||||
)}
|
||||
</details>
|
||||
);
|
||||
if (block.type === "host") {
|
||||
const rendered = renderHost(notebook, block, resolved.values, runtime, hostNames);
|
||||
const capture = (name: string) => {
|
||||
if (block.capture) dispatch({ type: "runtime.set", notebookPath: path, name: block.capture, value: name });
|
||||
};
|
||||
return (
|
||||
<HostBlockCard
|
||||
key={block.id}
|
||||
rendered={rendered}
|
||||
existing={hosts.find((h) => h.name === rendered.name)}
|
||||
captured={block.capture ? runtime[block.capture] : undefined}
|
||||
onUse={capture}
|
||||
onAdd={async (host) => {
|
||||
await api.addHost(host);
|
||||
setHosts((await api.hosts()).hosts);
|
||||
window.dispatchEvent(new Event("shellbook:hosts-changed"));
|
||||
capture(host.name);
|
||||
}}
|
||||
/>
|
||||
);
|
||||
}
|
||||
if (block.type === "after") {
|
||||
const run = runFor(block.stepId);
|
||||
return run &&
|
||||
@@ -265,7 +337,7 @@ export function App({ api }: { api: Api }) {
|
||||
block.step,
|
||||
resolved.values,
|
||||
runtime,
|
||||
hosts.map((h) => h.name),
|
||||
hostNames,
|
||||
);
|
||||
return (
|
||||
<StepCard
|
||||
@@ -292,13 +364,16 @@ export function App({ api }: { api: Api }) {
|
||||
if (!doc) return;
|
||||
try {
|
||||
const saved = await api.save(path, source, doc.version);
|
||||
const parsed = parseNotebook(source);
|
||||
setDoc({
|
||||
...doc,
|
||||
source,
|
||||
notebook: parseNotebook(source),
|
||||
notebook: parsed,
|
||||
version: saved.version,
|
||||
});
|
||||
setConflict(false);
|
||||
// Includes are composed on the server: reload to show them.
|
||||
if (notebookIncludes(parsed).length || doc.includes.length) await load(path);
|
||||
} catch (e) {
|
||||
if (e instanceof HttpError && e.status === 409) setConflict(true);
|
||||
else setError(String(e));
|
||||
@@ -428,6 +503,7 @@ export function App({ api }: { api: Api }) {
|
||||
values={resolved.values}
|
||||
errors={resolved.errors}
|
||||
hosts={hosts}
|
||||
groups={groups}
|
||||
onChange={(name, value) =>
|
||||
setValues((previous) => ({ ...previous, [name]: value }))
|
||||
}
|
||||
@@ -472,7 +548,8 @@ export function App({ api }: { api: Api }) {
|
||||
disabled={
|
||||
!connected ||
|
||||
dirty ||
|
||||
Object.keys(resolved.errors).length > 0 ||
|
||||
activeChecks.length === 0 ||
|
||||
!activeChecks.every(runnable) ||
|
||||
steps(notebook.blocks).some(
|
||||
(s) =>
|
||||
runFor(s.attrs.id)?.status === "running" ||
|
||||
@@ -480,11 +557,8 @@ export function App({ api }: { api: Api }) {
|
||||
)
|
||||
}
|
||||
onClick={() => {
|
||||
const checks = steps(notebook.blocks).filter(
|
||||
(s) => s.kind === "check",
|
||||
);
|
||||
setCheckIds(checks.map((s) => s.attrs.id));
|
||||
checks.forEach(requestRun);
|
||||
setCheckIds(activeChecks.map((s) => s.attrs.id));
|
||||
activeChecks.forEach(requestRun);
|
||||
}}
|
||||
>
|
||||
Run all checks
|
||||
|
||||
Reference in New Issue
Block a user