Skip to content

Commit 2bece61

Browse files
authored
fix: filtering out empty answer prompts (#26)
1 parent dc145cf commit 2bece61

File tree

7 files changed

+50
-41
lines changed

7 files changed

+50
-41
lines changed

src/components/Chat.tsx

+3-3
Original file line numberDiff line numberDiff line change
@@ -33,16 +33,16 @@ export function Chat() {
3333
<ChatBubbleMessage variant="sent" className="bg-zinc-700">
3434
<Markdown className="text-gray-300">
3535
{sanitizeQuestionPrompt({
36-
question: question?.message,
37-
answer: answer?.message,
36+
question: question?.message ?? "",
37+
answer: answer?.message ?? "",
3838
})}
3939
</Markdown>
4040
</ChatBubbleMessage>
4141
</ChatBubble>
4242
<ChatBubble variant="received">
4343
<ChatBubbleAvatar fallback="AI" />
4444
<ChatBubbleMessage variant="received">
45-
<Markdown>{answer?.message}</Markdown>
45+
<Markdown>{answer?.message ?? ""}</Markdown>
4646
</ChatBubbleMessage>
4747
</ChatBubble>
4848
</div>

src/components/PromptList.tsx

+3-3
Original file line numberDiff line numberDiff line change
@@ -26,18 +26,18 @@ export function PromptList({ prompts }: { prompts: Prompt[] }) {
2626
to={`/prompt/${prompt.chat_id}`}
2727
className={clsx(
2828
`text-gray-800 text-sm truncate hover:text-gray-500`,
29-
{ "font-bold": currentPromptId === prompt.chat_id }
29+
{ "font-bold": currentPromptId === prompt.chat_id },
3030
)}
3131
>
3232
{extractTitleFromMessage(
33-
prompt.question_answers?.[0].question.message
33+
prompt.question_answers?.[0].question?.message
3434
? sanitizeQuestionPrompt({
3535
question:
3636
prompt.question_answers?.[0].question.message,
3737
answer:
3838
prompt.question_answers?.[0]?.answer?.message ?? "",
3939
})
40-
: `Prompt ${prompt.conversation_timestamp}`
40+
: `Prompt ${prompt.conversation_timestamp}`,
4141
)}
4242
</Link>
4343
</li>

src/hooks/useAlertsStore.ts

+9-3
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,13 @@ export const useAlertsStore = create<AlertState>((set, get) => ({
1212
set({ loading: true });
1313
const alerts = await getAlerts();
1414
set({
15-
alerts: alerts.filter((alert) => alert.trigger_category === "critical"),
15+
alerts: alerts
16+
.filter((alert) => alert.trigger_category === "critical")
17+
.filter((alert) =>
18+
alert.conversation.question_answers.every(
19+
(item) => item.answer && item.question,
20+
),
21+
),
1622
loading: false,
1723
});
1824
get().updateFilteredAlerts();
@@ -57,11 +63,11 @@ export const useAlertsStore = create<AlertState>((set, get) => ({
5763
})
5864
.sort(
5965
(a, b) =>
60-
new Date(b.timestamp).getTime() - new Date(a.timestamp).getTime()
66+
new Date(b.timestamp).getTime() - new Date(a.timestamp).getTime(),
6167
)
6268
: alerts.sort(
6369
(a, b) =>
64-
new Date(b.timestamp).getTime() - new Date(a.timestamp).getTime()
70+
new Date(b.timestamp).getTime() - new Date(a.timestamp).getTime(),
6571
);
6672

6773
set({ filteredAlerts });

src/hooks/useBreadcrumb.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ export function useBreadcrumb() {
1919
if (match?.path === "/prompt/") {
2020
try {
2121
const chat = prompts.find((prompt) => prompt.chat_id === currentPromptId);
22-
const title = chat?.question_answers?.[0].question.message ?? "";
22+
const title = chat?.question_answers?.[0].question?.message ?? "";
2323

2424
const sanitized = sanitizeQuestionPrompt({
2525
question: title,

src/hooks/usePromptsStore.ts

+6-1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,11 @@ export const usePromptsStore = create<PromptState>((set) => ({
1010
fetchPrompts: async () => {
1111
set({ loading: true });
1212
const prompts = await getPrompts();
13-
set({ prompts, loading: false });
13+
set({
14+
prompts: prompts.filter((prompt) =>
15+
prompt.question_answers?.every((item) => item.answer && item.question),
16+
),
17+
loading: false,
18+
});
1419
},
1520
}));

src/lib/utils.ts

+26-28
Original file line numberDiff line numberDiff line change
@@ -53,22 +53,25 @@ export function groupPromptsByRelativeDate(prompts: Prompt[]) {
5353
const promptsSorted = prompts.sort(
5454
(a, b) =>
5555
new Date(b.conversation_timestamp).getTime() -
56-
new Date(a.conversation_timestamp).getTime()
56+
new Date(a.conversation_timestamp).getTime(),
5757
);
5858

59-
const grouped = promptsSorted.reduce((groups, prompt) => {
60-
const promptDate = new Date(prompt.conversation_timestamp);
61-
const now = new Date();
62-
const differenceInMs = now.getTime() - promptDate.getTime();
63-
const group = getGroup(differenceInMs, promptDate);
59+
const grouped = promptsSorted.reduce(
60+
(groups, prompt) => {
61+
const promptDate = new Date(prompt.conversation_timestamp);
62+
const now = new Date();
63+
const differenceInMs = now.getTime() - promptDate.getTime();
64+
const group = getGroup(differenceInMs, promptDate);
6465

65-
if (!groups[group]) {
66-
groups[group] = [];
67-
}
66+
if (!groups[group]) {
67+
groups[group] = [];
68+
}
6869

69-
groups[group].push(prompt);
70-
return groups;
71-
}, {} as Record<string, Prompt[]>);
70+
groups[group].push(prompt);
71+
return groups;
72+
},
73+
{} as Record<string, Prompt[]>,
74+
);
7275

7376
return grouped;
7477
}
@@ -82,13 +85,13 @@ export function getAllIssues(alerts: Alert[]) {
8285
}
8386
return acc;
8487
},
85-
{}
88+
{},
8689
);
8790

8891
const maxCount = Math.max(...Object.values(groupedTriggerCounts));
8992

9093
const sortedTagCounts = Object.entries(groupedTriggerCounts).sort(
91-
([, countA], [, countB]) => countB - countA
94+
([, countA], [, countB]) => countB - countA,
9295
);
9396
return { maxCount, sortedTagCounts };
9497
}
@@ -120,22 +123,17 @@ export function sanitizeQuestionPrompt({
120123
answer: string;
121124
}) {
122125
try {
126+
// it shouldn't be possible to receive the prompt answer without a question
127+
if (!answer) return question;
128+
123129
// Check if 'answer' is truthy; if so, try to find and return the text after "Query:"
124-
if (answer) {
125-
const index = question.indexOf("Query:");
126-
if (index !== -1) {
127-
// Return the substring starting right after the first occurrence of "Query:"
128-
// Adding the length of "Query:" to the index to start after it
129-
return question.substring(index + "Query:".length).trim();
130-
} else {
131-
// If there is no "Query:" in the string, log the condition and return an empty string
132-
console.log("No 'Query:' found in the question.");
133-
return "";
134-
}
135-
} else {
136-
// If 'answer' is not provided or falsy, return the original question
137-
return question;
130+
const index = question.indexOf("Query:");
131+
if (index !== -1) {
132+
// Return the substring starting right after the first occurrence of "Query:"
133+
// Adding the length of "Query:" to the index to start after it
134+
return question.substring(index + "Query:".length).trim();
138135
}
136+
return answer;
139137
} catch (error) {
140138
// Log the error and return the original question as a fallback
141139
console.error("Error processing the question:", error);

src/types.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -68,10 +68,10 @@ export type Chat = {
6868
message: string;
6969
timestamp: string;
7070
message_id: string;
71-
};
71+
} | null;
7272
answer: {
7373
message: string;
7474
timestamp: string;
7575
message_id: string;
76-
};
76+
} | null;
7777
};

0 commit comments

Comments
 (0)