Skip to content

Commit b3bbb1c

Browse files
committed
refactor(run): replace OpenAI import with createOpenAI from @ai-sdk/openai
1 parent 94ebdd8 commit b3bbb1c

3 files changed

Lines changed: 33 additions & 79 deletions

File tree

bun.lockb

9.09 KB
Binary file not shown.

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,11 @@
3838
},
3939
"type": "module",
4040
"dependencies": {
41+
"@ai-sdk/openai": "^1.0.10",
4142
"@clack/core": "^0.3.4",
4243
"@clack/prompts": "^0.7.0",
4344
"@google/generative-ai": "^0.12.0",
45+
"ai": "^4.0.20",
4446
"cleye": "^1.3.2",
4547
"openai": "^4.68.1",
4648
"simple-git": "^3.27.0"

src/run.ts

Lines changed: 31 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import { $ } from "bun";
2-
import OpenAI from "openai";
2+
import { createOpenAI } from "@ai-sdk/openai";
3+
import { generateText } from "ai";
34
import { readConfigFile } from "./config";
45
import simpleGit from "simple-git";
5-
import { GoogleGenerativeAI } from "@google/generative-ai";
66

77
interface RunOptions {
88
verbose?: boolean;
@@ -12,11 +12,10 @@ async function getStagedDiff(target_dir: string) {
1212
try {
1313
const git = simpleGit(target_dir);
1414
const diff = await git.diff(["--cached"]);
15-
1615
return diff;
1716
} catch (error) {
1817
console.error("Error getting git diff:", error);
19-
throw error; // Re-throw the error after logging it
18+
throw error;
2019
}
2120
}
2221

@@ -30,7 +29,7 @@ export async function run(options: RunOptions, templateName?: string) {
3029
if (templateName) {
3130
if (!Object.prototype.hasOwnProperty.call(config.templates, templateName)) {
3231
console.error(
33-
`Error: Template '${templateName}' does not exist in the configuration.`,
32+
`Error: Template '${templateName}' does not exist in the configuration.`
3433
);
3534
process.exit(1);
3635
}
@@ -48,7 +47,7 @@ export async function run(options: RunOptions, templateName?: string) {
4847
const templateFile = Bun.file(templateFilePath);
4948
if (!(await templateFile.exists())) {
5049
console.error(
51-
`Error: The template file '${templateFilePath}' does not exist.`,
50+
`Error: The template file '${templateFilePath}' does not exist.`
5251
);
5352
process.exit(1);
5453
}
@@ -93,82 +92,35 @@ export async function run(options: RunOptions, templateName?: string) {
9392

9493
const system_message =
9594
"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.";
96-
if (config.provider === "openai") {
97-
const oai = new OpenAI({
98-
apiKey: config.API_KEY,
99-
});
10095

101-
try {
102-
if (options.verbose) {
103-
console.debug("Sending request to OpenAI...");
104-
}
105-
const response = await oai.chat.completions.create({
106-
messages: [
107-
{
108-
role: "system",
109-
content: system_message,
110-
},
111-
{
112-
role: "user",
113-
content: rendered_template,
114-
},
115-
],
116-
model: config.model,
117-
response_format: { type: "json_object" },
118-
});
119-
120-
if (options.verbose) {
121-
console.debug("Response received from OpenAI.");
122-
console.debug(JSON.stringify(response, null, 2));
123-
}
124-
const content = response.choices[0].message.content;
125-
if (!content) {
126-
console.error("Failed to generate commit message");
127-
process.exit(1);
128-
}
129-
try {
130-
const content_json = JSON.parse(content);
131-
let counter = 1
132-
for (const message of content_json.commitMessages) {
133-
console.log(counter+ ". "+message);
134-
counter += 1;
135-
}
136-
}
137-
catch (error) {
138-
console.error("Error parsing JSON response:", error);
139-
process.exit(1);
140-
}
141-
if (options.verbose) {
142-
console.debug("Commit message generated and outputted.");
143-
}
144-
} catch (error) {
145-
console.error(`Failed to fetch from openai: ${error}`);
146-
process.exit(1);
96+
const aiProvider = createOpenAI({
97+
compatibility: 'strict',
98+
apiKey: config.API_KEY,
99+
});
100+
101+
try {
102+
if (options.verbose) {
103+
console.debug("Sending request to OpenAI service...");
147104
}
148-
} else if (config.provider === "google") {
149-
const genAI = new GoogleGenerativeAI(config.API_KEY);
150-
const model = genAI.getGenerativeModel({
151-
model: config.model,
152-
systemInstruction: system_message,
153-
generationConfig: { responseMimeType: "application/json" },
154-
});
155-
const session = model.startChat({
156-
history: [],
105+
106+
const { text } = await generateText({
107+
model: aiProvider('gpt-4-turbo'),
108+
prompt: `${system_message}\n${rendered_template}`,
157109
});
158-
const response = await session.sendMessage(rendered_template);
159-
try {
160-
const content_json = JSON.parse(response.response.text());
161-
let counter = 1;
162-
for (const message of content_json.commitMessages) {
163-
console.log(counter+ ". "+message);
164-
counter += 1;
165-
}
166-
} catch (error) {
167-
console.error("Error parsing JSON response:", error);
168-
process.exit(1);
110+
111+
if (options.verbose) {
112+
console.debug("Response received from OpenAI service.");
113+
console.debug(text);
169114
}
170-
} else {
171-
console.error("Provider not supported");
115+
116+
console.log(text.trim());
117+
118+
if (options.verbose) {
119+
console.debug("Commit message generated and outputted.");
120+
}
121+
122+
} catch (error) {
123+
console.error(`Failed to fetch from OpenAI service: ${error}`);
172124
process.exit(1);
173125
}
174-
}
126+
}

0 commit comments

Comments
 (0)