@pixerate/editor 0.44.0 → 0.46.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.
Files changed (88) hide show
  1. package/README.md +29 -3
  2. package/dist/agent/index.cjs +548 -45
  3. package/dist/agent/index.cjs.map +1 -1
  4. package/dist/agent/index.d.cts +47 -17
  5. package/dist/agent/index.d.ts +47 -17
  6. package/dist/agent/index.js +536 -33
  7. package/dist/agent/index.js.map +1 -1
  8. package/dist/{builtins-BLud97fC.d.cts → builtins-CZW-aYKS.d.cts} +1 -1
  9. package/dist/{builtins-D0BDyAgw.d.ts → builtins-DghNQbT-.d.ts} +1 -1
  10. package/dist/chart/index.cjs +1984 -0
  11. package/dist/chart/index.cjs.map +1 -0
  12. package/dist/chart/index.d.cts +646 -0
  13. package/dist/chart/index.d.ts +646 -0
  14. package/dist/chart/index.js +1984 -0
  15. package/dist/chart/index.js.map +1 -0
  16. package/dist/{chunk-I6BZB5ZH.cjs → chunk-EFDCLENW.cjs} +73 -6
  17. package/dist/chunk-EFDCLENW.cjs.map +1 -0
  18. package/dist/chunk-HHQ4XJWT.js +826 -0
  19. package/dist/chunk-HHQ4XJWT.js.map +1 -0
  20. package/dist/chunk-J3R2OWOE.cjs +1465 -0
  21. package/dist/chunk-J3R2OWOE.cjs.map +1 -0
  22. package/dist/chunk-K2LVZ4FA.cjs +826 -0
  23. package/dist/chunk-K2LVZ4FA.cjs.map +1 -0
  24. package/dist/{chunk-6MDQKHRH.cjs → chunk-N4MLNJPO.cjs} +21 -899
  25. package/dist/chunk-N4MLNJPO.cjs.map +1 -0
  26. package/dist/chunk-PQM5W7ZN.js +168 -0
  27. package/dist/chunk-PQM5W7ZN.js.map +1 -0
  28. package/dist/{chunk-DDADOHJW.js → chunk-Q25ZS4F7.js} +5 -883
  29. package/dist/chunk-Q25ZS4F7.js.map +1 -0
  30. package/dist/chunk-TK2XZK6Z.js +1465 -0
  31. package/dist/chunk-TK2XZK6Z.js.map +1 -0
  32. package/dist/commands/index.cjs +3 -2
  33. package/dist/commands/index.cjs.map +1 -1
  34. package/dist/commands/index.d.cts +5 -5
  35. package/dist/commands/index.d.ts +5 -5
  36. package/dist/commands/index.js +2 -1
  37. package/dist/commands/index.js.map +1 -1
  38. package/dist/extensions/index.cjs +4 -4
  39. package/dist/extensions/index.cjs.map +1 -1
  40. package/dist/extensions/index.d.cts +3 -3
  41. package/dist/extensions/index.d.ts +3 -3
  42. package/dist/extensions/index.js +12 -12
  43. package/dist/generation/index.cjs +2 -2
  44. package/dist/generation/index.d.cts +4 -4
  45. package/dist/generation/index.d.ts +4 -4
  46. package/dist/generation/index.js +1 -1
  47. package/dist/image-editor/index.cjs +24 -24
  48. package/dist/image-editor/index.cjs.map +1 -1
  49. package/dist/image-editor/index.d.cts +2 -2
  50. package/dist/image-editor/index.d.ts +2 -2
  51. package/dist/image-editor/index.js +24 -24
  52. package/dist/image-editor/index.js.map +1 -1
  53. package/dist/{index-C6gaQRH0.d.cts → index-CfOQf70b.d.cts} +1 -1
  54. package/dist/{index-DXAv_R08.d.ts → index-jwJ_onWf.d.ts} +1 -1
  55. package/dist/index.cjs +9 -9
  56. package/dist/index.cjs.map +1 -1
  57. package/dist/index.d.cts +4 -4
  58. package/dist/index.d.ts +4 -4
  59. package/dist/index.js +12 -12
  60. package/dist/parameters/index.d.cts +2 -2
  61. package/dist/parameters/index.d.ts +2 -2
  62. package/dist/{registry-DnzpgY27.d.cts → registry-B4l-lYts.d.cts} +1 -1
  63. package/dist/{registry-DhVUZioj.d.ts → registry-BfVD-vVg.d.ts} +1 -1
  64. package/dist/{registry-BDe-vlv3.d.ts → registry-CSlH4iUn.d.ts} +1 -1
  65. package/dist/{registry-D0GADKc7.d.cts → registry-vz6HOiiy.d.cts} +1 -1
  66. package/dist/{schema-DYDGdYL9.d.cts → schema-Di0CS6Pe.d.cts} +26 -2
  67. package/dist/{schema-DYDGdYL9.d.ts → schema-Di0CS6Pe.d.ts} +26 -2
  68. package/dist/tools/index.cjs +37 -3
  69. package/dist/tools/index.cjs.map +1 -1
  70. package/dist/tools/index.d.cts +871 -11
  71. package/dist/tools/index.d.ts +871 -11
  72. package/dist/tools/index.js +36 -2
  73. package/dist/vector/index.d.cts +2 -2
  74. package/dist/vector/index.d.ts +2 -2
  75. package/package.json +6 -1
  76. package/dist/chunk-36BLROHV.js +0 -438
  77. package/dist/chunk-36BLROHV.js.map +0 -1
  78. package/dist/chunk-5H2OPMXZ.js +0 -586
  79. package/dist/chunk-5H2OPMXZ.js.map +0 -1
  80. package/dist/chunk-6MDQKHRH.cjs.map +0 -1
  81. package/dist/chunk-D6S5M7HV.js +0 -101
  82. package/dist/chunk-D6S5M7HV.js.map +0 -1
  83. package/dist/chunk-DDADOHJW.js.map +0 -1
  84. package/dist/chunk-I6BZB5ZH.cjs.map +0 -1
  85. package/dist/chunk-MC2LQN5S.cjs +0 -586
  86. package/dist/chunk-MC2LQN5S.cjs.map +0 -1
  87. package/dist/chunk-V7CC7RZ6.cjs +0 -438
  88. package/dist/chunk-V7CC7RZ6.cjs.map +0 -1
