The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import * as Effect from "effect/Effect"; |
| 2 | import * as HttpApi from "effect/http-api/HttpApi"; |
| 3 | import * as HttpApiBuilder from "effect/http-api/HttpApiBuilder"; |
| 4 | import * as HttpApiEndpoint from "effect/http-api/HttpApiEndpoint"; |
| 5 | import * as HttpApiGroup from "effect/http-api/HttpApiGroup"; |
| 6 | import * as HttpServerResponse from "effect/http/HttpServerResponse"; |
| 7 | import * as Layer from "effect/Layer"; |
| 8 | import * as Schema from "effect/Schema"; |
| 9 | import { Job, JobId } from "./Job.ts"; |
| 10 | import { JobNotifications, NotifyJobError } from "./JobNotifications.ts"; |
| 11 | import { GetJobError, JobStorage, PutJobError } from "./JobStorage.ts"; |
| 12 | |
| 13 | export const getJob = HttpApiEndpoint.get("getJob", "/", { |
| 14 | success: Job, |
| 15 | query: { |
| 16 | jobId: JobId.pipe(Schema.optional), |
| 17 | }, |
| 18 | }); |
| 19 | |
| 20 | export const createJob = HttpApiEndpoint.post("createJob", "/", { |
| 21 | success: JobId, |
| 22 | payload: Schema.Struct({ |
| 23 | content: Schema.String, |
| 24 | }), |
| 25 | }); |
| 26 | |
| 27 | export const JobApi = HttpApi.make("JobApi").add(HttpApiGroup.make("Jobs").add(getJob, createJob)); |
| 28 | |
| 29 | export const JobApiLive = HttpApiBuilder.layer(JobApi).pipe( |
| 30 | Layer.provide( |
| 31 | HttpApiBuilder.group(JobApi, "Jobs", (handlers) => |
| 32 | Effect.gen(function* () { |
| 33 | const jobService = yield* JobStorage; |
| 34 | const notifications = yield* JobNotifications; |
| 35 | |
| 36 | return handlers |
| 37 | .handle( |
| 38 | "getJob", |
| 39 | Effect.fn(function* (req) { |
| 40 | if (!req.query.jobId) { |
| 41 | return HttpServerResponse.text("Job ID is required", { |
| 42 | status: 400, |
| 43 | }); |
| 44 | } |
| 45 | const job = yield* jobService.getJob(req.query.jobId).pipe( |
| 46 | Effect.catchTag("GetJobError", (error) => |
| 47 | Effect.succeed( |
| 48 | HttpServerResponse.text(error.message, { |
| 49 | status: 500, |
| 50 | }), |
| 51 | ), |
| 52 | ), |
| 53 | ); |
| 54 | if (job instanceof GetJobError) { |
| 55 | return HttpServerResponse.text(job.message, { |
| 56 | status: 500, |
| 57 | }); |
| 58 | } |
| 59 | if (!job) { |
| 60 | return HttpServerResponse.text("Job not found", { |
| 61 | status: 404, |
| 62 | }); |
| 63 | } |
| 64 | return job!; |
| 65 | }), |
| 66 | ) |
| 67 | .handle( |
| 68 | "createJob", |
| 69 | Effect.fn(function* (req) { |
| 70 | const jobId = crypto.randomUUID(); |
| 71 | const job = yield* jobService |
| 72 | .putJob({ |
| 73 | id: jobId, |
| 74 | content: req.payload.content, |
| 75 | }) |
| 76 | .pipe(Effect.catchTag("PutJobError", (error) => Effect.succeed(error))); |
| 77 | if (job instanceof PutJobError) { |
| 78 | return HttpServerResponse.text(job.message, { |
| 79 | status: 500, |
| 80 | }); |
| 81 | } |
| 82 | const notificationResult = yield* notifications |
| 83 | .notifyJobCreated(job) |
| 84 | .pipe(Effect.catchTag("NotifyJobError", (error) => Effect.succeed(error))); |
| 85 | if (notificationResult instanceof NotifyJobError) { |
| 86 | return HttpServerResponse.text(notificationResult.message, { |
| 87 | status: 500, |
| 88 | }); |
| 89 | } |
| 90 | return job.id; |
| 91 | }), |
| 92 | ); |
| 93 | }), |
| 94 | ), |
| 95 | ), |
| 96 | ); |