diff --git a/docs/code_samples/default_v2.txt b/docs/code_samples/v2_default.txt similarity index 88% rename from docs/code_samples/default_v2.txt rename to docs/code_samples/v2_default.txt index d909f3a0..d7f41d0e 100644 --- a/docs/code_samples/default_v2.txt +++ b/docs/code_samples/v2_default.txt @@ -12,7 +12,7 @@ const mindeeClient = new mindee.Client( ); // Set inference parameters -const inferenceParams = { +const extractionParams = { modelId: modelId, // Options: set to `true` or `false` to override defaults @@ -32,10 +32,10 @@ const inferenceParams = { const inputSource = new mindee.PathInput({ inputPath: filePath }); // Send for processing -const response = mindeeClient.enqueueAndGetInference( - mindee.v2.ExtractionInference, +const response = mindeeClient.enqueueAndGetResult( + mindee.v2.product.Extraction, inputSource, - inferenceParams + extractionParams ); // Handle the response Promise diff --git a/src/index.ts b/src/index.ts index cbcf42fd..f39c7949 100644 --- a/src/index.ts +++ b/src/index.ts @@ -18,20 +18,8 @@ export * as v1 from "./v1/index.js"; export * as v2 from "./v2/index.js"; export { Client, - ExtractionParameters, - DataSchema, InferenceFile, InferenceModel, - ClassificationInference, - ClassificationResponse, - CropInference, - CropResponse, - ExtractionInference, - ExtractionResponse, - OcrInference, - OcrResponse, - SplitInference, - SplitResponse, JobResponse, ErrorResponse, } from "./v2/index.js"; diff --git a/src/input/localInputSource.ts b/src/input/localInputSource.ts index cf18216a..2a0e0dea 100644 --- a/src/input/localInputSource.ts +++ b/src/input/localInputSource.ts @@ -54,7 +54,7 @@ export abstract class LocalInputSource extends InputSource { ); } this.inputType = inputType; - logger.debug(`New local input source of type: ${inputType}`); + logger.debug(`Initialized local input source of type: ${inputType}`); } protected async checkMimetype(): Promise { diff --git a/src/input/urlInput.ts b/src/input/urlInput.ts index 20e2a3d9..1b33ed0a 100644 --- a/src/input/urlInput.ts +++ b/src/input/urlInput.ts @@ -15,6 +15,7 @@ export class UrlInput extends InputSource { super(); this.url = url; this.dispatcher = dispatcher ?? getGlobalDispatcher(); + logger.debug("Initialized URL input source."); } async init() { diff --git a/src/v2/cli.ts b/src/v2/cli.ts index af9c5b8e..c3c34c41 100644 --- a/src/v2/cli.ts +++ b/src/v2/cli.ts @@ -2,15 +2,15 @@ import { Command, OptionValues } from "commander"; import { Client } from "./client.js"; import { PathInput } from "../input/index.js"; import * as console from "console"; +import { BaseInference } from "@/v2/parsing/inference/index.js"; +import { BaseProduct } from "@/v2/product/baseProduct.js"; import { - BaseInference, - ClassificationInference, - CropInference, - ExtractionInference, - OcrInference, - SplitInference, - InferenceResponseConstructor, -} from "@/v2/parsing/inference/index.js"; + Classification, + Crop, + Extraction, + Ocr, + Split, +} from "@/v2/product/index.js"; const program = new Command(); @@ -27,14 +27,14 @@ function initClient(options: OptionValues): Client { } async function enqueueAndGetInference( - responseType: InferenceResponseConstructor, + product: typeof BaseProduct, inputPath: string, options: OptionValues ): Promise { const mindeeClient = initClient(options); const inputSource = new PathInput({ inputPath: inputPath }); - const response = await mindeeClient.enqueueAndGetInference( - responseType, + const response = await mindeeClient.enqueueAndGetResult( + product, inputSource, { modelId: options.model, @@ -78,11 +78,11 @@ export function cli() { .option("-k, --api-key ", "your Mindee API key"); const inferenceTypes = [ - { name: "extract", description: "Extract data from a document.", responseType: ExtractionInference }, - { name: "crop", description: "Crop a document.", responseType: CropInference }, - { name: "split", description: "Split a document into pages.", responseType: SplitInference }, - { name: "ocr", description: "Read text from a document.", responseType: OcrInference }, - { name: "classify", description: "Classify a document.", responseType: ClassificationInference }, + { name: "extract", description: "Extract data from a document.", product: Extraction }, + { name: "crop", description: "Crop a document.", product: Crop }, + { name: "split", description: "Split a document into pages.", product: Split }, + { name: "ocr", description: "Read text from a document.", product: Ocr }, + { name: "classify", description: "Classify a document.", product: Classification }, ]; for (const inference of inferenceTypes) { @@ -96,7 +96,7 @@ export function cli() { options: OptionValues, ) { const allOptions = { ...program.opts(), ...options }; - return enqueueAndGetInference(inference.responseType, inputPath, allOptions); + return enqueueAndGetInference(inference.product, inputPath, allOptions); }); } diff --git a/src/v2/client.ts b/src/v2/client.ts index aebad4e6..01accf0a 100644 --- a/src/v2/client.ts +++ b/src/v2/client.ts @@ -4,20 +4,11 @@ import { InputSource } from "@/input/index.js"; import { MindeeError } from "@/errors/index.js"; import { errorHandler } from "@/errors/handler.js"; import { LOG_LEVELS, logger } from "@/logger.js"; -import { - ErrorResponse, - JobResponse, - InferenceResponseConstructor, - BaseInference, - BaseInferenceResponse, - CropInference, - OcrInference, - SplitInference, - ExtractionInference, -} from "./parsing/index.js"; +import { ErrorResponse, JobResponse } from "./parsing/index.js"; import { MindeeApiV2 } from "./http/mindeeApiV2.js"; import { MindeeHttpErrorV2 } from "./http/errors.js"; -import { ExtractionParameters, UtilityParameters, ValidatedPollingOptions } from "./client/index.js"; +import { ValidatedPollingOptions } from "./client/index.js"; +import { BaseProduct } from "@/v2/product/baseProduct.js"; /** * Options for the V2 Mindee Client. @@ -39,12 +30,6 @@ export interface ClientOptions { dispatcher?: Dispatcher; } -type InferenceParameters = - | UtilityParameters - | ExtractionParameters - | ConstructorParameters[0] - | ConstructorParameters[0]; - /** * Mindee Client V2 class that centralizes most basic operations. * @@ -73,41 +58,20 @@ export class Client { logger.debug("Client V2 Initialized"); } - #getParametersClassFromInference( - inferenceClass: InferenceResponseConstructor, - params: any, - ): ExtractionParameters | UtilityParameters { - if (params instanceof ExtractionParameters || params instanceof UtilityParameters) { - return params; - } - switch (inferenceClass as any) { - case CropInference: - return new UtilityParameters(params); - case OcrInference: - return new UtilityParameters(params); - case SplitInference: - return new UtilityParameters(params); - case ExtractionInference: - return new ExtractionParameters(params); - default: - throw new Error("Unsupported inference class."); - } - } - - async enqueueInference( - responseType: InferenceResponseConstructor, + async enqueue

