From 2e03653f4b725a366058fad42d450e415efc9f52 Mon Sep 17 00:00:00 2001 From: Yolanda Robla Date: Fri, 20 Dec 2024 15:51:47 +0100 Subject: [PATCH 1/2] fix: properly parse title and prompt when there are snippets Refine a bit more the parsing of the query, to support code snippets and properly format title and detail Related-to: #398 --- src/components/PromptList.tsx | 1 + src/hooks/useBreadcrumb.ts | 9 +++++++-- src/lib/utils.ts | 26 +++++++++++++++++++++----- 3 files changed, 29 insertions(+), 7 deletions(-) diff --git a/src/components/PromptList.tsx b/src/components/PromptList.tsx index 9b7c0291..695c1135 100644 --- a/src/components/PromptList.tsx +++ b/src/components/PromptList.tsx @@ -12,6 +12,7 @@ export function PromptList({ prompts }: { prompts: Prompt[] }) { const { currentPromptId, setCurrentPromptId } = usePromptsStore(); const groupedPrompts = groupPromptsByRelativeDate(prompts); + return (
{Object.entries(groupedPrompts).map(([group, prompts]) => ( diff --git a/src/hooks/useBreadcrumb.ts b/src/hooks/useBreadcrumb.ts index 83609da7..0189afcc 100644 --- a/src/hooks/useBreadcrumb.ts +++ b/src/hooks/useBreadcrumb.ts @@ -1,4 +1,4 @@ -import { extractTitleFromMessage } from "@/lib/utils"; +import { extractTitleFromMessage, sanitizeQuestionPrompt } from "@/lib/utils"; import { useLocation } from "react-router-dom"; import { usePromptsStore } from "./usePromptsStore"; @@ -20,7 +20,12 @@ export function useBreadcrumb() { try { const chat = prompts.find((prompt) => prompt.chat_id === currentPromptId); const title = chat?.question_answers?.[0].question.message ?? ""; - return extractTitleFromMessage(title) ?? ""; + + const sanitized = sanitizeQuestionPrompt({ + question: title, + answer: chat?.question_answers?.[0]?.answer?.message ?? "", + }); + return extractTitleFromMessage(sanitized) ?? ""; } catch { return ""; } diff --git a/src/lib/utils.ts b/src/lib/utils.ts index 502f353f..fb403049 100644 --- a/src/lib/utils.ts +++ b/src/lib/utils.ts @@ -13,6 +13,10 @@ export function cn(...inputs: ClassValue[]) { } export function extractTitleFromMessage(message: string) { + if (message.includes("review codegate")) { + console.log("message is"); + console.log(JSON.stringify(message, null, 2)); + } try { const regex = /^(.*)```[\s\S]*?```(.*)$/s; const match = message.match(regex); @@ -20,7 +24,6 @@ export function extractTitleFromMessage(message: string) { if (match) { const beforeMarkdown = match[1].trim(); const afterMarkdown = match[2].trim(); - const title = beforeMarkdown || afterMarkdown; return title; } @@ -121,12 +124,25 @@ export function sanitizeQuestionPrompt({ answer: string; }) { try { + // Check if 'answer' is truthy; if so, try to find and return the text after "Query:" if (answer) { - return question.split("Query:").pop() ?? ""; + const index = question.indexOf("Query:"); + if (index !== -1) { + // Return the substring starting right after the first occurrence of "Query:" + // Adding the length of "Query:" to the index to start after it + return question.substring(index + "Query:".length).trim(); + } else { + // If there is no "Query:" in the string, log the condition and return an empty string + console.log("No 'Query:' found in the question."); + return ""; + } + } else { + // If 'answer' is not provided or falsy, return the original question + return question; } - - return question; - } catch { + } catch (error) { + // Log the error and return the original question as a fallback + console.error("Error processing the question:", error); return question; } } From b64a0a6904b00aefdcbfc53322d81c5ef65feb3c Mon Sep 17 00:00:00 2001 From: Yolanda Robla Mota Date: Thu, 2 Jan 2025 09:24:54 +0100 Subject: [PATCH 2/2] Update utils.ts --- src/lib/utils.ts | 4 ---- 1 file changed, 4 deletions(-) diff --git a/src/lib/utils.ts b/src/lib/utils.ts index fb403049..107fdfde 100644 --- a/src/lib/utils.ts +++ b/src/lib/utils.ts @@ -13,10 +13,6 @@ export function cn(...inputs: ClassValue[]) { } export function extractTitleFromMessage(message: string) { - if (message.includes("review codegate")) { - console.log("message is"); - console.log(JSON.stringify(message, null, 2)); - } try { const regex = /^(.*)```[\s\S]*?```(.*)$/s; const match = message.match(regex);