Commit graph pcb-retrace/docs/cmms-bridge.js
Author SHA1 Message Date
Stuart
eeb2af1e15 CMMS fork: Phase 6 polish — schema.html light theme + load-time IndexedDB wipe
- cmms-bridge.js: wipeProjectData() clears the project object stores (devices,
  boards, components, images, overlappedImages, nets, schemas, schemaComponents
  — not the KiCad symbol library) and resets studio.js's shared globals before
  the CMMS attachment ZIP is imported. The ZIP is the single source of truth for
  a CMMS board, so restoreDevice()'s merge path (which only de-dupes on a newer
  meta.lastModified, absent on CMMS-created boards) no longer accretes duplicate
  image/net rows across repeated opens. init() also stamps data-theme=light as
  well as =dark. All gated on PCBRETRACE_CONFIG — standalone untouched.
- schema.html: inline head script mirrors ?theme=light|dark (passed by studio.js
  on the iframe src) onto <html> and, for light, injects cmms-schema-light.css.
  Full no-op with no ?theme=.
- cmms-schema-light.css (new): [data-theme="light"]-gated override of
  schema.html's :root palette for the CMMS light theme. Canvas stays dark.
- studio.js: append ?theme=<active> to the schema.html iframe src.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
2026-09-09 22:08:45 +10:00
Stuart
514551c9f2 CMMS fork: AI bridge reads the project from IndexedDB, not the globals
Console showed buildProjectView reporting "4 photos" but NO
buildProjectImages line at all - it was hitting the silent
`!bomImages.length` early return. The studio.js bomData/bomImages
globals get transiently emptied while a project loads / auto-stitches,
and the get-buffer handler's `await` between buildProjectView and
buildProjectImages is enough of a yield to catch that window.

- buildProjectView + buildProjectImages + applyProjectPatch now call
  db.getComponents/getImages/getNets(currentBomId) directly instead of
  reading bomData/bomImages/bomList.
- buildProjectImages traces on entry ("start (board: ...)") and after
  the DB read ("N image record(s)") so an empty result is never silent.
2026-09-03 15:35:16 +10:00
Stuart
8976c52f42 CMMS fork: robust board-photo encoding for the AI bridge
read_board_image reported "no board photos available" - buildProjectImages
was returning [] (both photos failed to encode) with no visibility.

- split encodeImage into downscale() + blobToB64(); on any failure
  (createImageBitmap / canvas.toBlob returning null / EXIF-heavy JPEGs)
  fall back to sending the raw blob base64 if it's under ~3MB.
- coerce a non-Blob img.blob through new Blob([...]).
- per-image [cmms-pcb-bridge] tracing: "downscaled ok" / "downscale
  failed: <msg> - trying raw" / "raw encode also failed" etc.
2026-09-03 15:31:06 +10:00
Stuart
40373493b5 CMMS fork: hand the board photos to the AI bridge
The board photos live in pcb-retrace's IndexedDB, not as CMMS
attachments, so the assistant had no way to see them and burned all its
tool-call rounds looking. cmms-bridge.js now downscales each project
photo (long edge ~1400px, JPEG q0.8 - a phone photo lands under ~300KB)
and sends them alongside the buffer in the cmms-ai-editor-buffer reply
as `images: [{id,name,mime,data_base64}]` (max 6). The CMMS
read_board_image tool surfaces one at a time.
2026-09-03 15:23:17 +10:00
Stuart
04d29dc0ac CMMS fork: pcb AI bridge — robustness for the first-message race
The chat's get-buffer request can arrive before studio.js init() has
built the DB / selected a board (or while an existing project ZIP is
still streaming in), which read back as "the editor is still loading".

- buildProjectView() waits up to 4.5s for db + a board, then still
  returns a valid (possibly empty) project view with a "no board open"
  note rather than null - only a missing db is "still loading".
- init() with no loadUrl (a "+ New board RE") now ensures a starter
  board exists (studio.js only auto-creates one when the whole DB is
  empty, not when the selected device just has no boards).
- reply prefers evt.source, falls back to a DOM query for the chat
  iframe (cad-editor hit an unusable cross-realm Window).
- [cmms-pcb-bridge] console tracing on every step.
2026-09-03 15:09:33 +10:00
Stuart
c15ba490b2 CMMS fork: AI Assistant editor-buffer bridge (pcb kind)
cmms-bridge.js gains a window 'message' listener answering the CMMS AI
assistant's cmms-ai-editor-* protocol (same role as cad-editor's Rust
listener, since studio.html is the top-level page here):

- cmms-ai-editor-get-buffer  -> replies { type:'cmms-ai-editor-buffer',
  buffer: <JSON> } — device/board name, placed components
  ({label,value,desc,x,y}), nets ({name,pins:[...]}), and the imported
  board-photo list.
- cmms-ai-editor-apply (editor:'pcb') -> parses a JSON patch of proposed
  components + nets and merges it into the open project (upsert components
  by label, nets by name; new parts with no coordinate are staggered so
  they're draggable; net pins inherit their component's position if it
  exists). Refreshes the BOM list / map / nets view. Nothing is saved -
  the user checks each against the board photo and clicks Save.

Inert unless window.PCBRETRACE_CONFIG is set.
2026-09-03 14:48:55 +10:00
Stuart
8bf5fd3a9a CMMS fork: embedding hooks (window.PCBRETRACE_CONFIG)
New docs/cmms-bridge.js — inert unless window.PCBRETRACE_CONFIG is present
(only public/vendor/pcb-retrace/edit.php in the CMMS sets it). When set:
- injects "Save" / "Save & close" into the top bar; hides the standalone
  import/export/share buttons.
- on boot, if config.loadUrl is set, pulls the project .pcbretrace.zip
  from the CMMS and restores it silently (reuses processUrlImport).
- Save builds the whole-device ZIP in memory and POSTs it multipart to
  the CMMS attachment endpoint (action/csrf/job_id/id/original_name/file,
  same contract as circuit-edit.php); first save of a new board switches
  to in-place updates.

studio.js:
- exportDeviceZIP() split into buildDeviceZIP(pass, forCmms) -> {blob,
  filename} + a thin exportDeviceZIP() wrapper. forCmms adjusts the
  embedded README + manifest.source.
- importFile/processZIP/processUrlImport take an opts arg; opts.silent
  skips the restore/import confirms and re-throws instead of alert().
- init() awaits window.CMMS.init() at the end when present.

studio.html: loads cmms-bridge.js (defer, after studio.js).

Standalone pcb.etaras.com / local use is unchanged — every new path is
gated on the config object or opts.silent, both absent by default.
Dark mode: config.dark is recorded as data-theme="dark" but studio.html
is light-only today; CMMS-theme-follow is a later task.
2026-09-03 14:16:42 +10:00