Skip to content

Commit

Permalink
feat: Rename onNavigateSymbol to onLookupSymbol and update related lo…
Browse files Browse the repository at this point in the history
…gic for consistency across components
  • Loading branch information
Sma1lboy committed Dec 5, 2024
1 parent e4d0428 commit 9be446b
Show file tree
Hide file tree
Showing 8 changed files with 101 additions and 54 deletions.
11 changes: 4 additions & 7 deletions ee/tabby-ui/app/chat/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,7 @@ export default function ChatPage() {
// server feature support check
const [supportsOnApplyInEditorV2, setSupportsOnApplyInEditorV2] =
useState(false)
const [supportsOnNavigateSymbol, setSupportsOnNavigateSymbol] =
useState(false)
const [supportsOnLookupSymbol, setSupportsOnLookupSymbol] = useState(false)

const sendMessage = (message: ChatMessage) => {
if (chatRef.current) {
Expand Down Expand Up @@ -238,9 +237,7 @@ export default function ChatPage() {
server
?.hasCapability('onApplyInEditorV2')
.then(setSupportsOnApplyInEditorV2)
server
?.hasCapability('onNavigateSymbol')
.then(setSupportsOnNavigateSymbol)
server?.hasCapability('onLookupSymbol').then(setSupportsOnLookupSymbol)
}

checkCapabilities()
Expand Down Expand Up @@ -393,9 +390,9 @@ export default function ChatPage() {
: server?.onApplyInEditor)
}
supportsOnApplyInEditorV2={supportsOnApplyInEditorV2}
onNavigateSymbol={
onLookupSymbol={
isInEditor &&
(supportsOnNavigateSymbol ? server?.onNavigateSymbol : undefined)
(supportsOnLookupSymbol ? server?.onLookupSymbol : undefined)
}
/>
</ErrorBoundary>
Expand Down
4 changes: 3 additions & 1 deletion ee/tabby-ui/app/files/components/chat-side-bar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,9 @@ export const ChatSideBar: React.FC<ChatSideBarProps> = ({
onLoaded() {},
onCopy(_content) {},
onKeyboardEvent() {},
onNavigateSymbol(_filepath, _keywords) {}
async onLookupSymbol(_filepath, _keywords) {
return undefined
}
})

const getPrompt = ({ action }: QuickActionEventPayload) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,8 @@ export function AssistantMessageSection({
onUpdateMessage
} = useContext(SearchContext)

const { supportsOnApplyInEditorV2 } = useContext(ChatContext)
const { supportsOnApplyInEditorV2, onNavigateToContext } =
useContext(ChatContext)

const [isEditing, setIsEditing] = useState(false)
const [showMoreSource, setShowMoreSource] = useState(false)
Expand Down Expand Up @@ -332,6 +333,7 @@ export function AssistantMessageSection({
fetchingContextInfo={fetchingContextInfo}
canWrapLongLines={!isLoading}
supportsOnApplyInEditorV2={supportsOnApplyInEditorV2}
onNavigateToContext={onNavigateToContext}
/>
{/* if isEditing, do not display error message block */}
{message.error && <ErrorMessageBlock error={message.error} />}
Expand Down
21 changes: 16 additions & 5 deletions ee/tabby-ui/components/chat/chat.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
import React, { RefObject } from 'react'
import { compact, findIndex, isEqual, some, uniqWith } from 'lodash-es'
import type { Context, FileContext, NavigateOpts } from 'tabby-chat-panel'
import type {
Context,
FileContext,
NavigateOpts,
SymbolInfo
} from 'tabby-chat-panel'

import { ERROR_CODE_NOT_FOUND } from '@/lib/constants'
import {
Expand Down Expand Up @@ -45,7 +50,10 @@ type ChatContextValue = {
onApplyInEditor?:
| ((content: string) => void)
| ((content: string, opts?: { languageId: string; smart: boolean }) => void)
onNavigateSymbol?: (filepaths: string[], keyword: string) => void
onLookupSymbol?: (
filepaths: string[],
keyword: string
) => Promise<SymbolInfo | undefined>
relevantContext: Context[]
activeSelection: Context | null
removeRelevantContext: (index: number) => void
Expand Down Expand Up @@ -84,7 +92,10 @@ interface ChatProps extends React.ComponentProps<'div'> {
onApplyInEditor?:
| ((content: string) => void)
| ((content: string, opts?: { languageId: string; smart: boolean }) => void)
onNavigateSymbol?: (filepaths: string[], keyword: string) => void
onLookupSymbol?: (
filepaths: string[],
keyword: string
) => Promise<SymbolInfo | undefined>
chatInputRef: RefObject<HTMLTextAreaElement>
supportsOnApplyInEditorV2: boolean
}
Expand All @@ -106,7 +117,7 @@ function ChatRenderer(
onCopyContent,
onSubmitMessage,
onApplyInEditor,
onNavigateSymbol,
onLookupSymbol,
chatInputRef,
supportsOnApplyInEditorV2
}: ChatProps,
Expand Down Expand Up @@ -532,7 +543,7 @@ function ChatRenderer(
container,
onCopyContent,
onApplyInEditor,
onNavigateSymbol,
onLookupSymbol,
relevantContext,
removeRelevantContext,
chatInputRef,
Expand Down
5 changes: 3 additions & 2 deletions ee/tabby-ui/components/chat/question-answer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,7 @@ function AssistantMessageCard(props: AssistantMessageCardProps) {
onNavigateToContext,
onApplyInEditor,
onCopyContent,
onNavigateSymbol,
onLookupSymbol,
supportsOnApplyInEditorV2
} = React.useContext(ChatContext)
const [relevantCodeHighlightIndex, setRelevantCodeHighlightIndex] =
Expand Down Expand Up @@ -399,9 +399,10 @@ function AssistantMessageCard(props: AssistantMessageCardProps) {
onCodeCitationMouseEnter={onCodeCitationMouseEnter}
onCodeCitationMouseLeave={onCodeCitationMouseLeave}
canWrapLongLines={!isLoading}
onNavigateSymbol={onNavigateSymbol}
onLookupSymbol={onLookupSymbol}
supportsOnApplyInEditorV2={supportsOnApplyInEditorV2}
activeSelection={userMessage.activeContext}
onNavigateToContext={onNavigateToContext}
/>
{!!message.error && <ErrorMessageBlock error={message.error} />}
</>
Expand Down
76 changes: 45 additions & 31 deletions ee/tabby-ui/components/message-markdown/code.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { ReactNode, useContext } from 'react'
import { ReactNode, useContext, useEffect, useState } from 'react'
import { Element } from 'react-markdown/lib/ast-to-react'
import { SymbolInfo } from 'tabby-chat-panel/index'

import { cn } from '@/lib/utils'

Expand All @@ -12,6 +13,7 @@ export interface CodeElementProps {
className?: string
children: ReactNode & ReactNode[]
}

/**
* Code element in Markdown AST.
*/
Expand All @@ -22,51 +24,63 @@ export function CodeElement({
...props
}: CodeElementProps) {
const {
onNavigateSymbol,
onLookupSymbol,
canWrapLongLines,
onApplyInEditor,
onCopyContent,
supportsOnApplyInEditorV2,
activeSelection
activeSelection,
onNavigateToContext
} = useContext(MessageMarkdownContext)

const [symbolLocation, setSymbolLocation] = useState<SymbolInfo | undefined>(
undefined
)

const keyword = children[0]?.toString()

useEffect(() => {
const lookupSymbol = async () => {
if (!inline || !onLookupSymbol || !keyword) return

const symbolInfo = await onLookupSymbol(
activeSelection?.filepath ? [activeSelection?.filepath] : [],
keyword
)
setSymbolLocation(symbolInfo)
}

lookupSymbol()
}, [inline, keyword, onLookupSymbol, activeSelection?.filepath])

if (children.length) {
if (children[0] === '▍') {
return <span className="mt-1 animate-pulse cursor-default"></span>
}
children[0] = (children[0] as string).replace('`▍`', '▍')
}

const match = /language-(\w+)/.exec(className || '')

if (inline) {
if (!onNavigateSymbol) {
return (
<code className={className} {...props}>
{children}
</code>
)
}

const keyword = children[0]?.toString()
if (!keyword) {
return (
<code className={className} {...props}>
{children}
</code>
)
}

const isClickable = Boolean(canWrapLongLines)
const isClickable = Boolean(symbolLocation)

const handleClick = () => {
if (!isClickable) return
if (onNavigateSymbol) {
onNavigateSymbol(
activeSelection?.filepath ? [activeSelection?.filepath] : [],
keyword
)
}
if (!isClickable || !symbolLocation || !onNavigateToContext) return

onNavigateToContext(
{
filepath: symbolLocation.targetFile,
range: {
start: symbolLocation.targetLine,
end: symbolLocation.targetLine
},
git_url: '',
content: '',
kind: 'file'
},
{
openInEditor: true
}
)
}

return (
Expand All @@ -84,7 +98,7 @@ export function CodeElement({
</code>
)
}

const match = /language-(\w+)/.exec(className || '')
return (
<CodeBlock
key={Math.random()}
Expand Down
21 changes: 16 additions & 5 deletions ee/tabby-ui/components/message-markdown/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,12 @@ import { MemoizedReactMarkdown } from '@/components/markdown'

import './style.css'

import { FileContext } from 'tabby-chat-panel/index'
import {
Context,
FileContext,
NavigateOpts,
SymbolInfo
} from 'tabby-chat-panel/index'

import {
MARKDOWN_CITATION_REGEX,
Expand Down Expand Up @@ -73,7 +78,11 @@ export interface MessageMarkdownProps {
content: string,
opts?: { languageId: string; smart: boolean }
) => void
onNavigateSymbol?: (filepaths: string[], keyword: string) => void
onLookupSymbol?: (
filepaths: string[],
keyword: string
) => Promise<SymbolInfo | undefined>
onNavigateToContext?: (context: Context, opts?: NavigateOpts) => void
onCodeCitationClick?: (code: AttachmentCodeItem) => void
onCodeCitationMouseEnter?: (index: number) => void
onCodeCitationMouseLeave?: (index: number) => void
Expand All @@ -97,9 +106,10 @@ export function MessageMarkdown({
fetchingContextInfo,
className,
canWrapLongLines,
onNavigateSymbol,
onLookupSymbol,
supportsOnApplyInEditorV2,
activeSelection,
onNavigateToContext,
...rest
}: MessageMarkdownProps) {
const messageAttachments: MessageAttachments = useMemo(() => {
Expand Down Expand Up @@ -176,8 +186,9 @@ export function MessageMarkdown({
fetchingContextInfo: !!fetchingContextInfo,
canWrapLongLines: !!canWrapLongLines,
supportsOnApplyInEditorV2,
onNavigateSymbol,
activeSelection
onLookupSymbol,
activeSelection,
onNavigateToContext
}}
>
<MemoizedReactMarkdown
Expand Down
13 changes: 11 additions & 2 deletions ee/tabby-ui/components/message-markdown/markdown-context.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
import { createContext } from 'react'
import { FileContext } from 'tabby-chat-panel/index'
import {
Context,
FileContext,
NavigateOpts,
SymbolInfo
} from 'tabby-chat-panel/index'

import { ContextInfo } from '@/lib/gql/generates/graphql'
import { AttachmentCodeItem } from '@/lib/types'
Expand All @@ -16,7 +21,11 @@ export type MessageMarkdownContextValue = {
contextInfo: ContextInfo | undefined
fetchingContextInfo: boolean
canWrapLongLines: boolean
onNavigateSymbol?: (filepaths: string[], keyword: string) => void
onLookupSymbol?: (
filepaths: string[],
keyword: string
) => Promise<SymbolInfo | undefined>
onNavigateToContext?: (context: Context, opts?: NavigateOpts) => void
supportsOnApplyInEditorV2: boolean
activeSelection?: FileContext
}
Expand Down

0 comments on commit 9be446b

Please sign in to comment.