Open History to see the last commit
1import { COLORS } from "./catalogue";
2import {
3 CATALOGUE_VERSION,
4 MAX_AI_PARTS,
5 type Part,
6 type Connection,
7 type Edit,
8} from "./types";
9import { compileBlueprint } from "./blueprint";
10import { validPart } from "./geometry";
11import {
12 designRowSchema,
13 designSchema,
14 applyDesignPatch,
15 type Design,
16} from "./design";
17
18export type AISnapshot = {
19 generation: number;
20 title: string;
21 parts: Part[];
22 connections: Connection[];
23};
24
25export function sameAISnapshot(a: AISnapshot, b: AISnapshot) {
26 const parts = (s: AISnapshot) =>
27 s.parts
28 .map((p) => [
29 p.id,
30 p.key,
31 p.color,
32 p.position.x,
33 p.position.y,
34 p.position.z,
35 p.rotation,
36 p.revision,
37 !!p.deleted,
38 ])
39 .sort((x, y) => String(x[0]).localeCompare(String(y[0])));
40 const edges = (s: AISnapshot) =>
41 s.connections
42 .map((e) => [e.id, e.a, e.b, e.ca, e.cb, e.revision])
43 .sort((x, y) => String(x[0]).localeCompare(String(y[0])));
44 return (
45 a.generation === b.generation &&
46 JSON.stringify(parts(a)) === JSON.stringify(parts(b)) &&
47 JSON.stringify(edges(a)) === JSON.stringify(edges(b))
48 );
49}
50
51export function workshopReference(snapshot: AISnapshot): unknown {
52 if (!snapshot.parts.length) return null;
53 const pieces = snapshot.parts.map((p, i) => ({
54 id: `p${i}`,
55 part: p.key,
56 colour:
57 COLORS.find(
58 (c) => c.hex.toLowerCase() === p.color.toLowerCase(),
59 )?.name.toLowerCase() || "sand",
60 turn: p.rotation,
61 at: [p.position.x / 6, p.position.y / 2, p.position.z / 6],
62 repeat: null,
63 }));
64 if (pieces.length <= 100)
65 return {
66 version: 2,
67 title: snapshot.title,
68 summary: "The current workshop model.",
69 front: "+z",
70 pieces,
71 };
72 return {
73 version: 1,
74 catalogue: CATALOGUE_VERSION,
75 title: snapshot.title,
76 summary: "The current workshop model.",
77 parts: pieces.map(({ repeat, ...p }) => ({ ...p, attach: null })),
78 };
79}
80
81// An in-progress drawing is never a committed workshop. Only complete rows are
82// drawn, and final acceptance still performs full validation.
83export function streamingPreview(
84 input: unknown,
85 previous?: Design,
86): { parts: Part[]; title: string } | null {
87 if (!input || typeof input !== "object") return null;
88 const partial = input as Record<string, unknown>;
89 let design: unknown;
90 if (previous) {
91 if (!Array.isArray(partial.changes)) return null;
92 const changes = partial.changes.filter((c) => {
93 if (!c || typeof c !== "object") return false;
94 const v = c as { id?: unknown; row?: unknown };
95 return (
96 typeof v.id === "string" &&
97 (v.row === null || designRowSchema.safeParse(v.row).success)
98 );
99 });
100 if (!changes.length) return null;
101 try {
102 design = applyDesignPatch(previous, {
103 title: typeof partial.title === "string" ? partial.title : null,
104 summary: typeof partial.summary === "string" ? partial.summary : null,
105 changes,
106 });
107 } catch {
108 return null;
109 }
110 } else {
111 if (!Array.isArray(partial.pieces)) return null;
112 const pieces = partial.pieces
113 .map((row) => designRowSchema.safeParse(row))
114 .filter((r) => r.success)
115 .map((r) => r.data);
116 if (!pieces.length || pieces.length > 100) return null;
117 design = {
118 version: 2,
119 title:
120 typeof partial.title === "string" && partial.title
121 ? partial.title
122 : "Building your model",
123 summary: typeof partial.summary === "string" ? partial.summary : "",
124 front: partial.front || "+z",
125 pieces,
126 };
127 }
128 try {
129 const compiled = compileBlueprint(designSchema.parse(design));
130 if (
131 compiled.parts.some((p) => !validPart(p)) ||
132 compiled.issues.some((i) => i.code === "duplicate_id")
133 )
134 return null;
135 return { parts: compiled.parts, title: compiled.blueprint.title };
136 } catch {
137 return null;
138 }
139}
140
141export function aiWorkshopEdit(
142 snapshot: AISnapshot,
143 parts: Part[],
144 operationId: string,
145 uuid: () => string,
146): Edit {
147 const before = new Map(snapshot.parts.map((p, i) => [`p${i}`, p]));
148 const kept = new Set<string>();
149 const changes: NonNullable<Edit["changes"]> = [];
150 for (const p of parts) {
151 const old = before.get(p.id);
152 if (old) kept.add(old.id);
153 if (
154 !old ||
155 old.key !== p.key ||
156 old.color !== p.color ||
157 old.rotation !== p.rotation ||
158 old.position.x !== p.position.x ||
159 old.position.y !== p.position.y ||
160 old.position.z !== p.position.z
161 )
162 changes.push({
163 id: old?.id || uuid(),
164 ...(old ? { expected: old.revision } : {}),
165 key: p.key,
166 color: p.color,
167 position: p.position,
168 rotation: p.rotation,
169 });
170 }
171 for (const old of snapshot.parts)
172 if (!kept.has(old.id))
173 changes.push({ id: old.id, expected: old.revision, deleted: true });
174 if (parts.length > MAX_AI_PARTS || changes.length > MAX_AI_PARTS * 2)
175 throw new Error("AI model is too large");
176 const touched = new Set(changes.map((c) => c.id));
177 return {
178 operationId,
179 generation: snapshot.generation,
180 action: "edit",
181 changes,
182 connections: snapshot.connections
183 .filter((c) => touched.has(c.a) || touched.has(c.b))
184 .map(({ id, revision }) => ({ id, revision })),
185 };
186}