Skip to content

Commit a7d8299

Browse files
authored
Javascript (v3): Bedrock Runtime Agent - add Invoke Flow (#6991)
1 parent c838b2e commit a7d8299

File tree

7 files changed

+160
-4
lines changed

7 files changed

+160
-4
lines changed

.doc_gen/metadata/bedrock-agent-runtime_metadata.yaml

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,22 @@ bedrock-agent-runtime_InvokeAgent:
1313
versions:
1414
- sdk_version: 3
1515
github: javascriptv3/example_code/bedrock-agent-runtime
16-
sdkguide:
1716
excerpts:
1817
- description:
1918
snippet_files:
2019
- javascriptv3/example_code/bedrock-agent-runtime/actions/invoke-agent.js
2120
services:
2221
bedrock-agent-runtime: {InvokeAgent}
22+
23+
bedrock-agent-runtime_InvokeFlow:
24+
languages:
25+
JavaScript:
26+
versions:
27+
- sdk_version: 3
28+
github: javascriptv3/example_code/bedrock-agent-runtime
29+
excerpts:
30+
- description:
31+
snippet_files:
32+
- javascriptv3/example_code/bedrock-agent-runtime/actions/invoke-flow.js
33+
services:
34+
bedrock-agent-runtime: {InvokeFlow}

.tools/verify_python.sh

100644100755
File mode changed.

javascriptv3/example_code/bedrock-agent-runtime/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ For prerequisites, see the [README](../../README.md#Prerequisites) in the `javas
3434
Code excerpts that show you how to call individual service functions.
3535

3636
- [InvokeAgent](actions/invoke-agent.js)
37+
- [InvokeFlow](actions/invoke-flow.js)
3738

3839

3940
<!--custom.examples.start-->
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
import { fileURLToPath } from "node:url";
5+
6+
import {
7+
BedrockAgentRuntimeClient,
8+
InvokeFlowCommand,
9+
} from "@aws-sdk/client-bedrock-agent-runtime";
10+
11+
/**
12+
* Invokes an alias of a flow to run the inputs that you specify and return
13+
* the output of each node as a stream.
14+
*
15+
* @param {{
16+
* flowIdentifier: string,
17+
* flowAliasIdentifier: string,
18+
* prompt?: string,
19+
* region?: string
20+
* }} options
21+
* @returns {Promise<import("@aws-sdk/client-bedrock-agent").FlowNodeOutput>} An object containing information about the output from flow invocation.
22+
*/
23+
export const invokeBedrockFlow = async ({
24+
flowIdentifier,
25+
flowAliasIdentifier,
26+
prompt = "Hi, how are you?",
27+
region = "us-east-1",
28+
}) => {
29+
const client = new BedrockAgentRuntimeClient({ region });
30+
31+
const command = new InvokeFlowCommand({
32+
flowIdentifier,
33+
flowAliasIdentifier,
34+
inputs: [
35+
{
36+
content: {
37+
document: prompt,
38+
},
39+
nodeName: "FlowInputNode",
40+
nodeOutputName: "document",
41+
},
42+
],
43+
});
44+
45+
let flowResponse = {};
46+
const response = await client.send(command);
47+
48+
for await (const chunkEvent of response.responseStream) {
49+
const { flowOutputEvent, flowCompletionEvent } = chunkEvent;
50+
51+
if (flowOutputEvent) {
52+
flowResponse = { ...flowResponse, ...flowOutputEvent };
53+
console.log("Flow output event:", flowOutputEvent);
54+
} else if (flowCompletionEvent) {
55+
flowResponse = { ...flowResponse, ...flowCompletionEvent };
56+
console.log("Flow completion event:", flowCompletionEvent);
57+
}
58+
}
59+
60+
return flowResponse;
61+
};
62+
63+
// Call function if run directly
64+
import { parseArgs } from "node:util";
65+
import {
66+
isMain,
67+
validateArgs,
68+
} from "@aws-doc-sdk-examples/lib/utils/util-node.js";
69+
70+
const loadArgs = () => {
71+
const options = {
72+
flowIdentifier: {
73+
type: "string",
74+
required: true,
75+
},
76+
flowAliasIdentifier: {
77+
type: "string",
78+
required: true,
79+
},
80+
prompt: {
81+
type: "string",
82+
},
83+
region: {
84+
type: "string",
85+
},
86+
};
87+
const results = parseArgs({ options });
88+
const { errors } = validateArgs({ options }, results);
89+
return { errors, results };
90+
};
91+
92+
if (isMain(import.meta.url)) {
93+
const { errors, results } = loadArgs();
94+
if (!errors) {
95+
invokeBedrockFlow(results.values);
96+
} else {
97+
console.error(errors.join("\n"));
98+
}
99+
}

javascriptv3/example_code/bedrock-agent-runtime/package.json

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,9 @@
88
"test": "vitest run **/*.unit.test.js"
99
},
1010
"dependencies": {
11-
"@aws-sdk/client-bedrock-agent-runtime": "^3.501.0"
11+
"@aws-sdk/client-bedrock-agent-runtime": "^3.675.0"
1212
},
1313
"devDependencies": {
14-
"vitest": "^1.6.0",
15-
"zod": "^3.22.4"
14+
"vitest": "^1.6.0"
1615
}
1716
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
import { describe, it, expect, vi } from "vitest";
5+
import { invokeBedrockFlow } from "../actions/invoke-flow.js";
6+
7+
const mocks = vi.hoisted(() => ({
8+
clientSendResolve: () =>
9+
Promise.resolve({
10+
responseStream: [
11+
{
12+
flowOutputEvent: {
13+
content: {
14+
document: "Test prompt",
15+
},
16+
},
17+
},
18+
],
19+
}),
20+
clientSendReject: () => Promise.reject(new Error("Mocked error")),
21+
send: vi.fn(),
22+
}));
23+
24+
vi.mock("@aws-sdk/client-bedrock-agent-runtime", () => {
25+
return {
26+
BedrockAgentRuntimeClient: vi.fn(() => ({ send: mocks.send })),
27+
InvokeFlowCommand: vi.fn(),
28+
};
29+
});
30+
31+
describe("invokeBedrockFlow", () => {
32+
it("should return an object with responseStream", async () => {
33+
const prompt = "Test prompt";
34+
mocks.send.mockImplementationOnce(mocks.clientSendResolve);
35+
36+
const result = await invokeBedrockFlow(prompt);
37+
38+
expect(result).toEqual({
39+
content: {
40+
document: prompt,
41+
},
42+
});
43+
});
44+
});

javascriptv3/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@
6868
],
6969
"devDependencies": {
7070
"@biomejs/biome": "1.9.3",
71+
"eslint": "^9.13.0",
7172
"husky": "^9.1.6",
7273
"junit-report-merger": "^7.0.0",
7374
"lint-staged": "^14.0.1",

0 commit comments

Comments
 (0)