@puckeditor/plugin-ai 0.3.0-canary.41e65b36 → 0.3.0-canary.78ff547c

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/dist/index.d.mts CHANGED
@@ -1,6 +1,6 @@
1
1
  import * as react_jsx_runtime from 'react/jsx-runtime';
2
2
  import { LanguageModelUsage, UIMessage, DataUIPart, ChatStatus } from 'ai';
3
- import { PuckAction } from '@measured/puck';
3
+ import { PuckAction, Data } from '@measured/puck';
4
4
 
5
5
  type _JSONSchema = boolean | JSONSchema;
6
6
  type JSONSchema = {
@@ -160,6 +160,7 @@ type PuckDataParts = {
160
160
  input: any;
161
161
  }[];
162
162
  };
163
+ page: Data;
163
164
  };
164
165
  type PuckProviderMetadata = {
165
166
  tokenUsage?: LanguageModelUsage;
package/dist/index.d.ts CHANGED
@@ -1,6 +1,6 @@
1
1
  import * as react_jsx_runtime from 'react/jsx-runtime';
2
2
  import { LanguageModelUsage, UIMessage, DataUIPart, ChatStatus } from 'ai';
3
- import { PuckAction } from '@measured/puck';
3
+ import { PuckAction, Data } from '@measured/puck';
4
4
 
5
5
  type _JSONSchema = boolean | JSONSchema;
6
6
  type JSONSchema = {
@@ -160,6 +160,7 @@ type PuckDataParts = {
160
160
  input: any;
161
161
  }[];
162
162
  };
163
+ page: Data;
163
164
  };
