Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/errors/GatewayError.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
export class GatewayError extends Error {
constructor(
message: string,
public status: number = 500,
public cause?: Error
) {
super(message);
this.name = 'GatewayError';
this.status = status;
}
Comment on lines 2 to 10
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🐛 Bug Fix

Issue: Redundant assignment of status property in constructor
Fix: Remove redundant assignment since it's already initialized in parameter
Impact: Cleaner code, no functional change

Suggested change
constructor(
message: string,
public status: number = 500,
public cause?: Error
) {
super(message);
this.name = 'GatewayError';
this.status = status;
}
constructor(
message: string,
public status: number = 500,
public cause?: Error
) {
super(message);
this.name = 'GatewayError';
}

}
2 changes: 1 addition & 1 deletion src/handlers/handlerUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -809,7 +809,7 @@ export async function tryTargetsRecursively(
message: errorMessage,
}),
{
status: 500,
status: error instanceof GatewayError ? error.status : 500,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Code Refactor

Issue: Hardcoded error status code
Fix: Use status from GatewayError if available, otherwise default to 500
Impact: More accurate error status propagation

Suggested change
status: error instanceof GatewayError ? error.status : 500,
status: error instanceof GatewayError ? error.status : 500,

headers: {
'content-type': 'application/json',
// Add this header so that the fallback loop can be interrupted if its an exception.
Expand Down
17 changes: 12 additions & 5 deletions src/providers/google-vertex-ai/api.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { GatewayError } from '../../errors/GatewayError';
import { Options } from '../../types/requestBody';
import { endpointStrings, ProviderAPIConfig } from '../types';
import { getModelAndProvider, getAccessToken, getBucketAndFile } from './utils';

const getApiVersion = (provider: string, inputModel: string) => {
const getApiVersion = (provider: string) => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Code Refactor

Issue: Unused parameter inputModel in getApiVersion function
Fix: Remove unused parameter
Impact: Cleaner function signature

Suggested change
const getApiVersion = (provider: string) => {
const getApiVersion = (provider: string) => {

if (provider === 'meta') return 'v1beta1';
return 'v1';
};
Expand All @@ -17,12 +18,12 @@ const getProjectRoute = (
vertexServiceAccountJson,
} = providerOptions;
let projectId = inputProjectId;
if (vertexServiceAccountJson && vertexServiceAccountJson.project_id) {
if (vertexServiceAccountJson?.project_id) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Code Refactor

Issue: Unnecessary explicit check for nested property
Fix: Use optional chaining for cleaner code
Impact: More concise and readable code

Suggested change
if (vertexServiceAccountJson?.project_id) {
if (vertexServiceAccountJson?.project_id) {

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Resolved: Previous suggestion was for optional chaining which is not needed as the code already handles null case correctly

projectId = vertexServiceAccountJson.project_id;
}

const { provider } = getModelAndProvider(inputModel as string);
let routeVersion = getApiVersion(provider, inputModel as string);
const routeVersion = getApiVersion(provider);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Code Refactor

Issue: Unused parameter in function call
Fix: Remove unused inputModel parameter
Impact: Cleaner function call

Suggested change
const routeVersion = getApiVersion(provider);
const routeVersion = getApiVersion(provider);

return `/${routeVersion}/projects/${projectId}/locations/${vertexRegion}`;
};

Expand Down Expand Up @@ -68,7 +69,6 @@ export const GoogleApiConfig: ProviderAPIConfig = {
if (vertexServiceAccountJson) {
authToken = await getAccessToken(c, vertexServiceAccountJson);
}

return {
'Content-Type': 'application/json',
Authorization: `Bearer ${authToken}`,
Expand Down Expand Up @@ -99,7 +99,7 @@ export const GoogleApiConfig: ProviderAPIConfig = {
const jobId = gatewayRequestURL.split('/').at(jobIdIndex);

const url = new URL(gatewayRequestURL);
const searchParams = url.searchParams;
const searchParams = new URLSearchParams(url.search);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Code Refactor

Issue: Inconsistent URL search params initialization
Fix: Use new URLSearchParams(url.search) for consistency
Impact: More consistent code pattern

Suggested change
const searchParams = new URLSearchParams(url.search);
const searchParams = new URLSearchParams(url.search);

const pageSize = searchParams.get('limit') ?? 20;
const after = searchParams.get('after') ?? '';

Expand Down Expand Up @@ -140,9 +140,15 @@ export const GoogleApiConfig: ProviderAPIConfig = {
case 'cancelFinetune': {
return `/v1/projects/${projectId}/locations/${vertexRegion}/tuningJobs/${jobId}:cancel`;
}
default:
return '';
Comment on lines +154 to +155
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Code Refactor

Issue: Missing default case in switch statement
Fix: Add default case returning empty string
Impact: Prevents potential undefined returns

Suggested change
default:
return '';
default:
return '';

}
}

if (!inputModel) {
throw new GatewayError('Model is required', 400);
}
Comment on lines +159 to +161
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔒 Security Issue Fix

Issue: Missing model validation could lead to unexpected behavior
Fix: Add explicit model validation with GatewayError
Impact: Prevents processing requests without required model parameter

Suggested change
if (!inputModel) {
throw new GatewayError('Model is required', 400);
}
if (!inputModel) {
throw new GatewayError('Model is required', 400);
}


const { provider, model } = getModelAndProvider(inputModel as string);
const projectRoute = getProjectRoute(providerOptions, inputModel as string);
const googleUrlMap = new Map<string, string>([
Expand Down Expand Up @@ -181,6 +187,7 @@ export const GoogleApiConfig: ProviderAPIConfig = {
} else if (mappedFn === 'messagesCountTokens') {
return `${projectRoute}/publishers/${provider}/models/count-tokens:rawPredict`;
}
return `${projectRoute}/publishers/${provider}/models/${model}:rawPredict`;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Code Refactor

Issue: Missing default return in switch statement
Fix: Add default return for rawPredict endpoint
Impact: Ensures consistent return value

Suggested change
return `${projectRoute}/publishers/${provider}/models/${model}:rawPredict`;
return `${projectRoute}/publishers/${provider}/models/${model}:rawPredict`;

}

case 'meta': {
Expand Down