Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 13 additions & 1 deletion .doc_gen/metadata/bedrock-agent-runtime_metadata.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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}
Empty file modified .tools/verify_python.sh
100644 → 100755
Empty file.
1 change: 1 addition & 0 deletions javascriptv3/example_code/bedrock-agent-runtime/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)


<!--custom.examples.start-->
Expand Down
Original file line number Diff line number Diff line change
@@ -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<import("@aws-sdk/client-bedrock-agent").FlowNodeOutput>} 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"));
}
}
5 changes: 2 additions & 3 deletions javascriptv3/example_code/bedrock-agent-runtime/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
}
Original file line number Diff line number Diff line change
@@ -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,
},
});
});
});
1 change: 1 addition & 0 deletions javascriptv3/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
Loading