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 | /** |
| 2 | * DesktopUpdatePolicy - what the administrator's managed policy allows the desktop updater to do. |
| 3 | * The desktop reads the policy file itself, before any backend runs, and fails closed: a policy it |
| 4 | * cannot read turns updates off. |
| 5 | * |
| 6 | * @module DesktopUpdatePolicy |
| 7 | */ |
| 8 | import { |
| 9 | managedUpdatesMessage, |
| 10 | type ManagedPolicyDocument, |
| 11 | type ManagedUpdatesMode, |
| 12 | } from "@t3tools/contracts"; |
| 13 | import * as Context from "effect/Context"; |
| 14 | import * as Effect from "effect/Effect"; |
| 15 | import * as Layer from "effect/Layer"; |
| 16 | |
| 17 | import * as DesktopManagedPolicy from "../app/DesktopManagedPolicy.ts"; |
| 18 | import * as DesktopObservability from "../app/DesktopObservability.ts"; |
| 19 | |
| 20 | export interface UpdatePolicy { |
| 21 | readonly mode: ManagedUpdatesMode; |
| 22 | readonly feedUrl: string | undefined; |
| 23 | readonly allowNightly: boolean; |
| 24 | /** Why the updater is restricted, shown in place of update actions; `null` when unmanaged. */ |
| 25 | readonly message: string | null; |
| 26 | } |
| 27 | |
| 28 | export class DesktopUpdatePolicy extends Context.Service<DesktopUpdatePolicy, UpdatePolicy>()( |
| 29 | "@t3tools/desktop/updates/DesktopUpdatePolicy", |
| 30 | ) {} |
| 31 | |
| 32 | export const unmanagedUpdatePolicy: UpdatePolicy = { |
| 33 | mode: "auto", |
| 34 | feedUrl: undefined, |
| 35 | allowNightly: true, |
| 36 | message: null, |
| 37 | }; |
| 38 | |
| 39 | function resolveUpdatePolicy(document: ManagedPolicyDocument | undefined): UpdatePolicy { |
| 40 | const updates = document?.limits?.updates; |
| 41 | if (document === undefined || updates === undefined) return unmanagedUpdatePolicy; |
| 42 | return { |
| 43 | mode: updates.mode, |
| 44 | feedUrl: updates.feedUrl, |
| 45 | allowNightly: updates.allowNightly ?? true, |
| 46 | message: managedUpdatesMessage(document.organization), |
| 47 | }; |
| 48 | } |
| 49 | |
| 50 | const { logInfo, logError } = DesktopObservability.makeComponentLogger("desktop-update-policy"); |
| 51 | |
| 52 | const make = Effect.gen(function* () { |
| 53 | return yield* (yield* DesktopManagedPolicy.DesktopManagedPolicy).read.pipe( |
| 54 | Effect.map((found) => ({ |
| 55 | source: found?.source, |
| 56 | policy: resolveUpdatePolicy(found?.document), |
| 57 | })), |
| 58 | Effect.tap(({ source, policy }) => |
| 59 | policy.message === null |
| 60 | ? Effect.void |
| 61 | : logInfo("managed policy restricts updates", { |
| 62 | source, |
| 63 | mode: policy.mode, |
| 64 | customFeed: policy.feedUrl !== undefined, |
| 65 | allowNightly: policy.allowNightly, |
| 66 | }), |
| 67 | ), |
| 68 | Effect.map(({ policy }) => policy), |
| 69 | Effect.catchTags({ |
| 70 | ManagedPolicyFileError: (error) => |
| 71 | logError("updates are off because the managed policy cannot be used", { |
| 72 | path: error.path, |
| 73 | reason: error.reason, |
| 74 | }).pipe( |
| 75 | Effect.as<UpdatePolicy>({ |
| 76 | mode: "off", |
| 77 | feedUrl: undefined, |
| 78 | allowNightly: false, |
| 79 | message: `Updates are off because T3 Code cannot use the managed policy at ${error.path}. Contact your administrator.`, |
| 80 | }), |
| 81 | ), |
| 82 | }), |
| 83 | ); |
| 84 | }); |
| 85 | |
| 86 | export const layer = Layer.effect(DesktopUpdatePolicy, make); |
| 87 | |
| 88 | export const layerTest = (policy: UpdatePolicy = unmanagedUpdatePolicy) => |
| 89 | Layer.succeed(DesktopUpdatePolicy, policy); |