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

main
ts 241 lines 8.79 KB
Raw
1 import * as data from "@distilled.cloud/aws/redshift-data";
2 import * as Effect from "effect/Effect";
3 import * as Layer from "effect/Layer";
4 import * as Schedule from "effect/Schedule";
5 import * as AWSBindings from "../AWSBindings.ts";
6 import type { Workgroup } from "../RedshiftServerless/Workgroup.ts";
7 import {
8 RedshiftStatementFailed,
9 Statements,
10 type BatchExecuteStatementRequest,
11 type DescribeTableRequest,
12 type ExecuteStatementRequest,
13 type ListDatabasesRequest,
14 type ListSchemasRequest,
15 type ListStatementsRequest,
16 type ListTablesRequest,
17 type StatementsClient,
18 type StatementsOptions,
19 } from "./Statements.ts";
20
21 /** Statement statuses that mean the run is over. */
22 const isTerminal = (status: string | undefined): boolean =>
23 status === "FINISHED" || status === "FAILED" || status === "ABORTED";
24
25 export const StatementsHttp = Layer.effect(
26 Statements,
27 Effect.gen(function* () {
28 const bind = yield* AWSBindings.Bind;
29 const executeStatement = yield* AWSBindings.withCredentials(data.executeStatement);
30 const batchExecuteStatement = yield* AWSBindings.withCredentials(data.batchExecuteStatement);
31 const describeStatement = yield* AWSBindings.withCredentials(data.describeStatement);
32 const cancelStatement = yield* AWSBindings.withCredentials(data.cancelStatement);
33 const getStatementResult = yield* AWSBindings.withCredentials(data.getStatementResult);
34 const getStatementResultV2 = yield* AWSBindings.withCredentials(data.getStatementResultV2);
35 const describeTableOp = yield* AWSBindings.withCredentials(data.describeTable);
36 const listDatabasesOp = yield* AWSBindings.withCredentials(data.listDatabases);
37 const listSchemasOp = yield* AWSBindings.withCredentials(data.listSchemas);
38 const listTablesOp = yield* AWSBindings.withCredentials(data.listTables);
39 const listStatementsOp = yield* AWSBindings.withCredentials(data.listStatements);
40
41 return Effect.fn(function* (workgroup: Workgroup, options: StatementsOptions = {}) {
42 const workgroupName = yield* workgroup.workgroupName;
43 if (!globalThis.__ALCHEMY_RUNTIME__) {
44 yield* bind`AWS.RedshiftData.Statements(${workgroup})`({
45 policyStatements: [
46 {
47 // Workgroup-scoped Data API actions (submit statements and
48 // read database metadata through the workgroup).
49 Effect: "Allow",
50 Action: [
51 "redshift-data:ExecuteStatement",
52 "redshift-data:BatchExecuteStatement",
53 "redshift-data:DescribeTable",
54 "redshift-data:ListDatabases",
55 "redshift-data:ListSchemas",
56 "redshift-data:ListTables",
57 ],
58 Resource: [workgroup.workgroupArn],
59 },
60 {
61 // Statement-scoped actions are authorized per-statement
62 // (owner condition), not by ARN.
63 Effect: "Allow",
64 Action: [
65 "redshift-data:DescribeStatement",
66 "redshift-data:GetStatementResult",
67 "redshift-data:GetStatementResultV2",
68 "redshift-data:CancelStatement",
69 "redshift-data:ListStatements",
70 ],
71 Resource: ["*"],
72 },
73 {
74 // IAM temporary-credential auth against the serverless
75 // workgroup (used when no SecretArn is supplied).
76 Effect: "Allow",
77 Action: ["redshift-serverless:GetCredentials"],
78 Resource: [workgroup.workgroupArn],
79 },
80 ...(options.secretArn
81 ? [
82 {
83 Effect: "Allow" as const,
84 Action: ["secretsmanager:GetSecretValue", "secretsmanager:DescribeSecret"],
85 Resource: [options.secretArn],
86 },
87 ]
88 : []),
89 ],
90 });
91 }
92
93 const database = options.database ?? "dev";
94
95 const execute = Effect.fn(`AWS.RedshiftData.Statements.execute(${workgroup.LogicalId})`)(
96 function* (request: ExecuteStatementRequest) {
97 return yield* executeStatement({
98 ...request,
99 WorkgroupName: yield* workgroupName,
100 Database: database,
101 SecretArn: options.secretArn,
102 DbUser: options.dbUser,
103 });
104 },
105 );
106
107 const executeBatch = Effect.fn(
108 `AWS.RedshiftData.Statements.executeBatch(${workgroup.LogicalId})`,
109 )(function* (request: BatchExecuteStatementRequest) {
110 return yield* batchExecuteStatement({
111 ...request,
112 WorkgroupName: yield* workgroupName,
113 Database: database,
114 SecretArn: options.secretArn,
115 DbUser: options.dbUser,
116 });
117 });
118
119 const describe = Effect.fn(`AWS.RedshiftData.Statements.describe(${workgroup.LogicalId})`)(
120 function* (id: string) {
121 return yield* describeStatement({ Id: id });
122 },
123 );
124
125 const cancel = Effect.fn(`AWS.RedshiftData.Statements.cancel(${workgroup.LogicalId})`)(
126 function* (id: string) {
127 return yield* cancelStatement({ Id: id });
128 },
129 );
130
131 const getResult = Effect.fn(`AWS.RedshiftData.Statements.getResult(${workgroup.LogicalId})`)(
132 function* (id: string, nextToken?: string) {
133 return yield* getStatementResult({ Id: id, NextToken: nextToken });
134 },
135 );
136
137 const getResultV2 = Effect.fn(
138 `AWS.RedshiftData.Statements.getResultV2(${workgroup.LogicalId})`,
139 )(function* (id: string, nextToken?: string) {
140 return yield* getStatementResultV2({ Id: id, NextToken: nextToken });
141 });
142
143 const describeTable = Effect.fn(
144 `AWS.RedshiftData.Statements.describeTable(${workgroup.LogicalId})`,
145 )(function* (request: DescribeTableRequest) {
146 return yield* describeTableOp({
147 ...request,
148 WorkgroupName: yield* workgroupName,
149 Database: database,
150 SecretArn: options.secretArn,
151 DbUser: options.dbUser,
152 });
153 });
154
155 const listDatabases = Effect.fn(
156 `AWS.RedshiftData.Statements.listDatabases(${workgroup.LogicalId})`,
157 )(function* (request: ListDatabasesRequest = {}) {
158 return yield* listDatabasesOp({
159 ...request,
160 WorkgroupName: yield* workgroupName,
161 Database: database,
162 SecretArn: options.secretArn,
163 DbUser: options.dbUser,
164 });
165 });
166
167 const listSchemas = Effect.fn(
168 `AWS.RedshiftData.Statements.listSchemas(${workgroup.LogicalId})`,
169 )(function* (request: ListSchemasRequest = {}) {
170 return yield* listSchemasOp({
171 ...request,
172 WorkgroupName: yield* workgroupName,
173 Database: database,
174 SecretArn: options.secretArn,
175 DbUser: options.dbUser,
176 });
177 });
178
179 const listTables = Effect.fn(
180 `AWS.RedshiftData.Statements.listTables(${workgroup.LogicalId})`,
181 )(function* (request: ListTablesRequest = {}) {
182 return yield* listTablesOp({
183 ...request,
184 WorkgroupName: yield* workgroupName,
185 Database: database,
186 SecretArn: options.secretArn,
187 DbUser: options.dbUser,
188 });
189 });
190
191 const listStatements = Effect.fn(
192 `AWS.RedshiftData.Statements.listStatements(${workgroup.LogicalId})`,
193 )(function* (request: ListStatementsRequest = {}) {
194 return yield* listStatementsOp({
195 ...request,
196 WorkgroupName: yield* workgroupName,
197 });
198 });
199
200 const query = Effect.fn(`AWS.RedshiftData.Statements.query(${workgroup.LogicalId})`)(
201 function* (sql: string, parameters?: data.SqlParameter[]) {
202 const submitted = yield* execute({ Sql: sql, Parameters: parameters });
203 const id = submitted.Id!;
204 // Poll until the statement reaches a terminal status. Redshift
205 // Serverless typically finishes simple statements in a few seconds;
206 // budget ~2.5 min (30 * 5s).
207 const described = yield* describe(id).pipe(
208 Effect.repeat({
209 schedule: Schedule.spaced("5 seconds"),
210 until: (r) => isTerminal(r.Status),
211 times: 30,
212 }),
213 );
214 if (described.Status !== "FINISHED") {
215 return yield* new RedshiftStatementFailed({
216 statementId: id,
217 status: described.Status ?? "UNKNOWN",
218 error: described.Error,
219 });
220 }
221 return yield* getResult(id);
222 },
223 );
224
225 return {
226 execute,
227 executeBatch,
228 describe,
229 cancel,
230 getResult,
231 getResultV2,
232 describeTable,
233 listDatabases,
234 listSchemas,
235 listTables,
236 listStatements,
237 query,
238 } satisfies StatementsClient;
239 });
240 }),
241 );