support for multi-workspace dirs and pagination

This commit is contained in:
Test 2024-08-28 11:42:06 -07:00
parent aec9b63fac
commit cbd341dd2d
2 changed files with 57 additions and 41 deletions

View file

@ -48,7 +48,7 @@ class RepoMapContextProvider extends BaseContextProvider {
private async generateRepoMap({ llm, ide }: ContextProviderExtras) {
const repoMapPath = getRepoMapFilePath();
const [workspaceDir] = await ide.getWorkspaceDirs();
const workspaceDirs = await ide.getWorkspaceDirs();
const maxRepoMapTokens =
llm.contextLength * this.REPO_MAX_CONTEXT_LENGTH_RATIO;
@ -60,15 +60,23 @@ class RepoMapContextProvider extends BaseContextProvider {
writeStream.write(this.repoMapPreamble);
let curTokens = llm.countTokens(this.repoMapPreamble);
let offset = 0;
const batchSize = 100;
while (true) {
const { groupedByPath, hasMore } =
await CodeSnippetsCodebaseIndex.getPathsAndSignatures(
workspaceDirs,
offset,
batchSize,
);
for await (const pathsAndSignatures of CodeSnippetsCodebaseIndex.getAllPathsAndSignatures(
workspaceDir,
)) {
let content = "";
for (const [absolutePath, signatures] of Object.entries(
pathsAndSignatures,
)) {
for (const [absolutePath, signatures] of Object.entries(groupedByPath)) {
const workspaceDir =
workspaceDirs.find((dir) => absolutePath.startsWith(dir)) || "";
const relativePath = path.relative(workspaceDir, absolutePath);
content += `${relativePath}:\n`;
@ -76,10 +84,12 @@ class RepoMapContextProvider extends BaseContextProvider {
content += `${this.indentMultilineString(signature)}\n\t...\n`;
}
// Don't add the trailing elipsis for the last entry
content += `${this.indentMultilineString(
signatures[signatures.length - 1],
)}\n\n`;
if (signatures.length > 0) {
// Don't add the trailing ellipsis for the last entry
content += `${this.indentMultilineString(
signatures[signatures.length - 1],
)}\n\n`;
}
}
curTokens += llm.countTokens(content);
@ -89,6 +99,12 @@ class RepoMapContextProvider extends BaseContextProvider {
}
writeStream.write(content);
if (!hasMore) {
break;
}
offset += batchSize;
}
writeStream.end();

View file

@ -365,43 +365,43 @@ export class CodeSnippetsCodebaseIndex implements CodebaseIndex {
}
}
static async *getAllPathsAndSignatures(
workspaceDir: string,
static async getPathsAndSignatures(
workspaceDirs: string[],
offset: number = 0,
batchSize: number = 100,
): AsyncGenerator<{ [path: string]: string[] }> {
): Promise<{
groupedByPath: { [path: string]: string[] };
hasMore: boolean;
}> {
const db = await SqliteDb.get();
await CodeSnippetsCodebaseIndex._createTables(db);
let offset = 0;
let hasMore = true;
const likePatterns = workspaceDirs.map((dir) => `${dir}%`);
const placeholders = likePatterns.map(() => "?").join(" OR path LIKE ");
while (hasMore) {
const rows = await db.all(
`SELECT path, signature
FROM code_snippets
WHERE path LIKE ?
ORDER BY path
LIMIT ? OFFSET ?`,
[`${workspaceDir}%`, batchSize, offset],
);
const query = `
SELECT path, signature
FROM code_snippets
WHERE path LIKE ${placeholders}
ORDER BY path
LIMIT ? OFFSET ?
`;
if (rows.length === 0) {
hasMore = false;
continue;
const rows = await db.all(query, [...likePatterns, batchSize, offset]);
const validRows = rows.filter((row) => row.path && row.signature !== null);
const groupedByPath: { [path: string]: string[] } = {};
for (const { path, signature } of validRows) {
if (!groupedByPath[path]) {
groupedByPath[path] = [];
}
const groupedByPath: { [path: string]: string[] } = {};
for (const row of rows) {
if (!groupedByPath[row.path]) {
groupedByPath[row.path] = [];
}
groupedByPath[row.path].push(row.signature);
}
yield groupedByPath;
offset += batchSize;
groupedByPath[path].push(signature);
}
const hasMore = rows.length === batchSize;
return { groupedByPath, hasMore };
}
}