agent-sync/export_shared_agents.js
2025-10-21 07:36:11 -05:00

132 lines
3.4 KiB
JavaScript
Executable file

// export_shared_agents.js
// Emits strict Extended JSON to stdout (safe JSON for ObjectId/Date)
const pipeline = [
{ $match: { resourceType: "agent" } },
{ $group: { _id: "$resourceId", count: { $sum: 1 } } },
{ $match: { count: { $gt: 1 } } },
{
$lookup: {
from: "agents",
localField: "_id",
foreignField: "_id",
as: "agentDetails"
}
},
{ $unwind: "$agentDetails" },
{
$lookup: {
from: "users",
localField: "agentDetails.author",
foreignField: "_id",
as: "authorDetails"
}
},
{ $unwind: { path: "$authorDetails", preserveNullAndEmptyArrays: true } },
{ $set: { "agentDetails.author": "$authorDetails.email" } },
// NEW: Get usage timeline - daily message counts
{
$lookup: {
from: "conversations",
let: { agentId: "$agentDetails.id" },
pipeline: [
{
$match: {
$expr: { $eq: ["$agent_id", "$$agentId"] },
endpoint: "agents"
}
},
// Unwind messages array to get individual message ObjectIds
{ $unwind: "$messages" },
// Lookup actual message documents
{
$lookup: {
from: "messages",
localField: "messages",
foreignField: "_id",
as: "messageDoc"
}
},
{ $unwind: "$messageDoc" },
// Group by date to count messages per day
{
$group: {
_id: {
$dateToString: {
format: "%Y-%m-%d",
date: "$messageDoc.createdAt"
}
},
messageCount: { $sum: 1 }
}
},
// Sort by date
{ $sort: { _id: 1 } },
// Reshape to nice format
{
$project: {
_id: 0,
date: "$_id",
message_count: "$messageCount"
}
}
],
as: "usageTimeline"
}
},
// NEW: Get usage summary stats
{
$lookup: {
from: "conversations",
let: { agentId: "$agentDetails.id" },
pipeline: [
{
$match: {
$expr: { $eq: ["$agent_id", "$$agentId"] },
endpoint: "agents"
}
},
{
$group: {
_id: null,
conversationCount: { $sum: 1 },
uniqueUsers: { $addToSet: "$user" },
totalMessages: { $sum: { $size: "$messages" } },
firstUsed: { $min: "$createdAt" },
lastUsed: { $max: "$updatedAt" }
}
}
],
as: "usageSummary"
}
},
{ $unwind: { path: "$usageSummary", preserveNullAndEmptyArrays: true } },
// Add usage data to agentDetails
{
$set: {
"agentDetails.usage_timeline": "$usageTimeline",
"agentDetails.usage_summary": {
conversation_count: { $ifNull: ["$usageSummary.conversationCount", 0] },
unique_users: { $ifNull: [{ $size: "$usageSummary.uniqueUsers" }, 0] },
total_messages: { $ifNull: ["$usageSummary.totalMessages", 0] },
first_used: "$usageSummary.firstUsed",
last_used: "$usageSummary.lastUsed"
}
}
},
{ $project: {
"agentDetails.versions": 0,
"agentDetails.instructions": 0,
"authorDetails": 0,
"usageTimeline": 0,
"usageSummary": 0
}
}
];
const out = db.aclentries.aggregate(pipeline).toArray();
print(EJSON.stringify(out, { relaxed: false, indent: 2 }));