Files
editor/packages/mcp/src/templates/empty-studio.ts
T
Adrian PerezandClaude Opus 4.7 e8d0b13ff5 feat(mcp,editor): Option A+B storage + 10 agent deliverables (Phase 7)
Ships the combined filesystem/Supabase storage adapter + MCP scene
lifecycle tools + Next.js API routes + editor /scene/[id] route, so
an MCP save is directly openable at /scene/<id> without any
injection hack. End-to-end verified: 10/10 e2e steps pass.

Storage (A1/A2/A3):
- SceneStore interface + error classes + slug helpers
- FilesystemSceneStore at $PASCAL_DATA_DIR (defaults XDG/~/.pascal)
  with atomic writes, .index sidecar, optimistic locking
- SupabaseSceneStore with scenes + scene_revisions tables, RLS
  migration SQL, mock-backed unit tests
- createSceneStore(env) auto-selects based on SUPABASE_URL +
  SUPABASE_SERVICE_ROLE_KEY

MCP tools (A4, A8, A9, A10):
- save_scene / load_scene / list_scenes / delete_scene / rename_scene
- list_templates / create_from_template (3 seed templates:
  empty-studio, two-bedroom, garden-house)
- generate_variants (7 mutation kinds, seeded RNG, save=true|false)
- photo_to_scene (vision sampling → scene graph → save)

Editor (A5, A6):
- /api/scenes + /api/scenes/[id] with RFC 7232 If-Match locking
- /scene/[id] and /scenes route pages with save button, SceneLoader
- Removed the window.__pascalScene dev injection hack

