Skip to content

Commit

Permalink
fix(server): Ensure AI description is valid JSON before update
Browse files Browse the repository at this point in the history
  • Loading branch information
csulit committed Oct 25, 2024
1 parent 7406e89 commit d820746
Showing 1 changed file with 15 additions and 6 deletions.
21 changes: 15 additions & 6 deletions server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 });
Expand Down

0 comments on commit d820746

Please sign in to comment.