-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathschema.ts
More file actions
445 lines (395 loc) · 18.9 KB
/
schema.ts
File metadata and controls
445 lines (395 loc) · 18.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
// Config schema without zod dependency to avoid version conflicts with OpenCode SDK
import { DEFAULT_INCLUDE, DEFAULT_EXCLUDE, EMBEDDING_MODELS, DEFAULT_PROVIDER_MODELS } from "./constants.js";
import { substituteEnvString } from "./env-substitution.js";
export type IndexScope = "project" | "global";
export interface IndexingConfig {
autoIndex: boolean;
watchFiles: boolean;
maxFileSize: number;
maxChunksPerFile: number;
semanticOnly: boolean;
retries: number;
retryDelayMs: number;
autoGc: boolean;
gcIntervalDays: number;
gcOrphanThreshold: number;
/**
* When true (default), requires a project marker (.git, package.json, Cargo.toml, etc.)
* to be present before enabling file watching and auto-indexing.
*/
requireProjectMarker: boolean;
/**
* Max directory traversal depth. -1 = unlimited, 0 = only files in the root dir,
* 1 = one level of subdirectories, etc. Default: 5
*/
maxDepth: number;
/**
* Max number of files to index per directory. Always picks the smallest files first.
* Default: 100
*/
maxFilesPerDirectory: number;
/**
* When a file hits maxChunksPerFile, fallback to text-based (chunk_by_lines) parsing
* instead of skipping the rest of the file. Default: true
*/
fallbackToTextOnMaxChunks: boolean;
}
export interface SearchConfig {
maxResults: number;
minScore: number;
includeContext: boolean;
hybridWeight: number;
fusionStrategy: "weighted" | "rrf";
rrfK: number;
rerankTopN: number;
contextLines: number;
}
export type RerankerProvider = "cohere" | "jina" | "custom";
export interface RerankerConfig {
/** Whether to enable reranking. Default: false */
enabled: boolean;
/** Provider shortcut for hosted rerank APIs. Use 'custom' to provide only baseUrl. */
provider: RerankerProvider;
/** Model name for reranking */
model: string;
/** Base URL of the rerank API endpoint */
baseUrl: string;
/** API key for the rerank service */
apiKey?: string;
/** Number of top documents to rerank */
topN: number;
/** Request timeout in milliseconds */
timeoutMs: number;
}
export type LogLevel = "error" | "warn" | "info" | "debug";
export interface DebugConfig {
enabled: boolean;
logLevel: LogLevel;
logSearch: boolean;
logEmbedding: boolean;
logCache: boolean;
logGc: boolean;
logBranch: boolean;
metrics: boolean;
}
export interface CustomProviderConfig {
/** Base URL of the OpenAI-compatible embeddings API. The path /embeddings is appended automatically (e.g. "http://localhost:11434/v1", "https://api.example.com/v1") */
baseUrl: string;
/** Model name to send in the API request (e.g. "nomic-embed-text") */
model: string;
/** Vector dimensions the model produces (e.g. 768 for nomic-embed-text) */
dimensions: number;
/** Optional API key for authenticated endpoints */
apiKey?: string;
/** Max tokens per input text (default: 8192) */
maxTokens?: number;
/** Request timeout in milliseconds (default: 30000) */
timeoutMs?: number;
/** Max concurrent embedding requests (default: 3). Increase for local servers like llama.cpp or vLLM. */
concurrency?: number;
/** Minimum delay between requests in milliseconds (default: 1000). Set to 0 for local servers. */
requestIntervalMs?: number;
maxBatchSize?: number;
max_batch_size?: number;
}
export interface CodebaseIndexConfig {
embeddingProvider: EmbeddingProvider | 'custom' | 'auto';
embeddingModel?: EmbeddingModelName;
/** Configuration for custom OpenAI-compatible embedding providers (required when embeddingProvider is 'custom') */
customProvider?: CustomProviderConfig;
scope: IndexScope;
indexing?: Partial<IndexingConfig>;
search?: Partial<SearchConfig>;
debug?: Partial<DebugConfig>;
/** Reranking configuration for improving search result quality */
reranker?: Partial<RerankerConfig>;
/** External directories to index as knowledge bases (absolute or relative paths) */
knowledgeBases?: string[];
/** Override the default include patterns (replaces defaults) */
include: string[];
/** Override the default exclude patterns (replaces defaults) */
exclude: string[];
/** Additional file patterns to include (extends defaults) */
additionalInclude?: string[];
}
export type ParsedCodebaseIndexConfig = CodebaseIndexConfig & {
indexing: IndexingConfig;
search: SearchConfig;
debug: DebugConfig;
reranker?: RerankerConfig;
knowledgeBases: string[];
additionalInclude: string[];
};
function getDefaultIndexingConfig(): IndexingConfig {
return {
autoIndex: false,
watchFiles: true,
maxFileSize: 1048576,
maxChunksPerFile: 100,
semanticOnly: false,
retries: 3,
retryDelayMs: 1000,
autoGc: true,
gcIntervalDays: 7,
gcOrphanThreshold: 100,
requireProjectMarker: true,
maxDepth: 5,
maxFilesPerDirectory: 100,
fallbackToTextOnMaxChunks: true,
};
}
function getDefaultSearchConfig(): SearchConfig {
return {
maxResults: 20,
minScore: 0.1,
includeContext: true,
hybridWeight: 0.5,
fusionStrategy: "rrf",
rrfK: 60,
rerankTopN: 20,
contextLines: 0,
};
}
function isValidFusionStrategy(value: unknown): value is SearchConfig["fusionStrategy"] {
return value === "weighted" || value === "rrf";
}
function isValidRerankerProvider(value: unknown): value is RerankerProvider {
return value === "cohere" || value === "jina" || value === "custom";
}
function getDefaultRerankerBaseUrl(provider: RerankerProvider): string {
switch (provider) {
case "cohere":
return "https://api.cohere.ai/v1";
case "jina":
return "https://api.jina.ai/v1";
case "custom":
return "";
}
}
function getDefaultDebugConfig(): DebugConfig {
return {
enabled: false,
logLevel: "info",
logSearch: true,
logEmbedding: true,
logCache: true,
logGc: true,
logBranch: true,
metrics: true,
};
}
const VALID_SCOPES: IndexScope[] = ["project", "global"];
const VALID_LOG_LEVELS: LogLevel[] = ["error", "warn", "info", "debug"];
function isValidProvider(value: unknown): value is EmbeddingProvider {
return typeof value === "string" && Object.keys(EMBEDDING_MODELS).includes(value);
}
export function isValidModel<P extends EmbeddingProvider>(
value: unknown,
provider: P
): value is ProviderModels[P] {
return typeof value === "string" && Object.keys(EMBEDDING_MODELS[provider]).includes(value);
}
function isValidScope(value: unknown): value is IndexScope {
return typeof value === "string" && VALID_SCOPES.includes(value as IndexScope);
}
function isStringArray(value: unknown): value is string[] {
return Array.isArray(value) && value.every(item => typeof item === "string");
}
function getResolvedString(value: unknown, keyPath: string): string | undefined {
if (typeof value !== "string") {
return undefined;
}
return substituteEnvString(value, keyPath);
}
function getResolvedStringArray(value: unknown, keyPath: string): string[] | undefined {
if (!isStringArray(value)) {
return undefined;
}
return value.map((item, index) => substituteEnvString(item, `${keyPath}[${index}]`));
}
function isValidLogLevel(value: unknown): value is LogLevel {
return typeof value === "string" && VALID_LOG_LEVELS.includes(value as LogLevel);
}
export function parseConfig(raw: unknown): ParsedCodebaseIndexConfig {
const input = (raw && typeof raw === "object" ? raw : {}) as Record<string, unknown>;
const embeddingProviderValue = getResolvedString(input.embeddingProvider, "$root.embeddingProvider");
const scopeValue = getResolvedString(input.scope, "$root.scope");
const includeValue = getResolvedStringArray(input.include, "$root.include");
const excludeValue = getResolvedStringArray(input.exclude, "$root.exclude");
const defaultIndexing = getDefaultIndexingConfig();
const defaultSearch = getDefaultSearchConfig();
const defaultDebug = getDefaultDebugConfig();
const rawIndexing = (input.indexing && typeof input.indexing === "object" ? input.indexing : {}) as Record<string, unknown>;
const indexing: IndexingConfig = {
autoIndex: typeof rawIndexing.autoIndex === "boolean" ? rawIndexing.autoIndex : defaultIndexing.autoIndex,
watchFiles: typeof rawIndexing.watchFiles === "boolean" ? rawIndexing.watchFiles : defaultIndexing.watchFiles,
maxFileSize: typeof rawIndexing.maxFileSize === "number" ? rawIndexing.maxFileSize : defaultIndexing.maxFileSize,
maxChunksPerFile: typeof rawIndexing.maxChunksPerFile === "number" ? Math.max(1, rawIndexing.maxChunksPerFile) : defaultIndexing.maxChunksPerFile,
semanticOnly: typeof rawIndexing.semanticOnly === "boolean" ? rawIndexing.semanticOnly : defaultIndexing.semanticOnly,
retries: typeof rawIndexing.retries === "number" ? rawIndexing.retries : defaultIndexing.retries,
retryDelayMs: typeof rawIndexing.retryDelayMs === "number" ? rawIndexing.retryDelayMs : defaultIndexing.retryDelayMs,
autoGc: typeof rawIndexing.autoGc === "boolean" ? rawIndexing.autoGc : defaultIndexing.autoGc,
gcIntervalDays: typeof rawIndexing.gcIntervalDays === "number" ? Math.max(1, rawIndexing.gcIntervalDays) : defaultIndexing.gcIntervalDays,
gcOrphanThreshold: typeof rawIndexing.gcOrphanThreshold === "number" ? Math.max(0, rawIndexing.gcOrphanThreshold) : defaultIndexing.gcOrphanThreshold,
requireProjectMarker: typeof rawIndexing.requireProjectMarker === "boolean" ? rawIndexing.requireProjectMarker : defaultIndexing.requireProjectMarker,
maxDepth: typeof rawIndexing.maxDepth === "number" ? (rawIndexing.maxDepth < -1 ? -1 : rawIndexing.maxDepth) : defaultIndexing.maxDepth,
maxFilesPerDirectory: typeof rawIndexing.maxFilesPerDirectory === "number" ? Math.max(1, rawIndexing.maxFilesPerDirectory) : defaultIndexing.maxFilesPerDirectory,
fallbackToTextOnMaxChunks: typeof rawIndexing.fallbackToTextOnMaxChunks === "boolean" ? rawIndexing.fallbackToTextOnMaxChunks : defaultIndexing.fallbackToTextOnMaxChunks,
};
const rawSearch = (input.search && typeof input.search === "object" ? input.search : {}) as Record<string, unknown>;
const search: SearchConfig = {
maxResults: typeof rawSearch.maxResults === "number" ? rawSearch.maxResults : defaultSearch.maxResults,
minScore: typeof rawSearch.minScore === "number" ? rawSearch.minScore : defaultSearch.minScore,
includeContext: typeof rawSearch.includeContext === "boolean" ? rawSearch.includeContext : defaultSearch.includeContext,
hybridWeight: typeof rawSearch.hybridWeight === "number" ? Math.min(1, Math.max(0, rawSearch.hybridWeight)) : defaultSearch.hybridWeight,
fusionStrategy: isValidFusionStrategy(rawSearch.fusionStrategy) ? rawSearch.fusionStrategy : defaultSearch.fusionStrategy,
rrfK: typeof rawSearch.rrfK === "number" ? Math.max(1, Math.floor(rawSearch.rrfK)) : defaultSearch.rrfK,
rerankTopN: typeof rawSearch.rerankTopN === "number" ? Math.min(200, Math.max(0, Math.floor(rawSearch.rerankTopN))) : defaultSearch.rerankTopN,
contextLines: typeof rawSearch.contextLines === "number" ? Math.min(50, Math.max(0, rawSearch.contextLines)) : defaultSearch.contextLines,
};
const rawDebug = (input.debug && typeof input.debug === "object" ? input.debug : {}) as Record<string, unknown>;
const debug: DebugConfig = {
enabled: typeof rawDebug.enabled === "boolean" ? rawDebug.enabled : defaultDebug.enabled,
logLevel: isValidLogLevel(rawDebug.logLevel) ? rawDebug.logLevel : defaultDebug.logLevel,
logSearch: typeof rawDebug.logSearch === "boolean" ? rawDebug.logSearch : defaultDebug.logSearch,
logEmbedding: typeof rawDebug.logEmbedding === "boolean" ? rawDebug.logEmbedding : defaultDebug.logEmbedding,
logCache: typeof rawDebug.logCache === "boolean" ? rawDebug.logCache : defaultDebug.logCache,
logGc: typeof rawDebug.logGc === "boolean" ? rawDebug.logGc : defaultDebug.logGc,
logBranch: typeof rawDebug.logBranch === "boolean" ? rawDebug.logBranch : defaultDebug.logBranch,
metrics: typeof rawDebug.metrics === "boolean" ? rawDebug.metrics : defaultDebug.metrics,
};
const rawKnowledgeBases = input.knowledgeBases;
const knowledgeBases: string[] = isStringArray(rawKnowledgeBases)
? rawKnowledgeBases.filter(p => typeof p === "string" && p.trim().length > 0).map(p => p.trim())
: [];
const rawAdditionalInclude = input.additionalInclude;
const additionalInclude: string[] = isStringArray(rawAdditionalInclude)
? rawAdditionalInclude.filter(p => typeof p === "string" && p.trim().length > 0).map(p => p.trim())
: [];
let embeddingProvider: EmbeddingProvider | 'custom' | 'auto';
let embeddingModel: EmbeddingModelName | undefined = undefined;
let customProvider: CustomProviderConfig | undefined = undefined;
let reranker: RerankerConfig | undefined = undefined;
if (embeddingProviderValue === 'custom') {
embeddingProvider = 'custom';
const rawCustom = (input.customProvider && typeof input.customProvider === 'object' ? input.customProvider : null) as Record<string, unknown> | null;
const baseUrlValue = getResolvedString(rawCustom?.baseUrl, "$root.customProvider.baseUrl");
const modelValue = getResolvedString(rawCustom?.model, "$root.customProvider.model");
const apiKeyValue = getResolvedString(rawCustom?.apiKey, "$root.customProvider.apiKey");
if (rawCustom && typeof baseUrlValue === 'string' && baseUrlValue.trim().length > 0 && typeof modelValue === 'string' && modelValue.trim().length > 0 && typeof rawCustom.dimensions === 'number' && Number.isInteger(rawCustom.dimensions) && rawCustom.dimensions > 0) {
customProvider = {
baseUrl: baseUrlValue.trim().replace(/\/+$/, ''),
model: modelValue,
dimensions: rawCustom.dimensions,
apiKey: apiKeyValue,
maxTokens: typeof rawCustom.maxTokens === 'number' ? rawCustom.maxTokens : undefined,
timeoutMs: typeof rawCustom.timeoutMs === 'number' ? Math.max(1000, rawCustom.timeoutMs) : undefined,
concurrency: typeof rawCustom.concurrency === 'number' ? Math.max(1, Math.floor(rawCustom.concurrency)) : undefined,
requestIntervalMs: typeof rawCustom.requestIntervalMs === 'number' ? Math.max(0, Math.floor(rawCustom.requestIntervalMs)) : undefined,
maxBatchSize: typeof rawCustom.maxBatchSize === 'number'
? Math.max(1, Math.floor(rawCustom.maxBatchSize))
: typeof rawCustom.max_batch_size === 'number'
? Math.max(1, Math.floor(rawCustom.max_batch_size))
: undefined,
};
// Warn if baseUrl doesn't end with an API version path like /v1.
// Note: using console.warn here because Logger isn't initialized yet at config parse time.
if (!/\/v\d+\/?$/.test(customProvider.baseUrl)) {
console.warn(
`[codebase-index] Warning: customProvider.baseUrl ("${customProvider.baseUrl}") does not end with an API version path like /v1. ` +
`The plugin appends /embeddings automatically, so the full URL will be "${customProvider.baseUrl}/embeddings". ` +
`If your provider expects /v1/embeddings, set baseUrl to "${customProvider.baseUrl}/v1".`
);
}
} else {
throw new Error(
"embeddingProvider is 'custom' but customProvider config is missing or invalid. " +
"Required fields: baseUrl (string), model (string), dimensions (positive integer)."
);
}
} else if (isValidProvider(embeddingProviderValue)) {
embeddingProvider = embeddingProviderValue;
const rawEmbeddingModel = input.embeddingModel;
if (typeof rawEmbeddingModel === "string") {
const embeddingModelValue = substituteEnvString(rawEmbeddingModel, "$root.embeddingModel");
if (embeddingModelValue) {
embeddingModel = isValidModel(embeddingModelValue, embeddingProvider) ? embeddingModelValue : DEFAULT_PROVIDER_MODELS[embeddingProvider];
}
} else if (rawEmbeddingModel) {
embeddingModel = DEFAULT_PROVIDER_MODELS[embeddingProvider];
}
} else {
embeddingProvider = 'auto';
}
const rawReranker = (input.reranker && typeof input.reranker === "object"
? input.reranker
: {}) as Record<string, unknown>;
const rerankerEnabled = typeof rawReranker.enabled === "boolean" ? rawReranker.enabled : false;
if (rerankerEnabled) {
const provider = isValidRerankerProvider(rawReranker.provider) ? rawReranker.provider : "custom";
const model = getResolvedString(rawReranker.model, "$root.reranker.model");
if (!model || model.trim().length === 0) {
throw new Error("reranker is enabled but reranker.model is missing or invalid.");
}
const configuredBaseUrl = getResolvedString(rawReranker.baseUrl, "$root.reranker.baseUrl");
const baseUrl = configuredBaseUrl?.trim() || getDefaultRerankerBaseUrl(provider);
if (baseUrl.length === 0) {
throw new Error("reranker is enabled but reranker.baseUrl is missing or invalid for provider 'custom'.");
}
const apiKey = getResolvedString(rawReranker.apiKey, "$root.reranker.apiKey");
if ((provider === "cohere" || provider === "jina") && (!apiKey || apiKey.trim().length === 0)) {
throw new Error(`reranker provider '${provider}' requires reranker.apiKey when enabled.`);
}
reranker = {
enabled: true,
provider,
model: model.trim(),
baseUrl: baseUrl.replace(/\/+$/, ""),
apiKey: apiKey?.trim() || undefined,
topN: typeof rawReranker.topN === "number" ? Math.min(50, Math.max(1, Math.floor(rawReranker.topN))) : 15,
timeoutMs: typeof rawReranker.timeoutMs === "number" ? Math.max(1000, Math.floor(rawReranker.timeoutMs)) : 10000,
};
}
return {
embeddingProvider,
embeddingModel,
customProvider,
scope: isValidScope(scopeValue) ? scopeValue : "project",
include: includeValue ?? DEFAULT_INCLUDE,
exclude: excludeValue ?? DEFAULT_EXCLUDE,
additionalInclude,
indexing,
search,
debug,
reranker,
knowledgeBases,
};
}
export function getDefaultModelForProvider(provider: EmbeddingProvider): EmbeddingModelInfo {
const models = EMBEDDING_MODELS[provider]
const providerDefault = DEFAULT_PROVIDER_MODELS[provider]
return models[providerDefault as keyof typeof models]
}
/**
* Built-in embedding providers derived from the static EMBEDDING_MODELS catalog.
* 'custom' is intentionally excluded from this union because it has no static model
* catalog — its model/dimensions/config are entirely user-defined at runtime via
* CustomProviderConfig. Code that handles all providers uses `EmbeddingProvider | 'custom'`.
*/
export type EmbeddingProvider = keyof typeof EMBEDDING_MODELS;
export const availableProviders: EmbeddingProvider[] = Object.keys(EMBEDDING_MODELS) as EmbeddingProvider[]
export type ProviderModels = {
[P in keyof typeof EMBEDDING_MODELS]: keyof (typeof EMBEDDING_MODELS)[P]
}
export type EmbeddingModelName = ProviderModels[keyof ProviderModels]
export type EmbeddingProviderModelInfo = {
[P in EmbeddingProvider]: (typeof EMBEDDING_MODELS)[P][keyof (typeof EMBEDDING_MODELS)[P]]
}
/** Shared fields across all embedding model types (built-in and custom) */
export interface BaseModelInfo {
model: string;
dimensions: number;
maxTokens: number;
costPer1MTokens: number;
}
export type EmbeddingModelInfo = EmbeddingProviderModelInfo[EmbeddingProvider]