UNPKG

@hashgraphonline/conversational-agent

Version:

Hashgraph Online conversational AI agent implementing HCS-10 communication, HCS-2 registries, and content inscription on Hedera. https://hol.org

132 lines (131 loc) 3.78 kB
import { NOT_FOUND_STATUS, DEFAULT_GATEWAY_BATCH_ID } from "./index63.js"; function hexToBytes(hex) { const bytes = new Uint8Array(hex.length / 2); for (let i = 0; i < hex.length; i += 2) { bytes[i / 2] = parseInt(hex.slice(i, i + 2), 16); } return bytes; } const getBatchSummary = (batch) => ({ stampID: typeof batch.batchID === "string" ? batch.batchID : batch.batchID.toHex(), usage: batch.usageText, capacity: `${batch.remainingSize.toFormattedString()} remaining out of ${batch.size.toFormattedString()}`, immutable: batch.immutableFlag, ttl: `${batch.duration.represent()} (${batch.duration.toEndDate().toDateString()})` }); const getResponseWithStructuredContent = (data) => ({ content: [ { type: "text", text: JSON.stringify(data, null, 2) } ], structuredContent: data }); const errorHasStatus = (error, status) => { if (typeof error === "object" && error !== null && "status" in error) { return error.status === status; } return false; }; const getErrorMessage = (error) => { if (typeof error === "object" && error !== null && "responseBody" in error && typeof error.responseBody === "object" && error.responseBody !== null && "message" in error.responseBody) { return error.responseBody.message; } return ""; }; const runWithTimeout = async (asyncAction, timeout) => { let hasTimedOut = false; const timeoutPromise = new Promise( (resolve) => setTimeout(() => { hasTimedOut = true; resolve(true); }, timeout) ); const response = await Promise.race([asyncAction, timeoutPromise]); return [response, hasTimedOut]; }; const getUploadPostageBatchId = async (argsPostageBatchId, bee, config) => { let postageBatchId = argsPostageBatchId; const autoAssignStamp = config.autoAssignStamp ?? true; let maxRemainingSize = 0; if (!postageBatchId && !autoAssignStamp) { throw new Error("No postageBatchId was provided. Please repeat the prompt and also specify the usable postage batch id."); } else if (!postageBatchId) { try { const rawPostageBatches = await bee.getPostageBatches(); rawPostageBatches.forEach((batch) => { if (!batch.usable) { return; } const remainingSize = batch.remainingSize.toBytes(); if (remainingSize > maxRemainingSize) { maxRemainingSize = remainingSize; postageBatchId = batch.batchID.toHex(); } }); } catch (error) { if (errorHasStatus(error, NOT_FOUND_STATUS)) { postageBatchId = DEFAULT_GATEWAY_BATCH_ID; } else { throw new Error("Retrieval of postage batches failed."); } } } if (!postageBatchId) { throw new Error("There is no usable postage batch with capacity."); } return postageBatchId; }; const dateUnits = { ms: 1, milli: 1, millis: 1, millisecond: 1, milliseconds: 1, s: 1e3, sec: 1e3, second: 1e3, seconds: 1e3, m: 6e4, min: 6e4, minute: 6e4, minutes: 6e4, h: 36e5, hour: 36e5, hours: 36e5, d: 864e5, day: 864e5, days: 864e5, w: 6048e5, week: 6048e5, weeks: 6048e5, month: 2592e6, months: 2592e6, y: 31536e6, year: 31536e6, years: 31536e6 }; function makeDate(numberWithUnit) { const number = parseFloat(numberWithUnit); if (isNaN(number)) { throw Error("makeDate got NaN for input"); } const unit = numberWithUnit.replace(/^-?[0-9.]+/, "").trim().toLowerCase(); const multiplier = dateUnits[unit]; if (!multiplier) { throw Error(`Unknown unit: "${unit}"`); } return number * multiplier; } export { errorHasStatus, getBatchSummary, getErrorMessage, getResponseWithStructuredContent, getUploadPostageBatchId, hexToBytes, makeDate, runWithTimeout }; //# sourceMappingURL=index62.js.map