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
+51
View File
@@ -0,0 +1,51 @@
import type { WorkplaneShape } from "@/types/sketchforge";
// Client-side contract for the scene bridge. Mirrors the record written by
// apps/web/src/app/api/scene/route.ts. `stagingRevision` is the bridge's OWN
// monotonic counter — distinct from the app's per-project revision.
export type SceneCommandOp = "upsertScene";
export type StagedSceneCommand = {
stagingRevision: number;
op: SceneCommandOp;
projectId: string;
name?: string;
shapes: WorkplaneShape[];
updatedAt: number;
};
// The seam V2 swaps out: today a poller, later a WebSocket `subscribe()`.
// Keeping the route and the MCP tools untouched when that happens.
export interface SceneTransport {
// Returns the staged command if its stagingRevision is newer than `sinceStaging`,
// otherwise null (the route answers 204 when nothing newer is staged).
poll(sinceStaging: number): Promise<StagedSceneCommand | null>;
// subscribe?(onCommand): () => void; // reserved for V2 (live push)
}
export function createPollingSceneTransport(projectId: string): SceneTransport {
return {
async poll(sinceStaging: number) {
const params = new URLSearchParams({ projectId, sinceStaging: String(sinceStaging) });
const response = await fetch(`/api/scene?${params.toString()}`, {
method: "GET",
headers: { "Cache-Control": "no-store" },
});
if (response.status === 204 || !response.ok) {
return null;
}
return (await response.json()) as StagedSceneCommand;
},
};
}
// Pure config resolver so the enable/target logic is testable without a DOM.
// Enabled by `?bridge=1` (any value except 0/false) or the env flag; the target
// project id comes from the existing `project` query param.
export function resolveSceneBridgeTarget(search: string, envEnabled = false): { enabled: boolean; projectId: string | null } {
const params = new URLSearchParams(search);
const flag = params.get("bridge");
const flagEnabled = flag !== null && flag !== "0" && flag !== "false";
const projectId = params.get("project");
return { enabled: (envEnabled || flagEnabled) && Boolean(projectId), projectId: projectId || null };
}