Skip to content

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

7.0 KB
Raw Blame
import * as ai from 'ai'
import {
  convertToModelMessages,
  isStepCount,
  type LanguageModel,
  type ModelMessage,
  type SystemModelMessage,
  type TimeoutConfiguration,
  type ToolSet,
  type UIMessage,
} from 'ai'
import { startSpan, traced, withCurrent, wrapAISDK, type Span } from 'braintrust'
import { source } from 'common-tags'

import type { AssistantEvalInput } from '@/evals/scorer'
import type { AiOptInLevel } from '@/hooks/misc/useOrgOptedIntoAi'
import { buildAssistantContextMessages, NO_SCHEMA_ACCESS_MESSAGE } from '@/lib/ai/assistant-context'
import { IS_TRACING_ENABLED } from '@/lib/ai/braintrust-logger'
import {
  hideToolErrorsFromModel,
  prepareMessagesForModel,
} from '@/lib/ai/generate-assistant-response.utils'
import {
  CHAT_PROMPT,
  GENERAL_PROMPT,
  LIMITATIONS_PROMPT,
  NOTEBOOKS_PROMPT,
  SECURITY_PROMPT,
} from '@/lib/ai/prompts'
import type { contract as renameChatContract } from '@/lib/ai/tools/rename-chat'
import type { ToolContractInput } from '@/lib/ai/tools/types'

const { streamText: tracedStreamText } = wrapAISDK(ai)

export async function generateAssistantResponse({
  messages: rawMessages,
  model,
  tools,
  aiOptInLevel = 'schema',
  getSchemas,
  projectRef,
  chatId,
  chatName,
  allowTracing,
  supportMode,
  userId,
  orgId,
  orgSlug,
  planId,
  isHighComplianceProject,
  includesLogsSnippets,
  isExplorerEnabled,
  systemProviderOptions,
  providerOptions,
  requestedModel,
  effectiveModel,
  abortSignal,
  timeout,
  onSpanCreated,
}: {
  messages: UIMessage[]
  model: LanguageModel
  tools: ToolSet
  aiOptInLevel?: AiOptInLevel
  getSchemas?: () => Promise<string>
  projectRef?: string
  chatId?: string
  chatName?: string
  allowTracing?: boolean
  supportMode?: boolean
  userId?: string
  orgId?: number
  orgSlug?: string
  planId?: string
  isHighComplianceProject?: boolean
  /** Whether any user message in the conversation attached a logs (ClickHouse) query. */
  includesLogsSnippets?: boolean
  isExplorerEnabled?: boolean
  requestedModel?: string
  /** The model actually used after entitlement/throttle fallback. */
  effectiveModel?: string
  systemProviderOptions?: Record<string, any>
  providerOptions?: Record<string, any>
  abortSignal?: AbortSignal
  timeout?: TimeoutConfiguration<ToolSet>
  onSpanCreated?: (spanId: string) => void
}) {
  const shouldTrace = allowTracing ?? IS_TRACING_ENABLED

  const run = async (span?: Span) => {
    const messages = prepareMessagesForModel(rawMessages, aiOptInLevel)

    const schemasString =
      aiOptInLevel !== 'disabled' && getSchemas
        ? shouldTrace
          ? await traced(async () => getSchemas(), { name: 'getSchemas', type: 'function' })
          : await getSchemas()
        : NO_SCHEMA_ACCESS_MESSAGE

    // Important: do not use per-request dynamic content in the system prompt or Bedrock will
    // not cache it. isExplorerEnabled is a per-user flag, not per-request, so it only produces
    // two prompt variants (on/off) rather than defeating caching.
    const system = source`
      ${GENERAL_PROMPT}
      ${CHAT_PROMPT}
      ${isExplorerEnabled ? NOTEBOOKS_PROMPT : ''}
      ${SECURITY_PROMPT}
      ${LIMITATIONS_PROMPT}

      ## Available Knowledge

      Before writing SQL or answering questions about the following topics, call \`load_knowledge\` to load detailed knowledge:
      - \`pg_best_practices\` — PostgreSQL best practices. Always load before writing any SQL, even simple queries.
      - \`logs\` — ClickHouse SQL against the project's logs table. Always load before calling \`query_logs\`.
      - \`rls\` — Row Level Security policies for database tables.
      - \`storage\` — Supabase Storage buckets, public/private bucket access, and \`storage.objects\` policies. Always load before creating Storage buckets or \`storage.objects\` policies.
      - \`edge_functions\` — Supabase Edge Functions
      - \`realtime\` — Supabase Realtime
    `

    const systemMessage: SystemModelMessage = {
      role: 'system',
      content: system,
      ...(systemProviderOptions && { providerOptions: systemProviderOptions }),
    }

    const coreMessages: ModelMessage[] = [
      ...buildAssistantContextMessages({
        projectRef,
        chatName,
        schemasString,
        supportMode,
        includesLogsSnippets,
        aiOptInLevel,
      }),
      ...(await convertToModelMessages(messages)),
    ]

    const streamTextFn = shouldTrace ? tracedStreamText : ai.streamText

    // onEnd still fires after an abort once a step has finished, so end the span only once.
    let isSpanEnded = false
    const endSpan = (metadata: Record<string, unknown>) => {
      if (!span || isSpanEnded) return
      isSpanEnded = true
      span.log({ metadata })
      span.end()
    }

    return streamTextFn({
      model,
      instructions: systemMessage,
      stopWhen: isStepCount(20),
      messages: coreMessages,
      prepareStep: ({ messages }) => ({
        messages: hideToolErrorsFromModel(messages, aiOptInLevel),
      }),
      ...(providerOptions && { providerOptions }),
      tools,
      ...(abortSignal && { abortSignal }),
      ...(timeout && { timeout }),
      ...(span && {
        onEnd: ({ steps, finishReason }) => {
          const metadata: Record<string, unknown> = {
            isFinalStep: finishReason === 'stop',
          }
          for (const step of steps) {
            for (const toolCall of step.toolCalls) {
              if (toolCall.toolName === 'rename_chat') {
                const { newName } = toolCall.input as ToolContractInput<typeof renameChatContract>
                metadata.chatName = newName
              }
            }
          }
          endSpan(metadata)
        },
        // The call aborts on either the request signal or `timeout`, so an unaborted
        // request signal means the deadline stopped it.
        onAbort: () => {
          endSpan({ isAborted: true, isTimedOut: !abortSignal?.aborted })
        },
      }),
    } satisfies Parameters<typeof ai.streamText>[0])
  }

  if (shouldTrace) {
    // startSpan instead of traced() so we control when the span closes via onEnd.
    // Scorers read from child spans (LLM + tool) in the trace rather than a root span output field.
    const span = startSpan({ name: 'generateAssistantResponse', type: 'function' })
    onSpanCreated?.(span.id)

    const lastUserMessage = rawMessages.findLast((m) => m.role === 'user')
    const lastUserText = lastUserMessage?.parts
      ?.filter((p): p is { type: 'text'; text: string } => p.type === 'text')
      .map((p) => p.text)
      .join('\n')

    span.log({
      input: { prompt: lastUserText ?? '' } satisfies AssistantEvalInput,
      metadata: {
        projectRef,
        chatId,
        chatName,
        aiOptInLevel,
        userId,
        orgId,
        orgSlug,
        planId,
        isHighComplianceProject,
        requestedModel,
        effectiveModel,
        gitBranch: process.env.VERCEL_GIT_COMMIT_REF,
        environment: process.env.NEXT_PUBLIC_ENVIRONMENT,
      },
    })

    return withCurrent(span, () => run(span))
  }

  return run()
}