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 Console from "effect/Console"; |
| 2 | import type { ManagedPolicyDocument } from "@t3tools/contracts"; |
| 3 | import * as Effect from "effect/Effect"; |
| 4 | import * as FileSystem from "effect/FileSystem"; |
| 5 | import * as Layer from "effect/Layer"; |
| 6 | import * as Option from "effect/Option"; |
| 7 | import * as Path from "effect/Path"; |
| 8 | import * as References from "effect/References"; |
| 9 | import { Argument, Command, Flag, GlobalFlag } from "effect/cli"; |
| 10 | import * as CliError from "effect/cli/CliError"; |
| 11 | import * as SecureRandom from "effect/crypto/SecureRandom"; |
| 12 | |
| 13 | import * as ManagedPolicy from "../policy/ManagedPolicy.ts"; |
| 14 | import { macosProfile, windowsAdml, windowsAdmx } from "../policy/policyTemplates.ts"; |
| 15 | import { isProcessAlive, readPersistedServerRuntimeState } from "../serverRuntimeState.ts"; |
| 16 | import { projectLocationFlags, resolveCliAuthConfig } from "./config.ts"; |
| 17 | |
| 18 | const describePolicy = ( |
| 19 | policy: ManagedPolicy.ManagedPolicy["Service"], |
| 20 | loaded: Effect.Success<ReturnType<typeof ManagedPolicy.loadedPolicy>>, |
| 21 | ) => { |
| 22 | const { document } = policy; |
| 23 | if (document === undefined || loaded === null) { |
| 24 | return "No managed policy on this machine. Every setting belongs to the user.\n"; |
| 25 | } |
| 26 | const settings = Object.entries(document.settings ?? {}); |
| 27 | const lines = [ |
| 28 | `Managed by ${document.organization} (${policy.path}, fingerprint ${loaded.fingerprint}).`, |
| 29 | ...(document.supportUrl === undefined ? [] : [`Support: ${document.supportUrl}`]), |
| 30 | "", |
| 31 | settings.length === 0 ? "No managed settings." : "Settings:", |
| 32 | ...settings.map( |
| 33 | ([path, setting]) => |
| 34 | ` ${path} = ${JSON.stringify(setting.value)} (${setting.locked ? "locked" : "default"})`, |
| 35 | ), |
| 36 | ]; |
| 37 | if (document.limits !== undefined) { |
| 38 | lines.push("", "Limits:", JSON.stringify(document.limits, null, 2)); |
| 39 | } |
| 40 | return `${lines.join("\n")}\n`; |
| 41 | }; |
| 42 | |
| 43 | /** Whether the server running from this T3 home loaded the installed policy, which waits for a restart. */ |
| 44 | export const describeRunningServer = Effect.fn("cli.policy.describeRunningServer")(function* ( |
| 45 | statePath: string, |
| 46 | installed: Effect.Success<ReturnType<typeof ManagedPolicy.loadedPolicy>>, |
| 47 | ) { |
| 48 | const state = yield* readPersistedServerRuntimeState(statePath); |
| 49 | if (Option.isNone(state) || !isProcessAlive(state.value.pid)) { |
| 50 | return "No T3 Code server is running from this T3 home. The installed policy applies when one starts."; |
| 51 | } |
| 52 | const { pid, startedAt, managedPolicy: loaded } = state.value; |
| 53 | const server = `The running server (pid ${pid}, started ${startedAt})`; |
| 54 | if (loaded === undefined) { |
| 55 | return `${server} does not report which policy it loaded. Restart it to apply the installed policy.`; |
| 56 | } |
| 57 | if ((loaded?.fingerprint ?? null) === (installed?.fingerprint ?? null)) { |
| 58 | return loaded === null |
| 59 | ? `${server} runs without a policy, as installed.` |
| 60 | : `${server} loaded this policy.`; |
| 61 | } |
| 62 | const what = |
| 63 | loaded === null |
| 64 | ? "no policy" |
| 65 | : `${loaded.organization}'s policy from ${loaded.source} (fingerprint ${loaded.fingerprint})`; |
| 66 | return `${server} loaded ${what}. Restart T3 Code to apply the installed policy.`; |
| 67 | }); |
| 68 | |
| 69 | const showCommand = Command.make("show", projectLocationFlags).pipe( |
| 70 | Command.withDescription( |
| 71 | "Show the policy installed on this machine, and whether the running server loaded it.", |
| 72 | ), |
| 73 | Command.withHandler( |
| 74 | Effect.fn("cli.policy.show")(function* (flags) { |
| 75 | // The command prints the policy itself; the server's startup log line would repeat it. |
| 76 | const policy = yield* ManagedPolicy.ManagedPolicy.pipe( |
| 77 | Effect.provide( |
| 78 | ManagedPolicy.layer.pipe( |
| 79 | Layer.provide(Layer.succeed(References.MinimumLogLevel, "Warn")), |
| 80 | ), |
| 81 | ), |
| 82 | ); |
| 83 | const installed = yield* ManagedPolicy.loadedPolicy(policy); |
| 84 | yield* Console.log(describePolicy(policy, installed)); |
| 85 | const config = yield* resolveCliAuthConfig(flags, yield* GlobalFlag.LogLevel); |
| 86 | yield* Console.log(yield* describeRunningServer(config.serverRuntimeStatePath, installed)); |
| 87 | }), |
| 88 | ), |
| 89 | ); |
| 90 | |
| 91 | const checkCommand = Command.make("check", { |
| 92 | file: Argument.String("file").pipe(Argument.withDescription("Policy file to validate.")), |
| 93 | }).pipe( |
| 94 | Command.withDescription("Validate a policy file before deploying it."), |
| 95 | Command.withHandler( |
| 96 | Effect.fn("cli.policy.check")(function* ({ file }) { |
| 97 | // Startup treats a missing file as no policy; a draft that is not there checks nothing. |
| 98 | if (!(yield* (yield* FileSystem.FileSystem).exists(file))) { |
| 99 | return yield* new CliError.UserError({ |
| 100 | cause: file, |
| 101 | userMessage: `No policy file at ${file}.`, |
| 102 | }); |
| 103 | } |
| 104 | const policy = yield* ManagedPolicy.load(file); |
| 105 | yield* Console.log(describePolicy(policy, yield* ManagedPolicy.loadedPolicy(policy))); |
| 106 | }), |
| 107 | ), |
| 108 | ); |
| 109 | |
| 110 | const SAMPLE_POLICY: ManagedPolicyDocument = { |
| 111 | schemaVersion: 1, |
| 112 | organization: "Example Corp", |
| 113 | limits: { maxRuntimeMode: "auto-accept-edits", clients: ["desktop"], telemetry: false }, |
| 114 | }; |
| 115 | |
| 116 | const templatesCommand = Command.make("templates", { |
| 117 | directory: Argument.String("directory").pipe( |
| 118 | Argument.withDescription("Directory to write the templates to."), |
| 119 | ), |
| 120 | policy: Flag.String("policy").pipe( |
| 121 | Flag.withDescription("Policy file to put in the macOS profile. Default: a small sample."), |
| 122 | Flag.optional, |
| 123 | ), |
| 124 | }).pipe( |
| 125 | Command.withDescription( |
| 126 | "Write the Windows Group Policy template and a macOS configuration profile.", |
| 127 | ), |
| 128 | Command.withHandler( |
| 129 | Effect.fn("cli.policy.templates")(function* ({ directory, policy }) { |
| 130 | const fs = yield* FileSystem.FileSystem; |
| 131 | const path = yield* Path.Path; |
| 132 | let document = SAMPLE_POLICY; |
| 133 | if (Option.isSome(policy)) { |
| 134 | if (!(yield* fs.exists(policy.value))) { |
| 135 | return yield* new CliError.UserError({ |
| 136 | cause: policy.value, |
| 137 | userMessage: `No policy file at ${policy.value}.`, |
| 138 | }); |
| 139 | } |
| 140 | const loaded = (yield* ManagedPolicy.load(policy.value)).document; |
| 141 | if (loaded !== undefined) document = loaded; |
| 142 | } |
| 143 | const random = yield* SecureRandom.SecureRandom; |
| 144 | const profile = macosProfile(document, { |
| 145 | profile: yield* random.uuidV4().pipe(Effect.orDie), |
| 146 | payload: yield* random.uuidV4().pipe(Effect.orDie), |
| 147 | }); |
| 148 | if (profile._tag === "Invalid") { |
| 149 | return yield* new CliError.UserError({ cause: document, userMessage: profile.reason }); |
| 150 | } |
| 151 | const files = [ |
| 152 | [path.join(directory, "windows", "t3code.admx"), windowsAdmx()], |
| 153 | [path.join(directory, "windows", "en-US", "t3code.adml"), windowsAdml()], |
| 154 | [path.join(directory, "macos", "t3code.mobileconfig"), profile.profile], |
| 155 | ] as const; |
| 156 | for (const [file, contents] of files) { |
| 157 | yield* fs.makeDirectory(path.dirname(file), { recursive: true }); |
| 158 | yield* fs.writeFileString(file, contents); |
| 159 | yield* Console.log(file); |
| 160 | } |
| 161 | }), |
| 162 | ), |
| 163 | ); |
| 164 | |
| 165 | export const policyCommand = Command.make("policy").pipe( |
| 166 | Command.withDescription("Inspect the administrator's managed policy."), |
| 167 | Command.withSubcommands([showCommand, checkCommand, templatesCommand]), |
| 168 | ); |