Mayson Monorepo Documentation
    Preparing search index...
    Index

    Constructors

    • Creates a new AIManager instance configured for OpenRouter

      Parameters

      • OptionalapiKey: string

        OpenRouter API key (defaults to OPENROUTER_API_KEY env var)

      Returns default

    Methods

    • Checks if a model ID is valid and available

      Parameters

      • modelId: string

        The model ID to validate

      Returns Promise<boolean>

      True if the model is valid, false otherwise

    • Fetches all available models from OpenRouter with their pricing information Results are cached in Redis for 3 hours to avoid excessive API calls

      Returns Promise<AIModel[]>

      Array of available models with pricing details

    • Checks if a user has an active subscription and is within their spending limits

      Parameters

      • userId: string

        The user ID to check

      Returns Promise<void>

      SubscriptionError if user doesn't have access

    • Sends a chat completion request to OpenRouter and returns the response with usage data and cost Automatically fetches pricing data if not already cached Records usage to the database within a MongoDB transaction Checks subscription status and reports metered overage to Stripe

      Parameters

      • model: string

        The model ID to use (e.g., "anthropic/claude-3.5-sonnet")

      • messages: AIMessage[]

        Array of chat messages

      • userId: string

        The user ID to associate with this usage record (required)

      Returns Promise<AIMessageResponse>

      Object containing the AI's response text, token usage, cost, and detailed cost breakdown

      Error if userId is not provided or if any part of the operation fails

      SubscriptionError if user doesn't have an active subscription or has exceeded limits

    • Sends a chat completion request with tool definitions to OpenRouter Used by the agent loop for LLM-driven tool orchestration

      Parameters

      • model: string

        The model ID to use (e.g., "anthropic/claude-sonnet-4.5")

      • messages: AIMessageWithTools[]

        Array of chat messages (including tool results)

      • tools: AIToolDefinition[]

        Array of tool definitions

      • userId: string

        The user ID to associate with this usage record

      Returns Promise<AIMessageWithToolsResponse>

      Object containing the AI's response, tool calls, usage, and cost

    • Calculates the cost of a completed API request based on token usage and model pricing Uses Decimal.js for precise financial calculations without floating-point errors Automatically fetches pricing data if not already cached

      Parameters

      • model: string

        The model ID that was used

      • usage: AITokenUsage

        Token usage information from the API response

      Returns Promise<number>

      Cost in USD as a number

    • Calculates detailed cost breakdown for a completed API request Uses Decimal.js for precise financial calculations without floating-point errors Automatically fetches pricing data if not already cached

      Parameters

      • model: string

        The model ID that was used

      • usage: AITokenUsage

        Token usage information from the API response

      Returns Promise<AICostBreakdown>

      Detailed cost breakdown with prompt, completion, and total costs

    • Clears the pricing and models cache from Redis, forcing fresh data on next request

      Returns Promise<void>

    • Transcribes audio to text using Replicate's Whisper model Records usage to the database and calculates cost based on predict_time

      Parameters

      • audioInput: string

        Either a data URI (data:audio/...) or a URL to the audio file

      • userId: string

        The user ID to associate with this usage record (required)

      • Optionallanguage: string

        Optional language hint (e.g., "en", "es")

      Returns Promise<TranscribeAudioResponse>

      Object containing transcribed text, duration, and cost

      SubscriptionError if user doesn't have an active subscription or has exceeded limits