@statelyai/sdk 0.10.1 → 0.11.0

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/index.d.mts CHANGED
@@ -2,8 +2,8 @@ import { StatelyApiClientOptions, StatelyApiError, StatelyApiUrlOptions, createS
2
2
  import { CommentsConfig, EmbedEventHandler, EmbedEventMap, EmbedEventName, EmbedMode, ExportCallOptions, ExportFormat, ExportFormatMap, InitOptions, ProjectEmbedMachine, UploadResult } from "./protocol.mjs";
3
3
  import { AssetUploadAdapter, AssetUploadContext, AssetUploadRequest, CreateS3AssetUploadAdapterOptions, CreateSupabaseAssetUploadAdapterOptions, S3UploadTarget, SupabaseStorageClient, createS3AssetUploadAdapter, createSupabaseAssetUploadAdapter } from "./assetStorage.mjs";
4
4
  import { AssetConfig, StatelyEmbed, StatelyEmbedOptions, createStatelyEmbed } from "./embed.mjs";
5
- import { C as EventTypeData, E as JSONSchema7, S as DigraphNodeConfig, T as CodeGenGraph, _ 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-DmXh22Zu.mjs";
6
- import { a as ManualActorOptions, c as createPostMessageTransport, i as InspectorEvents, l as createWebSocketTransport, n as CreateInspectorOptions, o as createStatelyInspector, r as Inspector, s as Transport, t as AdoptedActor } from "./inspect-BLlM3qKf.mjs";
5
+ import { C as EventTypeData, E as JSONSchema7, S as DigraphNodeConfig, T as CodeGenGraph, _ 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-GeuH-mFK.mjs";
6
+ import { a as ManualActorOptions, c as createPostMessageTransport, i as InspectorEvents, l as createWebSocketTransport, n as CreateInspectorOptions, o as createStatelyInspector, r as Inspector, s as Transport, t as AdoptedActor } from "./inspect-CZ8iLJR7.mjs";
7
7
  import { ConnectedRepo, CreateMachineFromDefinitionInput, CreateMachineFromTemplateInput, CreateMachineInput, CreateMachineTemplate, CreateProjectInput, EnsureProjectInput, ExtractMachinesResponse, ExtractedMachine, GetMachineOptions, ProjectData, ProjectMachine, ProjectVisibility, RepoType, StudioApiError, StudioClient, StudioClientOptions, StudioMachineRecord, UpdateMachineInput, VerifyApiKeyResponse, XStateVersion, createStatelyClient } from "./studio.mjs";
8
8
  import { PlanSyncOptions, PullSyncResult, PushLocalMachineLinksResult, PushSyncOptions, PushSyncProjectOptions, PushSyncResult, ResolvedSyncInput, SyncInputFormat, SyncPlan, SyncPlanSummary } from "./sync.mjs";
9
9
  import { ActionLocation, GraphPatch } from "./patchTypes.mjs";
@@ -35,6 +35,14 @@ declare function upsertStatelyPragma(sourceText: string, id: string, options?: U
35
35
  interface MachineConfigOptions {
36
36
  showDescriptions?: boolean;
37
37
  showMeta?: boolean;
38
+ /**
39
+ * Emit inline-code guards as a `{ type }` reference instead of an inline code
40
+ * expression directive. The simulation needs this: it can't evaluate guard
41
+ * source, so it stubs each guard by `type` (see getSimImplementations). A
42
+ * code-expression guard has no `type`, so XState reads `undefined` and throws
43
+ * "Guard 'undefined' is not implemented" the moment that transition is taken.
44
+ */
45
+ inlineGuardsAsTypeRef?: boolean;
38
46
  }
39
47
  declare function graphToMachineConfig(graph: CodeGenGraph, options?: MachineConfigOptions): UnknownMachineConfig;
40
48
  //#endregion
@@ -42,6 +50,7 @@ declare function graphToMachineConfig(graph: CodeGenGraph, options?: MachineConf
42
50
  interface XStateTSOptions extends MachineConfigOptions {
43
51
  exportStyle?: 'named' | 'default' | 'none';
44
52
  targetLanguage?: 'typescript' | 'javascript';
53
+ xstateVersion?: 5 | 6;
45
54
  }
46
55
  declare function graphToXStateTS(graph: CodeGenGraph, options?: XStateTSOptions): string;
47
56
  //#endregion
package/dist/index.mjs CHANGED
@@ -4,7 +4,7 @@ import { createS3AssetUploadAdapter, createSupabaseAssetUploadAdapter } from "./
4
4
  import { createStatelyInspector } from "./inspect.mjs";
5
5
  import { StudioApiError, createStatelyClient } from "./studio.mjs";
6
6
  import { StatelyApiError, createStatelyApiClient, createStatelyApiUrl } from "./api.mjs";
7
- import { a as graphToMachineConfig, c as serializeJS, d as upsertStatelyPragma, i as jsonSchemaToTSType, l as findStatelyPragmaAttachments, n as contextSchemaToTSType, o as RawCode, r as eventsSchemaToTSType, s as raw, t as graphToXStateTS, u as getStatelyPragma } from "./graphToXStateTS-moihsH_U.mjs";
7
+ import { S as upsertStatelyPragma, a as graphToMachineConfig, b as findStatelyPragmaAttachments, c as serializeJS, i as jsonSchemaToTSType, n as contextSchemaToTSType, o as RawCode, r as eventsSchemaToTSType, s as raw, t as graphToXStateTS, x as getStatelyPragma } from "./graphToXStateTS-DV62vO_d.mjs";
8
8
  import { fromStudioMachine, studioMachineConverter, toStudioMachine } from "./graph.mjs";
9
9
 
10
10
  export { RawCode, StatelyApiError, StudioApiError, contextSchemaToTSType, createPostMessageTransport, createS3AssetUploadAdapter, createStatelyApiClient, createStatelyApiUrl, createStatelyClient, createStatelyEmbed, createStatelyInspector, createSupabaseAssetUploadAdapter, createWebSocketTransport, eventsSchemaToTSType, findStatelyPragmaAttachments, fromStudioMachine, getStatelyPragma, graphToMachineConfig, graphToXStateTS, jsonSchemaToTSType, raw, serializeJS, studioMachineConverter, toStudioMachine, upsertStatelyPragma };
@@ -120,19 +120,13 @@ interface ManualActorOptions {
120
120
  systemId?: string;
121
121
  }
122
122
  interface CreateInspectorOptions {
123
- /**
124
- * Root xstate actor to adopt. The inspector walks its `system`.
125
- * Omit this for manual-only mode where you call `.actor()` / `.snapshot()`.
126
- */
127
- actor?: any;
128
123
  /** WebSocket URL of the devtools server. Default: `'ws://localhost:4242'`. */
129
124
  url?: string;
130
125
  /** Auto-open browser to visualizer. Default: `true`. */
131
126
  autoOpen?: boolean;
132
127
  /**
133
- * Relay session id. Defaults to the root actor's `sessionId` (auto mode)
134
- * or a random id (manual mode). This is the `?session=<id>` value the viz
135
- * iframe connects with.
128
+ * Relay session id. Defaults to a random id. This is the `?session=<id>`
129
+ * value the viz iframe connects with.
136
130
  */
137
131
  sessionId?: string;
138
132
  /** Display name for this system in the visualizer. */
@@ -148,7 +142,7 @@ interface CreateInspectorOptions {
148
142
  * `actor.logic.config`. Return `null` for non-machine actors.
149
143
  */
150
144
  extractMachineConfig?: (actor: any) => unknown;
151
- /** Selected actor id to focus on first. Defaults to the root actor. */
145
+ /** Selected actor id to focus on first. Defaults to the first inspected root. */
152
146
  selectedActorId?: string;
153
147
  /** Panel layout forwarded into the system init message. */
154
148
  panels?: {
@@ -171,6 +165,10 @@ interface Inspector {
171
165
  readonly actors: ReadonlyMap<string, AdoptedActor>;
172
166
  /** The relay sessionId this inspector registered under. */
173
167
  readonly sessionId: string;
168
+ /** Adopt an xstate actor system. Existing actors are backfilled immediately. */
169
+ inspect(actor: any): {
170
+ unsubscribe(): void;
171
+ };
174
172
  /** Subscribe to lifecycle / error events. Returns an unsubscribe function. */
175
173
  on<E extends EventName>(event: E, handler: Handler<E>): () => void;
176
174
  off<E extends EventName>(event: E, handler: Handler<E>): void;
@@ -201,6 +199,6 @@ interface Inspector {
201
199
  /** Tear down the WebSocket + xstate subscription. */
202
200
  destroy(): void;
203
201
  }
204
- declare function createStatelyInspector(options: CreateInspectorOptions): Inspector;
202
+ declare function createStatelyInspector(options?: CreateInspectorOptions): Inspector;
205
203
  //#endregion
206
204
  export { ManualActorOptions as a, createPostMessageTransport as c, InspectorEvents as i, createWebSocketTransport as l, CreateInspectorOptions as n, createStatelyInspector as o, Inspector as r, Transport as s, AdoptedActor as t };
@@ -1,3 +1,3 @@
1
1
  import { EmbedEventHandler, EmbedEventMap, EmbedEventName, EmbedMode, ExportCallOptions, ExportFormat, ExportFormatMap } from "./protocol.mjs";
2
- import { a as ManualActorOptions, i as InspectorEvents, n as CreateInspectorOptions, o as createStatelyInspector, r as Inspector, s as Transport, t as AdoptedActor } from "./inspect-BLlM3qKf.mjs";
2
+ import { a as ManualActorOptions, i as InspectorEvents, n as CreateInspectorOptions, o as createStatelyInspector, r as Inspector, s as Transport, t as AdoptedActor } from "./inspect-CZ8iLJR7.mjs";
3
3
  export { AdoptedActor, CreateInspectorOptions, EmbedEventHandler, EmbedEventMap, EmbedEventName, EmbedMode, ExportCallOptions, ExportFormat, ExportFormatMap, Inspector, InspectorEvents, ManualActorOptions, Transport, createStatelyInspector };
package/dist/inspect.mjs CHANGED
@@ -10,12 +10,10 @@ let nextManualId = 0;
10
10
  function generateSessionId() {
11
11
  return `manual-${Date.now()}-${nextManualId++}`;
12
12
  }
13
- function createStatelyInspector(options) {
14
- const { url = "ws://localhost:4242", autoOpen = true, actor: rootActor, name, serializeSnapshot = defaultSerializeSnapshot, extractMachineConfig = defaultExtractMachineConfig, selectedActorId, panels, theme, readOnly, depth, transport: injectedTransport } = options;
15
- const root = rootActor ? rootActor : null;
16
- const rootActorId = root?.sessionId ?? generateSessionId();
17
- const sessionId = options.sessionId ?? rootActorId;
18
- const displayName = name ?? (root ? `system:${root.id}` : "system:manual");
13
+ function createStatelyInspector(options = {}) {
14
+ const { url = "ws://localhost:4242", autoOpen = true, name, serializeSnapshot = defaultSerializeSnapshot, extractMachineConfig = defaultExtractMachineConfig, selectedActorId, panels, theme, readOnly, depth, transport: injectedTransport } = options;
15
+ const sessionId = options.sessionId ?? generateSessionId();
16
+ const displayName = name ?? "system:manual";
19
17
  let destroyed = false;
20
18
  let initSent = false;
21
19
  const pendingMessages = [];
@@ -92,7 +90,7 @@ function createStatelyInspector(options) {
92
90
  return null;
93
91
  }
94
92
  }
95
- function toEntry(actorRef) {
93
+ function toEntry(actorRef, rootActorId) {
96
94
  return {
97
95
  actorId: actorRef.sessionId,
98
96
  parentActorId: actorRef._parent?.sessionId ?? null,
@@ -116,22 +114,23 @@ function createStatelyInspector(options) {
116
114
  * Walk the actor tree and populate the local `actors` map. Idempotent —
117
115
  * existing entries stay in place but have their snapshot refreshed.
118
116
  */
119
- function backfillActorTree(actorRef) {
117
+ function backfillActorTree(actorRef, rootActorId) {
120
118
  const existing = actors.get(actorRef.sessionId);
121
- const entry = existing ?? toEntry(actorRef);
119
+ const entry = existing ?? toEntry(actorRef, rootActorId);
122
120
  if (existing) existing.snapshot = snapshotValue(actorRef);
123
121
  else actors.set(entry.actorId, entry);
124
122
  emitRegisterOnce(entry);
125
123
  const children = actorRef.getSnapshot()?.children ?? {};
126
- for (const child of Object.values(children)) if (child) backfillActorTree(child);
124
+ for (const child of Object.values(children)) if (child) backfillActorTree(child, rootActorId);
127
125
  }
126
+ const inspectedRoots = /* @__PURE__ */ new Map();
128
127
  function sendInit() {
129
- if (root) backfillActorTree(root);
128
+ for (const [rootActorId, { actor }] of inspectedRoots) backfillActorTree(actor, rootActorId);
130
129
  send({
131
130
  type: "@statelyai.system.init",
132
131
  mode: "inspecting",
133
132
  actors: Array.from(actors.values()).map(toSystemEntry),
134
- selectedActorId: selectedActorId ?? rootActorId,
133
+ selectedActorId: selectedActorId ?? inspectedRoots.keys().next().value ?? sessionId,
135
134
  ...theme && { theme },
136
135
  ...readOnly !== void 0 && { readOnly },
137
136
  ...depth !== void 0 && { depth },
@@ -141,9 +140,9 @@ function createStatelyInspector(options) {
141
140
  });
142
141
  initSent = true;
143
142
  }
144
- function handleActor(actorRef) {
143
+ function handleActor(actorRef, rootActorId) {
145
144
  if (actors.has(actorRef.sessionId)) return;
146
- const entry = toEntry(actorRef);
145
+ const entry = toEntry(actorRef, rootActorId);
147
146
  actors.set(entry.actorId, entry);
148
147
  if (!initSent) return;
149
148
  send({
@@ -156,8 +155,8 @@ function createStatelyInspector(options) {
156
155
  });
157
156
  emitRegisterOnce(entry);
158
157
  }
159
- function handleSnapshot(actorRef, snapshot, triggeringEvent) {
160
- if (!actors.has(actorRef.sessionId)) handleActor(actorRef);
158
+ function handleSnapshot(actorRef, snapshot, triggeringEvent, rootActorId) {
159
+ if (!actors.has(actorRef.sessionId)) handleActor(actorRef, rootActorId);
161
160
  const entry = actors.get(actorRef.sessionId);
162
161
  if (!entry) return;
163
162
  const serialized = serializeSnapshot(snapshot);
@@ -183,28 +182,43 @@ function createStatelyInspector(options) {
183
182
  emitLocal("stopped", { actorId: entry.actorId });
184
183
  }
185
184
  }
186
- if (root) backfillActorTree(root);
187
- const subscription = root ? root.system.inspect((inspectionEvent) => {
188
- if (destroyed) return;
189
- switch (inspectionEvent.type) {
190
- case "@xstate.actor":
191
- handleActor(inspectionEvent.actorRef);
192
- break;
193
- case "@xstate.event":
194
- if (inspectionEvent.sourceRef) pendingEventSource.set(inspectionEvent.actorRef.sessionId, inspectionEvent.sourceRef.sessionId);
195
- break;
196
- case "@xstate.snapshot":
197
- handleSnapshot(inspectionEvent.actorRef, inspectionEvent.snapshot, inspectionEvent.event);
198
- break;
199
- default: break;
200
- }
201
- }) : null;
185
+ function inspectActor(actor) {
186
+ const root = actor;
187
+ const rootActorId = root.sessionId;
188
+ const existing = inspectedRoots.get(rootActorId);
189
+ if (existing) return existing.subscription;
190
+ const subscription = root.system.inspect((inspectionEvent) => {
191
+ if (destroyed) return;
192
+ switch (inspectionEvent.type) {
193
+ case "@xstate.actor":
194
+ handleActor(inspectionEvent.actorRef, rootActorId);
195
+ break;
196
+ case "@xstate.event":
197
+ if (inspectionEvent.sourceRef) pendingEventSource.set(inspectionEvent.actorRef.sessionId, inspectionEvent.sourceRef.sessionId);
198
+ break;
199
+ case "@xstate.snapshot":
200
+ handleSnapshot(inspectionEvent.actorRef, inspectionEvent.snapshot, inspectionEvent.event, rootActorId);
201
+ break;
202
+ default: break;
203
+ }
204
+ });
205
+ inspectedRoots.set(rootActorId, {
206
+ actor: root,
207
+ subscription
208
+ });
209
+ backfillActorTree(root, rootActorId);
210
+ if (initSent) sendInit();
211
+ return { unsubscribe() {
212
+ subscription.unsubscribe();
213
+ inspectedRoots.delete(rootActorId);
214
+ } };
215
+ }
202
216
  function manualRegister(id, opts) {
203
217
  if (actors.has(id)) return;
204
218
  const entry = {
205
219
  actorId: id,
206
220
  parentActorId: opts?.parent ?? null,
207
- rootId: rootActorId,
221
+ rootId: sessionId,
208
222
  id,
209
223
  systemId: opts?.systemId,
210
224
  snapshot: opts?.snapshot ?? null,
@@ -252,6 +266,7 @@ function createStatelyInspector(options) {
252
266
  get sessionId() {
253
267
  return sessionId;
254
268
  },
269
+ inspect: inspectActor,
255
270
  on(event, handler) {
256
271
  if (!localListeners[event]) localListeners[event] = /* @__PURE__ */ new Set();
257
272
  localListeners[event].add(handler);
@@ -273,12 +288,13 @@ function createStatelyInspector(options) {
273
288
  if (destroyed) return;
274
289
  destroyed = true;
275
290
  try {
276
- subscription?.unsubscribe();
291
+ for (const { subscription } of inspectedRoots.values()) subscription.unsubscribe();
277
292
  } catch {}
278
293
  if (ownsTransport) transport.destroy();
279
294
  exportManager.clear("Inspector destroyed");
280
295
  events.clear();
281
296
  actors.clear();
297
+ inspectedRoots.clear();
282
298
  pendingMessages.length = 0;
283
299
  for (const set of Object.values(localListeners)) set?.clear();
284
300
  }
@@ -45,6 +45,7 @@ interface StateSourceLocation extends SourceLocation {
45
45
  interface MachineSourceLocations {
46
46
  version: 1;
47
47
  root?: SourceLocation;
48
+ statesObject?: StateSourceLocation;
48
49
  states: StateSourceLocation[];
49
50
  staticValues?: Array<SourceLocation & {
50
51
  value: string;
@@ -78,6 +79,12 @@ interface ExportFormatMap {
78
79
  options: object;
79
80
  result: Record<string, unknown>;
80
81
  };
82
+ d2: {
83
+ options: {
84
+ includeLayout?: boolean;
85
+ };
86
+ result: string;
87
+ };
81
88
  mermaid: {
82
89
  options: object;
83
90
  result: string;
@@ -159,6 +166,7 @@ interface MachineInitOptions {
159
166
  };
160
167
  comments?: CommentsConfig;
161
168
  sourceLocations?: MachineSourceLocations;
169
+ settings?: Record<string, unknown>;
162
170
  /**
163
171
  * Show a persistent "Save to apply" pill in the embed whenever Viz has
164
172
  * local edits that have not yet been saved. Intended for contexts like
@@ -189,6 +197,7 @@ interface ProjectInitOptions {
189
197
  activePanels?: string[];
190
198
  };
191
199
  commentsByMachineId?: Record<string, CommentsConfig>;
200
+ settings?: Record<string, unknown>;
192
201
  unsavedIndicator?: {
193
202
  enabled?: boolean;
194
203
  mode?: "structural" | "all";
@@ -208,6 +217,7 @@ interface InitMessage {
208
217
  activePanels?: string[];
209
218
  comments?: CommentsConfig;
210
219
  sourceLocations?: MachineSourceLocations;
220
+ settings?: Record<string, unknown>;
211
221
  /** Mirror of `InitOptions.unsavedIndicator`. */
212
222
  unsavedIndicator?: {
213
223
  enabled?: boolean;
@@ -228,6 +238,7 @@ interface ProjectInitMessage {
228
238
  rightPanels?: string[];
229
239
  activePanels?: string[];
230
240
  commentsByMachineId?: Record<string, CommentsConfig>;
241
+ settings?: Record<string, unknown>;
231
242
  unsavedIndicator?: {
232
243
  enabled?: boolean;
233
244
  mode?: "structural" | "all";
package/dist/sync.d.mts CHANGED
@@ -1,4 +1,4 @@
1
- import { b as DigraphConfig, i as StatelyGraph } from "./graph-DmXh22Zu.mjs";
1
+ import { b as DigraphConfig, i as StatelyGraph } from "./graph-GeuH-mFK.mjs";
2
2
  import { CreateProjectInput, ProjectData, StudioClient, StudioMachineRecord, XStateVersion } from "./studio.mjs";
3
3
  import { GraphDiff } from "@statelyai/graph";
4
4