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
15 changes: 15 additions & 0 deletions core/llm/llms/OpenRouter.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { ChatCompletionCreateParams } from "openai/resources/index";

import { OPENROUTER_HEADERS } from "@continuedev/openai-adapters";

import { LLMOptions } from "../../index.js";
import { osModelsEditPrompt } from "../templates/edit.js";

Expand All @@ -18,6 +20,19 @@ class OpenRouter extends OpenAI {
useLegacyCompletionsEndpoint: false,
};

constructor(options: LLMOptions) {
super({
...options,
requestOptions: {
...options.requestOptions,
headers: {
...OPENROUTER_HEADERS,
...options.requestOptions?.headers,
},
},
});
}

private isAnthropicModel(model?: string): boolean {
if (!model) return false;
const modelLower = model.toLowerCase();
Expand Down
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
5 changes: 3 additions & 2 deletions packages/openai-adapters/src/apis/OpenRouter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,10 @@ export interface OpenRouterConfig extends OpenAIConfig {

// TODO: Extract detailed error info from OpenRouter's error.metadata.raw to surface better messages

const OPENROUTER_HEADERS: Record<string, string> = {
export const OPENROUTER_HEADERS: Record<string, string> = {
"HTTP-Referer": "https://www.continue.dev/",
"X-Title": "Continue",
"X-OpenRouter-Title": "Continue",
"X-OpenRouter-Categories": "ide-extension",
};

export class OpenRouterApi extends OpenAIApi {
Expand Down
1 change: 1 addition & 0 deletions packages/openai-adapters/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -243,4 +243,5 @@ export {
} from "./apis/AnthropicUtils.js";

export { isResponsesModel } from "./apis/openaiResponses.js";
export { OPENROUTER_HEADERS } from "./apis/OpenRouter.js";
export { extractBase64FromDataUrl, parseDataUrl } from "./util/url.js";
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