@datalayer/reactor-excalidraw 0.0.0 → 1.3.11
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/LICENSE +29 -0
- package/README.md +113 -1
- package/lib/definitions.d.ts +30 -0
- package/lib/definitions.d.ts.map +1 -0
- package/lib/definitions.js +186 -0
- package/lib/definitions.js.map +1 -0
- package/lib/handlers.d.ts +128 -0
- package/lib/handlers.d.ts.map +1 -0
- package/lib/handlers.js +570 -0
- package/lib/handlers.js.map +1 -0
- package/lib/host.d.ts +88 -0
- package/lib/host.d.ts.map +1 -0
- package/lib/host.js +73 -0
- package/lib/host.js.map +1 -0
- package/lib/index.d.ts +21 -0
- package/lib/index.d.ts.map +1 -0
- package/lib/index.js +26 -0
- package/lib/index.js.map +1 -0
- package/lib/operations.d.ts +73 -0
- package/lib/operations.d.ts.map +1 -0
- package/lib/operations.js +85 -0
- package/lib/operations.js.map +1 -0
- package/lib/plugin.d.ts +10 -0
- package/lib/plugin.d.ts.map +1 -0
- package/lib/plugin.js +66 -0
- package/lib/plugin.js.map +1 -0
- package/lib/scene.d.ts +101 -0
- package/lib/scene.d.ts.map +1 -0
- package/lib/scene.js +168 -0
- package/lib/scene.js.map +1 -0
- package/lib/schemas.d.ts +230 -0
- package/lib/schemas.d.ts.map +1 -0
- package/lib/schemas.js +209 -0
- package/lib/schemas.js.map +1 -0
- package/lib/tools.d.ts +33 -0
- package/lib/tools.d.ts.map +1 -0
- package/lib/tools.js +92 -0
- package/lib/tools.js.map +1 -0
- package/package.json +44 -4
package/lib/host.d.ts
ADDED
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Where drawings live, and what Excalidraw does for the tools.
|
|
3
|
+
*
|
|
4
|
+
* The tools do not know whether a drawing is a block in a Lexical document, a
|
|
5
|
+
* canvas on a page, or a file on a server. They ask a host, which keeps the
|
|
6
|
+
* drawings and answers for them by id, and they are handed Excalidraw's two
|
|
7
|
+
* converters rather than importing Excalidraw: it is a browser bundle several
|
|
8
|
+
* megabytes wide, and the host that renders drawings has it already.
|
|
9
|
+
*
|
|
10
|
+
* @module host
|
|
11
|
+
*/
|
|
12
|
+
import type { ExcalidrawElementData, ExcalidrawScene } from './scene';
|
|
13
|
+
/** One drawing as a host lists it. */
|
|
14
|
+
export interface ExcalidrawHostedDrawing {
|
|
15
|
+
/** The id the tools address it by (`blockId`). */
|
|
16
|
+
id: string;
|
|
17
|
+
scene: ExcalidrawScene;
|
|
18
|
+
/** Its rendered size, when the host keeps one. */
|
|
19
|
+
width?: number;
|
|
20
|
+
height?: number;
|
|
21
|
+
}
|
|
22
|
+
/** Where a new drawing goes, and how large it is rendered. */
|
|
23
|
+
export interface ExcalidrawPlacement {
|
|
24
|
+
/** 'TOP', 'BOTTOM' or the id to insert after, for a host that orders drawings. */
|
|
25
|
+
afterId?: string;
|
|
26
|
+
width?: number;
|
|
27
|
+
height?: number;
|
|
28
|
+
}
|
|
29
|
+
/**
|
|
30
|
+
* What keeps the drawings.
|
|
31
|
+
*
|
|
32
|
+
* Every method that names a drawing throws when there is none of that id,
|
|
33
|
+
* with a message that tells the model what to do next: the tools pass it on
|
|
34
|
+
* as it is.
|
|
35
|
+
*/
|
|
36
|
+
export interface ExcalidrawDrawingHost {
|
|
37
|
+
/** Every drawing, in the host's order. */
|
|
38
|
+
listDrawings(): Promise<ExcalidrawHostedDrawing[]>;
|
|
39
|
+
/** The scene of one drawing. */
|
|
40
|
+
readDrawing(id: string): Promise<ExcalidrawScene>;
|
|
41
|
+
/** Replace the scene of one drawing. */
|
|
42
|
+
writeDrawing(id: string, scene: ExcalidrawScene): Promise<void>;
|
|
43
|
+
/** Make a new drawing; answers its id. */
|
|
44
|
+
insertDrawing(scene: ExcalidrawScene, placement: ExcalidrawPlacement): Promise<string>;
|
|
45
|
+
/** Set how large a drawing is rendered; `'inherit'` sizes it by content. */
|
|
46
|
+
resizeDrawing(id: string, width: number | 'inherit', height: number | 'inherit'): Promise<void>;
|
|
47
|
+
}
|
|
48
|
+
/**
|
|
49
|
+
* Excalidraw's own conversions, handed in by the host.
|
|
50
|
+
*
|
|
51
|
+
* `convertToExcalidrawElements` from `@excalidraw/excalidraw`, and
|
|
52
|
+
* `parseMermaidToExcalidraw` from `@excalidraw/mermaid-to-excalidraw` — the
|
|
53
|
+
* second only for a host that offers `excalidrawFromMermaid`.
|
|
54
|
+
*/
|
|
55
|
+
export interface ExcalidrawConverters {
|
|
56
|
+
convertToExcalidrawElements: (skeletons: Record<string, any>[], options?: {
|
|
57
|
+
regenerateIds?: boolean;
|
|
58
|
+
}) => readonly ExcalidrawElementData[] | ExcalidrawElementData[];
|
|
59
|
+
parseMermaidToExcalidraw?: (source: string, options?: {
|
|
60
|
+
fontSize?: number;
|
|
61
|
+
}) => Promise<{
|
|
62
|
+
elements: unknown[];
|
|
63
|
+
files?: Record<string, unknown>;
|
|
64
|
+
}>;
|
|
65
|
+
}
|
|
66
|
+
/**
|
|
67
|
+
* A host that keeps its drawings in memory, in insertion order.
|
|
68
|
+
*
|
|
69
|
+
* What a page with a canvas or two needs, and what the tests use. `onChange`
|
|
70
|
+
* is told after every write, so a view can redraw.
|
|
71
|
+
*/
|
|
72
|
+
export declare function createMemoryDrawingHost(options?: {
|
|
73
|
+
/** Drawings to start with. */
|
|
74
|
+
drawings?: ExcalidrawHostedDrawing[];
|
|
75
|
+
/** Told after every change, with the drawing's id. */
|
|
76
|
+
onChange?: (id: string) => void;
|
|
77
|
+
/** How a new drawing's id is made. */
|
|
78
|
+
newId?: () => string;
|
|
79
|
+
}): ExcalidrawDrawingHost & {
|
|
80
|
+
/** The drawing of an id, or undefined: for a view, which does not throw. */
|
|
81
|
+
get(id: string): ExcalidrawHostedDrawing | undefined;
|
|
82
|
+
/**
|
|
83
|
+
* Put a drawing in, or replace the one of its id, at the end if it is new:
|
|
84
|
+
* what a view does when a person draws, or a page restores what it had.
|
|
85
|
+
*/
|
|
86
|
+
set(drawing: ExcalidrawHost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;AACH,MAAM,WAAW,oBAAoB;IACnC,2BAA2B,EAAE,CAC3B,SAAS,EAAE,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,EAAE,EAChC,OAAO,CAAC,EAAE;QAAE,aAAa,CAAC,EAAE,OAAO,CAAA;KAAE,KAClC,SAAS,qBAAqB,EAAE,GAAG,qBAAqB,EAAE,CAAC;IAChE,wBAAwB,CAAC,EAAE,CACzB,MAAM,EAAE,MAAM,EACd,OAAO,CAAC,EAAE;QAAE,QAAQ,CAAC,EAAE,MAAM,CAAA;KAAE,KAC5B,OAAO,CAAC;QAAE,QAAQ,EAAE,OAAO,EAAE,CAAC;QAAC,KAAK,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAA;KAAE,CAAC,CAAC;CACxE;AAED;;;;;GAKG;AACH,wBAAgB,uBAAuB,CACrC,OAAO,GAAE;IACP,8BAA8B;IAC9B,QAAQ,CAAC,EAAE,uBAAuB,EAAE,CAAC;IACrC,sDAAsD;IACtD,QAAQ,CAAC,EAAE,CAAC,EAAE,EAAE,MAAM,KAAK,IAAI,CAAC;IAChC,sCAAsC;IACtC,KAAK,CAAC,EAAE,MAAM,MAAM,CAAC;CACjB,GACL,qBAAqB,GAAG;IACzB,4EAA4E;IAC5E,GAAG,CAAC,EAAE,EAAE,MAAM,GAAG,uBAAuB,GAAG,SAAS,CAAC;IACrD;;;OAGG;IACH,GAAG,CAAC,OAAO,EAAE,uBAAuB,GAAG,IAAI,CAAC;CAC7C,CAiEA"}
|
package/lib/host.js
ADDED
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Copyright (c) 2026-Present Datalayer, Inc.
|
|
3
|
+
*
|
|
4
|
+
* BSD 3-Clause License
|
|
5
|
+
*/
|
|
6
|
+
/**
|
|
7
|
+
* A host that keeps its drawings in memory, in insertion order.
|
|
8
|
+
*
|
|
9
|
+
* What a page with a canvas or two needs, and what the tests use. `onChange`
|
|
10
|
+
* is told after every write, so a view can redraw.
|
|
11
|
+
*/
|
|
12
|
+
export function createMemoryDrawingHost(options = {}) {
|
|
13
|
+
const drawings = new Map();
|
|
14
|
+
for (const drawing of options.drawings ?? []) {
|
|
15
|
+
drawings.set(drawing.id, drawing);
|
|
16
|
+
}
|
|
17
|
+
let counter = drawings.size;
|
|
18
|
+
const newId = options.newId ?? (() => `drawing-${(counter += 1)}`);
|
|
19
|
+
const changed = (id) => options.onChange?.(id);
|
|
20
|
+
const require = (id) => {
|
|
21
|
+
const found = drawings.get(id);
|
|
22
|
+
if (!found) {
|
|
23
|
+
throw new Error(`No drawing '${id}'. Call excalidrawListDrawings to find the drawings there are.`);
|
|
24
|
+
}
|
|
25
|
+
return found;
|
|
26
|
+
};
|
|
27
|
+
return {
|
|
28
|
+
get: (id) => drawings.get(id),
|
|
29
|
+
set: (drawing) => {
|
|
30
|
+
drawings.set(drawing.id, { ...drawing, scene: structuredClone(drawing.scene) });
|
|
31
|
+
changed(drawing.id);
|
|
32
|
+
},
|
|
33
|
+
listDrawings: async () => [...drawings.values()],
|
|
34
|
+
readDrawing: async (id) => structuredClone(require(id).scene),
|
|
35
|
+
writeDrawing: async (id, scene) => {
|
|
36
|
+
require(id).scene = structuredClone(scene);
|
|
37
|
+
changed(id);
|
|
38
|
+
},
|
|
39
|
+
insertDrawing: async (scene, placement) => {
|
|
40
|
+
let id = newId();
|
|
41
|
+
while (drawings.has(id)) {
|
|
42
|
+
id = newId();
|
|
43
|
+
}
|
|
44
|
+
const drawing = {
|
|
45
|
+
id,
|
|
46
|
+
scene: structuredClone(scene),
|
|
47
|
+
width: placement.width,
|
|
48
|
+
height: placement.height,
|
|
49
|
+
};
|
|
50
|
+
const entries = [...drawings.entries()];
|
|
51
|
+
const after = placement.afterId;
|
|
52
|
+
const at = after === 'TOP'
|
|
53
|
+
? 0
|
|
54
|
+
: after && after !== 'BOTTOM' && drawings.has(after)
|
|
55
|
+
? entries.findIndex(([key]) => key === after) + 1
|
|
56
|
+
: entries.length;
|
|
57
|
+
entries.splice(at, 0, [id, drawing]);
|
|
58
|
+
drawings.clear();
|
|
59
|
+
for (const [key, value] of entries) {
|
|
60
|
+
drawings.set(key, value);
|
|
61
|
+
}
|
|
62
|
+
changed(id);
|
|
63
|
+
return id;
|
|
64
|
+
},
|
|
65
|
+
resizeDrawing: async (id, width, height) => {
|
|
66
|
+
const drawing = require(id);
|
|
67
|
+
drawing.width = width === 'inherit' ? undefined : width;
|
|
68
|
+
drawing.height = height === 'inherit' ? undefined : height;
|
|
69
|
+
changed(id);
|
|
70
|
+
},
|
|
71
|
+
};
|
|
72
|
+
}
|
|
73
|
+
//# sourceMappingURL=host.js.map
|
package/lib/host.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"host.js","sourceRoot":"","sources":["../src/host.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAwEH;;;;;GAKG;AACH,MAAM,UAAU,uBAAuB,CACrC,UAOI,EAAE;IAUN,MAAM,QAAQ,GAAG,IAAI,GAAG,EAAmC,CAAC;IAC5D,KAAK,MAAM,OAAO,IAAI,OAAO,CAAC,QAAQ,IAAI,EAAE,EAAE,CAAC;QAC7C,QAAQ,CAAC,GAAG,CAAC,OAAO,CAAC,EAAE,EAAE,OAAO,CAAC,CAAC;IACpC,CAAC;IACD,IAAI,OAAO,GAAG,QAAQ,CAAC,IAAI,CAAC;IAC5B,MAAM,KAAK,GAAG,OAAO,CAAC,KAAK,IAAI,CAAC,GAAG,EAAE,CAAC,WAAW,CAAC,OAAO,IAAI,CAAC,CAAC,EAAE,CAAC,CAAC;IACnE,MAAM,OAAO,GAAG,CAAC,EAAU,EAAE,EAAE,CAAC,OAAO,CAAC,QAAQ,EAAE,CAAC,EAAE,CAAC,CAAC;IAEvD,MAAM,OAAO,GAAG,CAAC,EAAU,EAA2B,EAAE;QACtD,MAAM,KAAK,GAAG,QAAQ,CAAC,GAAG,CAAC,EAAE,CAAC,CAAC;QAC/B,IAAI,CAAC,KAA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1,21 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `@datalayer/reactor-excalidraw` — Excalidraw drawings as AI agent tools.
|
|
3
|
+
*
|
|
4
|
+
* The eleven tools an agent draws with (definitions, Zod schemas, the
|
|
5
|
+
* operations that validate a call), the handlers that carry it out over any
|
|
6
|
+
* host that keeps drawings, a pure scene model, and the reactor plugin that
|
|
7
|
+
* offers them as commands and an `AgentTools` bundle. No editor is assumed:
|
|
8
|
+
* a Lexical document, a page's canvas and a server each bring a host.
|
|
9
|
+
*
|
|
10
|
+
* @module index
|
|
11
|
+
*/
|
|
12
|
+
export * from './scene';
|
|
13
|
+
export * from './schemas';
|
|
14
|
+
export * from './definitions';
|
|
15
|
+
export * from './operations';
|
|
16
|
+
export * from './host';
|
|
17
|
+
export * from './handlers';
|
|
18
|
+
export * from './tools';
|
|
19
|
+
export * from './plugin';
|
|
20
|
+
export { ExcalidrawPlugin as default } from './plugin';
|
|
21
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAMA;;;;;;;;;;GAUG;AAEH,cAAc,SAAS,CAAC;AACxB,cAAc,WAAW,CAAC;AAC1B,cAAc,eAAe,CAAC;AAC9B,cAAc,cAAc,CAAC;AAC7B,cAAc,QAAQ,CAAC;AACvB,cAAc,YAAY,CAAC;AAC3B,cAAc,SAAS,CAAC;AACxB,cAAc,UAAU,CAAC;AACzB,OAAO,EAAE,gBAAgB,IAAI,OAAO,EAAE,MAAM,UAAU,CAAC"}
|
package/lib/index.js
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Copyright (c) 2026-Present Datalayer, Inc.
|
|
3
|
+
*
|
|
4
|
+
* BSD 3-Clause License
|
|
5
|
+
*/
|
|
6
|
+
/**
|
|
7
|
+
* `@datalayer/reactor-excalidraw` — Excalidraw drawings as AI agent tools.
|
|
8
|
+
*
|
|
9
|
+
* The eleven tools an agent draws with (definitions, Zod schemas, the
|
|
10
|
+
* operations that validate a call), the handlers that carry it out over any
|
|
11
|
+
* host that keeps drawings, a pure scene model, and the reactor plugin that
|
|
12
|
+
* offers them as commands and an `AgentTools` bundle. No editor is assumed:
|
|
13
|
+
* a Lexical document, a page's canvas and a server each bring a host.
|
|
14
|
+
*
|
|
15
|
+
* @module index
|
|
16
|
+
*/
|
|
17
|
+
export * from './scene.js';
|
|
18
|
+
export * from './schemas.js';
|
|
19
|
+
export * from './definitions.js';
|
|
20
|
+
export * from './operations.js';
|
|
21
|
+
export * from './host.js';
|
|
22
|
+
export * from './handlers.js';
|
|
23
|
+
export * from './tools.js';
|
|
24
|
+
export * from './plugin.js';
|
|
25
|
+
export { ExcalidrawPlugin as default } from './plugin.js';
|
|
26
|
+
//# sourceMappingURL=index.js.map
|
package/lib/index.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH;;;;;;;;;;GAUG;AAEH,cAAc,SAAS,CAAC;AACxB,cAAc,WAAW,CAAC;AAC1B,cAAc,eAAe,CAAC;AAC9B,cAAc,cAAc,CAAC;AAC7B,cAAc,QAAQ,CAAC;AACvB,cAAc,YAAY,CAAC;AAC3B,cAAc,SAAS,CAAC;AACxB,cAAc,UAAU,CAAC;AACzB,OAAO,EAAE,gBAAgB,IAAI,OAAO,EAAE,MAAM,UAAU,CAAC"}
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The agent-facing half of the Excalidraw tools.
|
|
3
|
+
*
|
|
4
|
+
* Each one validates its parameters and hands them to the executor; the work
|
|
5
|
+
* happens in `handlers.ts`, against the host that keeps the drawings, which
|
|
6
|
+
* is usually a browser page or editor. These operations run wherever the
|
|
7
|
+
* agent runs.
|
|
8
|
+
*
|
|
9
|
+
* So: nothing in this module may import an editor or Excalidraw, directly or
|
|
10
|
+
* transitively. That is what keeps the tool list loadable in Node.
|
|
11
|
+
*
|
|
12
|
+
* The eleven of them are the same three lines with different names, which is
|
|
13
|
+
* why they are built rather than written out. Writing them out would be
|
|
14
|
+
* eleven chances to get the validation call wrong, and nothing gained.
|
|
15
|
+
*
|
|
16
|
+
* @module operations
|
|
17
|
+
*/
|
|
18
|
+
import { type ToolOperation } from '@datalayer/reactor/tools';
|
|
19
|
+
import type { ExcalidrawElementSummary } from './scene';
|
|
20
|
+
/** What a call that changed a drawing reports back. */
|
|
21
|
+
export interface ExcalidrawSceneResult {
|
|
22
|
+
success: boolean;
|
|
23
|
+
/** The drawing that was touched. */
|
|
24
|
+
blockId: string;
|
|
25
|
+
/** How many elements it holds afterwards. */
|
|
26
|
+
elementCount: number;
|
|
27
|
+
/** The ids of the elements the call created, in the order they were given. */
|
|
28
|
+
createdIds?: string[];
|
|
29
|
+
message?: string;
|
|
30
|
+
}
|
|
31
|
+
/** What inserting a drawing reports back. */
|
|
32
|
+
export interface ExcalidrawInsertNodeResult extends ExcalidrawSceneResult {
|
|
33
|
+
/** The new block's id, for the calls that follow. */
|
|
34
|
+
blockId: string;
|
|
35
|
+
}
|
|
36
|
+
/** One drawing in a listing. */
|
|
37
|
+
export interface ExcalidrawDrawingSummary {
|
|
38
|
+
blockId: string;
|
|
39
|
+
elementCount: number;
|
|
40
|
+
/** "3 rectangles, 2 arrows: Start, Finish" */
|
|
41
|
+
description: string;
|
|
42
|
+
width?: number;
|
|
43
|
+
height?: number;
|
|
44
|
+
}
|
|
45
|
+
/** What a listing reports back. */
|
|
46
|
+
export interface ExcalidrawListDrawingsResult {
|
|
47
|
+
drawings: ExcalidrawDrawingSummary[];
|
|
48
|
+
count: number;
|
|
49
|
+
}
|
|
50
|
+
/** What reading a drawing reports back. */
|
|
51
|
+
export interface ExcalidrawReadSceneResult {
|
|
52
|
+
blockId: string;
|
|
53
|
+
elementCount: number;
|
|
54
|
+
elements: ExcalidrawElementSummary[] | Record<string, any>[];
|
|
55
|
+
appState: Record<string, any>;
|
|
56
|
+
description: string;
|
|
57
|
+
}
|
|
58
|
+
export declare const excalidrawInsertNodeOperation: ToolOperation<unknown, ExcalidrawInsertNodeResult>;
|
|
59
|
+
export declare const excalidrawListDrawingsOperation: ToolOperation<unknown, ExcalidrawListDrawingsResult>;
|
|
60
|
+
export declare const excalidrawReadSceneOperation: ToolOperation<unknown, ExcalidrawReadSceneResult>;
|
|
61
|
+
export declare const excalidrawAddElementsOperation: ToolOperation<unknown, ExcalidrawSceneResult>;
|
|
62
|
+
export declare const excalidrawUpdateElementsOperation: ToolOperation<unknown, ExcalidrawSceneResult>;
|
|
63
|
+
export declare const excalidrawDeleteElementsOperation: ToolOperation<unknown, ExcalidrawSceneResult>;
|
|
64
|
+
export declare const excalidrawConnectElementsOperation: ToolOperation<unknown, ExcalidrawSceneResult>;
|
|
65
|
+
export declare const excalidrawSetAppStateOperation: ToolOperation<unknown, ExcalidrawSceneResult>;
|
|
66
|
+
export declare const excalidrawResizeOperation: ToolOperation<unknown, ExcalidrawSceneResult>;
|
|
67
|
+
export declare const excalidrawClearSceneOperation: ToolOperation<unknown, ExcalidrawSceneResult>;
|
|
68
|
+
export declare const excalidrawFromMermaidOperation: ToolOperation<unknown, ExcalidrawInsertNodeResult>;
|
|
69
|
+
/**
|
|
70
|
+
* Every Excalidraw operation, keyed by the name its definition points at.
|
|
71
|
+
*/
|
|
72
|
+
export declare const excalidrawToolOperations: Record<string, ToolOperation<unknown, unknown>>;
|
|
73
|
+
//# sourceMappingURL=operations.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"operations.d.ts","sourceRoot":"","sources":["../src/operations.ts"],"names":[],"mappings":"AAMA;;;;;;;;;;;;;;;;GAgBG;AAEH,OAAO,EAGL,KAAK,aAAa,EACnB,MAAM,0BAA0B,CAAC;AAGlC,OAAO,KAAK,EAAE,wBAAwB,EAAE,MAAM,SAAS,CAAC;AAiEx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3C,CAAC;AAEJ,eAAO,MAAM,kCAAkC,+CAI5C,CAAC;AAEJ,eAAO,MAAM,8BAA8B,+CAIxC,CAAC;AAEJ,eAAO,MAAM,yBAAyB,+CAInC,CAAC;AAEJ,eAAO,MAAM,6BAA6B,+CAIvC,CAAC;AAEJ,eAAO,MAAM,8BAA8B,oDAIxC,CAAC;AAEJ;;GAEG;AACH,eAAO,MAAM,wBAAwB,EAAE,MAAM,CAC3C,MAAM,EACN,aAAa,CAAC,OAAO,EAAE,OAAO,CAAC,CAamB,CAAC"}
|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Copyright (c) 2026-Present Datalayer, Inc.
|
|
3
|
+
*
|
|
4
|
+
* BSD 3-Clause License
|
|
5
|
+
*/
|
|
6
|
+
/**
|
|
7
|
+
* The agent-facing half of the Excalidraw tools.
|
|
8
|
+
*
|
|
9
|
+
* Each one validates its parameters and hands them to the executor; the work
|
|
10
|
+
* happens in `handlers.ts`, against the host that keeps the drawings, which
|
|
11
|
+
* is usually a browser page or editor. These operations run wherever the
|
|
12
|
+
* agent runs.
|
|
13
|
+
*
|
|
14
|
+
* So: nothing in this module may import an editor or Excalidraw, directly or
|
|
15
|
+
* transitively. That is what keeps the tool list loadable in Node.
|
|
16
|
+
*
|
|
17
|
+
* The eleven of them are the same three lines with different names, which is
|
|
18
|
+
* why they are built rather than written out. Writing them out would be
|
|
19
|
+
* eleven chances to get the validation call wrong, and nothing gained.
|
|
20
|
+
*
|
|
21
|
+
* @module operations
|
|
22
|
+
*/
|
|
23
|
+
import { validateWithZod, } from '@datalayer/reactor/tools';
|
|
24
|
+
import { excalidrawAddElementsParamsSchema, excalidrawClearSceneParamsSchema, excalidrawConnectElementsParamsSchema, excalidrawDeleteElementsParamsSchema, excalidrawFromMermaidParamsSchema, excalidrawInsertNodeParamsSchema, excalidrawListDrawingsParamsSchema, excalidrawReadSceneParamsSchema, excalidrawResizeParamsSchema, excalidrawSetAppStateParamsSchema, excalidrawUpdateElementsParamsSchema, } from './schemas.js';
|
|
25
|
+
/**
|
|
26
|
+
* Build one operation.
|
|
27
|
+
*
|
|
28
|
+
* @param name - The operation name, which is also the handler's key and the
|
|
29
|
+
* `operation` field of the tool definition that points here.
|
|
30
|
+
* @param schema - What the tool takes.
|
|
31
|
+
*/
|
|
32
|
+
function excalidrawOperation(name, schema) {
|
|
33
|
+
return {
|
|
34
|
+
name,
|
|
35
|
+
async execute(params, context) {
|
|
36
|
+
const validated = validateWithZod(schema, params ?? {}, name);
|
|
37
|
+
if (!context.documentId) {
|
|
38
|
+
throw new Error(`Document ID is required for ${name}. ` +
|
|
39
|
+
'Ensure the tool execution context includes a valid documentId.');
|
|
40
|
+
}
|
|
41
|
+
if (!context.executor) {
|
|
42
|
+
throw new Error(`Executor is required for ${name}. ` +
|
|
43
|
+
'This should be provided by the platform (DefaultExecutor, BridgeExecutor, etc.)');
|
|
44
|
+
}
|
|
45
|
+
try {
|
|
46
|
+
return (await context.executor.execute(name, validated));
|
|
47
|
+
}
|
|
48
|
+
catch (error) {
|
|
49
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
50
|
+
throw new Error(`Failed to run ${name}: ${message}`, { cause: error });
|
|
51
|
+
}
|
|
52
|
+
},
|
|
53
|
+
};
|
|
54
|
+
}
|
|
55
|
+
// ============================================================================
|
|
56
|
+
// The operations
|
|
57
|
+
// ============================================================================
|
|
58
|
+
export const excalidrawInsertNodeOperation = excalidrawOperation('excalidrawInsertNode', excalidrawInsertNodeParamsSchema);
|
|
59
|
+
export const excalidrawListDrawingsOperation = excalidrawOperation('excalidrawListDrawings', excalidrawListDrawingsParamsSchema);
|
|
60
|
+
export const excalidrawReadSceneOperation = excalidrawOperation('excalidrawReadScene', excalidrawReadSceneParamsSchema);
|
|
61
|
+
export const excalidrawAddElementsOperation = excalidrawOperation('excalidrawAddElements', excalidrawAddElementsParamsSchema);
|
|
62
|
+
export const excalidrawUpdateElementsOperation = excalidrawOperation('excalidrawUpdateElements', excalidrawUpdateElementsParamsSchema);
|
|
63
|
+
export const excalidrawDeleteElementsOperation = excalidrawOperation('excalidrawDeleteElements', excalidrawDeleteElementsParamsSchema);
|
|
64
|
+
export const excalidrawConnectElementsOperation = excalidrawOperation('excalidrawConnectElements', excalidrawConnectElementsParamsSchema);
|
|
65
|
+
export const excalidrawSetAppStateOperation = excalidrawOperation('excalidrawSetAppState', excalidrawSetAppStateParamsSchema);
|
|
66
|
+
export const excalidrawResizeOperation = excalidrawOperation('excalidrawResize', excalidrawResizeParamsSchema);
|
|
67
|
+
export const excalidrawClearSceneOperation = excalidrawOperation('excalidrawClearScene', excalidrawClearSceneParamsSchema);
|
|
68
|
+
export const excalidrawFromMermaidOperation = excalidrawOperation('excalidrawFromMermaid', excalidrawFromMermaidParamsSchema);
|
|
69
|
+
/**
|
|
70
|
+
* Every Excalidraw operation, keyed by the name its definition points at.
|
|
71
|
+
*/
|
|
72
|
+
export const excalidrawToolOperations = {
|
|
73
|
+
excalidrawInsertNode: excalidrawInsertNodeOperation,
|
|
74
|
+
excalidrawListDrawings: excalidrawListDrawingsOperation,
|
|
75
|
+
excalidrawReadScene: excalidrawReadSceneOperation,
|
|
76
|
+
excalidrawAddElements: excalidrawAddElementsOperation,
|
|
77
|
+
excalidrawUpdateElements: excalidrawUpdateElementsOperation,
|
|
78
|
+
excalidrawDeleteElements: excalidrawDeleteElementsOperation,
|
|
79
|
+
excalidrawConnectElements: excalidrawConnectElementsOperation,
|
|
80
|
+
excalidrawSetAppState: excalidrawSetAppStateOperation,
|
|
81
|
+
excalidrawResize: excalidrawResizeOperation,
|
|
82
|
+
excalidrawClearScene: excalidrawClearSceneOperation,
|
|
83
|
+
excalidrawFromMermaid: excalidrawFromMermaidOperation,
|
|
84
|
+
};
|
|
85
|
+
//# sourceMappingURL=operations.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"operations.js","sourceRoot":"","sources":["../src/operations.ts"],"names":[],"mappings":"AAAA;;;;GAI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D,wBAAwB,EAAE,iCAAiC;IAC3D,yBAAyB,EAAE,kCAAkC;IAC7D,qBAAqB,EAAE,8BAA8B;IACrD,gBAAgB,EAAE,yBAAyB;IAC3C,oBAAoB,EAAE,6BAA6B;IACnD,qBAAqB,EAAE,8BAA8B;CACH,CAAC"}
|
package/lib/plugin.d.ts
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import type { ExcalidrawConverters, ExcalidrawDrawingHost } from './host';
|
|
2
|
+
export declare const EXCALIDRAW_PLUGIN_NAME = "@datalayer/reactor-excalidraw";
|
|
3
|
+
export type ExcalidrawPluginConfig = {
|
|
4
|
+
/** What keeps the drawings. Without one, every command says so. */
|
|
5
|
+
host: ExcalidrawDrawingHost | null;
|
|
6
|
+
/** Excalidraw's conversions. Without them, every command says so. */
|
|
7
|
+
converters: ExcalidrawConverters | null;
|
|
8
|
+
};
|
|
9
|
+
export declare const ExcalidrawPlugin: import("@datalayer/reactor").ReactorPlugin<ExcalidrawPluginConfig, unknown, unknown>;
|
|
10
|
+
//# sourceMappingURL=plugin.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"plugin.d.ts","sourceRoot":"","sources":["../src/plugin.ts"],"names":[],"mappings":"AA8BA,OAAO,KAAK,EAAE,oBAAoB,EAAE,qBAAqB,EAAE,MAAM,QAAQ,CAAC;AAS1E,eAAO,MAAM,sBAAsB,kCAAkC,CAAC;AAEtE,MAAM,MAAM,sBAAsB,GAAG;IACnC,mEAAmE;IACnE,IAAI,EAAE,qBAAqB,GAAG,IAAI,CAAC;IACnC,qEAAqE;IACrE,UAAU,EAAE,oBAAoB,GAAG,IAAI,CAAC;CACzC,CAAC;AAEF,eAAO,MAAM,gBAAgB,sFAuC3B,CAAC"}
|
package/lib/plugin.js
ADDED
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Copyright (c) 2026-Present Datalayer, Inc.
|
|
3
|
+
*
|
|
4
|
+
* BSD 3-Clause License
|
|
5
|
+
*/
|
|
6
|
+
/**
|
|
7
|
+
* The reactor plugin: the Excalidraw tools as commands, and the bundle that
|
|
8
|
+
* offers them to an agent.
|
|
9
|
+
*
|
|
10
|
+
* Each tool's operation is registered as a command, `excalidraw.<operation>`,
|
|
11
|
+
* which validates its argument and runs it against the host the plugin was
|
|
12
|
+
* configured with; the plugin's `AgentTools` bundle names those commands, so
|
|
13
|
+
* a host that mounts the plugin hands an agent the tools without declaring
|
|
14
|
+
* any.
|
|
15
|
+
*
|
|
16
|
+
* ```ts
|
|
17
|
+
* configurePlugin(ExcalidrawPlugin, {
|
|
18
|
+
* host: createMemoryDrawingHost(),
|
|
19
|
+
* converters: { convertToExcalidrawElements, parseMermaidToExcalidraw },
|
|
20
|
+
* });
|
|
21
|
+
* ```
|
|
22
|
+
*
|
|
23
|
+
* @module plugin
|
|
24
|
+
*/
|
|
25
|
+
import { AgentTools, contribution, definePlugin } from '@datalayer/reactor';
|
|
26
|
+
import { excalidrawToolDefinitions } from './definitions.js';
|
|
27
|
+
import { createExcalidrawToolHandlers } from './handlers.js';
|
|
28
|
+
import { excalidrawToolOperations } from './operations.js';
|
|
29
|
+
import { createExcalidrawExecutor, EXCALIDRAW_AGENT_TOOLS, EXCALIDRAW_TOOLS_NAME, excalidrawCommandOf, } from './tools.js';
|
|
30
|
+
export const EXCALIDRAW_PLUGIN_NAME = '@datalayer/reactor-excalidraw';
|
|
31
|
+
export const ExcalidrawPlugin = definePlugin({
|
|
32
|
+
name: EXCALIDRAW_PLUGIN_NAME,
|
|
33
|
+
displayName: 'Excalidraw',
|
|
34
|
+
description: 'Excalidraw drawings as tools an agent draws with.',
|
|
35
|
+
octicon: 'paintbrush',
|
|
36
|
+
emoji: '\u{1F58C}\u{FE0F}',
|
|
37
|
+
config: { host: null, converters: null },
|
|
38
|
+
contributes: [contribution(AgentTools, EXCALIDRAW_AGENT_TOOLS, { id: EXCALIDRAW_TOOLS_NAME })],
|
|
39
|
+
register: ({ config, registerCommand }) => {
|
|
40
|
+
const { host, converters } = config;
|
|
41
|
+
const executor = host && converters
|
|
42
|
+
? createExcalidrawExecutor(createExcalidrawToolHandlers(host, converters))
|
|
43
|
+
: null;
|
|
44
|
+
const undo = excalidrawToolDefinitions.map((definition) => registerCommand({
|
|
45
|
+
id: excalidrawCommandOf(definition.operation),
|
|
46
|
+
name: definition.displayName,
|
|
47
|
+
description: definition.description,
|
|
48
|
+
category: 'Excalidraw',
|
|
49
|
+
execute: (argument) => {
|
|
50
|
+
if (!executor) {
|
|
51
|
+
throw new Error(`${EXCALIDRAW_PLUGIN_NAME} was configured without a drawing host and converters.`);
|
|
52
|
+
}
|
|
53
|
+
return excalidrawToolOperations[definition.operation].execute(argument ?? {}, {
|
|
54
|
+
executor,
|
|
55
|
+
documentId: EXCALIDRAW_TOOLS_NAME,
|
|
56
|
+
});
|
|
57
|
+
},
|
|
58
|
+
}));
|
|
59
|
+
return () => {
|
|
60
|
+
for (const dispose of undo) {
|
|
61
|
+
dispose();
|
|
62
|
+
}
|
|
63
|
+
};
|
|
64
|
+
},
|
|
65
|
+
});
|
|
66
|
+
//# sourceMappingURL=plugin.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"plugin.js","sourceRoot":"","sources":["../src/plugin.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH;;;;;;;;;;;;;;;;;;GAkBG;AAEH,OAAO,EAAE,UAAU,EAAE,YAAY,EAAE,YAAY,EAAE,MAAM,oBAAoB,CAAC;AAE5E,OAAO,EAAE,yBAAyB,EAAE,MAAM,eAAe,CAAC;AAC1D,OAAO,EAAE,4BAA4B,EAAE,MAAM,YAAY,CAAC;AAE1D,OAAO,EAAE,wBAAwB,EAAE,MAAM,cAAc,CAAC;AACxD,OAAO,EACL,wBAAwB,EACxB,sBAAsB,EACtB,qBAAqB,EACrB,mBAAmB,GACpB,MAAM,SAAS,CAAC;AAEjB,MAAM,CAAC,MAAM,sBAAsB,GAAG,+BAA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AAC,CACH,CAAC;QACF,OAAO,GAAG,EAAE;YACV,KAAK,MAAM,OAAO,IAAI,IAAI,EAAE,CAAC;gBAC3B,OAAO,EAAE,CAAC;YACZ,CAAC;QACH,CAAC,CAAC;IACJ,CAAC;CACF,CAAC,CAAC"}
|
package/lib/scene.d.ts
ADDED
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A drawing, read and written as data.
|
|
3
|
+
*
|
|
4
|
+
* A host keeps a whole scene as one JSON string — a Lexical `ExcalidrawNode`
|
|
5
|
+
* does, a page's canvas does — and the tools need to read it, change part of
|
|
6
|
+
* it and write it back. The parsing lives here once, rather than once per
|
|
7
|
+
* host with a set of assumptions each.
|
|
8
|
+
*
|
|
9
|
+
* **Two shapes, one reader.** A drawing created empty may be the string
|
|
10
|
+
* `'[]'`, and a drawing saved from Excalidraw holds
|
|
11
|
+
* `{elements, appState, files}`. Both are found in real documents, so both
|
|
12
|
+
* are read here and only the second is ever written.
|
|
13
|
+
*
|
|
14
|
+
* Nothing in this module imports Excalidraw or any editor: it is arithmetic
|
|
15
|
+
* on plain objects, which is what lets the operations that summarise a scene
|
|
16
|
+
* run outside a browser.
|
|
17
|
+
*
|
|
18
|
+
* @module scene
|
|
19
|
+
*/
|
|
20
|
+
/** An Excalidraw element, as it sits in the scene's JSON. */
|
|
21
|
+
export type ExcalidrawElementData = Record<string, any>;
|
|
22
|
+
/**
|
|
23
|
+
* A whole drawing: what is in it, how the canvas is set up, and any images it
|
|
24
|
+
* embeds.
|
|
25
|
+
*/
|
|
26
|
+
export interface ExcalidrawScene {
|
|
27
|
+
elements: ExcalidrawElementData[];
|
|
28
|
+
appState: Record<string, any>;
|
|
29
|
+
files: Record<string, any>;
|
|
30
|
+
}
|
|
31
|
+
/** An empty drawing. */
|
|
32
|
+
export declare const emptyScene: () => ExcalidrawScene;
|
|
33
|
+
/**
|
|
34
|
+
* Read a drawing's stored string.
|
|
35
|
+
*
|
|
36
|
+
* Tolerant on purpose: a drawing that cannot be parsed reads as an empty one
|
|
37
|
+
* rather than throwing, because the alternative is a tool call that fails on
|
|
38
|
+
* a document the editor itself opens quite happily.
|
|
39
|
+
*/
|
|
40
|
+
export declare function parseScene(data: string | undefined | null): ExcalidrawScene;
|
|
41
|
+
/** Write a scene back to the string a host stores. */
|
|
42
|
+
export declare function serializeScene(scene: ExcalidrawScene): string;
|
|
43
|
+
/** The elements that are actually on the canvas. */
|
|
44
|
+
export declare const liveElements: (scene: ExcalidrawScene) => ExcalidrawElementData[];
|
|
45
|
+
/**
|
|
46
|
+
* One element, reduced to what somebody deciding what to do next needs.
|
|
47
|
+
*
|
|
48
|
+
* A raw Excalidraw element carries about thirty fields, most of them about
|
|
49
|
+
* how it is drawn rather than what it is — seeds, version nonces, fractional
|
|
50
|
+
* indices, roughness. Handing all of that to a model for every element buries
|
|
51
|
+
* the two things it actually needs, which are the id it can address the
|
|
52
|
+
* element by and where the element sits.
|
|
53
|
+
*/
|
|
54
|
+
export interface ExcalidrawElementSummary {
|
|
55
|
+
id: string;
|
|
56
|
+
type: string;
|
|
57
|
+
x: number;
|
|
58
|
+
y: number;
|
|
59
|
+
width: number;
|
|
60
|
+
height: number;
|
|
61
|
+
/** The element's own text, or the text bound inside it. */
|
|
62
|
+
text?: string;
|
|
63
|
+
strokeColor?: string;
|
|
64
|
+
backgroundColor?: string;
|
|
65
|
+
/** For bound text: the element it sits inside. */
|
|
66
|
+
containerId?: string;
|
|
67
|
+
/** For arrows: what they are attached to. */
|
|
68
|
+
startBoundElementId?: string;
|
|
69
|
+
endBoundElementId?: string;
|
|
70
|
+
/** Groups the element belongs to, if any. */
|
|
71
|
+
groupIds?: string[];
|
|
72
|
+
}
|
|
73
|
+
/** Reduce one element to its summary. */
|
|
74
|
+
export declare function summarizeElement(element: ExcalidrawElementData): ExcalidrawElementSummary;
|
|
75
|
+
/**
|
|
76
|
+
* The text a container shows, gathered from the element bound inside it.
|
|
77
|
+
*
|
|
78
|
+
* Excalidraw does not put a label on the shape; it makes a separate text
|
|
79
|
+
* element whose `containerId` points back. Reading a rectangle on its own
|
|
80
|
+
* therefore says nothing about what the rectangle is called, which is
|
|
81
|
+
* usually the only thing worth knowing about it.
|
|
82
|
+
*/
|
|
83
|
+
export declare function withBoundText(elements: ExcalidrawElementData[]): ExcalidrawElementSummary[];
|
|
84
|
+
/**
|
|
85
|
+
* A one-line description of a drawing, for a listing.
|
|
86
|
+
*
|
|
87
|
+
* What is in it and what it says — "3 rectangles, 2 arrows: Start, Finish" —
|
|
88
|
+
* which is enough to pick the right drawing out of a document holding
|
|
89
|
+
* several.
|
|
90
|
+
*/
|
|
91
|
+
export declare function describeScene(scene: ExcalidrawScene): string;
|
|
92
|
+
/**
|
|
93
|
+
* Stamp an element as changed.
|
|
94
|
+
*
|
|
95
|
+
* Excalidraw reconciles concurrent edits by comparing `version` and, when
|
|
96
|
+
* those tie, `versionNonce`. An element written back with its old version is
|
|
97
|
+
* an element a collaborating client is entitled to discard, so every
|
|
98
|
+
* mutation goes through here.
|
|
99
|
+
*/
|
|
100
|
+
export declare function touchElement(element: ExcalidrawElementData): ExcalidrawElementData;
|
|
101
|
+
//# sourceMappingURL=scene.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"scene.d.ts","sourceRoot":"","sources":["../src/scene.ts"],"names":[],"mappings":"AAMA;;;;;;;;;;;;;;;;;;GAkBG;AAEH,6DAA6D;AAC7D,MAAM,MAAM,qBAAqB,GAAG,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,CAAC;AAExD;;;GAGG;AACH,MAAM,WAAW,eAAe;IAC9B,QAAQ,EAAE,qBAAqB,EAAE,CAAC;IAClC,QAAQ,EAAE,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,CAAC;IAC9B,KAAK,EAAE,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,CAAC;CAC5B;AAED,wBAAwB;AACxB,eAAO,MAAM,UAAU,QAAO,eAI5B,CAAC;AAEH;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