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>
This commit is contained in:
co-authored by
Claude Opus 4.7
parent
42bd05db9c
commit
e8d0b13ff5
@@ -0,0 +1,24 @@
|
||||
import type { McpServer } from '@modelcontextprotocol/sdk/server/mcp.js'
|
||||
import type { SceneBridge } from '../../bridge/scene-bridge'
|
||||
import type { SceneStore } from '../../storage/types'
|
||||
import { registerPhotoToScene } from './photo-to-scene'
|
||||
|
||||
/**
|
||||
* Register the `photo_to_scene` orchestrator tool. Chains the vision
|
||||
* (`analyze_floorplan_image`-equivalent sampling call) → SceneGraph
|
||||
* synthesis → optional `SceneStore.save` → `bridge.setScene` so callers get a
|
||||
* navigable Pascal scene from a single photo upload.
|
||||
*/
|
||||
export function registerPhotoToSceneTool(
|
||||
server: McpServer,
|
||||
bridge: SceneBridge,
|
||||
store: SceneStore,
|
||||
): void {
|
||||
registerPhotoToScene(server, bridge, store)
|
||||
}
|
||||
|
||||
export {
|
||||
photoToSceneInput,
|
||||
photoToSceneOutput,
|
||||
registerPhotoToScene,
|
||||
} from './photo-to-scene'
|
||||
@@ -0,0 +1,194 @@
|
||||
import { describe, expect, test } from 'bun:test'
|
||||
import { Client } from '@modelcontextprotocol/sdk/client/index.js'
|
||||
import { InMemoryTransport } from '@modelcontextprotocol/sdk/inMemory.js'
|
||||
import { McpServer } from '@modelcontextprotocol/sdk/server/mcp.js'
|
||||
import { CreateMessageRequestSchema } from '@modelcontextprotocol/sdk/types.js'
|
||||
import { SceneBridge } from '../../bridge/scene-bridge'
|
||||
import { InMemorySceneStore } from '../scene-lifecycle/test-utils'
|
||||
import { registerPhotoToScene } from './photo-to-scene'
|
||||
|
||||
type Handler = (req: unknown) => unknown | Promise<unknown>
|
||||
|
||||
/**
|
||||
* Build a connected client/server pair for the `photo_to_scene` orchestrator.
|
||||
* Optionally advertises the `sampling` capability on the client and installs
|
||||
* a mock sampling handler that returns a caller-provided reply.
|
||||
*/
|
||||
async function makeWiredPair(opts: { withSampling: boolean; samplingHandler?: Handler }): Promise<{
|
||||
client: Client
|
||||
bridge: SceneBridge
|
||||
store: InMemorySceneStore
|
||||
}> {
|
||||
const bridge = new SceneBridge()
|
||||
bridge.setScene({}, [])
|
||||
const store = new InMemorySceneStore()
|
||||
const server = new McpServer({ name: 'test', version: '0.0.0' })
|
||||
registerPhotoToScene(server, bridge, store)
|
||||
|
||||
const [srvT, cliT] = InMemoryTransport.createLinkedPair()
|
||||
|
||||
const client = new Client(
|
||||
{ name: 'test-client', version: '0.0.0' },
|
||||
{
|
||||
capabilities: opts.withSampling ? { sampling: {} } : {},
|
||||
},
|
||||
)
|
||||
|
||||
if (opts.withSampling && opts.samplingHandler) {
|
||||
const handler = opts.samplingHandler
|
||||
client.setRequestHandler(
|
||||
CreateMessageRequestSchema,
|
||||
async (request) =>
|
||||
// Cast to unknown — tests return arbitrary shapes to exercise
|
||||
// parse/validation paths in the tool handler.
|
||||
(await handler(request)) as never,
|
||||
)
|
||||
}
|
||||
|
||||
await Promise.all([server.connect(srvT), client.connect(cliT)])
|
||||
return { client, bridge, store }
|
||||
}
|
||||
|
||||
const VALID_VISION_JSON = {
|
||||
walls: [
|
||||
{ start: [0, 0], end: [5, 0], thickness: 0.2 },
|
||||
{ start: [5, 0], end: [5, 4] },
|
||||
{ start: [5, 4], end: [0, 4] },
|
||||
{ start: [0, 4], end: [0, 0] },
|
||||
],
|
||||
rooms: [
|
||||
{
|
||||
name: 'Living Room',
|
||||
polygon: [
|
||||
[0, 0],
|
||||
[5, 0],
|
||||
[5, 4],
|
||||
[0, 4],
|
||||
],
|
||||
approximateAreaSqM: 20,
|
||||
},
|
||||
],
|
||||
approximateDimensions: { widthM: 5, depthM: 4 },
|
||||
confidence: 0.82,
|
||||
}
|
||||
|
||||
const VALID_REPLY = {
|
||||
model: 'mock-model',
|
||||
role: 'assistant',
|
||||
content: {
|
||||
type: 'text',
|
||||
text: JSON.stringify(VALID_VISION_JSON),
|
||||
},
|
||||
}
|
||||
|
||||
describe('photo_to_scene', () => {
|
||||
test('happy path: vision reply → walls + rooms + scene in bridge + saved', async () => {
|
||||
const { client, bridge, store } = await makeWiredPair({
|
||||
withSampling: true,
|
||||
samplingHandler: () => VALID_REPLY,
|
||||
})
|
||||
const result = await client.callTool({
|
||||
name: 'photo_to_scene',
|
||||
arguments: {
|
||||
image: 'aGVsbG8=',
|
||||
scaleHint: '1 cm = 1 m',
|
||||
name: 'Test Scene',
|
||||
},
|
||||
})
|
||||
expect(result.isError).toBeFalsy()
|
||||
const structured = result.structuredContent as {
|
||||
sceneId?: string
|
||||
url?: string
|
||||
walls: number
|
||||
rooms: number
|
||||
confidence: number
|
||||
}
|
||||
expect(structured.walls).toBe(4)
|
||||
expect(structured.rooms).toBe(1)
|
||||
expect(structured.confidence).toBe(0.82)
|
||||
expect(typeof structured.sceneId).toBe('string')
|
||||
expect(structured.url).toBe(`/scene/${structured.sceneId}`)
|
||||
|
||||
// Bridge was swapped.
|
||||
const rootIds = bridge.getRootNodeIds()
|
||||
expect(rootIds.length).toBe(1)
|
||||
const rootId = rootIds[0]!
|
||||
const root = bridge.getNode(rootId)
|
||||
expect(root?.type).toBe('site')
|
||||
|
||||
// Walls and zones exist in the flat dict.
|
||||
const allNodes = Object.values(bridge.getNodes())
|
||||
const walls = allNodes.filter((n) => n.type === 'wall')
|
||||
const zones = allNodes.filter((n) => n.type === 'zone')
|
||||
expect(walls.length).toBe(4)
|
||||
expect(zones.length).toBe(1)
|
||||
|
||||
// Scene was persisted in the store.
|
||||
const saved = await store.load(structured.sceneId!)
|
||||
expect(saved).not.toBeNull()
|
||||
expect(saved?.name).toBe('Test Scene')
|
||||
})
|
||||
|
||||
test('sampling unavailable → sampling_unavailable error', async () => {
|
||||
const { client } = await makeWiredPair({ withSampling: false })
|
||||
const result = await client.callTool({
|
||||
name: 'photo_to_scene',
|
||||
arguments: { image: 'aGVsbG8=' },
|
||||
})
|
||||
expect(result.isError).toBe(true)
|
||||
const text = (result.content as Array<{ type: string; text: string }>)[0]!.text
|
||||
expect(text).toContain('sampling_unavailable')
|
||||
})
|
||||
|
||||
test('invalid JSON reply → sampling_response_unparseable', async () => {
|
||||
const { client } = await makeWiredPair({
|
||||
withSampling: true,
|
||||
samplingHandler: () => ({
|
||||
model: 'mock-model',
|
||||
role: 'assistant',
|
||||
content: { type: 'text', text: 'not json at all' },
|
||||
}),
|
||||
})
|
||||
const result = await client.callTool({
|
||||
name: 'photo_to_scene',
|
||||
arguments: { image: 'aGVsbG8=' },
|
||||
})
|
||||
expect(result.isError).toBe(true)
|
||||
const text = (result.content as Array<{ type: string; text: string }>)[0]!.text
|
||||
expect(text).toContain('sampling_response_unparseable')
|
||||
})
|
||||
|
||||
test('save=false → returns graph inline, no sceneId', async () => {
|
||||
const { client, store } = await makeWiredPair({
|
||||
withSampling: true,
|
||||
samplingHandler: () => VALID_REPLY,
|
||||
})
|
||||
const result = await client.callTool({
|
||||
name: 'photo_to_scene',
|
||||
arguments: {
|
||||
image: 'aGVsbG8=',
|
||||
save: false,
|
||||
},
|
||||
})
|
||||
expect(result.isError).toBeFalsy()
|
||||
const structured = result.structuredContent as {
|
||||
sceneId?: string
|
||||
url?: string
|
||||
walls: number
|
||||
rooms: number
|
||||
confidence: number
|
||||
graph?: { nodes: Record<string, unknown>; rootNodeIds: string[] }
|
||||
}
|
||||
expect(structured.sceneId).toBeUndefined()
|
||||
expect(structured.url).toBeUndefined()
|
||||
expect(structured.graph).toBeDefined()
|
||||
expect(Array.isArray(structured.graph?.rootNodeIds)).toBe(true)
|
||||
expect(structured.graph?.rootNodeIds.length).toBe(1)
|
||||
expect(structured.walls).toBe(4)
|
||||
expect(structured.rooms).toBe(1)
|
||||
|
||||
// Nothing persisted.
|
||||
const list = await store.list()
|
||||
expect(list.length).toBe(0)
|
||||
})
|
||||
})
|
||||
@@ -0,0 +1,427 @@
|
||||
import type { McpServer } from '@modelcontextprotocol/sdk/server/mcp.js'
|
||||
import { ErrorCode, McpError } from '@modelcontextprotocol/sdk/types.js'
|
||||
import type { SceneGraph } from '@pascal-app/core/clone-scene-graph'
|
||||
import type { AnyNodeId, AnyNode as AnyNodeT } from '@pascal-app/core/schema'
|
||||
import {
|
||||
AnyNode,
|
||||
BuildingNode,
|
||||
LevelNode,
|
||||
SiteNode,
|
||||
WallNode,
|
||||
ZoneNode,
|
||||
} from '@pascal-app/core/schema'
|
||||
import { z } from 'zod'
|
||||
import type { SceneBridge } from '../../bridge/scene-bridge'
|
||||
import type { SceneStore } from '../../storage/types'
|
||||
|
||||
/**
|
||||
* Input shape for the `photo_to_scene` orchestrator. `image` matches the
|
||||
* contract documented on `analyze_floorplan_image` — base64 or http(s) URL.
|
||||
*/
|
||||
export const photoToSceneInput = {
|
||||
image: z.string().describe('Base64 or https URL of the floor-plan photo'),
|
||||
scaleHint: z.string().optional().describe('e.g. "1 cm = 1 m" or "approx 80 m²"'),
|
||||
name: z.string().default('Scene from photo'),
|
||||
save: z.boolean().default(true),
|
||||
defaultWallThickness: z.number().default(0.2),
|
||||
defaultWallHeight: z.number().default(2.6),
|
||||
}
|
||||
|
||||
export const photoToSceneOutput = {
|
||||
sceneId: z.string().optional(),
|
||||
url: z.string().optional(),
|
||||
walls: z.number(),
|
||||
rooms: z.number(),
|
||||
confidence: z.number(),
|
||||
notes: z.string().optional(),
|
||||
graph: z.any().optional(),
|
||||
}
|
||||
|
||||
/**
|
||||
* Shape of the vision JSON we consume. Kept in-sync with
|
||||
* `analyze_floorplan_image`'s output schema (walls / rooms /
|
||||
* approximateDimensions / confidence).
|
||||
*/
|
||||
const VisionResponseSchema = z.object({
|
||||
walls: z.array(
|
||||
z.object({
|
||||
start: z.tuple([z.number(), z.number()]),
|
||||
end: z.tuple([z.number(), z.number()]),
|
||||
thickness: z.number().optional(),
|
||||
}),
|
||||
),
|
||||
rooms: z.array(
|
||||
z.object({
|
||||
name: z.string(),
|
||||
polygon: z.array(z.tuple([z.number(), z.number()])),
|
||||
approximateAreaSqM: z.number().optional(),
|
||||
}),
|
||||
),
|
||||
approximateDimensions: z.object({
|
||||
widthM: z.number(),
|
||||
depthM: z.number(),
|
||||
}),
|
||||
confidence: z.number().min(0).max(1),
|
||||
})
|
||||
|
||||
type VisionResponse = z.infer<typeof VisionResponseSchema>
|
||||
|
||||
/**
|
||||
* System prompt mirrors `analyze_floorplan_image` — the contract between
|
||||
* orchestrator and host is identical, so we keep the prompt verbatim to
|
||||
* guarantee wire-compatible responses.
|
||||
*/
|
||||
const SYSTEM_PROMPT = `You are a vision assistant that extracts structured floor-plan data from an image.
|
||||
Your ONLY job: return a JSON object that exactly matches this schema — no prose, no markdown fences.
|
||||
|
||||
{
|
||||
"walls": [{ "start": [x, z], "end": [x, z], "thickness": number? }, ...],
|
||||
"rooms": [{ "name": string, "polygon": [[x,z], ...], "approximateAreaSqM": number? }, ...],
|
||||
"approximateDimensions": { "widthM": number, "depthM": number },
|
||||
"confidence": number 0..1
|
||||
}
|
||||
|
||||
Coordinates are in metres. Origin can be the floor plan's centre or bottom-left — be consistent.
|
||||
If the image is unclear, lower the confidence score but still produce your best attempt.
|
||||
DO NOT wrap the JSON in markdown. DO NOT explain. Just output the raw JSON.`
|
||||
|
||||
const DATA_URI_RE = /^data:(image\/[a-z0-9.+-]+);base64,(.+)$/i
|
||||
|
||||
type ImageBlock = {
|
||||
type: 'image'
|
||||
data: string
|
||||
mimeType: string
|
||||
}
|
||||
|
||||
/**
|
||||
* Resolve the `image` input into a sampling-ready image block. Follows the
|
||||
* same fetch/data-uri/raw-base64 rules as the vision tool so the user gets
|
||||
* consistent behaviour whether they call `photo_to_scene` or
|
||||
* `analyze_floorplan_image` directly.
|
||||
*/
|
||||
async function resolveImageBlock(image: string): Promise<ImageBlock> {
|
||||
if (/^https?:\/\//i.test(image)) {
|
||||
const res = await fetch(image)
|
||||
if (!res.ok) {
|
||||
throw new McpError(
|
||||
ErrorCode.InvalidParams,
|
||||
`failed to fetch image: ${res.status} ${res.statusText}`,
|
||||
{ url: image, status: res.status },
|
||||
)
|
||||
}
|
||||
const buf = Buffer.from(await res.arrayBuffer())
|
||||
const data = buf.toString('base64')
|
||||
const mimeType = res.headers.get('content-type') ?? 'image/jpeg'
|
||||
return { type: 'image', data, mimeType }
|
||||
}
|
||||
|
||||
const dataUriMatch = image.match(DATA_URI_RE)
|
||||
if (dataUriMatch) {
|
||||
return {
|
||||
type: 'image',
|
||||
mimeType: dataUriMatch[1]!,
|
||||
data: dataUriMatch[2]!,
|
||||
}
|
||||
}
|
||||
|
||||
return { type: 'image', mimeType: 'image/jpeg', data: image }
|
||||
}
|
||||
|
||||
/** Collect all text content blocks returned by the sampling host into one string. */
|
||||
function extractText(
|
||||
content:
|
||||
| { type: 'text'; text: string }
|
||||
| { type: 'image' | 'audio'; data: string; mimeType: string }
|
||||
| Array<
|
||||
| { type: 'text'; text: string }
|
||||
| { type: 'image' | 'audio'; data: string; mimeType: string }
|
||||
| { type: string; [k: string]: unknown }
|
||||
>,
|
||||
): string {
|
||||
const blocks = Array.isArray(content) ? content : [content]
|
||||
const texts: string[] = []
|
||||
for (const block of blocks) {
|
||||
if (block && typeof block === 'object' && (block as { type?: string }).type === 'text') {
|
||||
const t = (block as { text?: unknown }).text
|
||||
if (typeof t === 'string') texts.push(t)
|
||||
}
|
||||
}
|
||||
return texts.join('\n').trim()
|
||||
}
|
||||
|
||||
/**
|
||||
* Call the host's sampling capability to analyse a floor-plan photo. Throws
|
||||
* `sampling_unavailable` when the host has not advertised the capability and
|
||||
* `sampling_response_unparseable` / `sampling_response_invalid` when the
|
||||
* reply cannot be mapped onto `VisionResponseSchema`.
|
||||
*/
|
||||
async function callVisionSampling(
|
||||
server: McpServer,
|
||||
image: string,
|
||||
scaleHint: string | undefined,
|
||||
): Promise<VisionResponse> {
|
||||
const caps = server.server.getClientCapabilities()
|
||||
if (!caps?.sampling) {
|
||||
throw new McpError(ErrorCode.InvalidRequest, 'sampling_unavailable')
|
||||
}
|
||||
|
||||
const imageBlock = await resolveImageBlock(image)
|
||||
const instruction = scaleHint
|
||||
? `Analyze this floor plan. Scale hint: ${scaleHint}. Return ONLY the JSON described by the system prompt.`
|
||||
: 'Analyze this floor plan. Return ONLY the JSON described by the system prompt.'
|
||||
|
||||
const response = await server.server.createMessage({
|
||||
systemPrompt: SYSTEM_PROMPT,
|
||||
temperature: 0,
|
||||
maxTokens: 2000,
|
||||
messages: [
|
||||
{
|
||||
role: 'user',
|
||||
content: [imageBlock, { type: 'text', text: instruction }],
|
||||
},
|
||||
],
|
||||
})
|
||||
|
||||
const text = extractText(response.content as Parameters<typeof extractText>[0])
|
||||
if (!text) {
|
||||
throw new McpError(ErrorCode.InternalError, 'sampling_response_unparseable', {
|
||||
reason: 'no text content returned by host',
|
||||
})
|
||||
}
|
||||
|
||||
let parsed: unknown
|
||||
try {
|
||||
parsed = JSON.parse(text)
|
||||
} catch (err) {
|
||||
throw new McpError(ErrorCode.InternalError, 'sampling_response_unparseable', {
|
||||
raw: text,
|
||||
reason: err instanceof Error ? err.message : String(err),
|
||||
})
|
||||
}
|
||||
|
||||
const validation = VisionResponseSchema.safeParse(parsed)
|
||||
if (!validation.success) {
|
||||
throw new McpError(ErrorCode.InternalError, 'sampling_response_invalid', {
|
||||
raw: text,
|
||||
errors: validation.error.issues,
|
||||
})
|
||||
}
|
||||
|
||||
return validation.data
|
||||
}
|
||||
|
||||
type BuildResult = {
|
||||
nodes: Record<AnyNodeId, AnyNodeT>
|
||||
rootNodeIds: AnyNodeId[]
|
||||
walls: number
|
||||
rooms: number
|
||||
warnings: string[]
|
||||
levelId: AnyNodeId
|
||||
}
|
||||
|
||||
/**
|
||||
* Build a SceneGraph (flat `nodes` dict + `rootNodeIds`) from the vision
|
||||
* response. Uses the schema factories for every node so IDs, defaults, and
|
||||
* parent linkage match what the core store would produce. Each node is
|
||||
* revalidated via `AnyNode.safeParse`; failures are dropped with a warning.
|
||||
*/
|
||||
function buildSceneGraphFromVision(
|
||||
vision: VisionResponse,
|
||||
defaultWallThickness: number,
|
||||
defaultWallHeight: number,
|
||||
): BuildResult {
|
||||
const warnings: string[] = []
|
||||
|
||||
// Build the skeleton: site → building → level.
|
||||
const building = BuildingNode.parse({})
|
||||
const level = LevelNode.parse({ level: 0 })
|
||||
const site = SiteNode.parse({ children: [building] })
|
||||
|
||||
// Link parent ids so downstream traversal works.
|
||||
const siteId = site.id as AnyNodeId
|
||||
const buildingId = building.id as AnyNodeId
|
||||
const levelId = level.id as AnyNodeId
|
||||
const linkedBuilding: AnyNodeT = {
|
||||
...(building as AnyNodeT),
|
||||
parentId: siteId,
|
||||
}
|
||||
const linkedLevel: AnyNodeT = {
|
||||
...(level as AnyNodeT),
|
||||
parentId: buildingId,
|
||||
}
|
||||
|
||||
// BuildingNode children stores level ids (string[]).
|
||||
;(linkedBuilding as BuildingNode).children = [levelId as BuildingNode['children'][number]]
|
||||
|
||||
// Collect level children (ids of walls/zones we create below).
|
||||
const levelChildren: string[] = []
|
||||
|
||||
const nodes: Record<AnyNodeId, AnyNodeT> = {}
|
||||
|
||||
// Validate + add site, building, level in that order.
|
||||
const siteValidated = AnyNode.safeParse(site)
|
||||
if (!siteValidated.success) {
|
||||
warnings.push(`site node failed schema validation: ${siteValidated.error.message}`)
|
||||
}
|
||||
nodes[siteId] = (siteValidated.success ? siteValidated.data : site) as AnyNodeT
|
||||
|
||||
const buildingValidated = AnyNode.safeParse(linkedBuilding)
|
||||
if (!buildingValidated.success) {
|
||||
warnings.push(`building node failed schema validation: ${buildingValidated.error.message}`)
|
||||
}
|
||||
nodes[buildingId] = (
|
||||
buildingValidated.success ? buildingValidated.data : linkedBuilding
|
||||
) as AnyNodeT
|
||||
|
||||
// Walls.
|
||||
let wallsAdded = 0
|
||||
for (let i = 0; i < vision.walls.length; i++) {
|
||||
const w = vision.walls[i]!
|
||||
try {
|
||||
const wall = WallNode.parse({
|
||||
start: w.start,
|
||||
end: w.end,
|
||||
thickness: w.thickness ?? defaultWallThickness,
|
||||
height: defaultWallHeight,
|
||||
})
|
||||
const linkedWall: AnyNodeT = {
|
||||
...(wall as AnyNodeT),
|
||||
parentId: levelId,
|
||||
}
|
||||
const validated = AnyNode.safeParse(linkedWall)
|
||||
if (!validated.success) {
|
||||
warnings.push(`wall[${i}] dropped: ${validated.error.message}`)
|
||||
continue
|
||||
}
|
||||
nodes[wall.id as AnyNodeId] = validated.data as AnyNodeT
|
||||
levelChildren.push(wall.id)
|
||||
wallsAdded++
|
||||
} catch (err) {
|
||||
const msg = err instanceof Error ? err.message : String(err)
|
||||
warnings.push(`wall[${i}] dropped: ${msg}`)
|
||||
}
|
||||
}
|
||||
|
||||
// Rooms → zones.
|
||||
let roomsAdded = 0
|
||||
for (let i = 0; i < vision.rooms.length; i++) {
|
||||
const r = vision.rooms[i]!
|
||||
try {
|
||||
const zone = ZoneNode.parse({
|
||||
name: r.name,
|
||||
polygon: r.polygon,
|
||||
})
|
||||
const linkedZone: AnyNodeT = {
|
||||
...(zone as AnyNodeT),
|
||||
parentId: levelId,
|
||||
}
|
||||
const validated = AnyNode.safeParse(linkedZone)
|
||||
if (!validated.success) {
|
||||
warnings.push(`room[${i}] dropped: ${validated.error.message}`)
|
||||
continue
|
||||
}
|
||||
nodes[zone.id as AnyNodeId] = validated.data as AnyNodeT
|
||||
levelChildren.push(zone.id)
|
||||
roomsAdded++
|
||||
} catch (err) {
|
||||
const msg = err instanceof Error ? err.message : String(err)
|
||||
warnings.push(`room[${i}] dropped: ${msg}`)
|
||||
}
|
||||
}
|
||||
// Finalise the level's children array now that walls/zones are in the dict.
|
||||
;(linkedLevel as LevelNode).children = levelChildren as LevelNode['children']
|
||||
const levelValidated = AnyNode.safeParse(linkedLevel)
|
||||
if (!levelValidated.success) {
|
||||
warnings.push(`level node failed schema validation: ${levelValidated.error.message}`)
|
||||
}
|
||||
nodes[levelId] = (levelValidated.success ? levelValidated.data : linkedLevel) as AnyNodeT
|
||||
|
||||
return {
|
||||
nodes,
|
||||
rootNodeIds: [siteId],
|
||||
walls: wallsAdded,
|
||||
rooms: roomsAdded,
|
||||
warnings,
|
||||
levelId,
|
||||
}
|
||||
}
|
||||
|
||||
export function registerPhotoToScene(
|
||||
server: McpServer,
|
||||
bridge: SceneBridge,
|
||||
store: SceneStore,
|
||||
): void {
|
||||
server.registerTool(
|
||||
'photo_to_scene',
|
||||
{
|
||||
title: 'Photo to Pascal scene',
|
||||
description:
|
||||
'Orchestrator: analyse a floor-plan photo via MCP sampling, translate the structured vision result into a Pascal SceneGraph (site → building → level with walls and zones), optionally save it, and swap the bridge to the new scene. Requires host support for sampling.',
|
||||
inputSchema: photoToSceneInput,
|
||||
outputSchema: photoToSceneOutput,
|
||||
},
|
||||
async ({ image, scaleHint, name, save, defaultWallThickness, defaultWallHeight }) => {
|
||||
// 1. Vision.
|
||||
const vision = await callVisionSampling(server, image, scaleHint)
|
||||
|
||||
// 2. Build scene graph.
|
||||
const built = buildSceneGraphFromVision(vision, defaultWallThickness, defaultWallHeight)
|
||||
|
||||
const graph: SceneGraph = {
|
||||
nodes: built.nodes as SceneGraph['nodes'],
|
||||
rootNodeIds: built.rootNodeIds as SceneGraph['rootNodeIds'],
|
||||
collections: {} as SceneGraph['collections'],
|
||||
}
|
||||
|
||||
// 5. Swap the bridge to the new scene so follow-up MCP calls operate on it.
|
||||
bridge.setScene(graph.nodes, graph.rootNodeIds)
|
||||
|
||||
const notes = built.warnings.length > 0 ? built.warnings.join('; ') : undefined
|
||||
|
||||
// 4. Save or return inline.
|
||||
if (save) {
|
||||
const meta = await store.save({
|
||||
name,
|
||||
graph,
|
||||
})
|
||||
const payload: {
|
||||
sceneId: string
|
||||
url: string
|
||||
walls: number
|
||||
rooms: number
|
||||
confidence: number
|
||||
notes?: string
|
||||
} = {
|
||||
sceneId: meta.id,
|
||||
url: `/scene/${meta.id}`,
|
||||
walls: built.walls,
|
||||
rooms: built.rooms,
|
||||
confidence: vision.confidence,
|
||||
}
|
||||
if (notes) payload.notes = notes
|
||||
return {
|
||||
content: [{ type: 'text' as const, text: JSON.stringify(payload) }],
|
||||
structuredContent: payload,
|
||||
}
|
||||
}
|
||||
|
||||
const payload: {
|
||||
walls: number
|
||||
rooms: number
|
||||
confidence: number
|
||||
notes?: string
|
||||
graph: SceneGraph
|
||||
} = {
|
||||
walls: built.walls,
|
||||
rooms: built.rooms,
|
||||
confidence: vision.confidence,
|
||||
graph,
|
||||
}
|
||||
if (notes) payload.notes = notes
|
||||
return {
|
||||
content: [{ type: 'text' as const, text: JSON.stringify(payload) }],
|
||||
structuredContent: payload,
|
||||
}
|
||||
},
|
||||
)
|
||||
}
|
||||
Reference in New Issue
Block a user