@@ -0,0 +1,826 @@
1
+ import {
2
+ validateJsonSchema
3
+ } from "./chunk-PQM5W7ZN.js";
4
+
5
+ // src/tools/registry.ts
6
+ var ToolRegistryError = class extends Error {
7
+ code;
8
+ constructor(code, message) {
9
+ super(message);
10
+ this.name = "ToolRegistryError";
11
+ this.code = code;
12
+ }
13
+ };
14
+ var TOOL_NAMESPACE_SEPARATOR = "__";
15
+ var TOOL_NAME_REGEX = /^[A-Za-z0-9_.-]{1,128}$/;
16
+ var sanitizeNamespace = (id) => id.replace(/[^A-Za-z0-9_-]/g, "_");
17
+ function readTools(provider) {
18
+ return typeof provider.tools === "function" ? provider.tools() : provider.tools;
19
+ }
20
+ function toDefinition(name, tool) {
21
+ const { handler: _handler, name: _name, ...rest } = tool;
22
+ return { name, ...rest };
23
+ }
24
+ function errorResult(error) {
25
+ const details = error.details?.length ? "\n" + error.details.map((d) => `- ${d.path}: ${d.message}`).join("\n") : "";
26
+ return {
27
+ isError: true,
28
+ content: [{ type: "text", text: `${error.message}${details}` }],
29
+ structuredContent: { error }
30
+ };
31
+ }
32
+ function toToolResult(value) {
33
+ if (value && typeof value === "object" && Array.isArray(value.content)) {
34
+ return value;
35
+ }
36
+ if (value === void 0) return { content: [] };
37
+ if (typeof value === "string") return { content: [{ type: "text", text: value }] };
38
+ const text = JSON.stringify(value);
39
+ const isPlainObject = typeof value === "object" && value !== null && !Array.isArray(value);
40
+ return {
41
+ content: [{ type: "text", text: text ?? String(value) }],
42
+ ...isPlainObject ? { structuredContent: value } : {}
43
+ };
44
+ }
45
+ var ToolRegistry = class {
46
+ /** MCP server capabilities this registry supports. */
47
+ capabilities = { tools: { listChanged: true } };
48
+ entries = [];
49
+ listeners = /* @__PURE__ */ new Set();
50
+ options;
51
+ timer = null;
52
+ lastSignature;
53
+ constructor(options = {}) {
54
+ this.options = {
55
+ onCollision: options.onCollision ?? "namespace",
56
+ listChangedDelayMs: options.listChangedDelayMs ?? 0,
57
+ validateArguments: options.validateArguments ?? true
58
+ };
59
+ this.lastSignature = this.signature();
60
+ }
61
+ /**
62
+ * Registers a provider and returns a function that unregisters it.
63
+ * Throws a {@link ToolRegistryError} for a duplicate provider id, an invalid
64
+ * tool name, or (with `onCollision: 'error'`) a tool name collision.
65
+ */
66
+ register(provider) {
67
+ if (this.entries.some((e) => e.provider.id === provider.id)) {
68
+ throw new ToolRegistryError("duplicate_provider", `Tool provider "${provider.id}" is already registered.`);
69
+ }
70
+ for (const tool of readTools(provider)) {
71
+ if (!TOOL_NAME_REGEX.test(tool.name)) {
72
+ throw new ToolRegistryError("invalid_tool", `Invalid tool name "${tool.name}" from provider "${provider.id}".`);
73
+ }
74
+ }
75
+ const entry = { provider };
76
+ this.entries.push(entry);
77
+ try {
78
+ const names = this.resolve(this.options.onCollision === "error").map((t) => t.name);
79
+ const duplicate = names.find((name, i) => names.indexOf(name) !== i);
80
+ if (duplicate) {
81
+ throw new ToolRegistryError("name_collision", `Tool name "${duplicate}" is exposed more than once.`);
82
+ }
83
+ } catch (error) {
84
+ this.entries.pop();
85
+ throw error;
86
+ }
87
+ if (provider.subscribe) {
88
+ entry.unsubscribe = provider.subscribe(() => this.notifyListChanged());
89
+ }
90
+ this.notifyListChanged();
91
+ let registered = true;
92
+ return () => {
93
+ if (!registered) return;
94
+ registered = false;
95
+ this.unregister(provider);
96
+ };
97
+ }
98
+ /** Removes a provider by id or instance. Returns whether it was registered. */
99
+ unregister(providerOrId) {
100
+ const index = this.entries.findIndex(
101
+ (e) => typeof providerOrId === "string" ? e.provider.id === providerOrId : e.provider === providerOrId
102
+ );
103
+ if (index === -1) return false;
104
+ const [entry] = this.entries.splice(index, 1);
105
+ entry.unsubscribe?.();
106
+ this.notifyListChanged();
107
+ return true;
108
+ }
109
+ /** Registered provider ids in registration order. */
110
+ providerIds() {
111
+ return this.entries.map((e) => e.provider.id);
112
+ }
113
+ /**
114
+ * Resolves exposed names. Tools are ordered by provider registration, then
115
+ * by their order within the provider.
116
+ */
117
+ resolve(throwOnCollision = false) {
118
+ const all = [];
119
+ const counts = /* @__PURE__ */ new Map();
120
+ for (const { provider } of this.entries) {
121
+ for (const tool of readTools(provider)) {
122
+ all.push({ name: tool.name, tool, provider });
123
+ if (!provider.namespace) counts.set(tool.name, (counts.get(tool.name) ?? 0) + 1);
124
+ }
125
+ }
126
+ for (const item of all) {
127
+ const prefix = item.provider.namespace ?? ((counts.get(item.name) ?? 0) > 1 ? item.provider.id : void 0);
128
+ if (prefix !== void 0) {
129
+ if (throwOnCollision && !item.provider.namespace) {
130
+ throw new ToolRegistryError(
131
+ "name_collision",
132
+ `Tool "${item.name}" from provider "${item.provider.id}" collides with another provider's tool.`
133
+ );
134
+ }
135
+ item.name = `${sanitizeNamespace(prefix)}${TOOL_NAMESPACE_SEPARATOR}${item.name}`;
136
+ }
137
+ }
138
+ return all;
139
+ }
140
+ /** MCP `tools/list` result for the current providers. */
141
+ listTools() {
142
+ return { tools: this.resolve().map(({ name, tool }) => toDefinition(name, tool)) };
143
+ }
144
+ /** Whether a tool with this exposed name exists. */
145
+ hasTool(name) {
146
+ return this.resolve().some((t) => t.name === name);
147
+ }
148
+ /** Looks up a tool definition by exposed name. */
149
+ getTool(name) {
150
+ const found = this.resolve().find((t) => t.name === name);
151
+ return found ? toDefinition(found.name, found.tool) : void 0;
152
+ }
153
+ /**
154
+ * Calls a tool by exposed name and returns an MCP `tools/call` result.
155
+ * Never rejects: unknown tools, invalid arguments and handler errors come
156
+ * back as `isError: true` results with a {@link ToolCallError} in
157
+ * `structuredContent.error` (MCP transports may map `unknown_tool` to a
158
+ * JSON-RPC `-32602` error instead).
159
+ */
160
+ async callTool(name, args = {}, options = {}) {
161
+ const found = this.resolve().find((t) => t.name === name);
162
+ if (!found) {
163
+ return errorResult({ code: "unknown_tool", message: `Unknown tool "${name}".`, tool: name });
164
+ }
165
+ const input = args ?? {};
166
+ if (this.options.validateArguments) {
167
+ const details = validateJsonSchema(input, found.tool.inputSchema);
168
+ if (details.length > 0) {
169
+ return errorResult({
170
+ code: "invalid_arguments",
171
+ message: `Invalid arguments for tool "${name}".`,
172
+ tool: name,
173
+ details
174
+ });
175
+ }
176
+ }
177
+ try {
178
+ const value = await found.tool.handler(input, {
179
+ name,
180
+ providerId: found.provider.id,
181
+ signal: options.signal
182
+ });
183
+ return toToolResult(value);
184
+ } catch (error) {
185
+ return errorResult({
186
+ code: "tool_failed",
187
+ message: error instanceof Error ? error.message : String(error),
188
+ tool: name
189
+ });
190
+ }
191
+ }
192
+ /**
193
+ * Subscribes to tool-list changes. Notifications are coalesced: many
194
+ * registrations or provider signals within the delay window produce one
195
+ * call, and none if the exposed list ends up unchanged (e.g. a provider
196
+ * unmounted and remounted). Returns an unsubscribe function.
197
+ */
198
+ onListChanged(listener) {
199
+ this.listeners.add(listener);
200
+ return () => {
201
+ this.listeners.delete(listener);
202
+ };
203
+ }
204
+ /** Schedules a coalesced `listChanged` check; providers may call it directly. */
205
+ notifyListChanged() {
206
+ if (this.timer !== null) return;
207
+ this.timer = setTimeout(() => this.flush(), this.options.listChangedDelayMs);
208
+ }
209
+ /** Runs a pending `listChanged` check immediately. */
210
+ flush() {
211
+ if (this.timer !== null) {
212
+ clearTimeout(this.timer);
213
+ this.timer = null;
214
+ }
215
+ const signature = this.signature();
216
+ if (signature === this.lastSignature) return;
217
+ this.lastSignature = signature;
218
+ const { tools } = this.listTools();
219
+ for (const listener of Array.from(this.listeners)) listener(tools);
220
+ }
221
+ signature() {
222
+ try {
223
+ return JSON.stringify(this.listTools().tools);
224
+ } catch {
225
+ return String(Math.random());
226
+ }
227
+ }
228
+ /** Unregisters every provider and listener and cancels pending notifications. */
229
+ dispose() {
230
+ if (this.timer !== null) clearTimeout(this.timer);
231
+ this.timer = null;
232
+ for (const entry of this.entries.splice(0)) entry.unsubscribe?.();
233
+ this.listeners.clear();
234
+ }
235
+ };
236
+ function createToolRegistry(options) {
237
+ return new ToolRegistry(options);
238
+ }
239
+
240
+ // src/tools/definitions.ts
241
+ var MCP_TOOL_GET_EDITOR_STATE = {
242
+ name: "get_editor_state",
243
+ title: "Get editor state",
244
+ annotations: {
245
+ title: "Get editor state",
246
+ readOnlyHint: true,
247
+ destructiveHint: false,
248
+ idempotentHint: true,
249
+ openWorldHint: false
250
+ },
251
+ description: "Retrieves the current state of the document, variables, selection, and canvas graph (nodes and edges).",
252
+ inputSchema: {
253
+ type: "object",
254
+ properties: {
255
+ includeDocument: {
256
+ type: "boolean",
257
+ description: "Whether to include the prompt document text and variables (default: true)."
258
+ },
259
+ includeCanvas: {
260
+ type: "boolean",
261
+ description: "Whether to include the canvas nodes and edges (default: true)."
262
+ }
263
+ }
264
+ }
265
+ };
266
+ var MCP_TOOL_EDIT_CANVAS = {
267
+ name: "edit_canvas",
268
+ title: "Edit the canvas",
269
+ annotations: {
270
+ title: "Edit the canvas",
271
+ readOnlyHint: false,
272
+ destructiveHint: true,
273
+ idempotentHint: false,
274
+ openWorldHint: false
275
+ },
276
+ description: "Applies high-level structural operations to the visual canvas (adding nodes, connecting edges, organizing layout, or moving elements). Prefer the per-operation canvas_* tools when the host exposes them. Returns the ids of created nodes and edges.",
277
+ inputSchema: {
278
+ type: "object",
279
+ properties: {
280
+ operation: {
281
+ type: "string",
282
+ enum: [
283
+ "add_node",
284
+ "remove_nodes",
285
+ "connect",
286
+ "update_node",
287
+ "layout",
288
+ "select",
289
+ "highlight"
290
+ ],
291
+ description: "The canvas operation to execute."
292
+ },
293
+ node: {
294
+ type: "object",
295
+ description: "Node definition when operation is add_node. Can omit position to auto-place."
296
+ },
297
+ nearNodeId: {
298
+ type: "string",
299
+ description: "Anchor node ID to position beside using collision-aware placement."
300
+ },
301
+ side: {
302
+ type: "string",
303
+ enum: ["top", "right", "bottom", "left"],
304
+ description: "Side of the anchor node to place the new node beside."
305
+ },
306
+ nodeIds: {
307
+ type: "array",
308
+ items: { type: "string" },
309
+ description: "Array of node IDs for remove_nodes, select, or highlight."
310
+ },
311
+ id: {
312
+ type: "string",
313
+ description: "Node ID for update_node (or pass `node: { id, data }`)."
314
+ },
315
+ data: {
316
+ type: "object",
317
+ description: "Data fields to merge into the node for update_node."
318
+ },
319
+ color: {
320
+ type: "string",
321
+ description: "CSS color for highlight (default #3b82f6)."
322
+ },
323
+ durationMs: {
324
+ type: "integer",
325
+ minimum: 0,
326
+ description: "How long the highlight lasts in ms (default 2000)."
327
+ },
328
+ source: {
329
+ type: "string",
330
+ description: "Source node ID for connect operation."
331
+ },
332
+ target: {
333
+ type: "string",
334
+ description: "Target node ID for connect operation."
335
+ },
336
+ label: {
337
+ type: "string",
338
+ description: "Optional edge label for connect operation."
339
+ },
340
+ direction: {
341
+ type: "string",
342
+ enum: ["LR", "TB", "RL", "BT"],
343
+ description: "Layout direction (default LR)."
344
+ },
345
+ incremental: {
346
+ type: "boolean",
347
+ description: "Preserve existing node coordinates and blend layout (default true)."
348
+ }
349
+ },
350
+ required: ["operation"]
351
+ }
352
+ };
353
+ var MCP_TOOL_EDIT_PROMPT = {
354
+ name: "edit_prompt",
355
+ title: "Edit the prompt",
356
+ annotations: {
357
+ title: "Edit the prompt",
358
+ readOnlyHint: false,
359
+ destructiveHint: true,
360
+ idempotentHint: false,
361
+ openWorldHint: false
362
+ },
363
+ description: "Applies edits to the prompt template document, including inserting text, replacing ranges, and setting variables.",
364
+ inputSchema: {
365
+ type: "object",
366
+ properties: {
367
+ operation: {
368
+ type: "string",
369
+ enum: ["insert_text", "replace_range", "set_variable", "select"],
370
+ description: "The prompt operation to execute."
371
+ },
372
+ text: {
373
+ type: "string",
374
+ description: "Text to insert or replace."
375
+ },
376
+ at: {
377
+ type: "integer",
378
+ minimum: 0,
379
+ description: "Position to insert text at."
380
+ },
381
+ from: {
382
+ type: "integer",
383
+ minimum: 0,
384
+ description: "Start offset for replace_range or select."
385
+ },
386
+ to: {
387
+ type: "integer",
388
+ minimum: 0,
389
+ description: "End offset for replace_range or select."
390
+ },
391
+ name: {
392
+ type: "string",
393
+ description: "Variable name for set_variable."
394
+ },
395
+ value: {
396
+ description: "Variable value for set_variable."
397
+ }
398
+ },
399
+ required: ["operation"]
400
+ }
401
+ };
402
+ var MCP_TOOL_VIEW_EDITOR = {
403
+ name: "view_editor",
404
+ title: "View the editor",
405
+ annotations: {
406
+ title: "View the editor",
407
+ readOnlyHint: true,
408
+ destructiveHint: false,
409
+ idempotentHint: true,
410
+ openWorldHint: false
411
+ },
412
+ description: "Captures a visual image snapshot (JPEG or PNG) of the editor canvas or viewport for multimodal evaluation. Active agent cursor indicators are automatically excluded from the snapshot.",
413
+ inputSchema: {
414
+ type: "object",
415
+ properties: {
416
+ scope: {
417
+ type: "string",
418
+ enum: ["viewport", "canvas"],
419
+ description: "Whether to capture only the visible viewport or the full canvas bounding box (default: viewport)."
420
+ },
421
+ format: {
422
+ type: "string",
423
+ enum: ["jpeg", "png"],
424
+ description: "Image format of the base64 snapshot (default: jpeg)."
425
+ },
426
+ maxEdge: {
427
+ type: "integer",
428
+ description: "Maximum width or height dimension in pixels (default: 1024)."
429
+ }
430
+ }
431
+ }
432
+ };
433
+ var WRITE_ANNOTATIONS = { readOnlyHint: false, destructiveHint: false, idempotentHint: false, openWorldHint: false };
434
+ var DESTRUCTIVE_ANNOTATIONS = { readOnlyHint: false, destructiveHint: true, idempotentHint: false, openWorldHint: false };
435
+ var IDEMPOTENT_ANNOTATIONS = { readOnlyHint: false, destructiveHint: false, idempotentHint: true, openWorldHint: false };
436
+ var NODE_IDS = {
437
+ type: "array",
438
+ items: { type: "string", minLength: 1 },
439
+ minItems: 1,
440
+ uniqueItems: true,
441
+ description: "Node ids (from get_editor_state)."
442
+ };
443
+ var POSITION = {
444
+ type: "object",
445
+ properties: { x: { type: "number" }, y: { type: "number" } },
446
+ required: ["x", "y"],
447
+ additionalProperties: false
448
+ };
449
+ var DOC_POSITION = { type: "integer", minimum: 0 };
450
+ var MCP_TOOL_CANVAS_ADD_NODE = {
451
+ name: "canvas_add_node",
452
+ title: "Add a canvas node",
453
+ annotations: { title: "Add a canvas node", ...WRITE_ANNOTATIONS },
454
+ description: "Adds a node to the canvas. Without `position` it is placed without overlaps beside `nearNodeId` (and connected from it) or beside the last node. Returns the new node id (generated unless you pass `node.id`) and the id of the connecting edge.",
455
+ inputSchema: {
456
+ type: "object",
457
+ properties: {
458
+ node: {
459
+ type: "object",
460
+ description: "The node. Extra host-specific fields are kept.",
461
+ properties: {
462
+ id: { type: "string", minLength: 1, description: "Node id (default: generated). Must be unused." },
463
+ type: { type: "string", description: "Node type registered by the host." },
464
+ data: { type: "object", description: 'Node data (e.g. { "label": "..." }).' },
465
+ position: { ...POSITION, description: "Canvas coordinates (default: auto-placed)." },
466
+ width: { type: "number", exclusiveMinimum: 0 },
467
+ height: { type: "number", exclusiveMinimum: 0 },
468
+ parentId: { type: "string", description: "Parent (group/frame) node id." }
469
+ }
470
+ },
471
+ nearNodeId: { type: "string", description: "Existing node to place the new node beside and connect from." },
472
+ side: {
473
+ type: "string",
474
+ enum: ["top", "right", "bottom", "left"],
475
+ description: "Side of `nearNodeId` to place the node on (default right)."
476
+ }
477
+ },
478
+ required: ["node"],
479
+ additionalProperties: false
480
+ }
481
+ };
482
+ var MCP_TOOL_CANVAS_REMOVE_NODES = {
483
+ name: "canvas_remove_nodes",
484
+ title: "Remove canvas nodes",
485
+ annotations: { title: "Remove canvas nodes", ...DESTRUCTIVE_ANNOTATIONS },
486
+ description: "Removes nodes (and their edges) from the canvas. Every id must exist.",
487
+ inputSchema: {
488
+ type: "object",
489
+ properties: { nodeIds: NODE_IDS },
490
+ required: ["nodeIds"],
491
+ additionalProperties: false
492
+ }
493
+ };
494
+ var MCP_TOOL_CANVAS_CONNECT = {
495
+ name: "canvas_connect",
496
+ title: "Connect canvas nodes",
497
+ annotations: { title: "Connect canvas nodes", ...WRITE_ANNOTATIONS },
498
+ description: "Adds an edge from one existing node to another. Returns the new edge id.",
499
+ inputSchema: {
500
+ type: "object",
501
+ properties: {
502
+ source: { type: "string", minLength: 1, description: "Source node id." },
503
+ target: { type: "string", minLength: 1, description: "Target node id." },
504
+ label: { type: "string", description: "Edge label." }
505
+ },
506
+ required: ["source", "target"],
507
+ additionalProperties: false
508
+ }
509
+ };
510
+ var MCP_TOOL_CANVAS_UPDATE_NODE = {
511
+ name: "canvas_update_node",
512
+ title: "Update a canvas node",
513
+ annotations: { title: "Update a canvas node", ...DESTRUCTIVE_ANNOTATIONS, idempotentHint: true },
514
+ description: "Merges fields into an existing node's data.",
515
+ inputSchema: {
516
+ type: "object",
517
+ properties: {
518
+ id: { type: "string", minLength: 1, description: "Node id." },
519
+ data: { type: "object", description: 'Data fields to merge (e.g. { "label": "New" }).' }
520
+ },
521
+ required: ["id", "data"],
522
+ additionalProperties: false
523
+ }
524
+ };
525
+ var MCP_TOOL_CANVAS_LAYOUT = {
526
+ name: "canvas_layout",
527
+ title: "Lay out the canvas",
528
+ annotations: { title: "Lay out the canvas", ...IDEMPOTENT_ANNOTATIONS },
529
+ description: "Arranges the canvas graph automatically.",
530
+ inputSchema: {
531
+ type: "object",
532
+ properties: {
533
+ direction: { type: "string", enum: ["LR", "TB", "RL", "BT"], description: "Layout direction (default LR)." },
534
+ incremental: {
535
+ type: "boolean",
536
+ description: "Keep nodes that already have a position where they are (default true)."
537
+ }
538
+ },
539
+ additionalProperties: false
540
+ }
541
+ };
542
+ var MCP_TOOL_CANVAS_SELECT = {
543
+ name: "canvas_select",
544
+ title: "Select canvas nodes",
545
+ annotations: { title: "Select canvas nodes", ...IDEMPOTENT_ANNOTATIONS },
546
+ description: "Replaces the canvas selection with these nodes (an empty list clears it).",
547
+ inputSchema: {
548
+ type: "object",
549
+ properties: { nodeIds: { ...NODE_IDS, minItems: 0 } },
550
+ required: ["nodeIds"],
551
+ additionalProperties: false
552
+ }
553
+ };
554
+ var MCP_TOOL_CANVAS_HIGHLIGHT = {
555
+ name: "canvas_highlight",
556
+ title: "Highlight canvas nodes",
557
+ annotations: { title: "Highlight canvas nodes", ...IDEMPOTENT_ANNOTATIONS },
558
+ description: "Briefly highlights nodes to draw the user's attention to them.",
559
+ inputSchema: {
560
+ type: "object",
561
+ properties: {
562
+ nodeIds: NODE_IDS,
563
+ color: { type: "string", description: "CSS color (default #3b82f6)." },
564
+ durationMs: { type: "integer", minimum: 0, maximum: 6e4, description: "Duration in ms (default 2000)." }
565
+ },
566
+ required: ["nodeIds"],
567
+ additionalProperties: false
568
+ }
569
+ };
570
+ var MCP_TOOL_PROMPT_INSERT_TEXT = {
571
+ name: "prompt_insert_text",
572
+ title: "Insert prompt text",
573
+ annotations: { title: "Insert prompt text", ...WRITE_ANNOTATIONS },
574
+ description: "Inserts text into the prompt at a position, or at the cursor when `at` is omitted.",
575
+ inputSchema: {
576
+ type: "object",
577
+ properties: {
578
+ text: { type: "string", description: "Text to insert." },
579
+ at: { ...DOC_POSITION, description: "Position (as in get_editor_state `document.selection`)." }
580
+ },
581
+ required: ["text"],
582
+ additionalProperties: false
583
+ }
584
+ };
585
+ var MCP_TOOL_PROMPT_REPLACE_RANGE = {
586
+ name: "prompt_replace_range",
587
+ title: "Replace prompt text",
588
+ annotations: { title: "Replace prompt text", ...DESTRUCTIVE_ANNOTATIONS },
589
+ description: "Replaces the prompt text between two positions (an empty text deletes it).",
590
+ inputSchema: {
591
+ type: "object",
592
+ properties: {
593
+ from: { ...DOC_POSITION, description: "Start position." },
594
+ to: { ...DOC_POSITION, description: "End position (>= from)." },
595
+ text: { type: "string", description: "Replacement text." }
596
+ },
597
+ required: ["from", "to", "text"],
598
+ additionalProperties: false
599
+ }
600
+ };
601
+ var MCP_TOOL_PROMPT_SET_VARIABLE = {
602
+ name: "prompt_set_variable",
603
+ title: "Set a prompt variable",
604
+ annotations: { title: "Set a prompt variable", ...DESTRUCTIVE_ANNOTATIONS, idempotentHint: true },
605
+ description: "Sets the value of a template variable used by the prompt.",
606
+ inputSchema: {
607
+ type: "object",
608
+ properties: {
609
+ name: { type: "string", minLength: 1, description: "Variable name." },
610
+ value: { description: "Variable value (any JSON value)." }
611
+ },
612
+ required: ["name", "value"],
613
+ additionalProperties: false
614
+ }
615
+ };
616
+ var MCP_TOOL_PROMPT_SELECT = {
617
+ name: "prompt_select",
618
+ title: "Select prompt text",
619
+ annotations: { title: "Select prompt text", ...IDEMPOTENT_ANNOTATIONS },
620
+ description: "Selects the prompt text between two positions (from === to places the cursor).",
621
+ inputSchema: {
622
+ type: "object",
623
+ properties: {
624
+ from: { ...DOC_POSITION, description: "Start position." },
625
+ to: { ...DOC_POSITION, description: "End position (>= from)." }
626
+ },
627
+ required: ["from", "to"],
628
+ additionalProperties: false
629
+ }
630
+ };
631
+ var EDITOR_CANVAS_TOOLS = [
632
+ MCP_TOOL_CANVAS_ADD_NODE,
633
+ MCP_TOOL_CANVAS_REMOVE_NODES,
634
+ MCP_TOOL_CANVAS_CONNECT,
635
+ MCP_TOOL_CANVAS_UPDATE_NODE,
636
+ MCP_TOOL_CANVAS_LAYOUT,
637
+ MCP_TOOL_CANVAS_SELECT,
638
+ MCP_TOOL_CANVAS_HIGHLIGHT
639
+ ];
640
+ var EDITOR_PROMPT_TOOLS = [
641
+ MCP_TOOL_PROMPT_INSERT_TEXT,
642
+ MCP_TOOL_PROMPT_REPLACE_RANGE,
643
+ MCP_TOOL_PROMPT_SET_VARIABLE,
644
+ MCP_TOOL_PROMPT_SELECT
645
+ ];
646
+ var EDIT_OPERATION_REQUIRED_ARGUMENTS = {
647
+ edit_canvas: {
648
+ add_node: ["node"],
649
+ remove_nodes: ["nodeIds"],
650
+ connect: ["source", "target"],
651
+ update_node: ["id"],
652
+ layout: [],
653
+ select: ["nodeIds"],
654
+ highlight: ["nodeIds"]
655
+ },
656
+ edit_prompt: {
657
+ insert_text: ["text"],
658
+ replace_range: ["from", "to", "text"],
659
+ set_variable: ["name"],
660
+ select: ["from", "to"]
661
+ }
662
+ };
663
+ var EXPORT_EXCLUDED_SELECTORS = [
664
+ ".pixerate-presence",
665
+ ".agent-presence",
666
+ ".ursula-presence"
667
+ ];
668
+ var EDITOR_MCP_TOOLS = [
669
+ MCP_TOOL_GET_EDITOR_STATE,
670
+ MCP_TOOL_EDIT_CANVAS,
671
+ MCP_TOOL_EDIT_PROMPT,
672
+ MCP_TOOL_VIEW_EDITOR
673
+ ];
674
+
675
+ // src/tools/mcp-server.ts
676
+ var MCP_PROTOCOL_VERSIONS = ["2025-06-18", "2025-03-26", "2024-11-05"];
677
+ var JSON_RPC_ERRORS = {
678
+ parseError: -32700,
679
+ invalidRequest: -32600,
680
+ methodNotFound: -32601,
681
+ invalidParams: -32602,
682
+ internalError: -32603
683
+ };
684
+ var errorResponse = (id, code, message, data) => ({
685
+ jsonrpc: "2.0",
686
+ id,
687
+ error: { code, message, ...data !== void 0 ? { data } : {} }
688
+ });
689
+ function createMcpServer(registry, options) {
690
+ let initialized = false;
691
+ let unsubscribe = null;
692
+ const running = /* @__PURE__ */ new Map();
693
+ const startNotifications = () => {
694
+ if (unsubscribe || !options.send) return;
695
+ const send = options.send;
696
+ unsubscribe = registry.onListChanged(() => send({ jsonrpc: "2.0", method: "notifications/tools/list_changed" }));
697
+ };
698
+ async function dispatch(request) {
699
+ const id = request.id ?? null;
700
+ const isNotification = request.id === void 0 || request.id === null;
701
+ const params = request.params ?? {};
702
+ switch (request.method) {
703
+ case "initialize": {
704
+ const requested = String(params.protocolVersion ?? "");
705
+ const protocolVersion = MCP_PROTOCOL_VERSIONS.includes(requested) ? requested : MCP_PROTOCOL_VERSIONS[0];
706
+ return {
707
+ jsonrpc: "2.0",
708
+ id,
709
+ result: {
710
+ protocolVersion,
711
+ capabilities: registry.capabilities,
712
+ serverInfo: { name: options.name, version: options.version, ...options.title ? { title: options.title } : {} },
713
+ ...options.instructions ? { instructions: options.instructions } : {}
714
+ }
715
+ };
716
+ }
717
+ case "notifications/initialized":
718
+ initialized = true;
719
+ startNotifications();
720
+ return null;
721
+ case "notifications/cancelled": {
722
+ const requestId = params.requestId;
723
+ if (requestId !== void 0) running.get(requestId)?.abort(params.reason);
724
+ return null;
725
+ }
726
+ case "ping":
727
+ return { jsonrpc: "2.0", id, result: {} };
728
+ case "tools/list":
729
+ return { jsonrpc: "2.0", id, result: registry.listTools() };
730
+ case "tools/call": {
731
+ const name = params.name;
732
+ if (typeof name !== "string") {
733
+ return errorResponse(id, JSON_RPC_ERRORS.invalidParams, 'tools/call needs a string "name".');
734
+ }
735
+ if (!registry.hasTool(name)) {
736
+ return errorResponse(id, JSON_RPC_ERRORS.invalidParams, `Unknown tool: ${name}`);
737
+ }
738
+ const args = params.arguments;
739
+ if (args !== void 0 && (typeof args !== "object" || args === null || Array.isArray(args))) {
740
+ return errorResponse(id, JSON_RPC_ERRORS.invalidParams, '"arguments" must be an object.');
741
+ }
742
+ const controller = new AbortController();
743
+ if (!isNotification) running.set(id, controller);
744
+ try {
745
+ const result = await registry.callTool(name, args ?? {}, { signal: controller.signal });
746
+ if (controller.signal.aborted) return null;
747
+ return { jsonrpc: "2.0", id, result };
748
+ } finally {
749
+ if (!isNotification) running.delete(id);
750
+ }
751
+ }
752
+ default:
753
+ if (isNotification) return null;
754
+ return errorResponse(id, JSON_RPC_ERRORS.methodNotFound, `Method not found: ${request.method}`);
755
+ }
756
+ }
757
+ return {
758
+ get initialized() {
759
+ return initialized;
760
+ },
761
+ async handle(message) {
762
+ let request;
763
+ if (typeof message === "string") {
764
+ try {
765
+ request = JSON.parse(message);
766
+ } catch {
767
+ return errorResponse(null, JSON_RPC_ERRORS.parseError, "Parse error.");
768
+ }
769
+ } else {
770
+ request = message;
771
+ }
772
+ if (!request || typeof request !== "object" || Array.isArray(request) || request.jsonrpc !== "2.0" || typeof request.method !== "string") {
773
+ const id = request && typeof request === "object" && !Array.isArray(request) ? request.id ?? null : null;
774
+ return errorResponse(id, JSON_RPC_ERRORS.invalidRequest, "Invalid JSON-RPC 2.0 request.");
775
+ }
776
+ try {
777
+ return await dispatch(request);
778
+ } catch (error) {
779
+ if (request.id === void 0 || request.id === null) return null;
780
+ return errorResponse(
781
+ request.id,
782
+ JSON_RPC_ERRORS.internalError,
783
+ error instanceof Error ? error.message : String(error)
784
+ );
785
+ }
786
+ },
787
+ close() {
788
+ unsubscribe?.();
789
+ unsubscribe = null;
790
+ for (const controller of running.values()) controller.abort("Server closed.");
791
+ running.clear();
792
+ }
793
+ };
794
+ }
795
+
796
+ export {
797
+ ToolRegistryError,
798
+ TOOL_NAMESPACE_SEPARATOR,
799
+ toToolResult,
800
+ ToolRegistry,
801
+ createToolRegistry,
802
+ MCP_TOOL_GET_EDITOR_STATE,
803
+ MCP_TOOL_EDIT_CANVAS,
804
+ MCP_TOOL_EDIT_PROMPT,
805
+ MCP_TOOL_VIEW_EDITOR,
806
+ MCP_TOOL_CANVAS_ADD_NODE,
807
+ MCP_TOOL_CANVAS_REMOVE_NODES,
808
+ MCP_TOOL_CANVAS_CONNECT,
809
+ MCP_TOOL_CANVAS_UPDATE_NODE,
810
+ MCP_TOOL_CANVAS_LAYOUT,
811
+ MCP_TOOL_CANVAS_SELECT,
812
+ MCP_TOOL_CANVAS_HIGHLIGHT,
813
+ MCP_TOOL_PROMPT_INSERT_TEXT,
814
+ MCP_TOOL_PROMPT_REPLACE_RANGE,
815
+ MCP_TOOL_PROMPT_SET_VARIABLE,
816
+ MCP_TOOL_PROMPT_SELECT,
817
+ EDITOR_CANVAS_TOOLS,
818
+ EDITOR_PROMPT_TOOLS,
819
+ EDIT_OPERATION_REQUIRED_ARGUMENTS,
820
+ EXPORT_EXCLUDED_SELECTORS,
821
+ EDITOR_MCP_TOOLS,
822
+ MCP_PROTOCOL_VERSIONS,
823
+ JSON_RPC_ERRORS,
824
+ createMcpServer
825
+ };
826
+ //# sourceMappingURL=chunk-HHQ4XJWT.js.map