@puckeditor/plugin-ai 0.8.3-canary.89ddeea1 → 0.8.3-canary.8cefba87

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.js CHANGED
@@ -4392,12 +4392,28 @@ var waitForIframeReady = async (root, imageTimeout = 1e4) => {
4392
4392
  await waitForNextPaint(document2);
4393
4393
  };
4394
4394
 
4395
+ // src/lib/client-tool-response.ts
4396
+ init_react_import();
4397
+ async function postClientToolResponse(host, body) {
4398
+ const response = await fetch(`${host.replace(/\/+$/, "")}/tool`, {
4399
+ method: "POST",
4400
+ headers: { "Content-Type": "application/json" },
4401
+ body: JSON.stringify(body)
4402
+ });
4403
+ if (!response.ok) {
4404
+ throw new Error(
4405
+ `Client tool endpoint returned ${response.status} ${response.statusText}`.trim()
4406
+ );
4407
+ }
4408
+ }
4409
+
4395
4410
  // src/components/Chat/index.tsx
4396
4411
  var import_jsx_runtime35 = require("react/jsx-runtime");
4397
4412
  var q = (0, import_qler.default)();
4398
4413
  var DEFAULT_API_VERSION = "v2";
4399
4414
  var BUILD_OP_DEBOUNCE_MS = 1e3 / 60;
4400
- var PLUGIN_AI_VERSION = true ? "0.8.3-canary.89ddeea1" : "unknown";
4415
+ var CLIENT_TOOL_ENDPOINT_UNAVAILABLE_ERROR_MESSAGE = "Unable to verify the page data. Please enable the `/chat/tool` endpoint on the server and try again.";
4416
+ var PLUGIN_AI_VERSION = true ? "0.8.3-canary.8cefba87" : "unknown";
4401
4417
  var BENCHMARK = false;
4402
4418
  var prefixedUlid = (prefix = "") => `${prefix ? `${prefix}_` : ""}${(0, import_ulid.ulid)()}`;
4403
4419
  var getClassName18 = getClassNameFactory("Chat", styles_module_default);
@@ -4435,6 +4451,8 @@ function Chat2(props) {
4435
4451
  }, [puckData, getPuck]);
4436
4452
  const [error, setError] = (0, import_react27.useState)();
4437
4453
  const [userError, setUserError] = (0, import_react27.useState)();
4454
+ const clientToolEndpointUnavailableRef = (0, import_react27.useRef)(false);
4455
+ const stopChatRef = (0, import_react27.useRef)(null);
4438
4456
  const [composerAttachments, setComposerAttachments] = (0, import_react27.useState)([]);
4439
4457
  const removedAttachmentClientIdsRef = (0, import_react27.useRef)(/* @__PURE__ */ new Set());
4440
4458
  const uploadControllersRef = (0, import_react27.useRef)(/* @__PURE__ */ new Map());
@@ -4551,6 +4569,40 @@ function Chat2(props) {
4551
4569
  },
4552
4570
  [debouncedFlushBuildOps]
4553
4571
  );
