mindee
Version:
Mindee Client Library for Node.js
91 lines (90 loc) • 3.76 kB
JavaScript
var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
};
var _WorkflowEndpoint_instances, _WorkflowEndpoint_workflowReqPost;
import { URLSearchParams } from "url";
import { FormData } from "undici";
import { LocalInputSource } from "../../input/index.js";
import { cutDocPages, sendRequestAndReadResponse, } from "../../http/apiCore.js";
import { handleError } from "./errors.js";
import { isValidSyncResponse } from "./responseValidation.js";
/**
* Endpoint for a workflow.
*/
export class WorkflowEndpoint {
constructor(settings, workflowId) {
_WorkflowEndpoint_instances.add(this);
this.settings = settings;
this.urlRoot = `/v1/workflows/${workflowId}/executions`;
}
/**
* Sends a document to a workflow execution.
* Throws an error if the server's response contains one.
* @param {WorkflowParams} params parameters relating to prediction options.
* @category Synchronous
* @returns a `Promise` containing parsing results.
*/
async executeWorkflow(params) {
await params.inputDoc.init();
if (params.pageOptions !== undefined) {
await cutDocPages(params.inputDoc, params.pageOptions);
}
const response = await __classPrivateFieldGet(this, _WorkflowEndpoint_instances, "m", _WorkflowEndpoint_workflowReqPost).call(this, params);
if (!isValidSyncResponse(response)) {
handleError(this.urlRoot, response, response.messageObj?.statusMessage);
}
return response;
}
/**
* Send a file to a prediction API.
* @param input
* @param alias
* @param priority
* @param fullText
* @param publicUrl
* @param rag
*/
async sendFileForPrediction(input, alias = null, priority = null, fullText = false, publicUrl = null, rag = null) {
const searchParams = new URLSearchParams();
if (fullText) {
searchParams.set("full_text_ocr", "true");
}
if (rag) {
searchParams.set("rag", "true");
}
const form = new FormData();
if (input instanceof LocalInputSource && input.fileObject instanceof Buffer) {
form.set("document", new Blob([input.fileObject]), input.filename);
}
else {
form.set("document", input.fileObject);
}
if (alias) {
form.set("alias", alias);
}
if (publicUrl) {
form.set("public_url", publicUrl);
}
if (priority) {
form.set("priority", priority.toString());
}
let path = this.urlRoot;
if (searchParams.toString().length > 0) {
path += `?${searchParams}`;
}
const options = {
method: "POST",
headers: this.settings.baseHeaders,
hostname: this.settings.hostname,
path: path,
timeoutSecs: this.settings.timeoutSecs,
body: form,
};
return await sendRequestAndReadResponse(this.settings.dispatcher, options);
}
}
_WorkflowEndpoint_instances = new WeakSet(), _WorkflowEndpoint_workflowReqPost = function _WorkflowEndpoint_workflowReqPost(params) {
return this.sendFileForPrediction(params.inputDoc, params.alias, params.priority, params.fullText, params.publicUrl, params.rag);
};