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

main
ts 147 lines 4.84 KB
Raw
1 import * as Lambda from "@alchemy.run/aws/Lambda";
2 import * as OSIS from "@alchemy.run/aws/OSIS";
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 OsisTestFunction extends Lambda.Function<Lambda.Function>()("OsisTestFunction") {}
13
14 // A syntactically-valid Data Prepper configuration (http source -> s3 sink).
15 // ValidatePipeline is a static check; the role/bucket are never assumed.
16 const validConfig = `version: "2"
17 log-pipeline:
18 source:
19 http:
20 path: "/logs/ingest"
21 sink:
22 - s3:
23 aws:
24 sts_role_arn: "arn:aws:iam::123456789012:role/osis-validate-fixture"
25 region: "us-west-2"
26 bucket: "osis-validate-fixture"
27 threshold:
28 event_collect_timeout: "60s"
29 codec:
30 ndjson:
31 `;
32
33 // Structurally broken: the sink references a plugin that does not exist.
34 const invalidConfig = `version: "2"
35 bad-pipeline:
36 source:
37 http:
38 path: "/logs/ingest"
39 sink:
40 - not-a-real-sink-plugin:
41 some_option: true
42 `;
43
44 export default OsisTestFunction.make(
45 {
46 main,
47 functionUrl: true,
48 timeout: Duration.seconds(30),
49 },
50 Effect.gen(function* () {
51 // Account-level bindings — no resource argument.
52 const validatePipeline = yield* OSIS.ValidatePipeline();
53 const listPipelineBlueprints = yield* OSIS.ListPipelineBlueprints();
54 const getPipelineBlueprint = yield* OSIS.GetPipelineBlueprint();
55 const listPipelineEndpointConnections = yield* OSIS.ListPipelineEndpointConnections();
56
57 const bound = {
58 validatePipeline,
59 listPipelineBlueprints,
60 getPipelineBlueprint,
61 listPipelineEndpointConnections,
62 };
63
64 // ValidatePipeline surfaces some invalid configs as a typed
65 // ValidationException rather than `isValid: false`; normalize both.
66 const validate = (body: string) =>
67 validatePipeline({ PipelineConfigurationBody: body }).pipe(
68 Effect.map((response) => ({
69 isValid: response.isValid === true,
70 errors: (response.Errors ?? []).flatMap((e) =>
71 e.Message !== undefined ? [e.Message] : [],
72 ),
73 })),
74 Effect.catchTag("ValidationException", (error) =>
75 Effect.succeed({
76 isValid: false,
77 errors: [error.message ?? "ValidationException"],
78 }),
79 ),
80 );
81
82 return {
83 fetch: Effect.gen(function* () {
84 const request = yield* HttpServerRequest;
85 const url = new URL(request.originalUrl);
86 const pathname = url.pathname;
87
88 if (request.method === "GET" && pathname === "/bindings") {
89 return yield* HttpServerResponse.json({
90 bound: Object.keys(bound),
91 });
92 }
93
94 if (request.method === "GET" && pathname === "/validate-good") {
95 return yield* HttpServerResponse.json(yield* validate(validConfig));
96 }
97
98 if (request.method === "GET" && pathname === "/validate-bad") {
99 return yield* HttpServerResponse.json(yield* validate(invalidConfig));
100 }
101
102 if (request.method === "GET" && pathname === "/blueprints") {
103 const { Blueprints } = yield* listPipelineBlueprints();
104 return yield* HttpServerResponse.json({
105 names: (Blueprints ?? []).flatMap((blueprint) =>
106 blueprint.BlueprintName !== undefined ? [blueprint.BlueprintName] : [],
107 ),
108 });
109 }
110
111 if (request.method === "GET" && pathname === "/blueprint") {
112 const name = url.searchParams.get("name") ?? "";
113 const { Blueprint } = yield* getPipelineBlueprint({
114 BlueprintName: name,
115 });
116 return yield* HttpServerResponse.json({
117 name: Blueprint?.BlueprintName,
118 hasBody:
119 typeof Blueprint?.PipelineConfigurationBody === "string" &&
120 Blueprint.PipelineConfigurationBody.length > 0,
121 });
122 }
123
124 if (request.method === "GET" && pathname === "/endpoint-connections") {
125 const { PipelineEndpointConnections } = yield* listPipelineEndpointConnections();
126 return yield* HttpServerResponse.json({
127 count: (PipelineEndpointConnections ?? []).length,
128 });
129 }
130
131 return yield* HttpServerResponse.json(
132 { error: "Not found", method: request.method, pathname },
133 { status: 404 },
134 );
135 }).pipe(Effect.orDie),
136 };
137 }).pipe(
138 Effect.provide(
139 Layer.mergeAll(
140 OSIS.ValidatePipelineHttp,
141 OSIS.ListPipelineBlueprintsHttp,
142 OSIS.GetPipelineBlueprintHttp,
143 OSIS.ListPipelineEndpointConnectionsHttp,
144 ),
145 ),
146 ),
147 );