The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import * as EffectContext from "effect/Context"; |
| 2 | import * as Effect from "effect/Effect"; |
| 3 | import * as Option from "effect/Option"; |
| 4 | import * as Schema from "effect/Schema"; |
| 5 | import { DefinitionError, UnknownRequestContext } from "./DefinitionError.ts"; |
| 6 | |
| 7 | /** The JSON form of a request's context, by service key. */ |
| 8 | export type Json = Readonly<Record<string, unknown>>; |
| 9 | |
| 10 | interface Registered { |
| 11 | readonly tag: EffectContext.Key<unknown, unknown>; |
| 12 | readonly encode: (value: unknown) => unknown; |
| 13 | readonly decode: (json: unknown) => unknown; |
| 14 | } |
| 15 | |
| 16 | const registry = new Map<string, Registered>(); |
| 17 | |
| 18 | /** |
| 19 | * The class type returned by {@link make}. |
| 20 | */ |
| 21 | export interface RequestContextClass< |
| 22 | Self, |
| 23 | Key extends string, |
| 24 | S extends Schema.Top, |
| 25 | > extends EffectContext.ServiceClass<Self, Key, S["Type"]> { |
| 26 | /** Encodes the value so it can travel with a request into the domain. */ |
| 27 | readonly schema: S; |
| 28 | } |
| 29 | |
| 30 | /** |
| 31 | * A service middleware provides to a request, such as the current actor. It |
| 32 | * belongs to the request: a command carries it to its aggregate's `decide`, |
| 33 | * and a read carries it to `validate`. Both may run elsewhere, such as in a |
| 34 | * Durable Object, so it has a schema. Policies provide it themselves when |
| 35 | * they send commands. |
| 36 | * |
| 37 | * ### Declaring a request context |
| 38 | * **Example:** The current actor |
| 39 | * ```typescript |
| 40 | * import * as Domain from "@alchemy.run/domain"; |
| 41 | * import * as Schema from "effect/Schema"; |
| 42 | * |
| 43 | * export class CurrentActor extends Domain.RequestContext<CurrentActor>()( |
| 44 | * "Bank/CurrentActor", |
| 45 | * Schema.Struct({ customerId: Schema.String }), |
| 46 | * ) {} |
| 47 | * ``` |
| 48 | * |
| 49 | * ### Providing it from middleware |
| 50 | * **Example:** A session middleware that provides it |
| 51 | * ```typescript |
| 52 | * import * as RpcMiddleware from "effect/rpc/RpcMiddleware"; |
| 53 | * import * as Schema from "effect/Schema"; |
| 54 | * import { CurrentActor } from "./CurrentActor.ts"; |
| 55 | * |
| 56 | * export class Unauthorized extends Schema.TaggedError<Unauthorized>()("Unauthorized", {}) {} |
| 57 | * |
| 58 | * export class Session extends RpcMiddleware.Service<Session, { provides: CurrentActor }>()( |
| 59 | * "Bank/Session", |
| 60 | * { error: Unauthorized }, |
| 61 | * ) {} |
| 62 | * ``` |
| 63 | * |
| 64 | * @primitive |
| 65 | */ |
| 66 | export const make = |
| 67 | <Self>() => |
| 68 | <const Key extends string, S extends Schema.Top>( |
| 69 | key: Key, |
| 70 | schema: S, |
| 71 | ): RequestContextClass<Self, Key, S> => { |
| 72 | const Tag = EffectContext.Service<Self, S["Type"]>()(key); |
| 73 | const codec = Schema.toCodecJson( |
| 74 | schema as unknown as Schema.Codec<unknown, unknown>, |
| 75 | ) as Schema.Codec<unknown, unknown>; |
| 76 | registry.set(key, { |
| 77 | tag: Tag as unknown as EffectContext.Key<unknown, unknown>, |
| 78 | // oxlint-disable-next-line effecttsgo/schema-sync -- context codecs are sync by contract |
| 79 | encode: Schema.encodeUnknownSync(codec), |
| 80 | // oxlint-disable-next-line effecttsgo/schema-sync -- context codecs are sync by contract |
| 81 | decode: Schema.decodeUnknownSync(codec), |
| 82 | }); |
| 83 | return class extends Tag { |
| 84 | static readonly schema: S = schema; |
| 85 | } as unknown as RequestContextClass<Self, Key, S>; |
| 86 | }; |
| 87 | |
| 88 | /** |
| 89 | * Encode every request context service present in the current fiber. |
| 90 | * |
| 91 | * @internal |
| 92 | */ |
| 93 | export const capture: Effect.Effect<Json> = Effect.gen(function* () { |
| 94 | const captured: Record<string, unknown> = {}; |
| 95 | for (const [key, entry] of registry) { |
| 96 | const value = yield* Effect.serviceOption(entry.tag); |
| 97 | if (Option.isSome(value)) captured[key] = entry.encode(value.value); |
| 98 | } |
| 99 | return captured; |
| 100 | }); |
| 101 | |
| 102 | /** |
| 103 | * Decode a request's context back into services, for `decide` or `validate`. |
| 104 | * |
| 105 | * @internal |
| 106 | */ |
| 107 | export const restore = Effect.fn(function* (json: Json) { |
| 108 | let context = EffectContext.empty() as EffectContext.Context<unknown>; |
| 109 | for (const [key, value] of Object.entries(json)) { |
| 110 | const entry = registry.get(key); |
| 111 | if (!entry) |
| 112 | return yield* Effect.die(new DefinitionError({ reason: new UnknownRequestContext({ key }) })); |
| 113 | context = EffectContext.add(context, entry.tag, entry.decode(value)); |
| 114 | } |
| 115 | return context as EffectContext.Context<never>; |
| 116 | }); |