@statelyai/sdk 0.2.0 → 0.3.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/dist/graph.mjs ADDED
@@ -0,0 +1,344 @@
1
+ import { createFormatConverter, createGraph } from "@statelyai/graph";
2
+
3
+ //#region src/graph.ts
4
+ function toJsonObject(value) {
5
+ return value;
6
+ }
7
+ function toUnknownRecord(value) {
8
+ return value;
9
+ }
10
+ function normalizeStudioNodeType(type) {
11
+ return type === "annotation" ? void 0 : type;
12
+ }
13
+ function normalizeSchemas(schemas) {
14
+ if (!schemas) return;
15
+ return {
16
+ input: schemas.input ?? null,
17
+ output: schemas.output ?? null,
18
+ context: schemas.context ?? {},
19
+ events: schemas.events ?? {},
20
+ actions: schemas.actions ?? {},
21
+ guards: schemas.guards ?? {},
22
+ actors: schemas.actors ?? {},
23
+ tags: schemas.tags ?? {},
24
+ delays: schemas.delays ?? {}
25
+ };
26
+ }
27
+ function toCanvasColor(color) {
28
+ return color;
29
+ }
30
+ function normalizeStudioHistory(history) {
31
+ if (history === true) return "shallow";
32
+ return history || void 0;
33
+ }
34
+ function toActionSource(action) {
35
+ return {
36
+ type: "action",
37
+ id: action.id,
38
+ name: action.name,
39
+ ...action.description ? { description: action.description } : {},
40
+ ...action.icon ? { icon: action.icon } : {},
41
+ lang: action.code?.lang ?? "js",
42
+ imports: [],
43
+ code: action.code?.body,
44
+ ...action.paramsSchema ? { paramsSchema: action.paramsSchema } : {}
45
+ };
46
+ }
47
+ function toGuardSource(guard) {
48
+ return {
49
+ type: "guard",
50
+ id: guard.id,
51
+ name: guard.name,
52
+ ...guard.description ? { description: guard.description } : {},
53
+ ...guard.icon ? { icon: guard.icon } : {},
54
+ lang: guard.code?.lang ?? "js",
55
+ imports: [],
56
+ code: guard.code?.body,
57
+ ...guard.paramsSchema ? { paramsSchema: guard.paramsSchema } : {}
58
+ };
59
+ }
60
+ function toActorSource(actor) {
61
+ return {
62
+ type: "actor",
63
+ id: actor.id,
64
+ name: actor.name,
65
+ ...actor.description ? { description: actor.description } : {},
66
+ ...actor.icon ? { icon: actor.icon } : {},
67
+ lang: actor.code?.lang ?? "js",
68
+ imports: [],
69
+ code: actor.code?.body,
70
+ kind: "named",
71
+ ...actor.inputSchema ? { inputSchema: actor.inputSchema } : {},
72
+ ...actor.outputSchema ? { outputSchema: actor.outputSchema } : {}
73
+ };
74
+ }
75
+ function fromActionSource(action) {
76
+ return {
77
+ id: action.id,
78
+ name: action.name,
79
+ description: action.description ?? null,
80
+ ...action.icon ? { icon: action.icon } : {},
81
+ paramsSchema: action.paramsSchema ?? null,
82
+ ...action.code ? { code: {
83
+ body: action.code,
84
+ ...action.lang ? { lang: action.lang } : {}
85
+ } } : {}
86
+ };
87
+ }
88
+ function fromGuardSource(guard) {
89
+ return {
90
+ id: guard.id,
91
+ name: guard.name,
92
+ description: guard.description ?? null,
93
+ ...guard.icon ? { icon: guard.icon } : {},
94
+ paramsSchema: guard.paramsSchema ?? null,
95
+ ...guard.code ? { code: {
96
+ body: guard.code,
97
+ ...guard.lang ? { lang: guard.lang } : {}
98
+ } } : {}
99
+ };
100
+ }
101
+ function fromActorSource(actor) {
102
+ return {
103
+ id: actor.id,
104
+ name: actor.name,
105
+ description: actor.description ?? null,
106
+ ...actor.icon ? { icon: actor.icon } : {},
107
+ inputSchema: actor.inputSchema ?? null,
108
+ outputSchema: actor.outputSchema ?? null,
109
+ ...actor.code ? { code: {
110
+ body: actor.code,
111
+ ...actor.lang ? { lang: actor.lang } : {}
112
+ } } : {}
113
+ };
114
+ }
115
+ function toStudioAction(action) {
116
+ return {
117
+ kind: "named",
118
+ action: {
119
+ type: action.type,
120
+ ...action.params && Object.keys(action.params).length > 0 ? { params: toJsonObject(action.params) } : {}
121
+ }
122
+ };
123
+ }
124
+ function fromStudioAction(action) {
125
+ if (action.kind === "inline") return {
126
+ type: "inline",
127
+ params: { expr: action.action.expr }
128
+ };
129
+ if ("params" in action.action && action.action.params) return {
130
+ type: action.action.type,
131
+ params: toUnknownRecord(action.action.params)
132
+ };
133
+ return { type: action.action.type };
134
+ }
135
+ function toStudioEventTypeData(eventType) {
136
+ if (eventType === "") return { type: "always" };
137
+ if (eventType === "*") return { type: "wildcard" };
138
+ if (eventType.startsWith("xstate.after.")) return {
139
+ type: "after",
140
+ delay: eventType.slice(13).split(".")[0] ?? ""
141
+ };
142
+ return {
143
+ type: "named",
144
+ eventType
145
+ };
146
+ }
147
+ function fromStudioEventTypeData(eventTypeData) {
148
+ switch (eventTypeData.type) {
149
+ case "always": return "";
150
+ case "wildcard": return "*";
151
+ case "after": return `xstate.after.${eventTypeData.delay ?? ""}`;
152
+ case "named": return eventTypeData.eventType ?? "";
153
+ case "invocation.done": return `xstate.done.actor.${eventTypeData.invocationId}`;
154
+ case "invocation.error": return `xstate.error.actor.${eventTypeData.invocationId}`;
155
+ case "state.done": return "xstate.done.state";
156
+ case "init": return "xstate.init";
157
+ default: return "";
158
+ }
159
+ }
160
+ function toStudioNode(graph, nodeId, pathIds, parentPath) {
161
+ const node = graph.nodes.find((candidate) => candidate.id === nodeId);
162
+ if (!node) throw new Error(`Node not found: ${nodeId}`);
163
+ const pathId = parentPath ? `${parentPath}.${node.data.key}` : node.data.key;
164
+ pathIds.set(node.id, pathId);
165
+ const childNodes = graph.nodes.filter((candidate) => candidate.parentId === node.id).map((child) => toStudioNode(graph, child.id, pathIds, pathId));
166
+ const initialKey = node.data.initialId ? graph.nodes.find((candidate) => candidate.id === node.data.initialId)?.data.key : void 0;
167
+ return {
168
+ id: pathId,
169
+ ...node.x !== void 0 && node.y !== void 0 ? { position: {
170
+ x: node.x,
171
+ y: node.y
172
+ } } : {},
173
+ ...node.width !== void 0 && node.height !== void 0 ? { size: {
174
+ width: node.width,
175
+ height: node.height
176
+ } } : {},
177
+ data: {
178
+ key: node.data.key,
179
+ ...normalizeStudioNodeType(node.data.type) ? { type: normalizeStudioNodeType(node.data.type) } : {},
180
+ ...normalizeStudioHistory(node.data.history) ? { history: normalizeStudioHistory(node.data.history) } : {},
181
+ ...initialKey ? { initial: initialKey } : {},
182
+ entry: (node.data.entry ?? []).map(toStudioAction),
183
+ exit: (node.data.exit ?? []).map(toStudioAction),
184
+ invoke: (node.data.invokes ?? []).map((invoke) => ({
185
+ src: invoke.src,
186
+ id: invoke.id,
187
+ input: toJsonObject(invoke.input),
188
+ settings: {},
189
+ kind: invoke.src.startsWith("inline:") ? "inline" : "named"
190
+ })),
191
+ tags: node.data.tags ?? [],
192
+ ...node.data.description ? { description: node.data.description } : {},
193
+ ...node.data.color ? { color: toCanvasColor(node.data.color) } : {},
194
+ ...node.data.meta ? { metaEntries: Object.entries(node.data.meta) } : {}
195
+ },
196
+ nodes: childNodes
197
+ };
198
+ }
199
+ function flattenStudioNodes(studioNode, parentId, nodes, pathIdToNodeId) {
200
+ const nodeId = studioNode.id;
201
+ pathIdToNodeId.set(studioNode.id, nodeId);
202
+ const initialId = studioNode.data.initial ? `${studioNode.id}.${studioNode.data.initial}` : void 0;
203
+ nodes.push({
204
+ type: "node",
205
+ id: nodeId,
206
+ parentId,
207
+ label: studioNode.data.key,
208
+ ...initialId ? { initialNodeId: initialId } : {},
209
+ ...studioNode.position ? {
210
+ x: studioNode.position.x,
211
+ y: studioNode.position.y
212
+ } : {},
213
+ ...studioNode.size ? {
214
+ width: studioNode.size.width,
215
+ height: studioNode.size.height
216
+ } : {},
217
+ data: {
218
+ key: studioNode.data.key,
219
+ ...normalizeStudioNodeType(studioNode.data.type) ? { type: normalizeStudioNodeType(studioNode.data.type) } : {},
220
+ ...studioNode.data.history ? { history: studioNode.data.history } : {},
221
+ ...initialId ? { initialId } : {},
222
+ entry: studioNode.data.entry.map(fromStudioAction),
223
+ exit: studioNode.data.exit.map(fromStudioAction),
224
+ invokes: studioNode.data.invoke.map((invoke) => ({
225
+ src: invoke.src,
226
+ id: invoke.id,
227
+ input: toUnknownRecord(invoke.input)
228
+ })),
229
+ tags: studioNode.data.tags,
230
+ ...studioNode.data.description ? { description: studioNode.data.description } : {},
231
+ ...studioNode.data.color ? { color: studioNode.data.color } : {},
232
+ ...studioNode.data.metaEntries ? { meta: Object.fromEntries(studioNode.data.metaEntries) } : {}
233
+ }
234
+ });
235
+ studioNode.nodes.forEach((child) => flattenStudioNodes(child, nodeId, nodes, pathIdToNodeId));
236
+ }
237
+ function toStudioMachine(graph) {
238
+ const rootNode = graph.nodes.find((node) => node.parentId == null);
239
+ if (!rootNode) throw new Error("No root node found in graph");
240
+ const pathIds = /* @__PURE__ */ new Map();
241
+ const studioRootNode = toStudioNode(graph, rootNode.id, pathIds);
242
+ const edgeGroupCounts = /* @__PURE__ */ new Map();
243
+ const studioEdges = graph.edges.map((edge) => {
244
+ const source = pathIds.get(edge.sourceId) ?? edge.sourceId;
245
+ const target = edge.targetId ? pathIds.get(edge.targetId) ?? edge.targetId : void 0;
246
+ const eventType = edge.data.eventType;
247
+ const groupKey = `${source}:${eventType}`;
248
+ const index = edgeGroupCounts.get(groupKey) ?? 0;
249
+ edgeGroupCounts.set(groupKey, index + 1);
250
+ return {
251
+ id: `${source}#${eventType}[${index}]`,
252
+ source,
253
+ target: target && edge.data.transitionType !== "targetless" ? target : void 0,
254
+ ...edge.x !== void 0 && edge.y !== void 0 ? { position: {
255
+ x: edge.x,
256
+ y: edge.y
257
+ } } : {},
258
+ ...edge.width !== void 0 && edge.height !== void 0 ? { size: {
259
+ width: edge.width,
260
+ height: edge.height
261
+ } } : {},
262
+ data: {
263
+ eventTypeData: toStudioEventTypeData(eventType),
264
+ actions: (edge.data.actions ?? []).map(toStudioAction),
265
+ ...edge.data.guard ? { guard: {
266
+ kind: edge.data.guard.code ? "inline" : "named",
267
+ type: edge.data.guard.type,
268
+ params: toJsonObject(edge.data.guard.params ?? {})
269
+ } } : {},
270
+ ...edge.data.description ? { description: edge.data.description } : {},
271
+ ...edge.data.transitionType === "reenter" ? { internal: false } : {},
272
+ ...edge.data.color ? { color: toCanvasColor(edge.data.color) } : {},
273
+ ...edge.data.meta ? { metaEntries: Object.entries(edge.data.meta) } : {}
274
+ }
275
+ };
276
+ });
277
+ return {
278
+ id: graph.id,
279
+ rootNode: studioRootNode,
280
+ edges: studioEdges,
281
+ context: {},
282
+ ...graph.data?.schemas ? { schemas: normalizeSchemas(graph.data.schemas) } : {},
283
+ ...graph.data?.implementations ? { implementations: {
284
+ actions: Object.fromEntries(graph.data.implementations.actions.map((action) => [action.name, toActionSource(action)])),
285
+ guards: Object.fromEntries(graph.data.implementations.guards.map((guard) => [guard.name, toGuardSource(guard)])),
286
+ actors: Object.fromEntries(graph.data.implementations.actors.map((actor) => [actor.name, toActorSource(actor)]))
287
+ } } : {}
288
+ };
289
+ }
290
+ function fromStudioMachine(studioMachine) {
291
+ const nodes = [];
292
+ const pathIdToNodeId = /* @__PURE__ */ new Map();
293
+ flattenStudioNodes(studioMachine.rootNode, null, nodes, pathIdToNodeId);
294
+ const edges = studioMachine.edges.map((edge) => ({
295
+ type: "edge",
296
+ id: edge.id,
297
+ sourceId: pathIdToNodeId.get(edge.source) ?? edge.source,
298
+ targetId: edge.target ? pathIdToNodeId.get(edge.target) ?? edge.target : pathIdToNodeId.get(edge.source) ?? edge.source,
299
+ label: fromStudioEventTypeData(edge.data.eventTypeData),
300
+ ...edge.position ? {
301
+ x: edge.position.x,
302
+ y: edge.position.y
303
+ } : {},
304
+ ...edge.size ? {
305
+ width: edge.size.width,
306
+ height: edge.size.height
307
+ } : {},
308
+ data: {
309
+ eventType: fromStudioEventTypeData(edge.data.eventTypeData),
310
+ transitionType: edge.target ? "normal" : "targetless",
311
+ ...edge.data.guard ? { guard: {
312
+ type: edge.data.guard.type,
313
+ params: toUnknownRecord(edge.data.guard.params),
314
+ ...edge.data.guard.kind === "inline" ? { code: edge.data.guard.type } : {}
315
+ } } : {},
316
+ actions: edge.data.actions.map(fromStudioAction),
317
+ ...edge.data.description ? { description: edge.data.description } : {},
318
+ ...edge.data.color ? { color: edge.data.color } : {},
319
+ ...edge.data.metaEntries ? { meta: Object.fromEntries(edge.data.metaEntries) } : {}
320
+ }
321
+ }));
322
+ return createGraph({
323
+ id: studioMachine.id,
324
+ nodes: nodes.map(({ type: _type, initialNodeId, ...node }) => ({
325
+ ...node,
326
+ ...initialNodeId ? { initialNodeId } : {}
327
+ })),
328
+ edges: edges.map(({ type: _type, ...edge }) => edge),
329
+ data: {
330
+ ...studioMachine.schemas ? { schemas: studioMachine.schemas } : {},
331
+ ...studioMachine.implementations ? { implementations: {
332
+ actions: Object.values(studioMachine.implementations.actions).map(fromActionSource),
333
+ guards: Object.values(studioMachine.implementations.guards).map(fromGuardSource),
334
+ actors: Object.values(studioMachine.implementations.actors).map(fromActorSource),
335
+ delays: [],
336
+ tags: []
337
+ } } : {}
338
+ }
339
+ });
340
+ }
341
+ const studioMachineConverter = createFormatConverter(toStudioMachine, fromStudioMachine);
342
+
343
+ //#endregion
344
+ export { fromStudioMachine, studioMachineConverter, toStudioMachine };
package/dist/index.d.mts CHANGED
@@ -1,45 +1,10 @@
1
- import { a as EmbedEventHandler, c as EmbedMode, d as ExportFormatMap, f as InitOptions, i as createInspector, l as ExportCallOptions, n as InspectOptions, o as EmbedEventMap, p as ProtocolMessage, r as Inspector, s as EmbedEventName, t as CreateInspectorOptions, u as ExportFormat } from "./inspect-Bt5Ohkrp.mjs";
1
+ import { a as ExportCallOptions, c as InitOptions, i as EmbedMode, l as ProtocolMessage, n as EmbedEventMap, o as ExportFormat, r as EmbedEventName, s as ExportFormatMap, t as EmbedEventHandler } from "./protocol-DTIQmjHH.mjs";
2
+ import { StatelyEmbed, StatelyEmbedOptions, createStatelyEmbed } from "./embed.mjs";
3
+ import { C as EventTypeData, S as DigraphNodeConfig, _ as studioMachineConverter, a as StatelyGraphData, b as DigraphConfig, c as StatelyInvoke, d as StudioAction, f as StudioEdge, g as fromStudioMachine, h as StudioNode, i as StatelyGraph, l as StatelyNodeData, m as StudioMachine, o as StatelyGuard, r as StatelyEdgeData, t as StatelyAction, v as toStudioMachine, w as StateNodeJSONData, x as DigraphEdgeConfig, y as DigraphAction } from "./graph-BTUkUCsl.mjs";
4
+ import { CreateInspectorOptions, InspectOptions, Inspector, createStatelyInspector } from "./inspect.mjs";
5
+ import { ExtractMachinesResponse, ExtractedMachine, GetMachineOptions, ProjectData, ProjectMachine, StudioApiError, StudioClient, StudioClientOptions, VerifyApiKeyResponse, createStatelyClient } from "./studio.mjs";
6
+ import { PlanSyncOptions, PullSyncResult, ResolvedSyncInput, SyncInputFormat, SyncPlan, SyncPlanSummary } from "./sync.mjs";
2
7
 
