@copilotkit/react-textarea 0.18.0 → 0.19.0-alpha.1

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 (236) hide show
  1. package/.turbo/turbo-build.log +303 -189
  2. package/CHANGELOG.md +19 -0
  3. package/dist/chunk-2NURR2DX.mjs +47 -0
  4. package/dist/chunk-2NURR2DX.mjs.map +1 -0
  5. package/dist/chunk-3YJ63D5D.mjs +106 -0
  6. package/dist/chunk-3YJ63D5D.mjs.map +1 -0
  7. package/dist/chunk-4S5ZJH3I.mjs +18 -0
  8. package/dist/chunk-4S5ZJH3I.mjs.map +1 -0
  9. package/dist/chunk-55EGOC5T.mjs +87 -0
  10. package/dist/chunk-55EGOC5T.mjs.map +1 -0
  11. package/dist/{chunk-3A2CNIG5.mjs → chunk-5ARCOTW3.mjs} +8 -5
  12. package/dist/chunk-5ARCOTW3.mjs.map +1 -0
  13. package/dist/chunk-5EJ5XOGP.mjs +22 -0
  14. package/dist/chunk-5EJ5XOGP.mjs.map +1 -0
  15. package/dist/chunk-5FO6ISW4.mjs +3 -0
  16. package/dist/{chunk-5GGCWNTT.mjs → chunk-5SL5L4VS.mjs} +67 -29
  17. package/dist/chunk-5SL5L4VS.mjs.map +1 -0
  18. package/dist/{chunk-JWN2VEE3.mjs → chunk-A2RRLD23.mjs} +13 -12
  19. package/dist/chunk-A2RRLD23.mjs.map +1 -0
  20. package/dist/chunk-CDB7HFCY.mjs +65 -0
  21. package/dist/chunk-CDB7HFCY.mjs.map +1 -0
  22. package/dist/{chunk-MFJNLKRC.mjs → chunk-D7SEV5PR.mjs} +4 -3
  23. package/dist/chunk-D7SEV5PR.mjs.map +1 -0
  24. package/dist/chunk-HAFHLU4N.mjs +55 -0
  25. package/dist/chunk-HAFHLU4N.mjs.map +1 -0
  26. package/dist/chunk-ITZHK3YV.mjs +477 -0
  27. package/dist/chunk-ITZHK3YV.mjs.map +1 -0
  28. package/dist/{chunk-3UQM3NLM.mjs → chunk-IXJ2HCOA.mjs} +42 -7
  29. package/dist/chunk-IXJ2HCOA.mjs.map +1 -0
  30. package/dist/{chunk-UW3ITU2Y.mjs → chunk-JAFCXEPU.mjs} +1 -1
  31. package/dist/chunk-JAFCXEPU.mjs.map +1 -0
  32. package/dist/chunk-KDFCAQGV.mjs +44 -0
  33. package/dist/chunk-KDFCAQGV.mjs.map +1 -0
  34. package/dist/chunk-L7VVZH4Q.mjs +3 -0
  35. package/dist/chunk-ND5PXTAW.mjs +17 -0
  36. package/dist/chunk-ND5PXTAW.mjs.map +1 -0
  37. package/dist/chunk-O5OWT5GE.mjs +114 -0
  38. package/dist/chunk-O5OWT5GE.mjs.map +1 -0
  39. package/dist/chunk-OD7ZMOVE.mjs +45 -0
  40. package/dist/chunk-OD7ZMOVE.mjs.map +1 -0
  41. package/dist/chunk-OM5WQQOU.mjs +47 -0
  42. package/dist/chunk-OM5WQQOU.mjs.map +1 -0
  43. package/dist/chunk-QL2GYGG5.mjs +19 -0
  44. package/dist/chunk-QL2GYGG5.mjs.map +1 -0
  45. package/dist/chunk-RPDVSCLO.mjs +107 -0
  46. package/dist/chunk-RPDVSCLO.mjs.map +1 -0
  47. package/dist/{chunk-HZGSG7ST.mjs → chunk-UHD44NC5.mjs} +10 -5
  48. package/dist/chunk-UHD44NC5.mjs.map +1 -0
  49. package/dist/chunk-VBIJPE3H.mjs +108 -0
  50. package/dist/chunk-VBIJPE3H.mjs.map +1 -0
  51. package/dist/chunk-XDT7BF3V.mjs +81 -0
  52. package/dist/chunk-XDT7BF3V.mjs.map +1 -0
  53. package/dist/chunk-XHUMROEY.mjs +91 -0
  54. package/dist/chunk-XHUMROEY.mjs.map +1 -0
  55. package/dist/{chunk-LWVCQYWV.mjs → chunk-YQU7WG7T.mjs} +2 -2
  56. package/dist/chunk-YTOPHPSG.mjs +45 -0
  57. package/dist/chunk-YTOPHPSG.mjs.map +1 -0
  58. package/dist/chunk-YW3REYX6.mjs +23 -0
  59. package/dist/chunk-YW3REYX6.mjs.map +1 -0
  60. package/dist/components/base-copilot-textarea/base-copilot-textarea.d.ts +3 -5
  61. package/dist/components/base-copilot-textarea/base-copilot-textarea.mjs +21 -7
  62. package/dist/components/base-copilot-textarea/track-cursor-moved-since-last-text-change.d.ts +6 -0
  63. package/dist/components/base-copilot-textarea/track-cursor-moved-since-last-text-change.mjs +5 -0
  64. package/dist/components/copilot-textarea/copilot-textarea.d.ts +13 -10
  65. package/dist/components/copilot-textarea/copilot-textarea.mjs +31 -12
  66. package/dist/components/hovering-toolbar/hovering-editor-provider.d.ts +13 -0
  67. package/dist/components/hovering-toolbar/hovering-editor-provider.mjs +4 -0
  68. package/dist/components/hovering-toolbar/hovering-toolbar-components.d.ts +18 -0
  69. package/dist/components/hovering-toolbar/hovering-toolbar-components.mjs +4 -0
  70. package/dist/components/hovering-toolbar/hovering-toolbar.d.ts +8 -0
  71. package/dist/components/hovering-toolbar/hovering-toolbar.mjs +17 -0
  72. package/dist/components/hovering-toolbar/hovering-toolbar.mjs.map +1 -0
  73. package/dist/components/hovering-toolbar/text-insertion-prompt-box/hovering-insertion-prompt-box.d.ts +12 -0
  74. package/dist/components/hovering-toolbar/text-insertion-prompt-box/hovering-insertion-prompt-box.mjs +12 -0
  75. package/dist/components/hovering-toolbar/text-insertion-prompt-box/hovering-insertion-prompt-box.mjs.map +1 -0
  76. package/dist/components/hovering-toolbar/text-insertion-prompt-box/index.d.ts +3 -0
  77. package/dist/components/hovering-toolbar/text-insertion-prompt-box/index.mjs +13 -0
  78. package/dist/components/hovering-toolbar/text-insertion-prompt-box/index.mjs.map +1 -0
  79. package/dist/components/hovering-toolbar/text-insertion-prompt-box/mode-pre-suggestion.d.ts +16 -0
  80. package/dist/components/hovering-toolbar/text-insertion-prompt-box/mode-pre-suggestion.mjs +5 -0
  81. package/dist/components/hovering-toolbar/text-insertion-prompt-box/mode-pre-suggestion.mjs.map +1 -0
  82. package/dist/components/hovering-toolbar/text-insertion-prompt-box/mode-suggestion-appearing.d.ts +37 -0
  83. package/dist/components/hovering-toolbar/text-insertion-prompt-box/mode-suggestion-appearing.mjs +10 -0
  84. package/dist/components/hovering-toolbar/text-insertion-prompt-box/mode-suggestion-appearing.mjs.map +1 -0
  85. package/dist/components/index.d.ts +9 -4
  86. package/dist/components/index.mjs +31 -12
  87. package/dist/components/manual-ui/chip-with-icon.d.ts +10 -0
  88. package/dist/components/manual-ui/chip-with-icon.mjs +29 -0
  89. package/dist/components/manual-ui/chip-with-icon.mjs.map +1 -0
  90. package/dist/components/source-search-box/source-search-box.d.ts +17 -0
  91. package/dist/components/source-search-box/source-search-box.mjs +10 -0
  92. package/dist/components/source-search-box/source-search-box.mjs.map +1 -0
  93. package/dist/components/ui/button.d.ts +14 -0
  94. package/dist/components/ui/button.mjs +5 -0
  95. package/dist/components/ui/button.mjs.map +1 -0
  96. package/dist/components/ui/card.d.ts +10 -0
  97. package/dist/components/ui/card.mjs +63 -0
  98. package/dist/components/ui/card.mjs.map +1 -0
  99. package/dist/components/ui/command.d.ts +48 -0
  100. package/dist/components/ui/command.mjs +6 -0
  101. package/dist/components/ui/command.mjs.map +1 -0
  102. package/dist/components/ui/dialog.d.ts +18 -0
  103. package/dist/components/ui/dialog.mjs +5 -0
  104. package/dist/components/ui/dialog.mjs.map +1 -0
  105. package/dist/components/ui/label.d.ts +8 -0
  106. package/dist/components/ui/label.mjs +5 -0
  107. package/dist/components/ui/label.mjs.map +1 -0
  108. package/dist/components/ui/separator.d.ts +6 -0
  109. package/dist/components/ui/separator.mjs +26 -0
  110. package/dist/components/ui/separator.mjs.map +1 -0
  111. package/dist/components/ui/textarea.d.ts +7 -0
  112. package/dist/components/ui/textarea.mjs +22 -0
  113. package/dist/components/ui/textarea.mjs.map +1 -0
  114. package/dist/hooks/base-copilot-textarea-implementation/use-autosuggestions.mjs +2 -2
  115. package/dist/hooks/base-copilot-textarea-implementation/use-populate-copilot-textarea-ref.d.ts +2 -2
  116. package/dist/hooks/base-copilot-textarea-implementation/use-populate-copilot-textarea-ref.mjs +2 -2
  117. package/dist/hooks/make-autosuggestions-function/use-make-standard-autosuggestions-function.d.ts +5 -7
  118. package/dist/hooks/make-autosuggestions-function/use-make-standard-autosuggestions-function.mjs +2 -1
  119. package/dist/hooks/make-autosuggestions-function/use-make-standard-insertion-function.d.ts +23 -0
  120. package/dist/hooks/make-autosuggestions-function/use-make-standard-insertion-function.mjs +5 -0
  121. package/dist/hooks/make-autosuggestions-function/use-make-standard-insertion-function.mjs.map +1 -0
  122. package/dist/hooks/misc/use-autosize-textarea.d.ts +5 -0
  123. package/dist/hooks/misc/use-autosize-textarea.mjs +4 -0
  124. package/dist/hooks/misc/use-autosize-textarea.mjs.map +1 -0
  125. package/dist/index.css +674 -0
  126. package/dist/index.css.map +1 -1
  127. package/dist/index.d.ts +8 -3
  128. package/dist/index.mjs +31 -12
  129. package/dist/lib/editor-to-text.mjs +1 -42
  130. package/dist/lib/editor-to-text.mjs.map +1 -1
  131. package/dist/lib/get-text-around-cursor.d.ts +10 -3
  132. package/dist/lib/get-text-around-cursor.mjs +1 -1
  133. package/dist/lib/retry.d.ts +3 -0
  134. package/dist/lib/retry.mjs +4 -0
  135. package/dist/lib/retry.mjs.map +1 -0
  136. package/dist/types/autosuggestions-config/autosuggestions-config-user-specified.d.ts +25 -0
  137. package/dist/types/autosuggestions-config/autosuggestions-config-user-specified.mjs +3 -0
  138. package/dist/types/autosuggestions-config/autosuggestions-config-user-specified.mjs.map +1 -0
  139. package/dist/types/autosuggestions-config/autosuggestions-config.d.ts +20 -0
  140. package/dist/types/autosuggestions-config/autosuggestions-config.mjs +10 -0
  141. package/dist/types/autosuggestions-config/autosuggestions-config.mjs.map +1 -0
  142. package/dist/types/autosuggestions-config/editing-api-config.d.ts +17 -0
  143. package/dist/types/autosuggestions-config/editing-api-config.mjs +5 -0
  144. package/dist/types/autosuggestions-config/editing-api-config.mjs.map +1 -0
  145. package/dist/types/autosuggestions-config/index.d.ts +10 -0
  146. package/dist/types/autosuggestions-config/index.mjs +11 -0
  147. package/dist/types/autosuggestions-config/index.mjs.map +1 -0
  148. package/dist/types/autosuggestions-config/insertions-api-config.d.ts +17 -0
  149. package/dist/types/autosuggestions-config/insertions-api-config.mjs +5 -0
  150. package/dist/types/autosuggestions-config/insertions-api-config.mjs.map +1 -0
  151. package/dist/types/{standard-autosuggestions → autosuggestions-config/subtypes}/chatlike-api-endpoint.d.ts +7 -4
  152. package/dist/types/autosuggestions-config/subtypes/chatlike-api-endpoint.mjs +4 -0
  153. package/dist/types/autosuggestions-config/subtypes/chatlike-api-endpoint.mjs.map +1 -0
  154. package/dist/types/autosuggestions-config/subtypes/make-system-prompt.d.ts +3 -0
  155. package/dist/types/autosuggestions-config/subtypes/make-system-prompt.mjs +3 -0
  156. package/dist/types/autosuggestions-config/subtypes/make-system-prompt.mjs.map +1 -0
  157. package/dist/types/autosuggestions-config/subtypes/minimal-chat-gpt-message.mjs.map +1 -0
  158. package/dist/types/autosuggestions-config/suggestions-api-config.d.ts +17 -0
  159. package/dist/types/autosuggestions-config/suggestions-api-config.mjs +5 -0
  160. package/dist/types/autosuggestions-config/suggestions-api-config.mjs.map +1 -0
  161. package/dist/types/base/autosuggestions-bare-function.d.ts +16 -2
  162. package/dist/types/base/base-autosuggestions-config.d.ts +5 -1
  163. package/dist/types/base/base-autosuggestions-config.mjs +1 -1
  164. package/dist/types/base/base-copilot-textarea-props.d.ts +3 -1
  165. package/dist/types/base/editor-autocomplete-state.mjs +1 -1
  166. package/dist/types/base/index.mjs +1 -1
  167. package/dist/types/index.d.ts +8 -3
  168. package/dist/types/index.mjs +7 -4
  169. package/package.json +19 -5
  170. package/src/components/base-copilot-textarea/base-copilot-textarea.tsx +64 -13
  171. package/src/components/base-copilot-textarea/track-cursor-moved-since-last-text-change.tsx +61 -0
  172. package/src/components/copilot-textarea/copilot-textarea.tsx +29 -17
  173. package/src/components/hovering-toolbar/hovering-editor-provider.tsx +29 -0
  174. package/src/components/hovering-toolbar/hovering-toolbar-components.tsx +115 -0
  175. package/src/components/hovering-toolbar/hovering-toolbar.tsx +156 -0
  176. package/src/components/hovering-toolbar/text-insertion-prompt-box/hovering-insertion-prompt-box.tsx +73 -0
  177. package/src/components/hovering-toolbar/text-insertion-prompt-box/index.ts +2 -0
  178. package/src/components/hovering-toolbar/text-insertion-prompt-box/mode-pre-suggestion.tsx +83 -0
  179. package/src/components/hovering-toolbar/text-insertion-prompt-box/mode-suggestion-appearing.tsx +410 -0
  180. package/src/components/manual-ui/chip-with-icon.tsx +28 -0
  181. package/src/components/source-search-box/source-search-box.tsx +133 -0
  182. package/src/components/ui/button.tsx +56 -0
  183. package/src/components/ui/card.tsx +86 -0
  184. package/src/components/ui/command.tsx +155 -0
  185. package/src/components/ui/dialog.tsx +123 -0
  186. package/src/components/ui/label.tsx +26 -0
  187. package/src/components/ui/separator.tsx +31 -0
  188. package/src/components/ui/textarea.tsx +24 -0
  189. package/src/hooks/base-copilot-textarea-implementation/use-autosuggestions.ts +10 -3
  190. package/src/hooks/make-autosuggestions-function/use-make-standard-autosuggestions-function.tsx +29 -44
  191. package/src/hooks/make-autosuggestions-function/use-make-standard-insertion-function.tsx +150 -0
  192. package/src/hooks/misc/use-autosize-textarea.tsx +21 -0
  193. package/src/lib/get-text-around-cursor.ts +71 -6
  194. package/src/lib/retry.tsx +23 -0
  195. package/src/styles.css +2 -0
  196. package/src/types/autosuggestions-config/autosuggestions-config-user-specified.tsx +27 -0
  197. package/src/types/autosuggestions-config/autosuggestions-config.tsx +43 -0
  198. package/src/types/autosuggestions-config/editing-api-config.tsx +102 -0
  199. package/src/types/autosuggestions-config/index.ts +11 -0
  200. package/src/types/autosuggestions-config/insertions-api-config.tsx +95 -0
  201. package/src/types/{standard-autosuggestions → autosuggestions-config/subtypes}/chatlike-api-endpoint.tsx +19 -7
  202. package/src/types/autosuggestions-config/subtypes/make-system-prompt.ts +4 -0
  203. package/src/types/{standard-autosuggestions/autosuggestions-config.tsx → autosuggestions-config/suggestions-api-config.tsx} +12 -23
  204. package/src/types/base/autosuggestions-bare-function.ts +24 -2
  205. package/src/types/base/base-autosuggestions-config.tsx +7 -2
  206. package/src/types/base/base-copilot-textarea-props.tsx +3 -1
  207. package/src/types/base/editor-autocomplete-state.ts +1 -0
  208. package/src/types/index.ts +1 -1
  209. package/tailwind.config.js +2 -0
  210. package/dist/chunk-3A2CNIG5.mjs.map +0 -1
  211. package/dist/chunk-3UQM3NLM.mjs.map +0 -1
  212. package/dist/chunk-5GGCWNTT.mjs.map +0 -1
  213. package/dist/chunk-BLRD23HM.mjs +0 -32
  214. package/dist/chunk-BLRD23HM.mjs.map +0 -1
  215. package/dist/chunk-BTAUKCBN.mjs +0 -60
  216. package/dist/chunk-BTAUKCBN.mjs.map +0 -1
  217. package/dist/chunk-HZGSG7ST.mjs.map +0 -1
  218. package/dist/chunk-JWN2VEE3.mjs.map +0 -1
  219. package/dist/chunk-MFJNLKRC.mjs.map +0 -1
  220. package/dist/chunk-SL4J5HMW.mjs +0 -3
  221. package/dist/chunk-UW3ITU2Y.mjs.map +0 -1
  222. package/dist/types/standard-autosuggestions/autosuggestions-config.d.ts +0 -19
  223. package/dist/types/standard-autosuggestions/autosuggestions-config.mjs +0 -6
  224. package/dist/types/standard-autosuggestions/chatlike-api-endpoint.mjs +0 -4
  225. package/dist/types/standard-autosuggestions/index.d.ts +0 -4
  226. package/dist/types/standard-autosuggestions/index.mjs +0 -8
  227. package/src/types/standard-autosuggestions/index.ts +0 -14
  228. /package/dist/{chunk-SL4J5HMW.mjs.map → chunk-5FO6ISW4.mjs.map} +0 -0
  229. /package/dist/{types/standard-autosuggestions/autosuggestions-config.mjs.map → chunk-L7VVZH4Q.mjs.map} +0 -0
  230. /package/dist/{chunk-LWVCQYWV.mjs.map → chunk-YQU7WG7T.mjs.map} +0 -0
  231. /package/dist/{types/standard-autosuggestions/chatlike-api-endpoint.mjs.map → components/base-copilot-textarea/track-cursor-moved-since-last-text-change.mjs.map} +0 -0
  232. /package/dist/{types/standard-autosuggestions/index.mjs.map → components/hovering-toolbar/hovering-editor-provider.mjs.map} +0 -0
  233. /package/dist/{types/standard-autosuggestions/minimal-chat-gpt-message.mjs.map → components/hovering-toolbar/hovering-toolbar-components.mjs.map} +0 -0
  234. /package/dist/types/{standard-autosuggestions → autosuggestions-config/subtypes}/minimal-chat-gpt-message.d.ts +0 -0
  235. /package/dist/types/{standard-autosuggestions → autosuggestions-config/subtypes}/minimal-chat-gpt-message.mjs +0 -0
  236. /package/src/types/{standard-autosuggestions → autosuggestions-config/subtypes}/minimal-chat-gpt-message.tsx +0 -0
