@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 216 lines 7.13 KB
Raw
1 import * as androidmanagement from "@distilled.cloud/gcp/androidmanagement_v1";
2 import { createPhysicalName } from "alchemy/PhysicalName";
3 import * as Effect from "effect/Effect";
4 import * as Stream from "effect/Stream";
5
6 export const ENTERPRISE_PREFIX = "enterprises/";
7 export const MAX_DISPLAY_NAME_LENGTH = 100;
8 export const MAX_WEB_APP_TITLE_LENGTH = 100;
9 export const MAX_ADDITIONAL_DATA_LENGTH = 1024;
10 export const DEFAULT_DURATION = "315360000s";
11 export const DEFAULT_DISPLAY_MODE = "STANDALONE";
12 export const DEFAULT_START_URL = "https://example.com/";
13
14 export const DEFAULT_WEB_APP_ICON: androidmanagement.WebAppIcon = {
15 imageData:
16 "iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mP8z8BQDwAEhQGAhKmMIQAAAABJRU5ErkJggg==",
17 };
18
19 export const lastSegment = (value: string) => {
20 const trimmed = value.replace(/\/+$/, "");
21 const parts = trimmed.split("/");
22 return parts[parts.length - 1] || trimmed;
23 };
24
25 export const parentOf = (name: string) => {
26 const parts = name.split("/").filter((part) => part.length > 0);
27 return parts.slice(0, -2).join("/");
28 };
29
30 export const normalizeName = (value: string) => value.replace(/\/+$/, "");
31
32 export const toEnterpriseName = (value: string) => {
33 const trimmed = normalizeName(value);
34 if (trimmed.length === 0) return "";
35 if (trimmed.startsWith(ENTERPRISE_PREFIX)) return trimmed;
36 return `${ENTERPRISE_PREFIX}${trimmed}`;
37 };
38
39 export const toEnrollmentTokenName = (parent: string, tokenId?: string) => {
40 if (tokenId !== undefined && tokenId.includes("/enrollmentTokens/")) {
41 return normalizeName(tokenId);
42 }
43 if (tokenId !== undefined && tokenId.length > 0 && parent.length > 0) {
44 return `${toEnterpriseName(parent)}/enrollmentTokens/${lastSegment(tokenId)}`;
45 }
46 return "";
47 };
48
49 export const toWebAppName = (parent: string, webAppId?: string) => {
50 if (webAppId !== undefined && webAppId.includes("/webApps/")) {
51 return normalizeName(webAppId);
52 }
53 if (webAppId !== undefined && webAppId.length > 0 && parent.length > 0) {
54 return `${toEnterpriseName(parent)}/webApps/${lastSegment(webAppId)}`;
55 }
56 return "";
57 };
58
59 export const sameText = (left: string | undefined, right: string | undefined) =>
60 (left ?? "") === (right ?? "");
61
62 export const sameBoolean = (left: boolean | undefined, right: boolean | undefined) =>
63 (left ?? false) === (right ?? false);
64
65 export const jsonEqual = (left: unknown, right: unknown) =>
66 JSON.stringify(left ?? null) === JSON.stringify(right ?? null);
67
68 export const sameStringList = (
69 left: readonly string[] | undefined,
70 right: readonly string[] | undefined,
71 ) => jsonEqual([...(left ?? [])].slice().sort(), [...(right ?? [])].slice().sort());
72
73 export const updateMaskOf = (...fields: Array<string | undefined>) =>
74 fields.filter((field): field is string => field !== undefined).join(",");
75
76 export const replaceOnIdentity = (input: {
77 previousId?: string;
78 nextId?: string;
79 previousParent?: string;
80 nextParent?: string;
81 extra?: boolean;
82 }) => {
83 if (input.extra === true) {
84 return { action: "replace" as const, deleteFirst: false };
85 }
86 if (
87 input.previousId !== undefined &&
88 input.nextId !== undefined &&
89 input.previousId !== input.nextId
90 ) {
91 return { action: "replace" as const, deleteFirst: false };
92 }
93 if (
94 input.previousParent !== undefined &&
95 input.nextParent !== undefined &&
96 toEnterpriseName(input.previousParent) !== toEnterpriseName(input.nextParent)
97 ) {
98 return { action: "replace" as const, deleteFirst: true };
99 }
100 return undefined;
101 };
102
103 export const toDisplayName = (
104 id: string,
105 requested: string | undefined,
106 existing: string | undefined,
107 maxLength = MAX_DISPLAY_NAME_LENGTH,
108 ) =>
109 Effect.gen(function* () {
110 if (requested !== undefined && requested.length > 0) {
111 return requested.slice(0, maxLength);
112 }
113 if (existing !== undefined && existing.length > 0) {
114 return existing.slice(0, maxLength);
115 }
116 return yield* createPhysicalName({
117 id,
118 maxLength: Math.min(40, maxLength),
119 lowercase: true,
120 });
121 });
122
123 const emptyList = <A>() => Effect.succeed([] as A[]);
124
125 export const collectPages = <Page, A, E, R>(
126 pages: Stream.Stream<Page, E, R>,
127 items: (page: Page) => readonly A[] | undefined,
128 ) =>
129 pages.pipe(
130 Stream.flatMap((page) => Stream.fromIterable(items(page) ?? [])),
131 Stream.runCollect,
132 Effect.map((chunk) => Array.from(chunk)),
133 );
134
135 export const getEnterprise = (name: string) =>
136 name.length === 0
137 ? Effect.undefined
138 : androidmanagement
139 .getEnterprises({ name: toEnterpriseName(name) })
140 .pipe(Effect.catchTag("NotFound", () => Effect.undefined));
141
142 export const getEnrollmentToken = (name: string) =>
143 name.length === 0
144 ? Effect.undefined
145 : androidmanagement
146 .getEnterprisesEnrollmentTokens({ name })
147 .pipe(Effect.catchTag("NotFound", () => Effect.undefined));
148
149 export const getWebApp = (name: string) =>
150 name.length === 0
151 ? Effect.undefined
152 : androidmanagement
153 .getEnterprisesWebApps({ name })
154 .pipe(Effect.catchTag("NotFound", () => Effect.undefined));
155
156 export const listEnterprisesAt = (projectId: string) =>
157 projectId.length === 0
158 ? emptyList<androidmanagement.Enterprise>()
159 : collectPages(
160 androidmanagement.listEnterprises.pages({
161 projectId,
162 view: "BASIC",
163 pageSize: 100,
164 }),
165 (page) => page.enterprises,
166 ).pipe(
167 // A missing parent has no children.
168 Effect.catchTag("NotFound", () => emptyList<androidmanagement.Enterprise>()),
169 );
170
171 export const listWebAppsAt = (parent: string) =>
172 parent.length === 0
173 ? emptyList<androidmanagement.WebApp>()
174 : collectPages(
175 androidmanagement.listEnterprisesWebApps.pages({
176 parent: toEnterpriseName(parent),
177 pageSize: 100,
178 }),
179 (page) => page.webApps,
180 ).pipe(
181 // A missing parent has no children.
182 Effect.catchTag("NotFound", () => emptyList<androidmanagement.WebApp>()),
183 );
184
185 const hydrateEnterprise = (enterprise: androidmanagement.Enterprise) =>
186 Effect.gen(function* () {
187 if (
188 enterprise.enterpriseDisplayName !== undefined &&
189 enterprise.enterpriseDisplayName.length > 0
190 ) {
191 return enterprise;
192 }
193 const name = enterprise.name ?? "";
194 if (name.length === 0) return enterprise;
195 return (yield* getEnterprise(name)) ?? enterprise;
196 });
197
198 /** Find an enterprise of `projectId` by its display name. */
199 export const findEnterpriseByDisplayName = (projectId: string, displayName: string) =>
200 Effect.gen(function* () {
201 const listed = yield* listEnterprisesAt(projectId);
202 for (const enterprise of listed) {
203 const hydrated = yield* hydrateEnterprise(enterprise);
204 if (hydrated.enterpriseDisplayName === displayName) return hydrated;
205 }
206 return undefined;
207 });
208
209 /** Find a web app of `parent` by its title. */
210 export const findWebAppByTitle = (parent: string, title: string) =>
211 listWebAppsAt(parent).pipe(Effect.map((apps) => apps.find((app) => app.title === title)));
212
213 export const defaultWebAppIcons = (
214 icons: readonly androidmanagement.WebAppIcon[] | undefined,
215 ): androidmanagement.WebAppIcon[] =>
216 icons && icons.length > 0 ? [...icons] : [DEFAULT_WEB_APP_ICON];