Why: GET .../surfaces returns SurfaceMeta without identity; writing it into surfaces let PreviewPane crash on x.identity.surfaceId after refresh. Evidence: npx vitest run (118 passed), including surfaceKey.test.ts Impact: openConversation fetches SurfaceSnapshot via surfaceGet; find paths use surfaceKey; Meta no longer enters the surfaces projection Refs: fix/surface-list-hydrate Co-authored-by: Cursor <cursoragent@cursor.com>
20 lines
846 B
TypeScript
20 lines
846 B
TypeScript
// surface 投影可能短暂混入无 identity 的条目;读 id 一律经此助手,避免渲染崩溃。
|
|
import type { SurfaceSnapshot } from './contracts'
|
|
|
|
export function surfaceKey(x: { identity?: { surfaceId?: unknown } } | null | undefined): string | null {
|
|
const id = x?.identity?.surfaceId
|
|
return id == null || id === '' ? null : String(id)
|
|
}
|
|
|
|
/** 按 surfaceId 合并;同 id 取 revision 较大者。跳过无 identity 的脏项。 */
|
|
export function mergeSurfacesByRevision(a: SurfaceSnapshot[], b: SurfaceSnapshot[]): SurfaceSnapshot[] {
|
|
const byId = new Map<string, SurfaceSnapshot>()
|
|
for (const s of [...a, ...b]) {
|
|
const id = surfaceKey(s)
|
|
if (!id) continue
|
|
const existing = byId.get(id)
|
|
if (!existing || (s.revision ?? 0) >= (existing.revision ?? 0)) byId.set(id, s)
|
|
}
|
|
return [...byId.values()]
|
|
}
|