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

main
ts 275 lines 9.88 KB
Raw
1 import * as Lambda from "@alchemy.run/aws/Lambda";
2 import * as SSMIncidents from "@alchemy.run/aws/SSMIncidents";
3 import * as Duration from "effect/Duration";
4 import * as Effect from "effect/Effect";
5 import { HttpServerRequest } from "effect/http/HttpServerRequest";
6 import * as HttpServerResponse from "effect/http/HttpServerResponse";
7 import * as Layer from "effect/Layer";
8 import path from "pathe";
9
10 const main = path.resolve(import.meta.dirname, "handler.ts");
11
12 export class IncidentsTestFunction extends Lambda.Function<Lambda.Function>()(
13 "IncidentsTestFunction",
14 ) {}
15
16 /**
17 * Routes answer `{ …fields }` on success or `{ errorTag }` when the
18 * operation fails with a TYPED error — the test asserts the tag is in a
19 * route-specific allowlist, which proves both the binding wiring and the
20 * IAM grant. An untyped error crashes into a 500 instead.
21 */
22 const errorTagged = <A, E extends { _tag: string }, R>(
23 effect: Effect.Effect<A, E, R>,
24 ): Effect.Effect<A | { errorTag: string; errorMessage?: string }, never, R> =>
25 effect.pipe(
26 Effect.map((a): A | { errorTag: string; errorMessage?: string } => a),
27 Effect.catch((e) =>
28 Effect.succeed({
29 errorTag: e._tag,
30 errorMessage: (e as { message?: string }).message,
31 }),
32 ),
33 );
34
35 // One binding per Incident Manager data-plane capability. The fixture is
36 // only deployable on an account onboarded to Incident Manager (the
37 // replication set is the account singleton and CreateReplicationSet was
38 // deprecated Nov 7, 2025), so the whole suite that deploys this is gated
39 // behind AWS_TEST_INCIDENT_MANAGER=1.
40 export default IncidentsTestFunction.make(
41 {
42 main,
43 functionUrl: true,
44 timeout: Duration.seconds(30),
45 },
46 Effect.gen(function* () {
47 // The account must already be onboarded (ReplicationSet deployed by the
48 // test's phase-1 deploy under the same logical id).
49 const replicationSet = yield* SSMIncidents.ReplicationSet("Incidents", {});
50
51 const plan = yield* SSMIncidents.ResponsePlan("BindingsPlan", {
52 displayName: "alchemy bindings fixture",
53 incidentTemplate: {
54 title: "alchemy bindings fixture incident",
55 impact: 5,
56 summary: "Started by the SSMIncidents Bindings test fixture",
57 },
58 // Reference the replication set so the engine orders plan creation
59 // after Incident Manager is onboarded.
60 tags: { fixture: "ssm-incidents-bindings", set: replicationSet.arn },
61 });
62
63 // Response-plan-scoped
64 const startIncident = yield* SSMIncidents.StartIncident(plan);
65
66 // Incident records
67 const listIncidentRecords = yield* SSMIncidents.ListIncidentRecords();
68 const getIncidentRecord = yield* SSMIncidents.GetIncidentRecord();
69 const updateIncidentRecord = yield* SSMIncidents.UpdateIncidentRecord();
70 const deleteIncidentRecord = yield* SSMIncidents.DeleteIncidentRecord();
71
72 // Timeline events
73 const createTimelineEvent = yield* SSMIncidents.CreateTimelineEvent();
74 const getTimelineEvent = yield* SSMIncidents.GetTimelineEvent();
75 const updateTimelineEvent = yield* SSMIncidents.UpdateTimelineEvent();
76 const deleteTimelineEvent = yield* SSMIncidents.DeleteTimelineEvent();
77 const listTimelineEvents = yield* SSMIncidents.ListTimelineEvents();
78
79 // Related items
80 const listRelatedItems = yield* SSMIncidents.ListRelatedItems();
81 const updateRelatedItems = yield* SSMIncidents.UpdateRelatedItems();
82
83 // Findings
84 const listIncidentFindings = yield* SSMIncidents.ListIncidentFindings();
85 const batchGetIncidentFindings = yield* SSMIncidents.BatchGetIncidentFindings();
86
87 const bound = {
88 startIncident,
89 listIncidentRecords,
90 getIncidentRecord,
91 updateIncidentRecord,
92 deleteIncidentRecord,
93 createTimelineEvent,
94 getTimelineEvent,
95 updateTimelineEvent,
96 deleteTimelineEvent,
97 listTimelineEvents,
98 listRelatedItems,
99 updateRelatedItems,
100 listIncidentFindings,
101 batchGetIncidentFindings,
102 };
103
104 return {
105 fetch: Effect.gen(function* () {
106 const request = yield* HttpServerRequest;
107 const url = new URL(request.originalUrl);
108 const pathname = url.pathname;
109 const arn = url.searchParams.get("arn") ?? "";
110 const eventId = url.searchParams.get("eventId") ?? "";
111
112 if (request.method === "GET" && pathname === "/bindings") {
113 return yield* HttpServerResponse.json({
114 bound: Object.keys(bound),
115 });
116 }
117
118 if (request.method === "POST" && pathname === "/start") {
119 const { incidentRecordArn } = yield* startIncident({
120 title: "alchemy bindings test incident",
121 impact: 5,
122 });
123 return yield* HttpServerResponse.json({ incidentRecordArn });
124 }
125
126 if (request.method === "GET" && pathname === "/records") {
127 const { incidentRecordSummaries } = yield* listIncidentRecords({});
128 return yield* HttpServerResponse.json({
129 count: incidentRecordSummaries.length,
130 arns: incidentRecordSummaries.map((r) => r.arn),
131 });
132 }
133
134 if (request.method === "GET" && pathname === "/record") {
135 const { incidentRecord } = yield* getIncidentRecord({ arn });
136 return yield* HttpServerResponse.json({
137 title: incidentRecord.title,
138 status: incidentRecord.status,
139 impact: incidentRecord.impact,
140 });
141 }
142
143 if (request.method === "POST" && pathname === "/resolve") {
144 yield* updateIncidentRecord({ arn, status: "RESOLVED" });
145 return yield* HttpServerResponse.json({ resolved: true });
146 }
147
148 if (request.method === "DELETE" && pathname === "/record") {
149 yield* deleteIncidentRecord({ arn });
150 return yield* HttpServerResponse.json({ deleted: true });
151 }
152
153 if (request.method === "POST" && pathname === "/timeline") {
154 const created = yield* createTimelineEvent({
155 incidentRecordArn: arn,
156 eventTime: new Date(),
157 eventType: "Custom Event",
158 eventData: JSON.stringify({ note: "created by bindings test" }),
159 });
160 return yield* HttpServerResponse.json({
161 eventId: created.eventId,
162 });
163 }
164
165 if (request.method === "GET" && pathname === "/timeline") {
166 const { event } = yield* getTimelineEvent({
167 incidentRecordArn: arn,
168 eventId,
169 });
170 return yield* HttpServerResponse.json({
171 eventType: event.eventType,
172 eventData: event.eventData,
173 });
174 }
175
176 if (request.method === "POST" && pathname === "/timeline-update") {
177 yield* updateTimelineEvent({
178 incidentRecordArn: arn,
179 eventId,
180 eventData: JSON.stringify({ note: "updated by bindings test" }),
181 });
182 return yield* HttpServerResponse.json({ updated: true });
183 }
184
185 if (request.method === "DELETE" && pathname === "/timeline") {
186 yield* deleteTimelineEvent({
187 incidentRecordArn: arn,
188 eventId,
189 });
190 return yield* HttpServerResponse.json({ deleted: true });
191 }
192
193 if (request.method === "GET" && pathname === "/timeline-events") {
194 const { eventSummaries } = yield* listTimelineEvents({
195 incidentRecordArn: arn,
196 });
197 return yield* HttpServerResponse.json({
198 count: eventSummaries.length,
199 });
200 }
201
202 if (request.method === "GET" && pathname === "/related") {
203 const { relatedItems } = yield* listRelatedItems({
204 incidentRecordArn: arn,
205 });
206 return yield* HttpServerResponse.json({
207 count: relatedItems.length,
208 });
209 }
210
211 if (request.method === "POST" && pathname === "/related") {
212 const result = yield* errorTagged(
213 updateRelatedItems({
214 incidentRecordArn: arn,
215 relatedItemsUpdate: {
216 itemToAdd: {
217 title: "alchemy bindings test link",
218 identifier: {
219 type: "OTHER",
220 value: { url: "https://alchemy.run" },
221 },
222 },
223 },
224 }).pipe(Effect.map(() => ({ added: true }))),
225 );
226 return yield* HttpServerResponse.json(result);
227 }
228
229 if (request.method === "GET" && pathname === "/findings") {
230 const { findings } = yield* listIncidentFindings({
231 incidentRecordArn: arn,
232 });
233 return yield* HttpServerResponse.json({
234 count: findings.length,
235 ids: findings.map((f) => f.id),
236 });
237 }
238
239 if (request.method === "POST" && pathname === "/findings") {
240 const result = yield* errorTagged(
241 batchGetIncidentFindings({
242 incidentRecordArn: arn,
243 findingIds: [],
244 }).pipe(Effect.map((r) => ({ count: r.findings.length }))),
245 );
246 return yield* HttpServerResponse.json(result);
247 }
248
249 return yield* HttpServerResponse.json(
250 { error: "Not found", method: request.method, pathname },
251 { status: 404 },
252 );
253 }).pipe(Effect.orDie),
254 };
255 }).pipe(
256 Effect.provide(
257 Layer.mergeAll(
258 SSMIncidents.StartIncidentHttp,
259 SSMIncidents.ListIncidentRecordsHttp,
260 SSMIncidents.GetIncidentRecordHttp,
261 SSMIncidents.UpdateIncidentRecordHttp,
262 SSMIncidents.DeleteIncidentRecordHttp,
263 SSMIncidents.CreateTimelineEventHttp,
264 SSMIncidents.GetTimelineEventHttp,
265 SSMIncidents.UpdateTimelineEventHttp,
266 SSMIncidents.DeleteTimelineEventHttp,
267 SSMIncidents.ListTimelineEventsHttp,
268 SSMIncidents.ListRelatedItemsHttp,
269 SSMIncidents.UpdateRelatedItemsHttp,
270 SSMIncidents.ListIncidentFindingsHttp,
271 SSMIncidents.BatchGetIncidentFindingsHttp,
272 ),
273 ),
274 ),
275 );