@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 244 lines 7.77 KB
Raw
1 import * as androidmanagement from "@distilled.cloud/gcp/androidmanagement_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 defaultWebAppIcons,
13 findWebAppByTitle,
14 getWebApp,
15 jsonEqual,
16 lastSegment,
17 MAX_WEB_APP_TITLE_LENGTH,
18 parentOf,
19 replaceOnIdentity,
20 sameText,
21 toDisplayName,
22 toEnterpriseName,
23 toWebAppName,
24 updateMaskOf,
25 } from "./internal.ts";
26
27 export type EnterprisesWebAppProps = {
28 /**
29 * Parent enterprise (`enterprises/{enterprise}` or `{enterprise}`).
30 * Immutable — changing it replaces the web app.
31 */
32 parent: string;
33 /**
34 * Start URL loaded when the user opens the app.
35 */
36 startUrl: string;
37 /**
38 * Title shown to users. Web apps have no labels field, so without
39 * state Alchemy finds the web app by this title.
40 * @default a unique name generated from the stack, stage, and logical id
41 */
42 title?: string;
43 /**
44 * Display mode (`MINIMAL_UI`, `STANDALONE`, or `FULL_SCREEN`).
45 * @default "STANDALONE"
46 */
47 displayMode?: androidmanagement.WebAppDisplayModeEnum | (string & {});
48 /**
49 * Icons for the web app. At least one is required; a 1x1 PNG is used
50 * when omitted.
51 */
52 icons?: androidmanagement.WebAppIconList;
53 };
54
55 export type EnterprisesWebApp = Resource<
56 "GCP.AndroidManagement.EnterprisesWebApp",
57 EnterprisesWebAppProps,
58 {
59 /** Resource name `enterprises/{enterprise}/webApps/{package}`. */
60 name: string;
61 /** Web app package id (last path segment). */
62 webAppId: string;
63 /** Parent enterprise name. */
64 parent: string;
65 /** Project id used when the web app was reconciled. */
66 project: string;
67 /** Title shown to users. */
68 title: string | undefined;
69 /** Start URL. */
70 startUrl: string | undefined;
71 /** Display mode. */
72 displayMode: string | undefined;
73 /** Icons. */
74 icons: androidmanagement.WebAppIconList | undefined;
75 /** Current version code. */
76 versionCode: string | undefined;
77 },
78 never,
79 Providers
80 >;
81
82 /**
83 * An Android Management web app.
84 *
85 * Web apps have no labels field, so Alchemy tracks the web app by its
86 * resource name; without state it is found by title, and one found under
87 * an explicit (non-generated) title is reported as unowned. `parent` is
88 * identity — changing it
89 * replaces the web app. Title, start URL, display mode, and icons
90 * update in place. At least one icon is required by the API; a 1x1 PNG
91 * is sent when `icons` is omitted.
92 *
93 * ### Creating a Web App
94 * **Example:** Standalone shortcut
95 * ```typescript
96 * const app = yield* GCP.AndroidManagement.EnterprisesWebApp("Docs", {
97 * parent: enterprise.name,
98 * startUrl: "https://docs.example.com/",
99 * title: "Docs",
100 * });
101 * ```
102 *
103 * **Example:** Full-screen kiosk URL
104 * ```typescript
105 * const app = yield* GCP.AndroidManagement.EnterprisesWebApp("Kiosk", {
106 * parent: enterprise.name,
107 * startUrl: "https://kiosk.example.com/",
108 * displayMode: "FULL_SCREEN",
109 * });
110 * ```
111 *
112 * ### Updating a Web App
113 * Change props on the same logical id; the engine keeps the physical id.
114 *
115 * **Example:** Change the title
116 * ```typescript
117 * const app = yield* GCP.AndroidManagement.EnterprisesWebApp("Docs", {
118 * parent: enterprise.name,
119 * startUrl: "https://docs.example.com/",
120 * title: "Internal docs",
121 * });
122 * ```
123 *
124 * @resource
125 * @category AndroidManagement
126 */
127 export const EnterprisesWebApp = Resource<EnterprisesWebApp>(
128 "GCP.AndroidManagement.EnterprisesWebApp",
129 );
130
131 export class EnterprisesWebAppNotResolved extends Data.TaggedError(
132 "GCP.AndroidManagement.EnterprisesWebAppNotResolved",
133 )<{
134 resourceName: string;
135 }> {}
136
137 const toAttrs = (app: androidmanagement.WebApp, project: string) => {
138 const name = app.name ?? "";
139 return {
140 name,
141 webAppId: lastSegment(name),
142 parent: parentOf(name),
143 project,
144 title: app.title,
145 startUrl: app.startUrl,
146 displayMode: app.displayMode,
147 icons: app.icons,
148 versionCode: app.versionCode,
149 };
150 };
151
152 export const EnterprisesWebAppProvider = () =>
153 Provider.succeed(EnterprisesWebApp, {
154 stables: ["name", "webAppId", "parent", "project"],
155
156 diff: Effect.fn(function* ({ news, olds, output }) {
157 if (!isResolved(news)) return undefined;
158 return replaceOnIdentity({
159 previousParent: olds?.parent ?? output?.parent,
160 nextParent: news.parent,
161 });
162 }),
163
164 read: Effect.fn(function* ({ id, olds, output }) {
165 const env = yield* GcpEnvironment.current;
166 const parent = olds?.parent ?? output?.parent ?? "";
167 const byName = yield* getWebApp(toWebAppName(parent, output?.name ?? output?.webAppId));
168 if (byName !== undefined) return toAttrs(byName, env.project);
169 if (parent.length === 0) return undefined;
170 const generated = yield* toDisplayName(id, undefined, undefined, MAX_WEB_APP_TITLE_LENGTH);
171 const title = olds?.title ?? generated;
172 const found = yield* findWebAppByTitle(toEnterpriseName(parent), title);
173 if (found === undefined) return undefined;
174 const attrs = toAttrs(found, env.project);
175 // A generated title is unique to this stack, stage and id.
176 return title === generated ? attrs : Unowned(attrs);
177 }),
178
179 reconcile: Effect.fn(function* ({ id, news, output }) {
180 const env = yield* GcpEnvironment.current;
181 const parent = toEnterpriseName(news.parent);
182 const title = yield* toDisplayName(id, news.title, output?.title, MAX_WEB_APP_TITLE_LENGTH);
183 const displayMode = news.displayMode ?? output?.displayMode ?? DEFAULT_DISPLAY_MODE;
184 const icons = defaultWebAppIcons(news.icons ?? output?.icons);
185 const desired: androidmanagement.WebApp = {
186 title,
187 startUrl: news.startUrl,
188 displayMode,
189 icons,
190 };
191
192 let current = yield* getWebApp(toWebAppName(parent, output?.name ?? output?.webAppId));
193 if (current === undefined) {
194 current = yield* findWebAppByTitle(parent, title);
195 }
196
197 if (current === undefined) {
198 const created = yield* androidmanagement
199 .createEnterprisesWebApps({
200 parent,
201 body: desired,
202 })
203 .pipe(Effect.catchTag("Conflict", () => findWebAppByTitle(parent, title)));
204 current = created ?? undefined;
205 }
206
207 if (current === undefined) {
208 return yield* new EnterprisesWebAppNotResolved({
209 resourceName: toWebAppName(parent, output?.name) || news.startUrl,
210 });
211 }
212
213 const name = current.name ?? output?.name ?? "";
214 const titleChanged = !sameText(current.title, title);
215 const urlChanged = !sameText(current.startUrl, news.startUrl);
216 const modeChanged = !sameText(current.displayMode, displayMode);
217 const iconsChanged = news.icons !== undefined && !jsonEqual(current.icons, icons);
218
219 const updateMask = updateMaskOf(
220 titleChanged ? "title" : undefined,
221 urlChanged ? "startUrl" : undefined,
222 modeChanged ? "displayMode" : undefined,
223 iconsChanged ? "icons" : undefined,
224 );
225
226 if (updateMask.length > 0 && name.length > 0) {
227 current = yield* androidmanagement.patchEnterprisesWebApps({
228 name,
229 updateMask,
230 body: desired,
231 });
232 }
233
234 const fresh = (yield* getWebApp(name)) ?? current;
235 return toAttrs(fresh, env.project);
236 }),
237
238 delete: Effect.fn(function* ({ output }) {
239 if (output.name.length === 0) return;
240 yield* androidmanagement
241 .deleteEnterprisesWebApps({ name: output.name })
242 .pipe(Effect.catchTag("NotFound", () => Effect.void));
243 }),
244 });