@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 75 lines 2.89 KB
Raw
1 import type * as cf from "@cloudflare/workers-types";
2 import type { RuntimeContext } from "alchemy/RuntimeContext";
3 import * as Effect from "effect/Effect";
4 import * as Layer from "effect/Layer";
5 import { Worker, WorkerEnvironment } from "../Workers/Worker.ts";
6 import type { App } from "./App.ts";
7 import { FlagshipError, ReadFlags, type ReadFlagsClient } from "./ReadFlags.ts";
8
9 export const ReadFlagsBinding = Layer.effect(
10 ReadFlags,
11 Effect.gen(function* () {
12 const env = yield* WorkerEnvironment;
13 const host = yield* Worker;
14
15 return Effect.fn(function* (app: App) {
16 if (!globalThis.__ALCHEMY_RUNTIME__) {
17 yield* host.bind(app.LogicalId, {
18 bindings: [
19 {
20 type: "flagship",
21 name: app.LogicalId,
22 appId: app.appId,
23 },
24 ],
25 });
26 }
27 const raw: Effect.Effect<cf.Flagship, never, RuntimeContext> = Effect.sync(
28 () => (env as Record<string, cf.Flagship>)[app.LogicalId]!,
29 );
30 return makeFlagshipClient(raw);
31 });
32 }),
33 );
34
35 /** An Effect produced by a {@link ReadFlagsClient} operation. */
36 type FlagshipEffect<A> = Effect.Effect<A, FlagshipError, RuntimeContext>;
37
38 const tryPromise = <T>(fn: () => Promise<T>): Effect.Effect<T, FlagshipError> =>
39 Effect.tryPromise({
40 try: fn,
41 catch: (error) =>
42 new FlagshipError({
43 message: error instanceof Error ? error.message : "Unknown Flagship error",
44 cause: error,
45 }),
46 });
47
48 /** @internal */
49 export const makeFlagshipClient = (
50 raw: Effect.Effect<cf.Flagship, never, RuntimeContext>,
51 ): ReadFlagsClient => {
52 const call = <T>(fn: (binding: cf.Flagship) => Promise<T>): FlagshipEffect<T> =>
53 raw.pipe(Effect.flatMap((binding) => tryPromise(() => fn(binding))));
54
55 return {
56 raw,
57 get: (flagKey, defaultValue, context) => call((b) => b.get(flagKey, defaultValue, context)),
58 getBooleanValue: (flagKey, defaultValue, context) =>
59 call((b) => b.getBooleanValue(flagKey, defaultValue, context)),
60 getStringValue: (flagKey, defaultValue, context) =>
61 call((b) => b.getStringValue(flagKey, defaultValue, context)),
62 getNumberValue: (flagKey, defaultValue, context) =>
63 call((b) => b.getNumberValue(flagKey, defaultValue, context)),
64 getObjectValue: (flagKey, defaultValue, context) =>
65 call((b) => b.getObjectValue(flagKey, defaultValue, context)),
66 getBooleanDetails: (flagKey, defaultValue, context) =>
67 call((b) => b.getBooleanDetails(flagKey, defaultValue, context)),
68 getStringDetails: (flagKey, defaultValue, context) =>
69 call((b) => b.getStringDetails(flagKey, defaultValue, context)),
70 getNumberDetails: (flagKey, defaultValue, context) =>
71 call((b) => b.getNumberDetails(flagKey, defaultValue, context)),
72 getObjectDetails: (flagKey, defaultValue, context) =>
73 call((b) => b.getObjectDetails(flagKey, defaultValue, context)),
74 } satisfies ReadFlagsClient;
75 };