@copilotkit/react-textarea 0.35.0-function-calling-fixes.2 → 0.35.0-mme-cloud.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 (117) hide show
  1. package/.turbo/turbo-build.log +428 -432
  2. package/CHANGELOG.md +8 -20
  3. package/dist/{chunk-VPEH6V7T.mjs → chunk-FMQG2ZM3.mjs} +1 -1
  4. package/dist/chunk-FMQG2ZM3.mjs.map +1 -0
  5. package/dist/{chunk-SP5HFO3H.mjs → chunk-HWM2CVKO.mjs} +2 -2
  6. package/dist/{chunk-22TYLEZZ.mjs → chunk-XU5Z5BWV.mjs} +2 -2
  7. package/dist/components/base-copilot-textarea/base-copilot-textarea.js.map +1 -1
  8. package/dist/components/base-copilot-textarea/base-copilot-textarea.mjs +36 -1752
  9. package/dist/components/base-copilot-textarea/base-copilot-textarea.mjs.map +1 -1
  10. package/dist/components/base-copilot-textarea/render-element.mjs +4 -48
  11. package/dist/components/base-copilot-textarea/render-element.mjs.map +1 -1
  12. package/dist/components/base-copilot-textarea/render-placeholder.mjs +4 -46
  13. package/dist/components/base-copilot-textarea/render-placeholder.mjs.map +1 -1
  14. package/dist/components/base-copilot-textarea/track-cursor-moved-since-last-text-change.mjs +5 -83
  15. package/dist/components/base-copilot-textarea/track-cursor-moved-since-last-text-change.mjs.map +1 -1
  16. package/dist/components/base-copilot-textarea/use-add-branding-css.mjs +4 -54
  17. package/dist/components/base-copilot-textarea/use-add-branding-css.mjs.map +1 -1
  18. package/dist/components/copilot-textarea/copilot-textarea.js.map +1 -1
  19. package/dist/components/copilot-textarea/copilot-textarea.mjs +45 -2196
  20. package/dist/components/copilot-textarea/copilot-textarea.mjs.map +1 -1
  21. package/dist/components/hovering-toolbar/hovering-editor-provider.mjs +5 -13
  22. package/dist/components/hovering-toolbar/hovering-editor-provider.mjs.map +1 -1
  23. package/dist/components/hovering-toolbar/hovering-toolbar-components.mjs +8 -130
  24. package/dist/components/hovering-toolbar/hovering-toolbar-components.mjs.map +1 -1
  25. package/dist/components/hovering-toolbar/hovering-toolbar.mjs +19 -876
  26. package/dist/components/hovering-toolbar/hovering-toolbar.mjs.map +1 -1
  27. package/dist/components/hovering-toolbar/text-insertion-prompt-box/hovering-insertion-prompt-box-core.mjs +14 -569
  28. package/dist/components/hovering-toolbar/text-insertion-prompt-box/hovering-insertion-prompt-box-core.mjs.map +1 -1
  29. package/dist/components/hovering-toolbar/text-insertion-prompt-box/hovering-insertion-prompt-box.mjs +15 -592
  30. package/dist/components/hovering-toolbar/text-insertion-prompt-box/hovering-insertion-prompt-box.mjs.map +1 -1
  31. package/dist/components/hovering-toolbar/text-insertion-prompt-box/included-files-preview.mjs +7 -98
  32. package/dist/components/hovering-toolbar/text-insertion-prompt-box/included-files-preview.mjs.map +1 -1
  33. package/dist/components/hovering-toolbar/text-insertion-prompt-box/index.mjs +16 -592
  34. package/dist/components/hovering-toolbar/text-insertion-prompt-box/index.mjs.map +1 -1
  35. package/dist/components/index.js.map +1 -1
  36. package/dist/components/index.mjs +48 -2196
  37. package/dist/components/index.mjs.map +1 -1
  38. package/dist/components/manual-ui/chip-with-icon.mjs +2 -0
  39. package/dist/components/manual-ui/chip-with-icon.mjs.map +1 -1
  40. package/dist/components/source-search-box/source-search-box.mjs +8 -207
  41. package/dist/components/source-search-box/source-search-box.mjs.map +1 -1
  42. package/dist/components/ui/button.mjs +6 -81
  43. package/dist/components/ui/button.mjs.map +1 -1
  44. package/dist/components/ui/card.mjs +7 -42
  45. package/dist/components/ui/card.mjs.map +1 -1
  46. package/dist/components/ui/command.mjs +14 -232
  47. package/dist/components/ui/command.mjs.map +1 -1
  48. package/dist/components/ui/dialog.mjs +11 -134
  49. package/dist/components/ui/dialog.mjs.map +1 -1
  50. package/dist/components/ui/label.mjs +5 -56
  51. package/dist/components/ui/label.mjs.map +1 -1
  52. package/dist/components/ui/separator.mjs +7 -42
  53. package/dist/components/ui/separator.mjs.map +1 -1
  54. package/dist/components/ui/textarea.mjs +7 -42
  55. package/dist/components/ui/textarea.mjs.map +1 -1
  56. package/dist/context/index.mjs +1 -0
  57. package/dist/hooks/base-copilot-textarea-implementation/use-autosuggestions.mjs +7 -155
  58. package/dist/hooks/base-copilot-textarea-implementation/use-autosuggestions.mjs.map +1 -1
  59. package/dist/hooks/base-copilot-textarea-implementation/use-copilot-textarea-editor.mjs +5 -165
  60. package/dist/hooks/base-copilot-textarea-implementation/use-copilot-textarea-editor.mjs.map +1 -1
  61. package/dist/hooks/base-copilot-textarea-implementation/use-populate-copilot-textarea-ref.js.map +1 -1
  62. package/dist/hooks/base-copilot-textarea-implementation/use-populate-copilot-textarea-ref.mjs +6 -145
  63. package/dist/hooks/base-copilot-textarea-implementation/use-populate-copilot-textarea-ref.mjs.map +1 -1
  64. package/dist/hooks/index.mjs +1 -0
  65. package/dist/hooks/make-autosuggestions-function/use-make-standard-autosuggestions-function.mjs +5 -111
  66. package/dist/hooks/make-autosuggestions-function/use-make-standard-autosuggestions-function.mjs.map +1 -1
  67. package/dist/hooks/make-autosuggestions-function/use-make-standard-insertion-function.mjs +5 -160
  68. package/dist/hooks/make-autosuggestions-function/use-make-standard-insertion-function.mjs.map +1 -1
  69. package/dist/hooks/misc/use-autosize-textarea.mjs +4 -12
  70. package/dist/hooks/misc/use-autosize-textarea.mjs.map +1 -1
  71. package/dist/index.js.map +1 -1
  72. package/dist/index.mjs +56 -2196
  73. package/dist/index.mjs.map +1 -1
  74. package/dist/lib/debouncer.mjs +4 -48
  75. package/dist/lib/debouncer.mjs.map +1 -1
  76. package/dist/lib/editor-to-text.mjs +4 -40
  77. package/dist/lib/editor-to-text.mjs.map +1 -1
  78. package/dist/lib/get-text-around-cursor.mjs +7 -96
  79. package/dist/lib/get-text-around-cursor.mjs.map +1 -1
  80. package/dist/lib/retry.mjs +4 -14
  81. package/dist/lib/retry.mjs.map +1 -1
  82. package/dist/lib/slatejs-edits/add-autocompletions.mjs +4 -22
  83. package/dist/lib/slatejs-edits/add-autocompletions.mjs.map +1 -1
  84. package/dist/lib/slatejs-edits/clear-autocompletions.mjs +4 -17
  85. package/dist/lib/slatejs-edits/clear-autocompletions.mjs.map +1 -1
  86. package/dist/lib/slatejs-edits/replace-text.mjs +4 -24
  87. package/dist/lib/slatejs-edits/replace-text.mjs.map +1 -1
  88. package/dist/lib/slatejs-edits/with-partial-history.mjs +5 -102
  89. package/dist/lib/slatejs-edits/with-partial-history.mjs.map +1 -1
  90. package/dist/lib/stream-promise-flatten.mjs +4 -44
  91. package/dist/lib/stream-promise-flatten.mjs.map +1 -1
  92. package/dist/lib/utils.mjs +9 -63
  93. package/dist/lib/utils.mjs.map +1 -1
  94. package/dist/types/autosuggestions-config/autosuggestions-config.mjs +9 -271
  95. package/dist/types/autosuggestions-config/autosuggestions-config.mjs.map +1 -1
  96. package/dist/types/autosuggestions-config/editing-api-config.mjs +6 -82
  97. package/dist/types/autosuggestions-config/editing-api-config.mjs.map +1 -1
  98. package/dist/types/autosuggestions-config/index.mjs +10 -271
  99. package/dist/types/autosuggestions-config/index.mjs.map +1 -1
  100. package/dist/types/autosuggestions-config/insertions-api-config.mjs +6 -73
  101. package/dist/types/autosuggestions-config/insertions-api-config.mjs.map +1 -1
  102. package/dist/types/autosuggestions-config/suggestions-api-config.mjs +6 -59
  103. package/dist/types/autosuggestions-config/suggestions-api-config.mjs.map +1 -1
  104. package/dist/types/base/base-autosuggestions-config.mjs +4 -23
  105. package/dist/types/base/base-autosuggestions-config.mjs.map +1 -1
  106. package/dist/types/base/editor-autocomplete-state.mjs +5 -14
  107. package/dist/types/base/editor-autocomplete-state.mjs.map +1 -1
  108. package/dist/types/base/index.mjs +5 -23
  109. package/dist/types/base/index.mjs.map +1 -1
  110. package/dist/types/html-copilot-textarea-element.mjs +1 -0
  111. package/dist/types/index.mjs +14 -271
  112. package/dist/types/index.mjs.map +1 -1
  113. package/package.json +6 -6
  114. package/src/hooks/base-copilot-textarea-implementation/use-populate-copilot-textarea-ref.ts +4 -1
  115. package/dist/chunk-VPEH6V7T.mjs.map +0 -1
  116. /package/dist/{chunk-SP5HFO3H.mjs.map → chunk-HWM2CVKO.mjs.map} +0 -0
  117. /package/dist/{chunk-22TYLEZZ.mjs.map → chunk-XU5Z5BWV.mjs.map} +0 -0