( + product: P, inputSource: InputSource, - params: InferenceParameters, + params: InstanceType | ConstructorParameters[0], ): Promise { if (inputSource === undefined) { throw new MindeeError("An input document is required."); } - const paramsInstance = this.#getParametersClassFromInference( - responseType, params - ); + const paramsInstance = params instanceof product.parameters + ? params + : new product.parameters(params); await inputSource.init(); - const jobResponse = await this.mindeeApi.reqPostInferenceEnqueue( - responseType, inputSource, paramsInstance + const jobResponse = await this.mindeeApi.reqPostProductEnqueue( + product, inputSource, paramsInstance ); if (jobResponse.job.id === undefined || jobResponse.job.id.length === 0) { logger.error(`Failed enqueueing:\n${jobResponse.getRawHttp()}`); @@ -120,26 +84,26 @@ export class Client { } /** - * Retrieves an inference. + * Retrieves the result of a previously enqueued request. * - * @param responseType class of the inference to retrieve. + * @param product the product to retrieve. * @param inferenceId id of the queue to poll. * @typeParam T an extension of an `Inference`. Can be omitted as it will be inferred from the `productClass`. * @category Asynchronous * @returns a `Promise` containing the inference. */ - async getInference( - responseType: InferenceResponseConstructor, + async getResult

( + product: P, inferenceId: string - ): Promise> { + ): Promise> { logger.debug( - `Attempting to get inference with ID: ${inferenceId} using response type: ${responseType.name}` + `Attempting to get inference with ID: ${inferenceId} using response type: ${product.name}` ); - return await this.mindeeApi.reqGetInference(responseType, inferenceId); + return await this.mindeeApi.reqGetResult(product, inferenceId); } /** - * Get the status of an inference that was previously enqueued. + * Get the processing status of a previously enqueued request. * Can be used for polling. * * @param jobId id of the queue to poll. @@ -153,10 +117,10 @@ export class Client { } /** - * Send a document to an endpoint and poll the server until the result is sent or + * Enqueue a request and poll the server until the result is sent or * until the maximum number of tries is reached. * - * @param responseType class of the inference to retrieve. + * @param product the product to retrieve. * @param inputSource file or URL to parse. * @param params parameters relating to prediction options. * @@ -164,22 +128,20 @@ export class Client { * @category Synchronous * @returns a `Promise` containing parsing results. */ - async enqueueAndGetInference( - responseType: InferenceResponseConstructor, + async enqueueAndGetResult

( + product: P, inputSource: InputSource, - params: InferenceParameters - ): Promise> { - const paramsInstance = this.#getParametersClassFromInference( - responseType, params - ); + params: InstanceType | ConstructorParameters[0], + ): Promise> { + const paramsInstance = new product.parameters(params); const pollingOptions = paramsInstance.getValidatedPollingOptions(); - const jobResponse: JobResponse = await this.enqueueInference( - responseType, inputSource, paramsInstance + const jobResponse: JobResponse = await this.enqueue( + product, inputSource, paramsInstance ); - return await this.pollForInference( - responseType, pollingOptions, jobResponse.job.id + return await this.pollForResult( + product, pollingOptions, jobResponse.job.id ); } @@ -188,11 +150,11 @@ export class Client { * until the maximum number of tries is reached. * @protected */ - protected async pollForInference( - responseType: InferenceResponseConstructor, + protected async pollForResult

( + product: typeof BaseProduct, pollingOptions: ValidatedPollingOptions, queueId: string, - ): Promise> { + ): Promise> { logger.debug( `Waiting ${pollingOptions.initialDelaySec} seconds before polling.` ); @@ -220,7 +182,7 @@ export class Client { break; } if (pollResults.job.status === "Processed") { - return this.getInference(responseType, pollResults.job.id); + return this.getResult(product, pollResults.job.id); } await setTimeout( pollingOptions.delaySec * 1000, diff --git a/src/v2/client/index.ts b/src/v2/client/index.ts index d64ca2b3..a625efc0 100644 --- a/src/v2/client/index.ts +++ b/src/v2/client/index.ts @@ -1,4 +1,2 @@ -export { DataSchema } from "./dataSchema.js"; export type { PollingOptions, ValidatedPollingOptions } from "./pollingOptions.js"; -export { ExtractionParameters } from "./extractionParameters.js"; -export { UtilityParameters } from "./utilityParameters.js"; +export { BaseParameters } from "./baseParameters.js"; diff --git a/src/v2/http/mindeeApiV2.ts b/src/v2/http/mindeeApiV2.ts index 72f5f509..b731a1fc 100644 --- a/src/v2/http/mindeeApiV2.ts +++ b/src/v2/http/mindeeApiV2.ts @@ -1,29 +1,18 @@ import { ApiSettingsV2 } from "./apiSettingsV2.js"; import { Dispatcher } from "undici"; -import { ExtractionParameters, UtilityParameters } from "@/v2/client/index.js"; +import { BaseParameters } from "@/v2/client/index.js"; import { BaseResponse, ErrorResponse, ResponseConstructor, - InferenceResponseConstructor, JobResponse, - CropResponse, - OcrResponse, - SplitResponse, - ExtractionResponse, - BaseInference, ExtractionInference, } from "@/v2/parsing/index.js"; import { sendRequestAndReadResponse, BaseHttpResponse } from "@/http/apiCore.js"; import { InputSource, LocalInputSource, UrlInput } from "@/input/index.js"; import { MindeeDeserializationError } from "@/errors/index.js"; import { MindeeHttpErrorV2 } from "./errors.js"; import { logger } from "@/logger.js"; -import { - BaseInferenceResponse, - CropInference, - OcrInference, - SplitInference -} from "@/v2/parsing/inference/index.js"; +import { BaseProduct } from "@/v2/product/baseProduct.js"; export class MindeeApiV2 { @@ -33,58 +22,23 @@ export class MindeeApiV2 { this.settings = new ApiSettingsV2({ dispatcher: dispatcher, apiKey: apiKey }); } - #getSlugFromInference( - responseClass: InferenceResponseConstructor - ): string { - switch (responseClass as any) { - case CropInference: - return "utilities/crop"; - case OcrInference: - return "utilities/ocr"; - case SplitInference: - return "utilities/split"; - case ExtractionInference: - return "inferences"; - default: - throw new Error("Unsupported response class."); - } - } - - #getResponseClassFromInference( - inferenceClass: InferenceResponseConstructor - ): ResponseConstructor> { - switch (inferenceClass as any) { - case CropInference: - return CropResponse as any; - case OcrInference: - return OcrResponse as any; - case SplitInference: - return SplitResponse as any; - case ExtractionInference: - return ExtractionResponse as any; - default: - throw new Error("Unsupported inference class."); - } - } - /** * Sends a file to the extraction inference queue. - * @param responseClass Class of the inference to enqueue. + * @param product product to enqueue. * @param inputSource Local file loaded as an input. * @param params {ExtractionParameters} parameters relating to the enqueueing options. * @category V2 * @throws Error if the server's response contains one. * @returns a `Promise` containing a job response. */ - async reqPostInferenceEnqueue( - responseClass: InferenceResponseConstructor, + async reqPostProductEnqueue( + product: typeof BaseProduct, inputSource: InputSource, - params: ExtractionParameters | UtilityParameters + params: BaseParameters ): Promise { await inputSource.init(); - const slug = this.#getSlugFromInference(responseClass); - const result: BaseHttpResponse = await this.#inferenceEnqueuePost( - inputSource, slug, params + const result: BaseHttpResponse = await this.#productEnqueuePost( + product, inputSource, params ); if (result.data.error !== undefined) { throw new MindeeHttpErrorV2(result.data.error); @@ -95,19 +49,19 @@ export class MindeeApiV2 { /** * Requests the job of a queued document from the API. * Throws an error if the server's response contains one. - * @param responseClass + * @param product * @param inferenceId The document's ID in the queue. * @category Asynchronous * @returns a `Promise` containing either the parsed result, or information on the queue. */ - async reqGetInference( - responseClass: InferenceResponseConstructor, + async reqGetResult

( + product: P, inferenceId: string, - ): Promise> { - const slug = this.#getSlugFromInference(responseClass); - const queueResponse: BaseHttpResponse = await this.#inferenceResultReqGet(inferenceId, slug); - const actualResponseClass = this.#getResponseClassFromInference(responseClass); - return this.#processResponse(queueResponse, actualResponseClass) as BaseInferenceResponse; + ): Promise> { + const queueResponse: BaseHttpResponse = await this.#inferenceResultReqGet( + inferenceId, product.getResultSlug + ); + return this.#processResponse(queueResponse, product.response) as InstanceType; } /** @@ -152,14 +106,14 @@ export class MindeeApiV2 { /** * Sends a document to the inference queue. * + * @param product Product to enqueue. * @param inputSource Local or remote file as an input. - * @param slug Slug of the inference to enqueue. * @param params {ExtractionParameters} parameters relating to the enqueueing options. */ - async #inferenceEnqueuePost( + async #productEnqueuePost( + product: typeof BaseProduct, inputSource: InputSource, - slug: string, - params: ExtractionParameters | UtilityParameters + params: BaseParameters ): Promise { const form = params.getFormData(); if (inputSource instanceof LocalInputSource) { @@ -167,7 +121,7 @@ export class MindeeApiV2 { } else { form.set("url", (inputSource as UrlInput).url); } - const path = `/v2/${slug}/enqueue`; + const path = `/v2/${product.enqueueSlug}/enqueue`; const options = { method: "POST", headers: this.settings.baseHeaders, diff --git a/src/v2/index.ts b/src/v2/index.ts index b8bc8608..384491f1 100644 --- a/src/v2/index.ts +++ b/src/v2/index.ts @@ -1,22 +1,12 @@ export * as http from "./http/index.js"; export * as parsing from "./parsing/index.js"; +export * as product from "./product/index.js"; export { LocalResponse } from "./parsing/localResponse.js"; export { Client } from "./client.js"; export { InferenceFile, InferenceModel, - ClassificationInference, - ClassificationResponse, - CropInference, - CropResponse, - ExtractionInference, - ExtractionResponse, - OcrInference, - OcrResponse, - SplitInference, - SplitResponse, JobResponse, ErrorResponse, } from "./parsing/index.js"; -export { ExtractionParameters, DataSchema } from "./client/index.js"; export type { PollingOptions } from "./client/index.js"; diff --git a/src/v2/parsing/baseResponse.ts b/src/v2/parsing/baseResponse.ts index b2068107..994e3a2f 100644 --- a/src/v2/parsing/baseResponse.ts +++ b/src/v2/parsing/baseResponse.ts @@ -1,7 +1,6 @@ import { StringDict } from "@/parsing/stringDict.js"; import { logger } from "@/logger.js"; - export abstract class BaseResponse { /** * Raw text representation of the API's response. diff --git a/src/v2/parsing/error/errorResponse.ts b/src/v2/parsing/error/errorResponse.ts index f32ab273..a2df695b 100644 --- a/src/v2/parsing/error/errorResponse.ts +++ b/src/v2/parsing/error/errorResponse.ts @@ -1,11 +1,12 @@ import { StringDict } from "@/parsing/stringDict.js"; import { ErrorItem } from "./errorItem.js"; import { ErrorDetails } from "./errorDetails.js"; +import { BaseResponse } from "@/v2/parsing/baseResponse.js"; /** * Error response detailing a problem. The format adheres to RFC 9457. */ -export class ErrorResponse implements ErrorDetails { +export class ErrorResponse extends BaseResponse implements ErrorDetails { status: number; detail: string; title: string; @@ -16,6 +17,7 @@ export class ErrorResponse implements ErrorDetails { * @param serverResponse JSON response from the server. */ constructor(serverResponse: StringDict) { + super(serverResponse); this.status = serverResponse["status"]; this.detail = serverResponse["detail"]; this.title = serverResponse["title"]; diff --git a/src/v2/parsing/index.ts b/src/v2/parsing/index.ts index ffb759ef..b5260f9a 100644 --- a/src/v2/parsing/index.ts +++ b/src/v2/parsing/index.ts @@ -10,23 +10,10 @@ export { } from "./job/index.js"; export { BaseInference, - BaseInferenceResponse, InferenceFile, InferenceModel, - ExtractionInference, - ExtractionActiveOptions, - ExtractionResponse, - ExtractionResult, - ClassificationResponse, - ClassificationInference, - CropResponse, - CropInference, - OcrResponse, - OcrInference, - SplitResponse, - SplitInference, } from "./inference/index.js"; export { LocalResponse } from "./localResponse.js"; +export { BaseResponse } from "./baseResponse.js"; +export type { ResponseConstructor } from "./baseResponse.js"; export { RawText, RagMetadata } from "./inference/field/index.js"; -export type { ResponseConstructor, BaseResponse } from "./baseResponse.js"; -export type { InferenceResponseConstructor } from "./inference/index.js"; diff --git a/src/v2/parsing/inference/baseInference.ts b/src/v2/parsing/inference/baseInference.ts index ab44a6ca..6552a368 100644 --- a/src/v2/parsing/inference/baseInference.ts +++ b/src/v2/parsing/inference/baseInference.ts @@ -1,4 +1,4 @@ -import { InferenceModel } from "@/v2/parsing/inference/inferenceModel.js"; +import { InferenceModel } from "./inferenceModel.js"; import { InferenceFile } from "@/v2/index.js"; import { StringDict } from "@/parsing/index.js"; diff --git a/src/v2/parsing/inference/baseInferenceResponse.ts b/src/v2/parsing/inference/baseInferenceResponse.ts deleted file mode 100644 index 766fdd2c..00000000 --- a/src/v2/parsing/inference/baseInferenceResponse.ts +++ /dev/null @@ -1,22 +0,0 @@ -import { StringDict } from "@/parsing/stringDict.js"; -import { BaseResponse } from "@/v2/parsing/baseResponse.js"; -import { BaseInference } from "./baseInference.js"; - -export abstract class BaseInferenceResponse extends BaseResponse { - /** - * The inference result of the request. - */ - public inference: T; - - /** - * @param serverResponse JSON response from the server. - */ - constructor(serverResponse: StringDict) { - super(serverResponse); - this.inference = this.setInferenceType(serverResponse["inference"]); - } - - public abstract setInferenceType(inferenceResponse: StringDict): T; -} - -export type InferenceResponseConstructor = new (serverResponse: StringDict) => T; diff --git a/src/v2/parsing/inference/classification/classificationResponse.ts b/src/v2/parsing/inference/classification/classificationResponse.ts deleted file mode 100644 index e348e76e..00000000 --- a/src/v2/parsing/inference/classification/classificationResponse.ts +++ /dev/null @@ -1,10 +0,0 @@ -import { StringDict } from "@/parsing/stringDict.js"; -import { BaseInferenceResponse } from "@/v2/parsing/inference/index.js"; -import { ClassificationInference } from "./classificationInference.js"; - -export class ClassificationResponse extends BaseInferenceResponse { - - setInferenceType(inferenceResponse: StringDict): ClassificationInference { - return new ClassificationInference(inferenceResponse); - } -} diff --git a/src/v2/parsing/inference/crop/cropResponse.ts b/src/v2/parsing/inference/crop/cropResponse.ts deleted file mode 100644 index 3f8f1941..00000000 --- a/src/v2/parsing/inference/crop/cropResponse.ts +++ /dev/null @@ -1,10 +0,0 @@ -import { StringDict } from "@/parsing/stringDict.js"; -import { CropInference } from "./cropInference.js"; -import { BaseInferenceResponse } from "@/v2/parsing/inference/baseInferenceResponse.js"; - -export class CropResponse extends BaseInferenceResponse { - - setInferenceType(inferenceResponse: StringDict): CropInference { - return new CropInference(inferenceResponse); - } -} diff --git a/src/v2/parsing/inference/crop/cropResult.ts b/src/v2/parsing/inference/crop/cropResult.ts deleted file mode 100644 index b2bcb51a..00000000 --- a/src/v2/parsing/inference/crop/cropResult.ts +++ /dev/null @@ -1,17 +0,0 @@ -import { StringDict } from "@/parsing/stringDict.js"; -import { CropItem } from "@/v2/parsing/inference/crop/cropItem.js"; - -export class CropResult { - /** - * Fields contained in the inference. - */ - public crop: CropItem[] = []; - - constructor(serverResponse: StringDict) { - this.crop = serverResponse["crop"].map((cropItem: StringDict) => new CropItem(cropItem)); - } - - toString(): string { - return `Crop\n====\n${this.crop}`; - } -} diff --git a/src/v2/parsing/inference/extraction/extractionResponse.ts b/src/v2/parsing/inference/extraction/extractionResponse.ts deleted file mode 100644 index e61e836c..00000000 --- a/src/v2/parsing/inference/extraction/extractionResponse.ts +++ /dev/null @@ -1,10 +0,0 @@ -import { ExtractionInference } from "./extractionInference.js"; -import { StringDict } from "@/parsing/stringDict.js"; -import { BaseInferenceResponse } from "@/v2/parsing/inference/baseInferenceResponse.js"; - -export class ExtractionResponse extends BaseInferenceResponse { - - setInferenceType(inferenceResponse: StringDict): ExtractionInference { - return new ExtractionInference(inferenceResponse); - } -} diff --git a/src/v2/parsing/inference/index.ts b/src/v2/parsing/inference/index.ts index ee712816..df0e9b36 100644 --- a/src/v2/parsing/inference/index.ts +++ b/src/v2/parsing/inference/index.ts @@ -2,33 +2,6 @@ export { InferenceFile } from "./inferenceFile.js"; export { InferenceModel } from "./inferenceModel.js"; export { BaseInference } from "./baseInference.js"; -export { BaseInferenceResponse } from "./baseInferenceResponse.js"; -export type { InferenceResponseConstructor } from "./baseInferenceResponse.js"; // Fields export * as field from "./field/index.js"; - -// Extraction -export { ExtractionInference } from "./extraction/extractionInference.js"; -export { ExtractionActiveOptions } from "./extraction/extractionActiveOptions.js"; -export { ExtractionResponse } from "./extraction/extractionResponse.js"; -export { ExtractionResult } from "./extraction/extractionResult.js"; - -// Classification -export { ClassificationResponse } from "./classification/classificationResponse.js"; -export { ClassificationInference } from "./classification/classificationInference.js"; - -// Crop -export { CropInference } from "./crop/cropInference.js"; -export { CropItem } from "./crop/cropItem.js"; -export { CropResponse } from "./crop/cropResponse.js"; -export { CropResult } from "./crop/cropResult.js"; - -// OCR -export { OcrResponse } from "./ocr/ocrResponse.js"; -export { OcrInference } from "./ocr/ocrInference.js"; - -// Split -export { SplitResponse } from "./split/splitResponse.js"; -export { SplitInference } from "./split/splitInference.js"; - diff --git a/src/v2/parsing/inference/ocr/ocrResponse.ts b/src/v2/parsing/inference/ocr/ocrResponse.ts deleted file mode 100644 index f0c8e452..00000000 --- a/src/v2/parsing/inference/ocr/ocrResponse.ts +++ /dev/null @@ -1,10 +0,0 @@ -import { StringDict } from "@/parsing/stringDict.js"; -import { BaseInferenceResponse } from "@/v2/parsing/inference/baseInferenceResponse.js"; -import { OcrInference } from "./ocrInference.js"; - -export class OcrResponse extends BaseInferenceResponse { - - setInferenceType(inferenceResponse: StringDict): OcrInference { - return new OcrInference(inferenceResponse); - } -} diff --git a/src/v2/parsing/inference/split/splitResponse.ts b/src/v2/parsing/inference/split/splitResponse.ts deleted file mode 100644 index 3336d93a..00000000 --- a/src/v2/parsing/inference/split/splitResponse.ts +++ /dev/null @@ -1,10 +0,0 @@ -import { StringDict } from "@/parsing/stringDict.js"; -import { BaseInferenceResponse } from "@/v2/parsing/inference/baseInferenceResponse.js"; -import { SplitInference } from "./splitInference.js"; - -export class SplitResponse extends BaseInferenceResponse { - - setInferenceType(inferenceResponse: StringDict): SplitInference { - return new SplitInference(inferenceResponse); - } -} diff --git a/src/v2/product/baseProduct.ts b/src/v2/product/baseProduct.ts new file mode 100644 index 00000000..1ae2a68e --- /dev/null +++ b/src/v2/product/baseProduct.ts @@ -0,0 +1,19 @@ +import { BaseParameters } from "@/v2/client/index.js"; +import { ResponseConstructor } from "@/v2/parsing/index.js"; + +export abstract class BaseProduct { + static get parameters(): new (...args: any[]) => BaseParameters { + throw new Error("Must define static parameters property"); + } + static get response(): ResponseConstructor { + throw new Error("Must define static response property"); + } + + static get enqueueSlug(): string { + throw new Error("Must define static enqueueSlug property"); + } + + static get getResultSlug(): string { + throw new Error("Must define static getResultSlug property"); + } +} diff --git a/src/v2/product/classification/classification.ts b/src/v2/product/classification/classification.ts new file mode 100644 index 00000000..65f8c85b --- /dev/null +++ b/src/v2/product/classification/classification.ts @@ -0,0 +1,18 @@ +import { ClassificationResponse } from "./classificationResponse.js"; +import { ClassificationParameters } from "./classificationParameters.js"; +import { BaseProduct } from "@/v2/product/baseProduct.js"; + +export class Classification extends BaseProduct { + static get parameters() { + return ClassificationParameters; + } + static get response() { + return ClassificationResponse; + } + static get enqueueSlug() { + return "utilities/classification"; + } + static get getResultSlug() { + return "utilities/classification"; + } +} diff --git a/src/v2/parsing/inference/classification/classificationInference.ts b/src/v2/product/classification/classificationInference.ts similarity index 100% rename from src/v2/parsing/inference/classification/classificationInference.ts rename to src/v2/product/classification/classificationInference.ts diff --git a/src/v2/product/classification/classificationParameters.ts b/src/v2/product/classification/classificationParameters.ts new file mode 100644 index 00000000..41021cc3 --- /dev/null +++ b/src/v2/product/classification/classificationParameters.ts @@ -0,0 +1,26 @@ +import { BaseParameters, BaseParametersConstructor } from "@/v2/client/baseParameters.js"; +import { logger } from "@/logger.js"; + +/** + * Parameters accepted by the asynchronous **inference** v2 endpoint. + * + * All fields are optional except `modelId`. + * + * @category ClientV2 + * @example + * const params = { + * modelId: "YOUR_MODEL_ID", + * alias: "YOUR_ALIAS", + * webhookIds: ["YOUR_WEBHOOK_ID_1", "YOUR_WEBHOOK_ID_2"], + * pollingOptions: { + * initialDelaySec: 2, + * delaySec: 1.5, + * } + * }; + */ +export class ClassificationParameters extends BaseParameters { + constructor(params: BaseParametersConstructor & {}) { + super({ ...params }); + logger.debug("Classification parameters initialized."); + } +} diff --git a/src/v2/product/classification/classificationResponse.ts b/src/v2/product/classification/classificationResponse.ts new file mode 100644 index 00000000..f1195ea2 --- /dev/null +++ b/src/v2/product/classification/classificationResponse.ts @@ -0,0 +1,18 @@ +import { StringDict } from "@/parsing/stringDict.js"; +import { ClassificationInference } from "./classificationInference.js"; +import { BaseResponse } from "@/v2/parsing/index.js"; + +export class ClassificationResponse extends BaseResponse { + /** + * The inference result for a classification utility request. + */ + inference: ClassificationInference; + + /** + * @param serverResponse JSON response from the server. + */ + constructor(serverResponse: StringDict) { + super(serverResponse); + this.inference = new ClassificationInference(serverResponse["inference"]); + } +} diff --git a/src/v2/product/classification/index.ts b/src/v2/product/classification/index.ts new file mode 100644 index 00000000..5c34b115 --- /dev/null +++ b/src/v2/product/classification/index.ts @@ -0,0 +1,4 @@ +export { Classification } from "./classification.js"; +export { ClassificationParameters } from "./classificationParameters.js"; +export { ClassificationResponse } from "./classificationResponse.js"; +export { ClassificationInference } from "./classificationInference.js"; diff --git a/src/v2/product/crop/crop.ts b/src/v2/product/crop/crop.ts new file mode 100644 index 00000000..63f8a5d6 --- /dev/null +++ b/src/v2/product/crop/crop.ts @@ -0,0 +1,18 @@ +import { CropResponse } from "./cropResponse.js"; +import { CropParameters } from "./cropParameters.js"; +import { BaseProduct } from "@/v2/product/baseProduct.js"; + +export class Crop extends BaseProduct { + static get parameters() { + return CropParameters; + } + static get response() { + return CropResponse; + } + static get enqueueSlug() { + return "utilities/crop"; + } + static get getResultSlug() { + return "utilities/crop"; + } +} diff --git a/src/v2/parsing/inference/crop/cropInference.ts b/src/v2/product/crop/cropInference.ts similarity index 89% rename from src/v2/parsing/inference/crop/cropInference.ts rename to src/v2/product/crop/cropInference.ts index 19e68c98..6b35c269 100644 --- a/src/v2/parsing/inference/crop/cropInference.ts +++ b/src/v2/product/crop/cropInference.ts @@ -1,6 +1,6 @@ import { StringDict } from "@/parsing/index.js"; import { BaseInference } from "@/v2/parsing/inference/baseInference.js"; -import { CropResult } from "@/v2/parsing/inference/crop/cropResult.js"; +import { CropResult } from "@/v2/product/crop/cropResult.js"; export class CropInference extends BaseInference { /** diff --git a/src/v2/parsing/inference/crop/cropItem.ts b/src/v2/product/crop/cropItem.ts similarity index 88% rename from src/v2/parsing/inference/crop/cropItem.ts rename to src/v2/product/crop/cropItem.ts index c1481a07..470042b0 100644 --- a/src/v2/parsing/inference/crop/cropItem.ts +++ b/src/v2/product/crop/cropItem.ts @@ -11,6 +11,6 @@ export class CropItem { } toString(): string { - return `${this.objectType} :: ${this.location}`; + return `${this.objectType}: ${this.location}`; } } diff --git a/src/v2/product/crop/cropParameters.ts b/src/v2/product/crop/cropParameters.ts new file mode 100644 index 00000000..ba53eb1f --- /dev/null +++ b/src/v2/product/crop/cropParameters.ts @@ -0,0 +1,26 @@ +import { BaseParameters, BaseParametersConstructor } from "@/v2/client/baseParameters.js"; +import { logger } from "@/logger.js"; + +/** + * Parameters accepted by the asynchronous **inference** v2 endpoint. + * + * All fields are optional except `modelId`. + * + * @category ClientV2 + * @example + * const params = { + * modelId: "YOUR_MODEL_ID", + * alias: "YOUR_ALIAS", + * webhookIds: ["YOUR_WEBHOOK_ID_1", "YOUR_WEBHOOK_ID_2"], + * pollingOptions: { + * initialDelaySec: 2, + * delaySec: 1.5, + * } + * }; + */ +export class CropParameters extends BaseParameters { + constructor(params: BaseParametersConstructor & {}) { + super({ ...params }); + logger.debug("Crop parameters initialized."); + } +} diff --git a/src/v2/product/crop/cropResponse.ts b/src/v2/product/crop/cropResponse.ts new file mode 100644 index 00000000..c50b2518 --- /dev/null +++ b/src/v2/product/crop/cropResponse.ts @@ -0,0 +1,18 @@ +import { StringDict } from "@/parsing/stringDict.js"; +import { CropInference } from "./cropInference.js"; +import { BaseResponse } from "@/v2/parsing/index.js"; + +export class CropResponse extends BaseResponse { + /** + * Response for a crop utility inference. + */ + inference: CropInference; + + /** + * @param serverResponse JSON response from the server. + */ + constructor(serverResponse: StringDict) { + super(serverResponse); + this.inference = new CropInference(serverResponse["inference"]); + } +} diff --git a/src/v2/product/crop/cropResult.ts b/src/v2/product/crop/cropResult.ts new file mode 100644 index 00000000..e15dc527 --- /dev/null +++ b/src/v2/product/crop/cropResult.ts @@ -0,0 +1,18 @@ +import { StringDict } from "@/parsing/stringDict.js"; +import { CropItem } from "@/v2/product/crop/cropItem.js"; + +export class CropResult { + /** + * Fields contained in the inference. + */ + public crops: CropItem[] = []; + + constructor(serverResponse: StringDict) { + this.crops = serverResponse["crops"].map((cropItem: StringDict) => new CropItem(cropItem)); + } + + toString(): string { + const crops = this.crops.map(item => item.toString()).join("\n * "); + return `Crop\n====\n * ${crops}`; + } +} diff --git a/src/v2/product/crop/index.ts b/src/v2/product/crop/index.ts new file mode 100644 index 00000000..7d3b2527 --- /dev/null +++ b/src/v2/product/crop/index.ts @@ -0,0 +1,6 @@ +export { Crop } from "./crop.js"; +export { CropParameters } from "./cropParameters.js"; +export { CropInference } from "./cropInference.js"; +export { CropItem } from "./cropItem.js"; +export { CropResponse } from "./cropResponse.js"; +export { CropResult } from "./cropResult.js"; diff --git a/src/v2/client/dataSchema.ts b/src/v2/product/extraction/dataSchema.ts similarity index 100% rename from src/v2/client/dataSchema.ts rename to src/v2/product/extraction/dataSchema.ts diff --git a/src/v2/parsing/inference/extraction/dataSchemaActiveOption.ts b/src/v2/product/extraction/dataSchemaActiveOption.ts similarity index 100% rename from src/v2/parsing/inference/extraction/dataSchemaActiveOption.ts rename to src/v2/product/extraction/dataSchemaActiveOption.ts diff --git a/src/v2/product/extraction/extraction.ts b/src/v2/product/extraction/extraction.ts new file mode 100644 index 00000000..39ca3992 --- /dev/null +++ b/src/v2/product/extraction/extraction.ts @@ -0,0 +1,18 @@ +import { ExtractionResponse } from "./extractionResponse.js"; +import { ExtractionParameters } from "./extractionParameters.js"; +import { BaseProduct } from "@/v2/product/baseProduct.js"; + +export class Extraction extends BaseProduct { + static get parameters() { + return ExtractionParameters; + } + static get response() { + return ExtractionResponse; + } + static get enqueueSlug() { + return "inferences"; + } + static get getResultSlug() { + return "inferences"; + } +} diff --git a/src/v2/parsing/inference/extraction/extractionActiveOptions.ts b/src/v2/product/extraction/extractionActiveOptions.ts similarity index 100% rename from src/v2/parsing/inference/extraction/extractionActiveOptions.ts rename to src/v2/product/extraction/extractionActiveOptions.ts diff --git a/src/v2/parsing/inference/extraction/extractionInference.ts b/src/v2/product/extraction/extractionInference.ts similarity index 100% rename from src/v2/parsing/inference/extraction/extractionInference.ts rename to src/v2/product/extraction/extractionInference.ts diff --git a/src/v2/client/extractionParameters.ts b/src/v2/product/extraction/extractionParameters.ts similarity index 97% rename from src/v2/client/extractionParameters.ts rename to src/v2/product/extraction/extractionParameters.ts index b63f83be..fe3b8f1c 100644 --- a/src/v2/client/extractionParameters.ts +++ b/src/v2/product/extraction/extractionParameters.ts @@ -1,6 +1,7 @@ import { StringDict } from "@/parsing/stringDict.js"; import { DataSchema } from "./dataSchema.js"; import { BaseParameters, BaseParametersConstructor } from "@/v2/client/baseParameters.js"; +import { logger } from "@/logger.js"; /** * Parameters accepted by the asynchronous **inference** v2 endpoint. @@ -71,6 +72,7 @@ export class ExtractionParameters extends BaseParameters { this.dataSchema = params.dataSchema; } } + logger.debug("Extraction parameters initialized."); } getFormData(): FormData { diff --git a/src/v2/product/extraction/extractionResponse.ts b/src/v2/product/extraction/extractionResponse.ts new file mode 100644 index 00000000..8a568f30 --- /dev/null +++ b/src/v2/product/extraction/extractionResponse.ts @@ -0,0 +1,19 @@ +import { ExtractionInference } from "./extractionInference.js"; +import { StringDict } from "@/parsing/stringDict.js"; +import { BaseResponse } from "@/v2/parsing/index.js"; + +export class ExtractionResponse extends BaseResponse { + + /** + * The inference result for an extraction request. + */ + inference: ExtractionInference; + + /** + * @param serverResponse JSON response from the server. + */ + constructor(serverResponse: StringDict) { + super(serverResponse); + this.inference = new ExtractionInference(serverResponse["inference"]); + } +} diff --git a/src/v2/parsing/inference/extraction/extractionResult.ts b/src/v2/product/extraction/extractionResult.ts similarity index 84% rename from src/v2/parsing/inference/extraction/extractionResult.ts rename to src/v2/product/extraction/extractionResult.ts index e1a89d50..8e3ce1b0 100644 --- a/src/v2/parsing/inference/extraction/extractionResult.ts +++ b/src/v2/product/extraction/extractionResult.ts @@ -1,7 +1,7 @@ import { InferenceFields } from "@/v2/parsing/inference/field/index.js"; import { StringDict } from "@/parsing/stringDict.js"; -import { RawText } from "../field/rawText.js"; -import { RagMetadata } from "../field/ragMetadata.js"; +import { RawText } from "@/v2/parsing/inference/field/rawText.js"; +import { RagMetadata } from "@/v2/parsing/inference/field/ragMetadata.js"; export class ExtractionResult { /** diff --git a/src/v2/product/extraction/index.ts b/src/v2/product/extraction/index.ts new file mode 100644 index 00000000..d6cb61bd --- /dev/null +++ b/src/v2/product/extraction/index.ts @@ -0,0 +1,8 @@ +export { Extraction } from "./extraction.js"; +export { ExtractionParameters } from "./extractionParameters.js"; +export { DataSchema } from "./dataSchema.js"; +export { ExtractionInference } from "./extractionInference.js"; +export { ExtractionActiveOptions } from "./extractionActiveOptions.js"; +export { ExtractionResponse } from "./extractionResponse.js"; +export { ExtractionResult } from "./extractionResult.js"; +export { DataSchemaActiveOption } from "./dataSchemaActiveOption.js"; diff --git a/src/v2/product/index.ts b/src/v2/product/index.ts new file mode 100644 index 00000000..5fc60b74 --- /dev/null +++ b/src/v2/product/index.ts @@ -0,0 +1,5 @@ +export { Classification } from "./classification/index.js"; +export { Crop } from "./crop/index.js"; +export { Extraction, ExtractionResponse } from "./extraction/index.js"; +export { Ocr } from "./ocr/index.js"; +export { Split } from "./split/index.js"; diff --git a/src/v2/product/ocr/index.ts b/src/v2/product/ocr/index.ts new file mode 100644 index 00000000..78caefa2 --- /dev/null +++ b/src/v2/product/ocr/index.ts @@ -0,0 +1,4 @@ +export { Ocr } from "./ocr.js"; +export { OcrParameters } from "./ocrParameters.js"; +export { OcrResponse } from "./ocrResponse.js"; +export { OcrInference } from "./ocrInference.js"; diff --git a/src/v2/product/ocr/ocr.ts b/src/v2/product/ocr/ocr.ts new file mode 100644 index 00000000..e82945cd --- /dev/null +++ b/src/v2/product/ocr/ocr.ts @@ -0,0 +1,18 @@ +import { OcrResponse } from "./ocrResponse.js"; +import { OcrParameters } from "./ocrParameters.js"; +import { BaseProduct } from "@/v2/product/baseProduct.js"; + +export class Ocr extends BaseProduct { + static get parameters() { + return OcrParameters; + } + static get response() { + return OcrResponse; + } + static get enqueueSlug() { + return "utilities/ocr"; + } + static get getResultSlug() { + return "utilities/ocr"; + } +} diff --git a/src/v2/parsing/inference/ocr/ocrInference.ts b/src/v2/product/ocr/ocrInference.ts similarity index 100% rename from src/v2/parsing/inference/ocr/ocrInference.ts rename to src/v2/product/ocr/ocrInference.ts diff --git a/src/v2/client/utilityParameters.ts b/src/v2/product/ocr/ocrParameters.ts similarity index 80% rename from src/v2/client/utilityParameters.ts rename to src/v2/product/ocr/ocrParameters.ts index b46676f0..8a17ac48 100644 --- a/src/v2/client/utilityParameters.ts +++ b/src/v2/product/ocr/ocrParameters.ts @@ -1,4 +1,5 @@ import { BaseParameters, BaseParametersConstructor } from "@/v2/client/baseParameters.js"; +import { logger } from "@/logger.js"; /** * Parameters accepted by the asynchronous **inference** v2 endpoint. @@ -17,9 +18,9 @@ import { BaseParameters, BaseParametersConstructor } from "@/v2/client/baseParam * } * }; */ -export class UtilityParameters extends BaseParameters { - +export class OcrParameters extends BaseParameters { constructor(params: BaseParametersConstructor & {}) { super({ ...params }); + logger.debug("Ocr parameters initialized."); } } diff --git a/src/v2/product/ocr/ocrResponse.ts b/src/v2/product/ocr/ocrResponse.ts new file mode 100644 index 00000000..2e6ddb3d --- /dev/null +++ b/src/v2/product/ocr/ocrResponse.ts @@ -0,0 +1,18 @@ +import { StringDict } from "@/parsing/stringDict.js"; +import { OcrInference } from "./ocrInference.js"; +import { BaseResponse } from "@/v2/parsing/index.js"; + +export class OcrResponse extends BaseResponse { + /** + * Response for an OCR utility inference. + */ + inference: OcrInference; + + /** + * @param serverResponse JSON response from the server. + */ + constructor(serverResponse: StringDict) { + super(serverResponse); + this.inference = new OcrInference(serverResponse["inference"]); + } +} diff --git a/src/v2/product/split/index.ts b/src/v2/product/split/index.ts new file mode 100644 index 00000000..10477bb7 --- /dev/null +++ b/src/v2/product/split/index.ts @@ -0,0 +1,4 @@ +export { Split } from "./split.js"; +export { SplitParameters } from "./splitParameters.js"; +export { SplitResponse } from "./splitResponse.js"; +export { SplitInference } from "./splitInference.js"; diff --git a/src/v2/product/split/split.ts b/src/v2/product/split/split.ts new file mode 100644 index 00000000..9b5a47c5 --- /dev/null +++ b/src/v2/product/split/split.ts @@ -0,0 +1,18 @@ +import { SplitResponse } from "./splitResponse.js"; +import { SplitParameters } from "./splitParameters.js"; +import { BaseProduct } from "@/v2/product/baseProduct.js"; + +export class Split extends BaseProduct { + static get parameters() { + return SplitParameters; + } + static get response() { + return SplitResponse; + } + static get enqueueSlug() { + return "utilities/split"; + } + static get getResultSlug() { + return "utilities/split"; + } +} diff --git a/src/v2/parsing/inference/split/splitInference.ts b/src/v2/product/split/splitInference.ts similarity index 100% rename from src/v2/parsing/inference/split/splitInference.ts rename to src/v2/product/split/splitInference.ts diff --git a/src/v2/product/split/splitParameters.ts b/src/v2/product/split/splitParameters.ts new file mode 100644 index 00000000..71cc146e --- /dev/null +++ b/src/v2/product/split/splitParameters.ts @@ -0,0 +1,29 @@ +import { + BaseParameters, + BaseParametersConstructor, +} from "@/v2/client/baseParameters.js"; +import { logger } from "@/logger.js"; + +/** + * Parameters accepted by the asynchronous **inference** v2 endpoint. + * + * All fields are optional except `modelId`. + * + * @category ClientV2 + * @example + * const params = { + * modelId: "YOUR_MODEL_ID", + * alias: "YOUR_ALIAS", + * webhookIds: ["YOUR_WEBHOOK_ID_1", "YOUR_WEBHOOK_ID_2"], + * pollingOptions: { + * initialDelaySec: 2, + * delaySec: 1.5, + * } + * }; + */ +export class SplitParameters extends BaseParameters { + constructor(params: BaseParametersConstructor & {}) { + super({ ...params }); + logger.debug("Split parameters initialized."); + } +} diff --git a/src/v2/product/split/splitResponse.ts b/src/v2/product/split/splitResponse.ts new file mode 100644 index 00000000..891cb30d --- /dev/null +++ b/src/v2/product/split/splitResponse.ts @@ -0,0 +1,18 @@ +import { StringDict } from "@/parsing/stringDict.js"; +import { SplitInference } from "./splitInference.js"; +import { BaseResponse } from "@/v2/parsing/index.js"; + +export class SplitResponse extends BaseResponse { + /** + * Response for an OCR utility inference. + */ + inference: SplitInference; + + /** + * @param serverResponse JSON response from the server. + */ + constructor(serverResponse: StringDict) { + super(serverResponse); + this.inference = new SplitInference(serverResponse["inference"]); + } +} diff --git a/tests/data b/tests/data index 7bed8f9b..ed58a9d5 160000 --- a/tests/data +++ b/tests/data @@ -1 +1 @@ -Subproject commit 7bed8f9b059f6ba3debc31e71375ea8437b6fbe2 +Subproject commit ed58a9d57a31294fef0b8554388db287335e3b79 diff --git a/tests/test_code_samples.sh b/tests/test_code_samples.sh index a3529d7a..69528399 100755 --- a/tests/test_code_samples.sh +++ b/tests/test_code_samples.sh @@ -33,7 +33,7 @@ do sed "s/my-api-key/$API_KEY/" "${f}" > $OUTPUT_FILE sed -i "s/\/path\/to\/the\/file.ext/..\/mindee-api-nodejs\/tests\/data\/file_types\/pdf\/blank_1.pdf/" $OUTPUT_FILE - if echo "${f}" | grep -q "default_v2.txt" + if echo "${f}" | grep -q "v2_default.txt" then sed -i "s/MY_API_KEY/$API_KEY_V2/" $OUTPUT_FILE sed -i "s/MY_MODEL_ID/$MODEL_ID/" $OUTPUT_FILE diff --git a/tests/v2/client.integration.ts b/tests/v2/client.integration.ts index 4cde80e8..bc760d42 100644 --- a/tests/v2/client.integration.ts +++ b/tests/v2/client.integration.ts @@ -3,17 +3,20 @@ import path from "node:path"; import { Client, - ExtractionParameters, PathInput, UrlInput, Base64Input, - ExtractionResponse, } from "@/index.js"; -import { ExtractionInference } from "@/v2/parsing/index.js"; +import { + ExtractionInference, + ExtractionParameters, + ExtractionResponse, +} from "@/v2/product/extraction/index.js"; import { SimpleField } from "@/v2/parsing/inference/field/index.js"; import { MindeeHttpErrorV2 } from "@/v2/http/index.js"; import * as fs from "node:fs"; import { RESOURCE_PATH, V2_PRODUCT_PATH, V2_RESOURCE_PATH } from "../index.js"; +import { Extraction } from "@/v2/product/index.js"; function check422(err: unknown) { expect(err).to.be.instanceOf(MindeeHttpErrorV2); @@ -80,8 +83,8 @@ describe("MindeeV2 – Client Integration Tests", () => { webhookIds: [], alias: "ts_integration_empty_multiple" }; - const response = await client.enqueueAndGetInference( - ExtractionInference, source, params + const response = await client.enqueueAndGetResult( + Extraction, source, params ); expect(response).to.exist; expect(response.inference).to.be.instanceOf(ExtractionInference); @@ -109,8 +112,8 @@ describe("MindeeV2 – Client Integration Tests", () => { alias: "ts_integration_binary_filled_single" }; - const response = await client.enqueueAndGetInference( - ExtractionInference, source, params + const response = await client.enqueueAndGetResult( + Extraction, source, params ); expect(response.inference).to.be.instanceOf(ExtractionInference); const inference: ExtractionInference = response.inference; @@ -138,7 +141,7 @@ describe("MindeeV2 – Client Integration Tests", () => { it("Filled, single-page image – Base64Input - enqueueAndGetInference must succeed", async () => { const data = fs.readFileSync(sampleBase64Path, "utf8"); const source = new Base64Input({ inputString: data, filename: "receipt.jpg" }); - const params = new ExtractionParameters({ + const params = { modelId, rag: false, rawText: false, @@ -146,10 +149,10 @@ describe("MindeeV2 – Client Integration Tests", () => { confidence: false, webhookIds: [], alias: "ts_integration_base64_filled_single" - }); + }; - const response = await client.enqueueAndGetInference( - ExtractionInference, source, params + const response = await client.enqueueAndGetResult( + Extraction, source, params ); expect(response.inference).to.be.instanceOf(ExtractionInference); const inference: ExtractionInference = response.inference; @@ -171,7 +174,7 @@ describe("MindeeV2 – Client Integration Tests", () => { const badParams = { modelId: "00000000-0000-0000-0000-000000000000" }; try { - await client.enqueueInference(ExtractionInference, source, badParams); + await client.enqueue(Extraction, source, badParams); expect.fail("Expected the call to throw, but it succeeded."); } catch (err) { check422(err); @@ -180,8 +183,8 @@ describe("MindeeV2 – Client Integration Tests", () => { it("Invalid job ID – getInference must raise 422", async () => { try { - await client.getInference( - ExtractionInference, + await client.getResult( + Extraction, "00000000-0000-0000-0000-000000000000" ); expect.fail("Expected the call to throw, but it succeeded."); @@ -202,8 +205,8 @@ describe("MindeeV2 – Client Integration Tests", () => { webhookIds: [], alias: "ts_integration_url_source" }); - const response: ExtractionResponse = await client.enqueueAndGetInference( - ExtractionInference, source, params + const response: ExtractionResponse = await client.enqueueAndGetResult( + Extraction, source, params ); expect(response).to.exist; expect(response.inference).to.be.instanceOf(ExtractionInference); @@ -221,8 +224,8 @@ describe("MindeeV2 – Client Integration Tests", () => { dataSchema: dataSchemaReplace, alias: "ts_integration_data_schema_replace" }); - const response = await client.enqueueAndGetInference( - ExtractionInference, source, params + const response = await client.enqueueAndGetResult( + Extraction, source, params ); expect(response).to.exist; expect(response.inference).to.be.instanceOf(ExtractionInference); diff --git a/tests/v2/client.spec.ts b/tests/v2/client.spec.ts index 6eeab60e..31d7ca61 100644 --- a/tests/v2/client.spec.ts +++ b/tests/v2/client.spec.ts @@ -6,7 +6,7 @@ import { MindeeHttpErrorV2 } from "@/v2/http/index.js"; import assert from "node:assert/strict"; import { RESOURCE_PATH, V2_RESOURCE_PATH } from "../index.js"; import fs from "node:fs/promises"; -import { CropInference, ExtractionInference } from "@/v2/parsing/index.js"; +import { Crop, Extraction } from "@/v2/product/index.js"; const mockAgent = new MockAgent(); setGlobalDispatcher(mockAgent); @@ -69,12 +69,12 @@ describe("MindeeV2 - ClientV2", () => { expect(api.settings.baseHeaders["User-Agent"]).to.match(/mindee/i); }); - it("enqueueInference(path) rejects with MindeeHttpErrorV2 on 400", async () => { + it("enqueue(path) on extraction rejects with MindeeHttpErrorV2 on 400", async () => { const filePath = path.join(fileTypesDir, "receipt.jpg"); const inputDoc = new PathInput({ inputPath: filePath }); await assert.rejects( - client.enqueueInference(ExtractionInference, inputDoc, { modelId: "dummy-model", textContext: "hello" }), + client.enqueue(Extraction, inputDoc, { modelId: "dummy-model", textContext: "hello" }), (error: any) => { assert.strictEqual(error instanceof MindeeHttpErrorV2, true); assert.strictEqual(error.status, 400); @@ -83,12 +83,12 @@ describe("MindeeV2 - ClientV2", () => { ); }); - it("enqueueUtility(path) rejects with MindeeHttpErrorV2 on 400", async () => { + it("enqueue(path) on crop rejects with MindeeHttpErrorV2 on 400", async () => { const filePath = path.join(fileTypesDir, "receipt.jpg"); const inputDoc = new PathInput({ inputPath: filePath }); await assert.rejects( - client.enqueueInference(CropInference, inputDoc, { modelId: "dummy-model" }), + client.enqueue(Crop, inputDoc, { modelId: "dummy-model" }), (error: any) => { assert.strictEqual(error instanceof MindeeHttpErrorV2, true); assert.strictEqual(error.status, 400); @@ -97,12 +97,12 @@ describe("MindeeV2 - ClientV2", () => { ); }); - it("enqueueAndGetInference(path) rejects with MindeeHttpErrorV2 on 400", async () => { + it("enqueueAndGetResult(path) on extraction rejects with MindeeHttpErrorV2 on 400", async () => { const filePath = path.join(fileTypesDir, "receipt.jpg"); const inputDoc = new PathInput({ inputPath: filePath }); await assert.rejects( - client.enqueueAndGetInference( - ExtractionInference, + client.enqueueAndGetResult( + Extraction, inputDoc, { modelId: "dummy-model", rag: false } ), @@ -124,7 +124,7 @@ describe("MindeeV2 - ClientV2", () => { ), }); await assert.rejects( - client.enqueueInference(ExtractionInference, input, { modelId: "dummy-model" }), + client.enqueue(Extraction, input, { modelId: "dummy-model" }), (error: any) => { expect(error).to.be.instanceOf(MindeeHttpErrorV2); expect(error.status).to.equal(400); diff --git a/tests/v2/client/inferenceParameter.spec.ts b/tests/v2/client/inferenceParameter.spec.ts index 8d3e6b4c..00907884 100644 --- a/tests/v2/client/inferenceParameter.spec.ts +++ b/tests/v2/client/inferenceParameter.spec.ts @@ -1,10 +1,9 @@ import { StringDict } from "@/parsing/index.js"; import path from "path"; import { V2_RESOURCE_PATH } from "../../index.js"; -import { ExtractionParameters } from "@/index.js"; import { expect } from "chai"; -import { DataSchema } from "@/index.js"; import { promises as fs } from "fs"; +import { ExtractionParameters, DataSchema } from "@/v2/product/extraction/index.js"; let expectedDataSchemaDict: StringDict; let expectedDataSchemaString: string; @@ -30,7 +29,9 @@ describe("MindeeV2 - Inference Parameter", () => { describe("Data Schema", () => { before(async () => { - const fileContents = await fs.readFile(path.join(V2_RESOURCE_PATH, "inference/data_schema_replace_param.json")); + const fileContents = await fs.readFile( + path.join(V2_RESOURCE_PATH, "inference/data_schema_replace_param.json") + ); expectedDataSchemaDict = JSON.parse(fileContents.toString()); expectedDataSchemaString = JSON.stringify(expectedDataSchemaDict); expectedDataSchemaObject = new DataSchema(expectedDataSchemaDict); diff --git a/tests/v2/parsing/inference.spec.ts b/tests/v2/parsing/inference.spec.ts index 86a29069..a9ec5234 100644 --- a/tests/v2/parsing/inference.spec.ts +++ b/tests/v2/parsing/inference.spec.ts @@ -10,11 +10,11 @@ import { } from "@/v2/parsing/inference/field/index.js"; import { LocalResponse, - ExtractionResponse, RagMetadata, RawText, } from "@/v2/parsing/index.js"; import { V2_RESOURCE_PATH } from "../../index.js"; +import { ExtractionResponse } from "@/v2/product/index.js"; const findocPath = path.join(V2_RESOURCE_PATH, "products", "financial_document"); const inferencePath = path.join(V2_RESOURCE_PATH, "inference"); diff --git a/tests/v2/parsing/localResponse.spec.ts b/tests/v2/parsing/localResponse.spec.ts index a91f6089..2fda9a8c 100644 --- a/tests/v2/parsing/localResponse.spec.ts +++ b/tests/v2/parsing/localResponse.spec.ts @@ -1,12 +1,13 @@ import * as fs from "node:fs/promises"; import { expect } from "chai"; -import { ExtractionResponse, LocalResponse } from "@/v2/index.js"; +import { LocalResponse } from "@/v2/index.js"; import path from "path"; import { V2_RESOURCE_PATH } from "../../index.js"; import { Buffer } from "node:buffer"; +import { ExtractionResponse } from "@/v2/product/index.js"; -const signature: string = "1df388c992d87897fe61dfc56c444c58fc3c7369c31e2b5fd20d867695e93e85"; +const signature: string = "e51bdf80f1a08ed44ee161100fc30a25cb35b4ede671b0a575dc9064a3f5dbf1"; const dummySecretKey: string = "ogNjY44MhvKPGTtVsI8zG82JqWQa68woYQH"; const filePath: string = path.join(V2_RESOURCE_PATH, "inference/standard_field_types.json");