@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 140 lines 4.32 KB
Raw
1 import * as Context from "effect/Context";
2 import * as Deferred from "effect/Deferred";
3 import * as Effect from "effect/Effect";
4 import * as Layer from "effect/Layer";
5 import * as Option from "effect/Option";
6
7 /**
8 * Per-resource in-memory artifacts shared across a single `Plan.make -> apply`
9 * execution.
10 *
11 * The engine scopes this service by resource `FQN` before invoking lifecycle
12 * handlers, so providers should treat it as a resource-local bag for expensive,
13 * deterministic intermediate results that can be reused across phases.
14 *
15 * Expected usage:
16 *
17 * - `diff` computes an expensive artifact once and stores it
18 * - `create` / `update` reads the same artifact and skips recomputing it
19 * - artifacts are ephemeral and must never be required for correctness on a
20 * later deploy because the bag is reset between runs
21 *
22 * Example:
23 *
24 * ```ts
25 * const artifacts = yield* Artifacts;
26 * const cached = yield* artifacts.get<PreparedBundle>("bundle");
27 * if (cached) return cached;
28 *
29 * const bundle = yield* prepareBundle();
30 * yield* artifacts.set("bundle", bundle);
31 * return bundle;
32 * ```
33 */
34 export class Artifacts extends Context.Service<
35 Artifacts,
36 {
37 /**
38 * Get an artifact by key from the current resource's bag.
39 */
40 get<T>(key: string): Effect.Effect<T | undefined>;
41 /**
42 * Store an artifact by key in the current resource's bag.
43 */
44 set<T>(key: string, value: T): Effect.Effect<void>;
45 /**
46 * Delete an artifact by key from the current resource's bag.
47 */
48 delete(key: string): Effect.Effect<void>;
49 }
50 >()("Artifacts") {}
51
52 type ArtifactBag = Map<string, unknown>;
53
54 export class ArtifactStore extends Context.Service<ArtifactStore, Map<string, ArtifactBag>>()(
55 "Artifacts/Store",
56 ) {}
57
58 /**
59 * Create a fresh root store for one deploy/test run.
60 */
61 export const createArtifactStore = (): ArtifactStore["Service"] => new Map<string, ArtifactBag>();
62
63 const getOrCreateBag = (store: Map<string, ArtifactBag>, fqn: string): ArtifactBag => {
64 const existing = store.get(fqn);
65 if (existing) {
66 return existing;
67 }
68 const bag = new Map<string, unknown>();
69 store.set(fqn, bag);
70 return bag;
71 };
72
73 export const makeScopedArtifacts = (
74 store: Map<string, ArtifactBag>,
75 fqn: string,
76 ): Artifacts["Service"] => {
77 const bag = getOrCreateBag(store, fqn);
78 return {
79 get: <T>(key: string) => Effect.sync(() => bag.get(key) as T | undefined),
80 set: <T>(key: string, value: T) =>
81 Effect.sync(() => {
82 bag.set(key, value);
83 }),
84 delete: (key: string) =>
85 Effect.sync(() => {
86 bag.delete(key);
87 }),
88 };
89 };
90
91 export const scopedArtifacts = (fqn: string): Layer.Layer<Artifacts, never, ArtifactStore> =>
92 Layer.effect(
93 Artifacts,
94 ArtifactStore.useSync((store) => makeScopedArtifacts(store, fqn)),
95 );
96
97 /**
98 * Run an effect with a fresh artifact root, replacing any existing store.
99 * Use this at top-level entrypoints that intentionally define a new deploy run.
100 */
101 export const provideFreshArtifactStore = <A, E, R>(
102 effect: Effect.Effect<A, E, R | ArtifactStore>,
103 ): Effect.Effect<A, E, R> =>
104 effect.pipe(Effect.provideServiceEffect(ArtifactStore, Effect.sync(createArtifactStore)));
105
106 /**
107 * Ensure an artifact root exists, reusing the ambient store when one is already
108 * present. This lets nested helpers participate in the same run-scoped cache.
109 */
110 export const ensureArtifactStore = <A, E, R>(
111 effect: Effect.Effect<A, E, R | ArtifactStore>,
112 ): Effect.Effect<A, E, R> =>
113 Effect.serviceOption(ArtifactStore).pipe(
114 Effect.map(Option.getOrUndefined),
115 Effect.flatMap((existing) =>
116 effect.pipe(Effect.provideService(ArtifactStore, existing ?? createArtifactStore())),
117 ),
118 );
119
120 export const cached =
121 (id: string) =>
122 <A, Err = never, Req = never>(
123 eff: Effect.Effect<A, Err, Req>,
124 ): Effect.Effect<A, Err, Req | Artifacts> =>
125 Effect.gen(function* () {
126 const artifacts = yield* Artifacts;
127 const deferred = yield* Deferred.make<A>();
128 const cached = yield* artifacts.get<A | Effect.Effect<A>>(id);
129 if (cached) {
130 if (Effect.isEffect(cached)) {
131 return yield* cached;
132 }
133 return cached;
134 }
135 yield* artifacts.set(id, Deferred.await(deferred));
136 const result = yield* eff;
137 yield* Deferred.succeed(deferred, result);
138 yield* artifacts.set(id, result);
139 return result;
140 });