@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 259 lines 9.61 KB
Raw
1 import { msg } from "gt-i18n";
2 import { MarkedText, messageFromError, translatableMessage } from "../errors/translatable.ts";
3 import type { EnvironmentId } from "@t3tools/contracts";
4 import * as Effect from "effect/Effect";
5 import * as Equal from "effect/Equal";
6 import * as Exit from "effect/Exit";
7 import * as Fiber from "effect/Fiber";
8 import * as Hash from "effect/Hash";
9 import * as RcMap from "effect/RcMap";
10 import * as Schema from "effect/Schema";
11 import * as Scope from "effect/Scope";
12 import * as Semaphore from "effect/Semaphore";
13
14 export class AttachmentUploadError extends Schema.TaggedError<AttachmentUploadError>()(
15 "AttachmentUploadError",
16 { message: Schema.String, i18nMessage: Schema.optionalKey(MarkedText), cause: Schema.Defect() },
17 ) {}
18
19 const isAttachmentUploadError = Schema.is(AttachmentUploadError);
20
21 export function attachmentUploadError(cause: unknown): AttachmentUploadError {
22 if (isAttachmentUploadError(cause)) return cause;
23 return new AttachmentUploadError({
24 ...(cause instanceof Error
25 ? messageFromError(cause)
26 : translatableMessage(msg("Upload failed. Tap to retry."))),
27 cause,
28 });
29 }
30
31 export type AttachmentUploadStatus =
32 /** `retrying` marks an upload started again after it failed. */
33 | { readonly status: "uploading"; readonly progress: number; readonly retrying?: true }
34 | { readonly status: "ready" }
35 | { readonly status: "failed"; readonly reason: string };
36
37 const uploading = (progress: number, retry: boolean): AttachmentUploadStatus =>
38 retry ? { status: "uploading", progress, retrying: true } : { status: "uploading", progress };
39
40 /** Equal by id; the request and retry flag only matter when starting a transfer. */
41 class UploadKey<Request> implements Equal.Equal {
42 readonly id: string;
43 readonly request: Request | null;
44 readonly retry: boolean;
45 constructor(id: string, request: Request | null, retry = false) {
46 this.id = id;
47 this.request = request;
48 this.retry = retry;
49 }
50 [Equal.symbol](that: unknown): boolean {
51 return that instanceof UploadKey && that.id === this.id;
52 }
53 [Hash.symbol](): number {
54 return Hash.string(this.id);
55 }
56 }
57
58 /**
59 * Every upload of an attachment, shared by key. Holders keep an upload running, and anyone who
60 * needs its result joins the running upload instead of starting another. An upload is
61 * interrupted once nothing holds it.
62 */
63 export function createAttachmentUploadRegistry<Request>(options: {
64 readonly key: (request: Request) => string;
65 readonly environmentId: (request: Request) => EnvironmentId;
66 /** Uploads running at once against one environment; the rest wait their turn. */
67 readonly concurrencyPerEnvironment: number;
68 /** Uploads one attachment and returns its uploaded id; interrupting it cancels the transfer. */
69 readonly upload: (
70 request: Request,
71 reportProgress: (progress: number) => void,
72 ) => Effect.Effect<string, AttachmentUploadError>;
73 /** Saves a finished upload's id; the upload is ready once this succeeds. */
74 readonly onUploaded?: (
75 request: Request,
76 uploadedId: string,
77 ) => Effect.Effect<void, AttachmentUploadError>;
78 readonly onChange?: (statuses: Readonly<Record<string, AttachmentUploadStatus>>) => void;
79 }) {
80 let statuses: Readonly<Record<string, AttachmentUploadStatus>> = {};
81 function setStatus(key: string, status: AttachmentUploadStatus | undefined) {
82 const previous = statuses[key];
83 if (
84 previous === status ||
85 (previous?.status === "uploading" &&
86 status?.status === "uploading" &&
87 previous.progress === status.progress &&
88 previous.retrying === status.retrying)
89 )
90 return;
91 const next = { ...statuses };
92 if (status) next[key] = status;
93 else delete next[key];
94 statuses = next;
95 options.onChange?.(statuses);
96 }
97
98 const permits = new Map<EnvironmentId, Semaphore.Semaphore>();
99 const permitsFor = (environmentId: EnvironmentId) => {
100 let semaphore = permits.get(environmentId);
101 if (semaphore === undefined) {
102 semaphore = Semaphore.makeUnsafe(options.concurrencyPerEnvironment);
103 permits.set(environmentId, semaphore);
104 }
105 return semaphore;
106 };
107 const running = new Map<string, Fiber.Fiber<string, AttachmentUploadError>>();
108 // The entry that owns each key's status: an invalidated entry finishing or closing late must
109 // not overwrite or clear the status of the entry that replaced it.
110 const owners = new Map<string, object>();
111 const setOwnStatus = (owner: object, key: string, status: AttachmentUploadStatus | undefined) => {
112 if (owners.get(key) === owner) setStatus(key, status);
113 };
114
115 const transfer = (key: UploadKey<Request>, owner: object) => {
116 const request = key.request!;
117 return Effect.suspend(() => {
118 if (statuses[key.id]?.status !== "uploading") {
119 setOwnStatus(owner, key.id, uploading(0, key.retry));
120 }
121 return options.upload(request, (progress) =>
122 setOwnStatus(
123 owner,
124 key.id,
125 uploading(Math.floor(Math.max(0, Math.min(1, progress)) * 20) / 20, key.retry),
126 ),
127 );
128 }).pipe(
129 Semaphore.withPermits(permitsFor(options.environmentId(request)), 1),
130 Effect.tap((uploadedId) => options.onUploaded?.(request, uploadedId) ?? Effect.void),
131 Effect.tap(() => Effect.sync(() => setOwnStatus(owner, key.id, { status: "ready" }))),
132 Effect.tapError((error) =>
133 Effect.sync(() => setOwnStatus(owner, key.id, { status: "failed", reason: error.message })),
134 ),
135 );
136 };
137
138 const scope = Scope.makeUnsafe();
139 const uploads = Effect.runSync(
140 Scope.provide(scope)(
141 RcMap.make({
142 lookup: Effect.fn("AttachmentUploads.lookup")(function* (key: UploadKey<Request>) {
143 const owner = {};
144 owners.set(key.id, owner);
145 setStatus(key.id, uploading(0, key.retry));
146 yield* Effect.addFinalizer(() =>
147 Effect.sync(() => {
148 setOwnStatus(owner, key.id, undefined);
149 if (owners.get(key.id) === owner) owners.delete(key.id);
150 }),
151 );
152 // Starts in the holder's call, so an upload begins as soon as something holds it.
153 const fiber = yield* Effect.forkScoped(transfer(key, owner), {
154 startImmediately: true,
155 });
156 running.set(key.id, fiber);
157 fiber.addObserver(() => {
158 if (running.get(key.id) === fiber) running.delete(key.id);
159 });
160 return fiber;
161 }),
162 }),
163 ),
164 );
165 let disposed = false;
166 function holdUpload(key: UploadKey<Request>): () => void {
167 if (disposed) return () => {};
168 const holder = Scope.makeUnsafe();
169 Effect.runSync(Scope.provide(holder)(RcMap.get(uploads, key)));
170 let released = false;
171 return () => {
172 if (released) return;
173 released = true;
174 Effect.runFork(Scope.close(holder, Exit.void));
175 };
176 }
177
178 const synced = new Map<string, () => void>();
179 let requested: ReadonlyArray<Request> = [];
180 function syncUploads(requests: ReadonlyArray<Request>, retryKey?: string) {
181 if (disposed) return;
182 requested = requests;
183 const keys = new Map(requests.map((request) => [options.key(request), request]));
184 for (const [id, release] of synced) {
185 if (keys.has(id)) continue;
186 synced.delete(id);
187 release();
188 }
189 for (const [id, request] of keys) {
190 if (!synced.has(id)) synced.set(id, holdUpload(new UploadKey(id, request, id === retryKey)));
191 }
192 }
193
194 /** Drops a failed upload so the next holder starts it again. */
195 function invalidate(key: string) {
196 Effect.runSync(RcMap.invalidate(uploads, new UploadKey<Request>(key, null)));
197 }
198
199 /** Drops the key's upload and replaces it if `sync` holds it. */
200 function restart(key: string) {
201 const retry = statuses[key]?.status === "failed";
202 invalidate(key);
203 synced.get(key)?.();
204 synced.delete(key);
205 syncUploads(requested, retry ? key : undefined);
206 }
207
208 return {
209 /** Keeps the request's upload running until the returned release is called. */
210 hold(request: Request): () => void {
211 return holdUpload(new UploadKey(options.key(request), request));
212 },
213 /** Holds exactly these uploads; anything no longer requested and held nowhere else stops. */
214 sync(requests: ReadonlyArray<Request>) {
215 syncUploads(requests);
216 },
217 invalidate,
218 restart,
219 /**
220 * Uploads the request, joining its running or finished upload. A failed one starts again,
221 * so a retrying send never just rejoins the failure.
222 */
223 upload(request: Request): Effect.Effect<string, AttachmentUploadError> {
224 return Effect.suspend(() => {
225 if (disposed) return Effect.fail(attachmentUploadError(new Error("Upload cancelled.")));
226 const key = options.key(request);
227 const retry = statuses[key]?.status === "failed";
228 if (retry) restart(key);
229 return RcMap.get(uploads, new UploadKey(key, request, retry)).pipe(
230 Effect.flatMap(Fiber.join),
231 Effect.scoped,
232 );
233 });
234 },
235 /** Starts a failed upload that `sync` holds again. */
236 retry(key: string) {
237 if (statuses[key]?.status === "failed") restart(key);
238 },
239 /** The key's running upload, to await it without holding it. */
240 current(key: string): Fiber.Fiber<string, AttachmentUploadError> | undefined {
241 return running.get(key);
242 },
243 status(key: string): AttachmentUploadStatus | undefined {
244 return statuses[key];
245 },
246 async settled() {
247 while (running.size > 0) {
248 await Effect.runPromise(Fiber.awaitAll([...running.values()]));
249 }
250 },
251 dispose() {
252 disposed = true;
253 synced.clear();
254 Effect.runFork(Scope.close(scope, Exit.void));
255 statuses = {};
256 options.onChange?.(statuses);
257 },
258 };
259 }