@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, 04:45 UTC

main
ts 76 lines 2.78 KB
Raw
1 import type { ProviderMode } from "alchemy/ProviderMode";
2 import type { RuntimeContext } from "alchemy/RuntimeContext";
3 import { Self } from "alchemy/Self";
4 import * as Effect from "effect/Effect";
5 import type * as HttpClient from "effect/http/HttpClient";
6 import { AccountApiToken } from "../ApiToken/AccountApiToken.ts";
7 import { CloudflareEnvironment } from "../CloudflareEnvironment.ts";
8 import type { Credentials } from "../Credentials.ts";
9 import { requireLiveResource } from "../HttpBindingDev.ts";
10 import { authorizeWith } from "../HttpClientUtils.ts";
11
12 /**
13 * Shared scaffolding for the HTTP-backed K2 services. Internal — not
14 * exported from the K2 barrel.
15 */
16
17 /**
18 * Injectable auth for the K2 HTTP clients. The scoped-token variant
19 * (`*Http`) and the current-credentials variant (`*Local`) share the same
20 * request path and differ only in how credentials reach the SDK operation.
21 * K2 data-plane operations are addressed by stream id, so no account id is
22 * needed.
23 */
24 export interface K2Auth {
25 authorize: <A, E>(
26 eff: Effect.Effect<A, E, Credentials | HttpClient.HttpClient>,
27 ) => Effect.Effect<A, E, RuntimeContext>;
28 }
29
30 type K2PermissionGroup = "K2 Produce" | "K2 Consume";
31
32 /**
33 * Mint (or extend) the host's scoped {@link AccountApiToken} with the given
34 * K2 permission group, bind it into the host at deploy time, and return
35 * the {@link K2Auth} that authorizes requests with it.
36 */
37 export const makeK2HttpAuth = Effect.gen(function* () {
38 const Token = yield* AccountApiToken;
39 const self = yield* Self;
40
41 return Effect.fn(function* (
42 resource: { readonly Type: string; readonly LogicalId: string; readonly Mode?: ProviderMode },
43 permissionGroup: K2PermissionGroup,
44 ) {
45 if (!globalThis.__ALCHEMY_RUNTIME__) yield* requireLiveResource(resource);
46 const token = yield* Token(`${self.LogicalId}Token`);
47 if (!globalThis.__ALCHEMY_RUNTIME__) {
48 const { accountId } = yield* CloudflareEnvironment.current;
49 yield* token.bind`${resource.LogicalId}`({
50 policies: [
51 {
52 effect: "allow",
53 permissionGroups: [permissionGroup],
54 resources: {
55 [`com.cloudflare.api.account.${accountId}`]: "*",
56 },
57 },
58 ],
59 });
60 }
61 const value = yield* token.value;
62 return { authorize: authorizeWith({ value }) } satisfies K2Auth;
63 });
64 });
65
66 /**
67 * Capture the ambient credentials (the stack's providers layer during
68 * stack-eval) so a client can run with the current credentials instead of a
69 * scoped token — for Actions and other deploy-time Effects.
70 */
71 export const makeK2LocalAuth = Effect.gen(function* () {
72 const context = yield* Effect.context<Credentials | HttpClient.HttpClient>();
73 return {
74 authorize: (eff) => eff.pipe(Effect.provideContext(context)),
75 } satisfies K2Auth;
76 });