Skip to content

Commit fcd236a

Browse files
authored
feat(ai-help): show canned answer without sources for off-topic questions (#10575)
* feat(ai-help): show error for off-topic questions * refactor(ai-help): extract AIHelpAssistantResponseSources * feat(ai-help): show canned answer without sources for off-topic question * fix(ai-help): override content/sources only for off-topic messages * chore(ai-help): remove unnecessary filter * chore(ai-help): remove unnecessary wrapper * refactor(ai-help): reuse ReactMarkdown for off-topic answer for simplicity
1 parent 78a9500 commit fcd236a

File tree

2 files changed

+71
-44
lines changed

2 files changed

+71
-44
lines changed

Diff for: client/src/plus/ai-help/constants.tsx

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
export const SORRY_BACKEND = "Sorry, I don't know how to help with that.";
1+
export const SORRY_BACKEND_PREFIX = "I'm sorry, but I can't";
22
export const SORRY_FRONTEND =
3-
"Sorry, I don’t know how to help with that.\n\nPlease keep in mind that I am only limited to answer based on the MDN documentation.";
3+
"I'm sorry, but I can't answer questions outside web development.";
44

55
export const MESSAGE_SEARCHING = "Searching for MDN content…";
66
export const MESSAGE_SEARCHED = "Consulted MDN content:";

Diff for: client/src/plus/ai-help/index.tsx

+69-42
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ import { useUIStatus } from "../../ui-context";
4848
import { QueueEntry } from "../../types/playground";
4949
import { AIHelpLanding } from "./landing";
5050
import {
51-
SORRY_BACKEND,
51+
SORRY_BACKEND_PREFIX,
5252
SORRY_FRONTEND,
5353
MESSAGE_SEARCHING,
5454
MESSAGE_ANSWERING,
@@ -288,6 +288,10 @@ function AIHelpAssistantResponse({
288288

289289
let sample = 0;
290290

291+
const isOffTopic =
292+
message.role === MessageRole.Assistant &&
293+
message.content?.startsWith(SORRY_BACKEND_PREFIX);
294+
291295
function messageForStatus(status: MessageStatus) {
292296
switch (status) {
293297
case MessageStatus.Errored:
@@ -314,33 +318,7 @@ function AIHelpAssistantResponse({
314318

315319
return (
316320
<>
317-
<div
318-
className={[
319-
"ai-help-message-progress",
320-
message.status === MessageStatus.Pending && "active",
321-
]
322-
.filter(Boolean)
323-
.join(" ")}
324-
>
325-
{message.status === MessageStatus.Pending
326-
? MESSAGE_SEARCHING
327-
: MESSAGE_SEARCHED}
328-
</div>
329-
{message.sources && message.sources.length > 0 && (
330-
<ul className="ai-help-message-sources">
331-
{message.sources.map(({ url, title }, index) => (
332-
<li key={index}>
333-
<InternalLink
334-
to={url}
335-
onClick={() => gleanClick(`${AI_HELP}: link source -> ${url}`)}
336-
target="_blank"
337-
>
338-
{title}
339-
</InternalLink>
340-
</li>
341-
))}
342-
</ul>
343-
)}
321+
{!isOffTopic && <AIHelpAssistantResponseSources message={message} />}
344322
{(message.content ||
345323
message.status === MessageStatus.InProgress ||
346324
message.status === MessageStatus.Errored) && (
@@ -509,34 +487,83 @@ function AIHelpAssistantResponse({
509487
},
510488
}}
511489
>
512-
{message.content?.replace(SORRY_BACKEND, SORRY_FRONTEND)}
490+
{isOffTopic ? SORRY_FRONTEND : message.content}
513491
</ReactMarkdown>
514-
{message.status === "complete" &&
515-
!message.content?.includes(SORRY_BACKEND) && (
516-
<>
517-
<section className="ai-help-feedback">
492+
{(message.status === "complete" || isOffTopic) && (
493+
<>
494+
<section className="ai-help-feedback">
495+
{!isOffTopic && (
518496
<GleanThumbs
519497
feature="ai-help-answer"
520498
featureKey={message.messageId}
521499
question={"Was this answer useful?"}
522500
upLabel={"Yes, this answer was useful."}
523501
downLabel={"No, this answer was not useful."}
524502
/>
525-
<ReportIssueOnGitHubLink
526-
messages={messages}
527-
currentMessage={message}
528-
>
529-
Report an issue with this answer on GitHub
530-
</ReportIssueOnGitHubLink>
531-
</section>
532-
</>
533-
)}
503+
)}
504+
<ReportIssueOnGitHubLink
505+
messages={messages}
506+
currentMessage={{
507+
...message,
508+
...(isOffTopic
509+
? {
510+
content: SORRY_FRONTEND,
511+
sources: [],
512+
}
513+
: {}),
514+
}}
515+
>
516+
Report an issue with this answer on GitHub
517+
</ReportIssueOnGitHubLink>
518+
</section>
519+
</>
520+
)}
534521
</div>
535522
)}
536523
</>
537524
);
538525
}
539526

527+
function AIHelpAssistantResponseSources({
528+
message,
529+
}: {
530+
message: Pick<Message, "status" | "sources">;
531+
}) {
532+
const gleanClick = useGleanClick();
533+
534+
return (
535+
<>
536+
<div
537+
className={[
538+
"ai-help-message-progress",
539+
message.status !== MessageStatus.Pending && "complete",
540+
]
541+
.filter(Boolean)
542+
.join(" ")}
543+
>
544+
{message.status === MessageStatus.Pending
545+
? MESSAGE_SEARCHING
546+
: MESSAGE_SEARCHED}
547+
</div>
548+
{message.sources && message.sources.length > 0 && (
549+
<ul className="ai-help-message-sources">
550+
{message.sources.map(({ url, title }, index) => (
551+
<li key={index}>
552+
<InternalLink
553+
to={url}
554+
onClick={() => gleanClick(`${AI_HELP}: link source -> ${url}`)}
555+
target="_blank"
556+
>
557+
{title}
558+
</InternalLink>
559+
</li>
560+
))}
561+
</ul>
562+
)}
563+
</>
564+
);
565+
}
566+
540567
export function AIHelpInner() {
541568
const formRef = useRef<HTMLFormElement>(null);
542569
const inputRef = useRef<HTMLTextAreaElement>(null);

0 commit comments

Comments
 (0)