Skip to content

Commit 5b9dbdd

Browse files
authored
feat: sanitize prompts (#10)
1 parent 7b70afe commit 5b9dbdd

File tree

3 files changed

+45
-12
lines changed

3 files changed

+45
-12
lines changed

src/components/Chat.tsx

+5-1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import { useParams } from "react-router-dom";
88
import { usePromptsStore } from "@/hooks/usePromptsStore";
99
import { Markdown } from "./Markdown";
1010
import { useEffect } from "react";
11+
import { sanitizeQuestionPrompt } from "@/lib/utils";
1112

1213
export function Chat() {
1314
const { id } = useParams();
@@ -31,7 +32,10 @@ export function Chat() {
3132
<ChatBubbleAvatar fallback="User" className="w-14" />
3233
<ChatBubbleMessage variant="sent" className="bg-zinc-700">
3334
<Markdown className="text-gray-300">
34-
{question?.message}
35+
{sanitizeQuestionPrompt({
36+
question: question?.message,
37+
answer: answer?.message,
38+
})}
3539
</Markdown>
3640
</ChatBubbleMessage>
3741
</ChatBubble>

src/components/PromptList.tsx

+9-2
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { Link } from "react-router-dom";
33
import {
44
extractTitleFromMessage,
55
groupPromptsByRelativeDate,
6+
sanitizeQuestionPrompt,
67
} from "@/lib/utils";
78
import { usePromptsStore } from "@/hooks/usePromptsStore";
89
import clsx from "clsx";
@@ -28,8 +29,14 @@ export function PromptList({ prompts }: { prompts: Prompt[] }) {
2829
)}
2930
>
3031
{extractTitleFromMessage(
31-
prompt.question_answers?.[0].question.message ??
32-
`Prompt ${prompt.conversation_timestamp}`
32+
prompt.question_answers?.[0].question.message
33+
? sanitizeQuestionPrompt({
34+
question:
35+
prompt.question_answers?.[0].question.message,
36+
answer:
37+
prompt.question_answers?.[0]?.answer?.message ?? "",
38+
})
39+
: `Prompt ${prompt.conversation_timestamp}`
3340
)}
3441
</Link>
3542
</li>

src/lib/utils.ts

+31-9
Original file line numberDiff line numberDiff line change
@@ -13,18 +13,22 @@ export function cn(...inputs: ClassValue[]) {
1313
}
1414

1515
export function extractTitleFromMessage(message: string) {
16-
const regex = /^(.*)```[\s\S]*?```(.*)$/s;
17-
const match = message.match(regex);
16+
try {
17+
const regex = /^(.*)```[\s\S]*?```(.*)$/s;
18+
const match = message.match(regex);
1819

19-
if (match) {
20-
const beforeMarkdown = match[1].trim();
21-
const afterMarkdown = match[2].trim();
20+
if (match) {
21+
const beforeMarkdown = match[1].trim();
22+
const afterMarkdown = match[2].trim();
2223

23-
const title = beforeMarkdown || afterMarkdown;
24-
return title;
25-
}
24+
const title = beforeMarkdown || afterMarkdown;
25+
return title;
26+
}
2627

27-
return message.trim();
28+
return message.trim();
29+
} catch {
30+
return message.trim();
31+
}
2832
}
2933

3034
function getGroup(differenceInMs: number, promptDate: Date): string {
@@ -108,3 +112,21 @@ export function getMaliciousPackages() {
108112

109113
return chartData;
110114
}
115+
116+
export function sanitizeQuestionPrompt({
117+
question,
118+
answer,
119+
}: {
120+
question: string;
121+
answer: string;
122+
}) {
123+
try {
124+
if (answer) {
125+
return question.split("Query:").pop() ?? "";
126+
}
127+
128+
return question;
129+
} catch {
130+
return question;
131+
}
132+
}

0 commit comments

Comments
 (0)