| 1 | import * as datalabeling from "@distilled.cloud/gcp/datalabeling_v1beta1"; |
| 2 | import * as Effect from "effect/Effect"; |
| 3 | import { waitForOperation as waitForGcpOperation } from "../Operation.ts"; |
| 4 | import { noRetryLayer } from "./internal.ts"; |
| 5 | |
| 6 | const stringField = (value: unknown): string | undefined => |
| 7 | typeof value === "string" && value.length > 0 ? value : undefined; |
| 8 | |
| 9 | export const resourceNameFromOperation = ( |
| 10 | operation: datalabeling.GoogleLongrunningOperation, |
| 11 | ): string | undefined => { |
| 12 | const response = operation.response; |
| 13 | const responseName = stringField(response?.name); |
| 14 | if (responseName !== undefined) { |
| 15 | return responseName; |
| 16 | } |
| 17 | const metadata = operation.metadata; |
| 18 | const metadataName = stringField(metadata?.name); |
| 19 | if (metadataName !== undefined) { |
| 20 | return metadataName; |
| 21 | } |
| 22 | const target = stringField(metadata?.target); |
| 23 | if (target !== undefined) { |
| 24 | return target; |
| 25 | } |
| 26 | return undefined; |
| 27 | }; |
| 28 | |
| 29 | const getOperation = (name: string) => |
| 30 | datalabeling |
| 31 | .getProjectsOperations({ name }) |
| 32 | .pipe(Effect.provide(noRetryLayer)); |
| 33 | |
| 34 | /** |
| 35 | * Wait for an Instruction / feedback-message create operation, then return |
| 36 | * the finished operation (with its `response`). |
| 37 | */ |
| 38 | export const waitForOperation = ( |
| 39 | operation: datalabeling.GoogleLongrunningOperation, |
| 40 | ) => |
| 41 | Effect.gen(function* () { |
| 42 | yield* waitForGcpOperation(operation, getOperation, { |
| 43 | budget: "10 minutes", |
| 44 | }); |
| 45 | if (operation.done === true || !operation.name) return operation; |
| 46 | return yield* getOperation(operation.name); |
| 47 | }); |