@copilotkit/react-textarea 0.19.0-alpha.3 → 0.19.0-alpha.4

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 (222) hide show
  1. package/.turbo/turbo-build.log +147 -143
  2. package/CHANGELOG.md +6 -0
  3. package/dist/components/base-copilot-textarea/base-copilot-textarea.mjs +1870 -33
  4. package/dist/components/base-copilot-textarea/base-copilot-textarea.mjs.map +1 -1
  5. package/dist/components/base-copilot-textarea/render-element.mjs +52 -3
  6. package/dist/components/base-copilot-textarea/render-element.mjs.map +1 -1
  7. package/dist/components/base-copilot-textarea/render-placeholder.mjs +46 -3
  8. package/dist/components/base-copilot-textarea/render-placeholder.mjs.map +1 -1
  9. package/dist/components/base-copilot-textarea/track-cursor-moved-since-last-text-change.mjs +86 -4
  10. package/dist/components/base-copilot-textarea/track-cursor-moved-since-last-text-change.mjs.map +1 -1
  11. package/dist/components/base-copilot-textarea/use-add-branding-css.mjs +57 -3
  12. package/dist/components/base-copilot-textarea/use-add-branding-css.mjs.map +1 -1
  13. package/dist/components/copilot-textarea/copilot-textarea.mjs +2339 -43
  14. package/dist/components/copilot-textarea/copilot-textarea.mjs.map +1 -1
  15. package/dist/components/hovering-toolbar/hovering-editor-provider.mjs +22 -3
  16. package/dist/components/hovering-toolbar/hovering-editor-provider.mjs.map +1 -1
  17. package/dist/components/hovering-toolbar/hovering-toolbar-components.mjs +125 -3
  18. package/dist/components/hovering-toolbar/hovering-toolbar-components.mjs.map +1 -1
  19. package/dist/components/hovering-toolbar/hovering-toolbar.mjs +1015 -16
  20. package/dist/components/hovering-toolbar/hovering-toolbar.mjs.map +1 -1
  21. package/dist/components/hovering-toolbar/text-insertion-prompt-box/hovering-insertion-prompt-box-core.mjs +721 -10
  22. package/dist/components/hovering-toolbar/text-insertion-prompt-box/hovering-insertion-prompt-box-core.mjs.map +1 -1
  23. package/dist/components/hovering-toolbar/text-insertion-prompt-box/hovering-insertion-prompt-box.mjs +734 -11
  24. package/dist/components/hovering-toolbar/text-insertion-prompt-box/hovering-insertion-prompt-box.mjs.map +1 -1
  25. package/dist/components/hovering-toolbar/text-insertion-prompt-box/index.mjs +734 -12
  26. package/dist/components/hovering-toolbar/text-insertion-prompt-box/index.mjs.map +1 -1
  27. package/dist/components/index.mjs +2340 -44
  28. package/dist/components/index.mjs.map +1 -1
  29. package/dist/components/manual-ui/chip-with-icon.mjs +5 -6
  30. package/dist/components/manual-ui/chip-with-icon.mjs.map +1 -1
  31. package/dist/components/source-search-box/source-search-box.mjs +323 -10
  32. package/dist/components/source-search-box/source-search-box.mjs.map +1 -1
  33. package/dist/components/ui/button.mjs +88 -4
  34. package/dist/components/ui/button.mjs.map +1 -1
  35. package/dist/components/ui/card.mjs +53 -7
  36. package/dist/components/ui/card.mjs.map +1 -1
  37. package/dist/components/ui/command.mjs +266 -5
  38. package/dist/components/ui/command.mjs.map +1 -1
  39. package/dist/components/ui/dialog.mjs +164 -4
  40. package/dist/components/ui/dialog.mjs.map +1 -1
  41. package/dist/components/ui/label.mjs +63 -4
  42. package/dist/components/ui/label.mjs.map +1 -1
  43. package/dist/components/ui/separator.mjs +50 -8
  44. package/dist/components/ui/separator.mjs.map +1 -1
  45. package/dist/components/ui/textarea.mjs +48 -7
  46. package/dist/components/ui/textarea.mjs.map +1 -1
  47. package/dist/context/index.mjs +0 -2
  48. package/dist/context/index.mjs.map +1 -1
  49. package/dist/hooks/base-copilot-textarea-implementation/use-autosuggestions.mjs +168 -6
  50. package/dist/hooks/base-copilot-textarea-implementation/use-autosuggestions.mjs.map +1 -1
  51. package/dist/hooks/base-copilot-textarea-implementation/use-copilot-textarea-editor.mjs +168 -4
  52. package/dist/hooks/base-copilot-textarea-implementation/use-copilot-textarea-editor.mjs.map +1 -1
  53. package/dist/hooks/base-copilot-textarea-implementation/use-populate-copilot-textarea-ref.mjs +155 -5
  54. package/dist/hooks/base-copilot-textarea-implementation/use-populate-copilot-textarea-ref.mjs.map +1 -1
  55. package/dist/hooks/index.mjs +0 -2
  56. package/dist/hooks/index.mjs.map +1 -1
  57. package/dist/hooks/make-autosuggestions-function/use-make-standard-autosuggestions-function.mjs +90 -4
  58. package/dist/hooks/make-autosuggestions-function/use-make-standard-autosuggestions-function.mjs.map +1 -1
  59. package/dist/hooks/make-autosuggestions-function/use-make-standard-insertion-function.mjs +143 -4
  60. package/dist/hooks/make-autosuggestions-function/use-make-standard-insertion-function.mjs.map +1 -1
  61. package/dist/hooks/misc/use-autosize-textarea.mjs +15 -3
  62. package/dist/hooks/misc/use-autosize-textarea.mjs.map +1 -1
  63. package/dist/index.css +3 -12
  64. package/dist/index.css.map +1 -1
  65. package/dist/index.mjs +2343 -48
  66. package/dist/index.mjs.map +1 -1
  67. package/dist/lib/debouncer.mjs +51 -3
  68. package/dist/lib/debouncer.mjs.map +1 -1
  69. package/dist/lib/editor-to-text.mjs +43 -3
  70. package/dist/lib/editor-to-text.mjs.map +1 -1
  71. package/dist/lib/get-text-around-cursor.mjs +109 -3
  72. package/dist/lib/get-text-around-cursor.mjs.map +1 -1
  73. package/dist/lib/retry.mjs +17 -3
  74. package/dist/lib/retry.mjs.map +1 -1
  75. package/dist/lib/slatejs-edits/add-autocompletions.mjs +25 -3
  76. package/dist/lib/slatejs-edits/add-autocompletions.mjs.map +1 -1
  77. package/dist/lib/slatejs-edits/clear-autocompletions.mjs +23 -3
  78. package/dist/lib/slatejs-edits/clear-autocompletions.mjs.map +1 -1
  79. package/dist/lib/slatejs-edits/replace-text.mjs +27 -3
  80. package/dist/lib/slatejs-edits/replace-text.mjs.map +1 -1
  81. package/dist/lib/slatejs-edits/with-partial-history.mjs +106 -3
  82. package/dist/lib/slatejs-edits/with-partial-history.mjs.map +1 -1
  83. package/dist/lib/stream-promise-flatten.mjs +47 -3
  84. package/dist/lib/stream-promise-flatten.mjs.map +1 -1
  85. package/dist/lib/utils.mjs +71 -3
  86. package/dist/lib/utils.mjs.map +1 -1
  87. package/dist/lib/utils.test.mjs +0 -1
  88. package/dist/lib/utils.test.mjs.map +1 -1
  89. package/dist/types/autosuggestions-config/autosuggestions-config-user-specified.mjs +0 -2
  90. package/dist/types/autosuggestions-config/autosuggestions-config-user-specified.mjs.map +1 -1
  91. package/dist/types/autosuggestions-config/autosuggestions-config.mjs +312 -9
  92. package/dist/types/autosuggestions-config/autosuggestions-config.mjs.map +1 -1
  93. package/dist/types/autosuggestions-config/editing-api-config.mjs +155 -4
  94. package/dist/types/autosuggestions-config/editing-api-config.mjs.map +1 -1
  95. package/dist/types/autosuggestions-config/index.mjs +313 -10
  96. package/dist/types/autosuggestions-config/index.mjs.map +1 -1
  97. package/dist/types/autosuggestions-config/insertions-api-config.mjs +149 -4
  98. package/dist/types/autosuggestions-config/insertions-api-config.mjs.map +1 -1
  99. package/dist/types/autosuggestions-config/subtypes/chatlike-api-endpoint.mjs +71 -3
  100. package/dist/types/autosuggestions-config/subtypes/chatlike-api-endpoint.mjs.map +1 -1
  101. package/dist/types/autosuggestions-config/subtypes/make-system-prompt.mjs +0 -2
  102. package/dist/types/autosuggestions-config/subtypes/make-system-prompt.mjs.map +1 -1
  103. package/dist/types/autosuggestions-config/subtypes/minimal-chat-gpt-message.mjs +0 -2
  104. package/dist/types/autosuggestions-config/subtypes/minimal-chat-gpt-message.mjs.map +1 -1
  105. package/dist/types/autosuggestions-config/suggestions-api-config.mjs +137 -4
  106. package/dist/types/autosuggestions-config/suggestions-api-config.mjs.map +1 -1
  107. package/dist/types/base/autosuggestion-state.mjs +0 -2
  108. package/dist/types/base/autosuggestion-state.mjs.map +1 -1
  109. package/dist/types/base/autosuggestions-bare-function.mjs +0 -2
  110. package/dist/types/base/autosuggestions-bare-function.mjs.map +1 -1
  111. package/dist/types/base/base-autosuggestions-config.mjs +11 -3
  112. package/dist/types/base/base-autosuggestions-config.mjs.map +1 -1
  113. package/dist/types/base/base-copilot-textarea-props.mjs +0 -2
  114. package/dist/types/base/base-copilot-textarea-props.mjs.map +1 -1
  115. package/dist/types/base/custom-editor.mjs +0 -2
  116. package/dist/types/base/custom-editor.mjs.map +1 -1
  117. package/dist/types/base/editor-autocomplete-state.mjs +17 -4
  118. package/dist/types/base/editor-autocomplete-state.mjs.map +1 -1
  119. package/dist/types/base/index.mjs +11 -4
  120. package/dist/types/base/index.mjs.map +1 -1
  121. package/dist/types/html-copilot-textarea-element.mjs +0 -2
  122. package/dist/types/html-copilot-textarea-element.mjs.map +1 -1
  123. package/dist/types/index.mjs +314 -12
  124. package/dist/types/index.mjs.map +1 -1
  125. package/package.json +3 -3
  126. package/src/components/hovering-toolbar/text-insertion-prompt-box/hovering-insertion-prompt-box-core.tsx +2 -1
  127. package/src/components/source-search-box/source-search-box.tsx +6 -9
  128. package/src/components/ui/command.tsx +1 -1
  129. package/dist/chunk-23UHDUUY.mjs +0 -461
  130. package/dist/chunk-23UHDUUY.mjs.map +0 -1
  131. package/dist/chunk-2NURR2DX.mjs +0 -47
  132. package/dist/chunk-2NURR2DX.mjs.map +0 -1
  133. package/dist/chunk-3YJ63D5D.mjs +0 -106
  134. package/dist/chunk-3YJ63D5D.mjs.map +0 -1
  135. package/dist/chunk-4MDDCR32.mjs +0 -107
  136. package/dist/chunk-4MDDCR32.mjs.map +0 -1
  137. package/dist/chunk-4S5ZJH3I.mjs +0 -18
  138. package/dist/chunk-4S5ZJH3I.mjs.map +0 -1
  139. package/dist/chunk-55EGOC5T.mjs +0 -87
  140. package/dist/chunk-55EGOC5T.mjs.map +0 -1
  141. package/dist/chunk-5ARCOTW3.mjs +0 -34
  142. package/dist/chunk-5ARCOTW3.mjs.map +0 -1
  143. package/dist/chunk-5EJ5XOGP.mjs +0 -22
  144. package/dist/chunk-5EJ5XOGP.mjs.map +0 -1
  145. package/dist/chunk-5FO6ISW4.mjs +0 -3
  146. package/dist/chunk-5FO6ISW4.mjs.map +0 -1
  147. package/dist/chunk-A2RRLD23.mjs +0 -69
  148. package/dist/chunk-A2RRLD23.mjs.map +0 -1
  149. package/dist/chunk-CSGFJU3L.mjs +0 -65
  150. package/dist/chunk-CSGFJU3L.mjs.map +0 -1
  151. package/dist/chunk-CZMPLTHF.mjs +0 -20
  152. package/dist/chunk-CZMPLTHF.mjs.map +0 -1
  153. package/dist/chunk-D7SEV5PR.mjs +0 -12
  154. package/dist/chunk-D7SEV5PR.mjs.map +0 -1
  155. package/dist/chunk-DE5K76I2.mjs +0 -3
  156. package/dist/chunk-DE5K76I2.mjs.map +0 -1
  157. package/dist/chunk-F3MHL6ZY.mjs +0 -25
  158. package/dist/chunk-F3MHL6ZY.mjs.map +0 -1
  159. package/dist/chunk-FN7GDKKG.mjs +0 -29
  160. package/dist/chunk-FN7GDKKG.mjs.map +0 -1
  161. package/dist/chunk-H4VKQGVU.mjs +0 -3
  162. package/dist/chunk-H4VKQGVU.mjs.map +0 -1
  163. package/dist/chunk-HAFHLU4N.mjs +0 -55
  164. package/dist/chunk-HAFHLU4N.mjs.map +0 -1
  165. package/dist/chunk-IU3WTXLQ.mjs +0 -3
  166. package/dist/chunk-IU3WTXLQ.mjs.map +0 -1
  167. package/dist/chunk-IXJ2HCOA.mjs +0 -101
  168. package/dist/chunk-IXJ2HCOA.mjs.map +0 -1
  169. package/dist/chunk-JAFCXEPU.mjs +0 -10
  170. package/dist/chunk-JAFCXEPU.mjs.map +0 -1
  171. package/dist/chunk-KCHYD3EB.mjs +0 -107
  172. package/dist/chunk-KCHYD3EB.mjs.map +0 -1
  173. package/dist/chunk-KGKLUWKW.mjs +0 -47
  174. package/dist/chunk-KGKLUWKW.mjs.map +0 -1
  175. package/dist/chunk-L7VVZH4Q.mjs +0 -3
  176. package/dist/chunk-L7VVZH4Q.mjs.map +0 -1
  177. package/dist/chunk-LNAIMEB2.mjs +0 -34
  178. package/dist/chunk-LNAIMEB2.mjs.map +0 -1
  179. package/dist/chunk-MMVDU6DF.mjs +0 -3
  180. package/dist/chunk-MMVDU6DF.mjs.map +0 -1
  181. package/dist/chunk-MPME5BW2.mjs +0 -59
  182. package/dist/chunk-MPME5BW2.mjs.map +0 -1
  183. package/dist/chunk-MRXNTQOX.mjs +0 -55
  184. package/dist/chunk-MRXNTQOX.mjs.map +0 -1
  185. package/dist/chunk-ND5PXTAW.mjs +0 -17
  186. package/dist/chunk-ND5PXTAW.mjs.map +0 -1
  187. package/dist/chunk-NKW5OU2S.mjs +0 -33
  188. package/dist/chunk-NKW5OU2S.mjs.map +0 -1
  189. package/dist/chunk-O5OWT5GE.mjs +0 -114
  190. package/dist/chunk-O5OWT5GE.mjs.map +0 -1
  191. package/dist/chunk-OD7ZMOVE.mjs +0 -45
  192. package/dist/chunk-OD7ZMOVE.mjs.map +0 -1
  193. package/dist/chunk-OELUUJZY.mjs +0 -16
  194. package/dist/chunk-OELUUJZY.mjs.map +0 -1
  195. package/dist/chunk-OS2YW2UX.mjs +0 -201
  196. package/dist/chunk-OS2YW2UX.mjs.map +0 -1
  197. package/dist/chunk-QL2GYGG5.mjs +0 -19
  198. package/dist/chunk-QL2GYGG5.mjs.map +0 -1
  199. package/dist/chunk-RQHOUUXQ.mjs +0 -29
  200. package/dist/chunk-RQHOUUXQ.mjs.map +0 -1
  201. package/dist/chunk-U5GM2PRT.mjs +0 -44
  202. package/dist/chunk-U5GM2PRT.mjs.map +0 -1
  203. package/dist/chunk-UHD44NC5.mjs +0 -101
  204. package/dist/chunk-UHD44NC5.mjs.map +0 -1
  205. package/dist/chunk-VBIJPE3H.mjs +0 -108
  206. package/dist/chunk-VBIJPE3H.mjs.map +0 -1
  207. package/dist/chunk-WADHCMPK.mjs +0 -3
  208. package/dist/chunk-WADHCMPK.mjs.map +0 -1
  209. package/dist/chunk-WJHSY5T6.mjs +0 -3
  210. package/dist/chunk-WJHSY5T6.mjs.map +0 -1
  211. package/dist/chunk-WJYQWL4I.mjs +0 -27
  212. package/dist/chunk-WJYQWL4I.mjs.map +0 -1
  213. package/dist/chunk-XDT7BF3V.mjs +0 -81
  214. package/dist/chunk-XDT7BF3V.mjs.map +0 -1
  215. package/dist/chunk-XHUMROEY.mjs +0 -91
  216. package/dist/chunk-XHUMROEY.mjs.map +0 -1
  217. package/dist/chunk-YQU7WG7T.mjs +0 -83
  218. package/dist/chunk-YQU7WG7T.mjs.map +0 -1
  219. package/dist/chunk-YTOPHPSG.mjs +0 -45
  220. package/dist/chunk-YTOPHPSG.mjs.map +0 -1
  221. package/dist/chunk-YW3REYX6.mjs +0 -23
  222. package/dist/chunk-YW3REYX6.mjs.map +0 -1
