dsh-data-notebook
v0.2.0-alpha.9
Published
DataStudio-inspired browser notebook for DSH: WASM SQL, Markdown, charts, Agent tools, and conversation context in the official right Sidebar
Readme
dsh-data-notebook
Data Notebook is a DSH-side adapter for the browser-first notebook ideas in
DataStudio. It mounts as an
official DSH right Sidebar tab named “数据 Notebook”. The tab type is
registered through sidebarRightTabs, and its session-scoped body is mounted
through the keyed sidebar.right.pane.tab slot.
The delivered runtime is deliberately split into two halves:
- The Host persists a small, versioned notebook document at
$DSH_HOME/storages/data-notebook/*.json, registers the Agent tools, and queues execution requests. It never evaluates user SQL. - The browser loads DuckDB-WASM and keeps imported data in the browser runtime. SQL cells execute locally; Markdown, Chart and Input cells are presentation and interaction cells. Results are bounded before they cross back to the Host. CSV, JSON/JSONL, Parquet and Excel imports are supported.
The browser bundle serves two DuckDB assets through the Host routes
/data-notebook/duckdb-browser-mvp.worker.js and
/data-notebook/duckdb-mvp.wasm.
DSH ↔ Notebook contract
The current conversation session is the notebook's association key. The panel
uses the shared sessions service and sends a captured selection through the
scoped conversation.send() method. The same selection is saved as
NotebookDocument.context, so the Agent can read it later with
notebook_context(action="get").
The seven tools are intentionally aggregate-root operations:
notebook_catalog: list/create/rename/delete notebooks.notebook_read: read a notebook, one cell, or the saved selection context.notebook_edit: revision-guarded, idempotent cell and variable mutations.notebook_execute: enqueue browser-side SQL execution and optionally wait.notebook_context: get/set/clear the selection context.notebook_history: list user-visible Versions, read one Version's Steps and content snapshot, or diff two Versions (cell / source line diff / variable).notebook_restore: restore a Version as a new content write; history is kept and the restored state becomes a freshrestoredVersion.
Agent edits should pass the revision returned by notebook_read as
baseRevision. That value is the content revision; the transport cursor
(which derived writes also move) is returned separately as changeCursor, so
running SQL never invalidates a pending edit. A conflicting content write
returns REVISION_CONFLICT instead of silently overwriting a user's work.
notebook_edit also accepts the shared write options: versionSessionId joins
consecutive user edits into one Version, transactionId makes several Agent
calls commit as one atomic Version, and boundary: "close" finalizes the
current Version (the write itself still belongs to it).
Version model
Versions follow the shared contract in dsh-data-contracts and the same
semantics as Data Sheet:
- A Version is a user-meaningful content change (
V1,V2, …) with Steps, actor, summary,startRevision/endRevisionand a content hash. - Only content writes — cell source/type/order/metadata, variables and data source bindings — advance the content revision and form Versions.
- SQL execution lifecycle, outputs,
executionCountand the selection context are derived/metadata events. They moverevision(the transport cursor used by change polling) but never create a Version or change the content hash. - An
openuser Version joins consecutive edits inside oneversionSessionIdwindow and is finalized after 2s of inactivity, an explicitboundary: "close", or when an Agent writes. - Each committed Version stores an immutable content checkpoint deduplicated by content hash, so previewing history never copies outputs or images.
- SQL results are derived artifacts: they record the content revision their
inputs came from (
computedFromRevision) and are markedoutputStalewhen the cell source, the variables or the data bindings change. They are never deleted, and a restored Version marks every result that does not belong to the target revision as needing a re-run. - Conflicts return one shared 409 body (
ConflictPayload): base/current content revision, the conflicting versions, and anextAction. Data Sheet returns the same shape.
Sidecars live beside the notebook document:
$DSH_HOME/storages/data-notebook/<id>.json 当前投影
$DSH_HOME/storages/data-notebook/versions/<id>.json Version 元数据 + Step
$DSH_HOME/storages/data-notebook/checkpoints/<id>/<hash>.json 不可变内容快照The version drawer is built from the shared version-drawer components
(VersionManager / VersionDiff / RestoreDialog), so Data Sheet and Data
Notebook behave identically: same list, status badges (未归档 / 已保存 / 已恢复 / 已被替代),
Step expansion, diff rendering and restore confirmation. V12 is the primary
label and the technical revision (r48) is secondary. 保存版本 names the
current content as a version without requiring a content change; the comparison
baseline is selectable.
Browser routes: GET /data-notebook/notebook/:id/versions,
GET …/versions/:versionId, GET …/versions/:versionId/diff?against=:versionId,
POST …/versions/checkpoint, POST …/versions/:versionId/restore. The legacy
history array is still written and read for compatibility.
The browser panel follows the Jupyter interaction model while keeping DSH's
session and revision contract: Shift+Enter executes the current SQL block and
advances to the next block, while Ctrl/Cmd+Enter executes without leaving the
current block. SQL blocks can collapse their source and output. The block menu
keeps the full unique ID and other metadata out of the main toolbar.
Supported blocks are SQL, full DSH Markdown, Chart, Input, Sheet reference,
and Divider. Images are Markdown content rather than a separate block type, so
the toolbar inserts  into the selected Markdown block (or
creates one). The toolbar exports a self-contained HTML report (including
rendered Markdown, bounded table results and embedded images) or a Jupyter
.ipynb file. Sheet blocks reference a data-sheet workbook without copying
its data.
For large workbooks, the browser keeps SQL execution local and sends only a preview of up to 500 rows and 50 columns to the Host. Agent-facing notebook views redact embedded image data URLs larger than 2 KiB; the full Markdown source remains available to the browser panel and exports.
Standalone page (「新 Tab 打开」)
The top bar can open the current Notebook as a full browser document:
| Route | Purpose |
|---|---|
| /data-notebook/notebook/:id/view?session=<id> | Minimal HTML shell for the new tab |
| /data-notebook/notebook-page.js | Self-contained page bundle (lib/notebook-page.js) |
The page is session-scoped like the embedded panel, so the Notebook id travels
in the path and the owning session in the query string; the panel reuses its own
controller and merely hides the 「新 Tab 打开」 entry and the conversation bridge
once standalone is set. Because the page is a plain document with no DSH
module loader, CellRenderer renders Markdown through
markdown-renderer.tsx: the embedded entry installs the platform Markdown atom,
and the standalone bundle falls back to the local renderer described below
instead of inlining that package (and its shiki/katex graph).
The user-visible Sidebar title is 「Notebook」; the package name, HTTP routes and
tab kind stay data-notebook.
Block presentation
- SQL renders through
SqlEditor.tsx: a line-number gutter plus a syntax-highlight layer painted underneath a transparent textarea. highlight.js is imported aslib/corewith only the SQL grammar. The textarea keeps native caret, selection and IME behaviour; the layer only supplies colour. Both layers take their metrics from one source, and the font lives on the.dn-sqlcontainer rather than on the shared class —[data-dsh-data-notebook] textarea { font: inherit }is an attribute+element selector and would otherwise outrank any single class. - Sheet renders a bounded read-only preview (
SheetPreview.tsx) under the workbook/sheet/range selectors, read through the same read-only range endpoint the "载入为 SQL 数据源" action uses. It is capped in rows and columns so a Sheet Block never becomes a second spreadsheet application. - Input separates title,
{{variable}}label and control, and shows the numeric unit frommetadata.input.unit. - Markdown reads as a document on both surfaces: the embedded panel installs
the platform Markdown atom, and the standalone page uses
local-markdown.tsx, a small React-element renderer. Neither needs the other's dependency. - Every Block keeps its actions in a floating strip at the top-right
(
.dn-cell-head), not in a reserved header row: the mockups start content at the Block's top edge and overlap the actions on it. The strip is icon-only through the shared icon components, so each button carries both atitleand anaria-label. - Each Block is a white card (
.dn-cell-main) on the canvas, with the insertion rail as a hairline in the 14px gap between cards rather than a reserved band. The rail is a sibling of the card, not a child —.dn-cell-mainis a stacking context (relative; z-index: 0), so a nested rail would be painted under the next Block and clip the floating+.
Known gaps
Three items were deliberately left out rather than half-built; each needs either a browser this environment does not have, or a Host capability that does not exist yet:
- Block virtualization. Windowing — and the cheaper
content-visibility— changes the layout semantics the auto-sizing editors rely on forscrollHeight, so it needs visual verification. A 200-Block notebook is otherwise served by result paging and column folding. - Automatic image-blob reclamation.
pruneImageAssetsis implemented and tested, but not wired to run automatically: a blob may be released only when no retained version references it, and that question can only be answered by the Host. Reclaiming on "current document" alone would break images after a version restore. - Image blob durability. Assets live in a session-scoped store, so a reload requires re-importing the file — which the missing-resource banner detects and explains. An IndexedDB/OPFS store would be a later step.
