Skip to content

armgit / E2B

Public Imported from GitHub
0

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

Fix attributes bug; Add MD serialization and context extraction; Improve inline context style; Fix context insertion and autocomplete [WIP]

Tomas Valentae01d29639438
diff --git a/components/Editor/Sidebar/index.tsx b/components/Editor/Sidebar/index.tsx
index abc9fc2e0..9acc56a5d 100644
--- a/components/Editor/Sidebar/index.tsx
+++ b/components/Editor/Sidebar/index.tsx
@@ -40,9 +40,10 @@ async function handlePostGenerate(url: string, { arg }: {
switch (b.type) {
case 'Description':
case 'Instructions':
+ const [markdown, context] = html2markdown(b.content)
const block: Block = {
...b,
- content: html2markdown(b.content),
+ content: markdown,
}
return block
default:
diff --git a/components/Editor/extensions/ContextAutocomplete/Item.tsx b/components/Editor/extensions/ContextAutocomplete/Item.tsx
index 16e03416a..709cdcfab 100644
--- a/components/Editor/extensions/ContextAutocomplete/Item.tsx
+++ b/components/Editor/extensions/ContextAutocomplete/Item.tsx
@@ -33,7 +33,7 @@ function Item({
'py-1',
'shrink-0',
'flex',
- 'font-sans',
+ 'font-mono',
'items-center',
'group',
'overflow-ellipsis',
diff --git a/components/Editor/extensions/ContextAutocomplete/index.tsx b/components/Editor/extensions/ContextAutocomplete/index.tsx
index 4db8f1f8c..ed6b3f91a 100644
--- a/components/Editor/extensions/ContextAutocomplete/index.tsx
+++ b/components/Editor/extensions/ContextAutocomplete/index.tsx
@@ -42,6 +42,7 @@ const ContextAutocomplete: CommandList = forwardRef(({
.extendCommand(cmd)
.setTextSelection(0)
.run()
+
}, [
range,
editor,
@@ -83,7 +84,7 @@ const ContextAutocomplete: CommandList = forwardRef(({
rounded
border
- border-indigo-400
+ border-indigo-300
bg-white
"
diff --git a/editor/extensions/command/findSuggestionMatch.ts b/editor/extensions/command/findSuggestionMatch.ts
index 021622320..f07340e78 100644
--- a/editor/extensions/command/findSuggestionMatch.ts
+++ b/editor/extensions/command/findSuggestionMatch.ts
@@ -16,7 +16,7 @@ export type SuggestionMatch = {
export function findSuggestionMatch(config: Trigger): SuggestionMatch {
const {
- char = '',
+ char = '/',
allowSpaces,
startOfLine,
$position,
@@ -33,6 +33,7 @@ export function findSuggestionMatch(config: Trigger): SuggestionMatch {
.split('')
.map(c => `\\${c}`)
.join('')
+
const suffix = new RegExp(`\\s${escapedChar}$`)
const prefix = startOfLine ? '^' : ''
const regexp = allowSpaces
@@ -43,6 +44,7 @@ export function findSuggestionMatch(config: Trigger): SuggestionMatch {
const textFrom = isTopLevelNode
? 0
: $position.before()
+
const textTo = $position.pos
const text = $position.doc.textBetween(textFrom, textTo, '\0', '\0') + textAfterAnchorUntilWhitespace
const match = Array.from(text.matchAll(regexp)).pop()
diff --git a/editor/extensions/command/index.ts b/editor/extensions/command/index.ts
index b43cd08eb..d0ebacae1 100644
--- a/editor/extensions/command/index.ts
+++ b/editor/extensions/command/index.ts
@@ -10,9 +10,9 @@ import tippy, {
import { ComponentClass } from 'react'
import { destroyOnEsc } from 'editor/tippyPlugins'
+import CommandListWrapper, { CommandList } from 'components/Editor/extensions/command/CommandListWrapper'
import { Suggestion, SuggestionOptions } from './suggestion'
-import CommandListWrapper, { CommandList } from 'components/Editor/extensions/command/CommandListWrapper'
export * from './findSuggestionMatch'
export * from './suggestion'
diff --git a/editor/extensions/command/suggestion.ts b/editor/extensions/command/suggestion.ts
index 73f19db12..36a1b513c 100644
--- a/editor/extensions/command/suggestion.ts
+++ b/editor/extensions/command/suggestion.ts
@@ -154,6 +154,8 @@ export function Suggestion<Item = unknown>({
const { selection } = transaction
const next = { ...prev }
+ console.log('apply--', prev.wasCharTyped)
+
// We can only be suggesting if there is no selection
if (!prev.wasCharTyped) {
next.active = false
@@ -204,12 +206,16 @@ export function Suggestion<Item = unknown>({
const state = this.getState(view.state)
if (!state) return
- const preceedingChar = view.state.doc.textBetween(from - 1, from)
+ // const preceedingChar = view.state.doc.textBetween(from - 1, from)
- if (text === char && (preceedingChar === ' ' || !preceedingChar)) {
+ if (text) {
state.wasCharTyped = true
+ console.log('char yes')
}
+ // if (text === char && (preceedingChar === ' ' || !preceedingChar)) {
+ // }
+
return false
},
// Call the keydown hook if suggestion is active.
diff --git a/editor/extensions/promptContext.ts b/editor/extensions/promptContext.ts
index fe6e06072..57728c0b5 100644
--- a/editor/extensions/promptContext.ts
+++ b/editor/extensions/promptContext.ts
@@ -1,6 +1,7 @@
import { mergeAttributes, Node } from '@tiptap/core'
import { Node as ProseMirrorNode } from '@tiptap/pm/model'
import { PluginKey } from '@tiptap/pm/state'
+
import attributeHandler from 'utils/attributeHandler'
import { Suggestion, SuggestionOptions } from './command'
@@ -23,6 +24,9 @@ declare module '@tiptap/core' {
}
}
+export const CONTEXT_VALUE_ATTRIBUTE_NAME = 'context-value'
+export const CONTEXT_TYPE_ATTRIBUTE_NAME = 'context-type'
+
export const promptContextItems: PromptContext[] = [
{
type: PromptContextType.NPMPackage,
@@ -47,13 +51,16 @@ export default Node.create<MentionOptions>({
addOptions() {
return {
- HTMLAttributes: {},
+ HTMLAttributes: {
+ class: 'prompt-context',
+ },
renderLabel({ options, node }) {
- return `${node.attrs.value}`
+ return `${node.attrs[CONTEXT_VALUE_ATTRIBUTE_NAME]}`
},
suggestion: {
pluginKey: PromptContextPluginKey,
command: ({ editor, range, props }) => {
+ console.log('Command >>>')
// increase range.to by one when the next node is of type "text"
// and starts with a space character
const nodeAfter = editor.view.state.selection.$to.nodeAfter
@@ -98,8 +105,8 @@ export default Node.create<MentionOptions>({
addAttributes() {
return {
- ...attributeHandler({ htmlPrefix: 'value', nodeAttribute: 'value' }),
- ...attributeHandler({ htmlPrefix: 'type', nodeAttribute: 'type' }),
+ ...attributeHandler({ nodeAttribute: CONTEXT_VALUE_ATTRIBUTE_NAME }),
+ ...attributeHandler({ nodeAttribute: CONTEXT_TYPE_ATTRIBUTE_NAME }),
}
},
@@ -162,20 +169,26 @@ export default Node.create<MentionOptions>({
}),
]
},
-}).extend({
- addCommands() {
- return {
- setPromptContext: (context) => ({ commands }) => {
- return commands.insertContent([
- {
- type: this.name,
- attrs: {
- ['type']: context.type,
- ['value']: context.value,
- },
- },
- ])
- },
- }
- },
})
+ .extend({
+ addCommands() {
+ return {
+ setPromptContext: (context) => ({ commands, state }) => {
+ return commands
+ .setContent([
+ {
+ type: this.name,
+ attrs: {
+ [CONTEXT_TYPE_ATTRIBUTE_NAME]: context.type,
+ [CONTEXT_VALUE_ATTRIBUTE_NAME]: context.value,
+ },
+ },
+ {
+ type: 'text',
+ text: ' ',
+ },
+ ])
+ },
+ }
+ },
+ })
diff --git a/hooks/useDocEditor.ts b/hooks/useDocEditor.ts
index f3bdd5847..b70779b40 100644
--- a/hooks/useDocEditor.ts
+++ b/hooks/useDocEditor.ts
@@ -13,7 +13,7 @@ import OrderedList from '@tiptap/extension-ordered-list'
import CodeBlock from '@tiptap/extension-code-block'
import ContextAutocomplete from 'editor/extensions/contextAutocomplete'
-import PromptContext from 'editor/extensions/promptContext'
+import Context, { CONTEXT_TYPE_ATTRIBUTE_NAME, CONTEXT_VALUE_ATTRIBUTE_NAME, PromptContext } from 'editor/extensions/promptContext'
const extensions = [
StarterKit.configure({
@@ -27,7 +27,7 @@ const extensions = [
italic: false,
strike: false,
- // We use the Ordered List, Bullet List and List item from explicit packages
+ // We use the Ordered List, Code block, Bullet List and List item from explicit packages
// so we can use their names in the markdown serializer.
codeBlock: false,
bulletList: false,
@@ -39,10 +39,9 @@ const extensions = [
ListItem,
BulletList,
ContextAutocomplete,
- PromptContext,
+ Context,
]
-
const schema = getSchema(extensions)
const serializer = new MarkdownSerializer({
@@ -51,13 +50,27 @@ const serializer = new MarkdownSerializer({
[ListItem.name]: defaultMarkdownSerializer.nodes.list_item,
[BulletList.name]: defaultMarkdownSerializer.nodes.bullet_list,
[CodeBlock.name]: defaultMarkdownSerializer.nodes.code_block,
-}, defaultMarkdownSerializer.marks)
-
-
-export function html2markdown(html: string) {
+ [Context.name]: (state, node, parent, index) => {
+ state.text(node.attrs[CONTEXT_VALUE_ATTRIBUTE_NAME])
+ },
+}, {
+ ...defaultMarkdownSerializer.marks,
+})
+
+export function html2markdown(html: string): [string, PromptContext[]] {
const node = createDocument(html, schema)
const markdown = serializer.serialize(node)
- return markdown
+ const context: PromptContext[] = []
+ node.descendants(n => {
+ if (n.type.name === Context.name) {
+ context.push({
+ type: n.attrs[CONTEXT_TYPE_ATTRIBUTE_NAME],
+ value: n.attrs[CONTEXT_VALUE_ATTRIBUTE_NAME],
+ })
+ }
+ })
+
+ return [markdown, context]
}
function useDocEditor({
@@ -106,7 +119,6 @@ function useDocEditor({
instance.on('transaction', t => {
if (t.transaction.docChanged) {
- console.log(t.editor.getHTML())
onContentChange(t.editor.getHTML())
}
})
diff --git a/styles/prosemirror.css b/styles/prosemirror.css
index 4f7d2bd1b..bdff8a5f7 100644
--- a/styles/prosemirror.css
+++ b/styles/prosemirror.css
@@ -24,7 +24,6 @@
.ProseMirror p {
@apply mx-0 my-0 py-0 leading-6;
- @apply whitespace-pre-wrap break-words;
}
/* Placeholder (on every new line) */
@@ -38,3 +37,8 @@
visibility: hidden;
}
}
+
+.prompt-context {
+ @apply py-0.5 text-indigo-400 font-semibold font-mono;
+ box-decoration-break: clone;
+}
\ No newline at end of file
diff --git a/utils/attributeHandler.ts b/utils/attributeHandler.ts
index ba7b523f6..868dfcbc4 100644
--- a/utils/attributeHandler.ts
+++ b/utils/attributeHandler.ts
@@ -10,9 +10,7 @@ function attributeHandler({
return {
[nodeAttribute]: {
default: undefined,
- parseHTML: (element: HTMLElement) => ({
- [nodeAttribute]: element.getAttribute(htmlAttribute),
- }),
+ parseHTML: (element: HTMLElement) => element.getAttribute(htmlAttribute),
renderHTML: (attributes: Record<string, any>) => ({
...attributes[nodeAttribute] && { [htmlAttribute]: attributes[nodeAttribute] },
}),