@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 135 lines 4.68 KB
Raw
1 import * as devopsguru from "@distilled.cloud/aws/devops-guru";
2 import { Unowned } from "alchemy/AdoptPolicy";
3 import * as Provider from "alchemy/Provider";
4 import { Resource } from "alchemy/Resource";
5 import * as Effect from "effect/Effect";
6 import * as Schedule from "effect/Schedule";
7 import type { Providers } from "../Providers.ts";
8
9 export interface EventSourcesConfigProps {
10 /**
11 * Consume Amazon CodeGuru Profiler recommendations as an event source so
12 * DevOps Guru can surface them as proactive insights.
13 * @default false
14 */
15 amazonCodeGuruProfiler?: boolean;
16 }
17
18 export interface EventSourcesConfig extends Resource<
19 "AWS.DevOpsGuru.EventSourcesConfig",
20 EventSourcesConfigProps,
21 {
22 /** Whether CodeGuru Profiler recommendations are consumed. */
23 amazonCodeGuruProfiler: boolean;
24 },
25 never,
26 Providers
27 > {}
28
29 /**
30 * The DevOps Guru event sources configuration — the account/region singleton
31 * that controls which integrated services DevOps Guru consumes
32 * recommendations from as event sources. The one supported source is Amazon
33 * CodeGuru Profiler: when enabled, profiler recommendations surface as
34 * DevOps Guru proactive insights.
35 *
36 * An account has exactly one configuration, so this resource is a
37 * capture-and-restore singleton: adopting an enabled configuration that
38 * Alchemy did not create requires `--adopt`. Destroying the resource
39 * restores the default (disabled).
40 *
41 * ### Enabling Event Sources
42 * **Example:** Consume CodeGuru Profiler Recommendations
43 * ```typescript
44 * const eventSources = yield* DevOpsGuru.EventSourcesConfig("EventSources", {
45 * amazonCodeGuruProfiler: true,
46 * });
47 * ```
48 *
49 * @resource
50 */
51 export const EventSourcesConfig = Resource<EventSourcesConfig>("AWS.DevOpsGuru.EventSourcesConfig");
52
53 /**
54 * Concurrent `UpdateEventSourcesConfig` calls conflict server-side — retry
55 * the typed conflict tag on a short bounded schedule.
56 *
57 * Explicitly typed: inlining `Effect.retry` with options in provider
58 * lifecycle code can widen the provider layer to `unknown` in declaration
59 * emit.
60 *
61 * @internal
62 */
63 const retryUpdateConflict = <A, E extends { readonly _tag: string }, R>(
64 self: Effect.Effect<A, E, R>,
65 ): Effect.Effect<A, E, R> =>
66 Effect.retry(self, {
67 while: (e) => e._tag === "ConflictException",
68 schedule: Schedule.max([Schedule.fixed("2 seconds"), Schedule.recurs(8)]),
69 });
70
71 export const EventSourcesConfigProvider = () =>
72 Provider.effect(
73 EventSourcesConfig,
74 Effect.gen(function* () {
75 // Observe the live configuration. An absent section or status means
76 // the account default (disabled).
77 const observe = Effect.gen(function* () {
78 const { EventSources } = yield* devopsguru.describeEventSourcesConfig({});
79 return {
80 amazonCodeGuruProfiler: EventSources?.AmazonCodeGuruProfiler?.Status === "ENABLED",
81 };
82 });
83
84 const setProfiler = Effect.fn(function* (status: devopsguru.EventSourceOptInStatus) {
85 yield* retryUpdateConflict(
86 devopsguru.updateEventSourcesConfig({
87 EventSources: { AmazonCodeGuruProfiler: { Status: status } },
88 }),
89 );
90 });
91
92 return {
93 // Account/region singleton — surfaced only when non-default.
94 list: () =>
95 observe.pipe(
96 Effect.map((observed) => (observed.amazonCodeGuruProfiler ? [observed] : [])),
97 ),
98
99 read: Effect.fn(function* ({ output }) {
100 const observed = yield* observe;
101 if (!observed.amazonCodeGuruProfiler) {
102 return undefined;
103 }
104 // The configuration can't carry ownership tags — an enabled
105 // configuration we have no record of belongs to someone else
106 // until explicitly adopted.
107 return output !== undefined ? observed : Unowned(observed);
108 }),
109
110 reconcile: Effect.fn(function* ({ news, session }) {
111 // 1. OBSERVE — the live configuration is authoritative.
112 const observed = yield* observe;
113
114 // 2. SYNC — apply only when drifted.
115 const desired = news.amazonCodeGuruProfiler ?? false;
116 if (observed.amazonCodeGuruProfiler !== desired) {
117 yield* setProfiler(desired ? "ENABLED" : "DISABLED");
118 }
119
120 // 3. RETURN fresh attributes.
121 const final = yield* observe;
122 yield* session.note(`amazonCodeGuruProfiler: ${final.amazonCodeGuruProfiler}`);
123 return final;
124 }),
125
126 // "Deleting" the singleton restores the default (disabled).
127 delete: Effect.fn(function* () {
128 const observed = yield* observe;
129 if (observed.amazonCodeGuruProfiler) {
130 yield* setProfiler("DISABLED");
131 }
132 }),
133 };
134 }),
135 );