CMMS embedding: AI Assistant editor-buffer bridge
Some checks failed
Test Build / Node v22 (push) Has been cancelled
Test Build / Node v24 (push) Has been cancelled
Test Build / Node v26 (push) Has been cancelled

App.tsx gains a postMessage listener (gated on window.CMMS_SCAD_CONFIG) that
answers 'cmms-ai-editor-get-buffer' with model.source and applies an
'cmms-ai-editor-apply' rewrite via model.source = new_source. Lets the CMMS
AI Assistant read and rewrite the live editor buffer. Standalone use is
untouched.
This commit is contained in:
Stuart 2026-09-03 11:02:14 +10:00
commit 57c0672624

View file

@ -37,6 +37,28 @@ export function App({initialState, statePersister, fs}: {initialState: State, st
};
}, []);
// CMMS AI Assistant editor-buffer bridge (patch, not upstream). Gated on
// window.CMMS_SCAD_CONFIG so standalone use is untouched. The embedded chat
// (injected by public/vendor/openscad-playground/edit.php) postMessages the
// parent window for the current source before each turn and pushes an AI
// rewrite back the same way. No dep array: `model` is recreated every render,
// so the listener is re-bound each time to keep model.source current.
useEffect(() => {
if (!(window as any).CMMS_SCAD_CONFIG) return;
const onMessage = (event: MessageEvent) => {
if (event.origin !== window.location.origin || !event.data || typeof event.data !== 'object') return;
const data = event.data as any;
if (data.type === 'cmms-ai-editor-get-buffer') {
const source = event.source as WindowProxy | null;
source?.postMessage({ type: 'cmms-ai-editor-buffer', buffer: model.source }, window.location.origin);
} else if (data.type === 'cmms-ai-editor-apply' && data.editor === 'scad' && typeof data.new_source === 'string') {
model.source = data.new_source;
}
};
window.addEventListener('message', onMessage);
return () => window.removeEventListener('message', onMessage);
});
const zIndexOfPanelsDependingOnFocus = {
editor: {
editor: 3,