1import * as AWS from "@/AWS";
2import { Agent, AgentAlias } from "@/AWS/Bedrock";
3import * as Test from "@/Test/Alchemy";
4import * as bedrock from "@distilled.cloud/aws/bedrock-agent";
5import * as iam from "@distilled.cloud/aws/iam";
6import { expect } from "alchemy-test";
7import * as Data from "effect/Data";
8import * as Effect from "effect/Effect";
9import * as Schedule from "effect/Schedule";
10
11const { test } = Test.make({ providers: AWS.providers() });
12
13// Nova Micro via the us cross-region inference profile — the cheapest
14// on-demand model enabled in the testing account, supported by Bedrock Agents.
15const MODEL = "us.amazon.nova-micro-v1:0";
16const INSTRUCTION =
17 "You are a helpful assistant. Answer every question as concisely as you can.";
18
19const findAgent = (agentId: string) =>
20 bedrock.getAgent({ agentId }).pipe(
21 Effect.map((r) => r.agent),
22 Effect.catchTag("ResourceNotFoundException", () =>
23 Effect.succeed(undefined),
24 ),
25 );
26
27const findAlias = (agentId: string, agentAliasId: string) =>
28 bedrock.getAgentAlias({ agentId, agentAliasId }).pipe(
29 Effect.map((r) => r.agentAlias),
30 Effect.catchTag("ResourceNotFoundException", () =>
31 Effect.succeed(undefined),
32 ),
33 );
34
35class StillExists extends Data.TaggedError("StillExists")<{
36 readonly what: string;
37}> {}
38
39const assertAgentGone = (agentId: string) =>
40 findAgent(agentId).pipe(
41 Effect.flatMap((a) =>
42 a === undefined || a.agentStatus === "DELETING"
43 ? Effect.void
44 : Effect.fail(new StillExists({ what: `agent ${agentId}` })),
45 ),
46 Effect.retry({
47 while: (e) => e._tag === "StillExists",
48 schedule: Schedule.max([Schedule.exponential(1000), Schedule.recurs(8)]),
49 }),
50 );
51
52test.provider(
53 "create + prepare agent, attach alias, update, delete",
54 (stack) =>
55 Effect.gen(function* () {
56 yield* stack.destroy();
57
58 // 1. Deploy an agent (auto-created execution role) and an alias.
59 const result = yield* stack.deploy(
60 Effect.gen(function* () {
61 const agent = yield* Agent("TestAgent", {
62 foundationModel: MODEL,
63 instruction: INSTRUCTION,
64 description: "alchemy bedrock agent test",
65 tags: { Environment: "test" },
66 });
67 const alias = yield* AgentAlias("TestAlias", {
68 agentId: agent.agentId,
69 description: "prod alias",
70 });
71 return {
72 agentId: agent.agentId,
73 agentArn: agent.agentArn,
74 roleName: agent.roleName,
75 aliasId: alias.agentAliasId,
76 aliasArn: alias.agentAliasArn,
77 };
78 }),
79 );
80
81 expect(result.agentId).toBeDefined();
82 expect(result.agentArn).toContain(":agent/");
83 expect(result.aliasId).toBeDefined();
84
85 // out-of-band: agent is prepared, tags branded, role exists.
86 const created = yield* findAgent(result.agentId);
87 expect(created).toBeDefined();
88 expect(["PREPARED", "NOT_PREPARED"]).toContain(created!.agentStatus);
89 expect(created!.foundationModel).toBe(MODEL);
90
91 const tags = yield* bedrock
92 .listTagsForResource({ resourceArn: result.agentArn })
93 .pipe(Effect.map((r) => r.tags ?? {}));
94 expect(tags.Environment).toBe("test");
95 expect(tags["alchemy::id"]).toBe("TestAgent");
96
97 expect(result.roleName).toBeDefined();
98 const role = yield* iam
99 .getRole({ RoleName: result.roleName! })
100 .pipe(Effect.map((r) => r.Role));
101 expect(role.Arn).toBe(created!.agentResourceRoleArn);
102
103 // out-of-band: alias exists and is prepared.
104 const alias = yield* findAlias(result.agentId, result.aliasId);
105 expect(alias).toBeDefined();
106 expect(["PREPARED", "UPDATING"]).toContain(alias!.agentAliasStatus);
107
108 // 2. Update the agent's instruction — converges via updateAgent.
109 yield* stack.deploy(
110 Effect.gen(function* () {
111 const agent = yield* Agent("TestAgent", {
112 foundationModel: MODEL,
113 instruction: `${INSTRUCTION} Always cite your reasoning.`,
114 description: "alchemy bedrock agent test (updated)",
115 tags: { Environment: "test" },
116 });
117 yield* AgentAlias("TestAlias", {
118 agentId: agent.agentId,
119 description: "prod alias",
120 });
121 return agent.agentId;
122 }),
123 );
124
125 const updated = yield* findAgent(result.agentId);
126 expect(updated!.description).toBe("alchemy bedrock agent test (updated)");
127
128 // 3. Destroy — agent, alias, and managed role are all removed.
129 yield* stack.destroy();
130 yield* assertAgentGone(result.agentId);
131
132 const roleGone = yield* iam.getRole({ RoleName: result.roleName! }).pipe(
133 Effect.map(() => false),
134 Effect.catchTag("NoSuchEntityException", () => Effect.succeed(true)),
135 );
136 expect(roleGone).toBe(true);
137 }),
138 {
139 tags: ["provider:aws", "provider:aws:bedrock", "provider:aws:iam", "live"],
140 timeout: 240_000,
141 },
142);