Skip to content

Commit b18dc36

Browse files
committed
feat: add Google Gemini provider support
1 parent 141882e commit b18dc36

4 files changed

Lines changed: 32 additions & 16 deletions

File tree

bun.lockb

459 Bytes
Binary file not shown.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
},
3939
"type": "module",
4040
"dependencies": {
41+
"@ai-sdk/google": "^1.0.11",
4142
"@ai-sdk/openai": "^1.0.10",
4243
"@clack/core": "^0.3.4",
4344
"@clack/prompts": "^0.7.0",

src/config.ts

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ function hasOwn<T extends object, K extends PropertyKey>(
6262
export const configPath = path.join(os.homedir(), ".lazycommit");
6363

6464
export interface Config {
65-
provider: "openai"; // Removed "google"
65+
provider: "openai" | "google";
6666
API_KEY: string;
6767
model: string;
6868
templates: Record<string, string>;
@@ -71,7 +71,7 @@ export interface Config {
7171
const DEFAULT_CONFIG: Config = {
7272
provider: "openai",
7373
API_KEY: "",
74-
model: "gpt-4o",
74+
model: "gpt-4",
7575
templates: {
7676
default: path.join(os.homedir(), ".lazycommit-template"),
7777
},
@@ -233,10 +233,8 @@ export async function showConfigUI() {
233233
const provider = await p.select({
234234
message: "Provider",
235235
options: [
236-
{
237-
label: "OpenAI",
238-
value: "openai",
239-
},
236+
{ label: "OpenAI", value: "openai" },
237+
{ label: "Google", value: "google" },
240238
],
241239
initialValue: config.provider,
242240
});
@@ -268,7 +266,14 @@ async function getModels() {
268266
});
269267
const models = await oai.models.list();
270268
return models.data.map((model) => model.id);
271-
} else {
272-
throw new Error("Invalid provider");
269+
} else if (provider === "google") {
270+
return [
271+
"gemini-2.0-flash-exp",
272+
"gemini-1.5-pro-latest",
273+
"gemini-1.5-pro",
274+
"gemini-1.5-flash-latest",
275+
"gemini-1.5-flash",
276+
];
273277
}
278+
throw new Error("Invalid provider");
274279
}

src/run.ts

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { $ } from "bun";
22
import { createOpenAI } from "@ai-sdk/openai";
3+
import { createGoogleGenerativeAI } from "@ai-sdk/google";
34
import { generateText } from "ai";
45
import { readConfigFile } from "./config";
56
import simpleGit from "simple-git";
@@ -93,14 +94,23 @@ export async function run(options: RunOptions, templateName?: string) {
9394
const system_message =
9495
"You are a commit message generator. I will provide you with a git diff, and I would like you to generate an appropriate commit message using the conventional commit format. Do not write any explanations or other words, just reply with the commit message.";
9596

96-
const aiProvider = createOpenAI({
97-
compatibility: 'strict',
98-
apiKey: config.API_KEY,
99-
});
100-
10197
try {
10298
if (options.verbose) {
103-
console.debug("Sending request to OpenAI service...");
99+
console.debug("Sending request to AI service...");
100+
}
101+
102+
let aiProvider;
103+
if (config.provider === "openai") {
104+
aiProvider = createOpenAI({
105+
compatibility: 'strict',
106+
apiKey: config.API_KEY,
107+
});
108+
} else if (config.provider === "google") {
109+
aiProvider = createGoogleGenerativeAI({
110+
apiKey: config.API_KEY,
111+
});
112+
} else {
113+
throw new Error("Invalid provider");
104114
}
105115

106116
const { text } = await generateText({
@@ -109,7 +119,7 @@ export async function run(options: RunOptions, templateName?: string) {
109119
});
110120

111121
if (options.verbose) {
112-
console.debug("Response received from OpenAI service.");
122+
console.debug("Response received from AI service.");
113123
console.debug(text);
114124
}
115125

@@ -120,7 +130,7 @@ export async function run(options: RunOptions, templateName?: string) {
120130
}
121131

122132
} catch (error) {
123-
console.error(`Failed to fetch from OpenAI service: ${error}`);
133+
console.error(`Failed to fetch from AI service: ${error}`);
124134
process.exit(1);
125135
}
126136
}

0 commit comments

Comments
 (0)