From e5bedae5eaad8ed377e7d9b689815dbd55fdb523 Mon Sep 17 00:00:00 2001 From: MohamedBassem Date: Sat, 2 Mar 2024 11:00:21 +0000 Subject: feature: Show a loading indicator when tags are still being fetched --- packages/workers/openai.ts | 32 +++++++++++++++++++++++++++----- 1 file changed, 27 insertions(+), 5 deletions(-) (limited to 'packages/workers/openai.ts') diff --git a/packages/workers/openai.ts b/packages/workers/openai.ts index 7dda1f9b..7d52c5bb 100644 --- a/packages/workers/openai.ts +++ b/packages/workers/openai.ts @@ -19,6 +19,26 @@ const openAIResponseSchema = z.object({ tags: z.array(z.string()), }); +async function attemptMarkTaggingStatus( + jobData: object | undefined, + status: "success" | "failure", +) { + if (!jobData) { + return; + } + try { + const request = zOpenAIRequestSchema.parse(jobData); + await db + .update(bookmarks) + .set({ + taggingStatus: status, + }) + .where(eq(bookmarks.id, request.bookmarkId)); + } catch (e) { + console.log(`Something went wrong when marking the tagging status: ${e}`); + } +} + export class OpenAiWorker { static async build() { logger.info("Starting openai worker ..."); @@ -31,14 +51,16 @@ export class OpenAiWorker { }, ); - worker.on("completed", (job) => { + worker.on("completed", async (job) => { const jobId = job?.id || "unknown"; logger.info(`[openai][${jobId}] Completed successfully`); + await attemptMarkTaggingStatus(job?.data, "success"); }); - worker.on("failed", (job, error) => { + worker.on("failed", async (job, error) => { const jobId = job?.id || "unknown"; logger.error(`[openai][${jobId}] openai job failed: ${error}`); + await attemptMarkTaggingStatus(job?.data, "failure"); }); return worker; @@ -58,9 +80,9 @@ function buildPrompt( bookmark: NonNullable>>, ) { if (bookmark.link) { - if (!bookmark.link.description) { + if (!bookmark.link.description && !bookmark.link.content) { throw new Error( - `No description found for link "${bookmark.id}". Skipping ...`, + `No content found for link "${bookmark.id}". Skipping ...`, ); } @@ -75,7 +97,7 @@ function buildPrompt( return ` ${PROMPT_BASE} URL: ${bookmark.link.url} -Description: ${bookmark.link.description} +Description: ${bookmark.link.description || ""} Content: ${content || ""} `; } -- cgit v1.2.3-70-g09d2