@devries-cameron20 / t3code Mirror · read-only
Fork 0

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

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 21, 06:00 UTC

main
ts 153 lines 5.83 KB
Raw
1 import * as GeoRoutes from "@alchemy.run/aws/GeoRoutes";
2 import * as Lambda from "@alchemy.run/aws/Lambda";
3 import * as Cause from "effect/Cause";
4 import * as Duration from "effect/Duration";
5 import * as Effect from "effect/Effect";
6 import { HttpServerRequest } from "effect/http/HttpServerRequest";
7 import * as HttpServerResponse from "effect/http/HttpServerResponse";
8 import * as Layer from "effect/Layer";
9 import path from "pathe";
10
11 const main = path.resolve(import.meta.dirname, "handler.ts");
12
13 export class GeoRoutesTestFunction extends Lambda.Function<Lambda.Function>()(
14 "GeoRoutesTestFunction",
15 ) {}
16
17 export default GeoRoutesTestFunction.make(
18 {
19 main,
20 functionUrl: true,
21 // Geo calls fan out to upstream providers and can exceed Lambda's 3s default.
22 timeout: Duration.seconds(30),
23 },
24 Effect.gen(function* () {
25 const calculateRoutes = yield* GeoRoutes.CalculateRoutes();
26 const calculateIsolines = yield* GeoRoutes.CalculateIsolines();
27 const calculateRouteMatrix = yield* GeoRoutes.CalculateRouteMatrix();
28 const optimizeWaypoints = yield* GeoRoutes.OptimizeWaypoints();
29 const snapToRoads = yield* GeoRoutes.SnapToRoads();
30
31 return {
32 fetch: Effect.gen(function* () {
33 const request = yield* HttpServerRequest;
34 const pathname = new URL(request.originalUrl).pathname;
35
36 // Cheap readiness route — no AWS call.
37 if (request.method === "GET" && pathname === "/ping") {
38 return yield* HttpServerResponse.json({ ok: true });
39 }
40
41 if (request.method === "GET" && pathname === "/calculate-routes") {
42 const result = yield* calculateRoutes({
43 // [longitude, latitude] — two points in Seattle.
44 Origin: [-122.339, 47.61],
45 Destination: [-122.201, 47.61],
46 TravelMode: "Car",
47 });
48 const routes = result.Routes ?? [];
49 return yield* HttpServerResponse.json({
50 count: routes.length,
51 distance: routes[0]?.Summary?.Distance,
52 duration: routes[0]?.Summary?.Duration,
53 });
54 }
55
56 if (request.method === "GET" && pathname === "/calculate-isolines") {
57 const result = yield* calculateIsolines({
58 // [longitude, latitude] — downtown Seattle.
59 Origin: [-122.339, 47.61],
60 // 10-minute drive-time isochrone.
61 Thresholds: { Time: [600] },
62 TravelMode: "Car",
63 });
64 const isolines = result.Isolines ?? [];
65 return yield* HttpServerResponse.json({
66 count: isolines.length,
67 geometryCount: isolines[0]?.Geometries?.length ?? 0,
68 pricingBucket: result.PricingBucket,
69 });
70 }
71
72 if (request.method === "GET" && pathname === "/calculate-route-matrix") {
73 const result = yield* calculateRouteMatrix({
74 // [longitude, latitude] — 2x2 matrix within the Seattle area.
75 Origins: [{ Position: [-122.339, 47.61] }, { Position: [-122.335, 47.608] }],
76 Destinations: [{ Position: [-122.201, 47.61] }, { Position: [-122.313, 47.62] }],
77 RoutingBoundary: {
78 Geometry: {
79 // Radius in meters — covers all origins and destinations.
80 Circle: { Center: [-122.3, 47.61], Radius: 30_000 },
81 },
82 },
83 });
84 const matrix = result.RouteMatrix ?? [];
85 return yield* HttpServerResponse.json({
86 rows: matrix.length,
87 columns: matrix[0]?.length ?? 0,
88 firstDuration: matrix[0]?.[0]?.Duration,
89 errorCount: result.ErrorCount,
90 pricingBucket: result.PricingBucket,
91 });
92 }
93
94 if (request.method === "GET" && pathname === "/optimize-waypoints") {
95 const result = yield* optimizeWaypoints({
96 // [longitude, latitude] — Seattle loop with two stops.
97 Origin: [-122.339, 47.61],
98 Destination: [-122.201, 47.61],
99 Waypoints: [
100 { Id: "stop-1", Position: [-122.335, 47.608] },
101 { Id: "stop-2", Position: [-122.313, 47.62] },
102 ],
103 });
104 return yield* HttpServerResponse.json({
105 optimizedCount: result.OptimizedWaypoints?.length ?? 0,
106 order: (result.OptimizedWaypoints ?? []).map((w) => w.Id),
107 distance: result.Distance,
108 duration: result.Duration,
109 pricingBucket: result.PricingBucket,
110 });
111 }
112
113 if (request.method === "GET" && pathname === "/snap-to-roads") {
114 const result = yield* snapToRoads({
115 // [longitude, latitude] — a short GPS trace along a Seattle street.
116 TracePoints: [
117 { Position: [-122.339, 47.61] },
118 { Position: [-122.337, 47.609] },
119 { Position: [-122.335, 47.608] },
120 ],
121 TravelMode: "Car",
122 });
123 return yield* HttpServerResponse.json({
124 snappedCount: result.SnappedTracePoints?.length ?? 0,
125 firstConfidence: result.SnappedTracePoints?.[0]?.Confidence,
126 pricingBucket: result.PricingBucket,
127 });
128 }
129
130 return yield* HttpServerResponse.json(
131 { error: "Not found", method: request.method, pathname },
132 { status: 404 },
133 );
134 }).pipe(
135 // Surface the typed error (tag + message) instead of an opaque 500 so
136 // test failures show the real cause in the response body.
137 Effect.catchCause((cause) =>
138 HttpServerResponse.json({ error: Cause.pretty(cause) }, { status: 500 }),
139 ),
140 ),
141 };
142 }).pipe(
143 Effect.provide(
144 Layer.mergeAll(
145 GeoRoutes.CalculateRoutesHttp,
146 GeoRoutes.CalculateIsolinesHttp,
147 GeoRoutes.CalculateRouteMatrixHttp,
148 GeoRoutes.OptimizeWaypointsHttp,
149 GeoRoutes.SnapToRoadsHttp,
150 ),
151 ),
152 ),
153 );