Skip to content

armgit / E2B

Public Imported from GitHub
0

Open-source, secure environment with real-world tools for enterprise-grade agents.

Fix backspace reference delete bug; Stop loading references from global var

Tomas Valentae50f12fca03e
diff --git a/components/Editor/RouteEditor/PromptEditor/index.tsx b/components/Editor/RouteEditor/PromptEditor/index.tsx
index 9cd884570..db10c69a5 100644
--- a/components/Editor/RouteEditor/PromptEditor/index.tsx
+++ b/components/Editor/RouteEditor/PromptEditor/index.tsx
@@ -1,14 +1,17 @@
import { EditorContent } from '@tiptap/react'
+import Fuse from 'fuse.js'
import Text from 'components/Text'
import { Block } from 'state/store'
import useDocEditor from 'hooks/useDocEditor'
+import { Reference } from 'editor/referenceType'
export interface Props {
title?: string
placeholder?: string
onChange: (value: string) => void
block: Block
+ referenceSearch: Fuse<Reference>
}
function PromptEditor({
@@ -16,11 +19,13 @@ function PromptEditor({
onChange,
block,
placeholder,
+ referenceSearch,
}: Props) {
const editor = useDocEditor({
initialContent: block.content,
onContentChange: onChange,
placeholder,
+ referenceSearch,
})
return (
diff --git a/components/Editor/RouteEditor/index.tsx b/components/Editor/RouteEditor/index.tsx
index 00f81ee4f..492bffb5c 100644
--- a/components/Editor/RouteEditor/index.tsx
+++ b/components/Editor/RouteEditor/index.tsx
@@ -3,6 +3,7 @@ import useBlock from 'hooks/useBlock'
import PromptEditor from './PromptEditor'
import RequestBodyEditor from './RequestBodyEditor'
+import useReferences from 'hooks/useReferences'
export interface Props {
route?: Route
@@ -13,6 +14,8 @@ function RouteEditor({ route }: Props) {
const [descriptionBlock, updateDescriptionBlock] = useBlock('Description', 1, route)
const [instructionsBlock, updateInstructionsBlock] = useBlock('Instructions', 1, route)
+ const [referenceSearch] = useReferences()
+
return (
<div className="
py-8
@@ -41,6 +44,7 @@ function RouteEditor({ route }: Props) {
">
{descriptionBlock &&
<PromptEditor
+ referenceSearch={referenceSearch}
title="What should this route do?"
placeholder="This is an API endpoint that ..."
block={descriptionBlock}
@@ -55,6 +59,7 @@ function RouteEditor({ route }: Props) {
}
{instructionsBlock &&
<PromptEditor
+ referenceSearch={referenceSearch}
title="Step-by-step instructions"
placeholder="1. Check if the incoming `email` is not empty ..."
block={instructionsBlock}
diff --git a/editor/extensions/autocomplete/suggestion.ts b/editor/extensions/autocomplete/suggestion.ts
index 17c6eab0e..500b7954f 100644
--- a/editor/extensions/autocomplete/suggestion.ts
+++ b/editor/extensions/autocomplete/suggestion.ts
@@ -29,20 +29,20 @@ export interface SuggestionOptions<Item = Reference> {
}
export interface SuggestionProps<Item = Reference> {
- editor: Editor,
- range: Range,
- query: string,
- text: string,
- items: Item[],
- command: (props: Item) => void,
- decorationNode: Element | null,
- clientRect: (() => DOMRect) | null,
+ editor: Editor
+ range: Range
+ query: string
+ text: string
+ items: Item[]
+ command: (props: Item) => void
+ decorationNode: Element | null
+ clientRect: (() => DOMRect) | null
}
export interface SuggestionKeyDownProps {
- view: EditorView,
- event: KeyboardEvent,
- range: Range,
+ view: EditorView
+ event: KeyboardEvent
+ range: Range
}
interface SuggestionPluginState {
@@ -217,15 +217,10 @@ export function Suggestion<Item = Reference>({
const state = this.getState(view.state)
if (!state) return
- // const preceedingChar = view.state.doc.textBetween(from - 1, from)
-
if (text) {
state.wasCharTyped = true
}
- // if (text === char && (preceedingChar === ' ' || !preceedingChar)) {
- // }
-
return false
},
// Call the keydown hook if suggestion is active.
diff --git a/editor/schema.ts b/editor/schema.ts
index a2775c5ae..75b4cf65b 100644
--- a/editor/schema.ts
+++ b/editor/schema.ts
@@ -5,13 +5,11 @@ import BulletList from '@tiptap/extension-bullet-list'
import ListItem from '@tiptap/extension-list-item'
import OrderedList from '@tiptap/extension-ordered-list'
import CodeBlock from '@tiptap/extension-code-block'
-import Fuse from 'fuse.js'
import ReferenceExtension, {
REFERENCE_TYPE_ATTRIBUTE_NAME,
REFERENCE_VALUE_ATTRIBUTE_NAME,
} from 'editor/extensions/reference'
-import AutocompleteExtension from 'editor/extensions/autocomplete'
import CustomKeymap from 'editor/extensions/keymap'
export enum ReferenceType {
@@ -24,19 +22,6 @@ export interface Reference {
value: string
}
-export const referenceItems: Reference[] = [
- {
- type: ReferenceType.NPMPackage,
- value: '@slack/web-api',
- },
- {
- type: ReferenceType.DEPLOYMENT,
- value: 'AWS Lambda',
- },
-]
-
-const searchEngine = new Fuse(referenceItems, { keys: ['value'], threshold: 0.3 })
-
export const extensions = [
StarterKit.configure({
blockquote: false,
@@ -61,11 +46,6 @@ export const extensions = [
ListItem,
BulletList,
ReferenceExtension,
- AutocompleteExtension.configure({
- suggestion: {
- items: (query) => searchEngine.search(query).map(q => q.item),
- },
- }),
CustomKeymap,
]
diff --git a/hooks/useDocEditor.ts b/hooks/useDocEditor.ts
index b7428093a..214cd9af6 100644
--- a/hooks/useDocEditor.ts
+++ b/hooks/useDocEditor.ts
@@ -1,13 +1,18 @@
import { useEditor } from '@tiptap/react'
import Placeholder from '@tiptap/extension-placeholder'
+import Fuse from 'fuse.js'
+import { Reference } from 'editor/referenceType'
+import AutocompleteExtension from 'editor/extensions/autocomplete'
import { extensions } from 'editor/schema'
function useDocEditor({
initialContent,
onContentChange,
placeholder,
+ referenceSearch,
}: {
+ referenceSearch: Fuse<Reference>
initialContent: string,
onContentChange: (content: string) => void,
placeholder?: string
@@ -17,7 +22,6 @@ function useDocEditor({
parseOptions: {
preserveWhitespace: 'full',
},
- injectCSS: false,
editorProps: {
attributes: {
'data-gramm': 'false',
@@ -26,6 +30,11 @@ function useDocEditor({
},
extensions: [
...extensions,
+ AutocompleteExtension.configure({
+ suggestion: {
+ items: query => referenceSearch.search(query).map(r => r.item),
+ },
+ }),
Placeholder.configure({
placeholder: ({ editor }) => {
if (!editor.getText()) {
@@ -35,7 +44,6 @@ function useDocEditor({
}
}),
],
-
onUpdate({ transaction, editor }) {
if (transaction.docChanged) {
onContentChange(editor.getHTML())
diff --git a/hooks/useReferences.ts b/hooks/useReferences.ts
new file mode 100644
index 000000000..a8ebac280
--- /dev/null
+++ b/hooks/useReferences.ts
@@ -0,0 +1,25 @@
+import {
+ useMemo,
+} from 'react'
+import Fuse from 'fuse.js'
+
+import { ReferenceType, Reference } from 'editor/referenceType'
+
+function useReferences(): [Fuse<Reference>, Reference[]] {
+ const references = useMemo<Reference[]>(() => [
+ {
+ type: ReferenceType.NPMPackage,
+ value: '@slack/web-api',
+ },
+ {
+ type: ReferenceType.DEPLOYMENT,
+ value: 'AWS Lambda',
+ },
+ ], [])
+
+ const engine = useMemo(() => new Fuse(references, { keys: ['value'], threshold: 0.3 }), [references])
+
+ return [engine, references]
+}
+
+export default useReferences