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 * as Effect from "effect/Effect"; |
| 2 | import * as Result from "effect/Result"; |
| 3 | import * as Schema from "effect/Schema"; |
| 4 | import type { EnvironmentId } from "@t3tools/contracts"; |
| 5 | |
| 6 | import type { SavedRemoteConnection } from "../../lib/connection"; |
| 7 | import type { CloudEnvironmentLinkError } from "../cloud/linkEnvironment"; |
| 8 | import { updateAgentAwarenessRegistrationPreferences } from "./remoteRegistration"; |
| 9 | |
| 10 | export class LiveActivityEnvironmentUpdateError extends Schema.TaggedError<LiveActivityEnvironmentUpdateError>()( |
| 11 | "LiveActivityEnvironmentUpdateError", |
| 12 | { |
| 13 | updatedEnvironments: Schema.Array(Schema.String), |
| 14 | cause: Schema.Defect(), |
| 15 | }, |
| 16 | ) {} |
| 17 | |
| 18 | export const setLiveActivityUpdatesEnabled = Effect.fn("setLiveActivityUpdatesEnabled")(function* < |
| 19 | SaveError, |
| 20 | >(input: { |
| 21 | readonly enabled: boolean; |
| 22 | readonly previousEnabled: boolean; |
| 23 | readonly clerkToken: string | null; |
| 24 | readonly connections: ReadonlyArray<SavedRemoteConnection>; |
| 25 | readonly updateEnvironment: ( |
| 26 | environmentId: EnvironmentId, |
| 27 | enabled: boolean, |
| 28 | ) => Effect.Effect< |
| 29 | "updated" | "unlinked" | "unsupported" | "disconnected", |
| 30 | CloudEnvironmentLinkError |
| 31 | >; |
| 32 | readonly linkEnvironment: ( |
| 33 | environmentId: EnvironmentId, |
| 34 | input: { readonly clerkToken: string; readonly liveActivitiesEnabled: boolean }, |
| 35 | ) => Effect.Effect<void, CloudEnvironmentLinkError>; |
| 36 | readonly canUpdateEnvironment: (environmentId: EnvironmentId) => boolean; |
| 37 | readonly canConfigureEnvironment: (environmentId: EnvironmentId) => boolean; |
| 38 | readonly saveLocalPreference: (enabled: boolean) => Effect.Effect<unknown, SaveError>; |
| 39 | }) { |
| 40 | const updateEnvironmentPreference = Effect.fn("updateEnvironmentPreference")(function* ( |
| 41 | connection: SavedRemoteConnection, |
| 42 | enabled: boolean, |
| 43 | clerkToken: string | null, |
| 44 | ) { |
| 45 | const result = input.canUpdateEnvironment(connection.environmentId) |
| 46 | ? yield* input.updateEnvironment(connection.environmentId, enabled) |
| 47 | : "unlinked"; |
| 48 | if (result === "updated" || result === "disconnected") return result; |
| 49 | if (!enabled || !clerkToken || !input.canConfigureEnvironment(connection.environmentId)) { |
| 50 | return result; |
| 51 | } |
| 52 | |
| 53 | yield* input.linkEnvironment(connection.environmentId, { |
| 54 | clerkToken, |
| 55 | liveActivitiesEnabled: enabled, |
| 56 | }); |
| 57 | return "updated" as const; |
| 58 | }); |
| 59 | |
| 60 | const updateRelayPreference = Effect.fn("updateRelayPreference")(function* (enabled: boolean) { |
| 61 | yield* updateAgentAwarenessRegistrationPreferences({ |
| 62 | liveActivitiesEnabled: enabled, |
| 63 | }); |
| 64 | |
| 65 | const clerkToken = input.clerkToken; |
| 66 | if (!clerkToken) return false; |
| 67 | |
| 68 | const results = yield* Effect.forEach( |
| 69 | input.connections, |
| 70 | (connection) => |
| 71 | updateEnvironmentPreference(connection, enabled, clerkToken).pipe( |
| 72 | Effect.result, |
| 73 | Effect.map((result) => ({ connection, result })), |
| 74 | ), |
| 75 | { concurrency: "unbounded" }, |
| 76 | ); |
| 77 | const failure = results.find(({ result }) => Result.isFailure(result)); |
| 78 | if (failure && Result.isFailure(failure.result)) { |
| 79 | const updatedEnvironments = results.flatMap(({ connection, result }) => { |
| 80 | const changed = Result.isSuccess(result) |
| 81 | ? result.success === "updated" |
| 82 | : result.failure.relayLinkCommitted === true; |
| 83 | return changed ? [connection.environmentLabel] : []; |
| 84 | }); |
| 85 | if (updatedEnvironments.length === 0) return yield* failure.result.failure; |
| 86 | return yield* new LiveActivityEnvironmentUpdateError({ |
| 87 | updatedEnvironments, |
| 88 | cause: failure.result.failure, |
| 89 | }); |
| 90 | } |
| 91 | return results.some( |
| 92 | ({ result }) => Result.isSuccess(result) && result.success === "unsupported", |
| 93 | ); |
| 94 | }); |
| 95 | |
| 96 | return yield* updateRelayPreference(input.enabled).pipe( |
| 97 | // Saved here so a device that cannot keep the setting restores its registration too. |
| 98 | Effect.tap(() => input.saveLocalPreference(input.enabled)), |
| 99 | Effect.onError(() => |
| 100 | updateAgentAwarenessRegistrationPreferences({ |
| 101 | liveActivitiesEnabled: input.previousEnabled, |
| 102 | }).pipe( |
| 103 | Effect.catchCause((cause) => |
| 104 | Effect.logWarning("Could not restore Live Activity device preference.", cause), |
| 105 | ), |
| 106 | ), |
| 107 | ), |
| 108 | ); |
| 109 | }); |