Skip to content

Commit 1698100

Browse files
committed
fix: improve type safety in VertexAI custom base URL implementation
- Replace 'any' type with proper VertexOptions type in anthropic-vertex.ts - Use isVertex property instead of fragile constructor.name comparison in gemini.ts - Improve test assertions clarity by checking undefined explicitly
1 parent a8a8311 commit 1698100

File tree

4 files changed

+16
-19
lines changed

4 files changed

+16
-19
lines changed

src/api/providers/__tests__/anthropic-vertex.spec.ts

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -893,11 +893,8 @@ describe("VertexHandler", () => {
893893
})
894894

895895
// Verify that AnthropicVertex was called without baseURL
896-
expect(AnthropicVertex).toHaveBeenCalledWith(
897-
expect.not.objectContaining({
898-
baseURL: expect.anything(),
899-
}),
900-
)
896+
const callArgs = (AnthropicVertex as any).mock.calls[0][0]
897+
expect(callArgs.baseURL).toBeUndefined()
901898
})
902899
})
903900
})

src/api/providers/__tests__/vertex.spec.ts

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -184,13 +184,8 @@ describe("VertexHandler", () => {
184184
await handler.completePrompt("Test prompt")
185185

186186
// Verify that httpOptions is undefined when no custom URL
187-
expect(mockGenerateContent).toHaveBeenCalledWith(
188-
expect.objectContaining({
189-
config: expect.objectContaining({
190-
httpOptions: undefined,
191-
}),
192-
}),
193-
)
187+
const callArgs = mockGenerateContent.mock.calls[0][0]
188+
expect(callArgs.config.httpOptions).toBeUndefined()
194189
})
195190
})
196191
})

src/api/providers/anthropic-vertex.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,14 @@ export class AnthropicVertexHandler extends BaseProvider implements SingleComple
3434
const projectId = this.options.vertexProjectId ?? "not-provided"
3535
const region = this.options.vertexRegion ?? "us-east5"
3636

37-
const baseOptions: any = {
37+
type VertexOptions = {
38+
projectId: string
39+
region: string
40+
baseURL?: string
41+
googleAuth?: GoogleAuth
42+
}
43+
44+
const baseOptions: VertexOptions = {
3845
projectId,
3946
region,
4047
}

src/api/providers/gemini.ts

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,15 @@ type GeminiHandlerOptions = ApiHandlerOptions & {
2727

2828
export class GeminiHandler extends BaseProvider implements SingleCompletionHandler {
2929
protected options: ApiHandlerOptions
30+
private isVertex: boolean
3031

3132
private client: GoogleGenAI
3233

3334
constructor({ isVertex, ...options }: GeminiHandlerOptions) {
3435
super()
3536

3637
this.options = options
38+
this.isVertex = isVertex ?? false
3739

3840
const project = this.options.vertexProjectId ?? "not-provided"
3941
const location = this.options.vertexRegion ?? "not-provided"
@@ -79,8 +81,7 @@ export class GeminiHandler extends BaseProvider implements SingleCompletionHandl
7981
}
8082

8183
// Use vertexBaseUrl if this is a Vertex handler, otherwise use googleGeminiBaseUrl
82-
const baseUrl =
83-
this.constructor.name === "VertexHandler" ? this.options.vertexBaseUrl : this.options.googleGeminiBaseUrl
84+
const baseUrl = this.isVertex ? this.options.vertexBaseUrl : this.options.googleGeminiBaseUrl
8485

8586
const config: GenerateContentConfig = {
8687
systemInstruction,
@@ -225,10 +226,7 @@ export class GeminiHandler extends BaseProvider implements SingleCompletionHandl
225226
tools.push({ googleSearch: {} })
226227
}
227228
// Use vertexBaseUrl if this is a Vertex handler, otherwise use googleGeminiBaseUrl
228-
const baseUrl =
229-
this.constructor.name === "VertexHandler"
230-
? this.options.vertexBaseUrl
231-
: this.options.googleGeminiBaseUrl
229+
const baseUrl = this.isVertex ? this.options.vertexBaseUrl : this.options.googleGeminiBaseUrl
232230

233231
const promptConfig: GenerateContentConfig = {
234232
httpOptions: baseUrl ? { baseUrl } : undefined,

0 commit comments

Comments
 (0)