Open-source, secure environment with real-world tools for enterprise-grade agents.
Python agentaiai-agentai-agentscode-interpretercopilotdevelopmentdevtoolsgptgpt-4javascriptllmnextjsopenaipythonreactsoftwaretypescript
Fix autocomplete positioning
Tomas Valentaae2542f01084
diff --git a/components/Editor/RouteEditor/PromptEditor/Autocomplete/Item.tsx b/components/Editor/RouteEditor/PromptEditor/Autocomplete/Item.tsxindex fdb7f3636..13513f7f9 100644--- a/components/Editor/RouteEditor/PromptEditor/Autocomplete/Item.tsx+++ b/components/Editor/RouteEditor/PromptEditor/Autocomplete/Item.tsx@@ -42,7 +42,6 @@ function Item({<divref={setScrollRef}className={cn(- 'py-0.5','px-2','py-1','space-x-2',diff --git a/editor/extensions/autocomplete/findSuggestionMatch.ts b/editor/extensions/autocomplete/findSuggestionMatch.tsindex 3f22f94e4..d907f1a92 100644--- a/editor/extensions/autocomplete/findSuggestionMatch.ts+++ b/editor/extensions/autocomplete/findSuggestionMatch.ts@@ -1,56 +1,68 @@-import { escapeForRegEx, Range } from '@tiptap/core'-import { ResolvedPos } from '@tiptap/pm/model'+import { Range } from '@tiptap/core'+import { ResolvedPos } from 'prosemirror-model'export interface Trigger {char: stringallowSpaces: boolean- allowedPrefixes: string[] | nullstartOfLine: boolean$position: ResolvedPos}export type SuggestionMatch = {- range: Range- query: string- text: string+ range: Range,+ query: string,+ text: string,} | nullexport function findSuggestionMatch(config: Trigger): SuggestionMatch {const {- char, allowSpaces, allowedPrefixes, startOfLine, $position,+ char,+ allowSpaces,+ startOfLine,+ $position,} = config- const escapedChar = escapeForRegEx(char)+ const textAfterAnchor = $position.nodeAfter?.textContent || ''+ const whitespaceAfterAnchorIndex = textAfterAnchor.indexOf(' ')+ const textAfterAnchorUntilWhitespace = whitespaceAfterAnchorIndex < 0+ ? textAfterAnchor+ : textAfterAnchor.slice(0, whitespaceAfterAnchorIndex + 1)++ // Matching expressions used for later+ const escapedChar = char+ .split('')+ .map(c => `\\${c}`)+ .join('')+const suffix = new RegExp(`\\s${escapedChar}$`)const prefix = startOfLine ? '^' : ''const regexp = allowSpaces? new RegExp(`${prefix}${escapedChar}.*?(?=\\s${escapedChar}|$)`, 'gm'): new RegExp(`${prefix}(?:^)?${escapedChar}[^\\s${escapedChar}]*`, 'gm')- const text = $position.nodeBefore?.isText && $position.nodeBefore.text-- if (!text) {- return null- }+ const isTopLevelNode = $position.depth <= 0+ const textFrom = isTopLevelNode+ ? 0+ : $position.before()- const textFrom = $position.pos - text.length+ const textTo = $position.pos+ const text = $position.doc.textBetween(textFrom, textTo, '\0', '\0') + textAfterAnchorUntilWhitespaceconst match = Array.from(text.matchAll(regexp)).pop()if (!match || match.input === undefined || match.index === undefined) {return null}- // JavaScript doesn't have lookbehinds. This hacks a check that first character- // is a space or the start of the line+ // JavaScript doesn't have lookbehinds; this hacks a check that first character is " "+ // or the line beginningconst matchPrefix = match.input.slice(Math.max(0, match.index - 1), match.index)- const matchPrefixIsAllowed = new RegExp(`^[${allowedPrefixes?.join('')}\0]?$`).test(matchPrefix)- if (allowedPrefixes !== null && !matchPrefixIsAllowed) {+ if (!/^[\s\0]?$/.test(matchPrefix)) {return null}// The absolute position of the match in the document- const from = textFrom + match.index+ const from = match.index + $position.start()let to = from + match[0].length// Edge case handling; if spaces are allowed and we're directly in betweendiff --git a/editor/extensions/autocomplete/index.ts b/editor/extensions/autocomplete/index.tsindex 44da1fc22..d298af52d 100644--- a/editor/extensions/autocomplete/index.ts+++ b/editor/extensions/autocomplete/index.ts@@ -7,17 +7,35 @@ import tippy, {Instance,Props,} from 'tippy.js'-import { ComponentClass } from 'react'import { destroyOnEsc } from 'editor/tippyPlugins'import AutocompleteListWrapper, { AutocompleteList } from 'components/Editor/RouteEditor/PromptEditor/Autocomplete/ListWrapper'-import { Suggestion, SuggestionOptions } from './suggestion'+import { Suggestion, SuggestionOptions, SuggestionProps } from './suggestion'import Autocomplete from 'components/Editor/RouteEditor/PromptEditor/Autocomplete'import reference from '../reference'export * from './suggestion'+/**+ * Create bounding rect that starts at the caret position.+ */+function getReferenceClientRect(props: SuggestionProps): () => DOMRect {+ return () => {+ const selection = props.editor.view.state.selection+ const anchor = props.editor.view.domAtPos(selection.anchor)+ const coords = props.editor.view.coordsAtPos(selection.anchor)++ const range = document.createRange()+ range.selectNodeContents(anchor.node)+ const rect = range.getBoundingClientRect()++ rect.x = coords.left++ return rect+ }+}+export type AutocompleteOptions = {list?: AutocompleteListsuggestion: Omit<SuggestionOptions, 'editor'>@@ -39,7 +57,7 @@ export default Node.create<AutocompleteOptions>({return {onStart: props => {disabled = false- reactRenderer = new ReactRenderer(AutocompleteListWrapper as unknown as ComponentClass, {+ reactRenderer = new ReactRenderer(AutocompleteListWrapper, {editor: props.editor as ReactEditor,props: {...props,@@ -50,14 +68,16 @@ export default Node.create<AutocompleteOptions>({const editorElement = props.editor.options.elementpopup = tippy(editorElement, {- getReferenceClientRect: props.clientRect,+ getReferenceClientRect: getReferenceClientRect(props),appendTo: () => document.body,content: reactRenderer.element,showOnCreate: true,interactive: true,hideOnClick: true,+ duration: 1,maxWidth: 'none',trigger: 'manual',+ offset: [1, 2],animation: false,placement: 'bottom-start',onHide() {@@ -76,7 +96,7 @@ export default Node.create<AutocompleteOptions>({reactRenderer.updateProps(props)popup?.setProps({- getReferenceClientRect: props.clientRect,+ getReferenceClientRect: getReferenceClientRect(props),})},onKeyDown(props) {diff --git a/editor/extensions/autocomplete/suggestion.ts b/editor/extensions/autocomplete/suggestion.tsindex 3a9e91da9..c22fa03b0 100644--- a/editor/extensions/autocomplete/suggestion.ts+++ b/editor/extensions/autocomplete/suggestion.ts@@ -187,8 +187,7 @@ export function Suggestion<Item = Reference>({const match = findSuggestionMatch({char,allowSpaces,- allowedPrefixes: [],- startOfLine,+ startOfLine: false,$position: selection.$from,})const decorationID = `id_${Math.floor(Math.random() * 0xFFFFFFFF)}`