The best way to control Claude Code, Codex and more from any computer or phone

main
ts 164 lines 5.34 KB
Raw
1 import * as contacts from "@distilled.cloud/aws/ssm-contacts";
2 import { isResolved } from "alchemy/Diff";
3 import * as Provider from "alchemy/Provider";
4 import { Resource } from "alchemy/Resource";
5 import * as Effect from "effect/Effect";
6 import type { Providers } from "../Providers.ts";
7
8 export interface PlanProps {
9 /**
10 * The ARN of the contact or escalation plan whose engagement plan this
11 * resource manages. Changing it replaces the plan.
12 */
13 contactId: string;
14
15 /**
16 * The stages that Incident Manager runs through when engaging the contact:
17 * each stage has a duration and a set of channel/contact targets.
18 */
19 stages?: contacts.Stage[];
20
21 /**
22 * ARNs of the on-call rotations associated with the plan (only valid for
23 * `ONCALL_SCHEDULE` contacts).
24 */
25 rotationIds?: string[];
26 }
27
28 /** @resource */
29 export interface Plan extends Resource<
30 "AWS.SSMContacts.Plan",
31 PlanProps,
32 {
33 /** ARN of the contact whose engagement plan is managed. */
34 contactArn: string;
35 /** Number of stages in the plan. */
36 stageCount: number;
37 },
38 never,
39 Providers
40 > {}
41
42 /**
43 * The engagement plan of an Incident Manager contact — the staged sequence
44 * of contact channels (for `PERSONAL` contacts), contacts (for `ESCALATION`
45 * plans), or rotations (for `ONCALL_SCHEDULE` contacts) that Incident
46 * Manager engages during an incident.
47 *
48 * Manage a contact's plan either inline via the `plan` prop on
49 * `SSMContacts.Contact` or standalone with this resource — not both.
50 * Deleting this resource resets the contact's plan to empty.
51 *
52 * ### Managing Engagement Plans
53 * **Example:** Engage an email channel for 5 minutes
54 * ```typescript
55 * const plan = yield* SSMContacts.Plan("OncallPlan", {
56 * contactId: oncall.contactArn,
57 * stages: [
58 * {
59 * DurationInMinutes: 5,
60 * Targets: [
61 * {
62 * ChannelTargetInfo: {
63 * ContactChannelId: email.contactChannelArn,
64 * RetryIntervalInMinutes: 1,
65 * },
66 * },
67 * ],
68 * },
69 * ],
70 * });
71 * ```
72 */
73 const PlanResource = Resource<Plan>("AWS.SSMContacts.Plan");
74
75 export { PlanResource as Plan };
76
77 export const PlanProvider = () =>
78 Provider.effect(
79 PlanResource,
80 Effect.gen(function* () {
81 const getContact = (arn: string) =>
82 contacts
83 .getContact({ ContactId: arn })
84 .pipe(Effect.catchTag("ResourceNotFoundException", () => Effect.undefined));
85
86 const buildAttrs = (contact: contacts.GetContactResult) => ({
87 contactArn: contact.ContactArn,
88 stageCount: contact.Plan.Stages?.length ?? 0,
89 });
90
91 return PlanResource.Provider.of({
92 stables: ["contactArn"],
93
94 // Settings sub-resource of a contact — nothing to enumerate.
95 list: () => Effect.succeed([]),
96
97 read: Effect.fn(function* ({ olds, output }) {
98 const arn = output?.contactArn ?? olds?.contactId;
99 if (arn === undefined) return undefined;
100 const contact = yield* getContact(arn);
101 if (contact === undefined) return undefined;
102 // The plan is a setting on the contact, not a distinct cloud
103 // object — it "exists" when the contact has stages or rotations.
104 const hasPlan =
105 (contact.Plan.Stages?.length ?? 0) > 0 || (contact.Plan.RotationIds?.length ?? 0) > 0;
106 return hasPlan ? buildAttrs(contact) : undefined;
107 }),
108
109 diff: Effect.fn(function* ({ news, olds }) {
110 if (!isResolved(news)) return undefined;
111 if (olds.contactId !== news.contactId) {
112 return { action: "replace" } as const;
113 }
114 }),
115
116 reconcile: Effect.fn(function* ({ news, session }) {
117 // 1. OBSERVE — the contact is authoritative for its current plan.
118 const contact = yield* contacts.getContact({
119 ContactId: news.contactId,
120 });
121
122 // 2/3. SYNC — apply the desired plan when it differs from the
123 // observed one (updateContact fully replaces the plan).
124 const desired: contacts.Plan = {
125 Stages: news.stages ?? [],
126 ...(news.rotationIds !== undefined ? { RotationIds: news.rotationIds } : {}),
127 };
128 const observed: contacts.Plan = {
129 Stages: contact.Plan.Stages ?? [],
130 ...(contact.Plan.RotationIds !== undefined
131 ? { RotationIds: contact.Plan.RotationIds }
132 : {}),
133 };
134 if (JSON.stringify(desired) !== JSON.stringify(observed)) {
135 yield* contacts.updateContact({
136 ContactId: contact.ContactArn,
137 Plan: desired,
138 });
139 }
140
141 // 4. RETURN fresh attributes.
142 const final = yield* contacts.getContact({
143 ContactId: news.contactId,
144 });
145 yield* session.note(final.ContactArn);
146 return buildAttrs(final);
147 }),
148
149 delete: Effect.fn(function* ({ output }) {
150 // Deleting the plan resets the contact's engagement plan. A
151 // vanished contact means there is nothing left to reset.
152 yield* contacts
153 .updateContact({
154 ContactId: output.contactArn,
155 Plan: { Stages: [] },
156 })
157 .pipe(
158 Effect.asVoid,
159 Effect.catchTag("ResourceNotFoundException", () => Effect.void),
160 );
161 }),
162 });
163 }),
164 );