3
- //#region src/embed.d.ts
4
- interface StatelyEmbedOptions {
5
- baseUrl: string;
6
- apiKey?: string;
7
- origin?: string;
8
- onReady?: () => void;
9
- onLoaded?: (graph: unknown) => void;
10
- onChange?: (graph: unknown, machineConfig: unknown) => void;
11
- onSave?: (graph: unknown, machineConfig: unknown) => void;
12
- onError?: (error: {
13
- code: string;
14
- message: string;
15
- }) => void;
16
- }
17
- interface StatelyEmbed {
18
- /** Attach to an existing iframe element. Sets src and begins handshake. */
19
- attach(iframe: HTMLIFrameElement): void;
20
- /** Create an iframe and mount it into a container element. */
21
- mount(container: HTMLElement): HTMLIFrameElement;
22
- /** Send init message (queued if iframe not ready yet). */
23
- init(options: InitOptions): void;
24
- /** Update the machine (shorthand for update message). */
25
- updateMachine(machine: unknown, format?: string): void;
26
- /** Change the embed mode. */
27
- setMode(mode: EmbedMode): void;
28
- /** Change the embed theme. */
29
- setTheme(theme: 'light' | 'dark'): void;
30
- /** Export the current machine in a given format. Returns a promise. */
31
- export<F extends ExportFormat>(format: F, options?: ExportCallOptions<F>): Promise<ExportFormatMap[F]['result']>;
32
- /** Subscribe to an embed event. */
33
- on<K extends EmbedEventName>(event: K, handler: EmbedEventHandler<K>): void;
34
- /** Unsubscribe from an embed event. */
35
- off<K extends EmbedEventName>(event: K, handler: EmbedEventHandler<K>): void;
36
- /** Show a toast message in the embed. */
37
- toast(message: string, toastType?: 'success' | 'error' | 'info' | 'warning'): void;
38
- /** Tear down: remove listener, reject pending promises, optionally remove iframe. */
39
- destroy(): void;
40
- }
41
- declare function createStatelyEmbed(options: StatelyEmbedOptions): StatelyEmbed;
42
- //#endregion
43
8
  //#region src/transport.d.ts
