The best way to control Claude Code, Codex and more from any computer or phone
| 1 | /** |
| 2 | * DesktopManagedPolicy - the administrator's managed policy as the desktop app reads it, at most |
| 3 | * once: every desktop check needs the same answer. |
| 4 | * |
| 5 | * @module DesktopManagedPolicy |
| 6 | */ |
| 7 | import type { ManagedPolicyDocument } from "@t3tools/contracts"; |
| 8 | import { |
| 9 | layerWindowsRegistry, |
| 10 | ManagedPolicyFileError, |
| 11 | readManagedPolicy, |
| 12 | WindowsRegistry, |
| 13 | } from "@t3tools/shared/managedPolicyFile"; |
| 14 | import * as Context from "effect/Context"; |
| 15 | import * as Effect from "effect/Effect"; |
| 16 | import * as FileSystem from "effect/FileSystem"; |
| 17 | import * as Layer from "effect/Layer"; |
| 18 | import * as Schema from "effect/Schema"; |
| 19 | import { ChildProcessSpawner } from "effect/process"; |
| 20 | |
| 21 | import * as DesktopEnvironment from "./DesktopEnvironment.ts"; |
| 22 | |
| 23 | type FoundPolicy = Effect.Success<ReturnType<typeof readManagedPolicy>>; |
| 24 | const isManagedPolicyFileError = Schema.is(ManagedPolicyFileError); |
| 25 | |
| 26 | export class DesktopManagedPolicy extends Context.Service< |
| 27 | DesktopManagedPolicy, |
| 28 | { |
| 29 | /** The policy on this machine, undefined without one; fails when it cannot be used. */ |
| 30 | readonly read: Effect.Effect<FoundPolicy, ManagedPolicyFileError>; |
| 31 | } |
| 32 | >()("@t3tools/desktop/app/DesktopManagedPolicy") {} |
| 33 | |
| 34 | export const layer = Layer.effect( |
| 35 | DesktopManagedPolicy, |
| 36 | Effect.gen(function* () { |
| 37 | const { platform } = yield* DesktopEnvironment.DesktopEnvironment; |
| 38 | const read = yield* Effect.cached( |
| 39 | readManagedPolicy(platform).pipe( |
| 40 | Effect.provideService(FileSystem.FileSystem, yield* FileSystem.FileSystem), |
| 41 | Effect.provideService( |
| 42 | ChildProcessSpawner.ChildProcessSpawner, |
| 43 | yield* ChildProcessSpawner.ChildProcessSpawner, |
| 44 | ), |
| 45 | Effect.provideService(WindowsRegistry, yield* WindowsRegistry), |
| 46 | ), |
| 47 | ); |
| 48 | return DesktopManagedPolicy.of({ read }); |
| 49 | }), |
| 50 | ).pipe(Layer.provide(layerWindowsRegistry(() => import("ffi-rs")))); |
| 51 | |
| 52 | /** A machine with `policy` deployed, one whose policy cannot be used, or none. */ |
| 53 | export const layerTest = (policy?: ManagedPolicyDocument | ManagedPolicyFileError) => |
| 54 | Layer.succeed( |
| 55 | DesktopManagedPolicy, |
| 56 | DesktopManagedPolicy.of({ |
| 57 | read: |
| 58 | policy === undefined |
| 59 | ? Effect.undefined |
| 60 | : isManagedPolicyFileError(policy) |
| 61 | ? Effect.fail(policy) |
| 62 | : Effect.succeed({ source: "/test/policy.json", document: policy }), |
| 63 | }), |
| 64 | ); |