@puckeditor/plugin-ai 0.8.3-canary.4e864c39 → 0.8.3-canary.89ddeea1

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
@@ -3420,6 +3420,29 @@ var syncFieldValues = (entry, fields, props) => {
3420
3420
  });
3421
3421
  }
3422
3422
  };
3423
+ var claimedHostStyles = /* @__PURE__ */ new WeakMap();
3424
+ var claimHostStyles = (el, applyWhiteSpace) => {
3425
+ if (!claimedHostStyles.has(el)) {
3426
+ claimedHostStyles.set(el, {
3427
+ cursor: el.style.cursor,
3428
+ whiteSpace: applyWhiteSpace ? el.style.whiteSpace : null
3429
+ });
3430
+ }
3431
+ applyHostStyles(el);
3432
+ };
3433
+ var applyHostStyles = (el) => {
3434
+ const claimed = claimedHostStyles.get(el);
3435
+ if (!claimed) return;
3436
+ el.style.cursor = "text";
3437
+ if (claimed.whiteSpace !== null) el.style.whiteSpace = "pre-wrap";
3438
+ };
3439
+ var releaseHostStyles = (el) => {
3440
+ const claimed = claimedHostStyles.get(el);
3441
+ if (!claimed) return;
3442
+ el.style.cursor = claimed.cursor;
3443
+ if (claimed.whiteSpace !== null) el.style.whiteSpace = claimed.whiteSpace;
3444
+ claimedHostStyles.delete(el);
3445
+ };
3423
3446
  var attachInlineEditing = (el, {
3424
3447
  componentId,
3425
3448
  propPath,
@@ -3432,8 +3455,7 @@ var attachInlineEditing = (el, {
3432
3455
  const syncEditable = () => {
3433
3456
  el.contentEditable = hovering || focused ? mode === "richtext" ? "true" : "plaintext-only" : "false";
3434
3457
  };
3435
- const previousCursor = el.style.cursor;
3436
- el.style.cursor = "text";
3458
+ claimHostStyles(el, mode !== "richtext");
3437
3459
  syncEditable();
3438
3460
  let lastValidNumberText = el.innerText.replaceAll(/\n/gm, "");
3439
3461
  const handleInput = () => {
@@ -3511,7 +3533,7 @@ var attachInlineEditing = (el, {
3511
3533
  el.removeEventListener("mouseout", handleMouseOut, true);
3512
3534
  el.removeEventListener("focus", handleFocus);
3513
3535
  el.removeEventListener("blur", handleBlur);
3514
- el.style.cursor = previousCursor;
3536
+ releaseHostStyles(el);
3515
3537
  el.removeAttribute("contenteditable");
3516
3538
  cleanupPortal?.();
3517
3539
  };
@@ -3577,7 +3599,7 @@ var wireArrayItemEditing = ({
3577
3599
  const mode = inlineEditModeFor(field);
3578
3600
  const registration = wiring.get(target);
3579
3601
  if (registration?.propPath === propPath && registration.mode === mode) {
3580
- target.style.cursor = "text";
3602
+ applyHostStyles(target);
3581
3603
  continue;
3582
3604
  }
3583
3605
  registration?.cleanup();
@@ -3903,7 +3925,7 @@ function createDesignComponentConfig(registration) {
3903
3925
  morphed.current = { el: entryEl, html, attrSig };
3904
3926
  morphHtml(entryEl, html, attrFields);
3905
3927
  for (const el of fieldWiring.current.keys()) {
3906
- if (el.isConnected) el.style.cursor = "text";
3928
+ if (el.isConnected) applyHostStyles(el);
3907
3929
  }
3908
3930
  }
3909
3931
  syncFieldValues(entryEl, annotations.fields, props);
@@ -4062,9 +4084,11 @@ function getDynamicConfigGlobals(dynamicConfig) {
4062
4084
  ...typeof script === "string" ? { script } : {}
4063
4085
  };
4064
4086
  }
4087
+ var INLINE_EDIT_RESET = ":where([data-puck-design] [contenteditable]){overflow-wrap:inherit;line-break:inherit;-webkit-nbsp-mode:inherit}";
4065
4088
  function DesignGlobals({
4066
4089
  styles,
4067
- script
4090
+ script,
4091
+ isEditing
4068
4092
  }) {
4069
4093
  const scriptRef = (0, import_react25.useRef)(null);
4070
4094
  const hostRef = (0, import_react25.useRef)(null);
@@ -4082,6 +4106,7 @@ function DesignGlobals({
4082
4106
  };
4083
4107
  }, [script]);
4084
4108
  return /* @__PURE__ */ (0, import_jsx_runtime33.jsxs)(import_jsx_runtime33.Fragment, { children: [
4109
+ isEditing && /* @__PURE__ */ (0, import_jsx_runtime33.jsx)("style", { children: INLINE_EDIT_RESET }),
4085
4110
  styles && /* @__PURE__ */ (0, import_jsx_runtime33.jsx)("style", { children: styles }),
4086
4111
  script && /* @__PURE__ */ (0, import_jsx_runtime33.jsx)("div", { ref: hostRef, style: { display: "none" } })
4087
4112
  ] });
@@ -4094,7 +4119,14 @@ function withDesignGlobals(root) {
4094
4119
  const render = (props) => {
4095
4120
  const { styles, script } = getDynamicConfigGlobals(props._dynamicConfig);
4096
4121
  return /* @__PURE__ */ (0, import_jsx_runtime33.jsxs)(import_jsx_runtime33.Fragment, { children: [
4097
- /* @__PURE__ */ (0, import_jsx_runtime33.jsx)(DesignGlobals, { styles, script }),
4122
+ /* @__PURE__ */ (0, import_jsx_runtime33.jsx)(
4123
+ DesignGlobals,
4124
+ {
4125
+ styles,
4126
+ script,
4127
+ isEditing: props.puck?.isEditing
4128
+ }
4129
+ ),
4098
4130
  userRender ? userRender(props) : props.children
4099
4131
  ] });
4100
4132
  };
@@ -4365,7 +4397,7 @@ var import_jsx_runtime35 = require("react/jsx-runtime");
4365
4397
  var q = (0, import_qler.default)();
4366
4398
  var DEFAULT_API_VERSION = "v2";
4367
4399
  var BUILD_OP_DEBOUNCE_MS = 1e3 / 60;
4368
- var PLUGIN_AI_VERSION = true ? "0.8.3-canary.4e864c39" : "unknown";
4400
+ var PLUGIN_AI_VERSION = true ? "0.8.3-canary.89ddeea1" : "unknown";
4369
4401
  var BENCHMARK = false;
4370
4402
  var prefixedUlid = (prefix = "") => `${prefix ? `${prefix}_` : ""}${(0, import_ulid.ulid)()}`;
4371
4403
  var getClassName18 = getClassNameFactory("Chat", styles_module_default);
@@ -4407,6 +4439,8 @@ function Chat2(props) {
4407
4439
  const removedAttachmentClientIdsRef = (0, import_react27.useRef)(/* @__PURE__ */ new Set());
4408
4440
  const uploadControllersRef = (0, import_react27.useRef)(/* @__PURE__ */ new Map());
4409
4441
  const pendingBuildOpsRef = (0, import_react27.useRef)([]);
4442
+ const lastStreamActivityRef = (0, import_react27.useRef)(0);
4443
+ const hiddenSinceRef = (0, import_react27.useRef)(null);
4410
4444
  const [toolStatus, setToolStatus] = (0, import_react27.useState)({});
4411
4445
  const [subagentState, setSubagentState] = (0, import_react27.useState)({});
4412
4446
  const attachmentConfig = (0, import_react27.useMemo)(
@@ -4519,6 +4553,7 @@ function Chat2(props) {
4519
4553
  );
4520
4554
  const processData = (0, import_react27.useCallback)(
4521
4555
  (dataPart) => {
4556
+ lastStreamActivityRef.current = Date.now();
4522
4557
  switch (dataPart.type) {
4523
4558
  case "data-new-chat-created": {
4524
4559
  localChatId.current = dataPart.data.chatId;
@@ -4745,6 +4780,56 @@ function Chat2(props) {
4745
4780
  },
4746
4781
  [stop]
4747
4782
  );
4783
+ const statusRef = (0, import_react27.useRef)(status);
4784
+ (0, import_react27.useEffect)(() => {
4785
+ statusRef.current = status;
4786
+ }, [status]);
4787
+ (0, import_react27.useEffect)(() => {
4788
+ lastStreamActivityRef.current = Date.now();
4789
+ }, [messages]);
4790
+ (0, import_react27.useEffect)(() => {
4791
+ const FROZEN_HIDDEN_MS = 3 * 6e4;
4792
+ const RESUME_GRACE_MS = 12e3;
4793
+ const isStreaming = () => statusRef.current === "streaming" || statusRef.current === "submitted";
4794
+ let graceTimer;
4795
+ const recoverIfSevered = () => {
4796
+ if (!isStreaming()) return;
4797
+ if (Date.now() - lastStreamActivityRef.current < RESUME_GRACE_MS) return;
4798
+ stop();
4799
+ setToolStatus((current) => {
4800
+ let changed = false;
4801
+ const next = {};
4802
+ for (const [id, toolState] of Object.entries(current)) {
4803
+ if (toolState.loading) {
4804
+ changed = true;
4805
+ next[id] = { ...toolState, loading: false, label: "Interrupted" };
4806
+ } else {
4807
+ next[id] = toolState;
4808
+ }
4809
+ }
4810
+ return changed ? next : current;
4811
+ });
4812
+ };
4813
+ const onVisibilityChange = () => {
4814
+ if (document.hidden) {
4815
+ hiddenSinceRef.current = isStreaming() ? Date.now() : null;
4816
+ return;
4817
+ }
4818
+ const hiddenSince = hiddenSinceRef.current;
4819
+ hiddenSinceRef.current = null;
4820
+ if (graceTimer) clearTimeout(graceTimer);
4821
+ if (hiddenSince === null || Date.now() - hiddenSince < FROZEN_HIDDEN_MS) {
4822
+ return;
4823
+ }
4824
+ lastStreamActivityRef.current = 0;
4825
+ graceTimer = setTimeout(recoverIfSevered, RESUME_GRACE_MS);
4826
+ };
4827
+ document.addEventListener("visibilitychange", onVisibilityChange);
4828
+ return () => {
4829
+ document.removeEventListener("visibilitychange", onVisibilityChange);
4830
+ if (graceTimer) clearTimeout(graceTimer);
4831
+ };
4832
+ }, [stop]);
4748
4833
  const [forcedStatus, setForcedStatus] = (0, import_react27.useState)();
4749
4834
  const resolvedStatus = (0, import_react27.useMemo)(
4750
4835
  () => forcedStatus ?? status,
package/dist/index.mjs CHANGED
@@ -3452,6 +3452,29 @@ var syncFieldValues = (entry, fields, props) => {
3452
3452
  });
3453
3453
  }
3454
3454
  };
3455
+ var claimedHostStyles = /* @__PURE__ */ new WeakMap();
3456
+ var claimHostStyles = (el, applyWhiteSpace) => {
3457
+ if (!claimedHostStyles.has(el)) {
3458
+ claimedHostStyles.set(el, {
3459
+ cursor: el.style.cursor,
3460
+ whiteSpace: applyWhiteSpace ? el.style.whiteSpace : null
3461
+ });
3462
+ }
3463
+ applyHostStyles(el);
3464
+ };
3465
+ var applyHostStyles = (el) => {
3466
+ const claimed = claimedHostStyles.get(el);
3467
+ if (!claimed) return;
3468
+ el.style.cursor = "text";
3469
+ if (claimed.whiteSpace !== null) el.style.whiteSpace = "pre-wrap";
3470
+ };
3471
+ var releaseHostStyles = (el) => {
3472
+ const claimed = claimedHostStyles.get(el);
3473
+ if (!claimed) return;
3474
+ el.style.cursor = claimed.cursor;
3475
+ if (claimed.whiteSpace !== null) el.style.whiteSpace = claimed.whiteSpace;
3476
+ claimedHostStyles.delete(el);
3477
+ };
3455
3478
  var attachInlineEditing = (el, {
3456
3479
  componentId,
3457
3480
  propPath,
@@ -3464,8 +3487,7 @@ var attachInlineEditing = (el, {
3464
3487
  const syncEditable = () => {
3465
3488
  el.contentEditable = hovering || focused ? mode === "richtext" ? "true" : "plaintext-only" : "false";
3466
3489
  };
3467
- const previousCursor = el.style.cursor;
3468
- el.style.cursor = "text";
3490
+ claimHostStyles(el, mode !== "richtext");
3469
3491
  syncEditable();
3470
3492
  let lastValidNumberText = el.innerText.replaceAll(/\n/gm, "");
3471
3493
  const handleInput = () => {
@@ -3543,7 +3565,7 @@ var attachInlineEditing = (el, {
3543
3565
  el.removeEventListener("mouseout", handleMouseOut, true);
3544
3566
  el.removeEventListener("focus", handleFocus);
3545
3567
  el.removeEventListener("blur", handleBlur);
3546
- el.style.cursor = previousCursor;
3568
+ releaseHostStyles(el);
3547
3569
  el.removeAttribute("contenteditable");
3548
3570
  cleanupPortal?.();
3549
3571
  };
@@ -3609,7 +3631,7 @@ var wireArrayItemEditing = ({
3609
3631
  const mode = inlineEditModeFor(field);
3610
3632
  const registration = wiring.get(target);
3611
3633
  if (registration?.propPath === propPath && registration.mode === mode) {
3612
- target.style.cursor = "text";
3634
+ applyHostStyles(target);
3613
3635
  continue;
3614
3636
  }
3615
3637
  registration?.cleanup();
@@ -3935,7 +3957,7 @@ function createDesignComponentConfig(registration) {
3935
3957
  morphed.current = { el: entryEl, html, attrSig };
3936
3958
  morphHtml(entryEl, html, attrFields);
3937
3959
  for (const el of fieldWiring.current.keys()) {
3938
- if (el.isConnected) el.style.cursor = "text";
3960
+ if (el.isConnected) applyHostStyles(el);
3939
3961
  }
3940
3962
  }
3941
3963
  syncFieldValues(entryEl, annotations.fields, props);
@@ -4094,9 +4116,11 @@ function getDynamicConfigGlobals(dynamicConfig) {
4094
4116
  ...typeof script === "string" ? { script } : {}
4095
4117
  };
4096
4118
  }
4119
+ var INLINE_EDIT_RESET = ":where([data-puck-design] [contenteditable]){overflow-wrap:inherit;line-break:inherit;-webkit-nbsp-mode:inherit}";
4097
4120
  function DesignGlobals({
4098
4121
  styles,
4099
- script
4122
+ script,
4123
+ isEditing
4100
4124
  }) {
4101
4125
  const scriptRef = useRef12(null);
4102
4126
  const hostRef = useRef12(null);
@@ -4114,6 +4138,7 @@ function DesignGlobals({
4114
4138
  };
4115
4139
  }, [script]);
4116
4140
  return /* @__PURE__ */ jsxs19(Fragment8, { children: [
4141
+ isEditing && /* @__PURE__ */ jsx33("style", { children: INLINE_EDIT_RESET }),
4117
4142
  styles && /* @__PURE__ */ jsx33("style", { children: styles }),
4118
4143
  script && /* @__PURE__ */ jsx33("div", { ref: hostRef, style: { display: "none" } })
4119
4144
  ] });
@@ -4126,7 +4151,14 @@ function withDesignGlobals(root) {
4126
4151
  const render = (props) => {
4127
4152
  const { styles, script } = getDynamicConfigGlobals(props._dynamicConfig);
4128
4153
  return /* @__PURE__ */ jsxs19(Fragment8, { children: [
4129
- /* @__PURE__ */ jsx33(DesignGlobals, { styles, script }),
4154
+ /* @__PURE__ */ jsx33(
4155
+ DesignGlobals,
4156
+ {
4157
+ styles,
4158
+ script,
4159
+ isEditing: props.puck?.isEditing
4160
+ }
4161
+ ),
4130
4162
  userRender ? userRender(props) : props.children
4131
4163
  ] });
4132
4164
  };
@@ -4397,7 +4429,7 @@ import { Fragment as Fragment9, jsx as jsx35, jsxs as jsxs20 } from "react/jsx-r
4397
4429
  var q = qler();
4398
4430
  var DEFAULT_API_VERSION = "v2";
4399
4431
  var BUILD_OP_DEBOUNCE_MS = 1e3 / 60;
4400
- var PLUGIN_AI_VERSION = true ? "0.8.3-canary.4e864c39" : "unknown";
4432
+ var PLUGIN_AI_VERSION = true ? "0.8.3-canary.89ddeea1" : "unknown";
4401
4433
  var BENCHMARK = false;
4402
4434
  var prefixedUlid = (prefix = "") => `${prefix ? `${prefix}_` : ""}${ulid()}`;
4403
4435
  var getClassName18 = getClassNameFactory("Chat", styles_module_default);
@@ -4439,6 +4471,8 @@ function Chat2(props) {
4439
4471
  const removedAttachmentClientIdsRef = useRef13(/* @__PURE__ */ new Set());
4440
4472
  const uploadControllersRef = useRef13(/* @__PURE__ */ new Map());
4441
4473
  const pendingBuildOpsRef = useRef13([]);
4474
+ const lastStreamActivityRef = useRef13(0);
4475
+ const hiddenSinceRef = useRef13(null);
4442
4476
  const [toolStatus, setToolStatus] = useState16({});
4443
4477
  const [subagentState, setSubagentState] = useState16({});
4444
4478
  const attachmentConfig = useMemo6(
@@ -4551,6 +4585,7 @@ function Chat2(props) {
4551
4585
  );
4552
4586
  const processData = useCallback6(
4553
4587
  (dataPart) => {
4588
+ lastStreamActivityRef.current = Date.now();
4554
4589
  switch (dataPart.type) {
4555
4590
  case "data-new-chat-created": {
4556
4591
  localChatId.current = dataPart.data.chatId;
@@ -4777,6 +4812,56 @@ function Chat2(props) {
4777
4812
  },
4778
4813
  [stop]
4779
4814
  );
4815
+ const statusRef = useRef13(status);
4816
+ useEffect13(() => {
4817
+ statusRef.current = status;
4818
+ }, [status]);
4819
+ useEffect13(() => {
4820
+ lastStreamActivityRef.current = Date.now();
4821
+ }, [messages]);
4822
+ useEffect13(() => {
4823
+ const FROZEN_HIDDEN_MS = 3 * 6e4;
4824
+ const RESUME_GRACE_MS = 12e3;
4825
+ const isStreaming = () => statusRef.current === "streaming" || statusRef.current === "submitted";
4826
+ let graceTimer;
4827
+ const recoverIfSevered = () => {
4828
+ if (!isStreaming()) return;
4829
+ if (Date.now() - lastStreamActivityRef.current < RESUME_GRACE_MS) return;
4830
+ stop();
4831
+ setToolStatus((current) => {
4832
+ let changed = false;
4833
+ const next = {};
4834
+ for (const [id, toolState] of Object.entries(current)) {
4835
+ if (toolState.loading) {
4836
+ changed = true;
4837
+ next[id] = { ...toolState, loading: false, label: "Interrupted" };
4838
+ } else {
4839
+ next[id] = toolState;
4840
+ }
4841
+ }
4842
+ return changed ? next : current;
4843
+ });
4844
+ };
4845
+ const onVisibilityChange = () => {
4846
+ if (document.hidden) {
4847
+ hiddenSinceRef.current = isStreaming() ? Date.now() : null;
4848
+ return;
4849
+ }
4850
+ const hiddenSince = hiddenSinceRef.current;
4851
+ hiddenSinceRef.current = null;
4852
+ if (graceTimer) clearTimeout(graceTimer);
4853
+ if (hiddenSince === null || Date.now() - hiddenSince < FROZEN_HIDDEN_MS) {
4854
+ return;
4855
+ }
4856
+ lastStreamActivityRef.current = 0;
4857
+ graceTimer = setTimeout(recoverIfSevered, RESUME_GRACE_MS);
4858
+ };
4859
+ document.addEventListener("visibilitychange", onVisibilityChange);
4860
+ return () => {
4861
+ document.removeEventListener("visibilitychange", onVisibilityChange);
4862
+ if (graceTimer) clearTimeout(graceTimer);
4863
+ };
4864
+ }, [stop]);
4780
4865
  const [forcedStatus, setForcedStatus] = useState16();
4781
4866
  const resolvedStatus = useMemo6(
4782
4867
  () => forcedStatus ?? status,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@puckeditor/plugin-ai",
3
- "version": "0.8.3-canary.4e864c39",
3
+ "version": "0.8.3-canary.89ddeea1",
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.4e864c39",
56
- "@puckeditor/platform-types": "0.8.3-canary.4e864c39",
57
- "eslint-config-custom": "0.8.3-canary.4e864c39",
58
- "reducer": "0.8.3-canary.4e864c39",
59
- "tsconfig": "0.8.3-canary.4e864c39",
60
- "tsup-config": "0.8.3-canary.4e864c39"
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"
61
61
  },
62
62
  "peerDependencies": {
63
63
  "@puckeditor/core": "^0",