diff --git a/ee/tabby-ui/app/search/components/assistant-message-section.tsx b/ee/tabby-ui/app/search/components/assistant-message-section.tsx index 6d5e4f056ead..6db85ebf5600 100644 --- a/ee/tabby-ui/app/search/components/assistant-message-section.tsx +++ b/ee/tabby-ui/app/search/components/assistant-message-section.tsx @@ -153,16 +153,9 @@ export function AssistantMessageSection({ if (!clientCode?.length) return [] return ( clientCode.map(code => { - const range = getRangeFromAttachmentCode(code) - return { kind: 'file', - range: range - ? { - start: range.startLine, - end: range.endLine - } - : undefined, + range: getRangeFromAttachmentCode(code), filepath: code.filepath || '', content: code.content, git_url: relevantCodeGitURL @@ -174,16 +167,9 @@ export function AssistantMessageSection({ const serverCodeContexts: RelevantCodeContext[] = useMemo(() => { return ( message?.attachment?.code?.map(code => { - const range = getRangeFromAttachmentCode(code) - return { kind: 'file', - range: range - ? { - start: range.startLine, - end: range.endLine - } - : undefined, + range: getRangeFromAttachmentCode(code), filepath: code.filepath, content: code.content, git_url: code.gitUrl, @@ -254,12 +240,7 @@ export function AssistantMessageSection({ searchParams.append('redirect_git_url', code.gitUrl) url.search = searchParams.toString() - const lineHash = range - ? formatLineHashForCodeBrowser({ - start: range.startLine, - end: range.endLine - }) - : undefined + const lineHash = range ? formatLineHashForCodeBrowser(range) : undefined if (lineHash) { url.hash = lineHash } diff --git a/ee/tabby-ui/components/chat/question-answer.tsx b/ee/tabby-ui/components/chat/question-answer.tsx index 2b583a73d804..ea2088bfd222 100644 --- a/ee/tabby-ui/components/chat/question-answer.tsx +++ b/ee/tabby-ui/components/chat/question-answer.tsx @@ -272,22 +272,13 @@ function AssistantMessageCard(props: AssistantMessageCardProps) { React.useState(undefined) const serverCode: Array = React.useMemo(() => { return ( - message?.relevant_code?.map(code => { - const range = getRangeFromAttachmentCode(code) - - return { - kind: 'file', - range: range - ? { - start: range.startLine, - end: range.endLine - } - : undefined, - filepath: code.filepath, - content: code.content, - git_url: code.gitUrl - } - }) ?? [] + message?.relevant_code?.map(code => ({ + kind: 'file', + range: getRangeFromAttachmentCode(code), + filepath: code.filepath, + content: code.content, + git_url: code.gitUrl + })) ?? [] ) }, [message?.relevant_code]) @@ -343,18 +334,12 @@ function AssistantMessageCard(props: AssistantMessageCardProps) { } const onCodeCitationClick = (code: AttachmentCodeItem) => { - const range = getRangeFromAttachmentCode(code) const ctx: Context = { git_url: code.gitUrl, content: code.content, filepath: code.filepath, kind: 'file', - range: range - ? { - start: range.startLine, - end: range.endLine - } - : undefined + range: getRangeFromAttachmentCode(code) } onNavigateToContext?.(ctx, { openInEditor: code.isClient diff --git a/ee/tabby-ui/lib/utils/index.ts b/ee/tabby-ui/lib/utils/index.ts index a596ad09dffa..938068fe15a8 100644 --- a/ee/tabby-ui/lib/utils/index.ts +++ b/ee/tabby-ui/lib/utils/index.ts @@ -1,6 +1,7 @@ import { clsx, type ClassValue } from 'clsx' import { compact, isNil } from 'lodash-es' import { customAlphabet } from 'nanoid' +import { LineRange } from 'tabby-chat-panel/index' import { twMerge } from 'tailwind-merge' import { AttachmentCodeItem, AttachmentDocItem } from '@/lib/types' @@ -110,35 +111,22 @@ export function formatLineHashForCodeBrowser( export function getRangeFromAttachmentCode(code: { startLine?: Maybe content: string -}): - | { - startLine: number - endLine: number - isMultiLine: boolean - } - | undefined { +}): LineRange | undefined { if (!code?.startLine) return undefined - const startLine = code.startLine + const start = code.startLine const lineCount = code.content.split('\n').length - const endLine = typeof startLine === 'number' ? startLine + lineCount - 1 : 0 + const end = typeof start === 'number' ? start + lineCount - 1 : 0 return { - // lineRange is 1-based - startLine, - endLine, - isMultiLine: !!startLine && !!endLine && startLine <= endLine + start, + end } } export function getRangeTextFromAttachmentCode(code: AttachmentCodeItem) { const range = getRangeFromAttachmentCode(code) - return range - ? formatLineHashForCodeBrowser({ - start: range.startLine, - end: range.endLine - }) - : '' + return range ? formatLineHashForCodeBrowser(range) : '' } export function getContent(item: AttachmentDocItem) {