Open-source, secure environment with real-world tools for enterprise-grade agents.
Python agentaiai-agentai-agentscode-interpretercopilotdevelopmentdevtoolsgptgpt-4javascriptllmnextjsopenaipythonreactsoftwaretypescript
Cleanup
Tomas Valentae6901583df7c
diff --git a/components/Editor/extensions/ContextAutocomplete/Item.tsx b/components/Editor/RouteEditor/PromptEditor/Autocomplete/Item.tsxsimilarity index 100%rename from components/Editor/extensions/ContextAutocomplete/Item.tsxrename to components/Editor/RouteEditor/PromptEditor/Autocomplete/Item.tsxdiff --git a/components/Editor/RouteEditor/PromptEditor/Autocomplete/ListWrapper.tsx b/components/Editor/RouteEditor/PromptEditor/Autocomplete/ListWrapper.tsxnew file mode 100644index 000000000..76c19ce62--- /dev/null+++ b/components/Editor/RouteEditor/PromptEditor/Autocomplete/ListWrapper.tsx@@ -0,0 +1,42 @@+import {+ createRef,+ ForwardRefExoticComponent,+ Component,+ createElement,+ RefAttributes,+ RefObject,+} from 'react'++import {+ SuggestionProps,+ SuggestionKeyDownProps,+} from 'editor/extensions/autocomplete/suggestion'++type AutocompleteListRef = Partial<HTMLDivElement> & { onKeyDown: (props: SuggestionKeyDownProps) => boolean }+export interface AutocompleteListProps extends SuggestionProps { }++export type AutocompleteList = ForwardRefExoticComponent<AutocompleteListProps & RefAttributes<AutocompleteListRef>>++export interface AutocompleteListWrapperProps extends SuggestionProps {+ list: AutocompleteList+}++class AutocompleteListWrapper extends Component<AutocompleteListWrapperProps, {}, {}> {+ childRef: RefObject<AutocompleteListRef>++ constructor(public props: AutocompleteListWrapperProps) {+ super(props)+ this.childRef = createRef<AutocompleteListRef>()+ props.list.name+ }++ onKeyDown(props: SuggestionKeyDownProps) {+ return this.childRef.current?.onKeyDown(props)+ }++ render() {+ return createElement(this.props.list, { ...this.props, ref: this.childRef, }, null)+ }+}++export default AutocompleteListWrapper\ No newline at end of filediff --git a/components/Editor/extensions/ContextAutocomplete/index.tsx b/components/Editor/RouteEditor/PromptEditor/Autocomplete/index.tsxsimilarity index 69%rename from components/Editor/extensions/ContextAutocomplete/index.tsxrename to components/Editor/RouteEditor/PromptEditor/Autocomplete/index.tsxindex ed6b3f91a..6d0fc1edf 100644--- a/components/Editor/extensions/ContextAutocomplete/index.tsx+++ b/components/Editor/RouteEditor/PromptEditor/Autocomplete/index.tsx@@ -6,28 +6,39 @@ import {} from 'react'import { ChainedCommands } from '@tiptap/core'-import { CommandList } from 'components/Editor/extensions/command/CommandListWrapper'-import { PromptContext, promptContextItems } from 'editor/extensions/promptContext'+import { Reference, ReferenceType } from 'editor/referenceType'import Item from './Item'+import { AutocompleteList } from './ListWrapper'-interface CommandItem {+interface AutocompleteItem {title: stringextendCommand: (cmd: ChainedCommands) => ChainedCommands}-function createCommandItem(context: PromptContext): CommandItem {+function createAutocompleteItem(reference: Reference): AutocompleteItem {return {- title: context.value,- extendCommand: (cmd) => cmd.setPromptContext(context)+ title: reference.value,+ extendCommand: (cmd) => cmd.setReference(reference)}}-const items: CommandItem[] = [- ...promptContextItems.map(createCommandItem),+export const referenceItems: Reference[] = [+ {+ type: ReferenceType.NPMPackage,+ value: '@slack/web-api',+ },+ {+ type: ReferenceType.DEPLOYMENT,+ value: 'AWS Lambda',+ },]-const ContextAutocomplete: CommandList = forwardRef(({+const items: AutocompleteItem[] = [+ ...referenceItems.map(createAutocompleteItem),+]++const Autocomplete: AutocompleteList = forwardRef(({editor,range,}, ref) => {@@ -101,6 +112,6 @@ const ContextAutocomplete: CommandList = forwardRef(({)})-ContextAutocomplete.displayName = 'ContextAutocomplete'+Autocomplete.displayName = 'Autocomplete'-export default ContextAutocomplete+export default Autocompletediff --git a/components/Editor/RouteEditor/PromptEditor.tsx b/components/Editor/RouteEditor/PromptEditor/index.tsxsimilarity index 100%rename from components/Editor/RouteEditor/PromptEditor.tsxrename to components/Editor/RouteEditor/PromptEditor/index.tsxdiff --git a/components/Editor/Sidebar/index.tsx b/components/Editor/Sidebar/index.tsxindex 9acc56a5d..fe16e6654 100644--- a/components/Editor/Sidebar/index.tsx+++ b/components/Editor/Sidebar/index.tsx@@ -8,7 +8,7 @@ import Text from 'components/Text'import { Route, Block } from 'state/store'import { useLatestDeployment } from 'hooks/useLatestDeployment'import { useStateStore } from 'state/StoreProvider'-import { html2markdown } from 'hooks/useDocEditor'+import { html2markdown } from 'editor/schema'import DeployButton from './DeployButton'import Logs from './Logs'@@ -40,7 +40,7 @@ async function handlePostGenerate(url: string, { arg }: {switch (b.type) {case 'Description':case 'Instructions':- const [markdown, context] = html2markdown(b.content)+ const [markdown, references] = html2markdown(b.content)const block: Block = {...b,content: markdown,diff --git a/components/Editor/extensions/command/CommandListWrapper.tsx b/components/Editor/extensions/command/CommandListWrapper.tsxdeleted file mode 100644index 2fc8f0131..000000000--- a/components/Editor/extensions/command/CommandListWrapper.tsx+++ /dev/null@@ -1,42 +0,0 @@-import {- createRef,- ForwardRefExoticComponent,- Component,- createElement,- RefAttributes,- RefObject,-} from 'react'--import {- SuggestionProps,- SuggestionKeyDownProps,-} from 'editor/extensions/command/suggestion'--type CommandListRef = Partial<HTMLDivElement> & { onKeyDown: (props: SuggestionKeyDownProps) => boolean }-export interface CommandListProps extends SuggestionProps { }--export type CommandList = ForwardRefExoticComponent<CommandListProps & RefAttributes<CommandListRef>>--export interface CommmandListWrapperProps extends SuggestionProps {- list: CommandList-}--class CommandListWrapper extends Component<CommmandListWrapperProps, {}, {}> {- childRef: RefObject<CommandListRef>-- constructor(public props: CommmandListWrapperProps) {- super(props)- this.childRef = createRef<CommandListRef>()- props.list.name- }-- onKeyDown(props: SuggestionKeyDownProps) {- return this.childRef.current?.onKeyDown(props)- }-- render() {- return createElement(this.props.list, { ...this.props, ref: this.childRef, }, null)- }-}--export default CommandListWrapper\ No newline at end of filediff --git a/editor/extensions/command/findSuggestionMatch.ts b/editor/extensions/autocomplete/findSuggestionMatch.tssimilarity index 100%rename from editor/extensions/command/findSuggestionMatch.tsrename to editor/extensions/autocomplete/findSuggestionMatch.tsdiff --git a/editor/extensions/command/index.ts b/editor/extensions/autocomplete/index.tssimilarity index 84%rename from editor/extensions/command/index.tsrename to editor/extensions/autocomplete/index.tsindex d0ebacae1..96e4224bd 100644--- a/editor/extensions/command/index.ts+++ b/editor/extensions/autocomplete/index.ts@@ -10,20 +10,21 @@ import tippy, {import { ComponentClass } from 'react'import { destroyOnEsc } from 'editor/tippyPlugins'-import CommandListWrapper, { CommandList } from 'components/Editor/extensions/command/CommandListWrapper'+import AutocompleteListWrapper, { AutocompleteList } from 'components/Editor/RouteEditor/PromptEditor/Autocomplete/ListWrapper'import { Suggestion, SuggestionOptions } from './suggestion'+import Autocomplete from 'components/Editor/RouteEditor/PromptEditor/Autocomplete'export * from './findSuggestionMatch'export * from './suggestion'-export type CommandOptions = {- list?: CommandList+export type AutocompleteOptions = {+ list?: AutocompleteListsuggestion: Omit<SuggestionOptions, 'editor'>}-export const Command = (component: CommandList) => Node.create<CommandOptions>({- name: 'command',+export default Node.create<AutocompleteOptions>({+ name: 'autocomplete',addOptions() {return {suggestion: {@@ -38,11 +39,11 @@ export const Command = (component: CommandList) => Node.create<CommandOptions>({return {onStart: props => {disabled = false- reactRenderer = new ReactRenderer(CommandListWrapper as unknown as ComponentClass, {+ reactRenderer = new ReactRenderer(AutocompleteListWrapper as unknown as ComponentClass, {editor: props.editor as ReactEditor,props: {...props,- list: component,+ list: Autocomplete,},})@@ -100,8 +101,9 @@ export const Command = (component: CommandList) => Node.create<CommandOptions>({return [Suggestion({editor: this.editor,+ startOfLine: false,...this.options.suggestion,}),]},-})\ No newline at end of file+})diff --git a/editor/extensions/command/suggestion.ts b/editor/extensions/autocomplete/suggestion.tssimilarity index 100%rename from editor/extensions/command/suggestion.tsrename to editor/extensions/autocomplete/suggestion.tsdiff --git a/editor/extensions/contextAutocomplete.ts b/editor/extensions/contextAutocomplete.tsdeleted file mode 100644index 6eafde378..000000000--- a/editor/extensions/contextAutocomplete.ts+++ /dev/null@@ -1,15 +0,0 @@-import ContextAutocomplete from 'components/Editor/extensions/ContextAutocomplete'--import { Command } from './command'--const ContextAutocompleteName = 'contextAutocomplete'--export default Command(ContextAutocomplete)- .extend({- name: ContextAutocompleteName,- })- .configure({- suggestion: {- startOfLine: false,- },- })diff --git a/editor/extensions/promptContext.ts b/editor/extensions/reference.tssimilarity index 70%rename from editor/extensions/promptContext.tsrename to editor/extensions/reference.tsindex fd86923c5..e820abfca 100644--- a/editor/extensions/promptContext.ts+++ b/editor/extensions/reference.ts@@ -3,62 +3,43 @@ import { Node as ProseMirrorNode } from '@tiptap/pm/model'import { PluginKey } from '@tiptap/pm/state'import attributeHandler from 'utils/attributeHandler'+import { Reference } from 'editor/referenceType'-import { Suggestion, SuggestionOptions } from './command'+import { Suggestion, SuggestionOptions } from './autocomplete'-export enum PromptContextType {- NPMPackage = 'NPM_PACKAGE',- DEPLOYMENT = 'DEPLOYMENT_SERVICE',-}--export interface PromptContext {- type: PromptContextType- value: string-}declare module '@tiptap/core' {interface Commands<ReturnType> {- promptContext: {- setPromptContext: (context: PromptContext) => ReturnType,+ reference: {+ setReference: (reference: Reference) => ReturnType,}}}-export const CONTEXT_VALUE_ATTRIBUTE_NAME = 'context-value'-export const CONTEXT_TYPE_ATTRIBUTE_NAME = 'context-type'--export const promptContextItems: PromptContext[] = [- {- type: PromptContextType.NPMPackage,- value: '@slack/web-api',- },- {- type: PromptContextType.DEPLOYMENT,- value: 'AWS Lambda',- },-]+export const REFERENCE_TYPE_ATTRIBUTE_NAME = 'reference-type'+export const REFERENCE_VALUE_ATTRIBUTE_NAME = 'reference-value'-export type MentionOptions = {+export type ReferenceOptions = {HTMLAttributes: Record<string, any>- renderLabel: (props: { options: MentionOptions; node: ProseMirrorNode }) => string+ renderLabel: (props: { options: ReferenceOptions; node: ProseMirrorNode }) => stringsuggestion: Omit<SuggestionOptions, 'editor'>}-export const NODE_NAME = 'promptContext'-export const PromptContextPluginKey = new PluginKey(NODE_NAME)+export const NODE_NAME = 'reference'+export const ReferencePluginKey = new PluginKey(NODE_NAME)-export default Node.create<MentionOptions>({+export default Node.create<ReferenceOptions>({name: NODE_NAME,addOptions() {return {HTMLAttributes: {- class: 'prompt-context',+ class: NODE_NAME,},renderLabel({ node }) {- return `${node.attrs[CONTEXT_VALUE_ATTRIBUTE_NAME]}`+ return `${node.attrs[REFERENCE_VALUE_ATTRIBUTE_NAME]}`},suggestion: {- pluginKey: PromptContextPluginKey,+ pluginKey: ReferencePluginKey,command: ({ editor, range, props }) => {console.log('Command >>>')// increase range.to by one when the next node is of type "text"@@ -105,8 +86,8 @@ export default Node.create<MentionOptions>({addAttributes() {return {- ...attributeHandler({ nodeAttribute: CONTEXT_VALUE_ATTRIBUTE_NAME }),- ...attributeHandler({ nodeAttribute: CONTEXT_TYPE_ATTRIBUTE_NAME }),+ ...attributeHandler({ nodeAttribute: REFERENCE_VALUE_ATTRIBUTE_NAME }),+ ...attributeHandler({ nodeAttribute: REFERENCE_TYPE_ATTRIBUTE_NAME }),}},@@ -173,14 +154,14 @@ export default Node.create<MentionOptions>({.extend({addCommands() {return {- setPromptContext: (context) => ({ commands, state }) => {+ setReference: (reference) => ({ commands, state }) => {return commands.setContent([{type: this.name,attrs: {- [CONTEXT_TYPE_ATTRIBUTE_NAME]: context.type,- [CONTEXT_VALUE_ATTRIBUTE_NAME]: context.value,+ [REFERENCE_TYPE_ATTRIBUTE_NAME]: reference.type,+ [REFERENCE_VALUE_ATTRIBUTE_NAME]: reference.value,},},{diff --git a/editor/referenceType.ts b/editor/referenceType.tsnew file mode 100644index 000000000..1eda7fbdf--- /dev/null+++ b/editor/referenceType.ts@@ -0,0 +1,9 @@+export enum ReferenceType {+ NPMPackage = 'NPM_PACKAGE',+ DEPLOYMENT = 'DEPLOYMENT_SERVICE',+}++export interface Reference {+ type: ReferenceType+ value: string+}diff --git a/editor/schema.ts b/editor/schema.tsnew file mode 100644index 000000000..371ba66fc--- /dev/null+++ b/editor/schema.ts@@ -0,0 +1,78 @@+import { createDocument, getSchema } from '@tiptap/react'+import { StarterKit } from '@tiptap/starter-kit'+import { MarkdownSerializer, defaultMarkdownSerializer } from 'prosemirror-markdown'+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 ReferenceExtension, { REFERENCE_TYPE_ATTRIBUTE_NAME, REFERENCE_VALUE_ATTRIBUTE_NAME } from 'editor/extensions/reference'+import AutocompleteExtension from 'editor/extensions/autocomplete'++export enum ReferenceType {+ NPMPackage = 'NPM_PACKAGE',+ DEPLOYMENT = 'DEPLOYMENT_SERVICE',+}++export interface Reference {+ type: ReferenceType+ value: string+}++export const extensions = [+ StarterKit.configure({+ blockquote: false,+ bold: false,+ // code: false,+ dropcursor: false,+ hardBreak: false,+ // heading: false,+ horizontalRule: false,+ italic: false,+ strike: false,++ // 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,+ listItem: false,+ orderedList: false,+ }),+ CodeBlock,+ OrderedList,+ ListItem,+ BulletList,+ AutocompleteExtension,+ ReferenceExtension,+]++const schema = getSchema(extensions)++const serializer = new MarkdownSerializer({+ ...defaultMarkdownSerializer.nodes,+ [OrderedList.name]: defaultMarkdownSerializer.nodes.ordered_list,+ [ListItem.name]: defaultMarkdownSerializer.nodes.list_item,+ [BulletList.name]: defaultMarkdownSerializer.nodes.bullet_list,+ [CodeBlock.name]: defaultMarkdownSerializer.nodes.code_block,+ [ReferenceExtension.name]: (state, node) => {+ state.text(node.attrs[REFERENCE_VALUE_ATTRIBUTE_NAME])+ },+}, {+ ...defaultMarkdownSerializer.marks,+})++export function html2markdown(html: string): [string, Reference[]] {+ const node = createDocument(html, schema)+ const markdown = serializer.serialize(node)+ const references: Reference[] = []+ node.descendants(n => {+ if (n.type.name === ReferenceExtension.name) {+ references.push({+ type: n.attrs[REFERENCE_TYPE_ATTRIBUTE_NAME],+ value: n.attrs[REFERENCE_VALUE_ATTRIBUTE_NAME],+ })+ }+ })++ return [markdown, references]+}diff --git a/hooks/useDocEditor.ts b/hooks/useDocEditor.tsindex b70779b40..969201cde 100644--- a/hooks/useDocEditor.ts+++ b/hooks/useDocEditor.ts@@ -2,76 +2,10 @@ import {useEffect,useState,} from 'react'-import { createDocument, Editor, getSchema } from '@tiptap/react'-import { StarterKit } from '@tiptap/starter-kit'+import { Editor } from '@tiptap/react'import Placeholder from '@tiptap/extension-placeholder'import { keymap } from 'prosemirror-keymap'-import { MarkdownSerializer, defaultMarkdownSerializer } from 'prosemirror-markdown'-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 ContextAutocomplete from 'editor/extensions/contextAutocomplete'-import Context, { CONTEXT_TYPE_ATTRIBUTE_NAME, CONTEXT_VALUE_ATTRIBUTE_NAME, PromptContext } from 'editor/extensions/promptContext'--const extensions = [- StarterKit.configure({- blockquote: false,- bold: false,- // code: false,- dropcursor: false,- hardBreak: false,- // heading: false,- horizontalRule: false,- italic: false,- strike: false,-- // 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,- listItem: false,- orderedList: false,- }),- CodeBlock,- OrderedList,- ListItem,- BulletList,- ContextAutocomplete,- Context,-]--const schema = getSchema(extensions)--const serializer = new MarkdownSerializer({- ...defaultMarkdownSerializer.nodes,- [OrderedList.name]: defaultMarkdownSerializer.nodes.ordered_list,- [ListItem.name]: defaultMarkdownSerializer.nodes.list_item,- [BulletList.name]: defaultMarkdownSerializer.nodes.bullet_list,- [CodeBlock.name]: defaultMarkdownSerializer.nodes.code_block,- [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)- 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]-}+import { extensions } from 'editor/schema'function useDocEditor({initialContent,diff --git a/styles/prosemirror.css b/styles/prosemirror.cssindex bdff8a5f7..863c1213a 100644--- a/styles/prosemirror.css+++ b/styles/prosemirror.css@@ -38,7 +38,7 @@}}-.prompt-context {+.reference {@apply py-0.5 text-indigo-400 font-semibold font-mono;box-decoration-break: clone;}\ No newline at end of file