Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
75 changes: 75 additions & 0 deletions src/client/maxun-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
import axios, { AxiosInstance, AxiosError } from 'axios';
import http from 'http';
import https from 'https';
import FormData from 'form-data';
import * as fs from 'fs';
import {
Config,
RobotData,
Expand Down Expand Up @@ -287,6 +289,79 @@ export class Client {
return response.data.data;
}

/**
* Create a document-extraction robot from a PDF file path or Buffer.
*/
async createDocumentExtractRobot(
file: string | Buffer,
prompt: string,
options?: { robotName?: string; ollamaModel?: string; fileName?: string }
): Promise<{ robot: RobotData; extractionSchema: Record<string, any> }> {
const form = new FormData();

if (typeof file === 'string') {
form.append('file', fs.createReadStream(file), options?.fileName || require('path').basename(file));
} else {
form.append('file', file, { filename: options?.fileName || 'document.pdf', contentType: 'application/pdf' });
}

form.append('prompt', prompt);
if (options?.robotName) form.append('robotName', options.robotName);
if (options?.ollamaModel) form.append('ollamaModel', options.ollamaModel);

const response = await this.axios.post<any>(
'/robots/document',
form,
{ headers: form.getHeaders(), timeout: 120000 }
);

const data = response.data;
if (!data?.data && !data?.robot) {
throw new MaxunError('Failed to create document robot');
}

return {
robot: data.data || data.robot,
extractionSchema: data.extractionSchema || {},
};
}

/**
* Create a document-parse robot from a PDF file path or Buffer.
*/
async createDocumentParseRobot(
file: string | Buffer,
outputFormats: ('markdown' | 'html' | 'links')[],
options?: { robotName?: string; fileName?: string }
): Promise<{ robot: RobotData; parsedOutput: Record<string, any> }> {
const form = new FormData();

if (typeof file === 'string') {
form.append('file', fs.createReadStream(file), options?.fileName || require('path').basename(file));
} else {
form.append('file', file, { filename: options?.fileName || 'document.pdf', contentType: 'application/pdf' });
}

if (options?.robotName) form.append('robotName', options.robotName);
outputFormats.forEach((f) => form.append('outputFormats[]', f));

const response = await this.axios.post<any>(
'/robots/document-parse',
form,
{ headers: form.getHeaders(), timeout: 120000 }
);

const data = response.data;
if (!data?.data && !data?.robot) {
throw new MaxunError('Failed to create document-parse robot');
}

return {
robot: data.data || data.robot,
parsedOutput: data.parsedOutput || {},
};
}

/**
* Create a crawl robot to discover and scrape multiple pages
*/
Expand Down