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
| 1 | import * as Context from "effect/Context"; |
| 2 | import * as Effect from "effect/Effect"; |
| 3 | import type * as Scope from "effect/Scope"; |
| 4 | import type { Simplify } from "effect/Types"; |
| 5 | import { PluginContext, type ConfigHook } from "./PluginContext.ts"; |
| 6 | import type { RuntimeError } from "./RuntimeError.shared.ts"; |
| 7 | import type * as WorkerdConfig from "./workerd/Config.ts"; |
| 8 | import type * as Workerd from "./workerd/Workerd.ts"; |
| 9 | |
| 10 | export type Plugin<Api = never> = WithApi< |
| 11 | PluginConfig & { readonly defer?: Effect.Effect<PluginConfig, RuntimeError> }, |
| 12 | Api |
| 13 | >; |
| 14 | |
| 15 | type WithApi<T, Api> = [Api] extends [never] ? Simplify<T> : Simplify<T & { readonly api: Api }>; |
| 16 | |
| 17 | export interface PluginConfig { |
| 18 | readonly services?: Array<WorkerdConfig.Service>; |
| 19 | readonly sockets?: Array<WorkerdConfig.Socket>; |
| 20 | readonly extensions?: Array<WorkerdConfig.Extension>; |
| 21 | readonly middlewares?: Array<Middleware>; |
| 22 | /** |
| 23 | * Worker-level fields merged into the user worker's configuration (e.g. |
| 24 | * `cacheApiOutbound`). Merged in plugin order; `RuntimeWorker.unsafe` still |
| 25 | * takes precedence. |
| 26 | */ |
| 27 | readonly userWorker?: Partial<WorkerdConfig.Worker>; |
| 28 | /** |
| 29 | * Initializes each workerd process, including replacements after a crash. |
| 30 | * Resources and tasks acquired here are released before the next process |
| 31 | * starts. Replay any state held only inside workerd from this hook. |
| 32 | */ |
| 33 | readonly start?: (ports: Workerd.WorkerdPorts) => Effect.Effect<void, RuntimeError, Scope.Scope>; |
| 34 | } |
| 35 | |
| 36 | export interface Middleware { |
| 37 | name: string; |
| 38 | worker: WorkerdConfig.Worker; |
| 39 | upstreamBindingName: string; |
| 40 | order?: number; |
| 41 | } |
| 42 | |
| 43 | export type PluginBuilder<Api = never> = |
| 44 | | Plugin<Api> |
| 45 | | Effect.Effect<Plugin<Api>, RuntimeError, PluginContext>; |
| 46 | |
| 47 | export type PluginIdentifier<T extends string = string> = `cloudflare-runtime/plugin/${T}`; |
| 48 | |
| 49 | /** |
| 50 | * When true, simulator services backed by Durable Objects (KV, R2, ...) |
| 51 | * handle control operations (fake timers, storage inspection) sent via their |
| 52 | * reserved control-op header. |
| 53 | * |
| 54 | * **Only intended for tests** (provide with |
| 55 | * `Layer.succeed(UnsafeEnableControlEndpoints, true)`): anyone able to reach |
| 56 | * such a service can then run arbitrary SQL queries and read arbitrary blobs |
| 57 | * across every namespace/bucket in this runtime. |
| 58 | */ |
| 59 | export const UnsafeEnableControlEndpoints = Context.Reference( |
| 60 | "cloudflare-runtime/plugin/UnsafeEnableControlEndpoints", |
| 61 | { defaultValue: () => false }, |
| 62 | ); |
| 63 | |
| 64 | export type PluginService< |
| 65 | Self, |
| 66 | Identifier extends PluginIdentifier, |
| 67 | Api = never, |
| 68 | > = Context.ServiceClass<Self, Identifier, PluginBuilder<Api>>; |
| 69 | |
| 70 | export const Service = |
| 71 | <Self, Api = never>() => |
| 72 | <Identifier extends PluginIdentifier>( |
| 73 | identifier: Identifier, |
| 74 | ): Context.ServiceClass<Self, Identifier, PluginBuilder<Api>> => |
| 75 | Context.Service<Self, PluginBuilder<Api>>()(identifier); |
| 76 | |
| 77 | export const use = <Self, Identifier extends PluginIdentifier, Api, A, E, R>( |
| 78 | plugin: PluginService<Self, Identifier, Api>, |
| 79 | f: (plugin: Plugin<Api>) => Effect.Effect<A, E, R>, |
| 80 | ): ConfigHook<A, E, R | Self> => |
| 81 | PluginContext.use((context) => context.get(plugin).pipe(Effect.flatMap(f))); |
| 82 | |
| 83 | export const useSync = <Self, Identifier extends PluginIdentifier, Api, A>( |
| 84 | plugin: PluginService<Self, Identifier, Api>, |
| 85 | f: (plugin: Plugin<Api>) => A, |
| 86 | ): ConfigHook<A, never, Self> => use(plugin, (plugin) => Effect.succeed(f(plugin))); |