-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.ts
57 lines (52 loc) · 1.54 KB
/
utils.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
import path from "path";
import fs from "fs";
import openai from "openai";
export function getTargetFiles(
targetPath: string,
extensions: string[] = [".md", ".mdx"],
ignoreDirs: string[] = ["node_modules", ".git", "dist", ".nuxt"]
): string[] {
const isDir = fs.statSync(targetPath).isDirectory();
let targetFiles: string[] = [];
if (isDir) {
fs.readdirSync(targetPath).forEach((file) => {
const fullPath = path.join(targetPath, file);
if (fs.statSync(fullPath).isDirectory()) {
if (!ignoreDirs.includes(file)) {
targetFiles = targetFiles.concat(
getTargetFiles(fullPath, extensions, ignoreDirs)
);
}
} else {
const ext = path.extname(fullPath);
if (extensions.includes(ext)) {
targetFiles.push(fullPath);
}
}
});
} else {
const ext = path.extname(targetPath);
if (extensions.includes(ext)) {
targetFiles.push(targetPath);
}
}
return targetFiles.sort((a, b) => a.localeCompare(b));
}
export async function getTranslateContent(content: string): Promise<string> {
const client = new openai({
apiKey: process.env.OPENAI_API_KEY,
baseURL: process.env.OPENAI_BASE_URL,
});
try {
const response = await client.chat.completions.create({
messages: [
{ role: "system", content: process.env.OPENAI_SYSTEM_PROMPT! },
{ role: "user", content },
],
model: process.env.OPENAI_MODEL!,
});
return response.choices[0].message.content || "";
} catch (error) {
return "";
}
}