164
165
  type PuckProviderMetadata = {
165
166
  tokenUsage?: LanguageModelUsage;
package/dist/index.js CHANGED
@@ -1365,6 +1365,147 @@ function ExamplePrompt({
1365
1365
 
1366
1366
  // src/components/Chat/index.tsx
1367
1367
  var import_qler = __toESM(require("qler"));
1368
+
1369
+ // ../reducer/index.ts
1370
+ init_react_import();
1371
+ var getSelectorForId = (state, id) => {
1372
+ const node = state.indexes.nodes[id];
1373
+ if (!node) return;
1374
+ const zoneCompound = `${node.parentId}:${node.zone}`;
1375
+ const index = state.indexes.zones[zoneCompound].contentIds.indexOf(id);
1376
+ return { zone: zoneCompound, index };
1377
+ };
1378
+ var getItemById = (state, id) => state.indexes.nodes[id].data;
1379
+ var dispatchOp = (operation, {
1380
+ getState,
1381
+ dispatchAction,
1382
+ config
1383
+ }) => {
1384
+ const state = getState();
1385
+ try {
1386
+ if (operation.op === "add") {
1387
+ if (operation.zone) {
1388
+ dispatchAction({
1389
+ type: "insert",
1390
+ destinationIndex: operation.index,
1391
+ destinationZone: operation.zone,
1392
+ componentType: operation.type,
1393
+ id: operation.id,
1394
+ recordHistory: false
1395
+ });
1396
+ const existing = getItemById(getState(), operation.id);
1397
+ if (!existing) {
1398
+ throw new Error(
1399
+ `Tried to update an item that doesn't exist: ${operation.id}`
1400
+ );
1401
+ }
1402
+ dispatchAction({
1403
+ type: "replace",
1404
+ destinationIndex: operation.index,
1405
+ destinationZone: operation.zone,
1406
+ data: {
1407
+ ...existing,
1408
+ props: {
1409
+ ...existing.props,
1410
+ ...operation.props
1411
+ }
1412
+ },
1413
+ recordHistory: false
1414
+ });
1415
+ }
1416
+ } else if (operation.op === "update") {
1417
+ const selector = getSelectorForId(state, operation.id);
1418
+ const existing = getItemById(state, operation.id);
1419
+ if (!selector || !existing) {
1420
+ throw new Error(
1421
+ `Tried to update an item that doesn't exist: ${operation.id}`
1422
+ );
1423
+ }
1424
+ dispatchAction({
1425
+ type: "replace",
1426
+ destinationIndex: selector.index,
1427
+ destinationZone: selector.zone,
1428
+ data: {
1429
+ ...existing,
1430
+ props: {
1431
+ ...existing.props,
1432
+ ...operation.props
1433
+ }
1434
+ },
1435
+ recordHistory: false
1436
+ });
1437
+ } else if (operation.op === "updateRoot") {
1438
+ const existing = state.data.root;
1439
+ const defaultProps = config.root?.defaultProps ?? {};
1440
+ dispatchAction({
1441
+ type: "replaceRoot",
1442
+ root: {
1443
+ ...existing,
1444
+ props: {
1445
+ ...defaultProps,
1446
+ ...existing.props,
1447
+ ...operation.props
1448
+ }
1449
+ },
1450
+ recordHistory: false
1451
+ });
1452
+ } else if (operation.op === "delete") {
1453
+ const selector = getSelectorForId(state, operation.id);
1454
+ if (!selector) {
1455
+ throw new Error(
1456
+ `Tried to delete an item that doesn't exist: ${operation.id}`
1457
+ );
1458
+ }
1459
+ dispatchAction({
1460
+ type: "remove",
1461
+ zone: selector.zone,
1462
+ index: selector.index,
1463
+ recordHistory: false
1464
+ });
1465
+ } else if (operation.op === "duplicate") {
1466
+ const selector = getSelectorForId(state, operation.id);
1467
+ if (!selector) {
1468
+ throw new Error(
1469
+ `Tried to duplicate an item that doesn't exist: ${operation.id}`
1470
+ );
1471
+ }
1472
+ dispatchAction({
1473
+ type: "duplicate",
1474
+ sourceZone: selector.zone,
1475
+ sourceIndex: selector.index,
1476
+ recordHistory: false
1477
+ });
1478
+ } else if (operation.op === "move") {
1479
+ const selector = getSelectorForId(state, operation.id);
1480
+ if (!selector) {
1481
+ throw new Error(
1482
+ `Tried to move an item that doesn't exist: ${operation.id}`
1483
+ );
1484
+ }
1485
+ dispatchAction({
1486
+ type: "move",
1487
+ sourceZone: selector.zone,
1488
+ sourceIndex: selector.index,
1489
+ destinationIndex: operation.index,
1490
+ destinationZone: operation.zone,
1491
+ recordHistory: false
1492
+ });
1493
+ } else if (operation.op === "reset") {
1494
+ const defaultRootProps = config.root?.defaultProps ?? {};
1495
+ dispatchAction({
1496
+ type: "setData",
1497
+ data: { content: [], root: defaultRootProps },
1498
+ recordHistory: false
1499
+ });
1500
+ } else {
1501
+ throw new Error(`Unknown operation: ${operation.op}`);
1502
+ }
1503
+ } catch (e) {
1504
+ console.error("Error applying operation, skipping...", operation, e);
1505
+ }
1506
+ };
1507
+
1508
+ // src/components/Chat/index.tsx
1368
1509
  var import_jsx_runtime27 = require("react/jsx-runtime");
1369
1510
  var q = (0, import_qler.default)();
1370
1511
  var BENCHMARK = false;
@@ -1401,130 +1542,15 @@ function Chat2({ chat, host = "/api/puck/chat" }) {
1401
1542
  case "data-build-op": {
1402
1543
  const data = dataPart.data;
1403
1544
  q.queue(() => {
1404
- const puckDispatch2 = getPuck().dispatch;
1405
- try {
1406
- if (data.op === "add") {
1407
- if (data.zone) {
1408
- puckDispatch2({
1409
- type: "insert",
1410
- destinationIndex: data.index,
1411
- destinationZone: data.zone,
1412
- componentType: data.type,
1413
- id: data.id,
1414
- recordHistory: false
1415
- });
1416
- const existing = getPuck().getItemById(data.id);
1417
- if (!existing) {
1418
- throw new Error(
1419
- `Tried to update an item that doesn't exist: ${data.id}`
1420
- );
1421
- }
1422
- puckDispatch2({
1423
- type: "replace",
1424
- destinationIndex: data.index,
1425
- destinationZone: data.zone,
1426
- data: {
1427
- ...existing,
1428
- props: {
1429
- ...existing.props,
1430
- ...data.props
1431
- }
1432
- },
1433
- recordHistory: false
1434
- });
1435
- }
1436
- } else if (data.op === "update") {
1437
- const selector = getPuck().getSelectorForId(data.id);
1438
- const existing = getPuck().getItemById(data.id);
1439
- if (!selector || !existing) {
1440
- throw new Error(
1441
- `Tried to update an item that doesn't exist: ${data.id}`
1442
- );
1443
- }
1444
- puckDispatch2({
1445
- type: "replace",
1446
- destinationIndex: selector.index,
1447
- destinationZone: selector.zone,
1448
- data: {
1449
- ...existing,
1450
- props: {
1451
- ...existing.props,
1452
- ...data.props
1453
- }
1454
- },
1455
- recordHistory: false
1456
- });
1457
- } else if (data.op === "updateRoot") {
1458
- const { appState, config } = getPuck();
1459
- const existing = appState.data.root;
1460
- const defaultProps = config.root?.defaultProps ?? {};
1461
- puckDispatch2({
1462
- type: "replaceRoot",
1463
- root: {
1464
- ...existing,
1465
- props: {
1466
- ...defaultProps,
1467
- ...existing.props,
1468
- ...data.props
1469
- }
1470
- },
1471
- recordHistory: false
1472
- });
1473
- } else if (data.op === "delete") {
1474
- const selector = getPuck().getSelectorForId(data.id);
1475
- if (!selector) {
1476
- throw new Error(
1477
- `Tried to delete an item that doesn't exist: ${data.id}`
1478
- );
1479
- }
1480
- puckDispatch2({
1481
- type: "remove",
1482
- zone: selector.zone,
1483
- index: selector.index,
1484
- recordHistory: false
1485
- });
1486
- } else if (data.op === "duplicate") {
1487
- const selector = getPuck().getSelectorForId(data.id);
1488
- if (!selector) {
1489
- throw new Error(
1490
- `Tried to duplicate an item that doesn't exist: ${data.id}`
1491
- );
1492
- }
1493
- puckDispatch2({
1494
- type: "duplicate",
1495
- sourceZone: selector.zone,
1496
- sourceIndex: selector.index,
1497
- recordHistory: false
1498
- });
1499
- } else if (data.op === "move") {
1500
- const selector = getPuck().getSelectorForId(data.id);
1501
- if (!selector) {
1502
- throw new Error(
1503
- `Tried to move an item that doesn't exist: ${data.id}`
1504
- );
1505
- }
1506
- puckDispatch2({
1507
- type: "move",
1508
- sourceZone: selector.zone,
1509
- sourceIndex: selector.index,
1510
- destinationIndex: data.index,
1511
- destinationZone: data.zone,
1512
- recordHistory: false
1513
- });
1514
- } else if (data.op === "reset") {
1515
- const { config } = getPuck();
1516
- const defaultRootProps = config.root?.defaultProps ?? {};
1517
- puckDispatch2({
1518
- type: "setData",
1519
- data: { content: [], root: defaultRootProps },
1520
- recordHistory: false
1521
- });
1522
- } else {
1523
- throw new Error(`Unknown operation: ${data.op}`);
1524
- }
1525
- } catch (e) {
1526
- console.error("Error applying operation, skipping...", data, e);
1527
- }
1545
+ const { dispatch, config } = getPuck();
1546
+ dispatchOp(data, {
1547
+ getState: () => {
1548
+ const { __private } = getPuck();
1549
+ return __private.appState;
1550
+ },
1551
+ dispatchAction: dispatch,
1552
+ config
1553
+ });
1528
1554
  });
1529
1555
  return;
1530
1556
  }
package/dist/index.mjs CHANGED
@@ -1363,6 +1363,147 @@ function ExamplePrompt({
1363
1363
 
1364
1364
  // src/components/Chat/index.tsx
1365
1365
  import qler from "qler";
1366
+
1367
+ // ../reducer/index.ts
1368
+ init_react_import();
1369
+ var getSelectorForId = (state, id) => {
1370
+ const node = state.indexes.nodes[id];
1371
+ if (!node) return;
1372
+ const zoneCompound = `${node.parentId}:${node.zone}`;
1373
+ const index = state.indexes.zones[zoneCompound].contentIds.indexOf(id);
1374
+ return { zone: zoneCompound, index };
1375
+ };
1376
+ var getItemById = (state, id) => state.indexes.nodes[id].data;
1377
+ var dispatchOp = (operation, {
1378
+ getState,
1379
+ dispatchAction,
1380
+ config
1381
+ }) => {
1382
+ const state = getState();
1383
+ try {
1384
+ if (operation.op === "add") {
1385
+ if (operation.zone) {
1386
+ dispatchAction({
1387
+ type: "insert",
1388
+ destinationIndex: operation.index,
1389
+ destinationZone: operation.zone,
1390
+ componentType: operation.type,
1391
+ id: operation.id,
1392
+ recordHistory: false
1393
+ });
1394
+ const existing = getItemById(getState(), operation.id);
1395
+ if (!existing) {
1396
+ throw new Error(
1397
+ `Tried to update an item that doesn't exist: ${operation.id}`
1398
+ );
1399
+ }
1400
+ dispatchAction({
1401
+ type: "replace",
1402
+ destinationIndex: operation.index,
1403
+ destinationZone: operation.zone,
1404
+ data: {
1405
+ ...existing,
1406
+ props: {
1407
+ ...existing.props,
1408
+ ...operation.props
1409
+ }
1410
+ },
1411
+ recordHistory: false
1412
+ });
1413
+ }
1414
+ } else if (operation.op === "update") {
1415
+ const selector = getSelectorForId(state, operation.id);
1416
+ const existing = getItemById(state, operation.id);
1417
+ if (!selector || !existing) {
1418
+ throw new Error(
1419
+ `Tried to update an item that doesn't exist: ${operation.id}`
1420
+ );
1421
+ }
1422
+ dispatchAction({
1423
+ type: "replace",
1424
+ destinationIndex: selector.index,
1425
+ destinationZone: selector.zone,
1426
+ data: {
1427
+ ...existing,
1428
+ props: {
1429
+ ...existing.props,
1430
+ ...operation.props
1431
+ }
1432
+ },
1433
+ recordHistory: false
1434
+ });
1435
+ } else if (operation.op === "updateRoot") {
1436
+ const existing = state.data.root;
1437
+ const defaultProps = config.root?.defaultProps ?? {};
1438
+ dispatchAction({
1439
+ type: "replaceRoot",
1440
+ root: {
1441
+ ...existing,
1442
+ props: {
1443
+ ...defaultProps,
1444
+ ...existing.props,
1445
+ ...operation.props
1446
+ }
1447
+ },
1448
+ recordHistory: false
1449
+ });
1450
+ } else if (operation.op === "delete") {
1451
+ const selector = getSelectorForId(state, operation.id);
1452
+ if (!selector) {
1453
+ throw new Error(
1454
+ `Tried to delete an item that doesn't exist: ${operation.id}`
1455
+ );
1456
+ }
1457
+ dispatchAction({
1458
+ type: "remove",
1459
+ zone: selector.zone,
1460
+ index: selector.index,
1461
+ recordHistory: false
1462
+ });
1463
+ } else if (operation.op === "duplicate") {
1464
+ const selector = getSelectorForId(state, operation.id);
1465
+ if (!selector) {
1466
+ throw new Error(
1467
+ `Tried to duplicate an item that doesn't exist: ${operation.id}`
1468
+ );
1469
+ }
1470
+ dispatchAction({
1471
+ type: "duplicate",
1472
+ sourceZone: selector.zone,
1473
+ sourceIndex: selector.index,
1474
+ recordHistory: false
1475
+ });
1476
+ } else if (operation.op === "move") {
1477
+ const selector = getSelectorForId(state, operation.id);
1478
+ if (!selector) {
1479
+ throw new Error(
1480
+ `Tried to move an item that doesn't exist: ${operation.id}`
1481
+ );
1482
+ }
1483
+ dispatchAction({
1484
+ type: "move",
1485
+ sourceZone: selector.zone,
1486
+ sourceIndex: selector.index,
1487
+ destinationIndex: operation.index,
1488
+ destinationZone: operation.zone,
1489
+ recordHistory: false
1490
+ });
1491
+ } else if (operation.op === "reset") {
1492
+ const defaultRootProps = config.root?.defaultProps ?? {};
1493
+ dispatchAction({
1494
+ type: "setData",
1495
+ data: { content: [], root: defaultRootProps },
1496
+ recordHistory: false
1497
+ });
1498
+ } else {
1499
+ throw new Error(`Unknown operation: ${operation.op}`);
1500
+ }
1501
+ } catch (e) {
1502
+ console.error("Error applying operation, skipping...", operation, e);
1503
+ }
1504
+ };
1505
+
1506
+ // src/components/Chat/index.tsx
1366
1507
  import { jsx as jsx27, jsxs as jsxs14 } from "react/jsx-runtime";
1367
1508
  var q = qler();
1368
1509
  var BENCHMARK = false;
@@ -1399,130 +1540,15 @@ function Chat2({ chat, host = "/api/puck/chat" }) {
1399
1540
  case "data-build-op": {
1400
1541
  const data = dataPart.data;
1401
1542
  q.queue(() => {
1402
- const puckDispatch2 = getPuck().dispatch;
1403
- try {
1404
- if (data.op === "add") {
1405
- if (data.zone) {
1406
- puckDispatch2({
1407
- type: "insert",
1408
- destinationIndex: data.index,
1409
- destinationZone: data.zone,
1410
- componentType: data.type,
1411
- id: data.id,
1412
- recordHistory: false
1413
- });
1414
- const existing = getPuck().getItemById(data.id);
1415
- if (!existing) {
1416
- throw new Error(
1417
- `Tried to update an item that doesn't exist: ${data.id}`
1418
- );
1419
- }
1420
- puckDispatch2({
1421
- type: "replace",
1422
- destinationIndex: data.index,
1423
- destinationZone: data.zone,
1424
- data: {
1425
- ...existing,
1426
- props: {
1427
- ...existing.props,
1428
- ...data.props
1429
- }
1430
- },
1431
- recordHistory: false
1432
- });
1433
- }
1434
- } else if (data.op === "update") {
1435
- const selector = getPuck().getSelectorForId(data.id);
1436
- const existing = getPuck().getItemById(data.id);
1437
- if (!selector || !existing) {
1438
- throw new Error(
1439
- `Tried to update an item that doesn't exist: ${data.id}`
1440
- );
1441
- }
1442
- puckDispatch2({
1443
- type: "replace",
1444
- destinationIndex: selector.index,
1445
- destinationZone: selector.zone,
1446
- data: {
1447
- ...existing,
1448
- props: {
1449
- ...existing.props,
1450
- ...data.props
1451
- }
1452
- },
1453
- recordHistory: false
1454
- });
1455
- } else if (data.op === "updateRoot") {
1456
- const { appState, config } = getPuck();
1457
- const existing = appState.data.root;
1458
- const defaultProps = config.root?.defaultProps ?? {};
1459
- puckDispatch2({
1460
- type: "replaceRoot",
1461
- root: {
1462
- ...existing,
1463
- props: {
1464
- ...defaultProps,
1465
- ...existing.props,
1466
- ...data.props
1467
- }
1468
- },
1469
- recordHistory: false
1470
- });
1471
- } else if (data.op === "delete") {
1472
- const selector = getPuck().getSelectorForId(data.id);
1473
- if (!selector) {
1474
- throw new Error(
1475
- `Tried to delete an item that doesn't exist: ${data.id}`
1476
- );
1477
- }
1478
- puckDispatch2({
1479
- type: "remove",
1480
- zone: selector.zone,
1481
- index: selector.index,
1482
- recordHistory: false
1483
- });
1484
- } else if (data.op === "duplicate") {
1485
- const selector = getPuck().getSelectorForId(data.id);
1486
- if (!selector) {
1487
- throw new Error(
1488
- `Tried to duplicate an item that doesn't exist: ${data.id}`
1489
- );
1490
- }
1491
- puckDispatch2({
1492
- type: "duplicate",
1493
- sourceZone: selector.zone,
1494
- sourceIndex: selector.index,
1495
- recordHistory: false
1496
- });
1497
- } else if (data.op === "move") {
1498
- const selector = getPuck().getSelectorForId(data.id);
1499
- if (!selector) {
1500
- throw new Error(
1501
- `Tried to move an item that doesn't exist: ${data.id}`
1502
- );
1503
- }
1504
- puckDispatch2({
1505
- type: "move",
1506
- sourceZone: selector.zone,
1507
- sourceIndex: selector.index,
1508
- destinationIndex: data.index,
1509
- destinationZone: data.zone,
1510
- recordHistory: false
1511
- });
1512
- } else if (data.op === "reset") {
1513
- const { config } = getPuck();
1514
- const defaultRootProps = config.root?.defaultProps ?? {};
1515
- puckDispatch2({
1516
- type: "setData",
1517
- data: { content: [], root: defaultRootProps },
1518
- recordHistory: false
1519
- });
1520
- } else {
1521
- throw new Error(`Unknown operation: ${data.op}`);
1522
- }
1523
- } catch (e) {
1524
- console.error("Error applying operation, skipping...", data, e);
1525
- }
1543
+ const { dispatch, config } = getPuck();
1544
+ dispatchOp(data, {
1545
+ getState: () => {
1546
+ const { __private } = getPuck();
1547
+ return __private.appState;
1548
+ },
1549
+ dispatchAction: dispatch,
1550
+ config
1551
+ });
1526
1552
  });
1527
1553
  return;
1528
1554
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@puckeditor/plugin-ai",
3
- "version": "0.3.0-canary.41e65b36",
3
+ "version": "0.3.0-canary.78ff547c",
4
4
  "author": "Chris Villa <chris@puckeditor.com>",
5
5
  "repository": "puckeditor/puck",
6
6
  "bugs": "https://github.com/puckeditor/puck/issues",
@@ -36,7 +36,7 @@
36
36
  },
37
37
  "devDependencies": {
38
38
  "@ai-sdk/react": "^2.0.29",
39
- "@measured/puck": "0.21.0-canary.8416d520",
39
+ "@measured/puck": "0.21.0-canary.c0db75c1",
40
40
  "@puckeditor/ai-types": "workspace:*",
41
41
  "@puckeditor/platform-types": "workspace:*",
42
42
  "@types/jest": "^30.0.0",
@@ -53,7 +53,7 @@
53
53
  "typescript": "^5.5.4"
54
54
  },
55
55
  "peerDependencies": {
56
- "@measured/puck": "0.21.0-canary.8416d520",
56
+ "@measured/puck": "0.21.0-canary.c0db75c1",
57
57
  "react": "^18.0.0 || ^19.0.0"
58
58
  }
59
59
  }