Skip to content

Commit

Permalink
feat(chat-ui): add smart apply functionality to chat panel
Browse files Browse the repository at this point in the history
  • Loading branch information
Sma1lboy committed Sep 10, 2024
1 parent 8d83f30 commit 7a7b3ed
Show file tree
Hide file tree
Showing 9 changed files with 64 additions and 5 deletions.
3 changes: 3 additions & 0 deletions clients/tabby-chat-panel/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,9 @@ export interface ClientApi {

onApplyInEditor: (content: string) => void

// smart apply into editor
onSmartApplyInEditor: (content: string) => void

// On current page is loaded.
onLoaded: () => void

Expand Down
4 changes: 4 additions & 0 deletions clients/vscode/src/chat/ChatViewProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import type { ServerInfo } from "tabby-agent";
import type { AgentFeature as Agent } from "../lsp/AgentFeature";
import { createClient } from "./chatPanel";
import { GitProvider } from "../git/GitProvider";
import { getLogger } from "../logger";

export class ChatViewProvider implements WebviewViewProvider {
webview?: WebviewView;
Expand Down Expand Up @@ -203,6 +204,9 @@ export class ChatViewProvider implements WebviewViewProvider {
});
}
},
onSmartApplyInEditor: (content: string) => {
getLogger("Tabby").info("Smart apply in editor is not implemented yet.", content);
},
onLoaded: () => {
setTimeout(() => {
this.refreshChatPage();
Expand Down
1 change: 1 addition & 0 deletions clients/vscode/src/chat/chatPanel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ export function createClient(webview: WebviewView, api: ClientApi): ServerApi {
onApplyInEditor: api.onApplyInEditor,
onCopy: api.onCopy,
onLoaded: api.onLoaded,
onSmartApplyInEditor: api.onSmartApplyInEditor,
},
});
}
1 change: 1 addition & 0 deletions ee/tabby-ui/app/chat/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,7 @@ export default function ChatPage() {
onCopyContent={isInEditor && server?.onCopy}
onSubmitMessage={isInEditor && server?.onSubmitMessage}
onApplyInEditor={isInEditor && server?.onApplyInEditor}
onSmartApplyInEditor={isInEditor && server?.onSmartApplyInEditor}
/>
</ErrorBoundary>
)
Expand Down
6 changes: 5 additions & 1 deletion ee/tabby-ui/components/chat/chat.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ type ChatContextValue = {
container?: HTMLDivElement
onCopyContent?: (value: string) => void
onApplyInEditor?: (value: string) => void
onSmartApplyInEditor?: (value: string) => void
relevantContext: Context[]
removeRelevantContext: (index: number) => void
}
Expand Down Expand Up @@ -72,6 +73,7 @@ interface ChatProps extends React.ComponentProps<'div'> {
onCopyContent?: (value: string) => void
onSubmitMessage?: (msg: string, relevantContext?: Context[]) => Promise<void>
onApplyInEditor?: (value: string) => void
onSmartApplyInEditor?: (value: string) => void
}

