oxylabs-ai-studio
Version:
JavaScript SDK for Oxylabs AI Studio API services
96 lines • 3.54 kB
JavaScript
/**
* AI-Browse Service
* Handles all AI-Browse related API calls
*/
export class AiBrowseService {
constructor(client) {
this.client = client;
}
/**
* Generate schema for browsing (POST /browse/schema)
*/
async generateSchema(options) {
return await this.client.post('/browser-agent/schema', options);
}
/**
* Submit browsing request (POST /browse)
*/
async submitBrowseRequest(options) {
const payload = {
url: options.url,
output_format: options.output_format || "markdown",
auxiliary_prompt: options.browse_prompt
};
// Only include openapi_schema if output_format is json
if (options.output_format === "json" && options.openapi_schema) {
payload.openapi_schema = options.openapi_schema;
}
return await this.client.post('/browser-agent/run', payload);
}
/**
* Get browsing run status (GET /browse/run)
*/
async getBrowseRunSteps(runId) {
if (!runId) {
throw new Error('run_id is required');
}
const params = new URLSearchParams();
params.append('run_id', runId);
const url = `/browser-agent/run/steps?${params.toString()}`;
return await this.client.get(url);
}
/**
* Get browsing run data/results (GET /browse/run/data)
*/
async getBrowseRunData(runId) {
if (!runId) {
throw new Error('run_id is required');
}
const params = new URLSearchParams();
params.append('run_id', runId);
const url = `/browser-agent/run/data?${params.toString()}`;
return await this.client.get(url);
}
/**
* Synchronous browsing (wait for results)
*/
async browse(options, timeout = 120000, pollInterval = 5000) {
const submitResult = await this.submitBrowseRequest(options);
const runId = submitResult.run_id || submitResult.id;
if (!runId) {
throw new Error('No run ID returned from browse request');
}
const startTime = Date.now();
while (Date.now() - startTime < timeout) {
const runStatus = await this.getBrowseRunSteps(runId);
const run_status = runStatus.run.status;
console.log('Run status:', run_status);
if (run_status === 'completed' || run_status === 'success') {
return await this.getBrowseRunData(runId);
}
else if (run_status === 'failed' || run_status === 'error') {
throw new Error(`Browsing failed: ${runStatus.run.error || runStatus.run.message || 'Unknown error'}`);
}
await new Promise(resolve => setTimeout(resolve, pollInterval));
}
throw new Error(`Browsing timeout after ${timeout}ms`);
}
/**
* Complete workflow with auto-schema and sync results
*/
async browseWithAutoSchema(options, timeout = 60000) {
// Generate schema first
const schemaResult = await this.generateSchema({
user_prompt: options.parse_prompt
});
// Then perform synchronous browsing
return await this.browse({
url: options.url,
browse_prompt: options.browse_prompt || "",
output_format: options.output_format || "markdown",
openapi_schema: schemaResult.openapi_schema,
render_html: options.render_html || false
}, timeout);
}
}
//# sourceMappingURL=aiBrowse.js.map