Open-source, secure environment with real-world tools for enterprise-grade agents.
Python agentaiai-agentai-agentscode-interpretercopilotdevelopmentdevtoolsgptgpt-4javascriptllmnextjsopenaipythonreactsoftwaretypescript
Add context schema node [WIP]
Tomas Valenta76cf1635043d
diff --git a/components/Editor/extensions/ContextAutocomplete/Item.tsx b/components/Editor/extensions/ContextAutocomplete/Item.tsxindex 2c41c079e..16e03416a 100644--- a/components/Editor/extensions/ContextAutocomplete/Item.tsx+++ b/components/Editor/extensions/ContextAutocomplete/Item.tsx@@ -29,17 +29,19 @@ function Item({<divref={setScrollRef}className={cn(- 'px-1.5',- 'py-0.5',- 'flex-shrink-0',+ 'px-4',+ 'py-1',+ 'shrink-0','flex',- 'rounded',+ 'font-sans','items-center','group','overflow-ellipsis','overflow-hidden','cursor-pointer',- { 'bg-slate-300': isSelected },+ {+ 'bg-indigo-50': isSelected,+ },)}onMouseDown={selectItem}>diff --git a/components/Editor/extensions/ContextAutocomplete/index.tsx b/components/Editor/extensions/ContextAutocomplete/index.tsxindex 6eefda182..4db8f1f8c 100644--- a/components/Editor/extensions/ContextAutocomplete/index.tsx+++ b/components/Editor/extensions/ContextAutocomplete/index.tsx@@ -7,7 +7,7 @@ import {import { ChainedCommands } from '@tiptap/core'import { CommandList } from 'components/Editor/extensions/command/CommandListWrapper'-import contextSchema from 'editor/extensions/context/contextSchema'+import { PromptContext, promptContextItems } from 'editor/extensions/promptContext'import Item from './Item'@@ -16,32 +16,10 @@ interface CommandItem {extendCommand: (cmd: ChainedCommands) => ChainedCommands}-export enum PromptContextType {- NPMPackage = 'NPM_PACKAGE'-}--export interface PromptContext {- type: PromptContextType- value: string-}--const promptContextItems: PromptContext[] = [- {- type: PromptContextType.NPMPackage,- value: '@slack/web-api',- },-]-function createCommandItem(context: PromptContext): CommandItem {return {title: context.value,- extendCommand: (cmd) => {- return cmd- .setNode(contextSchema.name, {- type: context.type,- value: context.value,- })- }+ extendCommand: (cmd) => cmd.setPromptContext(context)}}@@ -93,7 +71,6 @@ const ContextAutocomplete: CommandList = forwardRef(({return (<divclassName="- p-2w-56max-h-56@@ -103,11 +80,10 @@ const ContextAutocomplete: CommandList = forwardRef(({overflow-y-autooverscroll-none-roundedborder- border-slate-400+ border-indigo-400bg-white"diff --git a/editor/extensions/command/findSuggestionMatch.ts b/editor/extensions/command/findSuggestionMatch.tsindex f6133c6aa..021622320 100644--- a/editor/extensions/command/findSuggestionMatch.ts+++ b/editor/extensions/command/findSuggestionMatch.ts@@ -2,10 +2,10 @@ import { Range } from '@tiptap/core'import { ResolvedPos } from 'prosemirror-model'export interface Trigger {- char: string,- allowSpaces: boolean,- startOfLine: boolean,- $position: ResolvedPos,+ char?: string+ allowSpaces: boolean+ startOfLine: boolean+ $position: ResolvedPos}export type SuggestionMatch = {@@ -16,7 +16,7 @@ export type SuggestionMatch = {export function findSuggestionMatch(config: Trigger): SuggestionMatch {const {- char,+ char = '',allowSpaces,startOfLine,$position,diff --git a/editor/extensions/command/index.ts b/editor/extensions/command/index.tsindex 8c01195ee..b43cd08eb 100644--- a/editor/extensions/command/index.ts+++ b/editor/extensions/command/index.ts@@ -28,7 +28,8 @@ export const Command = (component: CommandList) => Node.create<CommandOptions>({return {suggestion: {// Check if the cursor is in a paragraph.- allow: ({ editor: { state: { doc, selection } } }) => doc.resolve(selection.anchor)?.parent?.type.name === 'paragraph',+ allow: ({ editor: { state: { doc, selection } } }) =>+ doc.resolve(selection.anchor)?.parent?.type.name === 'paragraph',render: () => {let reactRenderer: anylet popup: Instance<Props> | undefineddiff --git a/editor/extensions/command/suggestion.ts b/editor/extensions/command/suggestion.tsindex d95109553..73f19db12 100644--- a/editor/extensions/command/suggestion.ts+++ b/editor/extensions/command/suggestion.ts@@ -45,9 +45,22 @@ export interface SuggestionKeyDownProps {range: Range,}++interface SuggestionPluginState {+ active: boolean+ range?: {+ from: number+ to: number+ }+ decorationID?: string+ query: string | null+ text: string | null+ wasCharTyped: boolean+}+export function Suggestion<Item = unknown>({editor,- char = '@',+ char,startOfLine = false,decorationTag = 'span',decorationClass = 'suggestion',@@ -60,16 +73,7 @@ export function Suggestion<Item = unknown>({const allowSpaces = false- return new Plugin<{- active: boolean- range?: {- from: number- to: number- }- query: string | null- text: string | null- wasCharTyped: boolean- }>({+ return new Plugin<SuggestionPluginState>({key: new PluginKey('suggestion'),view() {@@ -155,7 +159,7 @@ export function Suggestion<Item = unknown>({next.active = false} else if (selection.from === selection.to) {// Reset active state if we just left the previous suggestion range- if (selection.from < prev.range.from || selection.from > prev.range.to) {+ if (prev.range && (selection.from < prev.range?.from || selection.from > prev.range?.to)) {next.active = false}@@ -166,12 +170,12 @@ export function Suggestion<Item = unknown>({startOfLine,$position: selection.$from,})- const decorationId = `id_${Math.floor(Math.random() * 0xFFFFFFFF)}`+ const decorationID = `id_${Math.floor(Math.random() * 0xFFFFFFFF)}`// If we found a match, update the current state to show itif (match && allow({ editor, range: match.range })) {next.active = true- next.decorationId = prev.decorationId ? prev.decorationId : decorationId+ next.decorationID = prev.decorationID ? prev.decorationID : decorationIDnext.range = match.rangenext.query = match.querynext.text = match.text@@ -185,8 +189,8 @@ export function Suggestion<Item = unknown>({// Make sure to empty the range if suggestion is inactiveif (!next.active) {next.wasCharTyped = false- next.decorationId = null- next.range = {}+ next.decorationID = undefined+ next.range = undefinednext.query = nullnext.text = null}@@ -219,12 +223,20 @@ export function Suggestion<Item = unknown>({return false}+ if (!range) {+ return false+ }+return renderer?.onKeyDown?.({ view, event, range }) || false},// Setup decorator on the currently active suggestion.decorations(state) {- const { active, range, decorationId } = this.getState(state)+ const { active, range, decorationID } = this.getState(state) as SuggestionPluginState++ if (!range) {+ return null+ }if (!active) {return null@@ -234,7 +246,7 @@ export function Suggestion<Item = unknown>({Decoration.inline(range.from, range.to, {nodeName: decorationTag,class: decorationClass,- 'data-decoration-id': decorationId,+ 'data-decoration-id': decorationID,}),])},diff --git a/editor/extensions/context/contextSchema.ts b/editor/extensions/context/contextSchema.tsdeleted file mode 100644index 2eeabedc3..000000000--- a/editor/extensions/context/contextSchema.ts+++ /dev/null@@ -1,204 +0,0 @@-import { Node } from '@tiptap/core'--import {- contextCell as constants,-} from 'utils/constants'--export default Node.create({- name: constants.CONTEXT_NODE_NAME,- content: 'text*',- marks: '',- atom: false,- selectable: true,- group: 'inline',- isolating: true,- defining: true,-- addAttributes() {- return {- // ...attributeHandler({ htmlPrefix: HTML_ATTR_PREFIX, nodeAttribute: constants.NODE_ATTR_CELL_RUN_ON_START }),- }- },-- parseHTML() {- return [- {- tag: 'context',- preserveWhitespace: 'full',- },- ]- },-- renderHTML({ HTMLAttributes }) {- return [- 'pre',- HTMLAttributes,- ['code', 0],- ]- },-})---// import { mergeAttributes, Node } from '@tiptap/core'-// import { Node as ProseMirrorNode } from '@tiptap/pm/model'-// import { PluginKey } from '@tiptap/pm/state'-// import Suggestion, { SuggestionOptions } from '@tiptap/suggestion'--// export type MentionOptions = {-// HTMLAttributes: Record<string, any>-// renderLabel: (props: { options: MentionOptions; node: ProseMirrorNode }) => string-// suggestion: Omit<SuggestionOptions, 'editor'>-// }--// export const MentionPluginKey = new PluginKey('mention')--// export const Mention = Node.create<MentionOptions>({-// name: 'mention',--// addOptions() {-// return {-// HTMLAttributes: {},-// renderLabel({ options, node }) {-// return `${options.suggestion.char}${node.attrs.label ?? node.attrs.id}`-// },-// suggestion: {-// char: '@',-// pluginKey: MentionPluginKey,-// command: ({ editor, range, props }) => {-// // 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-// const overrideSpace = nodeAfter?.text?.startsWith(' ')--// if (overrideSpace) {-// range.to += 1-// }--// editor-// .chain()-// .focus()-// .insertContentAt(range, [-// {-// type: this.name,-// attrs: props,-// },-// {-// type: 'text',-// text: ' ',-// },-// ])-// .run()--// window.getSelection()?.collapseToEnd()-// },-// allow: ({ state, range }) => {-// const $from = state.doc.resolve(range.from)-// const type = state.schema.nodes[this.name]-// const allow = !!$from.parent.type.contentMatch.matchType(type)--// return allow-// },-// },-// }-// },--// group: 'inline',--// inline: true,--// selectable: false,--// atom: true,--// addAttributes() {-// return {-// id: {-// default: null,-// parseHTML: element => element.getAttribute('data-id'),-// renderHTML: attributes => {-// if (!attributes.id) {-// return {}-// }--// return {-// 'data-id': attributes.id,-// }-// },-// },--// label: {-// default: null,-// parseHTML: element => element.getAttribute('data-label'),-// renderHTML: attributes => {-// if (!attributes.label) {-// return {}-// }--// return {-// 'data-label': attributes.label,-// }-// },-// },-// }-// },--// parseHTML() {-// return [-// {-// tag: `span[data-type="${this.name}"]`,-// },-// ]-// },--// renderHTML({ node, HTMLAttributes }) {-// return [-// 'span',-// mergeAttributes({ 'data-type': this.name }, this.options.HTMLAttributes, HTMLAttributes),-// this.options.renderLabel({-// options: this.options,-// node,-// }),-// ]-// },--// renderText({ node }) {-// return this.options.renderLabel({-// options: this.options,-// node,-// })-// },--// addKeyboardShortcuts() {-// return {-// Backspace: () => this.editor.commands.command(({ tr, state }) => {-// let isMention = false-// const { selection } = state-// const { empty, anchor } = selection--// if (!empty) {-// return false-// }--// state.doc.nodesBetween(anchor - 1, anchor, (node, pos) => {-// if (node.type.name === this.name) {-// isMention = true-// tr.insertText(this.options.suggestion.char || '', pos, pos + node.nodeSize)--// return false-// }-// })--// return isMention-// }),-// }-// },--// addProseMirrorPlugins() {-// return [-// Suggestion({-// editor: this.editor,-// ...this.options.suggestion,-// }),-// ]-// },-// })\ No newline at end of filediff --git a/editor/extensions/contextAutocomplete.ts b/editor/extensions/contextAutocomplete.tsindex e30e032dc..c919816e5 100644--- a/editor/extensions/contextAutocomplete.ts+++ b/editor/extensions/contextAutocomplete.ts@@ -10,7 +10,7 @@ export default Command(ContextAutocomplete)}).configure({suggestion: {- char: '/',startOfLine: false,+ char: '/',},})diff --git a/editor/extensions/promptContext.ts b/editor/extensions/promptContext.tsnew file mode 100644index 000000000..fe6e06072--- /dev/null+++ b/editor/extensions/promptContext.ts@@ -0,0 +1,181 @@+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'++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,+ }+ }+}++export const promptContextItems: PromptContext[] = [+ {+ type: PromptContextType.NPMPackage,+ value: '@slack/web-api',+ },+ {+ type: PromptContextType.DEPLOYMENT,+ value: 'AWS Lambda',+ },+]++export type MentionOptions = {+ HTMLAttributes: Record<string, any>+ renderLabel: (props: { options: MentionOptions; node: ProseMirrorNode }) => string+ suggestion: Omit<SuggestionOptions, 'editor'>+}++export const PromptContextPluginKey = new PluginKey('promptContext')++export default Node.create<MentionOptions>({+ name: 'promptContext',++ addOptions() {+ return {+ HTMLAttributes: {},+ renderLabel({ options, node }) {+ return `${node.attrs.value}`+ },+ suggestion: {+ pluginKey: PromptContextPluginKey,+ command: ({ editor, range, props }) => {+ // 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+ const overrideSpace = nodeAfter?.text?.startsWith(' ')++ if (overrideSpace) {+ range.to += 1+ }++ editor+ .chain()+ .focus()+ .insertContentAt(range, [+ {+ type: this.name,+ attrs: props,+ },+ {+ type: 'text',+ text: ' ',+ },+ ])+ .run()++ window.getSelection()?.collapseToEnd()+ },+ allow: ({ range, editor }) => {+ const state = editor.state+ const $from = state.doc.resolve(range.from)+ const type = state.schema.nodes[this.name]+ const allow = !!$from.parent.type.contentMatch.matchType(type)+ return allow+ },+ },+ }+ },++ group: 'inline',+ inline: true,+ selectable: false,+ atom: true,++ addAttributes() {+ return {+ ...attributeHandler({ htmlPrefix: 'value', nodeAttribute: 'value' }),+ ...attributeHandler({ htmlPrefix: 'type', nodeAttribute: 'type' }),+ }+ },++ parseHTML() {+ return [+ {+ tag: `span[data-type="${this.name}"]`,+ },+ ]+ },++ renderHTML({ node, HTMLAttributes }) {+ return [+ 'span',+ mergeAttributes({ 'data-type': this.name }, this.options.HTMLAttributes, HTMLAttributes),+ this.options.renderLabel({+ options: this.options,+ node,+ }),+ ]+ },++ renderText({ node }) {+ return this.options.renderLabel({+ options: this.options,+ node,+ })+ },++ addKeyboardShortcuts() {+ return {+ Backspace: () => this.editor.commands.command(({ tr, state }) => {+ let isMention = false+ const { selection } = state+ const { empty, anchor } = selection++ if (!empty) {+ return false+ }++ state.doc.nodesBetween(anchor - 1, anchor, (node, pos) => {+ if (node.type.name === this.name) {+ isMention = true+ tr.insertText(this.options.suggestion.char || '', pos, pos + node.nodeSize)++ return false+ }+ })++ return isMention+ }),+ }+ },++ addProseMirrorPlugins() {+ return [+ Suggestion({+ editor: this.editor,+ ...this.options.suggestion,+ }),+ ]+ },+}).extend({+ addCommands() {+ return {+ setPromptContext: (context) => ({ commands }) => {+ return commands.insertContent([+ {+ type: this.name,+ attrs: {+ ['type']: context.type,+ ['value']: context.value,+ },+ },+ ])+ },+ }+ },+})diff --git a/hooks/useDocEditor.ts b/hooks/useDocEditor.tsindex bc9c1d687..e5fe7db21 100644--- a/hooks/useDocEditor.ts+++ b/hooks/useDocEditor.ts@@ -13,6 +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'const extensions = [StarterKit.configure({@@ -38,8 +39,10 @@ const extensions = [ListItem,BulletList,ContextAutocomplete,+ PromptContext,]+const schema = getSchema(extensions)const serializer = new MarkdownSerializer({@@ -104,6 +107,7 @@ function useDocEditor({instance.on('transaction', t => {if (t.transaction.docChanged) {+ console.log(t.editor.getHTML())onContentChange(t.editor.getHTML())}})diff --git a/styles/global.css b/styles/global.cssindex 490c25644..1f56f682c 100644--- a/styles/global.css+++ b/styles/global.css@@ -1,5 +1,4 @@@import 'highlight.js/styles/a11y-dark.css';-@import 'tippy.js/dist/tippy.css';@import './prosemirror.css';diff --git a/styles/prosemirror.css b/styles/prosemirror.cssindex c687fbf6d..4f7d2bd1b 100644--- a/styles/prosemirror.css+++ b/styles/prosemirror.css@@ -1,5 +1,5 @@.ProseMirror {- @apply prose prose-base text-slate-600;+ @apply prose prose-base text-slate-600 whitespace-pre-wrap;/** NOTE: Disabling overflow anchor fixes a code cell scrolling bug* that happens in Chrome: