Skip to content

Commit a65a4ba

Browse files
committed
fix: add input validation for codebaseIndexEmbedderModelDimension and fix test expectations
- Added validation in config-manager.ts to ensure codebaseIndexEmbedderModelDimension is a valid positive number - Fixed error message formatting in service-factory.ts to include bold/underline formatting for 'OpenAI-Compatible' - Updated tests to use config.modelDimension instead of openAiCompatibleOptions.modelDimension - Added check for vectorSize <= 0 in addition to undefined check
1 parent ab039b5 commit a65a4ba

File tree

3 files changed

+27
-7
lines changed

3 files changed

+27
-7
lines changed

src/services/code-index/__tests__/service-factory.spec.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -406,8 +406,10 @@ describe("CodeIndexServiceFactory", () => {
406406
const testConfig = {
407407
embedderProvider: "openai-compatible",
408408
modelId: testModelId,
409+
modelDimension: manualDimension,
409410
openAiCompatibleOptions: {
410-
modelDimension: manualDimension,
411+
baseUrl: "https://api.example.com/v1",
412+
apiKey: "test-api-key",
411413
},
412414
qdrantUrl: "http://localhost:6333",
413415
qdrantApiKey: "test-key",
@@ -463,8 +465,10 @@ describe("CodeIndexServiceFactory", () => {
463465
const testConfig = {
464466
embedderProvider: "openai-compatible",
465467
modelId: testModelId,
468+
modelDimension: 0, // Invalid dimension
466469
openAiCompatibleOptions: {
467-
modelDimension: 0, // Invalid dimension
470+
baseUrl: "https://api.example.com/v1",
471+
apiKey: "test-api-key",
468472
},
469473
qdrantUrl: "http://localhost:6333",
470474
qdrantApiKey: "test-key",
@@ -474,7 +478,7 @@ describe("CodeIndexServiceFactory", () => {
474478

475479
// Act & Assert
476480
expect(() => factory.createVectorStore()).toThrow(
477-
"Could not determine vector dimension for model 'custom-model' with provider 'openai-compatible'. Please ensure the 'Embedding Dimension' is correctly set in the OpenAI-Compatible provider settings.",
481+
"Could not determine vector dimension for model 'custom-model' with provider 'openai-compatible'. Please ensure the 'Embedding Dimension' is correctly set in the \x1b[1m\x1b[4mOpenAI-Compatible\x1b[0m provider settings.",
478482
)
479483
})
480484

@@ -496,7 +500,7 @@ describe("CodeIndexServiceFactory", () => {
496500

497501
// Act & Assert
498502
expect(() => factory.createVectorStore()).toThrow(
499-
"Could not determine vector dimension for model 'unknown-model' with provider 'openai-compatible'. Please ensure the 'Embedding Dimension' is correctly set in the OpenAI-Compatible provider settings.",
503+
"Could not determine vector dimension for model 'unknown-model' with provider 'openai-compatible'. Please ensure the 'Embedding Dimension' is correctly set in the \x1b[1m\x1b[4mOpenAI-Compatible\x1b[0m provider settings.",
500504
)
501505
})
502506

src/services/code-index/config-manager.ts

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,23 @@ export class CodeIndexConfigManager {
7474
this.qdrantApiKey = qdrantApiKey ?? ""
7575
this.searchMinScore = codebaseIndexSearchMinScore
7676
this.searchMaxResults = codebaseIndexSearchMaxResults
77-
this.modelDimension = codebaseIndexConfig.codebaseIndexEmbedderModelDimension as number | undefined
77+
78+
// Validate and set model dimension
79+
const rawDimension = codebaseIndexConfig.codebaseIndexEmbedderModelDimension
80+
if (rawDimension !== undefined && rawDimension !== null) {
81+
const dimension = Number(rawDimension)
82+
if (!isNaN(dimension) && dimension > 0) {
83+
this.modelDimension = dimension
84+
} else {
85+
console.warn(
86+
`Invalid codebaseIndexEmbedderModelDimension value: ${rawDimension}. Must be a positive number.`,
87+
)
88+
this.modelDimension = undefined
89+
}
90+
} else {
91+
this.modelDimension = undefined
92+
}
93+
7894
this.openAiOptions = { openAiNativeApiKey: openAiKey }
7995

8096
// Set embedder provider with support for openai-compatible

src/services/code-index/service-factory.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -107,10 +107,10 @@ export class CodeIndexServiceFactory {
107107
vectorSize = getModelDimension(provider, modelId)
108108
}
109109

110-
if (vectorSize === undefined) {
110+
if (vectorSize === undefined || vectorSize <= 0) {
111111
let errorMessage = `Could not determine vector dimension for model '${modelId}' with provider '${provider}'. `
112112
if (provider === "openai-compatible") {
113-
errorMessage += `Please ensure the 'Embedding Dimension' is correctly set in the provider settings.`
113+
errorMessage += `Please ensure the 'Embedding Dimension' is correctly set in the \x1b[1m\x1b[4mOpenAI-Compatible\x1b[0m provider settings.`
114114
} else {
115115
errorMessage += `Check model profiles or configuration.`
116116
}

0 commit comments

Comments
 (0)