numbl 0.4.13 → 0.4.14

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -50,10 +50,6 @@ Full documentation is available at **[numbl.org/docs](https://numbl.org/docs)**,
50
50
 
51
51
  The [Numbl extension for VS Code](https://marketplace.visualstudio.com/items?itemName=jmagland.numbl) provides inline error diagnostics and a built-in figure viewer.
52
52
 
53
- ## Embedding
54
-
55
- Numbl scripts can be embedded in HTML and Markdown pages. See the [numbl-embed-example](https://magland.github.io/numbl-embed-example/) for usage and a [live demo](https://magland.github.io/numbl-embed-example/example1).
56
-
57
53
  ## Authors
58
54
 
59
55
  Jeremy Magland and Dan Fortunato, Center for Computational Mathematics, Flatiron Institute.
@@ -1,7 +1,7 @@
1
1
  /**
2
2
  * numbl/browser — managed in-browser sessions.
3
3
  *
4
- * Usage:
4
+ * Run-a-script usage:
5
5
  * import { createNumblSession } from "numbl/browser";
6
6
  * const session = await createNumblSession({
7
7
  * files: [{ path: "main.m", content: "mip load --install chebfun;\n..." }],
@@ -10,12 +10,21 @@
10
10
  * });
11
11
  * session.dispatchHtmlEvent(compId, "eventName", payload);
12
12
  *
13
+ * REPL usage (persistent workspace across calls):
14
+ * const session = await createNumblSession({ onOutput: console.log });
15
+ * await session.execute("x = linspace(0, 2*pi, 100);");
16
+ * const { plotInstructions } = await session.execute("plot(x, sin(x))");
17
+ * // render plotInstructions with figuresReducer/FigureView from numbl/graphics
18
+ *
13
19
  * The session runs in a worker numbl manages (inlined at build time — no
14
20
  * bundler worker support needed), with mip bootstrapped from its GitHub
15
21
  * release and installed packages persisted in IndexedDB across page loads.
16
22
  *
17
- * Not available in sessions: delaunay/convhull (qhull WASM is not loaded)
18
- * and non-uihtml figures (plot instructions other than uihtml are ignored).
23
+ * Figures are not rendered by the session itself: plot instructions are
24
+ * returned/surfaced for the host to render (numbl/graphics provides a React
25
+ * renderer). Not available in sessions: delaunay/convhull (qhull WASM is not
26
+ * loaded).
19
27
  */
20
28
  export { createNumblSession, type NumblSession, type NumblSessionOptions, } from "./session.js";
21
- export type { BootFile, UihtmlComponent } from "./protocol.js";
29
+ export type { BootFile, ExecuteResult, UihtmlComponent } from "./protocol.js";
30
+ export type { PlotInstruction } from "../graphics/types.js";
@@ -1,4 +1,5 @@
1
1
  /** Messages between the NumblSession host and its worker. */
2
+ import type { PlotInstruction } from "../graphics/types.js";
2
3
  export interface BootFile {
3
4
  path: string;
4
5
  content: string | Uint8Array;
@@ -6,7 +7,9 @@ export interface BootFile {
6
7
  export interface BootMessage {
7
8
  type: "boot";
8
9
  files: BootFile[];
9
- mainFile: string;
10
+ /** Script to run at boot. When omitted, the session boots idle and code is
11
+ * run incrementally via `execute`. */
12
+ mainFile?: string;
10
13
  mip: boolean;
11
14
  persistSystem: boolean;
12
15
  systemInactivityMs: number;
@@ -22,6 +25,10 @@ export type ToWorker = BootMessage | {
22
25
  type: "readFile";
23
26
  id: number;
24
27
  path: string;
28
+ } | {
29
+ type: "execute";
30
+ id: number;
31
+ code: string;
25
32
  } | {
26
33
  type: "dispatch";
27
34
  id: number;
@@ -31,8 +38,20 @@ export type ToWorker = BootMessage | {
31
38
  };
32
39
  export interface UihtmlComponent {
33
40
  compId: string;
41
+ /** The component's HTML markup (render with buildUihtmlSrcDoc). */
42
+ html: string;
43
+ /** The component's Data, JSON-encoded. */
34
44
  dataJson: string;
35
45
  }
46
+ export interface ExecuteResult {
47
+ ok: boolean;
48
+ /** Concatenated console output (also streamed live via onOutput). */
49
+ output: string;
50
+ /** Every plot instruction the execution produced, uihtml included. */
51
+ plotInstructions: PlotInstruction[];
52
+ /** Formatted error message when `ok` is false. */
53
+ error?: string;
54
+ }
36
55
  export type FromWorker = {
37
56
  type: "progress";
38
57
  message: string;
@@ -42,6 +61,7 @@ export type FromWorker = {
42
61
  } | {
43
62
  type: "uihtml";
44
63
  compId: string;
64
+ html: string;
45
65
  dataJson: string;
46
66
  } | {
47
67
  type: "ready";
@@ -55,6 +75,10 @@ export type FromWorker = {
55
75
  compId: string;
56
76
  name: string;
57
77
  dataJson: string;
78
+ } | {
79
+ type: "executeResult";
80
+ id: number;
81
+ result: ExecuteResult;
58
82
  } | {
59
83
  type: "dispatchResult";
60
84
  id: number;
@@ -1,9 +1,10 @@
1
- import type { BootFile, UihtmlComponent } from "./protocol.js";
1
+ import type { BootFile, ExecuteResult, UihtmlComponent } from "./protocol.js";
2
2
  export interface NumblSessionOptions {
3
- /** Project files. Relative paths land under /project/. */
4
- files: BootFile[];
5
- /** The script to run, as one of the paths in `files`. */
6
- mainFile: string;
3
+ /** Project files. Relative paths land under /project/. Default none. */
4
+ files?: BootFile[];
5
+ /** The script to run at boot, as one of the paths in `files`. When omitted
6
+ * the session boots idle; run code incrementally with `execute`. */
7
+ mainFile?: string;
7
8
  /**
8
9
  * Bootstrap the mip package manager (fetched from its GitHub release)
9
10
  * so the script can `mip load --install <pkg>`. Default true.
@@ -26,8 +27,8 @@ export interface NumblSessionOptions {
26
27
  onOutput?: (text: string) => void;
27
28
  /** Boot progress (package downloads, engine start). */
28
29
  onProgress?: (message: string) => void;
29
- /** A uihtml component was created/updated (its Data JSON-encoded). */
30
- onUihtml?: (compId: string, dataJson: string) => void;
30
+ /** A uihtml component was created/updated (Data JSON-encoded + markup). */
31
+ onUihtml?: (compId: string, dataJson: string, html: string) => void;
31
32
  /** Script -> host events (MATLAB `sendEventToHTMLSource`). */
32
33
  onHtmlSourceEvent?: (compId: string, name: string, dataJson: string) => void;
33
34
  }
@@ -36,6 +37,14 @@ export interface NumblSession {
36
37
  readonly uihtmlComponents: readonly UihtmlComponent[];
37
38
  /** True if the run left a live uihtml session (events can be dispatched). */
38
39
  readonly hasUihtmlSession: boolean;
40
+ /**
41
+ * Execute code against the session's persistent workspace (REPL semantics:
42
+ * variables persist across calls, expression results are auto-displayed).
43
+ * Resolves with the run's output and plot instructions; a numbl error
44
+ * resolves with `ok: false` and a formatted `error` (the promise rejects
45
+ * only on session-level failures). Calls run sequentially in the worker.
46
+ */
47
+ execute(code: string): Promise<ExecuteResult>;
39
48
  /** Write a file into the session VFS (visible to later dispatched events). */
40
49
  writeFile(path: string, content: string | Uint8Array): void;
41
50
  /**
@@ -53,8 +62,9 @@ export interface NumblSession {
53
62
  }
54
63
  /**
55
64
  * Boot a numbl session in a dedicated worker: restore the persisted /system
56
- * directory, bootstrap mip, run `mainFile`, and keep the uihtml session live.
57
- * Resolves once the main script has finished (including any
58
- * `mip load --install` downloads it performs).
65
+ * directory, bootstrap mip, run `mainFile` when given, and keep the uihtml
66
+ * session live. Resolves once boot (and the main script, if any) has
67
+ * finished, including any `mip load --install` downloads it performs.
68
+ * Without a `mainFile` the session boots idle — run code with `execute`.
59
69
  */
60
70
  export declare function createNumblSession(options: NumblSessionOptions): Promise<NumblSession>;