@@ -0,0 +1,150 @@
1
+ import { CopilotContext } from "@copilotkit/react-core";
2
+ import { useCallback, useContext } from "react";
3
+ import { MinimalChatGPTMessage } from "../../types";
4
+ import { retry } from "../../lib/retry";
5
+ import {
6
+ EditingEditorState,
7
+ Generator_InsertionOrEditingSuggestion,
8
+ InsertionEditorApiConfig,
9
+ InsertionEditorState,
10
+ } from "../../types/base/autosuggestions-bare-function";
11
+ import { InsertionsApiConfig } from "../../types/autosuggestions-config/insertions-api-config";
12
+ import { EditingApiConfig } from "../../types/autosuggestions-config/editing-api-config";
13
+
14
+ /**
15
+ * Returns a memoized function that sends a request to the specified API endpoint to get an autosuggestion for the user's input.
16
+ * The function takes in the text before and after the cursor, and an abort signal.
17
+ * It sends a POST request to the API endpoint with the messages array containing the system message, few shot messages, and user messages.
18
+ * The function returns the suggestion from the API response.
19
+ *
20
+ * @param textareaPurpose - The purpose of the textarea. This is included in the system message.
21
+ * @param apiEndpoint - The API endpoint to send the autosuggestion request to.
22
+ * @param makeSystemMessage - A function that takes in a context string and returns a system message to include in the autosuggestion request.
23
+ * @param fewShotMessages - An array of few shot messages to include in the autosuggestion request.
24
+ * @param contextCategories - The categories of context strings we want to include. By default, we include the (default) "global" context category.
25
+ * @returns A memoized function that sends a request to the specified API endpoint to get an autosuggestion for the user's input.
26
+ */
27
+ export function useMakeStandardInsertionOrEditingFunction(
28
+ textareaPurpose: string,
29
+ contextCategories: string[] | undefined,
30
+ insertionApiConfig: InsertionsApiConfig,
31
+ editingApiConfig: EditingApiConfig
32
+ ): Generator_InsertionOrEditingSuggestion {
33
+ const { getContextString } = useContext(CopilotContext);
34
+
35
+ const insertionFunction = useCallback(
36
+ async (
37
+ editorState: EditingEditorState,
38
+ insertionPrompt: string,
39
+ abortSignal: AbortSignal
40
+ ) => {
41
+ const res = await retry(async () => {
42
+ const messages: MinimalChatGPTMessage[] = [
43
+ {
44
+ role: "system",
45
+ content: insertionApiConfig.makeSystemPrompt(
46
+ textareaPurpose,
47
+ getContextString(contextCategories)
48
+ ),
49
+ },
50
+ ...insertionApiConfig.fewShotMessages,
51
+ {
52
+ role: "user",
53
+ name: "TextAfterCursor",
54
+ content: editorState.textAfterCursor,
55
+ },
56
+ {
57
+ role: "user",
58
+ name: "TextBeforeCursor",
59
+ content: editorState.textBeforeCursor,
60
+ },
61
+ {
62
+ role: "user",
63
+ name: "InsertionPrompt",
64
+ content: insertionPrompt,
65
+ },
66
+ ];
67
+
68
+ return await insertionApiConfig.apiEndpoint.run(
69
+ abortSignal,
70
+ messages,
71
+ insertionApiConfig.forwardedParams
72
+ );
73
+ });
74
+
75
+ return res;
76
+ },
77
+ [insertionApiConfig, getContextString, contextCategories, textareaPurpose]
78
+ );
79
+
80
+ const editingFunction = useCallback(
81
+ async (
82
+ editorState: EditingEditorState,
83
+ editingPrompt: string,
84
+ abortSignal: AbortSignal
85
+ ) => {
86
+ const res = await retry(async () => {
87
+ const messages: MinimalChatGPTMessage[] = [
88
+ {
89
+ role: "system",
90
+ content: editingApiConfig.makeSystemPrompt(
91
+ textareaPurpose,
92
+ getContextString(contextCategories)
93
+ ),
94
+ },
95
+ ...editingApiConfig.fewShotMessages,
96
+ {
97
+ role: "user",
98
+ name: "TextBeforeCursor",
99
+ content: editorState.textBeforeCursor,
100
+ },
101
+ {
102
+ role: "user",
103
+ name: "TextToEdit",
104
+ content: editorState.selectedText,
105
+ },
106
+ {
107
+ role: "user",
108
+ name: "TextAfterCursor",
109
+ content: editorState.textAfterCursor,
110
+ },
111
+ {
112
+ role: "user",
113
+ name: "EditingPrompt",
114
+ content: editingPrompt,
115
+ },
116
+ ];
117
+
118
+ return await editingApiConfig.apiEndpoint.run(
119
+ abortSignal,
120
+ messages,
121
+ editingApiConfig.forwardedParams
122
+ );
123
+ });
124
+
125
+ return res;
126
+ },
127
+ [editingApiConfig, getContextString, contextCategories, textareaPurpose]
128
+ );
129
+
130
+ const insertionOrEditingFunction = useCallback(
131
+ async (
132
+ editorState: EditingEditorState,
133
+ insertionPrompt: string,
134
+ abortSignal: AbortSignal
135
+ ) => {
136
+ if (editorState.selectedText === "") {
137
+ return await insertionFunction(
138
+ editorState,
139
+ insertionPrompt,
140
+ abortSignal
141
+ );
142
+ } else {
143
+ return await editingFunction(editorState, insertionPrompt, abortSignal);
144
+ }
145
+ },
146
+ [insertionFunction, editingFunction]
147
+ );
148
+
149
+ return insertionOrEditingFunction;
150
+ }
@@ -0,0 +1,21 @@
1
+ import { Ref, RefObject, useEffect } from "react";
2
+
3
+ // Updates the height of a <textarea> when the value changes.
4
+ const useAutosizeTextArea = (
5
+ textAreaRef: RefObject<HTMLTextAreaElement>,
6
+ value: string
7
+ ) => {
8
+ useEffect(() => {
9
+ if (textAreaRef.current !== null) {
10
+ // We need to reset the height momentarily to get the correct scrollHeight for the textarea
11
+ textAreaRef.current.style.height = "0px";
12
+ const scrollHeight = textAreaRef.current.scrollHeight;
13
+
14
+ // We then set the height directly, outside of the render loop
15
+ // Trying to set this with state or a ref will product an incorrect value.
16
+ textAreaRef.current.style.height = scrollHeight + "px";
17
+ }
18
+ }, [textAreaRef, value]);
19
+ };
20
+
21
+ export default useAutosizeTextArea;
@@ -1,21 +1,41 @@
1
- import { Editor, Node, Path, Range, Text, Element } from "slate";
1
+ import {
2
+ Editor,
3
+ Node,
4
+ Path,
5
+ Range,
6
+ Text,
7
+ Element,
8
+ BasePoint,
9
+ BaseRange,
10
+ Point,
11
+ } from "slate";
2
12
  import { EditorAutocompleteState } from "../types/base/editor-autocomplete-state";
