The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import * as Context from "effect/Context"; |
| 2 | import * as Effect from "effect/Effect"; |
| 3 | import * as Effectable from "effect/Effectable"; |
| 4 | |
| 5 | export type EffectClass<Shape, A, Err = never, Req = never> = Effect.Effect<A, Err, Req> & { |
| 6 | new (_: never): Shape; |
| 7 | }; |
| 8 | |
| 9 | export const effectClass: { |
| 10 | <A, Err = never, Req = never>(impl: Effect.Effect<A, Err, Req>): EffectClass<A, A, Err, Req>; |
| 11 | <Shape>(): <A, Err = never, Req = never>( |
| 12 | impl: Effect.Effect<A, Err, Req>, |
| 13 | ) => EffectClass<Shape, A, Err, Req>; |
| 14 | } = ((impl?: any) => |
| 15 | impl === undefined |
| 16 | ? (innerImpl: any) => effectClass(innerImpl) |
| 17 | : (Object.assign( |
| 18 | class {}, |
| 19 | // Spreading the Effect prototype onto the class (static side) makes the |
| 20 | // class itself a real Effect — `Effect.isEffect(X)` is true, so |
| 21 | // `Effect.all([X])` / `Effect.forEach` work — and subclasses |
| 22 | // (`class Y extends effectClass(impl)`) inherit the protocol statically. |
| 23 | Effectable.Prototype({ |
| 24 | label: "alchemy/EffectClass", |
| 25 | evaluate: () => impl, |
| 26 | }), |
| 27 | ) as unknown as EffectClass<any, any, any, any>)) as any; |
| 28 | |
| 29 | export const taggedFunction = < |
| 30 | Tag extends Context.ServiceClass<any, any, any>, |
| 31 | Fn extends (...args: any[]) => any, |
| 32 | >( |
| 33 | tag: Tag, |
| 34 | fn: Fn, |
| 35 | ): Tag & Fn => { |
| 36 | // The Proxy below forwards every Effect-protocol key to `tag` (already an |
| 37 | // Effect), so `asEffect`/`[Symbol.iterator]`/`pipe` need no explicit |
| 38 | // override — only `toString` diverges. |
| 39 | const overrides = { |
| 40 | toString: () => `${tag.toString()}.${fn.name}`, |
| 41 | }; |
| 42 | |
| 43 | return new Proxy(fn, { |
| 44 | get: (target, prop, receiver) => |
| 45 | Reflect.has(overrides, prop) |
| 46 | ? Reflect.get(overrides, prop, receiver) |
| 47 | : Reflect.has(target, prop) |
| 48 | ? Reflect.get(target, prop, receiver) |
| 49 | : Reflect.get(tag as object, prop, tag), |
| 50 | has: (target, prop) => |
| 51 | Reflect.has(overrides, prop) || Reflect.has(target, prop) || Reflect.has(tag as object, prop), |
| 52 | }) as Tag & Fn; |
| 53 | }; |
| 54 | |
| 55 | export const isYieldableEffect = ( |
| 56 | value: unknown, |
| 57 | ): value is Effect.Effect<unknown, unknown, unknown> => |
| 58 | Effect.isEffect(value) && |
| 59 | typeof (value as any as { [Symbol.iterator]?: unknown })[Symbol.iterator] === "function"; |
| 60 | |
| 61 | export type YieldableEffectLike<A = unknown, E = unknown, R = unknown> = |
| 62 | | Effect.Effect<A, E, R> |
| 63 | | { |
| 64 | asEffect: () => Effect.Effect<A, E, R>; |
| 65 | [Symbol.iterator]: () => Iterator<unknown>; |
| 66 | }; |
| 67 | |
| 68 | export const isEffectClassLike = (value: unknown): value is YieldableEffectLike => |
| 69 | typeof value === "function" && typeof (value as { asEffect?: unknown }).asEffect === "function"; |
| 70 | |
| 71 | export const isYieldableEffectLike = (value: unknown): value is YieldableEffectLike => |
| 72 | (isYieldableEffect(value) || isEffectClassLike(value)) && !("~alchemy/Kind" in value); |
| 73 | |
| 74 | export type UnwrapEffect<T> = T extends Effect.Effect<infer A, any, any> ? A : T; |
| 75 | |
| 76 | export type ToEffectInterface<T> = { |
| 77 | raw: T; |
| 78 | } & { |
| 79 | [K in keyof T]: T[K] extends (...args: any[]) => any |
| 80 | ? (...args: Parameters<T[K]>) => Effect.Effect<Awaited<ReturnType<T[K]>>> |
| 81 | : T[K]; |
| 82 | }; |
| 83 | |
| 84 | export const toEffectInterface = <T extends object>(raw: T) => |
| 85 | ({ |
| 86 | raw, |
| 87 | ...Object.fromEntries( |
| 88 | Object.entries(raw).map(([key, value]) => [ |
| 89 | key, |
| 90 | typeof value === "function" |
| 91 | ? (...args: any[]) => Effect.tryPromise(async () => value(...args)) |
| 92 | : value, |
| 93 | ]), |
| 94 | ), |
| 95 | }) as ToEffectInterface<T>; |