Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 62 additions & 0 deletions apps/@moku-protocol-explainer/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
# Protocol explainer

An interactive introduction to the host protocol’s object model and event streams.

```sh
bun run --cwd apps/@moku-protocol-explainer dev
```

Open `http://127.0.0.1:5180` to read the overview and object model. As the article
scrolls, the illustration introduces a cell’s source, identity, and configuration,
then its relationship to the document, notebook, host, and storage. These reveals
explain the model; they are not protocol events. Scrolling backward revisits earlier
concepts. The connection example below follows an actual sequence of simulated
requests and events, with playback controlled independently of scrolling.

Press Connect in that example. A request travels to the host, then the
streaming response opens a persistent connection. Parsing the source creates the
complete host notebook, including its document and cell records, in one step. The cells
contain `x = 10` and `print(x)`, providing a dependency for a later execution lesson.
This lesson does not run them. Playback pauses before sending anything to the client.

The notebook visually contains its document and cells, but each remains an independent
protocol record with its own event and inspector.

A compact toolbar keeps the chapter, playback, speed, and scrubbing controls available
above a diagram sized to its contents in a centered workspace. The lower detail panel
follows the diagram immediately, without a viewport-height spacer. Larger notebooks
scroll within the diagram.

The grid gives the client and host memory the main space. A small storage reference
represents the saved source; clicking it opens the complete file. The file is one
persistence format, while the host owns the live objects and decides what its storage
implementation persists when notebook.save is requested.

Continue to watch transient events carry copies across that same connection. The host
keeps its records and the client stores a record only when its event arrives. The ready
marker completes the snapshot without closing the stream. Events travel only along the
horizontal stream. Matching colors and record IDs connect each envelope to its
highlighted source and arriving replica. Drag the timeline to hold a handoff in flight,
advance one step, or click an object, file, or moving envelope to inspect it in the
detail panel below the scene. Cell config rows show a compact preview and expand in
place without opening that panel. Choose 0.25×, 0.5×, 1×, or 2× beside the timeline to
adjust speed without restarting playback.

This is a deterministic illustration of an unloaded file-backed host, not a live trace
of marimo OSS. The timing of loading and response headers is an implementation choice.
The filtered stream follows notebook, cell, and document records from the pinned
protocol. This notebook has no runtime or retained outputs.

React renders the scene from the timeline position. Effect owns playback and
subscriptions within a scope, interrupting pending animation frames when paused,
seeking, or unmounting. The app is isolated from the launcher and Moku core, with local
styles following Moku’s warm palette.

Run `bun setup` from the repository root on a fresh checkout to prepare the vendored
client and install workspace dependencies. The explainer is included in the workspace
lockfile and can be developed independently of the launcher.

```sh
bun run --cwd apps/@moku-protocol-explainer test
bun run --cwd apps/@moku-protocol-explainer build
```
17 changes: 17 additions & 0 deletions apps/@moku-protocol-explainer/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta name="theme-color" content="#faf9f6" />
<title>Connection · Moku protocol explainer</title>
<meta
name="description"
content="Step through a notebook connection and watch protocol events build a client replica."
/>
</head>
<body>
<div id="root"></div>
<script type="module" src="/src/main.tsx"></script>
</body>
</html>
21 changes: 21 additions & 0 deletions apps/@moku-protocol-explainer/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"name": "@moku/protocol-explainer",
"private": true,
"type": "module",
"scripts": {
"dev": "vp dev",
"build": "vp build",
"preview": "vp preview",
"test": "vp test run"
},
"dependencies": {
"@fontsource-variable/inter": "^5.3.0",
"@marimo-team/client": "file:../../vendor/client",
"effect": "4.1.0",
"react": "^19.3.0",
"react-dom": "^19.3.0"
},
"devDependencies": {
"@vitejs/plugin-react": "^6.1.2"
}
}
231 changes: 231 additions & 0 deletions apps/@moku-protocol-explainer/src/App.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,231 @@
/** An interactive first connection, presented as one continuous illustration. @module */
import * as React from "react";
import * as Connection from "./connection.ts";
import * as Playback from "./Playback.ts";
import { Scene, type Inspection } from "./Scene.tsx";
import { usePlayback } from "./usePlayback.ts";
import { Story } from "./Story.tsx";

function PlayIcon({ paused }: { paused: boolean }) {
return (
<svg width="14" height="14" viewBox="0 0 16 16" aria-hidden="true">
{paused ? (
<path d="M5 3v10M11 3v10" stroke="currentColor" strokeWidth="2" />
) : (
<path d="m5 2 9 6-9 6z" fill="currentColor" />
)}
</svg>
);
}

