@devries-cameron20 / t3code Mirror · read-only
Fork 0

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, 04:45 UTC

main
ts 60 lines 2.42 KB
Raw
1 import type * as Containers from "@distilled.cloud/cloudflare/containers";
2 import type { ScopedPlanStatusSession } from "alchemy/Report";
3 import * as Clock from "effect/Clock";
4 import * as Duration from "effect/Duration";
5 import * as Effect from "effect/Effect";
6 import * as Schedule from "effect/Schedule";
7 import {
8 ContainerImagePreparationError,
9 imagePreparationTimeoutMillis,
10 } from "./ContainerConfiguration.ts";
11
12 /** Wait for Cloudflare preparation, with an interruptible deadline including API calls. */
13 export const waitForContainerImage = <E, R>(options: {
14 image: string;
15 name: string;
16 timeout?: Duration.Input;
17 prepare: Effect.Effect<Containers.PrepareContainerImageResponse, E, R>;
18 session: Pick<ScopedPlanStatusSession, "note">;
19 }) =>
20 Effect.gen(function* () {
21 const timeout = yield* imagePreparationTimeoutMillis(options.timeout);
22 const started = yield* Clock.currentTimeMillis;
23 const elapsed = (now: number) => {
24 const seconds = Math.floor((now - started) / 1000);
25 return `${Math.floor(seconds / 60)}m ${seconds % 60}s`;
26 };
27 yield* options.session.note(`Preparing ${options.name} image… 0m 0s`, { kind: "status" });
28 yield* Effect.gen(function* () {
29 const result = yield* options.prepare;
30 const time = elapsed(yield* Clock.currentTimeMillis);
31 if (result.status === "ready") {
32 yield* options.session.note(`Prepared ${options.name} image (${time}).`, {
33 kind: "status",
34 });
35 return true;
36 }
37 if (result.status === "error") {
38 return yield* new ContainerImagePreparationError({
39 image: options.image,
40 status: "error",
41 message: result.reason ?? `Cloudflare could not prepare image '${options.name}'.`,
42 });
43 }
44 yield* options.session.note(`Preparing ${options.name} image… ${time}`, { kind: "status" });
45 return false;
46 }).pipe(
47 Effect.repeat({ schedule: Schedule.spaced("5 seconds"), until: (ready) => ready }),
48 Effect.timeoutOrElse({
49 duration: timeout,
50 orElse: () =>
51 Effect.fail(
52 new ContainerImagePreparationError({
53 image: options.image,
54 status: "pending",
55 message: `Image '${options.name}' (${options.image}) was not ready within ${timeout / 60000} minutes. Deploy again to resume from the published image, or increase imagePreparationTimeout.`,
56 }),
57 ),
58 }),
59 );
60 });