|
1 | 1 | import { QdrantClient, Schemas } from "@qdrant/js-client-rest" |
2 | 2 | import { createHash } from "crypto" |
3 | 3 | import * as path from "path" |
| 4 | +import * as fs from "fs" |
4 | 5 | import { getWorkspacePath } from "../../../utils/path" |
5 | 6 | import { IVectorStore } from "../interfaces/vector-store" |
6 | 7 | import { Payload, VectorStoreSearchResult } from "../interfaces" |
7 | 8 | import { DEFAULT_MAX_SEARCH_RESULTS, DEFAULT_SEARCH_MIN_SCORE } from "../constants" |
8 | 9 | import { t } from "../../../i18n" |
| 10 | +import { getGitRepositoryInfo } from "../../../utils/git" |
9 | 11 |
|
10 | 12 | /** |
11 | 13 | * Qdrant implementation of the vector store interface |
@@ -77,10 +79,135 @@ export class QdrantVectorStore implements IVectorStore { |
77 | 79 | }) |
78 | 80 | } |
79 | 81 |
|
80 | | - // Generate collection name from workspace path |
81 | | - const hash = createHash("sha256").update(workspacePath).digest("hex") |
| 82 | + // Generate deterministic collection name |
82 | 83 | this.vectorSize = vectorSize |
83 | | - this.collectionName = `ws-${hash.substring(0, 16)}` |
| 84 | + this.collectionName = this.generateCollectionName(workspacePath) |
| 85 | + } |
| 86 | + |
| 87 | + /** |
| 88 | + * Generates a deterministic collection name based on repository or workspace |
| 89 | + * @param workspacePath Path to the workspace |
| 90 | + * @returns Collection name |
| 91 | + */ |
| 92 | + private generateCollectionName(workspacePath: string): string { |
| 93 | + // First, check for a custom collection name in .roo/codebase-index.json |
| 94 | + const customName = this.loadCustomCollectionName(workspacePath) |
| 95 | + if (customName) { |
| 96 | + // Sanitize the custom name to ensure it's valid for Qdrant |
| 97 | + return this.sanitizeCollectionName(customName) |
| 98 | + } |
| 99 | + |
| 100 | + // Try to get git repository information for deterministic naming |
| 101 | + const gitInfo = this.getGitInfoSync(workspacePath) |
| 102 | + if (gitInfo?.repositoryUrl) { |
| 103 | + // Use repository URL to generate a deterministic name |
| 104 | + // This ensures the same collection name across worktrees and developers |
| 105 | + const hash = createHash("sha256").update(gitInfo.repositoryUrl).digest("hex") |
| 106 | + return `repo-${hash.substring(0, 16)}` |
| 107 | + } |
| 108 | + |
| 109 | + // Fallback to workspace path hash (original behavior) |
| 110 | + const hash = createHash("sha256").update(workspacePath).digest("hex") |
| 111 | + return `ws-${hash.substring(0, 16)}` |
| 112 | + } |
| 113 | + |
| 114 | + /** |
| 115 | + * Loads custom collection name from .roo/codebase-index.json if it exists |
| 116 | + * @param workspacePath Path to the workspace |
| 117 | + * @returns Custom collection name or undefined |
| 118 | + */ |
| 119 | + private loadCustomCollectionName(workspacePath: string): string | undefined { |
| 120 | + try { |
| 121 | + const configPath = path.join(workspacePath, ".roo", "codebase-index.json") |
| 122 | + if (fs.existsSync(configPath)) { |
| 123 | + const config = JSON.parse(fs.readFileSync(configPath, "utf8")) |
| 124 | + if (config.collectionName && typeof config.collectionName === "string") { |
| 125 | + return config.collectionName |
| 126 | + } |
| 127 | + } |
| 128 | + } catch (error) { |
| 129 | + // Ignore errors reading config file |
| 130 | + console.warn( |
| 131 | + `[QdrantVectorStore] Could not read custom collection name from .roo/codebase-index.json:`, |
| 132 | + error, |
| 133 | + ) |
| 134 | + } |
| 135 | + return undefined |
| 136 | + } |
| 137 | + |
| 138 | + /** |
| 139 | + * Synchronously gets git repository information |
| 140 | + * @param workspacePath Path to the workspace |
| 141 | + * @returns Git repository info or undefined |
| 142 | + */ |
| 143 | + private getGitInfoSync(workspacePath: string): { repositoryUrl?: string } | undefined { |
| 144 | + try { |
| 145 | + const gitDir = path.join(workspacePath, ".git") |
| 146 | + |
| 147 | + // Check if .git directory exists |
| 148 | + if (!fs.existsSync(gitDir)) { |
| 149 | + return undefined |
| 150 | + } |
| 151 | + |
| 152 | + // Try to read git config file |
| 153 | + const configPath = path.join(gitDir, "config") |
| 154 | + if (fs.existsSync(configPath)) { |
| 155 | + const configContent = fs.readFileSync(configPath, "utf8") |
| 156 | + |
| 157 | + // Extract remote URL |
| 158 | + const urlMatch = configContent.match(/url\s*=\s*(.+?)(?:\r?\n|$)/m) |
| 159 | + if (urlMatch && urlMatch[1]) { |
| 160 | + const url = urlMatch[1].trim() |
| 161 | + // Normalize the URL to ensure consistency |
| 162 | + const normalizedUrl = this.normalizeGitUrl(url) |
| 163 | + return { repositoryUrl: normalizedUrl } |
| 164 | + } |
| 165 | + } |
| 166 | + } catch (error) { |
| 167 | + // Ignore errors and fall back to workspace-based naming |
| 168 | + console.warn(`[QdrantVectorStore] Could not read git repository info:`, error) |
| 169 | + } |
| 170 | + return undefined |
| 171 | + } |
| 172 | + |
| 173 | + /** |
| 174 | + * Normalizes a git URL for consistent hashing |
| 175 | + * @param url Git URL to normalize |
| 176 | + * @returns Normalized URL |
| 177 | + */ |
| 178 | + private normalizeGitUrl(url: string): string { |
| 179 | + // Remove credentials |
| 180 | + let normalized = url.replace(/^https?:\/\/[^@]+@/, "https://") |
| 181 | + |
| 182 | + // Convert SSH to HTTPS format for consistency |
| 183 | + if (normalized.startsWith("git@")) { |
| 184 | + normalized = normalized.replace(/^git@([^:]+):/, "https://$1/") |
| 185 | + } else if (normalized.startsWith("ssh://")) { |
| 186 | + normalized = normalized.replace(/^ssh:\/\/(?:git@)?([^\/]+)\//, "https://$1/") |
| 187 | + } |
| 188 | + |
| 189 | + // Remove .git suffix |
| 190 | + normalized = normalized.replace(/\.git$/, "") |
| 191 | + |
| 192 | + // Convert to lowercase for consistency |
| 193 | + normalized = normalized.toLowerCase() |
| 194 | + |
| 195 | + return normalized |
| 196 | + } |
| 197 | + |
| 198 | + /** |
| 199 | + * Sanitizes a collection name to ensure it's valid for Qdrant |
| 200 | + * @param name Collection name to sanitize |
| 201 | + * @returns Sanitized collection name |
| 202 | + */ |
| 203 | + private sanitizeCollectionName(name: string): string { |
| 204 | + // Qdrant collection names must be alphanumeric with underscores or hyphens |
| 205 | + // Max length is typically 255 characters |
| 206 | + return name |
| 207 | + .toLowerCase() |
| 208 | + .replace(/[^a-z0-9_-]/g, "-") |
| 209 | + .replace(/^-+|-+$/g, "") // Remove leading/trailing hyphens |
| 210 | + .substring(0, 255) |
84 | 211 | } |
85 | 212 |
|
86 | 213 | /** |
|
0 commit comments