Skip to content

Commit d9f2a91

Browse files
committed
Create additional text field for redacting text from conversation
1 parent 39d5b74 commit d9f2a91

File tree

2 files changed

+35
-7
lines changed

2 files changed

+35
-7
lines changed

frontend/src/pages/Chat/components/FeedbackModal.tsx

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,14 @@ interface Props {
99

1010
export default function FeedbackModal({ messages, setOpenFeedback }: Props) {
1111
const [feedback, setFeedback] = useState("");
12+
const [wordsToRedact, setWordsToRedact] = useState("");
1213
const [status, setStatus] = useState("idle");
1314

1415
const handleModalClose = () => {
1516
setOpenFeedback(false);
1617
setStatus("idle");
1718
setFeedback("");
19+
setWordsToRedact("");
1820
};
1921

2022
return (
@@ -23,11 +25,19 @@ export default function FeedbackModal({ messages, setOpenFeedback }: Props) {
2325
className="absolute top-[50%] left-[50%] -translate-x-[50%] -translate-y-[50%] flex flex-col gap-4 items-center justify-center w-[300px] sm:w-[500px] h-[300px] rounded-lg p-4"
2426
>
2527
{status === "idle" ? (
26-
<textarea
27-
className="resize-none h-[80%] w-full px-3 py-2 border-1 border-[#ddd] rounded-md box-border transition-colors duration-300 focus:outline-0 focus:border-[#4a90e2] focus:shadow-[0_0_0_2px_rgba(74,144,226,0.2)]"
28-
placeholder="Please enter your feedback with regards to the chatbot here. A copy of your chat transcript will automatically be included with your response."
29-
onChange={(event) => setFeedback(event.target.value)}
30-
/>
28+
<>
29+
<textarea
30+
className="resize-none h-[80%] w-full px-3 py-2 border-1 border-[#ddd] rounded-md box-border transition-colors duration-300 focus:outline-0 focus:border-[#4a90e2] focus:shadow-[0_0_0_2px_rgba(74,144,226,0.2)]"
31+
placeholder="Please enter your feedback with regards to the chatbot here. A copy of your chat transcript will automatically be included with your response."
32+
onChange={(event) => setFeedback(event.target.value)}
33+
/>
34+
<input
35+
className="resize-none h-[20%] w-full px-3 py-2 border-1 border-[#ddd] rounded-md box-border transition-colors duration-300 focus:outline-0 focus:border-[#4a90e2] focus:shadow-[0_0_0_2px_rgba(74,144,226,0.2)]"
36+
placeholder="Please enter words to redact separated by commas"
37+
type="text"
38+
onChange={(event) => setWordsToRedact(event.target.value)}
39+
/>
40+
</>
3141
) : (
3242
<div className="flex items-center justify-center h-[80%] w-full">
3343
<p>Feedback Sent!</p>
@@ -40,7 +50,7 @@ export default function FeedbackModal({ messages, setOpenFeedback }: Props) {
4050
if (feedback.trim() === "") handleModalClose();
4151
setStatus("sending");
4252
setTimeout(() => {
43-
sendFeedback(messages, feedback);
53+
sendFeedback(messages, feedback, wordsToRedact);
4454
handleModalClose();
4555
}, 1000);
4656
}}

frontend/src/pages/Chat/utils/feedbackHelper.ts

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,27 @@ function sanitizeText(str: string) {
1212
.replace(/'/g, "&#039;");
1313
}
1414

15+
function redactText(message: string, wordsToRedact: string) {
16+
let redactedMessage = message;
17+
const redactList = wordsToRedact.split(/\s*,\s*/).map((s) => s.trim());
18+
redactList.forEach((word) => {
19+
const regex = new RegExp(`\\b${word.replace(/\s+/g, "\\s+")}\\b`, "gi");
20+
redactedMessage = redactedMessage.replace(regex, () => {
21+
return `<span style="
22+
background-color: black;
23+
color:transparent;
24+
white-space: nowrap;
25+
user-select: none;
26+
">${"_".repeat(10)}</span>`;
27+
});
28+
});
29+
return redactedMessage;
30+
}
31+
1532
export default async function sendFeedback(
1633
messages: IMessage[],
1734
userFeedback: string,
35+
wordsToRedact: string
1836
) {
1937
if (messages.length < 2) return;
2038

@@ -23,7 +41,7 @@ export default async function sendFeedback(
2341
({ role, content }) =>
2442
`<p><strong>${
2543
role.charAt(0).toUpperCase() + role.slice(1)
26-
}</strong>: ${sanitizeText(content)}</p>`,
44+
}</strong>: ${redactText(sanitizeText(content), wordsToRedact)}</p>`
2745
)
2846
.join("");
2947

0 commit comments

Comments
 (0)