diff --git a/packages/googlecloud/functions/getanswer/main.py b/packages/googlecloud/functions/getanswer/main.py index 324f377b..9a29cf00 100644 --- a/packages/googlecloud/functions/getanswer/main.py +++ b/packages/googlecloud/functions/getanswer/main.py @@ -23,8 +23,7 @@ supabase_key = os.environ.get("SUPABASE_SERVICE_KEY_STAGING") supabase = create_client(supabase_url, supabase_key) - -def update_supabase(responses, citations, card_id): +def update_supabase(responses, citations, card_id, processing_time_ms): transformed_citations = [] for citation in citations: transformed_citations.append({ @@ -36,13 +35,16 @@ def update_supabase(responses, citations, card_id): try: supabase.table("cards").update( - {"responses": responses, "citations": transformed_citations} + {"responses": responses, + "citations": transformed_citations, + "processing_time_ms": processing_time_ms} # Update this line ).eq("id", card_id).execute() logging.info("Data successfully updated in Supabase") except Exception as e: logging.error(f"Failed to update Supabase: {e}") + @functions_framework.http def getanswer(request): """HTTP Cloud Function. @@ -96,13 +98,11 @@ def getanswer(request): return ("Failed to process answer", 500, headers) responses_data = answer.get("responses") - citations_data = answer.get("citations") - update_supabase(responses_data, citations_data, card_id) - end = time.time() elapsed = math.ceil(end - start) + update_supabase(responses_data, citations_data, card_id, elapsed) logging.info(f"Completed getanswer in {elapsed} seconds") print(f"\n\t--------- Completed getanswer in {elapsed} seconds --------\n") diff --git a/packages/web/components/NewQuery.tsx b/packages/web/components/NewQuery.tsx index dd37eef4..012f317d 100644 --- a/packages/web/components/NewQuery.tsx +++ b/packages/web/components/NewQuery.tsx @@ -123,33 +123,32 @@ export default function NewQuery() { event: "UPDATE", schema: "public", }, - (payload: SupabaseRealtimePayload) => { - setCard((prevCard) => { - if (!prevCard || payload.new.id !== prevCard.id) return prevCard; + (payload: SupabaseRealtimePayload) => { + if (payload.new.id === card.id) { + setCard((prevCard) => { + // Update only if there are changes in responses or citations + const hasNewResponse = payload.new.responses !== prevCard?.responses; + const hasNewCitations = payload.new.citations !== prevCard?.citations; - const updatedCard = payload.new; - const hasNewResponse = updatedCard.responses !== prevCard.responses; - const hasNewCitations = updatedCard.citations !== prevCard.citations; - - if (hasNewResponse || hasNewCitations) { - return { - ...prevCard, - responses: hasNewResponse ? updatedCard.responses || [] : prevCard.responses, - citations: hasNewCitations ? updatedCard.citations || [] : prevCard.citations, - }; - } + if (hasNewResponse || hasNewCitations) { + return { ...prevCard, ...payload.new }; + } - return prevCard; - }); + return prevCard; + }); + } } ) .subscribe(); + // Cleanup subscription on component unmount return () => { channel.unsubscribe(); }; }, [card]); + + const submitQuery = async (e?: React.FormEvent) => { e?.preventDefault(); if (query.length <= 10) return;