Skip to content

Commit e450818

Browse files
authored
✨ add Job to inference responses (#445)
1 parent 61929e1 commit e450818

20 files changed

Lines changed: 143 additions & 49 deletions

src/v2/client/baseParameters.ts

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ export interface BaseParametersConstructor {
1111
}
1212

1313
/**
14-
* Parameters accepted by the asynchronous **inference** v2 endpoint.
14+
* Parameters accepted by all v2 products.
1515
*
1616
* All fields are optional except `modelId`.
1717
*
@@ -22,10 +22,6 @@ export interface BaseParametersConstructor {
2222
* rag: true,
2323
* alias: "YOUR_ALIAS",
2424
* webhookIds: ["YOUR_WEBHOOK_ID_1", "YOUR_WEBHOOK_ID_2"],
25-
* pollingOptions: {
26-
* initialDelaySec: 2,
27-
* delaySec: 1.5,
28-
* }
2925
* };
3026
*/
3127
export abstract class BaseParameters {

src/v2/client/index.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
11
export { PollingOptions } from "./pollingOptions.js";
2-
export type { PollingOptionsConstructor } from "./pollingOptions.js";
2+
export type {
3+
PollingOptionsConstructor,
4+
TimerOptions,
5+
} from "./pollingOptions.js";
36
export { BaseParameters } from "./baseParameters.js";

src/v2/client/pollingOptions.ts

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@ const minRetries = 2;
2222
* Parameters for the internal polling loop in `enqueueAndGetInference()`.
2323
*
2424
* Default behavior:
25-
* - `initialDelaySec` = 2s
26-
* - `delaySec` = 1.5s
25+
* - `initialDelaySec` = 2
26+
* - `delaySec` = 1.5
2727
* - `maxRetries` = 80
2828
*
2929
* Validation rules:
@@ -32,17 +32,21 @@ const minRetries = 2;
3232
* - `maxRetries` >= 2
3333
*
3434
* The `initialTimerOptions` and `recurringTimerOptions` objects let you pass an
35-
* `AbortSignal` or make the timer `unref`-ed to the `setTimeout()`.
35+
* `AbortSignal` or make the timer `ref`-ed to the `setTimeout()`.
3636
*
3737
* @category ClientV2
3838
* @example
39-
* const params = {
39+
* ```
40+
* const pollingOptions = {
4041
* initialDelaySec: 4,
4142
* delaySec: 2,
4243
* maxRetries: 50
4344
* };
4445
*
45-
* const inference = await client.enqueueAndGetInference(inputDoc, params);
46+
* const inference = await client.enqueueAndGetInference(
47+
* inputDoc, params, pollingOptions
48+
* );
49+
* ```
4650
*/
4751
export class PollingOptions {
4852
/** Number of seconds to wait *before the first poll*. */
@@ -52,15 +56,9 @@ export class PollingOptions {
5256
/** Maximum number of polling attempts (including the first one). */
5357
maxRetries: number;
5458
/** Options passed to the initial `setTimeout()`. */
55-
initialTimerOptions?: {
56-
ref?: boolean,
57-
signal?: AbortSignal
58-
};
59+
initialTimerOptions?: TimerOptions;
5960
/** Options passed to every recurring `setTimeout()`. */
60-
recurringTimerOptions?: {
61-
ref?: boolean,
62-
signal?: AbortSignal
63-
};
61+
recurringTimerOptions?: TimerOptions;
6462

6563
constructor(params?: PollingOptionsConstructor) {
6664
if (!params) {
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ const API_V2_KEY_ENVVAR_NAME: string = "MINDEE_V2_API_KEY";
66
const API_V2_HOST_ENVVAR_NAME: string = "MINDEE_V2_API_HOST";
77
const DEFAULT_MINDEE_API_HOST: string = "api-v2.mindee.net";
88

9-
export class ApiSettingsV2 extends BaseSettings {
9+
export class ApiSettings extends BaseSettings {
1010
baseHeaders: Record<string, string>;
1111

1212
constructor({
@@ -25,6 +25,7 @@ export class ApiSettingsV2 extends BaseSettings {
2525
"User-Agent": this.getUserAgent(),
2626
Authorization: `${this.apiKey}`,
2727
};
28+
/* eslint-enable @typescript-eslint/naming-convention */
2829
}
2930

3031
protected apiKeyFromEnv(): string {

src/v2/http/errors.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
import { ErrorDetails, ErrorItem, ErrorResponse } from "@/v2/parsing/index.js";
22
import { MindeeError } from "@/errors/index.js";
33

4+
/**
5+
* HTTP error returned by the API.
6+
*/
47
export class MindeeHttpErrorV2 extends MindeeError implements ErrorDetails {
58
public status: number;
69
public detail: string;

src/v2/http/mindeeApiV2.ts

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { ApiSettingsV2 } from "./apiSettingsV2.js";
1+
import { ApiSettings } from "./apiSettings.js";
22
import { Dispatcher } from "undici";
33
import { BaseParameters } from "@/v2/client/index.js";
44
import {
@@ -16,10 +16,10 @@ import { BaseProduct } from "@/v2/product/baseProduct.js";
1616

1717

1818
export class MindeeApiV2 {
19-
settings: ApiSettingsV2;
19+
settings: ApiSettings;
2020

2121
constructor(dispatcher?: Dispatcher, apiKey?: string) {
22-
this.settings = new ApiSettingsV2({ dispatcher: dispatcher, apiKey: apiKey });
22+
this.settings = new ApiSettings({ dispatcher: dispatcher, apiKey: apiKey });
2323
}
2424

2525
/**
@@ -80,7 +80,10 @@ export class MindeeApiV2 {
8080
result: BaseHttpResponse,
8181
responseClass: ResponseConstructor<T>,
8282
): T {
83-
if (result.messageObj?.statusCode && (result.messageObj?.statusCode > 399 || result.messageObj?.statusCode < 200)) {
83+
if (
84+
result.messageObj?.statusCode
85+
&& (result.messageObj?.statusCode > 399 || result.messageObj?.statusCode < 200)
86+
) {
8487
if (result.data?.status !== null) {
8588
throw new MindeeHttpErrorV2(new ErrorResponse(result.data));
8689
}
@@ -98,7 +101,9 @@ export class MindeeApiV2 {
98101
try {
99102
return new responseClass(result.data);
100103
} catch (e) {
101-
logger.error(`Raised '${e}' Couldn't deserialize response object:\n${JSON.stringify(result.data)}`);
104+
logger.error(
105+
`Raised '${e}' Couldn't deserialize response object:\n${JSON.stringify(result.data)}`
106+
);
102107
throw new MindeeDeserializationError("Couldn't deserialize response object.");
103108
}
104109
}

src/v2/index.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,5 @@ export {
77
ErrorResponse,
88
LocalResponse,
99
} from "./parsing/index.js";
10-
export type { PollingOptions, BaseParameters } from "./client/index.js";
10+
export type { BaseParameters, TimerOptions } from "./client/index.js";
11+
export { PollingOptions } from "./client/index.js";

src/v2/parsing/inference/baseInference.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,17 @@
1+
import { StringDict } from "@/parsing/index.js";
12
import { InferenceModel } from "./inferenceModel.js";
23
import { InferenceFile } from "./inferenceFile.js";
3-
import { StringDict } from "@/parsing/index.js";
4+
import { InferenceJob } from "./inferenceJob.js";
45

56
export abstract class BaseInference {
67
/**
78
* Model info for the inference.
89
*/
910
public model: InferenceModel;
11+
/**
12+
* Job the inference belongs to.
13+
*/
14+
public job: InferenceJob;
1015
/**
1116
* File info for the inference.
1217
*/
@@ -18,6 +23,7 @@ export abstract class BaseInference {
1823

1924
protected constructor(serverResponse: StringDict) {
2025
this.id = serverResponse["id"];
26+
this.job = new InferenceJob(serverResponse["job"]);
2127
this.model = new InferenceModel(serverResponse["model"]);
2228
this.file = new InferenceFile(serverResponse["file"]);
2329
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import { StringDict } from "@/parsing/stringDict.js";
2+
3+
export class InferenceJob {
4+
/**
5+
* UUID of the Job.
6+
*/
7+
public id: string;
8+
9+
constructor(serverResponse: StringDict) {
10+
this.id = serverResponse["id"];
11+
}
12+
13+
toString () {
14+
return(
15+
"Job\n" +
16+
"===\n" +
17+
`:ID: ${this.id}\n`
18+
);
19+
}
20+
}

src/v2/product/baseProduct.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
import { BaseParameters } from "@/v2/client/index.js";
22
import { ResponseConstructor } from "@/v2/parsing/index.js";
33

4+
/**
5+
* Base class for all V2 product definitions.
6+
*
7+
* Child classes are passed to the Client when making requests.
8+
*/
49
export abstract class BaseProduct {
510
static get parametersClass(): new (...args: any[]) => BaseParameters {
611
throw new Error("Must define static parameters property");

0 commit comments

Comments
 (0)