@@ -1 +1 @@
1
- {"version":3,"sources":["../../../src/hooks/base-copilot-textarea-implementation/use-populate-copilot-textarea-ref.ts","../../../src/lib/get-text-around-cursor.ts","../../../src/lib/slatejs-edits/replace-text.ts"],"sourcesContent":["import React from \"react\";\nimport { Editor } from \"slate\";\nimport { ReactEditor } from \"slate-react\";\nimport { getFullEditorTextWithNewlines } from \"../../lib/get-text-around-cursor\";\nimport { replaceEditorText } from \"../../lib/slatejs-edits/replace-text\";\nimport { HTMLCopilotTextAreaElement } from \"../../types\";\nimport { CustomEditor } from \"../../types/base/custom-editor\";\n\nexport function usePopulateCopilotTextareaRef(\n editor: Editor,\n ref: React.Ref<HTMLCopilotTextAreaElement>,\n) {\n React.useImperativeHandle(\n ref,\n () => {\n class Combined {\n constructor(private customMethods: CustomMethods, private editorHtmlElement: HTMLElement) {}\n\n [key: string]: any;\n\n get(target: any, propKey: string): any {\n if (this.isKeyOfCustomMethods(propKey)) {\n const value = this.customMethods[propKey];\n if (typeof value === \"function\") {\n return value.bind(this.customMethods);\n }\n return value;\n } else if (this.isKeyOfHTMLElement(propKey)) {\n const value = this.editorHtmlElement[propKey];\n if (typeof value === \"function\") {\n return value.bind(this.editorHtmlElement);\n }\n return value;\n }\n }\n\n set(target: any, propKey: string, value: any): boolean {\n if (this.isKeyOfCustomMethods(propKey)) {\n (this.customMethods as any)[propKey] = value;\n } else if (this.isKeyOfHTMLElement(propKey)) {\n (this.editorHtmlElement as any)[propKey] = value;\n } else {\n // Default behavior (optional)\n target[propKey] = value;\n }\n return true;\n }\n\n private isKeyOfCustomMethods(key: string): key is keyof CustomMethods {\n return key in this.customMethods;\n }\n\n private isKeyOfHTMLElement(key: string): key is keyof HTMLElement {\n return key in this.editorHtmlElement;\n }\n }\n\n const handler = {\n get(target: any, propKey: keyof CustomMethods | keyof HTMLElement) {\n return target.get(target, propKey);\n },\n set(target: any, propKey: keyof CustomMethods | keyof HTMLElement, value: any) {\n return target.set(target, propKey, value);\n },\n };\n\n class CustomMethods {\n constructor(private editor: CustomEditor) {}\n\n focus() {\n ReactEditor.focus(this.editor);\n }\n\n blur() {\n ReactEditor.blur(this.editor);\n }\n\n get value() {\n return getFullEditorTextWithNewlines(this.editor);\n }\n set value(value: string) {\n replaceEditorText(this.editor, value);\n }\n }\n\n const editorHtmlElement = ReactEditor.toDOMNode(editor, editor);\n const customMethods = new CustomMethods(editor);\n\n const combined = new Combined(customMethods, editorHtmlElement);\n return new Proxy(combined, handler);\n },\n [editor],\n );\n}\n","import { Editor, Node, Path, Range, Text, Element, BasePoint, BaseRange, Point } from \"slate\";\nimport { EditorAutocompleteState } from \"../types/base/editor-autocomplete-state\";\n\nexport interface EditorTextState {\n selection: BaseRange;\n\n textBeforeCursor: string;\n selectedText: string;\n textAfterCursor: string;\n}\n\nexport function getTextAroundCollapsedCursor(editor: Editor): EditorAutocompleteState | null {\n const { selection } = editor;\n if (!selection || !Range.isCollapsed(selection)) {\n return null;\n }\n\n const cursorPoint = selection.anchor;\n\n // Create two ranges: one before the anchor and one after\n const beforeRange: Range = {\n anchor: Editor.start(editor, []),\n focus: cursorPoint,\n };\n const afterRange: Range = {\n anchor: cursorPoint,\n focus: Editor.end(editor, []),\n };\n\n // Extract text for these ranges\n const before = extractTextWithNewlines(editor, beforeRange);\n const after = extractTextWithNewlines(editor, afterRange);\n\n return {\n cursorPoint: cursorPoint,\n textBeforeCursor: before,\n textAfterCursor: after,\n };\n}\n\nexport function getTextAroundSelection(editor: Editor): EditorTextState | null {\n const { selection } = editor;\n if (!selection) {\n return null;\n }\n\n const wellOrderedSelection = wellOrderedRange(selection);\n\n // Create two ranges: one before the anchor and one after\n const beforeRange: Range = {\n anchor: Editor.start(editor, []),\n focus: wellOrderedSelection.anchor,\n };\n const afterRange: Range = {\n anchor: wellOrderedSelection.focus,\n focus: Editor.end(editor, []),\n };\n\n // Extract text for these ranges\n const before = extractTextWithNewlines(editor, beforeRange);\n const after = extractTextWithNewlines(editor, afterRange);\n const selectedText = extractTextWithNewlines(editor, wellOrderedSelection);\n\n return {\n selection: wellOrderedSelection,\n textBeforeCursor: before,\n selectedText,\n textAfterCursor: after,\n };\n}\n\nexport function getFullEditorTextWithNewlines(editor: Editor): string {\n const fullDocumentRange: Range = {\n anchor: Editor.start(editor, []),\n focus: Editor.end(editor, []),\n };\n return extractTextWithNewlines(editor, fullDocumentRange);\n}\n\n// Helper function to extract text with newlines\nexport function extractTextWithNewlines(editor: Editor, range: Range): string {\n const voids = false;\n const [start, end] = Range.edges(range);\n let text = \"\";\n let lastBlock: Node | null = null;\n\n for (const [node, path] of Editor.nodes(editor, {\n at: range,\n match: Text.isText,\n voids,\n })) {\n let t = node.text;\n\n // Determine the parent block of the current text node\n const [block] = Editor.above(editor, {\n at: path,\n match: (n) => Element.isElement(n) && n.type === \"paragraph\",\n }) || [null];\n\n // If we encounter a new block, prepend a newline\n if (lastBlock !== block && block) {\n // check that lastBlock is not null to avoid adding a newline at the beginning\n if (lastBlock) {\n text += \"\\n\";\n }\n lastBlock = block;\n }\n\n if (Path.equals(path, end.path)) {\n t = t.slice(0, end.offset);\n }\n\n if (Path.equals(path, start.path)) {\n t = t.slice(start.offset);\n }\n\n text += t;\n }\n\n return text;\n}\n\nfunction wellOrderedRange(range: BaseRange): BaseRange {\n const { anchor, focus } = range;\n // if anchor is before focus, return range as is\n if (Point.isBefore(anchor, focus)) {\n return range;\n }\n\n // if focus is before anchor, return range with anchor and focus swapped\n return {\n anchor: focus,\n focus: anchor,\n };\n}\n","import { Editor, Transforms } from \"slate\";\n\nexport function replaceEditorText(editor: Editor, newText: string) {\n // clear all previous text\n Transforms.delete(editor, {\n at: {\n anchor: Editor.start(editor, []),\n focus: Editor.end(editor, []),\n },\n });\n\n // insert new text\n if (newText && newText !== \"\") {\n // don't insert empty text - results in strange visual behavior\n Transforms.insertNodes(\n editor,\n [\n {\n type: \"paragraph\",\n children: [{ text: newText }],\n },\n ],\n {\n at: [0],\n },\n );\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,mBAAkB;AAElB,yBAA4B;;;ACF5B,mBAAsF;AAuE/E,SAAS,8BAA8B,QAAwB;AACpE,QAAM,oBAA2B;AAAA,IAC/B,QAAQ,oBAAO,MAAM,QAAQ,CAAC,CAAC;AAAA,IAC/B,OAAO,oBAAO,IAAI,QAAQ,CAAC,CAAC;AAAA,EAC9B;AACA,SAAO,wBAAwB,QAAQ,iBAAiB;AAC1D;AAGO,SAAS,wBAAwB,QAAgB,OAAsB;AAC5E,QAAM,QAAQ;AACd,QAAM,CAAC,OAAO,GAAG,IAAI,mBAAM,MAAM,KAAK;AACtC,MAAI,OAAO;AACX,MAAI,YAAyB;AAE7B,aAAW,CAAC,MAAM,IAAI,KAAK,oBAAO,MAAM,QAAQ;AAAA,IAC9C,IAAI;AAAA,IACJ,OAAO,kBAAK;AAAA,IACZ;AAAA,EACF,CAAC,GAAG;AACF,QAAI,IAAI,KAAK;AAGb,UAAM,CAAC,KAAK,IAAI,oBAAO,MAAM,QAAQ;AAAA,MACnC,IAAI;AAAA,MACJ,OAAO,CAAC,MAAM,qBAAQ,UAAU,CAAC,KAAK,EAAE,SAAS;AAAA,IACnD,CAAC,KAAK,CAAC,IAAI;AAGX,QAAI,cAAc,SAAS,OAAO;AAEhC,UAAI,WAAW;AACb,gBAAQ;AAAA,MACV;AACA,kBAAY;AAAA,IACd;AAEA,QAAI,kBAAK,OAAO,MAAM,IAAI,IAAI,GAAG;AAC/B,UAAI,EAAE,MAAM,GAAG,IAAI,MAAM;AAAA,IAC3B;AAEA,QAAI,kBAAK,OAAO,MAAM,MAAM,IAAI,GAAG;AACjC,UAAI,EAAE,MAAM,MAA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B,OAAO,IAAI;AAAA,UACzC,WAAW,KAAK,mBAAmB,OAAO,GAAG;AAC3C,YAAC,KAAK,kBAA0B,OAAO,IAAI;AAAA,UAC7C,OAAO;AAEL,mBAAO,OAAO,IAAI;AAAA,UACpB;AACA,iBAAO;AAAA,QACT;AAAA,QAEQ,qBAAqB,KAAyC;AACpE,iBAAO,OAAO,KAAK;AAAA,QACrB;AAAA,QAEQ,mBAAmB,KAAuC;AAChE,iBAAO,OAAO,KAAK;AAAA,QACrB;AAAA,MACF;AAEA,YAAM,UAAU;AAAA,QACd,IAAI,QAAa,SAAkD;AACjE,iBAAO,OAAO,IAAI,QAAQ,OAAO;AAAA,QACnC;AAAA,QACA,IAAI,QAAa,SAAkD,OAAY;AAC7E,iBAAO,OAAO,IAAI,QAAQ,SAAS,KAAK;AAAA,QAC1C;AAAA,MACF;AAEA,YAAM,cAAc;AAAA,QAClB,YAAoBC,SAAsB;AAAtB,wBAAAA;AAAA,QAAuB;AAAA,QAE3C,QAAQ;AACN,yCAAY,MAAM,KAAK,MAAM;AAAA,QAC/B;AAAA,QAEA,OAAO;AACL,yCAAY,KAAK,KAAK,MAAM;AAAA,QAC9B;AAAA,QAEA,IAAI,QAAQ;AACV,iBAAO,8BAA8B,KAAK,MAAM;AAAA,QAClD;AAAA,QACA,IAAI,MAAM,OAAe;AACvB,4BAAkB,KAAK,QAAQ,KAAK;AAAA,QACtC;AAAA,MACF;AAEA,YAAM,oBAAoB,+BAAY,UAAU,QAAQ,MAAM;AAC9D,YAAM,gBAAgB,IAAI,cAAc,MAAM;AAE9C,YAAM,WAAW,IAAI,SAAS,eAAe,iBAAiB;AAC9D,aAAO,IAAI,MAAM,UAAU,OAAO;AAAA,IACpC;AAAA,IACA,CAAC,MAAM;AAAA,EACT;AACF;","names":["import_slate","React","customMethods","editorHtmlElement","editor"]}
1
+ {"version":3,"sources":["../../../src/hooks/base-copilot-textarea-implementation/use-populate-copilot-textarea-ref.ts","../../../src/lib/get-text-around-cursor.ts","../../../src/lib/slatejs-edits/replace-text.ts"],"sourcesContent":["import React from \"react\";\nimport { Editor } from \"slate\";\nimport { ReactEditor } from \"slate-react\";\nimport { getFullEditorTextWithNewlines } from \"../../lib/get-text-around-cursor\";\nimport { replaceEditorText } from \"../../lib/slatejs-edits/replace-text\";\nimport { HTMLCopilotTextAreaElement } from \"../../types\";\nimport { CustomEditor } from \"../../types/base/custom-editor\";\n\nexport function usePopulateCopilotTextareaRef(\n editor: Editor,\n ref: React.Ref<HTMLCopilotTextAreaElement>,\n) {\n React.useImperativeHandle(\n ref,\n () => {\n class Combined {\n constructor(\n private customMethods: CustomMethods,\n private editorHtmlElement: HTMLElement,\n ) {}\n\n [key: string]: any;\n\n get(target: any, propKey: string): any {\n if (this.isKeyOfCustomMethods(propKey)) {\n const value = this.customMethods[propKey];\n if (typeof value === \"function\") {\n return value.bind(this.customMethods);\n }\n return value;\n } else if (this.isKeyOfHTMLElement(propKey)) {\n const value = this.editorHtmlElement[propKey];\n if (typeof value === \"function\") {\n return value.bind(this.editorHtmlElement);\n }\n return value;\n }\n }\n\n set(target: any, propKey: string, value: any): boolean {\n if (this.isKeyOfCustomMethods(propKey)) {\n (this.customMethods as any)[propKey] = value;\n } else if (this.isKeyOfHTMLElement(propKey)) {\n (this.editorHtmlElement as any)[propKey] = value;\n } else {\n // Default behavior (optional)\n target[propKey] = value;\n }\n return true;\n }\n\n private isKeyOfCustomMethods(key: string): key is keyof CustomMethods {\n return key in this.customMethods;\n }\n\n private isKeyOfHTMLElement(key: string): key is keyof HTMLElement {\n return key in this.editorHtmlElement;\n }\n }\n\n const handler = {\n get(target: any, propKey: keyof CustomMethods | keyof HTMLElement) {\n return target.get(target, propKey);\n },\n set(target: any, propKey: keyof CustomMethods | keyof HTMLElement, value: any) {\n return target.set(target, propKey, value);\n },\n };\n\n class CustomMethods {\n constructor(private editor: CustomEditor) {}\n\n focus() {\n ReactEditor.focus(this.editor);\n }\n\n blur() {\n ReactEditor.blur(this.editor);\n }\n\n get value() {\n return getFullEditorTextWithNewlines(this.editor);\n }\n set value(value: string) {\n replaceEditorText(this.editor, value);\n }\n }\n\n const editorHtmlElement = ReactEditor.toDOMNode(editor, editor);\n const customMethods = new CustomMethods(editor);\n\n const combined = new Combined(customMethods, editorHtmlElement);\n return new Proxy(combined, handler);\n },\n [editor],\n );\n}\n","import { Editor, Node, Path, Range, Text, Element, BasePoint, BaseRange, Point } from \"slate\";\nimport { EditorAutocompleteState } from \"../types/base/editor-autocomplete-state\";\n\nexport interface EditorTextState {\n selection: BaseRange;\n\n textBeforeCursor: string;\n selectedText: string;\n textAfterCursor: string;\n}\n\nexport function getTextAroundCollapsedCursor(editor: Editor): EditorAutocompleteState | null {\n const { selection } = editor;\n if (!selection || !Range.isCollapsed(selection)) {\n return null;\n }\n\n const cursorPoint = selection.anchor;\n\n // Create two ranges: one before the anchor and one after\n const beforeRange: Range = {\n anchor: Editor.start(editor, []),\n focus: cursorPoint,\n };\n const afterRange: Range = {\n anchor: cursorPoint,\n focus: Editor.end(editor, []),\n };\n\n // Extract text for these ranges\n const before = extractTextWithNewlines(editor, beforeRange);\n const after = extractTextWithNewlines(editor, afterRange);\n\n return {\n cursorPoint: cursorPoint,\n textBeforeCursor: before,\n textAfterCursor: after,\n };\n}\n\nexport function getTextAroundSelection(editor: Editor): EditorTextState | null {\n const { selection } = editor;\n if (!selection) {\n return null;\n }\n\n const wellOrderedSelection = wellOrderedRange(selection);\n\n // Create two ranges: one before the anchor and one after\n const beforeRange: Range = {\n anchor: Editor.start(editor, []),\n focus: wellOrderedSelection.anchor,\n };\n const afterRange: Range = {\n anchor: wellOrderedSelection.focus,\n focus: Editor.end(editor, []),\n };\n\n // Extract text for these ranges\n const before = extractTextWithNewlines(editor, beforeRange);\n const after = extractTextWithNewlines(editor, afterRange);\n const selectedText = extractTextWithNewlines(editor, wellOrderedSelection);\n\n return {\n selection: wellOrderedSelection,\n textBeforeCursor: before,\n selectedText,\n textAfterCursor: after,\n };\n}\n\nexport function getFullEditorTextWithNewlines(editor: Editor): string {\n const fullDocumentRange: Range = {\n anchor: Editor.start(editor, []),\n focus: Editor.end(editor, []),\n };\n return extractTextWithNewlines(editor, fullDocumentRange);\n}\n\n// Helper function to extract text with newlines\nexport function extractTextWithNewlines(editor: Editor, range: Range): string {\n const voids = false;\n const [start, end] = Range.edges(range);\n let text = \"\";\n let lastBlock: Node | null = null;\n\n for (const [node, path] of Editor.nodes(editor, {\n at: range,\n match: Text.isText,\n voids,\n })) {\n let t = node.text;\n\n // Determine the parent block of the current text node\n const [block] = Editor.above(editor, {\n at: path,\n match: (n) => Element.isElement(n) && n.type === \"paragraph\",\n }) || [null];\n\n // If we encounter a new block, prepend a newline\n if (lastBlock !== block && block) {\n // check that lastBlock is not null to avoid adding a newline at the beginning\n if (lastBlock) {\n text += \"\\n\";\n }\n lastBlock = block;\n }\n\n if (Path.equals(path, end.path)) {\n t = t.slice(0, end.offset);\n }\n\n if (Path.equals(path, start.path)) {\n t = t.slice(start.offset);\n }\n\n text += t;\n }\n\n return text;\n}\n\nfunction wellOrderedRange(range: BaseRange): BaseRange {\n const { anchor, focus } = range;\n // if anchor is before focus, return range as is\n if (Point.isBefore(anchor, focus)) {\n return range;\n }\n\n // if focus is before anchor, return range with anchor and focus swapped\n return {\n anchor: focus,\n focus: anchor,\n };\n}\n","import { Editor, Transforms } from \"slate\";\n\nexport function replaceEditorText(editor: Editor, newText: string) {\n // clear all previous text\n Transforms.delete(editor, {\n at: {\n anchor: Editor.start(editor, []),\n focus: Editor.end(editor, []),\n },\n });\n\n // insert new text\n if (newText && newText !== \"\") {\n // don't insert empty text - results in strange visual behavior\n Transforms.insertNodes(\n editor,\n [\n {\n type: \"paragraph\",\n children: [{ text: newText }],\n },\n ],\n {\n at: [0],\n },\n );\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,mBAAkB;AAElB,yBAA4B;;;ACF5B,mBAAsF;AA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iBAAO,OAAO,KAAK;AAAA,QACrB;AAAA,MACF;AAEA,YAAM,UAAU;AAAA,QACd,IAAI,QAAa,SAAkD;AACjE,iBAAO,OAAO,IAAI,QAAQ,OAAO;AAAA,QACnC;AAAA,QACA,IAAI,QAAa,SAAkD,OAAY;AAC7E,iBAAO,OAAO,IAAI,QAAQ,SAAS,KAAK;AAAA,QAC1C;AAAA,MACF;AAEA,YAAM,cAAc;AAAA,QAClB,YAAoBC,SAAsB;AAAtB,wBAAAA;AAAA,QAAuB;AAAA,QAE3C,QAAQ;AACN,yCAAY,MAAM,KAAK,MAAM;AAAA,QAC/B;AAAA,QAEA,OAAO;AACL,yCAAY,KAAK,KAAK,MAAM;AAAA,QAC9B;AAAA,QAEA,IAAI,QAAQ;AACV,iBAAO,8BAA8B,KAAK,MAAM;AAAA,QAClD;AAAA,QACA,IAAI,MAAM,OAAe;AACvB,4BAAkB,KAAK,QAAQ,KAAK;AAAA,QACtC;AAAA,MACF;AAEA,YAAM,oBAAoB,+BAAY,UAAU,QAAQ,MAAM;AAC9D,YAAM,gBAAgB,IAAI,cAAc,MAAM;AAE9C,YAAM,WAAW,IAAI,SAAS,eAAe,iBAAiB;AAC9D,aAAO,IAAI,MAAM,UAAU,OAAO;AAAA,IACpC;AAAA,IACA,CAAC,MAAM;AAAA,EACT;AACF;","names":["import_slate","React","customMethods","editorHtmlElement","editor"]}
@@ -1,148 +1,9 @@
1
- // src/hooks/base-copilot-textarea-implementation/use-populate-copilot-textarea-ref.ts
2
- import React from "react";
3
- import { ReactEditor } from "slate-react";
4
-
5
- // src/lib/get-text-around-cursor.ts
6
- import { Editor, Path, Range, Text, Element, Point } from "slate";
7
- function getFullEditorTextWithNewlines(editor) {
8
- const fullDocumentRange = {
9
- anchor: Editor.start(editor, []),
10
- focus: Editor.end(editor, [])
11
- };
12
- return extractTextWithNewlines(editor, fullDocumentRange);
13
- }
14
- function extractTextWithNewlines(editor, range) {
15
- const voids = false;
16
- const [start, end] = Range.edges(range);
17
- let text = "";
18
- let lastBlock = null;
19
- for (const [node, path] of Editor.nodes(editor, {
20
- at: range,
21
- match: Text.isText,
22
- voids
23
- })) {
24
- let t = node.text;
25
- const [block] = Editor.above(editor, {
26
- at: path,
27
- match: (n) => Element.isElement(n) && n.type === "paragraph"
28
- }) || [null];
29
- if (lastBlock !== block && block) {
30
- if (lastBlock) {
31
- text += "\n";
32
- }
33
- lastBlock = block;
34
- }
35
- if (Path.equals(path, end.path)) {
36
- t = t.slice(0, end.offset);
37
- }
38
- if (Path.equals(path, start.path)) {
39
- t = t.slice(start.offset);
40
- }
41
- text += t;
42
- }
43
- return text;
44
- }
45
-
46
- // src/lib/slatejs-edits/replace-text.ts
47
- import { Editor as Editor2, Transforms } from "slate";
48
- function replaceEditorText(editor, newText) {
49
- Transforms.delete(editor, {
50
- at: {
51
- anchor: Editor2.start(editor, []),
52
- focus: Editor2.end(editor, [])
53
- }
54
- });
55
- if (newText && newText !== "") {
56
- Transforms.insertNodes(
57
- editor,
58
- [
59
- {
60
- type: "paragraph",
61
- children: [{ text: newText }]
62
- }
63
- ],
64
- {
65
- at: [0]
66
- }
67
- );
68
- }
69
- }
70
-
71
- // src/hooks/base-copilot-textarea-implementation/use-populate-copilot-textarea-ref.ts
72
- function usePopulateCopilotTextareaRef(editor, ref) {
73
- React.useImperativeHandle(
74
- ref,
75
- () => {
76
- class Combined {
77
- constructor(customMethods2, editorHtmlElement2) {
78
- this.customMethods = customMethods2;
79
- this.editorHtmlElement = editorHtmlElement2;
80
- }
81
- get(target, propKey) {
82
- if (this.isKeyOfCustomMethods(propKey)) {
83
- const value = this.customMethods[propKey];
84
- if (typeof value === "function") {
85
- return value.bind(this.customMethods);
86
- }
87
- return value;
88
- } else if (this.isKeyOfHTMLElement(propKey)) {
89
- const value = this.editorHtmlElement[propKey];
90
- if (typeof value === "function") {
91
- return value.bind(this.editorHtmlElement);
92
- }
93
- return value;
94
- }
95
- }
96
- set(target, propKey, value) {
97
- if (this.isKeyOfCustomMethods(propKey)) {
98
- this.customMethods[propKey] = value;
99
- } else if (this.isKeyOfHTMLElement(propKey)) {
100
- this.editorHtmlElement[propKey] = value;
101
- } else {
102
- target[propKey] = value;
103
- }
104
- return true;
105
- }
106
- isKeyOfCustomMethods(key) {
107
- return key in this.customMethods;
108
- }
109
- isKeyOfHTMLElement(key) {
110
- return key in this.editorHtmlElement;
111
- }
112
- }
113
- const handler = {
114
- get(target, propKey) {
115
- return target.get(target, propKey);
116
- },
117
- set(target, propKey, value) {
118
- return target.set(target, propKey, value);
119
- }
120
- };
121
- class CustomMethods {
122
- constructor(editor2) {
123
- this.editor = editor2;
124
- }
125
- focus() {
126
- ReactEditor.focus(this.editor);
127
- }
128
- blur() {
129
- ReactEditor.blur(this.editor);
130
- }
131
- get value() {
132
- return getFullEditorTextWithNewlines(this.editor);
133
- }
134
- set value(value) {
135
- replaceEditorText(this.editor, value);
136
- }
137
- }
138
- const editorHtmlElement = ReactEditor.toDOMNode(editor, editor);
139
- const customMethods = new CustomMethods(editor);
140
- const combined = new Combined(customMethods, editorHtmlElement);
141
- return new Proxy(combined, handler);
142
- },
143
- [editor]
144
- );
145
- }
1
+ import {
2
+ usePopulateCopilotTextareaRef
3
+ } from "../../chunk-FMQG2ZM3.mjs";
4
+ import "../../chunk-5UNJXFUO.mjs";
5
+ import "../../chunk-ECR45NSD.mjs";
6
+ import "../../chunk-MRXNTQOX.mjs";
146
7
  export {
147
8
  usePopulateCopilotTextareaRef
148
9
  };
@@ -1 +1 @@
1
- {"version":3,"sources":["../../../src/hooks/base-copilot-textarea-implementation/use-populate-copilot-textarea-ref.ts","../../../src/lib/get-text-around-cursor.ts","../../../src/lib/slatejs-edits/replace-text.ts"],"sourcesContent":["import React from \"react\";\nimport { Editor } from \"slate\";\nimport { ReactEditor } from \"slate-react\";\nimport { getFullEditorTextWithNewlines } from \"../../lib/get-text-around-cursor\";\nimport { replaceEditorText } from \"../../lib/slatejs-edits/replace-text\";\nimport { HTMLCopilotTextAreaElement } from \"../../types\";\nimport { CustomEditor } from \"../../types/base/custom-editor\";\n\nexport function usePopulateCopilotTextareaRef(\n editor: Editor,\n ref: React.Ref<HTMLCopilotTextAreaElement>,\n) {\n React.useImperativeHandle(\n ref,\n () => {\n class Combined {\n constructor(private customMethods: CustomMethods, private editorHtmlElement: HTMLElement) {}\n\n [key: string]: any;\n\n get(target: any, propKey: string): any {\n if (this.isKeyOfCustomMethods(propKey)) {\n const value = this.customMethods[propKey];\n if (typeof value === \"function\") {\n return value.bind(this.customMethods);\n }\n return value;\n } else if (this.isKeyOfHTMLElement(propKey)) {\n const value = this.editorHtmlElement[propKey];\n if (typeof value === \"function\") {\n return value.bind(this.editorHtmlElement);\n }\n return value;\n }\n }\n\n set(target: any, propKey: string, value: any): boolean {\n if (this.isKeyOfCustomMethods(propKey)) {\n (this.customMethods as any)[propKey] = value;\n } else if (this.isKeyOfHTMLElement(propKey)) {\n (this.editorHtmlElement as any)[propKey] = value;\n } else {\n // Default behavior (optional)\n target[propKey] = value;\n }\n return true;\n }\n\n private isKeyOfCustomMethods(key: string): key is keyof CustomMethods {\n return key in this.customMethods;\n }\n\n private isKeyOfHTMLElement(key: string): key is keyof HTMLElement {\n return key in this.editorHtmlElement;\n }\n }\n\n const handler = {\n get(target: any, propKey: keyof CustomMethods | keyof HTMLElement) {\n return target.get(target, propKey);\n },\n set(target: any, propKey: keyof CustomMethods | keyof HTMLElement, value: any) {\n return target.set(target, propKey, value);\n },\n };\n\n class CustomMethods {\n constructor(private editor: CustomEditor) {}\n\n focus() {\n ReactEditor.focus(this.editor);\n }\n\n blur() {\n ReactEditor.blur(this.editor);\n }\n\n get value() {\n return getFullEditorTextWithNewlines(this.editor);\n }\n set value(value: string) {\n replaceEditorText(this.editor, value);\n }\n }\n\n const editorHtmlElement = ReactEditor.toDOMNode(editor, editor);\n const customMethods = new CustomMethods(editor);\n\n const combined = new Combined(customMethods, editorHtmlElement);\n return new Proxy(combined, handler);\n },\n [editor],\n );\n}\n","import { Editor, Node, Path, Range, Text, Element, BasePoint, BaseRange, Point } from \"slate\";\nimport { EditorAutocompleteState } from \"../types/base/editor-autocomplete-state\";\n\nexport interface EditorTextState {\n selection: BaseRange;\n\n textBeforeCursor: string;\n selectedText: string;\n textAfterCursor: string;\n}\n\nexport function getTextAroundCollapsedCursor(editor: Editor): EditorAutocompleteState | null {\n const { selection } = editor;\n if (!selection || !Range.isCollapsed(selection)) {\n return null;\n }\n\n const cursorPoint = selection.anchor;\n\n // Create two ranges: one before the anchor and one after\n const beforeRange: Range = {\n anchor: Editor.start(editor, []),\n focus: cursorPoint,\n };\n const afterRange: Range = {\n anchor: cursorPoint,\n focus: Editor.end(editor, []),\n };\n\n // Extract text for these ranges\n const before = extractTextWithNewlines(editor, beforeRange);\n const after = extractTextWithNewlines(editor, afterRange);\n\n return {\n cursorPoint: cursorPoint,\n textBeforeCursor: before,\n textAfterCursor: after,\n };\n}\n\nexport function getTextAroundSelection(editor: Editor): EditorTextState | null {\n const { selection } = editor;\n if (!selection) {\n return null;\n }\n\n const wellOrderedSelection = wellOrderedRange(selection);\n\n // Create two ranges: one before the anchor and one after\n const beforeRange: Range = {\n anchor: Editor.start(editor, []),\n focus: wellOrderedSelection.anchor,\n };\n const afterRange: Range = {\n anchor: wellOrderedSelection.focus,\n focus: Editor.end(editor, []),\n };\n\n // Extract text for these ranges\n const before = extractTextWithNewlines(editor, beforeRange);\n const after = extractTextWithNewlines(editor, afterRange);\n const selectedText = extractTextWithNewlines(editor, wellOrderedSelection);\n\n return {\n selection: wellOrderedSelection,\n textBeforeCursor: before,\n selectedText,\n textAfterCursor: after,\n };\n}\n\nexport function getFullEditorTextWithNewlines(editor: Editor): string {\n const fullDocumentRange: Range = {\n anchor: Editor.start(editor, []),\n focus: Editor.end(editor, []),\n };\n return extractTextWithNewlines(editor, fullDocumentRange);\n}\n\n// Helper function to extract text with newlines\nexport function extractTextWithNewlines(editor: Editor, range: Range): string {\n const voids = false;\n const [start, end] = Range.edges(range);\n let text = \"\";\n let lastBlock: Node | null = null;\n\n for (const [node, path] of Editor.nodes(editor, {\n at: range,\n match: Text.isText,\n voids,\n })) {\n let t = node.text;\n\n // Determine the parent block of the current text node\n const [block] = Editor.above(editor, {\n at: path,\n match: (n) => Element.isElement(n) && n.type === \"paragraph\",\n }) || [null];\n\n // If we encounter a new block, prepend a newline\n if (lastBlock !== block && block) {\n // check that lastBlock is not null to avoid adding a newline at the beginning\n if (lastBlock) {\n text += \"\\n\";\n }\n lastBlock = block;\n }\n\n if (Path.equals(path, end.path)) {\n t = t.slice(0, end.offset);\n }\n\n if (Path.equals(path, start.path)) {\n t = t.slice(start.offset);\n }\n\n text += t;\n }\n\n return text;\n}\n\nfunction wellOrderedRange(range: BaseRange): BaseRange {\n const { anchor, focus } = range;\n // if anchor is before focus, return range as is\n if (Point.isBefore(anchor, focus)) {\n return range;\n }\n\n // if focus is before anchor, return range with anchor and focus swapped\n return {\n anchor: focus,\n focus: anchor,\n };\n}\n","import { Editor, Transforms } from \"slate\";\n\nexport function replaceEditorText(editor: Editor, newText: string) {\n // clear all previous text\n Transforms.delete(editor, {\n at: {\n anchor: Editor.start(editor, []),\n focus: Editor.end(editor, []),\n },\n });\n\n // insert new text\n if (newText && newText !== \"\") {\n // don't insert empty text - results in strange visual behavior\n Transforms.insertNodes(\n editor,\n [\n {\n type: \"paragraph\",\n children: [{ text: newText }],\n },\n ],\n {\n at: [0],\n },\n );\n }\n}\n"],"mappings":";AAAA,OAAO,WAAW;AAElB,SAAS,mBAAmB;;;ACF5B,SAAS,QAAc,MAAM,OAAO,MAAM,SAA+B,aAAa;AAuE/E,SAAS,8BAA8B,QAAwB;AACpE,QAAM,oBAA2B;AAAA,IAC/B,QAAQ,OAAO,MAAM,QAAQ,CAAC,CAAC;AAAA,IAC/B,OAAO,OAAO,IAAI,QAAQ,CAAC,CAAC;AAAA,EAC9B;AACA,SAAO,wBAAwB,QAAQ,iBAAiB;AAC1D;AAGO,SAAS,wBAAwB,QAAgB,OAAsB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;AAAA,MACF;AAEA,YAAM,cAAc;AAAA,QAClB,YAAoBC,SAAsB;AAAtB,wBAAAA;AAAA,QAAuB;AAAA,QAE3C,QAAQ;AACN,sBAAY,MAAM,KAAK,MAAM;AAAA,QAC/B;AAAA,QAEA,OAAO;AACL,sBAAY,KAAK,KAAK,MAAM;AAAA,QAC9B;AAAA,QAEA,IAAI,QAAQ;AACV,iBAAO,8BAA8B,KAAK,MAAM;AAAA,QAClD;AAAA,QACA,IAAI,MAAM,OAAe;AACvB,4BAAkB,KAAK,QAAQ,KAAK;AAAA,QACtC;AAAA,MACF;AAEA,YAAM,oBAAoB,YAAY,UAAU,QAAQ,MAAM;AAC9D,YAAM,gBAAgB,IAAI,cAAc,MAAM;AAE9C,YAAM,WAAW,IAAI,SAAS,eAAe,iBAAiB;AAC9D,aAAO,IAAI,MAAM,UAAU,OAAO;AAAA,IACpC;AAAA,IACA,CAAC,MAAM;AAAA,EACT;AACF;","names":["Editor","customMethods","editorHtmlElement","editor"]}
1
+ {"version":3,"sources":[],"sourcesContent":[],"mappings":"","names":[]}
@@ -1 +1,2 @@
1
+ import "../chunk-H4VKQGVU.mjs";
1
2
  //# sourceMappingURL=index.mjs.map
@@ -1,114 +1,8 @@
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 __async = (__this, __arguments, generator) => {
21
- return new Promise((resolve, reject) => {
22
- var fulfilled = (value) => {
23
- try {
24
- step(generator.next(value));
25
- } catch (e) {
26
- reject(e);
27
- }
28
- };
29
- var rejected = (value) => {
30
- try {
31
- step(generator.throw(value));
32
- } catch (e) {
33
- reject(e);
34
- }
35
- };
36
- var step = (x) => x.done ? resolve(x.value) : Promise.resolve(x.value).then(fulfilled, rejected);
37
- step((generator = generator.apply(__this, __arguments)).next());
38
- });
39
- };
40
-
41
- // src/hooks/make-autosuggestions-function/use-make-standard-autosuggestions-function.tsx
42
- import { CopilotContext } from "@copilotkit/react-core";
43
- import { useCallback, useContext } from "react";
44
-
45
- // src/lib/retry.tsx
46
- function retry(fn, retriesLeft = 2, interval = 200, backoff = 1.5) {
47
- return new Promise((resolve, reject) => {
48
- fn().then(resolve).catch((error) => {
49
- if (retriesLeft === 1) {
50
- reject(error);
51
- return;
52
- }
53
- setTimeout(() => {
54
- retry(fn, retriesLeft - 1, interval * backoff, backoff).then(resolve).catch(reject);
55
- }, interval);
56
- });
57
- });
58
- }
59
-
60
- // src/hooks/make-autosuggestions-function/use-make-standard-autosuggestions-function.tsx
61
- import { fetchAndDecodeChatCompletionAsText } from "@copilotkit/react-core";
62
- function useMakeStandardAutosuggestionFunction(textareaPurpose, contextCategories, apiConfig) {
63
- const { getContextString, copilotApiConfig } = useContext(CopilotContext);
64
- return useCallback(
65
- (editorState, abortSignal) => __async(this, null, function* () {
66
- const res = yield retry(() => __async(this, null, function* () {
67
- const messages = [
68
- {
69
- role: "system",
70
- content: apiConfig.makeSystemPrompt(
71
- textareaPurpose,
72
- getContextString([], contextCategories)
73
- )
74
- },
75
- ...apiConfig.fewShotMessages,
76
- {
77
- role: "user",
78
- name: "TextAfterCursor",
79
- content: editorState.textAfterCursor
80
- },
81
- {
82
- role: "user",
83
- name: "TextBeforeCursor",
84
- content: editorState.textBeforeCursor
85
- }
86
- ];
87
- const response = yield fetchAndDecodeChatCompletionAsText(__spreadProps(__spreadValues({
88
- messages
89
- }, apiConfig.forwardedParams), {
90
- copilotConfig: copilotApiConfig,
91
- signal: abortSignal
92
- }));
93
- if (!response.events) {
94
- throw new Error("Failed to fetch chat completion");
95
- }
96
- const reader = response.events.getReader();
97
- let result = "";
98
- while (!abortSignal.aborted) {
99
- const { done, value } = yield reader.read();
100
- if (done) {
101
- break;
102
- }
103
- result += value;
104
- }
105
- return result;
106
- }));
107
- return res;
108
- }),
109
- [apiConfig, getContextString, contextCategories, textareaPurpose]
110
- );
111
- }
1
+ import {
2
+ useMakeStandardAutosuggestionFunction
3
+ } from "../../chunk-RT4UTBH3.mjs";
4
+ import "../../chunk-KNQIEOFP.mjs";
5
+ import "../../chunk-MRXNTQOX.mjs";
112
6
  export {
113
7
  useMakeStandardAutosuggestionFunction
114
8
  };
@@ -1 +1 @@
1
- {"version":3,"sources":["../../../src/hooks/make-autosuggestions-function/use-make-standard-autosuggestions-function.tsx","../../../src/lib/retry.tsx"],"sourcesContent":["import { Message } from \"@copilotkit/shared\";\nimport { CopilotContext } from \"@copilotkit/react-core\";\nimport { useCallback, useContext } from \"react\";\nimport { AutosuggestionsBareFunction, MinimalChatGPTMessage } from \"../../types\";\nimport { retry } from \"../../lib/retry\";\nimport { InsertionEditorState } from \"../../types/base/autosuggestions-bare-function\";\nimport { SuggestionsApiConfig } from \"../../types/autosuggestions-config/suggestions-api-config\";\nimport { fetchAndDecodeChatCompletionAsText } from \"@copilotkit/react-core\";\n\n/**\n * Returns a memoized function that sends a request to the specified API endpoint to get an autosuggestion for the user's input.\n * The function takes in the text before and after the cursor, and an abort signal.\n * It sends a POST request to the API endpoint with the messages array containing the system message, few shot messages, and user messages.\n * The function returns the suggestion from the API response.\n *\n * @param textareaPurpose - The purpose of the textarea. This is included in the system message.\n * @param apiEndpoint - The API endpoint to send the autosuggestion request to.\n * @param makeSystemMessage - A function that takes in a context string and returns a system message to include in the autosuggestion request.\n * @param fewShotMessages - An array of few shot messages to include in the autosuggestion request.\n * @param contextCategories - The categories of context strings we want to include. By default, we include the (default) \"global\" context category.\n * @returns A memoized function that sends a request to the specified API endpoint to get an autosuggestion for the user's input.\n */\nexport function useMakeStandardAutosuggestionFunction(\n textareaPurpose: string,\n contextCategories: string[],\n apiConfig: SuggestionsApiConfig,\n): AutosuggestionsBareFunction {\n const { getContextString, copilotApiConfig } = useContext(CopilotContext);\n\n return useCallback(\n async (editorState: InsertionEditorState, abortSignal: AbortSignal) => {\n const res = await retry(async () => {\n const messages: MinimalChatGPTMessage[] = [\n {\n role: \"system\",\n content: apiConfig.makeSystemPrompt(\n textareaPurpose,\n getContextString([], contextCategories),\n ),\n },\n ...apiConfig.fewShotMessages,\n {\n role: \"user\",\n name: \"TextAfterCursor\",\n content: editorState.textAfterCursor,\n },\n {\n role: \"user\",\n name: \"TextBeforeCursor\",\n content: editorState.textBeforeCursor,\n },\n ];\n\n const response = await fetchAndDecodeChatCompletionAsText({\n messages: messages as Message[],\n ...apiConfig.forwardedParams,\n copilotConfig: copilotApiConfig,\n signal: abortSignal,\n });\n\n if (!response.events) {\n throw new Error(\"Failed to fetch chat completion\");\n }\n\n const reader = response.events.getReader();\n\n let result = \"\";\n while (!abortSignal.aborted) {\n const { done, value } = await reader.read();\n if (done) {\n break;\n }\n result += value;\n }\n return result;\n });\n\n return res;\n },\n [apiConfig, getContextString, contextCategories, textareaPurpose],\n );\n}\n","export function retry<T>(\n fn: () => Promise<T>,\n retriesLeft: number = 2,\n interval: number = 200,\n backoff: number = 1.5,\n): Promise<T> {\n return new Promise((resolve, reject) => {\n fn()\n .then(resolve)\n .catch((error) => {\n if (retriesLeft === 1) {\n reject(error);\n return;\n }\n\n setTimeout(() => {\n retry(fn, retriesLeft - 1, interval * backoff, backoff)\n .then(resolve)\n .catch(reject);\n }, interval);\n });\n });\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;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AEzC,YAAI,SAAS;AACb,eAAO,CAAC,YAAY,SAAS;AAC3B,gBAAM,EAAE,MAAM,MAAM,IAAI,MAAM,OAAO,KAAK;AAC1C,cAAI,MAAM;AACR;AAAA,UACF;AACA,oBAAU;AAAA,QACZ;AACA,eAAO;AAAA,MACT,EAAC;AAED,aAAO;AAAA,IACT;AAAA,IACA,CAAC,WAAW,kBAAkB,mBAAmB,eAAe;AAAA,EAClE;AACF;","names":[]}
1
+ {"version":3,"sources":[],"sourcesContent":[],"mappings":"","names":[]}
@@ -1,163 +1,8 @@
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 __async = (__this, __arguments, generator) => {
21
- return new Promise((resolve, reject) => {
22
- var fulfilled = (value) => {
23
- try {
24
- step(generator.next(value));
25
- } catch (e) {
26
- reject(e);
27
- }
28
- };
29
- var rejected = (value) => {
30
- try {
31
- step(generator.throw(value));
32
- } catch (e) {
33
- reject(e);
34
- }
35
- };
36
- var step = (x) => x.done ? resolve(x.value) : Promise.resolve(x.value).then(fulfilled, rejected);
37
- step((generator = generator.apply(__this, __arguments)).next());
38
- });
39
- };
40
-
41
- // src/hooks/make-autosuggestions-function/use-make-standard-insertion-function.tsx
42
- import { CopilotContext } from "@copilotkit/react-core";
43
- import { useCallback, useContext } from "react";
44
-
45
- // src/lib/retry.tsx
46
- function retry(fn, retriesLeft = 2, interval = 200, backoff = 1.5) {
47
- return new Promise((resolve, reject) => {
48
- fn().then(resolve).catch((error) => {
49
- if (retriesLeft === 1) {
50
- reject(error);
51
- return;
52
- }
53
- setTimeout(() => {
54
- retry(fn, retriesLeft - 1, interval * backoff, backoff).then(resolve).catch(reject);
55
- }, interval);
56
- });
57
- });
58
- }
59
-
60
- // src/hooks/make-autosuggestions-function/use-make-standard-insertion-function.tsx
61
- import { fetchAndDecodeChatCompletionAsText } from "@copilotkit/react-core";
62
- function useMakeStandardInsertionOrEditingFunction(textareaPurpose, contextCategories, insertionApiConfig, editingApiConfig) {
63
- const { getContextString, copilotApiConfig } = useContext(CopilotContext);
64
- const insertionFunction = useCallback(
65
- (editorState, insertionPrompt, documents, abortSignal) => __async(this, null, function* () {
66
- const res = yield retry(() => __async(this, null, function* () {
67
- const messages = [
68
- {
69
- role: "system",
70
- content: insertionApiConfig.makeSystemPrompt(
71
- textareaPurpose,
72
- getContextString(documents, contextCategories)
73
- )
74
- },
75
- ...insertionApiConfig.fewShotMessages,
76
- {
77
- role: "user",
78
- name: "TextAfterCursor",
79
- content: editorState.textAfterCursor
80
- },
81
- {
82
- role: "user",
83
- name: "TextBeforeCursor",
84
- content: editorState.textBeforeCursor
85
- },
86
- {
87
- role: "user",
88
- name: "InsertionPrompt",
89
- content: insertionPrompt
90
- }
91
- ];
92
- const stream = yield fetchAndDecodeChatCompletionAsText(__spreadProps(__spreadValues({
93
- messages
94
- }, insertionApiConfig.forwardedParams), {
95
- copilotConfig: copilotApiConfig,
96
- signal: abortSignal
97
- }));
98
- return stream.events;
99
- }));
100
- return res;
101
- }),
102
- [insertionApiConfig, getContextString, contextCategories, textareaPurpose]
103
- );
104
- const editingFunction = useCallback(
105
- (editorState, editingPrompt, documents, abortSignal) => __async(this, null, function* () {
106
- const res = yield retry(() => __async(this, null, function* () {
107
- const messages = [
108
- {
109
- role: "system",
110
- content: editingApiConfig.makeSystemPrompt(
111
- textareaPurpose,
112
- getContextString(documents, contextCategories)
113
- )
114
- },
115
- ...editingApiConfig.fewShotMessages,
116
- {
117
- role: "user",
118
- name: "TextBeforeCursor",
119
- content: editorState.textBeforeCursor
120
- },
121
- {
122
- role: "user",
123
- name: "TextToEdit",
124
- content: editorState.selectedText
125
- },
126
- {
127
- role: "user",
128
- name: "TextAfterCursor",
129
- content: editorState.textAfterCursor
130
- },
131
- {
132
- role: "user",
133
- name: "EditingPrompt",
134
- content: editingPrompt
135
- }
136
- ];
137
- const stream = yield fetchAndDecodeChatCompletionAsText(__spreadProps(__spreadValues({
138
- messages
139
- }, editingApiConfig.forwardedParams), {
140
- copilotConfig: copilotApiConfig,
141
- signal: abortSignal
142
- }));
143
- return stream.events;
144
- }));
145
- return res;
146
- }),
147
- [editingApiConfig, getContextString, contextCategories, textareaPurpose]
148
- );
149
- const insertionOrEditingFunction = useCallback(
150
- (editorState, insertionPrompt, documents, abortSignal) => __async(this, null, function* () {
151
- if (editorState.selectedText === "") {
152
- return yield insertionFunction(editorState, insertionPrompt, documents, abortSignal);
153
- } else {
154
- return yield editingFunction(editorState, insertionPrompt, documents, abortSignal);
155
- }
156
- }),
157
- [insertionFunction, editingFunction]
158
- );
159
- return insertionOrEditingFunction;
160
- }
1
+ import {
2
+ useMakeStandardInsertionOrEditingFunction
3
+ } from "../../chunk-QCPS6IYI.mjs";
4
+ import "../../chunk-KNQIEOFP.mjs";
5
+ import "../../chunk-MRXNTQOX.mjs";
161
6
  export {
162
7
  useMakeStandardInsertionOrEditingFunction
163
8
  };
@@ -1 +1 @@
1
- {"version":3,"sources":["../../../src/hooks/make-autosuggestions-function/use-make-standard-insertion-function.tsx","../../../src/lib/retry.tsx"],"sourcesContent":["import { Message } from \"@copilotkit/shared\";\nimport { CopilotContext } from \"@copilotkit/react-core\";\nimport { useCallback, useContext } from \"react\";\nimport { MinimalChatGPTMessage } from \"../../types\";\nimport { retry } from \"../../lib/retry\";\nimport {\n EditingEditorState,\n Generator_InsertionOrEditingSuggestion,\n InsertionEditorApiConfig,\n InsertionEditorState,\n} from \"../../types/base/autosuggestions-bare-function\";\nimport { InsertionsApiConfig } from \"../../types/autosuggestions-config/insertions-api-config\";\nimport { EditingApiConfig } from \"../../types/autosuggestions-config/editing-api-config\";\nimport { DocumentPointer } from \"@copilotkit/react-core\";\nimport { fetchAndDecodeChatCompletionAsText } from \"@copilotkit/react-core\";\n\n/**\n * Returns a memoized function that sends a request to the specified API endpoint to get an autosuggestion for the user's input.\n * The function takes in the text before and after the cursor, and an abort signal.\n * It sends a POST request to the API endpoint with the messages array containing the system message, few shot messages, and user messages.\n * The function returns the suggestion from the API response.\n *\n * @param textareaPurpose - The purpose of the textarea. This is included in the system message.\n * @param apiEndpoint - The API endpoint to send the autosuggestion request to.\n * @param makeSystemMessage - A function that takes in a context string and returns a system message to include in the autosuggestion request.\n * @param fewShotMessages - An array of few shot messages to include in the autosuggestion request.\n * @param contextCategories - The categories of context strings we want to include. By default, we include the (default) \"global\" context category.\n * @returns A memoized function that sends a request to the specified API endpoint to get an autosuggestion for the user's input.\n */\nexport function useMakeStandardInsertionOrEditingFunction(\n textareaPurpose: string,\n contextCategories: string[],\n insertionApiConfig: InsertionsApiConfig,\n editingApiConfig: EditingApiConfig,\n): Generator_InsertionOrEditingSuggestion {\n const { getContextString, copilotApiConfig } = useContext(CopilotContext);\n\n const insertionFunction = useCallback(\n async (\n editorState: EditingEditorState,\n insertionPrompt: string,\n documents: DocumentPointer[],\n abortSignal: AbortSignal,\n ) => {\n const res = await retry(async () => {\n const messages: MinimalChatGPTMessage[] = [\n {\n role: \"system\",\n content: insertionApiConfig.makeSystemPrompt(\n textareaPurpose,\n getContextString(documents, contextCategories),\n ),\n },\n ...insertionApiConfig.fewShotMessages,\n {\n role: \"user\",\n name: \"TextAfterCursor\",\n content: editorState.textAfterCursor,\n },\n {\n role: \"user\",\n name: \"TextBeforeCursor\",\n content: editorState.textBeforeCursor,\n },\n {\n role: \"user\",\n name: \"InsertionPrompt\",\n content: insertionPrompt,\n },\n ];\n\n const stream = await fetchAndDecodeChatCompletionAsText({\n messages: messages as Message[],\n ...insertionApiConfig.forwardedParams,\n copilotConfig: copilotApiConfig,\n signal: abortSignal,\n });\n return stream.events!;\n });\n\n return res;\n },\n [insertionApiConfig, getContextString, contextCategories, textareaPurpose],\n );\n\n const editingFunction = useCallback(\n async (\n editorState: EditingEditorState,\n editingPrompt: string,\n documents: DocumentPointer[],\n abortSignal: AbortSignal,\n ) => {\n const res = await retry(async () => {\n const messages: MinimalChatGPTMessage[] = [\n {\n role: \"system\",\n content: editingApiConfig.makeSystemPrompt(\n textareaPurpose,\n getContextString(documents, contextCategories),\n ),\n },\n ...editingApiConfig.fewShotMessages,\n {\n role: \"user\",\n name: \"TextBeforeCursor\",\n content: editorState.textBeforeCursor,\n },\n {\n role: \"user\",\n name: \"TextToEdit\",\n content: editorState.selectedText,\n },\n {\n role: \"user\",\n name: \"TextAfterCursor\",\n content: editorState.textAfterCursor,\n },\n {\n role: \"user\",\n name: \"EditingPrompt\",\n content: editingPrompt,\n },\n ];\n\n const stream = await fetchAndDecodeChatCompletionAsText({\n messages: messages as Message[],\n ...editingApiConfig.forwardedParams,\n copilotConfig: copilotApiConfig,\n signal: abortSignal,\n });\n return stream.events!;\n });\n\n return res;\n },\n [editingApiConfig, getContextString, contextCategories, textareaPurpose],\n );\n\n const insertionOrEditingFunction = useCallback(\n async (\n editorState: EditingEditorState,\n insertionPrompt: string,\n documents: DocumentPointer[],\n abortSignal: AbortSignal,\n ) => {\n if (editorState.selectedText === \"\") {\n return await insertionFunction(editorState, insertionPrompt, documents, abortSignal);\n } else {\n return await editingFunction(editorState, insertionPrompt, documents, abortSignal);\n }\n },\n [insertionFunction, editingFunction],\n );\n\n return insertionOrEditingFunction;\n}\n","export function retry<T>(\n fn: () => Promise<T>,\n retriesLeft: number = 2,\n interval: number = 200,\n backoff: number = 1.5,\n): Promise<T> {\n return new Promise((resolve, reject) => {\n fn()\n .then(resolve)\n .catch((error) => {\n if (retriesLeft === 1) {\n reject(error);\n return;\n }\n\n setTimeout(() => {\n retry(fn, retriesLeft - 1, interval * backoff, backoff)\n .then(resolve)\n .catch(reject);\n }, interval);\n });\n });\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AACA,SAAS,sBAAsB;AAC/B,SAAS,aAAa,kBAAkB;;;ACFjC,SAAS,MACd,IACA,cAAsB,GACtB,WAAmB,KACnB,UAAkB,KACN;AACZ,SAAO,IAAI,QAAQ,CAAC,SAAS,WAAW;AACtC,OAAG,EACA,KAAK,OAAO,EACZ,MAAM,CAAC,UAAU;AAChB,UAAI,gBAAgB,GAAG;AACrB,eAAO,KAAK;AACZ;AAAA,MACF;AAEA,iBAAW,MAAM;AACf,cAAM,IAAI,cAAc,GAAG,WAAW,SAAS,OAAO,EACnD,KAAK,OAAO,EACZ,MAAM,MAAM;AAAA,MACjB,GAAG,QAAQ;AAAA,IACb,CAAC;AAAA,EACL,CAAC;AACH;;;ADRA,SAAS,0CAA0C;AAe5C,SAAS,0CACd,iBACA,mBACA,oBACA,kBACwC;AACxC,QAAM,EAAE,kBAAkB,iBAAiB,IAAI,WAAW,cAAc;AAExE,QAAM,oBAAoB;AAAA,IACxB,CACE,aACA,iBACA,WACA,gBACG;AACH,YAAM,MAAM,MAAM,MAAM,MAAY;AAClC,cAAM,WAAoC;AAAA,UACxC;AAAA,YACE,MAAM;AAAA,YACN,SAAS,mBAAmB;AAAA,cAC1B;AAAA,cACA,iBAAiB,WAAW,iBAAiB;AAAA,YAC/C;AAAA,UACF;AAAA,UACA,GAAG,mBAAmB;AAAA,UACtB;AAAA,YACE,MAAM;AAAA,YACN,MAAM;AAAA,YACN,SAAS,YAAY;AAAA,UACvB;AAAA,UACA;AAAA,YACE,MAAM;AAAA,YACN,M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ACA,iBACA,WACA,gBACG;AACH,UAAI,YAAY,iBAAiB,IAAI;AACnC,eAAO,MAAM,kBAAkB,aAAa,iBAAiB,WAAW,WAAW;AAAA,MACrF,OAAO;AACL,eAAO,MAAM,gBAAgB,aAAa,iBAAiB,WAAW,WAAW;AAAA,MACnF;AAAA,IACF;AAAA,IACA,CAAC,mBAAmB,eAAe;AAAA,EACrC;AAEA,SAAO;AACT;","names":[]}
1
+ {"version":3,"sources":[],"sourcesContent":[],"mappings":"","names":[]}
@@ -1,15 +1,7 @@
1
- // src/hooks/misc/use-autosize-textarea.tsx
2
- import { useEffect } from "react";
3
- var useAutosizeTextArea = (textAreaRef, value) => {
4
- useEffect(() => {
5
- if (textAreaRef.current !== null) {
6
- textAreaRef.current.style.height = "0px";
7
- const scrollHeight = textAreaRef.current.scrollHeight;
8
- textAreaRef.current.style.height = scrollHeight + "px";
9
- }
10
- }, [textAreaRef, value]);
11
- };
12
- var use_autosize_textarea_default = useAutosizeTextArea;
1
+ import {
2
+ use_autosize_textarea_default
3
+ } from "../../chunk-3PQ7GSFE.mjs";
4
+ import "../../chunk-MRXNTQOX.mjs";
13
5
  export {
14
6
  use_autosize_textarea_default as default
15
7
  };
@@ -1 +1 @@
1
- {"version":3,"sources":["../../../src/hooks/misc/use-autosize-textarea.tsx"],"sourcesContent":["import { Ref, RefObject, useEffect } from \"react\";\n\n// Updates the height of a <textarea> when the value changes.\nconst useAutosizeTextArea = (textAreaRef: RefObject<HTMLTextAreaElement>, value: string) => {\n useEffect(() => {\n if (textAreaRef.current !== null) {\n // We need to reset the height momentarily to get the correct scrollHeight for the textarea\n textAreaRef.current.style.height = \"0px\";\n const scrollHeight = textAreaRef.current.scrollHeight;\n\n // We then set the height directly, outside of the render loop\n // Trying to set this with state or a ref will product an incorrect value.\n textAreaRef.current.style.height = scrollHeight + \"px\";\n }\n }, [textAreaRef, value]);\n};\n\nexport default useAutosizeTextArea;\n"],"mappings":";AAAA,SAAyB,iBAAiB;AAG1C,IAAM,sBAAsB,CAAC,aAA6C,UAAkB;AAC1F,YAAU,MAAM;AACd,QAAI,YAAY,YAAY,MAAM;AAEhC,kBAAY,QAAQ,MAAM,SAAS;AACnC,YAAM,eAAe,YAAY,QAAQ;AAIzC,kBAAY,QAAQ,MAAM,SAAS,eAAe;AAAA,IACpD;AAAA,EACF,GAAG,CAAC,aAAa,KAAK,CAAC;AACzB;AAEA,IAAO,gCAAQ;","names":[]}
1
+ {"version":3,"sources":[],"sourcesContent":[],"mappings":"","names":[]}