oxylabs-ai-studio
Version:
JavaScript SDK for Oxylabs AI Studio API services
72 lines • 2.48 kB
JavaScript
/**
* AI-Map Service
* Handles all AI-Map related API calls
*/
export class AiMapService {
constructor(client) {
this.client = client;
}
/**
* Submit map request (POST /map)
*/
async submitMapRequest(options) {
const payload = {
url: options.url,
user_prompt: options.user_prompt,
max_depth: options.max_depth,
return_sources_limit: options.return_sources_limit,
geo_location: options.geo_location,
render_html: options.render_javascript || false
};
return await this.client.post('/map', payload);
}
/**
* Get map run status (GET /map/run)
*/
async getMapRun(runId) {
if (!runId) {
throw new Error('run_id is required');
}
const params = new URLSearchParams();
params.append('run_id', runId);
const url = `/map/run?${params.toString()}`;
return await this.client.get(url);
}
/**
* Get map run data/results (GET /map/run/data)
*/
async getMapRunData(runId) {
if (!runId) {
throw new Error('run_id is required');
}
const params = new URLSearchParams();
params.append('run_id', runId);
const url = `/map/run/data?${params.toString()}`;
return await this.client.get(url);
}
/**
* Map URLs
*/
async map(options, timeout = 60000, pollInterval = 5000) {
const submitResult = await this.submitMapRequest(options);
const runId = submitResult.run_id || submitResult.id;
if (!runId) {
throw new Error('No run ID returned from map request');
}
const startTime = Date.now();
while (Date.now() - startTime < timeout) {
const runStatus = await this.getMapRun(runId);
const run_status = runStatus.status;
console.log('Run status:', run_status);
if (run_status === 'completed' || run_status === 'success') {
return await this.getMapRunData(runId);
}
else if (run_status === 'failed' || run_status === 'error') {
throw new Error(`Mapping failed: ${runStatus.error || runStatus.message || 'Unknown error'}`);
}
await new Promise(resolve => setTimeout(resolve, pollInterval));
}
throw new Error(`Mapping timeout after ${timeout}ms`);
}
}
//# sourceMappingURL=aiMap.js.map