Add AI scene bridge (V1): MCP -> /api/scene -> editor

Additive fork feature letting an MCP server (driven by Claude) push parametric
WorkplaneShape[] scenes into the running SketchForge editor. The Node side never
writes browser storage; it stages a command file that an in-page poller applies
through the editor's existing updateProjectShapes() save path.

- Phase 1: apps/web/src/app/api/scene/route.ts — stages/serves commands behind the
  existing same-origin guard, with its own monotonic stagingRevision (separate from
  the app's project revision).
- Phase 2: apps/web/src/lib/sceneBridge.ts (SceneTransport seam + polling transport)
  and a useSceneBridge poller hook in page.tsx (gated on ?bridge=1), which creates
  unknown projects and hydrates via updateProjectShapes; defers while dragging.
- Phase 3: apps/mcp/ MCP server — set_scene/get_scene/add_shapes/list_kinds. zod
  validates the ShapeKind union enum + base numerics, then reuses the app's own
  sceneShape/canonicalizeShape for defaults/canonicalisation (no hand-authored ranges).
- Phase 4: docs/scene-bridge/README.md plus vitest cases for the route handler and
  the schema validator.

Staged scenes live under .codex/pending-scenes/ (gitignored). MCP registered via
.mcp.json. npm run test: 29 passing.

Co-Authored-By: Claude Opus 4.8 <[email protected]>
This commit is contained in:
BloodSteyn
2026-06-26 13:34:44 +02:00
co-authored by Claude Opus 4.8
parent d99fc1e650
commit dee2765538
19 changed files with 2917 additions and 10 deletions
+88
View File
@@ -6,6 +6,7 @@ import { SketchForgeEditor, importedShapeFromStl } from "@/components/SketchForg
import { createLocalId } from "@/lib/localIds";
import { importExtensionSupported } from "@/lib/stlImport";
import { DEFAULT_SNAP_GRID, DEFAULT_WORKPLANE_WORKSPACE, normalizeSnapGrid, normalizeWorkspaceSettings } from "@/lib/workplaneSettings";
import { createPollingSceneTransport, resolveSceneBridgeTarget, type StagedSceneCommand } from "@/lib/sceneBridge";
import type { GridSize, WorkplaneShape, WorkplaneWorkspaceSettings } from "@/types/sketchforge";
type AppView = "dashboard" | "editor";
@@ -238,6 +239,62 @@ function projectNameFromFileName(fileName: string) {
return fileName.replace(/\.[^.]+$/, "").trim() || "Imported STL design";
}
const SCENE_BRIDGE_POLL_MS = 1000;
// Scene bridge poller. Lives in Home (not a components/ module) because applying a
// staged command needs the project lifecycle + updateProjectShapes that live here.
// V2 will swap createPollingSceneTransport for a WebSocket transport without touching
// this hook's apply path. Defers while a pointer is down so an incoming push never
// fights an in-progress drag.
function useSceneBridge({ ready, apply }: { ready: boolean; apply: (command: StagedSceneCommand) => void }) {
const applyRef = useRef(apply);
applyRef.current = apply;
const lastAppliedRef = useRef(0);
const pointerDownRef = useRef(false);
useEffect(() => {
if (!ready || typeof window === "undefined") return;
const envEnabled = process.env.NEXT_PUBLIC_SCENE_BRIDGE === "true";
const { enabled, projectId } = resolveSceneBridgeTarget(window.location.search, envEnabled);
if (!enabled || !projectId) return;
const transport = createPollingSceneTransport(projectId);
let active = true;
const onPointerDown = () => {
pointerDownRef.current = true;
};
const onPointerUp = () => {
pointerDownRef.current = false;
};
window.addEventListener("pointerdown", onPointerDown, true);
window.addEventListener("pointerup", onPointerUp, true);
window.addEventListener("pointercancel", onPointerUp, true);
const tick = async () => {
if (!active || pointerDownRef.current) return;
try {
const command = await transport.poll(lastAppliedRef.current);
if (!active || !command || command.stagingRevision <= lastAppliedRef.current) return;
lastAppliedRef.current = command.stagingRevision;
applyRef.current(command);
} catch {
// Transient (server reload, file mid-write). The command stays staged; retry next tick.
}
};
void tick();
const interval = window.setInterval(() => void tick(), SCENE_BRIDGE_POLL_MS);
return () => {
active = false;
window.clearInterval(interval);
window.removeEventListener("pointerdown", onPointerDown, true);
window.removeEventListener("pointerup", onPointerUp, true);
window.removeEventListener("pointercancel", onPointerUp, true);
};
}, [ready]);
}
export default function Home() {
const [mounted, setMounted] = useState(false);
const [view, setView] = useState<AppView>("dashboard");
@@ -498,6 +555,37 @@ export default function Home() {
);
}, []);
const applyBridgeCommand = useCallback(
(command: StagedSceneCommand) => {
if (command.op !== "upsertScene") return;
const projectId = command.projectId;
const name = command.name?.trim();
// Create the localStorage metadata entry if the id is unknown — the editor needs
// BOTH a project entry and a shapes record to open. Never overwrite an existing name.
setProjects((current) => {
if (current.some((project) => project.id === projectId)) return current;
const created: DashboardProject = { ...newProject(name || "AI scene", current.length), id: projectId };
return [created, ...current];
});
// Existing save path: writes IndexedDB + localStorage + a winning project revision.
// The editor's rehydration effect (keyed on initialShapes/projectRevision) re-renders.
updateProjectShapes({ projectId, shapes: command.shapes });
setActiveProjectId(projectId);
setEditorStarted(true);
setView("editor");
if (typeof window !== "undefined") {
const params = new URLSearchParams(window.location.search);
params.set("editor", "1");
params.set("project", projectId);
if (!params.has("bridge")) params.set("bridge", "1");
window.history.replaceState(null, "", `/?${params.toString()}`);
}
},
[updateProjectShapes],
);
useSceneBridge({ ready: mounted, apply: applyBridgeCommand });
const createAndOpenProject = (name?: string) => {
const project = newProject(name ?? `Untitled design ${projects.length + 1}`, projects.length);
setProjectShapesById((current) => ({