44
9
  interface Transport {
45
10
  send(msg: ProtocolMessage): void;
@@ -70,4 +35,4 @@ interface WebSocketTransportOptions {
70
35
  }
71
36
  declare function createWebSocketTransport(options: WebSocketTransportOptions): Transport;
72
37
  //#endregion
73
- export { type CreateInspectorOptions, type EmbedEventHandler, type EmbedEventMap, type EmbedEventName, type EmbedMode, type ExportCallOptions, type ExportFormat, type ExportFormatMap, type InitOptions, type InspectOptions, type Inspector, type StatelyEmbed, type StatelyEmbedOptions, type Transport, createInspector, createPostMessageTransport, createStatelyEmbed, createWebSocketTransport };
38
+ export { type CreateInspectorOptions, type DigraphAction, type DigraphConfig, type DigraphEdgeConfig, type DigraphNodeConfig, type EmbedEventHandler, type EmbedEventMap, type EmbedEventName, type EmbedMode, type EventTypeData, type ExportCallOptions, type ExportFormat, type ExportFormatMap, type ExtractMachinesResponse, type ExtractedMachine, type GetMachineOptions, type InitOptions, type InspectOptions, type Inspector, type PlanSyncOptions, type ProjectData, type ProjectMachine, type PullSyncResult, type ResolvedSyncInput, type StateNodeJSONData, type StatelyAction, type StatelyEdgeData, type StatelyEmbed, type StatelyEmbedOptions, type StatelyGraph, type StatelyGraphData, type StatelyGuard, type StatelyInvoke, type StatelyNodeData, type StudioAction, StudioApiError, type StudioClient, type StudioClientOptions, type StudioEdge, type StudioMachine, type StudioNode, type SyncInputFormat, type SyncPlan, type SyncPlanSummary, type Transport, type VerifyApiKeyResponse, createPostMessageTransport, createStatelyClient, createStatelyEmbed, createStatelyInspector, createWebSocketTransport, fromStudioMachine, studioMachineConverter, toStudioMachine };
package/dist/index.mjs CHANGED
@@ -1,163 +1,7 @@
1
- import { a as createPendingExportManager, i as createEventRegistry, n as createPostMessageTransport, o as toInitMessage, r as createWebSocketTransport, t as createInspector } from "./inspect-C3Rjgn1o.mjs";
1
+ import { n as createWebSocketTransport, t as createPostMessageTransport } from "./transport-D352iKKa.mjs";
2
+ import { createStatelyEmbed } from "./embed.mjs";
3
+ import { createStatelyInspector } from "./inspect.mjs";
4
+ import { StudioApiError, createStatelyClient } from "./studio.mjs";
5
+ import { fromStudioMachine, studioMachineConverter, toStudioMachine } from "./graph.mjs";
2
6
 
3
- //#region src/embed.ts
4
- function createStatelyEmbed(options) {
5
- const base = options.baseUrl.replace(/\/+$/, "") + "/embed";
6
- const embedUrl = options.apiKey ? `${base}?api_key=${encodeURIComponent(options.apiKey)}` : base;
7
- const targetOrigin = options.origin ?? new URL(embedUrl).origin;
8
- let iframe = null;
9
- let transport = null;
10
- let ownedIframe = false;
11
- let destroyed = false;
12
- const pendingMessages = [];
13
- const events = createEventRegistry();
14
- const exportManager = createPendingExportManager((message) => send(message));
15
- function send(msg) {
16
- if (!transport?.ready) {
17
- pendingMessages.push(msg);
18
- return;
19
- }
20
- transport.send(msg);
21
- }
22
- function flush() {
23
- if (!transport) return;
24
- while (pendingMessages.length > 0) {
25
- const msg = pendingMessages.shift();
26
- transport.send(msg);
27
- }
28
- }
29
- function handleMessage(data) {
30
- if (destroyed) return;
31
- switch (data.type) {
32
- case "@statelyai.ready":
33
- flush();
34
- options.onReady?.();
35
- events.emit("ready", { version: data.version });
36
- break;
37
- case "@statelyai.loaded":
38
- options.onLoaded?.(data.graph);
39
- events.emit("loaded", { graph: data.graph });
40
- break;
41
- case "@statelyai.change":
42
- options.onChange?.(data.graph, data.machineConfig);
43
- events.emit("change", {
44
- graph: data.graph,
45
- machineConfig: data.machineConfig
46
- });
47
- break;
48
- case "@statelyai.save":
49
- options.onSave?.(data.graph, data.machineConfig);
50
- events.emit("save", {
51
- graph: data.graph,
52
- machineConfig: data.machineConfig
53
- });
54
- break;
55
- case "@statelyai.retrieved":
56
- if (typeof data.requestId === "string") exportManager.resolve(data.requestId, data.data);
57
- break;
58
- case "@statelyai.error": {
59
- const err = {
60
- code: data.code,
61
- message: data.message
62
- };
63
- exportManager.reject(new Error(err.message), typeof data.requestId === "string" ? data.requestId : void 0);
64
- options.onError?.(err);
65
- events.emit("error", err);
66
- break;
67
- }
68
- }
69
- }
70
- function replaceTransport(nextTransport) {
71
- transport?.destroy();
72
- transport = nextTransport;
73
- }
74
- function setupTransport(el) {
75
- replaceTransport(createPostMessageTransport({
76
- iframe: el,
77
- targetOrigin
78
- }));
79
- transport.onMessage(handleMessage);
80
- transport.onReady(flush);
81
- }
82
- return {
83
- attach(el) {
84
- if (destroyed) return;
85
- const currentSrc = el.getAttribute("src");
86
- if (currentSrc && currentSrc !== "about:blank" && el.src !== embedUrl) console.warn("Replacing existing iframe src during attach()", {
87
- currentSrc,
88
- nextSrc: embedUrl
89
- });
90
- iframe = el;
91
- ownedIframe = false;
92
- el.src = embedUrl;
93
- setupTransport(el);
94
- },
95
- mount(container) {
96
- if (destroyed) throw new Error("Embed is destroyed");
97
- const el = document.createElement("iframe");
98
- el.src = embedUrl;
99
- el.style.border = "none";
100
- el.style.width = "100%";
101
- el.style.height = "100%";
102
- el.setAttribute("sandbox", "allow-scripts allow-same-origin");
103
- el.setAttribute("allow", "clipboard-read; clipboard-write");
104
- container.appendChild(el);
105
- iframe = el;
106
- ownedIframe = true;
107
- setupTransport(el);
108
- return el;
109
- },
110
- init(opts) {
111
- send(toInitMessage(opts));
112
- },
113
- updateMachine(machine, format) {
114
- send({
115
- type: "@statelyai.update",
116
- machine,
117
- format
118
- });
119
- },
120
- setMode(mode) {
121
- send({
122
- type: "@statelyai.setMode",
123
- mode
124
- });
125
- },
126
- setTheme(theme) {
127
- send({
128
- type: "@statelyai.setTheme",
129
- theme
130
- });
131
- },
132
- toast(message, toastType) {
133
- send({
134
- type: "@statelyai.toast",
135
- message,
136
- toastType
137
- });
138
- },
139
- export(format, callOptions) {
140
- return exportManager.start(format, callOptions, "Embed is destroyed", () => destroyed);
141
- },
142
- on(event, handler) {
143
- events.on(event, handler);
144
- },
145
- off(event, handler) {
146
- events.off(event, handler);
147
- },
148
- destroy() {
149
- if (destroyed) return;
150
- destroyed = true;
151
- transport?.destroy();
152
- transport = null;
153
- exportManager.clear("Embed destroyed");
154
- events.clear();
155
- pendingMessages.length = 0;
156
- if (ownedIframe && iframe) iframe.remove();
157
- iframe = null;
158
- }
159
- };
160
- }
161
-
162
- //#endregion
163
- export { createInspector, createPostMessageTransport, createStatelyEmbed, createWebSocketTransport };
7
+ export { StudioApiError, createPostMessageTransport, createStatelyClient, createStatelyEmbed, createStatelyInspector, createWebSocketTransport, fromStudioMachine, studioMachineConverter, toStudioMachine };
@@ -1,2 +1,45 @@
1
- import { a as EmbedEventHandler, c as EmbedMode, d as ExportFormatMap, f as InitOptions, i as createInspector, l as ExportCallOptions, n as InspectOptions, o as EmbedEventMap, r as Inspector, s as EmbedEventName, t as CreateInspectorOptions, u as ExportFormat } from "./inspect-Bt5Ohkrp.mjs";
2
- export { CreateInspectorOptions, EmbedEventHandler, EmbedEventMap, EmbedEventName, EmbedMode, ExportCallOptions, ExportFormat, ExportFormatMap, InitOptions, InspectOptions, Inspector, createInspector };
1
+ import { a as ExportCallOptions, c as InitOptions, i as EmbedMode, n as EmbedEventMap, o as ExportFormat, r as EmbedEventName, s as ExportFormatMap, t as EmbedEventHandler } from "./protocol-DTIQmjHH.mjs";
2
+
3
+ //#region src/inspect.d.ts
4
+ interface CreateInspectorOptions {
5
+ /** WebSocket URL of the devtools server. Default: 'ws://localhost:4242' */
6
+ url?: string;
7
+ /** Auto-open browser to visualizer. Default: true */
8
+ autoOpen?: boolean;
9
+ /** Unique session ID. Auto-generated if not provided. */
10
+ sessionId?: string;
11
+ /** Display name for this connection in the visualizer. */
12
+ name?: string;
13
+ }
14
+ interface Inspector {
15
+ /** Send a machine for inspection (starts visualization). */
16
+ inspect(options: InspectOptions): void;
17
+ /** Update the machine currently being inspected. */
18
+ update(machine: unknown, format?: string): void;
19
+ /** Change the visualization mode. */
20
+ setMode(mode: EmbedMode): void;
21
+ /** Export the current machine in a given format. Returns a promise. */
22
+ export<F extends ExportFormat>(format: F, options?: ExportCallOptions<F>): Promise<ExportFormatMap[F]['result']>;
23
+ /** Subscribe to events from the visualizer. */
24
+ on<K extends EmbedEventName>(event: K, handler: EmbedEventHandler<K>): void;
25
+ /** Unsubscribe from an event. */
26
+ off<K extends EmbedEventName>(event: K, handler: EmbedEventHandler<K>): void;
27
+ /** Send a state snapshot for real-time inspection. */
28
+ sendSnapshot(snapshot: unknown, event?: unknown): void;
29
+ /** Clean up the connection. */
30
+ destroy(): void;
31
+ /** Session ID for this connection. */
32
+ readonly sessionId: string;
33
+ }
34
+ interface InspectOptions {
35
+ machine: unknown;
36
+ format?: string;
37
+ mode?: EmbedMode;
38
+ theme?: 'light' | 'dark';
39
+ readOnly?: boolean;
40
+ depth?: number;
41
+ panels?: InitOptions['panels'];
42
+ }
43
+ declare function createStatelyInspector(options?: CreateInspectorOptions): Inspector;
44
+ //#endregion
45
+ export { CreateInspectorOptions, type EmbedEventHandler, type EmbedEventMap, type EmbedEventName, type EmbedMode, type ExportCallOptions, type ExportFormat, type ExportFormatMap, type InitOptions, InspectOptions, Inspector, createStatelyInspector };