@widgetic/canvas 0.5.40 → 0.5.42
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +7 -3
- package/dist/canvas/DomCanvas.svelte +4 -0
- package/dist/canvas/DomCanvas.svelte.d.ts +8 -0
- package/dist/canvas/host/CanvasHost.d.ts +6 -0
- package/dist/canvas/host/CanvasHost.js +39 -0
- package/dist/canvas/host/CanvasHostApi.d.ts +2 -2
- package/dist/canvas/host/CanvasHostApi.js +7 -1
- package/dist/canvas/ui/CanvasHint.svelte +16 -3
- package/dist/canvas/ui/CanvasHint.svelte.d.ts +2 -17
- package/dist/canvas/ui/CanvasToolbar.svelte +7 -7
- package/dist/canvas/ui/ToolButton.svelte +11 -3
- package/dist/canvas/ui/ToolButton.svelte.d.ts +2 -0
- package/dist/canvas/ui/sizeLabelVisibility.d.ts +1 -1
- package/dist/canvas/ui/sizeLabelVisibility.js +1 -1
- package/dist/canvas/view/SelectionOverlay.svelte +33 -14
- package/dist/canvas/view/geometry.d.ts +14 -4
- package/dist/canvas/view/geometry.js +14 -7
- package/dist/engine/EngineCore.d.ts +4 -1
- package/dist/engine/EngineCore.js +11 -4
- package/dist/engine/codec/canvasDocument.js +42 -6
- package/dist/engine/core/arrow.d.ts +10 -3
- package/dist/engine/core/arrow.js +10 -2
- package/dist/engine/core/arrowBinding.d.ts +69 -0
- package/dist/engine/core/arrowBinding.js +326 -0
- package/dist/engine/core/arrowBindingRecords.d.ts +43 -0
- package/dist/engine/core/arrowBindingRecords.js +126 -0
- package/dist/engine/core/hitTest.d.ts +3 -0
- package/dist/engine/core/hitTest.js +2 -1
- package/dist/engine/core/scene.d.ts +3 -0
- package/dist/engine/core/scene.js +7 -2
- package/dist/engine/core/types.d.ts +29 -0
- package/dist/engine/input/PointerController.js +23 -1
- package/dist/engine/input/gestures.d.ts +12 -1
- package/dist/engine/input/gestures.js +56 -11
- package/dist/engine/input/keyboard.d.ts +9 -1
- package/dist/engine/input/keyboard.js +18 -2
- package/dist/engine/state.d.ts +6 -1
- package/dist/engine/state.js +2 -1
- package/dist/i18n/shortcuts.d.ts +12 -0
- package/dist/i18n/shortcuts.js +19 -5
- package/dist/tools/components/catalog.d.ts +11 -0
- package/dist/tools/components/catalog.js +260 -0
- package/dist/tools/components/grouping.d.ts +9 -0
- package/dist/tools/components/grouping.js +11 -0
- package/dist/tools/definitions.d.ts +19 -0
- package/dist/tools/definitions.js +147 -0
- package/dist/tools/executors.d.ts +7 -0
- package/dist/tools/executors.js +288 -0
- package/dist/tools/hostAdapter.d.ts +14 -0
- package/dist/tools/hostAdapter.js +38 -0
- package/dist/tools/index.d.ts +6 -0
- package/dist/tools/index.js +6 -0
- package/dist/tools/sceneSummary.d.ts +24 -0
- package/dist/tools/sceneSummary.js +156 -0
- package/dist/tools/sketchIntent.d.ts +17 -0
- package/dist/tools/sketchIntent.js +158 -0
- package/dist/tools/types.d.ts +54 -0
- package/dist/tools/types.js +11 -0
- package/package.json +7 -2
|
@@ -0,0 +1,288 @@
|
|
|
1
|
+
import { createEllipseObject, createPathObject, createRectObject, createTextObject } from '../engine/core/defaults.js';
|
|
2
|
+
import { buildArrow } from '../engine/core/arrow.js';
|
|
3
|
+
import { serializePathData } from '../engine/core/pathData.js';
|
|
4
|
+
import { boundsOfObjects, findObject, indexScene, normalizeScene, removeObjects, shapeBounds, translateObjects } from '../engine/core/scene.js';
|
|
5
|
+
import { isBoxedObject } from '../engine/core/types.js';
|
|
6
|
+
import { drawUiComponent, makeArrowBetween } from './components/catalog.js';
|
|
7
|
+
import { centerOf, findByLabel, kindOfObject, labelOfObject, summarizeScene } from './sceneSummary.js';
|
|
8
|
+
function groupLabelForName(groupId, scene) {
|
|
9
|
+
for (const object of scene) {
|
|
10
|
+
if (object.parentId === groupId && object.type === 'text')
|
|
11
|
+
return object.text.trim();
|
|
12
|
+
}
|
|
13
|
+
return 'group';
|
|
14
|
+
}
|
|
15
|
+
function fail(code, error) {
|
|
16
|
+
return { ok: false, error, code };
|
|
17
|
+
}
|
|
18
|
+
function ok(message, extra = {}) {
|
|
19
|
+
return { ok: true, message, ...extra };
|
|
20
|
+
}
|
|
21
|
+
function clampInBounds(host, x, y, width, height) {
|
|
22
|
+
const pad = 8;
|
|
23
|
+
return (x >= -pad &&
|
|
24
|
+
y >= -pad &&
|
|
25
|
+
x + width <= host.bounds.width + pad &&
|
|
26
|
+
y + height <= host.bounds.height + pad &&
|
|
27
|
+
width > 0 &&
|
|
28
|
+
height > 0);
|
|
29
|
+
}
|
|
30
|
+
function addToScene(host, objects) {
|
|
31
|
+
const withIds = objects.map((object) => ({ ...object, id: object.id ?? host.createId(kindOfObject(object)) }));
|
|
32
|
+
const scene = normalizeScene([...host.getScene(), ...withIds]);
|
|
33
|
+
return { scene, ids: withIds.map((object) => object.id) };
|
|
34
|
+
}
|
|
35
|
+
const executors = {
|
|
36
|
+
get_canvas(host) {
|
|
37
|
+
const summary = summarizeScene(host.getScene(), host.bounds);
|
|
38
|
+
return ok(`Canvas has ${summary.elements.length} elements.`, { summary });
|
|
39
|
+
},
|
|
40
|
+
draw_ui_component(host, args) {
|
|
41
|
+
const component = String(args.component ?? '');
|
|
42
|
+
const x = Number(args.x);
|
|
43
|
+
const y = Number(args.y);
|
|
44
|
+
const width = Number(args.width ?? 360);
|
|
45
|
+
const height = Number(args.height ?? 240);
|
|
46
|
+
const label = typeof args.label === 'string' ? args.label : undefined;
|
|
47
|
+
if (!Number.isFinite(x) || !Number.isFinite(y))
|
|
48
|
+
return fail('invalid_args', 'x and y are required numbers.');
|
|
49
|
+
const placement = drawUiComponent(component, x, y, width, height, label);
|
|
50
|
+
const { scene, ids } = addToScene(host, placement.objects);
|
|
51
|
+
// The check is on what the component really covers (a button is 48 px high whatever height was asked for, a video
|
|
52
|
+
// player has its title above y and a navigation strip below the thumbnail), not on the size that was passed: a
|
|
53
|
+
// request with no size would otherwise be judged as 360 x 240, and a button near the bottom would be refused.
|
|
54
|
+
const covered = boundsOfObjects(scene, ids);
|
|
55
|
+
if (!covered || !clampInBounds(host, covered.x, covered.y, covered.width, covered.height)) {
|
|
56
|
+
const area = covered
|
|
57
|
+
? ` It would cover x ${Math.round(covered.x)} to ${Math.round(covered.x + covered.width)} and y ${Math.round(covered.y)} to ${Math.round(covered.y + covered.height)}, and the canvas is ${Math.round(host.bounds.width)} x ${Math.round(host.bounds.height)}.`
|
|
58
|
+
: '';
|
|
59
|
+
return fail('bounds', `Component would sit outside the canvas.${area}`);
|
|
60
|
+
}
|
|
61
|
+
host.commitScene(scene);
|
|
62
|
+
return ok(`Drew ${component} (${placement.rootLabel}).`, { createdIds: ids, summary: summarizeScene(scene, host.bounds) });
|
|
63
|
+
},
|
|
64
|
+
add_shape(host, args) {
|
|
65
|
+
const shape = String(args.shape ?? 'rectangle');
|
|
66
|
+
const x = Number(args.x);
|
|
67
|
+
const y = Number(args.y);
|
|
68
|
+
const width = Number(args.width);
|
|
69
|
+
const height = Number(args.height);
|
|
70
|
+
if (!Number.isFinite(x) || !Number.isFinite(y) || !Number.isFinite(width) || !Number.isFinite(height)) {
|
|
71
|
+
return fail('invalid_args', 'x, y, width and height are required.');
|
|
72
|
+
}
|
|
73
|
+
if (!clampInBounds(host, x, y, width, height))
|
|
74
|
+
return fail('bounds', 'Shape is outside the canvas.');
|
|
75
|
+
let object;
|
|
76
|
+
const label = typeof args.label === 'string' ? args.label : undefined;
|
|
77
|
+
if (shape === 'ellipse') {
|
|
78
|
+
object = createEllipseObject({ x, y, width, height });
|
|
79
|
+
}
|
|
80
|
+
else if (shape === 'diamond') {
|
|
81
|
+
const path = `M ${width / 2} 0 L ${width} ${height / 2} L ${width / 2} ${height} L 0 ${height / 2} Z`;
|
|
82
|
+
object = createPathObject({ x, y, width, height, d: path, shape: 'diamond' });
|
|
83
|
+
}
|
|
84
|
+
else {
|
|
85
|
+
object = createRectObject({ x, y, width, height, roundness: shape === 'rounded' ? 16 : 0 });
|
|
86
|
+
}
|
|
87
|
+
const objects = [object];
|
|
88
|
+
if (label) {
|
|
89
|
+
objects.push(createTextObject({ x: x + 4, y: y + 4, text: label, fontSize: 12, autoWidth: true }));
|
|
90
|
+
}
|
|
91
|
+
const { scene, ids } = addToScene(host, objects);
|
|
92
|
+
host.commitScene(scene);
|
|
93
|
+
return ok(`Added ${shape}.`, { createdIds: ids, summary: summarizeScene(scene, host.bounds) });
|
|
94
|
+
},
|
|
95
|
+
add_text(host, args) {
|
|
96
|
+
const text = String(args.text ?? '').trim();
|
|
97
|
+
const x = Number(args.x);
|
|
98
|
+
const y = Number(args.y);
|
|
99
|
+
if (!text)
|
|
100
|
+
return fail('invalid_args', 'text is required.');
|
|
101
|
+
if (!Number.isFinite(x) || !Number.isFinite(y))
|
|
102
|
+
return fail('invalid_args', 'x and y are required.');
|
|
103
|
+
const width = Number(args.width ?? 200);
|
|
104
|
+
const object = createTextObject({ x, y, width, text, autoWidth: width <= 40 });
|
|
105
|
+
if (!clampInBounds(host, x, y, object.width, object.height))
|
|
106
|
+
return fail('bounds', 'Text is outside the canvas.');
|
|
107
|
+
const { scene, ids } = addToScene(host, [object]);
|
|
108
|
+
host.commitScene(scene);
|
|
109
|
+
return ok('Added text.', { createdIds: ids, summary: summarizeScene(scene, host.bounds) });
|
|
110
|
+
},
|
|
111
|
+
add_arrow(host, args) {
|
|
112
|
+
let fromX = Number(args.fromX);
|
|
113
|
+
let fromY = Number(args.fromY);
|
|
114
|
+
let toX = Number(args.toX);
|
|
115
|
+
let toY = Number(args.toY);
|
|
116
|
+
const scene = host.getScene();
|
|
117
|
+
if (typeof args.fromId === 'string') {
|
|
118
|
+
const center = centerOf(args.fromId, scene);
|
|
119
|
+
if (!center)
|
|
120
|
+
return fail('not_found', `fromId ${args.fromId} not found.`);
|
|
121
|
+
fromX = center.x;
|
|
122
|
+
fromY = center.y;
|
|
123
|
+
}
|
|
124
|
+
if (typeof args.toId === 'string') {
|
|
125
|
+
const center = centerOf(args.toId, scene);
|
|
126
|
+
if (!center)
|
|
127
|
+
return fail('not_found', `toId ${args.toId} not found.`);
|
|
128
|
+
toX = center.x;
|
|
129
|
+
toY = center.y;
|
|
130
|
+
}
|
|
131
|
+
if (![fromX, fromY, toX, toY].every(Number.isFinite))
|
|
132
|
+
return fail('invalid_args', 'Need from/to points or ids.');
|
|
133
|
+
const objects = [makeArrowBetween(fromX, fromY, toX, toY)];
|
|
134
|
+
if (typeof args.label === 'string' && args.label.trim()) {
|
|
135
|
+
objects.push(createTextObject({ x: (fromX + toX) / 2, y: (fromY + toY) / 2 - 16, text: args.label.trim(), fontSize: 12, autoWidth: true }));
|
|
136
|
+
}
|
|
137
|
+
const added = addToScene(host, objects);
|
|
138
|
+
host.commitScene(added.scene);
|
|
139
|
+
return ok('Added arrow.', { createdIds: added.ids, summary: summarizeScene(added.scene, host.bounds) });
|
|
140
|
+
},
|
|
141
|
+
move_elements(host, args) {
|
|
142
|
+
const ids = Array.isArray(args.ids) ? args.ids.map(String) : [];
|
|
143
|
+
if (ids.length === 0)
|
|
144
|
+
return fail('invalid_args', 'ids required.');
|
|
145
|
+
let scene = host.getScene();
|
|
146
|
+
const index = indexScene(scene);
|
|
147
|
+
for (const id of ids) {
|
|
148
|
+
if (!index.has(id))
|
|
149
|
+
return fail('not_found', `Element ${id} not found.`);
|
|
150
|
+
}
|
|
151
|
+
const dx = Number(args.dx ?? 0);
|
|
152
|
+
const dy = Number(args.dy ?? 0);
|
|
153
|
+
if ((Number.isFinite(args.x) || Number.isFinite(args.y)) && ids.length > 0) {
|
|
154
|
+
const first = findObject(scene, ids[0]);
|
|
155
|
+
if (!first)
|
|
156
|
+
return fail('not_found', 'First id not movable.');
|
|
157
|
+
const bbox = first.type === 'group'
|
|
158
|
+
? boundsOfObjects(scene, [first.id])
|
|
159
|
+
: isBoxedObject(first)
|
|
160
|
+
? shapeBounds(first)
|
|
161
|
+
: null;
|
|
162
|
+
if (!bbox)
|
|
163
|
+
return fail('not_found', 'First id not movable.');
|
|
164
|
+
const targetDx = Number.isFinite(args.x) ? Number(args.x) - bbox.x : 0;
|
|
165
|
+
const targetDy = Number.isFinite(args.y) ? Number(args.y) - bbox.y : 0;
|
|
166
|
+
scene = translateObjects(scene, ids, targetDx, targetDy);
|
|
167
|
+
}
|
|
168
|
+
else if (dx !== 0 || dy !== 0) {
|
|
169
|
+
scene = translateObjects(scene, ids, dx, dy);
|
|
170
|
+
}
|
|
171
|
+
else {
|
|
172
|
+
return fail('invalid_args', 'Provide dx/dy or x/y.');
|
|
173
|
+
}
|
|
174
|
+
host.commitScene(scene);
|
|
175
|
+
return ok(`Moved ${ids.length} element(s).`, { affectedIds: ids, summary: summarizeScene(scene, host.bounds) });
|
|
176
|
+
},
|
|
177
|
+
delete_elements(host, args) {
|
|
178
|
+
const ids = Array.isArray(args.ids) ? args.ids.map(String) : [];
|
|
179
|
+
if (ids.length === 0)
|
|
180
|
+
return fail('invalid_args', 'ids required.');
|
|
181
|
+
let scene = host.getScene();
|
|
182
|
+
if (ids.length === 1 && ids[0] === 'all') {
|
|
183
|
+
scene = [];
|
|
184
|
+
}
|
|
185
|
+
else {
|
|
186
|
+
const missing = ids.filter((id) => !findObject(scene, id));
|
|
187
|
+
if (missing.length > 0)
|
|
188
|
+
return fail('not_found', `Unknown ids: ${missing.join(', ')}`);
|
|
189
|
+
scene = removeObjects(scene, ids);
|
|
190
|
+
}
|
|
191
|
+
host.commitScene(scene);
|
|
192
|
+
return ok('Deleted elements.', { affectedIds: ids, summary: summarizeScene(scene, host.bounds) });
|
|
193
|
+
},
|
|
194
|
+
clear_canvas(host) {
|
|
195
|
+
const scene = [];
|
|
196
|
+
host.commitScene(scene);
|
|
197
|
+
return ok('Canvas cleared.', { summary: summarizeScene(scene, host.bounds) });
|
|
198
|
+
},
|
|
199
|
+
name_components_with_arrows(host, args) {
|
|
200
|
+
const prefix = typeof args.prefix === 'string' ? args.prefix.trim() : '';
|
|
201
|
+
const sceneBefore = host.getScene();
|
|
202
|
+
const additions = [];
|
|
203
|
+
for (const object of sceneBefore) {
|
|
204
|
+
if (object.parentId !== null)
|
|
205
|
+
continue;
|
|
206
|
+
if (object.type === 'text')
|
|
207
|
+
continue;
|
|
208
|
+
if (object.type === 'path' && object.arrow)
|
|
209
|
+
continue;
|
|
210
|
+
if (!isBoxedObject(object) && object.type !== 'group')
|
|
211
|
+
continue;
|
|
212
|
+
const name = `${prefix ? `${prefix} ` : ''}${object.type === 'group' ? groupLabelForName(object.id, sceneBefore) : labelOfObject(object)}`.trim();
|
|
213
|
+
const bbox = object.type === 'group'
|
|
214
|
+
? boundsOfObjects(sceneBefore, [object.id])
|
|
215
|
+
: isBoxedObject(object)
|
|
216
|
+
? shapeBounds(object)
|
|
217
|
+
: null;
|
|
218
|
+
if (!bbox)
|
|
219
|
+
continue;
|
|
220
|
+
const labelX = bbox.x - 8;
|
|
221
|
+
const labelY = bbox.y - 28;
|
|
222
|
+
const label = createTextObject({ x: labelX, y: labelY, text: name, fontSize: 12, autoWidth: true });
|
|
223
|
+
additions.push(label);
|
|
224
|
+
const geometry = buildArrow({ x: labelX + 40, y: labelY + 10 }, { x: bbox.x + bbox.width / 2, y: bbox.y + bbox.height / 2 }, 0, 2);
|
|
225
|
+
additions.push(createPathObject({
|
|
226
|
+
x: geometry.box.x,
|
|
227
|
+
y: geometry.box.y,
|
|
228
|
+
width: geometry.box.width,
|
|
229
|
+
height: geometry.box.height,
|
|
230
|
+
d: serializePathData(geometry.commands),
|
|
231
|
+
shape: 'arrow',
|
|
232
|
+
arrow: geometry.spec,
|
|
233
|
+
stroke: '#111827',
|
|
234
|
+
strokeWidth: 2,
|
|
235
|
+
fill: 'transparent'
|
|
236
|
+
}));
|
|
237
|
+
}
|
|
238
|
+
const { scene, ids } = addToScene(host, additions);
|
|
239
|
+
host.commitScene(scene);
|
|
240
|
+
return ok(`Named ${additions.length / 2} components.`, { createdIds: ids, summary: summarizeScene(scene, host.bounds) });
|
|
241
|
+
}
|
|
242
|
+
};
|
|
243
|
+
export function executeCanvasTool(host, name, args) {
|
|
244
|
+
const run = executors[name];
|
|
245
|
+
if (!run)
|
|
246
|
+
return fail('invalid_args', `Unknown tool ${name}.`);
|
|
247
|
+
try {
|
|
248
|
+
return run(host, args);
|
|
249
|
+
}
|
|
250
|
+
catch (error) {
|
|
251
|
+
return { ok: false, error: error instanceof Error ? error.message : 'Tool failed.', code: 'internal' };
|
|
252
|
+
}
|
|
253
|
+
}
|
|
254
|
+
/** Navigation wireframe group (label "Navigation"). */
|
|
255
|
+
export function findNavigationId(scene) {
|
|
256
|
+
for (const object of scene) {
|
|
257
|
+
if (object.type === 'text' && object.text.trim().toLowerCase() === 'navigation' && object.parentId) {
|
|
258
|
+
return object.parentId;
|
|
259
|
+
}
|
|
260
|
+
}
|
|
261
|
+
const direct = findByLabel(scene, 'navigation');
|
|
262
|
+
if (direct) {
|
|
263
|
+
const object = findObject(scene, direct);
|
|
264
|
+
if (object?.type === 'group')
|
|
265
|
+
return direct;
|
|
266
|
+
if (object?.parentId)
|
|
267
|
+
return object.parentId;
|
|
268
|
+
}
|
|
269
|
+
return findByLabel(scene, 'nav');
|
|
270
|
+
}
|
|
271
|
+
export function findVideoThumbnailBounds(scene) {
|
|
272
|
+
for (const object of scene) {
|
|
273
|
+
if (object.type === 'text' && object.text.toLowerCase() === 'thumbnail') {
|
|
274
|
+
const frame = scene.find((candidate) => candidate.parentId === object.parentId || candidate.type === 'rect');
|
|
275
|
+
if (frame && isBoxedObject(frame))
|
|
276
|
+
return shapeBounds(frame);
|
|
277
|
+
}
|
|
278
|
+
}
|
|
279
|
+
const screen = scene.find((object) => object.type === 'rect' && object.fill === '#f3f4f6' && object.parentId !== null);
|
|
280
|
+
return screen && isBoxedObject(screen) ? shapeBounds(screen) : null;
|
|
281
|
+
}
|
|
282
|
+
export function findVideoPlayerRoot(scene) {
|
|
283
|
+
for (const object of scene) {
|
|
284
|
+
if (object.type === 'text' && object.text.toLowerCase().includes('video player'))
|
|
285
|
+
return object.id;
|
|
286
|
+
}
|
|
287
|
+
return findByLabel(scene, 'video player');
|
|
288
|
+
}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import type { EngineCore } from '../engine/EngineCore.js';
|
|
2
|
+
import type { CanvasBounds, CanvasToolHost } from './types.js';
|
|
3
|
+
export interface EngineToolHostOptions {
|
|
4
|
+
engine: EngineCore;
|
|
5
|
+
bounds?: CanvasBounds;
|
|
6
|
+
/** When `transient`, scene updates apply without a new undo step until a `push` flush. */
|
|
7
|
+
commitMode?: 'push' | 'transient';
|
|
8
|
+
}
|
|
9
|
+
/**
|
|
10
|
+
* Adapts EngineCore to CanvasToolHost: one `commit()` per assistant turn.
|
|
11
|
+
*/
|
|
12
|
+
export declare function createCanvasToolHostFromEngine(options: EngineToolHostOptions): CanvasToolHost;
|
|
13
|
+
/** Run several tool calls as a single undo step on the engine. */
|
|
14
|
+
export declare function runCanvasToolBatch(host: CanvasToolHost, run: () => void, engine?: EngineCore): void;
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import { createObjectId } from '../engine/core/defaults.js';
|
|
2
|
+
/**
|
|
3
|
+
* Adapts EngineCore to CanvasToolHost: one `commit()` per assistant turn.
|
|
4
|
+
*/
|
|
5
|
+
export function createCanvasToolHostFromEngine(options) {
|
|
6
|
+
const { engine } = options;
|
|
7
|
+
const bounds = options.bounds ?? {
|
|
8
|
+
width: engine.state.viewSize.width || 1200,
|
|
9
|
+
height: engine.state.viewSize.height || 800
|
|
10
|
+
};
|
|
11
|
+
return {
|
|
12
|
+
bounds,
|
|
13
|
+
getScene() {
|
|
14
|
+
return engine.state.scene;
|
|
15
|
+
},
|
|
16
|
+
createId(prefix) {
|
|
17
|
+
return createObjectId(prefix ?? 'sketch');
|
|
18
|
+
},
|
|
19
|
+
commitScene(next) {
|
|
20
|
+
engine.applyScene(next, options.commitMode ?? 'push');
|
|
21
|
+
}
|
|
22
|
+
};
|
|
23
|
+
}
|
|
24
|
+
/** Run several tool calls as a single undo step on the engine. */
|
|
25
|
+
export function runCanvasToolBatch(host, run, engine) {
|
|
26
|
+
// When backed by engine, defer commit until batch ends.
|
|
27
|
+
const engineHost = host;
|
|
28
|
+
if (engine) {
|
|
29
|
+
const snapshot = engine.state.scene;
|
|
30
|
+
run();
|
|
31
|
+
const finalScene = host.getScene();
|
|
32
|
+
if (finalScene !== snapshot) {
|
|
33
|
+
engine.applyScene(finalScene, 'push');
|
|
34
|
+
}
|
|
35
|
+
return;
|
|
36
|
+
}
|
|
37
|
+
run();
|
|
38
|
+
}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import type { Scene } from '../engine/core/scene.js';
|
|
2
|
+
import type { ObjectId, SceneObject } from '../engine/core/types.js';
|
|
3
|
+
import type { CanvasBounds, SceneSummary } from './types.js';
|
|
4
|
+
export declare const SCENE_SUMMARY_MAX_ELEMENTS = 48;
|
|
5
|
+
export declare function labelOfObject(object: SceneObject): string;
|
|
6
|
+
export declare function kindOfObject(object: SceneObject): string;
|
|
7
|
+
export declare function summarizeScene(scene: Scene, bounds?: CanvasBounds): SceneSummary;
|
|
8
|
+
export declare function findByLabel(scene: Scene, needle: string): ObjectId | null;
|
|
9
|
+
export declare function centerOf(id: ObjectId, scene: Scene): {
|
|
10
|
+
x: number;
|
|
11
|
+
y: number;
|
|
12
|
+
} | null;
|
|
13
|
+
export declare function sceneIndexMap(scene: Scene): Map<ObjectId, SceneObject>;
|
|
14
|
+
export declare function bboxIntersects(a: {
|
|
15
|
+
x: number;
|
|
16
|
+
y: number;
|
|
17
|
+
width: number;
|
|
18
|
+
height: number;
|
|
19
|
+
}, b: {
|
|
20
|
+
x: number;
|
|
21
|
+
y: number;
|
|
22
|
+
width: number;
|
|
23
|
+
height: number;
|
|
24
|
+
}): boolean;
|
|
@@ -0,0 +1,156 @@
|
|
|
1
|
+
import { boundsOfObjects, findObject, indexScene, shapeBounds } from '../engine/core/scene.js';
|
|
2
|
+
import { isBoxedObject } from '../engine/core/types.js';
|
|
3
|
+
const DEFAULT_BOUNDS = { width: 1200, height: 800 };
|
|
4
|
+
export const SCENE_SUMMARY_MAX_ELEMENTS = 48;
|
|
5
|
+
/** The longest label a summary carries (the assistant request schema accepts 256, and the model needs far fewer). */
|
|
6
|
+
const SCENE_LABEL_MAX_CHARS = 80;
|
|
7
|
+
/** A label cut to the length a summary carries, so a long text never makes a request the gateway refuses. */
|
|
8
|
+
function shortenLabel(text) {
|
|
9
|
+
return text.length > SCENE_LABEL_MAX_CHARS ? `${text.slice(0, SCENE_LABEL_MAX_CHARS - 3)}...` : text;
|
|
10
|
+
}
|
|
11
|
+
export function labelOfObject(object) {
|
|
12
|
+
if (object.type === 'text') {
|
|
13
|
+
return shortenLabel(object.text.trim());
|
|
14
|
+
}
|
|
15
|
+
if (object.type === 'widget')
|
|
16
|
+
return object.widgetName ?? 'widget';
|
|
17
|
+
if (object.type === 'group')
|
|
18
|
+
return 'group';
|
|
19
|
+
if (object.type === 'path' && object.arrow)
|
|
20
|
+
return 'arrow';
|
|
21
|
+
if (object.type === 'path' && object.shape)
|
|
22
|
+
return String(object.shape);
|
|
23
|
+
if (object.type === 'rect')
|
|
24
|
+
return 'rectangle';
|
|
25
|
+
if (object.type === 'ellipse')
|
|
26
|
+
return 'ellipse';
|
|
27
|
+
if (object.type === 'image')
|
|
28
|
+
return object.fileName ?? 'image';
|
|
29
|
+
return object.type;
|
|
30
|
+
}
|
|
31
|
+
function groupLabelFromMembers(groupId, scene) {
|
|
32
|
+
for (const object of scene) {
|
|
33
|
+
if (object.parentId === groupId && object.type === 'text') {
|
|
34
|
+
// Cut like the label of a loose text: a component's first text can be a whole paragraph.
|
|
35
|
+
return shortenLabel(object.text.trim()) || 'group';
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
return 'group';
|
|
39
|
+
}
|
|
40
|
+
export function kindOfObject(object) {
|
|
41
|
+
if (object.type === 'group')
|
|
42
|
+
return 'group';
|
|
43
|
+
if (object.type === 'path' && object.arrow)
|
|
44
|
+
return 'arrow';
|
|
45
|
+
if (object.type === 'text')
|
|
46
|
+
return 'text';
|
|
47
|
+
return object.type;
|
|
48
|
+
}
|
|
49
|
+
function elementPriority(element) {
|
|
50
|
+
if (element.kind === 'group')
|
|
51
|
+
return 0;
|
|
52
|
+
if (/navigation|video|thumbnail|card|button/i.test(element.label))
|
|
53
|
+
return 1;
|
|
54
|
+
if (element.kind === 'rect')
|
|
55
|
+
return 2;
|
|
56
|
+
return 3;
|
|
57
|
+
}
|
|
58
|
+
function compareElements(a, b) {
|
|
59
|
+
const pa = elementPriority(a);
|
|
60
|
+
const pb = elementPriority(b);
|
|
61
|
+
if (pa !== pb)
|
|
62
|
+
return pa - pb;
|
|
63
|
+
const areaA = a.bbox.width * a.bbox.height;
|
|
64
|
+
const areaB = b.bbox.width * b.bbox.height;
|
|
65
|
+
if (areaA !== areaB)
|
|
66
|
+
return areaB - areaA;
|
|
67
|
+
return a.zIndex - b.zIndex;
|
|
68
|
+
}
|
|
69
|
+
export function summarizeScene(scene, bounds = DEFAULT_BOUNDS) {
|
|
70
|
+
const elements = [];
|
|
71
|
+
scene.forEach((object, zIndex) => {
|
|
72
|
+
if (object.type === 'group') {
|
|
73
|
+
const memberIds = scene.filter((member) => member.parentId === object.id).map((member) => member.id);
|
|
74
|
+
const bbox = boundsOfObjects(scene, memberIds);
|
|
75
|
+
if (!bbox)
|
|
76
|
+
return;
|
|
77
|
+
elements.push({
|
|
78
|
+
id: object.id,
|
|
79
|
+
kind: 'group',
|
|
80
|
+
label: groupLabelFromMembers(object.id, scene),
|
|
81
|
+
bbox,
|
|
82
|
+
zIndex,
|
|
83
|
+
parentId: object.parentId
|
|
84
|
+
});
|
|
85
|
+
return;
|
|
86
|
+
}
|
|
87
|
+
if (object.parentId !== null)
|
|
88
|
+
return;
|
|
89
|
+
if (!isBoxedObject(object))
|
|
90
|
+
return;
|
|
91
|
+
const bbox = shapeBounds(object);
|
|
92
|
+
elements.push({
|
|
93
|
+
id: object.id,
|
|
94
|
+
kind: kindOfObject(object),
|
|
95
|
+
label: labelOfObject(object),
|
|
96
|
+
bbox,
|
|
97
|
+
zIndex,
|
|
98
|
+
parentId: object.parentId
|
|
99
|
+
});
|
|
100
|
+
});
|
|
101
|
+
const sorted = [...elements].sort(compareElements);
|
|
102
|
+
const totalCount = sorted.length;
|
|
103
|
+
const truncated = totalCount > SCENE_SUMMARY_MAX_ELEMENTS;
|
|
104
|
+
const limited = truncated ? sorted.slice(0, SCENE_SUMMARY_MAX_ELEMENTS) : sorted;
|
|
105
|
+
return {
|
|
106
|
+
bounds,
|
|
107
|
+
elements: limited,
|
|
108
|
+
totalCount,
|
|
109
|
+
truncated,
|
|
110
|
+
truncationNote: truncated ? `Showing ${SCENE_SUMMARY_MAX_ELEMENTS} of ${totalCount} top-level elements.` : undefined
|
|
111
|
+
};
|
|
112
|
+
}
|
|
113
|
+
export function findByLabel(scene, needle) {
|
|
114
|
+
const lower = needle.toLowerCase();
|
|
115
|
+
for (const object of scene) {
|
|
116
|
+
if (object.type === 'group') {
|
|
117
|
+
const label = groupLabelFromMembers(object.id, scene).toLowerCase();
|
|
118
|
+
if (label.includes(lower))
|
|
119
|
+
return object.id;
|
|
120
|
+
}
|
|
121
|
+
if (object.type === 'text') {
|
|
122
|
+
const label = object.text.toLowerCase();
|
|
123
|
+
if (label.includes(lower))
|
|
124
|
+
return object.id;
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
for (const object of scene) {
|
|
128
|
+
const label = labelOfObject(object).toLowerCase();
|
|
129
|
+
if (label.includes(lower))
|
|
130
|
+
return object.id;
|
|
131
|
+
}
|
|
132
|
+
return null;
|
|
133
|
+
}
|
|
134
|
+
export function centerOf(id, scene) {
|
|
135
|
+
const object = findObject(scene, id);
|
|
136
|
+
if (object?.type === 'group') {
|
|
137
|
+
const bbox = boundsOfObjects(scene, [id]);
|
|
138
|
+
if (!bbox)
|
|
139
|
+
return null;
|
|
140
|
+
return { x: bbox.x + bbox.width / 2, y: bbox.y + bbox.height / 2 };
|
|
141
|
+
}
|
|
142
|
+
if (!object || !isBoxedObject(object))
|
|
143
|
+
return null;
|
|
144
|
+
const bbox = shapeBounds(object);
|
|
145
|
+
return { x: bbox.x + bbox.width / 2, y: bbox.y + bbox.height / 2 };
|
|
146
|
+
}
|
|
147
|
+
export function sceneIndexMap(scene) {
|
|
148
|
+
return indexScene(scene);
|
|
149
|
+
}
|
|
150
|
+
export function bboxIntersects(a, b) {
|
|
151
|
+
const pad = 2;
|
|
152
|
+
return (a.x + a.width > b.x + pad &&
|
|
153
|
+
b.x + b.width > a.x + pad &&
|
|
154
|
+
a.y + a.height > b.y + pad &&
|
|
155
|
+
b.y + b.height > a.y + pad);
|
|
156
|
+
}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
export type SketchIntentVerdict = 'sketch' | 'generate' | 'unknown';
|
|
2
|
+
export declare const SKETCH_INTENT_COUNTER_EXAMPLES: readonly ["build me a countdown widget", "make the button blue in the widget", "generate the react code for this", "implement the checkout form", "ship this widget to production", "publish the widget now"];
|
|
3
|
+
/**
|
|
4
|
+
* Free-text requests that draw, in the languages the router knows. Every one must route to the sketch assistant
|
|
5
|
+
* (the creator's tests and the gateway's mirror hold the same list).
|
|
6
|
+
*/
|
|
7
|
+
export declare const SKETCH_FREE_TEXT_EXAMPLES: readonly ["Sketch a pricing page with three plans and a call to action under each", "wireframe a dashboard with a sidebar and four stat cards", "can you draw three circles in a row and connect them with arrows", "doodle a house with a red roof", "put the navigation outside the thumbnail", "dibuja una página de precios con tres planes", "dibújame un formulario de inicio de sesión", "haz un boceto de una tienda en línea", "dessine une page de tarification avec trois offres", "fais un croquis de la page d’accueil", "zeichne eine Preisseite mit drei Tarifen", "skizziere ein Dashboard mit vier Kacheln", "disegna una pagina di login", "fai uno schizzo della home page", "desenha uma página de preços com três planos", "faz um rascunho do painel", "desenează o pagină de prețuri cu trei planuri", "desenează-mi un formular de autentificare", "fă o schiță a paginii de start", "narysuj stronę z cennikiem", "zrób szkic panelu", "teken een prijzenpagina met drie abonnementen", "maak een schets van het dashboard", "ログイン画面のスケッチを描いて", "画一个登录表单", "로그인 폼을 그려줘"];
|
|
8
|
+
/**
|
|
9
|
+
* Requests that are not for a drawing, with the verdict each gets. Creating something ("make", "create", "I need") is a
|
|
10
|
+
* widget, so most of them are `unknown` and go to the code generator by default; the ones that name code are `generate`.
|
|
11
|
+
*/
|
|
12
|
+
export declare const SKETCH_FREE_TEXT_COUNTER_EXAMPLES: readonly [readonly ["a video player with a playlist", "unknown"], readonly ["create a countdown timer with a start button", "unknown"], readonly ["I want a quiz about world capitals", "unknown"], readonly ["make a picture slider with arrows", "unknown"], readonly ["dibuja y publica el widget", "generate"], readonly ["publie le widget maintenant", "generate"], readonly ["erstelle einen Countdown-Timer", "unknown"], readonly ["costruisci un widget con il conto alla rovescia", "generate"], readonly ["crea un widget di sondaggio", "unknown"], readonly ["faz um widget de contagem decrescente", "unknown"], readonly ["construiește un widget cu numărătoare inversă", "generate"], readonly ["opublikuj widget teraz", "generate"], readonly ["画面を作って", "unknown"], readonly ["kennzeichnen Sie die Felder", "unknown"]];
|
|
13
|
+
/**
|
|
14
|
+
* Lightweight sketch-vs-codegen classifier for the creator chat (gateway mirrors this in tests).
|
|
15
|
+
*/
|
|
16
|
+
export declare function classifySketchIntent(message: string): SketchIntentVerdict;
|
|
17
|
+
export declare const SKETCH_ACCEPTANCE_PROMPTS: readonly ["draw a video player for me", "move the navigation outside the video thumbnail", "delete all sketches", "name every component and draw an arrow to it"];
|