-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
113 lines (95 loc) · 3.33 KB
/
index.js
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
const core = require('@actions/core');
const fs = require('fs').promises;
const OpenAI = require('openai');
const path = require('path');
const instruction = require('./openai-instruction.json');
require('dotenv').config();
const {configureGit, commitAndPush, isGitHubAction} = require('./utils/github');
const getInput = (name) => {
if (isGitHubAction) {
return core.getInput(name);
}
switch (name) {
case 'source_file':
return process.env.SOURCE_FILE;
case 'api_key':
return process.env.OPENAI_API_KEY;
default:
return '';
}
};
const generateSystemCommands = async () => {
let editorialGuidelines = '';
try {
const filePath = path.join(__dirname, 'japanese-editorial-guidelines.txt');
editorialGuidelines = await fs.readFile(filePath, 'utf8');
} catch (error) {
console.log('Text files open failed:', error.message);
}
return `
${instruction["General"].map(s => `- ${s}`).join('\n')}
Translation Guidelines:
${instruction["Translation Guidelines"].map(s => `- ${s}`).join('\n')}
Editorial Guidelines:
${editorialGuidelines}
`
}
async function saveTranslation(content, isGitHubAction = false) {
const outputFile = path.basename(getInput('source_file')).replace('.md', '.ja.md');
if (isGitHubAction) {
await fs.writeFile(outputFile, content, 'utf8');
await commitAndPush(outputFile);
return outputFile;
} else {
const outputDir = path.dirname(getInput('source_file'));
await fs.mkdir(outputDir, {recursive: true});
const outputPath = path.join(outputDir, outputFile);
await fs.writeFile(outputPath, content, 'utf8');
console.log(`Translation saved to: ${outputPath}`);
return outputPath;
}
}
async function run() {
try {
const sourceFile = getInput('source_file');
const apiKey = getInput('api_key');
const openai = new OpenAI({
apiKey: apiKey
});
const isGitHubAction = !!process.env.GITHUB_ACTIONS;
if (isGitHubAction) {
await configureGit();
}
const content = await fs.readFile(sourceFile, 'utf8');
const response = await openai.chat.completions.create({
model: "gpt-4",
messages: [
{
role: "system",
content: await generateSystemCommands()
},
{
role: "user",
content: content
}
],
temperature: 0.3,
max_tokens: 4000
});
const translatedContent = response.choices[0].message.content
.replace(/^(以下のマークダウンコンテンツを日本語に翻訳してください:\n*)/g, '')
.replace(/^(Please translate the following markdown content to .+:\n*)/g, '')
.trim();
const outputFile = await saveTranslation(translatedContent, isGitHubAction);
if (isGitHubAction) {
core.setOutput('translated_file', outputFile);
}
console.log(`Translation completed: ${outputFile}`);
} catch (error) {
if (isGitHubAction) {
core.setFailed(error.message);
}
console.error('Translation failed:', error.message);
}
}
run();