-
Notifications
You must be signed in to change notification settings - Fork 5.8k
Expand file tree
/
Copy pathlist-agent-action-groups.js
More file actions
128 lines (107 loc) · 4.05 KB
/
list-agent-action-groups.js
File metadata and controls
128 lines (107 loc) · 4.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
import { fileURLToPath } from "node:url";
import { checkForPlaceholders } from "../lib/utils.js";
import {
BedrockAgentClient,
ListAgentActionGroupsCommand,
paginateListAgentActionGroups,
} from "@aws-sdk/client-bedrock-agent";
/**
* Retrieves a list of Action Groups of an agent utilizing the paginator function.
*
* This function leverages a paginator, which abstracts the complexity of pagination, providing
* a straightforward way to handle paginated results inside a `for await...of` loop.
*
* @param {string} agentId - The unique identifier of the agent.
* @param {string} agentVersion - The version of the agent.
* @param {string} [region='us-east-1'] - The AWS region in use.
* @returns {Promise<ActionGroupSummary[]>} An array of action group summaries.
*/
export const listAgentActionGroupsWithPaginator = async (
agentId,
agentVersion,
region = "us-east-1",
) => {
const client = new BedrockAgentClient({ region });
// Create a paginator configuration
const paginatorConfig = {
client,
pageSize: 10, // optional, added for demonstration purposes
};
const params = { agentId, agentVersion };
const pages = paginateListAgentActionGroups(paginatorConfig, params);
// Paginate until there are no more results
const actionGroupSummaries = [];
for await (const page of pages) {
actionGroupSummaries.push(...page.actionGroupSummaries);
}
return actionGroupSummaries;
};
/**
* Retrieves a list of Action Groups of an agent utilizing the ListAgentActionGroupsCommand.
*
* This function demonstrates the manual approach, sending a command to the client and processing the response.
* Pagination must manually be managed. For a simplified approach that abstracts away pagination logic, see
* the `listAgentActionGroupsWithPaginator()` example below.
*
* @param {string} agentId - The unique identifier of the agent.
* @param {string} agentVersion - The version of the agent.
* @param {string} [region='us-east-1'] - The AWS region in use.
* @returns {Promise<ActionGroupSummary[]>} An array of action group summaries.
*/
export const listAgentActionGroupsWithCommandObject = async (
agentId,
agentVersion,
region = "us-east-1",
) => {
const client = new BedrockAgentClient({ region });
let nextToken;
const actionGroupSummaries = [];
do {
const command = new ListAgentActionGroupsCommand({
agentId,
agentVersion,
nextToken,
maxResults: 10, // optional, added for demonstration purposes
});
/** @type {{actionGroupSummaries: ActionGroupSummary[], nextToken?: string}} */
const response = await client.send(command);
for (const actionGroup of response.actionGroupSummaries || []) {
actionGroupSummaries.push(actionGroup);
}
nextToken = response.nextToken;
} while (nextToken);
return actionGroupSummaries;
};
// Invoke main function if this file was run directly.
if (process.argv[1] === fileURLToPath(import.meta.url)) {
// Replace the placeholders for agentId and agentVersion with an existing agent's id and version.
// Ensure to remove the brackets '[]' before adding your data.
// The agentId must be an alphanumeric string with exactly 10 characters.
const agentId = "[ABC123DE45]";
// A string either containing `DRAFT` or a number with 1-5 digits (e.g., '123' or 'DRAFT').
const agentVersion = "[DRAFT]";
// Check for unresolved placeholders in agentId and agentVersion.
checkForPlaceholders([agentId, agentVersion]);
console.log("=".repeat(68));
console.log(
"Listing agent action groups using ListAgentActionGroupsCommand:",
);
for (const actionGroup of await listAgentActionGroupsWithCommandObject(
agentId,
agentVersion,
)) {
console.log(actionGroup);
}
console.log("=".repeat(68));
console.log(
"Listing agent action groups using the paginateListAgents function:",
);
for (const actionGroup of await listAgentActionGroupsWithPaginator(
agentId,
agentVersion,
)) {
console.log(actionGroup);
}
}