@puckeditor/plugin-ai 0.8.0-canary.c7a937d7 → 0.8.0-canary.d3caecc3

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.
Files changed (3) hide show
  1. package/dist/index.js +261 -229
  2. package/dist/index.mjs +261 -229
  3. package/package.json +2 -1
package/dist/index.js CHANGED
@@ -136,6 +136,259 @@ var require_classnames = __commonJS({
136
136
  }
137
137
  });
138
138
 
139
+ // ../reducer/dist/index.js
140
+ var require_dist = __commonJS({
141
+ "../reducer/dist/index.js"(exports2, module2) {
142
+ "use strict";
143
+ init_react_import();
144
+ var __defProp2 = Object.defineProperty;
145
+ var __getOwnPropDesc2 = Object.getOwnPropertyDescriptor;
146
+ var __getOwnPropNames2 = Object.getOwnPropertyNames;
147
+ var __hasOwnProp2 = Object.prototype.hasOwnProperty;
148
+ var __export2 = (target, all) => {
149
+ for (var name in all)
150
+ __defProp2(target, name, { get: all[name], enumerable: true });
151
+ };
152
+ var __copyProps2 = (to2, from, except, desc) => {
153
+ if (from && typeof from === "object" || typeof from === "function") {
154
+ for (let key of __getOwnPropNames2(from))
155
+ if (!__hasOwnProp2.call(to2, key) && key !== except)
156
+ __defProp2(to2, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc2(from, key)) || desc.enumerable });
157
+ }
158
+ return to2;
159
+ };
160
+ var __toCommonJS2 = (mod) => __copyProps2(__defProp2({}, "__esModule", { value: true }), mod);
161
+ var index_exports2 = {};
162
+ __export2(index_exports2, {
163
+ dispatchOp: () => dispatchOp2,
164
+ resolveAppends: () => resolveAppends
165
+ });
166
+ module2.exports = __toCommonJS2(index_exports2);
167
+ var parseAppendPath = (path) => {
168
+ const segments = [];
169
+ const pattern = /([^.[\]]+)|\[(\d+)\]/g;
170
+ let match;
171
+ while ((match = pattern.exec(path)) !== null) {
172
+ segments.push(match[2] !== void 0 ? Number(match[2]) : match[1]);
173
+ }
174
+ return segments;
175
+ };
176
+ var appendAtPath = (base, segments, tail) => {
177
+ if (segments.length === 0) {
178
+ return `${typeof base === "string" ? base : ""}${tail}`;
179
+ }
180
+ const [head, ...rest] = segments;
181
+ if (typeof head === "number") {
182
+ const arr = Array.isArray(base) ? [...base] : [];
183
+ arr[head] = appendAtPath(arr[head], rest, tail);
184
+ return arr;
185
+ }
186
+ const obj = typeof base === "object" && base !== null && !Array.isArray(base) ? { ...base } : {};
187
+ obj[head] = appendAtPath(obj[head], rest, tail);
188
+ return obj;
189
+ };
190
+ var resolveAppends = (existingProps, props, appends) => {
191
+ if (!appends) return props;
192
+ const resolved = { ...props };
193
+ for (const [path, tail] of Object.entries(appends)) {
194
+ const [rootKey, ...rest] = parseAppendPath(path);
195
+ if (typeof rootKey !== "string") continue;
196
+ const base = rootKey in resolved ? resolved[rootKey] : existingProps == null ? void 0 : existingProps[rootKey];
197
+ resolved[rootKey] = appendAtPath(base, rest, tail);
198
+ }
199
+ return resolved;
200
+ };
201
+ var getSelectorForId = (state, id) => {
202
+ const node = state.indexes.nodes[id];
203
+ if (!node) return;
204
+ const zoneCompound = `${node.parentId}:${node.zone}`;
205
+ const zone = state.indexes.zones[zoneCompound];
206
+ if (!zone) return;
207
+ const index = zone.contentIds.indexOf(id);
208
+ return { zone: zoneCompound, index };
209
+ };
210
+ var getItemById = (state, id) => {
211
+ var _a2;
212
+ return (_a2 = state.indexes.nodes[id]) == null ? void 0 : _a2.data;
213
+ };
214
+ var applyArrayDefaults = (oldProps, newProps, fields) => {
215
+ const updatedProps = {
216
+ ...oldProps,
217
+ ...newProps
218
+ };
219
+ for (const fieldName in fields) {
220
+ const field = fields[fieldName];
221
+ if (field.type === "array") {
222
+ const arrayField = field;
223
+ const arrayFields = arrayField.arrayFields;
224
+ updatedProps[fieldName] = updatedProps[fieldName].map(
225
+ (item, index) => {
226
+ var _a2, _b;
227
+ const newItem = {};
228
+ const defaultValue = typeof arrayField.defaultItemProps === "function" ? arrayField.defaultItemProps(index) : arrayField.defaultItemProps;
229
+ for (const arrayFieldName in arrayFields) {
230
+ const subField = arrayFields[arrayFieldName];
231
+ if (subField.type === "slot") {
232
+ newItem[arrayFieldName] = item[arrayFieldName] ?? ((_b = (_a2 = oldProps[fieldName]) == null ? void 0 : _a2[index]) == null ? void 0 : _b[arrayFieldName]) ?? (defaultValue == null ? void 0 : defaultValue[arrayFieldName]);
233
+ } else {
234
+ newItem[arrayFieldName] = item[arrayFieldName] ?? (defaultValue == null ? void 0 : defaultValue[arrayFieldName]);
235
+ }
236
+ }
237
+ return newItem;
238
+ }
239
+ );
240
+ }
241
+ }
242
+ return updatedProps;
243
+ };
244
+ var dispatchOp2 = (operation, {
245
+ getState,
246
+ dispatchAction,
247
+ config
248
+ }) => {
249
+ var _a2, _b;
250
+ const state = getState();
251
+ try {
252
+ if (operation.op === "add") {
253
+ if (operation.zone) {
254
+ dispatchAction({
255
+ type: "insert",
256
+ destinationIndex: operation.index,
257
+ destinationZone: operation.zone,
258
+ componentType: operation.type,
259
+ id: operation.id,
260
+ recordHistory: false
261
+ });
262
+ const existing = getItemById(getState(), operation.id);
263
+ if (!existing) {
264
+ throw new Error(
265
+ `Tried to update an item that doesn't exist: ${operation.id}`
266
+ );
267
+ }
268
+ const newData = {
269
+ ...existing,
270
+ props: applyArrayDefaults(
271
+ existing.props,
272
+ operation.props,
273
+ config.components[existing.type].fields
274
+ )
275
+ };
276
+ newData.props.id ||= operation.id;
277
+ const selector = getSelectorForId(getState(), operation.id);
278
+ if (!selector) {
279
+ throw new Error(
280
+ `Tried to replace an item that doesn't exist: ${operation.id}`
281
+ );
282
+ }
283
+ dispatchAction({
284
+ type: "replace",
285
+ destinationIndex: selector.index,
286
+ destinationZone: selector.zone,
287
+ data: newData,
288
+ recordHistory: false
289
+ });
290
+ }
291
+ } else if (operation.op === "update") {
292
+ const selector = getSelectorForId(state, operation.id);
293
+ const existing = getItemById(state, operation.id);
294
+ if (!selector || !existing) {
295
+ throw new Error(
296
+ `Tried to update an item that doesn't exist: ${operation.id}`
297
+ );
298
+ }
299
+ const newData = {
300
+ ...existing,
301
+ props: applyArrayDefaults(
302
+ existing.props,
303
+ resolveAppends(
304
+ existing.props,
305
+ operation.props,
306
+ operation.appends
307
+ ),
308
+ config.components[existing.type].fields
309
+ )
310
+ };
311
+ dispatchAction({
312
+ type: "replace",
313
+ destinationIndex: selector.index,
314
+ destinationZone: selector.zone,
315
+ data: newData,
316
+ recordHistory: false
317
+ });
318
+ } else if (operation.op === "updateRoot") {
319
+ const existing = state.data.root;
320
+ const defaultProps = ((_a2 = config.root) == null ? void 0 : _a2.defaultProps) ?? {};
321
+ dispatchAction({
322
+ type: "replaceRoot",
323
+ root: {
324
+ ...existing,
325
+ props: {
326
+ ...defaultProps,
327
+ ...existing.props,
328
+ ...resolveAppends(
329
+ existing.props ?? {},
330
+ operation.props,
331
+ operation.appends
332
+ )
333
+ }
334
+ },
335
+ recordHistory: false
336
+ });
337
+ } else if (operation.op === "delete") {
338
+ const selector = getSelectorForId(state, operation.id);
339
+ if (selector) {
340
+ dispatchAction({
341
+ type: "remove",
342
+ zone: selector.zone,
343
+ index: selector.index,
344
+ recordHistory: false
345
+ });
346
+ }
347
+ } else if (operation.op === "duplicate") {
348
+ const selector = getSelectorForId(state, operation.id);
349
+ if (!selector) {
350
+ throw new Error(
351
+ `Tried to duplicate an item that doesn't exist: ${operation.id}`
352
+ );
353
+ }
354
+ dispatchAction({
355
+ type: "duplicate",
356
+ sourceZone: selector.zone,
357
+ sourceIndex: selector.index,
358
+ recordHistory: false
359
+ });
360
+ } else if (operation.op === "move") {
361
+ const selector = getSelectorForId(state, operation.id);
362
+ if (!selector) {
363
+ throw new Error(
364
+ `Tried to move an item that doesn't exist: ${operation.id}`
365
+ );
366
+ }
367
+ dispatchAction({
368
+ type: "move",
369
+ sourceZone: selector.zone,
370
+ sourceIndex: selector.index,
371
+ destinationIndex: operation.index,
372
+ destinationZone: operation.zone,
373
+ recordHistory: false
374
+ });
375
+ } else if (operation.op === "reset") {
376
+ const defaultRootProps = ((_b = config.root) == null ? void 0 : _b.defaultProps) ?? {};
377
+ dispatchAction({
378
+ type: "setData",
379
+ data: { content: [], root: defaultRootProps },
380
+ recordHistory: false
381
+ });
382
+ } else {
383
+ throw new Error(`Unknown operation: ${operation.op}`);
384
+ }
385
+ } catch (e2) {
386
+ console.error("Error applying operation, skipping...", operation, e2);
387
+ }
388
+ };
389
+ }
390
+ });
391
+
139
392
  // index.ts
