1import * as Comprehend from "@/AWS/Comprehend";
2import * as Lambda from "@/AWS/Lambda";
3import * as Polly from "@/AWS/Polly";
4import * as Rekognition from "@/AWS/Rekognition";
5import * as Textract from "@/AWS/Textract";
6import * as Translate from "@/AWS/Translate";
7import * as Duration from "effect/Duration";
8import * as Effect from "effect/Effect";
9import * as Layer from "effect/Layer";
10import * as Stream from "effect/Stream";
11import { HttpServerRequest } from "effect/http/HttpServerRequest";
12import * as HttpServerResponse from "effect/http/HttpServerResponse";
13import path from "pathe";
14import {
15 HELLO_PNG_BASE64,
16 SENTIMENT_TEXT,
17 SPEECH_TEXT,
18 TRANSLATE_TEXT,
19} from "./constants.ts";
20
21const main = path.resolve(import.meta.dirname, "handler.ts");
22
23// Deterministic checked-in test input, decoded once at module init.
24const HELLO_PNG_BYTES = Buffer.from(HELLO_PNG_BASE64, "base64");
25
26export class AICapabilitiesTestFunction extends Lambda.Function<Lambda.Function>()(
27 "AICapabilitiesTestFunction",
28) {}
29
30export default AICapabilitiesTestFunction.make(
31 {
32 main,
33 functionUrl: true,
34 // Vision/speech inference regularly exceeds Lambda's 3s default timeout.
35 timeout: Duration.seconds(30),
36 },
37 Effect.gen(function* () {
38 const detectLabels = yield* Rekognition.DetectLabels();
39 const detectDocumentText = yield* Textract.DetectDocumentText();
40 const synthesizeSpeech = yield* Polly.SynthesizeSpeech();
41 const translateText = yield* Translate.TranslateText();
42 const detectSentiment = yield* Comprehend.DetectSentiment();
43
44 return {
45 fetch: Effect.gen(function* () {
46 const request = yield* HttpServerRequest;
47 const url = new URL(request.originalUrl);
48 const pathname = url.pathname;
49
50 // Cheap readiness route — no AWS call.
51 if (request.method === "GET" && pathname === "/ping") {
52 return yield* HttpServerResponse.json({ ok: true });
53 }
54
55 if (request.method === "GET" && pathname === "/detect-labels") {
56 const result = yield* detectLabels({
57 Image: { Bytes: HELLO_PNG_BYTES },
58 MaxLabels: 10,
59 MinConfidence: 50,
60 });
61 return yield* HttpServerResponse.json({
62 labels: (result.Labels ?? []).map((label) => label.Name),
63 labelModelVersion: result.LabelModelVersion,
64 });
65 }
66
67 if (request.method === "GET" && pathname === "/detect-document-text") {
68 const result = yield* detectDocumentText({
69 Document: { Bytes: HELLO_PNG_BYTES },
70 });
71 return yield* HttpServerResponse.json({
72 lines: (result.Blocks ?? [])
73 .filter((block) => block.BlockType === "LINE")
74 .map((block) => block.Text),
75 pages: result.DocumentMetadata?.Pages,
76 });
77 }
78
79 if (request.method === "GET" && pathname === "/synthesize-speech") {
80 const result = yield* synthesizeSpeech({
81 Engine: "neural",
82 OutputFormat: "mp3",
83 VoiceId: "Joanna",
84 Text: SPEECH_TEXT,
85 });
86 const chunks = yield* Stream.runCollect(result.AudioStream!);
87 const byteLength = Array.from(chunks).reduce(
88 (total, chunk) => total + chunk.length,
89 0,
90 );
91 return yield* HttpServerResponse.json({
92 contentType: result.ContentType,
93 byteLength,
94 });
95 }
96
97 if (request.method === "GET" && pathname === "/translate-text") {
98 const result = yield* translateText({
99 Text: TRANSLATE_TEXT,
100 SourceLanguageCode: "en",
101 TargetLanguageCode: "es",
102 });
103 return yield* HttpServerResponse.json({
104 translatedText: result.TranslatedText,
105 sourceLanguageCode: result.SourceLanguageCode,
106 targetLanguageCode: result.TargetLanguageCode,
107 });
108 }
109
110 if (request.method === "GET" && pathname === "/detect-sentiment") {
111 const result = yield* detectSentiment({
112 Text: SENTIMENT_TEXT,
113 LanguageCode: "en",
114 });
115 return yield* HttpServerResponse.json({
116 sentiment: result.Sentiment,
117 sentimentScore: result.SentimentScore,
118 });
119 }
120
121 return yield* HttpServerResponse.json(
122 { error: "Not found", method: request.method, pathname },
123 { status: 404 },
124 );
125 }).pipe(Effect.orDie),
126 };
127 }).pipe(
128 Effect.provide(
129 Layer.mergeAll(
130 Rekognition.DetectLabelsHttp,
131 Textract.DetectDocumentTextHttp,
132 Polly.SynthesizeSpeechHttp,
133 Translate.TranslateTextHttp,
134 Comprehend.DetectSentimentHttp,
135 ),
136 ),
137 ),
138);