@hashgraphonline/conversational-agent
Version:
Hashgraph Online conversational AI agent implementing HCS-10 communication, HCS-2 registries, and content inscription on Hedera. https://hol.org
81 lines (80 loc) • 2.79 kB
JavaScript
import { BaseHederaQueryTool } from "hedera-agent-kit";
import { z } from "zod";
import { errorHasStatus, getBatchSummary, getResponseWithStructuredContent } from "./index62.js";
import { NOT_FOUND_STATUS, GATEWAY_STAMP_ERROR_MESSAGE } from "./index63.js";
const ListPostageStampsSchema = z.object({
leastUsed: z.boolean().optional(),
limit: z.number().optional(),
minUsage: z.number().optional(),
maxUsage: z.number().optional()
});
class ListPostageStampsTool extends BaseHederaQueryTool {
constructor(params) {
const { bee, config, ...rest } = params;
super(rest);
this.name = "swarm-list-postage-stamps";
this.description = `
List the available postage stamps.
leastUsed: A boolean value that tells if stamps are sorted so least used comes first.
limit: Limit is the maximum number of returned stamps.
minUsage: Only list stamps with at least this usage percentage.
maxUsage: Only list stamps with at most this usage percentage.
`;
this.namespace = "swarm";
this.specificInputSchema = ListPostageStampsSchema;
this.bee = bee;
this.config = config;
}
async executeQuery(input) {
const { leastUsed, limit, minUsage, maxUsage } = input;
let rawPostageBatches;
try {
rawPostageBatches = await this.bee.getPostageBatches();
} catch (error) {
let errorMessage = "Retrieval of postage batches failed.";
if (errorHasStatus(error, NOT_FOUND_STATUS)) {
errorMessage = GATEWAY_STAMP_ERROR_MESSAGE;
}
this.logger.error(
errorMessage,
error
);
throw new Error(errorMessage);
}
const batches = rawPostageBatches.map((batch) => ({
...batch,
batchID: batch.batchID.toHex()
}));
let filteredPostageBatches = batches.filter((batch) => {
if (!batch.usable) {
return false;
}
const usagePercentage = batch.usage * 100;
if (minUsage !== void 0 && usagePercentage < minUsage) {
return false;
}
if (maxUsage !== void 0 && usagePercentage > maxUsage) {
return false;
}
return true;
});
if (Boolean(leastUsed) && filteredPostageBatches.length) {
filteredPostageBatches = filteredPostageBatches.sort(
(batch1, batch2) => batch1.usage - batch2.usage
);
}
if (limit !== void 0 && limit < filteredPostageBatches.length) {
filteredPostageBatches = filteredPostageBatches.slice(0, limit);
}
const computedPostageBatches = filteredPostageBatches.map((batch) => getBatchSummary(batch));
const content = {
raw: filteredPostageBatches,
summary: computedPostageBatches
};
return getResponseWithStructuredContent(content);
}
}
export {
ListPostageStampsTool
};
//# sourceMappingURL=index48.js.map