@hashgraphonline/conversational-agent
Version:
Hashgraph Online conversational AI agent implementing HCS-10 communication, HCS-2 registries, and content inscription on Hedera. https://hol.org
104 lines (103 loc) • 3.33 kB
JavaScript
import { BaseHederaQueryTool } from "hedera-agent-kit";
import { Size, Duration } from "@ethersphere/bee-js";
import { z } from "zod";
import { makeDate, runWithTimeout, errorHasStatus, getErrorMessage } from "./index62.js";
import { CALL_TIMEOUT, POSTAGE_CREATE_TIMEOUT_MESSAGE, GATEWAY_STAMP_ERROR_MESSAGE, NOT_FOUND_STATUS, BAD_REQUEST_STATUS } from "./index63.js";
const CreatePostageStampSchema = z.object({
size: z.number(),
duration: z.string(),
label: z.string().optional()
});
class CreatePostageStampTool extends BaseHederaQueryTool {
constructor(params) {
const { bee, config, ...rest } = params;
super(rest);
this.name = "swarm-create-postage-stamp";
this.description = `
Buy postage stamp based on size in megabytes and duration.
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.
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.
`;
this.namespace = "swarm";
this.specificInputSchema = CreatePostageStampSchema;
this.bee = bee;
this.config = config;
}
async executeQuery(input) {
const { size, duration, label } = input;
if (!size) {
this.logger.error(
"Missing required parameter: size."
);
throw new Error("Missing required parameter: size.");
} else if (!duration) {
this.logger.error(
"Missing required parameter: duration."
);
throw new Error("Missing required parameter: duration.");
}
let durationMs;
try {
durationMs = makeDate(duration);
} catch (makeDateError) {
this.logger.error(
"Invalid parameter: duration."
);
throw new Error("Invalid parameter: duration.");
}
let buyStorageResponse;
try {
let options = {};
if (label !== void 0) {
options = {
label
};
}
const buyStoragePromise = this.bee.buyStorage(
Size.fromMegabytes(size),
Duration.fromMilliseconds(durationMs),
options
);
const [response, hasTimedOut] = await runWithTimeout(
buyStoragePromise,
CALL_TIMEOUT
);
if (hasTimedOut) {
return JSON.stringify({
content: [
{
type: "text",
text: POSTAGE_CREATE_TIMEOUT_MESSAGE
}
]
});
}
buyStorageResponse = response;
} catch (error) {
let errorMessage = "Unable to buy storage.";
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 {
content: [
{
type: "text",
text: `Postage batch ID: ${buyStorageResponse.toHex()}`
}
]
};
}
}
export {
CreatePostageStampTool
};
//# sourceMappingURL=index51.js.map