kiri-mcp-server 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (212) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +399 -0
  3. package/config/default.example.yml +12 -0
  4. package/config/denylist.yml +15 -0
  5. package/config/scoring-profiles.yml +37 -0
  6. package/config/security.yml +10 -0
  7. package/dist/client/cli.js +68 -0
  8. package/dist/client/cli.js.map +1 -0
  9. package/dist/client/index.js +5 -0
  10. package/dist/client/index.js.map +1 -0
  11. package/dist/config/default.example.yml +12 -0
  12. package/dist/config/denylist.yml +15 -0
  13. package/dist/config/scoring-profiles.yml +37 -0
  14. package/dist/config/security.yml +10 -0
  15. package/dist/eval/metrics.js +47 -0
  16. package/dist/eval/metrics.js.map +1 -0
  17. package/dist/indexer/cli.js +362 -0
  18. package/dist/indexer/cli.js.map +1 -0
  19. package/dist/indexer/codeintel.js +182 -0
  20. package/dist/indexer/codeintel.js.map +1 -0
  21. package/dist/indexer/git.js +30 -0
  22. package/dist/indexer/git.js.map +1 -0
  23. package/dist/indexer/language.js +34 -0
  24. package/dist/indexer/language.js.map +1 -0
  25. package/dist/indexer/pipeline/filters/denylist.js +71 -0
  26. package/dist/indexer/pipeline/filters/denylist.js.map +1 -0
  27. package/dist/indexer/schema.js +101 -0
  28. package/dist/indexer/schema.js.map +1 -0
  29. package/dist/package.json +93 -0
  30. package/dist/server/bootstrap.js +19 -0
  31. package/dist/server/bootstrap.js.map +1 -0
  32. package/dist/server/context.js +2 -0
  33. package/dist/server/context.js.map +1 -0
  34. package/dist/server/fallbacks/degradeController.js +69 -0
  35. package/dist/server/fallbacks/degradeController.js.map +1 -0
  36. package/dist/server/handlers.js +800 -0
  37. package/dist/server/handlers.js.map +1 -0
  38. package/dist/server/main.js +151 -0
  39. package/dist/server/main.js.map +1 -0
  40. package/dist/server/observability/metrics.js +56 -0
  41. package/dist/server/observability/metrics.js.map +1 -0
  42. package/dist/server/observability/tracing.js +58 -0
  43. package/dist/server/observability/tracing.js.map +1 -0
  44. package/dist/server/rpc.js +477 -0
  45. package/dist/server/rpc.js.map +1 -0
  46. package/dist/server/runtime.js +47 -0
  47. package/dist/server/runtime.js.map +1 -0
  48. package/dist/server/scoring.js +111 -0
  49. package/dist/server/scoring.js.map +1 -0
  50. package/dist/server/stdio.js +76 -0
  51. package/dist/server/stdio.js.map +1 -0
  52. package/dist/shared/duckdb.js +121 -0
  53. package/dist/shared/duckdb.js.map +1 -0
  54. package/dist/shared/embedding.js +85 -0
  55. package/dist/shared/embedding.js.map +1 -0
  56. package/dist/shared/index.js +9 -0
  57. package/dist/shared/index.js.map +1 -0
  58. package/dist/shared/security/config.js +64 -0
  59. package/dist/shared/security/config.js.map +1 -0
  60. package/dist/shared/security/masker.js +56 -0
  61. package/dist/shared/security/masker.js.map +1 -0
  62. package/dist/shared/tokenizer.js +5 -0
  63. package/dist/shared/tokenizer.js.map +1 -0
  64. package/dist/shared/utils/simpleYaml.js +90 -0
  65. package/dist/shared/utils/simpleYaml.js.map +1 -0
  66. package/dist/sql/schema.sql +6 -0
  67. package/dist/src/client/cli.d.ts +3 -0
  68. package/dist/src/client/cli.d.ts.map +1 -0
  69. package/dist/src/client/cli.js +68 -0
  70. package/dist/src/client/cli.js.map +1 -0
  71. package/dist/src/client/index.d.ts +5 -0
  72. package/dist/src/client/index.d.ts.map +1 -0
  73. package/dist/src/client/index.js +5 -0
  74. package/dist/src/client/index.js.map +1 -0
  75. package/dist/src/client/proxy.d.ts +9 -0
  76. package/dist/src/client/proxy.d.ts.map +1 -0
  77. package/dist/src/client/proxy.js +198 -0
  78. package/dist/src/client/proxy.js.map +1 -0
  79. package/dist/src/client/start-daemon.d.ts +30 -0
  80. package/dist/src/client/start-daemon.d.ts.map +1 -0
  81. package/dist/src/client/start-daemon.js +175 -0
  82. package/dist/src/client/start-daemon.js.map +1 -0
  83. package/dist/src/daemon/daemon.d.ts +9 -0
  84. package/dist/src/daemon/daemon.d.ts.map +1 -0
  85. package/dist/src/daemon/daemon.js +149 -0
  86. package/dist/src/daemon/daemon.js.map +1 -0
  87. package/dist/src/daemon/lifecycle.d.ts +101 -0
  88. package/dist/src/daemon/lifecycle.d.ts.map +1 -0
  89. package/dist/src/daemon/lifecycle.js +266 -0
  90. package/dist/src/daemon/lifecycle.js.map +1 -0
  91. package/dist/src/daemon/socket.d.ts +26 -0
  92. package/dist/src/daemon/socket.d.ts.map +1 -0
  93. package/dist/src/daemon/socket.js +132 -0
  94. package/dist/src/daemon/socket.js.map +1 -0
  95. package/dist/src/eval/metrics.d.ts +23 -0
  96. package/dist/src/eval/metrics.d.ts.map +1 -0
  97. package/dist/src/eval/metrics.js +47 -0
  98. package/dist/src/eval/metrics.js.map +1 -0
  99. package/dist/src/index.d.ts +11 -0
  100. package/dist/src/index.d.ts.map +1 -0
  101. package/dist/src/index.js +11 -0
  102. package/dist/src/index.js.map +1 -0
  103. package/dist/src/indexer/cli.d.ts +9 -0
  104. package/dist/src/indexer/cli.d.ts.map +1 -0
  105. package/dist/src/indexer/cli.js +402 -0
  106. package/dist/src/indexer/cli.js.map +1 -0
  107. package/dist/src/indexer/codeintel.d.ts +28 -0
  108. package/dist/src/indexer/codeintel.d.ts.map +1 -0
  109. package/dist/src/indexer/codeintel.js +451 -0
  110. package/dist/src/indexer/codeintel.js.map +1 -0
  111. package/dist/src/indexer/git.d.ts +4 -0
  112. package/dist/src/indexer/git.d.ts.map +1 -0
  113. package/dist/src/indexer/git.js +30 -0
  114. package/dist/src/indexer/git.js.map +1 -0
  115. package/dist/src/indexer/language.d.ts +2 -0
  116. package/dist/src/indexer/language.d.ts.map +1 -0
  117. package/dist/src/indexer/language.js +34 -0
  118. package/dist/src/indexer/language.js.map +1 -0
  119. package/dist/src/indexer/pipeline/filters/denylist.d.ts +10 -0
  120. package/dist/src/indexer/pipeline/filters/denylist.d.ts.map +1 -0
  121. package/dist/src/indexer/pipeline/filters/denylist.js +71 -0
  122. package/dist/src/indexer/pipeline/filters/denylist.js.map +1 -0
  123. package/dist/src/indexer/schema.d.ts +9 -0
  124. package/dist/src/indexer/schema.d.ts.map +1 -0
  125. package/dist/src/indexer/schema.js +125 -0
  126. package/dist/src/indexer/schema.js.map +1 -0
  127. package/dist/src/indexer/watch.d.ts +97 -0
  128. package/dist/src/indexer/watch.d.ts.map +1 -0
  129. package/dist/src/indexer/watch.js +264 -0
  130. package/dist/src/indexer/watch.js.map +1 -0
  131. package/dist/src/server/bootstrap.d.ts +11 -0
  132. package/dist/src/server/bootstrap.d.ts.map +1 -0
  133. package/dist/src/server/bootstrap.js +19 -0
  134. package/dist/src/server/bootstrap.js.map +1 -0
  135. package/dist/src/server/context.d.ts +9 -0
  136. package/dist/src/server/context.d.ts.map +1 -0
  137. package/dist/src/server/context.js +2 -0
  138. package/dist/src/server/context.js.map +1 -0
  139. package/dist/src/server/fallbacks/degradeController.d.ts +24 -0
  140. package/dist/src/server/fallbacks/degradeController.d.ts.map +1 -0
  141. package/dist/src/server/fallbacks/degradeController.js +135 -0
  142. package/dist/src/server/fallbacks/degradeController.js.map +1 -0
  143. package/dist/src/server/handlers.d.ts +105 -0
  144. package/dist/src/server/handlers.d.ts.map +1 -0
  145. package/dist/src/server/handlers.js +954 -0
  146. package/dist/src/server/handlers.js.map +1 -0
  147. package/dist/src/server/indexBootstrap.d.ts +13 -0
  148. package/dist/src/server/indexBootstrap.d.ts.map +1 -0
  149. package/dist/src/server/indexBootstrap.js +109 -0
  150. package/dist/src/server/indexBootstrap.js.map +1 -0
  151. package/dist/src/server/main.d.ts +10 -0
  152. package/dist/src/server/main.d.ts.map +1 -0
  153. package/dist/src/server/main.js +217 -0
  154. package/dist/src/server/main.js.map +1 -0
  155. package/dist/src/server/observability/metrics.d.ts +35 -0
  156. package/dist/src/server/observability/metrics.d.ts.map +1 -0
  157. package/dist/src/server/observability/metrics.js +70 -0
  158. package/dist/src/server/observability/metrics.js.map +1 -0
  159. package/dist/src/server/observability/tracing.d.ts +3 -0
  160. package/dist/src/server/observability/tracing.d.ts.map +1 -0
  161. package/dist/src/server/observability/tracing.js +58 -0
  162. package/dist/src/server/observability/tracing.js.map +1 -0
  163. package/dist/src/server/rpc.d.ts +39 -0
  164. package/dist/src/server/rpc.d.ts.map +1 -0
  165. package/dist/src/server/rpc.js +551 -0
  166. package/dist/src/server/rpc.js.map +1 -0
  167. package/dist/src/server/runtime.d.ts +21 -0
  168. package/dist/src/server/runtime.d.ts.map +1 -0
  169. package/dist/src/server/runtime.js +59 -0
  170. package/dist/src/server/runtime.js.map +1 -0
  171. package/dist/src/server/scoring.d.ts +20 -0
  172. package/dist/src/server/scoring.d.ts.map +1 -0
  173. package/dist/src/server/scoring.js +112 -0
  174. package/dist/src/server/scoring.js.map +1 -0
  175. package/dist/src/server/stdio.d.ts +4 -0
  176. package/dist/src/server/stdio.d.ts.map +1 -0
  177. package/dist/src/server/stdio.js +88 -0
  178. package/dist/src/server/stdio.js.map +1 -0
  179. package/dist/src/shared/duckdb.d.ts +16 -0
  180. package/dist/src/shared/duckdb.d.ts.map +1 -0
  181. package/dist/src/shared/duckdb.js +121 -0
  182. package/dist/src/shared/duckdb.js.map +1 -0
  183. package/dist/src/shared/embedding.d.ts +19 -0
  184. package/dist/src/shared/embedding.d.ts.map +1 -0
  185. package/dist/src/shared/embedding.js +85 -0
  186. package/dist/src/shared/embedding.js.map +1 -0
  187. package/dist/src/shared/index.d.ts +3 -0
  188. package/dist/src/shared/index.d.ts.map +1 -0
  189. package/dist/src/shared/index.js +9 -0
  190. package/dist/src/shared/index.js.map +1 -0
  191. package/dist/src/shared/security/config.d.ts +23 -0
  192. package/dist/src/shared/security/config.d.ts.map +1 -0
  193. package/dist/src/shared/security/config.js +66 -0
  194. package/dist/src/shared/security/config.js.map +1 -0
  195. package/dist/src/shared/security/masker.d.ts +10 -0
  196. package/dist/src/shared/security/masker.d.ts.map +1 -0
  197. package/dist/src/shared/security/masker.js +56 -0
  198. package/dist/src/shared/security/masker.js.map +1 -0
  199. package/dist/src/shared/tokenizer.d.ts +2 -0
  200. package/dist/src/shared/tokenizer.d.ts.map +1 -0
  201. package/dist/src/shared/tokenizer.js +5 -0
  202. package/dist/src/shared/tokenizer.js.map +1 -0
  203. package/dist/src/shared/utils/lockfile.d.ts +46 -0
  204. package/dist/src/shared/utils/lockfile.d.ts.map +1 -0
  205. package/dist/src/shared/utils/lockfile.js +136 -0
  206. package/dist/src/shared/utils/lockfile.js.map +1 -0
  207. package/dist/src/shared/utils/simpleYaml.d.ts +6 -0
  208. package/dist/src/shared/utils/simpleYaml.d.ts.map +1 -0
  209. package/dist/src/shared/utils/simpleYaml.js +90 -0
  210. package/dist/src/shared/utils/simpleYaml.js.map +1 -0
  211. package/package.json +91 -0
  212. package/sql/schema.sql +6 -0
