@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 111 lines 4.04 KB
Raw
1 import * as Namespace from "alchemy/Namespace";
2 import * as Effect from "effect/Effect";
3 import * as Layer from "effect/Layer";
4 import * as Lambda from "../Lambda/Function.ts";
5 import { Permission as LambdaPermission } from "../Lambda/Permission.ts";
6 import { qualifiedArnOf, qualifiedNameOf } from "../Lambda/QualifiedArn.ts";
7 import type { BotAlias } from "./BotAlias.ts";
8 import {
9 CodeHookEventSource,
10 type CodeHookEvent,
11 type CodeHookEventSourceService,
12 type CodeHookHandler,
13 type CodeHookProps,
14 } from "./CodeHookEventSource.ts";
15
16 /**
17 * An Amazon Lex V2 code hook invocation — the
18 * `messageVersion`/`invocationSource`/`bot`/`sessionState` envelope Lex
19 * sends to dialog and fulfillment Lambda functions.
20 */
21 export const isCodeHookEvent = (event: any): event is CodeHookEvent =>
22 typeof event?.messageVersion === "string" &&
23 (event?.invocationSource === "DialogCodeHook" ||
24 event?.invocationSource === "FulfillmentCodeHook") &&
25 typeof event?.bot === "object" &&
26 event?.bot !== null &&
27 typeof event?.bot?.id === "string" &&
28 typeof event?.bot?.aliasId === "string" &&
29 typeof event?.sessionState === "object" &&
30 event?.sessionState !== null;
31
32 /**
33 * Connects an Amazon Lex V2 bot alias's Lambda code hook to the current
34 * Lambda function.
35 *
36 * At deploy time this layer injects the function ARN into the alias's
37 * `botAliasLocaleSettings` through the alias's binding contract and
38 * materializes the `lambda:InvokeFunction` Permission for
39 * `lexv2.amazonaws.com`; at runtime it dispatches matching code hook events
40 * (matched on the bot id, alias id, and locale) to the registered handler
41 * and returns the handler's response to Lex.
42 * ### Handling Code Hooks
43 * **Example:** Fulfill an intent
44 * ```typescript
45 * yield* LexV2.onCodeHook(alias, { localeId: "en_US" }, (event) =>
46 * Effect.succeed(LexV2.fulfillIntent(event, { message: "Done!" })),
47 * );
48 * ```
49 *
50 * @binding
51 */
52 export const LambdaCodeHookEventSource = Layer.effect(
53 CodeHookEventSource,
54 Effect.gen(function* () {
55 // this layer can only be used in a Lambda Function
56 const host = yield* Lambda.eventSourceHost("AWS.LexV2.LambdaCodeHookEventSource");
57
58 return Effect.fn(function* <Req = never>(
59 alias: BotAlias,
60 props: CodeHookProps,
61 handler: CodeHookHandler<Req>,
62 ) {
63 const BotId = yield* alias.botId;
64 const BotAliasId = yield* alias.botAliasId;
65
66 // Deploy-time: inject this function's ARN into the alias's locale
67 // settings (the alias's provider syncs it) and create the invoke
68 // Permission for lexv2. Skipped once running inside the deployed
69 // Function (the global guard), where the only work is registering
70 // the runtime dispatcher below. Namespaced under the host for stable
71 // logical identity.
72 if (!globalThis.__ALCHEMY_RUNTIME__) {
73 yield* Namespace.push(
74 host.LogicalId,
75 Effect.gen(function* () {
76 yield* alias.bind`AWS.LexV2.CodeHook(${host}, ${alias}, ${props.localeId})`({
77 codeHooks: { [props.localeId]: qualifiedArnOf(host) },
78 });
79
80 yield* LambdaPermission(`${alias.LogicalId}-${props.localeId}-CodeHookPermission`, {
81 action: "lambda:InvokeFunction",
82 functionName: qualifiedNameOf(host),
83 principal: "lexv2.amazonaws.com",
84 sourceArn: alias.botAliasArn,
85 });
86 }),
87 );
88 }
89
90 yield* host.listen(
91 Effect.gen(function* () {
92 const botId = yield* BotId;
93 const botAliasId = yield* BotAliasId;
94
95 return (event: any) => {
96 if (
97 isCodeHookEvent(event) &&
98 event.bot.id === botId &&
99 event.bot.aliasId === botAliasId &&
100 event.bot.localeId === props.localeId
101 ) {
102 // Code hooks are request-response: the Lambda's return value
103 // (sessionState + messages) IS the hook response.
104 return handler(event).pipe(Effect.orDie);
105 }
106 };
107 }),
108 );
109 }) as CodeHookEventSourceService;
110 }),
111 );