@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.
Files changed (53) hide show
  1. package/AGENTS.md +63 -0
  2. package/CHANGELOG.md +792 -0
  3. package/CONTRIBUTING.md +154 -0
  4. package/LICENSE +55 -0
  5. package/README.md +290 -2
  6. package/README.zh.md +449 -0
  7. package/cordis.patch.yml +7 -0
  8. package/docs/design-rules.md +254 -0
  9. package/docs/dsh-version-sweep.md +73 -0
  10. package/docs/pitfalls.md +99 -0
  11. package/docs/project-status.md +100 -0
  12. package/docs/release.md +215 -0
  13. package/docs/schematic-workflow.md +297 -0
  14. package/docs/tools.md +184 -0
  15. package/jlc-extension/api-bridge.js +879 -0
  16. package/jlc-extension/extension.json +104 -0
  17. package/lib/bridge.mjs +4179 -0
  18. package/lib/client.js +1605 -0
  19. package/lib/extension-digest.json +6 -0
  20. package/lib/index.js +1261 -0
  21. package/package.json +144 -4
  22. package/src/bridge/design-intake.ts +133 -0
  23. package/src/bridge/design-rules.ts +228 -0
  24. package/src/bridge/eda-launch.ts +267 -0
  25. package/src/bridge/extension-package.ts +35 -0
  26. package/src/bridge/index.ts +3576 -0
  27. package/src/bridge/netlist-erc.ts +305 -0
  28. package/src/bridge/schematic-audit.ts +131 -0
  29. package/src/bridge/workflow-guide.ts +65 -0
  30. package/src/bridge-diagnostics.ts +185 -0
  31. package/src/client/EdaMcpSettingsCard.tsx +571 -0
  32. package/src/client/index.ts +40 -0
  33. package/src/client/locales.ts +83 -0
  34. package/src/client/settings-scope.ts +160 -0
  35. package/src/config-values.ts +60 -0
  36. package/src/guidance.ts +35 -0
  37. package/src/index.ts +365 -0
  38. package/src/loopback.ts +48 -0
  39. package/src/node-runtime.ts +120 -0
  40. package/src/routes.ts +377 -0
  41. package/src/skills-content.ts +309 -0
  42. package/src/skills.ts +91 -0
  43. package/test/config-values.test.ts +60 -0
  44. package/test/design-intake.test.ts +40 -0
  45. package/test/design-rules.test.ts +72 -0
  46. package/test/eda-launch.test.ts +114 -0
  47. package/test/extension-package.test.ts +27 -0
  48. package/test/extension-routes.test.ts +116 -0
  49. package/test/netlist-erc.test.ts +90 -0
  50. package/test/node-runtime.test.ts +65 -0
  51. package/test/schematic-audit.test.ts +71 -0
  52. package/test/seams.test.ts +164 -0
  53. package/test/skills.test.ts +68 -0
