Skip to content

Commit

Permalink
chore(fern-bot): try/catch cohere calls (#1506)
Browse files Browse the repository at this point in the history
  • Loading branch information
armandobelardo authored Sep 19, 2024
1 parent d57cadf commit 935fb87
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export async function updateSpecInternal(
// Add + commit files
const commitDiff = await git.diff();
await git.add(["-A"]);
await git.commit(await generateCommitMessage(commitDiff));
await git.commit(await generateCommitMessage(commitDiff, "chore: update API specification"));

// Push the changes
await git.push([
Expand All @@ -45,7 +45,7 @@ export async function updateSpecInternal(
{
title: ":herb: :sparkles: [Scheduled] Update API Spec",
base: "main",
body: await generateChangelog(fullDiff),
body: await generateChangelog(fullDiff, "This PR updates your API Definition to the latest version."),
},
repository.full_name,
repository.full_name,
Expand Down
23 changes: 19 additions & 4 deletions servers/fern-bot/src/libs/cohere.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ async function coChat(prompt: string): Promise<string> {
return response.text;
}

export async function generateCommitMessage(diff: string): Promise<string> {
export async function generateCommitMessage(diff: string, fallbackMessage: string): Promise<string> {
if (diff === "") {
return DEFAULT_GITHUB_MESSAGE;
}
Expand All @@ -25,11 +25,18 @@ export async function generateCommitMessage(diff: string): Promise<string> {
${diff}
\`\`\`
`;
try {
return await coChat(prompt);
} catch (error) {
console.error(
`Call to Cohere failed generating commit message, with error: ${(error as Error).message}, using fallback message: ${fallbackMessage}`,
);

return coChat(prompt);
return fallbackMessage;
}
}

export async function generateChangelog(diff: string): Promise<string> {
export async function generateChangelog(diff: string, fallbackMessage: string): Promise<string> {
if (diff === "") {
return DEFAULT_GITHUB_MESSAGE;
}
Expand Down Expand Up @@ -216,5 +223,13 @@ export async function generateChangelog(diff: string): Promise<string> {
\`\`\`
`;

return coChat(prompt);
try {
return await coChat(prompt);
} catch (error) {
console.error(
`Call to Cohere failed writing the PR body, with error: ${(error as Error).message}, using fallback message: ${fallbackMessage}`,
);

return fallbackMessage;
}
}

0 comments on commit 935fb87

Please sign in to comment.