Skip to content

fix: properly parse title and prompt when there are snippets #18

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jan 2, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/components/PromptList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ export function PromptList({ prompts }: { prompts: Prompt[] }) {
const { currentPromptId, setCurrentPromptId } = usePromptsStore();

const groupedPrompts = groupPromptsByRelativeDate(prompts);

return (
<div className="mx-2">
{Object.entries(groupedPrompts).map(([group, prompts]) => (
Expand Down
9 changes: 7 additions & 2 deletions src/hooks/useBreadcrumb.ts
Original file line number Diff line number Diff line change
@@ -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";

Expand All @@ -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 "";
}
Expand Down
22 changes: 17 additions & 5 deletions src/lib/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ export function extractTitleFromMessage(message: string) {
if (match) {
const beforeMarkdown = match[1].trim();
const afterMarkdown = match[2].trim();

const title = beforeMarkdown || afterMarkdown;
return title;
}
Expand Down Expand Up @@ -121,12 +120,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;
}
}