// AIMLAPI Client - Wrapper around OpenAI SDK with AIMLAPI-specific defaults import OpenAI, { type ClientOptions } from 'openai'; import { DEFAULT_BASE_URL, AZURE_DEFAULT_BASE_URL } from './_version'; import { getEnv, API_KEY_ENV_VARS, BASE_URL_ENV_VARS, AIMLAPI_HEADERS } from './_constants'; import { Images } from './resources/images.js'; import { SpeechToText } from './resources/speech-to-text.js'; import { TextToSpeech } from './resources/text-to-speech.js'; import { Messages } from './resources/messages.js'; import { OCR } from './resources/ocr.js'; import { Video } from './resources/video.js'; import { Bagoodex } from './resources/bagoodex.js'; import { Music } from './resources/music.js'; import { Account } from './resources/account.js'; import { Batches } from './resources/batches.js'; // Re-export OpenAI for convenience export { OpenAI }; /** * Interface for client passed to resources. * Extends the basic HTTP methods with baseURL access. */ export interface AIMLAPIClient { post: ( path: string, options?: { body?: unknown | undefined; query?: Record | undefined; headers?: Record | undefined; } ) => Promise; get: ( path: string, options?: { query?: Record | undefined } ) => Promise; baseURL: string; } export interface AIMLAPIClientOptions extends Omit { /** * API key for authentication. * Defaults to process.env['AIML_API_KEY']. */ apiKey?: ClientOptions['apiKey']; /** * Override the default base URL. * Defaults to process.env['AIML_API_BASE']. */ baseURL?: ClientOptions['baseURL']; /** * Azure endpoint (only for AzureAIMLAPI). */ azure_endpoint?: string | undefined; } /** * Get default headers for AIMLAPI requests */ function getDefaultHeaders( opts?: ClientOptions['defaultHeaders'] ): ClientOptions['defaultHeaders'] { return { ...AIMLAPI_HEADERS, ...(opts || {}), }; } /** * Base client interface with AIMLAPI-specific resources */ interface AIMLAPIBaseClient { /** * Speech-to-text using new API with slam-1 and other models */ speechToText: SpeechToText; /** * Text-to-speech using various providers */ textToSpeech: TextToSpeech; /** * Messages API for Anthropic-compatible endpoint */ messages: Messages; /** * OCR and Vision processing */ ocr: OCR; /** * Video generation using various providers */ video: Video; /** * Bagoodex search for internal AI services */ bagoodex: Bagoodex; /** * Music generation with multiple providers */ music: Music; /** * Account balance and model information */ account: Account; /** * Batch processing for chat completions */ batch: Batches; } /** * Synchronous AIMLAPI Client */ export class AIMLAPI extends OpenAI implements AIMLAPIBaseClient { constructor(opts: AIMLAPIClientOptions = {}) { const apiKey = opts.apiKey ?? (getEnv(...API_KEY_ENV_VARS) as ClientOptions['apiKey']); const baseURL = opts.baseURL ?? getEnv(...BASE_URL_ENV_VARS) ?? DEFAULT_BASE_URL; super({ ...opts, apiKey, baseURL, defaultHeaders: getDefaultHeaders(opts?.defaultHeaders), dangerouslyAllowBrowser: opts.dangerouslyAllowBrowser ?? true, }); } private _speechToTextInstance: SpeechToText | null = null; private _textToSpeechInstance: TextToSpeech | null = null; private _messagesInstance: Messages | null = null; private _ocrInstance: OCR | null = null; private _videoInstance: Video | null = null; private _bagoodexInstance: Bagoodex | null = null; private _musicInstance: Music | null = null; private _accountInstance: Account | null = null; private _batchInstance: Batches | null = null; get speechToText(): SpeechToText { if (!this._speechToTextInstance) { this._speechToTextInstance = new SpeechToText(this); } return this._speechToTextInstance; } get textToSpeech(): TextToSpeech { if (!this._textToSpeechInstance) { this._textToSpeechInstance = new TextToSpeech(this); } return this._textToSpeechInstance; } get messages(): Messages { if (!this._messagesInstance) { this._messagesInstance = new Messages(this); } return this._messagesInstance; } get ocr(): OCR { if (!this._ocrInstance) { this._ocrInstance = new OCR(this); } return this._ocrInstance; } get video(): Video { if (!this._videoInstance) { this._videoInstance = new Video(this); } return this._videoInstance; } get bagoodex(): Bagoodex { if (!this._bagoodexInstance) { this._bagoodexInstance = new Bagoodex(this); } return this._bagoodexInstance; } get music(): Music { if (!this._musicInstance) { this._musicInstance = new Music(this); } return this._musicInstance; } get account(): Account { if (!this._accountInstance) { this._accountInstance = new Account(this); } return this._accountInstance; } get batch(): Batches { if (!this._batchInstance) { this._batchInstance = new Batches(this); } return this._batchInstance; } } export class AzureAIMLAPI extends OpenAI implements AIMLAPIBaseClient { constructor(opts: AIMLAPIClientOptions = {}) { const apiKey = opts.apiKey ?? (getEnv(...API_KEY_ENV_VARS) as ClientOptions['apiKey']); const baseURL = opts.azure_endpoint ? undefined : (opts.baseURL ?? getEnv(...BASE_URL_ENV_VARS) ?? AZURE_DEFAULT_BASE_URL); super({ ...opts, apiKey, baseURL, defaultHeaders: getDefaultHeaders(opts?.defaultHeaders), } as ClientOptions); } private _speechToTextInstance: SpeechToText | null = null; private _textToSpeechInstance: TextToSpeech | null = null; private _messagesInstance: Messages | null = null; private _ocrInstance: OCR | null = null; private _videoInstance: Video | null = null; private _bagoodexInstance: Bagoodex | null = null; private _musicInstance: Music | null = null; private _accountInstance: Account | null = null; private _batchInstance: Batches | null = null; get speechToText(): SpeechToText { if (!this._speechToTextInstance) { this._speechToTextInstance = new SpeechToText(this); } return this._speechToTextInstance; } get textToSpeech(): TextToSpeech { if (!this._textToSpeechInstance) { this._textToSpeechInstance = new TextToSpeech(this); } return this._textToSpeechInstance; } get messages(): Messages { if (!this._messagesInstance) { this._messagesInstance = new Messages(this); } return this._messagesInstance; } get ocr(): OCR { if (!this._ocrInstance) { this._ocrInstance = new OCR(this); } return this._ocrInstance; } get video(): Video { if (!this._videoInstance) { this._videoInstance = new Video(this); } return this._videoInstance; } get bagoodex(): Bagoodex { if (!this._bagoodexInstance) { this._bagoodexInstance = new Bagoodex(this); } return this._bagoodexInstance; } get music(): Music { if (!this._musicInstance) { this._musicInstance = new Music(this); } return this._musicInstance; } get account(): Account { if (!this._accountInstance) { this._accountInstance = new Account(this); } return this._accountInstance; } get batch(): Batches { if (!this._batchInstance) { this._batchInstance = new Batches(this); } return this._batchInstance; } } // Type aliases for convenience export type Client = AIMLAPI; export type AzureClient = AzureAIMLAPI; export { Images, Messages, OCR, Video, Bagoodex, Music, Batches, Account };