@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, 06:00 UTC

main
ts 272 lines 8.8 KB
Raw
1 import * as androidenterprise from "@distilled.cloud/gcp/androidenterprise_v1";
2 import { Unowned } from "alchemy/AdoptPolicy";
3 import { isResolved } from "alchemy/Diff";
4 import * as Provider from "alchemy/Provider";
5 import { Resource } from "alchemy/Resource";
6 import * as Data from "effect/Data";
7 import * as Effect from "effect/Effect";
8 import { GcpEnvironment } from "../Environment.ts";
9 import type { Providers } from "../Providers.ts";
10 import {
11 DEFAULT_DISPLAY_MODE,
12 findOwnedWebapp,
13 getWebapp,
14 hasOwnershipMarker,
15 jsonEqual,
16 listOwnedWebapps,
17 ownedByAlchemy,
18 parseOwnership,
19 sameText,
20 defaultTitle,
21 toDisplayName,
22 } from "./internal.ts";
23
24 export type WebappProps = {
25 /**
26 * Play EMM enterprise id. Immutable — changing it replaces the web app.
27 */
28 enterpriseId: string;
29 /**
30 * Server-assigned web app id (`app:com.google.enterprise.webapp.…`).
31 * Immutable — changing it replaces the web app.
32 */
33 webAppId?: string;
34 /**
35 * Title shown to users, as given. Defaults to a generated name.
36 */
37 title?: string;
38 /**
39 * Start URL loaded when the user opens the app. HTTP URLs require
40 * `displayMode: "minimalUi"`.
41 */
42 startUrl: string;
43 /**
44 * Display mode (`minimalUi`, `standalone`, or `fullScreen`).
45 * @default "standalone"
46 */
47 displayMode?: androidenterprise.WebAppDisplayModeEnum | (string & {});
48 /**
49 * Icons for the web app. PNG or JPEG, ideally 512x512, as base64url
50 * `imageData`.
51 */
52 icons?: androidenterprise.WebAppIcon[];
53 /**
54 * Whether the app is published to managed Google Play.
55 */
56 isPublished?: boolean;
57 };
58
59 export type Webapp = Resource<
60 "GCP.AndroidEnterprise.Webapp",
61 WebappProps,
62 {
63 /** Server-assigned web app id. */
64 webAppId: string;
65 /** Play EMM enterprise id. */
66 enterpriseId: string;
67 /** Project id used when the web app was reconciled. */
68 project: string;
69 /** User-facing title with the Alchemy ownership prefix stripped. */
70 title: string | undefined;
71 /** Start URL. */
72 startUrl: string | undefined;
73 /** Display mode. */
74 displayMode: string | undefined;
75 /** Whether the app is published. */
76 isPublished: boolean | undefined;
77 /** Current version code. */
78 versionCode: string | undefined;
79 },
80 never,
81 Providers
82 >;
83
84 /**
85 * A managed Google Play web app (`webapps`).
86 *
87 * Web apps have no labels field and their title is shown to users, so
88 * Alchemy tracks an app by its recorded id only (`list` finds apps from
89 * older releases that carried a title marker). `enterpriseId` and `webAppId` are identity
90 * — changing either replaces the app. Title, start URL, display mode,
91 * icons, and publish state update in place.
92 *
93 * ### Creating a Web App
94 * **Example:** Standalone HTTPS app
95 * ```typescript
96 * const app = yield* GCP.AndroidEnterprise.Webapp("Portal", {
97 * enterpriseId: "123456789",
98 * startUrl: "https://intranet.example.com/",
99 * title: "Portal",
100 * });
101 * ```
102 *
103 * **Example:** Minimal UI for an HTTP URL
104 * ```typescript
105 * const app = yield* GCP.AndroidEnterprise.Webapp("Legacy", {
106 * enterpriseId: "123456789",
107 * startUrl: "http://intranet.example.com/",
108 * displayMode: "minimalUi",
109 * title: "Legacy portal",
110 * });
111 * ```
112 *
113 * ### Updating a Web App
114 * Re-declare the same logical id with changed props; the engine keeps the
115 * physical resource and updates it in place.
116 *
117 * **Example:** Rename
118 * ```typescript
119 * const app = yield* GCP.AndroidEnterprise.Webapp("Portal", {
120 * enterpriseId: "123456789",
121 * startUrl: "https://intranet.example.com/",
122 * title: "Employee portal",
123 * });
124 * ```
125 *
126 * @resource
127 * @category AndroidEnterprise
128 */
129 export const Webapp = Resource<Webapp>("GCP.AndroidEnterprise.Webapp");
130
131 export class WebappNotResolved extends Data.TaggedError("GCP.AndroidEnterprise.WebappNotResolved")<{
132 enterpriseId: string;
133 webAppId: string;
134 }> {}
135
136 const toAttrs = (webapp: androidenterprise.WebApp, enterpriseId: string, project: string) => ({
137 webAppId: webapp.webAppId ?? "",
138 enterpriseId,
139 project,
140 title: parseOwnership(webapp.title).text,
141 startUrl: webapp.startUrl,
142 displayMode: webapp.displayMode,
143 isPublished: webapp.isPublished,
144 versionCode: webapp.versionCode,
145 });
146
147 const desiredBody = (input: {
148 webAppId?: string;
149 title: string;
150 news: WebappProps;
151 current?: androidenterprise.WebApp;
152 }): androidenterprise.WebApp => ({
153 webAppId: input.webAppId,
154 title: input.title,
155 startUrl: input.news.startUrl,
156 displayMode: input.news.displayMode ?? input.current?.displayMode ?? DEFAULT_DISPLAY_MODE,
157 icons: input.news.icons ?? input.current?.icons,
158 isPublished: input.news.isPublished ?? input.current?.isPublished,
159 });
160
161 const needsSync = (current: androidenterprise.WebApp, desired: androidenterprise.WebApp) =>
162 !sameText(current.title, desired.title) ||
163 !sameText(current.startUrl, desired.startUrl) ||
164 (desired.displayMode !== undefined && !sameText(current.displayMode, desired.displayMode)) ||
165 (desired.icons !== undefined && !jsonEqual(current.icons, desired.icons)) ||
166 (desired.isPublished !== undefined && current.isPublished !== desired.isPublished);
167
168 export const WebappProvider = () =>
169 Provider.succeed(Webapp, {
170 stables: ["webAppId", "enterpriseId", "project"],
171
172 diff: Effect.fn(function* ({ news, olds, output }) {
173 if (!isResolved(news)) return undefined;
174 const previousEnterprise = olds?.enterpriseId ?? output?.enterpriseId;
175 if (previousEnterprise !== undefined && news.enterpriseId !== previousEnterprise) {
176 return { action: "replace" as const, deleteFirst: false };
177 }
178 const previousId = olds?.webAppId ?? output?.webAppId;
179 if (previousId !== undefined && news.webAppId !== undefined && news.webAppId !== previousId) {
180 return { action: "replace" as const, deleteFirst: false };
181 }
182 return undefined;
183 }),
184
185 read: Effect.fn(function* ({ id, olds, output }) {
186 const env = yield* GcpEnvironment.current;
187 const enterpriseId = olds?.enterpriseId ?? output?.enterpriseId ?? "";
188 const webAppId = olds?.webAppId ?? output?.webAppId ?? "";
189 let existing = yield* getWebapp(enterpriseId, webAppId);
190 if (existing === undefined && enterpriseId.length > 0) {
191 existing = yield* findOwnedWebapp(id, enterpriseId);
192 }
193 if (existing === undefined) return undefined;
194 const attrs = toAttrs(existing, enterpriseId, env.project);
195 // Only a recorded id (or a legacy ownership marker) proves ownership;
196 // names and titles are user-visible and carry no marker.
197 return output !== undefined || (yield* ownedByAlchemy(id, existing.title))
198 ? attrs
199 : Unowned(attrs);
200 }),
201
202 list: () =>
203 Effect.gen(function* () {
204 const env = yield* GcpEnvironment.current;
205 const webapps = yield* listOwnedWebapps();
206 return webapps
207 .filter(({ webapp }) => hasOwnershipMarker(webapp.title))
208 .map(({ webapp, enterpriseId }) => toAttrs(webapp, enterpriseId, env.project));
209 }),
210
211 reconcile: Effect.fn(function* ({ id, news, output }) {
212 const env = yield* GcpEnvironment.current;
213 const enterpriseId = news.enterpriseId;
214 const displayName = yield* toDisplayName(id, news.title, output?.title);
215 const title = defaultTitle(news.title, displayName);
216
217 let current = yield* getWebapp(enterpriseId, news.webAppId ?? output?.webAppId ?? "");
218 if (current === undefined) {
219 current = yield* findOwnedWebapp(id, enterpriseId);
220 }
221
222 if (current === undefined) {
223 const created = yield* androidenterprise
224 .insertWebapps({
225 enterpriseId,
226 body: desiredBody({ title, news }),
227 })
228 .pipe(Effect.catchTag("Conflict", () => findOwnedWebapp(id, enterpriseId)));
229 current = created ?? undefined;
230 }
231
232 if (current === undefined) {
233 return yield* new WebappNotResolved({
234 enterpriseId,
235 webAppId: news.webAppId ?? output?.webAppId ?? displayName,
236 });
237 }
238
239 const webAppId = current.webAppId ?? news.webAppId ?? output?.webAppId ?? "";
240 const desired = desiredBody({
241 webAppId,
242 title,
243 news,
244 current,
245 });
246 if (needsSync(current, desired)) {
247 current = yield* androidenterprise.updateWebapps({
248 enterpriseId,
249 webAppId,
250 body: desired,
251 });
252 }
253
254 return toAttrs(current, enterpriseId, env.project);
255 }),
256
257 delete: Effect.fn(function* ({ output }) {
258 if (!output.enterpriseId || !output.webAppId) return;
259 yield* androidenterprise
260 .deleteWebapps({
261 enterpriseId: output.enterpriseId,
262 webAppId: output.webAppId,
263 })
264 .pipe(
265 Effect.catchTags({
266 NotFound: () => Effect.void,
267 BadRequest: () => Effect.void,
268 Conflict: () => Effect.void,
269 }),
270 );
271 }),
272 });