Chat

This endpoint provides an AI-powered chat interface that can reference and understand your saved memories. It supports streaming responses and maintains conversation context.

POST /api/chat

Request Body

{
  // Array of previous messages in the conversation
  messages: Array<{
    role: "user" | "assistant" | "system",
    content: string | Array<{
      type: "text" | "image" | "file",
      text?: string,
      image?: string,
      data?: string
    }>,
    annotations?: any[],
    experimental_attachments?: any[]
  }>,
  
  // Optional thread ID for continuing a conversation
  threadId?: string
}

Field Descriptions

FieldTypeRequiredDescription
messagesarrayYesArray of message objects representing the conversation history
messages[].rolestringYesRole of the message sender (“user”, “assistant”, or “system”)
messages[].contentstring | arrayYesMessage content, either as plain text or structured content
messages[].annotationsarrayNoAdditional metadata or annotations for the message
messages[].experimental_attachmentsarrayNoExperimental support for file attachments
threadIdstringNoID of an existing conversation thread to continue

Response

The endpoint returns a streaming response with chunks of the AI’s reply. The response includes:

  1. Text content streamed as it’s generated
  2. Context annotations with relevant memories
  3. Thread management information

Headers

HeaderDescription
Supermemory-Thread-UuidUnique identifier for the conversation thread
Content-Typetext/x-unknown
content-encodingidentity
transfer-encodingchunked

Stream Format

The response is streamed as text chunks with special annotations for context:

{
  text: string,           // The AI's response text
  annotations?: {         // Optional context from your memories
    similarity: number,   // Relevance score (0-1)
    id: string,          // Document ID
    content: string,     // Document content
    type: string,        // Content type
    url: string,         // Source URL
    title: string,       // Document title
    description?: string,// Document description
    ogImage?: string,    // Open Graph image URL
    createdAt: string,   // ISO timestamp
    updatedAt?: string   // ISO timestamp
  }[]
}

Error Responses

400 Bad Request

{
  "error": "Invalid prompt - please rephrase your message"
}

401 Unauthorized

{
  "error": "Unauthorized"
}

500 Internal Server Error

{
  "error": "An unexpected error occurred",
  "details": "Error details (in development mode)"
}

503 Service Unavailable

{
  "error": "Database connection failed"
}

Features

Memory Context

  • Automatically searches your saved memories for relevant context
  • Uses semantic search with cosine similarity
  • Includes content with similarity score > 0.4
  • Returns up to 5 most relevant unique documents

Thread Management

  • Creates new thread if threadId is not provided
  • Maintains conversation history
  • Updates thread with new messages automatically

Smart Processing

  • Filters out empty messages
  • Supports structured content (text, images, files)
  • Normalizes similarity scores for better context selection
  • Implements caching and performance optimizations

Examples

Starting a New Chat

{
  "messages": [
    {
      "role": "user",
      "content": "What do I know about machine learning?"
    }
  ]
}

Continuing a Conversation

{
  "messages": [
    {
      "role": "user",
      "content": "Tell me more about neural networks"
    }
  ],
  "threadId": "chat-123-abc"
}

Using Structured Content

{
  "messages": [
    {
      "role": "user",
      "content": [
        {
          "type": "text",
          "text": "What's in this image?"
        },
        {
          "type": "image",
          "image": "https://example.com/image.jpg"
        }
      ]
    }
  ]
}

Notes

  1. Authentication is required
  2. Responses are streamed in real-time
  3. Context is automatically injected based on message content
  4. Supports both text and structured content
  5. Thread history is automatically maintained
  6. Performance optimizations include:
    • Semantic search caching
    • Similarity score normalization
    • Unique document deduplication