Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
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
25 changes: 25 additions & 0 deletions packages/openai-adapters/src/apis/Gemini.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,30 @@ interface GeminiToolDelta
};
}

function buildGoogleGenAIHttpOptions(config: GeminiConfig) {
const httpOptions: {
headers?: Record<string, string>;
timeout?: number;
baseUrl?: string;
apiVersion?: string;
} = {};

if (config.requestOptions?.headers) {
httpOptions.headers = config.requestOptions.headers;
}

if (config.requestOptions?.timeout !== undefined) {
httpOptions.timeout = config.requestOptions.timeout;
}

if (config.apiBase) {
httpOptions.baseUrl = config.apiBase;
httpOptions.apiVersion = "";
}

return Object.keys(httpOptions).length ? httpOptions : undefined;
}

export class GeminiApi implements BaseLlmApi {
apiBase: string = "https://generativelanguage.googleapis.com/v1beta/";
private genAI: GoogleGenAI;
Expand All @@ -79,6 +103,7 @@ export class GeminiApi implements BaseLlmApi {
() =>
new GoogleGenAI({
apiKey: this.config.apiKey,
httpOptions: buildGoogleGenAIHttpOptions(this.config),
}),
);
}
Expand Down
68 changes: 68 additions & 0 deletions packages/openai-adapters/src/test/gemini-adapter.vitest.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
import { afterEach, describe, expect, it, vi } from "vitest";

const generateContentStream = vi.fn();
const GoogleGenAIMock = vi.fn().mockImplementation(() => ({
models: {
generateContentStream,
},
}));

vi.mock("@google/genai", () => ({
GoogleGenAI: GoogleGenAIMock,
}));

vi.mock("../util/nativeFetch.js", () => ({
withNativeFetch: (fn: () => unknown) => fn(),
}));

describe("GeminiApi", () => {
afterEach(() => {
vi.clearAllMocks();
});

it("passes custom headers and apiBase through GoogleGenAI httpOptions", async () => {
const { GeminiApi } = await import("../apis/Gemini.js");

new GeminiApi({
provider: "gemini",
apiKey: "primary-api-key",
apiBase:
"https://example.com/v1/streaming-models/locations/europe-west4/publishers/google",
requestOptions: {
timeout: 10000,
headers: {
"x-api-key": "secondary-api-key",
"Content-Type": "application/json",
},
},
});

expect(GoogleGenAIMock).toHaveBeenCalledWith({
apiKey: "primary-api-key",
httpOptions: {
apiVersion: "",
baseUrl:
"https://example.com/v1/streaming-models/locations/europe-west4/publishers/google",
timeout: 10000,
headers: {
"x-api-key": "secondary-api-key",
"Content-Type": "application/json",
},
},
});
});

it("omits httpOptions when no custom request options are provided", async () => {
const { GeminiApi } = await import("../apis/Gemini.js");

new GeminiApi({
provider: "gemini",
apiKey: "primary-api-key",
});

expect(GoogleGenAIMock).toHaveBeenCalledWith({
apiKey: "primary-api-key",
httpOptions: undefined,
});
});
});
Loading