/
training.ts
91 lines (82 loc) · 2.59 KB
/
training.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.
import { PollerLike, PollOperationState } from "@azure/core-lro";
import { OperationOptions } from "../../../../core/core-client/types/latest/core-client";
import { FormRecognizerError } from "../error";
import { GetOperationResponse, ModelInfo, OperationStatus } from "../generated";
import { PollerOptions } from "../options/PollerOptions";
/**
* The state of a model creation operation.
*/
export interface TrainingPollOperationState extends PollOperationState<ModelInfo> {
/**
* The status of the operation. One of:
*
* - "notStarted"
* - "running"
* - "succeeded"
* - "failed"
* - "canceled"
*/
status: OperationStatus;
/**
* The unique ID of this operation.
*/
operationId: string;
/**
* A number between 0 and 100 representing the progress of the operation.
*/
percentCompleted: number;
}
/**
* Convert an operation result into a training poller state.
* @internal
*/
export async function toTrainingPollOperationState(
response: GetOperationResponse
): Promise<TrainingPollOperationState> {
return {
operationId: response.operationId,
status: response.status,
percentCompleted: response.percentCompleted ?? 0,
result: response.result,
error: response.error && new FormRecognizerError(response.error),
isCancelled: response.status === "canceled",
isCompleted: response.status === "succeeded",
isStarted: response.status !== "notStarted",
};
}
/**
* A long-running operation (poller) that tracks the state of a model creation operation, eventually producing a
* {@link ModelInfo}.
*/
export type TrainingPoller = PollerLike<TrainingPollOperationState, ModelInfo>;
const operationLocationRegex = /\/operations\/([^?/]+)/;
/**
* Extract an operation ID from an operationLocation URL.
* @internal
*/
export function parseOperationLocation(url: string | undefined): string {
if (url === undefined) {
throw new Error("Failed to start training operation: no operation-location in the response.");
}
const parseResult = operationLocationRegex.exec(url);
if (!parseResult || !parseResult[1]) {
throw new Error(`Unable to parse operationLocation: "${url}"`);
}
return parseResult[1];
}
/**
* Defines a training operation.
* @internal
*/
export interface TrainingOperationDefinition {
/**
* A function to start the operation, producing an operationLocation.
*/
start: () => Promise<{ operationLocation?: string }>;
/**
* Options for the poller and requests.
*/
options: PollerOptions<TrainingPollOperationState> & OperationOptions;
}