Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle issues with OpenAI responses gracefully. #426

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 47 additions & 3 deletions lib/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -3150,7 +3150,11 @@ ${content}`,
}],
});

await fs.promises.writeFile(file, JSON.stringify(JSON.parse(chatResult.choices[0].message.content), false, 2));
if (!await this._validateTranslateResponse(file, chatResult)) {
return;
}

await this._writeResult(file, chatResult, true);

Log(`✅ Translated ${file}`);
} catch (err) {
Expand Down Expand Up @@ -3192,7 +3196,13 @@ ${content}`,
${enLocale}`,
}],
});
await fs.promises.writeFile(translatedLocalePath, JSON.stringify(JSON.parse(chatResult.choices[0].message.content), false, 2));

if (!await this._validateTranslateResponse(translatedLocalePath, chatResult)) {
return;
}

await this._writeResult(translatedLocalePath, chatResult, true);

Log(`✅ Translated ${translatedLocalePath}`);
} catch (err) {
Log(`❌ Error translating ${translatedLocalePath}`);
Expand Down Expand Up @@ -3228,7 +3238,12 @@ ${readme}`,
}],
});

await fs.promises.writeFile(translatedReadmePath, chatResult.choices[0].message.content);
if (!await this._validateTranslateResponse(translatedReadmePath, chatResult)) {
return;
}

await this._writeResult(translatedReadmePath, chatResult, false);

Log(`✅ Translated ${translatedReadmePath}`);
} catch (err) {
Log(`❌ Error translating ${translatedReadmePath}`);
Expand All @@ -3238,6 +3253,35 @@ ${readme}`,
}));
}

async _validateTranslateResponse(file, chatResult) {
if (chatResult.choices[0].finish_reason === 'stop') {
return true;
}

// Write the result to a file, so it can still be used instead of being tossed away
await fs.promises.writeFile(`${file}.fail`, chatResult.choices[0].message.content);

Log(`❌ File ${file} could not be fully translated (finish reason '${chatResult.choices[0].finish_reason}').`);
Log(`The (partial) response has been stored in ${file}.fail`);

return false;
}

async _writeResult(file, chatResult, asJson) {
try {
let content = chatResult.choices[0].message.content;
if (asJson) {
content = JSON.stringify(JSON.parse(content), false, 2);
}

await fs.promises.writeFile(file, content);
} catch (e) {
Log(`❌ File ${file} could not be translated. The (partial) response has been stored in ${file}.fail`);

throw e;
}
}

static async addTypes({ appPath, useTs }) {
if (useTs) {
await NpmCommands.installDev([
Expand Down