Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
liangfung committed Dec 23, 2024
1 parent 0226936 commit 6ec7614
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 64 deletions.
25 changes: 3 additions & 22 deletions ee/tabby-ui/app/search/components/assistant-message-section.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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,
Expand Down Expand Up @@ -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
}
Expand Down
31 changes: 8 additions & 23 deletions ee/tabby-ui/components/chat/question-answer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -272,22 +272,13 @@ function AssistantMessageCard(props: AssistantMessageCardProps) {
React.useState<number | undefined>(undefined)
const serverCode: Array<Context> = 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])

Expand Down Expand Up @@ -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
Expand Down
26 changes: 7 additions & 19 deletions ee/tabby-ui/lib/utils/index.ts
Original file line number Diff line number Diff line change
@@ -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'
Expand Down Expand Up @@ -110,35 +111,22 @@ export function formatLineHashForCodeBrowser(
export function getRangeFromAttachmentCode(code: {
startLine?: Maybe<number>
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) {
Expand Down

0 comments on commit 6ec7614

Please sign in to comment.