Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(ui): add commit hash to attachmentCode for accurate redirection #3627

Merged
merged 1 commit into from
Dec 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 15 additions & 4 deletions ee/tabby-ui/app/files/components/source-code-browser.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,12 @@ const SourceCodeBrowserContextProvider: React.FC<PropsWithChildren> = ({
})
} else {
const setParams: Record<string, string> = {}
let delList = ['redirect_filepath', 'redirect_git_url', 'line']
let delList = [
'redirect_filepath',
'redirect_git_url',
'redirect_rev',
'line'
]
if (options?.plain) {
setParams['plain'] = '1'
} else {
Expand Down Expand Up @@ -501,13 +506,19 @@ const SourceCodeBrowserRenderer: React.FC<SourceCodeBrowserProps> = ({
const repos = await fetchAllRepositories()
const redirect_filepath = searchParams.get('redirect_filepath')
const redirect_git_url = searchParams.get('redirect_git_url')
const redirect_rev = searchParams.get('redirect_rev')

if (repos?.length && redirect_filepath && redirect_git_url) {
const targetRepo = repos.find(repo => repo.gitUrl === redirect_git_url)
if (targetRepo) {
// use default rev
const defaultRef = getDefaultRepoRef(targetRepo.refs)
const refName = resolveRepoRef(defaultRef)?.name || ''
let refName = ''
if (redirect_rev) {
refName = redirect_rev
} else {
// use default rev
const defaultRef = getDefaultRepoRef(targetRepo.refs)
refName = resolveRepoRef(defaultRef)?.name || ''
}

const lineRangeInHash = parseLineNumberFromHash(window.location.hash)
const isValidLineHash = !isNil(lineRangeInHash?.start)
Expand Down
21 changes: 8 additions & 13 deletions ee/tabby-ui/app/search/components/assistant-message-section.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import {
RelevantCodeContext
} from '@/lib/types'
import {
buildCodeBrowserUrlForContext,
cn,
formatLineHashForCodeBrowser,
getContent,
Expand Down Expand Up @@ -172,13 +173,14 @@ export function AssistantMessageSection({
filepath: code.filepath,
content: code.content,
git_url: code.gitUrl,
commit: code.commit ?? undefined,
extra: {
scores: code?.extra?.scores
}
}
}) ?? []
)
}, [clientCode, message?.attachment?.code])
}, [message?.attachment?.code])

const messageAttachmentClientCode = useMemo(() => {
return clientCode?.map(o => ({
Expand All @@ -200,18 +202,8 @@ export function AssistantMessageSection({

const onCodeContextClick = (ctx: Context) => {
if (!ctx.filepath) return
const url = new URL(`${window.location.origin}/files`)
const searchParams = new URLSearchParams()
searchParams.append('redirect_filepath', ctx.filepath)
searchParams.append('redirect_git_url', ctx.git_url)
url.search = searchParams.toString()

const lineHash = formatLineHashForCodeBrowser(ctx.range)
if (lineHash) {
url.hash = lineHash
}

window.open(url.toString())
const url = buildCodeBrowserUrlForContext(window.location.origin, ctx)
window.open(url, '_blank')
}

const onCodeCitationMouseEnter = (index: number) => {
Expand All @@ -232,6 +224,9 @@ export function AssistantMessageSection({
const searchParams = new URLSearchParams()
searchParams.append('redirect_filepath', code.filepath)
searchParams.append('redirect_git_url', code.gitUrl)
if (code.commit) {
searchParams.append('redirect_rev', code.commit)
}
url.search = searchParams.toString()

const lineHash = formatLineHashForCodeBrowser(range)
Expand Down
3 changes: 2 additions & 1 deletion ee/tabby-ui/components/chat/question-answer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,8 @@ function AssistantMessageCard(props: AssistantMessageCardProps) {
range: getRangeFromAttachmentCode(code),
filepath: code.filepath,
content: code.content,
git_url: code.gitUrl
git_url: code.gitUrl,
commit: code.commit ?? undefined
})) ?? []
)
}, [message?.relevant_code])
Expand Down
2 changes: 2 additions & 0 deletions ee/tabby-ui/lib/hooks/use-thread-run.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ const CreateThreadAndRunSubscription = graphql(/* GraphQL */ `
hits {
code {
gitUrl
commit
filepath
language
content
Expand Down Expand Up @@ -118,6 +119,7 @@ const CreateThreadRunSubscription = graphql(/* GraphQL */ `
hits {
code {
gitUrl
commit
filepath
language
content
Expand Down
1 change: 1 addition & 0 deletions ee/tabby-ui/lib/tabby/query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -402,6 +402,7 @@ export const listThreadMessages = graphql(/* GraphQL */ `
attachment {
code {
gitUrl
commit
filepath
language
content
Expand Down
1 change: 1 addition & 0 deletions ee/tabby-ui/lib/types/chat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ export interface FileContext {
range?: { start: number; end: number }
content: string
git_url: string
commit?: string
}

export type Context = FileContext
Expand Down
4 changes: 4 additions & 0 deletions ee/tabby-ui/lib/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,10 @@ export function buildCodeBrowserUrlForContext(
const searchParams = new URLSearchParams()
searchParams.append('redirect_filepath', context.filepath)
searchParams.append('redirect_git_url', context.git_url)
if (context.commit) {
searchParams.append('redirect_rev', context.commit)
}

url.search = searchParams.toString()

url.hash = formatLineHashForCodeBrowser(context.range)
Expand Down
Loading