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 type { RuntimeContext } from "alchemy/RuntimeContext"; |
| 2 | import { Self } from "alchemy/Self"; |
| 3 | import * as Effect from "effect/Effect"; |
| 4 | import type * as HttpClient from "effect/http/HttpClient"; |
| 5 | import type * as Redacted from "effect/Redacted"; |
| 6 | import { AccountApiToken } from "../ApiToken/AccountApiToken.ts"; |
| 7 | import type { PermissionGroupRef } from "../ApiToken/Common.ts"; |
| 8 | import { CloudflareEnvironment } from "../CloudflareEnvironment.ts"; |
| 9 | import type { Credentials } from "../Credentials.ts"; |
| 10 | import { requireLiveResource } from "../HttpBindingDev.ts"; |
| 11 | import type { Queue } from "./Queue.ts"; |
| 12 | import { SendError } from "./QueueTypes.ts"; |
| 13 | |
| 14 | /** |
| 15 | * Injectable auth used by the Queue HTTP client builder. Both the |
| 16 | * scoped-token HTTP variant ({@link makeWriteQueueHttpClient}) and the |
| 17 | * current-credentials Local variant build this so they share the exact |
| 18 | * same request path — only the way credentials reach the SDK op differs. |
| 19 | * |
| 20 | * - `authorize` runs a raw distilled op (which needs |
| 21 | * `Credentials | HttpClient`) and discharges those requirements down to |
| 22 | * {@link RuntimeContext}. The HTTP variant provides a minted token; the |
| 23 | * Local variant provides the ambient current-credentials context. |
| 24 | * - `accountId` resolves the Cloudflare account the queue lives in. |
| 25 | */ |
| 26 | export interface QueueAuth { |
| 27 | authorize: <A, E>( |
| 28 | eff: Effect.Effect<A, E, Credentials | HttpClient.HttpClient>, |
| 29 | ) => Effect.Effect<A, E, RuntimeContext>; |
| 30 | accountId: Effect.Effect<string>; |
| 31 | } |
| 32 | |
| 33 | /** |
| 34 | * Shared scaffolding for the HTTP-backed Queue services. |
| 35 | * |
| 36 | * Creates a scoped {@link AccountApiToken}, binds its `value` / |
| 37 | * `accountId` into the host Worker at deploy time, then delegates to |
| 38 | * `makeClient` with the bound token and the queue's `queueId`. |
| 39 | */ |
| 40 | export const makeHttpQueueBinding = <Client>(options: { |
| 41 | permissionGroups: PermissionGroup[]; |
| 42 | makeClient: (token: HttpToken, queueId: Effect.Effect<string>) => Client; |
| 43 | }) => |
| 44 | Effect.gen(function* () { |
| 45 | const Token = yield* AccountApiToken; |
| 46 | const self = yield* Self; |
| 47 | |
| 48 | return Effect.fn(function* (queue: Queue) { |
| 49 | if (!globalThis.__ALCHEMY_RUNTIME__) yield* requireLiveResource(queue); |
| 50 | const token = yield* Token(`${self.LogicalId}Token`); |
| 51 | if (!globalThis.__ALCHEMY_RUNTIME__) { |
| 52 | const { accountId } = yield* CloudflareEnvironment.current; |
| 53 | yield* token.bind`${queue.LogicalId}`({ |
| 54 | policies: [ |
| 55 | { |
| 56 | effect: "allow", |
| 57 | permissionGroups: options.permissionGroups, |
| 58 | resources: { |
| 59 | [`com.cloudflare.api.account.${accountId}`]: "*", |
| 60 | }, |
| 61 | }, |
| 62 | ], |
| 63 | }); |
| 64 | } |
| 65 | const bound = { |
| 66 | value: yield* token.value, |
| 67 | accountId: yield* token.accountId, |
| 68 | } satisfies HttpToken; |
| 69 | const queueId = yield* queue.queueId; |
| 70 | return options.makeClient(bound, queueId); |
| 71 | }); |
| 72 | }); |
| 73 | |
| 74 | /** Resolve the account and queue id once per operation. */ |
| 75 | export const makeQueueHttpScope = ( |
| 76 | auth: QueueAuth, |
| 77 | queueId: Effect.Effect<string>, |
| 78 | ): Effect.Effect<HttpScope> => |
| 79 | Effect.gen(function* () { |
| 80 | const accountId = yield* auth.accountId; |
| 81 | const id = yield* queueId; |
| 82 | return { accountId, queueId: id }; |
| 83 | }); |
| 84 | |
| 85 | export const toQueueSendError = (error: unknown): SendError => |
| 86 | new SendError({ |
| 87 | message: |
| 88 | typeof error === "object" && error !== null && "message" in error |
| 89 | ? String((error as { message: unknown }).message) |
| 90 | : "Unknown queue error", |
| 91 | cause: error, |
| 92 | }); |
| 93 | |
| 94 | export interface HttpToken { |
| 95 | value: Effect.Effect<Redacted.Redacted<string>>; |
| 96 | accountId: Effect.Effect<string>; |
| 97 | } |
| 98 | |
| 99 | export interface HttpScope { |
| 100 | accountId: string; |
| 101 | queueId: string; |
| 102 | } |
| 103 | |
| 104 | const QUEUE_HTTP_PERMISSION_GROUPS: PermissionGroupRef[] = ["Queues Read", "Queues Write"]; |
| 105 | |
| 106 | type PermissionGroup = (typeof QUEUE_HTTP_PERMISSION_GROUPS)[number]; |