@devries-cameron20 / t3code Mirror · read-only
Fork 0

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

main
ts 124 lines 4.49 KB
Raw
1 import * as Context from "effect/Context";
2 import * as Effect from "effect/Effect";
3 import * as FileSystem from "effect/FileSystem";
4 import * as Layer from "effect/Layer";
5 import * as Option from "effect/Option";
6 import * as Path from "effect/Path";
7 import * as Ref from "effect/Ref";
8 import * as Schema from "effect/Schema";
9
10 import * as ElectronApp from "../electron/ElectronApp.ts";
11 import * as DesktopAssets from "./DesktopAssets.ts";
12 import * as DesktopEnvironment from "./DesktopEnvironment.ts";
13 import * as DesktopUserData from "./DesktopUserData.ts";
14
15 const COMMIT_HASH_PATTERN = /^[0-9a-f]{7,40}$/i;
16 const COMMIT_HASH_DISPLAY_LENGTH = 12;
17
18 const AppPackageMetadata = Schema.Struct({
19 t3codeCommitHash: Schema.optional(Schema.String),
20 });
21 const decodeAppPackageMetadata = Schema.decodeEffect(Schema.fromJsonString(AppPackageMetadata));
22
23 export class DesktopAppIdentity extends Context.Service<
24 DesktopAppIdentity,
25 {
26 readonly resolveUserDataPath: Effect.Effect<
27 string,
28 DesktopUserData.DesktopUserDataInitializationError
29 >;
30 readonly configure: Effect.Effect<void>;
31 }
32 >()("@t3tools/desktop/app/DesktopAppIdentity") {}
33
34 const normalizeCommitHash = (value: string): Option.Option<string> => {
35 const trimmed = value.trim();
36 return COMMIT_HASH_PATTERN.test(trimmed)
37 ? Option.some(trimmed.slice(0, COMMIT_HASH_DISPLAY_LENGTH).toLowerCase())
38 : Option.none();
39 };
40
41 /** @public */
42 export const make = Effect.gen(function* () {
43 const assets = yield* DesktopAssets.DesktopAssets;
44 const electronApp = yield* ElectronApp.ElectronApp;
45 const environment = yield* DesktopEnvironment.DesktopEnvironment;
46 const fileSystem = yield* FileSystem.FileSystem;
47 const userDataContext = yield* Effect.context<FileSystem.FileSystem | Path.Path>();
48 const commitHashCache = yield* Ref.make<Option.Option<Option.Option<string>>>(Option.none());
49
50 const resolveEmbeddedCommitHash = Effect.gen(function* () {
51 const packageJsonPath = environment.path.join(environment.appRoot, "package.json");
52 const raw = yield* fileSystem.readFileString(packageJsonPath).pipe(Effect.option);
53 return yield* Option.match(raw, {
54 onNone: () => Effect.succeed(Option.none<string>()),
55 onSome: (value) =>
56 decodeAppPackageMetadata(value).pipe(
57 Effect.map((parsed) =>
58 Option.fromNullishOr(parsed.t3codeCommitHash).pipe(Option.flatMap(normalizeCommitHash)),
59 ),
60 Effect.orElseSucceed(() => Option.none<string>()),
61 ),
62 });
63 });
64
65 const resolveAboutCommitHash = Effect.gen(function* () {
66 const cached = yield* Ref.get(commitHashCache);
67 if (Option.isSome(cached)) {
68 return cached.value;
69 }
70
71 const override = Option.flatMap(environment.commitHashOverride, normalizeCommitHash);
72 if (Option.isSome(override)) {
73 yield* Ref.set(commitHashCache, Option.some(override));
74 return override;
75 }
76
77 if (!environment.isPackaged) {
78 const empty = Option.none<string>();
79 yield* Ref.set(commitHashCache, Option.some(empty));
80 return empty;
81 }
82
83 const commitHash = yield* resolveEmbeddedCommitHash;
84 yield* Ref.set(commitHashCache, Option.some(commitHash));
85 return commitHash;
86 });
87
88 const userDataPath = DesktopUserData.resolveUserDataPath(environment).pipe(
89 Effect.provide(userDataContext),
90 );
91
92 const configure = Effect.gen(function* () {
93 const commitHash = yield* resolveAboutCommitHash;
94 // Electron strips spaces but keeps parentheses in its User-Agent token, breaking Turnstile in preview sessions.
95 yield* electronApp.setName(
96 `${environment.branding.baseName} ${environment.branding.stageLabel}`,
97 );
98 yield* electronApp.setAboutPanelOptions({
99 applicationName: environment.displayName,
100 applicationVersion: environment.appVersion,
101 version: Option.getOrElse(commitHash, () => "unknown"),
102 });
103
104 if (environment.platform === "win32") {
105 yield* electronApp.setAppUserModelId(environment.appUserModelId);
106 }
107
108 // Packaged bundles carry their icon in Info.plist; setting it again overwrites the user's custom icon.
109 if (environment.platform === "darwin" && !environment.isPackaged) {
110 const iconPaths = yield* assets.iconPaths;
111 yield* Option.match(iconPaths.png, {
112 onNone: () => Effect.void,
113 onSome: electronApp.setDockIcon,
114 });
115 }
116 }).pipe(Effect.withSpan("desktop.appIdentity.configure"));
117
118 return DesktopAppIdentity.of({
119 resolveUserDataPath: userDataPath,
120 configure,
121 });
122 });
123
124 export const layer = Layer.effect(DesktopAppIdentity, make);