@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 202 lines 7.13 KB
Raw
1 import * as lexm from "@distilled.cloud/aws/lex-models-v2";
2 import { isResolved } from "alchemy/Diff";
3 import * as Provider from "alchemy/Provider";
4 import { Resource } from "alchemy/Resource";
5 import * as Data from "effect/Data";
6 import * as Effect from "effect/Effect";
7 import type { Providers } from "../Providers.ts";
8 import { retryWhileConflict, waitForBotSettled, waitForLocaleBuilt } from "./internal.ts";
9
10 export class BotVersionUnexpectedResponse extends Data.TaggedError("BotVersionUnexpectedResponse")<{
11 readonly message: string;
12 readonly cause?: unknown;
13 }> {}
14
15 export interface BotVersionProps {
16 /**
17 * ID of the bot to version. Changing it replaces the version.
18 */
19 botId: string;
20 /**
21 * The locales included in the version, snapshot from `DRAFT`. Each locale
22 * is built first if it is not already built (building a small bot takes
23 * one to a few minutes). Pass the `localeId` attribute of the Intent (or
24 * BotLocale) resources so the version depends on the conversation graph.
25 * Changing the list replaces the version.
26 */
27 localeIds: string[];
28 /**
29 * Description of the version. Changing it replaces the version (versions
30 * are immutable).
31 */
32 description?: string;
33 }
34
35 export interface BotVersion extends Resource<
36 "AWS.LexV2.BotVersion",
37 BotVersionProps,
38 {
39 /** ID of the bot the version belongs to. */
40 botId: string;
41 /** The numbered version Amazon Lex assigned, e.g. `1`. */
42 botVersion: string;
43 /** Status of the bot version (e.g. `Available`). */
44 botStatus: string;
45 /** The locales included in the version. */
46 localeIds: string[];
47 },
48 never,
49 Providers
50 > {}
51
52 /**
53 * An immutable numbered version of an Amazon Lex V2 bot, snapshot from the
54 * DRAFT version. The provider builds each included locale first (if needed),
55 * so the version is immediately usable behind a `BotAlias`.
56 *
57 * Versions are immutable: any prop change replaces the resource with a newly
58 * created version.
59 *
60 * ### Creating a Version
61 * **Example:** Version a Built Locale
62 * ```typescript
63 * import * as AWS from "@alchemy.run/aws";
64 *
65 * const version = yield* AWS.LexV2.BotVersion("V1", {
66 * botId: intent.botId,
67 * // depend on the intent so the build includes it
68 * localeIds: [intent.localeId],
69 * });
70 * const alias = yield* AWS.LexV2.BotAlias("Live", {
71 * botId: version.botId,
72 * botVersion: version.botVersion,
73 * });
74 * ```
75 *
76 * @resource
77 */
78 export const BotVersion = Resource<BotVersion>("AWS.LexV2.BotVersion");
79
80 const describeVersion = Effect.fn(function* (botId: string, botVersion: string) {
81 return yield* lexm
82 .describeBotVersion({ botId, botVersion })
83 .pipe(Effect.catchTag("ResourceNotFoundException", () => Effect.undefined));
84 });
85
86 export const BotVersionProvider = () =>
87 Provider.effect(
88 BotVersion,
89 Effect.gen(function* () {
90 return {
91 stables: ["botId", "botVersion"],
92
93 // Sub-resource keyed entirely by its bot — nuke reaches it through
94 // the parent bot's deletion.
95 list: () => Effect.succeed([]),
96
97 read: Effect.fn(function* ({ output }) {
98 // Versions are auto-numbered — without cached output there is no
99 // deterministic identity to look up.
100 if (output === undefined) return undefined;
101 const observed = yield* describeVersion(output.botId, output.botVersion);
102 if (observed === undefined) return undefined;
103 return {
104 botId: observed.botId!,
105 botVersion: observed.botVersion!,
106 botStatus: observed.botStatus!,
107 localeIds: output.localeIds,
108 };
109 }),
110
111 diff: Effect.fn(function* ({ news, olds }) {
112 if (!isResolved(news)) return undefined;
113 // Versions are immutable — any change replaces (a new version is
114 // created and the old one deleted).
115 if (
116 olds?.botId !== news.botId ||
117 (olds?.description ?? undefined) !== (news.description ?? undefined) ||
118 JSON.stringify([...(olds?.localeIds ?? [])].sort()) !==
119 JSON.stringify([...news.localeIds].sort())
120 ) {
121 return { action: "replace" } as const;
122 }
123 }),
124
125 reconcile: Effect.fn(function* ({ news, output, session }) {
126 // 1. OBSERVE — versions are immutable; if the cached version still
127 // exists there is nothing to converge.
128 if (output?.botVersion !== undefined) {
129 const existing = yield* describeVersion(news.botId, output.botVersion);
130 if (existing !== undefined) {
131 yield* session.note(`${news.botId}/${output.botVersion}`);
132 return {
133 botId: existing.botId!,
134 botVersion: existing.botVersion!,
135 botStatus: existing.botStatus!,
136 localeIds: news.localeIds,
137 };
138 }
139 }
140
141 // 2. ENSURE each included DRAFT locale is built.
142 for (const localeId of news.localeIds) {
143 const locale = yield* lexm.describeBotLocale({
144 botId: news.botId,
145 botVersion: "DRAFT",
146 localeId,
147 });
148 if (locale.botLocaleStatus !== "Built") {
149 // An in-flight build surfaces as Conflict — treat as a race.
150 yield* lexm
151 .buildBotLocale({
152 botId: news.botId,
153 botVersion: "DRAFT",
154 localeId,
155 })
156 .pipe(Effect.catchTag("ConflictException", () => Effect.void));
157 yield* waitForLocaleBuilt(news.botId, localeId);
158 }
159 }
160
161 // 3. CREATE the version snapshot and wait for it to settle.
162 const created = yield* retryWhileConflict(
163 lexm.createBotVersion({
164 botId: news.botId,
165 description: news.description,
166 botVersionLocaleSpecification: Object.fromEntries(
167 news.localeIds.map((localeId) => [localeId, { sourceBotVersion: "DRAFT" }]),
168 ),
169 }),
170 );
171 const botVersion = created.botVersion!;
172 yield* waitForBotSettled(news.botId);
173 const observed = yield* describeVersion(news.botId, botVersion);
174 if (observed === undefined) {
175 return yield* new BotVersionUnexpectedResponse({
176 message: `failed to read created Lex bot version ${news.botId}/${botVersion}`,
177 });
178 }
179
180 yield* session.note(`${news.botId}/${botVersion}`);
181 return {
182 botId: observed.botId!,
183 botVersion: observed.botVersion!,
184 botStatus: observed.botStatus!,
185 localeIds: news.localeIds,
186 };
187 }),
188
189 delete: Effect.fn(function* ({ output }) {
190 // Lex reports a missing version (or already-deleted parent bot) as
191 // PreconditionFailed.
192 yield* retryWhileConflict(
193 lexm.deleteBotVersion({
194 botId: output.botId,
195 botVersion: output.botVersion,
196 skipResourceInUseCheck: true,
197 }),
198 ).pipe(Effect.catchTag("PreconditionFailedException", () => Effect.void));
199 }),
200 };
201 }),
202 );