@atezer/figma-mcp-bridge 1.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +241 -0
- package/dist/browser/base.d.ts +50 -0
- package/dist/browser/base.d.ts.map +1 -0
- package/dist/browser/base.js +6 -0
- package/dist/browser/base.js.map +1 -0
- package/dist/browser/local.d.ts +81 -0
- package/dist/browser/local.d.ts.map +1 -0
- package/dist/browser/local.js +283 -0
- package/dist/browser/local.js.map +1 -0
- package/dist/cloudflare/browser/base.js +5 -0
- package/dist/cloudflare/browser/cloudflare.js +156 -0
- package/dist/cloudflare/browser-manager.js +157 -0
- package/dist/cloudflare/core/audit-log.js +62 -0
- package/dist/cloudflare/core/config.js +163 -0
- package/dist/cloudflare/core/console-monitor.js +427 -0
- package/dist/cloudflare/core/design-system-manifest.js +260 -0
- package/dist/cloudflare/core/enrichment/enrichment-service.js +272 -0
- package/dist/cloudflare/core/enrichment/index.js +7 -0
- package/dist/cloudflare/core/enrichment/relationship-mapper.js +351 -0
- package/dist/cloudflare/core/enrichment/style-resolver.js +326 -0
- package/dist/cloudflare/core/figma-api.js +273 -0
- package/dist/cloudflare/core/figma-desktop-connector.js +1029 -0
- package/dist/cloudflare/core/figma-reconstruction-spec.js +402 -0
- package/dist/cloudflare/core/figma-style-extractor.js +311 -0
- package/dist/cloudflare/core/figma-tools.js +2883 -0
- package/dist/cloudflare/core/logger.js +53 -0
- package/dist/cloudflare/core/plugin-bridge-connector.js +154 -0
- package/dist/cloudflare/core/plugin-bridge-server.js +174 -0
- package/dist/cloudflare/core/snippet-injector.js +96 -0
- package/dist/cloudflare/core/types/enriched.js +5 -0
- package/dist/cloudflare/core/types/index.js +4 -0
- package/dist/cloudflare/index.js +1061 -0
- package/dist/cloudflare/test-browser.js +88 -0
- package/dist/core/audit-log.d.ts +26 -0
- package/dist/core/audit-log.d.ts.map +1 -0
- package/dist/core/audit-log.js +63 -0
- package/dist/core/audit-log.js.map +1 -0
- package/dist/core/config.d.ts +17 -0
- package/dist/core/config.d.ts.map +1 -0
- package/dist/core/config.js +164 -0
- package/dist/core/config.js.map +1 -0
- package/dist/core/console-monitor.d.ts +82 -0
- package/dist/core/console-monitor.d.ts.map +1 -0
- package/dist/core/console-monitor.js +428 -0
- package/dist/core/console-monitor.js.map +1 -0
- package/dist/core/design-system-manifest.d.ts +272 -0
- package/dist/core/design-system-manifest.d.ts.map +1 -0
- package/dist/core/design-system-manifest.js +261 -0
- package/dist/core/design-system-manifest.js.map +1 -0
- package/dist/core/enrichment/enrichment-service.d.ts +52 -0
- package/dist/core/enrichment/enrichment-service.d.ts.map +1 -0
- package/dist/core/enrichment/enrichment-service.js +273 -0
- package/dist/core/enrichment/enrichment-service.js.map +1 -0
- package/dist/core/enrichment/index.d.ts +8 -0
- package/dist/core/enrichment/index.d.ts.map +1 -0
- package/dist/core/enrichment/index.js +8 -0
- package/dist/core/enrichment/index.js.map +1 -0
- package/dist/core/enrichment/relationship-mapper.d.ts +106 -0
- package/dist/core/enrichment/relationship-mapper.d.ts.map +1 -0
- package/dist/core/enrichment/relationship-mapper.js +352 -0
- package/dist/core/enrichment/relationship-mapper.js.map +1 -0
- package/dist/core/enrichment/style-resolver.d.ts +80 -0
- package/dist/core/enrichment/style-resolver.d.ts.map +1 -0
- package/dist/core/enrichment/style-resolver.js +327 -0
- package/dist/core/enrichment/style-resolver.js.map +1 -0
- package/dist/core/figma-api.d.ts +137 -0
- package/dist/core/figma-api.d.ts.map +1 -0
- package/dist/core/figma-api.js +274 -0
- package/dist/core/figma-api.js.map +1 -0
- package/dist/core/figma-desktop-connector.d.ts +238 -0
- package/dist/core/figma-desktop-connector.d.ts.map +1 -0
- package/dist/core/figma-desktop-connector.js +1030 -0
- package/dist/core/figma-desktop-connector.js.map +1 -0
- package/dist/core/figma-reconstruction-spec.d.ts +166 -0
- package/dist/core/figma-reconstruction-spec.d.ts.map +1 -0
- package/dist/core/figma-reconstruction-spec.js +403 -0
- package/dist/core/figma-reconstruction-spec.js.map +1 -0
- package/dist/core/figma-style-extractor.d.ts +76 -0
- package/dist/core/figma-style-extractor.d.ts.map +1 -0
- package/dist/core/figma-style-extractor.js +312 -0
- package/dist/core/figma-style-extractor.js.map +1 -0
- package/dist/core/figma-tools.d.ts +21 -0
- package/dist/core/figma-tools.d.ts.map +1 -0
- package/dist/core/figma-tools.js +2884 -0
- package/dist/core/figma-tools.js.map +1 -0
- package/dist/core/logger.d.ts +22 -0
- package/dist/core/logger.d.ts.map +1 -0
- package/dist/core/logger.js +54 -0
- package/dist/core/logger.js.map +1 -0
- package/dist/core/plugin-bridge-connector.d.ts +133 -0
- package/dist/core/plugin-bridge-connector.d.ts.map +1 -0
- package/dist/core/plugin-bridge-connector.js +155 -0
- package/dist/core/plugin-bridge-connector.js.map +1 -0
- package/dist/core/plugin-bridge-server.d.ts +42 -0
- package/dist/core/plugin-bridge-server.d.ts.map +1 -0
- package/dist/core/plugin-bridge-server.js +175 -0
- package/dist/core/plugin-bridge-server.js.map +1 -0
- package/dist/core/snippet-injector.d.ts +24 -0
- package/dist/core/snippet-injector.d.ts.map +1 -0
- package/dist/core/snippet-injector.js +97 -0
- package/dist/core/snippet-injector.js.map +1 -0
- package/dist/core/types/enriched.d.ts +213 -0
- package/dist/core/types/enriched.d.ts.map +1 -0
- package/dist/core/types/enriched.js +6 -0
- package/dist/core/types/enriched.js.map +1 -0
- package/dist/core/types/index.d.ts +116 -0
- package/dist/core/types/index.d.ts.map +1 -0
- package/dist/core/types/index.js +5 -0
- package/dist/core/types/index.js.map +1 -0
- package/dist/local-plugin-only.d.ts +13 -0
- package/dist/local-plugin-only.d.ts.map +1 -0
- package/dist/local-plugin-only.js +567 -0
- package/dist/local-plugin-only.js.map +1 -0
- package/dist/local.d.ts +73 -0
- package/dist/local.d.ts.map +1 -0
- package/dist/local.js +2466 -0
- package/dist/local.js.map +1 -0
- package/f-mcp-plugin/README.md +280 -0
- package/f-mcp-plugin/code.js +2222 -0
- package/f-mcp-plugin/manifest.json +14 -0
- package/f-mcp-plugin/ui.html +877 -0
- package/package.json +82 -0
|
@@ -0,0 +1,1029 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Figma Desktop Connector
|
|
3
|
+
*
|
|
4
|
+
* This service connects directly to Figma Desktop's plugin context
|
|
5
|
+
* to execute code with access to the full Figma Plugin API,
|
|
6
|
+
* including variables without Enterprise access.
|
|
7
|
+
*
|
|
8
|
+
* Uses Puppeteer's Worker API to directly access plugin workers,
|
|
9
|
+
* bypassing CDP context enumeration limitations.
|
|
10
|
+
*/
|
|
11
|
+
import { logger } from './logger.js';
|
|
12
|
+
export class FigmaDesktopConnector {
|
|
13
|
+
constructor(page) {
|
|
14
|
+
this.page = page;
|
|
15
|
+
}
|
|
16
|
+
/**
|
|
17
|
+
* Initialize connection to Figma Desktop's plugin context
|
|
18
|
+
* No setup needed - Puppeteer handles worker access automatically
|
|
19
|
+
*/
|
|
20
|
+
async initialize() {
|
|
21
|
+
logger.info('Figma Desktop connector initialized (using Puppeteer Worker API)');
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
24
|
+
* Execute code in Figma's plugin context where the figma API is available
|
|
25
|
+
* Uses Puppeteer's direct worker access instead of CDP context enumeration
|
|
26
|
+
*/
|
|
27
|
+
async executeInPluginContext(code) {
|
|
28
|
+
try {
|
|
29
|
+
// Use Puppeteer's worker API directly - this can access plugin workers
|
|
30
|
+
// that CDP's Runtime.getExecutionContexts cannot enumerate
|
|
31
|
+
const workers = this.page.workers();
|
|
32
|
+
// Log to browser console so MCP can capture it
|
|
33
|
+
await this.page.evaluate((count, urls) => {
|
|
34
|
+
console.log(`[DESKTOP_CONNECTOR] Found ${count} workers via Puppeteer API:`, urls);
|
|
35
|
+
}, workers.length, workers.map(w => w.url()));
|
|
36
|
+
logger.info({
|
|
37
|
+
workerCount: workers.length,
|
|
38
|
+
workerUrls: workers.map(w => w.url())
|
|
39
|
+
}, 'Found workers via Puppeteer API');
|
|
40
|
+
// Try each worker to find one with figma API
|
|
41
|
+
for (const worker of workers) {
|
|
42
|
+
try {
|
|
43
|
+
// Log to browser console
|
|
44
|
+
await this.page.evaluate((url) => {
|
|
45
|
+
console.log(`[DESKTOP_CONNECTOR] Checking worker: ${url}`);
|
|
46
|
+
}, worker.url());
|
|
47
|
+
// Check if this worker has the figma API
|
|
48
|
+
// Use string evaluation to avoid TypeScript errors about figma global
|
|
49
|
+
const hasFigmaApi = await worker.evaluate('typeof figma !== "undefined"');
|
|
50
|
+
// Log result to browser console
|
|
51
|
+
await this.page.evaluate((url, hasApi) => {
|
|
52
|
+
console.log(`[DESKTOP_CONNECTOR] Worker ${url} has figma API: ${hasApi}`);
|
|
53
|
+
}, worker.url(), hasFigmaApi);
|
|
54
|
+
if (hasFigmaApi) {
|
|
55
|
+
logger.info({ workerUrl: worker.url() }, 'Found worker with Figma API');
|
|
56
|
+
await this.page.evaluate((url) => {
|
|
57
|
+
console.log(`[DESKTOP_CONNECTOR] ✅ SUCCESS! Found worker with Figma API: ${url}`);
|
|
58
|
+
}, worker.url());
|
|
59
|
+
// Execute the code in this worker context
|
|
60
|
+
// Wrap the code in a function to ensure proper evaluation
|
|
61
|
+
const wrappedCode = `(${code})`;
|
|
62
|
+
const result = await worker.evaluate(wrappedCode);
|
|
63
|
+
return result;
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
catch (workerError) {
|
|
67
|
+
// This worker doesn't have figma API or evaluation failed, try next
|
|
68
|
+
await this.page.evaluate((url, err) => {
|
|
69
|
+
console.error(`[DESKTOP_CONNECTOR] ❌ Worker ${url} check failed:`, err);
|
|
70
|
+
}, worker.url(), workerError instanceof Error ? workerError.message : String(workerError));
|
|
71
|
+
logger.error({ error: workerError, workerUrl: worker.url() }, 'Worker check failed, trying next');
|
|
72
|
+
continue;
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
// If no worker found with figma API, throw error
|
|
76
|
+
throw new Error('No plugin worker found with Figma API. Make sure a plugin is running in Figma Desktop.');
|
|
77
|
+
}
|
|
78
|
+
catch (error) {
|
|
79
|
+
logger.error({ error, code: code.substring(0, 200) }, 'Failed to execute in plugin context');
|
|
80
|
+
throw error;
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
/**
|
|
84
|
+
* Get Figma variables from plugin UI window object
|
|
85
|
+
* This bypasses Figma's plugin sandbox security restrictions
|
|
86
|
+
* by accessing data that the plugin posted to its UI iframe
|
|
87
|
+
*/
|
|
88
|
+
async getVariablesFromPluginUI(fileKey) {
|
|
89
|
+
try {
|
|
90
|
+
// Log to browser console
|
|
91
|
+
await this.page.evaluate((key) => {
|
|
92
|
+
console.log(`[DESKTOP_CONNECTOR] 🚀 getVariablesFromPluginUI() called, fileKey: ${key}`);
|
|
93
|
+
}, fileKey);
|
|
94
|
+
logger.info({ fileKey }, 'Getting variables from plugin UI iframe');
|
|
95
|
+
// Get all frames (iframes) in the page
|
|
96
|
+
const frames = this.page.frames();
|
|
97
|
+
await this.page.evaluate((count) => {
|
|
98
|
+
console.log(`[DESKTOP_CONNECTOR] Found ${count} frames (iframes)`);
|
|
99
|
+
}, frames.length);
|
|
100
|
+
logger.info({ frameCount: frames.length }, 'Found frames in page');
|
|
101
|
+
// Try to find plugin UI iframe with variables data
|
|
102
|
+
for (const frame of frames) {
|
|
103
|
+
try {
|
|
104
|
+
const frameUrl = frame.url();
|
|
105
|
+
await this.page.evaluate((url) => {
|
|
106
|
+
console.log(`[DESKTOP_CONNECTOR] Checking frame: ${url}`);
|
|
107
|
+
}, frameUrl);
|
|
108
|
+
// Check if this frame has our variables data
|
|
109
|
+
const hasData = await frame.evaluate('typeof window.__figmaVariablesData !== "undefined" && window.__figmaVariablesReady === true');
|
|
110
|
+
await this.page.evaluate((url, has) => {
|
|
111
|
+
console.log(`[DESKTOP_CONNECTOR] Frame ${url} has variables data: ${has}`);
|
|
112
|
+
}, frameUrl, hasData);
|
|
113
|
+
if (hasData) {
|
|
114
|
+
logger.info({ frameUrl }, 'Found frame with variables data');
|
|
115
|
+
await this.page.evaluate((url) => {
|
|
116
|
+
console.log(`[DESKTOP_CONNECTOR] ✅ SUCCESS! Found plugin UI with variables data: ${url}`);
|
|
117
|
+
}, frameUrl);
|
|
118
|
+
// Get the data from window object
|
|
119
|
+
const result = await frame.evaluate('window.__figmaVariablesData');
|
|
120
|
+
logger.info({
|
|
121
|
+
variableCount: result.variables?.length,
|
|
122
|
+
collectionCount: result.variableCollections?.length
|
|
123
|
+
}, 'Successfully retrieved variables from plugin UI');
|
|
124
|
+
await this.page.evaluate((varCount, collCount) => {
|
|
125
|
+
console.log(`[DESKTOP_CONNECTOR] ✅ Retrieved ${varCount} variables in ${collCount} collections`);
|
|
126
|
+
}, result.variables?.length || 0, result.variableCollections?.length || 0);
|
|
127
|
+
return result;
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
catch (frameError) {
|
|
131
|
+
await this.page.evaluate((url, err) => {
|
|
132
|
+
console.log(`[DESKTOP_CONNECTOR] Frame ${url} check failed: ${err}`);
|
|
133
|
+
}, frame.url(), frameError instanceof Error ? frameError.message : String(frameError));
|
|
134
|
+
logger.debug({ error: frameError, frameUrl: frame.url() }, 'Frame check failed, trying next');
|
|
135
|
+
continue;
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
// If no frame found with data, throw error
|
|
139
|
+
throw new Error('No plugin UI found with variables data. Make sure the Variables Exporter (Persistent) plugin is running.');
|
|
140
|
+
}
|
|
141
|
+
catch (error) {
|
|
142
|
+
logger.error({ error }, 'Failed to get variables from plugin UI');
|
|
143
|
+
await this.page.evaluate((msg) => {
|
|
144
|
+
console.error('[DESKTOP_CONNECTOR] ❌ getVariablesFromPluginUI failed:', msg);
|
|
145
|
+
}, error instanceof Error ? error.message : String(error));
|
|
146
|
+
throw error;
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
/**
|
|
150
|
+
* Get component data by node ID from plugin UI window object
|
|
151
|
+
* This bypasses the REST API bug where descriptions are missing
|
|
152
|
+
* by accessing data from the F-MCP ATezer Bridge plugin via its UI iframe
|
|
153
|
+
*/
|
|
154
|
+
async getComponentFromPluginUI(nodeId) {
|
|
155
|
+
try {
|
|
156
|
+
// Log to browser console
|
|
157
|
+
await this.page.evaluate((id) => {
|
|
158
|
+
console.log(`[DESKTOP_CONNECTOR] 🎯 getComponentFromPluginUI() called, nodeId: ${id}`);
|
|
159
|
+
}, nodeId);
|
|
160
|
+
logger.info({ nodeId }, 'Getting component from plugin UI iframe');
|
|
161
|
+
// Get all frames (iframes) in the page
|
|
162
|
+
const frames = this.page.frames();
|
|
163
|
+
await this.page.evaluate((count) => {
|
|
164
|
+
console.log(`[DESKTOP_CONNECTOR] Found ${count} frames (iframes)`);
|
|
165
|
+
}, frames.length);
|
|
166
|
+
logger.info({ frameCount: frames.length }, 'Found frames in page');
|
|
167
|
+
// Try to find plugin UI iframe with requestComponentData function
|
|
168
|
+
for (const frame of frames) {
|
|
169
|
+
try {
|
|
170
|
+
const frameUrl = frame.url();
|
|
171
|
+
await this.page.evaluate((url) => {
|
|
172
|
+
console.log(`[DESKTOP_CONNECTOR] Checking frame: ${url}`);
|
|
173
|
+
}, frameUrl);
|
|
174
|
+
// Check if this frame has our requestComponentData function
|
|
175
|
+
const hasFunction = await frame.evaluate('typeof window.requestComponentData === "function"');
|
|
176
|
+
await this.page.evaluate((url, has) => {
|
|
177
|
+
console.log(`[DESKTOP_CONNECTOR] Frame ${url} has requestComponentData: ${has}`);
|
|
178
|
+
}, frameUrl, hasFunction);
|
|
179
|
+
if (hasFunction) {
|
|
180
|
+
logger.info({ frameUrl }, 'Found frame with requestComponentData function');
|
|
181
|
+
await this.page.evaluate((url) => {
|
|
182
|
+
console.log(`[DESKTOP_CONNECTOR] ✅ SUCCESS! Found plugin UI with requestComponentData: ${url}`);
|
|
183
|
+
}, frameUrl);
|
|
184
|
+
// Call the function with the nodeId - it returns a Promise
|
|
185
|
+
// Use JSON.stringify to safely pass the nodeId as a string literal
|
|
186
|
+
const result = await frame.evaluate(`window.requestComponentData(${JSON.stringify(nodeId)})`);
|
|
187
|
+
logger.info({
|
|
188
|
+
nodeId,
|
|
189
|
+
componentName: result.component?.name,
|
|
190
|
+
hasDescription: !!result.component?.description
|
|
191
|
+
}, 'Successfully retrieved component from plugin UI');
|
|
192
|
+
await this.page.evaluate((name, hasDesc) => {
|
|
193
|
+
console.log(`[DESKTOP_CONNECTOR] ✅ Retrieved component "${name}", has description: ${hasDesc}`);
|
|
194
|
+
}, result.component?.name, !!result.component?.description);
|
|
195
|
+
return result;
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
catch (frameError) {
|
|
199
|
+
await this.page.evaluate((url, err) => {
|
|
200
|
+
console.log(`[DESKTOP_CONNECTOR] Frame ${url} check failed: ${err}`);
|
|
201
|
+
}, frame.url(), frameError instanceof Error ? frameError.message : String(frameError));
|
|
202
|
+
logger.debug({ error: frameError, frameUrl: frame.url() }, 'Frame check failed, trying next');
|
|
203
|
+
continue;
|
|
204
|
+
}
|
|
205
|
+
}
|
|
206
|
+
// If no frame found with function, throw error
|
|
207
|
+
throw new Error('No plugin UI found with requestComponentData function. Make sure the F-MCP ATezer Bridge plugin is running.');
|
|
208
|
+
}
|
|
209
|
+
catch (error) {
|
|
210
|
+
logger.error({ error, nodeId }, 'Failed to get component from plugin UI');
|
|
211
|
+
await this.page.evaluate((msg) => {
|
|
212
|
+
console.error('[DESKTOP_CONNECTOR] ❌ getComponentFromPluginUI failed:', msg);
|
|
213
|
+
}, error instanceof Error ? error.message : String(error));
|
|
214
|
+
throw error;
|
|
215
|
+
}
|
|
216
|
+
}
|
|
217
|
+
/**
|
|
218
|
+
* Get Figma variables using the desktop connection
|
|
219
|
+
* This bypasses the Enterprise requirement!
|
|
220
|
+
*/
|
|
221
|
+
async getVariables(fileKey) {
|
|
222
|
+
// Log to browser console
|
|
223
|
+
await this.page.evaluate((key) => {
|
|
224
|
+
console.log(`[DESKTOP_CONNECTOR] 🚀 getVariables() called, fileKey: ${key}`);
|
|
225
|
+
}, fileKey);
|
|
226
|
+
logger.info({ fileKey }, 'Getting variables via Desktop connection');
|
|
227
|
+
const code = `
|
|
228
|
+
(async () => {
|
|
229
|
+
try {
|
|
230
|
+
// Check if we're in the right context
|
|
231
|
+
if (typeof figma === 'undefined') {
|
|
232
|
+
throw new Error('Figma API not available in this context');
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
// Get variables just like the official MCP does
|
|
236
|
+
const variables = await figma.variables.getLocalVariablesAsync();
|
|
237
|
+
const collections = await figma.variables.getLocalVariableCollectionsAsync();
|
|
238
|
+
|
|
239
|
+
// Format the response with file metadata for context verification
|
|
240
|
+
const result = {
|
|
241
|
+
success: true,
|
|
242
|
+
timestamp: Date.now(),
|
|
243
|
+
// Include file metadata so we can verify we're querying the right file
|
|
244
|
+
fileMetadata: {
|
|
245
|
+
fileName: figma.root.name,
|
|
246
|
+
fileKey: figma.fileKey || null
|
|
247
|
+
},
|
|
248
|
+
variables: variables.map(v => ({
|
|
249
|
+
id: v.id,
|
|
250
|
+
name: v.name,
|
|
251
|
+
key: v.key,
|
|
252
|
+
resolvedType: v.resolvedType,
|
|
253
|
+
valuesByMode: v.valuesByMode,
|
|
254
|
+
variableCollectionId: v.variableCollectionId,
|
|
255
|
+
scopes: v.scopes,
|
|
256
|
+
description: v.description,
|
|
257
|
+
hiddenFromPublishing: v.hiddenFromPublishing
|
|
258
|
+
})),
|
|
259
|
+
variableCollections: collections.map(c => ({
|
|
260
|
+
id: c.id,
|
|
261
|
+
name: c.name,
|
|
262
|
+
key: c.key,
|
|
263
|
+
modes: c.modes,
|
|
264
|
+
defaultModeId: c.defaultModeId,
|
|
265
|
+
variableIds: c.variableIds
|
|
266
|
+
}))
|
|
267
|
+
};
|
|
268
|
+
|
|
269
|
+
return result;
|
|
270
|
+
} catch (error) {
|
|
271
|
+
return {
|
|
272
|
+
success: false,
|
|
273
|
+
error: error.message
|
|
274
|
+
};
|
|
275
|
+
}
|
|
276
|
+
})()
|
|
277
|
+
`;
|
|
278
|
+
try {
|
|
279
|
+
const result = await this.executeInPluginContext(code);
|
|
280
|
+
if (!result.success) {
|
|
281
|
+
throw new Error(result.error || 'Failed to get variables');
|
|
282
|
+
}
|
|
283
|
+
logger.info({
|
|
284
|
+
variableCount: result.variables?.length,
|
|
285
|
+
collectionCount: result.variableCollections?.length
|
|
286
|
+
}, 'Successfully retrieved variables via Desktop');
|
|
287
|
+
return result;
|
|
288
|
+
}
|
|
289
|
+
catch (error) {
|
|
290
|
+
logger.error({ error }, 'Failed to get variables via Desktop');
|
|
291
|
+
throw error;
|
|
292
|
+
}
|
|
293
|
+
}
|
|
294
|
+
/**
|
|
295
|
+
* Clean up resources (no-op since we use Puppeteer's built-in worker management)
|
|
296
|
+
*/
|
|
297
|
+
/**
|
|
298
|
+
* Get component data by node ID using Plugin API
|
|
299
|
+
* This bypasses the REST API bug where descriptions are missing
|
|
300
|
+
*/
|
|
301
|
+
async getComponentByNodeId(nodeId) {
|
|
302
|
+
await this.page.evaluate((id) => {
|
|
303
|
+
console.log(`[DESKTOP_CONNECTOR] 🎯 getComponentByNodeId() called, nodeId: ${id}`);
|
|
304
|
+
}, nodeId);
|
|
305
|
+
logger.info({ nodeId }, 'Getting component via Desktop Plugin API');
|
|
306
|
+
const code = `
|
|
307
|
+
(async () => {
|
|
308
|
+
try {
|
|
309
|
+
// Check if we're in the right context
|
|
310
|
+
if (typeof figma === 'undefined') {
|
|
311
|
+
throw new Error('Figma API not available in this context');
|
|
312
|
+
}
|
|
313
|
+
|
|
314
|
+
// Get the node by ID
|
|
315
|
+
const node = figma.getNodeById('${nodeId}');
|
|
316
|
+
|
|
317
|
+
if (!node) {
|
|
318
|
+
throw new Error('Node not found with ID: ${nodeId}');
|
|
319
|
+
}
|
|
320
|
+
|
|
321
|
+
// Check if it's a component-like node
|
|
322
|
+
if (node.type !== 'COMPONENT' && node.type !== 'COMPONENT_SET' && node.type !== 'INSTANCE') {
|
|
323
|
+
throw new Error('Node is not a component, component set, or instance. Type: ' + node.type);
|
|
324
|
+
}
|
|
325
|
+
|
|
326
|
+
// Detect if this is a variant (COMPONENT inside a COMPONENT_SET)
|
|
327
|
+
// Note: Can't use optional chaining (?.) - Figma plugin sandbox doesn't support it
|
|
328
|
+
const isVariant = node.type === 'COMPONENT' && node.parent && node.parent.type === 'COMPONENT_SET';
|
|
329
|
+
|
|
330
|
+
// Extract component data including description fields
|
|
331
|
+
const result = {
|
|
332
|
+
success: true,
|
|
333
|
+
timestamp: Date.now(),
|
|
334
|
+
component: {
|
|
335
|
+
id: node.id,
|
|
336
|
+
name: node.name,
|
|
337
|
+
type: node.type,
|
|
338
|
+
// Variants CAN have their own description
|
|
339
|
+
description: node.description || null,
|
|
340
|
+
descriptionMarkdown: node.descriptionMarkdown || null,
|
|
341
|
+
// Include other useful properties
|
|
342
|
+
visible: node.visible,
|
|
343
|
+
locked: node.locked,
|
|
344
|
+
// Flag to indicate if this is a variant
|
|
345
|
+
isVariant: isVariant,
|
|
346
|
+
// For component sets and non-variant components only (variants cannot access this)
|
|
347
|
+
componentPropertyDefinitions: node.type === 'COMPONENT_SET' || (node.type === 'COMPONENT' && !isVariant)
|
|
348
|
+
? node.componentPropertyDefinitions
|
|
349
|
+
: undefined,
|
|
350
|
+
// Get children info (lightweight)
|
|
351
|
+
children: node.children ? node.children.map(child => ({
|
|
352
|
+
id: child.id,
|
|
353
|
+
name: child.name,
|
|
354
|
+
type: child.type
|
|
355
|
+
})) : undefined
|
|
356
|
+
}
|
|
357
|
+
};
|
|
358
|
+
|
|
359
|
+
return result;
|
|
360
|
+
} catch (error) {
|
|
361
|
+
return {
|
|
362
|
+
success: false,
|
|
363
|
+
error: error.message,
|
|
364
|
+
stack: error.stack
|
|
365
|
+
};
|
|
366
|
+
}
|
|
367
|
+
})()
|
|
368
|
+
`;
|
|
369
|
+
try {
|
|
370
|
+
const result = await this.executeInPluginContext(code);
|
|
371
|
+
if (!result.success) {
|
|
372
|
+
throw new Error(result.error || 'Failed to get component data');
|
|
373
|
+
}
|
|
374
|
+
logger.info({
|
|
375
|
+
nodeId,
|
|
376
|
+
componentName: result.component?.name,
|
|
377
|
+
hasDescription: !!result.component?.description
|
|
378
|
+
}, 'Successfully retrieved component via Desktop Plugin API');
|
|
379
|
+
await this.page.evaluate((name, hasDesc) => {
|
|
380
|
+
console.log(`[DESKTOP_CONNECTOR] ✅ Retrieved component "${name}", has description: ${hasDesc}`);
|
|
381
|
+
}, result.component?.name, !!result.component?.description);
|
|
382
|
+
return result;
|
|
383
|
+
}
|
|
384
|
+
catch (error) {
|
|
385
|
+
logger.error({ error, nodeId }, 'Failed to get component via Desktop Plugin API');
|
|
386
|
+
await this.page.evaluate((id, err) => {
|
|
387
|
+
console.error(`[DESKTOP_CONNECTOR] ❌ getComponentByNodeId failed for ${id}:`, err);
|
|
388
|
+
}, nodeId, error instanceof Error ? error.message : String(error));
|
|
389
|
+
throw error;
|
|
390
|
+
}
|
|
391
|
+
}
|
|
392
|
+
async dispose() {
|
|
393
|
+
logger.info('Figma Desktop connector disposed');
|
|
394
|
+
}
|
|
395
|
+
// ============================================================================
|
|
396
|
+
// WRITE OPERATIONS - Execute commands via Plugin UI iframe
|
|
397
|
+
// ============================================================================
|
|
398
|
+
/**
|
|
399
|
+
* Find the F-MCP ATezer Bridge plugin UI iframe
|
|
400
|
+
* Returns the frame that has the write operation functions
|
|
401
|
+
* Handles detached frame errors gracefully
|
|
402
|
+
*/
|
|
403
|
+
async findPluginUIFrame() {
|
|
404
|
+
// Get fresh frames - don't cache this
|
|
405
|
+
const frames = this.page.frames();
|
|
406
|
+
logger.debug({ frameCount: frames.length }, 'Searching for F-MCP ATezer Bridge plugin UI frame');
|
|
407
|
+
for (const frame of frames) {
|
|
408
|
+
try {
|
|
409
|
+
// Skip detached frames
|
|
410
|
+
if (frame.isDetached()) {
|
|
411
|
+
continue;
|
|
412
|
+
}
|
|
413
|
+
// Check if this frame has the executeCode function (our F-MCP ATezer Bridge plugin)
|
|
414
|
+
const hasWriteOps = await frame.evaluate('typeof window.executeCode === "function"');
|
|
415
|
+
if (hasWriteOps) {
|
|
416
|
+
logger.info({ frameUrl: frame.url() }, 'Found F-MCP ATezer Bridge plugin UI frame');
|
|
417
|
+
return frame;
|
|
418
|
+
}
|
|
419
|
+
}
|
|
420
|
+
catch (error) {
|
|
421
|
+
// Frame might be inaccessible or detached, continue to next
|
|
422
|
+
const errorMsg = error instanceof Error ? error.message : String(error);
|
|
423
|
+
if (errorMsg.includes('detached')) {
|
|
424
|
+
logger.debug({ frameUrl: frame.url() }, 'Frame was detached, skipping');
|
|
425
|
+
}
|
|
426
|
+
continue;
|
|
427
|
+
}
|
|
428
|
+
}
|
|
429
|
+
throw new Error('F-MCP ATezer Bridge plugin UI not found. Make sure the F-MCP ATezer Bridge plugin is running in Figma. ' +
|
|
430
|
+
'The plugin must be open for write operations to work.');
|
|
431
|
+
}
|
|
432
|
+
/**
|
|
433
|
+
* Execute arbitrary code in Figma's plugin context
|
|
434
|
+
* This is the power tool that can run any Figma Plugin API code
|
|
435
|
+
* Includes retry logic for detached frame errors
|
|
436
|
+
*/
|
|
437
|
+
async executeCodeViaUI(code, timeout = 5000) {
|
|
438
|
+
// Log to console (but don't fail if page is stale - this is just for debugging)
|
|
439
|
+
try {
|
|
440
|
+
await this.page.evaluate((codeStr, timeoutMs) => {
|
|
441
|
+
console.log(`[DESKTOP_CONNECTOR] executeCodeViaUI() called, code length: ${codeStr.length}, timeout: ${timeoutMs}ms`);
|
|
442
|
+
}, code, timeout);
|
|
443
|
+
}
|
|
444
|
+
catch (logError) {
|
|
445
|
+
// If even page logging fails, the page is likely stale
|
|
446
|
+
const errorMsg = logError instanceof Error ? logError.message : String(logError);
|
|
447
|
+
if (errorMsg.includes('detached')) {
|
|
448
|
+
throw new Error(`Page or frame is detached. Please refresh the Figma page or reconnect. Original error: ${errorMsg}`);
|
|
449
|
+
}
|
|
450
|
+
// For other errors, just continue - logging is not critical
|
|
451
|
+
logger.warn({ error: errorMsg }, 'Failed to log to page console');
|
|
452
|
+
}
|
|
453
|
+
logger.info({ codeLength: code.length, timeout }, 'Executing code via plugin UI');
|
|
454
|
+
// Retry logic for detached frame errors
|
|
455
|
+
const maxRetries = 2;
|
|
456
|
+
let lastError = null;
|
|
457
|
+
for (let attempt = 1; attempt <= maxRetries; attempt++) {
|
|
458
|
+
try {
|
|
459
|
+
const frame = await this.findPluginUIFrame();
|
|
460
|
+
// Check if frame is still valid before using it
|
|
461
|
+
if (frame.isDetached()) {
|
|
462
|
+
throw new Error('Frame became detached');
|
|
463
|
+
}
|
|
464
|
+
// Call the executeCode function in the UI iframe
|
|
465
|
+
const result = await frame.evaluate(`window.executeCode(${JSON.stringify(code)}, ${timeout})`);
|
|
466
|
+
logger.info({ success: result.success, error: result.error }, 'Code execution completed');
|
|
467
|
+
await this.page.evaluate((success, errorMsg) => {
|
|
468
|
+
if (success) {
|
|
469
|
+
console.log('[DESKTOP_CONNECTOR] ✅ Code execution succeeded');
|
|
470
|
+
}
|
|
471
|
+
else {
|
|
472
|
+
console.log('[DESKTOP_CONNECTOR] ⚠️ Code execution returned error:', errorMsg);
|
|
473
|
+
}
|
|
474
|
+
}, result.success, result.error || '');
|
|
475
|
+
return result;
|
|
476
|
+
}
|
|
477
|
+
catch (error) {
|
|
478
|
+
const errorMsg = error instanceof Error ? error.message : String(error);
|
|
479
|
+
lastError = error instanceof Error ? error : new Error(errorMsg);
|
|
480
|
+
// Check if it's a detached frame error
|
|
481
|
+
if (errorMsg.includes('detached') && attempt < maxRetries) {
|
|
482
|
+
logger.warn({ attempt, maxRetries }, 'Frame detached, retrying with fresh frames');
|
|
483
|
+
// Small delay before retry
|
|
484
|
+
await new Promise(resolve => setTimeout(resolve, 100));
|
|
485
|
+
continue;
|
|
486
|
+
}
|
|
487
|
+
// Not a detached frame error or we've exhausted retries
|
|
488
|
+
logger.error({ error: errorMsg, attempt }, 'Code execution failed');
|
|
489
|
+
try {
|
|
490
|
+
await this.page.evaluate((err) => {
|
|
491
|
+
console.error('[DESKTOP_CONNECTOR] ❌ executeCodeViaUI failed:', err);
|
|
492
|
+
}, errorMsg);
|
|
493
|
+
}
|
|
494
|
+
catch {
|
|
495
|
+
// Ignore errors from logging
|
|
496
|
+
}
|
|
497
|
+
throw lastError;
|
|
498
|
+
}
|
|
499
|
+
}
|
|
500
|
+
throw lastError || new Error('Execution failed after retries');
|
|
501
|
+
}
|
|
502
|
+
/**
|
|
503
|
+
* Update a variable's value in a specific mode
|
|
504
|
+
*/
|
|
505
|
+
async updateVariable(variableId, modeId, value) {
|
|
506
|
+
await this.page.evaluate((vId, mId) => {
|
|
507
|
+
console.log(`[DESKTOP_CONNECTOR] updateVariable() called: ${vId} mode ${mId}`);
|
|
508
|
+
}, variableId, modeId);
|
|
509
|
+
logger.info({ variableId, modeId }, 'Updating variable via plugin UI');
|
|
510
|
+
const frame = await this.findPluginUIFrame();
|
|
511
|
+
try {
|
|
512
|
+
const result = await frame.evaluate(`window.updateVariable(${JSON.stringify(variableId)}, ${JSON.stringify(modeId)}, ${JSON.stringify(value)})`);
|
|
513
|
+
logger.info({ success: result.success, variableName: result.variable?.name }, 'Variable updated');
|
|
514
|
+
await this.page.evaluate((name) => {
|
|
515
|
+
console.log(`[DESKTOP_CONNECTOR] ✅ Variable "${name}" updated successfully`);
|
|
516
|
+
}, result.variable?.name || variableId);
|
|
517
|
+
return result;
|
|
518
|
+
}
|
|
519
|
+
catch (error) {
|
|
520
|
+
logger.error({ error, variableId }, 'Update variable failed');
|
|
521
|
+
throw error;
|
|
522
|
+
}
|
|
523
|
+
}
|
|
524
|
+
/**
|
|
525
|
+
* Create a new variable in a collection
|
|
526
|
+
*/
|
|
527
|
+
async createVariable(name, collectionId, resolvedType, options) {
|
|
528
|
+
await this.page.evaluate((n, cId, type) => {
|
|
529
|
+
console.log(`[DESKTOP_CONNECTOR] createVariable() called: "${n}" in collection ${cId}, type: ${type}`);
|
|
530
|
+
}, name, collectionId, resolvedType);
|
|
531
|
+
logger.info({ name, collectionId, resolvedType }, 'Creating variable via plugin UI');
|
|
532
|
+
const frame = await this.findPluginUIFrame();
|
|
533
|
+
try {
|
|
534
|
+
const result = await frame.evaluate(`window.createVariable(${JSON.stringify(name)}, ${JSON.stringify(collectionId)}, ${JSON.stringify(resolvedType)}, ${JSON.stringify(options || {})})`);
|
|
535
|
+
logger.info({ success: result.success, variableId: result.variable?.id }, 'Variable created');
|
|
536
|
+
await this.page.evaluate((id, n) => {
|
|
537
|
+
console.log(`[DESKTOP_CONNECTOR] ✅ Variable "${n}" created with ID: ${id}`);
|
|
538
|
+
}, result.variable?.id || 'unknown', name);
|
|
539
|
+
return result;
|
|
540
|
+
}
|
|
541
|
+
catch (error) {
|
|
542
|
+
logger.error({ error, name }, 'Create variable failed');
|
|
543
|
+
throw error;
|
|
544
|
+
}
|
|
545
|
+
}
|
|
546
|
+
/**
|
|
547
|
+
* Create a new variable collection
|
|
548
|
+
*/
|
|
549
|
+
async createVariableCollection(name, options) {
|
|
550
|
+
await this.page.evaluate((n) => {
|
|
551
|
+
console.log(`[DESKTOP_CONNECTOR] createVariableCollection() called: "${n}"`);
|
|
552
|
+
}, name);
|
|
553
|
+
logger.info({ name, options }, 'Creating variable collection via plugin UI');
|
|
554
|
+
const frame = await this.findPluginUIFrame();
|
|
555
|
+
try {
|
|
556
|
+
const result = await frame.evaluate(`window.createVariableCollection(${JSON.stringify(name)}, ${JSON.stringify(options || {})})`);
|
|
557
|
+
logger.info({ success: result.success, collectionId: result.collection?.id }, 'Collection created');
|
|
558
|
+
await this.page.evaluate((id, n) => {
|
|
559
|
+
console.log(`[DESKTOP_CONNECTOR] ✅ Collection "${n}" created with ID: ${id}`);
|
|
560
|
+
}, result.collection?.id || 'unknown', name);
|
|
561
|
+
return result;
|
|
562
|
+
}
|
|
563
|
+
catch (error) {
|
|
564
|
+
logger.error({ error, name }, 'Create collection failed');
|
|
565
|
+
throw error;
|
|
566
|
+
}
|
|
567
|
+
}
|
|
568
|
+
/**
|
|
569
|
+
* Delete a variable
|
|
570
|
+
*/
|
|
571
|
+
async deleteVariable(variableId) {
|
|
572
|
+
await this.page.evaluate((vId) => {
|
|
573
|
+
console.log(`[DESKTOP_CONNECTOR] deleteVariable() called: ${vId}`);
|
|
574
|
+
}, variableId);
|
|
575
|
+
logger.info({ variableId }, 'Deleting variable via plugin UI');
|
|
576
|
+
const frame = await this.findPluginUIFrame();
|
|
577
|
+
try {
|
|
578
|
+
const result = await frame.evaluate(`window.deleteVariable(${JSON.stringify(variableId)})`);
|
|
579
|
+
logger.info({ success: result.success, deletedName: result.deleted?.name }, 'Variable deleted');
|
|
580
|
+
await this.page.evaluate((name) => {
|
|
581
|
+
console.log(`[DESKTOP_CONNECTOR] ✅ Variable "${name}" deleted`);
|
|
582
|
+
}, result.deleted?.name || variableId);
|
|
583
|
+
return result;
|
|
584
|
+
}
|
|
585
|
+
catch (error) {
|
|
586
|
+
logger.error({ error, variableId }, 'Delete variable failed');
|
|
587
|
+
throw error;
|
|
588
|
+
}
|
|
589
|
+
}
|
|
590
|
+
/**
|
|
591
|
+
* Delete a variable collection
|
|
592
|
+
*/
|
|
593
|
+
async deleteVariableCollection(collectionId) {
|
|
594
|
+
await this.page.evaluate((cId) => {
|
|
595
|
+
console.log(`[DESKTOP_CONNECTOR] deleteVariableCollection() called: ${cId}`);
|
|
596
|
+
}, collectionId);
|
|
597
|
+
logger.info({ collectionId }, 'Deleting collection via plugin UI');
|
|
598
|
+
const frame = await this.findPluginUIFrame();
|
|
599
|
+
try {
|
|
600
|
+
const result = await frame.evaluate(`window.deleteVariableCollection(${JSON.stringify(collectionId)})`);
|
|
601
|
+
logger.info({ success: result.success, deletedName: result.deleted?.name }, 'Collection deleted');
|
|
602
|
+
await this.page.evaluate((name, count) => {
|
|
603
|
+
console.log(`[DESKTOP_CONNECTOR] ✅ Collection "${name}" deleted (had ${count} variables)`);
|
|
604
|
+
}, result.deleted?.name || collectionId, result.deleted?.variableCount || 0);
|
|
605
|
+
return result;
|
|
606
|
+
}
|
|
607
|
+
catch (error) {
|
|
608
|
+
logger.error({ error, collectionId }, 'Delete collection failed');
|
|
609
|
+
throw error;
|
|
610
|
+
}
|
|
611
|
+
}
|
|
612
|
+
/**
|
|
613
|
+
* Refresh variables data from Figma
|
|
614
|
+
*/
|
|
615
|
+
async refreshVariables() {
|
|
616
|
+
await this.page.evaluate(() => {
|
|
617
|
+
console.log('[DESKTOP_CONNECTOR] refreshVariables() called');
|
|
618
|
+
});
|
|
619
|
+
logger.info('Refreshing variables via plugin UI');
|
|
620
|
+
const frame = await this.findPluginUIFrame();
|
|
621
|
+
try {
|
|
622
|
+
const result = await frame.evaluate('window.refreshVariables()');
|
|
623
|
+
logger.info({
|
|
624
|
+
success: result.success,
|
|
625
|
+
variableCount: result.data?.variables?.length,
|
|
626
|
+
collectionCount: result.data?.variableCollections?.length
|
|
627
|
+
}, 'Variables refreshed');
|
|
628
|
+
await this.page.evaluate((vCount, cCount) => {
|
|
629
|
+
console.log(`[DESKTOP_CONNECTOR] ✅ Variables refreshed: ${vCount} variables in ${cCount} collections`);
|
|
630
|
+
}, result.data?.variables?.length || 0, result.data?.variableCollections?.length || 0);
|
|
631
|
+
return result;
|
|
632
|
+
}
|
|
633
|
+
catch (error) {
|
|
634
|
+
logger.error({ error }, 'Refresh variables failed');
|
|
635
|
+
throw error;
|
|
636
|
+
}
|
|
637
|
+
}
|
|
638
|
+
/**
|
|
639
|
+
* Rename a variable
|
|
640
|
+
*/
|
|
641
|
+
async renameVariable(variableId, newName) {
|
|
642
|
+
await this.page.evaluate((vId, name) => {
|
|
643
|
+
console.log(`[DESKTOP_CONNECTOR] renameVariable() called: ${vId} -> "${name}"`);
|
|
644
|
+
}, variableId, newName);
|
|
645
|
+
logger.info({ variableId, newName }, 'Renaming variable via plugin UI');
|
|
646
|
+
const frame = await this.findPluginUIFrame();
|
|
647
|
+
try {
|
|
648
|
+
const result = await frame.evaluate(`window.renameVariable(${JSON.stringify(variableId)}, ${JSON.stringify(newName)})`);
|
|
649
|
+
logger.info({ success: result.success, oldName: result.oldName, newName: result.variable?.name }, 'Variable renamed');
|
|
650
|
+
await this.page.evaluate((oldN, newN) => {
|
|
651
|
+
console.log(`[DESKTOP_CONNECTOR] ✅ Variable renamed from "${oldN}" to "${newN}"`);
|
|
652
|
+
}, result.oldName || 'unknown', result.variable?.name || newName);
|
|
653
|
+
return result;
|
|
654
|
+
}
|
|
655
|
+
catch (error) {
|
|
656
|
+
logger.error({ error, variableId }, 'Rename variable failed');
|
|
657
|
+
throw error;
|
|
658
|
+
}
|
|
659
|
+
}
|
|
660
|
+
/**
|
|
661
|
+
* Add a mode to a variable collection
|
|
662
|
+
*/
|
|
663
|
+
async addMode(collectionId, modeName) {
|
|
664
|
+
await this.page.evaluate((cId, name) => {
|
|
665
|
+
console.log(`[DESKTOP_CONNECTOR] addMode() called: "${name}" to collection ${cId}`);
|
|
666
|
+
}, collectionId, modeName);
|
|
667
|
+
logger.info({ collectionId, modeName }, 'Adding mode via plugin UI');
|
|
668
|
+
const frame = await this.findPluginUIFrame();
|
|
669
|
+
try {
|
|
670
|
+
const result = await frame.evaluate(`window.addMode(${JSON.stringify(collectionId)}, ${JSON.stringify(modeName)})`);
|
|
671
|
+
logger.info({ success: result.success, newModeId: result.newMode?.modeId }, 'Mode added');
|
|
672
|
+
await this.page.evaluate((name, modeId) => {
|
|
673
|
+
console.log(`[DESKTOP_CONNECTOR] ✅ Mode "${name}" added with ID: ${modeId}`);
|
|
674
|
+
}, modeName, result.newMode?.modeId || 'unknown');
|
|
675
|
+
return result;
|
|
676
|
+
}
|
|
677
|
+
catch (error) {
|
|
678
|
+
logger.error({ error, collectionId }, 'Add mode failed');
|
|
679
|
+
throw error;
|
|
680
|
+
}
|
|
681
|
+
}
|
|
682
|
+
/**
|
|
683
|
+
* Rename a mode in a variable collection
|
|
684
|
+
*/
|
|
685
|
+
async renameMode(collectionId, modeId, newName) {
|
|
686
|
+
await this.page.evaluate((cId, mId, name) => {
|
|
687
|
+
console.log(`[DESKTOP_CONNECTOR] renameMode() called: mode ${mId} in collection ${cId} -> "${name}"`);
|
|
688
|
+
}, collectionId, modeId, newName);
|
|
689
|
+
logger.info({ collectionId, modeId, newName }, 'Renaming mode via plugin UI');
|
|
690
|
+
const frame = await this.findPluginUIFrame();
|
|
691
|
+
try {
|
|
692
|
+
const result = await frame.evaluate(`window.renameMode(${JSON.stringify(collectionId)}, ${JSON.stringify(modeId)}, ${JSON.stringify(newName)})`);
|
|
693
|
+
logger.info({ success: result.success, oldName: result.oldName, newName }, 'Mode renamed');
|
|
694
|
+
await this.page.evaluate((oldN, newN) => {
|
|
695
|
+
console.log(`[DESKTOP_CONNECTOR] ✅ Mode renamed from "${oldN}" to "${newN}"`);
|
|
696
|
+
}, result.oldName || 'unknown', newName);
|
|
697
|
+
return result;
|
|
698
|
+
}
|
|
699
|
+
catch (error) {
|
|
700
|
+
logger.error({ error, collectionId, modeId }, 'Rename mode failed');
|
|
701
|
+
throw error;
|
|
702
|
+
}
|
|
703
|
+
}
|
|
704
|
+
/**
|
|
705
|
+
* Get all local components for design system manifest generation
|
|
706
|
+
*/
|
|
707
|
+
async getLocalComponents() {
|
|
708
|
+
await this.page.evaluate(() => {
|
|
709
|
+
console.log('[DESKTOP_CONNECTOR] getLocalComponents() called');
|
|
710
|
+
});
|
|
711
|
+
logger.info('Getting local components via plugin UI');
|
|
712
|
+
const frame = await this.findPluginUIFrame();
|
|
713
|
+
try {
|
|
714
|
+
const result = await frame.evaluate('window.getLocalComponents()');
|
|
715
|
+
logger.info({
|
|
716
|
+
success: result.success,
|
|
717
|
+
componentCount: result.data?.totalComponents,
|
|
718
|
+
componentSetCount: result.data?.totalComponentSets
|
|
719
|
+
}, 'Local components retrieved');
|
|
720
|
+
await this.page.evaluate((cCount, csCount) => {
|
|
721
|
+
console.log(`[DESKTOP_CONNECTOR] ✅ Found ${cCount} components and ${csCount} component sets`);
|
|
722
|
+
}, result.data?.totalComponents || 0, result.data?.totalComponentSets || 0);
|
|
723
|
+
return result;
|
|
724
|
+
}
|
|
725
|
+
catch (error) {
|
|
726
|
+
logger.error({ error }, 'Get local components failed');
|
|
727
|
+
throw error;
|
|
728
|
+
}
|
|
729
|
+
}
|
|
730
|
+
/**
|
|
731
|
+
* Instantiate a component with overrides
|
|
732
|
+
* Supports both published library components (by key) and local components (by nodeId)
|
|
733
|
+
*/
|
|
734
|
+
async instantiateComponent(componentKey, options) {
|
|
735
|
+
await this.page.evaluate((key, nodeId) => {
|
|
736
|
+
console.log(`[DESKTOP_CONNECTOR] instantiateComponent() called: key=${key}, nodeId=${nodeId}`);
|
|
737
|
+
}, componentKey, options?.nodeId || null);
|
|
738
|
+
logger.info({ componentKey, nodeId: options?.nodeId, options }, 'Instantiating component via plugin UI');
|
|
739
|
+
const frame = await this.findPluginUIFrame();
|
|
740
|
+
try {
|
|
741
|
+
const result = await frame.evaluate(`window.instantiateComponent(${JSON.stringify(componentKey)}, ${JSON.stringify(options || {})})`);
|
|
742
|
+
logger.info({ success: result.success, instanceId: result.instance?.id }, 'Component instantiated');
|
|
743
|
+
await this.page.evaluate((instanceId, name) => {
|
|
744
|
+
console.log(`[DESKTOP_CONNECTOR] ✅ Component instantiated: ${name} (${instanceId})`);
|
|
745
|
+
}, result.instance?.id || 'unknown', result.instance?.name || 'unknown');
|
|
746
|
+
return result;
|
|
747
|
+
}
|
|
748
|
+
catch (error) {
|
|
749
|
+
logger.error({ error, componentKey }, 'Instantiate component failed');
|
|
750
|
+
throw error;
|
|
751
|
+
}
|
|
752
|
+
}
|
|
753
|
+
// ============================================================================
|
|
754
|
+
// NEW: COMPONENT PROPERTY MANAGEMENT
|
|
755
|
+
// ============================================================================
|
|
756
|
+
/**
|
|
757
|
+
* Set description on a component or style
|
|
758
|
+
*/
|
|
759
|
+
async setNodeDescription(nodeId, description, descriptionMarkdown) {
|
|
760
|
+
logger.info({ nodeId, descriptionLength: description.length }, 'Setting node description via plugin UI');
|
|
761
|
+
const frame = await this.findPluginUIFrame();
|
|
762
|
+
try {
|
|
763
|
+
const result = await frame.evaluate(`window.setNodeDescription(${JSON.stringify(nodeId)}, ${JSON.stringify(description)}, ${JSON.stringify(descriptionMarkdown)})`);
|
|
764
|
+
logger.info({ success: result.success, nodeName: result.node?.name }, 'Description set');
|
|
765
|
+
return result;
|
|
766
|
+
}
|
|
767
|
+
catch (error) {
|
|
768
|
+
logger.error({ error, nodeId }, 'Set description failed');
|
|
769
|
+
throw error;
|
|
770
|
+
}
|
|
771
|
+
}
|
|
772
|
+
/**
|
|
773
|
+
* Add a component property
|
|
774
|
+
*/
|
|
775
|
+
async addComponentProperty(nodeId, propertyName, type, defaultValue, options) {
|
|
776
|
+
logger.info({ nodeId, propertyName, type }, 'Adding component property via plugin UI');
|
|
777
|
+
const frame = await this.findPluginUIFrame();
|
|
778
|
+
try {
|
|
779
|
+
const result = await frame.evaluate(`window.addComponentProperty(${JSON.stringify(nodeId)}, ${JSON.stringify(propertyName)}, ${JSON.stringify(type)}, ${JSON.stringify(defaultValue)}, ${JSON.stringify(options || {})})`);
|
|
780
|
+
logger.info({ success: result.success, propertyName: result.propertyName }, 'Property added');
|
|
781
|
+
return result;
|
|
782
|
+
}
|
|
783
|
+
catch (error) {
|
|
784
|
+
logger.error({ error, nodeId, propertyName }, 'Add component property failed');
|
|
785
|
+
throw error;
|
|
786
|
+
}
|
|
787
|
+
}
|
|
788
|
+
/**
|
|
789
|
+
* Edit an existing component property
|
|
790
|
+
*/
|
|
791
|
+
async editComponentProperty(nodeId, propertyName, newValue) {
|
|
792
|
+
logger.info({ nodeId, propertyName }, 'Editing component property via plugin UI');
|
|
793
|
+
const frame = await this.findPluginUIFrame();
|
|
794
|
+
try {
|
|
795
|
+
const result = await frame.evaluate(`window.editComponentProperty(${JSON.stringify(nodeId)}, ${JSON.stringify(propertyName)}, ${JSON.stringify(newValue)})`);
|
|
796
|
+
logger.info({ success: result.success, propertyName: result.propertyName }, 'Property edited');
|
|
797
|
+
return result;
|
|
798
|
+
}
|
|
799
|
+
catch (error) {
|
|
800
|
+
logger.error({ error, nodeId, propertyName }, 'Edit component property failed');
|
|
801
|
+
throw error;
|
|
802
|
+
}
|
|
803
|
+
}
|
|
804
|
+
/**
|
|
805
|
+
* Delete a component property
|
|
806
|
+
*/
|
|
807
|
+
async deleteComponentProperty(nodeId, propertyName) {
|
|
808
|
+
logger.info({ nodeId, propertyName }, 'Deleting component property via plugin UI');
|
|
809
|
+
const frame = await this.findPluginUIFrame();
|
|
810
|
+
try {
|
|
811
|
+
const result = await frame.evaluate(`window.deleteComponentProperty(${JSON.stringify(nodeId)}, ${JSON.stringify(propertyName)})`);
|
|
812
|
+
logger.info({ success: result.success }, 'Property deleted');
|
|
813
|
+
return result;
|
|
814
|
+
}
|
|
815
|
+
catch (error) {
|
|
816
|
+
logger.error({ error, nodeId, propertyName }, 'Delete component property failed');
|
|
817
|
+
throw error;
|
|
818
|
+
}
|
|
819
|
+
}
|
|
820
|
+
// ============================================================================
|
|
821
|
+
// NEW: NODE MANIPULATION
|
|
822
|
+
// ============================================================================
|
|
823
|
+
/**
|
|
824
|
+
* Resize a node
|
|
825
|
+
*/
|
|
826
|
+
async resizeNode(nodeId, width, height, withConstraints = true) {
|
|
827
|
+
logger.info({ nodeId, width, height, withConstraints }, 'Resizing node via plugin UI');
|
|
828
|
+
const frame = await this.findPluginUIFrame();
|
|
829
|
+
try {
|
|
830
|
+
const result = await frame.evaluate(`window.resizeNode(${JSON.stringify(nodeId)}, ${width}, ${height}, ${withConstraints})`);
|
|
831
|
+
logger.info({ success: result.success, nodeId: result.node?.id }, 'Node resized');
|
|
832
|
+
return result;
|
|
833
|
+
}
|
|
834
|
+
catch (error) {
|
|
835
|
+
logger.error({ error, nodeId }, 'Resize node failed');
|
|
836
|
+
throw error;
|
|
837
|
+
}
|
|
838
|
+
}
|
|
839
|
+
/**
|
|
840
|
+
* Move/position a node
|
|
841
|
+
*/
|
|
842
|
+
async moveNode(nodeId, x, y) {
|
|
843
|
+
logger.info({ nodeId, x, y }, 'Moving node via plugin UI');
|
|
844
|
+
const frame = await this.findPluginUIFrame();
|
|
845
|
+
try {
|
|
846
|
+
const result = await frame.evaluate(`window.moveNode(${JSON.stringify(nodeId)}, ${x}, ${y})`);
|
|
847
|
+
logger.info({ success: result.success, nodeId: result.node?.id }, 'Node moved');
|
|
848
|
+
return result;
|
|
849
|
+
}
|
|
850
|
+
catch (error) {
|
|
851
|
+
logger.error({ error, nodeId }, 'Move node failed');
|
|
852
|
+
throw error;
|
|
853
|
+
}
|
|
854
|
+
}
|
|
855
|
+
/**
|
|
856
|
+
* Set fills (colors) on a node
|
|
857
|
+
*/
|
|
858
|
+
async setNodeFills(nodeId, fills) {
|
|
859
|
+
logger.info({ nodeId, fillCount: fills.length }, 'Setting node fills via plugin UI');
|
|
860
|
+
const frame = await this.findPluginUIFrame();
|
|
861
|
+
try {
|
|
862
|
+
const result = await frame.evaluate(`window.setNodeFills(${JSON.stringify(nodeId)}, ${JSON.stringify(fills)})`);
|
|
863
|
+
logger.info({ success: result.success, nodeId: result.node?.id }, 'Fills set');
|
|
864
|
+
return result;
|
|
865
|
+
}
|
|
866
|
+
catch (error) {
|
|
867
|
+
logger.error({ error, nodeId }, 'Set fills failed');
|
|
868
|
+
throw error;
|
|
869
|
+
}
|
|
870
|
+
}
|
|
871
|
+
/**
|
|
872
|
+
* Set strokes on a node
|
|
873
|
+
*/
|
|
874
|
+
async setNodeStrokes(nodeId, strokes, strokeWeight) {
|
|
875
|
+
logger.info({ nodeId, strokeCount: strokes.length, strokeWeight }, 'Setting node strokes via plugin UI');
|
|
876
|
+
const frame = await this.findPluginUIFrame();
|
|
877
|
+
try {
|
|
878
|
+
const result = await frame.evaluate(`window.setNodeStrokes(${JSON.stringify(nodeId)}, ${JSON.stringify(strokes)}, ${strokeWeight})`);
|
|
879
|
+
logger.info({ success: result.success, nodeId: result.node?.id }, 'Strokes set');
|
|
880
|
+
return result;
|
|
881
|
+
}
|
|
882
|
+
catch (error) {
|
|
883
|
+
logger.error({ error, nodeId }, 'Set strokes failed');
|
|
884
|
+
throw error;
|
|
885
|
+
}
|
|
886
|
+
}
|
|
887
|
+
/**
|
|
888
|
+
* Set opacity on a node
|
|
889
|
+
*/
|
|
890
|
+
async setNodeOpacity(nodeId, opacity) {
|
|
891
|
+
logger.info({ nodeId, opacity }, 'Setting node opacity via plugin UI');
|
|
892
|
+
const frame = await this.findPluginUIFrame();
|
|
893
|
+
try {
|
|
894
|
+
const result = await frame.evaluate(`window.setNodeOpacity(${JSON.stringify(nodeId)}, ${opacity})`);
|
|
895
|
+
logger.info({ success: result.success, opacity: result.node?.opacity }, 'Opacity set');
|
|
896
|
+
return result;
|
|
897
|
+
}
|
|
898
|
+
catch (error) {
|
|
899
|
+
logger.error({ error, nodeId }, 'Set opacity failed');
|
|
900
|
+
throw error;
|
|
901
|
+
}
|
|
902
|
+
}
|
|
903
|
+
/**
|
|
904
|
+
* Set corner radius on a node
|
|
905
|
+
*/
|
|
906
|
+
async setNodeCornerRadius(nodeId, radius) {
|
|
907
|
+
logger.info({ nodeId, radius }, 'Setting node corner radius via plugin UI');
|
|
908
|
+
const frame = await this.findPluginUIFrame();
|
|
909
|
+
try {
|
|
910
|
+
const result = await frame.evaluate(`window.setNodeCornerRadius(${JSON.stringify(nodeId)}, ${radius})`);
|
|
911
|
+
logger.info({ success: result.success, radius: result.node?.cornerRadius }, 'Corner radius set');
|
|
912
|
+
return result;
|
|
913
|
+
}
|
|
914
|
+
catch (error) {
|
|
915
|
+
logger.error({ error, nodeId }, 'Set corner radius failed');
|
|
916
|
+
throw error;
|
|
917
|
+
}
|
|
918
|
+
}
|
|
919
|
+
/**
|
|
920
|
+
* Clone/duplicate a node
|
|
921
|
+
*/
|
|
922
|
+
async cloneNode(nodeId) {
|
|
923
|
+
logger.info({ nodeId }, 'Cloning node via plugin UI');
|
|
924
|
+
const frame = await this.findPluginUIFrame();
|
|
925
|
+
try {
|
|
926
|
+
const result = await frame.evaluate(`window.cloneNode(${JSON.stringify(nodeId)})`);
|
|
927
|
+
logger.info({ success: result.success, clonedId: result.node?.id }, 'Node cloned');
|
|
928
|
+
return result;
|
|
929
|
+
}
|
|
930
|
+
catch (error) {
|
|
931
|
+
logger.error({ error, nodeId }, 'Clone node failed');
|
|
932
|
+
throw error;
|
|
933
|
+
}
|
|
934
|
+
}
|
|
935
|
+
/**
|
|
936
|
+
* Delete a node
|
|
937
|
+
*/
|
|
938
|
+
async deleteNode(nodeId) {
|
|
939
|
+
logger.info({ nodeId }, 'Deleting node via plugin UI');
|
|
940
|
+
const frame = await this.findPluginUIFrame();
|
|
941
|
+
try {
|
|
942
|
+
const result = await frame.evaluate(`window.deleteNode(${JSON.stringify(nodeId)})`);
|
|
943
|
+
logger.info({ success: result.success, deletedName: result.deleted?.name }, 'Node deleted');
|
|
944
|
+
return result;
|
|
945
|
+
}
|
|
946
|
+
catch (error) {
|
|
947
|
+
logger.error({ error, nodeId }, 'Delete node failed');
|
|
948
|
+
throw error;
|
|
949
|
+
}
|
|
950
|
+
}
|
|
951
|
+
/**
|
|
952
|
+
* Rename a node
|
|
953
|
+
*/
|
|
954
|
+
async renameNode(nodeId, newName) {
|
|
955
|
+
logger.info({ nodeId, newName }, 'Renaming node via plugin UI');
|
|
956
|
+
const frame = await this.findPluginUIFrame();
|
|
957
|
+
try {
|
|
958
|
+
const result = await frame.evaluate(`window.renameNode(${JSON.stringify(nodeId)}, ${JSON.stringify(newName)})`);
|
|
959
|
+
logger.info({ success: result.success, oldName: result.node?.oldName, newName: result.node?.name }, 'Node renamed');
|
|
960
|
+
return result;
|
|
961
|
+
}
|
|
962
|
+
catch (error) {
|
|
963
|
+
logger.error({ error, nodeId }, 'Rename node failed');
|
|
964
|
+
throw error;
|
|
965
|
+
}
|
|
966
|
+
}
|
|
967
|
+
/**
|
|
968
|
+
* Set text content on a text node
|
|
969
|
+
*/
|
|
970
|
+
async setTextContent(nodeId, text, options) {
|
|
971
|
+
logger.info({ nodeId, textLength: text.length }, 'Setting text content via plugin UI');
|
|
972
|
+
const frame = await this.findPluginUIFrame();
|
|
973
|
+
try {
|
|
974
|
+
const result = await frame.evaluate(`window.setTextContent(${JSON.stringify(nodeId)}, ${JSON.stringify(text)}, ${JSON.stringify(options || {})})`);
|
|
975
|
+
logger.info({ success: result.success, characters: result.node?.characters?.substring(0, 50) }, 'Text content set');
|
|
976
|
+
return result;
|
|
977
|
+
}
|
|
978
|
+
catch (error) {
|
|
979
|
+
logger.error({ error, nodeId }, 'Set text content failed');
|
|
980
|
+
throw error;
|
|
981
|
+
}
|
|
982
|
+
}
|
|
983
|
+
/**
|
|
984
|
+
* Create a child node
|
|
985
|
+
*/
|
|
986
|
+
async createChildNode(parentId, nodeType, properties) {
|
|
987
|
+
logger.info({ parentId, nodeType, properties }, 'Creating child node via plugin UI');
|
|
988
|
+
const frame = await this.findPluginUIFrame();
|
|
989
|
+
try {
|
|
990
|
+
const result = await frame.evaluate(`window.createChildNode(${JSON.stringify(parentId)}, ${JSON.stringify(nodeType)}, ${JSON.stringify(properties || {})})`);
|
|
991
|
+
logger.info({ success: result.success, nodeId: result.node?.id, nodeType: result.node?.type }, 'Child node created');
|
|
992
|
+
return result;
|
|
993
|
+
}
|
|
994
|
+
catch (error) {
|
|
995
|
+
logger.error({ error, parentId, nodeType }, 'Create child node failed');
|
|
996
|
+
throw error;
|
|
997
|
+
}
|
|
998
|
+
}
|
|
999
|
+
/**
|
|
1000
|
+
* Capture screenshot via plugin UI (for visual validation)
|
|
1001
|
+
*/
|
|
1002
|
+
async captureScreenshot(nodeId, options) {
|
|
1003
|
+
logger.info({ nodeId, options }, 'Capturing screenshot via plugin UI');
|
|
1004
|
+
const frame = await this.findPluginUIFrame();
|
|
1005
|
+
try {
|
|
1006
|
+
const result = await frame.evaluate(`window.captureScreenshot(${JSON.stringify(nodeId)}, ${JSON.stringify(options || {})})`);
|
|
1007
|
+
return result;
|
|
1008
|
+
}
|
|
1009
|
+
catch (error) {
|
|
1010
|
+
logger.error({ error, nodeId }, 'Capture screenshot failed');
|
|
1011
|
+
throw error;
|
|
1012
|
+
}
|
|
1013
|
+
}
|
|
1014
|
+
/**
|
|
1015
|
+
* Set instance properties via plugin UI
|
|
1016
|
+
*/
|
|
1017
|
+
async setInstanceProperties(nodeId, properties) {
|
|
1018
|
+
logger.info({ nodeId, properties }, 'Setting instance properties via plugin UI');
|
|
1019
|
+
const frame = await this.findPluginUIFrame();
|
|
1020
|
+
try {
|
|
1021
|
+
const result = await frame.evaluate(`window.setInstanceProperties(${JSON.stringify(nodeId)}, ${JSON.stringify(properties)})`);
|
|
1022
|
+
return result;
|
|
1023
|
+
}
|
|
1024
|
+
catch (error) {
|
|
1025
|
+
logger.error({ error, nodeId }, 'Set instance properties failed');
|
|
1026
|
+
throw error;
|
|
1027
|
+
}
|
|
1028
|
+
}
|
|
1029
|
+
}
|