Skip to content

SmythOS Studio: Open-Source Visual AI Agent Builder and deployable runtime stack from SmythOS. Start with an intuitive drag-and-drop workspace, extend with custom code, and deploy your agents anywhere — local, cloud, or edge — with full governance and control.

7.9 KB
Raw Blame
import { DuplicateAgentResponse, IAgent } from '@react/features/agents/components/agentCard/types';
import { accquireLock } from '@react/features/agents/utils';
import { Agent, AgentData } from '@src/react/shared/types/agent-data.types';
import { errorToast, successToast } from '@src/shared/components/toast';
import { builderStore } from '@src/shared/state_stores/builder/store';
import { useQueryClient } from '@tanstack/react-query';
import { useCallback } from 'react';


interface UseAgentOperationsProps {
  agent: IAgent;
  onAgentDeleted?: () => void;
  onAgentDuplicated?: () => void;
  onAgentPinned?: (updatedAgent: IAgent) => void;
}

interface UseAgentOperationsResult {
  duplicateAgent: () => Promise<void>;
  deleteAgent: () => Promise<void>;
  pinAgent: () => Promise<void>;
  isLoading: boolean;
}

interface SaveAgentResponse {
  success: boolean;
  agent: Agent;
}

interface CreateAgentResponse {
  id: string;
  name: string;
  success: boolean;
}

interface CreateAgent {
  name: string;
  behavior?: string;
  description?: string;
  domain?: string[];
  data?: AgentData;
}

export const useAgentMutations = () => {
  const queryClient = useQueryClient();

  const createAgent = async (agentData: CreateAgent): Promise<CreateAgentResponse> => {
    const response = await fetch('/api/agent', {
      method: 'POST',
      headers: { 'Content-Type': 'application/json' },
      body: JSON.stringify(agentData),
    });

    if (!response.ok) {
      throw new Error('Failed to create agent');
    }

    const data = await response.json();
    await queryClient.invalidateQueries(['agents']); // Invalidate agents list
    return data;
  };

  const saveAgent = async (
    agentId: string,
    agentData: Partial<Agent>,
  ): Promise<SaveAgentResponse> => {
    const response = await fetch(`/api/agent/${agentId}`, {
      method: 'PUT',
      headers: {
        'Content-Type': 'application/json',
      },
      body: JSON.stringify(agentData),
    });

    if (!response.ok) {
      throw new Error('Failed to save agent');
    }

    const data = await response.json();
    await queryClient.invalidateQueries(['agent_data', agentId]); // Invalidate specific agent data
    await queryClient.invalidateQueries(['agents']); // Invalidate agents list
    return data.agent;
  };

  return { createAgent, saveAgent };
};

/**
 * Custom hook for handling agent operations (duplicate, delete)
 */
