// Bagoodex Search - Internal AI services search // Endpoints: /bagoodex/* import { type AIMLAPIClient } from '../client'; import { buildPath } from './_paths'; // ===== VERSION ===== const VERSION = 'v1' as const; /** * Bagoodex Image Result */ export interface BagoodexImageResult { source?: string; original: string; title?: string; source_name?: string; } /** * Bagoodex Video Result */ export interface BagoodexVideoResult { link: string; thumbnail: string; title?: string; } /** * Bagoodex Knowledge Result */ export interface BagoodexKnowledgeResult { title?: string; type?: string; description?: string; born?: string; died?: string; } /** * Bagoodex Weather Result */ export interface BagoodexWeatherResult { type?: string; temperature?: string; unit?: string; precipitation?: string; humidity?: string; wind?: string; location?: string; date?: string; weather?: string; thumbnail?: string; forecast?: Array<{ day: string; temperature: { high: string; low: string; }; thumbnail: string; weather: string; humidity: string; precipitation: string; wind: string; }>; hourly_forecast?: Array<{ time: string; thumbnail: string; weather: string; temperature: string; precipitation: string; humidity: string; wind: string; }>; } /** * Bagoodex Local Map Result */ export interface BagoodexLocalMapResult { link?: string; image?: string; gps_coordinates?: { latitude: number; longitude: number; }; } /** * Bagoodex Links Result */ export interface BagoodexLinksResult { query: string; links: Array<{ url: string; title: string; description?: string; domain?: string; }>; } /** * Bagoodex Search Response (for arrays like images, videos) */ export type BagoodexArrayResponse = T[] & { metadata?: BagoodexMetadata }; /** * Bagoodex Search Response (for objects like links) */ export interface BagoodexLinksResponse { query: string; links: Array<{ url: string; title: string; description?: string; domain?: string; }>; metadata?: BagoodexMetadata; } /** * Bagoodex metadata */ export interface BagoodexMetadata { query: string; timestamp: string; source: string; } /** * Chat completion response for generating followup_id */ export interface BagoodexChatResponse { id: string; object: string; created: number; model: string; choices: Array<{ index: number; message: { role: string; content: string | null; }; finish_reason: string; }>; usage?: { prompt_tokens: number; completion_tokens: number; total_tokens: number; }; } /** * Bagoodex Search resource for internal AI services */ export class Bagoodex { constructor(private readonly _client: AIMLAPIClient) {} /** * Generate followup_id using chat completion * @private */ private async generateFollowupId(query: string): Promise { const response = await this._client.post( buildPath('/chat/completions', VERSION), { body: { model: 'bagoodex/bagoodex-search-v1', messages: [ { role: 'user', content: query, }, ], }, } ); return response.id; } /** * Search for images. * @see https://docs.aimlapi.com/api-reference/bagoodex/images * * @example * ```typescript * const results = await client.bagoodex.searchImages("cute cats"); * console.log(results[0].original); * ``` */ async searchImages(query: string): Promise> { const followupId = await this.generateFollowupId(query); return this._client.get>( buildPath('/bagoodex/images', VERSION), { query: { followup_id: followupId }, } ); } /** * Search for videos. * @see https://docs.aimlapi.com/api-reference/bagoodex/videos * * @example * ```typescript * const results = await client.bagoodex.searchVideos("python tutorial"); * console.log(results[0].link); * ``` */ async searchVideos(query: string): Promise> { const followupId = await this.generateFollowupId(query); return this._client.get>( buildPath('/bagoodex/videos', VERSION), { query: { followup_id: followupId }, } ); } /** * Search for knowledge/information. * @see https://docs.aimlapi.com/api-reference/bagoodex/knowledge * * @example * ```typescript * const results = await client.bagoodex.searchKnowledge("how does photosynthesis work"); * console.log(results.results.title); * ``` */ async searchKnowledge(query: string): Promise { const followupId = await this.generateFollowupId(query); return this._client.get(buildPath('/bagoodex/knowledge', VERSION), { query: { followup_id: followupId }, }); } /** * Get weather information for a location. * @see https://docs.aimlapi.com/api-reference/bagoodex/weather * * @example * ```typescript * const weather = await client.bagoodex.getWeather("New York, NY"); * console.log(`Temperature: ${weather.results.temperature}°${weather.results.unit}`); * ``` */ async getWeather(location: string): Promise { const followupId = await this.generateFollowupId(location); return this._client.get(buildPath('/bagoodex/weather', VERSION), { query: { followup_id: followupId }, }); } /** * Get local map information for a query. * @see https://docs.aimlapi.com/api-reference/bagoodex/local-map * * @example * ```typescript * const map = await client.bagoodex.getLocalMap("coffee shops in downtown Seattle"); * console.log(map.results.link); * ``` */ async getLocalMap(query: string): Promise { const followupId = await this.generateFollowupId(query); return this._client.get(buildPath('/bagoodex/local-map', VERSION), { query: { followup_id: followupId }, }); } /** * Get links for a query. * @see https://docs.aimlapi.com/api-reference/bagoodex/links * * @example * ```typescript * const links = await client.bagoodex.getLinks("TypeScript documentation"); * console.log(links.links); * ``` */ async getLinks(query: string): Promise { const followupId = await this.generateFollowupId(query); return this._client.get(buildPath('/bagoodex/links', VERSION), { query: { followup_id: followupId }, }); } }