@@ -0,0 +1,954 @@
1
+ import path from "node:path";
2
+ import { generateEmbedding, structuralSimilarity } from "../shared/embedding.js";
3
+ import { encode as encodeGPT } from "../shared/tokenizer.js";
4
+ import { coerceProfileName, loadScoringProfile } from "./scoring.js";
5
+ const DEFAULT_SEARCH_LIMIT = 50;
6
+ const DEFAULT_SNIPPET_WINDOW = 150;
7
+ const DEFAULT_BUNDLE_LIMIT = 12;
8
+ const MAX_BUNDLE_LIMIT = 20;
9
+ const MAX_KEYWORDS = 12;
10
+ const MAX_MATCHES_PER_KEYWORD = 40;
11
+ const MAX_DEPENDENCY_SEEDS = 8;
12
+ const MAX_DEPENDENCY_SEEDS_QUERY_LIMIT = 100; // SQL injection防御用の上限
13
+ const NEARBY_LIMIT = 6;
14
+ const FALLBACK_SNIPPET_WINDOW = 120;
15
+ const MAX_RERANK_LIMIT = 50;
16
+ const STOP_WORDS = new Set([
17
+ "the",
18
+ "and",
19
+ "for",
20
+ "with",
21
+ "from",
22
+ "this",
23
+ "that",
24
+ "have",
25
+ "has",
26
+ "will",
27
+ "would",
28
+ "into",
29
+ "about",
30
+ "there",
31
+ "their",
32
+ "your",
33
+ "fix",
34
+ "test",
35
+ "tests",
36
+ "issue",
37
+ "error",
38
+ "bug",
39
+ "fail",
40
+ "failing",
41
+ "make",
42
+ "when",
43
+ "where",
44
+ "should",
45
+ "could",
46
+ "need",
47
+ "goal",
48
+ ]);
49
+ function normalizeLimit(limit) {
50
+ if (!limit || Number.isNaN(limit)) {
51
+ return DEFAULT_SEARCH_LIMIT;
52
+ }
53
+ return Math.min(Math.max(1, Math.floor(limit)), 100);
54
+ }
55
+ function buildPreview(content, query) {
56
+ const lowerContent = content.toLowerCase();
57
+ const lowerQuery = query.toLowerCase();
58
+ const index = lowerContent.indexOf(lowerQuery);
59
+ if (index === -1) {
60
+ return { preview: content.slice(0, 240), line: 1 };
61
+ }
62
+ const prefix = content.slice(0, index);
63
+ const prefixLines = prefix.split(/\r?\n/);
64
+ const matchLine = prefix.length === 0 ? 1 : prefixLines.length;
65
+ const snippetStart = Math.max(0, index - 120);
66
+ const snippetEnd = Math.min(content.length, index + query.length + 120);
67
+ const preview = content.slice(snippetStart, snippetEnd);
68
+ return { preview, line: matchLine };
69
+ }
70
+ function normalizeBundleLimit(limit) {
71
+ if (!limit || Number.isNaN(limit)) {
72
+ return DEFAULT_BUNDLE_LIMIT;
73
+ }
74
+ return Math.min(Math.max(1, Math.floor(limit)), MAX_BUNDLE_LIMIT);
75
+ }
76
+ function extractKeywords(text) {
77
+ const words = text
78
+ .toLowerCase()
79
+ .split(/[^a-z0-9_]+/iu)
80
+ .map((word) => word.trim())
81
+ .filter((word) => word.length >= 3 && !STOP_WORDS.has(word));
82
+ const unique = [];
83
+ for (const word of words) {
84
+ if (!unique.includes(word)) {
85
+ unique.push(word);
86
+ if (unique.length >= MAX_KEYWORDS) {
87
+ break;
88
+ }
89
+ }
90
+ }
91
+ return unique;
92
+ }
93
+ function ensureCandidate(map, filePath) {
94
+ let candidate = map.get(filePath);
95
+ if (!candidate) {
96
+ candidate = {
97
+ path: filePath,
98
+ score: 0,
99
+ reasons: new Set(),
100
+ matchLine: null,
101
+ content: null,
102
+ totalLines: null,
103
+ lang: null,
104
+ ext: null,
105
+ embedding: null,
106
+ semanticSimilarity: null,
107
+ };
108
+ map.set(filePath, candidate);
109
+ }
110
+ return candidate;
111
+ }
112
+ function parseEmbedding(vectorJson, vectorDims) {
113
+ if (!vectorJson || !vectorDims || vectorDims <= 0) {
114
+ return null;
115
+ }
116
+ try {
117
+ const parsed = JSON.parse(vectorJson);
118
+ if (!Array.isArray(parsed)) {
119
+ return null;
120
+ }
121
+ const values = [];
122
+ for (let i = 0; i < parsed.length && i < vectorDims; i += 1) {
123
+ const raw = parsed[i];
124
+ const num = typeof raw === "number" ? raw : Number(raw);
125
+ if (!Number.isFinite(num)) {
126
+ return null;
127
+ }
128
+ values.push(num);
129
+ }
130
+ return values.length === vectorDims ? values : null;
131
+ }
132
+ catch {
133
+ return null;
134
+ }
135
+ }
136
+ function applyStructuralScores(candidates, queryEmbedding, structuralWeight) {
137
+ if (!queryEmbedding || structuralWeight <= 0) {
138
+ return;
139
+ }
140
+ for (const candidate of candidates) {
141
+ if (!candidate.embedding) {
142
+ continue;
143
+ }
144
+ const similarity = structuralSimilarity(queryEmbedding, candidate.embedding);
145
+ if (!Number.isFinite(similarity) || similarity <= 0) {
146
+ continue;
147
+ }
148
+ candidate.semanticSimilarity = similarity;
149
+ candidate.score += structuralWeight * similarity;
150
+ candidate.reasons.add(`structural:${similarity.toFixed(2)}`);
151
+ }
152
+ }
153
+ async function fetchEmbeddingMap(db, repoId, paths) {
154
+ const map = new Map();
155
+ if (paths.length === 0) {
156
+ return map;
157
+ }
158
+ const placeholders = paths.map(() => "?").join(", ");
159
+ const rows = await db.all(`
160
+ SELECT path, vector_json, dims AS vector_dims
161
+ FROM file_embedding
162
+ WHERE repo_id = ? AND path IN (${placeholders})
163
+ `, [repoId, ...paths]);
164
+ for (const row of rows) {
165
+ const embedding = parseEmbedding(row.vector_json, row.vector_dims);
166
+ if (embedding) {
167
+ map.set(row.path, embedding);
168
+ }
169
+ }
170
+ return map;
171
+ }
172
+ async function loadFileContent(db, repoId, filePath) {
173
+ const rows = await db.all(`
174
+ SELECT f.path, f.lang, f.ext, f.is_binary, b.content, fe.vector_json, fe.dims AS vector_dims
175
+ FROM file f
176
+ JOIN blob b ON b.hash = f.blob_hash
177
+ LEFT JOIN file_embedding fe
178
+ ON fe.repo_id = f.repo_id
179
+ AND fe.path = f.path
180
+ WHERE f.repo_id = ? AND f.path = ?
181
+ LIMIT 1
182
+ `, [repoId, filePath]);
183
+ const row = rows[0];
184
+ if (!row || row.is_binary || row.content === null) {
185
+ return null;
186
+ }
187
+ const totalLines = row.content.length === 0 ? 0 : row.content.split(/\r?\n/).length;
188
+ return {
189
+ content: row.content,
190
+ lang: row.lang,
191
+ ext: row.ext,
192
+ totalLines,
193
+ embedding: parseEmbedding(row.vector_json ?? null, row.vector_dims ?? null),
194
+ };
195
+ }
196
+ function selectSnippet(snippets, matchLine) {
197
+ const firstSnippet = snippets[0];
198
+ if (!firstSnippet) {
199
+ return null;
200
+ }
201
+ if (matchLine === null) {
202
+ return firstSnippet;
203
+ }
204
+ const containing = snippets.find((snippet) => matchLine >= snippet.start_line && matchLine <= snippet.end_line);
205
+ if (containing) {
206
+ return containing;
207
+ }
208
+ if (matchLine < firstSnippet.start_line) {
209
+ return firstSnippet;
210
+ }
211
+ const lastSnippet = snippets[snippets.length - 1];
212
+ return lastSnippet ?? firstSnippet;
213
+ }
214
+ function buildSnippetPreview(content, startLine, endLine) {
215
+ const lines = content.split(/\r?\n/);
216
+ const startIndex = Math.max(0, Math.min(startLine - 1, lines.length));
217
+ const endIndex = Math.max(startIndex, Math.min(endLine, lines.length));
218
+ const snippet = lines.slice(startIndex, endIndex).join("\n");
219
+ if (snippet.length <= 480) {
220
+ return snippet;
221
+ }
222
+ return `${snippet.slice(0, 479)}…`;
223
+ }
224
+ /**
225
+ * トークン数を推定(コンテンツベース)
226
+ * 実際のGPTトークナイザーを使用して正確にカウント
227
+ *
228
+ * @param content - ファイル全体のコンテンツ
229
+ * @param startLine - 開始行(1-indexed)
230
+ * @param endLine - 終了行(1-indexed)
231
+ * @returns 推定トークン数
232
+ */
233
+ function estimateTokensFromContent(content, startLine, endLine) {
234
+ const lines = content.split(/\r?\n/);
235
+ const startIndex = Math.max(0, startLine - 1);
236
+ const endIndex = Math.min(endLine, lines.length);
237
+ const selectedLines = lines.slice(startIndex, endIndex);
238
+ const text = selectedLines.join("\n");
239
+ try {
240
+ // 実際のGPTトークナイザーを使用
241
+ return encodeGPT(text).length;
242
+ }
243
+ catch (error) {
244
+ // フォールバック: 平均的な英語テキストで4文字 ≈ 1トークン
245
+ console.warn("Token encoding failed, using character-based fallback", error);
246
+ return Math.max(1, Math.ceil(text.length / 4));
247
+ }
248
+ }
249
+ /**
250
+ * 複数単語クエリを単語分割してOR検索条件を構築
251
+ * @param query - 検索クエリ文字列
252
+ * @returns 単語配列(2文字以下を除外)
253
+ */
254
+ function splitQueryWords(query) {
255
+ // 空白、スラッシュ、ハイフン、アンダースコアで分割
256
+ const words = query.split(/[\s/\-_]+/).filter((w) => w.length > 2);
257
+ return words.length > 0 ? words : [query]; // 全て除外された場合は元のクエリを使用
258
+ }
259
+ /**
260
+ * ファイルタイプに基づいてスコアをブーストする
261
+ * プロファイルに応じて実装ファイルまたはドキュメントを優遇
262
+ * @param path - ファイルパス
263
+ * @param baseScore - 元のスコア
264
+ * @param profile - ブーストプロファイル ("default" | "docs" | "none")
265
+ * @returns ブースト適用後のスコア
266
+ */
267
+ function applyFileTypeBoost(path, baseScore, profile = "default") {
268
+ // ブースト無効
269
+ if (profile === "none") {
270
+ return baseScore;
271
+ }
272
+ // ドキュメントモード: ドキュメントを優遇、実装ファイルを軽度減点
273
+ if (profile === "docs") {
274
+ if (path.endsWith(".md") || path.endsWith(".yaml") || path.endsWith(".yml")) {
275
+ return baseScore * 1.5; // ドキュメント優遇
276
+ }
277
+ if (path.startsWith("src/") && (path.endsWith(".ts") || path.endsWith(".js"))) {
278
+ return baseScore * 0.7; // 実装ファイル軽度減点
279
+ }
280
+ return baseScore;
281
+ }
282
+ // デフォルトモード: 実装ファイルを優遇、ドキュメントを減点
283
+ if (path.startsWith("src/") && (path.endsWith(".ts") || path.endsWith(".js"))) {
284
+ return baseScore * 1.5; // 実装ファイル優遇
285
+ }
286
+ if (path.startsWith("tests/") && path.endsWith(".ts")) {
287
+ return baseScore * 1.2; // テストファイル軽度優遇
288
+ }
289
+ if (path.endsWith(".md") || path.endsWith(".yaml") || path.endsWith(".yml")) {
290
+ return baseScore * 0.5; // ドキュメント減点
291
+ }
292
+ return baseScore;
293
+ }
294
+ export async function filesSearch(context, params) {
295
+ const { db, repoId } = context;
296
+ const { query } = params;
297
+ if (!query || query.trim().length === 0) {
298
+ throw new Error("files.search requires a non-empty query. Provide a search keyword to continue.");
299
+ }
300
+ const limit = normalizeLimit(params.limit);
301
+ const hasFTS = context.features?.fts ?? false;
302
+ let sql;
303
+ let values;
304
+ if (hasFTS) {
305
+ // FTS拡張利用可能: fts_main_blob.match_bm25 を使用
306
+ const conditions = ["f.repo_id = ?"];
307
+ values = [repoId];
308
+ // 言語・拡張子フィルタ
309
+ if (params.lang) {
310
+ conditions.push("COALESCE(f.lang, '') = ?");
311
+ values.push(params.lang);
312
+ }
313
+ if (params.ext) {
314
+ conditions.push("COALESCE(f.ext, '') = ?");
315
+ values.push(params.ext);
316
+ }
317
+ if (params.path_prefix) {
318
+ conditions.push("f.path LIKE ?");
319
+ values.push(`${params.path_prefix}%`);
320
+ }
321
+ // FTS検索(BM25スコアリング)
322
+ sql = `
323
+ SELECT f.path, f.lang, f.ext, b.content, fts.score
324
+ FROM file f
325
+ JOIN blob b ON b.hash = f.blob_hash
326
+ JOIN (
327
+ SELECT hash, fts_main_blob.match_bm25(hash, ?) AS score
328
+ FROM blob
329
+ WHERE score IS NOT NULL
330
+ ) fts ON fts.hash = b.hash
331
+ WHERE ${conditions.join(" AND ")}
332
+ ORDER BY fts.score DESC
333
+ LIMIT ?
334
+ `;
335
+ values.unshift(query); // FTSクエリを先頭に追加
336
+ values.push(limit);
337
+ }
338
+ else {
339
+ // FTS拡張利用不可: ILIKE検索(Phase 1の単語分割ロジック)
340
+ const conditions = ["f.repo_id = ?", "b.content IS NOT NULL"];
341
+ values = [repoId];
342
+ const words = splitQueryWords(query);
343
+ if (words.length === 1) {
344
+ conditions.push("b.content ILIKE '%' || ? || '%'");
345
+ values.push(query);
346
+ }
347
+ else {
348
+ const wordConditions = words.map(() => "b.content ILIKE '%' || ? || '%'");
349
+ conditions.push(`(${wordConditions.join(" OR ")})`);
350
+ values.push(...words);
351
+ }
352
+ if (params.lang) {
353
+ conditions.push("COALESCE(f.lang, '') = ?");
354
+ values.push(params.lang);
355
+ }
356
+ if (params.ext) {
357
+ conditions.push("COALESCE(f.ext, '') = ?");
358
+ values.push(params.ext);
359
+ }
360
+ if (params.path_prefix) {
361
+ conditions.push("f.path LIKE ?");
362
+ values.push(`${params.path_prefix}%`);
363
+ }
364
+ sql = `
365
+ SELECT f.path, f.lang, f.ext, b.content
366
+ FROM file f
367
+ JOIN blob b ON b.hash = f.blob_hash
368
+ WHERE ${conditions.join(" AND ")}
369
+ ORDER BY f.path
370
+ LIMIT ?
371
+ `;
372
+ values.push(limit);
373
+ }
374
+ const rows = await db.all(sql, values);
375
+ const boostProfile = params.boost_profile ?? "default";
376
+ return rows
377
+ .map((row) => {
378
+ const { preview, line } = buildPreview(row.content ?? "", query);
379
+ const baseScore = row.score ?? 1.0; // FTS時はBM25スコア、ILIKE時は1.0
380
+ const boostedScore = applyFileTypeBoost(row.path, baseScore, boostProfile);
381
+ return {
382
+ path: row.path,
383
+ preview,
384
+ matchLine: line,
385
+ lang: row.lang,
386
+ ext: row.ext,
387
+ score: boostedScore,
388
+ };
389
+ })
390
+ .sort((a, b) => b.score - a.score); // スコアの高い順に再ソート
391
+ }
392
+ export async function snippetsGet(context, params) {
393
+ const { db, repoId } = context;
394
+ if (!params.path) {
395
+ throw new Error("snippets.get requires a file path. Specify a tracked text file path to continue.");
396
+ }
397
+ const rows = await db.all(`
398
+ SELECT f.path, f.lang, f.ext, f.is_binary, b.content
399
+ FROM file f
400
+ JOIN blob b ON b.hash = f.blob_hash
401
+ WHERE f.repo_id = ? AND f.path = ?
402
+ LIMIT 1
403
+ `, [repoId, params.path]);
404
+ if (rows.length === 0) {
405
+ throw new Error("Requested snippet file was not indexed. Re-run the indexer or choose another path.");
406
+ }
407
+ const row = rows[0];
408
+ if (!row) {
409
+ throw new Error("Requested snippet file was not indexed. Re-run the indexer or choose another path.");
410
+ }
411
+ if (row.is_binary) {
412
+ throw new Error("Binary snippets are not supported. Choose a text file to preview its content.");
413
+ }
414
+ if (row.content === null) {
415
+ throw new Error("Snippet content is unavailable. Re-run the indexer to refresh DuckDB state.");
416
+ }
417
+ const lines = row.content.split(/\r?\n/);
418
+ const totalLines = lines.length;
419
+ const snippetRows = await db.all(`
420
+ SELECT s.snippet_id, s.start_line, s.end_line, s.symbol_id, sym.name AS symbol_name, sym.kind AS symbol_kind
421
+ FROM snippet s
422
+ LEFT JOIN symbol sym
423
+ ON sym.repo_id = s.repo_id
424
+ AND sym.path = s.path
425
+ AND sym.symbol_id = s.symbol_id
426
+ WHERE s.repo_id = ? AND s.path = ?
427
+ ORDER BY s.start_line
428
+ `, [repoId, params.path]);
429
+ const requestedStart = params.start_line ?? 1;
430
+ const requestedEnd = params.end_line ?? Math.min(totalLines, requestedStart + DEFAULT_SNIPPET_WINDOW - 1);
431
+ const useSymbolSnippets = snippetRows.length > 0 && params.end_line === undefined;
432
+ let snippetSelection = null;
433
+ if (useSymbolSnippets) {
434
+ snippetSelection =
435
+ snippetRows.find((snippet) => requestedStart >= snippet.start_line && requestedStart <= snippet.end_line) ?? null;
436
+ if (!snippetSelection) {
437
+ const firstSnippet = snippetRows[0];
438
+ if (firstSnippet && requestedStart < firstSnippet.start_line) {
439
+ snippetSelection = firstSnippet;
440
+ }
441
+ else {
442
+ snippetSelection = snippetRows[snippetRows.length - 1] ?? null;
443
+ }
444
+ }
445
+ }
446
+ let startLine;
447
+ let endLine;
448
+ let symbolName = null;
449
+ let symbolKind = null;
450
+ if (snippetSelection) {
451
+ startLine = snippetSelection.start_line;
452
+ endLine = snippetSelection.end_line;
453
+ symbolName = snippetSelection.symbol_name;
454
+ symbolKind = snippetSelection.symbol_kind;
455
+ }
456
+ else {
457
+ startLine = Math.max(1, Math.min(totalLines, requestedStart));
458
+ endLine = Math.max(startLine, Math.min(totalLines, requestedEnd));
459
+ }
460
+ const snippetContent = lines.slice(startLine - 1, endLine).join("\n");
461
+ return {
462
+ path: row.path,
463
+ startLine,
464
+ endLine,
465
+ content: snippetContent,
466
+ totalLines,
467
+ symbolName,
468
+ symbolKind,
469
+ };
470
+ }
471
+ export async function contextBundle(context, params) {
472
+ const { db, repoId } = context;
473
+ const goal = params.goal?.trim() ?? "";
474
+ if (goal.length === 0) {
475
+ throw new Error("context.bundle requires a non-empty goal. Describe your objective to receive context.");
476
+ }
477
+ const limit = normalizeBundleLimit(params.limit);
478
+ const artifacts = params.artifacts ?? {};
479
+ // スコアリング重みをロード(将来的には設定ファイルや引数から)
480
+ const profileName = coerceProfileName(params.profile ?? null);
481
+ const weights = loadScoringProfile(profileName);
482
+ const keywordSources = [goal];
483
+ if (artifacts.failing_tests && artifacts.failing_tests.length > 0) {
484
+ keywordSources.push(artifacts.failing_tests.join(" "));
485
+ }
486
+ if (artifacts.last_diff) {
487
+ keywordSources.push(artifacts.last_diff);
488
+ }
489
+ if (artifacts.editing_path) {
490
+ keywordSources.push(artifacts.editing_path);
491
+ }
492
+ const semanticSeed = keywordSources.join(" ");
493
+ const queryEmbedding = generateEmbedding(semanticSeed)?.values ?? null;
494
+ let keywords = extractKeywords(semanticSeed);
495
+ if (keywords.length === 0 && artifacts.editing_path) {
496
+ const pathSegments = artifacts.editing_path
497
+ .split(/[/_.-]/)
498
+ .map((segment) => segment.toLowerCase())
499
+ .filter((segment) => segment.length >= 3 && !STOP_WORDS.has(segment));
500
+ keywords = pathSegments.slice(0, MAX_KEYWORDS);
501
+ }
502
+ const candidates = new Map();
503
+ const stringMatchSeeds = new Set();
504
+ const fileCache = new Map();
505
+ for (const keyword of keywords) {
506
+ const rows = await db.all(`
507
+ SELECT f.path, f.lang, f.ext, f.is_binary, b.content, fe.vector_json, fe.dims AS vector_dims
508
+ FROM file f
509
+ JOIN blob b ON b.hash = f.blob_hash
510
+ LEFT JOIN file_embedding fe
511
+ ON fe.repo_id = f.repo_id
512
+ AND fe.path = f.path
513
+ WHERE f.repo_id = ?
514
+ AND f.is_binary = FALSE
515
+ AND b.content ILIKE '%' || ? || '%'
516
+ ORDER BY f.path
517
+ LIMIT ?
518
+ `, [repoId, keyword, MAX_MATCHES_PER_KEYWORD]);
519
+ for (const row of rows) {
520
+ if (row.content === null) {
521
+ continue;
522
+ }
523
+ const candidate = ensureCandidate(candidates, row.path);
524
+ candidate.score += weights.textMatch;
525
+ candidate.reasons.add(`text:${keyword}`);
526
+ // ファイルタイプブーストを適用(boost_profileに応じて)
527
+ const boostProfile = params.boost_profile ?? "default";
528
+ if (boostProfile === "docs") {
529
+ // ドキュメントモード
530
+ if (row.path.endsWith(".md") || row.path.endsWith(".yaml") || row.path.endsWith(".yml")) {
531
+ candidate.score += 0.5; // ドキュメントに追加ボーナス
532
+ candidate.reasons.add("boost:doc-file");
533
+ }
534
+ if (row.path.startsWith("src/") && row.ext === ".ts") {
535
+ candidate.score -= 0.2; // 実装ファイルに軽度ペナルティ
536
+ candidate.reasons.add("penalty:impl-file");
537
+ }
538
+ }
539
+ else if (boostProfile === "default") {
540
+ // デフォルトモード
541
+ if (row.path.startsWith("src/") && row.ext === ".ts") {
542
+ candidate.score += 0.5; // 実装ファイルに追加ボーナス
543
+ candidate.reasons.add("boost:impl-file");
544
+ }
545
+ if (row.path.endsWith(".md") || row.path.endsWith(".yaml") || row.path.endsWith(".yml")) {
546
+ candidate.score -= 0.3; // ドキュメントにペナルティ
547
+ candidate.reasons.add("penalty:doc-file");
548
+ }
549
+ }
550
+ // boostProfile === "none" の場合はブースト適用なし
551
+ const { line } = buildPreview(row.content, keyword);
552
+ candidate.matchLine =
553
+ candidate.matchLine === null ? line : Math.min(candidate.matchLine, line);
554
+ candidate.content ??= row.content;
555
+ candidate.lang ??= row.lang;
556
+ candidate.ext ??= row.ext;
557
+ candidate.totalLines ??= row.content.length === 0 ? 0 : row.content.split(/\r?\n/).length;
558
+ candidate.embedding ??= parseEmbedding(row.vector_json ?? null, row.vector_dims ?? null);
559
+ stringMatchSeeds.add(row.path);
560
+ if (!fileCache.has(row.path)) {
561
+ fileCache.set(row.path, {
562
+ content: row.content,
563
+ lang: row.lang,
564
+ ext: row.ext,
565
+ totalLines: candidate.totalLines ?? 0,
566
+ embedding: candidate.embedding,
567
+ });
568
+ }
569
+ }
570
+ }
571
+ if (artifacts.editing_path) {
572
+ const editingCandidate = ensureCandidate(candidates, artifacts.editing_path);
573
+ editingCandidate.score += weights.editingPath;
574
+ editingCandidate.reasons.add("artifact:editing_path");
575
+ editingCandidate.matchLine ??= 1;
576
+ }
577
+ // SQL injection防御: ファイルパスの検証パターン
578
+ const SAFE_PATH_PATTERN = /^[a-zA-Z0-9_.\-/]+$/;
579
+ const dependencySeeds = new Set();
580
+ for (const pathSeed of stringMatchSeeds) {
581
+ if (!SAFE_PATH_PATTERN.test(pathSeed)) {
582
+ console.warn(`Skipping potentially unsafe path in dependency seeds: ${pathSeed}`);
583
+ continue;
584
+ }
585
+ dependencySeeds.add(pathSeed);
586
+ if (dependencySeeds.size >= MAX_DEPENDENCY_SEEDS) {
587
+ break;
588
+ }
589
+ }
590
+ if (artifacts.editing_path) {
591
+ if (!SAFE_PATH_PATTERN.test(artifacts.editing_path)) {
592
+ throw new Error(`Invalid editing_path format. Path must contain only alphanumeric characters, underscores, dots, hyphens, and forward slashes.`);
593
+ }
594
+ dependencySeeds.add(artifacts.editing_path);
595
+ }
596
+ if (dependencySeeds.size > 0) {
597
+ // SQL injection防御: プレースホルダー生成前にサイズを検証
598
+ if (dependencySeeds.size > MAX_DEPENDENCY_SEEDS_QUERY_LIMIT) {
599
+ throw new Error(`Too many dependency seeds: ${dependencySeeds.size} (max ${MAX_DEPENDENCY_SEEDS_QUERY_LIMIT}). Narrow your search criteria.`);
600
+ }
601
+ const placeholders = Array.from(dependencySeeds, () => "?").join(", ");
602
+ // 防御的チェック: プレースホルダーが正しい形式であることを確認
603
+ // 期待される形式: "?, ?, ..." (クエスチョンマーク、カンマ、スペースのみ)
604
+ if (!/^(\?)(,\s*\?)*$/.test(placeholders)) {
605
+ throw new Error("Invalid placeholder generation detected. Operation aborted for safety.");
606
+ }
607
+ const depRows = await db.all(`
608
+ SELECT src_path, dst_kind, dst, rel
609
+ FROM dependency
610
+ WHERE repo_id = ? AND src_path IN (${placeholders})
611
+ `, [repoId, ...dependencySeeds]);
612
+ for (const dep of depRows) {
613
+ if (dep.dst_kind !== "path") {
614
+ continue;
615
+ }
616
+ const candidate = ensureCandidate(candidates, dep.dst);
617
+ candidate.score += weights.dependency;
618
+ candidate.reasons.add(`dep:${dep.src_path}`);
619
+ }
620
+ }
621
+ if (artifacts.editing_path) {
622
+ const directory = path.posix.dirname(artifacts.editing_path);
623
+ if (directory && directory !== ".") {
624
+ const nearRows = await db.all(`
625
+ SELECT path
626
+ FROM file
627
+ WHERE repo_id = ?
628
+ AND is_binary = FALSE
629
+ AND path LIKE ?
630
+ ORDER BY path
631
+ LIMIT ?
632
+ `, [repoId, `${directory}/%`, NEARBY_LIMIT + 1]);
633
+ for (const near of nearRows) {
634
+ if (near.path === artifacts.editing_path) {
635
+ continue;
636
+ }
637
+ const candidate = ensureCandidate(candidates, near.path);
638
+ candidate.score += weights.proximity;
639
+ candidate.reasons.add(`near:${directory}`);
640
+ }
641
+ }
642
+ }
643
+ const materializedCandidates = [];
644
+ for (const candidate of candidates.values()) {
645
+ if (!candidate.content) {
646
+ const cached = fileCache.get(candidate.path);
647
+ if (cached) {
648
+ candidate.content = cached.content;
649
+ candidate.lang = cached.lang;
650
+ candidate.ext = cached.ext;
651
+ candidate.totalLines = cached.totalLines;
652
+ candidate.embedding = cached.embedding;
653
+ }
654
+ else {
655
+ const loaded = await loadFileContent(db, repoId, candidate.path);
656
+ if (!loaded) {
657
+ continue;
658
+ }
659
+ candidate.content = loaded.content;
660
+ candidate.lang = loaded.lang;
661
+ candidate.ext = loaded.ext;
662
+ candidate.totalLines = loaded.totalLines;
663
+ candidate.embedding = loaded.embedding;
664
+ fileCache.set(candidate.path, loaded);
665
+ }
666
+ }
667
+ materializedCandidates.push(candidate);
668
+ }
669
+ if (materializedCandidates.length === 0) {
670
+ return { context: [], tokens_estimate: 0 };
671
+ }
672
+ applyStructuralScores(materializedCandidates, queryEmbedding, weights.structural);
673
+ const sortedCandidates = materializedCandidates
674
+ .sort((a, b) => {
675
+ if (b.score === a.score) {
676
+ return a.path.localeCompare(b.path);
677
+ }
678
+ return b.score - a.score;
679
+ })
680
+ .slice(0, limit);
681
+ const maxScore = Math.max(...sortedCandidates.map((candidate) => candidate.score));
682
+ const results = [];
683
+ for (const candidate of sortedCandidates) {
684
+ if (!candidate.content) {
685
+ continue;
686
+ }
687
+ const snippets = await db.all(`
688
+ SELECT s.snippet_id, s.start_line, s.end_line, s.symbol_id, sym.name AS symbol_name, sym.kind AS symbol_kind
689
+ FROM snippet s
690
+ LEFT JOIN symbol sym
691
+ ON sym.repo_id = s.repo_id
692
+ AND sym.path = s.path
693
+ AND sym.symbol_id = s.symbol_id
694
+ WHERE s.repo_id = ? AND s.path = ?
695
+ ORDER BY s.start_line
696
+ `, [repoId, candidate.path]);
697
+ const selected = selectSnippet(snippets, candidate.matchLine);
698
+ let startLine;
699
+ let endLine;
700
+ if (selected) {
701
+ startLine = selected.start_line;
702
+ endLine = selected.end_line;
703
+ }
704
+ else {
705
+ const totalLines = candidate.totalLines ?? 0;
706
+ const matchLine = candidate.matchLine ?? 1;
707
+ const windowHalf = Math.floor(FALLBACK_SNIPPET_WINDOW / 2);
708
+ startLine = Math.max(1, matchLine - windowHalf);
709
+ endLine = Math.min(totalLines === 0 ? matchLine + windowHalf : totalLines, startLine + FALLBACK_SNIPPET_WINDOW - 1);
710
+ }
711
+ if (endLine < startLine) {
712
+ endLine = startLine;
713
+ }
714
+ const preview = buildSnippetPreview(candidate.content, startLine, endLine);
715
+ const reasons = new Set(candidate.reasons);
716
+ if (selected && selected.symbol_name) {
717
+ reasons.add(`symbol:${selected.symbol_name}`);
718
+ }
719
+ const normalizedScore = maxScore > 0 ? candidate.score / maxScore : 0;
720
+ results.push({
721
+ path: candidate.path,
722
+ range: [startLine, endLine],
723
+ preview,
724
+ why: Array.from(reasons).sort(),
725
+ score: Number.isFinite(normalizedScore) ? normalizedScore : 0,
726
+ });
727
+ }
728
+ // コンテンツベースのトークン推定を使用(より正確)
729
+ const tokensEstimate = results.reduce((acc, item) => {
730
+ const candidate = sortedCandidates.find((c) => c.path === item.path);
731
+ if (candidate && candidate.content) {
732
+ return acc + estimateTokensFromContent(candidate.content, item.range[0], item.range[1]);
733
+ }
734
+ // フォールバック: 行ベース推定(コンテンツが利用不可の場合)
735
+ const lineCount = Math.max(1, item.range[1] - item.range[0] + 1);
736
+ return acc + lineCount * 4;
737
+ }, 0);
738
+ return { context: results, tokens_estimate: tokensEstimate };
739
+ }
740
+ export async function semanticRerank(context, params) {
741
+ const text = params.text?.trim() ?? "";
742
+ if (text.length === 0) {
743
+ throw new Error("semantic.rerank requires non-empty text. Describe the intent to compute semantic similarity.");
744
+ }
745
+ if (!Array.isArray(params.candidates) || params.candidates.length === 0) {
746
+ return { candidates: [] };
747
+ }
748
+ const uniqueCandidates = [];
749
+ const seenPaths = new Set();
750
+ for (const candidate of params.candidates) {
751
+ if (!candidate || typeof candidate.path !== "string" || candidate.path.length === 0) {
752
+ continue;
753
+ }
754
+ if (seenPaths.has(candidate.path)) {
755
+ continue;
756
+ }
757
+ seenPaths.add(candidate.path);
758
+ uniqueCandidates.push(candidate);
759
+ if (uniqueCandidates.length >= MAX_RERANK_LIMIT) {
760
+ break;
761
+ }
762
+ }
763
+ if (uniqueCandidates.length === 0) {
764
+ return { candidates: [] };
765
+ }
766
+ const limitRaw = params.k ?? uniqueCandidates.length;
767
+ const limit = Math.max(1, Math.min(MAX_RERANK_LIMIT, Math.floor(limitRaw)));
768
+ const profileName = coerceProfileName(params.profile ?? null);
769
+ const weights = loadScoringProfile(profileName);
770
+ const structuralWeight = weights.structural;
771
+ const queryEmbedding = generateEmbedding(text)?.values ?? null;
772
+ let embeddingMap = new Map();
773
+ if (queryEmbedding && structuralWeight > 0) {
774
+ const paths = uniqueCandidates.map((candidate) => candidate.path);
775
+ embeddingMap = await fetchEmbeddingMap(context.db, context.repoId, paths);
776
+ }
777
+ const scored = uniqueCandidates.map((candidate) => {
778
+ const base = typeof candidate.score === "number" && Number.isFinite(candidate.score) ? candidate.score : 0;
779
+ let semantic = 0;
780
+ if (queryEmbedding && structuralWeight > 0) {
781
+ const embedding = embeddingMap.get(candidate.path);
782
+ if (embedding) {
783
+ const similarity = structuralSimilarity(queryEmbedding, embedding);
784
+ if (Number.isFinite(similarity) && similarity > 0) {
785
+ semantic = similarity;
786
+ }
787
+ }
788
+ }
789
+ const combined = base + structuralWeight * semantic;
790
+ return {
791
+ path: candidate.path,
792
+ base,
793
+ semantic,
794
+ combined,
795
+ };
796
+ });
797
+ const sorted = scored.sort((a, b) => {
798
+ if (b.combined === a.combined) {
799
+ if (b.semantic === a.semantic) {
800
+ return a.path.localeCompare(b.path);
801
+ }
802
+ return b.semantic - a.semantic;
803
+ }
804
+ return b.combined - a.combined;
805
+ });
806
+ return { candidates: sorted.slice(0, limit) };
807
+ }
808
+ export async function depsClosure(context, params) {
809
+ const { db, repoId } = context;
810
+ if (!params.path) {
811
+ throw new Error("deps.closure requires a file path. Provide a tracked source file path to continue.");
812
+ }
813
+ const direction = params.direction ?? "outbound";
814
+ const maxDepth = params.max_depth ?? 3;
815
+ const includePackages = params.include_packages ?? true;
816
+ const dependencyRows = await db.all(`
817
+ SELECT src_path, dst_kind, dst, rel
818
+ FROM dependency
819
+ WHERE repo_id = ?
820
+ `, [repoId]);
821
+ // outbound: このファイルが使用する依存関係
822
+ const outbound = new Map();
823
+ // inbound: このファイルを使用しているファイル
824
+ const inbound = new Map();
825
+ for (const row of dependencyRows) {
826
+ // outbound マップ構築
827
+ if (!outbound.has(row.src_path)) {
828
+ outbound.set(row.src_path, []);
829
+ }
830
+ outbound.get(row.src_path)?.push(row);
831
+ // inbound マップ構築(dst が path の場合のみ)
832
+ if (row.dst_kind === "path") {
833
+ if (!inbound.has(row.dst)) {
834
+ inbound.set(row.dst, []);
835
+ }
836
+ inbound.get(row.dst)?.push(row);
837
+ }
838
+ }
839
+ const queue = [{ path: params.path, depth: 0 }];
840
+ const visitedPaths = new Set([params.path]);
841
+ const nodeDepth = new Map();
842
+ const edgeSet = new Map();
843
+ const recordNode = (node) => {
844
+ const key = `${node.kind}:${node.target}`;
845
+ const existing = nodeDepth.get(key);
846
+ if (!existing || node.depth < existing.depth) {
847
+ nodeDepth.set(key, { ...node });
848
+ }
849
+ };
850
+ const recordEdge = (edge) => {
851
+ const key = `${edge.from}->${edge.to}:${edge.kind}:${edge.rel}`;
852
+ const existing = edgeSet.get(key);
853
+ if (!existing || edge.depth < existing.depth) {
854
+ edgeSet.set(key, { ...edge });
855
+ }
856
+ };
857
+ recordNode({ kind: "path", target: params.path, depth: 0 });
858
+ while (queue.length > 0) {
859
+ const current = queue.shift();
860
+ if (current.depth >= maxDepth) {
861
+ continue;
862
+ }
863
+ // direction に応じて使用するマップを選択
864
+ const edgeMap = direction === "inbound" ? inbound : outbound;
865
+ const edges = edgeMap.get(current.path) ?? [];
866
+ for (const edge of edges) {
867
+ const nextDepth = current.depth + 1;
868
+ if (direction === "inbound") {
869
+ // inbound: edge.src_path がこのファイルを使用している
870
+ recordEdge({
871
+ from: edge.src_path,
872
+ to: current.path,
873
+ kind: "path",
874
+ rel: edge.rel,
875
+ depth: nextDepth,
876
+ });
877
+ recordNode({ kind: "path", target: edge.src_path, depth: nextDepth });
878
+ if (!visitedPaths.has(edge.src_path)) {
879
+ visitedPaths.add(edge.src_path);
880
+ queue.push({ path: edge.src_path, depth: nextDepth });
881
+ }
882
+ }
883
+ else {
884
+ // outbound: このファイルが edge.dst を使用している
885
+ if (edge.dst_kind === "path") {
886
+ recordEdge({
887
+ from: current.path,
888
+ to: edge.dst,
889
+ kind: "path",
890
+ rel: edge.rel,
891
+ depth: nextDepth,
892
+ });
893
+ recordNode({ kind: "path", target: edge.dst, depth: nextDepth });
894
+ if (!visitedPaths.has(edge.dst)) {
895
+ visitedPaths.add(edge.dst);
896
+ queue.push({ path: edge.dst, depth: nextDepth });
897
+ }
898
+ }
899
+ else if (edge.dst_kind === "package" && includePackages) {
900
+ recordEdge({
901
+ from: current.path,
902
+ to: edge.dst,
903
+ kind: "package",
904
+ rel: edge.rel,
905
+ depth: nextDepth,
906
+ });
907
+ recordNode({ kind: "package", target: edge.dst, depth: nextDepth });
908
+ }
909
+ }
910
+ }
911
+ }
912
+ const nodes = Array.from(nodeDepth.values()).sort((a, b) => {
913
+ if (a.depth === b.depth) {
914
+ return a.target.localeCompare(b.target);
915
+ }
916
+ return a.depth - b.depth;
917
+ });
918
+ const edges = Array.from(edgeSet.values()).sort((a, b) => {
919
+ if (a.depth === b.depth) {
920
+ const fromCmp = a.from.localeCompare(b.from);
921
+ if (fromCmp !== 0) {
922
+ return fromCmp;
923
+ }
924
+ return a.to.localeCompare(b.to);
925
+ }
926
+ return a.depth - b.depth;
927
+ });
928
+ return {
929
+ root: params.path,
930
+ direction,
931
+ nodes,
932
+ edges,
933
+ };
934
+ }
935
+ export async function resolveRepoId(db, repoRoot) {
936
+ try {
937
+ const rows = await db.all("SELECT id FROM repo WHERE root = ?", [repoRoot]);
938
+ if (rows.length === 0) {
939
+ throw new Error("Target repository is missing from DuckDB. Run the indexer before starting the server.");
940
+ }
941
+ const row = rows[0];
942
+ if (!row) {
943
+ throw new Error("Failed to retrieve repository record. Database returned empty result.");
944
+ }
945
+ return row.id;
946
+ }
947
+ catch (error) {
948
+ if (error instanceof Error && error.message.includes("Table with name repo")) {
949
+ throw new Error("Target repository is missing from DuckDB. Run the indexer before starting the server.");
950
+ }
951
+ throw error;
952
+ }
953
+ }
954
+ //# sourceMappingURL=handlers.js.map