@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 81 lines 2.74 KB
Raw
1 import type { Credentials } from "@distilled.cloud/aws/Credentials";
2 import type { Region as AwsRegion } from "@distilled.cloud/aws/Region";
3 import * as Effect from "effect/Effect";
4 import type * as HttpClient from "effect/http/HttpClient";
5 import * as AWSBindings from "../AWSBindings.ts";
6 import type { BotAlias } from "./BotAlias.ts";
7
8 /**
9 * Shared HTTP scaffolding for the Lex V2 runtime bindings.
10 *
11 * Every Lex V2 conversation capability follows the same shape — resolve the
12 * distilled `lex-runtime-v2` operation, register a `lex:*` IAM policy
13 * statement on the binding host scoped to the bot alias, and return a
14 * runtime callable that injects the alias's `botId` + `botAliasId`. The only
15 * variation is the operation and the IAM action, so those are the inputs.
16 *
17 * @internal — not exported from `index.ts`.
18 */
19
20 type LexRequirements = Credentials | AwsRegion | HttpClient.HttpClient;
21
22 export interface LexAliasHttpBindingConfig<Req extends object, Out, Err> {
23 /**
24 * Short capability name used in the binding sid and runtime span, e.g.
25 * `"RecognizeText"`.
26 */
27 capability: string;
28 /**
29 * IAM actions granted to the binding host on the alias ARN, e.g.
30 * `["lex:RecognizeText"]`.
31 */
32 iamActions: readonly string[];
33 /**
34 * The distilled `lex-runtime-v2` operation implementing the capability.
35 */
36 operation: Effect.Effect<
37 (input: Req & { botId: string; botAliasId: string }) => Effect.Effect<Out, Err>,
38 never,
39 LexRequirements
40 >;
41 }
42
43 /**
44 * Build the implementation effect for a bot-alias-scoped Lex V2 runtime
45 * capability: `Layer.effect(Cap, makeLexAliasHttpBinding({ ... }))`.
46 *
47 * The runtime callable injects the bound alias's `botId` and `botAliasId`,
48 * so `Req` is the operation's request type without those fields.
49 */
50 export const makeLexAliasHttpBinding = <Req extends object, Out, Err>(
51 config: LexAliasHttpBindingConfig<Req, Out, Err>,
52 ) =>
53 Effect.gen(function* () {
54 const bind = yield* AWSBindings.Bind;
55 const op = yield* AWSBindings.withCredentials(config.operation);
56
57 return Effect.fn(function* (alias: BotAlias) {
58 const BotId = yield* alias.botId;
59 const BotAliasId = yield* alias.botAliasId;
60 if (!globalThis.__ALCHEMY_RUNTIME__) {
61 yield* bind`AWS.LexV2.${config.capability}(${alias})`({
62 policyStatements: [
63 {
64 Effect: "Allow",
65 Action: [...config.iamActions],
66 Resource: [alias.botAliasArn],
67 },
68 ],
69 });
70 }
71 return Effect.fn(`AWS.LexV2.${config.capability}(${alias.LogicalId})`)(function* (
72 request: Req,
73 ) {
74 return yield* op({
75 ...request,
76 botId: yield* BotId,
77 botAliasId: yield* BotAliasId,
78 });
79 });
80 });
81 });