4572
+ const handleClientToolEndpointUnavailable = (0, import_react27.useCallback)(() => {
4573
+ if (clientToolEndpointUnavailableRef.current) return;
4574
+ clientToolEndpointUnavailableRef.current = true;
4575
+ setError(CLIENT_TOOL_ENDPOINT_UNAVAILABLE_ERROR_MESSAGE);
4576
+ console.error(CLIENT_TOOL_ENDPOINT_UNAVAILABLE_ERROR_MESSAGE);
4577
+ setToolStatus((current) => {
4578
+ let changed = false;
4579
+ const next = {};
4580
+ for (const [id, toolState] of Object.entries(current)) {
4581
+ if (toolState.loading) {
4582
+ changed = true;
4583
+ next[id] = {
4584
+ ...toolState,
4585
+ loading: false,
4586
+ label: "Verification unavailable"
4587
+ };
4588
+ } else {
4589
+ next[id] = toolState;
4590
+ }
4591
+ }
4592
+ return changed ? next : current;
4593
+ });
4594
+ stopChatRef.current?.();
4595
+ }, []);
4596
+ const sendClientToolResponse = (0, import_react27.useCallback)(
4597
+ async (body) => {
4598
+ try {
4599
+ await postClientToolResponse(host, body);
4600
+ } catch {
4601
+ handleClientToolEndpointUnavailable();
4602
+ }
4603
+ },
4604
+ [handleClientToolEndpointUnavailable, host]
4605
+ );
4554
4606
  const processData = (0, import_react27.useCallback)(
4555
4607
  (dataPart) => {
4556
4608
  lastStreamActivityRef.current = Date.now();
@@ -4584,13 +4636,9 @@ function Chat2(props) {
4584
4636
  debouncedFlushBuildOps.flush();
4585
4637
  q.wait().then(() => {
4586
4638
  const { appState } = getPuck();
4587
- return fetch(`${host}/tool`, {
4588
- method: "POST",
4589
- headers: { "Content-Type": "application/json" },
4590
- body: JSON.stringify({
4591
- id: requestId,
4592
- responses: [{ action, output: appState.data }]
4593
- })
4639
+ return sendClientToolResponse({
4640
+ id: requestId,
4641
+ responses: [{ action, output: appState.data }]
4594
4642
  });
4595
4643
  }).catch((e) => {
4596
4644
  console.error("Failed to respond to client request:", e);
@@ -4632,13 +4680,9 @@ function Chat2(props) {
4632
4680
  `Upload failed with status ${uploadResponse.status}`
4633
4681
  );
4634
4682
  }
4635
- return fetch(`${host}/tool`, {
4636
- method: "POST",
4637
- headers: { "Content-Type": "application/json" },
4638
- body: JSON.stringify({
4639
- id: requestId,
4640
- responses: [{ action, output: { sizeBytes: blob.size } }]
4641
- })
4683
+ return sendClientToolResponse({
4684
+ id: requestId,
4685
+ responses: [{ action, output: { sizeBytes: blob.size } }]
4642
4686
  });
4643
4687
  }).catch((e) => {
4644
4688
  console.error("Failed to respond to client request:", e);
@@ -4685,6 +4729,7 @@ function Chat2(props) {
4685
4729
  getPuck,
4686
4730
  puckDispatch,
4687
4731
  queueBuildOp,
4732
+ sendClientToolResponse,
4688
4733
  uploadScreenshot
4689
4734
  ]
4690
4735
  );
@@ -4754,12 +4799,14 @@ function Chat2(props) {
4754
4799
  if (BENCHMARK) {
4755
4800
  console.timeEnd("chat");
4756
4801
  }
4757
- setError(e.message);
4802
+ if (!clientToolEndpointUnavailableRef.current) {
4803
+ setError(e.message);
4804
+ }
4758
4805
  },
4759
4806
  onFinish: (options) => {
4760
4807
  debouncedFlushBuildOps.flush();
4761
4808
  q.wait().then(() => {
4762
- if (!options.isAbort) {
4809
+ if (!options.isAbort || clientToolEndpointUnavailableRef.current) {
4763
4810
  puckDispatch({
4764
4811
  type: "set",
4765
4812
  state: getPuck().appState,
@@ -4774,12 +4821,13 @@ function Chat2(props) {
4774
4821
  });
4775
4822
  }
4776
4823
  });
4777
- (0, import_react27.useEffect)(
4778
- () => () => {
4824
+ (0, import_react27.useEffect)(() => {
4825
+ stopChatRef.current = stop;
4826
+ return () => {
4827
+ stopChatRef.current = null;
4779
4828
  stop();
4780
- },
4781
- [stop]
4782
- );
4829
+ };
4830
+ }, [stop]);
4783
4831
  const statusRef = (0, import_react27.useRef)(status);
4784
4832
  (0, import_react27.useEffect)(() => {
4785
4833
  statusRef.current = status;
@@ -4880,6 +4928,8 @@ function Chat2(props) {
4880
4928
  if (!text && readyAttachments.length === 0) {
4881
4929
  return false;
4882
4930
  }
4931
+ clientToolEndpointUnavailableRef.current = false;
4932
+ setError("");
4883
4933
  setUserError("");
4884
4934
  if (chat?.onSubmit) {
4885
4935
  try {
@@ -5119,6 +5169,7 @@ function Chat2(props) {
5119
5169
  ),
5120
5170
  error,
5121
5171
  handleRetry: () => {
5172
+ clientToolEndpointUnavailableRef.current = false;
5122
5173
  setError("");
5123
5174
  regenerate();
5124
5175
  },
package/dist/index.mjs CHANGED
@@ -4424,12 +4424,28 @@ var waitForIframeReady = async (root, imageTimeout = 1e4) => {
4424
4424
  await waitForNextPaint(document2);
4425
4425
  };
4426
4426
 
4427
+ // src/lib/client-tool-response.ts
4428
+ init_react_import();
4429
+ async function postClientToolResponse(host, body) {
4430
+ const response = await fetch(`${host.replace(/\/+$/, "")}/tool`, {
4431
+ method: "POST",
4432
+ headers: { "Content-Type": "application/json" },
4433
+ body: JSON.stringify(body)
4434
+ });
4435
+ if (!response.ok) {
4436
+ throw new Error(
4437
+ `Client tool endpoint returned ${response.status} ${response.statusText}`.trim()
4438
+ );
4439
+ }
4440
+ }
4441
+
4427
4442
  // src/components/Chat/index.tsx
4428
4443
  import { Fragment as Fragment9, jsx as jsx35, jsxs as jsxs20 } from "react/jsx-runtime";
4429
4444
  var q = qler();
4430
4445
  var DEFAULT_API_VERSION = "v2";
4431
4446
  var BUILD_OP_DEBOUNCE_MS = 1e3 / 60;
4432
- var PLUGIN_AI_VERSION = true ? "0.8.3-canary.89ddeea1" : "unknown";
4447
+ var CLIENT_TOOL_ENDPOINT_UNAVAILABLE_ERROR_MESSAGE = "Unable to verify the page data. Please enable the `/chat/tool` endpoint on the server and try again.";
4448
+ var PLUGIN_AI_VERSION = true ? "0.8.3-canary.8cefba87" : "unknown";
4433
4449
  var BENCHMARK = false;
4434
4450
  var prefixedUlid = (prefix = "") => `${prefix ? `${prefix}_` : ""}${ulid()}`;
4435
4451
  var getClassName18 = getClassNameFactory("Chat", styles_module_default);
@@ -4467,6 +4483,8 @@ function Chat2(props) {
4467
4483
  }, [puckData, getPuck]);
4468
4484
  const [error, setError] = useState16();
4469
4485
  const [userError, setUserError] = useState16();
4486
+ const clientToolEndpointUnavailableRef = useRef13(false);
4487
+ const stopChatRef = useRef13(null);
4470
4488
  const [composerAttachments, setComposerAttachments] = useState16([]);
4471
4489
  const removedAttachmentClientIdsRef = useRef13(/* @__PURE__ */ new Set());
4472
4490
  const uploadControllersRef = useRef13(/* @__PURE__ */ new Map());
@@ -4583,6 +4601,40 @@ function Chat2(props) {
4583
4601
  },
4584
4602
  [debouncedFlushBuildOps]
4585
4603
  );
4604
+ const handleClientToolEndpointUnavailable = useCallback6(() => {
4605
+ if (clientToolEndpointUnavailableRef.current) return;
4606
+ clientToolEndpointUnavailableRef.current = true;
4607
+ setError(CLIENT_TOOL_ENDPOINT_UNAVAILABLE_ERROR_MESSAGE);
4608
+ console.error(CLIENT_TOOL_ENDPOINT_UNAVAILABLE_ERROR_MESSAGE);
4609
+ setToolStatus((current) => {
4610
+ let changed = false;
4611
+ const next = {};
4612
+ for (const [id, toolState] of Object.entries(current)) {
4613
+ if (toolState.loading) {
4614
+ changed = true;
4615
+ next[id] = {
4616
+ ...toolState,
4617
+ loading: false,
4618
+ label: "Verification unavailable"
4619
+ };
4620
+ } else {
4621
+ next[id] = toolState;
4622
+ }
4623
+ }
4624
+ return changed ? next : current;
4625
+ });
4626
+ stopChatRef.current?.();
4627
+ }, []);
4628
+ const sendClientToolResponse = useCallback6(
4629
+ async (body) => {
4630
+ try {
4631
+ await postClientToolResponse(host, body);
4632
+ } catch {
4633
+ handleClientToolEndpointUnavailable();
4634
+ }
4635
+ },
4636
+ [handleClientToolEndpointUnavailable, host]
4637
+ );
4586
4638
  const processData = useCallback6(
4587
4639
  (dataPart) => {
4588
4640
  lastStreamActivityRef.current = Date.now();
@@ -4616,13 +4668,9 @@ function Chat2(props) {
4616
4668
  debouncedFlushBuildOps.flush();
4617
4669
  q.wait().then(() => {
4618
4670
  const { appState } = getPuck();
4619
- return fetch(`${host}/tool`, {
4620
- method: "POST",
4621
- headers: { "Content-Type": "application/json" },
4622
- body: JSON.stringify({
4623
- id: requestId,
4624
- responses: [{ action, output: appState.data }]
4625
- })
4671
+ return sendClientToolResponse({
4672
+ id: requestId,
4673
+ responses: [{ action, output: appState.data }]
4626
4674
  });
4627
4675
  }).catch((e) => {
4628
4676
  console.error("Failed to respond to client request:", e);
@@ -4664,13 +4712,9 @@ function Chat2(props) {
4664
4712
  `Upload failed with status ${uploadResponse.status}`
4665
4713
  );
4666
4714
  }
4667
- return fetch(`${host}/tool`, {
4668
- method: "POST",
4669
- headers: { "Content-Type": "application/json" },
4670
- body: JSON.stringify({
4671
- id: requestId,
4672
- responses: [{ action, output: { sizeBytes: blob.size } }]
4673
- })
4715
+ return sendClientToolResponse({
4716
+ id: requestId,
4717
+ responses: [{ action, output: { sizeBytes: blob.size } }]
4674
4718
  });
4675
4719
  }).catch((e) => {
4676
4720
  console.error("Failed to respond to client request:", e);
@@ -4717,6 +4761,7 @@ function Chat2(props) {
4717
4761
  getPuck,
4718
4762
  puckDispatch,
4719
4763
  queueBuildOp,
4764
+ sendClientToolResponse,
4720
4765
  uploadScreenshot
4721
4766
  ]
4722
4767
  );
@@ -4786,12 +4831,14 @@ function Chat2(props) {
4786
4831
  if (BENCHMARK) {
4787
4832
  console.timeEnd("chat");
4788
4833
  }
4789
- setError(e.message);
4834
+ if (!clientToolEndpointUnavailableRef.current) {
4835
+ setError(e.message);
4836
+ }
4790
4837
  },
4791
4838
  onFinish: (options) => {
4792
4839
  debouncedFlushBuildOps.flush();
4793
4840
  q.wait().then(() => {
4794
- if (!options.isAbort) {
4841
+ if (!options.isAbort || clientToolEndpointUnavailableRef.current) {
4795
4842
  puckDispatch({
4796
4843
  type: "set",
4797
4844
  state: getPuck().appState,
@@ -4806,12 +4853,13 @@ function Chat2(props) {
4806
4853
  });
4807
4854
  }
4808
4855
  });
4809
- useEffect13(
4810
- () => () => {
4856
+ useEffect13(() => {
4857
+ stopChatRef.current = stop;
4858
+ return () => {
4859
+ stopChatRef.current = null;
4811
4860
  stop();
4812
- },
4813
- [stop]
4814
- );
4861
+ };
4862
+ }, [stop]);
4815
4863
  const statusRef = useRef13(status);
4816
4864
  useEffect13(() => {
4817
4865
  statusRef.current = status;
@@ -4912,6 +4960,8 @@ function Chat2(props) {
4912
4960
  if (!text && readyAttachments.length === 0) {
4913
4961
  return false;
4914
4962
  }
4963
+ clientToolEndpointUnavailableRef.current = false;
4964
+ setError("");
4915
4965
  setUserError("");
4916
4966
  if (chat?.onSubmit) {
4917
4967
  try {
@@ -5151,6 +5201,7 @@ function Chat2(props) {
5151
5201
  ),
5152
5202
  error,
5153
5203
  handleRetry: () => {
5204
+ clientToolEndpointUnavailableRef.current = false;
5154
5205
  setError("");
5155
5206
  regenerate();
5156
5207
  },
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@puckeditor/plugin-ai",
3
- "version": "0.8.3-canary.89ddeea1",
3
+ "version": "0.8.3-canary.8cefba87",
4
4
  "author": "Chris Villa <chris@puckeditor.com>",
5
5
  "repository": {
6
6
  "type": "git",
@@ -52,12 +52,12 @@
52
52
  "tsup": "^8.2.4",
53
53
  "typescript": "^5.5.4",
54
54
  "vitest": "^4.1.0",
55
- "@puckeditor/ai-types": "0.8.3-canary.89ddeea1",
56
- "@puckeditor/platform-types": "0.8.3-canary.89ddeea1",
57
- "eslint-config-custom": "0.8.3-canary.89ddeea1",
58
- "reducer": "0.8.3-canary.89ddeea1",
59
- "tsconfig": "0.8.3-canary.89ddeea1",
60
- "tsup-config": "0.8.3-canary.89ddeea1"
55
+ "@puckeditor/ai-types": "0.8.3-canary.8cefba87",
56
+ "@puckeditor/platform-types": "0.8.3-canary.8cefba87",
57
+ "eslint-config-custom": "0.8.3-canary.8cefba87",
58
+ "reducer": "0.8.3-canary.8cefba87",
59
+ "tsconfig": "0.8.3-canary.8cefba87",
60
+ "tsup-config": "0.8.3-canary.8cefba87"
61
61
  },
62
62
  "peerDependencies": {
63
63
  "@puckeditor/core": "^0",