Skip to content

Commit 593cdb1

Browse files
Merge pull request #161 from olasunkanmi-SE/refactor-code-indexing
Refactor(code-indexing): Improve code indexing and context retrieval
2 parents 5b66d3b + 7a30214 commit 593cdb1

File tree

12 files changed

+117
-69
lines changed

12 files changed

+117
-69
lines changed

.github/workflows/workflow.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
name: CI for restaurant
1+
name: CI Pipeline
22

33
on:
44
pull_request:

src/agents/orchestrator.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ export class Orchestrator extends BaseAiAgent implements vscode.Disposable {
1111
this.disposables.push(
1212
this.onStatusChange(this.handleStatus.bind(this)),
1313
this.onPromptGenerated(this.handlePromptGeneratedEvent.bind(this)),
14-
this.onError(this.handleError.bind(this)),
14+
// this.onError(this.handleError.bind(this)),
1515
);
1616
}
1717

@@ -31,9 +31,9 @@ export class Orchestrator extends BaseAiAgent implements vscode.Disposable {
3131
this.publish("onResponse", event.message);
3232
}
3333

34-
public handleError(event: IEventPayload) {
35-
console.log(`Error: ${event.message}`);
36-
}
34+
// public handleError(event: IEventPayload) {
35+
// console.log(`Error: ${event.message}`);
36+
// }
3737