function ChatRenderer(
Expand All @@ -90,7 +92,8 @@ function ChatRenderer(
promptFormClassname,
onCopyContent,
onSubmitMessage,
onApplyInEditor
onApplyInEditor,
onSmartApplyInEditor
}: ChatProps,
ref: React.ForwardedRef<ChatRef>
) {
Expand Down Expand Up @@ -442,6 +445,7 @@ function ChatRenderer(
container,
onCopyContent,
onApplyInEditor,
onSmartApplyInEditor,
relevantContext,
removeRelevantContext
}}
Expand Down
9 changes: 7 additions & 2 deletions ee/tabby-ui/components/chat/question-answer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -233,8 +233,12 @@ function AssistantMessageCard(props: AssistantMessageCardProps) {
enableRegenerating,
...rest
} = props
const { onNavigateToContext, onApplyInEditor, onCopyContent } =
React.useContext(ChatContext)
const {
onNavigateToContext,
onApplyInEditor,
onSmartApplyInEditor,
onCopyContent
} = React.useContext(ChatContext)
const [relevantCodeHighlightIndex, setRelevantCodeHighlightIndex] =
React.useState<number | undefined>(undefined)
const serverCode: Array<Context> = React.useMemo(() => {
Expand Down Expand Up @@ -363,6 +367,7 @@ function AssistantMessageCard(props: AssistantMessageCardProps) {
<MessageMarkdown
message={message.message}
onApplyInEditor={onApplyInEditor}
onSmartApplyInEditor={onSmartApplyInEditor}
onCopyContent={onCopyContent}
attachmentCode={attachmentCode}
onCodeCitationClick={onCodeCitationClick}
Expand Down
3 changes: 3 additions & 0 deletions ee/tabby-ui/components/message-markdown/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ export interface MessageMarkdownProps {
attachmentCode?: Maybe<Array<AttachmentCodeItem>>
onCopyContent?: ((value: string) => void) | undefined
onApplyInEditor?: ((value: string) => void) | undefined
onSmartApplyInEditor?: ((value: string) => void) | undefined
onCodeCitationClick?: (code: MessageAttachmentCode) => void
onCodeCitationMouseEnter?: (index: number) => void
onCodeCitationMouseLeave?: (index: number) => void
Expand Down Expand Up @@ -93,6 +94,7 @@ export function MessageMarkdown({
attachmentDocs,
attachmentCode,
onApplyInEditor,
onSmartApplyInEditor,
onCopyContent,
contextInfo,
fetchingContextInfo,
Expand Down Expand Up @@ -236,6 +238,7 @@ export function MessageMarkdown({
language={(match && match[1]) || ''}
value={String(children).replace(/\n$/, '')}
onApplyInEditor={onApplyInEditor}
onSmartApplyInEditor={onSmartApplyInEditor}
onCopyContent={onCopyContent}
{...props}
/>
Expand Down
34 changes: 32 additions & 2 deletions ee/tabby-ui/components/ui/codeblock.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,12 @@ import { coldarkDark } from 'react-syntax-highlighter/dist/cjs/styles/prism'

import { useCopyToClipboard } from '@/lib/hooks/use-copy-to-clipboard'
import { Button } from '@/components/ui/button'
import { IconApplyInEditor, IconCheck, IconCopy } from '@/components/ui/icons'
import {
IconApplyInEditor,
IconCheck,
IconCopy,
IconSmartApplyInEditor
} from '@/components/ui/icons'
import {
Tooltip,
TooltipContent,
Expand All @@ -21,6 +26,7 @@ interface Props {
value: string
onCopyContent?: (value: string) => void
onApplyInEditor?: (value: string) => void
onSmartApplyInEditor?: (value: string) => void
}

interface languageMap {
Expand Down Expand Up @@ -64,7 +70,13 @@ export const generateRandomString = (length: number, lowercase = false) => {
}

const CodeBlock: FC<Props> = memo(
({ language, value, onCopyContent, onApplyInEditor }) => {
({
language,
value,
onCopyContent,
onApplyInEditor,
onSmartApplyInEditor
}) => {
const { isCopied, copyToClipboard } = useCopyToClipboard({
timeout: 2000,
onCopyContent
Expand All @@ -83,6 +95,24 @@ const CodeBlock: FC<Props> = memo(
<div className="flex w-full items-center justify-between bg-zinc-800 px-6 py-2 pr-4 text-zinc-100">
<span className="text-xs lowercase">{language}</span>
<div className="flex items-center space-x-1">
{onSmartApplyInEditor && (
<Tooltip>
<TooltipTrigger asChild>
<Button
variant="ghost"
size="icon"
className="text-xs hover:bg-[#3C382F] hover:text-[#F4F4F5] focus-visible:ring-1 focus-visible:ring-slate-700 focus-visible:ring-offset-0"
onClick={() => onSmartApplyInEditor(value)}
>
<IconSmartApplyInEditor />
<span className="sr-only">Smart Apply in Editor</span>
</Button>
</TooltipTrigger>
<TooltipContent>
<p className="m-0">Smart Apply in Editor</p>
</TooltipContent>
</Tooltip>
)}
{onApplyInEditor && (
<Tooltip>
<TooltipTrigger asChild>
Expand Down
8 changes: 8 additions & 0 deletions ee/tabby-ui/components/ui/icons.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -1523,6 +1523,13 @@ function IconApplyInEditor({
return <IndentIncrease className={cn('h-4 w-4', className)} {...props} />
}

function IconSmartApplyInEditor({
className,
...props
}: React.ComponentProps<typeof Sparkles>) {
return <Sparkles className={cn('h-4 w-4', className)} {...props} />
}

function IconBug({ className, ...props }: React.ComponentProps<typeof Bug>) {
return <Bug className={cn('h-4 w-4', className)} {...props} />
}
Expand Down Expand Up @@ -1682,6 +1689,7 @@ export {
IconTag,
IconFileText,
IconApplyInEditor,
IconSmartApplyInEditor,
IconBug,
IconFilter,
IconRemove,
Expand Down

0 comments on commit 7a7b3ed

Please sign in to comment.