@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, 06:00 UTC

main
ts 123 lines 4.69 KB
Raw
1 import * as Workflows from "alchemy/Workflows";
2 import * as Cause from "effect/Cause";
3 import * as Effect from "effect/Effect";
4 import { HttpServerRequest } from "effect/http/HttpServerRequest";
5 import * as HttpServerResponse from "effect/http/HttpServerResponse";
6 import { WorkflowEngine } from "effect/workflow";
7 import { Journal } from "./journal.ts";
8 import {
9 Approval,
10 Approve,
11 Checkout,
12 Crash,
13 Fanout,
14 Flaky,
15 Greet,
16 Guarded,
17 Hold,
18 Nap,
19 Naps,
20 Parent,
21 ParentOfFailing,
22 ParentOfWaiting,
23 Race,
24 Recover,
25 Retry,
26 Suspends,
27 WakeInStep,
28 } from "./workflows.ts";
29
30 /**
31 * The routes the conformance cases drive, whichever engine runs the workflows:
32 *
33 * - `POST /start/<workflow>/<id>?…` starts a run and answers its execution id
34 * - `GET /status/<workflow>/<id>?executionId=…` reads its status
35 * - `POST /approve/<workflow>/<id>?executionId=…&by=…` signals its approval
36 * - `POST /interrupt/<workflow>/<id>?executionId=…` interrupts it
37 * - `POST /resume/Suspends/<id>?executionId=…` resumes a run suspended on failure
38 * - `GET /journal/<id>` lists what its activities recorded
39 */
40 export const app = Effect.gen(function* () {
41 const journal = yield* Journal;
42 const engine = yield* WorkflowEngine.WorkflowEngine;
43 const workflows = {
44 Greet: yield* Workflows.RunWorkflow(Greet),
45 Nap: yield* Workflows.RunWorkflow(Nap),
46 Naps: yield* Workflows.RunWorkflow(Naps),
47 Approve: yield* Workflows.RunWorkflow(Approve),
48 Hold: yield* Workflows.RunWorkflow(Hold),
49 Checkout: yield* Workflows.RunWorkflow(Checkout),
50 Parent: yield* Workflows.RunWorkflow(Parent),
51 Flaky: yield* Workflows.RunWorkflow(Flaky),
52 Crash: yield* Workflows.RunWorkflow(Crash),
53 Retry: yield* Workflows.RunWorkflow(Retry),
54 WakeInStep: yield* Workflows.RunWorkflow(WakeInStep),
55 Guarded: yield* Workflows.RunWorkflow(Guarded),
56 Race: yield* Workflows.RunWorkflow(Race),
57 ParentOfWaiting: yield* Workflows.RunWorkflow(ParentOfWaiting),
58 Suspends: yield* Workflows.RunWorkflow(Suspends),
59 Recover: yield* Workflows.RunWorkflow(Recover),
60 Fanout: yield* Workflows.RunWorkflow(Fanout),
61 ParentOfFailing: yield* Workflows.RunWorkflow(ParentOfFailing),
62 };
63 return {
64 fetch: Effect.gen(function* () {
65 const request = yield* HttpServerRequest;
66 const url = new URL(request.originalUrl, "http://conformance");
67 const [, operation, name, id] = url.pathname.split("/");
68 if (operation === "ready") return HttpServerResponse.text("ready");
69 if (operation === "journal" && name) {
70 return yield* HttpServerResponse.json(yield* journal.list(name));
71 }
72 const workflow = workflows[name as keyof typeof workflows] as typeof workflows.Checkout;
73 if (!workflow || !id) return HttpServerResponse.text("Not Found", { status: 404 });
74 // Every workflow's payload fields; each workflow reads the ones it declares.
75 const payload = {
76 id,
77 name: url.searchParams.get("name") ?? "world",
78 seconds: Number(url.searchParams.get("seconds") ?? "2"),
79 count: Number(url.searchParams.get("count") ?? "1"),
80 decline: url.searchParams.get("decline") === "true",
81 abandon: url.searchParams.get("abandon") === "true",
82 };
83 const run = workflow.get(url.searchParams.get("executionId") ?? "");
84 switch (operation) {
85 case "start": {
86 const started = yield* workflow.start(payload);
87 return yield* HttpServerResponse.json({ executionId: started.executionId });
88 }
89 case "status": {
90 const status = yield* run.status;
91 // A defect is often an Error, whose message JSON leaves out.
92 return yield* HttpServerResponse.json(
93 status._tag === "Died" ? { _tag: "Died", defect: messageOf(status.defect) } : status,
94 );
95 }
96 case "resume": {
97 yield* engine.resume(Suspends, url.searchParams.get("executionId") ?? "");
98 return HttpServerResponse.text("ok");
99 }
100 case "approve": {
101 yield* run.signal(Approval, url.searchParams.get("by") ?? "nobody");
102 return HttpServerResponse.text("ok");
103 }
104 case "interrupt": {
105 yield* run.interrupt;
106 return HttpServerResponse.text("ok");
107 }
108 default:
109 return HttpServerResponse.text("Not Found", { status: 404 });
110 }
111 }).pipe(
112 // Report why a request failed, so a failing case shows it.
113 Effect.catchCause((cause) =>
114 Effect.succeed(HttpServerResponse.text(Cause.pretty(cause), { status: 500 })),
115 ),
116 ),
117 };
118 });
119
120 const messageOf = (defect: unknown) =>
121 typeof defect === "object" && defect !== null && "message" in defect
122 ? String(defect.message)
123 : String(defect);