Skip to content

armgit / E2B

Public Imported from GitHub
0

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

Transform structured prose

Tomas Valentab67ed8b94bed
diff --git a/components/ProjectBoard/RouteEditor/PromptEditor.tsx b/components/ProjectBoard/RouteEditor/PromptEditor.tsx
index ff2bc54f0..6e94f2d96 100644
--- a/components/ProjectBoard/RouteEditor/PromptEditor.tsx
+++ b/components/ProjectBoard/RouteEditor/PromptEditor.tsx
@@ -1,24 +1,30 @@
import { EditorContent } from '@tiptap/react'
+import { Block } from 'state/store'
import useDocEditor from 'hooks/useDocEditor'
export interface Props {
- initialContent: string
- onContentChange: (content: string) => void
+ onChange: (value: string) => void
+ block: Block
}
-function PromptEditor({ initialContent, onContentChange }: Props) {
+function PromptEditor({ onChange, block }: Props) {
const editor = useDocEditor({
- initialContent,
- onContentChange,
+ initialContent: block.prompt,
+ onContentChange: onChange,
})
return (
<div className="
flex
flex-1
+ bg-slate-100
+ min-w-full
">
- <EditorContent editor={editor || null} />
+ <EditorContent
+ editor={editor}
+ className="w-full h-full"
+ />
</div>
)
}
diff --git a/components/ProjectBoard/RouteEditor/RequestBodyEditor.tsx b/components/ProjectBoard/RouteEditor/RequestBodyEditor.tsx
index 19b5a20eb..5f5bb35e9 100644
--- a/components/ProjectBoard/RouteEditor/RequestBodyEditor.tsx
+++ b/components/ProjectBoard/RouteEditor/RequestBodyEditor.tsx
@@ -4,7 +4,7 @@ import TextareaAutosize from 'react-textarea-autosize'
import { Block } from 'state/store'
export interface Props {
- onChange: (block: Block) => void
+ onChange: (value: string) => void
block: Block
}
@@ -43,7 +43,7 @@ function RequestBodyEditor({
name="block"
placeholder="fieldName: Type,"
value={block.prompt}
- onChange={e => onChange({ ...block, prompt: e.target.value })}
+ onChange={e => onChange(e.target.value)}
className={clsx(
'w-full',
'pl-8',
diff --git a/components/ProjectBoard/RouteEditor/index.tsx b/components/ProjectBoard/RouteEditor/index.tsx
index e59247f2d..f3f1d3be3 100644
--- a/components/ProjectBoard/RouteEditor/index.tsx
+++ b/components/ProjectBoard/RouteEditor/index.tsx
@@ -1,11 +1,10 @@
-import { Fragment } from 'react'
-
import Select from 'components/Select'
import { methods, Method, Route } from 'state/store'
+import { useStateStore } from 'state/StoreProvider'
+import useBlock from 'hooks/useBlock'
import PromptEditor from './PromptEditor'
import RequestBodyEditor from './RequestBodyEditor'
-import { useStateStore } from 'state/StoreProvider'
export interface Props {
route?: Route
@@ -13,9 +12,10 @@ export interface Props {
function RouteEditor({ route }: Props) {
const store = useStateStore()
-
const changeRoute = store.use.changeRoute()
- const changeBlock = store.use.changeBlock()
+
+ const [requestBodyBlock, updateRequestBodyBlock] = useBlock('RequestBody', route)
+ const [promptBlock, updatePromptBlock] = useBlock('StructuredProse', route)
return (
<div className="
@@ -55,29 +55,23 @@ function RouteEditor({ route }: Props) {
transition-all
flex-1
">
- {route.blocks.map((b, i) =>
- <Fragment
- key={b.id}
- >
- {b.type === 'RequestBody' &&
- <RequestBodyEditor
- block={b}
- onChange={b => changeBlock(route.id, i, b)}
- />
- }
- {b.type === 'StructuredProse' &&
- <PromptEditor
- initialContent={b.prompt}
- onContentChange={c => changeBlock(route.id, i, { ...b, prompt: c })}
- />
- }
- </Fragment>
- )}
+ {requestBodyBlock &&
+ <RequestBodyEditor
+ block={requestBodyBlock}
+ onChange={updateRequestBodyBlock}
+ />
+ }
+ {promptBlock &&
+ <PromptEditor
+ block={promptBlock}
+ onChange={updatePromptBlock}
+ />
+ }
</div>
</div>
</>
}
- </div>
+ </div >
)
}
diff --git a/components/ProjectBoard/Sidebar/index.tsx b/components/ProjectBoard/Sidebar/index.tsx
index 663d7cecf..b6ef58865 100644
--- a/components/ProjectBoard/Sidebar/index.tsx
+++ b/components/ProjectBoard/Sidebar/index.tsx
@@ -5,13 +5,14 @@ import clsx from 'clsx'
import useSWRMutation from 'swr/mutation'
import Text from 'components/Text'
-import { Route } from 'state/store'
+import { Route, Block } from 'state/store'
import { useLatestDeployment } from 'hooks/useLatestDeployment'
import { useStateStore } from 'state/StoreProvider'
import DeployButton from './DeployButton'
import Logs from './Logs'
import Envs from './Envs'
+import { getPromptText } from 'hooks/useDocEditor'
export interface Props {
project: projects
@@ -34,7 +35,20 @@ async function handlePostGenerate(url: string, { arg }: {
body: JSON.stringify({
projectID: arg.projectID,
routeID: arg.route.id,
- blocks: arg.route.blocks,
+ // Transform block with structured prose into block with plain prompt text.
+ blocks: arg.route.blocks.map(b => {
+ switch (b.type) {
+ case 'StructuredProse':
+ const block: Block = {
+ ...b,
+ type: 'Basic',
+ prompt: getPromptText(b.prompt),
+ }
+ return block
+ default:
+ return b
+ }
+ }),
method: arg.route.method.toLowerCase(),
route: arg.route.route,
envs: arg.envs,
diff --git a/hooks/useBlock.tsx b/hooks/useBlock.tsx
new file mode 100644
index 000000000..1d7efce45
--- /dev/null
+++ b/hooks/useBlock.tsx
@@ -0,0 +1,27 @@
+import { useCallback } from 'react'
+import { Route, BlockType, Block } from 'state/store'
+import { useStateStore } from 'state/StoreProvider'
+
+function useBlock(blockType: BlockType, route?: Route): [Block | undefined, (prompt: string) => void] {
+ const store = useStateStore()
+
+ const changeBlock = store.use.changeBlock()
+
+ const blockIdx = route?.blocks.findIndex(r => r.type === blockType)
+ const block = blockIdx !== undefined &&
+ blockIdx !== -1 &&
+ route?.blocks.length !== undefined &&
+ route?.blocks.length >= blockIdx
+ ? route?.blocks[blockIdx]
+ : undefined
+
+ const setBlock = useCallback((prompt: string) => {
+ if (route?.id && blockIdx !== undefined && blockIdx !== -1) {
+ changeBlock(route.id, blockIdx, { prompt })
+ }
+ }, [blockIdx, route?.id])
+
+ return [block, setBlock]
+}
+
+export default useBlock
diff --git a/hooks/useDocEditor.tsx b/hooks/useDocEditor.tsx
index 727acf146..d3380f8a8 100644
--- a/hooks/useDocEditor.tsx
+++ b/hooks/useDocEditor.tsx
@@ -1,14 +1,51 @@
import {
useEffect,
+ useReducer,
useState,
} from 'react'
-import { Editor } from '@tiptap/react'
+import { createDocument, Editor, getSchema } from '@tiptap/react'
import { StarterKit } from '@tiptap/starter-kit'
import Placeholder from '@tiptap/extension-placeholder'
import { keymap } from 'prosemirror-keymap'
// import SlashCommand from 'src/editor/extensions/slashCommand'
+const extensions = [
+ StarterKit.configure({
+ blockquote: false,
+ bold: false,
+ bulletList: false,
+ code: false,
+ codeBlock: false,
+ dropcursor: false,
+ hardBreak: false,
+ heading: false,
+ horizontalRule: false,
+ italic: false,
+ listItem: false,
+ strike: false,
+ orderedList: false,
+ }),
+ Placeholder.configure({
+ placeholder: "Type '/' for commands",
+ }),
+ // SlashCommand,
+]
+
+const schema = getSchema(extensions)
+
+export function getPromptText(structuredProse: string) {
+ const node = createDocument(structuredProse, schema)
+
+ let text = ''
+
+ node.descendants(n => {
+ text += n.content
+ })
+
+ return text
+}
+
function useDocEditor({
initialContent,
onContentChange,
@@ -16,31 +53,11 @@ function useDocEditor({
initialContent: string,
onContentChange: (content: string) => void,
}) {
- const [editor, setEditor] = useState<Editor>()
+ const [editor, setEditor] = useState<Editor | null>(null)
- useEffect(function initialize() {
- const extensions = [
- StarterKit.configure({
- blockquote: false,
- bold: false,
- bulletList: false,
- code: false,
- codeBlock: false,
- dropcursor: false,
- hardBreak: false,
- heading: false,
- horizontalRule: false,
- italic: false,
- listItem: false,
- strike: false,
- orderedList: false,
- }),
- Placeholder.configure({
- placeholder: "Type '/' for commands",
- }),
- // SlashCommand,
- ]
+ const forceUpdate = useReducer(() => ({}), {})[1]
+ useEffect(function initialize() {
const instance = new Editor({
content: initialContent,
parseOptions: {
@@ -63,20 +80,28 @@ function useDocEditor({
},
}))
- instance.on('update', u => {
- if (u.transaction.docChanged) {
- onContentChange(u.transaction.doc.toJSON())
+ console.log('reinit')
+ instance.on('transaction', t => {
+ if (t.transaction.docChanged) {
+ onContentChange(t.transaction.doc.toJSON())
}
+
+ // requestAnimationFrame(() => {
+ // requestAnimationFrame(() => {
+ // forceUpdate()
+ // })
+ // })
})
setEditor(instance)
return () => {
instance.destroy()
- setEditor(undefined)
+ setEditor(null)
}
}, [
onContentChange,
+ forceUpdate,
])
return editor