From fcd236a8565e506ae255483984315b81950d738d Mon Sep 17 00:00:00 2001 From: Claas Augner <495429+caugner@users.noreply.github.com> Date: Fri, 1 Mar 2024 12:38:12 +0100 Subject: [PATCH] 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 --- client/src/plus/ai-help/constants.tsx | 4 +- client/src/plus/ai-help/index.tsx | 111 ++++++++++++++++---------- 2 files changed, 71 insertions(+), 44 deletions(-) diff --git a/client/src/plus/ai-help/constants.tsx b/client/src/plus/ai-help/constants.tsx index 2b4159ee1270..32c0f643fd1d 100644 --- a/client/src/plus/ai-help/constants.tsx +++ b/client/src/plus/ai-help/constants.tsx @@ -1,6 +1,6 @@ -export const SORRY_BACKEND = "Sorry, I don't know how to help with that."; +export const SORRY_BACKEND_PREFIX = "I'm sorry, but I can't"; export const SORRY_FRONTEND = - "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."; + "I'm sorry, but I can't answer questions outside web development."; export const MESSAGE_SEARCHING = "Searching for MDN content…"; export const MESSAGE_SEARCHED = "Consulted MDN content:"; diff --git a/client/src/plus/ai-help/index.tsx b/client/src/plus/ai-help/index.tsx index 7373d8daa374..2c1943b92c51 100644 --- a/client/src/plus/ai-help/index.tsx +++ b/client/src/plus/ai-help/index.tsx @@ -48,7 +48,7 @@ import { useUIStatus } from "../../ui-context"; import { QueueEntry } from "../../types/playground"; import { AIHelpLanding } from "./landing"; import { - SORRY_BACKEND, + SORRY_BACKEND_PREFIX, SORRY_FRONTEND, MESSAGE_SEARCHING, MESSAGE_ANSWERING, @@ -288,6 +288,10 @@ function AIHelpAssistantResponse({ let sample = 0; + const isOffTopic = + message.role === MessageRole.Assistant && + message.content?.startsWith(SORRY_BACKEND_PREFIX); + function messageForStatus(status: MessageStatus) { switch (status) { case MessageStatus.Errored: @@ -314,33 +318,7 @@ function AIHelpAssistantResponse({ return ( <> -
- {message.status === MessageStatus.Pending - ? MESSAGE_SEARCHING - : MESSAGE_SEARCHED} -
- {message.sources && message.sources.length > 0 && ( - - )} + {!isOffTopic && } {(message.content || message.status === MessageStatus.InProgress || message.status === MessageStatus.Errored) && ( @@ -509,12 +487,12 @@ function AIHelpAssistantResponse({ }, }} > - {message.content?.replace(SORRY_BACKEND, SORRY_FRONTEND)} + {isOffTopic ? SORRY_FRONTEND : message.content} - {message.status === "complete" && - !message.content?.includes(SORRY_BACKEND) && ( - <> -
+ {(message.status === "complete" || isOffTopic) && ( + <> +
+ {!isOffTopic && ( - - Report an issue with this answer on GitHub - -
- - )} + )} + + Report an issue with this answer on GitHub + +
+ + )} )} ); } +function AIHelpAssistantResponseSources({ + message, +}: { + message: Pick; +}) { + const gleanClick = useGleanClick(); + + return ( + <> +
+ {message.status === MessageStatus.Pending + ? MESSAGE_SEARCHING + : MESSAGE_SEARCHED} +
+ {message.sources && message.sources.length > 0 && ( + + )} + + ); +} + export function AIHelpInner() { const formRef = useRef(null); const inputRef = useRef(null);