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

main
ts 63 lines 2.31 KB
Raw
1 import type * as chatbot from "@distilled.cloud/aws/chatbot";
2 import { Region, type RegionName } from "@distilled.cloud/aws/Region";
3 import type { SourceError } from "effect/ConfigProvider";
4 import * as Effect from "effect/Effect";
5 import * as Option from "effect/Option";
6
7 /**
8 * AWS Chatbot has endpoints only in these regions (there is no
9 * `chatbot.{region}.amazonaws.com` anywhere else). Chat configurations are
10 * global (their ARNs carry no region), so any supported endpoint sees the
11 * same data. The providers and bindings follow the ambient region when it is
12 * supported and use `us-east-2` otherwise.
13 *
14 * @internal
15 */
16 const CHATBOT_REGIONS: ReadonlySet<string> = new Set([
17 "us-east-2",
18 "us-west-2",
19 "eu-west-1",
20 "ap-southeast-1",
21 ]);
22
23 const CHATBOT_HOME_REGION: RegionName = "us-east-2";
24
25 /**
26 * Run a distilled Chatbot effect in a supported region: the ambient region
27 * when Chatbot is offered there, otherwise `us-east-2`. A missing `Region`
28 * (distilled treats it as an optional override) also falls back to
29 * `us-east-2`, so the helper adds no requirement.
30 *
31 * `Region`'s service value is an `Effect<RegionName>`, so it is provided as
32 * an effect, not a bare string.
33 *
34 * @internal
35 */
36 export const inChatbotRegion = <A, E, R>(
37 effect: Effect.Effect<A, E, R>,
38 ): Effect.Effect<A, E | SourceError, R> =>
39 Effect.gen(function* () {
40 const ambient = yield* Effect.serviceOption(Region);
41 // No region configured falls back to the home region too.
42 const region = Option.isSome(ambient)
43 ? yield* ambient.value.pipe(
44 Effect.catchTag("Alchemy::AWS::MissingRegion", () => Effect.undefined),
45 )
46 : undefined;
47 return region !== undefined && CHATBOT_REGIONS.has(region)
48 ? yield* effect
49 : yield* effect.pipe(Effect.provideService(Region, Effect.succeed(CHATBOT_HOME_REGION)));
50 });
51
52 /**
53 * Convert a plain tag map to the Chatbot wire `Tag` list
54 * (`{ TagKey, TagValue }`).
55 */
56 export const toChatbotTags = (tags: Record<string, string>): chatbot.Tag[] =>
57 Object.entries(tags).map(([TagKey, TagValue]) => ({ TagKey, TagValue }));
58
59 /**
60 * Convert an observed Chatbot wire `Tag` list to a plain tag map.
61 */
62 export const fromChatbotTags = (tags: readonly chatbot.Tag[] | undefined): Record<string, string> =>
63 Object.fromEntries((tags ?? []).map((t) => [t.TagKey, t.TagValue]));