@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 93 lines 2.89 KB
Raw
1 import { useAtomValue } from "@effect/atom-react";
2 import type { DesktopBridge, DesktopUpdateState } from "@t3tools/contracts";
3 import * as Effect from "effect/Effect";
4 import * as Queue from "effect/Queue";
5 import * as Schema from "effect/Schema";
6 import * as Stream from "effect/Stream";
7 import * as AsyncResult from "effect/reactivity/AsyncResult";
8 import { Atom } from "effect/reactivity";
9 import { msg } from "gt-react";
10
11 import { m } from "../i18n/messages";
12
13 type DesktopUpdateBridge = Pick<DesktopBridge, "getUpdateState" | "onUpdateState">;
14
15 const INITIAL_STATE_READ_ATTEMPT_COUNT = 3;
16
17 export class DesktopUpdateStateReadError extends Schema.TaggedError<DesktopUpdateStateReadError>()(
18 "DesktopUpdateStateReadError",
19 {
20 attemptCount: Schema.Number,
21 cause: Schema.Defect(),
22 },
23 ) {
24 override get message(): string {
25 return m(
26 msg("Failed to read the initial desktop update state after {count} attempts.", {
27 count: this.attemptCount,
28 }),
29 );
30 }
31 }
32
33 function getDesktopUpdateBridge(): DesktopUpdateBridge | undefined {
34 return typeof window === "undefined" ? undefined : window.desktopBridge;
35 }
36
37 export function createDesktopUpdateStateAtom(getBridge: () => DesktopUpdateBridge | undefined) {
38 const updates = Stream.callback<DesktopUpdateState | null>((queue) =>
39 Effect.gen(function* () {
40 const bridge = getBridge();
41 if (!bridge) {
42 Queue.offerUnsafe(queue, null);
43 return yield* Effect.never;
44 }
45
46 let receivedUpdate = false;
47 yield* Effect.acquireRelease(
48 Effect.sync(() =>
49 bridge.onUpdateState((state) => {
50 receivedUpdate = true;
51 Queue.offerUnsafe(queue, state);
52 }),
53 ),
54 (unsubscribe) => Effect.sync(unsubscribe),
55 );
56
57 const initialState = yield* Effect.tryPromise({
58 try: () => bridge.getUpdateState(),
59 catch: (cause) =>
60 new DesktopUpdateStateReadError({
61 attemptCount: INITIAL_STATE_READ_ATTEMPT_COUNT,
62 cause,
63 }),
64 }).pipe(
65 Effect.retry({ times: INITIAL_STATE_READ_ATTEMPT_COUNT - 1 }),
66 Effect.catchTags({
67 DesktopUpdateStateReadError: (error) =>
68 Effect.logError(error.message, {
69 error,
70 errorTag: error._tag,
71 attemptCount: error.attemptCount,
72 }).pipe(Effect.as(null)),
73 }),
74 );
75 if (!receivedUpdate && initialState !== null) {
76 Queue.offerUnsafe(queue, initialState);
77 }
78
79 return yield* Effect.never;
80 }),
81 );
82
83 return Atom.make(updates, { initialValue: null }).pipe(
84 Atom.keepAlive,
85 Atom.withLabel("desktop:update-state"),
86 );
87 }
88
89 export const desktopUpdateStateAtom = createDesktopUpdateStateAtom(getDesktopUpdateBridge);
90
91 export function useDesktopUpdateState(): DesktopUpdateState | null {
92 return AsyncResult.getOrElse(useAtomValue(desktopUpdateStateAtom), () => null);
93 }