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,146 @@
|
||||
import {
|
||||
type SceneListOptions,
|
||||
type SceneMeta,
|
||||
type SceneMutateOptions,
|
||||
SceneNotFoundError,
|
||||
type SceneSaveOptions,
|
||||
type SceneStore,
|
||||
SceneVersionConflictError,
|
||||
type SceneWithGraph,
|
||||
} from '../../storage/types'
|
||||
|
||||
export type StoredTextContent = { type: string; text: string }
|
||||
|
||||
export function parseToolText(content: StoredTextContent[]): Record<string, unknown> {
|
||||
return JSON.parse(content[0]!.text) as Record<string, unknown>
|
||||
}
|
||||
|
||||
/**
|
||||
* In-memory `SceneStore` for tests. Backed by a plain `Map` keyed by id.
|
||||
* Implements the full interface including optimistic concurrency via
|
||||
* `expectedVersion`.
|
||||
*/
|
||||
export class InMemorySceneStore implements SceneStore {
|
||||
readonly backend = 'filesystem' as const
|
||||
private readonly data = new Map<string, SceneWithGraph>()
|
||||
private idCounter = 0
|
||||
|
||||
async save(opts: SceneSaveOptions): Promise<SceneMeta> {
|
||||
const existing = opts.id ? this.data.get(opts.id) : undefined
|
||||
if (existing) {
|
||||
if (opts.expectedVersion !== undefined && existing.version !== opts.expectedVersion) {
|
||||
throw new SceneVersionConflictError(
|
||||
`Expected version ${opts.expectedVersion}, have ${existing.version}`,
|
||||
)
|
||||
}
|
||||
const now = new Date().toISOString()
|
||||
const nodeCount = Object.keys(opts.graph.nodes ?? {}).length
|
||||
const serialized = JSON.stringify(opts.graph)
|
||||
const updated: SceneWithGraph = {
|
||||
id: existing.id,
|
||||
name: opts.name,
|
||||
projectId: opts.projectId ?? existing.projectId,
|
||||
thumbnailUrl: opts.thumbnailUrl ?? existing.thumbnailUrl,
|
||||
version: existing.version + 1,
|
||||
createdAt: existing.createdAt,
|
||||
updatedAt: now,
|
||||
ownerId: opts.ownerId ?? existing.ownerId,
|
||||
sizeBytes: serialized.length,
|
||||
nodeCount,
|
||||
graph: opts.graph,
|
||||
}
|
||||
this.data.set(existing.id, updated)
|
||||
return this.toMeta(updated)
|
||||
}
|
||||
|
||||
if (opts.expectedVersion !== undefined) {
|
||||
throw new SceneVersionConflictError('Cannot pass expectedVersion for a new scene')
|
||||
}
|
||||
|
||||
const id = opts.id ?? `scene_${++this.idCounter}`
|
||||
const now = new Date().toISOString()
|
||||
const serialized = JSON.stringify(opts.graph)
|
||||
const nodeCount = Object.keys(opts.graph.nodes ?? {}).length
|
||||
const record: SceneWithGraph = {
|
||||
id,
|
||||
name: opts.name,
|
||||
projectId: opts.projectId ?? null,
|
||||
thumbnailUrl: opts.thumbnailUrl ?? null,
|
||||
version: 1,
|
||||
createdAt: now,
|
||||
updatedAt: now,
|
||||
ownerId: opts.ownerId ?? null,
|
||||
sizeBytes: serialized.length,
|
||||
nodeCount,
|
||||
graph: opts.graph,
|
||||
}
|
||||
this.data.set(id, record)
|
||||
return this.toMeta(record)
|
||||
}
|
||||
|
||||
async load(id: string): Promise<SceneWithGraph | null> {
|
||||
const rec = this.data.get(id)
|
||||
if (!rec) return null
|
||||
return {
|
||||
...rec,
|
||||
graph: JSON.parse(JSON.stringify(rec.graph)),
|
||||
}
|
||||
}
|
||||
|
||||
async list(opts?: SceneListOptions): Promise<SceneMeta[]> {
|
||||
let scenes = Array.from(this.data.values()).map((r) => this.toMeta(r))
|
||||
if (opts?.projectId !== undefined) {
|
||||
scenes = scenes.filter((s) => s.projectId === opts.projectId)
|
||||
}
|
||||
if (opts?.ownerId !== undefined) {
|
||||
scenes = scenes.filter((s) => s.ownerId === opts.ownerId)
|
||||
}
|
||||
scenes.sort((a, b) => (a.updatedAt < b.updatedAt ? 1 : -1))
|
||||
if (opts?.limit !== undefined) scenes = scenes.slice(0, opts.limit)
|
||||
return scenes
|
||||
}
|
||||
|
||||
async delete(id: string, opts?: SceneMutateOptions): Promise<boolean> {
|
||||
const rec = this.data.get(id)
|
||||
if (!rec) throw new SceneNotFoundError(`Scene ${id} not found`)
|
||||
if (opts?.expectedVersion !== undefined && rec.version !== opts.expectedVersion) {
|
||||
throw new SceneVersionConflictError(
|
||||
`Expected version ${opts.expectedVersion}, have ${rec.version}`,
|
||||
)
|
||||
}
|
||||
return this.data.delete(id)
|
||||
}
|
||||
|
||||
async rename(id: string, newName: string, opts?: SceneMutateOptions): Promise<SceneMeta> {
|
||||
const rec = this.data.get(id)
|
||||
if (!rec) throw new SceneNotFoundError(`Scene ${id} not found`)
|
||||
if (opts?.expectedVersion !== undefined && rec.version !== opts.expectedVersion) {
|
||||
throw new SceneVersionConflictError(
|
||||
`Expected version ${opts.expectedVersion}, have ${rec.version}`,
|
||||
)
|
||||
}
|
||||
const updated: SceneWithGraph = {
|
||||
...rec,
|
||||
name: newName,
|
||||
version: rec.version + 1,
|
||||
updatedAt: new Date().toISOString(),
|
||||
}
|
||||
this.data.set(id, updated)
|
||||
return this.toMeta(updated)
|
||||
}
|
||||
|
||||
private toMeta(rec: SceneWithGraph): SceneMeta {
|
||||
return {
|
||||
id: rec.id,
|
||||
name: rec.name,
|
||||
projectId: rec.projectId,
|
||||
thumbnailUrl: rec.thumbnailUrl,
|
||||
version: rec.version,
|
||||
createdAt: rec.createdAt,
|
||||
updatedAt: rec.updatedAt,
|
||||
ownerId: rec.ownerId,
|
||||
sizeBytes: rec.sizeBytes,
|
||||
nodeCount: rec.nodeCount,
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user