The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 21, 06:00 UTC
| 1 | import * as translate from "@distilled.cloud/gcp/translate_v3"; |
| 2 | import type * as Duration from "effect/Duration"; |
| 3 | import * as Effect from "effect/Effect"; |
| 4 | import * as Predicate from "effect/Predicate"; |
| 5 | import { OperationFailed, waitForOperation as waitForGcpOperation } from "../Operation.ts"; |
| 6 | |
| 7 | /** |
| 8 | * Translate reports some failures (e.g. a model created from an empty |
| 9 | * dataset) as `done: true` with the error in `metadata.error` and no |
| 10 | * top-level `error`. |
| 11 | */ |
| 12 | const metadataFailure = (operation: translate.Operation) => { |
| 13 | const error = operation.metadata?.error; |
| 14 | if (!Predicate.isObject(error)) return undefined; |
| 15 | const code = |
| 16 | Predicate.hasProperty(error, "code") && Predicate.isNumber(error.code) ? error.code : undefined; |
| 17 | const message = |
| 18 | Predicate.hasProperty(error, "message") && Predicate.isString(error.message) |
| 19 | ? error.message |
| 20 | : "operation failed"; |
| 21 | return new OperationFailed({ |
| 22 | operation: operation.name ?? "", |
| 23 | code, |
| 24 | reason: undefined, |
| 25 | message, |
| 26 | }); |
| 27 | }; |
| 28 | |
| 29 | /** |
| 30 | * Wait for a Translate long-running operation, then re-read it so the |
| 31 | * returned operation carries its `response`. Model create/delete and |
| 32 | * glossary create/delete are LROs; Adaptive MT datasets and glossary |
| 33 | * entries are synchronous. ALREADY_EXISTS (code 6) counts as success |
| 34 | * (create race); with `notFoundOk`, so does NOT_FOUND (code 5, delete race) |
| 35 | * and an operation that is already gone. |
| 36 | */ |
| 37 | export const waitForOperation = ( |
| 38 | operation: translate.Operation, |
| 39 | options?: { notFoundOk?: boolean; budget?: Duration.Input }, |
| 40 | ) => |
| 41 | waitForGcpOperation(operation, (name) => translate.getProjectsLocationsOperations({ name }), { |
| 42 | budget: options?.budget ?? "10 minutes", |
| 43 | }).pipe( |
| 44 | Effect.flatMap(() => |
| 45 | operation.name |
| 46 | ? translate.getProjectsLocationsOperations({ name: operation.name }) |
| 47 | : Effect.succeed(operation), |
| 48 | ), |
| 49 | Effect.flatMap((done) => { |
| 50 | const failed = metadataFailure(done); |
| 51 | return failed ? Effect.fail(failed) : Effect.succeed(done); |
| 52 | }), |
| 53 | Effect.catchIf( |
| 54 | (error) => |
| 55 | (error._tag === "GCP.OperationFailed" && |
| 56 | (error.code === 6 || (options?.notFoundOk === true && error.code === 5))) || |
| 57 | (options?.notFoundOk === true && error._tag === "NotFound"), |
| 58 | () => Effect.succeed(operation), |
| 59 | ), |
| 60 | ); |