export function useAgentOperations({
  agent,
  onAgentDeleted,
  onAgentDuplicated,
  onAgentPinned,
}: UseAgentOperationsProps): UseAgentOperationsResult {
  // Only fetch full agent data when actually needed for duplication
  // This prevents unnecessary API calls for every agent card
  const { createAgent } = useAgentMutations();

  /**
   * Creates a duplicate of an existing agent with reset configurations
   */
  const createDuplicateAgent = useCallback(async (): Promise<DuplicateAgentResponse> => {
    try {
      // Fetch full agent data only when duplication is actually triggered
      const response = await fetch(`/api/agent/${agent.id}`);
      if (!response.ok) {
        throw new Error('Failed to fetch agent details');
      }

      const { agent: fullAgentData } = await response.json();

      if (!fullAgentData) {
        return {
          success: false,
          message: 'Unable to fetch agent details',
        };
      }

      const initialData = {
        description: fullAgentData.description || '',
        components: [],
        connections: [],
      };

      const newAgent = await createAgent({
        name: `Copy of ${fullAgentData.name}`,
        description: fullAgentData.description,
        data: fullAgentData.data || initialData,
      });

      if (!newAgent?.id) {
        return {
          success: false,
          message: 'Failed to create new agent',
        };
      }

      // Generate avatar for the new agent (non-blocking)
      const { generateAgentAvatar } = builderStore.getState();
      const avatarGenerated = await generateAgentAvatar(newAgent.id);
      if (!avatarGenerated) {
        console.warn('Avatar generation failed for duplicated agent');
      }

      return {
        success: true,
        message: 'Agent duplicated successfully',
        agentId: newAgent.id,
      };
    } catch (error) {
      console.error('Failed to duplicate agent:', error);
      return {
        success: false,
        message: 'Failed to duplicate agent',
      };
    }
  }, [agent.id, createAgent]);

  /**
   * Handles the duplication process and UI feedback
   */
  const duplicateAgent = useCallback(async (): Promise<void> => {
    try {
      const result = await createDuplicateAgent();

      if (result.success) {
        successToast(result.message);
        onAgentDuplicated?.();
      } else {
        errorToast(result.message);
      }
    } catch (error) {
      console.error('Failed to duplicate agent:', error);
      errorToast('Failed to duplicate agent');
    }
  }, [createDuplicateAgent, onAgentDuplicated]);

  /**
   * Handles agent deletion with proper error handling
   */
  const deleteAgent = useCallback(async (): Promise<void> => {
    const id = agent.id;

    try {
      // Acquire lock before deletion
      const lockResult = await accquireLock(id);
      if (!lockResult?.lockId) {
        throw new Error('Failed to acquire lock');
      }
    } catch (error: unknown) {
      console.error('Lock acquisition failed:', error);

      if (error && typeof error === 'object' && 'status' in error && error.status === 403) {
        errorToast('You do not have access to delete this agent.');
      } else if (
        error &&
        typeof error === 'object' &&
        'error' in error &&
        error.error === 'Request failed with status code 409'
      ) {
        errorToast(
          'Failed to delete agent as the agent is being edited by another user. Please try again later.',
        );
      } else {
        errorToast('Unable to delete agent. Please try again later.');
      }
      return;
    }

    try {
      const response = await fetch(`/api/agent/${id}`, {
        method: 'DELETE',
      });

      const result = await response.json();

      if (result.success) {
        successToast('Agent deleted successfully');
        onAgentDeleted?.();
      } else {
        errorToast('Failed to delete agent');
      }
    } catch (error) {
      console.error('Failed to delete agent:', error);
      errorToast('Failed to delete agent');
    }
  }, [agent.id, onAgentDeleted]);

  /**
   * Handles agent pin/unpin with proper error handling
   */
  const pinAgent = useCallback(async (): Promise<void> => {
    const id = agent.id;
    const newPinnedState = !agent.isPinned;
    const actionText = newPinnedState ? 'pin' : 'unpin';

    try {
      const endpoint = `/api/page/agents/ai-agent/${id}/pin`;
      const method = newPinnedState ? 'POST' : 'DELETE';

      const response = await fetch(endpoint, {
        method,
        headers: {
          'Content-Type': 'application/json',
        },
      });

      if (!response.ok) {
        // Try to get error message from response
        const errorData = await response.json().catch(() => null);
        const errorMessage = errorData?.error || `Failed to ${actionText} agent. Please try again.`;
        throw new Error(errorMessage);
      }

      const updatedAgent: IAgent = {
        ...agent,
        isPinned: newPinnedState,
      };

      successToast(`Agent ${newPinnedState ? 'pinned' : 'unpinned'} successfully`);
      // Update the agent in place instead of reloading the entire list
      onAgentPinned?.(updatedAgent);
    } catch (error) {
      console.error('Failed to pin/unpin agent:', error);

      // Check if error has a message, if not use generic error
      if (error && typeof error === 'object' && 'message' in error) {
        errorToast(error.message);
      } else {
        errorToast(`Failed to ${actionText} agent. Please try again.`);
      }
    }
  }, [agent, onAgentPinned]);

  return {
    duplicateAgent,
    deleteAgent,
    pinAgent,
    isLoading: false,
  };
}