@@ -1,44 +1,2340 @@
1
- export { CopilotTextarea } from '../../chunk-U5GM2PRT.mjs';
2
- import '../../chunk-VBIJPE3H.mjs';
3
- import '../../chunk-HAFHLU4N.mjs';
4
- import '../../chunk-5FO6ISW4.mjs';
5
- import '../../chunk-QL2GYGG5.mjs';
6
- import '../../chunk-55EGOC5T.mjs';
7
- import '../../chunk-XDT7BF3V.mjs';
8
- import '../../chunk-A2RRLD23.mjs';
9
- import '../../chunk-5ARCOTW3.mjs';
10
- import '../../chunk-OS2YW2UX.mjs';
11
- import '../../chunk-UHD44NC5.mjs';
12
- import '../../chunk-JAFCXEPU.mjs';
13
- import '../../chunk-CSGFJU3L.mjs';
14
- import '../../chunk-KCHYD3EB.mjs';
15
- import '../../chunk-YQU7WG7T.mjs';
16
- import '../../chunk-WJYQWL4I.mjs';
17
- import '../../chunk-F3MHL6ZY.mjs';
18
- import '../../chunk-RQHOUUXQ.mjs';
19
- import '../../chunk-OELUUJZY.mjs';
20
- import '../../chunk-OD7ZMOVE.mjs';
21
- import '../../chunk-MPME5BW2.mjs';
22
- import '../../chunk-4MDDCR32.mjs';
23
- import '../../chunk-L7VVZH4Q.mjs';
24
- import '../../chunk-CZMPLTHF.mjs';
25
- import '../../chunk-YW3REYX6.mjs';
26
- import '../../chunk-XHUMROEY.mjs';
27
- import '../../chunk-23UHDUUY.mjs';
28
- import '../../chunk-ND5PXTAW.mjs';
29
- import '../../chunk-2NURR2DX.mjs';
30
- import '../../chunk-3YJ63D5D.mjs';
31
- import '../../chunk-O5OWT5GE.mjs';
32
- import '../../chunk-5EJ5XOGP.mjs';
33
- import '../../chunk-FN7GDKKG.mjs';
34
- import '../../chunk-KGKLUWKW.mjs';
35
- import '../../chunk-WJHSY5T6.mjs';
36
- import '../../chunk-D7SEV5PR.mjs';
37
- import '../../chunk-LNAIMEB2.mjs';
38
- import '../../chunk-NKW5OU2S.mjs';
39
- import '../../chunk-YTOPHPSG.mjs';
40
- import '../../chunk-IXJ2HCOA.mjs';
41
- import '../../chunk-4S5ZJH3I.mjs';
42
- import '../../chunk-MRXNTQOX.mjs';
43
- //# sourceMappingURL=out.js.map
1
+ var __defProp = Object.defineProperty;
2
+ var __defProps = Object.defineProperties;
3
+ var __getOwnPropDescs = Object.getOwnPropertyDescriptors;
4
+ var __getOwnPropSymbols = Object.getOwnPropertySymbols;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __propIsEnum = Object.prototype.propertyIsEnumerable;
7
+ var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
8
+ var __spreadValues = (a, b) => {
9
+ for (var prop in b || (b = {}))
10
+ if (__hasOwnProp.call(b, prop))
11
+ __defNormalProp(a, prop, b[prop]);
12
+ if (__getOwnPropSymbols)
13
+ for (var prop of __getOwnPropSymbols(b)) {
14
+ if (__propIsEnum.call(b, prop))
15
+ __defNormalProp(a, prop, b[prop]);
16
+ }
17
+ return a;
18
+ };
19
+ var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
20
+ var __objRest = (source, exclude) => {
21
+ var target = {};
22
+ for (var prop in source)
23
+ if (__hasOwnProp.call(source, prop) && exclude.indexOf(prop) < 0)
24
+ target[prop] = source[prop];
25
+ if (source != null && __getOwnPropSymbols)
26
+ for (var prop of __getOwnPropSymbols(source)) {
27
+ if (exclude.indexOf(prop) < 0 && __propIsEnum.call(source, prop))
28
+ target[prop] = source[prop];
29
+ }
30
+ return target;
31
+ };
32
+ var __async = (__this, __arguments, generator) => {
33
+ return new Promise((resolve, reject) => {
34
+ var fulfilled = (value) => {
35
+ try {
36
+ step(generator.next(value));
37
+ } catch (e) {
38
+ reject(e);
39
+ }
40
+ };
41
+ var rejected = (value) => {
42
+ try {
43
+ step(generator.throw(value));
44
+ } catch (e) {
45
+ reject(e);
46
+ }
47
+ };
48
+ var step = (x) => x.done ? resolve(x.value) : Promise.resolve(x.value).then(fulfilled, rejected);
49
+ step((generator = generator.apply(__this, __arguments)).next());
50
+ });
51
+ };
52
+
53
+ // src/components/copilot-textarea/copilot-textarea.tsx
54
+ import React10 from "react";
55
+
56
+ // src/hooks/make-autosuggestions-function/use-make-standard-autosuggestions-function.tsx
57
+ import { CopilotContext } from "@copilotkit/react-core";
58
+ import { useCallback, useContext } from "react";
59
+
60
+ // src/lib/retry.tsx
61
+ function retry(fn, retriesLeft = 2, interval = 200, backoff = 1.5) {
62
+ return new Promise((resolve, reject) => {
63
+ fn().then(resolve).catch((error) => {
64
+ if (retriesLeft === 1) {
65
+ reject(error);
66
+ return;
67
+ }
68
+ setTimeout(() => {
69
+ retry(fn, retriesLeft - 1, interval * backoff, backoff).then(resolve).catch(reject);
70
+ }, interval);
71
+ });
72
+ });
73
+ }
74
+
75
+ // src/hooks/make-autosuggestions-function/use-make-standard-autosuggestions-function.tsx
76
+ function useMakeStandardAutosuggestionFunction(textareaPurpose, contextCategories, apiConfig) {
77
+ const { getContextString } = useContext(CopilotContext);
78
+ return useCallback(
79
+ (editorState2, abortSignal) => __async(this, null, function* () {
80
+ const res = yield retry(() => __async(this, null, function* () {
81
+ const messages = [
82
+ {
83
+ role: "system",
84
+ content: apiConfig.makeSystemPrompt(
85
+ textareaPurpose,
86
+ getContextString(contextCategories)
87
+ )
88
+ },
89
+ ...apiConfig.fewShotMessages,
90
+ {
91
+ role: "user",
92
+ name: "TextAfterCursor",
93
+ content: editorState2.textAfterCursor
94
+ },
95
+ {
96
+ role: "user",
97
+ name: "TextBeforeCursor",
98
+ content: editorState2.textBeforeCursor
99
+ }
100
+ ];
101
+ const stream = yield apiConfig.apiEndpoint.run(
102
+ abortSignal,
103
+ messages,
104
+ apiConfig.forwardedParams
105
+ );
106
+ const reader = stream.getReader();
107
+ let result = "";
108
+ while (true) {
109
+ const { done, value } = yield reader.read();
110
+ if (done) {
111
+ break;
112
+ }
113
+ result += value;
114
+ }
115
+ return result;
116
+ }));
117
+ return res;
118
+ }),
119
+ [apiConfig, getContextString, contextCategories, textareaPurpose]
120
+ );
121
+ }
122
+
123
+ // src/types/base/base-autosuggestions-config.tsx
124
+ var defaultBaseAutosuggestionsConfig = {
125
+ debounceTime: 250,
126
+ acceptAutosuggestionKey: "Tab",
127
+ disableWhenEmpty: true,
128
+ disabled: false,
129
+ temporarilyDisableWhenMovingCursorWithoutChangingText: true
130
+ };
131
+
132
+ // src/types/autosuggestions-config/subtypes/chatlike-api-endpoint.tsx
133
+ var ChatlikeApiEndpoint = class {
134
+ constructor(run) {
135
+ this.run = run;
136
+ }
137
+ static standardOpenAIEndpoint(apiEndpoint) {
138
+ return new ChatlikeApiEndpoint(
139
+ (abortSignal, messages, forwardedProps) => __async(this, null, function* () {
140
+ const res = yield fetch(apiEndpoint, {
141
+ method: "POST",
142
+ body: JSON.stringify(__spreadProps(__spreadValues({}, forwardedProps), {
143
+ messages
144
+ })),
145
+ signal: abortSignal
146
+ });
147
+ const bodySteram = res.body;
148
+ if (!bodySteram) {
149
+ throw new Error("The response body is empty.");
150
+ }
151
+ const stringStream = bodySteram.pipeThrough(new TextDecoderStream());
152
+ return stringStream;
153
+ })
154
+ );
155
+ }
156
+ static custom(run) {
157
+ return new ChatlikeApiEndpoint(run);
158
+ }
159
+ };
160
+
161
+ // src/types/autosuggestions-config/suggestions-api-config.tsx
162
+ var defaultSuggestionsMakeSystemPrompt = (textareaPurpose, contextString) => {
163
+ return `You are a versatile writing assistant.
164
+
165
+ The user is writing some text.
166
+ The purpose is: "${textareaPurpose}"
167
+
168
+ Your job is to guess what the user will write next AS BEST YOU CAN.
169
+ Only guess a SHORT distance ahead. Usually 1 sentence, or at most 1 paragraph.
170
+
171
+ Adjust yourself to the user's style and implied intent.
172
+
173
+ The user will provide both the text before and after the cursor. You should use this to infer what the user is likely to write next.
174
+ <TextAfterCursor>
175
+ <TextBeforeCursor>
176
+ <YourSuggestion>
177
+
178
+ If we need to add a whitespace character to the suggested text, make sure to explicitly add it in.
179
+
180
+ The following external context is also provided. Use it to help you make better suggestions!!!
181
+ \`\`\`
182
+ ${contextString}
183
+ \`\`\`
184
+ `;
185
+ };
186
+ var defaultSuggestionsFewShotMessages = [
187
+ {
188
+ role: "user",
189
+ name: "TextAfterCursor",
190
+ content: "While I was there I also picked up some apples, oranges, and bananas."
191
+ },
192
+ {
193
+ role: "user",
194
+ name: "TextBeforeCursor",
195
+ content: "This morning I woke up and went straight to the grocery store."
196
+ },
197
+ {
198
+ role: "assistant",
199
+ content: " When I arrived I went straight to the produce section and picked out a big watermelon. "
200
+ },
201
+ {
202
+ role: "user",
203
+ name: "TextAfterCursor",
204
+ content: "and (iii) to the appointment of the Equityholders' Representative pursuant to Section 10.7 of the Merger Agreement and to the provisions thereof."
205
+ },
206
+ {
207
+ role: "user",
208
+ name: "TextBeforeCursor",
209
+ content: `The Optionholder, in the Optionholder's capacity as a holder of vested Options, hereby irrevocably and unconditionally agrees: (i) that the Optionholder shall be deemed an "Equityholder" under the Merger Agreement and shall be entitled to the rights and benefits, and subject to the obligations, of an "Equityholder" thereunder;`
210
+ },
211
+ {
212
+ role: "assistant",
213
+ content: ` (ii) that, for purposes of this Agreement and the Merger Agreement, the applicable percentage set forth opposite the name of the Optionholder in the Distribution Waterfall shall be such the Optionholder's "Pro Rata Share"; `
214
+ }
215
+ ];
216
+ var defaultSuggestionsApiConfig = {
217
+ apiEndpoint: ChatlikeApiEndpoint.standardOpenAIEndpoint(
218
+ "/api/copilotkit_chatlike"
219
+ ),
220
+ makeSystemPrompt: defaultSuggestionsMakeSystemPrompt,
221
+ fewShotMessages: defaultSuggestionsFewShotMessages,
222
+ forwardedParams: void 0
223
+ };
224
+
225
+ // src/types/autosuggestions-config/insertions-api-config.tsx
226
+ var defaultInsertionsMakeSystemPrompt = (textareaPurpose, contextString) => {
227
+ return `You are a versatile writing assistant.
228
+
229
+ The user is writing some text.
230
+ The purpose is: "${textareaPurpose}"
231
+
232
+ The user also provides you with a prompt for INSERTIONS into the text they are writing.
233
+ Your job is to come up with an INSERTION into the text that the user would like AS BEST YOU CAN.
234
+ Only guess a SHORT distance ahead. Usually 1 sentence, or at most 1 paragraph.
235
+
236
+ Adjust yourself to the user's style and implied intent.
237
+
238
+ The user will provide the text before and after the cursor, as well as the insertion prompt. You should use this to infer the best relevant insertion.
239
+ <TextAfterCursor>
240
+ <TextBeforeCursor>
241
+ <InsertionPrompt>
242
+ <YourSuggestion>
243
+
244
+ If we need to add a whitespace character to the suggested text, make sure to explicitly add it in.
245
+
246
+ The following external context is also provided. Use it to help you make better suggestions!!!
247
+ \`\`\`
248
+ ${contextString}
249
+ \`\`\`
250
+ `;
251
+ };
252
+ var defaultInsertionsFewShotMessages = [
253
+ {
254
+ role: "user",
255
+ name: "TextAfterCursor",
256
+ content: "While I was there I also picked up some apples, oranges, and bananas."
257
+ },
258
+ {
259
+ role: "user",
260
+ name: "TextBeforeCursor",
261
+ content: "This morning I woke up and went straight to the grocery store."
262
+ },
263
+ {
264
+ role: "user",
265
+ name: "InsertionPrompt",
266
+ content: "I bought a big watermelon"
267
+ },
268
+ {
269
+ role: "assistant",
270
+ content: " When I arrived I went straight to the produce section and picked out a big watermelon. "
271
+ },
272
+ {
273
+ role: "user",
274
+ name: "TextAfterCursor",
275
+ content: "and (iii) to the appointment of the Equityholders' Representative pursuant to Section 10.7 of the Merger Agreement and to the provisions thereof."
276
+ },
277
+ {
278
+ role: "user",
279
+ name: "TextBeforeCursor",
280
+ content: `The Optionholder, in the Optionholder's capacity as a holder of vested Options, hereby irrevocably and unconditionally agrees: (i) that the Optionholder shall be deemed an "Equityholder" under the Merger Agreement and shall be entitled to the rights and benefits, and subject to the obligations, of an "Equityholder" thereunder;`
281
+ },
282
+ {
283
+ role: "user",
284
+ name: "InsertionPrompt",
285
+ content: "add section about the optionholder's pro rata share"
286
+ },
287
+ {
288
+ role: "assistant",
289
+ content: ` (ii) that, for purposes of this Agreement and the Merger Agreement, the applicable percentage set forth opposite the name of the Optionholder in the Distribution Waterfall shall be such the Optionholder's "Pro Rata Share"; `
290
+ }
291
+ ];
292
+ var defaultInsertionsApiConfig = {
293
+ apiEndpoint: ChatlikeApiEndpoint.standardOpenAIEndpoint(
294
+ "/api/copilotkit_chatlike"
295
+ ),
296
+ makeSystemPrompt: defaultInsertionsMakeSystemPrompt,
297
+ fewShotMessages: defaultInsertionsFewShotMessages,
298
+ forwardedParams: void 0
299
+ };
300
+
301
+ // src/types/autosuggestions-config/editing-api-config.tsx
302
+ var defaultEditingMakeSystemPrompt = (textareaPurpose, contextString) => {
303
+ return `You are a versatile writing assistant.
304
+
305
+ The user is writing some text.
306
+ The purpose is: "${textareaPurpose}"
307
+
308
+ The user also provides you with a prompt for EDITING some text they are writing.
309
+ Your job is to come up with an EDIT of the text that the user would like to use - AS BEST YOU CAN.
310
+
311
+ Adjust yourself to the user's style and implied intent.
312
+
313
+ The user will provide the following information; use this to infer the best relevant EDIT:
314
+ <TextBeforeCursor>
315
+ <TextToEdit>
316
+ <TextAfterCursor>
317
+ <EditingPrompt>
318
+
319
+ <YourEditSuggestion>
320
+
321
+ If we need to add a whitespace character to the suggested edit text, make sure to explicitly add it in.
322
+
323
+ The following external context is also provided. Use it to help you make better suggestions!!!
324
+ \`\`\`
325
+ ${contextString}
326
+ \`\`\`
327
+ `;
328
+ };
329
+ var defaultEditingFewShotMessages = [
330
+ {
331
+ role: "user",
332
+ name: "TextBeforeCursor",
333
+ content: "This morning I woke up and went straight to the grocery store. "
334
+ },
335
+ {
336
+ role: "user",
337
+ name: "TextToEdit",
338
+ content: "While I was there I also picked up some apples, oranges, and bananas. "
339
+ },
340
+ {
341
+ role: "user",
342
+ name: "TextAfterCursor",
343
+ content: "The grocery store was having a sale on fruit, so I decided to stock up."
344
+ },
345
+ {
346
+ role: "user",
347
+ name: "EditingPrompt",
348
+ content: "I bought a big watermelon"
349
+ },
350
+ {
351
+ role: "assistant",
352
+ content: " When I arrived I went straight to the produce section and picked out a big watermelon. "
353
+ },
354
+ {
355
+ role: "user",
356
+ name: "TextAfterCursor",
357
+ content: "and (iii) to the appointment of the Equityholders' Representative pursuant to Section 10.7 of the Merger Agreement and to the provisions thereof."
358
+ },
359
+ {
360
+ role: "user",
361
+ name: "TextBeforeCursor",
362
+ content: `The Optionholder, in the Optionholder's capacity as a holder of vested Options, hereby irrevocably and unconditionally agrees: (i) that the Optionholder shall be deemed an "Equityholder" under the Merger Agreement and shall be entitled to the rights and benefits, and subject to the obligations, of an "Equityholder" thereunder;`
363
+ },
364
+ {
365
+ role: "user",
366
+ name: "InsertionPrompt",
367
+ content: "add section about the optionholder's pro rata share"
368
+ },
369
+ {
370
+ role: "assistant",
371
+ content: ` (ii) that, for purposes of this Agreement and the Merger Agreement, the applicable percentage set forth opposite the name of the Optionholder in the Distribution Waterfall shall be such the Optionholder's "Pro Rata Share"; `
372
+ }
373
+ ];
374
+ var defaultEditingApiConfig = {
375
+ apiEndpoint: ChatlikeApiEndpoint.standardOpenAIEndpoint(
376
+ "/api/copilotkit_chatlike"
377
+ ),
378
+ makeSystemPrompt: defaultEditingMakeSystemPrompt,
379
+ fewShotMessages: defaultEditingFewShotMessages,
380
+ forwardedParams: void 0
381
+ };
382
+
383
+ // src/types/autosuggestions-config/autosuggestions-config.tsx
384
+ var defaultAutosuggestionsConfig = __spreadProps(__spreadValues({}, defaultBaseAutosuggestionsConfig), {
385
+ externalContextCategories: void 0,
386
+ chatApiConfigs: {
387
+ suggestionsApiConfig: defaultSuggestionsApiConfig,
388
+ insertionApiConfig: defaultInsertionsApiConfig,
389
+ editingApiConfig: defaultEditingApiConfig
390
+ }
391
+ });
392
+
393
+ // src/components/base-copilot-textarea/base-copilot-textarea.tsx
394
+ import React9, { useCallback as useCallback3, useEffect as useEffect7, useMemo as useMemo4, useState as useState6 } from "react";
395
+ import { Editor as Editor5 } from "slate";
396
+ import { Editable, Slate } from "slate-react";
397
+ import { twMerge as twMerge2 } from "tailwind-merge";
398
+
399
+ // src/hooks/base-copilot-textarea-implementation/use-autosuggestions.ts
400
+ import { useCallback as useCallback2, useEffect, useMemo, useState } from "react";
401
+
402
+ // src/lib/debouncer.ts
403
+ var Debouncer = class {
404
+ constructor(wait) {
405
+ this.wait = wait;
406
+ this.debounce = (func, ...args) => __async(this, null, function* () {
407
+ this.cancel();
408
+ this.timeoutId = setTimeout(() => __async(this, null, function* () {
409
+ try {
410
+ this.activeAbortController = new AbortController();
411
+ yield func(...args, this.activeAbortController.signal);
412
+ this.activeAbortController = void 0;
413
+ } catch (error) {
414
+ }
415
+ }), this.wait);
416
+ });
417
+ this.cancel = () => {
418
+ if (this.activeAbortController) {
419
+ this.activeAbortController.abort();
420
+ this.activeAbortController = void 0;
421
+ }
422
+ if (this.timeoutId !== void 0) {
423
+ clearTimeout(this.timeoutId);
424
+ this.timeoutId = void 0;
425
+ }
426
+ };
427
+ }
428
+ };
429
+
430
+ // src/lib/utils.ts
431
+ import { clsx } from "clsx";
432
+ import { customAlphabet } from "nanoid";
433
+ import { twMerge } from "tailwind-merge";
434
+ function cn(...inputs) {
435
+ return twMerge(clsx(inputs));
436
+ }
437
+ var nanoid = customAlphabet(
438
+ "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz",
439
+ 7
440
+ );
441
+ var arraysAreEqual = (arr1, arr2) => arr1.length === arr2.length && arr1.every((value, index) => value === arr2[index]);
442
+ function nullableCompatibleEqualityCheck(naiveEqualityCheck, a, b) {
443
+ if (a === null || a === void 0 || b === null || b === void 0) {
444
+ return a === b;
445
+ }
446
+ return naiveEqualityCheck(a, b);
447
+ }
448
+
449
+ // src/types/base/editor-autocomplete-state.ts
450
+ function areEqual_autocompleteState(prev, next) {
451
+ return prev.cursorPoint.offset === next.cursorPoint.offset && arraysAreEqual(prev.cursorPoint.path, next.cursorPoint.path) && prev.textBeforeCursor === next.textBeforeCursor && prev.textAfterCursor === next.textAfterCursor;
452
+ }
453
+
454
+ // src/hooks/base-copilot-textarea-implementation/use-autosuggestions.ts
455
+ function useAutosuggestions(debounceTime, acceptAutosuggestionKey, autosuggestionFunction, insertAutocompleteSuggestion, disableWhenEmpty, disabled) {
456
+ const [previousAutocompleteState, setPreviousAutocompleteState] = useState(null);
457
+ const [currentAutocompleteSuggestion, setCurrentAutocompleteSuggestion] = useState(null);
458
+ const awaitForAndAppendSuggestion = useCallback2(
459
+ (editorAutocompleteState, abortSignal) => __async(this, null, function* () {
460
+ if (disabled) {
461
+ return;
462
+ }
463
+ if (disableWhenEmpty && editorAutocompleteState.textBeforeCursor === "" && editorAutocompleteState.textAfterCursor === "") {
464
+ return;
465
+ }
466
+ const suggestion = yield autosuggestionFunction(
467
+ editorAutocompleteState,
468
+ abortSignal
469
+ );
470
+ if (!suggestion || abortSignal.aborted) {
471
+ throw new DOMException("Aborted", "AbortError");
472
+ }
473
+ setCurrentAutocompleteSuggestion({
474
+ text: suggestion,
475
+ point: editorAutocompleteState.cursorPoint
476
+ });
477
+ }),
478
+ [
479
+ autosuggestionFunction,
480
+ setCurrentAutocompleteSuggestion,
481
+ disableWhenEmpty,
482
+ disabled
483
+ ]
484
+ );
485
+ const debouncedFunction = useMemo(
486
+ () => new Debouncer(
487
+ debounceTime
488
+ ),
489
+ [debounceTime]
490
+ );
491
+ useEffect(() => {
492
+ return () => {
493
+ debouncedFunction.cancel();
494
+ setCurrentAutocompleteSuggestion(null);
495
+ };
496
+ }, [debouncedFunction, disabled]);
497
+ const onChange = useCallback2(
498
+ (newEditorState) => {
499
+ const editorStateHasChanged = !nullableCompatibleEqualityCheck(
500
+ areEqual_autocompleteState,
501
+ previousAutocompleteState,
502
+ newEditorState
503
+ );
504
+ setPreviousAutocompleteState(newEditorState);
505
+ if (!editorStateHasChanged) {
506
+ return;
507
+ }
508
+ setCurrentAutocompleteSuggestion(null);
509
+ if (newEditorState) {
510
+ debouncedFunction.debounce(awaitForAndAppendSuggestion, newEditorState);
511
+ } else {
512
+ debouncedFunction.cancel();
513
+ }
514
+ },
515
+ [
516
+ previousAutocompleteState,
517
+ setPreviousAutocompleteState,
518
+ debouncedFunction,
519
+ awaitForAndAppendSuggestion,
520
+ setCurrentAutocompleteSuggestion
521
+ ]
522
+ );
523
+ const keyDownHandler = useCallback2(
524
+ (event) => {
525
+ if (currentAutocompleteSuggestion) {
526
+ if (event.key === acceptAutosuggestionKey) {
527
+ event.preventDefault();
528
+ insertAutocompleteSuggestion(currentAutocompleteSuggestion);
529
+ setCurrentAutocompleteSuggestion(null);
530
+ }
531
+ }
532
+ },
533
+ [
534
+ currentAutocompleteSuggestion,
535
+ setCurrentAutocompleteSuggestion,
536
+ insertAutocompleteSuggestion,
537
+ acceptAutosuggestionKey
538
+ ]
539
+ );
540
+ return {
541
+ currentAutocompleteSuggestion,
542
+ onChangeHandler: onChange,
543
+ onKeyDownHandler: keyDownHandler
544
+ };
545
+ }
546
+
547
+ // src/hooks/base-copilot-textarea-implementation/use-copilot-textarea-editor.tsx
548
+ import { useMemo as useMemo2 } from "react";
549
+ import { createEditor, Element } from "slate";
550
+ import { withReact } from "slate-react";
551
+
552
+ // src/lib/slatejs-edits/with-partial-history.ts
553
+ import { Editor, Operation, Path, Transforms } from "slate";
554
+ import { HistoryEditor } from "slate-history";
555
+ var withPartialHistory = (editor, shouldSave2) => {
556
+ const e = editor;
557
+ const { apply } = e;
558
+ e.history = { undos: [], redos: [] };
559
+ e.redo = () => {
560
+ const { history } = e;
561
+ const { redos } = history;
562
+ if (redos.length > 0) {
563
+ const batch = redos[redos.length - 1];
564
+ if (batch.selectionBefore) {
565
+ Transforms.setSelection(e, batch.selectionBefore);
566
+ }
567
+ HistoryEditor.withoutSaving(e, () => {
568
+ Editor.withoutNormalizing(e, () => {
569
+ for (const op of batch.operations) {
570
+ e.apply(op);
571
+ }
572
+ });
573
+ });
574
+ history.redos.pop();
575
+ e.writeHistory("undos", batch);
576
+ }
577
+ };
578
+ e.undo = () => {
579
+ const { history } = e;
580
+ const { undos } = history;
581
+ if (undos.length > 0) {
582
+ const batch = undos[undos.length - 1];
583
+ HistoryEditor.withoutSaving(e, () => {
584
+ Editor.withoutNormalizing(e, () => {
585
+ const inverseOps = batch.operations.map(Operation.inverse).reverse();
586
+ for (const op of inverseOps) {
587
+ e.apply(op);
588
+ }
589
+ if (batch.selectionBefore) {
590
+ Transforms.setSelection(e, batch.selectionBefore);
591
+ }
592
+ });
593
+ });
594
+ e.writeHistory("redos", batch);
595
+ history.undos.pop();
596
+ }
597
+ };
598
+ e.apply = (op) => {
599
+ const { operations, history } = e;
600
+ const { undos } = history;
601
+ const lastBatch = undos[undos.length - 1];
602
+ const lastOp = lastBatch && lastBatch.operations[lastBatch.operations.length - 1];
603
+ let save = HistoryEditor.isSaving(e);
604
+ let merge2 = HistoryEditor.isMerging(e);
605
+ if (save == null) {
606
+ save = shouldSave2(op, lastOp);
607
+ }
608
+ if (save) {
609
+ if (merge2 == null) {
610
+ if (lastBatch == null) {
611
+ merge2 = false;
612
+ } else if (operations.length !== 0) {
613
+ merge2 = true;
614
+ } else {
615
+ merge2 = shouldMerge(op, lastOp);
616
+ }
617
+ }
618
+ if (lastBatch && merge2) {
619
+ lastBatch.operations.push(op);
620
+ } else {
621
+ const batch = {
622
+ operations: [op],
623
+ selectionBefore: e.selection
624
+ };
625
+ e.writeHistory("undos", batch);
626
+ }
627
+ while (undos.length > 100) {
628
+ undos.shift();
629
+ }
630
+ history.redos = [];
631
+ }
632
+ apply(op);
633
+ };
634
+ e.writeHistory = (stack, batch) => {
635
+ e.history[stack].push(batch);
636
+ };
637
+ return e;
638
+ };
639
+ var shouldMerge = (op, prev) => {
640
+ if (prev && op.type === "insert_text" && prev.type === "insert_text" && op.offset === prev.offset + prev.text.length && Path.equals(op.path, prev.path)) {
641
+ return true;
642
+ }
643
+ if (prev && op.type === "remove_text" && prev.type === "remove_text" && op.offset + op.text.length === prev.offset && Path.equals(op.path, prev.path)) {
644
+ return true;
645
+ }
646
+ return false;
647
+ };
648
+ var defaultShouldSave = (op, prev) => {
649
+ if (op.type === "set_selection") {
650
+ return false;
651
+ }
652
+ return true;
653
+ };
654
+
655
+ // src/hooks/base-copilot-textarea-implementation/use-copilot-textarea-editor.tsx
656
+ var shouldSave = (op, prev) => {
657
+ const excludedNodeType = "suggestion";
658
+ if (op.type === "insert_node" && Element.isElement(op.node) && op.node.type === excludedNodeType) {
659
+ return false;
660
+ }
661
+ if (op.type === "remove_node" && Element.isElement(op.node) && op.node.type === excludedNodeType) {
662
+ return false;
663
+ }
664
+ if (op.type === "set_node" && "type" in op.newProperties && op.newProperties.type === excludedNodeType) {
665
+ return false;
666
+ }
667
+ if (op.type == "set_node" && "type" in op.properties && op.properties.type === excludedNodeType) {
668
+ return false;
669
+ }
670
+ if (op.type === "merge_node" && "type" in op.properties && op.properties.type === excludedNodeType) {
671
+ return false;
672
+ }
673
+ if (op.type === "split_node" && "type" in op.properties && op.properties.type === excludedNodeType) {
674
+ return false;
675
+ }
676
+ return defaultShouldSave(op, prev);
677
+ };
678
+ function useCopilotTextareaEditor() {
679
+ const editor = useMemo2(() => {
680
+ const editor2 = withPartialHistory(withReact(createEditor()), shouldSave);
681
+ const { isVoid } = editor2;
682
+ editor2.isVoid = (element) => {
683
+ switch (element.type) {
684
+ case "suggestion":
685
+ return true;
686
+ default:
687
+ return isVoid(element);
688
+ }
689
+ };
690
+ const { markableVoid } = editor2;
691
+ editor2.markableVoid = (element) => {
692
+ switch (element.type) {
693
+ case "suggestion":
694
+ return true;
695
+ default:
696
+ return markableVoid(element);
697
+ }
698
+ };
699
+ const { isInline } = editor2;
700
+ editor2.isInline = (element) => {
701
+ switch (element.type) {
702
+ case "suggestion":
703
+ return element.inline;
704
+ default:
705
+ return isInline(element);
706
+ }
707
+ };
708
+ return editor2;
709
+ }, []);
710
+ return editor;
711
+ }
712
+
713
+ // src/hooks/base-copilot-textarea-implementation/use-populate-copilot-textarea-ref.ts
714
+ import React from "react";
715
+ import { ReactEditor } from "slate-react";
716
+
717
+ // src/lib/get-text-around-cursor.ts
718
+ import {
719
+ Editor as Editor2,
720
+ Path as Path2,
721
+ Range as Range2,
722
+ Text,
723
+ Element as Element2,
724
+ Point
725
+ } from "slate";
726
+ function getTextAroundCollapsedCursor(editor) {
727
+ const { selection } = editor;
728
+ if (!selection || !Range2.isCollapsed(selection)) {
729
+ return null;
730
+ }
731
+ const cursorPoint = selection.anchor;
732
+ const beforeRange = {
733
+ anchor: Editor2.start(editor, []),
734
+ focus: cursorPoint
735
+ };
736
+ const afterRange = {
737
+ anchor: cursorPoint,
738
+ focus: Editor2.end(editor, [])
739
+ };
740
+ const before = extractTextWithNewlines(editor, beforeRange);
741
+ const after = extractTextWithNewlines(editor, afterRange);
742
+ return {
743
+ cursorPoint,
744
+ textBeforeCursor: before,
745
+ textAfterCursor: after
746
+ };
747
+ }
748
+ function getTextAroundSelection(editor) {
749
+ const { selection } = editor;
750
+ if (!selection) {
751
+ return null;
752
+ }
753
+ const wellOrderedSelection = wellOrderedRange(selection);
754
+ const beforeRange = {
755
+ anchor: Editor2.start(editor, []),
756
+ focus: wellOrderedSelection.anchor
757
+ };
758
+ const afterRange = {
759
+ anchor: wellOrderedSelection.focus,
760
+ focus: Editor2.end(editor, [])
761
+ };
762
+ const before = extractTextWithNewlines(editor, beforeRange);
763
+ const after = extractTextWithNewlines(editor, afterRange);
764
+ const selectedText = extractTextWithNewlines(editor, wellOrderedSelection);
765
+ return {
766
+ selection: wellOrderedSelection,
767
+ textBeforeCursor: before,
768
+ selectedText,
769
+ textAfterCursor: after
770
+ };
771
+ }
772
+ function getFullEditorTextWithNewlines(editor) {
773
+ const fullDocumentRange = {
774
+ anchor: Editor2.start(editor, []),
775
+ focus: Editor2.end(editor, [])
776
+ };
777
+ return extractTextWithNewlines(editor, fullDocumentRange);
778
+ }
779
+ function extractTextWithNewlines(editor, range) {
780
+ const voids = false;
781
+ const [start, end] = Range2.edges(range);
782
+ let text = "";
783
+ let lastBlock = null;
784
+ for (const [node, path] of Editor2.nodes(editor, {
785
+ at: range,
786
+ match: Text.isText,
787
+ voids
788
+ })) {
789
+ let t = node.text;
790
+ const [block] = Editor2.above(editor, {
791
+ at: path,
792
+ match: (n) => Element2.isElement(n) && n.type === "paragraph"
793
+ }) || [null];
794
+ if (lastBlock !== block && block) {
795
+ if (lastBlock) {
796
+ text += "\n";
797
+ }
798
+ lastBlock = block;
799
+ }
800
+ if (Path2.equals(path, end.path)) {
801
+ t = t.slice(0, end.offset);
802
+ }
803
+ if (Path2.equals(path, start.path)) {
804
+ t = t.slice(start.offset);
805
+ }
806
+ text += t;
807
+ }
808
+ return text;
809
+ }
810
+ function wellOrderedRange(range) {
811
+ const { anchor, focus } = range;
812
+ if (Point.isBefore(anchor, focus)) {
813
+ return range;
814
+ }
815
+ return {
816
+ anchor: focus,
817
+ focus: anchor
818
+ };
819
+ }
820
+
821
+ // src/lib/slatejs-edits/replace-text.ts
822
+ import { Editor as Editor3, Transforms as Transforms2 } from "slate";
823
+ function replaceEditorText(editor, newText) {
824
+ Transforms2.delete(editor, {
825
+ at: {
826
+ anchor: Editor3.start(editor, []),
827
+ focus: Editor3.end(editor, [])
828
+ }
829
+ });
830
+ if (newText && newText !== "") {
831
+ Transforms2.insertNodes(
832
+ editor,
833
+ [
834
+ {
835
+ type: "paragraph",
836
+ children: [{ text: newText }]
837
+ }
838
+ ],
839
+ {
840
+ at: [0]
841
+ }
842
+ );
843
+ }
844
+ }
845
+
846
+ // src/hooks/base-copilot-textarea-implementation/use-populate-copilot-textarea-ref.ts
847
+ function usePopulateCopilotTextareaRef(editor, ref) {
848
+ React.useImperativeHandle(
849
+ ref,
850
+ () => {
851
+ class Combined {
852
+ constructor(customMethods2, editorHtmlElement2) {
853
+ this.customMethods = customMethods2;
854
+ this.editorHtmlElement = editorHtmlElement2;
855
+ }
856
+ get(target, propKey) {
857
+ if (this.isKeyOfCustomMethods(propKey)) {
858
+ const value = this.customMethods[propKey];
859
+ if (typeof value === "function") {
860
+ return value.bind(this.customMethods);
861
+ }
862
+ return value;
863
+ } else if (this.isKeyOfHTMLElement(propKey)) {
864
+ const value = this.editorHtmlElement[propKey];
865
+ if (typeof value === "function") {
866
+ return value.bind(this.editorHtmlElement);
867
+ }
868
+ return value;
869
+ }
870
+ }
871
+ set(target, propKey, value) {
872
+ if (this.isKeyOfCustomMethods(propKey)) {
873
+ this.customMethods[propKey] = value;
874
+ } else if (this.isKeyOfHTMLElement(propKey)) {
875
+ this.editorHtmlElement[propKey] = value;
876
+ } else {
877
+ target[propKey] = value;
878
+ }
879
+ return true;
880
+ }
881
+ isKeyOfCustomMethods(key) {
882
+ return key in this.customMethods;
883
+ }
884
+ isKeyOfHTMLElement(key) {
885
+ return key in this.editorHtmlElement;
886
+ }
887
+ }
888
+ const handler = {
889
+ get(target, propKey) {
890
+ return target.get(target, propKey);
891
+ },
892
+ set(target, propKey, value) {
893
+ return target.set(target, propKey, value);
894
+ }
895
+ };
896
+ class CustomMethods {
897
+ constructor(editor2) {
898
+ this.editor = editor2;
899
+ }
900
+ focus() {
901
+ ReactEditor.focus(this.editor);
902
+ }
903
+ blur() {
904
+ ReactEditor.blur(this.editor);
905
+ }
906
+ get value() {
907
+ return getFullEditorTextWithNewlines(this.editor);
908
+ }
909
+ set value(value) {
910
+ replaceEditorText(this.editor, value);
911
+ }
912
+ }
913
+ const editorHtmlElement = ReactEditor.toDOMNode(editor, editor);
914
+ const customMethods = new CustomMethods(editor);
915
+ const combined = new Combined(customMethods, editorHtmlElement);
916
+ return new Proxy(combined, handler);
917
+ },
918
+ [editor]
919
+ );
920
+ }
921
+
922
+ // src/lib/slatejs-edits/add-autocompletions.ts
923
+ import { Transforms as Transforms3 } from "slate";
924
+ function addAutocompletionsToEditor(editor, newSuggestion, point) {
925
+ const editorPosition = editor.selection;
926
+ Transforms3.insertNodes(
927
+ editor,
928
+ [
929
+ {
930
+ type: "suggestion",
931
+ inline: true,
932
+ content: newSuggestion,
933
+ children: [{ text: "" }]
934
+ }
935
+ ],
936
+ {
937
+ at: point
938
+ }
939
+ );
940
+ if (editorPosition) {
941
+ editor.selection = editorPosition;
942
+ }
943
+ }
944
+
945
+ // src/lib/slatejs-edits/clear-autocompletions.ts
946
+ import { Element as Element3, Node as Node2, Transforms as Transforms4 } from "slate";
947
+ function clearAutocompletionsFromEditor(editor) {
948
+ const paths = [];
949
+ for (const [node, path] of Node2.nodes(editor)) {
950
+ if (Element3.isElement(node) && node.type === "suggestion") {
951
+ paths.push(path);
952
+ }
953
+ }
954
+ for (const path of paths) {
955
+ try {
956
+ Transforms4.removeNodes(editor, { at: path });
957
+ } catch (e) {
958
+ console.log(
959
+ "CopilotTextarea.clearAutocompletionsFromEditor: error removing node",
960
+ e
961
+ );
962
+ }
963
+ }
964
+ }
965
+
966
+ // src/components/hovering-toolbar/hovering-toolbar.tsx
967
+ import { useEffect as useEffect4, useRef as useRef2, useState as useState5 } from "react";
968
+ import { Transforms as Transforms5 } from "slate";
969
+ import { useSlate, useSlateSelection } from "slate-react";
970
+
971
+ // src/hooks/misc/use-autosize-textarea.tsx
972
+ import { useEffect as useEffect2 } from "react";
973
+ var useAutosizeTextArea = (textAreaRef, value) => {
974
+ useEffect2(() => {
975
+ if (textAreaRef.current !== null) {
976
+ textAreaRef.current.style.height = "0px";
977
+ const scrollHeight = textAreaRef.current.scrollHeight;
978
+ textAreaRef.current.style.height = scrollHeight + "px";
979
+ }
980
+ }, [textAreaRef, value]);
981
+ };
982
+ var use_autosize_textarea_default = useAutosizeTextArea;
983
+
984
+ // src/components/source-search-box/source-search-box.tsx
985
+ import { useState as useState2 } from "react";
986
+
987
+ // src/components/ui/command.tsx
988
+ import * as React2 from "react";
989
+ import { Command as CommandPrimitive } from "cmdk";
990
+ import { jsx } from "react/jsx-runtime";
991
+ "use client";
992
+ var Command = React2.forwardRef((_a, ref) => {
993
+ var _b = _a, { className } = _b, props = __objRest(_b, ["className"]);
994
+ return /* @__PURE__ */ jsx(CommandPrimitive, __spreadValues({
995
+ ref,
996
+ className: cn(
997
+ "flex h-full w-full flex-col overflow-hidden rounded-md bg-popover text-popover-foreground",
998
+ className
999
+ )
1000
+ }, props));
1001
+ });
1002
+ Command.displayName = CommandPrimitive.displayName;
1003
+ var CommandInput = React2.forwardRef((_a, ref) => {
1004
+ var _b = _a, { className } = _b, props = __objRest(_b, ["className"]);
1005
+ return /* @__PURE__ */ jsx("div", {
1006
+ className: "flex items-center border-b px-3",
1007
+ "cmdk-input-wrapper": "",
1008
+ children: /* @__PURE__ */ jsx(CommandPrimitive.Input, __spreadValues({
1009
+ ref,
1010
+ className: cn(
1011
+ "flex h-11 w-full rounded-md bg-transparent py-3 text-sm outline-none placeholder:text-muted-foreground disabled:cursor-not-allowed disabled:opacity-50",
1012
+ className
1013
+ )
1014
+ }, props))
1015
+ });
1016
+ });
1017
+ CommandInput.displayName = CommandPrimitive.Input.displayName;
1018
+ var CommandList = React2.forwardRef((_a, ref) => {
1019
+ var _b = _a, { className } = _b, props = __objRest(_b, ["className"]);
1020
+ return /* @__PURE__ */ jsx(CommandPrimitive.List, __spreadValues({
1021
+ ref,
1022
+ className: cn("max-h-[300px] overflow-y-auto overflow-x-hidden", className)
1023
+ }, props));
1024
+ });
1025
+ CommandList.displayName = CommandPrimitive.List.displayName;
1026
+ var CommandEmpty = React2.forwardRef((props, ref) => /* @__PURE__ */ jsx(CommandPrimitive.Empty, __spreadValues({
1027
+ ref,
1028
+ className: "py-6 text-center text-sm"
1029
+ }, props)));
1030
+ CommandEmpty.displayName = CommandPrimitive.Empty.displayName;
1031
+ var CommandGroup = React2.forwardRef((_a, ref) => {
1032
+ var _b = _a, { className } = _b, props = __objRest(_b, ["className"]);
1033
+ return /* @__PURE__ */ jsx(CommandPrimitive.Group, __spreadValues({
1034
+ ref,
1035
+ className: cn(
1036
+ "overflow-hidden p-1 text-foreground [&_[cmdk-group-heading]]:px-2 [&_[cmdk-group-heading]]:py-1.5 [&_[cmdk-group-heading]]:text-xs [&_[cmdk-group-heading]]:font-medium [&_[cmdk-group-heading]]:text-muted-foreground",
1037
+ className
1038
+ )
1039
+ }, props));
1040
+ });
1041
+ CommandGroup.displayName = CommandPrimitive.Group.displayName;
1042
+ var CommandSeparator = React2.forwardRef((_a, ref) => {
1043
+ var _b = _a, { className } = _b, props = __objRest(_b, ["className"]);
1044
+ return /* @__PURE__ */ jsx(CommandPrimitive.Separator, __spreadValues({
1045
+ ref,
1046
+ className: cn("-mx-1 h-px bg-border", className)
1047
+ }, props));
1048
+ });
1049
+ CommandSeparator.displayName = CommandPrimitive.Separator.displayName;
1050
+ var CommandItem = React2.forwardRef((_a, ref) => {
1051
+ var _b = _a, { className } = _b, props = __objRest(_b, ["className"]);
1052
+ return /* @__PURE__ */ jsx(CommandPrimitive.Item, __spreadValues({
1053
+ ref,
1054
+ className: cn(
1055
+ "relative flex cursor-default select-none items-center rounded-sm text-sm outline-none aria-selected:bg-accent aria-selected:text-accent-foreground data-[disabled]:pointer-events-none data-[disabled]:opacity-50",
1056
+ className
1057
+ )
1058
+ }, props));
1059
+ });
1060
+ CommandItem.displayName = CommandPrimitive.Item.displayName;
1061
+ var CommandShortcut = (_a) => {
1062
+ var _b = _a, {
1063
+ className
1064
+ } = _b, props = __objRest(_b, [
1065
+ "className"
1066
+ ]);
1067
+ return /* @__PURE__ */ jsx("span", __spreadValues({
1068
+ className: cn(
1069
+ "ml-auto text-xs tracking-widest text-muted-foreground",
1070
+ className
1071
+ )
1072
+ }, props));
1073
+ };
1074
+ CommandShortcut.displayName = "CommandShortcut";
1075
+
1076
+ // src/components/source-search-box/source-search-box.tsx
1077
+ import {
1078
+ Calculator,
1079
+ Calendar,
1080
+ Smile
1081
+ } from "lucide-react";
1082
+ import { jsx as jsx2, jsxs } from "react/jsx-runtime";
1083
+ function SourceSearchBox(props) {
1084
+ const [selectedValue, setSelectedValue] = useState2("");
1085
+ return /* @__PURE__ */ jsxs(Command, {
1086
+ className: "rounded-lg border shadow-md",
1087
+ value: selectedValue,
1088
+ onValueChange: (value) => {
1089
+ setSelectedValue(value);
1090
+ },
1091
+ filter: (value, search) => {
1092
+ if (props.searchTerm === "")
1093
+ return 1;
1094
+ if (value.startsWith(props.searchTerm))
1095
+ return 1;
1096
+ return 0;
1097
+ },
1098
+ children: [
1099
+ /* @__PURE__ */ jsx2(CommandInput, {
1100
+ value: props.searchTerm,
1101
+ className: "rounded-t-lg hidden",
1102
+ placeholder: "Search for a command..."
1103
+ }),
1104
+ /* @__PURE__ */ jsxs(CommandList, {
1105
+ children: [
1106
+ /* @__PURE__ */ jsx2(CommandEmpty, {
1107
+ children: "No results found."
1108
+ }),
1109
+ /* @__PURE__ */ jsx2(CommandGroup, {
1110
+ heading: "Recents",
1111
+ children: props.recentFiles.map((filePointer) => {
1112
+ return /* @__PURE__ */ jsx2(CommandItem, {
1113
+ value: filePointer.name,
1114
+ onSelect: (value) => {
1115
+ console.log(filePointer.name);
1116
+ props.onSelectedFile(filePointer);
1117
+ },
1118
+ children: /* @__PURE__ */ jsxs("div", {
1119
+ className: "flex flex-row gap-3 items-center",
1120
+ children: [
1121
+ /* @__PURE__ */ jsx2(Logo, {
1122
+ size: "30px",
1123
+ children: /* @__PURE__ */ jsx2(IconForFilePointer, {
1124
+ filePointer,
1125
+ className: "mx-auto my-auto"
1126
+ })
1127
+ }),
1128
+ filePointer.name
1129
+ ]
1130
+ })
1131
+ }, `word-${filePointer.sourceApplication}.${filePointer.name}`);
1132
+ })
1133
+ }),
1134
+ /* @__PURE__ */ jsxs(CommandGroup, {
1135
+ heading: "Suggestions",
1136
+ children: [
1137
+ /* @__PURE__ */ jsxs(CommandItem, {
1138
+ onSelect: (value) => {
1139
+ console.log(value);
1140
+ console.log(value);
1141
+ },
1142
+ children: [
1143
+ /* @__PURE__ */ jsx2(Calendar, {
1144
+ className: "mr-2 h-4 w-4"
1145
+ }),
1146
+ /* @__PURE__ */ jsx2("span", {
1147
+ children: "Calendar"
1148
+ })
1149
+ ]
1150
+ }),
1151
+ /* @__PURE__ */ jsxs(CommandItem, {
1152
+ children: [
1153
+ /* @__PURE__ */ jsx2(Smile, {
1154
+ className: "mr-2 h-4 w-4"
1155
+ }),
1156
+ /* @__PURE__ */ jsx2("span", {
1157
+ children: "Search Emoji"
1158
+ })
1159
+ ]
1160
+ }),
1161
+ /* @__PURE__ */ jsxs(CommandItem, {
1162
+ children: [
1163
+ /* @__PURE__ */ jsx2(Calculator, {
1164
+ className: "mr-2 h-4 w-4"
1165
+ }),
1166
+ /* @__PURE__ */ jsx2("span", {
1167
+ children: "Calculator"
1168
+ })
1169
+ ]
1170
+ })
1171
+ ]
1172
+ }),
1173
+ /* @__PURE__ */ jsx2(CommandSeparator, {})
1174
+ ]
1175
+ })
1176
+ ]
1177
+ });
1178
+ }
1179
+ function Logo({
1180
+ children,
1181
+ size = "30px"
1182
+ }) {
1183
+ return /* @__PURE__ */ jsx2("div", {
1184
+ className: "flex items-center justify-center",
1185
+ style: { width: size, height: size },
1186
+ children
1187
+ });
1188
+ }
1189
+
1190
+ // src/components/ui/button.tsx
1191
+ import * as React3 from "react";
1192
+ import { Slot } from "@radix-ui/react-slot";
1193
+ import { cva } from "class-variance-authority";
1194
+ import { jsx as jsx3 } from "react/jsx-runtime";
1195
+ var buttonVariants = cva(
1196
+ "inline-flex items-center justify-center rounded-md text-sm font-medium ring-offset-background transition-colors focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring focus-visible:ring-offset-2 disabled:pointer-events-none disabled:opacity-50",
1197
+ {
1198
+ variants: {
1199
+ variant: {
1200
+ default: "bg-primary text-primary-foreground hover:bg-primary/90",
1201
+ destructive: "bg-destructive text-destructive-foreground hover:bg-destructive/90",
1202
+ outline: "border border-input bg-background hover:bg-accent hover:text-accent-foreground",
1203
+ secondary: "bg-secondary text-secondary-foreground hover:bg-secondary/80",
1204
+ ghost: "hover:bg-accent hover:text-accent-foreground",
1205
+ link: "text-primary underline-offset-4 hover:underline"
1206
+ },
1207
+ size: {
1208
+ default: "h-10 px-4 py-2",
1209
+ sm: "h-9 rounded-md px-3",
1210
+ lg: "h-11 rounded-md px-8",
1211
+ icon: "h-10 w-10"
1212
+ }
1213
+ },
1214
+ defaultVariants: {
1215
+ variant: "default",
1216
+ size: "default"
1217
+ }
1218
+ }
1219
+ );
1220
+ var Button = React3.forwardRef(
1221
+ (_a, ref) => {
1222
+ var _b = _a, { className, variant, size, asChild = false } = _b, props = __objRest(_b, ["className", "variant", "size", "asChild"]);
1223
+ const Comp = asChild ? Slot : "button";
1224
+ return /* @__PURE__ */ jsx3(Comp, __spreadValues({
1225
+ className: cn(buttonVariants({ variant, size, className })),
1226
+ ref
1227
+ }, props));
1228
+ }
1229
+ );
1230
+ Button.displayName = "Button";
1231
+
1232
+ // src/components/ui/label.tsx
1233
+ import * as React4 from "react";
1234
+ import * as LabelPrimitive from "@radix-ui/react-label";
1235
+ import { cva as cva2 } from "class-variance-authority";
1236
+ import { jsx as jsx4 } from "react/jsx-runtime";
1237
+ "use client";
1238
+ var labelVariants = cva2(
1239
+ "text-sm font-medium leading-none peer-disabled:cursor-not-allowed peer-disabled:opacity-70"
1240
+ );
1241
+ var Label = React4.forwardRef((_a, ref) => {
1242
+ var _b = _a, { className } = _b, props = __objRest(_b, ["className"]);
1243
+ return /* @__PURE__ */ jsx4(LabelPrimitive.Root, __spreadValues({
1244
+ ref,
1245
+ className: cn(labelVariants(), className)
1246
+ }, props));
1247
+ });
1248
+ Label.displayName = LabelPrimitive.Root.displayName;
1249
+
1250
+ // src/components/hovering-toolbar/text-insertion-prompt-box/hovering-insertion-prompt-box-core.tsx
1251
+ import { useEffect as useEffect3, useRef, useState as useState3 } from "react";
1252
+ import Chip from "@mui/material/Chip";
1253
+ import Avatar from "@mui/material/Avatar";
1254
+
1255
+ // src/lib/stream-promise-flatten.ts
1256
+ function streamPromiseFlatten(promise) {
1257
+ return new ReadableStream({
1258
+ start(controller) {
1259
+ return __async(this, null, function* () {
1260
+ try {
1261
+ const stream = yield promise;
1262
+ const reader = stream.getReader();
1263
+ while (true) {
1264
+ const { done, value } = yield reader.read();
1265
+ if (done) {
1266
+ controller.close();
1267
+ return;
1268
+ }
1269
+ controller.enqueue(value);
1270
+ }
1271
+ } catch (error) {
1272
+ controller.error(error);
1273
+ }
1274
+ });
1275
+ }
1276
+ });
1277
+ }
1278
+
1279
+ // src/components/hovering-toolbar/text-insertion-prompt-box/hovering-insertion-prompt-box-core.tsx
1280
+ import { Fragment, jsx as jsx5, jsxs as jsxs2 } from "react/jsx-runtime";
1281
+ var HoveringInsertionPromptBoxCore = ({ performInsertion, state, insertionOrEditingFunction }) => {
1282
+ const [editSuggestion, setEditSuggestion] = useState3("");
1283
+ const [suggestionIsLoading, setSuggestionIsLoading] = useState3(false);
1284
+ const [adjustmentPrompt, setAdjustmentPrompt] = useState3("");
1285
+ const [generatingSuggestion, setGeneratingSuggestion] = useState3(null);
1286
+ const adjustmentTextAreaRef = useRef(null);
1287
+ const suggestionTextAreaRef = useRef(null);
1288
+ const [filePointers, setFilePointers] = useState3([]);
1289
+ use_autosize_textarea_default(suggestionTextAreaRef, editSuggestion || "");
1290
+ use_autosize_textarea_default(adjustmentTextAreaRef, adjustmentPrompt || "");
1291
+ useEffect3(() => {
1292
+ var _a;
1293
+ (_a = adjustmentTextAreaRef.current) == null ? void 0 : _a.focus();
1294
+ }, []);
1295
+ useEffect3(() => {
1296
+ if (!generatingSuggestion) {
1297
+ return;
1298
+ }
1299
+ if (generatingSuggestion.locked) {
1300
+ return;
1301
+ }
1302
+ setEditSuggestion("");
1303
+ const reader = generatingSuggestion.getReader();
1304
+ const read = () => __async(void 0, null, function* () {
1305
+ setSuggestionIsLoading(true);
1306
+ while (true) {
1307
+ const { done, value } = yield reader.read();
1308
+ if (done) {
1309
+ break;
1310
+ }
1311
+ setEditSuggestion((prev) => {
1312
+ const newSuggestion = prev + value;
1313
+ if (suggestionTextAreaRef.current) {
1314
+ suggestionTextAreaRef.current.scrollTop = suggestionTextAreaRef.current.scrollHeight;
1315
+ }
1316
+ return newSuggestion;
1317
+ });
1318
+ }
1319
+ setSuggestionIsLoading(false);
1320
+ });
1321
+ read();
1322
+ return () => {
1323
+ const releaseLockIfNotClosed = () => __async(void 0, null, function* () {
1324
+ try {
1325
+ yield reader.closed;
1326
+ } catch (e) {
1327
+ reader.releaseLock();
1328
+ }
1329
+ });
1330
+ releaseLockIfNotClosed();
1331
+ };
1332
+ }, [generatingSuggestion]);
1333
+ const begingGeneratingAdjustment = () => __async(void 0, null, function* () {
1334
+ if (!adjustmentPrompt.trim()) {
1335
+ return;
1336
+ }
1337
+ let editorState2 = state.editorState;
1338
+ if (editSuggestion !== "") {
1339
+ editorState2.selectedText = editSuggestion;
1340
+ }
1341
+ const adjustmentSuggestionTextStreamPromise = insertionOrEditingFunction(
1342
+ editorState2,
1343
+ adjustmentPrompt,
1344
+ new AbortController().signal
1345
+ );
1346
+ const adjustmentSuggestionTextStream = streamPromiseFlatten(
1347
+ adjustmentSuggestionTextStreamPromise
1348
+ );
1349
+ setGeneratingSuggestion(adjustmentSuggestionTextStream);
1350
+ });
1351
+ const isLoading = suggestionIsLoading;
1352
+ const textToEdit = editSuggestion || state.editorState.selectedText;
1353
+ const adjustmentLabel = textToEdit === "" ? "Describe the text you want to insert" : "Describe adjustments to the suggested text";
1354
+ const placeholder = textToEdit === "" ? "e.g. 'summarize the client's top 3 pain-points from @CallTranscript'" : "e.g. 'make it more formal', 'be more specific', ...";
1355
+ const AdjustmentPromptComponent = /* @__PURE__ */ jsxs2(Fragment, {
1356
+ children: [
1357
+ /* @__PURE__ */ jsx5(Label, {
1358
+ className: "",
1359
+ children: adjustmentLabel
1360
+ }),
1361
+ /* @__PURE__ */ jsxs2("div", {
1362
+ className: "relative w-full flex items-center",
1363
+ children: [
1364
+ /* @__PURE__ */ jsx5("textarea", {
1365
+ disabled: suggestionIsLoading,
1366
+ ref: adjustmentTextAreaRef,
1367
+ value: adjustmentPrompt,
1368
+ onChange: (e) => setAdjustmentPrompt(e.target.value),
1369
+ onKeyDown: (e) => {
1370
+ if (e.key === "Enter" && e.shiftKey) {
1371
+ e.preventDefault();
1372
+ setAdjustmentPrompt(adjustmentPrompt + "\n");
1373
+ } else if (e.key === "Enter") {
1374
+ e.preventDefault();
1375
+ begingGeneratingAdjustment();
1376
+ }
1377
+ },
1378
+ placeholder,
1379
+ style: { minHeight: "3rem" },
1380
+ className: "w-full bg-slate-100 h-auto h-min-14 text-sm p-2 rounded-md resize-none overflow-visible focus:outline-none focus:ring-0 focus:border-non pr-[3rem]",
1381
+ rows: 1
1382
+ }),
1383
+ /* @__PURE__ */ jsx5("button", {
1384
+ onClick: begingGeneratingAdjustment,
1385
+ className: "absolute right-2 bg-blue-500 text-white w-8 h-8 rounded-full flex items-center justify-center",
1386
+ children: /* @__PURE__ */ jsx5("i", {
1387
+ className: "material-icons",
1388
+ children: "arrow_forward"
1389
+ })
1390
+ })
1391
+ ]
1392
+ })
1393
+ ]
1394
+ });
1395
+ const SuggestionComponent = /* @__PURE__ */ jsxs2(Fragment, {
1396
+ children: [
1397
+ /* @__PURE__ */ jsxs2("div", {
1398
+ className: "flex justify-between items-end w-full",
1399
+ children: [
1400
+ /* @__PURE__ */ jsx5(Label, {
1401
+ className: "mt-4",
1402
+ children: "Suggested:"
1403
+ }),
1404
+ /* @__PURE__ */ jsx5("div", {
1405
+ className: "ml-auto",
1406
+ children: isLoading && /* @__PURE__ */ jsx5("div", {
1407
+ className: "flex justify-center items-center",
1408
+ children: /* @__PURE__ */ jsx5("div", {
1409
+ className: "inline-block h-4 w-4 animate-spin rounded-full border-4 border-solid border-current border-r-transparent align-[-0.125em] motion-reduce:animate-[spin_1.5s_linear_infinite]",
1410
+ role: "status",
1411
+ children: /* @__PURE__ */ jsx5("span", {
1412
+ className: "!absolute !-m-px !h-px !w-px !overflow-hidden !whitespace-nowrap !border-0 !p-0 ![clip:rect(0,0,0,0)]",
1413
+ children: "Loading..."
1414
+ })
1415
+ })
1416
+ })
1417
+ })
1418
+ ]
1419
+ }),
1420
+ /* @__PURE__ */ jsx5("textarea", {
1421
+ ref: suggestionTextAreaRef,
1422
+ value: editSuggestion,
1423
+ disabled: suggestionIsLoading,
1424
+ onChange: (e) => setEditSuggestion(e.target.value),
1425
+ className: "w-full text-base p-2 border border-gray-300 rounded-md resize-none bg-green-50",
1426
+ style: { overflow: "auto", maxHeight: "10em" }
1427
+ })
1428
+ ]
1429
+ });
1430
+ const SubmitComponent = /* @__PURE__ */ jsx5("div", {
1431
+ className: "flex w-full gap-4 justify-start",
1432
+ children: /* @__PURE__ */ jsxs2(Button, {
1433
+ className: " bg-green-700 text-white",
1434
+ onClick: () => {
1435
+ performInsertion(editSuggestion);
1436
+ },
1437
+ children: [
1438
+ "Insert ",
1439
+ /* @__PURE__ */ jsx5("i", {
1440
+ className: "material-icons",
1441
+ children: "check"
1442
+ })
1443
+ ]
1444
+ })
1445
+ });
1446
+ const sourceSearchCandidate = adjustmentPrompt.split(" ").pop();
1447
+ const sourceSearchWord = (sourceSearchCandidate == null ? void 0 : sourceSearchCandidate.startsWith("@")) ? sourceSearchCandidate.slice(1) : void 0;
1448
+ return /* @__PURE__ */ jsxs2("div", {
1449
+ className: "w-full flex flex-col items-start relative gap-2",
1450
+ children: [
1451
+ AdjustmentPromptComponent,
1452
+ filePointers.length > 0 && /* @__PURE__ */ jsx5(IncludedFilesPreview, {
1453
+ includedFiles: filePointers,
1454
+ setIncludedFiles: setFilePointers
1455
+ }),
1456
+ sourceSearchWord !== void 0 && /* @__PURE__ */ jsx5(SourceSearchBox, {
1457
+ searchTerm: sourceSearchWord,
1458
+ recentFiles: mockFiles,
1459
+ onSelectedFile: (filePointer) => {
1460
+ var _a;
1461
+ setAdjustmentPrompt(
1462
+ adjustmentPrompt.replace(new RegExp(`@${sourceSearchWord}$`), "")
1463
+ );
1464
+ setFilePointers((prev) => [...prev, filePointer]);
1465
+ (_a = adjustmentTextAreaRef.current) == null ? void 0 : _a.focus();
1466
+ }
1467
+ }),
1468
+ generatingSuggestion ? SuggestionComponent : null,
1469
+ generatingSuggestion ? SubmitComponent : null
1470
+ ]
1471
+ });
1472
+ };
1473
+ var IncludedFilesPreview = ({
1474
+ includedFiles,
1475
+ setIncludedFiles
1476
+ }) => {
1477
+ return /* @__PURE__ */ jsxs2("div", {
1478
+ className: "flex flex-col gap-2 mt-2",
1479
+ children: [
1480
+ /* @__PURE__ */ jsx5(Label, {
1481
+ className: "",
1482
+ children: "Included context:"
1483
+ }),
1484
+ /* @__PURE__ */ jsx5("div", {
1485
+ className: "flex flex-wrap gap-2",
1486
+ children: includedFiles.map((filePointer, index) => {
1487
+ return /* @__PURE__ */ jsx5(FileChipPreview, {
1488
+ filePointer,
1489
+ onDelete: () => {
1490
+ setIncludedFiles(
1491
+ (prev) => prev.filter((fp) => fp !== filePointer)
1492
+ );
1493
+ }
1494
+ }, `file-${filePointer.sourceApplication}.${filePointer.name}`);
1495
+ })
1496
+ })
1497
+ ]
1498
+ });
1499
+ };
1500
+ var FileChipPreview = ({
1501
+ filePointer,
1502
+ onDelete
1503
+ }) => {
1504
+ return /* @__PURE__ */ jsx5(Chip, {
1505
+ label: filePointer.name,
1506
+ onDelete,
1507
+ avatar: /* @__PURE__ */ jsx5(Avatar, {
1508
+ sx: { backgroundColor: "transparent" },
1509
+ children: /* @__PURE__ */ jsx5(IconForFilePointer, {
1510
+ filePointer,
1511
+ className: "w-4 h-4 object-contain"
1512
+ })
1513
+ })
1514
+ });
1515
+ };
1516
+ function IconForFilePointer({
1517
+ filePointer,
1518
+ className
1519
+ }) {
1520
+ if (filePointer.sourceApplication === "Salesforce") {
1521
+ return /* @__PURE__ */ jsx5(IconSalesforce, {
1522
+ className
1523
+ });
1524
+ } else {
1525
+ return /* @__PURE__ */ jsx5(IconSalesforce, {
1526
+ className
1527
+ });
1528
+ }
1529
+ }
1530
+ function IconSalesforce(_a) {
1531
+ var _b = _a, { className } = _b, props = __objRest(_b, ["className"]);
1532
+ return /* @__PURE__ */ jsxs2("svg", __spreadProps(__spreadValues({
1533
+ xmlns: "http://www.w3.org/2000/svg",
1534
+ xmlnsXlink: "http://www.w3.org/1999/xlink",
1535
+ preserveAspectRatio: "xMidYMid meet",
1536
+ viewBox: "0 0 273 191"
1537
+ }, props), {
1538
+ children: [
1539
+ /* @__PURE__ */ jsx5("title", {
1540
+ children: "Salesforce.com logo"
1541
+ }),
1542
+ /* @__PURE__ */ jsx5("defs", {
1543
+ children: /* @__PURE__ */ jsx5("path", {
1544
+ id: "a",
1545
+ d: "M.06.5h272v190H.06z"
1546
+ })
1547
+ }),
1548
+ /* @__PURE__ */ jsxs2("g", {
1549
+ fillRule: "evenodd",
1550
+ children: [
1551
+ /* @__PURE__ */ jsx5("mask", {
1552
+ id: "b",
1553
+ fill: "#fff",
1554
+ children: /* @__PURE__ */ jsx5("use", {
1555
+ xlinkHref: "#a"
1556
+ })
1557
+ }),
1558
+ /* @__PURE__ */ jsx5("path", {
1559
+ fill: "#00A1E0",
1560
+ d: "M113 21.3c8.78-9.14 21-14.8 34.5-14.8 18 0 33.6 10 42 24.9a58 58 0 0 1 23.7-5.05c32.4 0 58.7 26.5 58.7 59.2s-26.3 59.2-58.7 59.2c-3.96 0-7.82-.398-11.6-1.15-7.35 13.1-21.4 22-37.4 22a42.7 42.7 0 0 1-18.8-4.32c-7.45 17.5-24.8 29.8-45 29.8-21.1 0-39-13.3-45.9-32a45.1 45.1 0 0 1-9.34.972c-25.1 0-45.4-20.6-45.4-45.9 0-17 9.14-31.8 22.7-39.8a52.6 52.6 0 0 1-4.35-21c0-29.2 23.7-52.8 52.9-52.8 17.1 0 32.4 8.15 42 20.8",
1561
+ mask: "url(#b)"
1562
+ }),
1563
+ /* @__PURE__ */ jsx5("path", {
1564
+ fill: "#FFFFFE",
1565
+ d: "M39.4 99.3c-.171.446.061.539.116.618.511.37 1.03.638 1.55.939 2.78 1.47 5.4 1.9 8.14 1.9 5.58 0 9.05-2.97 9.05-7.75v-.094c0-4.42-3.92-6.03-7.58-7.18l-.479-.155c-2.77-.898-5.16-1.68-5.16-3.5v-.093c0-1.56 1.4-2.71 3.56-2.71 2.4 0 5.26.799 7.09 1.81 0 0 .542.35.739-.173.107-.283 1.04-2.78 1.14-3.06.106-.293-.08-.514-.271-.628-2.1-1.28-5-2.15-8-2.15l-.557.002c-5.11 0-8.68 3.09-8.68 7.51v.095c0 4.66 3.94 6.18 7.62 7.23l.592.184c2.68.824 5 1.54 5 3.42v.094c0 1.73-1.51 3.02-3.93 3.02-.941 0-3.94-.016-7.19-2.07-.393-.229-.617-.394-.92-.579-.16-.097-.56-.272-.734.252l-1.1 3.06m81.7 0c-.171.446.061.539.118.618.509.37 1.03.638 1.55.939 2.78 1.47 5.4 1.9 8.14 1.9 5.58 0 9.05-2.97 9.05-7.75v-.094c0-4.42-3.91-6.03-7.58-7.18l-.479-.155c-2.77-.898-5.16-1.68-5.16-3.5v-.093c0-1.56 1.4-2.71 3.56-2.71 2.4 0 5.25.799 7.09 1.81 0 0 .542.35.74-.173.106-.283 1.04-2.78 1.13-3.06.107-.293-.08-.514-.27-.628-2.1-1.28-5-2.15-8-2.15l-.558.002c-5.11 0-8.68 3.09-8.68 7.51v.095c0 4.66 3.94 6.18 7.62 7.23l.591.184c2.69.824 5 1.54 5 3.42v.094c0 1.73-1.51 3.02-3.93 3.02-.943 0-3.95-.016-7.19-2.07-.393-.229-.623-.387-.921-.579-.101-.064-.572-.248-.733.252l-1.1 3.06m55.8-9.36c0 2.7-.504 4.83-1.49 6.34-.984 1.49-2.47 2.22-4.54 2.22s-3.55-.724-4.52-2.21c-.977-1.5-1.47-3.64-1.47-6.34 0-2.7.496-4.82 1.47-6.31.968-1.48 2.44-2.19 4.52-2.19s3.56.717 4.54 2.19c.992 1.49 1.49 3.61 1.49 6.31m4.66-5.01c-.459-1.55-1.17-2.91-2.12-4.05a10.151 10.151 0 0 0-3.58-2.72c-1.42-.665-3.1-1-5-1s-3.57.337-5 1c-1.42.664-2.63 1.58-3.58 2.72-.948 1.14-1.66 2.5-2.12 4.05-.455 1.54-.686 3.22-.686 5.01 0 1.79.231 3.47.686 5.01.457 1.55 1.17 2.91 2.12 4.05.951 1.14 2.16 2.05 3.58 2.7 1.43.648 3.11.978 5 .978 1.89 0 3.57-.33 4.99-.978 1.42-.648 2.63-1.56 3.58-2.7.949-1.14 1.66-2.5 2.12-4.05.454-1.54.685-3.22.685-5.01 0-1.78-.231-3.47-.685-5.01m38.3 12.8c-.153-.453-.595-.282-.595-.282-.677.259-1.4.499-2.17.619-.776.122-1.64.183-2.55.183-2.25 0-4.05-.671-5.33-2-1.29-1.33-2.01-3.47-2-6.37.007-2.64.645-4.62 1.79-6.14 1.13-1.5 2.87-2.28 5.17-2.28 1.92 0 3.39.223 4.93.705 0 0 .365.159.54-.322.409-1.13.711-1.94 1.15-3.18.124-.355-.18-.505-.291-.548-.604-.236-2.03-.623-3.11-.786-1.01-.154-2.18-.234-3.5-.234-1.96 0-3.7.335-5.19.999-1.49.663-2.75 1.58-3.75 2.72-1 1.14-1.76 2.5-2.27 4.05-.505 1.54-.76 3.23-.76 5.02 0 3.86 1.04 6.99 3.1 9.28 2.06 2.3 5.16 3.46 9.2 3.46 2.39 0 4.84-.483 6.6-1.18 0 0 .336-.162.19-.554l-1.15-3.16m8.15-10.4c.223-1.5.634-2.75 1.28-3.72.967-1.48 2.44-2.29 4.51-2.29s3.44.814 4.42 2.29c.65.975.934 2.27 1.04 3.72l-11.3-.002zm15.7-3.3c-.397-1.49-1.38-3-2.02-3.69-1.02-1.09-2.01-1.86-3-2.28a11.5 11.5 0 0 0-4.52-.917c-1.97 0-3.76.333-5.21 1.01-1.45.682-2.67 1.61-3.63 2.77-.959 1.16-1.68 2.53-2.14 4.1-.46 1.55-.692 3.25-.692 5.03 0 1.82.241 3.51.715 5.04.479 1.54 1.25 2.89 2.29 4.01 1.04 1.13 2.37 2.01 3.97 2.63 1.59.615 3.52.934 5.73.927 4.56-.015 6.96-1.03 7.94-1.58.175-.098.34-.267.134-.754l-1.03-2.89c-.158-.431-.594-.275-.594-.275-1.13.422-2.73 1.18-6.48 1.17-2.45-.004-4.26-.727-5.4-1.86-1.16-1.16-1.74-2.85-1.83-5.25l15.8.012s.416-.004.459-.41c.017-.168.541-3.24-.471-6.79zm-142 3.3c.223-1.5.635-2.75 1.28-3.72.968-1.48 2.44-2.29 4.51-2.29s3.44.814 4.42 2.29c.649.975.933 2.27 1.04 3.72l-11.3-.002zm15.7-3.3c-.396-1.49-1.38-3-2.02-3.69-1.02-1.09-2.01-1.86-3-2.28a11.5 11.5 0 0 0-4.52-.917c-1.97 0-3.76.333-5.21 1.01-1.45.682-2.67 1.61-3.63 2.77-.957 1.16-1.68 2.53-2.14 4.1-.459 1.55-.69 3.25-.69 5.03 0 1.82.239 3.51.716 5.04.478 1.54 1.25 2.89 2.28 4.01 1.04 1.13 2.37 2.01 3.97 2.63 1.59.615 3.51.934 5.73.927 4.56-.015 6.96-1.03 7.94-1.58.174-.098.34-.267.133-.754l-1.03-2.89c-.159-.431-.595-.275-.595-.275-1.13.422-2.73 1.18-6.48 1.17-2.44-.004-4.26-.727-5.4-1.86-1.16-1.16-1.74-2.85-1.83-5.25l15.8.012s.416-.004.459-.41c.017-.168.541-3.24-.472-6.79zm-49.8 13.6c-.619-.494-.705-.615-.91-.936-.313-.483-.473-1.17-.473-2.05 0-1.38.46-2.38 1.41-3.05-.01.002 1.36-1.18 4.58-1.14a32 32 0 0 1 4.28.365v7.17h.002s-2 .431-4.26.567c-3.21.193-4.63-.924-4.62-.921zm6.28-11.1c-.64-.047-1.47-.07-2.46-.07-1.35 0-2.66.168-3.88.498-1.23.332-2.34.846-3.29 1.53a7.63 7.63 0 0 0-2.29 2.6c-.559 1.04-.844 2.26-.844 3.64 0 1.4.243 2.61.723 3.6a6.54 6.54 0 0 0 2.06 2.47c.877.638 1.96 1.11 3.21 1.39 1.24.283 2.64.426 4.18.426 1.62 0 3.23-.136 4.79-.399a95.1 95.1 0 0 0 3.97-.772c.526-.121 1.11-.28 1.11-.28.39-.099.36-.516.36-.516l-.009-14.4c0-3.16-.844-5.51-2.51-6.96-1.66-1.45-4.09-2.18-7.24-2.18-1.18 0-3.09.16-4.23.389 0 0-3.44.668-4.86 1.78 0 0-.312.192-.142.627l1.12 3c.139.389.518.256.518.256s.119-.047.259-.13c3.03-1.65 6.87-1.6 6.87-1.6 1.7 0 3.02.345 3.9 1.02.861.661 1.3 1.66 1.3 3.76v.667c-1.35-.196-2.6-.309-2.6-.309zm127-8.13a.428.428 0 0 0-.237-.568c-.269-.102-1.61-.385-2.64-.449-1.98-.124-3.08.21-4.07.654-.978.441-2.06 1.15-2.66 1.97l-.002-1.92c0-.264-.187-.477-.453-.477h-4.04c-.262 0-.452.213-.452.477v23.5a.48.48 0 0 0 .479.479h4.14a.479.479 0 0 0 .478-.479v-11.8c0-1.58.174-3.15.521-4.14.342-.979.807-1.76 1.38-2.32a4.79 4.79 0 0 1 1.95-1.17 7.68 7.68 0 0 1 2.12-.298c.825 0 1.73.212 1.73.212.304.034.473-.152.576-.426.271-.721 1.04-2.88 1.19-3.31"
1566
+ }),
1567
+ /* @__PURE__ */ jsx5("path", {
1568
+ fill: "#FFFFFE",
1569
+ d: "M162.201 67.548a13.258 13.258 0 0 0-1.559-.37 12.217 12.217 0 0 0-2.144-.166c-2.853 0-5.102.806-6.681 2.398-1.568 1.58-2.635 3.987-3.17 7.154l-.193 1.069h-3.581s-.437-.018-.529.459l-.588 3.28c-.041.314.094.51.514.508h3.486l-3.537 19.743c-.277 1.59-.594 2.898-.945 3.889-.346.978-.684 1.711-1.1 2.243-.403.515-.785.894-1.444 1.115-.544.183-1.17.267-1.856.267-.382 0-.89-.064-1.265-.139-.375-.074-.57-.158-.851-.276 0 0-.409-.156-.57.254-.131.335-1.06 2.89-1.17 3.206-.112.312.045.558.243.629.464.166.809.272 1.441.421.878.207 1.618.22 2.311.22 1.452 0 2.775-.204 3.872-.6 1.104-.399 2.065-1.094 2.915-2.035.919-1.015 1.497-2.078 2.05-3.528.547-1.437 1.013-3.221 1.386-5.3l3.554-20.109h5.196s.438.016.529-.459l.588-3.28c.041-.314-.093-.51-.515-.508h-5.043c.025-.114.254-1.888.833-3.558.247-.713.712-1.288 1.106-1.683a3.273 3.273 0 0 1 1.321-.822 5.48 5.48 0 0 1 1.693-.244c.475 0 .941.057 1.296.131.489.104.679.159.807.197.514.157.583.005.684-.244l1.206-3.312c.124-.356-.178-.506-.29-.55m-70.474 34.117c0 .264-.188.479-.452.479h-4.183c-.265 0-.453-.215-.453-.479V67.997c0-.263.188-.476.453-.476h4.183c.264 0 .452.213.452.476v33.668"
1570
+ })
1571
+ ]
1572
+ })
1573
+ ]
1574
+ }));
1575
+ }
1576
+ var mockFiles = [
1577
+ {
1578
+ name: "prospecting call transcript",
1579
+ sourceApplication: "Salesforce",
1580
+ getContents: () => __async(void 0, null, function* () {
1581
+ return "some contents";
1582
+ })
1583
+ },
1584
+ {
1585
+ name: "customer feedback",
1586
+ sourceApplication: "Zendesk",
1587
+ getContents: () => __async(void 0, null, function* () {
1588
+ return "some contents";
1589
+ })
1590
+ },
1591
+ {
1592
+ name: "product specifications",
1593
+ sourceApplication: "Google Docs",
1594
+ getContents: () => __async(void 0, null, function* () {
1595
+ return "some contents";
1596
+ })
1597
+ },
1598
+ {
1599
+ name: "meeting minutes",
1600
+ sourceApplication: "Microsoft Teams",
1601
+ getContents: () => __async(void 0, null, function* () {
1602
+ return "some contents";
1603
+ })
1604
+ },
1605
+ {
1606
+ name: "project plan",
1607
+ sourceApplication: "Trello",
1608
+ getContents: () => __async(void 0, null, function* () {
1609
+ return "some contents";
1610
+ })
1611
+ },
1612
+ {
1613
+ name: "code review comments",
1614
+ sourceApplication: "Github",
1615
+ getContents: () => __async(void 0, null, function* () {
1616
+ return "some contents";
1617
+ })
1618
+ }
1619
+ ];
1620
+
1621
+ // src/components/hovering-toolbar/text-insertion-prompt-box/hovering-insertion-prompt-box.tsx
1622
+ import { jsx as jsx6 } from "react/jsx-runtime";
1623
+ var HoveringInsertionPromptBox = (props) => {
1624
+ return /* @__PURE__ */ jsx6("div", {
1625
+ className: "flex flex-col justify-center items-center space-y-4 rounded-md border shadow-lg p-4 border-gray- bg-white",
1626
+ style: { width: "35rem" },
1627
+ children: /* @__PURE__ */ jsx6(HoveringInsertionPromptBoxCore, {
1628
+ state: {
1629
+ editorState: props.editorState
1630
+ },
1631
+ performInsertion: props.performInsertion,
1632
+ insertionOrEditingFunction: props.apiConfig.insertionOrEditingFunction
1633
+ })
1634
+ });
1635
+ };
1636
+
1637
+ // src/components/hovering-toolbar/hovering-toolbar-components.tsx
1638
+ import { css, cx } from "@emotion/css";
1639
+ import React6 from "react";
1640
+ import ReactDOM from "react-dom";
1641
+ import { jsx as jsx7 } from "react/jsx-runtime";
1642
+ var Button2 = React6.forwardRef(
1643
+ (_a, ref) => {
1644
+ var _b = _a, {
1645
+ className,
1646
+ active,
1647
+ reversed
1648
+ } = _b, props = __objRest(_b, [
1649
+ "className",
1650
+ "active",
1651
+ "reversed"
1652
+ ]);
1653
+ return /* @__PURE__ */ jsx7("span", __spreadProps(__spreadValues({}, props), {
1654
+ ref,
1655
+ className: cx(
1656
+ className,
1657
+ css`
1658
+ cursor: pointer;
1659
+ color: ${reversed ? active ? "white" : "#aaa" : active ? "black" : "#ccc"};
1660
+ `
1661
+ )
1662
+ }));
1663
+ }
1664
+ );
1665
+ var Icon = React6.forwardRef(
1666
+ (_a, ref) => {
1667
+ var _b = _a, { className } = _b, props = __objRest(_b, ["className"]);
1668
+ return /* @__PURE__ */ jsx7("span", __spreadProps(__spreadValues({}, props), {
1669
+ ref,
1670
+ className: cx(
1671
+ "material-icons",
1672
+ className,
1673
+ css`
1674
+ font-size: 18px;
1675
+ vertical-align: text-bottom;
1676
+ `
1677
+ )
1678
+ }));
1679
+ }
1680
+ );
1681
+ var Menu = React6.forwardRef(
1682
+ (_a, ref) => {
1683
+ var _b = _a, { className } = _b, props = __objRest(_b, ["className"]);
1684
+ return /* @__PURE__ */ jsx7("div", __spreadProps(__spreadValues({}, props), {
1685
+ "data-test-id": "menu",
1686
+ ref,
1687
+ className: cx(
1688
+ className,
1689
+ css`
1690
+ & > * {
1691
+ display: inline-block;
1692
+ }
1693
+
1694
+ & > * + * {
1695
+ margin-left: 15px;
1696
+ }
1697
+ `
1698
+ )
1699
+ }));
1700
+ }
1701
+ );
1702
+ var Portal = ({ children }) => {
1703
+ return typeof document === "object" ? ReactDOM.createPortal(children, document.body) : null;
1704
+ };
1705
+ var Toolbar = React6.forwardRef(
1706
+ (_a, ref) => {
1707
+ var _b = _a, { className } = _b, props = __objRest(_b, ["className"]);
1708
+ return /* @__PURE__ */ jsx7(Menu, __spreadProps(__spreadValues({}, props), {
1709
+ ref,
1710
+ className: cx(
1711
+ className,
1712
+ css`
1713
+ position: relative;
1714
+ padding: 1px 18px 17px;
1715
+ margin: 0 -20px;
1716
+ border-bottom: 2px solid #eee;
1717
+ margin-bottom: 20px;
1718
+ `
1719
+ )
1720
+ }));
1721
+ }
1722
+ );
1723
+
1724
+ // src/components/hovering-toolbar/hovering-editor-provider.tsx
1725
+ import { createContext, useState as useState4, useContext as useContext2 } from "react";
1726
+ import { jsx as jsx8 } from "react/jsx-runtime";
1727
+ var HoveringEditorContext = createContext({
1728
+ isDisplayed: false,
1729
+ setIsDisplayed: () => {
1730
+ }
1731
+ });
1732
+ var HoveringEditorProvider = ({
1733
+ children
1734
+ }) => {
1735
+ const [isDisplayed, setIsDisplayed] = useState4(false);
1736
+ return /* @__PURE__ */ jsx8(HoveringEditorContext.Provider, {
1737
+ value: { isDisplayed, setIsDisplayed },
1738
+ children
1739
+ });
1740
+ };
1741
+ var useHoveringEditorContext = () => useContext2(HoveringEditorContext);
1742
+
1743
+ // src/components/hovering-toolbar/hovering-toolbar.tsx
1744
+ import { jsx as jsx9 } from "react/jsx-runtime";
1745
+ var HoveringToolbar = (props) => {
1746
+ const ref = useRef2(null);
1747
+ const editor = useSlate();
1748
+ const selection = useSlateSelection();
1749
+ const { isDisplayed, setIsDisplayed } = useHoveringEditorContext();
1750
+ const [isClient, setIsClient] = useState5(false);
1751
+ useEffect4(() => {
1752
+ setIsClient(true);
1753
+ }, []);
1754
+ useEffect4(() => {
1755
+ const el = ref.current;
1756
+ const { selection: selection2 } = editor;
1757
+ if (!el) {
1758
+ return;
1759
+ }
1760
+ if (!selection2) {
1761
+ el.removeAttribute("style");
1762
+ return;
1763
+ }
1764
+ const domSelection = window.getSelection();
1765
+ if (!domSelection) {
1766
+ return;
1767
+ }
1768
+ const domRange = domSelection.getRangeAt(0);
1769
+ const rect = domRange.getBoundingClientRect();
1770
+ if (rect.top === 0 && rect.left === 0 && rect.width === 0 && rect.height === 0) {
1771
+ return;
1772
+ }
1773
+ const minGapFromEdge = 60;
1774
+ const verticalOffsetFromCorner = 35;
1775
+ const horizontalOffsetFromCorner = 15;
1776
+ let top = rect.top + window.scrollY - el.offsetHeight + verticalOffsetFromCorner;
1777
+ if (top < minGapFromEdge) {
1778
+ top = rect.bottom + window.scrollY + minGapFromEdge;
1779
+ } else if (top + el.offsetHeight > window.innerHeight - minGapFromEdge) {
1780
+ top = rect.top + window.scrollY - el.offsetHeight - minGapFromEdge;
1781
+ }
1782
+ let left = rect.left + window.scrollX - el.offsetWidth / 2 + rect.width / 2 + horizontalOffsetFromCorner;
1783
+ if (left < minGapFromEdge) {
1784
+ left = minGapFromEdge;
1785
+ } else if (left + el.offsetWidth > window.innerWidth - minGapFromEdge) {
1786
+ left = window.innerWidth - el.offsetWidth - minGapFromEdge;
1787
+ }
1788
+ el.style.opacity = "1";
1789
+ el.style.top = `${top}px`;
1790
+ el.style.left = `${left}px`;
1791
+ });
1792
+ useEffect4(() => {
1793
+ const handleClickOutside = (event) => {
1794
+ if (ref.current && !ref.current.contains(event.target)) {
1795
+ setIsDisplayed(false);
1796
+ }
1797
+ };
1798
+ document.addEventListener("mousedown", handleClickOutside);
1799
+ return () => {
1800
+ document.removeEventListener("mousedown", handleClickOutside);
1801
+ };
1802
+ }, [ref, setIsDisplayed]);
1803
+ if (!isClient) {
1804
+ return null;
1805
+ }
1806
+ return /* @__PURE__ */ jsx9(Portal, {
1807
+ children: /* @__PURE__ */ jsx9(Menu, {
1808
+ ref,
1809
+ className: "p-2 absolute z-10 top-[-10000px] left-[-10000px] mt-[-6px] opacity-0 transition-opacity duration-700",
1810
+ children: isDisplayed && selection && /* @__PURE__ */ jsx9(HoveringInsertionPromptBox, {
1811
+ editorState: editorState(editor, selection),
1812
+ apiConfig: props.apiConfig,
1813
+ closeWindow: () => {
1814
+ setIsDisplayed(false);
1815
+ },
1816
+ performInsertion: (insertedText) => {
1817
+ console.log("inserted text", insertedText);
1818
+ Transforms5.delete(editor, { at: selection });
1819
+ Transforms5.insertText(editor, insertedText, {
1820
+ at: selection
1821
+ });
1822
+ setIsDisplayed(false);
1823
+ }
1824
+ })
1825
+ })
1826
+ });
1827
+ };
1828
+ function editorState(editor, selection) {
1829
+ const textAroundCursor = getTextAroundSelection(editor);
1830
+ if (textAroundCursor) {
1831
+ return textAroundCursor;
1832
+ }
1833
+ return {
1834
+ textBeforeCursor: getFullEditorTextWithNewlines(editor),
1835
+ textAfterCursor: "",
1836
+ selectedText: ""
1837
+ };
1838
+ }
1839
+
1840
+ // src/components/base-copilot-textarea/render-element.tsx
1841
+ import { jsx as jsx10, jsxs as jsxs3 } from "react/jsx-runtime";
1842
+ function makeRenderElementFunction(suggestionsStyle) {
1843
+ return (props) => {
1844
+ switch (props.element.type) {
1845
+ case "paragraph":
1846
+ return /* @__PURE__ */ jsx10(DefaultElement, __spreadValues({}, props));
1847
+ case "suggestion":
1848
+ return /* @__PURE__ */ jsx10(SuggestionElement, __spreadProps(__spreadValues({}, props), {
1849
+ suggestionsStyle
1850
+ }));
1851
+ }
1852
+ };
1853
+ }
1854
+ var DefaultElement = (props) => {
1855
+ return /* @__PURE__ */ jsx10("div", __spreadProps(__spreadValues({}, props.attributes), {
1856
+ children: props.children
1857
+ }));
1858
+ };
1859
+ var SuggestionElement = (props) => {
1860
+ return /* @__PURE__ */ jsxs3("span", __spreadProps(__spreadValues({}, props.attributes), {
1861
+ style: __spreadValues({}, props.suggestionsStyle),
1862
+ contentEditable: false,
1863
+ children: [
1864
+ props.children,
1865
+ props.element.type === "suggestion" && props.element.content
1866
+ ]
1867
+ }));
1868
+ };
1869
+
1870
+ // src/components/base-copilot-textarea/render-placeholder.tsx
1871
+ import { jsx as jsx11 } from "react/jsx-runtime";
1872
+ function makeRenderPlaceholderFunction(placeholderStyle) {
1873
+ return (props) => {
1874
+ const _a = props.attributes, { style } = _a, restAttributes = __objRest(_a, ["style"]);
1875
+ return /* @__PURE__ */ jsx11("div", __spreadProps(__spreadValues({}, restAttributes), {
1876
+ style: __spreadValues(__spreadValues({}, style), placeholderStyle),
1877
+ children: props.children
1878
+ }));
1879
+ };
1880
+ }
1881
+
1882
+ // src/components/base-copilot-textarea/use-add-branding-css.tsx
1883
+ import { useEffect as useEffect5 } from "react";
1884
+ function useAddBrandingCss(suggestionStyleAugmented, disableBranding) {
1885
+ const cssSelector = ".copilot-textarea.with-branding";
1886
+ useEffect5(() => {
1887
+ if (disableBranding) {
1888
+ return;
1889
+ }
1890
+ const styleEl = document.createElement("style");
1891
+ styleEl.id = "dynamic-styles";
1892
+ let dynamicStyles = Object.entries(suggestionStyleAugmented).map(([key, value]) => {
1893
+ const kebabCaseKey = key.replace(/([a-z0-9]|(?=[A-Z]))([A-Z])/g, "$1-$2").toLowerCase();
1894
+ return `${kebabCaseKey}: ${value};`;
1895
+ }).join(" ");
1896
+ dynamicStyles += `font-style: normal; font-size: x-small;`;
1897
+ dynamicStyles += `content: "CopilotKit";`;
1898
+ dynamicStyles += `bottom: 6px;`;
1899
+ dynamicStyles += `right: 6px;`;
1900
+ dynamicStyles += `pointer-events: none;`;
1901
+ dynamicStyles += `font-weight: 200;`;
1902
+ dynamicStyles += `padding: 0;`;
1903
+ dynamicStyles += `margin: 0;`;
1904
+ dynamicStyles += `border: 0;`;
1905
+ dynamicStyles += `line-height: 1;`;
1906
+ dynamicStyles += `position: absolute;`;
1907
+ styleEl.innerHTML = `
1908
+ ${cssSelector}::after {
1909
+ ${dynamicStyles}
1910
+ }
1911
+ `;
1912
+ document.head.appendChild(styleEl);
1913
+ const textarea = document.querySelector(cssSelector);
1914
+ const handleScroll = () => {
1915
+ const styleEl2 = document.getElementById("dynamic-styles");
1916
+ if (styleEl2 && textarea) {
1917
+ const offsetFromBottom = -textarea.scrollTop + 6;
1918
+ const offsetFromRight = -textarea.scrollLeft + 6;
1919
+ styleEl2.innerHTML = `
1920
+ ${cssSelector}::after {
1921
+ ${dynamicStyles}
1922
+ bottom: ${offsetFromBottom}px;
1923
+ right: ${offsetFromRight}px;
1924
+ }
1925
+ `;
1926
+ }
1927
+ };
1928
+ textarea == null ? void 0 : textarea.addEventListener("scroll", handleScroll);
1929
+ return () => {
1930
+ var _a;
1931
+ (_a = document.getElementById("dynamic-styles")) == null ? void 0 : _a.remove();
1932
+ textarea == null ? void 0 : textarea.removeEventListener("scroll", handleScroll);
1933
+ };
1934
+ }, [disableBranding, suggestionStyleAugmented]);
1935
+ }
1936
+
1937
+ // src/components/base-copilot-textarea/track-cursor-moved-since-last-text-change.tsx
1938
+ import { useEffect as useEffect6, useRef as useRef3 } from "react";
1939
+ import { useSlateSelector } from "slate-react";
1940
+ import { Range as Range4 } from "slate";
1941
+
1942
+ // src/lib/editor-to-text.ts
1943
+ import { Element as Element4 } from "slate";
1944
+ function nodeChildrenToTextComponents(editor, nodes) {
1945
+ const indeciesOfInlineElements = new Set(
1946
+ nodes.map((node, index) => {
1947
+ if (Element4.isElement(node) && editor.isInline(node)) {
1948
+ return index;
1949
+ }
1950
+ return -1;
1951
+ }).filter((index) => index !== -1)
1952
+ );
1953
+ const nonIgnorableItems = nodes.filter((node, index) => {
1954
+ const isInline = indeciesOfInlineElements.has(index);
1955
+ if (isInline) {
1956
+ return false;
1957
+ }
1958
+ const isNeighbourOfInline = indeciesOfInlineElements.has(index - 1) || indeciesOfInlineElements.has(index + 1);
1959
+ if (isNeighbourOfInline) {
1960
+ return node.text !== "";
1961
+ }
1962
+ return true;
1963
+ });
1964
+ return nonIgnorableItems.map((node) => {
1965
+ if (Element4.isElement(node)) {
1966
+ switch (node.type) {
1967
+ case "paragraph":
1968
+ return nodeChildrenToTextComponents(editor, node.children);
1969
+ case "suggestion":
1970
+ return [];
1971
+ }
1972
+ } else {
1973
+ return [node];
1974
+ }
1975
+ }).reduce((acc, val) => acc.concat(val), []);
1976
+ }
1977
+ var editorToText = (editor) => {
1978
+ const flattened = nodeChildrenToTextComponents(editor, editor.children);
1979
+ const text = flattened.map((textComponent) => textComponent.text).join("\n");
1980
+ return text;
1981
+ };
1982
+
1983
+ // src/components/base-copilot-textarea/track-cursor-moved-since-last-text-change.tsx
1984
+ import { Fragment as Fragment2, jsx as jsx12 } from "react/jsx-runtime";
1985
+ function TrackerTextEditedSinceLastCursorMovement(props) {
1986
+ const cursorState = useSlateSelector((state) => ({
1987
+ selection: state.selection,
1988
+ text: editorToText(state)
1989
+ }));
1990
+ const previousState = usePrevious(cursorState);
1991
+ useEffect6(() => {
1992
+ if (!previousState) {
1993
+ return;
1994
+ }
1995
+ if (cursorChangedWithoutTextChanged(previousState, cursorState)) {
1996
+ props.setCursorMovedSinceLastTextChange(true);
1997
+ }
1998
+ }, [props.setCursorMovedSinceLastTextChange, cursorState]);
1999
+ return /* @__PURE__ */ jsx12(Fragment2, {});
2000
+ }
2001
+ var cursorChangedWithoutTextChanged = (prev, next) => {
2002
+ const isSelectionChanged = !isSelectionEqual(prev.selection, next.selection);
2003
+ const isTextSame = prev.text === next.text;
2004
+ return isSelectionChanged && isTextSame;
2005
+ };
2006
+ var isSelectionEqual = (a, b) => {
2007
+ if (!a && !b)
2008
+ return true;
2009
+ if (!a || !b)
2010
+ return false;
2011
+ return Range4.equals(a, b);
2012
+ };
2013
+ function usePrevious(value) {
2014
+ const ref = useRef3();
2015
+ useEffect6(() => {
2016
+ ref.current = value;
2017
+ });
2018
+ return ref.current;
2019
+ }
2020
+
2021
+ // src/components/base-copilot-textarea/base-copilot-textarea.tsx
2022
+ import { jsx as jsx13, jsxs as jsxs4 } from "react/jsx-runtime";
2023
+ var BaseCopilotTextareaWithHoveringContext = React9.forwardRef(
2024
+ (props, ref) => {
2025
+ const autosuggestionsConfig = __spreadValues(__spreadValues({}, defaultBaseAutosuggestionsConfig), props.baseAutosuggestionsConfig);
2026
+ const valueOnInitialRender = useMemo4(() => {
2027
+ var _a2;
2028
+ return (_a2 = props.value) != null ? _a2 : "";
2029
+ }, []);
2030
+ const [lastKnownFullEditorText, setLastKnownFullEditorText] = useState6(valueOnInitialRender);
2031
+ const [cursorMovedSinceLastTextChange, setCursorMovedSinceLastTextChange] = useState6(false);
2032
+ const initialValue = useMemo4(() => {
2033
+ return [
2034
+ {
2035
+ type: "paragraph",
2036
+ children: [{ text: valueOnInitialRender }]
2037
+ }
2038
+ ];
2039
+ }, [valueOnInitialRender]);
2040
+ const editor = useCopilotTextareaEditor();
2041
+ const {
2042
+ isDisplayed: hoveringEditorIsDisplayed,
2043
+ setIsDisplayed: setHoveringEditorIsDisplayed
2044
+ } = useHoveringEditorContext();
2045
+ const insertText = useCallback3(
2046
+ (autosuggestion) => {
2047
+ Editor5.insertText(editor, autosuggestion.text, {
2048
+ at: autosuggestion.point
2049
+ });
2050
+ },
2051
+ [editor]
2052
+ );
2053
+ const {
2054
+ currentAutocompleteSuggestion,
2055
+ onChangeHandler: onChangeHandlerForAutocomplete,
2056
+ onKeyDownHandler: onKeyDownHandlerForAutocomplete
2057
+ } = useAutosuggestions(
2058
+ autosuggestionsConfig.debounceTime,
2059
+ autosuggestionsConfig.acceptAutosuggestionKey,
2060
+ autosuggestionsConfig.apiConfig.autosuggestionsFunction,
2061
+ insertText,
2062
+ autosuggestionsConfig.disableWhenEmpty,
2063
+ autosuggestionsConfig.disabled || hoveringEditorIsDisplayed || cursorMovedSinceLastTextChange && autosuggestionsConfig.temporarilyDisableWhenMovingCursorWithoutChangingText
2064
+ );
2065
+ const onKeyDownHandlerForHoveringEditor = useCallback3(
2066
+ (event) => {
2067
+ if (event.key === "k" && event.metaKey) {
2068
+ event.preventDefault();
2069
+ setHoveringEditorIsDisplayed(!hoveringEditorIsDisplayed);
2070
+ }
2071
+ },
2072
+ [hoveringEditorIsDisplayed, setHoveringEditorIsDisplayed]
2073
+ );
2074
+ useEffect7(() => {
2075
+ clearAutocompletionsFromEditor(editor);
2076
+ if (currentAutocompleteSuggestion) {
2077
+ addAutocompletionsToEditor(
2078
+ editor,
2079
+ currentAutocompleteSuggestion.text,
2080
+ currentAutocompleteSuggestion.point
2081
+ );
2082
+ }
2083
+ }, [currentAutocompleteSuggestion]);
2084
+ const suggestionStyleAugmented = useMemo4(() => {
2085
+ return __spreadValues({
2086
+ fontStyle: "italic",
2087
+ color: "gray"
2088
+ }, props.suggestionsStyle);
2089
+ }, [props.suggestionsStyle]);
2090
+ const renderElementMemoized = useMemo4(() => {
2091
+ return makeRenderElementFunction(suggestionStyleAugmented);
2092
+ }, [suggestionStyleAugmented]);
2093
+ const renderPlaceholderMemoized = useMemo4(() => {
2094
+ const placeholderStyleSlatejsOverrides = {
2095
+ top: void 0
2096
+ };
2097
+ const placeholderStyleAugmented = __spreadValues(__spreadValues({}, placeholderStyleSlatejsOverrides), props.placeholderStyle);
2098
+ return makeRenderPlaceholderFunction(placeholderStyleAugmented);
2099
+ }, [props.placeholderStyle]);
2100
+ useEffect7(() => {
2101
+ var _a2, _b;
2102
+ if (props.value === lastKnownFullEditorText) {
2103
+ return;
2104
+ }
2105
+ setLastKnownFullEditorText((_a2 = props.value) != null ? _a2 : "");
2106
+ replaceEditorText(editor, (_b = props.value) != null ? _b : "");
2107
+ }, [props.value]);
2108
+ const _a = props, {
2109
+ placeholderStyle,
2110
+ value,
2111
+ onValueChange,
2112
+ baseAutosuggestionsConfig: autosuggestionsConfigFromProps,
2113
+ className,
2114
+ onChange,
2115
+ onKeyDown,
2116
+ disableBranding
2117
+ } = _a, propsToForward = __objRest(_a, [
2118
+ "placeholderStyle",
2119
+ "value",
2120
+ "onValueChange",
2121
+ "baseAutosuggestionsConfig",
2122
+ "className",
2123
+ "onChange",
2124
+ "onKeyDown",
2125
+ "disableBranding"
2126
+ ]);
2127
+ useAddBrandingCss(suggestionStyleAugmented, disableBranding);
2128
+ usePopulateCopilotTextareaRef(editor, ref);
2129
+ const moddedClassName = (() => {
2130
+ const baseClassName = "copilot-textarea";
2131
+ const brandingClass = disableBranding ? "no-branding" : "with-branding";
2132
+ const defaultTailwindClassName = "bg-white overflow-y-auto resize-y";
2133
+ const mergedClassName = twMerge2(
2134
+ defaultTailwindClassName,
2135
+ className != null ? className : ""
2136
+ );
2137
+ return `${baseClassName} ${brandingClass} ${mergedClassName}`;
2138
+ })();
2139
+ return /* @__PURE__ */ jsxs4(Slate, {
2140
+ editor,
2141
+ initialValue,
2142
+ onChange: (value2) => {
2143
+ var _a2, _b;
2144
+ const newEditorState = getTextAroundCollapsedCursor(editor);
2145
+ const fullEditorText = newEditorState ? newEditorState.textBeforeCursor + newEditorState.textAfterCursor : getFullEditorTextWithNewlines(editor);
2146
+ setLastKnownFullEditorText((prev) => {
2147
+ if (prev !== fullEditorText) {
2148
+ setCursorMovedSinceLastTextChange(false);
2149
+ }
2150
+ return fullEditorText;
2151
+ });
2152
+ onChangeHandlerForAutocomplete(newEditorState);
2153
+ (_a2 = props.onValueChange) == null ? void 0 : _a2.call(props, fullEditorText);
2154
+ (_b = props.onChange) == null ? void 0 : _b.call(props, makeSemiFakeReactTextAreaEvent(fullEditorText));
2155
+ },
2156
+ children: [
2157
+ /* @__PURE__ */ jsx13(TrackerTextEditedSinceLastCursorMovement, {
2158
+ setCursorMovedSinceLastTextChange
2159
+ }),
2160
+ /* @__PURE__ */ jsx13(HoveringToolbar, {
2161
+ apiConfig: autosuggestionsConfig.apiConfig
2162
+ }),
2163
+ /* @__PURE__ */ jsx13(Editable, __spreadValues({
2164
+ renderElement: renderElementMemoized,
2165
+ renderPlaceholder: renderPlaceholderMemoized,
2166
+ onKeyDown: (event) => {
2167
+ var _a2;
2168
+ onKeyDownHandlerForHoveringEditor(event);
2169
+ onKeyDownHandlerForAutocomplete(event);
2170
+ (_a2 = props.onKeyDown) == null ? void 0 : _a2.call(props, event);
2171
+ },
2172
+ className: moddedClassName
2173
+ }, propsToForward))
2174
+ ]
2175
+ });
2176
+ }
2177
+ );
2178
+ function makeSemiFakeReactTextAreaEvent(currentText) {
2179
+ return {
2180
+ target: {
2181
+ value: currentText,
2182
+ type: "copilot-textarea"
2183
+ },
2184
+ currentTarget: {
2185
+ value: currentText,
2186
+ type: "copilot-textarea"
2187
+ }
2188
+ };
2189
+ }
2190
+ var BaseCopilotTextarea = React9.forwardRef(
2191
+ (props, ref) => {
2192
+ return /* @__PURE__ */ jsx13(HoveringEditorProvider, {
2193
+ children: /* @__PURE__ */ jsx13(BaseCopilotTextareaWithHoveringContext, __spreadProps(__spreadValues({}, props), {
2194
+ ref
2195
+ }))
2196
+ });
2197
+ }
2198
+ );
2199
+
2200
+ // src/hooks/make-autosuggestions-function/use-make-standard-insertion-function.tsx
2201
+ import { CopilotContext as CopilotContext2 } from "@copilotkit/react-core";
2202
+ import { useCallback as useCallback4, useContext as useContext3 } from "react";
2203
+ function useMakeStandardInsertionOrEditingFunction(textareaPurpose, contextCategories, insertionApiConfig, editingApiConfig) {
2204
+ const { getContextString } = useContext3(CopilotContext2);
2205
+ const insertionFunction = useCallback4(
2206
+ (editorState2, insertionPrompt, abortSignal) => __async(this, null, function* () {
2207
+ const res = yield retry(() => __async(this, null, function* () {
2208
+ const messages = [
2209
+ {
2210
+ role: "system",
2211
+ content: insertionApiConfig.makeSystemPrompt(
2212
+ textareaPurpose,
2213
+ getContextString(contextCategories)
2214
+ )
2215
+ },
2216
+ ...insertionApiConfig.fewShotMessages,
2217
+ {
2218
+ role: "user",
2219
+ name: "TextAfterCursor",
2220
+ content: editorState2.textAfterCursor
2221
+ },
2222
+ {
2223
+ role: "user",
2224
+ name: "TextBeforeCursor",
2225
+ content: editorState2.textBeforeCursor
2226
+ },
2227
+ {
2228
+ role: "user",
2229
+ name: "InsertionPrompt",
2230
+ content: insertionPrompt
2231
+ }
2232
+ ];
2233
+ return yield insertionApiConfig.apiEndpoint.run(
2234
+ abortSignal,
2235
+ messages,
2236
+ insertionApiConfig.forwardedParams
2237
+ );
2238
+ }));
2239
+ return res;
2240
+ }),
2241
+ [insertionApiConfig, getContextString, contextCategories, textareaPurpose]
2242
+ );
2243
+ const editingFunction = useCallback4(
2244
+ (editorState2, editingPrompt, abortSignal) => __async(this, null, function* () {
2245
+ const res = yield retry(() => __async(this, null, function* () {
2246
+ const messages = [
2247
+ {
2248
+ role: "system",
2249
+ content: editingApiConfig.makeSystemPrompt(
2250
+ textareaPurpose,
2251
+ getContextString(contextCategories)
2252
+ )
2253
+ },
2254
+ ...editingApiConfig.fewShotMessages,
2255
+ {
2256
+ role: "user",
2257
+ name: "TextBeforeCursor",
2258
+ content: editorState2.textBeforeCursor
2259
+ },
2260
+ {
2261
+ role: "user",
2262
+ name: "TextToEdit",
2263
+ content: editorState2.selectedText
2264
+ },
2265
+ {
2266
+ role: "user",
2267
+ name: "TextAfterCursor",
2268
+ content: editorState2.textAfterCursor
2269
+ },
2270
+ {
2271
+ role: "user",
2272
+ name: "EditingPrompt",
2273
+ content: editingPrompt
2274
+ }
2275
+ ];
2276
+ return yield editingApiConfig.apiEndpoint.run(
2277
+ abortSignal,
2278
+ messages,
2279
+ editingApiConfig.forwardedParams
2280
+ );
2281
+ }));
2282
+ return res;
2283
+ }),
2284
+ [editingApiConfig, getContextString, contextCategories, textareaPurpose]
2285
+ );
2286
+ const insertionOrEditingFunction = useCallback4(
2287
+ (editorState2, insertionPrompt, abortSignal) => __async(this, null, function* () {
2288
+ if (editorState2.selectedText === "") {
2289
+ return yield insertionFunction(
2290
+ editorState2,
2291
+ insertionPrompt,
2292
+ abortSignal
2293
+ );
2294
+ } else {
2295
+ return yield editingFunction(editorState2, insertionPrompt, abortSignal);
2296
+ }
2297
+ }),
2298
+ [insertionFunction, editingFunction]
2299
+ );
2300
+ return insertionOrEditingFunction;
2301
+ }
2302
+
2303
+ // src/components/copilot-textarea/copilot-textarea.tsx
2304
+ import merge from "lodash.merge";
2305
+ import { Fragment as Fragment3, jsx as jsx14 } from "react/jsx-runtime";
2306
+ var CopilotTextarea = React10.forwardRef(
2307
+ (props, ref) => {
2308
+ const autosuggestionsConfig = merge(
2309
+ defaultAutosuggestionsConfig,
2310
+ props.autosuggestionsConfig
2311
+ );
2312
+ const autosuggestionsFunction = useMakeStandardAutosuggestionFunction(
2313
+ autosuggestionsConfig.textareaPurpose,
2314
+ autosuggestionsConfig.externalContextCategories,
2315
+ autosuggestionsConfig.chatApiConfigs.suggestionsApiConfig
2316
+ );
2317
+ const insertionOrEditingFunction = useMakeStandardInsertionOrEditingFunction(
2318
+ autosuggestionsConfig.textareaPurpose,
2319
+ autosuggestionsConfig.externalContextCategories,
2320
+ autosuggestionsConfig.chatApiConfigs.insertionApiConfig,
2321
+ autosuggestionsConfig.chatApiConfigs.editingApiConfig
2322
+ );
2323
+ return /* @__PURE__ */ jsx14(Fragment3, {
2324
+ children: /* @__PURE__ */ jsx14(BaseCopilotTextarea, __spreadProps(__spreadValues({
2325
+ ref
2326
+ }, props), {
2327
+ baseAutosuggestionsConfig: __spreadProps(__spreadValues({}, autosuggestionsConfig), {
2328
+ apiConfig: {
2329
+ insertionOrEditingFunction,
2330
+ autosuggestionsFunction
2331
+ }
2332
+ })
2333
+ }))
2334
+ });
2335
+ }
2336
+ );
2337
+ export {
2338
+ CopilotTextarea
2339
+ };
44
2340
  //# sourceMappingURL=copilot-textarea.mjs.map