3838
public dispose(): void {
3939
this.disposables.forEach((d) => d.dispose());

src/application/constant.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ export enum FSPROPS {
6565
SRC_DIRECTORY = "src",
6666
TS_FILE_PATTERN = "**/*.ts",
6767
TSCONFIG_FILE = "tsconfig.json",
68+
NODE_MODULES_PATTERN = "**/node_modules/**",
6869
}
6970

7071
export const EmbeddingsConfig = {

src/application/interfaces/ts.code.mapper.interface.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ export interface ITypeScriptCodeMapper {
105105
* Builds a hierarchical map of the codebase by traversing TypeScript files
106106
* and extracting module and class information.
107107
*/
108-
buildCodebaseMap(): Promise<ICodebaseMap>;
108+
buildCodebaseMap(): Promise<Map<string, string>>;
109109

110110
/**
111111
* Extracts interface information from a TypeScript interface declaration.

src/extension.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ const {
4444

4545
const connectDB = async () => {
4646
await dbManager.connect(
47-
"file:/Users/olasunkanmioyinlola/Documents/Apps/codebuddy/patterns/aii.db",
47+
"file:/Users/olasunkanmi/Documents/Github/codebuddy/patterns/dev.db",
4848
);
4949
};
5050

@@ -76,10 +76,11 @@ export async function activate(context: vscode.ExtensionContext) {
7676
// const names = await fileUpload.getFileNames();
7777
// console.log(files, names);
7878

79-
// const index = CodeIndexingService.createInstance();
80-
// const result = await index.buildFunctionStructureMap();
79+
const index = CodeIndexingService.createInstance();
80+
// Get each of the folders and call the next line for each
81+
const result = await index.buildFunctionStructureMap();
8182
// await index.insertFunctionsinDB();
82-
// console.log(result);
83+
console.log(result);
8384
const {
8485
comment,
8586
review,

src/llms/gemini/gemini.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -280,6 +280,7 @@ export class GeminiLLM
280280

281281
return finalResult;
282282
} catch (error: any) {
283+
// Note. Use this approach to return error messages back to the FE
283284
this.orchestrator.publish(
284285
"onError",
285286
"Model not responding at this time, please try again",

src/providers/base.ts

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -103,11 +103,6 @@ export abstract class BaseWebViewProvider implements vscode.Disposable {
103103
public handleModelResponseEvent(event: IEventPayload) {
104104
this.sendResponse(formatText(event.message), "bot");
105105
}
106-
107-
public handleThinkingEvent(event: IEventPayload) {
108-
this.sendResponse(formatText(event.message), "bot");
109-
}
110-
111106
abstract generateResponse(
112107
message?: string,
113108
metaData?: Record<string, any>,

src/services/code-indexing.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ export class CodeIndexingService {
4747
*/
4848
async buildFunctionStructureMap(): Promise<Partial<IFunctionData>[]> {
4949
try {
50+
// TODO Get all the typescript project compilers information
5051
const codeATS = await TypeScriptAtsMapper.getInstance();
5152
if (!codeATS) {
5253
throw new Error("Failed to get TypeScriptAtsMapper instance");

src/services/context-retriever.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import { CodeRepository } from "../infrastructure/repository/code";
99
import { getGeminiAPIKey } from "../utils/utils";
1010
import { EmbeddingService } from "./embedding";
1111
import { WebSearchService } from "./web-search-service";
12+
import { Orchestrator } from "../agents/orchestrator";
1213

1314
export class ContextRetriever {
1415
private readonly codeRepository: CodeRepository;
@@ -17,12 +18,14 @@ export class ContextRetriever {
1718
private readonly logger: Logger;
1819
private static instance: ContextRetriever;
1920
private readonly webSearchService: WebSearchService;
21+
protected readonly orchestrator: Orchestrator;
2022
constructor() {
2123
this.codeRepository = CodeRepository.getInstance();
2224
const geminiApiKey = getGeminiAPIKey();
2325
this.embeddingService = new EmbeddingService(geminiApiKey);
2426
this.logger = new Logger("ContextRetriever");
2527
this.webSearchService = WebSearchService.getInstance();
28+
this.orchestrator = Orchestrator.getInstance();
2629
}
2730

2831
static initialize() {
@@ -36,6 +39,7 @@ export class ContextRetriever {
3639
try {
3740
const embedding = await this.embeddingService.generateEmbedding(input);
3841
this.logger.info("Retrieving context from DB");
42+
// this.orchestrator.publish("onUpdate", "Retrieving context from DB");
3943
return this.codeRepository.searchSimilarFunctions(
4044
embedding,
4145
ContextRetriever.SEARCH_RESULT_COUNT,
@@ -80,8 +84,8 @@ export class ContextRetriever {
8084

8185
async webSearch(query: string) {
8286
try {
83-
const x = Array.isArray(query) ? query.join("") : query;
84-
return await this.webSearchService.run(x);
87+
const text = Array.isArray(query) ? query.join("") : query;
88+
return await this.webSearchService.run(text);
8589
} catch (error) {
8690
this.logger.error("Error reading file:", error);
8791
throw error;

src/services/file-system.ts

Lines changed: 30 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,18 @@ import { Logger } from "../infrastructure/logger/logger";
66

77
export class FileService {
88
readonly logger: Logger;
9+
private static instance: FileService;
910
constructor() {
1011
this.logger = new Logger("FileSystemService");
1112
}
13+
14+
static getInstance() {
15+
if (!FileService.instance) {
16+
FileService.instance = new FileService();
17+
}
18+
return FileService.instance;
19+
}
20+
1221
getWorkspaceInfo(dir: string): IWorkspaceInfo | undefined {
1322
try {
1423
const workspaceFolder = vscode.workspace.workspaceFolders?.[0];
@@ -35,25 +44,29 @@ export class FileService {
3544
const directories = await vscode.workspace.fs.readDirectory(
3645
workSpaceInfo.root,
3746
);
47+
const tsFilePaths: string[] = [];
48+
for (const [name, type] of directories) {
49+
if (type === vscode.FileType.Directory) {
50+
const dirUri = vscode.Uri.joinPath(workSpaceInfo.root, name);
51+
const tsConfigUrl = vscode.Uri.joinPath(
52+
dirUri,
53+
FSPROPS.TSCONFIG_FILE,
54+
);
3855

39-
const directory = directories.filter(
40-
([name, type]) => type === vscode.FileType.Directory && name === dir,
41-
);
42-
43-
if (!directory) {
44-
throw Error(`${dir} does not exist within this workspace`);
56+
const tsConfigExists =
57+
(await Promise.resolve(vscode.workspace.fs.stat(tsConfigUrl)).catch(
58+
() => null,
59+
)) !== null;
60+
if (tsConfigExists) {
61+
const tsFiles = await vscode.workspace.findFiles(
62+
new vscode.RelativePattern(dirUri, pattern),
63+
FSPROPS.NODE_MODULES_PATTERN,
64+
);
65+
tsFilePaths.push(...tsFiles.map((file) => file.fsPath));
66+
}
67+
}
4568
}
46-
47-
const directoryFiles = directory.map(async ([file]) => {
48-
const srcUri = vscode.Uri.joinPath(workSpaceInfo.root, file);
49-
const srcFiles = await vscode.workspace.findFiles(
50-
new vscode.RelativePattern(srcUri, pattern),
51-
);
52-
return srcFiles.map((file) => file.fsPath);
53-
});
54-
55-
const srcFilePaths = await Promise.all(directoryFiles);
56-
return srcFilePaths.flat();
69+
return tsFilePaths;
5770
} catch (error) {
5871
handleError(
5972
error,

0 commit comments

Comments
 (0)