@hashgraphonline/conversational-agent
Version:
Hashgraph Online conversational AI agent implementing HCS-10 communication, HCS-2 registries, and content inscription on Hedera. https://hol.org
118 lines (114 loc) ⢠3.47 kB
JavaScript
class ResponseFormatter {
/**
* Checks if a parsed response contains HashLink block data for interactive rendering
*/
static isHashLinkResponse(parsed) {
if (!parsed || typeof parsed !== "object") {
return false;
}
const responseObj = parsed;
return !!(responseObj.success === true && responseObj.type === "inscription" && responseObj.hashLinkBlock && typeof responseObj.hashLinkBlock === "object");
}
/**
* Formats HashLink block response with simple text confirmation
* HTML template rendering is handled by HashLinkBlockRenderer component via metadata
*/
static formatHashLinkResponse(parsed) {
const hashLinkBlock = parsed.hashLinkBlock;
const metadata = parsed.metadata || {};
const inscription = parsed.inscription || {};
let message = "ā
Interactive content created successfully!\n\n";
if (metadata.name) {
message += `**${metadata.name}**
`;
}
if (metadata.description) {
message += `${metadata.description}
`;
}
if (inscription.topicId || hashLinkBlock.attributes.topicId) {
message += `š **Topic ID:** ${inscription.topicId || hashLinkBlock.attributes.topicId}
`;
}
if (inscription.hrl || hashLinkBlock.attributes.hrl) {
message += `š **HRL:** ${inscription.hrl || hashLinkBlock.attributes.hrl}
`;
}
if (inscription.cdnUrl) {
message += `š **CDN URL:** ${inscription.cdnUrl}
`;
}
if (metadata.creator) {
message += `š¤ **Creator:** ${metadata.creator}
`;
}
message += "\nā” Interactive content will load below";
return message.trim();
}
/**
* Checks if a parsed response is an inscription response that needs formatting
*/
static isInscriptionResponse(parsed) {
if (!parsed || typeof parsed !== "object") {
return false;
}
const responseObj = parsed;
return !!(responseObj.success === true && responseObj.type === "inscription" && responseObj.inscription && typeof responseObj.inscription === "object");
}
/**
* Formats inscription response into user-friendly message
*/
static formatInscriptionResponse(parsed) {
const inscription = parsed.inscription;
const metadata = parsed.metadata || {};
const title = parsed.title || "Inscription Complete";
let message = `ā
${title}
`;
if (metadata.name) {
message += `**${metadata.name}**
`;
}
if (metadata.description) {
message += `${metadata.description}
`;
}
if (inscription.topicId) {
message += `š **Topic ID:** ${inscription.topicId}
`;
}
if (inscription.hrl) {
message += `š **HRL:** ${inscription.hrl}
`;
}
if (inscription.cdnUrl) {
message += `š **CDN URL:** ${inscription.cdnUrl}
`;
}
if (metadata.creator) {
message += `š¤ **Creator:** ${metadata.creator}
`;
}
return message.trim();
}
/**
* Main formatting method that determines the best response format
*/
static formatResponse(toolOutput) {
try {
const parsed = JSON.parse(toolOutput);
if (ResponseFormatter.isHashLinkResponse(parsed)) {
return ResponseFormatter.formatHashLinkResponse(parsed);
}
if (ResponseFormatter.isInscriptionResponse(parsed)) {
return ResponseFormatter.formatInscriptionResponse(parsed);
}
return toolOutput;
} catch {
return toolOutput;
}
}
}
export {
ResponseFormatter
};
//# sourceMappingURL=index36.js.map