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

main
ts 122 lines 4.74 KB
Raw
1 import * as Output from "alchemy/Output";
2 import * as Effect from "effect/Effect";
3 import * as AWSBindings from "../AWSBindings.ts";
4 import type { QApp } from "./QApp.ts";
5
6 /**
7 * Shared HTTP scaffolding for the Amazon Q Apps runtime bindings.
8 *
9 * NOT exported from `index.ts` — every `{Op}Http.ts` in this service is a
10 * thin `Layer.effect(Cap, make…HttpBinding({ … }))` over one of the builders
11 * below. Everything except the operation and the IAM action list is
12 * boilerplate: every Q Apps operation runs against the Q Business
13 * application environment instance of the bound {@link QApp} (its
14 * `instanceId` is injected as the `instance-id` header), and app-keyed
15 * operations additionally inject the bound app's `appId`.
16 *
17 * Two IAM grant shapes:
18 *
19 * - {@link makeQAppHttpBinding} — operations keyed by the bound Q App or one
20 * of its sessions. Granted on the app's ARN and its sub-resources
21 * (`{appArn}`, `{appArn}/*` — Q App session ARNs are children of the app
22 * ARN).
23 * - {@link makeQAppsInstanceHttpBinding} — instance-level operations
24 * (library items, categories, listing, generation). Library-item and
25 * category authorization ARNs are service-assigned and not derivable from
26 * a bound Q App, so these grant on `Resource: ["*"]`.
27 */
28
29 /**
30 * Build the impl Effect for a Q Apps operation keyed by the bound
31 * {@link QApp} (or one of its sessions): the runtime callable injects the
32 * app's `instanceId` (and, with `injectAppId`, its `appId`); the deploy-time
33 * half grants `iamActions` on the app ARN and its session sub-resources.
34 */
35 export const makeQAppHttpBinding = <I extends { instanceId: string }, A, E, R>(options: {
36 /**
37 * Short capability name used in the binding sid and runtime span, e.g.
38 * `"StartQAppSession"`.
39 */
40 capability: string;
41 /** IAM actions granted on the app ARN + `{appArn}/*`. */
42 iamActions: readonly string[];
43 /** The distilled operation; `instanceId` (and `appId`) are injected. */
44 operation: Effect.Effect<(input: I) => Effect.Effect<A, E>, never, R>;
45 /** Inject the bound app's `appId` into the request. */
46 injectAppId?: boolean;
47 }) =>
48 Effect.gen(function* () {
49 const bind = yield* AWSBindings.Bind;
50 const op = yield* AWSBindings.withCredentials(options.operation);
51
52 return Effect.fn(function* (app: QApp) {
53 const instanceId = yield* app.instanceId;
54 const appId = yield* app.appId;
55 if (!globalThis.__ALCHEMY_RUNTIME__) {
56 yield* bind`AWS.QApps.${options.capability}(${app})`({
57 policyStatements: [
58 {
59 Effect: "Allow",
60 Action: [...options.iamActions],
61 Resource: [Output.interpolate`${app.appArn}`, Output.interpolate`${app.appArn}/*`],
62 },
63 ],
64 });
65 }
66 return Effect.fn(`AWS.QApps.${options.capability}(${app.LogicalId})`)(function* (
67 request?: Omit<I, "instanceId" | "appId">,
68 ) {
69 const input = options.injectAppId
70 ? {
71 ...request,
72 instanceId: yield* instanceId,
73 appId: yield* appId,
74 }
75 : { ...request, instanceId: yield* instanceId };
76 return yield* op(input as I);
77 });
78 });
79 });
80
81 /**
82 * Build the impl Effect for an instance-level Q Apps operation (library
83 * items, categories, listing, generation): the runtime callable injects the
84 * bound {@link QApp}'s `instanceId`. Library-item and category authorization
85 * ARNs are service-assigned and not derivable from a bound Q App, so the
86 * deploy-time half grants `iamActions` on `Resource: ["*"]`.
87 */
88 export const makeQAppsInstanceHttpBinding = <I extends { instanceId: string }, A, E, R>(options: {
89 /**
90 * Short capability name used in the binding sid and runtime span, e.g.
91 * `"ListLibraryItems"`.
92 */
93 capability: string;
94 /** IAM actions granted on `Resource: ["*"]`. */
95 iamActions: readonly string[];
96 /** The distilled operation; `instanceId` is injected. */
97 operation: Effect.Effect<(input: I) => Effect.Effect<A, E>, never, R>;
98 }) =>
99 Effect.gen(function* () {
100 const bind = yield* AWSBindings.Bind;
101 const op = yield* AWSBindings.withCredentials(options.operation);
102
103 return Effect.fn(function* (app: QApp) {
104 const instanceId = yield* app.instanceId;
105 if (!globalThis.__ALCHEMY_RUNTIME__) {
106 yield* bind`AWS.QApps.${options.capability}(${app})`({
107 policyStatements: [
108 {
109 Effect: "Allow",
110 Action: [...options.iamActions],
111 Resource: ["*"],
112 },
113 ],
114 });
115 }
116 return Effect.fn(`AWS.QApps.${options.capability}(${app.LogicalId})`)(function* (
117 request?: Omit<I, "instanceId">,
118 ) {
119 return yield* op({ ...request, instanceId: yield* instanceId } as I);
120 });
121 });
122 });