From d820746de861b4681be82e8fd680cb1f8ed1f724 Mon Sep 17 00:00:00 2001 From: casulit Date: Fri, 25 Oct 2024 13:30:58 +0800 Subject: [PATCH] fix(server): Ensure AI description is valid JSON before update --- server.ts | 21 +++++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) diff --git a/server.ts b/server.ts index 168c826..b0512aa 100644 --- a/server.ts +++ b/server.ts @@ -461,12 +461,21 @@ app.get("/api/properties/:id", async (c: Context) => { if (query.regenerate_ai_description === "true") { const aiDescription = await openaiAssistant(JSON.stringify(propertyData)); - propertyData.ai_generated_description = aiDescription; - - await client.queryObject({ - args: [propertyData.id, JSON.stringify(aiDescription)], - text: `UPDATE Property SET ai_generated_description = $2 WHERE id = $1`, - }); + try { + // Verify the aiDescription is valid JSON by parsing it + JSON.parse(aiDescription); + + propertyData.ai_generated_description = aiDescription; + + await client.queryObject({ + args: [propertyData.id, JSON.stringify(aiDescription)], + text: `UPDATE Property SET ai_generated_description = $2 WHERE id = $1`, + }); + } catch (error) { + console.error("Invalid AI description format:", error); + // Keep the existing description if new one is invalid + propertyData.ai_generated_description = propertyData.ai_generated_description || null; + } } return c.json({ data: propertyData });