Compare commits
3
Commits
dd0af5d580
...
bcf392e95a
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
bcf392e95a | ||
|
|
57305a2dc6 | ||
|
|
6bfcc9d70d |
@@ -15,5 +15,6 @@
|
||||
- [2026-11-16] **Chained notebooks (#11):** `::use` is flattened in core (`composeNotebook`): alias-prefixed params/ids/captures with `scope`, so render/run keep one notebook model and one substitution path. Server loads files and injects builtins (`shellbook.*`, server-only); `:::when` gates display and execution; `::host` adds to Hosts via the host-management API. Required params/secrets gate only their own scope, identically in core render and server `prepareRun`.
|
||||
- [2026-11-16] **Template delivery:** `install.sh` offers each shipped notebook template once (`/etc/shellbook/templates-offered`), copied as the service user, never overwriting or restoring deleted files.
|
||||
- [2026-11-16] **Web UI shell (#12):** top bar (tabs, live state, Chat/Terminal), notebook sidebar only on Notebooks, parameter panel beside the notebook via a container query (above it when narrow), dialogs/menus from `ui.tsx`. Page and notebook live in the URL hash (`route.ts`); typed non-secret values are remembered per notebook in localStorage (`paramMemory.ts`, secrets and undeclared names are dropped on read and write).
|
||||
- [2026-11-16] **Collapsible parameters (#13):** the form and each included group collapse (remembered via `uiPrefs.ts`). Collapsed, only missing required values and values changed from their default are listed as chips; untouched defaults are just counted, so the bar stays one line.
|
||||
|
||||
## Archive
|
||||
|
||||
@@ -17,5 +17,7 @@
|
||||
- [2026-11-16] **CJS named imports in the bundle:** `import { utils } from "ssh2"` passes Vitest but crashes `node dist/main.js`; import CommonJS packages via default export. `test/bundle.test.ts` starts the real bundle.
|
||||
- [2026-11-16] **Capture defaults deadlock if required in the same scope:** a required param whose default waits for a capture of its own notebook blocks the step producing it. Make such params optional (e.g. `ctid: default "{{nextid}}"`).
|
||||
- [2026-11-16] **Bash tool heredocs with apostrophes fail** in this harness ("unexpected EOF"); write scripts to the scratchpad and run them instead.
|
||||
- [2026-11-16] **Commas in YAML flow-mapping labels:** `{ label: Key (optional, else x) }` splits at the comma into a valueless key; quote such values. The parser now says so at the parameter line.
|
||||
- [2026-11-16] **Stopping a background dev server task leaves its Node child running** on Windows (ports 8080/5173 stay taken); reuse it or kill the process.
|
||||
|
||||
## Archive
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
> Always-loaded. Everything else is on-demand. Update TOC when files change.
|
||||
|
||||
## Current Focus
|
||||
- [2026-11-16] M0–M3 (#1–#4), UI-first follow-ups (#7–#10) and chained notebooks (#11: when/use/host, LXC + web-server templates) and the UI/UX overhaul (#12) closed.
|
||||
- [2026-11-16] M0–M3 (#1–#4), UI-first follow-ups (#7–#10) and chained notebooks (#11: when/use/host, LXC + web-server templates) the UI/UX overhaul (#12) and collapsible parameters (#13) closed.
|
||||
- [2026-11-16] Open: #5/#6 need a real CT 501 deploy (bootstrap.sh one-liner, setup token, real SSH run, tmux terminal). Not yet verified in a real browser or against real OpenSSH.
|
||||
|
||||
## Invariants
|
||||
@@ -15,7 +15,7 @@
|
||||
- Everything doable from the web UI; files are storage, not the only way in.
|
||||
|
||||
## Table of Contents
|
||||
- `architecture.md` — 14 entries — why custom app, stack, AI integration, workspace split, deploy/update, chained notebooks, template delivery, web UI shell
|
||||
- `architecture.md` — 15 entries — why custom app, stack, AI integration, workspace split, deploy/update, chained notebooks, template delivery, web UI shell, collapsible params
|
||||
- `attempts.md` — 4 entries — marimo + marimo-pair trial and why it was dropped
|
||||
- `open.md` — 2 entries — open design questions (sudo password, chat history)
|
||||
- `gotchas.md` — 15 entries — integration foot-guns (CRLF, host-key events, tmux/systemd, env names, pct exec PATH, TUN, chat paths, watcher timing, capture-default deadlock, heredocs)
|
||||
- `gotchas.md` — 17 entries — integration foot-guns (CRLF, host-key events, tmux/systemd, env names, pct exec PATH, TUN, chat paths, watcher timing, capture-default deadlock, heredocs, YAML flow commas, orphaned dev servers)
|
||||
|
||||
@@ -45,6 +45,7 @@ import { ChatPanel } from "./ChatPanel";
|
||||
import { parseRoute, routeHash } from "./route";
|
||||
import { recallValues, rememberValues } from "./paramMemory";
|
||||
import { Dialog } from "./ui";
|
||||
import { readFlag, writeFlag } from "./uiPrefs";
|
||||
const TerminalSlot = lazy(() =>
|
||||
import("./TerminalSlot").then((module) => ({ default: module.TerminalSlot })),
|
||||
);
|
||||
@@ -88,6 +89,7 @@ export function App({ api }: { api: Api }) {
|
||||
const [edit, setEdit] = useState(false);
|
||||
const [terminal, setTerminal] = useState(false);
|
||||
const [chat, setChat] = useState(false);
|
||||
const [paramsOpen, setParamsOpen] = useState(() => readFlag("params-open", true));
|
||||
const [terminalOpened, setTerminalOpened] = useState(false);
|
||||
const [conflict, setConflict] = useState(false);
|
||||
const [error, setError] = useState("");
|
||||
@@ -583,13 +585,18 @@ export function App({ api }: { api: Api }) {
|
||||
</div>
|
||||
)}
|
||||
{notebook && (
|
||||
<div className="notebook-layout">
|
||||
<div className={`notebook-layout${paramsOpen ? "" : " params-collapsed"}`}>
|
||||
<ParamsForm
|
||||
notebook={notebook}
|
||||
values={resolved.values}
|
||||
errors={resolved.errors}
|
||||
hosts={hosts}
|
||||
groups={groups}
|
||||
open={paramsOpen}
|
||||
onToggle={(next) => {
|
||||
writeFlag("params-open", next);
|
||||
setParamsOpen(next);
|
||||
}}
|
||||
onChange={(name, value) => changeValues({ ...values, [name]: value })}
|
||||
onReset={() => changeValues({})}
|
||||
/>
|
||||
|
||||
+122
-31
@@ -1,5 +1,6 @@
|
||||
import { useState } from "react";
|
||||
import { useEffect, useState } from "react";
|
||||
import type { HostInfo, Notebook, ParamDef, ParamValues } from "@shellbook/core";
|
||||
import { readFlag, writeFlag } from "./uiPrefs";
|
||||
|
||||
/** An included notebook (`::use`) whose parameters the form shows as a group. */
|
||||
export interface ParamGroup {
|
||||
@@ -7,12 +8,16 @@ export interface ParamGroup {
|
||||
title: string;
|
||||
}
|
||||
|
||||
const groupFlag = (title: string) => `params-group:${title}`;
|
||||
|
||||
export function ParamsForm({
|
||||
notebook,
|
||||
values,
|
||||
errors,
|
||||
hosts,
|
||||
groups = [],
|
||||
open = true,
|
||||
onToggle,
|
||||
onChange,
|
||||
onReset,
|
||||
}: {
|
||||
@@ -22,14 +27,88 @@ export function ParamsForm({
|
||||
hosts: HostInfo[];
|
||||
/** Active included notebooks; parameters of inactive ones are hidden. */
|
||||
groups?: ParamGroup[];
|
||||
/** Collapsed, the form shows a one-line summary of the values instead of the fields. */
|
||||
open?: boolean;
|
||||
onToggle?(open: boolean): void;
|
||||
onChange(name: string, value: string): void;
|
||||
/** Clears the values typed for this notebook. */
|
||||
onReset?(): void;
|
||||
}) {
|
||||
const [open, setOpen] = useState(true);
|
||||
// Group collapse choices are remembered per included notebook title.
|
||||
const [groupOpen, setGroupOpen] = useState<Record<string, boolean>>({});
|
||||
const isGroupOpen = (title: string) => groupOpen[title] ?? readFlag(groupFlag(title), true);
|
||||
const toggleGroup = (title: string, next: boolean) => {
|
||||
writeFlag(groupFlag(title), next);
|
||||
setGroupOpen((current) => ({ ...current, [title]: next }));
|
||||
};
|
||||
// A summary chip opens its field and moves focus there once it is rendered.
|
||||
const [focus, setFocus] = useState<string>();
|
||||
useEffect(() => {
|
||||
if (!focus) return;
|
||||
document.getElementById(`param-${focus}`)?.focus();
|
||||
setFocus(undefined);
|
||||
}, [focus]);
|
||||
|
||||
const top = notebook.params.filter((param) => !param.scope);
|
||||
const grouped = groups
|
||||
.map((group) => ({ group, params: notebook.params.filter((param) => param.scope === group.scope) }))
|
||||
.filter((entry) => entry.params.length);
|
||||
const shown = [...top, ...grouped.flatMap((entry) => entry.params)];
|
||||
if (!shown.length) return null;
|
||||
const missingOf = (params: ParamDef[]) =>
|
||||
params.filter((param) => param.required && !values[param.name]).length;
|
||||
const missing = missingOf(shown);
|
||||
const labelOf = (param: ParamDef) =>
|
||||
param.label ?? (param.scope ? param.name.slice(param.scope.length + 1) : param.name);
|
||||
|
||||
const reveal = (param: ParamDef) => {
|
||||
if (!open) onToggle?.(true);
|
||||
const group = grouped.find((entry) => entry.group.scope === param.scope);
|
||||
if (group && !isGroupOpen(group.group.title)) toggleGroup(group.group.title, true);
|
||||
setFocus(param.name);
|
||||
};
|
||||
/**
|
||||
* What needs attention: missing required values and values changed from their default,
|
||||
* each a button that opens its field. Untouched defaults are only counted.
|
||||
*/
|
||||
const summary = (params: ParamDef[], expand: () => void) => {
|
||||
const listed = params.filter((param) =>
|
||||
values[param.name] ? values[param.name] !== param.default : param.required,
|
||||
);
|
||||
const defaults = params.filter((param) => values[param.name] && values[param.name] === param.default).length;
|
||||
if (!listed.length && !defaults) return null;
|
||||
return (
|
||||
<span className="param-summary">
|
||||
{listed.map((param) => {
|
||||
const value = values[param.name];
|
||||
const label = labelOf(param);
|
||||
return (
|
||||
<button
|
||||
key={param.name}
|
||||
type="button"
|
||||
className={`param-chip${value ? "" : " pending"}${errors[param.name] && value ? " invalid" : ""}`}
|
||||
title={`${label}: ${errors[param.name] ?? "edit"}`}
|
||||
onClick={() => reveal(param)}
|
||||
>
|
||||
<span className="param-chip-label">{label}</span>
|
||||
<span className="param-chip-value">
|
||||
{!value ? "missing" : param.type === "secret" ? "••••" : value}
|
||||
</span>
|
||||
</button>
|
||||
);
|
||||
})}
|
||||
{defaults > 0 && (
|
||||
<button type="button" className="link param-defaults" onClick={expand}>
|
||||
{listed.length ? "+ " : ""}
|
||||
{defaults} at default
|
||||
</button>
|
||||
)}
|
||||
</span>
|
||||
);
|
||||
};
|
||||
|
||||
const field = (param: ParamDef) => {
|
||||
const id = `param-${param.name}`;
|
||||
const local = param.scope ? param.name.slice(param.scope.length + 1) : param.name;
|
||||
const common = {
|
||||
id,
|
||||
required: param.required,
|
||||
@@ -45,8 +124,8 @@ export function ParamsForm({
|
||||
};
|
||||
return (
|
||||
<label key={param.name} htmlFor={id}>
|
||||
<span id={`${id}-label`}>
|
||||
{param.label ?? local}
|
||||
<span id={`${id}-label`} title={labelOf(param)}>
|
||||
{labelOf(param)}
|
||||
{param.required && <b title="Required"> *</b>}
|
||||
</span>
|
||||
{param.type === "host" || param.type === "choice" ? (
|
||||
@@ -76,47 +155,59 @@ export function ParamsForm({
|
||||
</label>
|
||||
);
|
||||
};
|
||||
const shown = [
|
||||
...notebook.params.filter((param) => !param.scope),
|
||||
...groups.flatMap((group) => notebook.params.filter((param) => param.scope === group.scope)),
|
||||
];
|
||||
const missing = shown.filter((param) => param.required && !values[param.name]).length;
|
||||
if (!shown.length) return null;
|
||||
|
||||
return (
|
||||
<form
|
||||
className="params"
|
||||
className={`params${open ? "" : " collapsed"}`}
|
||||
aria-label="Notebook parameters"
|
||||
onSubmit={(e) => e.preventDefault()}
|
||||
>
|
||||
<header>
|
||||
<h2>Parameters</h2>
|
||||
<h2>
|
||||
<button
|
||||
type="button"
|
||||
className="params-toggle"
|
||||
aria-expanded={open}
|
||||
title={open ? "Collapse parameters" : "Expand parameters"}
|
||||
onClick={() => onToggle?.(!open)}
|
||||
>
|
||||
<span className="caret" aria-hidden="true">{open ? "▾" : "▸"}</span>
|
||||
Parameters
|
||||
</button>
|
||||
</h2>
|
||||
<small className={missing ? "pending" : "ok"}>
|
||||
{missing ? `${missing} required missing` : "All required set"}
|
||||
</small>
|
||||
{!open && summary(shown, () => onToggle?.(true))}
|
||||
{onReset && open && (
|
||||
<button type="button" className="link" onClick={onReset}>
|
||||
Reset
|
||||
</button>
|
||||
)}
|
||||
<button
|
||||
type="button"
|
||||
className="link"
|
||||
aria-expanded={open}
|
||||
onClick={() => setOpen(!open)}
|
||||
>
|
||||
{open ? "Hide" : "Show"}
|
||||
</button>
|
||||
</header>
|
||||
{open && notebook.params.filter((param) => !param.scope).map(field)}
|
||||
{open && groups.map((group) => {
|
||||
const params = notebook.params.filter((param) => param.scope === group.scope);
|
||||
return params.length ? (
|
||||
<fieldset key={group.scope}>
|
||||
<legend>{group.title}</legend>
|
||||
{params.map(field)}
|
||||
</fieldset>
|
||||
) : null;
|
||||
})}
|
||||
{open && top.map(field)}
|
||||
{open &&
|
||||
grouped.map(({ group, params }) => {
|
||||
const expanded = isGroupOpen(group.title);
|
||||
const groupMissing = missingOf(params);
|
||||
return (
|
||||
<fieldset key={group.scope} className={expanded ? "" : "collapsed"} aria-label={group.title}>
|
||||
<legend>
|
||||
<button
|
||||
type="button"
|
||||
className="params-toggle"
|
||||
aria-expanded={expanded}
|
||||
onClick={() => toggleGroup(group.title, !expanded)}
|
||||
>
|
||||
<span className="caret" aria-hidden="true">{expanded ? "▾" : "▸"}</span>
|
||||
{group.title}
|
||||
{groupMissing > 0 && <small className="pending"> · {groupMissing} missing</small>}
|
||||
</button>
|
||||
</legend>
|
||||
{expanded ? params.map(field) : summary(params, () => toggleGroup(group.title, true))}
|
||||
</fieldset>
|
||||
);
|
||||
})}
|
||||
</form>
|
||||
);
|
||||
}
|
||||
|
||||
+97
-9
@@ -492,8 +492,31 @@ nav[aria-label="Main navigation"] button[aria-current="page"] {
|
||||
}
|
||||
.params > header {
|
||||
display: flex;
|
||||
align-items: baseline;
|
||||
gap: 10px;
|
||||
align-items: center;
|
||||
flex-wrap: wrap;
|
||||
gap: 6px 10px;
|
||||
}
|
||||
.params > header > .link {
|
||||
margin-left: auto;
|
||||
}
|
||||
.params-toggle {
|
||||
display: inline-flex;
|
||||
align-items: center;
|
||||
gap: 6px;
|
||||
border: none;
|
||||
background: none;
|
||||
padding: 2px 0;
|
||||
color: var(--text);
|
||||
font: inherit;
|
||||
letter-spacing: inherit;
|
||||
text-transform: inherit;
|
||||
}
|
||||
.params-toggle:hover {
|
||||
color: var(--accent);
|
||||
}
|
||||
.params-toggle .caret {
|
||||
width: 10px;
|
||||
color: var(--muted);
|
||||
}
|
||||
.params > header h2 {
|
||||
font-size: 13px;
|
||||
@@ -501,13 +524,69 @@ nav[aria-label="Main navigation"] button[aria-current="page"] {
|
||||
letter-spacing: 0.6px;
|
||||
margin: 0;
|
||||
}
|
||||
.params > header small {
|
||||
flex: 1;
|
||||
.params > header > small {
|
||||
color: var(--muted);
|
||||
}
|
||||
.params > header small.pending {
|
||||
.params small.pending {
|
||||
color: var(--warn);
|
||||
}
|
||||
.params.collapsed {
|
||||
padding-top: 8px;
|
||||
padding-bottom: 8px;
|
||||
}
|
||||
/* Collapsed: a one-line summary of the values, each opening its field. */
|
||||
.param-summary {
|
||||
display: flex;
|
||||
flex-wrap: wrap;
|
||||
gap: 6px;
|
||||
min-width: 0;
|
||||
}
|
||||
.params fieldset > .param-summary {
|
||||
grid-column: 1 / -1;
|
||||
}
|
||||
.param-chip {
|
||||
display: inline-flex;
|
||||
align-items: baseline;
|
||||
gap: 6px;
|
||||
max-width: 100%;
|
||||
padding: 2px 9px;
|
||||
border: 1px solid var(--line);
|
||||
border-radius: 999px;
|
||||
background: var(--code);
|
||||
font-size: 12px;
|
||||
}
|
||||
.param-chip:hover {
|
||||
border-color: var(--accent);
|
||||
}
|
||||
.param-chip-label {
|
||||
color: var(--muted);
|
||||
max-width: 16ch;
|
||||
overflow: hidden;
|
||||
text-overflow: ellipsis;
|
||||
white-space: nowrap;
|
||||
}
|
||||
.param-chip-value {
|
||||
font-family: "Cascadia Code", Consolas, ui-monospace, monospace;
|
||||
max-width: 22ch;
|
||||
overflow: hidden;
|
||||
text-overflow: ellipsis;
|
||||
white-space: nowrap;
|
||||
}
|
||||
.param-chip.pending {
|
||||
border-color: var(--warn);
|
||||
}
|
||||
.param-chip.pending .param-chip-value {
|
||||
color: var(--warn);
|
||||
font-family: inherit;
|
||||
}
|
||||
.param-chip.invalid {
|
||||
border-color: var(--danger);
|
||||
}
|
||||
.param-defaults {
|
||||
align-self: center;
|
||||
font-size: 12px;
|
||||
color: var(--muted);
|
||||
}
|
||||
.params label {
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
@@ -517,6 +596,9 @@ nav[aria-label="Main navigation"] button[aria-current="page"] {
|
||||
}
|
||||
.params label > span {
|
||||
color: var(--muted);
|
||||
overflow: hidden;
|
||||
text-overflow: ellipsis;
|
||||
white-space: nowrap;
|
||||
}
|
||||
.params b {
|
||||
color: var(--accent);
|
||||
@@ -530,22 +612,28 @@ nav[aria-label="Main navigation"] button[aria-current="page"] {
|
||||
border-radius: 6px;
|
||||
padding: 6px 12px 12px;
|
||||
}
|
||||
.params fieldset.collapsed {
|
||||
padding-bottom: 8px;
|
||||
}
|
||||
.params legend {
|
||||
font-size: 12px;
|
||||
font-weight: 600;
|
||||
padding: 0 4px;
|
||||
}
|
||||
.params legend small {
|
||||
font-weight: 400;
|
||||
}
|
||||
@container (min-width: 1000px) {
|
||||
.notebook-layout {
|
||||
.notebook-layout:not(.params-collapsed) {
|
||||
display: grid;
|
||||
grid-template-columns: minmax(0, 1fr) 300px;
|
||||
align-items: start;
|
||||
}
|
||||
.notebook-layout > .notebook {
|
||||
.notebook-layout:not(.params-collapsed) > .notebook {
|
||||
grid-column: 1;
|
||||
grid-row: 1;
|
||||
}
|
||||
.notebook-layout > .params {
|
||||
.notebook-layout:not(.params-collapsed) > .params {
|
||||
grid-column: 2;
|
||||
grid-row: 1;
|
||||
position: sticky;
|
||||
@@ -557,7 +645,7 @@ nav[aria-label="Main navigation"] button[aria-current="page"] {
|
||||
border-left: 1px solid var(--line);
|
||||
min-height: calc(100vh - var(--topbar));
|
||||
}
|
||||
.notebook-layout > .params fieldset {
|
||||
.notebook-layout:not(.params-collapsed) > .params fieldset {
|
||||
grid-template-columns: minmax(0, 1fr);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -96,6 +96,25 @@ it("adds the created container to Hosts and feeds it into the parent's steps", a
|
||||
expect(screen.getByRole("button", { name: "Run all checks" })).toBeEnabled();
|
||||
});
|
||||
|
||||
it("collapses an included group to a remembered summary whose chips open the field", async () => {
|
||||
const { api } = harness();
|
||||
const view = render(<App api={api} />);
|
||||
fireEvent.change(await screen.findByLabelText("target"), { target: { value: "lxc" } });
|
||||
fireEvent.click(screen.getByRole("button", { name: "LXC container (ct) · 1 missing", expanded: true }));
|
||||
const group = screen.getByRole("group", { name: "LXC container (ct)" });
|
||||
expect(within(group).queryByLabelText("proxmox *")).not.toBeInTheDocument();
|
||||
// Untouched defaults are only counted; what needs attention is listed.
|
||||
expect(within(group).queryByRole("button", { name: /hostname/ })).not.toBeInTheDocument();
|
||||
expect(within(group).getByRole("button", { name: "+ 1 at default" })).toBeInTheDocument();
|
||||
view.unmount();
|
||||
render(<App api={harness().api} />);
|
||||
fireEvent.change(await screen.findByLabelText("target"), { target: { value: "lxc" } });
|
||||
fireEvent.click(screen.getByRole("button", { name: /proxmox.*missing/ }));
|
||||
await waitFor(() => expect(screen.getByLabelText("proxmox *")).toHaveFocus());
|
||||
// The rest of the form was never collapsed.
|
||||
expect(screen.getByLabelText("target")).toBeInTheDocument();
|
||||
});
|
||||
|
||||
it("reloads when an included notebook changes", async () => {
|
||||
const { api, emit } = harness();
|
||||
render(<App api={api} />);
|
||||
|
||||
@@ -102,6 +102,21 @@ it("opens the notebook named in the URL, keeps typed values across reloads and s
|
||||
await waitFor(() => expect(location.hash).toBe("#/hosts"));
|
||||
});
|
||||
|
||||
it("collapses the parameter form to a remembered summary whose chips open their field", async () => {
|
||||
const view = render(<App api={api()} />);
|
||||
fireEvent.change(await screen.findByLabelText("Target server *"), { target: { value: "staging" } });
|
||||
fireEvent.click(screen.getByRole("button", { name: "Parameters", expanded: true }));
|
||||
expect(screen.queryByLabelText("Target server *")).not.toBeInTheDocument();
|
||||
expect(screen.getByRole("button", { name: /Target server.*staging/ })).toBeInTheDocument();
|
||||
view.unmount();
|
||||
render(<App api={api()} />);
|
||||
const chip = await screen.findByRole("button", { name: /Target server.*staging/ });
|
||||
expect(screen.getByRole("button", { name: "Parameters", expanded: false })).toBeInTheDocument();
|
||||
fireEvent.click(chip);
|
||||
await waitFor(() => expect(screen.getByLabelText("Target server *")).toHaveFocus());
|
||||
expect(screen.getByRole("button", { name: "Parameters", expanded: true })).toBeInTheDocument();
|
||||
});
|
||||
|
||||
it("saves the draft with Ctrl+S", async () => {
|
||||
const live = api();
|
||||
render(<App api={live} />);
|
||||
|
||||
@@ -0,0 +1,22 @@
|
||||
/**
|
||||
* Per-browser layout preferences (collapsed panels and the like). Never holds data that
|
||||
* must persist reliably; storage may be missing or blocked, which falls back to defaults.
|
||||
*/
|
||||
const key = (name: string) => `shellbook:ui:${name}`;
|
||||
|
||||
export function readFlag(name: string, fallback: boolean): boolean {
|
||||
try {
|
||||
const stored = localStorage.getItem(key(name));
|
||||
return stored === null ? fallback : stored === "1";
|
||||
} catch {
|
||||
return fallback;
|
||||
}
|
||||
}
|
||||
|
||||
export function writeFlag(name: string, value: boolean): void {
|
||||
try {
|
||||
localStorage.setItem(key(name), value ? "1" : "0");
|
||||
} catch {
|
||||
// Unavailable storage only means the choice lasts for this page.
|
||||
}
|
||||
}
|
||||
+24
-10
@@ -46,6 +46,16 @@ function attributes(text: string, error: (message: string) => void): Map<string,
|
||||
return result;
|
||||
}
|
||||
|
||||
/** 1-based source line of a parameter's key inside the frontmatter, or the first frontmatter line. */
|
||||
function paramLine(lines: string[], end: number, name: string): number {
|
||||
const index = lines.findIndex((line, i) => {
|
||||
if (i === 0 || i >= end || !/^\s/.test(line)) return false;
|
||||
const rest = line.trimStart();
|
||||
return rest.startsWith(name) && /^\s*:/.test(rest.slice(name.length));
|
||||
});
|
||||
return index > 0 ? index + 1 : 2;
|
||||
}
|
||||
|
||||
export function parseNotebook(source: string): Notebook {
|
||||
const notebook: Notebook = { title: "Untitled", params: [], blocks: [], diagnostics: [] };
|
||||
const rawLines = source.split("\n");
|
||||
@@ -70,22 +80,26 @@ export function parseNotebook(source: string): Notebook {
|
||||
else if (data.title !== undefined) error("title must be a non-empty string", 2);
|
||||
if (data.params !== undefined && !record(data.params)) error("params must be a mapping", 2);
|
||||
if (record(data.params)) for (const [name, raw] of Object.entries(data.params)) {
|
||||
if (!/^[A-Za-z_][A-Za-z0-9_]*$/.test(name)) error(`Invalid parameter name: ${name}`, 2);
|
||||
if (!record(raw)) { error(`Parameter ${name} must be a mapping`, 2); continue; }
|
||||
if (!paramTypes.has(String(raw.type))) { error(`Unknown param type for ${name}`, 2); continue; }
|
||||
// Point at the parameter itself, not just the frontmatter start.
|
||||
const at = paramLine(lines, end, name);
|
||||
if (!/^[A-Za-z_][A-Za-z0-9_]*$/.test(name)) error(`Invalid parameter name: ${name}`, at);
|
||||
if (!record(raw)) { error(`Parameter ${name} must be a mapping`, at); continue; }
|
||||
if (!paramTypes.has(String(raw.type))) { error(`Unknown param type for ${name}`, at); continue; }
|
||||
const def: ParamDef = { name, type: raw.type as ParamDef["type"], required: raw.required === true };
|
||||
for (const key of Object.keys(raw)) if (!["type", "required", "default", "label", "options"].includes(key)) error(`Unknown parameter field ${key} for ${name}`, 2);
|
||||
if (raw.required !== undefined && typeof raw.required !== "boolean") error(`required must be boolean for ${name}`, 2);
|
||||
for (const key of Object.keys(raw)) if (!["type", "required", "default", "label", "options"].includes(key))
|
||||
// `{ label: a (b, c) }` splits at the comma and turns "c)" into a key without a value.
|
||||
error(`Unknown parameter field "${key}" for ${name}` + (raw[key] === null ? '. A value containing a comma needs quotes, e.g. label: "a, b"' : ""), at);
|
||||
if (raw.required !== undefined && typeof raw.required !== "boolean") error(`required must be boolean for ${name}`, at);
|
||||
if (raw.default !== undefined) {
|
||||
if (def.type === "secret") error(`Secret ${name} cannot have a default`, 2);
|
||||
if (def.type === "secret") error(`Secret ${name} cannot have a default`, at);
|
||||
else if (typeof raw.default === "string" || typeof raw.default === "number" || typeof raw.default === "boolean") def.default = String(raw.default);
|
||||
else error(`Invalid default for ${name}`, 2);
|
||||
else error(`Invalid default for ${name}`, at);
|
||||
}
|
||||
if (typeof raw.label === "string") def.label = raw.label;
|
||||
else if (raw.label !== undefined) error(`Invalid label for ${name}`, 2);
|
||||
else if (raw.label !== undefined) error(`Invalid label for ${name}`, at);
|
||||
if (Array.isArray(raw.options) && raw.options.every(x => typeof x === "string")) def.options = raw.options;
|
||||
else if (raw.options !== undefined) error(`Invalid options for ${name}`, 2);
|
||||
if (def.type === "choice" && !def.options?.length) error(`Choice ${name} needs options`, 2);
|
||||
else if (raw.options !== undefined) error(`Invalid options for ${name}`, at);
|
||||
if (def.type === "choice" && !def.options?.length) error(`Choice ${name} needs options`, at);
|
||||
notebook.params.push(def);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -80,6 +80,14 @@ describe("parser", () => {
|
||||
])("reports malformed input without throwing: %s", (source, message) => {
|
||||
expect(parseNotebook(source).diagnostics.some(d => d.message.includes(message))).toBe(true);
|
||||
});
|
||||
it("points an unquoted comma in a flow-mapping label at the parameter line with a fix", () => {
|
||||
const n = parseNotebook("---\ntitle: T\nparams:\n host: { type: host }\n key: { type: secret, label: Auth key (optional, else log in) }\n---\n# T\n");
|
||||
expect(n.diagnostics).toEqual([{
|
||||
severity: "error",
|
||||
line: 5,
|
||||
message: 'Unknown parameter field "else log in)" for key. A value containing a comma needs quotes, e.g. label: "a, b"',
|
||||
}]);
|
||||
});
|
||||
it("uses title fallback, numeric step ids, CRLF ranges and exact script whitespace", () => {
|
||||
expect(parseNotebook("").title).toBe("Untitled");
|
||||
const n = parseNotebook("---\r\ntitle: Front\r\n---\r\n# Heading\r\n```sh {run}\r\n echo x \r\n\r\n```\r\n");
|
||||
|
||||
Reference in New Issue
Block a user