|
| 1 | +import * as NodeModule from "node:module"; |
| 2 | +import { CuaApplication, CuaApplicationsError } from "@t3tools/contracts"; |
| 3 | +import type * as CuaSdk from "@trycua/cua-driver"; |
| 4 | +import * as Context from "effect/Context"; |
| 5 | +import * as Effect from "effect/Effect"; |
| 6 | +import * as FileSystem from "effect/FileSystem"; |
| 7 | +import * as Layer from "effect/Layer"; |
| 8 | +import * as Option from "effect/Option"; |
| 9 | +import * as Path from "effect/Path"; |
| 10 | +import * as Schema from "effect/Schema"; |
| 11 | +import * as CuaDriver from "./CuaDriver.ts"; |
| 12 | + |
| 13 | +export type Sdk = Pick<typeof CuaSdk, "CuaDriver" | "ListAppsInput">; |
| 14 | +type Client = Pick<CuaSdk.CuaDriver, "listApps" | "shutdown" | "uniffiDestroy">; |
| 15 | +const isApplication = Schema.is(CuaApplication); |
| 16 | +const requireSdk = NodeModule.createRequire(import.meta.url); |
| 17 | +const loadSdk = Effect.gen(function* () { |
| 18 | + const fs = yield* FileSystem.FileSystem; |
| 19 | + const path = yield* Path.Path; |
| 20 | + for (const root of requireSdk.resolve.paths("@trycua/cua-driver") ?? []) { |
| 21 | + const dir = path.join(root, "@trycua", "cua-driver"); |
| 22 | + if (yield* fs.exists(path.join(dir, "package.json"))) { |
| 23 | + return yield* Effect.try(() => requireSdk(path.join(dir, "dist", "index.js")) as Sdk); |
| 24 | + } |
| 25 | + } |
| 26 | + return yield* new CuaApplicationsError({ cause: "Cua Driver SDK is not installed." }); |
| 27 | +}).pipe(Effect.mapError((cause) => new CuaApplicationsError({ cause }))); |
| 28 | + |
| 29 | +export class CuaApplications extends Context.Service< |
| 30 | + CuaApplications, |
| 31 | + { |
| 32 | + readonly list: Effect.Effect<ReadonlyArray<CuaApplication>, CuaApplicationsError>; |
| 33 | + } |
| 34 | +>()("t3/cua/CuaApplications") {} |
| 35 | + |
| 36 | +export const make = Effect.fn("CuaApplications.make")(function* ( |
| 37 | + options: { loadSdk?: Effect.Effect<Sdk, CuaApplicationsError> } = {}, |
| 38 | +) { |
| 39 | + const driver = yield* CuaDriver.CuaDriver; |
| 40 | + const fs = yield* FileSystem.FileSystem; |
| 41 | + const path = yield* Path.Path; |
| 42 | + const sdk = |
| 43 | + options.loadSdk ?? |
| 44 | + loadSdk.pipe( |
| 45 | + Effect.provideService(FileSystem.FileSystem, fs), |
| 46 | + Effect.provideService(Path.Path, path), |
| 47 | + ); |
| 48 | + const list = Effect.gen(function* () { |
| 49 | + if (!(yield* driver.enabled)) return []; |
| 50 | + const config = yield* driver.acquire; |
| 51 | + if (Option.isNone(config) || !config.value.socketPath) return []; |
| 52 | + const module = yield* sdk; |
| 53 | + return yield* Effect.scoped( |
| 54 | + Effect.gen(function* () { |
| 55 | + const client = yield* Effect.acquireRelease( |
| 56 | + Effect.try(() => module.CuaDriver.connect(config.value.socketPath!) as unknown as Client), |
| 57 | + (client) => |
| 58 | + Effect.promise(() => client.shutdown()).pipe( |
| 59 | + Effect.ignore, |
| 60 | + Effect.andThen(Effect.sync(() => client.uniffiDestroy())), |
| 61 | + ), |
| 62 | + ); |
| 63 | + const result = yield* Effect.tryPromise((signal) => |
| 64 | + client.listApps(module.ListAppsInput.new({}), { signal }), |
| 65 | + ); |
| 66 | + const apps: CuaApplication[] = []; |
| 67 | + const seen = new Set<string>(); |
| 68 | + for (const app of result.apps) { |
| 69 | + const packagedId = /^shell:appsFolder\\(.+)$/iu.exec(app.launchPath ?? "")?.[1]; |
| 70 | + const bundleId = |
| 71 | + packagedId ?? |
| 72 | + (app.bundleId && /^[A-Za-z0-9._-]+(?:![A-Za-z0-9._-]+)?$/u.test(app.bundleId) |
| 73 | + ? app.bundleId |
| 74 | + : undefined); |
| 75 | + const candidate = { |
| 76 | + name: app.name, |
| 77 | + ...(bundleId ? { bundleId } : {}), |
| 78 | + ...(app.launchPath ? { launchPath: app.launchPath } : {}), |
| 79 | + ...(app.running && app.pid > 0 ? { pid: app.pid } : {}), |
| 80 | + }; |
| 81 | + if (!isApplication(candidate)) continue; |
| 82 | + const key = bundleId ?? app.launchPath ?? app.name; |
| 83 | + if (seen.has(key)) continue; |
| 84 | + seen.add(key); |
| 85 | + apps.push(candidate); |
| 86 | + } |
| 87 | + return apps.sort((a, b) => a.name.localeCompare(b.name)); |
| 88 | + }), |
| 89 | + ); |
| 90 | + }).pipe(Effect.mapError((cause) => new CuaApplicationsError({ cause }))); |
| 91 | + return CuaApplications.of({ list }); |
| 92 | +}); |
| 93 | + |
| 94 | +export const layer = Layer.effect(CuaApplications, make()); |
0 commit comments