{
  lib,
  python310,
  fetchFromGitHub,
  fetchPypi,
}:

let
  python = python310;
  pythonPackages = python.pkgs;

  # Define custom packages that aren't in nixpkgs or need special handling
  browsergym = pythonPackages.buildPythonPackage rec {
    pname = "browsergym";
    version = "0.13.3";
    format = "pyproject"; # Use pyproject format instead of setuptools

    src = fetchPypi {
      inherit pname version;
      sha256 = "sha256-w+4qxBz3oTq+ceD5xjwos3/uNI3MZPoabSteUT+ZKeA="; # Replace with actual hash
    };

    nativeBuildInputs = with pythonPackages; [
      setuptools
      wheel
    ];

    # Add dependencies as needed
    propagatedBuildInputs = with pythonPackages; [
      gymnasium
      playwright
    ];

    doCheck = false;
  };

  browser-use = pythonPackages.buildPythonPackage rec {
    pname = "browser-use";
    version = "0.1.40";
    format = "pyproject"; # Use pyproject format instead of setuptools

    src = fetchFromGitHub {
      owner = "browser-use";
      repo = "browser-use";
      rev = "${version}"; # Adjust if the tag format is different
      sha256 = "sha256-C/HT0AxL52iO/JWrjQFGrosYxY3TWNBQfVNnbFrGfEw="; # Replace with actual hash
    };

    nativeBuildInputs = with pythonPackages; [
      setuptools
      wheel
    ];

    # Add dependencies as needed
    propagatedBuildInputs = with pythonPackages; [
      browsergym
    ];

    doCheck = false;
  };

  googlesearch-python = pythonPackages.buildPythonPackage rec {
    pname = "googlesearch-python";
    version = "1.3.0";
    format = "pyproject"; # Use pyproject format instead of setuptools

    src = fetchFromGitHub {
      owner = "Nv7-GitHub";
      repo = "googlesearch";
      rev = "v${version}"; # Adjust if the tag format is different
      sha256 = "sha256-b0nh9FZ1M/q21W1i22YRjXdPC1lY+55u+XkJj5PYa74="; # Replace with actual hash
    };

    nativeBuildInputs = with pythonPackages; [
      setuptools
      wheel
    ];

    doCheck = false;
  };

  baidusearch = pythonPackages.buildPythonPackage rec {
    pname = "baidusearch";
    version = "1.0.3";
    format = "pyproject"; # Use pyproject format instead of setuptools

    src = fetchPypi {
      inherit pname version;
      sha256 = "sha256-s0ZI3TNwqVPPDuX5qRZBVNRsJJBRjkRA00n/d/+HoHs="; # Replace with actual hash
    };

    nativeBuildInputs = with pythonPackages; [
      setuptools
      wheel
    ];

    doCheck = false;
  };

  duckduckgo_search = pythonPackages.buildPythonPackage rec {
    pname = "duckduckgo_search";
    version = "7.5.1";
    format = "pyproject"; # Use pyproject format instead of setuptools

    src = fetchPypi {
      inherit pname version;
      sha256 = "sha256-VNGL/Np1xIUjKoaFUr6hzHxS2CRypz8Md5FhPpjTITc="; # Replace with actual hash
    };

    nativeBuildInputs = with pythonPackages; [
      setuptools
      wheel
    ];

    doCheck = false;
  };

in
pythonPackages.buildPythonApplication rec {
  pname = "openmanus";
  version = "0.1.0";

  src = fetchFromGitHub {
    owner = "mannaandpoem";
    repo = "OpenManus";
    rev = "main"; # You might want to pin this to a specific commit or tag later
    sha256 = "sha256-hsUTQ7a56QgHHbP8Ji/Pp6HXkvW9Gi6EHqTqLCSy71o="; # Replace this with the actual hash after first attempt
  };

  format = "setuptools";

  propagatedBuildInputs = with pythonPackages; [
    # Standard packages from nixpkgs
    pydantic
    openai
    tenacity
    pyyaml
    loguru
    numpy
    datasets
    html2text
    gymnasium
    pillow
    uvicorn
    unidiff
    aiofiles
    pydantic-core
    colorama
    fastapi
    tiktoken
    playwright

    # Custom packages defined above
    browsergym
    browser-use
    googlesearch-python
    baidusearch
    duckduckgo_search
  ];

  # Override Python version requirement
  postPatch = ''
    substituteInPlace setup.py --replace "python_requires=\">=3.12\"" "python_requires=\">=3.10\""
  '';

  preBuild = ''
    # Ensure the config directory exists
    mkdir -p $out/config
    # Copy the example config to the output
    cp -r config/config.example.toml $out/config/
  '';

  postInstall = ''
        # Create a wrapped executable that sets up the environment correctly
        mkdir -p $out/bin
        cat > $out/bin/openmanus <<EOF
    #!/bin/sh
    cd \$(mktemp -d)
    cp -r $out/config .
    exec python $out/lib/python3.10/site-packages/main.py "\$@"
    EOF
        chmod +x $out/bin/openmanus
  '';

  # Skip tests as they might require network or specific API keys
  doCheck = false;

  meta = with lib; {
    description = "A versatile agent that can solve various tasks using multiple tools";
    homepage = "https://github.com/mannaandpoem/OpenManus";
    license = licenses.mit;
    maintainers = [ maintainers.yourusername ]; # Replace with your maintainer name if applicable
    platforms = platforms.all;
  };
}