@hashgraphonline/conversational-agent 0.2.215 → 0.2.217
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 +4 -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 +280 -142
- package/dist/esm/index45.js.map +1 -1
- package/dist/esm/index46.js +181 -24
- package/dist/esm/index46.js.map +1 -1
- package/dist/esm/index48.js +67 -85
- package/dist/esm/index48.js.map +1 -1
- package/dist/esm/index49.js +75 -0
- package/dist/esm/index49.js.map +1 -0
- package/dist/esm/index5.js +2 -2
- package/dist/esm/index50.js +57 -0
- package/dist/esm/index50.js.map +1 -0
- package/dist/esm/index51.js +103 -0
- package/dist/esm/index51.js.map +1 -0
- package/dist/esm/index52.js +79 -0
- package/dist/esm/index52.js.map +1 -0
- package/dist/esm/index53.js +75 -0
- package/dist/esm/index53.js.map +1 -0
- package/dist/esm/index54.js +124 -0
- package/dist/esm/index54.js.map +1 -0
- package/dist/esm/index55.js +58 -0
- package/dist/esm/index55.js.map +1 -0
- package/dist/esm/index56.js +83 -0
- package/dist/esm/index56.js.map +1 -0
- package/dist/esm/index57.js +100 -0
- package/dist/esm/index57.js.map +1 -0
- package/dist/esm/index58.js +118 -0
- package/dist/esm/index58.js.map +1 -0
- package/dist/esm/index59.js +108 -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 +30 -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,109 @@
|
|
|
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
|
+
errorHasStatus,
|
|
10
|
+
getResponseWithStructuredContent,
|
|
11
|
+
ToolResponse,
|
|
12
|
+
} from "../utils";
|
|
13
|
+
import { GATEWAY_TAG_ERROR_MESSAGE } from "../constants";
|
|
14
|
+
import { SwarmConfig } from "../config";
|
|
15
|
+
|
|
16
|
+
const QueryUploadProgressSchema = z.object({
|
|
17
|
+
tagId: z.string(),
|
|
18
|
+
});
|
|
19
|
+
|
|
20
|
+
export class QueryUploadProgressTool extends BaseHederaQueryTool<typeof QueryUploadProgressSchema> {
|
|
21
|
+
name = "swarm-query-upload-progress";
|
|
22
|
+
description = `Query upload progress for a specific upload session identified with the returned Tag ID.
|
|
23
|
+
tagId: Tag ID returned by swarm-upload-file and swarm-upload-folder tools to track upload progress.
|
|
24
|
+
`;
|
|
25
|
+
namespace = "swarm";
|
|
26
|
+
specificInputSchema = QueryUploadProgressSchema;
|
|
27
|
+
bee: Bee;
|
|
28
|
+
config: SwarmConfig;
|
|
29
|
+
|
|
30
|
+
constructor(params: {
|
|
31
|
+
hederaKit: HederaAgentKit;
|
|
32
|
+
config: SwarmConfig;
|
|
33
|
+
logger?: GenericPluginContext['logger'];
|
|
34
|
+
bee: Bee;
|
|
35
|
+
}) {
|
|
36
|
+
const { bee, config, ...rest } = params;
|
|
37
|
+
super(rest);
|
|
38
|
+
this.bee = bee;
|
|
39
|
+
this.config = config;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
protected async executeQuery(
|
|
43
|
+
input: z.infer<typeof QueryUploadProgressSchema>
|
|
44
|
+
): Promise<ToolResponse | string> {
|
|
45
|
+
if (!input?.tagId) {
|
|
46
|
+
this.logger.error(
|
|
47
|
+
'Missing required parameter: tagId.'
|
|
48
|
+
);
|
|
49
|
+
|
|
50
|
+
throw new Error('Missing required parameter: tagId.');
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
const tagUid = Number.parseInt(input.tagId, 10);
|
|
54
|
+
if (Number.isNaN(tagUid)) {
|
|
55
|
+
this.logger.error(
|
|
56
|
+
'Invalid tagId format. Expected a numeric string.'
|
|
57
|
+
);
|
|
58
|
+
|
|
59
|
+
throw new Error('Invalid tagId format. Expected a numeric string.');
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
try {
|
|
63
|
+
const tag = await this.bee.retrieveTag(tagUid);
|
|
64
|
+
|
|
65
|
+
const synced = tag.synced ?? 0;
|
|
66
|
+
const seen = tag.seen ?? 0;
|
|
67
|
+
const processed = synced + seen;
|
|
68
|
+
const total = tag.split ?? 0;
|
|
69
|
+
const startedAt = tag.startedAt;
|
|
70
|
+
|
|
71
|
+
const processedPercentage =
|
|
72
|
+
total > 0 ? Math.round((processed / total) * 100) : 0;
|
|
73
|
+
const isComplete = processedPercentage === 100;
|
|
74
|
+
|
|
75
|
+
let tagDeleted = false;
|
|
76
|
+
if (isComplete) {
|
|
77
|
+
try {
|
|
78
|
+
await this.bee.deleteTag(tagUid);
|
|
79
|
+
tagDeleted = true;
|
|
80
|
+
} catch {
|
|
81
|
+
// Non-fatal: if deletion fails we still return progress
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
return getResponseWithStructuredContent({
|
|
86
|
+
processedPercentage,
|
|
87
|
+
message: isComplete
|
|
88
|
+
? "Upload completed successfully."
|
|
89
|
+
: `Upload progress: ${processedPercentage}% processed`,
|
|
90
|
+
startedAt,
|
|
91
|
+
tagAddress: tag.address,
|
|
92
|
+
});
|
|
93
|
+
} catch (error: any) {
|
|
94
|
+
let errorMessage = `Failed to retrieve upload progress: ${error?.message ?? "Unknown error"}`;
|
|
95
|
+
|
|
96
|
+
const status = error?.status ?? error?.response?.status;
|
|
97
|
+
if (status === 404) {
|
|
98
|
+
errorMessage = `Tag with ID ${input.tagId} does not exist or has been deleted. ` + GATEWAY_TAG_ERROR_MESSAGE;
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
this.logger.error(
|
|
102
|
+
errorMessage,
|
|
103
|
+
error
|
|
104
|
+
);
|
|
105
|
+
|
|
106
|
+
throw new Error(errorMessage);
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
}
|
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
import { Bee } from "@ethersphere/bee-js";
|
|
2
|
+
import { Wallet } from "@ethereumjs/wallet";
|
|
3
|
+
import { z } from "zod";
|
|
4
|
+
import crypto from "crypto";
|
|
5
|
+
import { errorHasStatus, getErrorMessage, getResponseWithStructuredContent, hexToBytes, ToolResponse } from "../utils";
|
|
6
|
+
import { BaseHederaQueryTool, GenericPluginContext, HederaAgentKit } from "hedera-agent-kit";
|
|
7
|
+
import { SwarmConfig } from "../config";
|
|
8
|
+
import { BAD_REQUEST_STATUS } from "../constants";
|
|
9
|
+
|
|
10
|
+
const ReadFeedSchema = z.object({
|
|
11
|
+
memoryTopic: z.string(),
|
|
12
|
+
owner: z.string().optional()
|
|
13
|
+
});
|
|
14
|
+
|
|
15
|
+
export class ReadFeedTool extends BaseHederaQueryTool<typeof ReadFeedSchema> {
|
|
16
|
+
name = "swarm-read-feed";
|
|
17
|
+
description = `Retrieve the latest data from the feed of a given topic.
|
|
18
|
+
memoryTopic: Feed topic.
|
|
19
|
+
owner: When accessing external memory or feed, ethereum address of the owner must be set.
|
|
20
|
+
`;
|
|
21
|
+
namespace = "swarm";
|
|
22
|
+
specificInputSchema = ReadFeedSchema;
|
|
23
|
+
bee: Bee;
|
|
24
|
+
config: SwarmConfig;
|
|
25
|
+
|
|
26
|
+
constructor(params: {
|
|
27
|
+
hederaKit: HederaAgentKit;
|
|
28
|
+
config: SwarmConfig;
|
|
29
|
+
logger?: GenericPluginContext['logger'];
|
|
30
|
+
bee: Bee;
|
|
31
|
+
}) {
|
|
32
|
+
const { bee, config, ...rest } = params;
|
|
33
|
+
super(rest);
|
|
34
|
+
this.bee = bee;
|
|
35
|
+
this.config = config;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
protected async executeQuery(
|
|
39
|
+
input: z.infer<typeof ReadFeedSchema>
|
|
40
|
+
): Promise<ToolResponse | string> {
|
|
41
|
+
const { memoryTopic, owner } = input;
|
|
42
|
+
|
|
43
|
+
if (!memoryTopic) {
|
|
44
|
+
this.logger.error(
|
|
45
|
+
'Missing required parameter: memoryTopic.'
|
|
46
|
+
);
|
|
47
|
+
|
|
48
|
+
throw new Error('Missing required parameter: memoryTopic.');
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
this.logger.info(`[API] Downloading text from Swarm feed with topic: ${memoryTopic}.`);
|
|
52
|
+
|
|
53
|
+
if (!this.config.beeFeedPK) {
|
|
54
|
+
this.logger.error('Feed private key not configured.');
|
|
55
|
+
|
|
56
|
+
throw new Error('Feed private key not configured.');
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
// Process topic - if not a hex string, hash it
|
|
60
|
+
let topic = memoryTopic;
|
|
61
|
+
if (topic.startsWith("0x")) {
|
|
62
|
+
topic = topic.slice(2);
|
|
63
|
+
}
|
|
64
|
+
const isHexString = /^[0-9a-fA-F]{64}$/.test(topic);
|
|
65
|
+
|
|
66
|
+
if (!isHexString) {
|
|
67
|
+
// Hash the topic string using SHA-256
|
|
68
|
+
const hash = crypto.createHash("sha256").update(memoryTopic).digest("hex");
|
|
69
|
+
topic = hash;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
// Convert topic string to bytes
|
|
73
|
+
const topicBytes = hexToBytes(topic);
|
|
74
|
+
|
|
75
|
+
let feedOwner = owner;
|
|
76
|
+
if (!feedOwner) {
|
|
77
|
+
const feedPrivateKey = hexToBytes(this.config.beeFeedPK);
|
|
78
|
+
const signer = new Wallet(feedPrivateKey);
|
|
79
|
+
feedOwner = signer.getAddressString().slice(2);
|
|
80
|
+
} else {
|
|
81
|
+
if (feedOwner.startsWith("0x")) {
|
|
82
|
+
feedOwner = feedOwner.slice(2);
|
|
83
|
+
}
|
|
84
|
+
if (feedOwner.length !== 40) {
|
|
85
|
+
this.logger.error('Owner must be a valid Ethereum address.');
|
|
86
|
+
|
|
87
|
+
throw new Error('Owner must be a valid Ethereum address.');
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
let textData;
|
|
92
|
+
|
|
93
|
+
try {
|
|
94
|
+
const feedReader = this.bee.makeFeedReader(topicBytes, feedOwner);
|
|
95
|
+
const latestUpdate = await feedReader.downloadPayload();
|
|
96
|
+
textData = latestUpdate.payload.toUtf8();
|
|
97
|
+
} catch (error) {
|
|
98
|
+
let errorMessage = 'Reading feed failed.';
|
|
99
|
+
if (errorHasStatus(error, BAD_REQUEST_STATUS)) {
|
|
100
|
+
errorMessage = getErrorMessage(error);
|
|
101
|
+
}
|
|
102
|
+
this.logger.error(errorMessage, error);
|
|
103
|
+
throw new Error(errorMessage);
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
return getResponseWithStructuredContent({
|
|
107
|
+
textData,
|
|
108
|
+
});
|
|
109
|
+
}
|
|
110
|
+
}
|
|
@@ -0,0 +1,149 @@
|
|
|
1
|
+
import {
|
|
2
|
+
BaseHederaQueryTool,
|
|
3
|
+
HederaAgentKit,
|
|
4
|
+
type GenericPluginContext,
|
|
5
|
+
} from "hedera-agent-kit";
|
|
6
|
+
import { Bee } from "@ethersphere/bee-js";
|
|
7
|
+
import { z } from "zod";
|
|
8
|
+
import crypto from "crypto";
|
|
9
|
+
import {
|
|
10
|
+
errorHasStatus,
|
|
11
|
+
getErrorMessage,
|
|
12
|
+
getResponseWithStructuredContent,
|
|
13
|
+
getUploadPostageBatchId,
|
|
14
|
+
hexToBytes,
|
|
15
|
+
ToolResponse,
|
|
16
|
+
} from "../utils";
|
|
17
|
+
import { BAD_REQUEST_STATUS } from "../constants";
|
|
18
|
+
import { Wallet } from "@ethereumjs/wallet";
|
|
19
|
+
import { SwarmConfig } from "../config";
|
|
20
|
+
|
|
21
|
+
const UpdateFeedSchema = z.object({
|
|
22
|
+
data: z.string(),
|
|
23
|
+
memoryTopic: z.string(),
|
|
24
|
+
postageBatchId: z.string().optional(),
|
|
25
|
+
});
|
|
26
|
+
|
|
27
|
+
export class UpdateFeedTool extends BaseHederaQueryTool<typeof UpdateFeedSchema> {
|
|
28
|
+
name = "swarm-update-feed";
|
|
29
|
+
description = `Update the feed of a given topic with new data.
|
|
30
|
+
data: Arbitrary string to upload.
|
|
31
|
+
memoryTopic: If provided, uploads the data to a feed with this topic. It is the label of the memory that can be used later to retrieve the data instead of its content hash. If not a hex string, it will be hashed to create a feed topic.
|
|
32
|
+
postageBatchId: The postage stamp batch ID which will be used to perform the upload, if it is provided.`;
|
|
33
|
+
namespace = "swarm";
|
|
34
|
+
specificInputSchema = UpdateFeedSchema;
|
|
35
|
+
bee: Bee;
|
|
36
|
+
config: SwarmConfig;
|
|
37
|
+
|
|
38
|
+
constructor(params: {
|
|
39
|
+
hederaKit: HederaAgentKit;
|
|
40
|
+
config: SwarmConfig;
|
|
41
|
+
logger?: GenericPluginContext['logger'];
|
|
42
|
+
bee: Bee;
|
|
43
|
+
}) {
|
|
44
|
+
const { bee, config, ...rest } = params;
|
|
45
|
+
super(rest);
|
|
46
|
+
this.bee = bee;
|
|
47
|
+
this.config = config;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
protected async executeQuery(
|
|
51
|
+
input: z.infer<typeof UpdateFeedSchema>
|
|
52
|
+
): Promise<ToolResponse | string> {
|
|
53
|
+
const { data, memoryTopic, postageBatchId: inputPostageBatchId } = input;
|
|
54
|
+
if (!data) {
|
|
55
|
+
this.logger.error(
|
|
56
|
+
'Missing required parameter: data.'
|
|
57
|
+
);
|
|
58
|
+
|
|
59
|
+
throw new Error('Missing required parameter: data.');
|
|
60
|
+
} else if (!memoryTopic) {
|
|
61
|
+
this.logger.error(
|
|
62
|
+
'Missing required parameter: topic.'
|
|
63
|
+
);
|
|
64
|
+
|
|
65
|
+
throw new Error('Missing required parameter: topic.');
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
let postageBatchId = "";
|
|
69
|
+
|
|
70
|
+
try {
|
|
71
|
+
postageBatchId = await getUploadPostageBatchId(
|
|
72
|
+
inputPostageBatchId,
|
|
73
|
+
this.bee,
|
|
74
|
+
this.config,
|
|
75
|
+
);
|
|
76
|
+
} catch (error) {
|
|
77
|
+
let errorMessage = 'Update feed failed.';
|
|
78
|
+
if (error instanceof Error) {
|
|
79
|
+
errorMessage = error.message;
|
|
80
|
+
}
|
|
81
|
+
this.logger.error(errorMessage);
|
|
82
|
+
|
|
83
|
+
throw new Error(errorMessage);
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
const binaryData = Buffer.from(data);
|
|
87
|
+
|
|
88
|
+
// Feed upload if memoryTopic is specified
|
|
89
|
+
if (!this.config.beeFeedPK) {
|
|
90
|
+
this.logger.error('Feed private key not configured.');
|
|
91
|
+
|
|
92
|
+
throw new Error('Feed private key not configured.');
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
// Process topic - if not a hex string, hash it
|
|
96
|
+
let topic = memoryTopic;
|
|
97
|
+
if (topic.startsWith("0x")) {
|
|
98
|
+
topic = topic.slice(2);
|
|
99
|
+
}
|
|
100
|
+
const isHexString = /^[0-9a-fA-F]{64}$/.test(memoryTopic);
|
|
101
|
+
|
|
102
|
+
if (!isHexString) {
|
|
103
|
+
// Hash the topic string using SHA-256
|
|
104
|
+
const hash = crypto
|
|
105
|
+
.createHash("sha256")
|
|
106
|
+
.update(memoryTopic)
|
|
107
|
+
.digest("hex");
|
|
108
|
+
topic = hash;
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
// Convert topic string to bytes
|
|
112
|
+
const topicBytes = hexToBytes(topic);
|
|
113
|
+
|
|
114
|
+
const feedPrivateKey = hexToBytes(this.config.beeFeedPK);
|
|
115
|
+
const signer = new Wallet(feedPrivateKey);
|
|
116
|
+
const owner = signer.getAddressString().slice(2);
|
|
117
|
+
|
|
118
|
+
let result;
|
|
119
|
+
|
|
120
|
+
try {
|
|
121
|
+
const feedWriter = this.bee.makeFeedWriter(topicBytes, feedPrivateKey);
|
|
122
|
+
|
|
123
|
+
result = await feedWriter.uploadPayload(postageBatchId!, binaryData);
|
|
124
|
+
} catch (error) {
|
|
125
|
+
let errorMessage = 'Unable to update feed.';
|
|
126
|
+
|
|
127
|
+
if (errorHasStatus(error, BAD_REQUEST_STATUS)) {
|
|
128
|
+
errorMessage = getErrorMessage(error);
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
this.logger.error(
|
|
132
|
+
errorMessage,
|
|
133
|
+
error
|
|
134
|
+
);
|
|
135
|
+
|
|
136
|
+
throw new Error(errorMessage);
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
const reference = result.reference.toString();
|
|
140
|
+
|
|
141
|
+
return getResponseWithStructuredContent({
|
|
142
|
+
reference,
|
|
143
|
+
topicString: memoryTopic,
|
|
144
|
+
topic: topic,
|
|
145
|
+
feedUrl: `${this.bee.url}/feeds/${owner}/${topic}`,
|
|
146
|
+
message: 'Data successfully uploaded to Swarm and linked to feed.',
|
|
147
|
+
});
|
|
148
|
+
}
|
|
149
|
+
}
|
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
import {
|
|
2
|
+
BaseHederaQueryTool,
|
|
3
|
+
HederaAgentKit,
|
|
4
|
+
type GenericPluginContext,
|
|
5
|
+
} from "hedera-agent-kit";
|
|
6
|
+
import { Bee } from "@ethersphere/bee-js";
|
|
7
|
+
import { z } from "zod";
|
|
8
|
+
import {
|
|
9
|
+
errorHasStatus,
|
|
10
|
+
getErrorMessage,
|
|
11
|
+
getResponseWithStructuredContent,
|
|
12
|
+
getUploadPostageBatchId,
|
|
13
|
+
ToolResponse,
|
|
14
|
+
} from "../utils";
|
|
15
|
+
import { BAD_REQUEST_STATUS } from "../constants";
|
|
16
|
+
import { SwarmConfig } from "../config";
|
|
17
|
+
|
|
18
|
+
const UploadDataSchema = z.object({
|
|
19
|
+
data: z.string(),
|
|
20
|
+
redundancyLevel: z.number().optional(),
|
|
21
|
+
postageBatchId: z.string().optional(),
|
|
22
|
+
});
|
|
23
|
+
|
|
24
|
+
export class UploadDataTool extends BaseHederaQueryTool<typeof UploadDataSchema> {
|
|
25
|
+
name = "swarm-upload-data";
|
|
26
|
+
description = `
|
|
27
|
+
Upload text data to Swarm.
|
|
28
|
+
data: Arbitrary string to upload.
|
|
29
|
+
redundancyLevel: Redundancy level for fault tolerance: 0 - none, 1 - medium, 2 - strong, 3 - insane, 4 - paranoid (higher values provide better fault tolerance but increase storage overhead). Optional, value is 0 if not requested.
|
|
30
|
+
postageBatchId: The postage stamp batch ID which will be used to perform the upload, if it is provided.
|
|
31
|
+
`;
|
|
32
|
+
namespace = "swarm";
|
|
33
|
+
specificInputSchema = UploadDataSchema;
|
|
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 UploadDataSchema>
|
|
51
|
+
): Promise<ToolResponse | string> {
|
|
52
|
+
const { data, redundancyLevel, postageBatchId: inputPostageBatchId } = input;
|
|
53
|
+
|
|
54
|
+
if (!data) {
|
|
55
|
+
this.logger.error(
|
|
56
|
+
'Missing required parameter: data.'
|
|
57
|
+
);
|
|
58
|
+
|
|
59
|
+
throw new Error('Missing required parameter: data.');
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
let postageBatchId = "";
|
|
63
|
+
|
|
64
|
+
try {
|
|
65
|
+
postageBatchId = await getUploadPostageBatchId(
|
|
66
|
+
inputPostageBatchId,
|
|
67
|
+
this.bee,
|
|
68
|
+
this.config,
|
|
69
|
+
);
|
|
70
|
+
} catch (error) {
|
|
71
|
+
let errorMessage = 'Upload data failed.';
|
|
72
|
+
if (error instanceof Error) {
|
|
73
|
+
errorMessage = error.message;
|
|
74
|
+
}
|
|
75
|
+
this.logger.error(errorMessage);
|
|
76
|
+
|
|
77
|
+
throw new Error(errorMessage);
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
const binaryData = Buffer.from(data);
|
|
81
|
+
|
|
82
|
+
const options = redundancyLevel ? { redundancyLevel } : undefined;
|
|
83
|
+
|
|
84
|
+
let result;
|
|
85
|
+
|
|
86
|
+
try {
|
|
87
|
+
result = await this.bee.uploadData(postageBatchId, binaryData, options);
|
|
88
|
+
} catch (error) {
|
|
89
|
+
let errorMessage = 'Unable to upload data.';
|
|
90
|
+
|
|
91
|
+
if (errorHasStatus(error, BAD_REQUEST_STATUS)) {
|
|
92
|
+
errorMessage = getErrorMessage(error);
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
this.logger.error(
|
|
96
|
+
errorMessage,
|
|
97
|
+
error
|
|
98
|
+
);
|
|
99
|
+
|
|
100
|
+
throw new Error(errorMessage);
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
return getResponseWithStructuredContent({
|
|
104
|
+
reference: result.reference.toString(),
|
|
105
|
+
url: this.bee.url + "/bytes/" + result.reference.toString(),
|
|
106
|
+
message: 'Data successfully uploaded to Swarm',
|
|
107
|
+
});
|
|
108
|
+
}
|
|
109
|
+
}
|
|
@@ -0,0 +1,163 @@
|
|
|
1
|
+
import {
|
|
2
|
+
BaseHederaQueryTool,
|
|
3
|
+
HederaAgentKit,
|
|
4
|
+
type GenericPluginContext,
|
|
5
|
+
} from "hedera-agent-kit";
|
|
6
|
+
import { Bee, FileUploadOptions } from "@ethersphere/bee-js";
|
|
7
|
+
import { z } from "zod";
|
|
8
|
+
import { promisify } from "util";
|
|
9
|
+
import fs from "fs";
|
|
10
|
+
import {
|
|
11
|
+
errorHasStatus,
|
|
12
|
+
getErrorMessage,
|
|
13
|
+
getResponseWithStructuredContent,
|
|
14
|
+
getUploadPostageBatchId,
|
|
15
|
+
ToolResponse,
|
|
16
|
+
} from "../utils";
|
|
17
|
+
import { BAD_REQUEST_STATUS, DEFAULT_DEFERRED_UPLOAD_SIZE_THRESHOLD_MB, GATEWAY_TAG_ERROR_MESSAGE, NOT_FOUND_STATUS } from "../constants";
|
|
18
|
+
import { SwarmConfig } from "../config";
|
|
19
|
+
|
|
20
|
+
const UploadFileSchema = z.object({
|
|
21
|
+
data: z.string(),
|
|
22
|
+
isPath: z.boolean(),
|
|
23
|
+
redundancyLevel: z.number().optional(),
|
|
24
|
+
postageBatchId: z.string().optional(),
|
|
25
|
+
});
|
|
26
|
+
|
|
27
|
+
export class UploadFileTool extends BaseHederaQueryTool<typeof UploadFileSchema> {
|
|
28
|
+
name = "swarm-upload-file";
|
|
29
|
+
description = `Upload a file to Swarm.
|
|
30
|
+
data: base64 encoded file content or file path.
|
|
31
|
+
isPath: Wether the data parameter is a path.
|
|
32
|
+
redundancyLevel: Redundancy level for fault tolerance (higher values provide better fault tolerance but increase storage overhead). 0 - none, 1 - medium, 2 - strong, 3 - insane, 4 - paranoid.
|
|
33
|
+
postageBatchId: The postage stamp batch ID which will be used to perform the upload, if it is provided.`;
|
|
34
|
+
namespace = "swarm";
|
|
35
|
+
specificInputSchema = UploadFileSchema;
|
|
36
|
+
bee: Bee;
|
|
37
|
+
config: SwarmConfig;
|
|
38
|
+
|
|
39
|
+
constructor(params: {
|
|
40
|
+
hederaKit: HederaAgentKit;
|
|
41
|
+
config: SwarmConfig;
|
|
42
|
+
logger?: GenericPluginContext['logger'];
|
|
43
|
+
bee: Bee;
|
|
44
|
+
}) {
|
|
45
|
+
const { bee, config, ...rest } = params;
|
|
46
|
+
super(rest);
|
|
47
|
+
this.bee = bee;
|
|
48
|
+
this.config = config;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
protected async executeQuery(
|
|
52
|
+
input: z.infer<typeof UploadFileSchema>
|
|
53
|
+
): Promise<ToolResponse | string> {
|
|
54
|
+
const { data, isPath, redundancyLevel: inputRedundancyLevel, postageBatchId: inputPostageBatchId } = input;
|
|
55
|
+
|
|
56
|
+
if (!data) {
|
|
57
|
+
this.logger.error(
|
|
58
|
+
'Missing required parameter: data.'
|
|
59
|
+
);
|
|
60
|
+
|
|
61
|
+
throw new Error('Missing required parameter: data.');
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
let postageBatchId = "";
|
|
65
|
+
|
|
66
|
+
try {
|
|
67
|
+
postageBatchId = await getUploadPostageBatchId(
|
|
68
|
+
inputPostageBatchId,
|
|
69
|
+
this.bee,
|
|
70
|
+
this.config,
|
|
71
|
+
);
|
|
72
|
+
} catch (error) {
|
|
73
|
+
let errorMessage = 'Upload file failed.';
|
|
74
|
+
if (error instanceof Error) {
|
|
75
|
+
errorMessage = error.message;
|
|
76
|
+
}
|
|
77
|
+
this.logger.error(errorMessage);
|
|
78
|
+
|
|
79
|
+
throw new Error(errorMessage);
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
let binaryData: Buffer;
|
|
83
|
+
let name: string | undefined;
|
|
84
|
+
|
|
85
|
+
if (isPath) {
|
|
86
|
+
// Read file from path
|
|
87
|
+
try {
|
|
88
|
+
binaryData = await promisify(fs.readFile)(data);
|
|
89
|
+
} catch (fileError) {
|
|
90
|
+
this.logger.error(
|
|
91
|
+
`Unable to read file at path: ${data}.`,
|
|
92
|
+
fileError
|
|
93
|
+
);
|
|
94
|
+
|
|
95
|
+
throw new Error(`Unable to read file at path: ${data}.`);
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
name = data.split("/").pop();
|
|
99
|
+
} else {
|
|
100
|
+
binaryData = Buffer.from(data, "base64");
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
const redundancyLevel = inputRedundancyLevel;
|
|
104
|
+
const options: FileUploadOptions = {};
|
|
105
|
+
const deferredUploadSizeThreshold = Number(this.config.deferredUploadSizeThresholdMB) ||
|
|
106
|
+
DEFAULT_DEFERRED_UPLOAD_SIZE_THRESHOLD_MB;
|
|
107
|
+
const deferred =
|
|
108
|
+
binaryData.length > deferredUploadSizeThreshold * 1024 * 1024;
|
|
109
|
+
options.deferred = deferred;
|
|
110
|
+
if (redundancyLevel) {
|
|
111
|
+
options.redundancyLevel = redundancyLevel;
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
let message = "File successfully uploaded to Swarm";
|
|
115
|
+
let tagId: string | undefined = undefined;
|
|
116
|
+
// Create tag for deferred uploads or when explicitly requested
|
|
117
|
+
if (deferred) {
|
|
118
|
+
try {
|
|
119
|
+
const tag = await this.bee.createTag();
|
|
120
|
+
options.tag = tag.uid;
|
|
121
|
+
tagId = tag.uid.toString();
|
|
122
|
+
message =
|
|
123
|
+
"File upload started in deferred mode. Use query_upload_progress to track progress.";
|
|
124
|
+
} catch (error) {
|
|
125
|
+
if (errorHasStatus(error, NOT_FOUND_STATUS)) {
|
|
126
|
+
this.logger.error(
|
|
127
|
+
GATEWAY_TAG_ERROR_MESSAGE,
|
|
128
|
+
error
|
|
129
|
+
);
|
|
130
|
+
|
|
131
|
+
throw new Error(GATEWAY_TAG_ERROR_MESSAGE);
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
let result;
|
|
137
|
+
|
|
138
|
+
try {
|
|
139
|
+
// Start the deferred upload
|
|
140
|
+
result = await this.bee.uploadFile(postageBatchId, binaryData, name, options);
|
|
141
|
+
} catch (error) {
|
|
142
|
+
let errorMessage = 'Unable to upload file.';
|
|
143
|
+
|
|
144
|
+
if (errorHasStatus(error, BAD_REQUEST_STATUS)) {
|
|
145
|
+
errorMessage = getErrorMessage(error);
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
this.logger.error(
|
|
149
|
+
errorMessage,
|
|
150
|
+
error
|
|
151
|
+
);
|
|
152
|
+
|
|
153
|
+
throw new Error(errorMessage);
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
return getResponseWithStructuredContent({
|
|
157
|
+
reference: result.reference.toString(),
|
|
158
|
+
url: this.bee.url + "/bzz/" + result.reference.toString(),
|
|
159
|
+
message,
|
|
160
|
+
tagId,
|
|
161
|
+
});
|
|
162
|
+
}
|
|
163
|
+
}
|