The best way to control Claude Code, Codex and more from any computer or phone

main
ts 48 lines 1.61 KB
Raw
1 import * as machines from "@distilled.cloud/fly-io/machines";
2 import * as Effect from "effect/Effect";
3 import * as FetchHttpClient from "effect/http/FetchHttpClient";
4 import * as Layer from "effect/Layer";
5 import { CredentialsFromAmbientOrEnv } from "./Credentials.ts";
6 import { Encrypt, type EncryptRequest } from "./Encrypt.ts";
7 import { base64ToBytes, bytesToBase64, makeHttpSecretKeyBinding } from "./SecretKeyHttp.ts";
8
9 /**
10 * HTTP implementation of {@link Encrypt}. Provide it on the
11 * {@link Service} or Action Effect.
12 *
13 *
14 * ### Provide the layer
15 * **Example:** On a Service
16 * ```typescript
17 * Effect.gen(function* () {
18 * const encrypt = yield* Fly.Encrypt(Box);
19 * // ...
20 * }).pipe(Effect.provide(Fly.EncryptHttp))
21 * ```
22 *
23 * @layer
24 * @product Secret Key
25 * @provides Fly.Encrypt
26 */
27 export const EncryptHttp = Layer.effect(
28 Encrypt,
29 Effect.suspend(() =>
30 makeHttpSecretKeyBinding({
31 makeClient: (auth, appName, secretName) =>
32 Effect.fn("Fly.Encrypt")(function* (request: EncryptRequest) {
33 const res = yield* auth.authorize(
34 machines.encryptSecretKey({
35 app_name: yield* appName,
36 secret_name: yield* secretName,
37 plaintext: bytesToBase64(request.plaintext),
38 associated_data:
39 request.associatedData === undefined
40 ? undefined
41 : bytesToBase64(request.associatedData),
42 }),
43 );
44 return { ciphertext: base64ToBytes(res.ciphertext) };
45 }),
46 }),
47 ),
48 ).pipe(Layer.provide(FetchHttpClient.layer), Layer.provide(CredentialsFromAmbientOrEnv));