Skip to content

feat: sanitize prompts #10

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 1 commit into from
Dec 17, 2024
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
6 changes: 5 additions & 1 deletion src/components/Chat.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { useParams } from "react-router-dom";
import { usePromptsStore } from "@/hooks/usePromptsStore";
import { Markdown } from "./Markdown";
import { useEffect } from "react";
import { sanitizeQuestionPrompt } from "@/lib/utils";

export function Chat() {
const { id } = useParams();
Expand All @@ -31,7 +32,10 @@ export function Chat() {
<ChatBubbleAvatar fallback="User" className="w-14" />
<ChatBubbleMessage variant="sent" className="bg-zinc-700">
<Markdown className="text-gray-300">
{question?.message}
{sanitizeQuestionPrompt({
question: question?.message,
answer: answer?.message,
})}
</Markdown>
</ChatBubbleMessage>
</ChatBubble>
Expand Down
11 changes: 9 additions & 2 deletions src/components/PromptList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { Link } from "react-router-dom";
import {
extractTitleFromMessage,
groupPromptsByRelativeDate,
sanitizeQuestionPrompt,
} from "@/lib/utils";
import { usePromptsStore } from "@/hooks/usePromptsStore";
import clsx from "clsx";
Expand All @@ -28,8 +29,14 @@ export function PromptList({ prompts }: { prompts: Prompt[] }) {
)}
>
{extractTitleFromMessage(
prompt.question_answers?.[0].question.message ??
`Prompt ${prompt.conversation_timestamp}`
prompt.question_answers?.[0].question.message
? sanitizeQuestionPrompt({
question:
prompt.question_answers?.[0].question.message,
answer:
prompt.question_answers?.[0]?.answer?.message ?? "",
})
: `Prompt ${prompt.conversation_timestamp}`
)}
</Link>
</li>
Expand Down
40 changes: 31 additions & 9 deletions src/lib/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,22 @@ export function cn(...inputs: ClassValue[]) {
}

export function extractTitleFromMessage(message: string) {
const regex = /^(.*)```[\s\S]*?```(.*)$/s;
const match = message.match(regex);
try {
const regex = /^(.*)```[\s\S]*?```(.*)$/s;
const match = message.match(regex);

if (match) {
const beforeMarkdown = match[1].trim();
const afterMarkdown = match[2].trim();
if (match) {
const beforeMarkdown = match[1].trim();
const afterMarkdown = match[2].trim();

const title = beforeMarkdown || afterMarkdown;
return title;
}
const title = beforeMarkdown || afterMarkdown;
return title;
}

return message.trim();
return message.trim();
} catch {
return message.trim();
}
}

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

return chartData;
}

export function sanitizeQuestionPrompt({
question,
answer,
}: {
question: string;
answer: string;
}) {
try {
if (answer) {
return question.split("Query:").pop() ?? "";
}

return question;
} catch {
return question;
}
}