-
Notifications
You must be signed in to change notification settings - Fork 86
fix(import): harden Bedrock Agent runtime imports #2153
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
aidandaly24
wants to merge
3
commits into
aws:refactor
Choose a base branch
from
aidandaly24:fix/bedrock-agent-import
base: refactor
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,138 @@ | ||
| import { describe, expect, test } from "bun:test"; | ||
| import type { | ||
| GetAgentAliasCommandOutput, | ||
| GetAgentCommandOutput, | ||
| } from "@aws-sdk/client-bedrock-agent"; | ||
| import { InputValidationError, MalformedServiceResponseError } from "../../errors"; | ||
| import { createDescribeBedrockAgent, type BedrockAgentControlClient } from "./bedrockAgent"; | ||
|
|
||
| const agent = { | ||
| agentId: "A1B2C3D4E5", | ||
| agentName: "SupportAgent", | ||
| agentArn: "arn:aws:bedrock:us-east-1:111122223333:agent/A1B2C3D4E5", | ||
| agentVersion: "DRAFT", | ||
| agentStatus: "PREPARED", | ||
| idleSessionTTLInSeconds: 600, | ||
| agentResourceRoleArn: "arn:aws:iam::111122223333:role/BedrockAgentRole", | ||
| createdAt: new Date(0), | ||
| updatedAt: new Date(0), | ||
| foundationModel: "us.amazon.nova-lite-v1:0", | ||
| }; | ||
|
|
||
| const agentAlias = { | ||
| agentId: agent.agentId, | ||
| agentAliasId: "TSTALIASID", | ||
| agentAliasName: "live", | ||
| agentAliasArn: `arn:aws:bedrock:us-east-1:111122223333:agent-alias/${agent.agentId}/TSTALIASID`, | ||
| routingConfiguration: [{ agentVersion: "1" }], | ||
| createdAt: new Date(0), | ||
| updatedAt: new Date(0), | ||
| agentAliasStatus: "PREPARED", | ||
| }; | ||
|
|
||
| class TestBedrockAgentControlClient implements BedrockAgentControlClient { | ||
| readonly calls: string[] = []; | ||
| agentOutput: GetAgentCommandOutput = { agent } as GetAgentCommandOutput; | ||
| aliasOutput: GetAgentAliasCommandOutput = { | ||
| agentAlias, | ||
| } as GetAgentAliasCommandOutput; | ||
| agentError?: Error; | ||
| aliasError?: Error; | ||
|
|
||
| async getAgent(): Promise<GetAgentCommandOutput> { | ||
| this.calls.push("getAgent"); | ||
| if (this.agentError) throw this.agentError; | ||
| return this.agentOutput; | ||
| } | ||
|
|
||
| async getAgentAlias(): Promise<GetAgentAliasCommandOutput> { | ||
| this.calls.push("getAgentAlias"); | ||
| if (this.aliasError) throw this.aliasError; | ||
| return this.aliasOutput; | ||
| } | ||
| } | ||
|
|
||
| function resourceNotFound(): Error { | ||
| return Object.assign(new Error("not found"), { name: "ResourceNotFoundException" }); | ||
| } | ||
|
|
||
| describe("describeBedrockAgent", () => { | ||
| test("returns metadata from the requested agent and alias", async () => { | ||
| const client = new TestBedrockAgentControlClient(); | ||
| const describeAgent = createDescribeBedrockAgent(() => client); | ||
|
|
||
| await expect( | ||
| describeAgent({ | ||
| region: "us-east-1", | ||
| agentId: agent.agentId, | ||
| agentAliasId: agentAlias.agentAliasId, | ||
| }), | ||
| ).resolves.toEqual({ | ||
| agentName: agent.agentName, | ||
| agentStatus: agent.agentStatus, | ||
| agentAliasArn: agentAlias.agentAliasArn, | ||
| agentAliasName: agentAlias.agentAliasName, | ||
| agentAliasStatus: agentAlias.agentAliasStatus, | ||
| foundationModel: agent.foundationModel, | ||
| description: undefined, | ||
| }); | ||
| expect(client.calls).toEqual(["getAgent", "getAgentAlias"]); | ||
| }); | ||
|
|
||
| test("rejects an incomplete agent response before requesting the alias", async () => { | ||
| const client = new TestBedrockAgentControlClient(); | ||
| client.agentOutput = {} as GetAgentCommandOutput; | ||
| const describeAgent = createDescribeBedrockAgent(() => client); | ||
|
|
||
| await expect( | ||
| describeAgent({ | ||
| region: "us-east-1", | ||
| agentId: agent.agentId, | ||
| agentAliasId: agentAlias.agentAliasId, | ||
| }), | ||
| ).rejects.toBeInstanceOf(MalformedServiceResponseError); | ||
| expect(client.calls).toEqual(["getAgent"]); | ||
| }); | ||
|
|
||
| test("rejects an alias response for a different agent", async () => { | ||
| const client = new TestBedrockAgentControlClient(); | ||
| client.aliasOutput = { | ||
| agentAlias: { ...agentAlias, agentId: "OTHERAGENT" }, | ||
| } as GetAgentAliasCommandOutput; | ||
| const describeAgent = createDescribeBedrockAgent(() => client); | ||
|
|
||
| await expect( | ||
| describeAgent({ | ||
| region: "us-east-1", | ||
| agentId: agent.agentId, | ||
| agentAliasId: agentAlias.agentAliasId, | ||
| }), | ||
| ).rejects.toBeInstanceOf(MalformedServiceResponseError); | ||
| }); | ||
|
|
||
| test("maps agent and alias not-found errors independently", async () => { | ||
| const missingAgentClient = new TestBedrockAgentControlClient(); | ||
| missingAgentClient.agentError = resourceNotFound(); | ||
| const describeMissingAgent = createDescribeBedrockAgent(() => missingAgentClient); | ||
| await expect( | ||
| describeMissingAgent({ | ||
| region: "us-east-1", | ||
| agentId: agent.agentId, | ||
| agentAliasId: agentAlias.agentAliasId, | ||
| }), | ||
| ).rejects.toBeInstanceOf(InputValidationError); | ||
| expect(missingAgentClient.calls).toEqual(["getAgent"]); | ||
|
|
||
| const missingAliasClient = new TestBedrockAgentControlClient(); | ||
| missingAliasClient.aliasError = resourceNotFound(); | ||
| const describeMissingAlias = createDescribeBedrockAgent(() => missingAliasClient); | ||
| await expect( | ||
| describeMissingAlias({ | ||
| region: "us-east-1", | ||
| agentId: agent.agentId, | ||
| agentAliasId: agentAlias.agentAliasId, | ||
| }), | ||
| ).rejects.toBeInstanceOf(InputValidationError); | ||
| expect(missingAliasClient.calls).toEqual(["getAgent", "getAgentAlias"]); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,10 @@ | ||
| import { | ||
| BedrockAgentClient, | ||
| GetAgentAliasCommand, | ||
| GetAgentCommand, | ||
| type GetAgentAliasCommandOutput, | ||
| type GetAgentCommandOutput, | ||
| } from "@aws-sdk/client-bedrock-agent"; | ||
| import { InputValidationError, MalformedServiceResponseError } from "../../errors"; | ||
|
|
||
| /** | ||
|
|
@@ -8,8 +15,12 @@ export const BEDROCK_AGENT_IMPORT_REGIONS = [ | |
| "us-east-1", | ||
| "us-west-2", | ||
| "eu-west-1", | ||
| "eu-west-2", | ||
| "eu-west-3", | ||
| "eu-central-1", | ||
| "eu-central-2", | ||
| "ap-southeast-1", | ||
| "ap-southeast-2", | ||
| "ap-northeast-1", | ||
| "ap-south-1", | ||
| "ca-central-1", | ||
|
|
@@ -40,6 +51,27 @@ export type DescribeBedrockAgent = ( | |
| input: DescribeBedrockAgentInput, | ||
| ) => Promise<BedrockAgentMetadata>; | ||
|
|
||
| export interface BedrockAgentControlClient { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What's the upshot of this interface? |
||
| getAgent(agentId: string): Promise<GetAgentCommandOutput>; | ||
| getAgentAlias(agentId: string, agentAliasId: string): Promise<GetAgentAliasCommandOutput>; | ||
| } | ||
|
|
||
| class AwsBedrockAgentControlClient implements BedrockAgentControlClient { | ||
| private readonly client: BedrockAgentClient; | ||
|
|
||
| constructor(region: string) { | ||
| this.client = new BedrockAgentClient({ region }); | ||
| } | ||
|
|
||
| getAgent(agentId: string): Promise<GetAgentCommandOutput> { | ||
| return this.client.send(new GetAgentCommand({ agentId })); | ||
| } | ||
|
|
||
| getAgentAlias(agentId: string, agentAliasId: string): Promise<GetAgentAliasCommandOutput> { | ||
| return this.client.send(new GetAgentAliasCommand({ agentId, agentAliasId })); | ||
| } | ||
| } | ||
|
|
||
| function isNamedError(error: unknown, name: string): boolean { | ||
| return error instanceof Error && error.name === name; | ||
| } | ||
|
|
@@ -49,55 +81,71 @@ function isNamedError(error: unknown, name: string): boolean { | |
| * both to fail fast on a nonexistent agent/alias and to capture the metadata | ||
| * the scaffolded proxy embeds. | ||
| */ | ||
| export const describeBedrockAgent: DescribeBedrockAgent = async (input) => { | ||
| const { BedrockAgentClient, GetAgentCommand, GetAgentAliasCommand } = | ||
| await import("@aws-sdk/client-bedrock-agent"); | ||
| const client = new BedrockAgentClient({ region: input.region }); | ||
|
|
||
| let agent; | ||
| try { | ||
| ({ agent } = await client.send(new GetAgentCommand({ agentId: input.agentId }))); | ||
| } catch (error) { | ||
| if (isNamedError(error, "ResourceNotFoundException")) { | ||
| throw new InputValidationError( | ||
| `no Bedrock Agent with id '${input.agentId}' exists in ${input.region}; ` + | ||
| `check --agent-id and --region`, | ||
| { cause: error }, | ||
| ); | ||
| export function createDescribeBedrockAgent( | ||
| createClient: (region: string) => BedrockAgentControlClient = (region) => | ||
| new AwsBedrockAgentControlClient(region), | ||
| ): DescribeBedrockAgent { | ||
| return async (input) => { | ||
| const client = createClient(input.region); | ||
|
|
||
| let agent; | ||
| try { | ||
| ({ agent } = await client.getAgent(input.agentId)); | ||
| } catch (error) { | ||
| if (isNamedError(error, "ResourceNotFoundException")) { | ||
| throw new InputValidationError( | ||
| `no Bedrock Agent with id '${input.agentId}' exists in ${input.region}; ` + | ||
| `check --agent-id and --region`, | ||
| { cause: error }, | ||
| ); | ||
| } | ||
| throw error; | ||
| } | ||
| throw error; | ||
| } | ||
|
|
||
| let agentAlias; | ||
| try { | ||
| ({ agentAlias } = await client.send( | ||
| new GetAgentAliasCommand({ agentId: input.agentId, agentAliasId: input.agentAliasId }), | ||
| )); | ||
| } catch (error) { | ||
| if (isNamedError(error, "ResourceNotFoundException")) { | ||
| throw new InputValidationError( | ||
| `Bedrock Agent '${input.agentId}' has no alias with id '${input.agentAliasId}' in ` + | ||
| `${input.region}; check --agent-alias-id`, | ||
| { cause: error }, | ||
| if (!agent?.agentId || agent.agentId !== input.agentId || !agent.agentName) { | ||
| throw new MalformedServiceResponseError( | ||
| `the Bedrock Agent service returned an incomplete description for agent '${input.agentId}'`, | ||
| ); | ||
| } | ||
| throw error; | ||
| } | ||
|
|
||
| if (!agent?.agentName || !agentAlias?.agentAliasArn || !agentAlias.agentAliasName) { | ||
| throw new MalformedServiceResponseError( | ||
| `the Bedrock Agent service returned an incomplete description for agent ` + | ||
| `'${input.agentId}' / alias '${input.agentAliasId}'`, | ||
| ); | ||
| } | ||
| let agentAlias; | ||
| try { | ||
| ({ agentAlias } = await client.getAgentAlias(input.agentId, input.agentAliasId)); | ||
| } catch (error) { | ||
| if (isNamedError(error, "ResourceNotFoundException")) { | ||
| throw new InputValidationError( | ||
| `Bedrock Agent '${input.agentId}' has no alias with id '${input.agentAliasId}' in ` + | ||
| `${input.region}; check --agent-alias-id`, | ||
| { cause: error }, | ||
| ); | ||
| } | ||
| throw error; | ||
| } | ||
|
|
||
| return { | ||
| agentName: agent.agentName, | ||
| agentStatus: agent.agentStatus ?? "UNKNOWN", | ||
| agentAliasArn: agentAlias.agentAliasArn, | ||
| agentAliasName: agentAlias.agentAliasName, | ||
| agentAliasStatus: agentAlias.agentAliasStatus ?? "UNKNOWN", | ||
| foundationModel: agent.foundationModel, | ||
| description: agent.description, | ||
| if ( | ||
| !agentAlias?.agentId || | ||
| agentAlias.agentId !== input.agentId || | ||
| !agentAlias.agentAliasId || | ||
| agentAlias.agentAliasId !== input.agentAliasId || | ||
| !agentAlias.agentAliasArn || | ||
| !agentAlias.agentAliasName | ||
| ) { | ||
| throw new MalformedServiceResponseError( | ||
| `the Bedrock Agent service returned an incomplete description for agent ` + | ||
| `'${input.agentId}' / alias '${input.agentAliasId}'`, | ||
| ); | ||
| } | ||
|
|
||
| return { | ||
| agentName: agent.agentName, | ||
| agentStatus: agent.agentStatus ?? "UNKNOWN", | ||
| agentAliasArn: agentAlias.agentAliasArn, | ||
| agentAliasName: agentAlias.agentAliasName, | ||
| agentAliasStatus: agentAlias.agentAliasStatus ?? "UNKNOWN", | ||
| foundationModel: agent.foundationModel, | ||
| description: agent.description, | ||
| }; | ||
| }; | ||
| }; | ||
| } | ||
|
|
||
| export const describeBedrockAgent = createDescribeBedrockAgent(); | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hah it looks like the AI didn't actually import this feature properly. It just proxies to Bedrock Agents. 😆
We'll have to implement this properly. I think we already have the code. We just need to port it over.