Skip to content

The Postgres development platform. Supabase gives you a dedicated Postgres database to build your web, mobile, and AI applications.

fix(assistant): preserve template URL placeholders in responses (#43495)

Matt Rossmanf49ed14dfb33
diff --git a/apps/studio/components/ui/AIAssistantPanel/Message.utils.ts b/apps/studio/components/ui/AIAssistantPanel/Message.utils.ts
index 16f6125896e..8212e445127 100644
--- a/apps/studio/components/ui/AIAssistantPanel/Message.utils.ts
+++ b/apps/studio/components/ui/AIAssistantPanel/Message.utils.ts
@@ -1,4 +1,32 @@
-import { type SafeParseReturnType, z } from 'zod'
+import { z, type SafeParseReturnType } from 'zod'
+
+// Splits markdown into alternating [plain, code, plain, code, ...] segments.
+// Odd-indexed segments are already inside code spans/fences and should be left alone.
+const CODE_SEGMENT_REGEX = /(```[\s\S]*?```|`[^`]*`)/g
+
+// Matches bare placeholder URLs like https://xxx/<project-ref>/... outside markdown link
+// syntax. Stops at whitespace, ), or ] to avoid consuming link delimiters. Trailing prose
+// punctuation is stripped in the replacement callback below.
+const PLACEHOLDER_URL_REGEX = /(?<!\()https?:\/\/[^\s)\]]*<[a-z][a-z0-9]*(?:-[a-z0-9]+)*>[^\s)\]]*/g
+
+/**
+ * Wraps bare URLs containing <placeholder> patterns in backticks so they render in
+ * code font, regardless of whether the LLM remembered to wrap them.
+ */
+export function wrapPlaceholderUrls(markdown: string): string {
+ if (!markdown.includes('<')) return markdown
+ const segments = markdown.split(CODE_SEGMENT_REGEX)
+ return segments
+ .map((segment, i) => {
+ if (i % 2 === 1) return segment
+ return segment.replace(PLACEHOLDER_URL_REGEX, (url) => {
+ const trailingPunct = url.match(/[.,;:!?'"]+$/)?.[0] ?? ''
+ const cleanUrl = url.slice(0, url.length - trailingPunct.length)
+ return `\`${cleanUrl}\`` + trailingPunct
+ })
+ })
+ .join('')
+}
// [Joshen] From https://github.com/remarkjs/react-markdown/blob/fda7fa560bec901a6103e195f9b1979dab543b17/lib/index.js#L425
export function defaultUrlTransform(value: string) {
diff --git a/apps/studio/components/ui/AIAssistantPanel/MessageMarkdown.test.tsx b/apps/studio/components/ui/AIAssistantPanel/MessageMarkdown.test.tsx
index c78f8b48b9c..e3edfd4662b 100644
--- a/apps/studio/components/ui/AIAssistantPanel/MessageMarkdown.test.tsx
+++ b/apps/studio/components/ui/AIAssistantPanel/MessageMarkdown.test.tsx
@@ -1,7 +1,74 @@
import { render } from '@testing-library/react'
import { describe, expect, test } from 'vitest'
+
+import { wrapPlaceholderUrls } from './Message.utils'
import { OrderedList } from './MessageMarkdown'
+describe('wrapPlaceholderUrls', () => {
+ test('wraps a bare URL containing a placeholder in backticks', () => {
+ expect(wrapPlaceholderUrls('https://<project-ref>.supabase.co/auth/v1/oauth/authorize')).toBe(
+ '`https://<project-ref>.supabase.co/auth/v1/oauth/authorize`'
+ )
+ })
+
+ test('leaves an already-wrapped URL unchanged', () => {
+ const input = '`https://<project-ref>.supabase.co`'
+ expect(wrapPlaceholderUrls(input)).toBe(input)
+ })
+
+ test('leaves URLs without placeholders unchanged', () => {
+ expect(wrapPlaceholderUrls('https://supabase.com/dashboard')).toBe(
+ 'https://supabase.com/dashboard'
+ )
+ })
+
+ test('wraps bare URL but preserves surrounding text', () => {
+ expect(
+ wrapPlaceholderUrls(
+ 'Authorization endpoint: https://<project-ref>.supabase.co/auth/v1/oauth/authorize'
+ )
+ ).toBe('Authorization endpoint: `https://<project-ref>.supabase.co/auth/v1/oauth/authorize`')
+ })
+
+ test('skips URLs inside markdown link destinations', () => {
+ const input = '[OAuth docs](https://<project-ref>.supabase.co/auth/v1/oauth/authorize)'
+ expect(wrapPlaceholderUrls(input)).toBe(input)
+ })
+
+ test('wraps placeholder URL used as markdown link text', () => {
+ expect(
+ wrapPlaceholderUrls('[https://<project-ref>.supabase.co](https://supabase.com/dashboard)')
+ ).toBe('[`https://<project-ref>.supabase.co`](https://supabase.com/dashboard)')
+ })
+
+ test('wraps bare URL but skips linked URL when both appear in the same string', () => {
+ expect(
+ wrapPlaceholderUrls(
+ 'Use [link](https://<project-ref>.supabase.co) or https://<project-ref>.supabase.co/raw'
+ )
+ ).toBe(
+ 'Use [link](https://<project-ref>.supabase.co) or `https://<project-ref>.supabase.co/raw`'
+ )
+ })
+
+ test('leaves placeholder URLs inside fenced code blocks unchanged', () => {
+ const input = '```\nhttps://<project-ref>.supabase.co\n```'
+ expect(wrapPlaceholderUrls(input)).toBe(input)
+ })
+
+ test('strips trailing prose punctuation before wrapping', () => {
+ expect(wrapPlaceholderUrls('See https://<project-ref>.supabase.co/auth, then proceed.')).toBe(
+ 'See `https://<project-ref>.supabase.co/auth`, then proceed.'
+ )
+ })
+
+ test('wraps URLs whose angle-bracket segment has no hyphen', () => {
+ expect(wrapPlaceholderUrls('https://example.com/path?id=<ref>')).toBe(
+ '`https://example.com/path?id=<ref>`'
+ )
+ })
+})
+
describe('OrderedList', () => {
test('sets counter-reset based on start prop for split lists', () => {
const { container } = render(
diff --git a/apps/studio/components/ui/AIAssistantPanel/MessageMarkdown.tsx b/apps/studio/components/ui/AIAssistantPanel/MessageMarkdown.tsx
index 8ecf0ee4ad4..8b01132a234 100644
--- a/apps/studio/components/ui/AIAssistantPanel/MessageMarkdown.tsx
+++ b/apps/studio/components/ui/AIAssistantPanel/MessageMarkdown.tsx
@@ -1,3 +1,4 @@
+import { ChartConfig } from 'components/interfaces/SQLEditor/UtilityPanel/ChartConfig'
import dynamic from 'next/dynamic'
import Link from 'next/link'
import {
@@ -10,14 +11,11 @@ import {
type ReactElement,
} from 'react'
import type { StreamdownProps } from 'streamdown'
-
-import { ChartConfig } from 'components/interfaces/SQLEditor/UtilityPanel/ChartConfig'
import {
Button,
cn,
CodeBlock,
CodeBlockLang,
- markdownComponents,
Dialog,
DialogClose,
DialogContent,
@@ -26,12 +24,14 @@ import {
DialogSection,
DialogTitle,
DialogTrigger,
+ markdownComponents,
} from 'ui'
+
import { EdgeFunctionBlock } from '../EdgeFunctionBlock/EdgeFunctionBlock'
import { AssistantSnippetProps } from './AIAssistant.types'
import { CollapsibleCodeBlock } from './CollapsibleCodeBlock'
import { DisplayBlockRenderer } from './DisplayBlockRenderer'
-import { defaultUrlTransform } from './Message.utils'
+import { defaultUrlTransform, wrapPlaceholderUrls } from './Message.utils'
const Streamdown = dynamic<StreamdownProps>(
() => import('streamdown').then((mod) => mod.Streamdown),
@@ -155,13 +155,13 @@ export function MessageMarkdown({
}) {
const markdownSource = useMemo(() => {
if (typeof children === 'string') {
- return children
+ return wrapPlaceholderUrls(children)
}
-
if (Array.isArray(children)) {
- return children.filter((child): child is string => typeof child === 'string').join('')
+ return wrapPlaceholderUrls(
+ children.filter((child): child is string => typeof child === 'string').join('')
+ )
}
-
return ''
}, [children])
diff --git a/apps/studio/lib/ai/prompts.ts b/apps/studio/lib/ai/prompts.ts
index d212c09bb91..4e0fbccea75 100644
--- a/apps/studio/lib/ai/prompts.ts
+++ b/apps/studio/lib/ai/prompts.ts
@@ -611,7 +611,7 @@ export const CHAT_PROMPT = `
- When invoking a tool, call it directly without pausing.
- Provide succinct outputs unless the complexity of the user request requires additional explanation.
- Be confident in your responses and tool calling
-- When referencing template URLs with placeholders, use angle bracket syntax (e.g., \`https://<project-ref>.supabase.co\`)
+- Always format template URLs as inline code using backticks and angle brackets (e.g., \`https://<project-ref>.supabase.co\`)
## Chat Naming
- At the start of each conversation, if the chat is unnamed, call \`rename_chat\` with a succinct 2–4 word descriptive name (e.g., "User Authentication Setup", "Sales Data Analysis", "Product Table Creation").