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