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

main
ts 159 lines 5.64 KB
Raw
1 import * as AMP from "@alchemy.run/aws/AMP";
2 import * as Lambda from "@alchemy.run/aws/Lambda";
3 import * as Effect from "effect/Effect";
4 import { HttpServerRequest } from "effect/http/HttpServerRequest";
5 import * as HttpServerResponse from "effect/http/HttpServerResponse";
6 import * as Layer from "effect/Layer";
7 import path from "pathe";
8
9 const main = path.resolve(import.meta.dirname, "handler.ts");
10
11 export class AmpTestFunction extends Lambda.Function<Lambda.Function>()("AmpTestFunction") {}
12
13 export default AmpTestFunction.make(
14 {
15 main,
16 functionUrl: true,
17 },
18 Effect.gen(function* () {
19 const workspace = yield* AMP.Workspace("BindingsWorkspace", {
20 alias: "alchemy-test-amp-bindings",
21 tags: { Environment: "test" },
22 });
23
24 const remoteWrite = yield* AMP.RemoteWrite(workspace);
25 const metrics = yield* AMP.QueryMetrics(workspace);
26 const labels = yield* AMP.GetLabels(workspace);
27 const getSeries = yield* AMP.GetSeries(workspace);
28 const getMetricMetadata = yield* AMP.GetMetricMetadata(workspace);
29 const describeWorkspace = yield* AMP.DescribeWorkspace(workspace);
30 const listWorkspaces = yield* AMP.ListWorkspaces();
31 const getDefaultScraperConfiguration = yield* AMP.GetDefaultScraperConfiguration();
32
33 return {
34 fetch: Effect.gen(function* () {
35 const request = yield* HttpServerRequest;
36 const url = new URL(request.originalUrl);
37 const pathname = url.pathname;
38
39 if (request.method === "GET" && pathname === "/health") {
40 return HttpServerResponse.text("ok");
41 }
42
43 if (request.method === "POST" && pathname === "/remote-write") {
44 const body = (yield* request.json) as unknown as {
45 name: string;
46 labels?: Record<string, string>;
47 value: number;
48 timestamp?: number;
49 };
50 yield* remoteWrite({
51 timeseries: [
52 {
53 name: body.name,
54 labels: body.labels,
55 samples: [{ value: body.value, timestamp: body.timestamp }],
56 },
57 ],
58 });
59 return yield* HttpServerResponse.json({ success: true });
60 }
61
62 if (request.method === "GET" && pathname === "/query") {
63 const query = url.searchParams.get("query");
64 if (!query) {
65 return HttpServerResponse.text("Missing query", { status: 400 });
66 }
67 const result = yield* metrics.query({
68 query,
69 timeout: "30 seconds",
70 });
71 return yield* HttpServerResponse.json({ result });
72 }
73
74 if (request.method === "GET" && pathname === "/query-range") {
75 const query = url.searchParams.get("query");
76 if (!query) {
77 return HttpServerResponse.text("Missing query", { status: 400 });
78 }
79 const now = yield* Effect.sync(() => Date.now());
80 const result = yield* metrics.queryRange({
81 query,
82 start: new Date(now - 15 * 60_000),
83 end: new Date(now),
84 step: "30 seconds",
85 });
86 return yield* HttpServerResponse.json({ result });
87 }
88
89 if (request.method === "GET" && pathname === "/labels") {
90 const names = yield* labels.labelNames();
91 return yield* HttpServerResponse.json({ names });
92 }
93
94 if (request.method === "GET" && pathname === "/label-values") {
95 const label = url.searchParams.get("label");
96 if (!label) {
97 return HttpServerResponse.text("Missing label", { status: 400 });
98 }
99 const values = yield* labels.labelValues({ label });
100 return yield* HttpServerResponse.json({ values });
101 }
102
103 if (request.method === "GET" && pathname === "/series") {
104 const match = url.searchParams.getAll("match");
105 if (match.length === 0) {
106 return HttpServerResponse.text("Missing match", { status: 400 });
107 }
108 const series = yield* getSeries({ match });
109 return yield* HttpServerResponse.json({ series });
110 }
111
112 if (request.method === "GET" && pathname === "/describe-workspace") {
113 const response = yield* describeWorkspace();
114 return yield* HttpServerResponse.json({
115 workspaceId: response.workspace.workspaceId,
116 status: response.workspace.status.statusCode,
117 });
118 }
119
120 if (request.method === "GET" && pathname === "/list-workspaces") {
121 const response = yield* listWorkspaces();
122 return yield* HttpServerResponse.json({
123 workspaceIds: response.workspaces.map((w) => w.workspaceId),
124 });
125 }
126
127 if (request.method === "GET" && pathname === "/default-scraper-config") {
128 const configuration = yield* getDefaultScraperConfiguration();
129 return yield* HttpServerResponse.json({ configuration });
130 }
131
132 if (request.method === "GET" && pathname === "/metadata") {
133 const metadata = yield* getMetricMetadata({
134 metric: url.searchParams.get("metric") ?? undefined,
135 });
136 return yield* HttpServerResponse.json({ metadata });
137 }
138
139 return yield* HttpServerResponse.json(
140 { error: "Not found", method: request.method, pathname },
141 { status: 404 },
142 );
143 }).pipe(Effect.orDie),
144 };
145 }).pipe(
146 Effect.provide(
147 Layer.mergeAll(
148 AMP.RemoteWriteHttp,
149 AMP.QueryMetricsHttp,
150 AMP.GetLabelsHttp,
151 AMP.GetSeriesHttp,
152 AMP.GetMetricMetadataHttp,
153 AMP.DescribeWorkspaceHttp,
154 AMP.ListWorkspacesHttp,
155 AMP.GetDefaultScraperConfigurationHttp,
156 ),
157 ),
158 ),
159 );