@lightblues/jig 0.1.0 → 0.1.1
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 +1 -0
- package/dist/canvas/bridge.d.ts +110 -0
- package/dist/canvas/bridge.js +255 -0
- package/dist/canvas/build.d.ts +25 -0
- package/dist/canvas/build.js +54 -0
- package/dist/canvas/compile.d.ts +29 -0
- package/dist/canvas/compile.js +218 -0
- package/dist/canvas/context.d.ts +43 -0
- package/dist/canvas/context.js +79 -0
- package/dist/canvas/document.d.ts +50 -0
- package/dist/canvas/document.js +128 -0
- package/dist/canvas/frame.d.ts +65 -0
- package/dist/canvas/frame.js +197 -0
- package/dist/canvas/names.d.ts +1 -0
- package/dist/canvas/names.js +6 -0
- package/dist/canvas/protocol.d.ts +93 -0
- package/dist/canvas/protocol.js +152 -0
- package/dist/canvas/runtime.d.ts +25 -0
- package/dist/canvas/runtime.js +110 -0
- package/dist/canvas/server.d.ts +5 -0
- package/dist/canvas/server.js +8 -0
- package/dist/canvas/state.d.ts +31 -0
- package/dist/canvas/state.js +72 -0
- package/dist/canvas/store.d.ts +39 -0
- package/dist/canvas/store.js +82 -0
- package/dist/index.d.ts +1 -1
- package/dist/index.js +1 -1
- package/package.json +30 -1
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
// `@lightblues/jig/canvas/state`: React bindings of the canvas store: `CanvasStateProvider` and the hooks canvases and kit components call. The same
|
|
2
|
+
// hooks work in-page (tier A: the provider's callbacks go to the host) and in the frame (tier B: the runtime's
|
|
3
|
+
// provider holds a store bridged over postMessage). Cursor's names are kept: `useCanvasState`, `useCanvasAction`.
|
|
4
|
+
import { createContext, createElement, useCallback, useContext, useEffect, useRef, useState, useSyncExternalStore } from 'react';
|
|
5
|
+
import { CanvasStore } from "./store.js";
|
|
6
|
+
export { CanvasStore } from "./store.js";
|
|
7
|
+
export { LIMITS } from "./protocol.js";
|
|
8
|
+
const StoreContext = createContext(null);
|
|
9
|
+
/** One surface's state. Several providers on one page keep separate state. */
|
|
10
|
+
export function CanvasStateProvider(props) {
|
|
11
|
+
const { store: given, surface, initial, values, theme, children } = props;
|
|
12
|
+
const made = useRef(null);
|
|
13
|
+
if (!given && (!made.current || made.current.surface !== (surface ?? '')))
|
|
14
|
+
made.current = new CanvasStore({ surface, initial: values ?? initial, theme });
|
|
15
|
+
const store = given ?? made.current;
|
|
16
|
+
// The latest callbacks, without making a new store. A given store keeps its own (the frame runtime's bridge).
|
|
17
|
+
if (!given)
|
|
18
|
+
store.options = { ...store.options, onState: props.onState, onEvent: props.onEvent, onAction: props.onAction, onDrop: props.onDrop };
|
|
19
|
+
const first = useRef(true);
|
|
20
|
+
useEffect(() => {
|
|
21
|
+
if (first.current)
|
|
22
|
+
return void (first.current = false);
|
|
23
|
+
if (values)
|
|
24
|
+
store.replace(values);
|
|
25
|
+
}, [store, values]);
|
|
26
|
+
useEffect(() => {
|
|
27
|
+
if (theme)
|
|
28
|
+
store.setTheme(theme);
|
|
29
|
+
}, [store, theme]);
|
|
30
|
+
return createElement(StoreContext.Provider, { value: store }, children);
|
|
31
|
+
}
|
|
32
|
+
const noSubscribe = () => () => { };
|
|
33
|
+
/**
|
|
34
|
+
* `useState` whose value the host keeps per surface (and the agent can read). Outside a provider it is a plain
|
|
35
|
+
* `useState`. Values must be JSON; a write over a limit is dropped (`LIMITS`).
|
|
36
|
+
*/
|
|
37
|
+
export function useCanvasState(key, defaultValue) {
|
|
38
|
+
const store = useContext(StoreContext);
|
|
39
|
+
const fallback = useRef(defaultValue);
|
|
40
|
+
const [local, setLocal] = useState(defaultValue);
|
|
41
|
+
const read = () => store?.get(key);
|
|
42
|
+
const stored = useSyncExternalStore(store ? store.subscribe : noSubscribe, read, read);
|
|
43
|
+
const value = store ? (store.has(key) ? stored : fallback.current) : local;
|
|
44
|
+
const set = useCallback((action) => {
|
|
45
|
+
if (!store)
|
|
46
|
+
return setLocal(action);
|
|
47
|
+
const prev = (store.has(key) ? store.get(key) : fallback.current);
|
|
48
|
+
const next = typeof action === 'function' ? action(prev) : action;
|
|
49
|
+
store.set(key, next);
|
|
50
|
+
}, [store, key]);
|
|
51
|
+
return [value, set];
|
|
52
|
+
}
|
|
53
|
+
/** `(name, data) => void`: an append-only event the host stores and the agent reads (a guess, a result). */
|
|
54
|
+
export function useCanvasEvent() {
|
|
55
|
+
const store = useContext(StoreContext);
|
|
56
|
+
return useCallback((name, data = null) => void store?.event(name, data), [store]);
|
|
57
|
+
}
|
|
58
|
+
/** A stable `dispatch` for buttons. The host decides what each action does, and acts only during a click. */
|
|
59
|
+
export function useCanvasAction() {
|
|
60
|
+
const store = useContext(StoreContext);
|
|
61
|
+
return useCallback((action) => void store?.action(action), [store]);
|
|
62
|
+
}
|
|
63
|
+
/** The host's theme kind (`light` / `dark`); `light` outside a provider. */
|
|
64
|
+
export function useCanvasTheme() {
|
|
65
|
+
const store = useContext(StoreContext);
|
|
66
|
+
const read = () => store?.theme ?? 'light';
|
|
67
|
+
return useSyncExternalStore(store ? store.subscribe : noSubscribe, read, read);
|
|
68
|
+
}
|
|
69
|
+
/** The surface id of the nearest provider, `''` outside one. */
|
|
70
|
+
export function useCanvasSurface() {
|
|
71
|
+
return useContext(StoreContext)?.surface ?? '';
|
|
72
|
+
}
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import { type CanvasAction, type CanvasState, type ThemeKind } from './protocol.ts';
|
|
2
|
+
export interface CanvasStoreOptions {
|
|
3
|
+
/** The host's id for this surface (`"lemma:econ/fig-growth"`, `"artifact:<id>"`, `"msg:<id>/<n>"`). */
|
|
4
|
+
surface?: string;
|
|
5
|
+
/** Saved values. */
|
|
6
|
+
initial?: CanvasState;
|
|
7
|
+
theme?: ThemeKind;
|
|
8
|
+
/** A `useCanvasState` write: latest wins, the host debounces and stores it. */
|
|
9
|
+
onState?: (key: string, value: unknown) => void;
|
|
10
|
+
/** A `useCanvasEvent` call: append-only, sent at once. */
|
|
11
|
+
onEvent?: (name: string, data: unknown) => void;
|
|
12
|
+
/** A `useCanvasAction` dispatch: the host decides, during a click only. */
|
|
13
|
+
onAction?: (action: CanvasAction) => void;
|
|
14
|
+
/** A write, event or action dropped by the store's checks (the same limits as the frame bridge). */
|
|
15
|
+
onDrop?: (reason: string) => void;
|
|
16
|
+
}
|
|
17
|
+
export declare class CanvasStore {
|
|
18
|
+
readonly surface: string;
|
|
19
|
+
private values;
|
|
20
|
+
private kind;
|
|
21
|
+
private readonly listeners;
|
|
22
|
+
/** Callbacks; the provider swaps them for the latest props. */
|
|
23
|
+
options: CanvasStoreOptions;
|
|
24
|
+
constructor(options?: CanvasStoreOptions);
|
|
25
|
+
get snapshot(): CanvasState;
|
|
26
|
+
get theme(): ThemeKind;
|
|
27
|
+
has(key: string): boolean;
|
|
28
|
+
get(key: string): unknown;
|
|
29
|
+
/** A write from the page: kept, shown, and passed to `onState`. False when a limit drops it. */
|
|
30
|
+
set(key: string, value: unknown): boolean;
|
|
31
|
+
/** Values from the host (another device or the agent changed them): shown, not sent back. */
|
|
32
|
+
replace(values: CanvasState): void;
|
|
33
|
+
setTheme(kind: ThemeKind): void;
|
|
34
|
+
event(name: string, data?: unknown): boolean;
|
|
35
|
+
action(action: CanvasAction): boolean;
|
|
36
|
+
subscribe: (listener: () => void) => (() => void);
|
|
37
|
+
private notify;
|
|
38
|
+
private drop;
|
|
39
|
+
}
|
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
// The in-page state store behind the canvas hooks: one per surface. Pure (no React, no DOM): the React provider wraps
|
|
2
|
+
// it for tier A, and the frame runtime connects it to postMessage for tier B.
|
|
3
|
+
import { EVENT_NAME, isJson, jsonSize, LIMITS } from "./protocol.js";
|
|
4
|
+
export class CanvasStore {
|
|
5
|
+
surface;
|
|
6
|
+
values;
|
|
7
|
+
kind;
|
|
8
|
+
listeners = new Set();
|
|
9
|
+
/** Callbacks; the provider swaps them for the latest props. */
|
|
10
|
+
options;
|
|
11
|
+
constructor(options = {}) {
|
|
12
|
+
this.options = options;
|
|
13
|
+
this.surface = options.surface ?? '';
|
|
14
|
+
this.values = { ...(options.initial ?? {}) };
|
|
15
|
+
this.kind = options.theme ?? 'light';
|
|
16
|
+
}
|
|
17
|
+
get snapshot() {
|
|
18
|
+
return this.values;
|
|
19
|
+
}
|
|
20
|
+
get theme() {
|
|
21
|
+
return this.kind;
|
|
22
|
+
}
|
|
23
|
+
has(key) {
|
|
24
|
+
return Object.hasOwn(this.values, key);
|
|
25
|
+
}
|
|
26
|
+
get(key) {
|
|
27
|
+
return this.has(key) ? this.values[key] : undefined;
|
|
28
|
+
}
|
|
29
|
+
/** A write from the page: kept, shown, and passed to `onState`. False when a limit drops it. */
|
|
30
|
+
set(key, value) {
|
|
31
|
+
if (typeof key !== 'string' || !key || key.length > LIMITS.key)
|
|
32
|
+
return this.drop('state: bad key');
|
|
33
|
+
if (!isJson(value))
|
|
34
|
+
return this.drop(`state ${key}: not JSON`);
|
|
35
|
+
const next = { ...this.values, [key]: value };
|
|
36
|
+
if (jsonSize(next) > LIMITS.state)
|
|
37
|
+
return this.drop(`state ${key}: surface state over ${LIMITS.state} chars`);
|
|
38
|
+
if (this.has(key) && Object.is(this.values[key], value))
|
|
39
|
+
return true;
|
|
40
|
+
this.values = next;
|
|
41
|
+
this.notify();
|
|
42
|
+
this.options.onState?.(key, value);
|
|
43
|
+
return true;
|
|
44
|
+
}
|
|
45
|
+
/** Values from the host (another device or the agent changed them): shown, not sent back. */
|
|
46
|
+
replace(values) {
|
|
47
|
+
this.values = { ...values };
|
|
48
|
+
this.notify();
|
|
49
|
+
}
|
|
50
|
+
setTheme(kind) {
|
|
51
|
+
if (kind === this.kind)
|
|
52
|
+
return;
|
|
53
|
+
this.kind = kind;
|
|
54
|
+
this.notify();
|
|
55
|
+
}
|
|
56
|
+
event(name, data = null) {
|
|
57
|
+
if (typeof name !== 'string' || !EVENT_NAME.test(name))
|
|
58
|
+
return this.drop(`event: bad name`);
|
|
59
|
+
if (!isJson(data) || jsonSize(data) > LIMITS.eventData)
|
|
60
|
+
return this.drop(`event ${name}: data not JSON or over ${LIMITS.eventData} chars`);
|
|
61
|
+
this.options.onEvent?.(name, data);
|
|
62
|
+
return true;
|
|
63
|
+
}
|
|
64
|
+
action(action) {
|
|
65
|
+
if (!action || typeof action !== 'object' || typeof action.type !== 'string' || !isJson(action) || jsonSize(action) > LIMITS.action)
|
|
66
|
+
return this.drop('action: not a JSON object with a type');
|
|
67
|
+
this.options.onAction?.(action);
|
|
68
|
+
return true;
|
|
69
|
+
}
|
|
70
|
+
subscribe = (listener) => {
|
|
71
|
+
this.listeners.add(listener);
|
|
72
|
+
return () => this.listeners.delete(listener);
|
|
73
|
+
};
|
|
74
|
+
notify() {
|
|
75
|
+
for (const listener of [...this.listeners])
|
|
76
|
+
listener();
|
|
77
|
+
}
|
|
78
|
+
drop(reason) {
|
|
79
|
+
this.options.onDrop?.(reason);
|
|
80
|
+
return false;
|
|
81
|
+
}
|
|
82
|
+
}
|
package/dist/index.d.ts
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
export declare const parts: readonly ["agent-paseo", "agent-pi", "agent-chat", "markdown"];
|
|
1
|
+
export declare const parts: readonly ["agent-paseo", "agent-pi", "agent-chat", "markdown", "canvas"];
|
package/dist/index.js
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
1
|
// @lightblues/jig: reusable parts for my apps. Each part is its own subpath export; this root only names the parts.
|
|
2
|
-
export const parts = ['agent-paseo', 'agent-pi', 'agent-chat', 'markdown'];
|
|
2
|
+
export const parts = ['agent-paseo', 'agent-pi', 'agent-chat', 'markdown', 'canvas'];
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@lightblues/jig",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.1",
|
|
4
4
|
"description": "Reusable parts for my apps: agent runtimes (Paseo, pi) that speak AG-UI, a thin thread-following client, one markdown renderer, and more as they prove reusable.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"license": "MIT",
|
|
@@ -48,6 +48,26 @@
|
|
|
48
48
|
"types": "./dist/markdown/react.d.ts",
|
|
49
49
|
"default": "./dist/markdown/react.js"
|
|
50
50
|
},
|
|
51
|
+
"./canvas/server": {
|
|
52
|
+
"types": "./dist/canvas/server.d.ts",
|
|
53
|
+
"default": "./dist/canvas/server.js"
|
|
54
|
+
},
|
|
55
|
+
"./canvas/state": {
|
|
56
|
+
"types": "./dist/canvas/state.d.ts",
|
|
57
|
+
"default": "./dist/canvas/state.js"
|
|
58
|
+
},
|
|
59
|
+
"./canvas/frame": {
|
|
60
|
+
"types": "./dist/canvas/frame.d.ts",
|
|
61
|
+
"default": "./dist/canvas/frame.js"
|
|
62
|
+
},
|
|
63
|
+
"./canvas/runtime": {
|
|
64
|
+
"types": "./dist/canvas/runtime.d.ts",
|
|
65
|
+
"default": "./dist/canvas/runtime.js"
|
|
66
|
+
},
|
|
67
|
+
"./canvas/build": {
|
|
68
|
+
"types": "./dist/canvas/build.d.ts",
|
|
69
|
+
"default": "./dist/canvas/build.js"
|
|
70
|
+
},
|
|
51
71
|
"./package.json": "./package.json"
|
|
52
72
|
},
|
|
53
73
|
"scripts": {
|
|
@@ -82,6 +102,7 @@
|
|
|
82
102
|
"react": "19.3.1",
|
|
83
103
|
"react-dom": "19.3.1",
|
|
84
104
|
"react-test-renderer": "19.3.1",
|
|
105
|
+
"sucrase": "3.35.1",
|
|
85
106
|
"typescript": "^5.9.0",
|
|
86
107
|
"yaml": "2.9.0",
|
|
87
108
|
"zod": "4.6.7"
|
|
@@ -92,6 +113,7 @@
|
|
|
92
113
|
"@earendil-works/pi-ai": "~1.4.0",
|
|
93
114
|
"@earendil-works/pi-coding-agent": "~1.4.0",
|
|
94
115
|
"@getpaseo/client": "0.16.1 || 0.17.4",
|
|
116
|
+
"esbuild": ">=0.25",
|
|
95
117
|
"highlight.js": "^11.0.0",
|
|
96
118
|
"katex": ">=0.16.21",
|
|
97
119
|
"markdown-it": "^15.0.0",
|
|
@@ -100,6 +122,7 @@
|
|
|
100
122
|
"mermaid": "^12.0.0",
|
|
101
123
|
"react": ">=18",
|
|
102
124
|
"react-dom": ">=18",
|
|
125
|
+
"sucrase": "~3.35.0",
|
|
103
126
|
"yaml": "^2.0.0"
|
|
104
127
|
},
|
|
105
128
|
"peerDependenciesMeta": {
|
|
@@ -118,6 +141,9 @@
|
|
|
118
141
|
"@getpaseo/client": {
|
|
119
142
|
"optional": true
|
|
120
143
|
},
|
|
144
|
+
"esbuild": {
|
|
145
|
+
"optional": true
|
|
146
|
+
},
|
|
121
147
|
"highlight.js": {
|
|
122
148
|
"optional": true
|
|
123
149
|
},
|
|
@@ -142,6 +168,9 @@
|
|
|
142
168
|
"react-dom": {
|
|
143
169
|
"optional": true
|
|
144
170
|
},
|
|
171
|
+
"sucrase": {
|
|
172
|
+
"optional": true
|
|
173
|
+
},
|
|
145
174
|
"yaml": {
|
|
146
175
|
"optional": true
|
|
147
176
|
}
|