Skip to content

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

2.9 KB
Raw Blame
import {
  isToolUIPart,
  type DynamicToolUIPart,
  type ModelMessage,
  type ToolUIPart,
  type UIMessage,
} from 'ai'

import type { AiOptInLevel } from '@/hooks/misc/useOrgOptedIntoAi'
import { isOptInLevelAtLeast } from '@/lib/ai/opt-in-level'
import { NO_DATA_ERROR } from '@/lib/ai/tools/execute-sql-output'
import { decodeNotebookToolError } from '@/lib/ai/tools/notebook-tool-error'
import { sanitizeMessagePart } from '@/lib/ai/tools/tool-sanitizer'

const INVALID_TOOL_STATES = [
  'input-streaming',
  'input-available',
  'approval-requested',
  'output-error',
]

/**
 * Notebook tool errors are opted into model visibility explicitly (NotebookToolError,
 * encoded in generate-v4.ts's onError). A successful parse against the schema — including
 * its literal `tag` — is proof enough that this was a NotebookToolError; every other
 * output-error stays hidden, same as before. Rewrites errorText back to the plain message
 * so the model sees prose, not JSON.
 */
function exposedNotebookErrorPart(
  part: ToolUIPart | DynamicToolUIPart
): ToolUIPart | DynamicToolUIPart | null {
  if (part.state !== 'output-error') return null
  const decoded = decodeNotebookToolError(part.errorText)
  if (!decoded?.exposeToAssistant) return null
  return { ...part, errorText: decoded.message }
}

/** Trims history to the last 7 messages and strips tool parts the model shouldn't see. */
export function prepareMessagesForModel(rawMessages: UIMessage[], aiOptInLevel: AiOptInLevel) {
  return (rawMessages || []).slice(-7).map((msg) => {
    if (msg && msg.role === 'assistant' && 'results' in msg) {
      const cleanedMsg = { ...msg }
      delete cleanedMsg.results
      return cleanedMsg
    }
    if (msg && msg.role === 'assistant' && msg.parts) {
      const cleanedParts = msg.parts.flatMap((part) => {
        if (!isToolUIPart(part)) return [part]
        if (!INVALID_TOOL_STATES.includes(part.state))
          return [sanitizeMessagePart(part, aiOptInLevel)]
        const exposed = exposedNotebookErrorPart(part)
        return exposed ? [sanitizeMessagePart(exposed, aiOptInLevel)] : []
      })
      return { ...msg, parts: cleanedParts }
    }
    return msg
  })
}

/**
 * Database errors can echo row values. Sanitize same-request steps here.
 * `prepareMessagesForModel` drops errors from later turns.
 */
export function hideToolErrorsFromModel(messages: ModelMessage[], aiOptInLevel: AiOptInLevel) {
  if (isOptInLevelAtLeast(aiOptInLevel, 'schema_and_log_and_data')) return messages

  return messages.map((message) => {
    if (message.role !== 'tool') return message
    return {
      ...message,
      content: message.content.map((part) =>
        part.type === 'tool-result' &&
        part.toolName === 'execute_sql' &&
        (part.output.type === 'error-text' || part.output.type === 'error-json')
          ? { ...part, output: { type: 'error-text' as const, value: NO_DATA_ERROR } }
          : part
      ),
    }
  })
}