@hashgraphonline/conversational-agent 0.2.216 → 0.2.218
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/cli/readme.md +181 -0
- package/dist/cjs/index.cjs +1 -1
- package/dist/cjs/index.cjs.map +1 -1
- package/dist/cjs/index.d.ts +1 -0
- package/dist/cjs/plugins/community/swarm/SwarmPlugin.d.ts +17 -0
- package/dist/cjs/plugins/community/swarm/__tests__/SwarmPlugin.mocks.d.ts +1 -0
- package/dist/cjs/plugins/community/swarm/__tests__/SwarmPlugin.test.d.ts +1 -0
- package/dist/cjs/plugins/community/swarm/__tests__/tools/CreatePostageStampTool.test.d.ts +1 -0
- package/dist/cjs/plugins/community/swarm/__tests__/tools/DownloadDataTool.test.d.ts +1 -0
- package/dist/cjs/plugins/community/swarm/__tests__/tools/DownloadFilesTool.test.d.ts +1 -0
- package/dist/cjs/plugins/community/swarm/__tests__/tools/ExtendPostageStampTool.test.d.ts +1 -0
- package/dist/cjs/plugins/community/swarm/__tests__/tools/GetPostageStampTool.test.d.ts +1 -0
- package/dist/cjs/plugins/community/swarm/__tests__/tools/ListPostageStampsTool.test.d.ts +1 -0
- package/dist/cjs/plugins/community/swarm/__tests__/tools/QueryUploadProgressTool.test.d.ts +1 -0
- package/dist/cjs/plugins/community/swarm/__tests__/tools/ReadFeedTool.test.d.ts +1 -0
- package/dist/cjs/plugins/community/swarm/__tests__/tools/SwarmTool.mocks.d.ts +14 -0
- package/dist/cjs/plugins/community/swarm/__tests__/tools/UpdateFeedTool.test.d.ts +1 -0
- package/dist/cjs/plugins/community/swarm/__tests__/tools/UploadDataTool.test.d.ts +1 -0
- package/dist/cjs/plugins/community/swarm/__tests__/tools/UploadFileTool.test.d.ts +1 -0
- package/dist/cjs/plugins/community/swarm/__tests__/tools/UploadFolderTool.test.d.ts +1 -0
- package/dist/cjs/plugins/community/swarm/config.d.ts +6 -0
- package/dist/cjs/plugins/community/swarm/constants.d.ts +8 -0
- package/dist/cjs/plugins/community/swarm/index.d.ts +2 -0
- package/dist/cjs/plugins/community/swarm/model.d.ts +23 -0
- package/dist/cjs/plugins/community/swarm/tools/CreatePostageStampTool.d.ts +47 -0
- package/dist/cjs/plugins/community/swarm/tools/DownloadDataTool.d.ts +35 -0
- package/dist/cjs/plugins/community/swarm/tools/DownloadFilesTool.d.ts +41 -0
- package/dist/cjs/plugins/community/swarm/tools/ExtendPostageStampTool.d.ts +47 -0
- package/dist/cjs/plugins/community/swarm/tools/GetPostageStampTool.d.ts +35 -0
- package/dist/cjs/plugins/community/swarm/tools/ListPostageStampsTool.d.ts +53 -0
- package/dist/cjs/plugins/community/swarm/tools/QueryUploadProgressTool.d.ts +35 -0
- package/dist/cjs/plugins/community/swarm/tools/ReadFeedTool.d.ts +41 -0
- package/dist/cjs/plugins/community/swarm/tools/UpdateFeedTool.d.ts +47 -0
- package/dist/cjs/plugins/community/swarm/tools/UploadDataTool.d.ts +47 -0
- package/dist/cjs/plugins/community/swarm/tools/UploadFileTool.d.ts +53 -0
- package/dist/cjs/plugins/community/swarm/tools/UploadFolderTool.d.ts +47 -0
- package/dist/cjs/plugins/community/swarm/utils.d.ts +22 -0
- package/dist/cjs/plugins/index.d.ts +1 -0
- package/dist/esm/index.js +33 -31
- package/dist/esm/index.js.map +1 -1
- package/dist/esm/index10.js +13 -677
- package/dist/esm/index10.js.map +1 -1
- package/dist/esm/index11.js +601 -234
- package/dist/esm/index11.js.map +1 -1
- package/dist/esm/index12.js +296 -136
- package/dist/esm/index12.js.map +1 -1
- package/dist/esm/index13.js +127 -235
- package/dist/esm/index13.js.map +1 -1
- package/dist/esm/index14.js +247 -84
- package/dist/esm/index14.js.map +1 -1
- package/dist/esm/index15.js +81 -159
- package/dist/esm/index15.js.map +1 -1
- package/dist/esm/index16.js +155 -229
- package/dist/esm/index16.js.map +1 -1
- package/dist/esm/index17.js +238 -140
- package/dist/esm/index17.js.map +1 -1
- package/dist/esm/index18.js +139 -493
- package/dist/esm/index18.js.map +1 -1
- package/dist/esm/index19.js +479 -91
- package/dist/esm/index19.js.map +1 -1
- package/dist/esm/index20.js +88 -147
- package/dist/esm/index20.js.map +1 -1
- package/dist/esm/index21.js +127 -666
- package/dist/esm/index21.js.map +1 -1
- package/dist/esm/index22.js +698 -44
- package/dist/esm/index22.js.map +1 -1
- package/dist/esm/index23.js +45 -304
- package/dist/esm/index23.js.map +1 -1
- package/dist/esm/index24.js +303 -153
- package/dist/esm/index24.js.map +1 -1
- package/dist/esm/index25.js +150 -117
- package/dist/esm/index25.js.map +1 -1
- package/dist/esm/index26.js +154 -18
- package/dist/esm/index26.js.map +1 -1
- package/dist/esm/index27.js +18 -22
- package/dist/esm/index27.js.map +1 -1
- package/dist/esm/index28.js +15 -74
- package/dist/esm/index28.js.map +1 -1
- package/dist/esm/index29.js +70 -295
- package/dist/esm/index29.js.map +1 -1
- package/dist/esm/index30.js +279 -100
- package/dist/esm/index30.js.map +1 -1
- package/dist/esm/index31.js +86 -922
- package/dist/esm/index31.js.map +1 -1
- package/dist/esm/index32.js +904 -189
- package/dist/esm/index32.js.map +1 -1
- package/dist/esm/index33.js +185 -1169
- package/dist/esm/index33.js.map +1 -1
- package/dist/esm/index34.js +1218 -112
- package/dist/esm/index34.js.map +1 -1
- package/dist/esm/index35.js +111 -99
- package/dist/esm/index35.js.map +1 -1
- package/dist/esm/index36.js +113 -8
- package/dist/esm/index36.js.map +1 -1
- package/dist/esm/index37.js +8 -45
- package/dist/esm/index37.js.map +1 -1
- package/dist/esm/index38.js +41 -102
- package/dist/esm/index38.js.map +1 -1
- package/dist/esm/index39.js +96 -55
- package/dist/esm/index39.js.map +1 -1
- package/dist/esm/index4.js +1 -1
- package/dist/esm/index40.js +58 -71
- package/dist/esm/index40.js.map +1 -1
- package/dist/esm/index41.js +79 -21
- package/dist/esm/index41.js.map +1 -1
- package/dist/esm/index42.js +21 -5
- package/dist/esm/index42.js.map +1 -1
- package/dist/esm/index43.js +26 -11
- package/dist/esm/index43.js.map +1 -1
- package/dist/esm/index44.js +12 -322
- package/dist/esm/index44.js.map +1 -1
- package/dist/esm/index45.js +66 -173
- package/dist/esm/index45.js.map +1 -1
- package/dist/esm/index46.js +69 -24
- package/dist/esm/index46.js.map +1 -1
- package/dist/esm/index47.js +47 -88
- package/dist/esm/index47.js.map +1 -1
- package/dist/esm/index48.js +100 -7
- package/dist/esm/index48.js.map +1 -1
- package/dist/esm/index49.js +79 -0
- package/dist/esm/index49.js.map +1 -0
- package/dist/esm/index5.js +2 -2
- package/dist/esm/index50.js +75 -0
- package/dist/esm/index50.js.map +1 -0
- package/dist/esm/index51.js +124 -0
- package/dist/esm/index51.js.map +1 -0
- package/dist/esm/index52.js +58 -0
- package/dist/esm/index52.js.map +1 -0
- package/dist/esm/index53.js +83 -0
- package/dist/esm/index53.js.map +1 -0
- package/dist/esm/index54.js +100 -0
- package/dist/esm/index54.js.map +1 -0
- package/dist/esm/index55.js +118 -0
- package/dist/esm/index55.js.map +1 -0
- package/dist/esm/index56.js +108 -0
- package/dist/esm/index56.js.map +1 -0
- package/dist/esm/index57.js +8 -0
- package/dist/esm/index57.js.map +1 -0
- package/dist/esm/index58.js +325 -0
- package/dist/esm/index58.js.map +1 -0
- package/dist/esm/index59.js +187 -0
- package/dist/esm/index59.js.map +1 -0
- package/dist/esm/index6.js +132 -833
- package/dist/esm/index6.js.map +1 -1
- package/dist/esm/index60.js +10 -0
- package/dist/esm/index60.js.map +1 -0
- package/dist/esm/index61.js +98 -0
- package/dist/esm/index61.js.map +1 -0
- package/dist/esm/index62.js +131 -0
- package/dist/esm/index62.js.map +1 -0
- package/dist/esm/index63.js +19 -0
- package/dist/esm/index63.js.map +1 -0
- package/dist/esm/index7.js +826 -75
- package/dist/esm/index7.js.map +1 -1
- package/dist/esm/index8.js +91 -13
- package/dist/esm/index8.js.map +1 -1
- package/dist/esm/index9.js +13 -17
- package/dist/esm/index9.js.map +1 -1
- package/dist/types/index.d.ts +1 -0
- package/dist/types/plugins/community/swarm/SwarmPlugin.d.ts +17 -0
- package/dist/types/plugins/community/swarm/__tests__/SwarmPlugin.mocks.d.ts +1 -0
- package/dist/types/plugins/community/swarm/__tests__/SwarmPlugin.test.d.ts +1 -0
- package/dist/types/plugins/community/swarm/__tests__/tools/CreatePostageStampTool.test.d.ts +1 -0
- package/dist/types/plugins/community/swarm/__tests__/tools/DownloadDataTool.test.d.ts +1 -0
- package/dist/types/plugins/community/swarm/__tests__/tools/DownloadFilesTool.test.d.ts +1 -0
- package/dist/types/plugins/community/swarm/__tests__/tools/ExtendPostageStampTool.test.d.ts +1 -0
- package/dist/types/plugins/community/swarm/__tests__/tools/GetPostageStampTool.test.d.ts +1 -0
- package/dist/types/plugins/community/swarm/__tests__/tools/ListPostageStampsTool.test.d.ts +1 -0
- package/dist/types/plugins/community/swarm/__tests__/tools/QueryUploadProgressTool.test.d.ts +1 -0
- package/dist/types/plugins/community/swarm/__tests__/tools/ReadFeedTool.test.d.ts +1 -0
- package/dist/types/plugins/community/swarm/__tests__/tools/SwarmTool.mocks.d.ts +14 -0
- package/dist/types/plugins/community/swarm/__tests__/tools/UpdateFeedTool.test.d.ts +1 -0
- package/dist/types/plugins/community/swarm/__tests__/tools/UploadDataTool.test.d.ts +1 -0
- package/dist/types/plugins/community/swarm/__tests__/tools/UploadFileTool.test.d.ts +1 -0
- package/dist/types/plugins/community/swarm/__tests__/tools/UploadFolderTool.test.d.ts +1 -0
- package/dist/types/plugins/community/swarm/config.d.ts +6 -0
- package/dist/types/plugins/community/swarm/constants.d.ts +8 -0
- package/dist/types/plugins/community/swarm/index.d.ts +2 -0
- package/dist/types/plugins/community/swarm/model.d.ts +23 -0
- package/dist/types/plugins/community/swarm/tools/CreatePostageStampTool.d.ts +47 -0
- package/dist/types/plugins/community/swarm/tools/DownloadDataTool.d.ts +35 -0
- package/dist/types/plugins/community/swarm/tools/DownloadFilesTool.d.ts +41 -0
- package/dist/types/plugins/community/swarm/tools/ExtendPostageStampTool.d.ts +47 -0
- package/dist/types/plugins/community/swarm/tools/GetPostageStampTool.d.ts +35 -0
- package/dist/types/plugins/community/swarm/tools/ListPostageStampsTool.d.ts +53 -0
- package/dist/types/plugins/community/swarm/tools/QueryUploadProgressTool.d.ts +35 -0
- package/dist/types/plugins/community/swarm/tools/ReadFeedTool.d.ts +41 -0
- package/dist/types/plugins/community/swarm/tools/UpdateFeedTool.d.ts +47 -0
- package/dist/types/plugins/community/swarm/tools/UploadDataTool.d.ts +47 -0
- package/dist/types/plugins/community/swarm/tools/UploadFileTool.d.ts +53 -0
- package/dist/types/plugins/community/swarm/tools/UploadFolderTool.d.ts +47 -0
- package/dist/types/plugins/community/swarm/utils.d.ts +22 -0
- package/dist/types/plugins/index.d.ts +1 -0
- package/package.json +37 -27
- package/src/index.ts +1 -0
- package/src/plugins/community/swarm/README.md +279 -0
- package/src/plugins/community/swarm/SwarmPlugin.ts +178 -0
- package/src/plugins/community/swarm/__tests__/SwarmPlugin.mocks.ts +105 -0
- package/src/plugins/community/swarm/__tests__/SwarmPlugin.test.ts +93 -0
- package/src/plugins/community/swarm/__tests__/tools/CreatePostageStampTool.test.ts +152 -0
- package/src/plugins/community/swarm/__tests__/tools/DownloadDataTool.test.ts +93 -0
- package/src/plugins/community/swarm/__tests__/tools/DownloadFilesTool.test.ts +163 -0
- package/src/plugins/community/swarm/__tests__/tools/ExtendPostageStampTool.test.ts +132 -0
- package/src/plugins/community/swarm/__tests__/tools/GetPostageStampTool.test.ts +83 -0
- package/src/plugins/community/swarm/__tests__/tools/ListPostageStampsTool.test.ts +219 -0
- package/src/plugins/community/swarm/__tests__/tools/QueryUploadProgressTool.test.ts +169 -0
- package/src/plugins/community/swarm/__tests__/tools/ReadFeedTool.test.ts +133 -0
- package/src/plugins/community/swarm/__tests__/tools/SwarmTool.mocks.ts +67 -0
- package/src/plugins/community/swarm/__tests__/tools/UpdateFeedTool.test.ts +148 -0
- package/src/plugins/community/swarm/__tests__/tools/UploadDataTool.test.ts +125 -0
- package/src/plugins/community/swarm/__tests__/tools/UploadFileTool.test.ts +194 -0
- package/src/plugins/community/swarm/__tests__/tools/UploadFolderTool.test.ts +118 -0
- package/src/plugins/community/swarm/config.ts +6 -0
- package/src/plugins/community/swarm/constants.ts +12 -0
- package/src/plugins/community/swarm/index.ts +2 -0
- package/src/plugins/community/swarm/model.ts +23 -0
- package/src/plugins/community/swarm/tools/CreatePostageStampTool.ts +137 -0
- package/src/plugins/community/swarm/tools/DownloadDataTool.ts +79 -0
- package/src/plugins/community/swarm/tools/DownloadFilesTool.ts +155 -0
- package/src/plugins/community/swarm/tools/ExtendPostageStampTool.ts +112 -0
- package/src/plugins/community/swarm/tools/GetPostageStampTool.ts +92 -0
- package/src/plugins/community/swarm/tools/ListPostageStampsTool.ts +124 -0
- package/src/plugins/community/swarm/tools/QueryUploadProgressTool.ts +109 -0
- package/src/plugins/community/swarm/tools/ReadFeedTool.ts +110 -0
- package/src/plugins/community/swarm/tools/UpdateFeedTool.ts +149 -0
- package/src/plugins/community/swarm/tools/UploadDataTool.ts +109 -0
- package/src/plugins/community/swarm/tools/UploadFileTool.ts +163 -0
- package/src/plugins/community/swarm/tools/UploadFolderTool.ts +150 -0
- package/src/plugins/community/swarm/utils.ts +172 -0
- package/src/plugins/index.ts +1 -0
- package/cli/dist/CLIApp.d.ts +0 -11
- package/cli/dist/CLIApp.d.ts.map +0 -1
- package/cli/dist/CLIApp.js +0 -128
- package/cli/dist/CLIApp.js.map +0 -1
- package/cli/dist/LocalConversationalAgent.d.ts +0 -37
- package/cli/dist/LocalConversationalAgent.js +0 -58
- package/cli/dist/app.d.ts +0 -18
- package/cli/dist/app.d.ts.map +0 -1
- package/cli/dist/app.js +0 -14
- package/cli/dist/app.js.map +0 -1
- package/cli/dist/cli.d.ts +0 -3
- package/cli/dist/cli.d.ts.map +0 -1
- package/cli/dist/cli.js +0 -87
- package/cli/dist/cli.js.map +0 -1
- package/cli/dist/components/AppContainer.d.ts +0 -16
- package/cli/dist/components/AppContainer.js +0 -24
- package/cli/dist/components/AppScreens.d.ts +0 -2
- package/cli/dist/components/AppScreens.js +0 -259
- package/cli/dist/components/ChatScreen.d.ts +0 -21
- package/cli/dist/components/ChatScreen.d.ts.map +0 -1
- package/cli/dist/components/ChatScreen.js +0 -40
- package/cli/dist/components/ChatScreen.js.map +0 -1
- package/cli/dist/components/DebugLoadingScreen.d.ts +0 -5
- package/cli/dist/components/DebugLoadingScreen.js +0 -31
- package/cli/dist/components/LoadingScreen.d.ts +0 -3
- package/cli/dist/components/LoadingScreen.d.ts.map +0 -1
- package/cli/dist/components/LoadingScreen.js +0 -17
- package/cli/dist/components/LoadingScreen.js.map +0 -1
- package/cli/dist/components/LoadingScreenDebug.d.ts +0 -5
- package/cli/dist/components/LoadingScreenDebug.js +0 -27
- package/cli/dist/components/MCPConfigScreen.d.ts +0 -28
- package/cli/dist/components/MCPConfigScreen.d.ts.map +0 -1
- package/cli/dist/components/MCPConfigScreen.js +0 -186
- package/cli/dist/components/MCPConfigScreen.js.map +0 -1
- package/cli/dist/components/ScreenRouter.d.ts +0 -13
- package/cli/dist/components/ScreenRouter.d.ts.map +0 -1
- package/cli/dist/components/ScreenRouter.js +0 -23
- package/cli/dist/components/ScreenRouter.js.map +0 -1
- package/cli/dist/components/SetupScreen.d.ts +0 -16
- package/cli/dist/components/SetupScreen.d.ts.map +0 -1
- package/cli/dist/components/SetupScreen.js +0 -67
- package/cli/dist/components/SetupScreen.js.map +0 -1
- package/cli/dist/components/SingleLoadingScreen.d.ts +0 -5
- package/cli/dist/components/SingleLoadingScreen.js +0 -27
- package/cli/dist/components/StatusBadge.d.ts +0 -10
- package/cli/dist/components/StatusBadge.d.ts.map +0 -1
- package/cli/dist/components/StatusBadge.js +0 -24
- package/cli/dist/components/StatusBadge.js.map +0 -1
- package/cli/dist/components/TerminalWindow.d.ts +0 -9
- package/cli/dist/components/TerminalWindow.d.ts.map +0 -1
- package/cli/dist/components/TerminalWindow.js +0 -19
- package/cli/dist/components/TerminalWindow.js.map +0 -1
- package/cli/dist/components/WelcomeScreen.d.ts +0 -12
- package/cli/dist/components/WelcomeScreen.d.ts.map +0 -1
- package/cli/dist/components/WelcomeScreen.js +0 -47
- package/cli/dist/components/WelcomeScreen.js.map +0 -1
- package/cli/dist/context/AppContext.d.ts +0 -68
- package/cli/dist/context/AppContext.js +0 -363
- package/cli/dist/headless-runner.d.ts +0 -17
- package/cli/dist/headless-runner.d.ts.map +0 -1
- package/cli/dist/headless-runner.js +0 -128
- package/cli/dist/headless-runner.js.map +0 -1
- package/cli/dist/hooks/useInitializeAgent.d.ts +0 -19
- package/cli/dist/hooks/useInitializeAgent.d.ts.map +0 -1
- package/cli/dist/hooks/useInitializeAgent.js +0 -29
- package/cli/dist/hooks/useInitializeAgent.js.map +0 -1
- package/cli/dist/hooks/useStableState.d.ts +0 -38
- package/cli/dist/hooks/useStableState.d.ts.map +0 -1
- package/cli/dist/hooks/useStableState.js +0 -69
- package/cli/dist/hooks/useStableState.js.map +0 -1
- package/cli/dist/managers/AgentManager.d.ts +0 -58
- package/cli/dist/managers/AgentManager.d.ts.map +0 -1
- package/cli/dist/managers/AgentManager.js +0 -121
- package/cli/dist/managers/AgentManager.js.map +0 -1
- package/cli/dist/managers/ConfigManager.d.ts +0 -54
- package/cli/dist/managers/ConfigManager.d.ts.map +0 -1
- package/cli/dist/managers/ConfigManager.js +0 -188
- package/cli/dist/managers/ConfigManager.js.map +0 -1
- package/cli/dist/types.d.ts +0 -52
- package/cli/dist/types.d.ts.map +0 -1
- package/cli/dist/types.js +0 -34
- package/cli/dist/types.js.map +0 -1
|
@@ -0,0 +1,137 @@
|
|
|
1
|
+
import {
|
|
2
|
+
BaseHederaQueryTool,
|
|
3
|
+
HederaAgentKit,
|
|
4
|
+
type GenericPluginContext,
|
|
5
|
+
} from "hedera-agent-kit";
|
|
6
|
+
import { BatchId, Bee, Duration, Size } from "@ethersphere/bee-js";
|
|
7
|
+
import { z } from "zod";
|
|
8
|
+
import {
|
|
9
|
+
errorHasStatus,
|
|
10
|
+
getErrorMessage,
|
|
11
|
+
makeDate,
|
|
12
|
+
runWithTimeout,
|
|
13
|
+
ToolResponse,
|
|
14
|
+
} from "../utils";
|
|
15
|
+
import { BAD_REQUEST_STATUS, CALL_TIMEOUT, GATEWAY_STAMP_ERROR_MESSAGE, NOT_FOUND_STATUS, POSTAGE_CREATE_TIMEOUT_MESSAGE } from "../constants";
|
|
16
|
+
import { SwarmConfig } from "../config";
|
|
17
|
+
|
|
18
|
+
const CreatePostageStampSchema = z.object({
|
|
19
|
+
size: z.number(),
|
|
20
|
+
duration: z.string(),
|
|
21
|
+
label: z.string().optional(),
|
|
22
|
+
});
|
|
23
|
+
|
|
24
|
+
export class CreatePostageStampTool extends BaseHederaQueryTool<typeof CreatePostageStampSchema> {
|
|
25
|
+
name = "swarm-create-postage-stamp";
|
|
26
|
+
description = `
|
|
27
|
+
Buy postage stamp based on size in megabytes and duration.
|
|
28
|
+
size: The storage size in MB (Megabytes). These other size units convert like this to MB: 1 byte = 0.000001 MB, 1 KB = 0.001 MB, 1GB= 1000MB.
|
|
29
|
+
duration: Duration for which the data should be stored. Time to live of the postage stamp, e.g. 1d - 1 day, 1w - 1 week, 1month - 1 month.
|
|
30
|
+
label: Sets label for the postage batch (omit if the user didn't ask for one). Do not set a label with with specific capacity values because they can get misleading.
|
|
31
|
+
`;
|
|
32
|
+
namespace = "swarm";
|
|
33
|
+
specificInputSchema = CreatePostageStampSchema;
|
|
34
|
+
bee: Bee;
|
|
35
|
+
config: SwarmConfig;
|
|
36
|
+
|
|
37
|
+
constructor(params: {
|
|
38
|
+
hederaKit: HederaAgentKit;
|
|
39
|
+
config: SwarmConfig;
|
|
40
|
+
logger?: GenericPluginContext['logger'];
|
|
41
|
+
bee: Bee;
|
|
42
|
+
}) {
|
|
43
|
+
const { bee, config, ...rest } = params;
|
|
44
|
+
super(rest);
|
|
45
|
+
this.bee = bee;
|
|
46
|
+
this.config = config;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
protected async executeQuery(
|
|
50
|
+
input: z.infer<typeof CreatePostageStampSchema>
|
|
51
|
+
): Promise<ToolResponse | string> {
|
|
52
|
+
const { size, duration, label } = input;
|
|
53
|
+
|
|
54
|
+
if (!size) {
|
|
55
|
+
this.logger.error(
|
|
56
|
+
'Missing required parameter: size.'
|
|
57
|
+
);
|
|
58
|
+
|
|
59
|
+
throw new Error('Missing required parameter: size.');
|
|
60
|
+
} else if (!duration) {
|
|
61
|
+
this.logger.error(
|
|
62
|
+
'Missing required parameter: duration.'
|
|
63
|
+
);
|
|
64
|
+
|
|
65
|
+
throw new Error('Missing required parameter: duration.');
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
let durationMs;
|
|
69
|
+
|
|
70
|
+
try {
|
|
71
|
+
durationMs = makeDate(duration);
|
|
72
|
+
} catch (makeDateError) {
|
|
73
|
+
this.logger.error(
|
|
74
|
+
'Invalid parameter: duration.'
|
|
75
|
+
);
|
|
76
|
+
|
|
77
|
+
throw new Error('Invalid parameter: duration.');
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
let buyStorageResponse: BatchId;
|
|
81
|
+
|
|
82
|
+
try {
|
|
83
|
+
let options = {};
|
|
84
|
+
if (label !== undefined) {
|
|
85
|
+
options = {
|
|
86
|
+
label
|
|
87
|
+
};
|
|
88
|
+
}
|
|
89
|
+
const buyStoragePromise = this.bee.buyStorage(
|
|
90
|
+
Size.fromMegabytes(size),
|
|
91
|
+
Duration.fromMilliseconds(durationMs),
|
|
92
|
+
options
|
|
93
|
+
);
|
|
94
|
+
const [response, hasTimedOut] = await runWithTimeout(
|
|
95
|
+
buyStoragePromise,
|
|
96
|
+
CALL_TIMEOUT
|
|
97
|
+
);
|
|
98
|
+
|
|
99
|
+
if (hasTimedOut) {
|
|
100
|
+
return JSON.stringify({
|
|
101
|
+
content: [
|
|
102
|
+
{
|
|
103
|
+
type: 'text',
|
|
104
|
+
text: POSTAGE_CREATE_TIMEOUT_MESSAGE,
|
|
105
|
+
},
|
|
106
|
+
],
|
|
107
|
+
});
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
buyStorageResponse = response as BatchId;
|
|
111
|
+
} catch (error) {
|
|
112
|
+
let errorMessage = 'Unable to buy storage.';
|
|
113
|
+
|
|
114
|
+
if (errorHasStatus(error, NOT_FOUND_STATUS)) {
|
|
115
|
+
errorMessage = GATEWAY_STAMP_ERROR_MESSAGE;
|
|
116
|
+
} else if (errorHasStatus(error, BAD_REQUEST_STATUS)) {
|
|
117
|
+
errorMessage = getErrorMessage(error);
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
this.logger.error(
|
|
121
|
+
errorMessage,
|
|
122
|
+
error
|
|
123
|
+
);
|
|
124
|
+
|
|
125
|
+
throw new Error(errorMessage);
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
return {
|
|
129
|
+
content: [
|
|
130
|
+
{
|
|
131
|
+
type: 'text',
|
|
132
|
+
text: `Postage batch ID: ${buyStorageResponse.toHex()}`,
|
|
133
|
+
},
|
|
134
|
+
],
|
|
135
|
+
};
|
|
136
|
+
}
|
|
137
|
+
}
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
import { Bee } from "@ethersphere/bee-js";
|
|
2
|
+
import { z } from "zod";
|
|
3
|
+
import { errorHasStatus, getErrorMessage, getResponseWithStructuredContent, ToolResponse } from "../utils";
|
|
4
|
+
import { BaseHederaQueryTool, GenericPluginContext, HederaAgentKit } from "hedera-agent-kit";
|
|
5
|
+
import { SwarmConfig } from "../config";
|
|
6
|
+
import { BAD_REQUEST_STATUS } from "../constants";
|
|
7
|
+
|
|
8
|
+
const DownloadDataSchema = z.object({
|
|
9
|
+
reference: z.string(),
|
|
10
|
+
});
|
|
11
|
+
|
|
12
|
+
export class DownloadDataTool extends BaseHederaQueryTool<typeof DownloadDataSchema> {
|
|
13
|
+
name = "swarm-download-data";
|
|
14
|
+
description = `
|
|
15
|
+
Downloads immutable data from a Swarm content address hash.
|
|
16
|
+
reference: Swarm reference hash.
|
|
17
|
+
`;
|
|
18
|
+
namespace = "swarm";
|
|
19
|
+
specificInputSchema = DownloadDataSchema;
|
|
20
|
+
bee: Bee;
|
|
21
|
+
config: SwarmConfig;
|
|
22
|
+
|
|
23
|
+
constructor(params: {
|
|
24
|
+
hederaKit: HederaAgentKit;
|
|
25
|
+
config: SwarmConfig;
|
|
26
|
+
logger?: GenericPluginContext['logger'];
|
|
27
|
+
bee: Bee;
|
|
28
|
+
}) {
|
|
29
|
+
const { bee, config, ...rest } = params;
|
|
30
|
+
super(rest);
|
|
31
|
+
this.bee = bee;
|
|
32
|
+
this.config = config;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
protected async executeQuery(
|
|
36
|
+
input: z.infer<typeof DownloadDataSchema>
|
|
37
|
+
): Promise<ToolResponse | string> {
|
|
38
|
+
const { reference } = input;
|
|
39
|
+
|
|
40
|
+
if (!reference) {
|
|
41
|
+
this.logger.error(
|
|
42
|
+
'Missing required parameter: reference.'
|
|
43
|
+
);
|
|
44
|
+
|
|
45
|
+
throw new Error("Missing required parameter: reference.");
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
const isRefNotSwarmHash =
|
|
49
|
+
reference.length !== 64 && reference.length !== 66;
|
|
50
|
+
|
|
51
|
+
if (isRefNotSwarmHash) {
|
|
52
|
+
this.logger.error(
|
|
53
|
+
'Invalid Swarm content address hash value for reference.'
|
|
54
|
+
);
|
|
55
|
+
|
|
56
|
+
throw new Error("Invalid Swarm content address hash value for reference.");
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
let data;
|
|
60
|
+
try {
|
|
61
|
+
data = await this.bee.downloadData(reference);
|
|
62
|
+
} catch (error) {
|
|
63
|
+
let errorMessage = 'Downloading data failed.';
|
|
64
|
+
|
|
65
|
+
if (errorHasStatus(error, BAD_REQUEST_STATUS)) {
|
|
66
|
+
errorMessage = getErrorMessage(error);
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
this.logger.error(errorMessage, error);
|
|
70
|
+
throw new Error(errorMessage);
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
const textData = data.toUtf8();
|
|
74
|
+
|
|
75
|
+
return getResponseWithStructuredContent({
|
|
76
|
+
textData,
|
|
77
|
+
});
|
|
78
|
+
}
|
|
79
|
+
}
|
|
@@ -0,0 +1,155 @@
|
|
|
1
|
+
import { Bee, MantarayNode } from "@ethersphere/bee-js";
|
|
2
|
+
import { z } from "zod";
|
|
3
|
+
import fs from "fs";
|
|
4
|
+
import path from "path";
|
|
5
|
+
import { BaseHederaQueryTool, GenericPluginContext, HederaAgentKit } from "hedera-agent-kit";
|
|
6
|
+
import { promisify } from "util";
|
|
7
|
+
import { ToolResponse } from "../utils";
|
|
8
|
+
import { SwarmConfig } from "../config";
|
|
9
|
+
|
|
10
|
+
const DownloadFilesSchema = z.object({
|
|
11
|
+
reference: z.string(),
|
|
12
|
+
filePath: z.string().optional()
|
|
13
|
+
});
|
|
14
|
+
|
|
15
|
+
export class DownloadFilesTool extends BaseHederaQueryTool<typeof DownloadFilesSchema> {
|
|
16
|
+
name = "swarm-download-files";
|
|
17
|
+
description = `Download folder, files from a Swarm reference and save to file path or return file list of the reference.
|
|
18
|
+
Prioritizes this tool over swarm-download-data if there is no assumption about the data type.
|
|
19
|
+
reference: Swarm reference hash.
|
|
20
|
+
filePath: Optional file path to save the downloaded content (only available in stdio mode). If not provided list of files in the manifest will be returned.
|
|
21
|
+
`;
|
|
22
|
+
namespace = "swarm";
|
|
23
|
+
specificInputSchema = DownloadFilesSchema;
|
|
24
|
+
bee: Bee;
|
|
25
|
+
config: SwarmConfig;
|
|
26
|
+
|
|
27
|
+
constructor(params: {
|
|
28
|
+
hederaKit: HederaAgentKit;
|
|
29
|
+
config: SwarmConfig;
|
|
30
|
+
logger?: GenericPluginContext['logger'];
|
|
31
|
+
bee: Bee;
|
|
32
|
+
}) {
|
|
33
|
+
const { bee, config, ...rest } = params;
|
|
34
|
+
super(rest);
|
|
35
|
+
this.bee = bee;
|
|
36
|
+
this.config = config;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
protected async executeQuery(
|
|
40
|
+
input: z.infer<typeof DownloadFilesSchema>
|
|
41
|
+
): Promise<ToolResponse | string> {
|
|
42
|
+
const { reference, filePath } = input;
|
|
43
|
+
|
|
44
|
+
if (!reference) {
|
|
45
|
+
this.logger.error(
|
|
46
|
+
'Missing required parameter: reference.'
|
|
47
|
+
);
|
|
48
|
+
|
|
49
|
+
throw new Error("Missing required parameter: reference.");
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
this.logger.info(`[API] Downloading folder from Swarm with reference: ${reference}.`);
|
|
53
|
+
|
|
54
|
+
// Check if the reference is a manifest
|
|
55
|
+
let isManifest = false;
|
|
56
|
+
let node: MantarayNode;
|
|
57
|
+
|
|
58
|
+
try {
|
|
59
|
+
node = await MantarayNode.unmarshal(this.bee, reference);
|
|
60
|
+
await node.loadRecursively(this.bee);
|
|
61
|
+
isManifest = true;
|
|
62
|
+
} catch (error) {
|
|
63
|
+
// ignore
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
if (isManifest) {
|
|
67
|
+
if (filePath) {
|
|
68
|
+
const destinationFolder = filePath;
|
|
69
|
+
|
|
70
|
+
if (!fs.existsSync(destinationFolder)) {
|
|
71
|
+
await promisify(fs.mkdir)(destinationFolder, { recursive: true });
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
const nodes = node!.collect();
|
|
75
|
+
|
|
76
|
+
if (nodes.length === 1) {
|
|
77
|
+
const node = nodes[0];
|
|
78
|
+
const data = await this.bee.downloadData(node.targetAddress);
|
|
79
|
+
await promisify(fs.writeFile)(
|
|
80
|
+
path.join(
|
|
81
|
+
destinationFolder,
|
|
82
|
+
node.fullPathString.split("\\").slice(-1)[0]
|
|
83
|
+
),
|
|
84
|
+
data.toUint8Array()
|
|
85
|
+
);
|
|
86
|
+
} else {
|
|
87
|
+
// Download each node
|
|
88
|
+
for (const node of nodes) {
|
|
89
|
+
const parsedPath = path.parse(node.fullPathString);
|
|
90
|
+
const nodeDestFolder = path.join(destinationFolder, parsedPath.dir);
|
|
91
|
+
// Create subdirectories if necessary
|
|
92
|
+
if (!fs.existsSync(nodeDestFolder)) {
|
|
93
|
+
await promisify(fs.mkdir)(nodeDestFolder, { recursive: true });
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
const data = await this.bee.downloadData(node.targetAddress);
|
|
97
|
+
await promisify(fs.writeFile)(
|
|
98
|
+
path.join(destinationFolder, node.fullPathString),
|
|
99
|
+
data.toUint8Array()
|
|
100
|
+
);
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
return {
|
|
105
|
+
content: [
|
|
106
|
+
{
|
|
107
|
+
type: "text",
|
|
108
|
+
text: JSON.stringify(
|
|
109
|
+
{
|
|
110
|
+
reference: reference,
|
|
111
|
+
manifestNodeCount: nodes.length,
|
|
112
|
+
savedTo: destinationFolder,
|
|
113
|
+
message: `Manifest content (${nodes.length} files) successfully downloaded to ${destinationFolder}`,
|
|
114
|
+
},
|
|
115
|
+
null,
|
|
116
|
+
2
|
|
117
|
+
),
|
|
118
|
+
},
|
|
119
|
+
],
|
|
120
|
+
};
|
|
121
|
+
} else {
|
|
122
|
+
// regular file
|
|
123
|
+
const nodes = node!.collect();
|
|
124
|
+
const filesList = nodes.map((node) => ({
|
|
125
|
+
path: node.fullPathString || "/",
|
|
126
|
+
targetAddress: Array.from(node.targetAddress)
|
|
127
|
+
.map((e) => e.toString(16).padStart(2, "0"))
|
|
128
|
+
.join(""),
|
|
129
|
+
metadata: node.metadata,
|
|
130
|
+
}));
|
|
131
|
+
|
|
132
|
+
return {
|
|
133
|
+
content: [
|
|
134
|
+
{
|
|
135
|
+
type: "text",
|
|
136
|
+
text: JSON.stringify(
|
|
137
|
+
{
|
|
138
|
+
reference: reference,
|
|
139
|
+
type: "manifest",
|
|
140
|
+
files: filesList,
|
|
141
|
+
message:
|
|
142
|
+
"This is a manifest with multiple files. Provide a filePath to download all files or download individual files using their specific references.",
|
|
143
|
+
},
|
|
144
|
+
null,
|
|
145
|
+
2
|
|
146
|
+
),
|
|
147
|
+
},
|
|
148
|
+
],
|
|
149
|
+
};
|
|
150
|
+
}
|
|
151
|
+
} else {
|
|
152
|
+
return "Try swarm-download-data tool instead since the given reference is not a manifest.";
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
}
|
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
import {
|
|
2
|
+
BaseHederaQueryTool,
|
|
3
|
+
HederaAgentKit,
|
|
4
|
+
type GenericPluginContext,
|
|
5
|
+
} from "hedera-agent-kit";
|
|
6
|
+
import { BatchId, Bee, Duration, Size } from "@ethersphere/bee-js";
|
|
7
|
+
import { z } from "zod";
|
|
8
|
+
import {
|
|
9
|
+
errorHasStatus,
|
|
10
|
+
getErrorMessage,
|
|
11
|
+
getResponseWithStructuredContent,
|
|
12
|
+
makeDate,
|
|
13
|
+
ToolResponse,
|
|
14
|
+
} from "../utils";
|
|
15
|
+
import { BAD_REQUEST_STATUS, GATEWAY_STAMP_ERROR_MESSAGE, NOT_FOUND_STATUS } from "../constants";
|
|
16
|
+
import { SwarmConfig } from "../config";
|
|
17
|
+
|
|
18
|
+
const ExtendPostageStampSchema = z.object({
|
|
19
|
+
postageBatchId: z.string(),
|
|
20
|
+
duration: z.string().optional(),
|
|
21
|
+
size: z.number().optional(),
|
|
22
|
+
});
|
|
23
|
+
|
|
24
|
+
export class ExtendPostageStampTool extends BaseHederaQueryTool<typeof ExtendPostageStampSchema> {
|
|
25
|
+
name = "swarm-extend-postage-stamp";
|
|
26
|
+
description = `Increase the duration (relative to current duration) or size (in megabytes) of a postage stamp.
|
|
27
|
+
postageBatchId: The id of the batch for which extend is performed.
|
|
28
|
+
size: The storage size in MB (Megabytes). These other size units convert like this to MB: 1 byte = 0.000001 MB, 1 KB = 0.001 MB, 1GB= 1000MB.
|
|
29
|
+
duration: Duration for which the data should be stored. Time to live of the postage stamp, e.g. 1d - 1 day, 1w - 1 week, 1month - 1 month.
|
|
30
|
+
`;
|
|
31
|
+
namespace = "swarm";
|
|
32
|
+
specificInputSchema = ExtendPostageStampSchema;
|
|
33
|
+
bee: Bee;
|
|
34
|
+
config: SwarmConfig;
|
|
35
|
+
|
|
36
|
+
constructor(params: {
|
|
37
|
+
hederaKit: HederaAgentKit;
|
|
38
|
+
config: SwarmConfig;
|
|
39
|
+
logger?: GenericPluginContext['logger'];
|
|
40
|
+
bee: Bee;
|
|
41
|
+
}) {
|
|
42
|
+
const { bee, config, ...rest } = params;
|
|
43
|
+
super(rest);
|
|
44
|
+
this.bee = bee;
|
|
45
|
+
this.config = config;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
protected async executeQuery(
|
|
49
|
+
input: z.infer<typeof ExtendPostageStampSchema>
|
|
50
|
+
): Promise<ToolResponse | string> {
|
|
51
|
+
const { postageBatchId, duration, size } = input;
|
|
52
|
+
|
|
53
|
+
if (!postageBatchId) {
|
|
54
|
+
this.logger.error(
|
|
55
|
+
'Missing required parameter: postageBatchId.'
|
|
56
|
+
);
|
|
57
|
+
|
|
58
|
+
throw new Error('Missing required parameter: postageBatchId.');
|
|
59
|
+
} else if (!duration && !size) {
|
|
60
|
+
this.logger.error(
|
|
61
|
+
'You need at least one parameter from duration and size.'
|
|
62
|
+
);
|
|
63
|
+
|
|
64
|
+
throw new Error('You need at least one parameter from duration and size.');
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
// If size is missing, Size.fromBytes(1) will get smallest depth and not increase size.
|
|
68
|
+
const extendSize = !!size ? Size.fromMegabytes(size) : Size.fromBytes(1);
|
|
69
|
+
let extendDuration = Duration.ZERO;
|
|
70
|
+
|
|
71
|
+
try {
|
|
72
|
+
if (duration) {
|
|
73
|
+
extendDuration = Duration.fromMilliseconds(makeDate(duration));
|
|
74
|
+
}
|
|
75
|
+
} catch (makeDateError) {
|
|
76
|
+
this.logger.error(
|
|
77
|
+
'Invalid parameter: duration.'
|
|
78
|
+
);
|
|
79
|
+
|
|
80
|
+
throw new Error('Invalid parameter: duration.');
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
let extendStorageResponse;
|
|
84
|
+
|
|
85
|
+
try {
|
|
86
|
+
extendStorageResponse = await this.bee.extendStorage(
|
|
87
|
+
postageBatchId,
|
|
88
|
+
extendSize,
|
|
89
|
+
extendDuration
|
|
90
|
+
);
|
|
91
|
+
} catch (error) {
|
|
92
|
+
let errorMessage = 'Extend failed.';
|
|
93
|
+
|
|
94
|
+
if (errorHasStatus(error, NOT_FOUND_STATUS)) {
|
|
95
|
+
errorMessage = GATEWAY_STAMP_ERROR_MESSAGE;
|
|
96
|
+
} else if (errorHasStatus(error, BAD_REQUEST_STATUS)) {
|
|
97
|
+
errorMessage = getErrorMessage(error);
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
this.logger.error(
|
|
101
|
+
errorMessage,
|
|
102
|
+
error
|
|
103
|
+
);
|
|
104
|
+
|
|
105
|
+
throw new Error(errorMessage);
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
return getResponseWithStructuredContent({
|
|
109
|
+
postageBatchId: extendStorageResponse.toHex(),
|
|
110
|
+
});
|
|
111
|
+
}
|
|
112
|
+
}
|
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
import {
|
|
2
|
+
type GenericPluginContext,
|
|
3
|
+
BaseHederaQueryTool,
|
|
4
|
+
type HederaAgentKit,
|
|
5
|
+
} from "hedera-agent-kit";
|
|
6
|
+
import { Bee } from "@ethersphere/bee-js";
|
|
7
|
+
import { z } from "zod";
|
|
8
|
+
import {
|
|
9
|
+
PostageBatchCurated,
|
|
10
|
+
PostageBatchSummary,
|
|
11
|
+
ResponseContent,
|
|
12
|
+
} from "../model";
|
|
13
|
+
import {
|
|
14
|
+
errorHasStatus,
|
|
15
|
+
getBatchSummary,
|
|
16
|
+
getResponseWithStructuredContent,
|
|
17
|
+
ToolResponse,
|
|
18
|
+
} from "../utils";
|
|
19
|
+
import { GATEWAY_STAMP_ERROR_MESSAGE, NOT_FOUND_STATUS } from "../constants";
|
|
20
|
+
import { SwarmConfig } from "../config";
|
|
21
|
+
|
|
22
|
+
const GetPostageStampSchema = z.object({
|
|
23
|
+
postageBatchId: z.string(),
|
|
24
|
+
});
|
|
25
|
+
|
|
26
|
+
export class GetPostageStampTool extends BaseHederaQueryTool<typeof GetPostageStampSchema> {
|
|
27
|
+
name = "swarm-get-postage-stamp";
|
|
28
|
+
description = `Get a specific postage stamp based on postageBatchId.
|
|
29
|
+
postageBatchId: The id of the stamp which is requested.
|
|
30
|
+
`;
|
|
31
|
+
namespace = "swarm";
|
|
32
|
+
specificInputSchema = GetPostageStampSchema;
|
|
33
|
+
bee: Bee;
|
|
34
|
+
config: SwarmConfig;
|
|
35
|
+
|
|
36
|
+
constructor(params: {
|
|
37
|
+
hederaKit: HederaAgentKit;
|
|
38
|
+
config: SwarmConfig;
|
|
39
|
+
logger?: GenericPluginContext['logger'];
|
|
40
|
+
bee: Bee;
|
|
41
|
+
}) {
|
|
42
|
+
const { bee, config, ...rest } = params;
|
|
43
|
+
super(rest);
|
|
44
|
+
this.bee = bee;
|
|
45
|
+
this.config = config;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
protected async executeQuery(
|
|
49
|
+
input: z.infer<typeof GetPostageStampSchema>
|
|
50
|
+
): Promise<ToolResponse | string> {
|
|
51
|
+
const { postageBatchId } = input;
|
|
52
|
+
if (!postageBatchId) {
|
|
53
|
+
this.logger.error(
|
|
54
|
+
'Missing required parameter: postageBatchId.'
|
|
55
|
+
);
|
|
56
|
+
|
|
57
|
+
throw new Error("Missing required parameter: postageBatchId.");
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
let rawPostageBatch;
|
|
61
|
+
|
|
62
|
+
try {
|
|
63
|
+
rawPostageBatch = await this.bee.getPostageBatch(postageBatchId);
|
|
64
|
+
} catch (error) {
|
|
65
|
+
let errorMessage = 'Retrieval of postage batch failed.';
|
|
66
|
+
|
|
67
|
+
if (errorHasStatus(error, NOT_FOUND_STATUS)) {
|
|
68
|
+
errorMessage = GATEWAY_STAMP_ERROR_MESSAGE;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
this.logger.error(
|
|
72
|
+
errorMessage,
|
|
73
|
+
error
|
|
74
|
+
);
|
|
75
|
+
|
|
76
|
+
throw new Error(errorMessage);
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
const batch: PostageBatchCurated = {
|
|
80
|
+
...rawPostageBatch,
|
|
81
|
+
batchID: rawPostageBatch.batchID.toHex(),
|
|
82
|
+
};
|
|
83
|
+
const batchSummary: PostageBatchSummary = getBatchSummary(rawPostageBatch);
|
|
84
|
+
|
|
85
|
+
const content: ResponseContent<PostageBatchCurated, PostageBatchSummary> = {
|
|
86
|
+
raw: batch,
|
|
87
|
+
summary: batchSummary,
|
|
88
|
+
};
|
|
89
|
+
|
|
90
|
+
return getResponseWithStructuredContent(content);
|
|
91
|
+
}
|
|
92
|
+
}
|
|
@@ -0,0 +1,124 @@
|
|
|
1
|
+
import {
|
|
2
|
+
type GenericPluginContext,
|
|
3
|
+
BaseHederaQueryTool,
|
|
4
|
+
type HederaAgentKit,
|
|
5
|
+
} from "hedera-agent-kit";
|
|
6
|
+
import { Bee } from "@ethersphere/bee-js";
|
|
7
|
+
import { z } from "zod";
|
|
8
|
+
import {
|
|
9
|
+
PostageBatchCurated,
|
|
10
|
+
PostageBatchSummary,
|
|
11
|
+
ResponseContent,
|
|
12
|
+
} from "../model";
|
|
13
|
+
import {
|
|
14
|
+
errorHasStatus,
|
|
15
|
+
getBatchSummary,
|
|
16
|
+
getResponseWithStructuredContent,
|
|
17
|
+
ToolResponse,
|
|
18
|
+
} from "../utils";
|
|
19
|
+
import { GATEWAY_STAMP_ERROR_MESSAGE, NOT_FOUND_STATUS } from "../constants";
|
|
20
|
+
import { SwarmConfig } from "../config";
|
|
21
|
+
|
|
22
|
+
const ListPostageStampsSchema = z.object({
|
|
23
|
+
leastUsed: z.boolean().optional(),
|
|
24
|
+
limit: z.number().optional(),
|
|
25
|
+
minUsage: z.number().optional(),
|
|
26
|
+
maxUsage: z.number().optional(),
|
|
27
|
+
});
|
|
28
|
+
|
|
29
|
+
export class ListPostageStampsTool extends BaseHederaQueryTool<typeof ListPostageStampsSchema> {
|
|
30
|
+
name = "swarm-list-postage-stamps";
|
|
31
|
+
description =`
|
|
32
|
+
List the available postage stamps.
|
|
33
|
+
leastUsed: A boolean value that tells if stamps are sorted so least used comes first.
|
|
34
|
+
limit: Limit is the maximum number of returned stamps.
|
|
35
|
+
minUsage: Only list stamps with at least this usage percentage.
|
|
36
|
+
maxUsage: Only list stamps with at most this usage percentage.
|
|
37
|
+
`;
|
|
38
|
+
namespace = "swarm";
|
|
39
|
+
specificInputSchema = ListPostageStampsSchema;
|
|
40
|
+
bee: Bee;
|
|
41
|
+
config: SwarmConfig;
|
|
42
|
+
|
|
43
|
+
constructor(params: {
|
|
44
|
+
hederaKit: HederaAgentKit;
|
|
45
|
+
config: SwarmConfig;
|
|
46
|
+
logger?: GenericPluginContext['logger'];
|
|
47
|
+
bee: Bee;
|
|
48
|
+
}) {
|
|
49
|
+
const { bee, config, ...rest } = params;
|
|
50
|
+
super(rest);
|
|
51
|
+
this.bee = bee;
|
|
52
|
+
this.config = config;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
protected async executeQuery(
|
|
56
|
+
input: z.infer<typeof ListPostageStampsSchema>
|
|
57
|
+
): Promise<ToolResponse | string> {
|
|
58
|
+
const { leastUsed, limit, minUsage, maxUsage } = input;
|
|
59
|
+
|
|
60
|
+
let rawPostageBatches;
|
|
61
|
+
|
|
62
|
+
try {
|
|
63
|
+
rawPostageBatches = await this.bee.getPostageBatches();
|
|
64
|
+
} catch (error) {
|
|
65
|
+
let errorMessage = 'Retrieval of postage batches failed.';
|
|
66
|
+
|
|
67
|
+
if (errorHasStatus(error, NOT_FOUND_STATUS)) {
|
|
68
|
+
errorMessage = GATEWAY_STAMP_ERROR_MESSAGE;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
this.logger.error(
|
|
72
|
+
errorMessage,
|
|
73
|
+
error
|
|
74
|
+
);
|
|
75
|
+
|
|
76
|
+
throw new Error(errorMessage);
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
const batches: PostageBatchCurated[] = rawPostageBatches.map((batch) => ({
|
|
80
|
+
...batch,
|
|
81
|
+
batchID: batch.batchID.toHex(),
|
|
82
|
+
}));
|
|
83
|
+
let filteredPostageBatches = batches.filter((batch) => {
|
|
84
|
+
if (!batch.usable) {
|
|
85
|
+
return false;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
const usagePercentage = batch.usage * 100;
|
|
89
|
+
|
|
90
|
+
if (minUsage !== undefined && usagePercentage < minUsage) {
|
|
91
|
+
return false;
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
if (maxUsage !== undefined && usagePercentage > maxUsage) {
|
|
95
|
+
return false;
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
return true;
|
|
99
|
+
});
|
|
100
|
+
|
|
101
|
+
if (Boolean(leastUsed) && filteredPostageBatches.length) {
|
|
102
|
+
filteredPostageBatches = filteredPostageBatches.sort(
|
|
103
|
+
(batch1, batch2) => batch1.usage - batch2.usage
|
|
104
|
+
);
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
if (limit !== undefined && limit < filteredPostageBatches.length) {
|
|
108
|
+
filteredPostageBatches = filteredPostageBatches.slice(0, limit);
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
const computedPostageBatches: PostageBatchSummary[] =
|
|
112
|
+
filteredPostageBatches.map((batch) => getBatchSummary(batch));
|
|
113
|
+
|
|
114
|
+
const content: ResponseContent<
|
|
115
|
+
PostageBatchCurated[],
|
|
116
|
+
PostageBatchSummary[]
|
|
117
|
+
> = {
|
|
118
|
+
raw: filteredPostageBatches,
|
|
119
|
+
summary: computedPostageBatches,
|
|
120
|
+
};
|
|
121
|
+
|
|
122
|
+
return getResponseWithStructuredContent(content);
|
|
123
|
+
}
|
|
124
|
+
}
|