@defikitdotnet/x-ai-combat
Version:
XCombatAI - Social Media Engagement Template for the Agent Framework
143 lines • 6.24 kB
JavaScript
;
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __metadata = (this && this.__metadata) || function (k, v) {
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.XAICombatInteractionController = void 0;
const agent_framework_backend_1 = require("@defikitdotnet/agent-framework-backend");
const XAIAdapter_1 = require("../db/XAIAdapter");
const redis_service_1 = require("../services/redis-service");
/**
* Interaction API controller
*/
let XAICombatInteractionController = class XAICombatInteractionController extends agent_framework_backend_1.ResourceController {
constructor(adapter) {
super({
name: "interactions",
findAll: async () => [],
findById: async () => null,
create: async () => null,
update: async () => null,
delete: async () => false
});
this.adapter = adapter;
// Initialize Redis service with a default agent ID
// Use environment variable or fallback to a default value
const agentId = process.env.DEFAULT_AGENT_ID || 'default';
this.redisService = new redis_service_1.RedisService(agentId);
}
async getMyStats(req, res) {
try {
const userId = req.XaicombatUser?.userId;
if (!userId) {
res.status(401).json({ error: "Unauthorized: User not authenticated" });
return;
}
// Get agentId from JWT token
const agentId = req.XaicombatUser?.agentId;
const { posts } = await this.adapter.getPostsByUserId(userId, 1, 1000, agentId);
// Initialize stats with fame points
const stats = {
likes: 0,
retweets: 0,
quotes: 0,
replies: 0,
total: 0,
famePoints: 0
};
// Combine stats from all posts for this user
posts.forEach(post => {
if (post.stats) {
stats.likes += post.stats.likes || 0;
stats.retweets += post.stats.retweets || 0;
stats.quotes += post.stats.quotes || 0;
stats.replies += post.stats.replies || 0;
stats.total += post.stats.total || 0;
stats.famePoints += post.stats.famePoints || 0;
}
});
// Round fame points to 1 decimal place
stats.famePoints = Math.round(stats.famePoints * 10) / 10;
res.status(200).json({ data: stats });
}
catch (error) {
console.error("Error retrieving user statistics:", error);
res.status(500).json({ error: "Failed to retrieve user statistics" });
}
}
/**
* Get leaderboard of users based on fame points
*/
async getLeaderboard(req, res) {
try {
const limit = parseInt(req.query.limit) || 10;
const period = req.query.period || 'all';
const agentId = req.query.agentId;
// Try to get cached leaderboard data first
const cachedData = await this.redisService.getCachedLeaderboard(period, limit, agentId);
if (cachedData) {
res.status(200).json({
...cachedData,
cached: true
});
return;
}
// If no cache hit, fetch fresh data
const leaderboardData = await this.adapter.getLeaderboard(period, limit, agentId);
const responseData = {
data: leaderboardData.users,
totalUsers: leaderboardData.totalUsers,
lastUpdated: new Date().toISOString(),
period,
periodLabel: this.getPeriodLabel(period)
};
// Cache the leaderboard data for 5 minutes
await this.redisService.cacheLeaderboard(period, limit, responseData, agentId);
res.status(200).json(responseData);
}
catch (error) {
console.error("Error retrieving leaderboard:", error);
res.status(500).json({ error: "Failed to retrieve leaderboard" });
}
}
/**
* Get period label for leaderboard
*/
getPeriodLabel(period) {
switch (period) {
case 'current_day':
return 'Today';
case 'current_week':
return 'This week';
case 'current_month':
return 'This month';
case 'all':
default:
return 'All time';
}
}
};
exports.XAICombatInteractionController = XAICombatInteractionController;
__decorate([
(0, agent_framework_backend_1.Get)("/my-stats"),
__metadata("design:type", Function),
__metadata("design:paramtypes", [Object, Object]),
__metadata("design:returntype", Promise)
], XAICombatInteractionController.prototype, "getMyStats", null);
__decorate([
(0, agent_framework_backend_1.Get)("/leaderboard"),
__metadata("design:type", Function),
__metadata("design:paramtypes", [Object, Object]),
__metadata("design:returntype", Promise)
], XAICombatInteractionController.prototype, "getLeaderboard", null);
exports.XAICombatInteractionController = XAICombatInteractionController = __decorate([
(0, agent_framework_backend_1.Controller)("/xaicombat/api/interactions"),
__metadata("design:paramtypes", [XAIAdapter_1.XAIAdapter])
], XAICombatInteractionController);
//# sourceMappingURL=InteractionController.js.map