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, 07:15 UTC
| 1 | import * as Data from "effect/Data"; |
| 2 | import * as Effect from "effect/Effect"; |
| 3 | import * as HttpClient from "effect/http/HttpClient"; |
| 4 | import * as HttpClientResponse from "effect/http/HttpClientResponse"; |
| 5 | import * as Schedule from "effect/Schedule"; |
| 6 | import type { RecorderSnapshot } from "./queue-sink-worker.ts"; |
| 7 | |
| 8 | /** Test-side HTTP helpers shared by the live and local QueueSink suites. */ |
| 9 | |
| 10 | class WorkerNotReady extends Data.TaggedError("WorkerNotReady")<{ |
| 11 | run: string; |
| 12 | status: number; |
| 13 | }> { |
| 14 | override get message() { |
| 15 | return `POST /produce for run "${this.run}" answered ${this.status}`; |
| 16 | } |
| 17 | } |
| 18 | |
| 19 | class NotDrained extends Data.TaggedError("NotDrained")<{ |
| 20 | run: string; |
| 21 | expected: number; |
| 22 | distinct: number; |
| 23 | }> { |
| 24 | override get message() { |
| 25 | return `run "${this.run}": ${this.distinct}/${this.expected} distinct messages drained`; |
| 26 | } |
| 27 | } |
| 28 | |
| 29 | /** |
| 30 | * `POST /produce`, retrying while a fresh workers.dev URL comes up: its 404 |
| 31 | * placeholder has been observed to outlast 40 seconds. Bounded to roughly |
| 32 | * 85 seconds. |
| 33 | */ |
| 34 | export const produce = (url: string, params: { run: string; count: number; padding?: number }) => |
| 35 | HttpClient.post( |
| 36 | `${url}/produce?run=${params.run}&count=${params.count}&padding=${params.padding ?? 0}`, |
| 37 | ).pipe( |
| 38 | Effect.flatMap((res) => |
| 39 | res.status === 202 |
| 40 | ? Effect.succeed(res) |
| 41 | : Effect.fail(new WorkerNotReady({ run: params.run, status: res.status })), |
| 42 | ), |
| 43 | Effect.retry({ |
| 44 | schedule: Schedule.max([ |
| 45 | Schedule.min([Schedule.exponential("500 millis"), Schedule.spaced("3 seconds")]), |
| 46 | Schedule.recurs(30), |
| 47 | ]), |
| 48 | }), |
| 49 | ); |
| 50 | |
| 51 | /** Poll `GET /count` until every produced index reached the result queue. */ |
| 52 | export const awaitDrained = (url: string, run: string, expected: number) => |
| 53 | HttpClient.get(`${url}/count?run=${run}`).pipe( |
| 54 | Effect.flatMap(HttpClientResponse.filterStatusOk), |
| 55 | Effect.flatMap((res) => res.json), |
| 56 | Effect.map((body) => body as unknown as RecorderSnapshot), |
| 57 | Effect.flatMap((snapshot) => |
| 58 | snapshot.distinct >= expected |
| 59 | ? Effect.succeed(snapshot) |
| 60 | : Effect.fail(new NotDrained({ run, expected, distinct: snapshot.distinct })), |
| 61 | ), |
| 62 | // GET /count is idempotent: retry any failure (edge 404s, a waking DO's |
| 63 | // 500) as well as an incomplete count. Bounded to roughly 90 seconds. |
| 64 | Effect.retry({ |
| 65 | schedule: Schedule.max([ |
| 66 | Schedule.min([Schedule.exponential("500 millis"), Schedule.spaced("4 seconds")]), |
| 67 | Schedule.recurs(25), |
| 68 | ]), |
| 69 | }), |
| 70 | ); |