The best way to control Claude Code, Codex and more from any computer or phone

main
ts 119 lines 3.85 KB
Raw
1 import * as Context from "effect/Context";
2 import * as Effect from "effect/Effect";
3 import * as Exit from "effect/Exit";
4 import * as Schema from "effect/Schema";
5 import { Workflow } from "effect/workflow";
6
7 /**
8 * How the workflow engines store what a run produces as JSON: step and signal
9 * exits, payloads, and a workflow's own result.
10 *
11 * @internal
12 */
13
14 const ExitJson = Schema.toCodecJson(Schema.Exit(Schema.Unknown, Schema.Unknown, Schema.Defect()));
15
16 /**
17 * Activity and deferred exits arrive with their values already encoded; a
18 * void value is stored as null, since undefined isn't JSON.
19 */
20 export const encodeExit = (exit: Exit.Exit<unknown, unknown>) =>
21 Schema.encodeEffect(ExitJson)(Exit.map(exit, (value) => value ?? null)).pipe(
22 Effect.map(JSON.stringify),
23 Effect.orDie,
24 );
25
26 export const decodeExit = (text: string) =>
27 Schema.decodeEffect(ExitJson)(JSON.parse(text)).pipe(Effect.orDie);
28
29 /**
30 * The schemas a workflow's payload and result are stored through. Concrete
31 * `Workflow`s don't assign to Effect's own `Workflow.AnyWithProps` (its
32 * `execute` options), so accept just what encoding reads.
33 */
34 export type WorkflowSchemas = Pick<
35 Workflow.AnyWithProps,
36 "payloadSchema" | "successSchema" | "errorSchema"
37 >;
38
39 export const payloadCodec = (workflow: WorkflowSchemas) =>
40 Schema.toCodecJson(workflow.payloadSchema) as unknown as Schema.Codec<unknown, unknown>;
41
42 interface ResultCodec {
43 readonly encode: (
44 exit: Exit.Exit<unknown, unknown>,
45 ) => Effect.Effect<unknown, Schema.SchemaError>;
46 readonly decode: (
47 value: unknown,
48 ) => Effect.Effect<Exit.Exit<unknown, unknown>, Schema.SchemaError>;
49 }
50
51 // Schemas are immutable values. Key by both current schema objects instead of
52 // the workflow name or definition, so replaced schemas never reuse a stale codec.
53 const resultCodecs = new WeakMap<Schema.Top, WeakMap<Schema.Top, ResultCodec>>();
54
55 const resultCodec = (workflow: WorkflowSchemas): ResultCodec => {
56 const { successSchema, errorSchema } = workflow;
57 let errors = resultCodecs.get(successSchema);
58 if (errors === undefined) {
59 errors = new WeakMap();
60 resultCodecs.set(successSchema, errors);
61 }
62 let cached = errors.get(errorSchema);
63 if (cached === undefined) {
64 const codec = Schema.toCodecJson(
65 Schema.Exit(successSchema, errorSchema, Schema.Defect()),
66 ) as unknown as Schema.Codec<Exit.Exit<unknown, unknown>, unknown>;
67 cached = {
68 encode: Schema.encodeEffect(codec),
69 decode: Schema.decodeEffect(codec),
70 };
71 errors.set(errorSchema, cached);
72 }
73 return cached;
74 };
75
76 /** A workflow's own result is stored through its success and error schemas. */
77 export const encodeResult = (workflow: WorkflowSchemas, exit: Exit.Exit<unknown, unknown>) =>
78 resultCodec(workflow).encode(exit).pipe(Effect.map(JSON.stringify), Effect.orDie);
79
80 export const decodeResult = (workflow: WorkflowSchemas, text: string) =>
81 resultCodec(workflow)
82 .decode(JSON.parse(text))
83 .pipe(
84 Effect.map((exit) => new Workflow.Complete({ exit })),
85 Effect.orDie,
86 );
87
88 /**
89 * The step a workflow engine is running: provided around each `Activity` so
90 * the step's own work can derive stable ids from it. A step that runs again,
91 * after a retry or a crash, sees the same values.
92 *
93 * @internal
94 */
95 export class CurrentActivity extends Context.Service<
96 CurrentActivity,
97 {
98 readonly workflow: string;
99 readonly executionId: string;
100 readonly activity: string;
101 /** The next id in this step's sequence, restarting on every attempt. */
102 readonly next: () => string;
103 }
104 >()("alchemy/Workflow/CurrentActivity") {}
105
106 /** @internal */
107 export const makeCurrentActivity = (
108 workflow: string,
109 executionId: string,
110 activity: string,
111 ): CurrentActivity["Service"] => {
112 let sequence = 0;
113 return {
114 workflow,
115 executionId,
116 activity,
117 next: () => `${workflow}/${executionId}/${activity}/${++sequence}`,
118 };
119 };