mirror of
https://github.com/supabase/supabase.git
synced 2026-09-06 09:59:03 +08:00
Fixes FE-4200, FE-4206. ## What is the current behavior? Submitting a support ticket from an org-level page (with no project in the URL) shows a "While you wait" AI assistant card. However, the assistant is built around project-scoped context from the URL, so making it work here required adding project-context fallbacks across several features. Two previous PRs addressed individual issues, but testing continued to surface the same underlying problem in other areas, including chat persistence, message rating, table browsing, and SQL editor actions. - **#49244** - **#49430** Rather than keep adding fallbacks, this PR removes the underlying context mismatch. ## What is the new behavior? When a support ticket is submitted from an org-level page, the "While you wait" card now links to the relevant project instead of trying to run the AI Assistant without real project context. The link opens the project with the AI Assistant sidebar and hands off the support ticket. The chat is created there, so project-scoped features like schema browsing, SQL actions, message rating, and chat persistence work natively without special-casing. If there’s no relevant project, the card isn’t shown. The ticket form also restores the "No specific project" option for cases where the auto-selected project isn't relevant. ## Additional context Also fixes ?sidebar= deep links not opening the sidebar after client-side navigation. LayoutSidebarProvider now reacts to URL param changes instead of only checking on initial load. ## How to test 1. Submit a project-related support ticket from an org-level page. 2. Confirm the "While you wait" card shows "Open Assistant in project". 3. Click it and confirm the correct project opens with the AI Assistant sidebar and support chat active. 4. Verify project-scoped features work, such as schema questions and Edit query / Run. 5. Refresh and confirm the chat persists. 6. Select "No specific project" and confirm no assistant card is shown. 7. Submit a ticket from a project support page and confirm the existing inline assistant behavior is unchanged. 8. Verify a project ?sidebar=ai-assistant deep link still opens the sidebar normally. <!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit * **New Features** * Added support handoff links when a submitted ticket belongs to another project. * Handoff links securely preserve support request details without exposing them in the URL. * Opening a valid handoff link creates and selects a support chat with the submitted request context. * **Bug Fixes** * Improved sidebar behavior when URL state changes. * Project selector validation messages now remain visible. * Invalid, expired, or mismatched handoffs now fall back to a new chat and display an error message. * Handoff details are securely handled only once and cleared after use. <!-- end of auto-generated comment: release notes by coderabbit.ai --> --------- Co-authored-by: Claude Sonnet 5 <noreply@anthropic.com>
142 lines
5.1 KiB
TypeScript
142 lines
5.1 KiB
TypeScript
import { SupportCategories } from '@supabase/shared-types/out/constants'
|
|
import { z } from 'zod'
|
|
|
|
import type { SubmittedSupportRequest } from './SupportForm.state'
|
|
|
|
const SUPPORT_ASSISTANT_FIELD_LABELS = {
|
|
assistant_context: 'Assistant context',
|
|
organization_slug: 'Organization',
|
|
project_ref: 'Project',
|
|
category: 'Category',
|
|
severity: 'Severity',
|
|
subject: 'Subject',
|
|
message: 'Message',
|
|
affected_services: 'Affected services',
|
|
library: 'Client library',
|
|
support_access: 'Support access',
|
|
dashboard_logs: 'Dashboard logs',
|
|
} as const
|
|
|
|
export type ParsedSupportAssistantPrompt = Partial<
|
|
Record<keyof typeof SUPPORT_ASSISTANT_FIELD_LABELS, string>
|
|
>
|
|
|
|
function escapeSupportTagValue(value: string | boolean | undefined) {
|
|
if (value === undefined || value === '') return 'Not provided'
|
|
|
|
return String(value).replace(/&/g, '&').replace(/</g, '<').replace(/>/g, '>')
|
|
}
|
|
|
|
function unescapeSupportTagValue(value: string) {
|
|
return value.replace(/</g, '<').replace(/>/g, '>').replace(/&/g, '&').trim()
|
|
}
|
|
|
|
function supportField(
|
|
name: keyof typeof SUPPORT_ASSISTANT_FIELD_LABELS,
|
|
value: string | boolean | undefined
|
|
) {
|
|
return ` <${name}>${escapeSupportTagValue(value)}</${name}>`
|
|
}
|
|
|
|
export function buildSupportAssistantPrompt(request: SubmittedSupportRequest) {
|
|
return [
|
|
'<support>',
|
|
supportField(
|
|
'assistant_context',
|
|
'A support request has already been submitted and a human member of the Supabase Support team is already looking at it. Your role is to help the user troubleshoot in the interim while they wait for the human support response. Do not ask them to submit another support ticket for this same issue.'
|
|
),
|
|
supportField('category', request.category),
|
|
supportField('severity', request.severity),
|
|
supportField('subject', request.subject),
|
|
supportField('message', request.message),
|
|
supportField('affected_services', request.affectedServices),
|
|
supportField('library', request.library),
|
|
supportField('support_access', request.allowSupportAccess ? 'Granted' : 'Not granted'),
|
|
supportField('dashboard_logs', request.dashboardLogs ? 'Attached' : 'Not attached'),
|
|
'</support>',
|
|
].join('\n')
|
|
}
|
|
|
|
export const ASSISTANT_HANDOFF_QUERY_PARAM = 'assistantHandoff'
|
|
|
|
// Not typed as z.ZodType<SubmittedSupportRequest>: zod always models "may be undefined"
|
|
// as an optional key, while SubmittedSupportRequest declares organizationSlug/projectRef
|
|
// as required keys whose value may be undefined — a distinction JSON can't carry anyway,
|
|
// since JSON.stringify drops undefined-valued keys entirely.
|
|
const AssistantHandoffSchema = z.object({
|
|
organizationSlug: z.string().optional(),
|
|
projectRef: z.string().optional(),
|
|
category: z.union([
|
|
z.nativeEnum(SupportCategories),
|
|
z.literal('Plan_upgrade'),
|
|
z.literal('Others'),
|
|
]),
|
|
severity: z.string(),
|
|
subject: z.string(),
|
|
message: z.string(),
|
|
affectedServices: z.string(),
|
|
allowSupportAccess: z.boolean(),
|
|
library: z.string().optional(),
|
|
dashboardLogs: z.string().optional(),
|
|
threadRef: z.string().optional(),
|
|
frontConversationId: z.string().optional(),
|
|
})
|
|
|
|
const ASSISTANT_HANDOFF_STORAGE_PREFIX = 'assistant-handoff:'
|
|
|
|
// The handoff URL only ever carries an opaque token — the actual ticket content (subject,
|
|
// message, any attached log links) is kept out of the URL entirely (browser history,
|
|
// referrer headers, server logs, a copy-pasted link) by round-tripping it through
|
|
// sessionStorage instead, scoped to this one tab and gone once it's been read or the tab closes.
|
|
export function storeAssistantHandoff(token: string, request: SubmittedSupportRequest): void {
|
|
try {
|
|
sessionStorage.setItem(ASSISTANT_HANDOFF_STORAGE_PREFIX + token, JSON.stringify(request))
|
|
} catch {
|
|
// Handoff will just fail closed (no context) on the receiving end — not worth
|
|
// surfacing an error for a private-browsing/storage-full edge case.
|
|
}
|
|
}
|
|
|
|
export function consumeAssistantHandoff(token: string): SubmittedSupportRequest | null {
|
|
const key = ASSISTANT_HANDOFF_STORAGE_PREFIX + token
|
|
|
|
try {
|
|
const raw = sessionStorage.getItem(key)
|
|
if (!raw) return null
|
|
|
|
const parsed = AssistantHandoffSchema.safeParse(JSON.parse(raw))
|
|
return parsed.success ? (parsed.data as SubmittedSupportRequest) : null
|
|
} catch {
|
|
return null
|
|
} finally {
|
|
try {
|
|
sessionStorage.removeItem(key)
|
|
} catch {
|
|
// Best-effort cleanup — a failure here shouldn't override the value (or null)
|
|
// already produced above.
|
|
}
|
|
}
|
|
}
|
|
|
|
export function parseSupportAssistantPrompt(text: string): ParsedSupportAssistantPrompt | null {
|
|
const supportMatch = text.match(/<support>([\s\S]*?)<\/support>/i)
|
|
if (!supportMatch) return null
|
|
|
|
const parsed = Object.keys(SUPPORT_ASSISTANT_FIELD_LABELS).reduce<ParsedSupportAssistantPrompt>(
|
|
(acc, field) => {
|
|
const fieldMatch = supportMatch[1].match(
|
|
new RegExp(`<${field}>([\\s\\S]*?)<\\/${field}>`, 'i')
|
|
)
|
|
if (fieldMatch) {
|
|
acc[field as keyof typeof SUPPORT_ASSISTANT_FIELD_LABELS] = unescapeSupportTagValue(
|
|
fieldMatch[1]
|
|
)
|
|
}
|
|
return acc
|
|
},
|
|
{}
|
|
)
|
|
|
|
return Object.keys(parsed).length > 0 ? parsed : null
|
|
}
|