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

main
ts 239 lines 7.98 KB
Raw
1 import * as schemas from "@distilled.cloud/aws/schemas";
2 import { Unowned } from "alchemy/AdoptPolicy";
3 import { isResolved } from "alchemy/Diff";
4 import { createPhysicalName } from "alchemy/PhysicalName";
5 import * as Provider from "alchemy/Provider";
6 import { Resource } from "alchemy/Resource";
7 import { createInternalTags, hasAlchemyTags } from "alchemy/Tags";
8 import * as Effect from "effect/Effect";
9 import type { Providers } from "../Providers.ts";
10 import { canonicalJson, syncSchemasTags } from "./internal.ts";
11
12 export interface SchemaProps {
13 /**
14 * The name of the registry the schema belongs to. Changing it replaces the
15 * schema.
16 */
17 registryName: string;
18
19 /**
20 * Name of the schema. Must match `[a-zA-Z0-9-_.@]+` and be at most 385
21 * characters. If omitted, a unique name is generated. Changing it replaces
22 * the schema.
23 */
24 schemaName?: string;
25
26 /**
27 * The type of the schema document. Changing it replaces the schema.
28 * @default "OpenApi3"
29 */
30 type?: "OpenApi3" | "JSONSchemaDraft4";
31
32 /**
33 * The schema document as a JSON string (an OpenAPI 3 document or a
34 * JSONSchema Draft 4 document, depending on `type`). Updating the content
35 * publishes a new schema version.
36 */
37 content: string;
38
39 /**
40 * A description of the schema.
41 */
42 description?: string;
43
44 /**
45 * User tags to attach to the schema.
46 */
47 tags?: Record<string, string>;
48 }
49
50 export interface Schema extends Resource<
51 "AWS.Schemas.Schema",
52 SchemaProps,
53 {
54 /** The name of the registry the schema belongs to. */
55 registryName: string;
56 /** The name of the schema. */
57 schemaName: string;
58 /** The ARN of the schema. */
59 schemaArn: string;
60 /** The version of the schema currently described by `content`. */
61 schemaVersion: string;
62 /** The type of the schema document. */
63 type: string;
64 },
65 never,
66 Providers
67 > {}
68
69 /**
70 * An event schema in an EventBridge Schema Registry — a versioned OpenAPI 3
71 * or JSONSchema Draft 4 document describing the structure of an event.
72 * Updating the content publishes a new schema version; previous versions are
73 * retained by the registry.
74 *
75 * ### Creating a Schema
76 * **Example:** OpenAPI 3 Schema
77 * ```typescript
78 * const registry = yield* AWS.Schemas.Registry("app-events", {});
79 *
80 * const schema = yield* AWS.Schemas.Schema("OrderCreated", {
81 * registryName: registry.registryName,
82 * type: "OpenApi3",
83 * content: JSON.stringify({
84 * openapi: "3.0.0",
85 * info: { version: "1.0.0", title: "OrderCreated" },
86 * paths: {},
87 * components: {
88 * schemas: {
89 * OrderCreated: {
90 * type: "object",
91 * properties: { orderId: { type: "string" } },
92 * },
93 * },
94 * },
95 * }),
96 * });
97 * ```
98 *
99 * **Example:** JSONSchema Draft 4 Schema
100 * ```typescript
101 * const schema = yield* AWS.Schemas.Schema("UserSignedUp", {
102 * registryName: registry.registryName,
103 * type: "JSONSchemaDraft4",
104 * content: JSON.stringify({
105 * $schema: "http://json-schema.org/draft-04/schema#",
106 * type: "object",
107 * properties: { userId: { type: "string" } },
108 * }),
109 * description: "Emitted when a user completes sign-up",
110 * });
111 * ```
112 *
113 * @resource
114 */
115 export const Schema = Resource<Schema>("AWS.Schemas.Schema");
116
117 export const SchemaProvider = () =>
118 Provider.effect(
119 Schema,
120 Effect.gen(function* () {
121 const createName = Effect.fn(function* (id: string, props: SchemaProps) {
122 return props.schemaName ?? (yield* createPhysicalName({ id, maxLength: 385 }));
123 });
124
125 const describe = (registryName: string, schemaName: string) =>
126 schemas
127 .describeSchema({
128 RegistryName: registryName,
129 SchemaName: schemaName,
130 })
131 .pipe(Effect.catchTag("NotFoundException", () => Effect.undefined));
132
133 return Schema.Provider.of({
134 stables: ["registryName", "schemaName", "schemaArn"],
135
136 // Sub-resource keyed by its parent registry — listSchemas requires a
137 // RegistryName, so there is no account-wide enumeration.
138 list: () => Effect.succeed([]),
139
140 read: Effect.fn(function* ({ id, olds, output }) {
141 const registryName = output?.registryName ?? olds?.registryName;
142 if (registryName === undefined) return undefined;
143 const schemaName =
144 output?.schemaName ?? (yield* createName(id, olds ?? { registryName, content: "" }));
145 const found = yield* describe(registryName, schemaName);
146 if (!found) return undefined;
147 const attrs = {
148 registryName,
149 schemaName,
150 schemaArn: found.SchemaArn!,
151 schemaVersion: found.SchemaVersion!,
152 type: found.Type ?? "OpenApi3",
153 };
154 return (yield* hasAlchemyTags(id, found.Tags)) ? attrs : Unowned(attrs);
155 }),
156
157 diff: Effect.fn(function* ({ id, news, olds }) {
158 if (!isResolved(news)) return undefined;
159 if (news.registryName !== olds.registryName) {
160 return { action: "replace" } as const;
161 }
162 const oldName = yield* createName(id, olds);
163 const newName = yield* createName(id, news);
164 if (oldName !== newName) return { action: "replace" } as const;
165 if ((news.type ?? "OpenApi3") !== (olds.type ?? "OpenApi3")) {
166 return { action: "replace" } as const;
167 }
168 return undefined;
169 }),
170
171 reconcile: Effect.fn(function* ({ id, news, output, session }) {
172 const registryName = news.registryName;
173 const schemaName = output?.schemaName ?? (yield* createName(id, news));
174 const internalTags = yield* createInternalTags(id);
175 const desiredType = news.type ?? "OpenApi3";
176
177 // OBSERVE
178 let live = yield* describe(registryName, schemaName);
179
180 // ENSURE
181 if (live === undefined) {
182 yield* schemas.createSchema({
183 RegistryName: registryName,
184 SchemaName: schemaName,
185 Type: desiredType,
186 Content: news.content,
187 Description: news.description,
188 Tags: { ...news.tags, ...internalTags },
189 });
190 live = yield* schemas.describeSchema({
191 RegistryName: registryName,
192 SchemaName: schemaName,
193 });
194 }
195
196 // SYNC content / type / description — diff observed against desired.
197 // Content is compared canonically (sorted keys, no whitespace) so a
198 // reformatted-but-equal document does not publish a new version.
199 const contentChanged =
200 canonicalJson(news.content) !== canonicalJson(live.Content ?? "") ||
201 desiredType !== (live.Type ?? "OpenApi3");
202 const descriptionChanged = (news.description ?? "") !== (live.Description ?? "");
203 if (contentChanged || descriptionChanged) {
204 yield* schemas.updateSchema({
205 RegistryName: registryName,
206 SchemaName: schemaName,
207 ...(contentChanged ? { Content: news.content, Type: desiredType } : {}),
208 ...(descriptionChanged ? { Description: news.description ?? "" } : {}),
209 });
210 live = yield* schemas.describeSchema({
211 RegistryName: registryName,
212 SchemaName: schemaName,
213 });
214 }
215
216 // SYNC tags — diff against observed cloud tags.
217 yield* syncSchemasTags(live.SchemaArn!, id, news.tags);
218
219 yield* session.note(`${registryName}/${schemaName}`);
220 return {
221 registryName,
222 schemaName,
223 schemaArn: live.SchemaArn!,
224 schemaVersion: live.SchemaVersion!,
225 type: live.Type ?? desiredType,
226 };
227 }),
228
229 delete: Effect.fn(function* ({ output }) {
230 yield* schemas
231 .deleteSchema({
232 RegistryName: output.registryName,
233 SchemaName: output.schemaName,
234 })
235 .pipe(Effect.catchTag("NotFoundException", () => Effect.void));
236 }),
237 });
238 }),
239 );