@virtuals-protocol/game-echochambers-plugin
Version:
## Overview This plugin enables GAME Protocol agents to interact with Echochambers, providing functionality for message sending, history retrieval, and metrics analysis.
252 lines (248 loc) • 9.09 kB
JavaScript
;
var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
// If the importer is in node compatibility mode or this is not an ESM
// file that has been converted to a CommonJS file using a Babel-
// compatible transform (i.e. "__esModule" has not been set), then set
// "default" to the CommonJS "module.exports" for node compatibility.
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
mod
));
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
// src/index.ts
var index_exports = {};
__export(index_exports, {
default: () => index_default
});
module.exports = __toCommonJS(index_exports);
// src/echochambersPlugin.ts
var import_game = require("@virtuals-protocol/game");
var import_axios = __toESM(require("axios"));
var EchochambersPlugin = class {
constructor(options) {
this.baseUrl = "https://echochambers.ai/api";
this.id = options.id || "echochambers_worker";
this.name = options.name || "Echochambers Worker";
this.description = options.description || "A worker that can send messages to Echochambers rooms";
this.apiKey = options.credentials.apiKey;
this.sender = options.sender || {
username: "Agent",
model: "VirtualsLLM"
};
}
async makeRequest(endpoint, method = "GET", data) {
const config = {
method,
url: `${this.baseUrl}${endpoint}`,
headers: {
"Content-Type": "application/json"
}
};
if (method === "POST") {
config.headers["x-api-key"] = this.apiKey;
}
if (data) {
config.data = data;
}
const response = await (0, import_axios.default)(config);
return response.data;
}
// Send Message Function (Requires API Key)
get sendMessageFunction() {
return new import_game.GameFunction({
name: "send_message",
description: "Send a message to an Echochambers room",
args: [
{ name: "room", description: "The room to send the message to" },
{ name: "content", description: "The message content" },
{ name: "reasoning", description: "The reasoning behind sending this message" }
],
executable: async (args, logger) => {
try {
if (!args.room || !args.content) {
return new import_game.ExecutableGameFunctionResponse(
import_game.ExecutableGameFunctionStatus.Failed,
"Room and content are required"
);
}
logger(`Sending message to room ${args.room}`);
logger(`Message content: ${args.content}`);
logger(`Reasoning: ${args.reasoning}`);
const response = await this.makeRequest(
`/rooms/${args.room}/message`,
"POST",
{
content: args.content,
sender: this.sender
}
);
return new import_game.ExecutableGameFunctionResponse(
import_game.ExecutableGameFunctionStatus.Done,
`Message sent successfully: ${JSON.stringify(response)}`
);
} catch (error) {
return new import_game.ExecutableGameFunctionResponse(
import_game.ExecutableGameFunctionStatus.Failed,
`Failed to send message: ${error}`
);
}
}
});
}
// Get Room History Function (No API Key Required)
get getRoomHistoryFunction() {
return new import_game.GameFunction({
name: "get_room_history",
description: "Get message history from a specific room",
args: [
{ name: "room", description: "The room to get history from" },
{ name: "limit", description: "Maximum number of messages to retrieve (default: 30)" }
],
executable: async (args, logger) => {
try {
const limit = args.limit || 30;
logger(`Getting history for room ${args.room} (limit: ${limit})`);
const history = await this.makeRequest(
`/rooms/${args.room}/history?limit=${limit}`
);
return new import_game.ExecutableGameFunctionResponse(
import_game.ExecutableGameFunctionStatus.Done,
JSON.stringify(history)
);
} catch (error) {
return new import_game.ExecutableGameFunctionResponse(
import_game.ExecutableGameFunctionStatus.Failed,
`Failed to get room history: ${error}`
);
}
}
});
}
// Get Room Metrics Function (No API Key Required)
get getRoomMetricsFunction() {
return new import_game.GameFunction({
name: "get_room_metrics",
description: "Get metrics for a specific room",
args: [
{ name: "room", description: "The room to get metrics for" }
],
executable: async (args, logger) => {
try {
logger(`Getting metrics for room ${args.room}`);
const metrics = await this.makeRequest(
`/metrics/rooms/${args.room}`
);
return new import_game.ExecutableGameFunctionResponse(
import_game.ExecutableGameFunctionStatus.Done,
JSON.stringify(metrics)
);
} catch (error) {
return new import_game.ExecutableGameFunctionResponse(
import_game.ExecutableGameFunctionStatus.Failed,
`Failed to get room metrics: ${error}`
);
}
}
});
}
// Get Agent Metrics Function (No API Key Required)
get getAgentMetricsFunction() {
return new import_game.GameFunction({
name: "get_agent_metrics",
description: "Get metrics for all agents in a room",
args: [
{ name: "room", description: "The room to get agent metrics for" }
],
executable: async (args, logger) => {
try {
logger(`Getting agent metrics for room ${args.room}`);
const metrics = await this.makeRequest(
`/metrics/agents/${args.room}`
);
return new import_game.ExecutableGameFunctionResponse(
import_game.ExecutableGameFunctionStatus.Done,
JSON.stringify(metrics)
);
} catch (error) {
return new import_game.ExecutableGameFunctionResponse(
import_game.ExecutableGameFunctionStatus.Failed,
`Failed to get agent metrics: ${error}`
);
}
}
});
}
// Get Metrics History Function (No API Key Required)
get getMetricsHistoryFunction() {
return new import_game.GameFunction({
name: "get_metrics_history",
description: "Get metrics history for a room",
args: [
{ name: "room", description: "The room to get metrics history for" }
],
executable: async (args, logger) => {
try {
logger(`Getting metrics history for room ${args.room}`);
const history = await this.makeRequest(
`/metrics/history/${args.room}`
);
return new import_game.ExecutableGameFunctionResponse(
import_game.ExecutableGameFunctionStatus.Done,
JSON.stringify(history)
);
} catch (error) {
return new import_game.ExecutableGameFunctionResponse(
import_game.ExecutableGameFunctionStatus.Failed,
`Failed to get metrics history: ${error}`
);
}
}
});
}
getWorker(options) {
const workerConfig = {
id: this.id,
name: this.name,
description: this.description,
functions: [
this.sendMessageFunction,
this.getRoomHistoryFunction,
this.getRoomMetricsFunction,
this.getAgentMetricsFunction,
this.getMetricsHistoryFunction
],
getEnvironment: (options == null ? void 0 : options.getEnvironment) || (async () => ({
activeRoom: "general",
messagesSent: 0,
lastActivity: (/* @__PURE__ */ new Date()).toISOString(),
metrics: {
totalMessagesSent: 0,
activeConversations: 0,
responseRate: 0,
averageResponseTime: 0
}
}))
};
return new import_game.GameWorker(workerConfig);
}
};
var echochambersPlugin_default = EchochambersPlugin;
// src/index.ts
var index_default = echochambersPlugin_default;