Skip to content

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

4.4 KB
Raw Blame
import type { ToolUIPart, UIMessage } from 'ai'

export function createUserMessage(content: string, id = 'user-msg-1'): UIMessage {
  return {
    id,
    role: 'user',
    parts: [
      {
        type: 'text',
        text: content,
      },
    ],
  }
}

export function createAssistantTextMessage(content: string, id = 'assistant-msg-1'): UIMessage {
  return {
    id,
    role: 'assistant',
    parts: [
      {
        type: 'text',
        text: content,
      },
    ],
  }
}

export function createAssistantMessageWithExecuteSqlTool(
  query: string,
  results: Array<Record<string, any>> = [{ id: 1, name: 'test' }],
  id = 'assistant-tool-msg-1'
): UIMessage {
  return {
    id,
    role: 'assistant',
    parts: [
      {
        type: 'text',
        text: "I'll run that SQL query for you.",
      },
      {
        type: 'tool-execute_sql',
        state: 'output-available',
        toolCallId: 'call-123',
        input: { sql: query },
        output: { rows: results, optInLevel: 'schema_and_log_and_data' },
      } satisfies ToolUIPart,
    ],
  }
}

export function createAssistantMessageWithUpdateNotebookTool(
  output: Record<string, unknown> = {
    id: 'notebook-1',
    name: 'Signup funnel',
    previous_content: { schema_version: 1, cells: [] },
  },
  id = 'assistant-notebook-msg-1'
): UIMessage {
  return {
    id,
    role: 'assistant',
    parts: [
      {
        type: 'tool-update_notebook',
        state: 'output-available',
        toolCallId: 'call-notebook-1',
        input: { id: 'notebook-1', expected_updated_at: '2026-01-01T00:00:00.000Z' },
        output,
      } satisfies ToolUIPart,
    ],
  }
}

export function createAssistantMessageWithCreateNotebookTool(
  output: Record<string, unknown> = {
    id: 'notebook-1',
    name: 'Signup funnel',
  },
  id = 'assistant-notebook-msg-1'
): UIMessage {
  return {
    id,
    role: 'assistant',
    parts: [
      {
        type: 'tool-create_notebook',
        state: 'output-available',
        toolCallId: 'call-notebook-1',
        input: { name: 'Signup funnel', content: { schema_version: 1, cells: [] } },
        output,
      } satisfies ToolUIPart,
    ],
  }
}

export function createAssistantMessageWithDeleteNotebookTool(
  output: Record<string, unknown> = {
    id: 'notebook-1',
    name: 'Signup funnel',
  },
  id = 'assistant-notebook-msg-1'
): UIMessage {
  return {
    id,
    role: 'assistant',
    parts: [
      {
        type: 'tool-delete_notebook',
        state: 'output-available',
        toolCallId: 'call-notebook-1',
        input: { id: 'notebook-1' },
        output,
      } satisfies ToolUIPart,
    ],
  }
}

export function createAssistantMessageWithMultipleTools(
  id = 'assistant-multi-tool-msg-1'
): UIMessage {
  return {
    id,
    role: 'assistant',
    parts: [
      {
        type: 'text',
        text: 'Let me check the database structure and run some queries.',
      },
      {
        type: 'tool-execute_sql',
        state: 'output-available',
        toolCallId: 'call-456',
        input: { sql: 'SELECT * FROM users LIMIT 5' },
        output: [
          { id: 1, email: '[email protected]' },
          { id: 2, email: '[email protected]' },
        ],
      } satisfies ToolUIPart,
      {
        type: 'tool-execute_sql',
        state: 'output-available',
        toolCallId: 'call-789',
        toolName: 'execute_sql',
        input: { sql: 'DESCRIBE users' },
        output: [
          { column: 'id', type: 'integer', nullable: false },
          { column: 'email', type: 'varchar', nullable: false },
        ],
      } as ToolUIPart,
    ],
  }
}

export function createLongConversation(): Array<UIMessage> {
  return [
    createUserMessage('Show me all users', 'msg-1'),
    createAssistantMessageWithExecuteSqlTool('SELECT * FROM users', [{ id: 1 }], 'msg-2'),
    createUserMessage('How many users are there?', 'msg-3'),
    createAssistantMessageWithExecuteSqlTool(
      'SELECT COUNT(*) FROM users',
      [{ count: 100 }],
      'msg-4'
    ),
    createUserMessage('Show me the schema', 'msg-5'),
    createAssistantTextMessage("Here's the database schema...", 'msg-6'),
    createUserMessage('Create a new table', 'msg-7'),
    createAssistantMessageWithExecuteSqlTool(
      'CREATE TABLE posts (id SERIAL PRIMARY KEY)',
      [],
      'msg-8'
    ),
    createUserMessage('Add some data', 'msg-9'),
    createAssistantMessageWithExecuteSqlTool(
      "INSERT INTO posts (title) VALUES ('Test')",
      [],
      'msg-10'
    ),
  ]
}