3
13
 
4
- export function getTextAroundCursor(
14
+ export interface EditorTextState {
15
+ selection: BaseRange;
16
+
17
+ textBeforeCursor: string;
18
+ selectedText: string;
19
+ textAfterCursor: string;
20
+ }
21
+
22
+ export function getTextAroundCollapsedCursor(
5
23
  editor: Editor
6
24
  ): EditorAutocompleteState | null {
7
25
  const { selection } = editor;
8
-
9
26
  if (!selection || !Range.isCollapsed(selection)) {
10
27
  return null;
11
28
  }
29
+
30
+ const cursorPoint = selection.anchor;
31
+
12
32
  // Create two ranges: one before the anchor and one after
13
33
  const beforeRange: Range = {
14
34
  anchor: Editor.start(editor, []),
15
- focus: selection.anchor,
35
+ focus: cursorPoint,
16
36
  };
17
37
  const afterRange: Range = {
18
- anchor: selection.anchor,
38
+ anchor: cursorPoint,
19
39
  focus: Editor.end(editor, []),
20
40
  };
21
41
 
@@ -24,12 +44,43 @@ export function getTextAroundCursor(
24
44
  const after = extractTextWithNewlines(editor, afterRange);
25
45
 
26
46
  return {
27
- cursorPoint: selection.anchor,
47
+ cursorPoint: cursorPoint,
28
48
  textBeforeCursor: before,
29
49
  textAfterCursor: after,
30
50
  };
31
51
  }
32
52
 
53
+ export function getTextAroundSelection(editor: Editor): EditorTextState | null {
54
+ const { selection } = editor;
55
+ if (!selection) {
56
+ return null;
57
+ }
58
+
59
+ const wellOrderedSelection = wellOrderedRange(selection);
60
+
61
+ // Create two ranges: one before the anchor and one after
62
+ const beforeRange: Range = {
63
+ anchor: Editor.start(editor, []),
64
+ focus: wellOrderedSelection.anchor,
65
+ };
66
+ const afterRange: Range = {
67
+ anchor: wellOrderedSelection.focus,
68
+ focus: Editor.end(editor, []),
69
+ };
70
+
71
+ // Extract text for these ranges
72
+ const before = extractTextWithNewlines(editor, beforeRange);
73
+ const after = extractTextWithNewlines(editor, afterRange);
74
+ const selectedText = extractTextWithNewlines(editor, wellOrderedSelection);
75
+
76
+ return {
77
+ selection: wellOrderedSelection,
78
+ textBeforeCursor: before,
79
+ selectedText,
80
+ textAfterCursor: after,
81
+ };
82
+ }
83
+
33
84
  export function getFullEditorTextWithNewlines(editor: Editor): string {
34
85
  const fullDocumentRange: Range = {
35
86
  anchor: Editor.start(editor, []),
@@ -80,3 +131,17 @@ export function extractTextWithNewlines(editor: Editor, range: Range): string {
80
131
 
81
132
  return text;
82
133
  }
134
+
135
+ function wellOrderedRange(range: BaseRange): BaseRange {
136
+ const { anchor, focus } = range;
137
+ // if anchor is before focus, return range as is
138
+ if (Point.isBefore(anchor, focus)) {
139
+ return range;
140
+ }
141
+
142
+ // if focus is before anchor, return range with anchor and focus swapped
143
+ return {
144
+ anchor: focus,
145
+ focus: anchor,
146
+ };
147
+ }
@@ -0,0 +1,23 @@
1
+ export function retry<T>(
2
+ fn: () => Promise<T>,
3
+ retriesLeft: number = 2,
4
+ interval: number = 200,
5
+ backoff: number = 1.5
6
+ ): Promise<T> {
7
+ return new Promise((resolve, reject) => {
8
+ fn()
9
+ .then(resolve)
10
+ .catch((error) => {
11
+ if (retriesLeft === 1) {
12
+ reject(error);
13
+ return;
14
+ }
15
+
16
+ setTimeout(() => {
17
+ retry(fn, retriesLeft - 1, interval * backoff, backoff)
18
+ .then(resolve)
19
+ .catch(reject);
20
+ }, interval);
21
+ });
22
+ });
23
+ }
package/src/styles.css CHANGED
@@ -1,3 +1,5 @@
1
+ @import url('material-icons/iconfont/material-icons.css');
2
+
1
3
  @tailwind base;
2
4
  @tailwind components;
3
5
  @tailwind utilities;
@@ -0,0 +1,27 @@
1
+ import { AutosuggestionsConfig, ChatlikeApiEndpoint } from ".";
2
+ import { InsertionsApiConfig } from "./insertions-api-config";
3
+ import { SuggestionsApiConfig } from "./suggestions-api-config";
4
+
5
+ // Mostly mirrors a partial SuggestionsApiConfig, but with some fields MANDATORY.
6
+ export interface SuggestionsApiConfigUserSpecified
7
+ extends Partial<Omit<SuggestionsApiConfig, "apiEndpoint">> {
8
+ apiEndpoint: ChatlikeApiEndpoint;
9
+ }
10
+
11
+ // Mostly mirrors a partial InsertionsApiConfig, but with some fields MANDATORY.
12
+ export interface InsertionsApiConfigUserSpecified
13
+ extends Partial<Omit<InsertionsApiConfig, "apiEndpoint">> {
14
+ apiEndpoint: ChatlikeApiEndpoint;
15
+ }
16
+
17
+ // Mostly mirrors a partial AutosuggestionsConfig, but with some fields MANDATORY.
18
+ export interface AutosuggestionsConfigUserSpecified
19
+ extends Partial<
20
+ Omit<AutosuggestionsConfig, "chatApiConfigs" | "textareaPurpose">
21
+ > {
22
+ textareaPurpose: string; // the user MUST specify textareaPurpose - it's not optional
23
+ chatApiConfigs: {
24
+ suggestionsApiConfig?: SuggestionsApiConfigUserSpecified;
25
+ insertionApiConfig?: InsertionsApiConfigUserSpecified;
26
+ };
27
+ }
@@ -0,0 +1,43 @@
1
+ import {
2
+ BaseAutosuggestionsConfig,
3
+ defaultBaseAutosuggestionsConfig,
4
+ } from "../base";
5
+ import {
6
+ SuggestionsApiConfig,
7
+ defaultSuggestionsApiConfig,
8
+ } from "./suggestions-api-config";
9
+ import {
10
+ InsertionsApiConfig,
11
+ defaultInsertionsApiConfig,
12
+ } from "./insertions-api-config";
13
+ import { ChatlikeApiEndpoint } from ".";
14
+ import {
15
+ EditingApiConfig,
16
+ defaultEditingApiConfig,
17
+ } from "./editing-api-config";
18
+
19
+ // Like the base autosuggestions config, with 2 additional fields:
20
+ // 1. externalContextCategories: string[] | undefined;
21
+ // 2. instead of apiConfigs, we have chatApiConfigs: a higher-level abstraction that uses a ChatGPT-like API endpoint.
22
+ export interface AutosuggestionsConfig
23
+ extends Omit<BaseAutosuggestionsConfig, "apiConfig"> {
24
+ externalContextCategories: string[] | undefined;
25
+ chatApiConfigs: {
26
+ suggestionsApiConfig: SuggestionsApiConfig;
27
+ insertionApiConfig: InsertionsApiConfig;
28
+ editingApiConfig: EditingApiConfig;
29
+ };
30
+ }
31
+
32
+ export const defaultAutosuggestionsConfig: Omit<
33
+ AutosuggestionsConfig,
34
+ "textareaPurpose" | "apiEndpoint"
35
+ > = {
36
+ ...defaultBaseAutosuggestionsConfig,
37
+ externalContextCategories: undefined,
38
+ chatApiConfigs: {
39
+ suggestionsApiConfig: defaultSuggestionsApiConfig,
40
+ insertionApiConfig: defaultInsertionsApiConfig,
41
+ editingApiConfig: defaultEditingApiConfig,
42
+ },
43
+ };
@@ -0,0 +1,102 @@
1
+ import { ChatlikeApiEndpoint } from "./subtypes/chatlike-api-endpoint";
2
+ import { MakeSystemPrompt } from "./subtypes/make-system-prompt";
3
+ import { MinimalChatGPTMessage } from "./subtypes/minimal-chat-gpt-message";
4
+
5
+ export interface EditingApiConfig {
6
+ apiEndpoint: ChatlikeApiEndpoint;
7
+ makeSystemPrompt: MakeSystemPrompt;
8
+ fewShotMessages: MinimalChatGPTMessage[];
9
+ forwardedParams: { [key: string]: any } | undefined;
10
+ }
11
+
12
+ export const defaultEditingMakeSystemPrompt: MakeSystemPrompt = (
13
+ textareaPurpose,
14
+ contextString
15
+ ) => {
16
+ return `You are a versatile writing assistant.
17
+
18
+ The user is writing some text.
19
+ The purpose is: \"${textareaPurpose}\"
20
+
21
+ The user also provides you with a prompt for EDITING some text they are writing.
22
+ Your job is to come up with an EDIT of the text that the user would like to use - AS BEST YOU CAN.
23
+
24
+ Adjust yourself to the user's style and implied intent.
25
+
26
+ The user will provide the following information; use this to infer the best relevant EDIT:
27
+ <TextBeforeCursor>
28
+ <TextToEdit>
29
+ <TextAfterCursor>
30
+ <EditingPrompt>
31
+
32
+ <YourEditSuggestion>
33
+
34
+ If we need to add a whitespace character to the suggested edit text, make sure to explicitly add it in.
35
+
36
+ The following external context is also provided. Use it to help you make better suggestions!!!
37
+ \`\`\`
38
+ ${contextString}
39
+ \`\`\`
40
+ `;
41
+ };
42
+
43
+ export const defaultEditingFewShotMessages: MinimalChatGPTMessage[] = [
44
+ {
45
+ role: "user",
46
+ name: "TextBeforeCursor",
47
+ content: "This morning I woke up and went straight to the grocery store. ",
48
+ },
49
+ {
50
+ role: "user",
51
+ name: "TextToEdit",
52
+ content:
53
+ "While I was there I also picked up some apples, oranges, and bananas. ",
54
+ },
55
+ {
56
+ role: "user",
57
+ name: "TextAfterCursor",
58
+ content:
59
+ "The grocery store was having a sale on fruit, so I decided to stock up.",
60
+ },
61
+ {
62
+ role: "user",
63
+ name: "EditingPrompt",
64
+ content: "I bought a big watermelon",
65
+ },
66
+ {
67
+ role: "assistant",
68
+ content:
69
+ " When I arrived I went straight to the produce section and picked out a big watermelon. ",
70
+ },
71
+ {
72
+ role: "user",
73
+ name: "TextAfterCursor",
74
+ content:
75
+ "and (iii) to the appointment of the Equityholders' Representative pursuant to Section 10.7 of the Merger Agreement and to the provisions thereof.",
76
+ },
77
+ {
78
+ role: "user",
79
+ name: "TextBeforeCursor",
80
+ content:
81
+ '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;',
82
+ },
83
+ {
84
+ role: "user",
85
+ name: "InsertionPrompt",
86
+ content: "add section about the optionholder's pro rata share",
87
+ },
88
+ {
89
+ role: "assistant",
90
+ content:
91
+ ' (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"; ',
92
+ },
93
+ ];
94
+
95
+ export const defaultEditingApiConfig: EditingApiConfig = {
96
+ apiEndpoint: ChatlikeApiEndpoint.standardOpenAIEndpoint(
97
+ "/api/copilotkit_chatlike"
98
+ ),
99
+ makeSystemPrompt: defaultEditingMakeSystemPrompt,
100
+ fewShotMessages: defaultEditingFewShotMessages,
101
+ forwardedParams: undefined,
102
+ };
@@ -0,0 +1,11 @@
1
+ export { defaultAutosuggestionsConfig } from "./autosuggestions-config";
2
+ export type { AutosuggestionsConfig } from "./autosuggestions-config";
3
+
4
+ export type { MinimalChatGPTMessage } from "./subtypes/minimal-chat-gpt-message";
5
+ export type { MakeSystemPrompt } from "./subtypes/make-system-prompt";
6
+ export { ChatlikeApiEndpoint } from "./subtypes/chatlike-api-endpoint";
7
+ export type { ChatlikeApiEndpointImpl } from "./subtypes/chatlike-api-endpoint";
8
+
9
+ export type { AutosuggestionsConfigUserSpecified } from "./autosuggestions-config-user-specified";
10
+ export type { SuggestionsApiConfigUserSpecified } from "./autosuggestions-config-user-specified";
11
+ export type { InsertionsApiConfigUserSpecified } from "./autosuggestions-config-user-specified";
@@ -0,0 +1,95 @@
1
+ import { ChatlikeApiEndpoint } from "./subtypes/chatlike-api-endpoint";
2
+ import { MakeSystemPrompt } from "./subtypes/make-system-prompt";
3
+ import { MinimalChatGPTMessage } from "./subtypes/minimal-chat-gpt-message";
4
+
5
+ export interface InsertionsApiConfig {
6
+ apiEndpoint: ChatlikeApiEndpoint;
7
+ makeSystemPrompt: MakeSystemPrompt;
8
+ fewShotMessages: MinimalChatGPTMessage[];
9
+ forwardedParams: { [key: string]: any } | undefined;
10
+ }
11
+
12
+ export const defaultInsertionsMakeSystemPrompt: MakeSystemPrompt = (
13
+ textareaPurpose,
14
+ contextString
15
+ ) => {
16
+ return `You are a versatile writing assistant.
17
+
18
+ The user is writing some text.
19
+ The purpose is: \"${textareaPurpose}\"
20
+
21
+ The user also provides you with a prompt for INSERTIONS into the text they are writing.
22
+ Your job is to come up with an INSERTION into the text that the user would like AS BEST YOU CAN.
23
+ Only guess a SHORT distance ahead. Usually 1 sentence, or at most 1 paragraph.
24
+
25
+ Adjust yourself to the user's style and implied intent.
26
+
27
+ 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.
28
+ <TextAfterCursor>
29
+ <TextBeforeCursor>
30
+ <InsertionPrompt>
31
+ <YourSuggestion>
32
+
33
+ If we need to add a whitespace character to the suggested text, make sure to explicitly add it in.
34
+
35
+ The following external context is also provided. Use it to help you make better suggestions!!!
36
+ \`\`\`
37
+ ${contextString}
38
+ \`\`\`
39
+ `;
40
+ };
41
+
42
+ export const defaultInsertionsFewShotMessages: MinimalChatGPTMessage[] = [
43
+ {
44
+ role: "user",
45
+ name: "TextAfterCursor",
46
+ content:
47
+ "While I was there I also picked up some apples, oranges, and bananas.",
48
+ },
49
+ {
50
+ role: "user",
51
+ name: "TextBeforeCursor",
52
+ content: "This morning I woke up and went straight to the grocery store.",
53
+ },
54
+ {
55
+ role: "user",
56
+ name: "InsertionPrompt",
57
+ content: "I bought a big watermelon",
58
+ },
59
+ {
60
+ role: "assistant",
61
+ content:
62
+ " When I arrived I went straight to the produce section and picked out a big watermelon. ",
63
+ },
64
+ {
65
+ role: "user",
66
+ name: "TextAfterCursor",
67
+ content:
68
+ "and (iii) to the appointment of the Equityholders' Representative pursuant to Section 10.7 of the Merger Agreement and to the provisions thereof.",
69
+ },
70
+ {
71
+ role: "user",
72
+ name: "TextBeforeCursor",
73
+ content:
74
+ '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;',
75
+ },
76
+ {
77
+ role: "user",
78
+ name: "InsertionPrompt",
79
+ content: "add section about the optionholder's pro rata share",
80
+ },
81
+ {
82
+ role: "assistant",
83
+ content:
84
+ ' (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"; ',
85
+ },
86
+ ];
87
+
88
+ export const defaultInsertionsApiConfig: InsertionsApiConfig = {
89
+ apiEndpoint: ChatlikeApiEndpoint.standardOpenAIEndpoint(
90
+ "/api/copilotkit_chatlike"
91
+ ),
92
+ makeSystemPrompt: defaultInsertionsMakeSystemPrompt,
93
+ fewShotMessages: defaultInsertionsFewShotMessages,
94
+ forwardedParams: undefined,
95
+ };
@@ -1,4 +1,4 @@
1
- import { MinimalChatGPTMessage } from "..";
1
+ import { MinimalChatGPTMessage } from "./minimal-chat-gpt-message";
2
2
 
3
3
  export type ChatlikeApiEndpointImpl = (
4
4
  abortSignal: AbortSignal,
@@ -6,10 +6,16 @@ export type ChatlikeApiEndpointImpl = (
6
6
  forwardedProps?: { [key: string]: any }
7
7
  ) => Promise<string>;
8
8
 
9
+ export type StreamingChatlikeApiEndpointImpl = (
10
+ abortSignal: AbortSignal,
11
+ messages: MinimalChatGPTMessage[],
12
+ forwardedProps?: { [key: string]: any }
13
+ ) => Promise<ReadableStream<string>>;
14
+
9
15
  export class ChatlikeApiEndpoint {
10
- public run: ChatlikeApiEndpointImpl;
16
+ public run: StreamingChatlikeApiEndpointImpl;
11
17
 
12
- constructor(run: ChatlikeApiEndpointImpl) {
18
+ constructor(run: StreamingChatlikeApiEndpointImpl) {
13
19
  this.run = run;
14
20
  }
15
21
 
@@ -34,9 +40,15 @@ export class ChatlikeApiEndpoint {
34
40
  signal: abortSignal,
35
41
  });
36
42
 
37
- const json = await res.json();
38
- const suggestion = json.choices[0].message.content;
39
- return suggestion;
43
+ const bodySteram: ReadableStream<Uint8Array> | null = res.body;
44
+ if (!bodySteram) {
45
+ throw new Error("The response body is empty.");
46
+ }
47
+
48
+ // map the stream to a stream of strings
49
+ const stringStream = bodySteram.pipeThrough(new TextDecoderStream());
50
+
51
+ return stringStream;
40
52
  }
41
53
  );
42
54
  }
@@ -46,7 +58,7 @@ export class ChatlikeApiEndpoint {
46
58
  * @param run - The implementation of the ChatlikeApiEndpointImpl interface.
47
59
  * @returns A new instance of ChatlikeApiEndpoint .
48
60
  */
49
- static custom(run: ChatlikeApiEndpointImpl): ChatlikeApiEndpoint {
61
+ static custom(run: StreamingChatlikeApiEndpointImpl): ChatlikeApiEndpoint {
50
62
  return new ChatlikeApiEndpoint(run);
51
63
  }
52
64
  }
@@ -0,0 +1,4 @@
1
+ export type MakeSystemPrompt = (
2
+ textareaPurpose: string,
3
+ contextString: string
4
+ ) => string;