import * as cci from "@distilled.cloud/gcp/contactcenterinsights_v1";
import type { GcpOpContext } from "@distilled.cloud/gcp/Protocol";
import * as Effect from "effect/Effect";
import * as Schedule from "effect/Schedule";
import { waitForOperation as waitForLongRunningOperation } from "../Operation.ts";
/** Name of the resource a finished create operation produced. */
export const resourceNameFromOperation = (
operation: cci.GoogleLongrunningOperation,
): string | undefined => {
const response = operation.response;
const responseName = response?.name;
if (typeof responseName === "string" && responseName.length > 0) {
return responseName;
}
const metadata = operation.metadata;
const target = metadata?.target;
if (typeof target === "string" && target.length > 0) {
return target;
}
return undefined;
};
/**
* Wait on a Contact Center AI Insights long-running operation. Issue-model deploy/undeploy can take tens of minutes.
* `ALREADY_EXISTS` (a create race) counts as success; so does `NOT_FOUND`
* when `notFoundOk` (deletes). Returns the final operation.
*/
export const waitForOperation = (
operation: cci.GoogleLongrunningOperation,
options?: { notFoundOk?: boolean },
) =>
Effect.suspend(() => {
let latest = operation;
return waitForLongRunningOperation(
operation,
(name) => {
const get = cci.getProjectsLocationsOperations({ name }).pipe(
Effect.tap((current) =>
Effect.sync(() => {
latest = current;
}),
),
);
const observe: Effect.Effect<
cci.GoogleLongrunningOperation,
cci.GetProjectsLocationsOperationsError,
GcpOpContext
> =
options?.notFoundOk === true
? get.pipe(
Effect.catchTag("NotFound", () =>
Effect.succeed<cci.GoogleLongrunningOperation>({
name,
done: true,
}),
),
)
: get.pipe(
// A just-returned operation can briefly 404 on read.
Effect.retry({
while: (error) => error._tag === "NotFound",
times: 5,
schedule: Schedule.exponential("250 millis"),
}),
);
return observe;
},
{ budget: "30 minutes", interval: "10 seconds" },
).pipe(
Effect.map(() => latest),
// google.rpc.Code ALREADY_EXISTS = 6, NOT_FOUND = 5.
Effect.catchTag("GCP.OperationFailed", (error) =>
error.code === 6 || (options?.notFoundOk === true && error.code === 5)
? Effect.succeed(latest)
: Effect.fail(error),
),
);
});