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,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)
|
||||
})
|
||||
})
|
||||
Reference in New Issue
Block a user