@@ -0,0 +1,305 @@
1
+ /**
2
+ * Netlist-level electrical smoke test.
3
+ *
4
+ * DRC only reports geometry/rule problems and, on EDA 3.2.186, hands back
5
+ * aggregated counts for the schematic. A generated circuit can pass DRC and still be
6
+ * electrically wrong (measured in public LLM-vs-EDA comparisons: USB ESD ground left
7
+ * floating, D+ missing its pull-up, CH340G without a 12 MHz crystal, Type-C wired for
8
+ * one insert direction only, too few decoupling caps). Those all show up in the
9
+ * netlist, so they are checked here without any application support.
10
+ */
11
+
12
+ export interface ErcPin {
13
+ designator: string
14
+ number: string
15
+ name: string
16
+ net: string
17
+ }
18
+
19
+ export interface ErcComponent {
20
+ designator: string
21
+ part: string
22
+ footprint: string
23
+ /** LCSC / supplier part number, empty when the part is not orderable by number. */
24
+ supplierPart: string
25
+ addIntoBom: boolean
26
+ convertToPcb: boolean
27
+ pins: Array<{ number: string; name: string; net: string }>
28
+ }
29
+
30
+ export type ErcStatus = 'pass' | 'fail' | 'warn' | 'skip'
31
+
32
+ export interface ErcCheck {
33
+ id: string
34
+ status: ErcStatus
35
+ message: string
36
+ details?: string[]
37
+ }
38
+
39
+ export interface ErcReport {
40
+ ok: boolean
41
+ counts: { pass: number; fail: number; warn: number; skip: number }
42
+ componentCount: number
43
+ netCount: number
44
+ checks: ErcCheck[]
45
+ }
46
+
47
+ const POWER_NET = /^(?:\+?\d+V\d*|V?(?:CC|DD|BAT|IN|BUS|SYS|MOTOR)|AVDD|DVDD|VBUS|VUSB|VBAT|3V3|5V|12V)$/i
48
+ const GROUND_NET = /^(?:GND|AGND|DGND|PGND|GNDA|GNDD|VSS|0V|EARTH)$/i
49
+ const USD_DP = /(?:^|_)(?:USB_?D\+?P|D\+|USB_?DP|DP)$/i
50
+ const USD_DM = /(?:^|_)(?:USB_?D-?M|D-|USB_?DM|DM)$/i
51
+ const CRYSTAL_HINT = /crystal|xtal|osc|谐振|晶振|5032|3225/i
52
+ const MCU_PART = /STM32|GD32|ATmega|ATMEGA|ESP32|ESP8266|CH32|HC32|MM32|APM32|N76|PIC\d|RP2040|MSP430/i
53
+
54
+ function normNet(value: unknown): string {
55
+ return typeof value === 'string' ? value.trim() : ''
56
+ }
57
+
58
+ /** Reads the JLCEDA netlist shape: netlist.components[key].pinInfoMap[pinKey]. */
59
+ export function parseNetlist(raw: any): { components: ErcComponent[]; pins: ErcPin[] } {
60
+ const source = raw && typeof raw === 'object' && raw.components ? raw : {}
61
+ const map = source.components ?? {}
62
+ const components: ErcComponent[] = []
63
+ const pins: ErcPin[] = []
64
+ for (const key of Object.keys(map)) {
65
+ const entry = map[key] ?? {}
66
+ const props = entry.props ?? {}
67
+ const designator = String(props.Designator ?? props.designator ?? key)
68
+ const pinInfoMap = entry.pinInfoMap ?? {}
69
+ const own: Array<{ number: string; name: string; net: string }> = []
70
+ for (const pinKey of Object.keys(pinInfoMap)) {
71
+ const pin = pinInfoMap[pinKey] ?? {}
72
+ const number = String(pin.number ?? pin.props?.['Pin Number'] ?? pinKey)
73
+ const name = String(pin.name ?? '')
74
+ const net = normNet(pin.net)
75
+ own.push({ number, name, net })
76
+ pins.push({ designator, number, name, net })
77
+ }
78
+ const flag = (value: unknown, fallback: boolean) => {
79
+ if (value === undefined || value === null || value === '') return fallback
80
+ return !/^(?:no|false|0)$/i.test(String(value).trim())
81
+ }
82
+ components.push({
83
+ designator,
84
+ part: String(props['Manufacturer Part'] ?? props.Device ?? ''),
85
+ footprint: String(props.Footprint ?? ''),
86
+ supplierPart: String(props['Supplier Part'] ?? props['LCSC Part'] ?? ''),
87
+ addIntoBom: flag(props['Add into BOM'], true),
88
+ convertToPcb: flag(props['Convert to PCB'], true),
89
+ pins: own,
90
+ })
91
+ }
92
+ return { components, pins }
93
+ }
94
+
95
+ function netsOf(pins: ErcPin[]): Map<string, ErcPin[]> {
96
+ const nets = new Map<string, ErcPin[]>()
97
+ for (const pin of pins) {
98
+ if (pin.net === '') continue
99
+ const list = nets.get(pin.net)
100
+ if (list) list.push(pin)
101
+ else nets.set(pin.net, [pin])
102
+ }
103
+ return nets
104
+ }
105
+
106
+ function capsBridging(net: string, groundNet: string, components: ErcComponent[]): number {
107
+ let count = 0
108
+ for (const component of components) {
109
+ if (!/^C\d*$/i.test(component.designator)) continue
110
+ const onNet = component.pins.some(pin => pin.net === net)
111
+ const onGround = component.pins.some(pin => pin.net === groundNet)
112
+ if (onNet && onGround) count++
113
+ }
114
+ return count
115
+ }
116
+
117
+ function resistorsOn(net: string, components: ErcComponent[], otherNet?: string): string[] {
118
+ const found: string[] = []
119
+ for (const component of components) {
120
+ if (!/^R\d*$/i.test(component.designator)) continue
121
+ const onNet = component.pins.some(pin => pin.net === net)
122
+ if (!onNet) continue
123
+ if (otherNet === undefined) { found.push(component.designator); continue }
124
+ const others = component.pins.filter(pin => pin.net !== net).map(pin => pin.net)
125
+ if (others.some(name => name === otherNet)) found.push(component.designator)
126
+ }
127
+ return found
128
+ }
129
+
130
+ export function runNetlistErc(rawNetlist: any): ErcReport {
131
+ const { components, pins } = parseNetlist(rawNetlist)
132
+ const nets = netsOf(pins)
133
+ const checks: ErcCheck[] = []
134
+ const add = (id: string, status: ErcStatus, message: string, details?: string[]) => {
135
+ checks.push(details && details.length > 0 ? { id, status, message, details } : { id, status, message })
136
+ }
137
+
138
+ if (components.length === 0) {
139
+ add('netlist', 'skip', 'netlist has no components: open a schematic page first (eda_open_document)')
140
+ return { ok: false, counts: { pass: 0, fail: 0, warn: 0, skip: 1 }, componentCount: 0, netCount: 0, checks }
141
+ }
142
+
143
+ // 1. duplicate designators
144
+ const seen = new Map<string, number>()
145
+ for (const component of components) seen.set(component.designator, (seen.get(component.designator) ?? 0) + 1)
146
+ const duplicates = [...seen.entries()].filter(([, n]) => n > 1).map(([name, n]) => name + ' x' + n)
147
+ add('duplicate-designator', duplicates.length === 0 ? 'pass' : 'fail',
148
+ duplicates.length === 0 ? 'every designator is unique' : duplicates.length + ' duplicated designator(s)', duplicates)
149
+
150
+ // 2. unconnected pins
151
+ const floating = pins.filter(pin => pin.net === '').map(pin => pin.designator + '.' + pin.number + (pin.name ? ' (' + pin.name + ')' : ''))
152
+ add('unconnected-pin', floating.length === 0 ? 'pass' : 'warn',
153
+ floating.length === 0 ? 'every pin is on a net' : floating.length + ' pin(s) are on no net (mark unused pins with eda_set_no_connect or wire them)',
154
+ floating.slice(0, 25))
155
+
156
+ // 3. single-endpoint nets
157
+ const dangling = [...nets.entries()].filter(([, list]) => list.length < 2)
158
+ .map(([name, list]) => name + ' <- ' + list.map(p => p.designator + '.' + p.number).join(','))
159
+ add('dangling-net', dangling.length === 0 ? 'pass' : 'fail',
160
+ dangling.length === 0 ? 'every net has at least two endpoints' : dangling.length + ' net(s) have a single endpoint',
161
+ dangling.slice(0, 25))
162
+
163
+ // 4. ground
164
+ const groundNet = [...nets.keys()].find(name => GROUND_NET.test(name)) ?? ''
165
+ add('ground-net', groundNet === '' ? 'fail' : (nets.get(groundNet)!.length >= 3 ? 'pass' : 'warn'),
166
+ groundNet === ''
167
+ ? 'no ground net (GND/VSS/AGND) found'
168
+ : 'ground net ' + groundNet + ' has ' + nets.get(groundNet)!.length + ' pins')
169
+
170
+ // 5. shorted component: a part whose every pin sits on one net
171
+ const shorted = components.filter(component => {
172
+ const names = new Set(component.pins.map(pin => pin.net).filter(name => name !== ''))
173
+ return component.pins.length >= 2 && names.size === 1
174
+ }).map(component => component.designator)
175
+ add('shorted-component', shorted.length === 0 ? 'pass' : 'fail',
176
+ shorted.length === 0 ? 'no component has all pins on one net' : shorted.length + ' component(s) have every pin on one net',
177
+ shorted)
178
+
179
+ // 6. power rails feed the ICs; each rail that an IC draws from needs decoupling
180
+ const powerNets = [...nets.keys()].filter(name => POWER_NET.test(name) && !GROUND_NET.test(name))
181
+ if (powerNets.length === 0) {
182
+ add('power-rail', 'warn', 'no power net recognised (3V3/5V/VCC/VDD/VBAT)')
183
+ } else {
184
+ const icPowerPins = pins.filter(pin => /^(?:VDD|VCC|VBAT|VDDA|AVDD|DVDD|VIN|V\+)/i.test(pin.name) && !GROUND_NET.test(pin.net))
185
+ const unpowered = icPowerPins.filter(pin => pin.net === '').map(pin => pin.designator + '.' + pin.number)
186
+ add('power-rail', unpowered.length === 0 ? 'pass' : 'fail',
187
+ unpowered.length === 0
188
+ ? powerNets.length + ' power net(s) (' + powerNets.slice(0, 4).join(', ') + ') feed ' + icPowerPins.length + ' IC power pin(s)'
189
+ : unpowered.length + ' IC power pin(s) are not on any net',
190
+ unpowered)
191
+ if (groundNet !== '') {
192
+ const thin = powerNets.filter(name => capsBridging(name, groundNet, components) === 0)
193
+ add('decoupling', thin.length === 0 ? 'pass' : 'warn',
194
+ thin.length === 0
195
+ ? 'every power rail has a capacitor to ' + groundNet
196
+ : thin.length + ' power rail(s) have no capacitor to ' + groundNet + ' (decoupling missing)',
197
+ thin)
198
+ } else {
199
+ add('decoupling', 'skip', 'no ground net, decoupling cannot be judged')
200
+ }
201
+ }
202
+
203
+ // 7. USB data pair + series resistors
204
+ const dp = [...nets.keys()].filter(name => USD_DP.test(name))
205
+ const dm = [...nets.keys()].filter(name => USD_DM.test(name))
206
+ if (dp.length === 0 && dm.length === 0) {
207
+ add('usb-data-pair', 'skip', 'no USB D+/D- net, skipped')
208
+ } else {
209
+ const missing = dp.length === 0 ? 'D+ net missing' : (dm.length === 0 ? 'D- net missing' : '')
210
+ const halves = dp.length === 0 || dm.length === 0 ? ['D+/D- are not a pair: ' + (dp.concat(dm).join(', ') || 'none')] : []
211
+ add('usb-data-pair', missing === '' ? 'pass' : 'fail',
212
+ missing === '' ? 'D+ (' + dp.join(',') + ') and D- (' + dm.join(',') + ') both exist' : missing, halves)
213
+ }
214
+
215
+ // 8. crystal: two distinct nets, a load cap on each, and an MCU clock pin
216
+ const crystals = components.filter(component =>
217
+ /^[XY]\d*$/i.test(component.designator) || CRYSTAL_HINT.test(component.part) || CRYSTAL_HINT.test(component.footprint))
218
+ const realCrystals = crystals.filter(component => {
219
+ const names = new Set(component.pins.map(pin => pin.net).filter(name => name !== ''))
220
+ return component.pins.length === 2 || (component.pins.length >= 2 && names.size === 2)
221
+ })
222
+ if (realCrystals.length === 0) {
223
+ add('crystal', 'skip', 'no crystal found, skipped (an RC oscillator design is fine)')
224
+ } else if (groundNet === '') {
225
+ add('crystal', 'skip', 'crystal present but no ground net, load caps cannot be judged')
226
+ } else {
227
+ const problems: string[] = []
228
+ for (const crystal of realCrystals) {
229
+ const own = crystal.pins.map(pin => pin.net).filter(name => name !== '')
230
+ if (new Set(own).size < 2) problems.push(crystal.designator + ': both pins are on ' + (own[0] ?? 'no net'))
231
+ for (const net of new Set(own)) {
232
+ if (capsBridging(net, groundNet, components) === 0) problems.push(crystal.designator + ': ' + net + ' has no load capacitor to ' + groundNet)
233
+ }
234
+ const touchesMcu = pins.some(pin => own.includes(pin.net) && MCU_PART.test(components.find(c => c.designator === pin.designator)?.part ?? ''))
235
+ if (!touchesMcu) problems.push(crystal.designator + ': neither pin reaches an MCU clock pin')
236
+ }
237
+ add('crystal', problems.length === 0 ? 'pass' : 'warn',
238
+ problems.length === 0 ? realCrystals.length + ' crystal(s) have two nets, load caps and an MCU connection' : problems.length + ' crystal problem(s)',
239
+ problems)
240
+ }
241
+
242
+ // 9. reset + boot strapping
243
+ const resetNet = [...nets.keys()].find(name => /^(?:N?RST|RESET|RESET_N|MCLR)$/i.test(name))
244
+ if (resetNet === undefined) {
245
+ add('reset', 'skip', 'no reset net, skipped')
246
+ } else if (groundNet === '') {
247
+ add('reset', 'skip', 'reset net present but no ground net')
248
+ } else {
249
+ const cap = capsBridging(resetNet, groundNet, components)
250
+ const pullup = resistorsOn(resetNet, components).length
251
+ const problems: string[] = []
252
+ if (cap === 0) problems.push(resetNet + ' has no capacitor to ' + groundNet)
253
+ if (pullup === 0) problems.push(resetNet + ' has no series/pull resistor')
254
+ add('reset', problems.length === 0 ? 'pass' : 'warn',
255
+ problems.length === 0 ? resetNet + ' has a capacitor and a resistor' : problems.length + ' reset problem(s)', problems)
256
+ }
257
+
258
+ const bootNet = [...nets.keys()].find(name => /^BOOT\d$/i.test(name))
259
+ if (bootNet === undefined) {
260
+ add('boot', 'skip', 'no BOOT0 net, skipped')
261
+ } else {
262
+ const resistors = resistorsOn(bootNet, components)
263
+ add('boot', resistors.length > 0 ? 'pass' : 'warn',
264
+ resistors.length > 0 ? bootNet + ' has resistor(s): ' + resistors.join(',') : bootNet + ' has no pull resistor (floating boot pin)')
265
+ }
266
+
267
+ // 10. USB Type-C CC resistors
268
+ const ccPins = pins.filter(pin => /^CC[12]$/i.test(pin.name))
269
+ if (ccPins.length === 0) {
270
+ add('type-c-cc', 'skip', 'no CC1/CC2 pins, skipped')
271
+ } else {
272
+ const missing = ccPins.filter(pin => pin.net === '' || (groundNet !== '' && resistorsOn(pin.net, components, groundNet).length === 0))
273
+ .map(pin => (pin.designator + '.' + pin.name + ' on ' + (pin.net || '<no net>') + ' without a resistor to GND'))
274
+ add('type-c-cc', missing.length === 0 ? 'pass' : 'warn',
275
+ missing.length === 0 ? 'CC1/CC2 each have a resistor to ground' : missing.length + ' CC pin(s) lack a 5.1k pull-down', missing)
276
+ }
277
+
278
+ // 11. orderability: a part without a supplier part number cannot be bought, and a
279
+ // part excluded from the PCB conversion never reaches the board.
280
+ const missingSupplier = components.filter(component => component.supplierPart.trim() === '')
281
+ .map(component => component.designator + (component.part ? ' (' + component.part + ')' : ''))
282
+ add('bom-part-number', missingSupplier.length === 0 ? 'pass' : 'warn',
283
+ missingSupplier.length === 0
284
+ ? 'every part carries a supplier part number'
285
+ : missingSupplier.length + ' part(s) have no supplier/LPSC part number (not orderable by number)',
286
+ missingSupplier.slice(0, 20))
287
+
288
+ const excluded = components.filter(component => !component.convertToPcb).map(component => component.designator)
289
+ add('pcb-conversion', excluded.length === 0 ? 'pass' : 'warn',
290
+ excluded.length === 0
291
+ ? 'every part is marked Convert to PCB'
292
+ : excluded.length + ' part(s) are excluded from the PCB conversion (they will be missing on the board)',
293
+ excluded.slice(0, 20))
294
+
295
+ const noFootprint = components.filter(component => component.convertToPcb && component.footprint.trim() === '')
296
+ .map(component => component.designator)
297
+ add('footprint', noFootprint.length === 0 ? 'pass' : 'fail',
298
+ noFootprint.length === 0
299
+ ? 'every PCB-bound part has a footprint'
300
+ : noFootprint.length + ' PCB-bound part(s) have no footprint and cannot be placed',
301
+ noFootprint.slice(0, 20))
302
+ const counts = { pass: 0, fail: 0, warn: 0, skip: 0 }
303
+ for (const check of checks) counts[check.status] += 1
304
+ return { ok: counts.fail === 0, counts, componentCount: components.length, netCount: nets.size, checks }
305
+ }
@@ -0,0 +1,131 @@
1
+ /**
2
+ * Schematic geometry audit.
3
+ *
4
+ * The netlist ERC judges connectivity; it cannot see that two symbols sit on top of
5
+ * each other, that a part hangs outside the A4 frame, or that half the sheet is off the
6
+ * 10-unit grid. Those make a sheet unreviewable while remaining electrically correct,
7
+ * so they are checked here from the bounding boxes the app reports.
8
+ */
9
+
10
+ export interface Bounds { left: number; right: number; top: number; bottom: number }
11
+
12
+ export interface AuditComponent {
13
+ designator: string
14
+ x: number
15
+ y: number
16
+ bounds?: Bounds
17
+ }
18
+
19
+ export interface SchematicAuditInput {
20
+ components: AuditComponent[]
21
+ frame: Bounds
22
+ titleBlock?: Bounds
23
+ /** Text/label anchor points, when the caller can read them. */
24
+ labels?: Array<{ text: string; x: number; y: number }>
25
+ grid?: number
26
+ }
27
+
28
+ export type AuditStatus = 'pass' | 'fail' | 'warn' | 'skip'
29
+
30
+ export interface AuditCheck { id: string; status: AuditStatus; message: string; details?: string[] }
31
+
32
+ export interface SchematicAuditReport {
33
+ ok: boolean
34
+ counts: { pass: number; fail: number; warn: number; skip: number }
35
+ componentCount: number,
36
+ checks: AuditCheck[]
37
+ }
38
+
39
+ const EPS = 0.5
40
+
41
+ function intersects(a: Bounds, b: Bounds): boolean {
42
+ return a.left < b.right - EPS && b.left < a.right - EPS && a.bottom < b.top - EPS && b.bottom < a.top - EPS
43
+ }
44
+
45
+ function area(b: Bounds): number {
46
+ return Math.max(0, b.right - b.left) * Math.max(0, b.top - b.bottom)
47
+ }
48
+
49
+ function inside(inner: Bounds, outer: Bounds): boolean {
50
+ return inner.left >= outer.left - EPS && inner.right <= outer.right + EPS && inner.bottom >= outer.bottom - EPS && inner.top <= outer.top + EPS
51
+ }
52
+
53
+ export function auditSchematicGeometry(input: SchematicAuditInput): SchematicAuditReport {
54
+ const grid = input.grid === undefined ? 10 : input.grid
55
+ const checks: AuditCheck[] = []
56
+ const add = (id: string, status: AuditStatus, message: string, details?: string[]) => {
57
+ checks.push(details && details.length > 0 ? { id, status, message, details } : { id, status, message })
58
+ }
59
+ const withBounds = input.components.filter(component => component.bounds !== undefined) as Array<AuditComponent & { bounds: Bounds }>
60
+
61
+ if (input.components.length === 0) {
62
+ add('sheet', 'skip', 'no component on the active schematic page')
63
+ return { ok: false, counts: { pass: 0, fail: 0, warn: 0, skip: 1 }, componentCount: 0, checks }
64
+ }
65
+ add('sheet', 'pass', input.components.length + ' component(s) read from the active page, ' + withBounds.length + ' with bounds')
66
+
67
+ // 1. outside the frame
68
+ const outside = withBounds.filter(component => !inside(component.bounds, input.frame))
69
+ .map(component => component.designator)
70
+ add('outside-frame', outside.length === 0 ? 'pass' : 'fail',
71
+ outside.length === 0 ? 'every symbol sits inside the frame' : outside.length + ' symbol(s) stick out of the frame',
72
+ outside.slice(0, 20))
73
+
74
+ // 2. title block keep-out
75
+ if (input.titleBlock === undefined) {
76
+ add('title-block', 'skip', 'no title block range supplied')
77
+ } else {
78
+ const block = input.titleBlock
79
+ const intruding = withBounds.filter(component => intersects(component.bounds, block)).map(component => component.designator)
80
+ add('title-block', intruding.length === 0 ? 'pass' : 'fail',
81
+ intruding.length === 0 ? 'nothing overlaps the title block' : intruding.length + ' symbol(s) overlap the title block',
82
+ intruding.slice(0, 20))
83
+ }
84
+
85
+ // 3. overlapping symbols
86
+ const overlaps: string[] = []
87
+ for (let i = 0; i < withBounds.length; i++) {
88
+ for (let j = i + 1; j < withBounds.length; j++) {
89
+ const a = withBounds[i]
90
+ const b = withBounds[j]
91
+ if (!intersects(a.bounds, b.bounds)) continue
92
+ const sharedW = Math.min(a.bounds.right, b.bounds.right) - Math.max(a.bounds.left, b.bounds.left)
93
+ const sharedH = Math.min(a.bounds.top, b.bounds.top) - Math.max(a.bounds.bottom, b.bounds.bottom)
94
+ const shared = Math.max(0, sharedW) * Math.max(0, sharedH)
95
+ const smaller = Math.min(area(a.bounds), area(b.bounds))
96
+ // Touching edges are normal; only a real body-on-body overlap is a defect.
97
+ if (smaller > 0 && shared / smaller > 0.08) overlaps.push(a.designator + ' x ' + b.designator)
98
+ }
99
+ }
100
+
101
+ add('symbol-overlap', overlaps.length === 0 ? 'pass' : 'fail',
102
+ overlaps.length === 0 ? 'no two symbols overlap' : overlaps.length + ' overlapping symbol pair(s)',
103
+ overlaps.slice(0, 20))
104
+
105
+ // 4. grid alignment
106
+ const offGrid = input.components.filter(component =>
107
+ Math.abs(component.x / grid - Math.round(component.x / grid)) > 0.01 ||
108
+ Math.abs(component.y / grid - Math.round(component.y / grid)) > 0.01)
109
+ .map(component => component.designator + ' @' + component.x + ',' + component.y)
110
+ add('grid', offGrid.length === 0 ? 'pass' : 'warn',
111
+ offGrid.length === 0 ? 'every symbol sits on the ' + grid + '-unit grid' : offGrid.length + ' symbol(s) are off the ' + grid + '-unit grid',
112
+ offGrid.slice(0, 20))
113
+
114
+ // 5. labels sitting on top of a symbol body
115
+ const labels = input.labels ?? []
116
+ if (labels.length === 0) {
117
+ add('label-overlap', 'skip', 'no label anchors supplied')
118
+ } else {
119
+ const crowded = labels.filter(label => withBounds.some(component =>
120
+ label.x > component.bounds.left && label.x < component.bounds.right &&
121
+ label.y > component.bounds.bottom && label.y < component.bounds.top))
122
+ .map(label => label.text + ' @' + label.x + ',' + label.y)
123
+ add('label-overlap', crowded.length === 0 ? 'pass' : 'warn',
124
+ crowded.length === 0 ? 'no label anchor lands inside a symbol' : crowded.length + ' label(s) anchored inside a symbol body',
125
+ crowded.slice(0, 20))
126
+ }
127
+
128
+ const counts = { pass: 0, fail: 0, warn: 0, skip: 0 }
129
+ for (const check of checks) counts[check.status] += 1
130
+ return { ok: counts.fail === 0, counts, componentCount: input.components.length, checks }
131
+ }
@@ -0,0 +1,65 @@
1
+ /**
2
+ * Schematic working standard, in one place: the short form goes into the DSH system
3
+ * prompt (guidance.ts) and the long form is served by the `eda_workflow_guide` tool,
4
+ * so the agent can pull the full playbook without bloating every session prompt.
5
+ *
6
+ * DSH has no plugin-facing skill-registration API, so a prompt section plus this
7
+ * on-demand document is the supported way to register a skill with a plugin.
8
+ */
9
+ export const WORKFLOW_SUMMARY = [
10
+ '原理图工作规范(默认全部手工,禁止图省事用自动布局/自动布线):',
11
+ '1) 布局:按 10 单位栅格对齐;信号从左到右、电源在上、地在下;同级器件等间距;不压字、不压标题栏;先看 eda_get_page_info 的安全区再摆位。',
12
+ '2) 连线:只用 eda_draw_wire 走直线/45 度;**跨区域同名网络必须用网络端口(eda_place_net_port)或网络标签**,不能靠在两处画一条叫同名的导线——导线一旦接触引脚,net 会被连通性重算,指定名会被覆盖。',
13
+ '3) 网络标签:先试 eda_place_net_label;若该构建的 createNetLabel 不响应,会自动降级为纯文本标签(无电气语义),此时靠 stub 导线承载网络;批量用 eda_place_net_labels 一次调用,不要串行上百次。',
14
+ '4) 校验:eda_export_netlist 看每个网络的端点是否 ≥2;eda_run_drc 看逐条 DRC 明细。',
15
+ '5) 看图(eda_capture)只在这 4 个时刻:布局完成待布线、布线完成待 DRC、丝印文字放完、坐标与直觉冲突时;不要每改一个图元截一次,看完 eda_capture({cleanup:true}) 删掉临时 PNG。',
16
+ '6) 只有用户明确要求时才 eda_auto_layout / eda_auto_route(需 force:true 且设置里放行)。',
17
+ '7) 引脚信息用 eda_get_pins 一次拿全(含 pinName/pinNumber),不要逐脚调 raw API。',
18
+ ].join('\n')
19
+
20
+ export const WORKFLOW_GUIDE = WORKFLOW_SUMMARY + '\n\n' + [
21
+ '## 布局细则是怎么落地的',
22
+ '',
23
+ '- 坐标单位是 0.01 英寸(MCP 单位);A4 图框 0..1170 / 0..825,标题栏占 right 460..1160 / bottom 10..190,安全区见 eda_get_page_info。',
24
+ '- 一页只放一个功能块;器件按信号流从左到右排,反馈元件放在放大器下方,去耦电容贴近电源脚。',
25
+ '- 器件之间留出至少 20 单位用于走线和标签;网络端口放在模块边缘,左右各一列。',
26
+ '- 摆好一个模块再摆下一个;每次摆完用 eda_get_components 检查没有重叠(包围盒相交)。',
27
+ '',
28
+ '## 网络与标签',
29
+ '',
30
+ '- eda_place_net_port 产生真正的网络端口图元,是跨区域连接的首选。',
31
+ '- eda_place_net_label 优先调 sch_PrimitiveAttribute.createNetLabel;该 API 在 3.2.186 上不响应,探测结论按会话缓存,之后直接降级为文本标签(不再每次付 4 秒超时)。',
32
+ '- 降级后的文本标签没有电气语义:连通性由 eda_place_net_label_at_pin 同时画的 stub 导线承担。',
33
+ '- eda_get_net_labels 会同时列出属性图元(netlabel/netport/netflag)和文本标签,删除用 eda_delete_primitive 的对应 kind。',
34
+ '- 批量放置用 eda_place_net_labels([{pinPrimitiveId, net}, ...]),一次调用完成并逐条回报成败。',
35
+ '',
36
+ '## 导线 net 被覆盖(务必知道)',
37
+ '',
38
+ 'eda_draw_wire 传的 net 只对**悬空导线**有效;导线端点一旦落在引脚/网络上,EDA 会按连通性重算网络名(实测 TESTNET 并入了 LED_A)。所以“同名导线 = 同名网络”不成立,跨区域同名必须用网络标签或网络端口。',
39
+ '',
40
+ '## 什么时候必须看图(eda_capture),什么时候不要',
41
+ '',
42
+ '看图不是"可选加分项",也不是"每步都截"。判据按风险而不是按习惯:',
43
+ '',
44
+ '**必须看图(4 个时刻)**',
45
+ '',
46
+ '1. 布局完成、布线之前:板面最空,最容易一眼看出器件越界、器件互相压住、连接器朝向反了、安装孔压到器件。',
47
+ '2. 布线完成、DRC 之前:看有没有跨网络贴合、天线 stub、绕远的长走线、GND 被打散成孤岛。',
48
+ '3. 丝印/工艺文字放完:看文字有没有压焊盘、被板边裁切、方向反了;原理图同理看标签重叠、图框线穿过器件。',
49
+ '4. 坐标算术与直觉冲突时(工具说在框内但你怀疑越界、audit 说没问题但用户说难看):这时图是唯一裁决者。',
50
+ '',
51
+ '**不要看图**',
52
+ '',
53
+ '- 每放一个图元就截一次:单次约 2.7 s、0.4 MB,还会挤占上下文预算——批量改完再截。',
54
+ '- 只是确认某个返回值/属性:用 eda_get_* 读数据比图准。',
55
+ '- DRC 已给出 primitiveId/坐标的错误:直接定位,不用图。',
56
+ '',
57
+ '**看完立刻删**:eda_capture({cleanup:true})。',
58
+ '',
59
+ '## 校验顺序',
60
+ '',
61
+ '1. eda_get_active_document 确认在原理图页(不是 HOME/PCB)。',
62
+ '2. eda_export_netlist(已解析成对象)检查每个网络 ≥2 端点、无悬空。',
63
+ '3. eda_run_drc 看逐条明细(含 primitiveId 与坐标)。',
64
+ '4. 需要人看效果就导出图片自行识别,不要用自动布局“修”版面。',
65
+ ].join('\n')