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

main
ts 199 lines 6.65 KB
Raw
1 import * as b2bi from "@distilled.cloud/aws/b2bi";
2 import { Unowned } from "alchemy/AdoptPolicy";
3 import * as Provider from "alchemy/Provider";
4 import { Resource } from "alchemy/Resource";
5 import { createInternalTags, hasAlchemyTags } from "alchemy/Tags";
6 import * as Effect from "effect/Effect";
7 import * as Stream from "effect/Stream";
8 import type { Providers } from "../Providers.ts";
9 import { readB2biTags, syncB2biTags, toWireTags } from "./internal.ts";
10
11 export interface CapabilityProps {
12 /**
13 * The name of the capability.
14 */
15 name: string;
16 /**
17 * The type of the capability. EDI is the only supported type today.
18 * @default "edi"
19 */
20 type?: "edi";
21 /**
22 * The capability configuration. For EDI capabilities this describes the
23 * X12 transaction set, direction, transformer, and the input/output S3
24 * locations. `configuration.edi.transformerId` may reference a
25 * {@link Transformer}'s `transformerId` output.
26 */
27 configuration: b2bi.CapabilityConfiguration;
28 /**
29 * S3 locations of instruction documents (implementation guides) attached
30 * to the capability.
31 */
32 instructionsDocuments?: b2bi.S3Location[];
33 /**
34 * User-defined tags for the capability.
35 */
36 tags?: Record<string, string>;
37 }
38
39 export interface Capability extends Resource<
40 "AWS.B2BI.Capability",
41 CapabilityProps,
42 {
43 /**
44 * Service-assigned unique ID of the capability.
45 */
46 capabilityId: string;
47 /**
48 * ARN of the capability.
49 */
50 capabilityArn: string;
51 /**
52 * Name of the capability.
53 */
54 name: string;
55 /**
56 * Capability type (`edi`).
57 */
58 type: string;
59 },
60 never,
61 Providers
62 > {}
63
64 /**
65 * An AWS B2B Data Interchange (B2BI) capability. A capability contains the
66 * information required to transform incoming or outgoing EDI documents: the
67 * X12 transaction set, a transformer, and the S3 input/output locations.
68 * ### Creating a Capability
69 * **Example:** Inbound X12 850 Capability
70 * ```typescript
71 * const capability = yield* B2BI.Capability("Orders", {
72 * name: "inbound-orders",
73 * configuration: {
74 * edi: {
75 * capabilityDirection: "INBOUND",
76 * type: { x12Details: { transactionSet: "X12_850", version: "VERSION_4010" } },
77 * inputLocation: { bucketName: bucket.bucketName, key: "inbound/" },
78 * outputLocation: { bucketName: bucket.bucketName, key: "processed/" },
79 * transformerId: transformer.transformerId,
80 * },
81 * },
82 * });
83 * ```
84 *
85 * @resource
86 */
87 export const Capability = Resource<Capability>("AWS.B2BI.Capability");
88
89 const toAttrs = (
90 r: b2bi.GetCapabilityResponse | b2bi.CreateCapabilityResponse | b2bi.UpdateCapabilityResponse,
91 ) => ({
92 capabilityId: r.capabilityId,
93 capabilityArn: r.capabilityArn,
94 name: r.name,
95 type: r.type,
96 });
97
98 export const CapabilityProvider = () =>
99 Provider.effect(
100 Capability,
101 Effect.gen(function* () {
102 const findByName = (name: string) =>
103 b2bi.listCapabilities.items({}).pipe(
104 Stream.filter((s) => s.name === name),
105 Stream.runHead,
106 Effect.flatMap((head) =>
107 head._tag === "Some"
108 ? b2bi
109 .getCapability({ capabilityId: head.value.capabilityId })
110 .pipe(Effect.catchTag("ResourceNotFoundException", () => Effect.undefined))
111 : Effect.undefined,
112 ),
113 );
114
115 return {
116 stables: ["capabilityId", "capabilityArn"],
117
118 read: Effect.fn(function* ({ id, olds, output }) {
119 const found = output?.capabilityId
120 ? yield* b2bi
121 .getCapability({ capabilityId: output.capabilityId })
122 .pipe(Effect.catchTag("ResourceNotFoundException", () => Effect.undefined))
123 : yield* findByName(olds?.name ?? "");
124 if (found === undefined) return undefined;
125 const attrs = toAttrs(found);
126 const tags = yield* readB2biTags(attrs.capabilityArn);
127 return (yield* hasAlchemyTags(id, tags)) ? attrs : Unowned(attrs);
128 }),
129
130 reconcile: Effect.fn(function* ({ id, news, output, session }) {
131 const internalTags = yield* createInternalTags(id);
132 const desiredTags = { ...internalTags, ...news.tags };
133
134 // 1. Observe.
135 let live = output?.capabilityId
136 ? yield* b2bi
137 .getCapability({ capabilityId: output.capabilityId })
138 .pipe(Effect.catchTag("ResourceNotFoundException", () => Effect.undefined))
139 : yield* findByName(news.name);
140
141 // 2. Ensure.
142 if (live === undefined) {
143 const created = yield* b2bi.createCapability({
144 name: news.name,
145 type: news.type ?? "edi",
146 configuration: news.configuration,
147 instructionsDocuments: news.instructionsDocuments,
148 tags: toWireTags(desiredTags),
149 });
150 live = yield* b2bi.getCapability({
151 capabilityId: created.capabilityId,
152 });
153 } else {
154 // 3. Sync — converge name, configuration, and instruction docs.
155 const nameDrift = live.name !== news.name;
156 const configDrift =
157 JSON.stringify(live.configuration) !== JSON.stringify(news.configuration);
158 const docsDrift =
159 JSON.stringify(live.instructionsDocuments ?? null) !==
160 JSON.stringify(news.instructionsDocuments ?? null);
161 if (nameDrift || configDrift || docsDrift) {
162 const updated = yield* b2bi.updateCapability({
163 capabilityId: live.capabilityId,
164 name: news.name,
165 configuration: news.configuration,
166 instructionsDocuments: news.instructionsDocuments,
167 });
168 live = { ...live, ...updated };
169 }
170 }
171
172 // 3b. Sync tags.
173 yield* syncB2biTags(live.capabilityArn, desiredTags);
174
175 yield* session.note(live.capabilityId);
176 return toAttrs(live);
177 }),
178
179 delete: Effect.fn(function* ({ output }) {
180 yield* b2bi
181 .deleteCapability({ capabilityId: output.capabilityId })
182 .pipe(Effect.catchTag("ResourceNotFoundException", () => Effect.void));
183 }),
184
185 list: () =>
186 b2bi.listCapabilities.items({}).pipe(
187 Stream.mapEffect((s) =>
188 b2bi.getCapability({ capabilityId: s.capabilityId }).pipe(
189 Effect.map(toAttrs),
190 Effect.catchTag("ResourceNotFoundException", () => Effect.undefined),
191 ),
192 ),
193 Stream.filter((item) => item !== undefined),
194 Stream.runCollect,
195 Effect.map((chunk) => Array.from(chunk)),
196 ),
197 };
198 }),
199 );