140
393
  var index_exports = {};
141
394
  __export(index_exports, {
@@ -7622,228 +7875,7 @@ function ExamplePrompt({
7622
7875
 
7623
7876
  // src/components/Chat/index.tsx
7624
7877
  var import_qler = __toESM(require("qler"));
7625
-
7626
- // ../reducer/index.ts
7627
- init_react_import();
7628
- var parseAppendPath = (path) => {
7629
- const segments = [];
7630
- const pattern = /([^.[\]]+)|\[(\d+)\]/g;
7631
- let match;
7632
- while ((match = pattern.exec(path)) !== null) {
7633
- segments.push(match[2] !== void 0 ? Number(match[2]) : match[1]);
7634
- }
7635
- return segments;
7636
- };
7637
- var appendAtPath = (base, segments, tail) => {
7638
- if (segments.length === 0) {
7639
- return `${typeof base === "string" ? base : ""}${tail}`;
7640
- }
7641
- const [head, ...rest] = segments;
7642
- if (typeof head === "number") {
7643
- const arr = Array.isArray(base) ? [...base] : [];
7644
- arr[head] = appendAtPath(arr[head], rest, tail);
7645
- return arr;
7646
- }
7647
- const obj = typeof base === "object" && base !== null && !Array.isArray(base) ? { ...base } : {};
7648
- obj[head] = appendAtPath(obj[head], rest, tail);
7649
- return obj;
7650
- };
7651
- var resolveAppends = (existingProps, props, appends) => {
7652
- if (!appends) return props;
7653
- const resolved = { ...props };
7654
- for (const [path, tail] of Object.entries(appends)) {
7655
- const [rootKey, ...rest] = parseAppendPath(path);
7656
- if (typeof rootKey !== "string") continue;
7657
- const base = rootKey in resolved ? resolved[rootKey] : existingProps?.[rootKey];
7658
- resolved[rootKey] = appendAtPath(base, rest, tail);
7659
- }
7660
- return resolved;
7661
- };
7662
- var getSelectorForId = (state, id) => {
7663
- const node = state.indexes.nodes[id];
7664
- if (!node) return;
7665
- const zoneCompound = `${node.parentId}:${node.zone}`;
7666
- const zone = state.indexes.zones[zoneCompound];
7667
- if (!zone) return;
7668
- const index = zone.contentIds.indexOf(id);
7669
- return { zone: zoneCompound, index };
7670
- };
7671
- var getItemById = (state, id) => state.indexes.nodes[id]?.data;
7672
- var applyArrayDefaults = (oldProps, newProps, fields) => {
7673
- const updatedProps = {
7674
- ...oldProps,
7675
- ...newProps
7676
- };
7677
- for (const fieldName in fields) {
7678
- const field = fields[fieldName];
7679
- if (field.type === "array") {
7680
- const arrayField = field;
7681
- const arrayFields = arrayField.arrayFields;
7682
- updatedProps[fieldName] = updatedProps[fieldName].map(
7683
- (item, index) => {
7684
- const newItem = {};
7685
- const defaultValue = typeof arrayField.defaultItemProps === "function" ? arrayField.defaultItemProps(index) : arrayField.defaultItemProps;
7686
- for (const arrayFieldName in arrayFields) {
7687
- const subField = arrayFields[arrayFieldName];
7688
- if (subField.type === "slot") {
7689
- newItem[arrayFieldName] = item[arrayFieldName] ?? oldProps[fieldName]?.[index]?.[arrayFieldName] ?? defaultValue?.[arrayFieldName];
7690
- } else {
7691
- newItem[arrayFieldName] = item[arrayFieldName] ?? defaultValue?.[arrayFieldName];
7692
- }
7693
- }
7694
- return newItem;
7695
- }
7696
- );
7697
- }
7698
- }
7699
- return updatedProps;
7700
- };
7701
- var dispatchOp = (operation, {
7702
- getState,
7703
- dispatchAction,
7704
- config
7705
- }) => {
7706
- const state = getState();
7707
- try {
7708
- if (operation.op === "add") {
7709
- if (operation.zone) {
7710
- dispatchAction({
7711
- type: "insert",
7712
- destinationIndex: operation.index,
7713
- destinationZone: operation.zone,
7714
- componentType: operation.type,
7715
- id: operation.id,
7716
- recordHistory: false
7717
- });
7718
- const existing = getItemById(getState(), operation.id);
7719
- if (!existing) {
7720
- throw new Error(
7721
- `Tried to update an item that doesn't exist: ${operation.id}`
7722
- );
7723
- }
7724
- const newData = {
7725
- ...existing,
7726
- props: applyArrayDefaults(
7727
- existing.props,
7728
- operation.props,
7729
- config.components[existing.type].fields
7730
- )
7731
- };
7732
- newData.props.id ||= operation.id;
7733
- const selector = getSelectorForId(getState(), operation.id);
7734
- if (!selector) {
7735
- throw new Error(
7736
- `Tried to replace an item that doesn't exist: ${operation.id}`
7737
- );
7738
- }
7739
- dispatchAction({
7740
- type: "replace",
7741
- destinationIndex: selector.index,
7742
- destinationZone: selector.zone,
7743
- data: newData,
7744
- recordHistory: false
7745
- });
7746
- }
7747
- } else if (operation.op === "update") {
7748
- const selector = getSelectorForId(state, operation.id);
7749
- const existing = getItemById(state, operation.id);
7750
- if (!selector || !existing) {
7751
- throw new Error(
7752
- `Tried to update an item that doesn't exist: ${operation.id}`
7753
- );
7754
- }
7755
- const newData = {
7756
- ...existing,
7757
- props: applyArrayDefaults(
7758
- existing.props,
7759
- resolveAppends(
7760
- existing.props,
7761
- operation.props,
7762
- operation.appends
7763
- ),
7764
- config.components[existing.type].fields
7765
- )
7766
- };
7767
- dispatchAction({
7768
- type: "replace",
7769
- destinationIndex: selector.index,
7770
- destinationZone: selector.zone,
7771
- data: newData,
7772
- recordHistory: false
7773
- });
7774
- } else if (operation.op === "updateRoot") {
7775
- const existing = state.data.root;
7776
- const defaultProps = config.root?.defaultProps ?? {};
7777
- dispatchAction({
7778
- type: "replaceRoot",
7779
- root: {
7780
- ...existing,
7781
- props: {
7782
- ...defaultProps,
7783
- ...existing.props,
7784
- ...resolveAppends(
7785
- existing.props ?? {},
7786
- operation.props,
7787
- operation.appends
7788
- )
7789
- }
7790
- },
7791
- recordHistory: false
7792
- });
7793
- } else if (operation.op === "delete") {
7794
- const selector = getSelectorForId(state, operation.id);
7795
- if (selector) {
7796
- dispatchAction({
7797
- type: "remove",
7798
- zone: selector.zone,
7799
- index: selector.index,
7800
- recordHistory: false
7801
- });
7802
- }
7803
- } else if (operation.op === "duplicate") {
7804
- const selector = getSelectorForId(state, operation.id);
7805
- if (!selector) {
7806
- throw new Error(
7807
- `Tried to duplicate an item that doesn't exist: ${operation.id}`
7808
- );
7809
- }
7810
- dispatchAction({
7811
- type: "duplicate",
7812
- sourceZone: selector.zone,
7813
- sourceIndex: selector.index,
7814
- recordHistory: false
7815
- });
7816
- } else if (operation.op === "move") {
7817
- const selector = getSelectorForId(state, operation.id);
7818
- if (!selector) {
7819
- throw new Error(
7820
- `Tried to move an item that doesn't exist: ${operation.id}`
7821
- );
7822
- }
7823
- dispatchAction({
7824
- type: "move",
7825
- sourceZone: selector.zone,
7826
- sourceIndex: selector.index,
7827
- destinationIndex: operation.index,
7828
- destinationZone: operation.zone,
7829
- recordHistory: false
7830
- });
7831
- } else if (operation.op === "reset") {
7832
- const defaultRootProps = config.root?.defaultProps ?? {};
7833
- dispatchAction({
7834
- type: "setData",
7835
- data: { content: [], root: defaultRootProps },
7836
- recordHistory: false
7837
- });
7838
- } else {
7839
- throw new Error(`Unknown operation: ${operation.op}`);
7840
- }
7841
- } catch (e2) {
7842
- console.error("Error applying operation, skipping...", operation, e2);
7843
- }
7844
- };
7845
-
7846
- // src/components/Chat/index.tsx
7878
+ var import_reducer = __toESM(require_dist());
7847
7879
  var import_html2canvas_pro = __toESM(require("html2canvas-pro"));
7848
7880
  var import_use_debounce = require("use-debounce");
7849
7881
 
@@ -8781,9 +8813,9 @@ var attachInlineEditing = (el, {
8781
8813
  nextValue = parsed;
8782
8814
  lastValidNumberText = String(parsed);
8783
8815
  }
8784
- const { dispatch, getSelectorForId: getSelectorForId2, getItemById: getItemById2, resolveDataById } = getPuck();
8785
- const selector = getSelectorForId2(componentId);
8786
- const item = getItemById2(componentId);
8816
+ const { dispatch, getSelectorForId, getItemById, resolveDataById } = getPuck();
8817
+ const selector = getSelectorForId(componentId);
8818
+ const item = getItemById(componentId);
8787
8819
  if (!selector || !item) return;
8788
8820
  dispatch({
8789
8821
  type: "replace",
@@ -8796,8 +8828,8 @@ var attachInlineEditing = (el, {
8796
8828
  const handleClick = (e2) => {
8797
8829
  e2.preventDefault();
8798
8830
  e2.stopPropagation();
8799
- const { getSelectorForId: getSelectorForId2, dispatch } = getPuck();
8800
- const itemSelector = getSelectorForId2(componentId);
8831
+ const { getSelectorForId, dispatch } = getPuck();
8832
+ const itemSelector = getSelectorForId(componentId);
8801
8833
  if (itemSelector) {
8802
8834
  dispatch({ type: "setUi", ui: { itemSelector } });
8803
8835
  }
@@ -9563,7 +9595,7 @@ var import_jsx_runtime35 = require("react/jsx-runtime");
9563
9595
  var q2 = (0, import_qler.default)();
9564
9596
  var DEFAULT_API_VERSION = "v2";
9565
9597
  var BUILD_OP_DEBOUNCE_MS = 1e3 / 60;
9566
- var PLUGIN_AI_VERSION = true ? "0.8.0-canary.c7a937d7" : "unknown";
9598
+ var PLUGIN_AI_VERSION = true ? "0.8.0-canary.d3caecc3" : "unknown";
9567
9599
  var BENCHMARK = false;
9568
9600
  var prefixedUlid = (prefix = "") => `${prefix ? `${prefix}_` : ""}${(0, import_ulid.ulid)()}`;
9569
9601
  var getClassName18 = getClassNameFactory("Chat", styles_module_default);
@@ -9680,7 +9712,7 @@ function Chat2(props) {
9680
9712
  nextFrame(() => {
9681
9713
  const { dispatch, config, resolveDataById } = getPuck();
9682
9714
  operations.forEach((queuedOperation) => {
9683
- dispatchOp(queuedOperation, {
9715
+ (0, import_reducer.dispatchOp)(queuedOperation, {
9684
9716
  getState: () => {
9685
9717
  const { __private } = getPuck();
9686
9718
  return __private.appState;
package/dist/index.mjs CHANGED
@@ -129,6 +129,259 @@ var require_classnames = __commonJS({
129
129
  }
130
130
  });
131
131
 
132
+ // ../reducer/dist/index.js
133
+ var require_dist = __commonJS({
134
+ "../reducer/dist/index.js"(exports, module) {
135
+ "use strict";
136
+ init_react_import();
137
+ var __defProp2 = Object.defineProperty;
138
+ var __getOwnPropDesc2 = Object.getOwnPropertyDescriptor;
139
+ var __getOwnPropNames2 = Object.getOwnPropertyNames;
140
+ var __hasOwnProp2 = Object.prototype.hasOwnProperty;
141
+ var __export = (target, all) => {
142
+ for (var name in all)
143
+ __defProp2(target, name, { get: all[name], enumerable: true });
144
+ };
145
+ var __copyProps2 = (to2, from, except, desc) => {
146
+ if (from && typeof from === "object" || typeof from === "function") {
147
+ for (let key of __getOwnPropNames2(from))
148
+ if (!__hasOwnProp2.call(to2, key) && key !== except)
149
+ __defProp2(to2, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc2(from, key)) || desc.enumerable });
150
+ }
151
+ return to2;
152
+ };
153
+ var __toCommonJS = (mod) => __copyProps2(__defProp2({}, "__esModule", { value: true }), mod);
154
+ var index_exports = {};
155
+ __export(index_exports, {
156
+ dispatchOp: () => dispatchOp2,
157
+ resolveAppends: () => resolveAppends
158
+ });
159
+ module.exports = __toCommonJS(index_exports);
160
+ var parseAppendPath = (path) => {
161
+ const segments = [];
162
+ const pattern = /([^.[\]]+)|\[(\d+)\]/g;
163
+ let match;
164
+ while ((match = pattern.exec(path)) !== null) {
165
+ segments.push(match[2] !== void 0 ? Number(match[2]) : match[1]);
166
+ }
167
+ return segments;
168
+ };
169
+ var appendAtPath = (base, segments, tail) => {
170
+ if (segments.length === 0) {
171
+ return `${typeof base === "string" ? base : ""}${tail}`;
172
+ }
173
+ const [head, ...rest] = segments;
174
+ if (typeof head === "number") {
175
+ const arr = Array.isArray(base) ? [...base] : [];
176
+ arr[head] = appendAtPath(arr[head], rest, tail);
177
+ return arr;
178
+ }
179
+ const obj = typeof base === "object" && base !== null && !Array.isArray(base) ? { ...base } : {};
180
+ obj[head] = appendAtPath(obj[head], rest, tail);
181
+ return obj;
182
+ };
183
+ var resolveAppends = (existingProps, props, appends) => {
184
+ if (!appends) return props;
185
+ const resolved = { ...props };
186
+ for (const [path, tail] of Object.entries(appends)) {
187
+ const [rootKey, ...rest] = parseAppendPath(path);
188
+ if (typeof rootKey !== "string") continue;
189
+ const base = rootKey in resolved ? resolved[rootKey] : existingProps == null ? void 0 : existingProps[rootKey];
190
+ resolved[rootKey] = appendAtPath(base, rest, tail);
191
+ }
192
+ return resolved;
193
+ };
194
+ var getSelectorForId = (state, id) => {
195
+ const node = state.indexes.nodes[id];
196
+ if (!node) return;
197
+ const zoneCompound = `${node.parentId}:${node.zone}`;
198
+ const zone = state.indexes.zones[zoneCompound];
199
+ if (!zone) return;
200
+ const index = zone.contentIds.indexOf(id);
201
+ return { zone: zoneCompound, index };
202
+ };
203
+ var getItemById = (state, id) => {
204
+ var _a2;
205
+ return (_a2 = state.indexes.nodes[id]) == null ? void 0 : _a2.data;
206
+ };
207
+ var applyArrayDefaults = (oldProps, newProps, fields) => {
208
+ const updatedProps = {
209
+ ...oldProps,
210
+ ...newProps
211
+ };
212
+ for (const fieldName in fields) {
213
+ const field = fields[fieldName];
214
+ if (field.type === "array") {
215
+ const arrayField = field;
216
+ const arrayFields = arrayField.arrayFields;
217
+ updatedProps[fieldName] = updatedProps[fieldName].map(
218
+ (item, index) => {
219
+ var _a2, _b;
220
+ const newItem = {};
221
+ const defaultValue = typeof arrayField.defaultItemProps === "function" ? arrayField.defaultItemProps(index) : arrayField.defaultItemProps;
222
+ for (const arrayFieldName in arrayFields) {
223
+ const subField = arrayFields[arrayFieldName];
224
+ if (subField.type === "slot") {
225
+ newItem[arrayFieldName] = item[arrayFieldName] ?? ((_b = (_a2 = oldProps[fieldName]) == null ? void 0 : _a2[index]) == null ? void 0 : _b[arrayFieldName]) ?? (defaultValue == null ? void 0 : defaultValue[arrayFieldName]);
226
+ } else {
227
+ newItem[arrayFieldName] = item[arrayFieldName] ?? (defaultValue == null ? void 0 : defaultValue[arrayFieldName]);
228
+ }
229
+ }
230
+ return newItem;
231
+ }
232
+ );
233
+ }
234
+ }
235
+ return updatedProps;
236
+ };
237
+ var dispatchOp2 = (operation, {
238
+ getState,
239
+ dispatchAction,
240
+ config
241
+ }) => {
242
+ var _a2, _b;
243
+ const state = getState();
244
+ try {
245
+ if (operation.op === "add") {
246
+ if (operation.zone) {
247
+ dispatchAction({
248
+ type: "insert",
249
+ destinationIndex: operation.index,
250
+ destinationZone: operation.zone,
251
+ componentType: operation.type,
252
+ id: operation.id,
253
+ recordHistory: false
254
+ });
255
+ const existing = getItemById(getState(), operation.id);
256
+ if (!existing) {
257
+ throw new Error(
258
+ `Tried to update an item that doesn't exist: ${operation.id}`
259
+ );
260
+ }
261
+ const newData = {
262
+ ...existing,
263
+ props: applyArrayDefaults(
264
+ existing.props,
265
+ operation.props,
266
+ config.components[existing.type].fields
267
+ )
268
+ };
269
+ newData.props.id ||= operation.id;
270
+ const selector = getSelectorForId(getState(), operation.id);
271
+ if (!selector) {
272
+ throw new Error(
273
+ `Tried to replace an item that doesn't exist: ${operation.id}`
274
+ );
275
+ }
276
+ dispatchAction({
277
+ type: "replace",
278
+ destinationIndex: selector.index,
279
+ destinationZone: selector.zone,
280
+ data: newData,
281
+ recordHistory: false
282
+ });
283
+ }
284
+ } else if (operation.op === "update") {
285
+ const selector = getSelectorForId(state, operation.id);
286
+ const existing = getItemById(state, operation.id);
287
+ if (!selector || !existing) {
288
+ throw new Error(
289
+ `Tried to update an item that doesn't exist: ${operation.id}`
290
+ );
291
+ }
292
+ const newData = {
293
+ ...existing,
294
+ props: applyArrayDefaults(
295
+ existing.props,
296
+ resolveAppends(
297
+ existing.props,
298
+ operation.props,
299
+ operation.appends
300
+ ),
301
+ config.components[existing.type].fields
302
+ )
303
+ };
304
+ dispatchAction({
305
+ type: "replace",
306
+ destinationIndex: selector.index,
307
+ destinationZone: selector.zone,
308
+ data: newData,
309
+ recordHistory: false
310
+ });
311
+ } else if (operation.op === "updateRoot") {
312
+ const existing = state.data.root;
313
+ const defaultProps = ((_a2 = config.root) == null ? void 0 : _a2.defaultProps) ?? {};
314
+ dispatchAction({
315
+ type: "replaceRoot",
316
+ root: {
317
+ ...existing,
318
+ props: {
319
+ ...defaultProps,
320
+ ...existing.props,
321
+ ...resolveAppends(
322
+ existing.props ?? {},
323
+ operation.props,
324
+ operation.appends
325
+ )
326
+ }
327
+ },
328
+ recordHistory: false
329
+ });
330
+ } else if (operation.op === "delete") {
331
+ const selector = getSelectorForId(state, operation.id);
332
+ if (selector) {
333
+ dispatchAction({
334
+ type: "remove",
335
+ zone: selector.zone,
336
+ index: selector.index,
337
+ recordHistory: false
338
+ });
339
+ }
340
+ } else if (operation.op === "duplicate") {
341
+ const selector = getSelectorForId(state, operation.id);
342
+ if (!selector) {
343
+ throw new Error(
344
+ `Tried to duplicate an item that doesn't exist: ${operation.id}`
345
+ );
346
+ }
347
+ dispatchAction({
348
+ type: "duplicate",
349
+ sourceZone: selector.zone,
350
+ sourceIndex: selector.index,
351
+ recordHistory: false
352
+ });
353
+ } else if (operation.op === "move") {
354
+ const selector = getSelectorForId(state, operation.id);
355
+ if (!selector) {
356
+ throw new Error(
357
+ `Tried to move an item that doesn't exist: ${operation.id}`
358
+ );
359
+ }
360
+ dispatchAction({
361
+ type: "move",
362
+ sourceZone: selector.zone,
363
+ sourceIndex: selector.index,
364
+ destinationIndex: operation.index,
365
+ destinationZone: operation.zone,
366
+ recordHistory: false
367
+ });
368
+ } else if (operation.op === "reset") {
369
+ const defaultRootProps = ((_b = config.root) == null ? void 0 : _b.defaultProps) ?? {};
370
+ dispatchAction({
371
+ type: "setData",
372
+ data: { content: [], root: defaultRootProps },
373
+ recordHistory: false
374
+ });
375
+ } else {
376
+ throw new Error(`Unknown operation: ${operation.op}`);
377
+ }
378
+ } catch (e2) {
379
+ console.error("Error applying operation, skipping...", operation, e2);
380
+ }
381
+ };
382
+ }
383
+ });
384
+
132
385
  // index.ts
133
386
  init_react_import();
134
387
 
@@ -7639,229 +7892,8 @@ function ExamplePrompt({
7639
7892
  }
7640
7893
 
7641
7894
  // src/components/Chat/index.tsx
7895
+ var import_reducer = __toESM(require_dist());
7642
7896
  import qler from "qler";
7643
-
7644
- // ../reducer/index.ts
7645
- init_react_import();
7646
- var parseAppendPath = (path) => {
7647
- const segments = [];
7648
- const pattern = /([^.[\]]+)|\[(\d+)\]/g;
7649
- let match;
7650
- while ((match = pattern.exec(path)) !== null) {
7651
- segments.push(match[2] !== void 0 ? Number(match[2]) : match[1]);
7652
- }
7653
- return segments;
7654
- };
7655
- var appendAtPath = (base, segments, tail) => {
7656
- if (segments.length === 0) {
7657
- return `${typeof base === "string" ? base : ""}${tail}`;
7658
- }
7659
- const [head, ...rest] = segments;
7660
- if (typeof head === "number") {
7661
- const arr = Array.isArray(base) ? [...base] : [];
7662
- arr[head] = appendAtPath(arr[head], rest, tail);
7663
- return arr;
7664
- }
7665
- const obj = typeof base === "object" && base !== null && !Array.isArray(base) ? { ...base } : {};
7666
- obj[head] = appendAtPath(obj[head], rest, tail);
7667
- return obj;
7668
- };
7669
- var resolveAppends = (existingProps, props, appends) => {
7670
- if (!appends) return props;
7671
- const resolved = { ...props };
7672
- for (const [path, tail] of Object.entries(appends)) {
7673
- const [rootKey, ...rest] = parseAppendPath(path);
7674
- if (typeof rootKey !== "string") continue;
7675
- const base = rootKey in resolved ? resolved[rootKey] : existingProps?.[rootKey];
7676
- resolved[rootKey] = appendAtPath(base, rest, tail);
7677
- }
7678
- return resolved;
7679
- };
7680
- var getSelectorForId = (state, id) => {
7681
- const node = state.indexes.nodes[id];
7682
- if (!node) return;
7683
- const zoneCompound = `${node.parentId}:${node.zone}`;
7684
- const zone = state.indexes.zones[zoneCompound];
7685
- if (!zone) return;
7686
- const index = zone.contentIds.indexOf(id);
7687
- return { zone: zoneCompound, index };
7688
- };
7689
- var getItemById = (state, id) => state.indexes.nodes[id]?.data;
7690
- var applyArrayDefaults = (oldProps, newProps, fields) => {
7691
- const updatedProps = {
7692
- ...oldProps,
7693
- ...newProps
7694
- };
7695
- for (const fieldName in fields) {
7696
- const field = fields[fieldName];
7697
- if (field.type === "array") {
7698
- const arrayField = field;
7699
- const arrayFields = arrayField.arrayFields;
7700
- updatedProps[fieldName] = updatedProps[fieldName].map(
7701
- (item, index) => {
7702
- const newItem = {};
7703
- const defaultValue = typeof arrayField.defaultItemProps === "function" ? arrayField.defaultItemProps(index) : arrayField.defaultItemProps;
7704
- for (const arrayFieldName in arrayFields) {
7705
- const subField = arrayFields[arrayFieldName];
7706
- if (subField.type === "slot") {
7707
- newItem[arrayFieldName] = item[arrayFieldName] ?? oldProps[fieldName]?.[index]?.[arrayFieldName] ?? defaultValue?.[arrayFieldName];
7708
- } else {
7709
- newItem[arrayFieldName] = item[arrayFieldName] ?? defaultValue?.[arrayFieldName];
7710
- }
7711
- }
7712
- return newItem;
7713
- }
7714
- );
7715
- }
7716
- }
7717
- return updatedProps;
7718
- };
7719
- var dispatchOp = (operation, {
7720
- getState,
7721
- dispatchAction,
7722
- config
7723
- }) => {
7724
- const state = getState();
7725
- try {
7726
- if (operation.op === "add") {
7727
- if (operation.zone) {
7728
- dispatchAction({
7729
- type: "insert",
7730
- destinationIndex: operation.index,
7731
- destinationZone: operation.zone,
7732
- componentType: operation.type,
7733
- id: operation.id,
7734
- recordHistory: false
7735
- });
7736
- const existing = getItemById(getState(), operation.id);
7737
- if (!existing) {
7738
- throw new Error(
7739
- `Tried to update an item that doesn't exist: ${operation.id}`
7740
- );
7741
- }
7742
- const newData = {
7743
- ...existing,
7744
- props: applyArrayDefaults(
7745
- existing.props,
7746
- operation.props,
7747
- config.components[existing.type].fields
7748
- )
7749
- };
7750
- newData.props.id ||= operation.id;
7751
- const selector = getSelectorForId(getState(), operation.id);
7752
- if (!selector) {
7753
- throw new Error(
7754
- `Tried to replace an item that doesn't exist: ${operation.id}`
7755
- );
7756
- }
7757
- dispatchAction({
7758
- type: "replace",
7759
- destinationIndex: selector.index,
7760
- destinationZone: selector.zone,
7761
- data: newData,
7762
- recordHistory: false
7763
- });
7764
- }
7765
- } else if (operation.op === "update") {
7766
- const selector = getSelectorForId(state, operation.id);
7767
- const existing = getItemById(state, operation.id);
7768
- if (!selector || !existing) {
7769
- throw new Error(
7770
- `Tried to update an item that doesn't exist: ${operation.id}`
7771
- );
7772
- }
7773
- const newData = {
7774
- ...existing,
7775
- props: applyArrayDefaults(
7776
- existing.props,
7777
- resolveAppends(
7778
- existing.props,
7779
- operation.props,
7780
- operation.appends
7781
- ),
7782
- config.components[existing.type].fields
7783
- )
7784
- };
7785
- dispatchAction({
7786
- type: "replace",
7787
- destinationIndex: selector.index,
7788
- destinationZone: selector.zone,
7789
- data: newData,
7790
- recordHistory: false
7791
- });
7792
- } else if (operation.op === "updateRoot") {
7793
- const existing = state.data.root;
7794
- const defaultProps = config.root?.defaultProps ?? {};
7795
- dispatchAction({
7796
- type: "replaceRoot",
7797
- root: {
7798
- ...existing,
7799
- props: {
7800
- ...defaultProps,
7801
- ...existing.props,
7802
- ...resolveAppends(
7803
- existing.props ?? {},
7804
- operation.props,
7805
- operation.appends
7806
- )
7807
- }
7808
- },
7809
- recordHistory: false
7810
- });
7811
- } else if (operation.op === "delete") {
7812
- const selector = getSelectorForId(state, operation.id);
7813
- if (selector) {
7814
- dispatchAction({
7815
- type: "remove",
7816
- zone: selector.zone,
7817
- index: selector.index,
7818
- recordHistory: false
7819
- });
7820
- }
7821
- } else if (operation.op === "duplicate") {
7822
- const selector = getSelectorForId(state, operation.id);
7823
- if (!selector) {
7824
- throw new Error(
7825
- `Tried to duplicate an item that doesn't exist: ${operation.id}`
7826
- );
7827
- }
7828
- dispatchAction({
7829
- type: "duplicate",
7830
- sourceZone: selector.zone,
7831
- sourceIndex: selector.index,
7832
- recordHistory: false
7833
- });
7834
- } else if (operation.op === "move") {
7835
- const selector = getSelectorForId(state, operation.id);
7836
- if (!selector) {
7837
- throw new Error(
7838
- `Tried to move an item that doesn't exist: ${operation.id}`
7839
- );
7840
- }
7841
- dispatchAction({
7842
- type: "move",
7843
- sourceZone: selector.zone,
7844
- sourceIndex: selector.index,
7845
- destinationIndex: operation.index,
7846
- destinationZone: operation.zone,
7847
- recordHistory: false
7848
- });
7849
- } else if (operation.op === "reset") {
7850
- const defaultRootProps = config.root?.defaultProps ?? {};
7851
- dispatchAction({
7852
- type: "setData",
7853
- data: { content: [], root: defaultRootProps },
7854
- recordHistory: false
7855
- });
7856
- } else {
7857
- throw new Error(`Unknown operation: ${operation.op}`);
7858
- }
7859
- } catch (e2) {
7860
- console.error("Error applying operation, skipping...", operation, e2);
7861
- }
7862
- };
7863
-
7864
- // src/components/Chat/index.tsx
7865
7897
  import html2canvas from "html2canvas-pro";
7866
7898
  import { useDebouncedCallback } from "use-debounce";
7867
7899
 
@@ -8811,9 +8843,9 @@ var attachInlineEditing = (el, {
8811
8843
  nextValue = parsed;
8812
8844
  lastValidNumberText = String(parsed);
8813
8845
  }
8814
- const { dispatch, getSelectorForId: getSelectorForId2, getItemById: getItemById2, resolveDataById } = getPuck();
8815
- const selector = getSelectorForId2(componentId);
8816
- const item = getItemById2(componentId);
8846
+ const { dispatch, getSelectorForId, getItemById, resolveDataById } = getPuck();
8847
+ const selector = getSelectorForId(componentId);
8848
+ const item = getItemById(componentId);
8817
8849
  if (!selector || !item) return;
8818
8850
  dispatch({
8819
8851
  type: "replace",
@@ -8826,8 +8858,8 @@ var attachInlineEditing = (el, {
8826
8858
  const handleClick = (e2) => {
8827
8859
  e2.preventDefault();
8828
8860
  e2.stopPropagation();
8829
- const { getSelectorForId: getSelectorForId2, dispatch } = getPuck();
8830
- const itemSelector = getSelectorForId2(componentId);
8861
+ const { getSelectorForId, dispatch } = getPuck();
8862
+ const itemSelector = getSelectorForId(componentId);
8831
8863
  if (itemSelector) {
8832
8864
  dispatch({ type: "setUi", ui: { itemSelector } });
8833
8865
  }
@@ -9593,7 +9625,7 @@ import { Fragment as Fragment8, jsx as jsx35, jsxs as jsxs20 } from "react/jsx-r
9593
9625
  var q2 = qler();
9594
9626
  var DEFAULT_API_VERSION = "v2";
9595
9627
  var BUILD_OP_DEBOUNCE_MS = 1e3 / 60;
9596
- var PLUGIN_AI_VERSION = true ? "0.8.0-canary.c7a937d7" : "unknown";
9628
+ var PLUGIN_AI_VERSION = true ? "0.8.0-canary.d3caecc3" : "unknown";
9597
9629
  var BENCHMARK = false;
9598
9630
  var prefixedUlid = (prefix = "") => `${prefix ? `${prefix}_` : ""}${ulid()}`;
9599
9631
  var getClassName18 = getClassNameFactory("Chat", styles_module_default);
@@ -9710,7 +9742,7 @@ function Chat2(props) {
9710
9742
  nextFrame(() => {
9711
9743
  const { dispatch, config, resolveDataById } = getPuck();
9712
9744
  operations.forEach((queuedOperation) => {
9713
- dispatchOp(queuedOperation, {
9745
+ (0, import_reducer.dispatchOp)(queuedOperation, {
9714
9746
  getState: () => {
9715
9747
  const { __private } = getPuck();
9716
9748
  return __private.appState;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@puckeditor/plugin-ai",
3
- "version": "0.8.0-canary.c7a937d7",
3
+ "version": "0.8.0-canary.d3caecc3",
4
4
  "author": "Chris Villa <chris@puckeditor.com>",
5
5
  "repository": "puckeditor/puck",
6
6
  "bugs": "https://github.com/puckeditor/puck/issues",
@@ -55,6 +55,7 @@
55
55
  "lucide-react": "^0.452.0",
56
56
  "openai": "^5.5.1",
57
57
  "react-dom": "catalog:",
58
+ "reducer": "workspace:*",
58
59
  "tsconfig": "workspace:*",
59
60
  "tsup": "^8.2.4",
60
61
  "tsup-config": "workspace:*",