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
| 1 | import type { DurableObjectNamespace, DurableObjectState } from "@cloudflare/workers-types" |
| 2 | import * as SqliteClient from "@effect/sql-sqlite-do/SqliteClient" |
| 3 | import { Effect } from "effect" |
| 4 | import * as EventJournal from "effect/eventlog/EventJournal" |
| 5 | import * as SqlEventJournal from "effect/eventlog/SqlEventJournal" |
| 6 | import * as SqlClient from "effect/sql/SqlClient" |
| 7 | |
| 8 | export class JournalObject { |
| 9 | constructor(private readonly state: DurableObjectState) {} |
| 10 | |
| 11 | async fetch() { |
| 12 | const program = Effect.gen(function*() { |
| 13 | const sql = yield* SqlClient.SqlClient |
| 14 | const journal = yield* SqlEventJournal.make() |
| 15 | // Saturated `rand_a` bits at the maximum timestamp leave no later local id. |
| 16 | const id = EventJournal.makeEntryIdUnsafe({ msecs: 2 ** 48 - 1 }) |
| 17 | id[6] = 0x7f |
| 18 | id[7] = 0xff |
| 19 | const remote = new EventJournal.Entry({ id, event: "Repro", primaryKey: "key", payload: new Uint8Array() }, { |
| 20 | disableChecks: true |
| 21 | }) |
| 22 | // The import itself succeeds; the transaction around it then fails. |
| 23 | const rolledBack = yield* journal.writeFromRemote({ |
| 24 | remoteId: EventJournal.makeRemoteIdUnsafe(), |
| 25 | entries: [new EventJournal.RemoteEntry({ remoteSequence: 0, entry: remote })], |
| 26 | effect: () => Effect.void |
| 27 | }).pipe(Effect.andThen(Effect.fail("rolled back")), sql.withTransaction, Effect.exit) |
| 28 | const written = yield* Effect.exit(journal.write({ |
| 29 | event: "Repro", |
| 30 | primaryKey: "key", |
| 31 | payload: new Uint8Array(), |
| 32 | effect: Effect.succeed |
| 33 | })) |
| 34 | return { |
| 35 | rolledBack: rolledBack._tag, |
| 36 | written: written._tag === "Success" ? [written.value.idString] : written._tag, |
| 37 | entries: (yield* journal.entries).map((entry) => entry.idString) |
| 38 | } |
| 39 | }).pipe(Effect.scoped, Effect.provide(SqliteClient.layer({ storage: this.state.storage }))) |
| 40 | return Response.json(await Effect.runPromise(program)) |
| 41 | } |
| 42 | } |
| 43 | |
| 44 | export default { |
| 45 | fetch(request: Request, env: { JOURNAL: DurableObjectNamespace }) { |
| 46 | return env.JOURNAL.get(env.JOURNAL.idFromName("journal")).fetch(request.url) |
| 47 | } |
| 48 | } |