-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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): fill repository context into chat sidebar. #1950
Closed
Closed
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
c5550de
feat(ui): fill repository context into chat sidebar.
wsxiaoys 4371be5
update
wsxiaoys 0b93f24
update
wsxiaoys c68dc7c
update
wsxiaoys 992163b
update
wsxiaoys a6e6f88
fix
wsxiaoys 380720f
update
wsxiaoys File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -2,6 +2,8 @@ import React from 'react' | |||||
|
||||||
import { useStore } from '@/lib/hooks/use-store' | ||||||
import { useChatStore } from '@/lib/stores/chat-store' | ||||||
import fetcher from '@/lib/tabby/fetcher' | ||||||
import { ISearchHit, SearchReponse } from '@/lib/types' | ||||||
import { cn } from '@/lib/utils' | ||||||
import { Button } from '@/components/ui/button' | ||||||
import { IconClose } from '@/components/ui/icons' | ||||||
|
@@ -22,14 +24,15 @@ export const ChatSideBar: React.FC<ChatSideBarProps> = ({ | |||||
const activeChatId = useStore(useChatStore, state => state.activeChatId) | ||||||
const iframeRef = React.useRef<HTMLIFrameElement>(null) | ||||||
|
||||||
const getPrompt = ({ | ||||||
const getPrompt = async ({ | ||||||
action, | ||||||
code, | ||||||
language, | ||||||
path, | ||||||
lineFrom, | ||||||
lineTo | ||||||
}: QuickActionEventPayload) => { | ||||||
const contextPrompt = await buildContextPrompt(language, code, path) | ||||||
let builtInPrompt = '' | ||||||
switch (action) { | ||||||
case 'explain': | ||||||
|
@@ -47,18 +50,22 @@ export const ChatSideBar: React.FC<ChatSideBarProps> = ({ | |||||
const codeBlockMeta = `${ | ||||||
language ?? '' | ||||||
} is_reference=1 path=${path} line_from=${lineFrom} line_to=${lineTo}` | ||||||
return `${builtInPrompt}\n${'```'}${codeBlockMeta}\n${code}\n${'```'}\n` | ||||||
return `${contextPrompt}${builtInPrompt}\n${'```'}${codeBlockMeta}\n${code}\n${'```'}\n` | ||||||
} | ||||||
|
||||||
React.useEffect(() => { | ||||||
async function postPrompt(e: QuickActionEventPayload) { | ||||||
const contentWindow = iframeRef.current?.contentWindow | ||||||
contentWindow?.postMessage({ | ||||||
action: 'append', | ||||||
payload: await getPrompt(e) | ||||||
}) | ||||||
} | ||||||
|
||||||
React.useEffect(() => { | ||||||
if (pendingEvent) { | ||||||
contentWindow?.postMessage({ | ||||||
action: 'append', | ||||||
payload: getPrompt(pendingEvent) | ||||||
postPrompt(pendingEvent).then(() => { | ||||||
setPendingEvent(undefined) | ||||||
}) | ||||||
setPendingEvent(undefined) | ||||||
} | ||||||
}, [pendingEvent, iframeRef.current?.contentWindow]) | ||||||
|
||||||
|
@@ -90,3 +97,74 @@ function Header() { | |||||
</div> | ||||||
) | ||||||
} | ||||||
|
||||||
async function buildContextPrompt( | ||||||
language: string | undefined, | ||||||
code: string, | ||||||
path: string | undefined | ||||||
) { | ||||||
if (!language || !path) { | ||||||
return [] | ||||||
} | ||||||
|
||||||
if (code.length < 128) { | ||||||
return [] | ||||||
} | ||||||
|
||||||
const segments = path.split('/'); | ||||||
const repo = segments[0]; | ||||||
path = segments.slice(1).join('/'); | ||||||
|
||||||
const tokens = code.split(/[^\w]/).filter(x => x) | ||||||
|
||||||
// FIXME(jueliang): restrict query with `git_url` of `repo`. | ||||||
const languageQuery = buildLanguageQuery(language) | ||||||
const bodyQuery = tokens.map(x => `body:${x}`).join(' OR ') | ||||||
const query = `${languageQuery} AND (${bodyQuery})` | ||||||
|
||||||
const queryParam = `q=${encodeURIComponent(query)}&limit=20` | ||||||
|
||||||
const data: SearchReponse = await fetcher(`/v1beta/search?${queryParam}`, { | ||||||
responseFormat: 'json' | ||||||
}) | ||||||
const snippets = | ||||||
data.hits.filter(x => x.score > 30 && path !== x.doc.filepath) || [] | ||||||
return formatContextPrompt(repo, language, snippets.slice(0, 3)) | ||||||
} | ||||||
|
||||||
function formatContextPrompt( | ||||||
repo: string, | ||||||
language: string, | ||||||
snippets: ISearchHit[] | ||||||
) { | ||||||
let prompt = 'Given following relevant code snippets:\n\n' | ||||||
for (const { doc } of snippets) { | ||||||
const numLines = doc.body.split(/\r\n|\r|\n/).length | ||||||
const fromLine = doc.start_line | ||||||
const toLine = doc.start_line + numLines - 1 | ||||||
const reference = `\`\`\`${language} is_reference=1 path=${repo}/${doc.filepath} line_from=${fromLine} line_to=${toLine} | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
${doc.body} | ||||||
\`\`\` | ||||||
` | ||||||
prompt += reference | ||||||
} | ||||||
|
||||||
if (snippets.length) { | ||||||
return prompt | ||||||
} else { | ||||||
return '' | ||||||
} | ||||||
} | ||||||
|
||||||
function buildLanguageQuery(language: string) { | ||||||
if ( | ||||||
language == 'javascript' || | ||||||
language == 'jsx' || | ||||||
language == 'typescript' || | ||||||
language == 'tsx' | ||||||
) { | ||||||
language = 'javascript-typescript' | ||||||
} | ||||||
|
||||||
return `language:${language}` | ||||||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@liangfung as we have
git_url
fromrepositoryList
, please fill it in a query likegit_url:${gitUrl}
, and append it toAND ${gitUrlQuery}