@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, 04:45 UTC

main
ts 59 lines 1.89 KB
Raw
1 import { Effect, Layer, Schema } from "effect";
2 import { Rpc, RpcGroup, RpcSerialization, RpcServer } from "effect/rpc";
3 import { Job, JobId } from "./Job.ts";
4 import { JobNotifications } from "./JobNotifications.ts";
5 import { JobStorage } from "./JobStorage.ts";
6
7 export class JobNotFound extends Schema.TaggedClass<JobNotFound>()("JobNotFound", {
8 jobId: JobId,
9 }) {}
10
11 export class GetJobFailed extends Schema.TaggedClass<GetJobFailed>()("GetJobFailed", {
12 message: Schema.String,
13 }) {}
14
15 export class PutJobFailed extends Schema.TaggedClass<PutJobFailed>()("PutJobFailed", {
16 message: Schema.String,
17 }) {}
18
19 const getJob = Rpc.make("getJob", {
20 success: Job,
21 error: Schema.Union([JobNotFound, GetJobFailed]),
22 payload: { jobId: JobId },
23 });
24
25 const createJob = Rpc.make("createJob", {
26 success: JobId,
27 error: PutJobFailed,
28 payload: { content: Schema.String },
29 });
30
31 export class JobRpcs extends RpcGroup.make(getJob, createJob) {}
32
33 export const JobRpcsLive = JobRpcs.toLayer(
34 Effect.gen(function* () {
35 const jobService = yield* JobStorage;
36 const notifications = yield* JobNotifications;
37
38 return {
39 getJob: ({ jobId }) =>
40 jobService.getJob(jobId).pipe(
41 Effect.mapError((error) => new GetJobFailed({ message: error.message })),
42 Effect.flatMap((job) =>
43 job ? Effect.succeed(job) : Effect.fail(new JobNotFound({ jobId })),
44 ),
45 ),
46 createJob: ({ content }) =>
47 Effect.gen(function* () {
48 const jobId = crypto.randomUUID();
49 const job = yield* jobService.putJob({ id: jobId, content });
50 yield* notifications.notifyJobCreated(job);
51 return job.id;
52 }).pipe(Effect.mapError((error) => new PutJobFailed({ message: error.message }))),
53 };
54 }),
55 );
56
57 export const JobRpcHttpEffect = RpcServer.toHttpEffect(JobRpcs).pipe(
58 Effect.provide(Layer.mergeAll(JobRpcsLive, RpcSerialization.layerJson)),
59 );