@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, 06:00 UTC

main
ts 200 lines 12 KB
Raw
1 /// <reference types="@cloudflare/workers-types" />
2
3 import type { Output } from "alchemy/Output";
4 // Aliased so the ambient Cloudflare `Rpc` namespace (from
5 // @cloudflare/workers-types, referenced above) stays reachable for
6 // `Rpc.DurableObjectBranded`.
7 import type { Rpc as AlchemyRpc } from "alchemy/Rpc";
8 import type { Pipeline } from "cloudflare:pipelines";
9 import type * as Effect from "effect/Effect";
10 import type { Redacted } from "effect/Redacted";
11 import type * as Stream from "effect/Stream";
12 // NOTE: import the service modules directly rather than `import * as Cloudflare
13 // from "../index.ts"`. Importing the whole Cloudflare barrel here creates a
14 // circular re-export when the barrel does `export * from "./Workers/index.ts"`
15 // (the barrel must enumerate this module, which imports the barrel back), which
16 // collapses inference of the large `providers()` collection to `unknown`.
17 import type * as AI from "../AI/index.ts";
18 import type * as AnalyticsEngine from "../AnalyticsEngine/index.ts";
19 import type * as ArtifactsNs from "../Artifacts/index.ts";
20 import type { Container } from "../Containers/Container.ts";
21 import type * as D1 from "../D1/index.ts";
22 import type * as Email from "../Email/index.ts";
23 import type * as FlagshipNs from "../Flagship/index.ts";
24 import type * as HyperdriveNs from "../Hyperdrive/index.ts";
25 import type * as ImagesNs from "../Images/index.ts";
26 import type * as K2Ns from "../K2/index.ts";
27 import type { K2StreamBinding } from "../K2/WriteStreamBinding.ts";
28 import type * as KV from "../KV/index.ts";
29 import type { MtlsCertificate } from "../MtlsCertificate/MtlsCertificate.ts";
30 import type * as PipelinesNs from "../Pipelines/index.ts";
31 import type * as Queues from "../Queues/index.ts";
32 import type * as R2 from "../R2/index.ts";
33 import type * as StreamNs from "../Stream/index.ts";
34 import type { VpcService } from "../VpcService/VpcService.ts";
35 import type { VpcServiceLookup } from "../VpcService/VpcServiceLookup.ts";
36 import type { DispatchNamespace as DispatchNamespaceResource } from "../WorkersForPlatforms/DispatchNamespace.ts";
37 import type { WorkflowBinding, WorkflowLike } from "../Workflows/Workflow.ts";
38 import type { AIBinding } from "./AIBinding.ts";
39 import type { Assets } from "./Assets.ts";
40 import type * as WorkerOnlyBinding from "./Binding.ts";
41 import type { BrowserBinding } from "./BrowserBinding.ts";
42 import type { DurableObjectLike } from "./DurableObject.ts";
43 import type { RateLimitBinding } from "./RateLimitBinding.ts";
44 import type { RpcErrorEnvelope, RpcStreamEnvelope } from "./Rpc.ts";
45 import type { SecretKeyBinding } from "./SecretKeyBinding.ts";
46 import type { VersionMetadataBinding } from "./VersionMetadataBinding.ts";
47 import type { Self, Worker } from "./Worker.ts";
48 import type { WorkerEntrypointBinding } from "./WorkerEntrypoint.ts";
49 import type { WorkerLoader as WorkerLoaderResource } from "./WorkerLoader.ts";
50
51 export type InferEnv<W> =
52 W extends Effect.Effect<infer A, infer _E, infer _R>
53 ? InferEnv<A>
54 : W extends Worker<any>
55 ? InferEnv<Exclude<W["Props"]["env"], undefined>>
56 : W extends { readonly "~alchemy/WorkerEnv": infer Env }
57 ? InferEnv<Env>
58 : {
59 [k in keyof W]: GetBindingType<W[k]>;
60 };
61
62 export type GetBindingType<T> =
63 // A named-entrypoint service binding (`Cloudflare.WorkerEntrypoint`).
64 // Tested first: the marker *contains* a Worker, so the later Worker
65 // branches must never see it.
66 T extends WorkerEntrypointBinding<infer Entrypoint>
67 ? Service<Entrypoint>
68 : // A service binding to this Worker itself (`Workers.Self` /
69 // `Workers.SelfEntrypoint`). Typed as a plain `Fetcher`: carrying the
70 // entrypoint class would make the Worker's type depend on its own
71 // module. `ctx.exports` (see `InferGlobalProps`) is the typed path.
72 T extends Self
73 ? Service
74 : // A Container bound in `env` is a container-backed Durable Object class —
75 // the runtime binding is the class's namespace. Must be tested BEFORE the
76 // generic Effect unwrap: a Container declaration is itself an Effect.
77 T extends Container.Decl<any, any, any, any, infer DOShape>
78 ? DurableObjectNamespace<DOShape & Rpc.DurableObjectBranded>
79 : // A Worker-only binding lifted by `.pipe(Alchemy.remote())` — the brand
80 // carries the underlying binding value type; recurse on it. Must also
81 // be tested BEFORE the generic Effect unwrap (its Effect `A` is the
82 // runtime client, not the binding value).
83 T extends WorkerOnlyBinding.BindingEffect<infer B>
84 ? GetBindingType<B>
85 : // `R2.S3Credentials` is an Effect whose `A` is the Effect-native
86 // accessor; on an async Worker's `env` it arrives as a JSON string
87 // (a `secret_text` when deployed). Tested BEFORE the Effect unwrap.
88 T extends R2.S3Credentials
89 ? string
90 : // `Worker.URL` (an Effect resolving to a deferred string accessor) needs
91 // no case of its own: the generic Effect unwrap below reduces it to
92 // `string` via the fallthrough.
93 T extends Output<infer A, infer _Req> | Effect.Effect<infer A, infer _E, infer _R>
94 ? GetBindingType<A>
95 : T extends FlagshipNs.App
96 ? Flagship
97 : T extends Assets
98 ? Service
99 : T extends AlchemyRpc<infer Shape extends object>
100 ? RpcWireShape<Shape> & Service
101 : T extends D1.Database
102 ? D1Database
103 : T extends R2.Bucket
104 ? R2Bucket
105 : T extends KV.InstantNamespace
106 ? KV.InstantNamespaceBindingClient
107 : T extends KV.Namespace
108 ? KVNamespace
109 : T extends DispatchNamespaceResource
110 ? DispatchNamespace
111 : T extends Queues.Queue
112 ? Queue<unknown>
113 : T extends AI.Gateway
114 ? Ai
115 : T extends AIBinding
116 ? Ai
117 : T extends AI.Search
118 ? AiSearchInstance
119 : T extends AI.SearchNamespace
120 ? AiSearchNamespace
121 : T extends Email.SendEmail
122 ? SendEmail
123 : T extends AnalyticsEngine.Dataset
124 ? AnalyticsEngineDataset
125 : T extends ArtifactsNs.Namespace
126 ? Artifacts
127 : T extends RateLimitBinding
128 ? RateLimit
129 : T extends SecretKeyBinding
130 ? CryptoKey
131 : T extends ImagesNs.ImagesBinding
132 ? ImagesBinding
133 : T extends BrowserBinding
134 ? BrowserRun
135 : // The ambient global `StreamBinding` from
136 // @cloudflare/workers-types (the alchemy binding value
137 // type of the same name is only reachable as
138 // `StreamNs.StreamBinding`).
139 T extends StreamNs.StreamBinding
140 ? StreamBinding
141 : T extends
142 | HyperdriveNs.Connection
143 | HyperdriveNs.ConnectionLookup
144 ? Hyperdrive
145 : T extends VersionMetadataBinding
146 ? WorkerVersionMetadata
147 : T extends WorkerLoaderResource
148 ? WorkerLoader
149 : T extends
150 | WorkflowLike<infer Params>
151 | WorkflowBinding<infer Params>
152 ? Workflow<Params>
153 : T extends DurableObjectLike
154 ? DurableObjectNamespace<
155 Exclude<T["Shape"], undefined>
156 >
157 : T extends
158 | VpcService
159 | VpcServiceLookup
160 | MtlsCertificate
161 ? Fetcher
162 : T extends
163 | PipelinesNs.Stream
164 | PipelinesNs.LegacyPipeline
165 ? Pipeline
166 : T extends K2Ns.Stream
167 ? K2StreamBinding
168 : T extends Redacted<any>
169 ? // redacteds are always stored as secret_text, so are always string
170 // we JSON.stringify when not a Redacted<string>
171 string
172 : T;
173
174 /**
175 * Cloudflare service-binding wire shape for an Effect-native Worker.
176 *
177 * Effect/Stream return values are encoded as envelopes on the wire (see
178 * `RpcErrorEnvelope`, `RpcStreamEnvelope`), so the mapped types reflect what
179 * the raw binding actually resolves to. `fetch` is dropped from the user
180 * shape and re-introduced via `Service` so callers get the standard
181 * `(input, init?) => Promise<Response>` signature.
182 *
183 * Use {@link toRpcAsync} to wrap a binding into a Promise<T>-flavored view
184 * where envelopes are decoded for you.
185 */
186 export type RpcWireShape<Shape> = {
187 [K in keyof Shape as K extends "fetch" ? never : K]: Shape[K] extends (
188 ...args: infer A
189 ) => Effect.Effect<infer T, any, any>
190 ? (...args: A) => Promise<T | RpcErrorEnvelope>
191 : Shape[K] extends (...args: infer A) => Stream.Stream<any, any, any>
192 ? (...args: A) => Promise<RpcStreamEnvelope | RpcErrorEnvelope>
193 : Shape[K] extends Effect.Effect<infer T, any, any>
194 ? Promise<T | RpcErrorEnvelope>
195 : Shape[K] extends Stream.Stream<any, any, any>
196 ? Promise<RpcStreamEnvelope | RpcErrorEnvelope>
197 : Shape[K] extends (...args: infer A) => infer R
198 ? (...args: A) => Promise<Awaited<R>>
199 : Promise<Shape[K]>;
200 };