Files
supabase/apps/docs/features/SearchV2/AskAiPanel.tsx
claude[bot]andClaude 1d31266c82 fix(docs): stop sending Ask AI question text to telemetry (#53142)
<!-- claude-slack-attribution -->
_Requested via [Slack
thread](https://supabase.slack.com/archives/C076KTY11DF/p1794187436590809?thread_ts=1794187436.590809&cid=C076KTY11DF)_

## Problem

**Before:** `docs_search_v2_ask_ai_question_submitted` sent the user's
Ask AI question, cut to its first 300 characters, as a `question`
property (added in #52459). Free-text questions can contain personal
data such as names, emails, pasted logs, or connection strings.

**After:** the event sends only `questionLength` and `questionSource`.
No question text leaves the browser. This matches `kb_search_submitted`,
which already leaves the query text out and sends only its length.

Linear:
[GROWTH-1458](https://linear.app/supabase/issue/GROWTH-1458/stop-sending-ask-ai-question-text-in-docs-search-v2-ask-ai-question)

## Solution

- `packages/common/telemetry-constants.ts`: remove `question` from
`DocsSearchV2AskAiQuestionSubmittedEvent` and note in the event doc that
the question text is left out, as `KbSearchSubmittedEvent` does.
- `apps/docs/features/SearchV2/AskAiPanel.tsx` (typed questions) and
`SearchV2Provider.tsx` (questions carried over from search): drop the
`question` property.
- `apps/docs/features/SearchV2/constants.ts`: delete
`ASK_AI_QUESTION_TELEMETRY_LENGTH`, which nothing uses now.
- `SearchV2.telemetry.test.tsx`: replace the truncation test with one
that checks a typed question sends exactly `{ questionLength,
questionSource: 'typed' }`, has no `question` property, and does not
contain the question text.

Out of scope: `docs_search_v2_search_submitted.query` and
`docs_search_v2_result_clicked.query` stay as they are.

## Review instructions

1. Check that the event type in `telemetry-constants.ts` no longer has
`question`, and that typecheck enforces this at both call sites.
2. Run `pnpm vitest --run features/SearchV2` in `apps/docs`. All 33
tests pass locally.
3. `tsc --noEmit` passes in `apps/docs` (after `codegen:graphql`) and
`packages/common`. Prettier is clean on changed files.

## Checklist

Check all before review:

- [ ] I have read
[CONTRIBUTING.md](https://github.com/supabase/supabase/blob/master/CONTRIBUTING.md)
- [ ] If I wrote a new docs topic or edited an existing topic, I used
the `/write-the-docs` or `/edit-the-docs` skill, which applies the docs
[style
guide](https://github.com/supabase/supabase/tree/master/apps/docs/style-guide)
(not applicable: no docs content changed)

---
_Generated by [Claude
Code](https://claude.ai/code/session_011aGhSD3LLt9nZYbsWkZBjJ)_

Co-authored-by: Claude <noreply@anthropic.com>
2026-11-09 15:39:55 +09:00

364 lines
14 KiB
TypeScript

'use client'
import type { DocsSearchV2AskAiQuestionSubmittedEvent } from 'common/telemetry-constants'
import { SearchInputRow, type SearchDialogProps } from 'docs-search/components'
import { FileText } from 'lucide-react'
import { useReducedMotion } from 'motion/react'
import Link from 'next/link'
import {
useCallback,
useEffect,
useRef,
useState,
type ChangeEvent,
type ComponentProps,
type FormEvent,
type KeyboardEvent,
type UIEvent,
} from 'react'
import { CodeCopyButton } from 'ui-patterns/CodeBlock/CodeBlockControls'
import {
MessageRole,
MessageStatus,
useAiChat,
type Message,
} from 'ui-patterns/CommandMenu/prepackaged/ai'
import { Markdown } from 'ui-patterns/Markdown'
import { AskAiIcon } from './AskAiIcon'
import { CurvedScrollArea } from '@/features/ui/CurvedScrollArea'
import { useSendTelemetryEvent } from '@/lib/telemetry'
type QuestionSource = DocsSearchV2AskAiQuestionSubmittedEvent['properties']['questionSource']
interface AskAiPanelProps {
initialQuestion: string
}
interface AiMessagesProps {
messages: Message[]
hasError: boolean
}
interface AiAnswerMarkdownProps {
content: string
}
interface AiSourcesProps {
sources: NonNullable<Message['sources']>
onSourceClick: (sourcePath: string) => void
}
export const ASK_AI_DIALOG_LABELS: SearchDialogProps['labels'] = {
dialogTitle: 'Ask Supabase AI',
dialogDescription: 'Ask about the Supabase docs',
}
export function AskAiPanel({ initialQuestion }: AskAiPanelProps) {
const [question, setQuestion] = useState('')
const [isLoading, setIsLoading] = useState(false)
const sendTelemetryEvent = useSendTelemetryEvent()
// the question that search sent is the first one, and every later question is typed
const questionSourceRef = useRef<QuestionSource>(initialQuestion ? 'search' : 'typed')
const { submit, reset, messages, isResponding, hasError } = useAiChat({
setIsLoading,
onAnswerComplete: ({ durationMs, sourceCount }) =>
sendTelemetryEvent({
action: 'docs_search_v2_ask_ai_answer_completed',
properties: { questionSource: questionSourceRef.current, durationMs, sourceCount },
}),
onAnswerError: ({ durationMs }) =>
sendTelemetryEvent({
action: 'docs_search_v2_ask_ai_answer_failed',
properties: { questionSource: questionSourceRef.current, durationMs },
}),
})
const inputRef = useRef<HTMLInputElement>(null)
const isPinnedRef = useRef(true)
const lastScrollTopRef = useRef(0)
const listRef = useRef<HTMLDivElement>(null)
const isBusy = isLoading || isResponding
const hasConversation = messages.length > 0
const hasAnswer = messages.some(
({ role, content }) => role === MessageRole.Assistant && content !== ''
)
// exception: asks the question that search sent on mount. the dialog unmounts this panel
// on close, so the cleanup closes the answer stream
useEffect(() => {
if (initialQuestion) submit(initialQuestion)
return reset
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [])
const handleSubmit = (event: FormEvent<HTMLFormElement>) => {
event.preventDefault()
const trimmedQuestion = question.trim()
if (!trimmedQuestion || isBusy) return
questionSourceRef.current = 'typed'
submit(trimmedQuestion)
sendTelemetryEvent({
action: 'docs_search_v2_ask_ai_question_submitted',
properties: {
questionLength: trimmedQuestion.length,
questionSource: 'typed',
},
})
setQuestion('')
isPinnedRef.current = true
}
const handleListScroll = (event: UIEvent<HTMLDivElement>) => {
const list = event.currentTarget
if (list.scrollHeight - list.scrollTop - list.clientHeight < 32) isPinnedRef.current = true
else if (list.scrollTop < lastScrollTopRef.current) isPinnedRef.current = false
lastScrollTopRef.current = list.scrollTop
}
// a stable callback ref, so react does not create the observer again on each render
const followConversation = useCallback((content: HTMLDivElement | null) => {
if (!content) return
const observer = new ResizeObserver(() => {
const list = listRef.current
if (list && isPinnedRef.current) list.scrollTop = list.scrollHeight
})
observer.observe(content)
return () => observer.disconnect()
}, [])
const handleKeyDown = (event: KeyboardEvent<HTMLInputElement>) => {
// enter confirms an IME composition, so it must not submit
if (event.key === 'Enter' && event.nativeEvent.isComposing) event.preventDefault()
}
const handleQuestionChange = (event: ChangeEvent<HTMLInputElement>) => {
setQuestion(event.target.value)
}
// focus stays in the input while the answer renders, so this tells a screen reader what changed.
// it never reads the streamed text
const getStatusMessage = (): string => {
if (hasError) return 'No answer from Supabase AI. Try again in a moment.'
if (isLoading) return 'Searching the docs…'
if (isResponding) return 'Writing the answer…'
if (messages.at(-1)?.status === MessageStatus.Complete) return 'Answer ready.'
return ''
}
// reset() does not clear the loading state that submit sets. the clear button unmounts with
// the conversation, so focus goes back to the input
const handleClear = () => {
reset()
setIsLoading(false)
setQuestion('')
inputRef.current?.focus()
}
return (
<section className="flex flex-col">
<div role="status" aria-live="polite" className="sr-only">
{getStatusMessage()}
</div>
{hasConversation ? (
<div className="relative">
<CurvedScrollArea
viewportRef={listRef}
onScroll={handleListScroll}
className="max-h-[min(477px,70dvh)] mx-px scroll-fade-bottom motion-safe:scroll-smooth"
>
<div ref={followConversation}>
<AiMessages messages={messages} hasError={hasError} />
{hasAnswer && !isBusy ? (
<p className="px-4 pt-4 pb-3 text-center text-xs text-foreground-lighter">
Supabase AI can make mistakes. Verify answers before you run them.
</p>
) : null}
</div>
</CurvedScrollArea>
<div aria-hidden className="ask-ai-edge" />
</div>
) : null}
<form onSubmit={handleSubmit}>
<SearchInputRow
icon={<AskAiIcon isThinking={isBusy} className="size-full" />}
onClear={hasConversation ? handleClear : undefined}
// under a conversation the input is far from the search input,
// so a morph would move it across the dialog
hasMorph={!hasConversation}
className={hasConversation ? 'border-t' : undefined}
>
<span className="relative flex size-full items-center">
<input
ref={inputRef}
autoFocus
autoComplete="off"
data-1p-ignore // 1Password
data-lpignore="true" // LastPass
data-form-type="other" // Dashlane
data-bwignore // Bitwarden
value={question}
onChange={handleQuestionChange}
onKeyDown={handleKeyDown}
placeholder={isBusy ? undefined : 'Ask Supabase AI a question'}
aria-label="Ask Supabase AI a question"
className="size-full border-none bg-transparent p-0 text-search-dialog outline-hidden placeholder:text-foreground-lighter focus:ring-0"
/>
{isLoading && !question ? (
<span
aria-hidden
className="shimmer pointer-events-none absolute inset-y-0 left-0 flex items-center text-search-dialog text-foreground-lighter motion-reduce:text-foreground-light"
>
Searching the docs
<span className="hidden motion-reduce:inline">…</span>
</span>
) : null}
</span>
</SearchInputRow>
</form>
</section>
)
}
function AiMessages({ messages, hasError }: AiMessagesProps) {
const sendTelemetryEvent = useSendTelemetryEvent()
// the input shows a pending answer. a failed request leaves its answer incomplete.
// the last answer failed when an error is set, and an earlier answer failed when it is incomplete
const isFailedAnswer = (message: Message, index: number) =>
message.status !== MessageStatus.Complete && (hasError || index < messages.length - 1)
const handleSourceClick = (sourcePath: string) => {
sendTelemetryEvent({
action: 'docs_search_v2_ask_ai_source_clicked',
properties: { sourcePath },
})
}
const handleAnswerCopy = () => {
sendTelemetryEvent({
action: 'copy_as_markdown_clicked',
properties: { pageType: 'ask_ai_answer' },
})
}
return (
<ol className="flex flex-col gap-4 py-4 pr-4 pl-3">
{messages.map((message, index) =>
message.role === MessageRole.User ? (
// messages have no id and are only ever appended, so the index is stable
<li key={index} className="group flex max-w-[80%] flex-col items-end gap-1 self-end">
<p className="rounded-lg bg-surface-400 px-3 py-1.5 text-search-dialog text-foreground wrap-break-word">
{message.content}
</p>
<CodeCopyButton
content={message.content}
label="Copy question"
copiedLabel="Question copied"
className="border-transparent bg-transparent opacity-0 hover:border-transparent hover:bg-transparent focus-visible:opacity-100 group-hover:opacity-100 pointer-coarse:opacity-100"
/>
</li>
) : message.status === MessageStatus.Pending && !isFailedAnswer(message, index) ? null : (
<li
key={index}
className="group"
aria-busy={!isFailedAnswer(message, index) && message.status !== MessageStatus.Complete}
>
{message.status === MessageStatus.Pending ? null : (
<AiAnswerMarkdown content={message.content} />
)}
{isFailedAnswer(message, index) ? (
<p className="text-search-dialog text-foreground-lighter">
No answer from Supabase AI. Try again in a moment.
</p>
) : null}
{message.sources?.length ? (
<AiSources sources={message.sources} onSourceClick={handleSourceClick} />
) : null}
{message.status === MessageStatus.Complete ? (
<CodeCopyButton
// an agent cannot open docs paths, so the sources go in as full urls
content={
message.sources?.length
? `${message.content}\n\n### Sources\n\n${message.sources.map(({ url }) => `- ${url}`).join('\n')}`
: message.content
}
label="Copy as Markdown"
copiedLabel="Answer copied as Markdown"
onCopied={handleAnswerCopy}
className="mt-2 border-transparent bg-transparent opacity-0 hover:border-transparent hover:bg-transparent focus-visible:opacity-100 group-hover:opacity-100 pointer-coarse:opacity-100"
/>
) : null}
</li>
)
)}
</ol>
)
}
function AiAnswerMarkdown({ content }: AiAnswerMarkdownProps) {
const [visibleLength, setVisibleLength] = useState(0)
const isReducedMotion = useReducedMotion()
const isCaughtUp = visibleLength >= content.length
// exception: chunks arrive in bursts. each animation frame shows 1/12 of the hidden text.
// a large backlog drains fast, and a small one shows at a steady rate
useEffect(() => {
if (isCaughtUp || isReducedMotion) return
const frame = requestAnimationFrame(() =>
setVisibleLength((length) => length + Math.ceil((content.length - length) / 12))
)
return () => cancelAnimationFrame(frame)
}, [content.length, isCaughtUp, isReducedMotion, visibleLength])
return (
<Markdown
codeBlock
className="ask-ai-answer prose-sm wrap-break-word"
// a link component that changes on each frame makes react replace the link, so a click can get lost
components={{ a: AiAnswerLink }}
>
{isReducedMotion ? content : content.slice(0, visibleLength)}
</Markdown>
)
}
function AiAnswerLink(props: ComponentProps<'a'>) {
const sendTelemetryEvent = useSendTelemetryEvent()
const handleClick = () => {
sendTelemetryEvent({
action: 'docs_search_v2_ask_ai_answer_link_clicked',
properties: { href: props.href ?? '' },
})
}
return <a {...props} target="_blank" rel="noopener noreferrer" onClick={handleClick} />
}
function AiSources({ sources, onSourceClick }: AiSourcesProps) {
return (
<nav aria-label="Sources" className="mt-3">
<ul className="flex flex-wrap gap-1.5">
{sources.map((source, index) => (
<li
key={source.path}
style={{ transitionDelay: `${index * 50}ms` }}
className="transition-[opacity,scale,filter] duration-300 ease-out starting:scale-85 starting:opacity-0 starting:blur-xs motion-reduce:transition-none"
>
<Link
href={source.path}
target="_blank"
rel="noopener noreferrer"
prefetch={false}
onClick={() => onSourceClick(source.path)}
className="inline-flex items-center gap-1.5 rounded-full bg-surface-400 px-2.5 py-1 text-xs text-foreground-light transition-colors hover:text-foreground focus-ring"
>
<FileText aria-hidden className="size-3.5 shrink-0" strokeWidth={2} />
{source.path}
</Link>
</li>
))}
</ul>
</nav>
)
}