@qmingjun/dsh-eda-mcp 0.0.0-stage → 1.1.12
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/AGENTS.md +63 -0
- package/CHANGELOG.md +792 -0
- package/CONTRIBUTING.md +154 -0
- package/LICENSE +55 -0
- package/README.md +290 -2
- package/README.zh.md +449 -0
- package/cordis.patch.yml +7 -0
- package/docs/design-rules.md +254 -0
- package/docs/dsh-version-sweep.md +73 -0
- package/docs/pitfalls.md +99 -0
- package/docs/project-status.md +100 -0
- package/docs/release.md +215 -0
- package/docs/schematic-workflow.md +297 -0
- package/docs/tools.md +184 -0
- package/jlc-extension/api-bridge.js +879 -0
- package/jlc-extension/extension.json +104 -0
- package/lib/bridge.mjs +4179 -0
- package/lib/client.js +1605 -0
- package/lib/extension-digest.json +6 -0
- package/lib/index.js +1261 -0
- package/package.json +144 -4
- package/src/bridge/design-intake.ts +133 -0
- package/src/bridge/design-rules.ts +228 -0
- package/src/bridge/eda-launch.ts +267 -0
- package/src/bridge/extension-package.ts +35 -0
- package/src/bridge/index.ts +3576 -0
- package/src/bridge/netlist-erc.ts +305 -0
- package/src/bridge/schematic-audit.ts +131 -0
- package/src/bridge/workflow-guide.ts +65 -0
- package/src/bridge-diagnostics.ts +185 -0
- package/src/client/EdaMcpSettingsCard.tsx +571 -0
- package/src/client/index.ts +40 -0
- package/src/client/locales.ts +83 -0
- package/src/client/settings-scope.ts +160 -0
- package/src/config-values.ts +60 -0
- package/src/guidance.ts +35 -0
- package/src/index.ts +365 -0
- package/src/loopback.ts +48 -0
- package/src/node-runtime.ts +120 -0
- package/src/routes.ts +377 -0
- package/src/skills-content.ts +309 -0
- package/src/skills.ts +91 -0
- package/test/config-values.test.ts +60 -0
- package/test/design-intake.test.ts +40 -0
- package/test/design-rules.test.ts +72 -0
- package/test/eda-launch.test.ts +114 -0
- package/test/extension-package.test.ts +27 -0
- package/test/extension-routes.test.ts +116 -0
- package/test/netlist-erc.test.ts +90 -0
- package/test/node-runtime.test.ts +65 -0
- package/test/schematic-audit.test.ts +71 -0
- package/test/seams.test.ts +164 -0
- package/test/skills.test.ts +68 -0
|
@@ -0,0 +1,879 @@
|
|
|
1
|
+
/* eslint-disable */
|
|
2
|
+
/**
|
|
3
|
+
* dsh-eda-mcp — JLCEDA Pro bridge extension (version injected from extension.json).
|
|
4
|
+
*
|
|
5
|
+
* Connects to the local dsh-eda-mcp bridge process over WebSocket and executes
|
|
6
|
+
* a strict allow-list of official EasyEDA Pro extension APIs. Import through:
|
|
7
|
+
* Extensions → Import → select dsh-eda-bridge-extension-<version>.zip
|
|
8
|
+
* then enable "Allow interactive with external" for this extension.
|
|
9
|
+
*/
|
|
10
|
+
var DSH_EDA_MCP = {
|
|
11
|
+
version: '__EXTENSION_VERSION__',
|
|
12
|
+
connect: null,
|
|
13
|
+
disconnect: null,
|
|
14
|
+
status: null,
|
|
15
|
+
isConnected: function () { return false; }
|
|
16
|
+
};
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* Header-menu callbacks. The JLCEDA extension runner resolves registerFn names
|
|
20
|
+
* in this script scope, so these declarations must stay at top level.
|
|
21
|
+
*/
|
|
22
|
+
function dshEdaMcpConnect() {
|
|
23
|
+
if (typeof DSH_EDA_MCP.connect === 'function') return DSH_EDA_MCP.connect();
|
|
24
|
+
return undefined;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
function dshEdaMcpDisconnect() {
|
|
28
|
+
if (typeof DSH_EDA_MCP.disconnect === 'function') return DSH_EDA_MCP.disconnect();
|
|
29
|
+
return undefined;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
function dshEdaMcpStatus() {
|
|
33
|
+
if (typeof DSH_EDA_MCP.status === 'function') return DSH_EDA_MCP.status();
|
|
34
|
+
return undefined;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
(function () {
|
|
38
|
+
'use strict';
|
|
39
|
+
|
|
40
|
+
// Injected from extension.json at build time: a hand-maintained constant here
|
|
41
|
+
// went stale once and made the bridge report a version mismatch forever.
|
|
42
|
+
var VERSION = '__EXTENSION_VERSION__';
|
|
43
|
+
var WS_ID = 'dsh-eda-mcp-bridge';
|
|
44
|
+
var WS_PORT = 39009;
|
|
45
|
+
var WS_URL = 'ws://127.0.0.1:' + WS_PORT + '/ws';
|
|
46
|
+
var HEARTBEAT_MS = 10000;
|
|
47
|
+
|
|
48
|
+
function earlyToast(message, type) {
|
|
49
|
+
try {
|
|
50
|
+
if (typeof eda !== 'undefined' && eda.sys_Message && typeof eda.sys_Message.showToastMessage === 'function') {
|
|
51
|
+
eda.sys_Message.showToastMessage(message, type || 'info', 5);
|
|
52
|
+
}
|
|
53
|
+
} catch (_) { /* ignore */ }
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
earlyToast('DSH EDA MCP 扩展已加载 v' + VERSION, 'info');
|
|
57
|
+
|
|
58
|
+
var globalScope = typeof globalThis !== 'undefined' ? globalThis
|
|
59
|
+
: (typeof window !== 'undefined' ? window : self);
|
|
60
|
+
var previous = globalScope.__DSH_EDA_MCP_BRIDGE_STATE__;
|
|
61
|
+
if (previous && previous.timer) { try { clearInterval(previous.timer); } catch (_) {} }
|
|
62
|
+
|
|
63
|
+
var ALLOWED_API = new Set([
|
|
64
|
+
'sch_Document.save',
|
|
65
|
+
'sch_Document.navigateToCoordinates',
|
|
66
|
+
'sch_Document.navigateToRegion',
|
|
67
|
+
'sch_Document.getPrimitiveAtPoint',
|
|
68
|
+
'sch_Document.getPrimitivesInRegion',
|
|
69
|
+
'sch_Document.autoLayout',
|
|
70
|
+
'sch_Document.autoRouting',
|
|
71
|
+
'sch_Drc.check',
|
|
72
|
+
'sch_Net.getAllNets',
|
|
73
|
+
'sch_Net.getCurrentProjectAllNets',
|
|
74
|
+
'sch_Net.getAllNetsName',
|
|
75
|
+
'sch_Net.getNet',
|
|
76
|
+
'sch_ManufactureData.getNetlistFile',
|
|
77
|
+
'sch_ManufactureData.getBomFile',
|
|
78
|
+
'sch_Primitive.getPrimitivesBBox',
|
|
79
|
+
'sch_PrimitiveComponent.create',
|
|
80
|
+
'sch_PrimitiveComponent.createNetFlag',
|
|
81
|
+
'sch_PrimitiveComponent.createNetPort',
|
|
82
|
+
'sch_PrimitiveComponent.createShortCircuitFlag',
|
|
83
|
+
'sch_PrimitiveComponent.delete',
|
|
84
|
+
'sch_PrimitiveComponent.modify',
|
|
85
|
+
'sch_PrimitiveComponent.get',
|
|
86
|
+
'sch_PrimitiveComponent.getAll',
|
|
87
|
+
'sch_PrimitiveComponent.getAllPrimitiveId',
|
|
88
|
+
'sch_PrimitiveComponent.getAllPinsByPrimitiveId',
|
|
89
|
+
'sch_PrimitiveComponent.getAllPropertyNames',
|
|
90
|
+
'sch_PrimitiveWire.create',
|
|
91
|
+
'sch_PrimitiveWire.delete',
|
|
92
|
+
'sch_PrimitiveWire.modify',
|
|
93
|
+
'sch_PrimitiveWire.get',
|
|
94
|
+
'sch_PrimitiveWire.getAll',
|
|
95
|
+
'sch_PrimitiveWire.getAllPrimitiveId',
|
|
96
|
+
'sch_PrimitiveRectangle.create',
|
|
97
|
+
'sch_PrimitiveRectangle.delete',
|
|
98
|
+
'sch_PrimitiveRectangle.modify',
|
|
99
|
+
'sch_PrimitiveRectangle.get',
|
|
100
|
+
'sch_PrimitiveRectangle.getAll',
|
|
101
|
+
'sch_PrimitiveRectangle.getAllPrimitiveId',
|
|
102
|
+
'sch_PrimitiveCircle.create',
|
|
103
|
+
'sch_PrimitiveCircle.delete',
|
|
104
|
+
'sch_PrimitiveCircle.modify',
|
|
105
|
+
'sch_PrimitiveCircle.get',
|
|
106
|
+
'sch_PrimitiveCircle.getAll',
|
|
107
|
+
'sch_PrimitiveCircle.getAllPrimitiveId',
|
|
108
|
+
'sch_PrimitiveArc.create',
|
|
109
|
+
'sch_PrimitiveArc.delete',
|
|
110
|
+
'sch_PrimitiveArc.modify',
|
|
111
|
+
'sch_PrimitiveArc.get',
|
|
112
|
+
'sch_PrimitiveArc.getAll',
|
|
113
|
+
'sch_PrimitiveArc.getAllPrimitiveId',
|
|
114
|
+
'sch_PrimitivePolygon.create',
|
|
115
|
+
'sch_PrimitivePolygon.delete',
|
|
116
|
+
'sch_PrimitivePolygon.modify',
|
|
117
|
+
'sch_PrimitivePolygon.get',
|
|
118
|
+
'sch_PrimitivePolygon.getAll',
|
|
119
|
+
'sch_PrimitivePolygon.getAllPrimitiveId',
|
|
120
|
+
'sch_PrimitiveText.create',
|
|
121
|
+
'sch_PrimitiveText.delete',
|
|
122
|
+
'sch_PrimitiveText.modify',
|
|
123
|
+
'sch_PrimitiveText.get',
|
|
124
|
+
'sch_PrimitiveText.getAll',
|
|
125
|
+
'sch_PrimitiveText.getAllPrimitiveId',
|
|
126
|
+
'sch_PrimitivePin.get',
|
|
127
|
+
'sch_PrimitivePin.modify',
|
|
128
|
+
'lib_Device.search',
|
|
129
|
+
'lib_Device.getByLcscIds',
|
|
130
|
+
'lib_Symbol.search',
|
|
131
|
+
'lib_Footprint.search',
|
|
132
|
+
'dmt_Schematic.getCurrentSchematicInfo',
|
|
133
|
+
'dmt_Schematic.getCurrentSchematicPageInfo',
|
|
134
|
+
'dmt_Schematic.getCurrentSchematicAllSchematicPagesInfo',
|
|
135
|
+
'dmt_Schematic.getSchematicInfo',
|
|
136
|
+
'dmt_Schematic.getSchematicPageInfo',
|
|
137
|
+
'dmt_Schematic.getAllSchematicPagesInfo',
|
|
138
|
+
'dmt_Team.getAllTeamsInfo',
|
|
139
|
+
'dmt_Team.getAllInvolvedTeamInfo',
|
|
140
|
+
'dmt_Team.getCurrentTeamInfo',
|
|
141
|
+
'dmt_Folder.getAllFoldersUuid',
|
|
142
|
+
'dmt_Folder.getFolderInfo',
|
|
143
|
+
'dmt_Workspace.getCurrentWorkspaceInfo',
|
|
144
|
+
'dmt_Project.getAllProjectsUuid',
|
|
145
|
+
'dmt_Project.getProjectInfo',
|
|
146
|
+
'dmt_Project.getCurrentProjectInfo',
|
|
147
|
+
'dmt_Project.openProject',
|
|
148
|
+
'dmt_Project.createProject',
|
|
149
|
+
'dmt_Project.deleteProject',
|
|
150
|
+
'dmt_Project.modifyProjectFriendlyName',
|
|
151
|
+
'dmt_Project.modifyProjectDescription',
|
|
152
|
+
'dmt_Project.copyProject',
|
|
153
|
+
'dmt_Project.moveProjectToFolder',
|
|
154
|
+
'dmt_Board.createBoard',
|
|
155
|
+
'dmt_Board.getAllBoardsInfo',
|
|
156
|
+
'dmt_Board.getCurrentBoardInfo',
|
|
157
|
+
'dmt_EditorControl.getSplitScreenTree',
|
|
158
|
+
'dmt_EditorControl.getTabsBySplitScreenId',
|
|
159
|
+
'dmt_EditorControl.openDocument',
|
|
160
|
+
'dmt_EditorControl.closeDocument',
|
|
161
|
+
'dmt_EditorControl.activateDocument',
|
|
162
|
+
'dmt_EditorControl.zoomToRegion',
|
|
163
|
+
'dmt_EditorControl.zoomToAllPrimitives',
|
|
164
|
+
'dmt_EditorControl.getCurrentRenderedAreaImage',
|
|
165
|
+
'dmt_EditorControl.generateIndicatorMarkers',
|
|
166
|
+
'dmt_EditorControl.removeIndicatorMarkers',
|
|
167
|
+
'pcb_Document.save',
|
|
168
|
+
'pcb_Document.importChanges',
|
|
169
|
+
'pcb_Document.getPrimitivesInRegion',
|
|
170
|
+
'pcb_Document.navigateToRegion',
|
|
171
|
+
'pcb_Document.navigateToCoordinates',
|
|
172
|
+
'pcb_Document.zoomToBoardOutline',
|
|
173
|
+
'pcb_Document.getCanvasOrigin',
|
|
174
|
+
'pcb_Document.setCanvasOrigin',
|
|
175
|
+
'pcb_Document.convertCanvasOriginToDataOrigin',
|
|
176
|
+
'pcb_Document.convertDataOriginToCanvasOrigin',
|
|
177
|
+
'pcb_Primitive.getPrimitivesBBox',
|
|
178
|
+
'pcb_PrimitiveComponent.create',
|
|
179
|
+
'pcb_PrimitiveComponent.getAll',
|
|
180
|
+
'pcb_PrimitiveComponent.getAllPrimitiveId',
|
|
181
|
+
'pcb_PrimitiveComponent.getAllPinsByPrimitiveId',
|
|
182
|
+
'pcb_PrimitiveComponent.modify',
|
|
183
|
+
'pcb_PrimitiveComponent.delete',
|
|
184
|
+
'pcb_PrimitiveLine.create',
|
|
185
|
+
'pcb_PrimitiveLine.getAll',
|
|
186
|
+
'pcb_PrimitiveLine.getAllPrimitiveId',
|
|
187
|
+
'pcb_PrimitiveLine.modify',
|
|
188
|
+
'pcb_PrimitiveLine.delete',
|
|
189
|
+
'pcb_PrimitiveVia.create',
|
|
190
|
+
'pcb_PrimitiveVia.getAll',
|
|
191
|
+
'pcb_PrimitiveVia.getAllPrimitiveId',
|
|
192
|
+
'pcb_PrimitiveVia.modify',
|
|
193
|
+
'pcb_PrimitiveVia.delete',
|
|
194
|
+
'pcb_PrimitivePolyline.create',
|
|
195
|
+
'pcb_PrimitivePolyline.modify',
|
|
196
|
+
'pcb_PrimitivePolyline.delete',
|
|
197
|
+
'pcb_PrimitivePolyline.getAll',
|
|
198
|
+
'pcb_PrimitivePolyline.getAllPrimitiveId',
|
|
199
|
+
'pcb_Document.clearRouting',
|
|
200
|
+
'pcb_PrimitiveRegion.create',
|
|
201
|
+
'pcb_PrimitiveRegion.getAll',
|
|
202
|
+
'pcb_PrimitiveRegion.getAllPrimitiveId',
|
|
203
|
+
'pcb_PrimitiveRegion.modify',
|
|
204
|
+
'pcb_PrimitiveRegion.delete',
|
|
205
|
+
'pcb_MathPolygon.createPolygon',
|
|
206
|
+
'pcb_MathPolygon.createComplexPolygon',
|
|
207
|
+
'pcb_PrimitiveString.create',
|
|
208
|
+
'pcb_PrimitiveString.get',
|
|
209
|
+
'pcb_PrimitiveString.getAll',
|
|
210
|
+
'pcb_PrimitiveString.modify',
|
|
211
|
+
'pcb_PrimitiveString.delete',
|
|
212
|
+
'pcb_PrimitivePad.create',
|
|
213
|
+
'pcb_PrimitivePad.getAll',
|
|
214
|
+
'pcb_PrimitivePad.delete',
|
|
215
|
+
'pcb_PrimitiveFill.create',
|
|
216
|
+
'pcb_PrimitiveFill.getAll',
|
|
217
|
+
'pcb_PrimitiveFill.delete',
|
|
218
|
+
'pcb_PrimitivePour.create',
|
|
219
|
+
'pcb_PrimitivePour.modify',
|
|
220
|
+
'pcb_PrimitivePour.delete',
|
|
221
|
+
'pcb_PrimitivePour.getAll',
|
|
222
|
+
'pcb_PrimitivePour.getAllPrimitiveId',
|
|
223
|
+
// PATCHED(2026-10-31): registered pour borders stay empty until the copper is
|
|
224
|
+
// recomputed; the app exposes this static rebuild entry point but it was not on
|
|
225
|
+
// the allowlist, so a pour created outside the API could never be filled.
|
|
226
|
+
'pcb_PrimitivePour.rebuildCopperRegions',
|
|
227
|
+
'pcb_PrimitivePour.rebuildCopperRegion',
|
|
228
|
+
'pcb_PrimitivePoured.getAll',
|
|
229
|
+
'pcb_Drc.check',
|
|
230
|
+
'pcb_Drc.getCurrentRuleConfiguration',
|
|
231
|
+
'pcb_Drc.getCurrentRuleConfigurationName',
|
|
232
|
+
'pcb_Drc.getRealTimeDrcStatus',
|
|
233
|
+
'pcb_Drc.startRealTimeDrc',
|
|
234
|
+
'pcb_Drc.stopRealTimeDrc',
|
|
235
|
+
'sch_PrimitiveAttribute.createNetLabel',
|
|
236
|
+
'sch_PrimitiveAttribute.modify',
|
|
237
|
+
'sch_PrimitiveAttribute.delete',
|
|
238
|
+
'sch_PrimitiveAttribute.getAll',
|
|
239
|
+
'sch_Netlist.getNetlist',
|
|
240
|
+
'dmt_SelectControl.getCurrentDocumentInfo',
|
|
241
|
+
'pcb_Document.autoLayout',
|
|
242
|
+
'pcb_Document.autoRouting',
|
|
243
|
+
'pcb_Net.getAllNets',
|
|
244
|
+
'pcb_Net.getAllNetsName',
|
|
245
|
+
'pcb_Layer.getAllLayers',
|
|
246
|
+
'pcb_Layer.getCurrentLayer',
|
|
247
|
+
'pcb_Layer.modifyLayer'
|
|
248
|
+
]);
|
|
249
|
+
|
|
250
|
+
var FILE_METHODS = new Set([
|
|
251
|
+
'sch_ManufactureData.getNetlistFile',
|
|
252
|
+
'sch_ManufactureData.getBomFile',
|
|
253
|
+
'sch_ManufactureData.getBomTemplateFile',
|
|
254
|
+
'pcb_ManufactureData.getNetlistFile',
|
|
255
|
+
'pcb_ManufactureData.getBomFile',
|
|
256
|
+
'pcb_ManufactureData.getGerberFile'
|
|
257
|
+
]);
|
|
258
|
+
|
|
259
|
+
var JOB = null;
|
|
260
|
+
var wsReady = false;
|
|
261
|
+
var lastPongAt = 0;
|
|
262
|
+
var lastRegisterAt = 0;
|
|
263
|
+
|
|
264
|
+
function resolveApi(path) {
|
|
265
|
+
var parts = path.split('.');
|
|
266
|
+
var cursor = typeof eda !== 'undefined' ? eda : undefined;
|
|
267
|
+
for (var i = 0; i < parts.length; i++) {
|
|
268
|
+
if (cursor == null) return undefined;
|
|
269
|
+
cursor = cursor[parts[i]];
|
|
270
|
+
}
|
|
271
|
+
return cursor;
|
|
272
|
+
}
|
|
273
|
+
|
|
274
|
+
function callApi(method, args) {
|
|
275
|
+
var dot = method.lastIndexOf('.');
|
|
276
|
+
if (dot <= 0 || dot >= method.length - 1) throw new Error('bad API method: ' + method);
|
|
277
|
+
var ownerPath = method.slice(0, dot);
|
|
278
|
+
var fnName = method.slice(dot + 1);
|
|
279
|
+
var owner = resolveApi(ownerPath);
|
|
280
|
+
if (owner == null || typeof owner[fnName] !== 'function') {
|
|
281
|
+
throw new Error('API method not found: ' + method);
|
|
282
|
+
}
|
|
283
|
+
var callArgs = (args || []).map(function (value) { return value === null ? undefined : value; });
|
|
284
|
+
return owner[fnName].apply(owner, callArgs);
|
|
285
|
+
}
|
|
286
|
+
|
|
287
|
+
function toJsonable(value) {
|
|
288
|
+
if (value === undefined || value === null || typeof value !== 'object') {
|
|
289
|
+
if (typeof value === 'number' && !Number.isFinite(value)) return String(value);
|
|
290
|
+
return value;
|
|
291
|
+
}
|
|
292
|
+
if (typeof value.toJSON === 'function') {
|
|
293
|
+
try { return toJsonable(value.toJSON()); } catch (_) { /* fall through */ }
|
|
294
|
+
}
|
|
295
|
+
if (Array.isArray(value)) return value.map(toJsonable);
|
|
296
|
+
|
|
297
|
+
var out = {};
|
|
298
|
+
var proto = Object.getPrototypeOf(value);
|
|
299
|
+
if (proto != null) {
|
|
300
|
+
var names = Object.getOwnPropertyNames(proto);
|
|
301
|
+
for (var i = 0; i < names.length; i++) {
|
|
302
|
+
var key = names[i];
|
|
303
|
+
if (key === 'constructor') continue;
|
|
304
|
+
var descriptor = Object.getOwnPropertyDescriptor(proto, key);
|
|
305
|
+
var fn = descriptor && typeof descriptor.value === 'function' ? descriptor.value : undefined;
|
|
306
|
+
if (fn && key.indexOf('getState_') === 0) {
|
|
307
|
+
try { out[key.slice('getState_'.length)] = toJsonable(fn.call(value)); } catch (_) { /* skip */ }
|
|
308
|
+
}
|
|
309
|
+
}
|
|
310
|
+
}
|
|
311
|
+
var ownKeys = Object.keys(value);
|
|
312
|
+
for (var j = 0; j < ownKeys.length; j++) {
|
|
313
|
+
var ownKey = ownKeys[j];
|
|
314
|
+
var ownValue = value[ownKey];
|
|
315
|
+
if (typeof ownValue === 'function') continue;
|
|
316
|
+
out[ownKey] = toJsonable(ownValue);
|
|
317
|
+
}
|
|
318
|
+
return out;
|
|
319
|
+
}
|
|
320
|
+
|
|
321
|
+
function bytesToBase64(bytes) {
|
|
322
|
+
var chunk = 0x8000;
|
|
323
|
+
var parts = [];
|
|
324
|
+
for (var i = 0; i < bytes.length; i += chunk) {
|
|
325
|
+
parts.push(String.fromCharCode.apply(null, bytes.subarray(i, i + chunk)));
|
|
326
|
+
}
|
|
327
|
+
return btoa(parts.join(''));
|
|
328
|
+
}
|
|
329
|
+
|
|
330
|
+
// JSON turns undefined array slots into null; the official API expects
|
|
331
|
+
// undefined (omitted) for optional parameters. One shared helper: the earlier
|
|
332
|
+
// per-helper copies were invisible to the project file helpers.
|
|
333
|
+
function optional(value) { return value === null ? undefined : value; }
|
|
334
|
+
|
|
335
|
+
async function blobToDataUrl(blob) {
|
|
336
|
+
var buffer = await blob.arrayBuffer();
|
|
337
|
+
var bytes = new Uint8Array(buffer);
|
|
338
|
+
var mimeType = blob.type || 'image/png';
|
|
339
|
+
return { mimeType: mimeType, dataUrl: 'data:' + mimeType + ';base64,' + bytesToBase64(bytes) };
|
|
340
|
+
}
|
|
341
|
+
|
|
342
|
+
async function blobToFilePayload(blob) {
|
|
343
|
+
var mimeType = blob.type || 'application/octet-stream';
|
|
344
|
+
var name = blob.name || '';
|
|
345
|
+
var buffer = await blob.arrayBuffer();
|
|
346
|
+
var bytes = new Uint8Array(buffer);
|
|
347
|
+
var isText = /^text\//i.test(mimeType) ||
|
|
348
|
+
/(csv|json|net|plain|xml)/i.test(mimeType) ||
|
|
349
|
+
/\.(csv|json|txt|net|xml)$/i.test(name);
|
|
350
|
+
if (isText && typeof TextDecoder !== 'undefined') {
|
|
351
|
+
// A blanket UTF-8 decode destroyed text payloads that carry another encoding: the
|
|
352
|
+
// BOM CSV is UTF-16LE, so the bytes became replacement characters before the bridge
|
|
353
|
+
// ever saw them. Detect the BOM and report which decoder ran.
|
|
354
|
+
var encoding = 'utf-8';
|
|
355
|
+
var offset = 0;
|
|
356
|
+
if (bytes.length >= 2 && bytes[0] === 0xFF && bytes[1] === 0xFE) { encoding = 'utf-16le'; offset = 2; }
|
|
357
|
+
else if (bytes.length >= 2 && bytes[0] === 0xFE && bytes[1] === 0xFF) { encoding = 'utf-16be'; offset = 2; }
|
|
358
|
+
else if (bytes.length >= 3 && bytes[0] === 0xEF && bytes[1] === 0xBB && bytes[2] === 0xBF) { offset = 3; }
|
|
359
|
+
var body = bytes.subarray(offset);
|
|
360
|
+
var text;
|
|
361
|
+
try { text = new TextDecoder(encoding).decode(body); }
|
|
362
|
+
catch (_) { text = new TextDecoder('utf-8').decode(body); encoding = 'utf-8'; }
|
|
363
|
+
return { mimeType: mimeType, name: name, size: bytes.length, encoding: encoding, text: text };
|
|
364
|
+
}
|
|
365
|
+
return { mimeType: mimeType, name: name, size: bytes.length, dataUrl: 'data:' + mimeType + ';base64,' + bytesToBase64(bytes) };
|
|
366
|
+
}
|
|
367
|
+
|
|
368
|
+
async function handleMessage(message) {
|
|
369
|
+
if (typeof message !== 'object' || message === null) return;
|
|
370
|
+
if (typeof message.id === 'undefined') return;
|
|
371
|
+
if (typeof message.method !== 'string') {
|
|
372
|
+
throw new Error('API method not allowed: ' + String(message.method));
|
|
373
|
+
}
|
|
374
|
+
// Internal helper: export a project as a File and hand it back as base64
|
|
375
|
+
// (the File object cannot cross the WebSocket boundary).
|
|
376
|
+
if (message.method === 'dsh.internal.exportProjectFile') {
|
|
377
|
+
var expArgs = message.args || [];
|
|
378
|
+
if (typeof eda === 'undefined' || !eda.sys_FileManager) {
|
|
379
|
+
throw new Error('dsh.internal.exportProjectFile: sys_FileManager is not available');
|
|
380
|
+
}
|
|
381
|
+
var expUuid = expArgs[0] == null ? undefined : String(expArgs[0]);
|
|
382
|
+
var expFile = expUuid === undefined
|
|
383
|
+
? await eda.sys_FileManager.getProjectFile(optional(expArgs[1]), optional(expArgs[2]), optional(expArgs[3]))
|
|
384
|
+
: await eda.sys_FileManager.getProjectFileByProjectUuid(expUuid, optional(expArgs[1]), optional(expArgs[2]), optional(expArgs[3]));
|
|
385
|
+
if (!expFile) throw new Error('dsh.internal.exportProjectFile: the app returned no file (project not open, or the export was cancelled)');
|
|
386
|
+
var expBuffer = new Uint8Array(await expFile.arrayBuffer());
|
|
387
|
+
var expBinary = '';
|
|
388
|
+
for (var expIndex = 0; expIndex < expBuffer.length; expIndex += 1) expBinary += String.fromCharCode(expBuffer[expIndex]);
|
|
389
|
+
return { name: expFile.name || String(expArgs[1] || 'project.epro'), size: expBuffer.length, base64: btoa(expBinary) };
|
|
390
|
+
}
|
|
391
|
+
// Internal helper: import a project from base64 (local .eprj2/.epro handed over
|
|
392
|
+
// by the bridge) by rebuilding the File the official API expects.
|
|
393
|
+
if (message.method === 'dsh.internal.importProjectFile') {
|
|
394
|
+
var impArgs = message.args || [];
|
|
395
|
+
if (typeof eda === 'undefined' || !eda.sys_FileManager) {
|
|
396
|
+
throw new Error('dsh.internal.importProjectFile: sys_FileManager is not available');
|
|
397
|
+
}
|
|
398
|
+
var impBinary = atob(String(impArgs[0] || ''));
|
|
399
|
+
var impBytes = new Uint8Array(impBinary.length);
|
|
400
|
+
for (var impIndex = 0; impIndex < impBinary.length; impIndex += 1) impBytes[impIndex] = impBinary.charCodeAt(impIndex);
|
|
401
|
+
var impFile = new File([impBytes], String(impArgs[1] || 'import.eprj2'), { type: 'application/octet-stream' });
|
|
402
|
+
// saveTo (4th argument) is what gives the import a target team; without it the
|
|
403
|
+
// app answers undefined and imports nothing.
|
|
404
|
+
var impResult = await eda.sys_FileManager.importProjectByProjectFile(
|
|
405
|
+
impFile, optional(impArgs[2]), undefined, optional(impArgs[3])
|
|
406
|
+
);
|
|
407
|
+
return { result: impResult === undefined ? null : toJsonable(impResult) };
|
|
408
|
+
}
|
|
409
|
+
// Internal helper: build an IPCB_Polygon from a raw source array and create
|
|
410
|
+
// Internal helper: report THIS extension's own allow-list. The bridge keeps a
|
|
411
|
+
// copy for docs/offline hints, but it drifts — and a bridge that is stricter
|
|
412
|
+
// than the extension silently rejects methods that actually work. This is the
|
|
413
|
+
// authority: what the running extension will accept right now.
|
|
414
|
+
if (message.method === 'dsh.internal.listApiMethods') {
|
|
415
|
+
return {
|
|
416
|
+
version: VERSION,
|
|
417
|
+
allowed: Array.from(ALLOWED_API).sort(),
|
|
418
|
+
files: Array.from(FILE_METHODS).sort(),
|
|
419
|
+
};
|
|
420
|
+
}
|
|
421
|
+
// a PCB region (used for the board outline).
|
|
422
|
+
if (message.method === 'dsh.internal.pcbCreateRegion') {
|
|
423
|
+
var regionArgs = message.args || [];
|
|
424
|
+
if (typeof eda === 'undefined' || !eda.pcb_MathPolygon || !eda.pcb_PrimitiveRegion) {
|
|
425
|
+
throw new Error('dsh.internal.pcbCreateRegion: PCB region APIs are not available');
|
|
426
|
+
}
|
|
427
|
+
var regionPolygon = eda.pcb_MathPolygon.createPolygon(regionArgs[1]);
|
|
428
|
+
if (!regionPolygon) throw new Error('dsh.internal.pcbCreateRegion: invalid polygon source');
|
|
429
|
+
var region = await eda.pcb_PrimitiveRegion.create(
|
|
430
|
+
optional(regionArgs[0]), regionPolygon, optional(regionArgs[2]), optional(regionArgs[3]),
|
|
431
|
+
optional(regionArgs[4]), optional(regionArgs[5])
|
|
432
|
+
);
|
|
433
|
+
if (!region) throw new Error('dsh.internal.pcbCreateRegion: pcb_PrimitiveRegion.create returned undefined (check the layer - regions only support copper/MULTI layers)');
|
|
434
|
+
return toJsonable(region);
|
|
435
|
+
}
|
|
436
|
+
// Internal helper: compose pcb_MathPolygon.createPolygon + pcb_PrimitivePolyline.create.
|
|
437
|
+
// A polyline is the primitive JLCEDA stores for a board outline, silkscreen
|
|
438
|
+
// shape or mechanical drawing; the polygon object cannot be serialized over
|
|
439
|
+
// WebSocket, so both official calls have to happen in one extension round trip.
|
|
440
|
+
if (message.method === 'dsh.internal.polylineCreate') {
|
|
441
|
+
var polyArgs = message.args || [];
|
|
442
|
+
if (typeof eda === 'undefined' || !eda.pcb_MathPolygon || !eda.pcb_PrimitivePolyline) {
|
|
443
|
+
throw new Error('dsh.internal.polylineCreate: PCB polyline APIs are not available');
|
|
444
|
+
}
|
|
445
|
+
var source = polyArgs[2];
|
|
446
|
+
var polygon = eda.pcb_MathPolygon.createPolygon(source);
|
|
447
|
+
if (!polygon) throw new Error('dsh.internal.polylineCreate: invalid polygon source ' + JSON.stringify(source));
|
|
448
|
+
var polyline = await eda.pcb_PrimitivePolyline.create(
|
|
449
|
+
optional(polyArgs[0]), optional(polyArgs[1]), polygon, optional(polyArgs[3]), optional(polyArgs[4])
|
|
450
|
+
);
|
|
451
|
+
if (!polyline) {
|
|
452
|
+
throw new Error('dsh.internal.polylineCreate: pcb_PrimitivePolyline.create returned undefined (net/layer/polygon rejected)');
|
|
453
|
+
}
|
|
454
|
+
return toJsonable(polyline);
|
|
455
|
+
}
|
|
456
|
+
// Internal helper: compose pcb_MathPolygon.createPolygon + pcb_PrimitivePour.create
|
|
457
|
+
// (copper pour / 覆铜). Same reason as dsh.internal.polylineCreate: the polygon
|
|
458
|
+
// object cannot be serialized over WebSocket.
|
|
459
|
+
if (message.method === 'dsh.internal.pourCreate') {
|
|
460
|
+
var pourArgs = message.args || [];
|
|
461
|
+
if (typeof eda === 'undefined' || !eda.pcb_MathPolygon || !eda.pcb_PrimitivePour) {
|
|
462
|
+
throw new Error('dsh.internal.pourCreate: PCB pour APIs are not available');
|
|
463
|
+
}
|
|
464
|
+
// PATCHED(2026-10-31): the app's create() wants a SIMPLE IPCB_Polygon; the typings
|
|
465
|
+
// name the parameter "complexPolygon" but type it IPCB_Polygon, which misled the
|
|
466
|
+
// original code into always passing createComplexPolygon() -> 无法创建覆铜边框图元.
|
|
467
|
+
// A known-working implementation (zhoushoujianwork/easyeda-agent) uses createPolygon.
|
|
468
|
+
// Try several argument shapes so one of them can match this build.
|
|
469
|
+
var pourSource = pourArgs[2];
|
|
470
|
+
var _mkPoly = function (simple) {
|
|
471
|
+
try {
|
|
472
|
+
if (simple) {
|
|
473
|
+
return eda.pcb_MathPolygon.createPolygon ? eda.pcb_MathPolygon.createPolygon(pourSource) : undefined;
|
|
474
|
+
}
|
|
475
|
+
return eda.pcb_MathPolygon.createComplexPolygon
|
|
476
|
+
? eda.pcb_MathPolygon.createComplexPolygon(Array.isArray(pourSource) && Array.isArray(pourSource[0]) ? pourSource : [pourSource])
|
|
477
|
+
: undefined;
|
|
478
|
+
} catch (_mkErr) { return undefined; }
|
|
479
|
+
};
|
|
480
|
+
var _pSimple = _mkPoly(true);
|
|
481
|
+
var _pComplex = _mkPoly(false);
|
|
482
|
+
var _net = optional(pourArgs[0]), _layer = optional(pourArgs[1]);
|
|
483
|
+
var _fill = optional(pourArgs[3]), _sil = optional(pourArgs[4]), _name = optional(pourArgs[5]);
|
|
484
|
+
var _prio = optional(pourArgs[6]), _wid = optional(pourArgs[7]), _lock = optional(pourArgs[8]);
|
|
485
|
+
var _variants = [];
|
|
486
|
+
if (_pSimple) {
|
|
487
|
+
_variants.push({ tag: 'simple-full', args: [_net, _layer, _pSimple, _fill, _sil, _name, _prio, _wid, _lock] });
|
|
488
|
+
_variants.push({ tag: 'simple-w0.2', args: [_net, _layer, _pSimple, _fill, _sil, _name, _prio, 0.2, _lock] });
|
|
489
|
+
_variants.push({ tag: 'simple-min', args: [_net, _layer, _pSimple] });
|
|
490
|
+
}
|
|
491
|
+
if (_pComplex) {
|
|
492
|
+
_variants.push({ tag: 'complex-full', args: [_net, _layer, _pComplex, _fill, _sil, _name, _prio, _wid, _lock] });
|
|
493
|
+
}
|
|
494
|
+
if (!_variants.length) throw new Error('dsh.internal.pourCreate: no usable polygon from ' + JSON.stringify(pourSource));
|
|
495
|
+
var pourRoutes = [];
|
|
496
|
+
var pour = null;
|
|
497
|
+
var usedRoute = 'none';
|
|
498
|
+
for (var _vi = 0; _vi < _variants.length && !pour; _vi++) {
|
|
499
|
+
try {
|
|
500
|
+
pour = await eda.pcb_PrimitivePour.create.apply(eda.pcb_PrimitivePour, _variants[_vi].args);
|
|
501
|
+
if (pour) usedRoute = 'static/' + _variants[_vi].tag;
|
|
502
|
+
} catch (_vErr) {
|
|
503
|
+
pourRoutes.push(_variants[_vi].tag + ': ' + (_vErr && _vErr.message ? _vErr.message : String(_vErr)));
|
|
504
|
+
}
|
|
505
|
+
}
|
|
506
|
+
var pourPolygon = _pSimple || _pComplex;
|
|
507
|
+
// Route 2: the instance entry point (@internal in the typings, but the class
|
|
508
|
+
// constructor is exported at runtime on some builds).
|
|
509
|
+
if (!pour) {
|
|
510
|
+
var PourCtor = null;
|
|
511
|
+
try {
|
|
512
|
+
if (typeof eda.IPCB_PrimitivePour === 'function') PourCtor = eda.IPCB_PrimitivePour;
|
|
513
|
+
else if (eda.pcb_PrimitivePour && typeof eda.pcb_PrimitivePour.constructor === 'function' && eda.pcb_PrimitivePour.constructor !== Object) PourCtor = eda.pcb_PrimitivePour.constructor;
|
|
514
|
+
} catch (ctorProbeError) { PourCtor = null; }
|
|
515
|
+
if (PourCtor !== null) {
|
|
516
|
+
usedRoute = 'instance';
|
|
517
|
+
try {
|
|
518
|
+
var pourInstance = new PourCtor(
|
|
519
|
+
optional(pourArgs[0]), optional(pourArgs[1]), pourPolygon, optional(pourArgs[3]), optional(pourArgs[4]),
|
|
520
|
+
optional(pourArgs[5]), optional(pourArgs[6]), optional(pourArgs[7]), optional(pourArgs[8])
|
|
521
|
+
);
|
|
522
|
+
pour = await pourInstance.create();
|
|
523
|
+
} catch (instanceError) {
|
|
524
|
+
pourRoutes.push('instance: ' + (instanceError && instanceError.message ? instanceError.message : String(instanceError)));
|
|
525
|
+
usedRoute = 'none';
|
|
526
|
+
}
|
|
527
|
+
} else {
|
|
528
|
+
pourRoutes.push('instance: no exported constructor');
|
|
529
|
+
usedRoute = 'none';
|
|
530
|
+
}
|
|
531
|
+
}
|
|
532
|
+
if (!pour) {
|
|
533
|
+
throw new Error('dsh.internal.pourCreate: every route refused (' + pourRoutes.join(' | ') + ')');
|
|
534
|
+
}
|
|
535
|
+
// A pour border alone leaves no copper: rebuild so the fill (and the clearance
|
|
536
|
+
// against other nets) is actually computed.
|
|
537
|
+
var rebuilt = false;
|
|
538
|
+
if (pour && typeof pour.rebuildCopperRegion === 'function') {
|
|
539
|
+
try { await pour.rebuildCopperRegion(); rebuilt = true; } catch (error) { rebuilt = false; }
|
|
540
|
+
}
|
|
541
|
+
var pourJson = toJsonable(pour);
|
|
542
|
+
if (pourJson && typeof pourJson === 'object') { pourJson.rebuilt = rebuilt; pourJson.route = usedRoute; }
|
|
543
|
+
return pourJson;
|
|
544
|
+
}
|
|
545
|
+
// PATCHED(2026-10-31): a pour border that came from the project file (or from a
|
|
546
|
+
// build where create() worked) carries no POURED copper until the app recomputes
|
|
547
|
+
// it. The static rebuildCopperRegions() is absent at runtime on 4.1.93, so try the
|
|
548
|
+
// instance method per pour, and report what the runtime actually offers.
|
|
549
|
+
if (message.method === 'dsh.internal.pourRebuild') {
|
|
550
|
+
var prArgs = message.args || [];
|
|
551
|
+
var prIds = Array.isArray(prArgs[0]) && prArgs[0].length ? prArgs[0] : null;
|
|
552
|
+
if (typeof eda === 'undefined' || !eda.pcb_PrimitivePour) {
|
|
553
|
+
throw new Error('dsh.internal.pourRebuild: PCB pour APIs are not available');
|
|
554
|
+
}
|
|
555
|
+
var prAll = await eda.pcb_PrimitivePour.getAll();
|
|
556
|
+
var prList = Array.isArray(prAll) ? prAll : [];
|
|
557
|
+
var prReport = [];
|
|
558
|
+
var prProtoKeys = [];
|
|
559
|
+
if (prList.length && prList[0]) {
|
|
560
|
+
try {
|
|
561
|
+
var _p0 = prList[0];
|
|
562
|
+
var _proto = Object.getPrototypeOf(_p0);
|
|
563
|
+
prProtoKeys = _proto ? Object.getOwnPropertyNames(_proto) : [];
|
|
564
|
+
} catch (_pkErr) { prProtoKeys = []; }
|
|
565
|
+
}
|
|
566
|
+
for (var _pi = 0; _pi < prList.length; _pi++) {
|
|
567
|
+
var _pour = prList[_pi];
|
|
568
|
+
var _pid = '';
|
|
569
|
+
try {
|
|
570
|
+
_pid = (_pour && typeof _pour.getState_PrimitiveId === 'function') ? _pour.getState_PrimitiveId() : '';
|
|
571
|
+
} catch (_idErr) { _pid = ''; }
|
|
572
|
+
if (!_pid && _pour && _pour.primitiveId) _pid = String(_pour.primitiveId);
|
|
573
|
+
if (prIds && prIds.indexOf(_pid) < 0) continue;
|
|
574
|
+
var _entry = { id: _pid, tries: [] };
|
|
575
|
+
var _methods = ['rebuildCopperRegion', 'rebuildCopperRegions', 'updateCopperRegion', 'composeCopperRegion', 'refreshCopperRegion'];
|
|
576
|
+
for (var _mi = 0; _mi < _methods.length; _mi++) {
|
|
577
|
+
var _mn = _methods[_mi];
|
|
578
|
+
if (typeof _pour[_mn] !== 'function') { _entry.tries.push(_mn + ':absent'); continue; }
|
|
579
|
+
try {
|
|
580
|
+
var _r = await _pour[_mn]();
|
|
581
|
+
_entry.tries.push(_mn + ':ok' + (_r ? '' : ':undefined'));
|
|
582
|
+
} catch (_me) {
|
|
583
|
+
_entry.tries.push(_mn + ':err:' + (_me && _me.message ? _me.message : String(_me)));
|
|
584
|
+
}
|
|
585
|
+
}
|
|
586
|
+
prReport.push(_entry);
|
|
587
|
+
}
|
|
588
|
+
return {
|
|
589
|
+
pourCount: prList.length,
|
|
590
|
+
filtered: prIds ? prIds.length : 0,
|
|
591
|
+
prototypeMethods: prProtoKeys,
|
|
592
|
+
staticKeys: (function () { try { return Object.getOwnPropertyNames(eda.pcb_PrimitivePour); } catch (_sk) { return []; } })(),
|
|
593
|
+
pours: prReport
|
|
594
|
+
};
|
|
595
|
+
}
|
|
596
|
+
// Internal helper: long-running jobs (auto-routing / auto-layout) are started
|
|
597
|
+
// without awaiting so the MCP call returns immediately; the operation keeps
|
|
598
|
+
// running inside EDA (with its progress bar) and is polled via jobStatus.
|
|
599
|
+
// Awaiting it would hit the MCP tool timeout while EDA keeps working.
|
|
600
|
+
if (message.method === 'dsh.internal.jobStart') {
|
|
601
|
+
var jobArgs = message.args || [];
|
|
602
|
+
var kind = jobArgs[0];
|
|
603
|
+
var jobProps = jobArgs[1];
|
|
604
|
+
if (kind !== 'autoRoute' && kind !== 'autoLayout' && kind !== 'importChanges' && kind !== 'clearRouting') {
|
|
605
|
+
throw new Error('dsh.internal.jobStart: unsupported job kind ' + String(kind));
|
|
606
|
+
}
|
|
607
|
+
if (JOB && JOB.running) {
|
|
608
|
+
return { started: false, alreadyRunning: true, kind: JOB.kind, startedAt: JOB.startedAt, elapsedMs: Date.now() - JOB.startedAt };
|
|
609
|
+
}
|
|
610
|
+
JOB = { kind: kind, startedAt: Date.now(), finishedAt: 0, running: true, result: null, error: null };
|
|
611
|
+
// importChanges() blocks until the user answers EDA's "sync schematic changes"
|
|
612
|
+
// confirmation dialog, so it must never be awaited by the MCP call either.
|
|
613
|
+
var runner = kind === 'autoRoute'
|
|
614
|
+
? eda.pcb_Document.autoRouting(jobProps === null ? undefined : jobProps)
|
|
615
|
+
: (kind === 'autoLayout'
|
|
616
|
+
? eda.pcb_Document.autoLayout()
|
|
617
|
+
: (kind === 'importChanges'
|
|
618
|
+
? eda.pcb_Document.importChanges(jobProps === null ? undefined : jobProps)
|
|
619
|
+
: eda.pcb_Document.clearRouting(jobProps === null ? undefined : jobProps)));
|
|
620
|
+
Promise.resolve().then(function () { return runner; }).then(function (result) {
|
|
621
|
+
JOB.running = false;
|
|
622
|
+
JOB.finishedAt = Date.now();
|
|
623
|
+
JOB.result = toJsonable(result);
|
|
624
|
+
}).catch(function (error) {
|
|
625
|
+
JOB.running = false;
|
|
626
|
+
JOB.finishedAt = Date.now();
|
|
627
|
+
JOB.error = error && error.message ? error.message : String(error);
|
|
628
|
+
});
|
|
629
|
+
return { started: true, kind: kind, startedAt: JOB.startedAt };
|
|
630
|
+
}
|
|
631
|
+
if (message.method === 'dsh.internal.jobStatus') {
|
|
632
|
+
if (!JOB) return { running: false, job: null };
|
|
633
|
+
return {
|
|
634
|
+
running: JOB.running,
|
|
635
|
+
kind: JOB.kind,
|
|
636
|
+
startedAt: JOB.startedAt,
|
|
637
|
+
finishedAt: JOB.finishedAt || null,
|
|
638
|
+
elapsedMs: (JOB.running ? Date.now() : (JOB.finishedAt || Date.now())) - JOB.startedAt,
|
|
639
|
+
result: JOB.result,
|
|
640
|
+
error: JOB.error
|
|
641
|
+
};
|
|
642
|
+
}
|
|
643
|
+
// Two sets gate the wire: ALLOWED_API for plain calls and FILE_METHODS for the ones
|
|
644
|
+
// that hand a File back. Only checking the first one made every export (BOM,
|
|
645
|
+
// netlist, Gerber) answer "API method not allowed".
|
|
646
|
+
if (!ALLOWED_API.has(message.method) && !FILE_METHODS.has(message.method)) {
|
|
647
|
+
throw new Error('API method not allowed: ' + String(message.method) +
|
|
648
|
+
' (the extension accepts ' + (ALLOWED_API.size + FILE_METHODS.size) + ' methods: ALLOWED_API + FILE_METHODS)');
|
|
649
|
+
}
|
|
650
|
+
if (message.method === 'dmt_EditorControl.getCurrentRenderedAreaImage') {
|
|
651
|
+
var blob = await callApi(message.method, message.args || []);
|
|
652
|
+
if (!blob) return null;
|
|
653
|
+
return await blobToDataUrl(blob);
|
|
654
|
+
}
|
|
655
|
+
if (FILE_METHODS.has(message.method)) {
|
|
656
|
+
var file = await callApi(message.method, message.args || []);
|
|
657
|
+
if (!file) return null;
|
|
658
|
+
return await blobToFilePayload(file);
|
|
659
|
+
}
|
|
660
|
+
return toJsonable(await callApi(message.method, message.args || []));
|
|
661
|
+
}
|
|
662
|
+
|
|
663
|
+
function sendObject(object) {
|
|
664
|
+
if (!wsReady) return;
|
|
665
|
+
try {
|
|
666
|
+
eda.sys_WebSocket.send(WS_ID, JSON.stringify(object));
|
|
667
|
+
} catch (error) {
|
|
668
|
+
console.error('[dsh-eda-mcp] send failed:', error);
|
|
669
|
+
}
|
|
670
|
+
}
|
|
671
|
+
|
|
672
|
+
function onReceiveMessage(event) {
|
|
673
|
+
var raw = event && event.data;
|
|
674
|
+
var text = typeof raw === 'string' ? raw : String(raw);
|
|
675
|
+
var message;
|
|
676
|
+
try { message = JSON.parse(text); } catch (_) { return; }
|
|
677
|
+
if (!message || typeof message !== 'object') return;
|
|
678
|
+
if (message.type === 'pong') { lastPongAt = Date.now(); return; }
|
|
679
|
+
if (typeof message.id === 'undefined') return;
|
|
680
|
+
Promise.resolve().then(function () {
|
|
681
|
+
return handleMessage(message);
|
|
682
|
+
}).then(function (result) {
|
|
683
|
+
sendObject({ id: message.id, ok: true, result: result });
|
|
684
|
+
}).catch(function (error) {
|
|
685
|
+
sendObject({ id: message.id, ok: false, error: { message: error && error.message ? error.message : String(error) } });
|
|
686
|
+
});
|
|
687
|
+
}
|
|
688
|
+
|
|
689
|
+
function registerWebSocket() {
|
|
690
|
+
lastRegisterAt = Date.now();
|
|
691
|
+
try {
|
|
692
|
+
eda.sys_WebSocket.register(
|
|
693
|
+
WS_ID,
|
|
694
|
+
WS_URL,
|
|
695
|
+
onReceiveMessage,
|
|
696
|
+
function () {
|
|
697
|
+
wsReady = true;
|
|
698
|
+
lastPongAt = Date.now();
|
|
699
|
+
// The 'not connected' tip is sticky by design while we are offline; once the
|
|
700
|
+
// socket is up it must go away, otherwise it sits on the canvas forever.
|
|
701
|
+
hideConnectTip();
|
|
702
|
+
sendObject({ type: 'hello', version: VERSION });
|
|
703
|
+
}
|
|
704
|
+
);
|
|
705
|
+
} catch (error) {
|
|
706
|
+
wsReady = false;
|
|
707
|
+
console.error('[dsh-eda-mcp] websocket register failed:', error);
|
|
708
|
+
setTimeout(registerWebSocket, 5000);
|
|
709
|
+
}
|
|
710
|
+
}
|
|
711
|
+
|
|
712
|
+
/**
|
|
713
|
+
* The official SYS_WebSocket API has no close/error callback and no
|
|
714
|
+
* auto-reconnect, so the heartbeat below re-registers the socket whenever the
|
|
715
|
+
* DSH bridge process restarts or disappears.
|
|
716
|
+
*/
|
|
717
|
+
function reconnectIfIdle() {
|
|
718
|
+
if (Date.now() - lastRegisterAt < 5000) return;
|
|
719
|
+
registerWebSocket();
|
|
720
|
+
}
|
|
721
|
+
|
|
722
|
+
function showToast(message, type, seconds) {
|
|
723
|
+
try {
|
|
724
|
+
if (typeof eda !== 'undefined' && eda.sys_Message && typeof eda.sys_Message.showToastMessage === 'function') {
|
|
725
|
+
// Always pass a finite timer: omitting it (or 0) leaves the toast on screen forever.
|
|
726
|
+
eda.sys_Message.showToastMessage(message, type || 'info', seconds || 4);
|
|
727
|
+
} else {
|
|
728
|
+
console.log('[dsh-eda-mcp] ' + message);
|
|
729
|
+
}
|
|
730
|
+
} catch (error) {
|
|
731
|
+
console.error('[dsh-eda-mcp] toast failed:', error);
|
|
732
|
+
}
|
|
733
|
+
}
|
|
734
|
+
|
|
735
|
+
function menuGroup() {
|
|
736
|
+
return {
|
|
737
|
+
id: 'dsh-eda-mcp',
|
|
738
|
+
title: 'DSH MCP',
|
|
739
|
+
menuItems: [
|
|
740
|
+
{ id: 'connect', title: '连接 DSH MCP 服务器', registerFn: 'dshEdaMcpConnect' },
|
|
741
|
+
{ id: 'disconnect', title: '断开 DSH MCP 连接', registerFn: 'dshEdaMcpDisconnect' },
|
|
742
|
+
{ id: 'status', title: '查看 DSH MCP 连接状态', registerFn: 'dshEdaMcpStatus' }
|
|
743
|
+
]
|
|
744
|
+
};
|
|
745
|
+
}
|
|
746
|
+
|
|
747
|
+
function registerHeaderMenus(attempt) {
|
|
748
|
+
attempt = attempt || 0;
|
|
749
|
+
if (typeof eda === 'undefined' || !eda.sys_HeaderMenu || typeof eda.sys_HeaderMenu.insertHeaderMenus !== 'function') {
|
|
750
|
+
console.warn('[dsh-eda-mcp] SYS_HeaderMenu not available yet, attempt ' + attempt);
|
|
751
|
+
if (attempt < 5) setTimeout(function () { registerHeaderMenus(attempt + 1); }, 1000 * (attempt + 1));
|
|
752
|
+
return;
|
|
753
|
+
}
|
|
754
|
+
try {
|
|
755
|
+
var result = eda.sys_HeaderMenu.insertHeaderMenus({ schematic: [menuGroup()], pcb: [menuGroup()] });
|
|
756
|
+
console.log('[dsh-eda-mcp] header menus registered (attempt ' + attempt + ')');
|
|
757
|
+
if (result && typeof result.catch === 'function') {
|
|
758
|
+
result.catch(function (error) {
|
|
759
|
+
console.error('[dsh-eda-mcp] header menu registration rejected:', error);
|
|
760
|
+
});
|
|
761
|
+
}
|
|
762
|
+
} catch (error) {
|
|
763
|
+
console.error('[dsh-eda-mcp] header menu registration failed:', error);
|
|
764
|
+
if (attempt < 5) setTimeout(function () { registerHeaderMenus(attempt + 1); }, 1000 * (attempt + 1));
|
|
765
|
+
}
|
|
766
|
+
}
|
|
767
|
+
|
|
768
|
+
try {
|
|
769
|
+
globalScope.dshEdaMcpConnect = dshEdaMcpConnect;
|
|
770
|
+
globalScope.dshEdaMcpDisconnect = dshEdaMcpDisconnect;
|
|
771
|
+
globalScope.dshEdaMcpStatus = dshEdaMcpStatus;
|
|
772
|
+
} catch (_) { /* ignore */ }
|
|
773
|
+
|
|
774
|
+
DSH_EDA_MCP.connect = function () {
|
|
775
|
+
try {
|
|
776
|
+
wsReady = false;
|
|
777
|
+
registerWebSocket();
|
|
778
|
+
showToast('DSH MCP:正在连接 ws://127.0.0.1:39009/ws ...', 'info');
|
|
779
|
+
setTimeout(function () {
|
|
780
|
+
showToast(
|
|
781
|
+
wsReady ? 'DSH MCP:已连接' : 'DSH MCP:连接请求已发送;请确认 DSH 桥接服务已启动,且本扩展已开启「允许外部交互」',
|
|
782
|
+
wsReady ? 'success' : 'warning'
|
|
783
|
+
);
|
|
784
|
+
}, 1000);
|
|
785
|
+
return true;
|
|
786
|
+
} catch (error) {
|
|
787
|
+
showToast('DSH MCP:连接失败 ' + (error && error.message ? error.message : String(error)), 'error');
|
|
788
|
+
return false;
|
|
789
|
+
}
|
|
790
|
+
};
|
|
791
|
+
|
|
792
|
+
DSH_EDA_MCP.disconnect = function () {
|
|
793
|
+
try {
|
|
794
|
+
wsReady = false;
|
|
795
|
+
eda.sys_WebSocket.close(WS_ID);
|
|
796
|
+
showToast('DSH MCP:已断开连接', 'info');
|
|
797
|
+
return true;
|
|
798
|
+
} catch (error) {
|
|
799
|
+
showToast('DSH MCP:断开失败 ' + (error && error.message ? error.message : String(error)), 'error');
|
|
800
|
+
return false;
|
|
801
|
+
}
|
|
802
|
+
};
|
|
803
|
+
|
|
804
|
+
DSH_EDA_MCP.status = function () {
|
|
805
|
+
var connected = wsReady === true;
|
|
806
|
+
showToast(
|
|
807
|
+
'DSH MCP:' + (connected ? '已连接' : '未连接') + ';端口 39009;扩展 v' + VERSION,
|
|
808
|
+
connected ? 'success' : 'warning'
|
|
809
|
+
);
|
|
810
|
+
return connected;
|
|
811
|
+
};
|
|
812
|
+
|
|
813
|
+
DSH_EDA_MCP.isConnected = function () { return wsReady === true; };
|
|
814
|
+
|
|
815
|
+
function registerManualConnectBus() {
|
|
816
|
+
try {
|
|
817
|
+
if (typeof eda === 'undefined' || !eda.sys_MessageBus || typeof eda.sys_MessageBus.subscribePublic !== 'function') return;
|
|
818
|
+
eda.sys_MessageBus.subscribePublic('dsh-eda-mcp/connect', function () {
|
|
819
|
+
if (typeof DSH_EDA_MCP.connect === 'function') DSH_EDA_MCP.connect();
|
|
820
|
+
});
|
|
821
|
+
eda.sys_MessageBus.subscribePublic('dsh-eda-mcp/status', function () {
|
|
822
|
+
if (typeof DSH_EDA_MCP.status === 'function') DSH_EDA_MCP.status();
|
|
823
|
+
});
|
|
824
|
+
console.log('[dsh-eda-mcp] public connect bus registered');
|
|
825
|
+
} catch (error) {
|
|
826
|
+
console.error('[dsh-eda-mcp] public connect bus registration failed:', error);
|
|
827
|
+
}
|
|
828
|
+
}
|
|
829
|
+
|
|
830
|
+
// One removable tip instead of a stack of toasts: showToastMessage has no hide API
|
|
831
|
+
// (timer:0 meant "never"), so a stack of them used to pile up over the canvas and the
|
|
832
|
+
// '未连接' banner survived even after the connection was established (measured).
|
|
833
|
+
var CONNECT_TIP = 'DSH MCP 未连接:菜单 DSH MCP → 连接 DSH MCP 服务器(ws://127.0.0.1:39009/ws)';
|
|
834
|
+
|
|
835
|
+
function hideConnectTip() {
|
|
836
|
+
try {
|
|
837
|
+
if (typeof eda !== 'undefined' && eda.sys_Message && typeof eda.sys_Message.removeFollowMouseTip === 'function') {
|
|
838
|
+
eda.sys_Message.removeFollowMouseTip(CONNECT_TIP);
|
|
839
|
+
}
|
|
840
|
+
} catch (_) { /* ignore */ }
|
|
841
|
+
}
|
|
842
|
+
|
|
843
|
+
function showConnectToast() {
|
|
844
|
+
if (wsReady) return;
|
|
845
|
+
try {
|
|
846
|
+
if (typeof eda === 'undefined' || !eda.sys_Message) return;
|
|
847
|
+
if (typeof eda.sys_Message.showFollowMouseTip === 'function') {
|
|
848
|
+
eda.sys_Message.showFollowMouseTip(CONNECT_TIP, 8000);
|
|
849
|
+
return;
|
|
850
|
+
}
|
|
851
|
+
eda.sys_Message.showToastMessage(CONNECT_TIP, 'warning', 8);
|
|
852
|
+
} catch (error) {
|
|
853
|
+
console.error('[dsh-eda-mcp] connect tip failed:', error);
|
|
854
|
+
}
|
|
855
|
+
}
|
|
856
|
+
|
|
857
|
+
var timer = setInterval(function () {
|
|
858
|
+
if (!wsReady) { reconnectIfIdle(); return; }
|
|
859
|
+
if (lastPongAt > 0 && Date.now() - lastPongAt > HEARTBEAT_MS * 2.5) {
|
|
860
|
+
// The bridge is gone (e.g. the DSH plugin rebooted its bridge child).
|
|
861
|
+
wsReady = false;
|
|
862
|
+
try { eda.sys_WebSocket.close(WS_ID); } catch (_) { /* ignore */ }
|
|
863
|
+
reconnectIfIdle();
|
|
864
|
+
return;
|
|
865
|
+
}
|
|
866
|
+
try {
|
|
867
|
+
eda.sys_WebSocket.send(WS_ID, JSON.stringify({ type: 'ping' }));
|
|
868
|
+
} catch (error) {
|
|
869
|
+
wsReady = false;
|
|
870
|
+
reconnectIfIdle();
|
|
871
|
+
}
|
|
872
|
+
}, HEARTBEAT_MS);
|
|
873
|
+
|
|
874
|
+
globalScope.__DSH_EDA_MCP_BRIDGE_STATE__ = { version: VERSION, timer: timer };
|
|
875
|
+
registerManualConnectBus();
|
|
876
|
+
registerHeaderMenus();
|
|
877
|
+
registerWebSocket();
|
|
878
|
+
setTimeout(showConnectToast, 3000);
|
|
879
|
+
})();
|