@hashgraphonline/conversational-agent
Version:
Hashgraph Online conversational AI agent implementing HCS-10 communication, HCS-2 registries, and content inscription on Hedera. https://hol.org
80 lines (79 loc) • 2.95 kB
JavaScript
import { BaseHederaQueryTool } from "hedera-agent-kit";
import { Size, Duration } from "@ethersphere/bee-js";
import { z } from "zod";
import { makeDate, errorHasStatus, getErrorMessage, getResponseWithStructuredContent } from "./index62.js";
import { NOT_FOUND_STATUS, GATEWAY_STAMP_ERROR_MESSAGE, BAD_REQUEST_STATUS } from "./index63.js";
const ExtendPostageStampSchema = z.object({
postageBatchId: z.string(),
duration: z.string().optional(),
size: z.number().optional()
});
class ExtendPostageStampTool extends BaseHederaQueryTool {
constructor(params) {
const { bee, config, ...rest } = params;
super(rest);
this.name = "swarm-extend-postage-stamp";
this.description = `Increase the duration (relative to current duration) or size (in megabytes) of a postage stamp.
postageBatchId: The id of the batch for which extend is performed.
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.
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.
`;
this.namespace = "swarm";
this.specificInputSchema = ExtendPostageStampSchema;
this.bee = bee;
this.config = config;
}
async executeQuery(input) {
const { postageBatchId, duration, size } = input;
if (!postageBatchId) {
this.logger.error(
"Missing required parameter: postageBatchId."
);
throw new Error("Missing required parameter: postageBatchId.");
} else if (!duration && !size) {
this.logger.error(
"You need at least one parameter from duration and size."
);
throw new Error("You need at least one parameter from duration and size.");
}
const extendSize = !!size ? Size.fromMegabytes(size) : Size.fromBytes(1);
let extendDuration = Duration.ZERO;
try {
if (duration) {
extendDuration = Duration.fromMilliseconds(makeDate(duration));
}
} catch (makeDateError) {
this.logger.error(
"Invalid parameter: duration."
);
throw new Error("Invalid parameter: duration.");
}
let extendStorageResponse;
try {
extendStorageResponse = await this.bee.extendStorage(
postageBatchId,
extendSize,
extendDuration
);
} catch (error) {
let errorMessage = "Extend failed.";
if (errorHasStatus(error, NOT_FOUND_STATUS)) {
errorMessage = GATEWAY_STAMP_ERROR_MESSAGE;
} else if (errorHasStatus(error, BAD_REQUEST_STATUS)) {
errorMessage = getErrorMessage(error);
}
this.logger.error(
errorMessage,
error
);
throw new Error(errorMessage);
}
return getResponseWithStructuredContent({
postageBatchId: extendStorageResponse.toHex()
});
}
}
export {
ExtendPostageStampTool
};
//# sourceMappingURL=index52.js.map