From 935fb87e2c61dedc3f45e13560475feb58ae4546 Mon Sep 17 00:00:00 2001 From: Armando Belardo <11140328+armandobelardo@users.noreply.github.com> Date: Thu, 19 Sep 2024 07:55:33 -0400 Subject: [PATCH] chore(fern-bot): try/catch cohere calls (#1506) --- .../oas-cron/shared/updateSpecInternal.ts | 4 ++-- servers/fern-bot/src/libs/cohere.ts | 23 +++++++++++++++---- 2 files changed, 21 insertions(+), 6 deletions(-) diff --git a/servers/fern-bot/src/functions/oas-cron/shared/updateSpecInternal.ts b/servers/fern-bot/src/functions/oas-cron/shared/updateSpecInternal.ts index dbab006c62..61e98e85bf 100644 --- a/servers/fern-bot/src/functions/oas-cron/shared/updateSpecInternal.ts +++ b/servers/fern-bot/src/functions/oas-cron/shared/updateSpecInternal.ts @@ -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([ @@ -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, diff --git a/servers/fern-bot/src/libs/cohere.ts b/servers/fern-bot/src/libs/cohere.ts index e25e3ed98d..11b3ae9c56 100644 --- a/servers/fern-bot/src/libs/cohere.ts +++ b/servers/fern-bot/src/libs/cohere.ts @@ -13,7 +13,7 @@ async function coChat(prompt: string): Promise { return response.text; } -export async function generateCommitMessage(diff: string): Promise { +export async function generateCommitMessage(diff: string, fallbackMessage: string): Promise { if (diff === "") { return DEFAULT_GITHUB_MESSAGE; } @@ -25,11 +25,18 @@ export async function generateCommitMessage(diff: string): Promise { ${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 { +export async function generateChangelog(diff: string, fallbackMessage: string): Promise { if (diff === "") { return DEFAULT_GITHUB_MESSAGE; } @@ -216,5 +223,13 @@ export async function generateChangelog(diff: string): Promise { \`\`\` `; - 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; + } }