@pod-protocol/cli
Version:
Command-line interface for PoD Protocol (Prompt or Die) AI Agent Communication Protocol
180 lines (179 loc) • 7.7 kB
JavaScript
import { PublicKey } from "@solana/web3.js";
import { createSpinner } from "../../utils/shared.js";
import { BaseChannelHandler } from "./base-handler.js";
import { ChannelDataHandler } from "./data-handler.js";
import { ChannelValidators } from "./validators.js";
import { ChannelDisplayer } from "./displayer.js";
export class ChannelHandlers extends BaseChannelHandler {
constructor(context) {
super(context);
this.displayer = new ChannelDisplayer();
}
async handleCreate(options) {
try {
const channelData = await ChannelDataHandler.prepareChannelData(options);
const spinner = createSpinner("Creating channel...");
const result = await this.executeWithSpinner("Creating channel...", () => this.context.client.createChannel(this.context.wallet, channelData), {
Name: channelData.name,
Description: channelData.description,
Visibility: channelData.visibility,
"Max Participants": channelData.maxParticipants.toString(),
"Fee per Message": `${channelData.feePerMessage} lamports`,
});
if (result) {
this.showSuccessWithTransaction(spinner, "Channel created successfully!", result, {
Name: channelData.name,
Description: channelData.description,
Visibility: channelData.visibility,
});
}
}
catch (error) {
this.handleError("create channel", error);
}
}
async handleInfo(channelId) {
try {
const channelPubkey = new PublicKey(channelId);
const spinner = createSpinner("Fetching channel information...");
const channelData = await this.context.client.getChannel(channelPubkey);
if (!channelData) {
spinner.fail("Channel not found");
return;
}
spinner.succeed("Channel information retrieved");
this.displayer.displayChannelInfo(channelData);
}
catch (error) {
this.handleError("fetch channel info", error);
}
}
async handleList(options) {
try {
const spinner = createSpinner("Fetching channels...");
const limit = parseInt(options.limit, 10) || 10;
let channels;
if (options.owner) {
channels = await this.context.client.getChannelsByCreator(this.context.wallet.publicKey, limit);
}
else {
const visibilityFilter = options.visibility
? options.visibility
: undefined;
channels = await this.context.client.getAllChannels(limit, visibilityFilter);
}
if (channels.length === 0) {
spinner.succeed("No channels found");
return;
}
spinner.succeed(`Found ${channels.length} channel(s)`);
this.displayer.displayChannelsList(channels);
}
catch (error) {
this.handleError("list channels", error);
}
}
async handleJoin(channelId) {
try {
const channelPubkey = new PublicKey(channelId);
const result = await this.executeWithSpinner("Joining channel...", () => this.context.client.joinChannel(this.context.wallet, channelPubkey), { Channel: channelId });
if (result) {
const spinner = createSpinner("");
this.showSuccessWithTransaction(spinner, "Successfully joined channel!", result);
}
}
catch (error) {
this.handleError("join channel", error);
}
}
async handleLeave(channelId) {
try {
const channelPubkey = new PublicKey(channelId);
const result = await this.executeWithSpinner("Leaving channel...", () => this.context.client.leaveChannel(this.context.wallet, channelPubkey), { Channel: channelId });
if (result) {
const spinner = createSpinner("");
this.showSuccessWithTransaction(spinner, "Successfully left channel!", result);
}
}
catch (error) {
this.handleError("leave channel", error);
}
}
async handleBroadcast(channelId, message, options) {
try {
ChannelValidators.validateMessage(message);
const channelPubkey = new PublicKey(channelId);
const messageType = ChannelValidators.parseMessageType(options.type);
const replyTo = options.replyTo
? new PublicKey(options.replyTo)
: undefined;
const dryRunData = {
Channel: channelId,
Message: message,
Type: messageType,
};
if (replyTo) {
dryRunData["Reply to"] = replyTo.toBase58();
}
const result = await this.executeWithSpinner("Broadcasting message...", () => this.context.client.broadcastMessage(this.context.wallet, channelPubkey, message, messageType, replyTo), dryRunData);
if (result) {
const spinner = createSpinner("");
this.showSuccessWithTransaction(spinner, "Message broadcast successfully!", result);
}
}
catch (error) {
this.handleError("broadcast message", error);
}
}
async handleInvite(channelId, invitee) {
try {
const channelPubkey = new PublicKey(channelId);
const inviteePubkey = new PublicKey(invitee);
const result = await this.executeWithSpinner("Sending invitation...", () => this.context.client.inviteToChannel(this.context.wallet, channelPubkey, inviteePubkey), {
Channel: channelId,
Invitee: invitee,
});
if (result) {
const spinner = createSpinner("");
this.showSuccessWithTransaction(spinner, "Invitation sent successfully!", result);
}
}
catch (error) {
this.handleError("send invitation", error);
}
}
async handleParticipants(channelId, options) {
try {
const channelPubkey = new PublicKey(channelId);
const limit = parseInt(options.limit, 10) || 20;
const spinner = createSpinner("Fetching participants...");
const participants = await this.context.client.getChannelParticipants(channelPubkey, limit);
if (participants.length === 0) {
spinner.succeed("No participants found");
return;
}
spinner.succeed(`Found ${participants.length} participant(s)`);
this.displayer.displayParticipantsList(participants);
}
catch (error) {
this.handleError("fetch participants", error);
}
}
async handleMessages(channelId, options) {
try {
const channelPubkey = new PublicKey(channelId);
const limit = parseInt(options.limit, 10) || 20;
const spinner = createSpinner("Fetching messages...");
const messages = await this.context.client.getChannelMessages(channelPubkey, limit);
if (messages.length === 0) {
spinner.succeed("No messages found");
return;
}
spinner.succeed(`Found ${messages.length} message(s)`);
this.displayer.displayMessagesList(messages);
}
catch (error) {
this.handleError("fetch messages", error);
}
}
}