The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import * as Comprehend from "@alchemy.run/aws/Comprehend"; |
| 2 | import * as IAM from "@alchemy.run/aws/IAM"; |
| 3 | import * as Lambda from "@alchemy.run/aws/Lambda"; |
| 4 | import * as S3 from "@alchemy.run/aws/S3"; |
| 5 | import * as Duration from "effect/Duration"; |
| 6 | import * as Effect from "effect/Effect"; |
| 7 | import { HttpServerRequest } from "effect/http/HttpServerRequest"; |
| 8 | import * as HttpServerResponse from "effect/http/HttpServerResponse"; |
| 9 | import * as Layer from "effect/Layer"; |
| 10 | import * as Schedule from "effect/Schedule"; |
| 11 | import path from "pathe"; |
| 12 | |
| 13 | const main = path.resolve(import.meta.dirname, "handler.ts"); |
| 14 | |
| 15 | // Deterministic fixture names (bucket names are account-global; this suite |
| 16 | // owns them in the testing account). |
| 17 | export const FIXTURE_BUCKET_NAME = "alchemy-test-comprehend-bindings"; |
| 18 | |
| 19 | // Well-formed-but-nonexistent job id used to drive the typed |
| 20 | // JobNotFoundException paths. An IAM gap would surface AccessDeniedException |
| 21 | // (a 500 through the handler's orDie), so a typed not-found tag proves the |
| 22 | // grant end-to-end. |
| 23 | const BOGUS_JOB_ID = "00000000000000000000000000000000"; |
| 24 | |
| 25 | // An S3 URI that fails Comprehend's server-side pattern validation — drives |
| 26 | // the Start*Job bindings through their typed ValidationException path |
| 27 | // without actually starting (and paying for) an async job per family. |
| 28 | const INVALID_S3_URI = "invalid-uri"; |
| 29 | |
| 30 | export class ComprehendTestFunction extends Lambda.Function<Lambda.Function>()( |
| 31 | "ComprehendTestFunction", |
| 32 | ) {} |
| 33 | |
| 34 | export default ComprehendTestFunction.make( |
| 35 | { |
| 36 | main, |
| 37 | functionUrl: true, |
| 38 | // The sentiment job lifecycle route retries StartSentimentDetectionJob |
| 39 | // through fresh-role IAM propagation (bounded ~40s). |
| 40 | timeout: Duration.seconds(120), |
| 41 | }, |
| 42 | Effect.gen(function* () { |
| 43 | const bucket = yield* S3.Bucket("ComprehendInputBucket", { |
| 44 | bucketName: FIXTURE_BUCKET_NAME, |
| 45 | forceDestroy: true, |
| 46 | }); |
| 47 | // The role Amazon Comprehend assumes to read input documents and write |
| 48 | // job results for the Start*Job bindings. |
| 49 | const dataAccessRole = yield* IAM.Role("ComprehendDataAccessRole", { |
| 50 | assumeRolePolicyDocument: { |
| 51 | Version: "2012-10-17", |
| 52 | Statement: [ |
| 53 | { |
| 54 | Effect: "Allow", |
| 55 | Principal: { Service: "comprehend.amazonaws.com" }, |
| 56 | Action: ["sts:AssumeRole"], |
| 57 | }, |
| 58 | ], |
| 59 | }, |
| 60 | inlinePolicies: { |
| 61 | s3Access: { |
| 62 | Version: "2012-10-17", |
| 63 | Statement: [ |
| 64 | { |
| 65 | Effect: "Allow", |
| 66 | Action: ["s3:GetObject", "s3:ListBucket", "s3:PutObject"], |
| 67 | Resource: [ |
| 68 | `arn:aws:s3:::${FIXTURE_BUCKET_NAME}`, |
| 69 | `arn:aws:s3:::${FIXTURE_BUCKET_NAME}/*`, |
| 70 | ], |
| 71 | }, |
| 72 | ], |
| 73 | }, |
| 74 | }, |
| 75 | }); |
| 76 | // Used by /classify-document to build a well-formed same-account |
| 77 | // endpoint ARN (a cross-account ARN returns NotAuthorizedException |
| 78 | // instead of the typed ResourceUnavailableException). |
| 79 | const roleArn = yield* dataAccessRole.roleArn; |
| 80 | |
| 81 | const putObject = yield* S3.PutObject(bucket); |
| 82 | |
| 83 | // --- real-time analysis bindings --- |
| 84 | const detectDominantLanguage = yield* Comprehend.DetectDominantLanguage(); |
| 85 | const detectEntities = yield* Comprehend.DetectEntities(); |
| 86 | const detectKeyPhrases = yield* Comprehend.DetectKeyPhrases(); |
| 87 | const detectPiiEntities = yield* Comprehend.DetectPiiEntities(); |
| 88 | const detectSentiment = yield* Comprehend.DetectSentiment(); |
| 89 | const detectSyntax = yield* Comprehend.DetectSyntax(); |
| 90 | const detectTargetedSentiment = yield* Comprehend.DetectTargetedSentiment(); |
| 91 | const detectToxicContent = yield* Comprehend.DetectToxicContent(); |
| 92 | const containsPiiEntities = yield* Comprehend.ContainsPiiEntities(); |
| 93 | const classifyDocument = yield* Comprehend.ClassifyDocument(); |
| 94 | |
| 95 | // --- batch real-time analysis bindings --- |
| 96 | const batchDetectDominantLanguage = yield* Comprehend.BatchDetectDominantLanguage(); |
| 97 | const batchDetectEntities = yield* Comprehend.BatchDetectEntities(); |
| 98 | const batchDetectKeyPhrases = yield* Comprehend.BatchDetectKeyPhrases(); |
| 99 | const batchDetectSentiment = yield* Comprehend.BatchDetectSentiment(); |
| 100 | const batchDetectSyntax = yield* Comprehend.BatchDetectSyntax(); |
| 101 | const batchDetectTargetedSentiment = yield* Comprehend.BatchDetectTargetedSentiment(); |
| 102 | |
| 103 | // --- async job bindings (role injection + PassRole grant) --- |
| 104 | const startDocumentClassificationJob = |
| 105 | yield* Comprehend.StartDocumentClassificationJob(dataAccessRole); |
| 106 | const startDominantLanguageDetectionJob = |
| 107 | yield* Comprehend.StartDominantLanguageDetectionJob(dataAccessRole); |
| 108 | const startEntitiesDetectionJob = yield* Comprehend.StartEntitiesDetectionJob(dataAccessRole); |
| 109 | const startEventsDetectionJob = yield* Comprehend.StartEventsDetectionJob(dataAccessRole); |
| 110 | const startKeyPhrasesDetectionJob = |
| 111 | yield* Comprehend.StartKeyPhrasesDetectionJob(dataAccessRole); |
| 112 | const startPiiEntitiesDetectionJob = |
| 113 | yield* Comprehend.StartPiiEntitiesDetectionJob(dataAccessRole); |
| 114 | const startSentimentDetectionJob = yield* Comprehend.StartSentimentDetectionJob(dataAccessRole); |
| 115 | const startTargetedSentimentDetectionJob = |
| 116 | yield* Comprehend.StartTargetedSentimentDetectionJob(dataAccessRole); |
| 117 | const startTopicsDetectionJob = yield* Comprehend.StartTopicsDetectionJob(dataAccessRole); |
| 118 | |
| 119 | const describeDocumentClassificationJob = yield* Comprehend.DescribeDocumentClassificationJob(); |
| 120 | const describeDominantLanguageDetectionJob = |
| 121 | yield* Comprehend.DescribeDominantLanguageDetectionJob(); |
| 122 | const describeEntitiesDetectionJob = yield* Comprehend.DescribeEntitiesDetectionJob(); |
| 123 | const describeEventsDetectionJob = yield* Comprehend.DescribeEventsDetectionJob(); |
| 124 | const describeKeyPhrasesDetectionJob = yield* Comprehend.DescribeKeyPhrasesDetectionJob(); |
| 125 | const describePiiEntitiesDetectionJob = yield* Comprehend.DescribePiiEntitiesDetectionJob(); |
| 126 | const describeSentimentDetectionJob = yield* Comprehend.DescribeSentimentDetectionJob(); |
| 127 | const describeTargetedSentimentDetectionJob = |
| 128 | yield* Comprehend.DescribeTargetedSentimentDetectionJob(); |
| 129 | const describeTopicsDetectionJob = yield* Comprehend.DescribeTopicsDetectionJob(); |
| 130 | |
| 131 | const listDocumentClassificationJobs = yield* Comprehend.ListDocumentClassificationJobs(); |
| 132 | const listDominantLanguageDetectionJobs = yield* Comprehend.ListDominantLanguageDetectionJobs(); |
| 133 | const listEntitiesDetectionJobs = yield* Comprehend.ListEntitiesDetectionJobs(); |
| 134 | const listEventsDetectionJobs = yield* Comprehend.ListEventsDetectionJobs(); |
| 135 | const listKeyPhrasesDetectionJobs = yield* Comprehend.ListKeyPhrasesDetectionJobs(); |
| 136 | const listPiiEntitiesDetectionJobs = yield* Comprehend.ListPiiEntitiesDetectionJobs(); |
| 137 | const listSentimentDetectionJobs = yield* Comprehend.ListSentimentDetectionJobs(); |
| 138 | const listTargetedSentimentDetectionJobs = |
| 139 | yield* Comprehend.ListTargetedSentimentDetectionJobs(); |
| 140 | const listTopicsDetectionJobs = yield* Comprehend.ListTopicsDetectionJobs(); |
| 141 | |
| 142 | const stopDominantLanguageDetectionJob = yield* Comprehend.StopDominantLanguageDetectionJob(); |
| 143 | const stopEntitiesDetectionJob = yield* Comprehend.StopEntitiesDetectionJob(); |
| 144 | const stopEventsDetectionJob = yield* Comprehend.StopEventsDetectionJob(); |
| 145 | const stopKeyPhrasesDetectionJob = yield* Comprehend.StopKeyPhrasesDetectionJob(); |
| 146 | const stopPiiEntitiesDetectionJob = yield* Comprehend.StopPiiEntitiesDetectionJob(); |
| 147 | const stopSentimentDetectionJob = yield* Comprehend.StopSentimentDetectionJob(); |
| 148 | const stopTargetedSentimentDetectionJob = yield* Comprehend.StopTargetedSentimentDetectionJob(); |
| 149 | |
| 150 | const PII_TEXT = "My name is Jane Doe and my email address is jane@example.com."; |
| 151 | const REVIEW_TEXT = "I love this product, it works wonderfully!"; |
| 152 | const BATCH_TEXTS = [ |
| 153 | "I love this product, it works wonderfully!", |
| 154 | "The delivery was late and the box arrived damaged.", |
| 155 | ]; |
| 156 | |
| 157 | const jobInput = { |
| 158 | InputDataConfig: { |
| 159 | S3Uri: `s3://${FIXTURE_BUCKET_NAME}/comprehend-input/`, |
| 160 | InputFormat: "ONE_DOC_PER_LINE" as const, |
| 161 | }, |
| 162 | OutputDataConfig: { |
| 163 | S3Uri: `s3://${FIXTURE_BUCKET_NAME}/comprehend-output/`, |
| 164 | }, |
| 165 | }; |
| 166 | const invalidJobInput = { |
| 167 | InputDataConfig: { S3Uri: INVALID_S3_URI }, |
| 168 | OutputDataConfig: { S3Uri: INVALID_S3_URI }, |
| 169 | }; |
| 170 | |
| 171 | return { |
| 172 | fetch: Effect.gen(function* () { |
| 173 | const request = yield* HttpServerRequest; |
| 174 | const url = new URL(request.originalUrl); |
| 175 | const pathname = url.pathname; |
| 176 | |
| 177 | // Cheap readiness route — no Comprehend call. |
| 178 | if (request.method === "GET" && pathname === "/ping") { |
| 179 | return yield* HttpServerResponse.json({ ok: true }); |
| 180 | } |
| 181 | |
| 182 | // One route drives every single-document real-time binding. |
| 183 | if (request.method === "GET" && pathname === "/detect-all") { |
| 184 | const language = yield* detectDominantLanguage({ |
| 185 | Text: "Bob ordered two sandwiches yesterday.", |
| 186 | }); |
| 187 | const entities = yield* detectEntities({ |
| 188 | Text: "Bob moved to Seattle in 2017.", |
| 189 | LanguageCode: "en", |
| 190 | }); |
| 191 | const keyPhrases = yield* detectKeyPhrases({ |
| 192 | Text: "The quarterly earnings report exceeded analyst expectations.", |
| 193 | LanguageCode: "en", |
| 194 | }); |
| 195 | const pii = yield* detectPiiEntities({ |
| 196 | Text: PII_TEXT, |
| 197 | LanguageCode: "en", |
| 198 | }); |
| 199 | const sentiment = yield* detectSentiment({ |
| 200 | Text: REVIEW_TEXT, |
| 201 | LanguageCode: "en", |
| 202 | }); |
| 203 | const syntax = yield* detectSyntax({ |
| 204 | Text: "The cat sat on the mat.", |
| 205 | LanguageCode: "en", |
| 206 | }); |
| 207 | const targeted = yield* detectTargetedSentiment({ |
| 208 | Text: "The screen is gorgeous but the battery is disappointing.", |
| 209 | LanguageCode: "en", |
| 210 | }); |
| 211 | const toxic = yield* detectToxicContent({ |
| 212 | TextSegments: [{ Text: "You are a wonderful person." }], |
| 213 | LanguageCode: "en", |
| 214 | }); |
| 215 | const piiLabels = yield* containsPiiEntities({ |
| 216 | Text: PII_TEXT, |
| 217 | LanguageCode: "en", |
| 218 | }); |
| 219 | return yield* HttpServerResponse.json({ |
| 220 | languageCode: language.Languages?.[0]?.LanguageCode, |
| 221 | entityTypes: (entities.Entities ?? []).map((e) => e.Type), |
| 222 | keyPhraseCount: (keyPhrases.KeyPhrases ?? []).length, |
| 223 | piiTypes: (pii.Entities ?? []).map((e) => e.Type), |
| 224 | sentiment: sentiment.Sentiment, |
| 225 | syntaxTags: (syntax.SyntaxTokens ?? []).map((t) => t.PartOfSpeech?.Tag), |
| 226 | targetedEntityCount: (targeted.Entities ?? []).length, |
| 227 | toxicity: toxic.ResultList?.[0]?.Toxicity, |
| 228 | piiLabels: (piiLabels.Labels ?? []).map((l) => l.Name), |
| 229 | }); |
| 230 | } |
| 231 | |
| 232 | // One route drives every batch real-time binding. |
| 233 | if (request.method === "GET" && pathname === "/batch-all") { |
| 234 | const language = yield* batchDetectDominantLanguage({ |
| 235 | TextList: BATCH_TEXTS, |
| 236 | }); |
| 237 | const entities = yield* batchDetectEntities({ |
| 238 | TextList: ["Bob moved to Seattle in 2017."], |
| 239 | LanguageCode: "en", |
| 240 | }); |
| 241 | const keyPhrases = yield* batchDetectKeyPhrases({ |
| 242 | TextList: BATCH_TEXTS, |
| 243 | LanguageCode: "en", |
| 244 | }); |
| 245 | const sentiment = yield* batchDetectSentiment({ |
| 246 | TextList: BATCH_TEXTS, |
| 247 | LanguageCode: "en", |
| 248 | }); |
| 249 | const syntax = yield* batchDetectSyntax({ |
| 250 | TextList: ["The cat sat on the mat."], |
| 251 | LanguageCode: "en", |
| 252 | }); |
| 253 | const targeted = yield* batchDetectTargetedSentiment({ |
| 254 | TextList: ["The screen is gorgeous but the battery is bad."], |
| 255 | LanguageCode: "en", |
| 256 | }); |
| 257 | return yield* HttpServerResponse.json({ |
| 258 | languageResults: (language.ResultList ?? []).length, |
| 259 | firstLanguage: language.ResultList?.[0]?.Languages?.[0]?.LanguageCode, |
| 260 | entityResults: (entities.ResultList ?? []).length, |
| 261 | keyPhraseResults: (keyPhrases.ResultList ?? []).length, |
| 262 | sentimentResults: (sentiment.ResultList ?? []).length, |
| 263 | firstSentiment: sentiment.ResultList?.[0]?.Sentiment, |
| 264 | syntaxResults: (syntax.ResultList ?? []).length, |
| 265 | targetedResults: (targeted.ResultList ?? []).length, |
| 266 | }); |
| 267 | } |
| 268 | |
| 269 | // ClassifyDocument requires a trained custom endpoint; a well-formed |
| 270 | // same-account ARN that doesn't exist drives the typed |
| 271 | // ResourceUnavailableException path end-to-end (proving IAM + call |
| 272 | // plumbing without a multi-hour classifier training run). |
| 273 | if (request.method === "GET" && pathname === "/classify-document") { |
| 274 | const region = yield* Effect.sync(() => process.env.AWS_REGION); |
| 275 | const account = (yield* roleArn).split(":")[4]; |
| 276 | const tag = yield* classifyDocument({ |
| 277 | Text: "Subject: your invoice for March is attached", |
| 278 | EndpointArn: `arn:aws:comprehend:${region}:${account}:document-classifier-endpoint/alchemy-nonexistent`, |
| 279 | }).pipe( |
| 280 | Effect.map(() => "Classified"), |
| 281 | Effect.catchTag( |
| 282 | ["ResourceUnavailableException", "InvalidRequestException", "NotAuthorizedException"], |
| 283 | (e) => Effect.succeed(e._tag), |
| 284 | ), |
| 285 | ); |
| 286 | return yield* HttpServerResponse.json({ tag }); |
| 287 | } |
| 288 | |
| 289 | // One route drives all nine List*Jobs bindings. |
| 290 | if (request.method === "GET" && pathname === "/jobs/list-all") { |
| 291 | const counts = { |
| 292 | documentClassification: ( |
| 293 | (yield* listDocumentClassificationJobs({ MaxResults: 5 })) |
| 294 | .DocumentClassificationJobPropertiesList ?? [] |
| 295 | ).length, |
| 296 | dominantLanguage: ( |
| 297 | (yield* listDominantLanguageDetectionJobs({ MaxResults: 5 })) |
| 298 | .DominantLanguageDetectionJobPropertiesList ?? [] |
| 299 | ).length, |
| 300 | entities: ( |
| 301 | (yield* listEntitiesDetectionJobs({ MaxResults: 5 })) |
| 302 | .EntitiesDetectionJobPropertiesList ?? [] |
| 303 | ).length, |
| 304 | events: ( |
| 305 | (yield* listEventsDetectionJobs({ MaxResults: 5 })) |
| 306 | .EventsDetectionJobPropertiesList ?? [] |
| 307 | ).length, |
| 308 | keyPhrases: ( |
| 309 | (yield* listKeyPhrasesDetectionJobs({ MaxResults: 5 })) |
| 310 | .KeyPhrasesDetectionJobPropertiesList ?? [] |
| 311 | ).length, |
| 312 | piiEntities: ( |
| 313 | (yield* listPiiEntitiesDetectionJobs({ MaxResults: 5 })) |
| 314 | .PiiEntitiesDetectionJobPropertiesList ?? [] |
| 315 | ).length, |
| 316 | sentiment: ( |
| 317 | (yield* listSentimentDetectionJobs({ MaxResults: 5 })) |
| 318 | .SentimentDetectionJobPropertiesList ?? [] |
| 319 | ).length, |
| 320 | targetedSentiment: ( |
| 321 | (yield* listTargetedSentimentDetectionJobs({ MaxResults: 5 })) |
| 322 | .TargetedSentimentDetectionJobPropertiesList ?? [] |
| 323 | ).length, |
| 324 | topics: ( |
| 325 | (yield* listTopicsDetectionJobs({ MaxResults: 5 })) |
| 326 | .TopicsDetectionJobPropertiesList ?? [] |
| 327 | ).length, |
| 328 | }; |
| 329 | return yield* HttpServerResponse.json(counts); |
| 330 | } |
| 331 | |
| 332 | // One route drives all nine Describe*Job bindings through their |
| 333 | // typed JobNotFoundException path. |
| 334 | if (request.method === "GET" && pathname === "/jobs/describe-not-found-all") { |
| 335 | const probe = { JobId: BOGUS_JOB_ID }; |
| 336 | const tags = { |
| 337 | documentClassification: yield* describeDocumentClassificationJob(probe).pipe( |
| 338 | Effect.map(() => "Found"), |
| 339 | Effect.catchTag("JobNotFoundException", (e) => Effect.succeed(e._tag)), |
| 340 | ), |
| 341 | dominantLanguage: yield* describeDominantLanguageDetectionJob(probe).pipe( |
| 342 | Effect.map(() => "Found"), |
| 343 | Effect.catchTag("JobNotFoundException", (e) => Effect.succeed(e._tag)), |
| 344 | ), |
| 345 | entities: yield* describeEntitiesDetectionJob(probe).pipe( |
| 346 | Effect.map(() => "Found"), |
| 347 | Effect.catchTag("JobNotFoundException", (e) => Effect.succeed(e._tag)), |
| 348 | ), |
| 349 | events: yield* describeEventsDetectionJob(probe).pipe( |
| 350 | Effect.map(() => "Found"), |
| 351 | Effect.catchTag(["JobNotFoundException", "NotAuthorizedException"], (e) => |
| 352 | Effect.succeed(e._tag), |
| 353 | ), |
| 354 | ), |
| 355 | keyPhrases: yield* describeKeyPhrasesDetectionJob(probe).pipe( |
| 356 | Effect.map(() => "Found"), |
| 357 | Effect.catchTag("JobNotFoundException", (e) => Effect.succeed(e._tag)), |
| 358 | ), |
| 359 | piiEntities: yield* describePiiEntitiesDetectionJob(probe).pipe( |
| 360 | Effect.map(() => "Found"), |
| 361 | Effect.catchTag("JobNotFoundException", (e) => Effect.succeed(e._tag)), |
| 362 | ), |
| 363 | sentiment: yield* describeSentimentDetectionJob(probe).pipe( |
| 364 | Effect.map(() => "Found"), |
| 365 | Effect.catchTag("JobNotFoundException", (e) => Effect.succeed(e._tag)), |
| 366 | ), |
| 367 | targetedSentiment: yield* describeTargetedSentimentDetectionJob(probe).pipe( |
| 368 | Effect.map(() => "Found"), |
| 369 | Effect.catchTag("JobNotFoundException", (e) => Effect.succeed(e._tag)), |
| 370 | ), |
| 371 | topics: yield* describeTopicsDetectionJob(probe).pipe( |
| 372 | Effect.map(() => "Found"), |
| 373 | Effect.catchTag("JobNotFoundException", (e) => Effect.succeed(e._tag)), |
| 374 | ), |
| 375 | }; |
| 376 | return yield* HttpServerResponse.json(tags); |
| 377 | } |
| 378 | |
| 379 | // One route drives all seven Stop*Job bindings through their typed |
| 380 | // JobNotFoundException path. |
| 381 | if (request.method === "POST" && pathname === "/jobs/stop-not-found-all") { |
| 382 | const probe = { JobId: BOGUS_JOB_ID }; |
| 383 | const tags = { |
| 384 | dominantLanguage: yield* stopDominantLanguageDetectionJob(probe).pipe( |
| 385 | Effect.map(() => "Stopped"), |
| 386 | Effect.catchTag("JobNotFoundException", (e) => Effect.succeed(e._tag)), |
| 387 | ), |
| 388 | entities: yield* stopEntitiesDetectionJob(probe).pipe( |
| 389 | Effect.map(() => "Stopped"), |
| 390 | Effect.catchTag("JobNotFoundException", (e) => Effect.succeed(e._tag)), |
| 391 | ), |
| 392 | // Events detection is closed to new customers — the account |
| 393 | // surfaces the (patched) typed NotAuthorizedException instead of |
| 394 | // JobNotFoundException. |
| 395 | events: yield* stopEventsDetectionJob(probe).pipe( |
| 396 | Effect.map(() => "Stopped"), |
| 397 | Effect.catchTag(["JobNotFoundException", "NotAuthorizedException"], (e) => |
| 398 | Effect.succeed(e._tag), |
| 399 | ), |
| 400 | ), |
| 401 | keyPhrases: yield* stopKeyPhrasesDetectionJob(probe).pipe( |
| 402 | Effect.map(() => "Stopped"), |
| 403 | Effect.catchTag("JobNotFoundException", (e) => Effect.succeed(e._tag)), |
| 404 | ), |
| 405 | piiEntities: yield* stopPiiEntitiesDetectionJob(probe).pipe( |
| 406 | Effect.map(() => "Stopped"), |
| 407 | Effect.catchTag("JobNotFoundException", (e) => Effect.succeed(e._tag)), |
| 408 | ), |
| 409 | sentiment: yield* stopSentimentDetectionJob(probe).pipe( |
| 410 | Effect.map(() => "Stopped"), |
| 411 | Effect.catchTag("JobNotFoundException", (e) => Effect.succeed(e._tag)), |
| 412 | ), |
| 413 | targetedSentiment: yield* stopTargetedSentimentDetectionJob(probe).pipe( |
| 414 | Effect.map(() => "Stopped"), |
| 415 | Effect.catchTag("JobNotFoundException", (e) => Effect.succeed(e._tag)), |
| 416 | ), |
| 417 | }; |
| 418 | return yield* HttpServerResponse.json(tags); |
| 419 | } |
| 420 | |
| 421 | // Drives the eight non-sentiment Start*Job bindings through |
| 422 | // Comprehend's server-side ValidationException (invalid S3 URI) — |
| 423 | // a real runtime call proving role injection + IAM without paying |
| 424 | // for eight async jobs. Sentiment gets the real lifecycle below. |
| 425 | if (request.method === "POST" && pathname === "/jobs/start-invalid-all") { |
| 426 | const region = yield* Effect.sync(() => process.env.AWS_REGION); |
| 427 | const account = (yield* roleArn).split(":")[4]; |
| 428 | const tags = { |
| 429 | documentClassification: yield* startDocumentClassificationJob({ |
| 430 | ...invalidJobInput, |
| 431 | DocumentClassifierArn: `arn:aws:comprehend:${region}:${account}:document-classifier/alchemy-nonexistent`, |
| 432 | }).pipe( |
| 433 | Effect.map(() => "Started"), |
| 434 | Effect.catchTag( |
| 435 | ["ValidationException", "InvalidRequestException", "ResourceNotFoundException"], |
| 436 | (e) => Effect.succeed(e._tag), |
| 437 | ), |
| 438 | ), |
| 439 | dominantLanguage: yield* startDominantLanguageDetectionJob(invalidJobInput).pipe( |
| 440 | Effect.map(() => "Started"), |
| 441 | Effect.catchTag(["ValidationException", "InvalidRequestException"], (e) => |
| 442 | Effect.succeed(e._tag), |
| 443 | ), |
| 444 | ), |
| 445 | entities: yield* startEntitiesDetectionJob({ |
| 446 | ...invalidJobInput, |
| 447 | LanguageCode: "en", |
| 448 | }).pipe( |
| 449 | Effect.map(() => "Started"), |
| 450 | Effect.catchTag(["ValidationException", "InvalidRequestException"], (e) => |
| 451 | Effect.succeed(e._tag), |
| 452 | ), |
| 453 | ), |
| 454 | events: yield* startEventsDetectionJob({ |
| 455 | ...invalidJobInput, |
| 456 | LanguageCode: "en", |
| 457 | TargetEventTypes: ["BANKRUPTCY"], |
| 458 | }).pipe( |
| 459 | Effect.map(() => "Started"), |
| 460 | Effect.catchTag( |
| 461 | ["ValidationException", "InvalidRequestException", "NotAuthorizedException"], |
| 462 | (e) => Effect.succeed(e._tag), |
| 463 | ), |
| 464 | ), |
| 465 | keyPhrases: yield* startKeyPhrasesDetectionJob({ |
| 466 | ...invalidJobInput, |
| 467 | LanguageCode: "en", |
| 468 | }).pipe( |
| 469 | Effect.map(() => "Started"), |
| 470 | Effect.catchTag(["ValidationException", "InvalidRequestException"], (e) => |
| 471 | Effect.succeed(e._tag), |
| 472 | ), |
| 473 | ), |
| 474 | piiEntities: yield* startPiiEntitiesDetectionJob({ |
| 475 | ...invalidJobInput, |
| 476 | Mode: "ONLY_OFFSETS", |
| 477 | LanguageCode: "en", |
| 478 | }).pipe( |
| 479 | Effect.map(() => "Started"), |
| 480 | Effect.catchTag(["ValidationException", "InvalidRequestException"], (e) => |
| 481 | Effect.succeed(e._tag), |
| 482 | ), |
| 483 | ), |
| 484 | targetedSentiment: yield* startTargetedSentimentDetectionJob({ |
| 485 | ...invalidJobInput, |
| 486 | LanguageCode: "en", |
| 487 | }).pipe( |
| 488 | Effect.map(() => "Started"), |
| 489 | Effect.catchTag(["ValidationException", "InvalidRequestException"], (e) => |
| 490 | Effect.succeed(e._tag), |
| 491 | ), |
| 492 | ), |
| 493 | topics: yield* startTopicsDetectionJob(invalidJobInput).pipe( |
| 494 | Effect.map(() => "Started"), |
| 495 | Effect.catchTag(["ValidationException", "InvalidRequestException"], (e) => |
| 496 | Effect.succeed(e._tag), |
| 497 | ), |
| 498 | ), |
| 499 | }; |
| 500 | return yield* HttpServerResponse.json(tags); |
| 501 | } |
| 502 | |
| 503 | // Full real lifecycle for the sentiment job family: seed the input |
| 504 | // object, start a real async job, describe it, then stop it. |
| 505 | if (request.method === "POST" && pathname === "/jobs/sentiment/lifecycle") { |
| 506 | yield* putObject({ |
| 507 | Key: "comprehend-input/reviews.txt", |
| 508 | Body: "I love this product, it works wonderfully!\nThe delivery was late and the box arrived damaged.\n", |
| 509 | ContentType: "text/plain", |
| 510 | }); |
| 511 | // A freshly created data-access role can take a few seconds to |
| 512 | // become assumable by Comprehend — surfaced as |
| 513 | // InvalidRequestException. Bounded retry (8 × 5s). |
| 514 | const started = yield* startSentimentDetectionJob({ |
| 515 | ...jobInput, |
| 516 | JobName: "alchemy-comprehend-bindings-lifecycle", |
| 517 | LanguageCode: "en", |
| 518 | }).pipe( |
| 519 | Effect.retry({ |
| 520 | while: (e): boolean => e._tag === "InvalidRequestException", |
| 521 | schedule: Schedule.spaced("5 seconds"), |
| 522 | times: 8, |
| 523 | }), |
| 524 | ); |
| 525 | const jobId = started.JobId ?? ""; |
| 526 | const described = yield* describeSentimentDetectionJob({ |
| 527 | JobId: jobId, |
| 528 | }); |
| 529 | const stopped = yield* stopSentimentDetectionJob({ JobId: jobId }); |
| 530 | return yield* HttpServerResponse.json({ |
| 531 | jobId, |
| 532 | startStatus: started.JobStatus, |
| 533 | describedStatus: described.SentimentDetectionJobProperties?.JobStatus, |
| 534 | stopStatus: stopped.JobStatus, |
| 535 | }); |
| 536 | } |
| 537 | |
| 538 | return yield* HttpServerResponse.json( |
| 539 | { error: "Not found", method: request.method, pathname }, |
| 540 | { status: 404 }, |
| 541 | ); |
| 542 | }).pipe(Effect.orDie), |
| 543 | }; |
| 544 | }).pipe( |
| 545 | Effect.provide( |
| 546 | Layer.mergeAll( |
| 547 | S3.PutObjectHttp, |
| 548 | Comprehend.DetectDominantLanguageHttp, |
| 549 | Comprehend.DetectEntitiesHttp, |
| 550 | Comprehend.DetectKeyPhrasesHttp, |
| 551 | Comprehend.DetectPiiEntitiesHttp, |
| 552 | Comprehend.DetectSentimentHttp, |
| 553 | Comprehend.DetectSyntaxHttp, |
| 554 | Comprehend.DetectTargetedSentimentHttp, |
| 555 | Comprehend.DetectToxicContentHttp, |
| 556 | Comprehend.ContainsPiiEntitiesHttp, |
| 557 | Comprehend.ClassifyDocumentHttp, |
| 558 | Comprehend.BatchDetectDominantLanguageHttp, |
| 559 | Comprehend.BatchDetectEntitiesHttp, |
| 560 | Comprehend.BatchDetectKeyPhrasesHttp, |
| 561 | Comprehend.BatchDetectSentimentHttp, |
| 562 | Comprehend.BatchDetectSyntaxHttp, |
| 563 | Comprehend.BatchDetectTargetedSentimentHttp, |
| 564 | Comprehend.StartDocumentClassificationJobHttp, |
| 565 | Comprehend.StartDominantLanguageDetectionJobHttp, |
| 566 | Comprehend.StartEntitiesDetectionJobHttp, |
| 567 | Comprehend.StartEventsDetectionJobHttp, |
| 568 | Comprehend.StartKeyPhrasesDetectionJobHttp, |
| 569 | Comprehend.StartPiiEntitiesDetectionJobHttp, |
| 570 | Comprehend.StartSentimentDetectionJobHttp, |
| 571 | Comprehend.StartTargetedSentimentDetectionJobHttp, |
| 572 | Comprehend.StartTopicsDetectionJobHttp, |
| 573 | Comprehend.DescribeDocumentClassificationJobHttp, |
| 574 | Comprehend.DescribeDominantLanguageDetectionJobHttp, |
| 575 | Comprehend.DescribeEntitiesDetectionJobHttp, |
| 576 | Comprehend.DescribeEventsDetectionJobHttp, |
| 577 | Comprehend.DescribeKeyPhrasesDetectionJobHttp, |
| 578 | Comprehend.DescribePiiEntitiesDetectionJobHttp, |
| 579 | Comprehend.DescribeSentimentDetectionJobHttp, |
| 580 | Comprehend.DescribeTargetedSentimentDetectionJobHttp, |
| 581 | Comprehend.DescribeTopicsDetectionJobHttp, |
| 582 | Comprehend.ListDocumentClassificationJobsHttp, |
| 583 | Comprehend.ListDominantLanguageDetectionJobsHttp, |
| 584 | Comprehend.ListEntitiesDetectionJobsHttp, |
| 585 | Comprehend.ListEventsDetectionJobsHttp, |
| 586 | Comprehend.ListKeyPhrasesDetectionJobsHttp, |
| 587 | Comprehend.ListPiiEntitiesDetectionJobsHttp, |
| 588 | Comprehend.ListSentimentDetectionJobsHttp, |
| 589 | Comprehend.ListTargetedSentimentDetectionJobsHttp, |
| 590 | Comprehend.ListTopicsDetectionJobsHttp, |
| 591 | Comprehend.StopDominantLanguageDetectionJobHttp, |
| 592 | Comprehend.StopEntitiesDetectionJobHttp, |
| 593 | Comprehend.StopEventsDetectionJobHttp, |
| 594 | Comprehend.StopKeyPhrasesDetectionJobHttp, |
| 595 | Comprehend.StopPiiEntitiesDetectionJobHttp, |
| 596 | Comprehend.StopSentimentDetectionJobHttp, |
| 597 | Comprehend.StopTargetedSentimentDetectionJobHttp, |
| 598 | ), |
| 599 | ), |
| 600 | ), |
| 601 | ); |