@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, 06:00 UTC

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