diff --git a/.doc_gen/metadata/bedrock-agent-runtime_metadata.yaml b/.doc_gen/metadata/bedrock-agent-runtime_metadata.yaml index 73eb2fcfcd1..9d2e42ce39b 100644 --- a/.doc_gen/metadata/bedrock-agent-runtime_metadata.yaml +++ b/.doc_gen/metadata/bedrock-agent-runtime_metadata.yaml @@ -13,10 +13,22 @@ bedrock-agent-runtime_InvokeAgent: versions: - sdk_version: 3 github: javascriptv3/example_code/bedrock-agent-runtime - sdkguide: excerpts: - description: snippet_files: - javascriptv3/example_code/bedrock-agent-runtime/actions/invoke-agent.js services: bedrock-agent-runtime: {InvokeAgent} + +bedrock-agent-runtime_InvokeFlow: + languages: + JavaScript: + versions: + - sdk_version: 3 + github: javascriptv3/example_code/bedrock-agent-runtime + excerpts: + - description: + snippet_files: + - javascriptv3/example_code/bedrock-agent-runtime/actions/invoke-flow.js + services: + bedrock-agent-runtime: {InvokeFlow} diff --git a/.tools/verify_python.sh b/.tools/verify_python.sh old mode 100644 new mode 100755 diff --git a/javascriptv3/example_code/bedrock-agent-runtime/README.md b/javascriptv3/example_code/bedrock-agent-runtime/README.md index c93565e57bf..b84f54c0f52 100644 --- a/javascriptv3/example_code/bedrock-agent-runtime/README.md +++ b/javascriptv3/example_code/bedrock-agent-runtime/README.md @@ -34,6 +34,7 @@ For prerequisites, see the [README](../../README.md#Prerequisites) in the `javas Code excerpts that show you how to call individual service functions. - [InvokeAgent](actions/invoke-agent.js) +- [InvokeFlow](actions/invoke-flow.js) diff --git a/javascriptv3/example_code/bedrock-agent-runtime/actions/invoke-flow.js b/javascriptv3/example_code/bedrock-agent-runtime/actions/invoke-flow.js new file mode 100644 index 00000000000..987cbd045dd --- /dev/null +++ b/javascriptv3/example_code/bedrock-agent-runtime/actions/invoke-flow.js @@ -0,0 +1,99 @@ +// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. +// SPDX-License-Identifier: Apache-2.0 + +import { fileURLToPath } from "node:url"; + +import { + BedrockAgentRuntimeClient, + InvokeFlowCommand, +} from "@aws-sdk/client-bedrock-agent-runtime"; + +/** + * Invokes an alias of a flow to run the inputs that you specify and return + * the output of each node as a stream. + * + * @param {{ + * flowIdentifier: string, + * flowAliasIdentifier: string, + * prompt?: string, + * region?: string + * }} options + * @returns {Promise} An object containing information about the output from flow invocation. + */ +export const invokeBedrockFlow = async ({ + flowIdentifier, + flowAliasIdentifier, + prompt = "Hi, how are you?", + region = "us-east-1", +}) => { + const client = new BedrockAgentRuntimeClient({ region }); + + const command = new InvokeFlowCommand({ + flowIdentifier, + flowAliasIdentifier, + inputs: [ + { + content: { + document: prompt, + }, + nodeName: "FlowInputNode", + nodeOutputName: "document", + }, + ], + }); + + let flowResponse = {}; + const response = await client.send(command); + + for await (const chunkEvent of response.responseStream) { + const { flowOutputEvent, flowCompletionEvent } = chunkEvent; + + if (flowOutputEvent) { + flowResponse = { ...flowResponse, ...flowOutputEvent }; + console.log("Flow output event:", flowOutputEvent); + } else if (flowCompletionEvent) { + flowResponse = { ...flowResponse, ...flowCompletionEvent }; + console.log("Flow completion event:", flowCompletionEvent); + } + } + + return flowResponse; +}; + +// Call function if run directly +import { parseArgs } from "node:util"; +import { + isMain, + validateArgs, +} from "@aws-doc-sdk-examples/lib/utils/util-node.js"; + +const loadArgs = () => { + const options = { + flowIdentifier: { + type: "string", + required: true, + }, + flowAliasIdentifier: { + type: "string", + required: true, + }, + prompt: { + type: "string", + }, + region: { + type: "string", + }, + }; + const results = parseArgs({ options }); + const { errors } = validateArgs({ options }, results); + return { errors, results }; +}; + +if (isMain(import.meta.url)) { + const { errors, results } = loadArgs(); + if (!errors) { + invokeBedrockFlow(results.values); + } else { + console.error(errors.join("\n")); + } +} diff --git a/javascriptv3/example_code/bedrock-agent-runtime/package.json b/javascriptv3/example_code/bedrock-agent-runtime/package.json index ca3104513f6..44a3a43bb4a 100644 --- a/javascriptv3/example_code/bedrock-agent-runtime/package.json +++ b/javascriptv3/example_code/bedrock-agent-runtime/package.json @@ -8,10 +8,9 @@ "test": "vitest run **/*.unit.test.js" }, "dependencies": { - "@aws-sdk/client-bedrock-agent-runtime": "^3.501.0" + "@aws-sdk/client-bedrock-agent-runtime": "^3.675.0" }, "devDependencies": { - "vitest": "^1.6.0", - "zod": "^3.22.4" + "vitest": "^1.6.0" } } diff --git a/javascriptv3/example_code/bedrock-agent-runtime/tests/invoke-flow.unit.test.js b/javascriptv3/example_code/bedrock-agent-runtime/tests/invoke-flow.unit.test.js new file mode 100644 index 00000000000..4c83d3c632d --- /dev/null +++ b/javascriptv3/example_code/bedrock-agent-runtime/tests/invoke-flow.unit.test.js @@ -0,0 +1,44 @@ +// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. +// SPDX-License-Identifier: Apache-2.0 + +import { describe, it, expect, vi } from "vitest"; +import { invokeBedrockFlow } from "../actions/invoke-flow.js"; + +const mocks = vi.hoisted(() => ({ + clientSendResolve: () => + Promise.resolve({ + responseStream: [ + { + flowOutputEvent: { + content: { + document: "Test prompt", + }, + }, + }, + ], + }), + clientSendReject: () => Promise.reject(new Error("Mocked error")), + send: vi.fn(), +})); + +vi.mock("@aws-sdk/client-bedrock-agent-runtime", () => { + return { + BedrockAgentRuntimeClient: vi.fn(() => ({ send: mocks.send })), + InvokeFlowCommand: vi.fn(), + }; +}); + +describe("invokeBedrockFlow", () => { + it("should return an object with responseStream", async () => { + const prompt = "Test prompt"; + mocks.send.mockImplementationOnce(mocks.clientSendResolve); + + const result = await invokeBedrockFlow(prompt); + + expect(result).toEqual({ + content: { + document: prompt, + }, + }); + }); +}); diff --git a/javascriptv3/package.json b/javascriptv3/package.json index 68438a46173..97e25baa84b 100644 --- a/javascriptv3/package.json +++ b/javascriptv3/package.json @@ -68,6 +68,7 @@ ], "devDependencies": { "@biomejs/biome": "1.9.3", + "eslint": "^9.13.0", "husky": "^9.1.6", "junit-report-merger": "^7.0.0", "lint-staged": "^14.0.1",