@hashgraphonline/conversational-agent
Version:
Hashgraph Online conversational AI agent implementing HCS-10 communication, HCS-2 registries, and content inscription on Hedera. https://hol.org
76 lines (75 loc) • 2.62 kB
JavaScript
import { BaseHederaQueryTool } from "hedera-agent-kit";
import { z } from "zod";
import { getUploadPostageBatchId, errorHasStatus, getErrorMessage, getResponseWithStructuredContent } from "./index62.js";
import { BAD_REQUEST_STATUS } from "./index63.js";
const UploadDataSchema = z.object({
data: z.string(),
redundancyLevel: z.number().optional(),
postageBatchId: z.string().optional()
});
class UploadDataTool extends BaseHederaQueryTool {
constructor(params) {
const { bee, config, ...rest } = params;
super(rest);
this.name = "swarm-upload-data";
this.description = `
Upload text data to Swarm.
data: Arbitrary string to upload.
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.
postageBatchId: The postage stamp batch ID which will be used to perform the upload, if it is provided.
`;
this.namespace = "swarm";
this.specificInputSchema = UploadDataSchema;
this.bee = bee;
this.config = config;
}
async executeQuery(input) {
const { data, redundancyLevel, postageBatchId: inputPostageBatchId } = input;
if (!data) {
this.logger.error(
"Missing required parameter: data."
);
throw new Error("Missing required parameter: data.");
}
let postageBatchId = "";
try {
postageBatchId = await getUploadPostageBatchId(
inputPostageBatchId,
this.bee,
this.config
);
} catch (error) {
let errorMessage = "Upload data failed.";
if (error instanceof Error) {
errorMessage = error.message;
}
this.logger.error(errorMessage);
throw new Error(errorMessage);
}
const binaryData = Buffer.from(data);
const options = redundancyLevel ? { redundancyLevel } : void 0;
let result;
try {
result = await this.bee.uploadData(postageBatchId, binaryData, options);
} catch (error) {
let errorMessage = "Unable to upload data.";
if (errorHasStatus(error, BAD_REQUEST_STATUS)) {
errorMessage = getErrorMessage(error);
}
this.logger.error(
errorMessage,
error
);
throw new Error(errorMessage);
}
return getResponseWithStructuredContent({
reference: result.reference.toString(),
url: this.bee.url + "/bytes/" + result.reference.toString(),
message: "Data successfully uploaded to Swarm"
});
}
}
export {
UploadDataTool
};
//# sourceMappingURL=index49.js.map