From 34cef2b370ac13844b56cf94b271ba4cdfdee644 Mon Sep 17 00:00:00 2001 From: liangfung <1098486429@qq.com> Date: Mon, 1 Apr 2024 18:52:49 +0800 Subject: [PATCH] iframe --- .../action-bar-widget-extension.tsx | 7 +- .../app/files/components/completion-panel.tsx | 151 +++++++----------- .../components/file-directory-breadcrumb.tsx | 64 ++++---- .../files/components/source-code-browser.tsx | 53 ++++-- ee/tabby-ui/app/files/lib/event-emitter.ts | 2 +- .../app/playground/components/chats.tsx | 31 +++- ee/tabby-ui/components/chat-panel.tsx | 23 ++- ee/tabby-ui/components/chat.tsx | 1 + 8 files changed, 182 insertions(+), 150 deletions(-) diff --git a/ee/tabby-ui/app/files/components/action-bar-widget/action-bar-widget-extension.tsx b/ee/tabby-ui/app/files/components/action-bar-widget/action-bar-widget-extension.tsx index 747ed4f2e7fe..0b83af12bebf 100644 --- a/ee/tabby-ui/app/files/components/action-bar-widget/action-bar-widget-extension.tsx +++ b/ee/tabby-ui/app/files/components/action-bar-widget/action-bar-widget-extension.tsx @@ -41,10 +41,9 @@ function createActionBarWidget(state: EditorState): Tooltip { const lineTo = state.doc.lineAt(selection.main.to) const isMultiline = lineFrom.number !== lineTo.number const pos = isMultiline ? lineTo.from : selection.main.from - const text = state.doc.sliceString( - state.selection.main.from, - state.selection.main.to - ) || '' + const text = + state.doc.sliceString(state.selection.main.from, state.selection.main.to) || + '' return { pos, diff --git a/ee/tabby-ui/app/files/components/completion-panel.tsx b/ee/tabby-ui/app/files/components/completion-panel.tsx index d1550539a749..7b697a0850cd 100644 --- a/ee/tabby-ui/app/files/components/completion-panel.tsx +++ b/ee/tabby-ui/app/files/components/completion-panel.tsx @@ -28,19 +28,14 @@ import { import { Chat, ChatRef } from '@/components/chat' import { ClearChatsButton } from '@/components/clear-chats-button' import { EditChatTitleDialog } from '@/components/edit-chat-title-dialog' +import LoadingWrapper from '@/components/loading-wrapper' import { ListSkeleton } from '@/components/skeleton' -import { SourceCodeBrowserContext } from './source-code-browser' -import LoadingWrapper from '@/components/loading-wrapper' import { CodeBrowserQuickAction } from '../lib/event-emitter' +import { SourceCodeBrowserContext } from './source-code-browser' interface CompletionPanelProps - extends Omit, 'children'> { - // open: boolean - // onOpenChange: (v: boolean) => void -} - -const emptyMessages: Message[] = [] + extends Omit, 'children'> {} enum CompletionPanelView { CHAT, @@ -57,47 +52,20 @@ export const CompletionPanel: React.FC = ({ setPendingEvent, setCompletionPanelViewType } = React.useContext(SourceCodeBrowserContext) - const _hasHydrated = useStore(useChatStore, state => state._hasHydrated) const chats = useStore(useChatStore, state => state.chats) const activeChatId = useStore(useChatStore, state => state.activeChatId) const chatId = activeChatId const chat = getChatById(chats, chatId) - const chatRef = React.useRef(null) const appending = React.useRef(false) - - // const quickActionBarCallback = (action: CodeBrowserQuickAction) => { - // let builtInPrompt = '' - // switch (action) { - // case 'explain': - // builtInPrompt = 'Explain the following code:' - // break - // case 'generate_unittest': - // builtInPrompt = 'Generate a unit test for the following code:' - // break - // case 'generate_doc': - // builtInPrompt = 'Generate documentation for the following code:' - // break - // default: - // break - // } - // const view = editorRef.current?.editorView - // const text = - // view?.state.doc.sliceString( - // view?.state.selection.main.from, - // view?.state.selection.main.to - // ) || '' - - // const initialMessage = `${builtInPrompt}\n${'```'}${ - // language ?? '' - // }\n${text}\n${'```'}\n` - // if (initialMessage) { - // window.open( - // `/playground?initialMessage=${encodeURIComponent(initialMessage)}` - // ) - // } - // } - - const getPrompt = ({ action, payload }: { action: CodeBrowserQuickAction, payload: string }) => { + const iframeRef = React.useRef(null) + + const getPrompt = ({ + action, + payload + }: { + action: CodeBrowserQuickAction + payload: string + }) => { let builtInPrompt = '' switch (action) { case 'explain': @@ -116,43 +84,29 @@ export const CompletionPanel: React.FC = ({ } React.useEffect(() => { - if (chatRef.current?.append && pendingEvent) { - setCompletionPanelViewType(CompletionPanelView.CHAT) - if (!appending.current) { - appending.current = true + const contentWindow = iframeRef.current?.contentWindow - const prompt = getPrompt(pendingEvent as any) + if (pendingEvent) { + setCompletionPanelViewType(CompletionPanelView.CHAT) - chatRef.current - ?.append({ - role: 'user', - content: prompt - }) - .then(() => { - setPendingEvent(undefined) - appending.current = false - }) - if (!chat) { - addChat(activeChatId, truncateText(prompt)) - } - } + contentWindow?.postMessage({ + action: 'append', + payload: getPrompt(pendingEvent) + }) + setPendingEvent(undefined) } - }, [pendingEvent, chatRef.current?.append]) + }, [pendingEvent, iframeRef.current?.contentWindow]) return ( -
+
{completionPanelViewType === CompletionPanelView.CHAT && ( - - - +