function ConnectionLesson() {
const playback = usePlayback();
const scene = Connection.sceneAt(playback.position);
const last = Connection.steps.length - 1;
const inMotion = playback.position !== scene.index;
const [inspection, setInspection] = React.useState<Inspection | null>(null);
const caption = inMotion
? Connection.steps[scene.index + 1].moving
: scene.step.title;
const snapshotStart = Connection.steps.findIndex((step) => step.breakpoint);
const phase = playback.position < 2 ? 0 : playback.position <= snapshotStart ? 1 : 2;
const action = playback.running
? "Pause"
: playback.position === 0
? "Connect"
: scene.ready
? "Replay"
: "Continue";

function inspect(value: Inspection) {
playback.pause();
setInspection(value);
}

return (
<>
<div className="connection-lesson">
<section className="illustration" aria-label="Connection walkthrough">
<div className="lesson-toolbar">
<nav className="chapters" aria-label="Lesson moments">
{[
{ title: "Connect", position: 0 },
{ title: "Load objects", position: 2 },
{ title: "Send snapshot", position: snapshotStart },
].map((chapter, index) => (
<React.Fragment key={chapter.title}>
{index > 0 && <span className="chapter-divider" aria-hidden="true" />}
<button
type="button"
aria-current={phase === index ? "step" : undefined}
onClick={() => playback.seek(chapter.position)}
>
<span className="chapter-number">0{index + 1}</span>
{chapter.title}
</button>
</React.Fragment>
))}
</nav>
<div className="playback">
<button
className="play-button"
type="button"
onClick={playback.running ? playback.pause : playback.play}
>
<PlayIcon paused={playback.running} />
{action}
</button>
<button
className="icon-button"
type="button"
aria-label="Advance one step"
title="Advance one step"
onClick={playback.next}
disabled={scene.ready}
>
<svg width="16" height="16" viewBox="0 0 16 16" aria-hidden="true">
<path d="m3 3 7 5-7 5z" fill="currentColor" />
<path d="M12 3v10" stroke="currentColor" strokeWidth="1.5" />
</svg>
</button>
<div className="scrubber">
<input
aria-label="Timeline"
type="range"
min="0"
max={last}
step="0.01"
value={playback.position}
onChange={(event) => playback.seek(event.currentTarget.valueAsNumber)}
style={
{
"--progress": `${(playback.position / last) * 100}%`,
} as React.CSSProperties
}
/>
<div className="scrubber-stops" aria-hidden="true">
{Connection.steps.map((_, index) => (
<i
key={index}
className={index <= playback.position ? "visited" : ""}
/>
))}
</div>
</div>
<select
className="speed-control"
aria-label="Playback speed"
title="Playback speed"
value={playback.speed}
onChange={(event) => {
const speed = Playback.speeds.find(
(value) => String(value) === event.currentTarget.value,
);
if (speed !== undefined) playback.setSpeed(speed);
}}
>
{Playback.speeds.map((speed) => (
<option key={speed} value={speed}>
{speed}×
</option>
))}
</select>
<button
className="icon-button"
type="button"
aria-label="Reset lesson"
title="Reset lesson"
onClick={() => playback.seek(0)}
>
<svg width="16" height="16" viewBox="0 0 16 16" aria-hidden="true">
<path
d="M3 5a5 5 0 1 1-1 5M3 1v4h4"
fill="none"
stroke="currentColor"
strokeWidth="1.4"
strokeLinecap="round"
strokeLinejoin="round"
/>
</svg>
</button>
</div>
</div>
<div className="scene-viewport">
<Scene position={playback.position} onInspect={inspect} />
</div>
<div className="narration">
<span
className={`narration-mark ${scene.step.breakpoint && !inMotion ? "breakpoint" : ""}`}
aria-hidden="true"
/>
<p role="status">{caption}</p>
</div>
</section>
{inspection && (
<section className="inspector" aria-label="Object details">
<div className="inspector-heading">
<h2>{inspection.title}</h2>
<button
type="button"
aria-label="Close details"
onClick={() => setInspection(null)}
>
×
</button>
</div>
<pre>{inspection.content}</pre>
</section>
)}
<footer className="lesson-footer">
<span>Drag time. Click an object to look inside.</span>
</footer>
</div>
</>
);
}

/** A guided introduction that opens into an inspectable connection scenario. */
export function App() {
return (
<>
<header className="masthead">
<a className="wordmark" href="/" aria-label="Moku protocol explainer">
moku<span className="wordmark-dot">.</span>
</a>
<span className="masthead-divider" />
<span>Host protocol</span>
<a className="skip-story" href="#connection">
Connection example ↗
</a>
</header>
<main className="story-page">
<Story />
<section
className="connection-chapter"
id="connection"
aria-labelledby="connection-title"
>
<div className="chapter-intro">
<h2 id="connection-title">Connecting to a notebook</h2>
<p>
A client opens an event stream to receive the notebook’s current records.
The host sends a snapshot, followed by a ready event. Subsequent events
describe changes to those records over the same connection.
</p>
<p>
This example shows a host loading a notebook from a file when the client
first connects. Playback pauses after loading, before the host sends the
snapshot.
</p>
</div>
<ConnectionLesson />
<p className="story-coda">
At <code>ready</code>, the snapshot is complete. The connection stays open:
later record events replace earlier records with the same identity, and
removal events tell the client what to discard.
</p>
</section>
</main>
</>
);
}
Loading