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

main
ts 153 lines 5.51 KB
Raw
1 import * as AWS from "@alchemy.run/aws";
2 import * as Effect from "effect/Effect";
3 import { HttpServerRequest } from "effect/http/HttpServerRequest";
4 import * as HttpServerResponse from "effect/http/HttpServerResponse";
5 import * as Layer from "effect/Layer";
6
7 export class QAppsTestFunction extends AWS.Lambda.Function<AWS.Lambda.Function>()(
8 "QAppsTestFunction",
9 ) {}
10
11 /**
12 * Every route answers `{ …fields }` on success or `{ errorTag }` when the
13 * operation fails with a TYPED error — the test asserts on concrete fields
14 * (or a typed tag), which proves the binding wiring, the instance-id/app-id
15 * injection, and the IAM grants (app-ARN-scoped for the session/permission
16 * bindings, account-level for the instance-level bindings). An untyped
17 * error crashes into a 500.
18 */
19 const errorTagged = <A, E extends { _tag: string }, R>(
20 effect: Effect.Effect<A, E, R>,
21 ): Effect.Effect<A | { errorTag: string }, never, R> =>
22 effect.pipe(
23 Effect.map((a): A | { errorTag: string } => a),
24 Effect.catch((e) => Effect.succeed({ errorTag: e._tag })),
25 );
26
27 const TEXT_CARD_ID = "11111111-1111-4111-8111-111111111111";
28 const QUERY_CARD_ID = "22222222-2222-4222-8222-222222222222";
29
30 /**
31 * Q App binding fixture: deploys a Q App into the Q Business application
32 * environment instance named by QAPPS_INSTANCE_ID plus a Lambda bound to a
33 * representative set of Q Apps bindings (both builders: app/session-scoped
34 * and instance-level). Only deployed behind the AWS_TEST_QAPPS gate — the
35 * QApp resource itself cannot exist without an entitled Q Business instance.
36 */
37 export default QAppsTestFunction.make(
38 { main: import.meta.url, functionUrl: true },
39 Effect.gen(function* () {
40 const instanceId = yield* Effect.sync(() => process.env.QAPPS_INSTANCE_ID ?? "");
41
42 const app = yield* AWS.QApps.QApp("BindingsQApp", {
43 instanceId,
44 description: "alchemy QApps bindings fixture",
45 appDefinition: {
46 cards: [
47 {
48 textInput: {
49 id: TEXT_CARD_ID,
50 title: "Source Text",
51 type: "text-input",
52 },
53 },
54 {
55 qQuery: {
56 id: QUERY_CARD_ID,
57 title: "Summary",
58 type: "q-query",
59 prompt: "Summarize the following text: @Source Text",
60 },
61 },
62 ],
63 },
64 tags: { fixture: "qapps-bindings" },
65 });
66
67 const appVersion = yield* app.appVersion;
68
69 const startQAppSession = yield* AWS.QApps.StartQAppSession(app);
70 const getQAppSession = yield* AWS.QApps.GetQAppSession(app);
71 const stopQAppSession = yield* AWS.QApps.StopQAppSession(app);
72 const describeQAppPermissions = yield* AWS.QApps.DescribeQAppPermissions(app);
73 const listQApps = yield* AWS.QApps.ListQApps(app);
74 const listCategories = yield* AWS.QApps.ListCategories(app);
75
76 const bound = {
77 startQAppSession,
78 getQAppSession,
79 stopQAppSession,
80 describeQAppPermissions,
81 listQApps,
82 listCategories,
83 };
84
85 return {
86 fetch: Effect.gen(function* () {
87 const request = yield* HttpServerRequest;
88 const url = new URL(request.originalUrl ?? request.url, "http://x");
89 const pathname = url.pathname;
90
91 if (pathname === "/bindings") {
92 return yield* HttpServerResponse.json({ bound: Object.keys(bound) });
93 }
94
95 if (pathname === "/session") {
96 // start → get → stop, proving app-id injection and the session
97 // sub-resource grant end-to-end.
98 const started = yield* errorTagged(startQAppSession({ appVersion: yield* appVersion }));
99 if ("errorTag" in started) {
100 return yield* HttpServerResponse.json(started);
101 }
102 const state = yield* errorTagged(getQAppSession({ sessionId: started.sessionId }));
103 const stopped = yield* errorTagged(stopQAppSession({ sessionId: started.sessionId }));
104 return yield* HttpServerResponse.json({
105 sessionId: started.sessionId,
106 status: "errorTag" in state ? undefined : state.status,
107 getErrorTag: "errorTag" in state ? state.errorTag : undefined,
108 stopErrorTag: "errorTag" in stopped ? stopped.errorTag : undefined,
109 });
110 }
111
112 if (pathname === "/apps") {
113 const result = yield* errorTagged(listQApps({ limit: 10 }));
114 return yield* HttpServerResponse.json(
115 "errorTag" in result ? result : { count: result.apps.length },
116 );
117 }
118
119 if (pathname === "/categories") {
120 const result = yield* errorTagged(listCategories());
121 return yield* HttpServerResponse.json(
122 "errorTag" in result ? result : { count: result.categories?.length ?? 0 },
123 );
124 }
125
126 if (pathname === "/permissions") {
127 const result = yield* errorTagged(describeQAppPermissions());
128 return yield* HttpServerResponse.json(
129 "errorTag" in result
130 ? result
131 : {
132 resourceArn: result.resourceArn,
133 count: result.permissions?.length ?? 0,
134 },
135 );
136 }
137
138 return yield* HttpServerResponse.json({ error: "Not found", pathname }, { status: 404 });
139 }).pipe(Effect.orDie),
140 };
141 }).pipe(
142 Effect.provide(
143 Layer.mergeAll(
144 AWS.QApps.StartQAppSessionHttp,
145 AWS.QApps.GetQAppSessionHttp,
146 AWS.QApps.StopQAppSessionHttp,
147 AWS.QApps.DescribeQAppPermissionsHttp,
148 AWS.QApps.ListQAppsHttp,
149 AWS.QApps.ListCategoriesHttp,
150 ),
151 ),
152 ),
153 );