SDK
Detailed description of the Javascript SDK
fetchAd()
Fetches and renders an ad based on the provided parameters and callbacks.
Parameters
Configuration options for fetching ads
Optional event handlers for the ad fetching process
markAdAsViewed()
Marks an ad as viewed when it becomes visible in the viewport.
Parameters
The ID of the impression to be marked as viewed
The server URL. Defaults to “https://server.megabrain.co”
Message Object
The Message object represents a single message in the conversation context. Each message must have:
Unique identifier for the message
Timestamp when message was created (ISO 8601 string or Date object)
Who sent the message: “user” | “assistant”
The text content of the message
Example Message Array
Error Handling
The SDK provides robust error handling with:
- Request timeout after 16 seconds by default
- Automatic retry up to 3 times for retriable errors
- Unretriable errors include HTTP 403, 429, and 404
Example error handling:
Best Practices
- Chronological Order: Ensure messages are listed in chronological order
- Context Completeness: Include sufficient prior messages for context
- Assistant Reply: Always end messages array with an “assistant” role message
- Unique IDs: Use unique, immutable identifiers for each message