@memberjunction/ai-agent-manager-actions
Version:
Agent Management actions for creating and managing AI agents in MemberJunction
167 lines • 7.22 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;
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.LoadGetAgentDetailsAction = exports.GetAgentDetailsAction = void 0;
const global_1 = require("@memberjunction/global");
const core_1 = require("@memberjunction/core");
const base_agent_management_action_1 = require("./base-agent-management.action");
const actions_1 = require("@memberjunction/actions");
/**
* Gets detailed information about a specific agent including its full hierarchy
* (sub-agents, sub-sub-agents, etc.) and associated actions at each level.
* This action is restricted to the Agent Manager agent only.
*
* @example
* ```typescript
* const result = await runAction({
* ActionName: 'Get Agent Details',
* Params: [
* { Name: 'AgentID', Value: 'agent-id' },
* { Name: 'IncludePrompts', Value: 'true' } // Optional, default false
* ]
* });
* // Returns AgentDetails object in output params
* ```
*/
let GetAgentDetailsAction = class GetAgentDetailsAction extends base_agent_management_action_1.BaseAgentManagementAction {
async InternalRunAction(params) {
try {
// Validate permission
const permissionError = await this.validateAgentManagerPermission(params);
if (permissionError)
return permissionError;
// Extract parameters
const agentIDResult = this.getStringParam(params, 'AgentID');
if (agentIDResult.error)
return agentIDResult.error;
const includePromptsResult = this.getBooleanParam(params, 'IncludePrompts', false);
const includePrompts = includePromptsResult.value ?? false;
// Load the agent
const agentResult = await this.loadAgent(agentIDResult.value, params.ContextUser);
if (agentResult.error)
return agentResult.error;
// Get full agent details including hierarchy
const agentDetails = await this.getAgentDetailsWithHierarchy(agentResult.agent, includePrompts, params.ContextUser);
// Add output parameter
params.Params.push({
Name: 'AgentDetails',
Value: agentDetails,
Type: 'Output'
});
return {
Success: true,
ResultCode: 'SUCCESS',
Message: `Successfully retrieved details for agent '${agentResult.agent.Name}'`,
Params: params.Params
};
}
catch (e) {
return this.handleError(e, 'get agent details');
}
}
/**
* Recursively builds the agent hierarchy with all sub-agents and their actions
*/
async getAgentDetailsWithHierarchy(agent, includePrompts, contextUser) {
const rv = new core_1.RunView();
// Build the views to run in parallel
const viewsToRun = [
{
EntityName: 'AI Agent Actions',
ExtraFilter: `AgentID = '${agent.ID}' AND Status = 'Active'`,
OrderBy: 'CreatedAt',
ResultType: 'entity_object'
},
{
EntityName: 'AI Agents',
ExtraFilter: `ParentID = '${agent.ID}'`,
OrderBy: 'ExecutionOrder, Name',
ResultType: 'entity_object'
}
];
// Add prompts view if requested
if (includePrompts) {
viewsToRun.push({
EntityName: 'AI Agent Prompts',
ExtraFilter: `AgentID = '${agent.ID}' AND Status = 'Active'`,
OrderBy: 'ExecutionOrder',
ResultType: 'entity_object'
});
}
// Run all views in parallel with a single database operation
const results = await rv.RunViews(viewsToRun, contextUser);
// Extract results
const actionsResult = results[0];
const subAgentsResult = results[1];
const promptsResult = includePrompts ? results[2] : null;
const actions = actionsResult.Success ? (actionsResult.Results || []) : [];
const subAgents = subAgentsResult.Success ? (subAgentsResult.Results || []) : [];
const prompts = promptsResult?.Success ? (promptsResult.Results || []) : [];
// Recursively get details for each sub-agent
const subAgentDetails = await Promise.all(subAgents.map(subAgent => this.getAgentDetailsWithHierarchy(subAgent, includePrompts, contextUser)));
// Build the result object
const details = {
ID: agent.ID,
Name: agent.Name,
Description: agent.Description,
Type: agent.Type,
Status: agent.Status,
ExecutionOrder: agent.ExecutionOrder,
ExposeAsAction: agent.ExposeAsAction,
ParentID: agent.ParentID,
Actions: actions.map(a => ({
ID: a.ID,
ActionID: a.ActionID,
ActionName: a.Action,
Status: a.Status
}))
};
if (includePrompts) {
details.Prompts = prompts.map(p => ({
ID: p.ID,
PromptID: p.PromptID,
PromptName: p.Prompt,
ExecutionOrder: p.ExecutionOrder,
Status: p.Status
}));
}
if (subAgentDetails.length > 0) {
details.SubAgents = subAgentDetails;
}
return details;
}
/**
* Helper to parse boolean parameters
*/
getBooleanParam(params, paramName, isRequired = true) {
const param = params.Params.find(p => p.Name === paramName);
if (!param) {
if (isRequired) {
return {
error: {
Success: false,
ResultCode: 'MISSING_PARAMETER',
Message: `Required parameter '${paramName}' not found`
}
};
}
return { value: undefined };
}
const value = param.Value === 'true' || param.Value === true || param.Value === '1' || param.Value === 1;
return { value };
}
};
exports.GetAgentDetailsAction = GetAgentDetailsAction;
exports.GetAgentDetailsAction = GetAgentDetailsAction = __decorate([
(0, global_1.RegisterClass)(actions_1.BaseAction, "Get Agent Details")
], GetAgentDetailsAction);
function LoadGetAgentDetailsAction() {
// This function exists to prevent tree shaking from removing the action class
}
exports.LoadGetAgentDetailsAction = LoadGetAgentDetailsAction;
//# sourceMappingURL=get-agent-details.action.js.map