Skip to content

Commit ceb858b

Browse files
committed
fix: index branch name as well & fix incorrect file url when not main
Signed-off-by: Koichi Shiraishi <[email protected]>
1 parent f50bb47 commit ceb858b

File tree

4 files changed

+47
-4
lines changed

4 files changed

+47
-4
lines changed

packages/chrome-extension/src/background.ts

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,7 @@ class MilvusVectorDB {
124124
return results.map(result => ({
125125
id: result.id,
126126
content: result.content,
127+
branch: result.branch, // Include branch name for frontend display
127128
relativePath: result.relativePath,
128129
startLine: result.startLine,
129130
endLine: result.endLine,
@@ -310,6 +311,27 @@ async function handleRateLimit(response: Response): Promise<void> {
310311
}
311312
}
312313

314+
async function fetchRepoBranch(owner: string, repo: string): Promise<string> {
315+
const token = await getGitHubToken();
316+
317+
// First get the default branch
318+
const repoInfoUrl = `https://api.github.com/repos/${owner}/${repo}`;
319+
const repoResponse = await fetch(repoInfoUrl, {
320+
headers: {
321+
'Authorization': `Bearer ${token}`,
322+
'Accept': 'application/vnd.github+json',
323+
'X-GitHub-Api-Version': '2022-11-28'
324+
}
325+
});
326+
327+
if (!repoResponse.ok) {
328+
throw new Error(`GitHub API error: ${repoResponse.status} - ${await repoResponse.text()}`);
329+
}
330+
331+
const repoData = await repoResponse.json();
332+
return repoData.default_branch || 'main';
333+
}
334+
313335
async function fetchRepoFiles(owner: string, repo: string): Promise<any[]> {
314336
const token = await getGitHubToken();
315337

@@ -457,11 +479,14 @@ async function handleIndexRepo(request: any, sendResponse: Function) {
457479
const chunkOverlap = 200; // Same as VSCode extension default
458480

459481
// Fetch repository files
460-
const files = await fetchRepoFiles(owner, repo);
482+
const [branch, files] = await Promise.all([
483+
fetchRepoBranch(owner, repo),
484+
fetchRepoFiles(owner, repo),
485+
]);
461486
console.log(`Found ${files.length} files to index`);
462487

463488
// Process files using core package approach
464-
const result = await processFileList(files, owner, repo, repoId, vectorDB, chunkSize, chunkOverlap);
489+
const result = await processFileList(files, owner, repo, branch, repoId, vectorDB, chunkSize, chunkOverlap);
465490

466491
// Send completion message
467492
chrome.tabs.query({ active: true, currentWindow: true }, (tabs) => {
@@ -499,6 +524,7 @@ async function processFileList(
499524
files: any[],
500525
owner: string,
501526
repo: string,
527+
branch: string,
502528
repoId: string,
503529
vectorDB: MilvusVectorDB,
504530
chunkSize: number,
@@ -532,6 +558,7 @@ async function processFileList(
532558
const codeChunk: CodeChunk = {
533559
id: `${file.path}_chunk_${j}`,
534560
content: chunk.content,
561+
branch: branch,
535562
relativePath: file.path,
536563
startLine: chunk.startLine,
537564
endLine: chunk.endLine,

packages/chrome-extension/src/content.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -366,6 +366,7 @@ function displayResults(results: any[]) {
366366
const [owner, repo] = window.location.pathname.slice(1).split('/');
367367

368368
// Format the file path to show it nicely
369+
const branch = result.branch;
369370
const filePath = result.relativePath;
370371
const fileExt = filePath.split('.').pop();
371372

@@ -383,7 +384,7 @@ function displayResults(results: any[]) {
383384
<svg class="octicon mr-2 color-fg-muted" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 16 16" width="16" height="16">
384385
<path fill-rule="evenodd" d="M3.75 1.5a.25.25 0 00-.25.25v11.5c0 .138.112.25.25.25h8.5a.25.25 0 00.25-.25V6H9.75A1.75 1.75 0 018 4.25V1.5H3.75zm5.75.56v2.19c0 .138.112.25.25.25h2.19L9.5 2.06zM2 1.75C2 .784 2.784 0 3.75 0h5.086c.464 0 .909.184 1.237.513l3.414 3.414c.329.328.513.773.513 1.237v8.086A1.75 1.75 0 0112.25 15h-8.5A1.75 1.75 0 012 13.25V1.75z"></path>
385386
</svg>
386-
<a href="https://github.com/${owner}/${repo}/blob/main/${result.relativePath}#L${result.startLine}" class="Link--primary flex-auto" style="font-weight: 600;">
387+
<a href="https://github.com/${owner}/${repo}/blob/${branch}/${result.relativePath}#L${result.startLine}" class="Link--primary flex-auto" style="font-weight: 600;">
387388
${result.relativePath}
388389
</a>
389390
<span class="Label Label--secondary ml-1">${fileExt}</span>
@@ -603,4 +604,4 @@ new MutationObserver((mutations, observer) => {
603604
// Just check if UI needs to be injected (for dynamic content)
604605
injectUI();
605606
}
606-
}).observe(document.body, { childList: true, subtree: true });
607+
}).observe(document.body, { childList: true, subtree: true });

packages/chrome-extension/src/milvus/chromeMilvusAdapter.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import { MilvusRestfulVectorDatabase } from '../stubs/milvus-vectordb-stub';
99
export interface CodeChunk {
1010
id: string;
1111
content: string;
12+
branch: string;
1213
relativePath: string;
1314
startLine: number;
1415
endLine: number;
@@ -20,6 +21,7 @@ export interface CodeChunk {
2021
export interface SearchResult {
2122
id: string;
2223
content: string;
24+
branch: string;
2325
relativePath: string;
2426
startLine: number;
2527
endLine: number;
@@ -113,6 +115,7 @@ export class ChromeMilvusAdapter {
113115
id: chunk.id,
114116
vector: chunk.vector || [],
115117
content: chunk.content,
118+
branch: chunk.branch,
116119
relativePath: chunk.relativePath,
117120
startLine: chunk.startLine,
118121
endLine: chunk.endLine,
@@ -149,6 +152,7 @@ export class ChromeMilvusAdapter {
149152
const searchResults = results.map(result => ({
150153
id: result.document.id,
151154
content: result.document.content,
155+
branch: result.document.branch,
152156
relativePath: result.document.relativePath,
153157
startLine: result.document.startLine,
154158
endLine: result.document.endLine,

packages/chrome-extension/src/stubs/milvus-vectordb-stub.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ export interface VectorDocument {
66
id: string;
77
vector: number[];
88
content: string;
9+
branch: string;
910
relativePath: string;
1011
startLine: number;
1112
endLine: number;
@@ -141,6 +142,13 @@ export class MilvusRestfulVectorDatabase {
141142
max_length: 65535
142143
}
143144
},
145+
{
146+
fieldName: "branch",
147+
dataType: "VarChar",
148+
elementTypeParams: {
149+
max_length: 512
150+
}
151+
},
144152
{
145153
fieldName: "relativePath",
146154
dataType: "VarChar",
@@ -247,6 +255,7 @@ export class MilvusRestfulVectorDatabase {
247255
id: doc.id,
248256
vector: doc.vector,
249257
content: doc.content,
258+
branch: doc.branch,
250259
relativePath: doc.relativePath,
251260
startLine: doc.startLine,
252261
endLine: doc.endLine,
@@ -280,6 +289,7 @@ export class MilvusRestfulVectorDatabase {
280289
limit: topK,
281290
outputFields: [
282291
"content",
292+
"branch",
283293
"relativePath",
284294
"startLine",
285295
"endLine",
@@ -308,6 +318,7 @@ export class MilvusRestfulVectorDatabase {
308318
id: item.id?.toString() || '',
309319
vector: queryVector,
310320
content: item.content || '',
321+
branch: item.branch || '',
311322
relativePath: item.relativePath || '',
312323
startLine: item.startLine || 0,
313324
endLine: item.endLine || 0,

0 commit comments

Comments
 (0)