Security + UX edges (A7, A8):
- AssetUrl Zod validator: asset:// blob: data:image/ /path https:
  (http://localhost for dev) + PASCAL_ALLOWED_ASSET_ORIGINS env
  allowlist. Hardens scan.url, guide.url, item.asset.src,
  material.texture.url, MaterialMaps.*Map
- Auto-frame camera on empty→non-empty scene transition
  (camera-controls:fit-scene emitter event)

Shared utilities:
- rehydrateSiteChildren() extracted to packages/mcp/src/lib/ and
  used by both create-from-template and generate-variants to work
  around the SiteNode.children-as-objects vs. ids inconsistency
  (CROSS_CUTTING §2)
- Storage + MCP subpath exports added to packages/mcp/package.json
  (CROSS_CUTTING §4)

Tests: 293 pass / 0 fail across 40 files (was 142 pre-Phase-7).
Biome: clean.

Phase-7 e2e script at packages/mcp/test-reports/phase7-e2e.ts:
MCP HTTP + editor Next.js both point at $PASCAL_DATA_DIR =
/tmp/pascal-e2e, save_scene from MCP, GET /api/scenes/<id> from
editor server, /scenes list page renders all saved scenes, scene
page renders SceneLoader, delete_scene works.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-04-18 19:29:28 +02:00

265 lines
6.4 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import type { SceneGraph } from '@pascal-app/core/clone-scene-graph'
import type { AnyNode, AnyNodeId } from '@pascal-app/core/schema'
/**
* 40 m² studio apartment — a single open room with one window, one front door
* and a single "Living/Kitchen" zone. Used as a starting point for small unit
* briefs. Deterministic ids (`site_empty`, `building_empty`, `level_0`, etc.)
* are regenerated by the MCP tool via `cloneSceneGraph` before applying.
*/
// Footprint: 8 m × 5 m = 40 m² (centered at origin).
// Walls traverse the boundary counter-clockwise (right-handed XZ plane).
const W = 4 // half-width
const D = 2.5 // half-depth
type StudioNodes = {
site: AnyNode
building: AnyNode
level: AnyNode
walls: AnyNode[]
zone: AnyNode
door: AnyNode
window: AnyNode
}
function buildNodes(): StudioNodes {
const site: AnyNode = {
object: 'node',
id: 'site_empty' as AnyNodeId,
type: 'site',
parentId: null,
visible: true,
metadata: {},
polygon: {
type: 'polygon',
points: [
[-15, -15],
[15, -15],
[15, 15],
[-15, 15],
],
},
children: ['building_empty' as AnyNodeId],
} as unknown as AnyNode
const building: AnyNode = {
object: 'node',
id: 'building_empty' as AnyNodeId,
type: 'building',
parentId: 'site_empty' as AnyNodeId,
visible: true,
metadata: {},
position: [0, 0, 0],
rotation: [0, 0, 0],
children: ['level_0' as AnyNodeId],
} as unknown as AnyNode
// Walls with deterministic ids; child ids are listed below after doors/windows
// are created, so we fill this array after computing them.
const wallIds = ['wall_n', 'wall_e', 'wall_s', 'wall_w'] as const
// South wall carries the front door; west wall carries the window.
const door: AnyNode = {
object: 'node',
id: 'door_front' as AnyNodeId,
type: 'door',
parentId: 'wall_s' as AnyNodeId,
visible: true,
metadata: {},
wallId: 'wall_s',
position: [0, 1.05, 0],
rotation: [0, 0, 0],
width: 0.9,
height: 2.1,
frameThickness: 0.05,
frameDepth: 0.07,
threshold: true,
thresholdHeight: 0.02,
hingesSide: 'left',
swingDirection: 'inward',
segments: [
{
type: 'panel',
heightRatio: 0.4,
columnRatios: [1],
dividerThickness: 0.03,
panelDepth: 0.01,
panelInset: 0.04,
},
{
type: 'panel',
heightRatio: 0.6,
columnRatios: [1],
dividerThickness: 0.03,
panelDepth: 0.01,
panelInset: 0.04,
},
],
handle: true,
handleHeight: 1.05,
handleSide: 'right',
contentPadding: [0.04, 0.04],
doorCloser: false,
panicBar: false,
panicBarHeight: 1.0,
} as unknown as AnyNode
const windowNode: AnyNode = {
object: 'node',
id: 'window_w' as AnyNodeId,
type: 'window',
parentId: 'wall_w' as AnyNodeId,
visible: true,
metadata: {},
wallId: 'wall_w',
position: [0, 1.2, 0],
rotation: [0, 0, 0],
width: 1.5,
height: 1.2,
frameThickness: 0.05,
frameDepth: 0.07,
columnRatios: [1],
rowRatios: [1],
columnDividerThickness: 0.03,
rowDividerThickness: 0.03,
sill: true,
sillDepth: 0.08,
sillThickness: 0.03,
} as unknown as AnyNode
const wallNorth: AnyNode = {
object: 'node',
id: 'wall_n' as AnyNodeId,
type: 'wall',
parentId: 'level_0' as AnyNodeId,
visible: true,
metadata: {},
children: [],
thickness: 0.1,
height: 2.5,
start: [-W, -D],
end: [W, -D],
frontSide: 'unknown',
backSide: 'unknown',
} as unknown as AnyNode
const wallEast: AnyNode = {
object: 'node',
id: 'wall_e' as AnyNodeId,
type: 'wall',
parentId: 'level_0' as AnyNodeId,
visible: true,
metadata: {},
children: [],
thickness: 0.1,
height: 2.5,
start: [W, -D],
end: [W, D],
frontSide: 'unknown',
backSide: 'unknown',
} as unknown as AnyNode
const wallSouth: AnyNode = {
object: 'node',
id: 'wall_s' as AnyNodeId,
type: 'wall',
parentId: 'level_0' as AnyNodeId,
visible: true,
metadata: {},
children: ['door_front'],
thickness: 0.1,
height: 2.5,
start: [W, D],
end: [-W, D],
frontSide: 'unknown',
backSide: 'unknown',
} as unknown as AnyNode
const wallWest: AnyNode = {
object: 'node',
id: 'wall_w' as AnyNodeId,
type: 'wall',
parentId: 'level_0' as AnyNodeId,
visible: true,
metadata: {},
children: ['window_w'],
thickness: 0.1,
height: 2.5,
start: [-W, D],
end: [-W, -D],
frontSide: 'unknown',
backSide: 'unknown',
} as unknown as AnyNode
const zone: AnyNode = {
object: 'node',
id: 'zone_living' as AnyNodeId,
type: 'zone',
parentId: 'level_0' as AnyNodeId,
visible: true,
metadata: {},
name: 'Living / Kitchen',
color: '#60a5fa',
polygon: [
[-W, -D],
[W, -D],
[W, D],
[-W, D],
],
} as unknown as AnyNode
const level: AnyNode = {
object: 'node',
id: 'level_0' as AnyNodeId,
type: 'level',
parentId: 'building_empty' as AnyNodeId,
visible: true,
metadata: {},
level: 0,
children: [...wallIds, 'zone_living'] as AnyNodeId[],
} as unknown as AnyNode
return {
site,
building,
level,
walls: [wallNorth, wallEast, wallSouth, wallWest],
zone,
door,
window: windowNode,
}
}
function buildTemplate(): SceneGraph {
const n = buildNodes()
const nodes: Record<AnyNodeId, AnyNode> = {}
for (const node of [n.site, n.building, n.level, ...n.walls, n.zone, n.door, n.window]) {
nodes[node.id as AnyNodeId] = node
}
// SiteNode.children is a discriminatedUnion of BuildingNode/ItemNode objects
// (not string ids) — so the site must embed the full building node. The
// rest of the tree uses string ids per the BaseNode/LevelNode/WallNode
// schemas. We mutate the flat-dict copy of the site here so the nested
// representation round-trips through AnyNode.safeParse.
const siteInDict = nodes['site_empty' as AnyNodeId] as unknown as {
children: unknown[]
}
siteInDict.children = [nodes['building_empty' as AnyNodeId]]
return {
nodes,
rootNodeIds: ['site_empty'] as AnyNodeId[],
}
}
export const template: SceneGraph = buildTemplate()
export const metadata = {
id: 'empty-studio',
name: 'Empty studio',
description:
'40 m² single-room studio apartment: 4 walls, 1 living/kitchen zone, 1 window, 1 front door.',
} as const