Skip to content
This repository was archived by the owner on Apr 7, 2025. It is now read-only.

Commit 80e7dae

Browse files
authored
Merge pull request #7 from m7medVision/add-custom-endpoint
Add custom endpoint option for OpenAI-like APIs
2 parents 5cf464a + d3ca5d7 commit 80e7dae

File tree

3 files changed

+46
-3
lines changed

3 files changed

+46
-3
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,3 +72,7 @@ customCommands:
7272
check out these other projects that inspired this one:
7373
7474
- https://github.com/BuilderIO/ai-shell
75+
76+
## Custom Endpoint
77+
78+
You can now specify a custom endpoint for using OpenAI-like APIs. To set a custom endpoint, use the `bunx @m7medvision/lazycommit@latest config` command and select the "Custom Endpoint" option. This allows you to use different API endpoints that are compatible with OpenAI.

src/config.ts

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@ async function editFile(filePath: string, onExit: () => void) {
1111
(await p.select({
1212
message: "Select an editor",
1313
options: [
14+
{
15+
label: "neovim",
16+
value: "nvim",
17+
},
1418
{
1519
label: "vim",
1620
value: "vim",
@@ -19,6 +23,10 @@ async function editFile(filePath: string, onExit: () => void) {
1923
label: "nano",
2024
value: "nano",
2125
},
26+
{
27+
label: "code",
28+
value: "code",
29+
},
2230
{
2331
label: "cancel",
2432
value: "cancel",
@@ -62,16 +70,18 @@ function hasOwn<T extends object, K extends PropertyKey>(
6270
export const configPath = path.join(os.homedir(), ".lazycommit");
6371

6472
export interface Config {
65-
provider: "openai" | "google";
73+
provider: "openai" | "google" | "custom";
6674
API_KEY: string;
6775
model: string;
6876
templates: Record<string, string>;
77+
customEndpoint?: string;
6978
}
7079

7180
const DEFAULT_CONFIG: Config = {
7281
provider: "openai",
7382
API_KEY: "",
74-
model: "gpt-4",
83+
customEndpoint: "",
84+
model: "gpt-4o",
7585
templates: {
7686
default: path.join(os.homedir(), ".lazycommit-template"),
7787
},
@@ -157,6 +167,11 @@ export async function showConfigUI() {
157167
value: "template",
158168
hint: "edit the prompt template",
159169
},
170+
{
171+
label: "Custom Endpoint",
172+
value: "customEndpoint",
173+
hint: config.customEndpoint || "not set",
174+
},
160175
{
161176
label: "Done", // Changed from "Cancel" to "Done"
162177
value: "done",
@@ -235,11 +250,19 @@ export async function showConfigUI() {
235250
options: [
236251
{ label: "OpenAI", value: "openai" },
237252
{ label: "Google", value: "google" },
253+
{ label: "Custom", value: "custom" },
238254
],
239255
initialValue: config.provider,
240256
});
241257

242258
await setConfigs([["provider", provider as string]]);
259+
} else if (choice === "customEndpoint") {
260+
const customEndpoint = await p.text({
261+
message: "Custom Endpoint",
262+
initialValue: config.customEndpoint,
263+
});
264+
265+
await setConfigs([["customEndpoint", customEndpoint as string]]);
243266
}
244267

245268
if (p.isCancel(choice)) {
@@ -266,6 +289,16 @@ async function getModels() {
266289
});
267290
const models = await oai.models.list();
268291
return models.data.map((model) => model.id);
292+
} else if (provider === "custom") {
293+
if (!config.customEndpoint) {
294+
throw new Error("Custom endpoint is not set");
295+
}
296+
const oai = new OpenAI({
297+
apiKey,
298+
baseURL: config.customEndpoint,
299+
});
300+
const models = await oai.models.list();
301+
return models.data.map((model) => model.id);
269302
} else if (provider === "google") {
270303
return [
271304
"gemini-2.0-flash-exp",

src/run.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,12 @@ export async function run(options: RunOptions, templateName?: string) {
109109
aiProvider = createGoogleGenerativeAI({
110110
apiKey: config.API_KEY,
111111
});
112+
} else if (config.provider === "custom") {
113+
aiProvider = createOpenAI({
114+
compatibility: 'strict',
115+
apiKey: config.API_KEY,
116+
baseURL: config.customEndpoint
117+
})
112118
} else {
113119
throw new Error("Invalid provider");
114120
}
@@ -133,4 +139,4 @@ export async function run(options: RunOptions, templateName?: string) {
133139
console.error(`Failed to fetch from AI service: ${error}`);
134140
process.exit(1);
135141
}
136-
}
142+
}

0 commit comments

Comments
 (0)