Makefile 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  1. SHELL=/bin/bash
  2. # Makefile for OpenHands project
  3. # Variables
  4. BACKEND_PORT = 3000
  5. BACKEND_HOST = "127.0.0.1:$(BACKEND_PORT)"
  6. FRONTEND_PORT = 3001
  7. DEFAULT_WORKSPACE_DIR = "./workspace"
  8. DEFAULT_MODEL = "gpt-4o"
  9. CONFIG_FILE = config.toml
  10. PRE_COMMIT_CONFIG_PATH = "./dev_config/python/.pre-commit-config.yaml"
  11. PYTHON_VERSION = 3.11
  12. # ANSI color codes
  13. GREEN=$(shell tput -Txterm setaf 2)
  14. YELLOW=$(shell tput -Txterm setaf 3)
  15. RED=$(shell tput -Txterm setaf 1)
  16. BLUE=$(shell tput -Txterm setaf 6)
  17. RESET=$(shell tput -Txterm sgr0)
  18. # Build
  19. build:
  20. @echo "$(GREEN)Building project...$(RESET)"
  21. @$(MAKE) -s check-dependencies
  22. @$(MAKE) -s install-python-dependencies
  23. @$(MAKE) -s install-frontend-dependencies
  24. @$(MAKE) -s install-pre-commit-hooks
  25. @$(MAKE) -s build-frontend
  26. @echo "$(GREEN)Build completed successfully.$(RESET)"
  27. check-dependencies:
  28. @echo "$(YELLOW)Checking dependencies...$(RESET)"
  29. @$(MAKE) -s check-system
  30. @$(MAKE) -s check-python
  31. @$(MAKE) -s check-npm
  32. @$(MAKE) -s check-nodejs
  33. ifeq ($(INSTALL_DOCKER),)
  34. @$(MAKE) -s check-docker
  35. endif
  36. @$(MAKE) -s check-poetry
  37. @echo "$(GREEN)Dependencies checked successfully.$(RESET)"
  38. check-system:
  39. @echo "$(YELLOW)Checking system...$(RESET)"
  40. @if [ "$(shell uname)" = "Darwin" ]; then \
  41. echo "$(BLUE)macOS detected.$(RESET)"; \
  42. elif [ "$(shell uname)" = "Linux" ]; then \
  43. if [ -f "/etc/manjaro-release" ]; then \
  44. echo "$(BLUE)Manjaro Linux detected.$(RESET)"; \
  45. else \
  46. echo "$(BLUE)Linux detected.$(RESET)"; \
  47. fi; \
  48. elif [ "$$(uname -r | grep -i microsoft)" ]; then \
  49. echo "$(BLUE)Windows Subsystem for Linux detected.$(RESET)"; \
  50. else \
  51. echo "$(RED)Unsupported system detected. Please use macOS, Linux, or Windows Subsystem for Linux (WSL).$(RESET)"; \
  52. exit 1; \
  53. fi
  54. check-python:
  55. @echo "$(YELLOW)Checking Python installation...$(RESET)"
  56. @if command -v python$(PYTHON_VERSION) > /dev/null; then \
  57. echo "$(BLUE)$(shell python$(PYTHON_VERSION) --version) is already installed.$(RESET)"; \
  58. else \
  59. echo "$(RED)Python $(PYTHON_VERSION) is not installed. Please install Python $(PYTHON_VERSION) to continue.$(RESET)"; \
  60. exit 1; \
  61. fi
  62. check-npm:
  63. @echo "$(YELLOW)Checking npm installation...$(RESET)"
  64. @if command -v npm > /dev/null; then \
  65. echo "$(BLUE)npm $(shell npm --version) is already installed.$(RESET)"; \
  66. else \
  67. echo "$(RED)npm is not installed. Please install Node.js to continue.$(RESET)"; \
  68. exit 1; \
  69. fi
  70. check-nodejs:
  71. @echo "$(YELLOW)Checking Node.js installation...$(RESET)"
  72. @if command -v node > /dev/null; then \
  73. NODE_VERSION=$(shell node --version | sed -E 's/v//g'); \
  74. IFS='.' read -r -a NODE_VERSION_ARRAY <<< "$$NODE_VERSION"; \
  75. if [ "$${NODE_VERSION_ARRAY[0]}" -gt 18 ] || ([ "$${NODE_VERSION_ARRAY[0]}" -eq 18 ] && [ "$${NODE_VERSION_ARRAY[1]}" -gt 17 ]) || ([ "$${NODE_VERSION_ARRAY[0]}" -eq 18 ] && [ "$${NODE_VERSION_ARRAY[1]}" -eq 17 ] && [ "$${NODE_VERSION_ARRAY[2]}" -ge 1 ]); then \
  76. echo "$(BLUE)Node.js $$NODE_VERSION is already installed.$(RESET)"; \
  77. else \
  78. echo "$(RED)Node.js 18.17.1 or later is required. Please install Node.js 18.17.1 or later to continue.$(RESET)"; \
  79. exit 1; \
  80. fi; \
  81. else \
  82. echo "$(RED)Node.js is not installed. Please install Node.js to continue.$(RESET)"; \
  83. exit 1; \
  84. fi
  85. check-docker:
  86. @echo "$(YELLOW)Checking Docker installation...$(RESET)"
  87. @if command -v docker > /dev/null; then \
  88. echo "$(BLUE)$(shell docker --version) is already installed.$(RESET)"; \
  89. else \
  90. echo "$(RED)Docker is not installed. Please install Docker to continue.$(RESET)"; \
  91. exit 1; \
  92. fi
  93. check-poetry:
  94. @echo "$(YELLOW)Checking Poetry installation...$(RESET)"
  95. @if command -v poetry > /dev/null; then \
  96. POETRY_VERSION=$(shell poetry --version 2>&1 | sed -E 's/Poetry \(version ([0-9]+\.[0-9]+\.[0-9]+)\)/\1/'); \
  97. IFS='.' read -r -a POETRY_VERSION_ARRAY <<< "$$POETRY_VERSION"; \
  98. if [ $${POETRY_VERSION_ARRAY[0]} -ge 1 ] && [ $${POETRY_VERSION_ARRAY[1]} -ge 8 ]; then \
  99. echo "$(BLUE)$(shell poetry --version) is already installed.$(RESET)"; \
  100. else \
  101. echo "$(RED)Poetry 1.8 or later is required. You can install poetry by running the following command, then adding Poetry to your PATH:"; \
  102. echo "$(RED) curl -sSL https://install.python-poetry.org | python$(PYTHON_VERSION) -$(RESET)"; \
  103. echo "$(RED)More detail here: https://python-poetry.org/docs/#installing-with-the-official-installer$(RESET)"; \
  104. exit 1; \
  105. fi; \
  106. else \
  107. echo "$(RED)Poetry is not installed. You can install poetry by running the following command, then adding Poetry to your PATH:"; \
  108. echo "$(RED) curl -sSL https://install.python-poetry.org | python$(PYTHON_VERSION) -$(RESET)"; \
  109. echo "$(RED)More detail here: https://python-poetry.org/docs/#installing-with-the-official-installer$(RESET)"; \
  110. exit 1; \
  111. fi
  112. install-python-dependencies:
  113. @echo "$(GREEN)Installing Python dependencies...$(RESET)"
  114. @if [ -z "${TZ}" ]; then \
  115. echo "Defaulting TZ (timezone) to UTC"; \
  116. export TZ="UTC"; \
  117. fi
  118. poetry env use python$(PYTHON_VERSION)
  119. @if [ "$(shell uname)" = "Darwin" ]; then \
  120. echo "$(BLUE)Installing chroma-hnswlib...$(RESET)"; \
  121. export HNSWLIB_NO_NATIVE=1; \
  122. poetry run pip install chroma-hnswlib; \
  123. fi
  124. @poetry install --without llama-index
  125. @if [ -f "/etc/manjaro-release" ]; then \
  126. echo "$(BLUE)Detected Manjaro Linux. Installing Playwright dependencies...$(RESET)"; \
  127. poetry run pip install playwright; \
  128. poetry run playwright install chromium; \
  129. else \
  130. if [ ! -f cache/playwright_chromium_is_installed.txt ]; then \
  131. echo "Running playwright install --with-deps chromium..."; \
  132. poetry run playwright install --with-deps chromium; \
  133. mkdir -p cache; \
  134. touch cache/playwright_chromium_is_installed.txt; \
  135. else \
  136. echo "Setup already done. Skipping playwright installation."; \
  137. fi \
  138. fi
  139. @echo "$(GREEN)Python dependencies installed successfully.$(RESET)"
  140. install-frontend-dependencies:
  141. @echo "$(YELLOW)Setting up frontend environment...$(RESET)"
  142. @echo "$(YELLOW)Detect Node.js version...$(RESET)"
  143. @cd frontend && node ./scripts/detect-node-version.js
  144. echo "$(BLUE)Installing frontend dependencies with npm...$(RESET)"
  145. @cd frontend && npm install
  146. @echo "$(GREEN)Frontend dependencies installed successfully.$(RESET)"
  147. install-pre-commit-hooks:
  148. @echo "$(YELLOW)Installing pre-commit hooks...$(RESET)"
  149. @git config --unset-all core.hooksPath || true
  150. @poetry run pre-commit install --config $(PRE_COMMIT_CONFIG_PATH)
  151. @echo "$(GREEN)Pre-commit hooks installed successfully.$(RESET)"
  152. lint-backend:
  153. @echo "$(YELLOW)Running linters...$(RESET)"
  154. @poetry run pre-commit run --files openhands/**/* agenthub/**/* evaluation/**/* --show-diff-on-failure --config $(PRE_COMMIT_CONFIG_PATH)
  155. lint-frontend:
  156. @echo "$(YELLOW)Running linters for frontend...$(RESET)"
  157. @cd frontend && npm run lint
  158. lint:
  159. @$(MAKE) -s lint-frontend
  160. @$(MAKE) -s lint-backend
  161. test-frontend:
  162. @echo "$(YELLOW)Running tests for frontend...$(RESET)"
  163. @cd frontend && npm run test
  164. test:
  165. @$(MAKE) -s test-frontend
  166. build-frontend:
  167. @echo "$(YELLOW)Building frontend...$(RESET)"
  168. @cd frontend && npm run build
  169. # Start backend
  170. start-backend:
  171. @echo "$(YELLOW)Starting backend...$(RESET)"
  172. @poetry run uvicorn openhands.server.listen:app --port $(BACKEND_PORT) --reload --reload-exclude "workspace/*"
  173. # Start frontend
  174. start-frontend:
  175. @echo "$(YELLOW)Starting frontend...$(RESET)"
  176. @cd frontend && VITE_BACKEND_HOST=$(BACKEND_HOST) VITE_FRONTEND_PORT=$(FRONTEND_PORT) npm run start
  177. # Common setup for running the app (non-callable)
  178. _run_setup:
  179. @if [ "$(OS)" = "Windows_NT" ]; then \
  180. echo "$(RED) Windows is not supported, use WSL instead!$(RESET)"; \
  181. exit 1; \
  182. fi
  183. @mkdir -p logs
  184. @echo "$(YELLOW)Starting backend server...$(RESET)"
  185. @poetry run uvicorn openhands.server.listen:app --port $(BACKEND_PORT) &
  186. @echo "$(YELLOW)Waiting for the backend to start...$(RESET)"
  187. @until nc -z localhost $(BACKEND_PORT); do sleep 0.1; done
  188. @echo "$(GREEN)Backend started successfully.$(RESET)"
  189. # Run the app (standard mode)
  190. run:
  191. @echo "$(YELLOW)Running the app...$(RESET)"
  192. @$(MAKE) -s _run_setup
  193. @cd frontend && echo "$(BLUE)Starting frontend with npm...$(RESET)" && npm run start -- --port $(FRONTEND_PORT)
  194. @echo "$(GREEN)Application started successfully.$(RESET)"
  195. # Run the app (WSL mode)
  196. run-wsl:
  197. @echo "$(YELLOW)Running the app in WSL mode...$(RESET)"
  198. @$(MAKE) -s _run_setup
  199. @cd frontend && echo "$(BLUE)Starting frontend with npm (WSL mode)...$(RESET)" && npm run dev_wsl -- --port $(FRONTEND_PORT)
  200. @echo "$(GREEN)Application started successfully in WSL mode.$(RESET)"
  201. # Setup config.toml
  202. setup-config:
  203. @echo "$(YELLOW)Setting up config.toml...$(RESET)"
  204. @$(MAKE) setup-config-prompts
  205. @mv $(CONFIG_FILE).tmp $(CONFIG_FILE)
  206. @echo "$(GREEN)Config.toml setup completed.$(RESET)"
  207. setup-config-prompts:
  208. @echo "[core]" > $(CONFIG_FILE).tmp
  209. @read -p "Enter your workspace directory (as absolute path) [default: $(DEFAULT_WORKSPACE_DIR)]: " workspace_dir; \
  210. workspace_dir=$${workspace_dir:-$(DEFAULT_WORKSPACE_DIR)}; \
  211. echo "workspace_base=\"$$workspace_dir\"" >> $(CONFIG_FILE).tmp
  212. @echo "" >> $(CONFIG_FILE).tmp
  213. @echo "[llm]" >> $(CONFIG_FILE).tmp
  214. @read -p "Enter your LLM model name, used for running without UI. Set the model in the UI after you start the app. (see https://docs.litellm.ai/docs/providers for full list) [default: $(DEFAULT_MODEL)]: " llm_model; \
  215. llm_model=$${llm_model:-$(DEFAULT_MODEL)}; \
  216. echo "model=\"$$llm_model\"" >> $(CONFIG_FILE).tmp
  217. @read -p "Enter your LLM api key: " llm_api_key; \
  218. echo "api_key=\"$$llm_api_key\"" >> $(CONFIG_FILE).tmp
  219. @read -p "Enter your LLM base URL [mostly used for local LLMs, leave blank if not needed - example: http://localhost:5001/v1/]: " llm_base_url; \
  220. if [[ ! -z "$$llm_base_url" ]]; then echo "base_url=\"$$llm_base_url\"" >> $(CONFIG_FILE).tmp; fi
  221. @echo "Enter your LLM Embedding Model"; \
  222. echo "Choices are:"; \
  223. echo " - openai"; \
  224. echo " - azureopenai"; \
  225. echo " - Embeddings available only with OllamaEmbedding:"; \
  226. echo " - llama2"; \
  227. echo " - mxbai-embed-large"; \
  228. echo " - nomic-embed-text"; \
  229. echo " - all-minilm"; \
  230. echo " - stable-code"; \
  231. echo " - Leave blank to default to 'BAAI/bge-small-en-v1.5' via huggingface"; \
  232. read -p "> " llm_embedding_model; \
  233. echo "embedding_model=\"$$llm_embedding_model\"" >> $(CONFIG_FILE).tmp; \
  234. if [ "$$llm_embedding_model" = "llama2" ] || [ "$$llm_embedding_model" = "mxbai-embed-large" ] || [ "$$llm_embedding_model" = "nomic-embed-text" ] || [ "$$llm_embedding_model" = "all-minilm" ] || [ "$$llm_embedding_model" = "stable-code" ]; then \
  235. read -p "Enter the local model URL for the embedding model (will set llm.embedding_base_url): " llm_embedding_base_url; \
  236. echo "embedding_base_url=\"$$llm_embedding_base_url\"" >> $(CONFIG_FILE).tmp; \
  237. elif [ "$$llm_embedding_model" = "azureopenai" ]; then \
  238. read -p "Enter the Azure endpoint URL (will overwrite llm.base_url): " llm_base_url; \
  239. echo "base_url=\"$$llm_base_url\"" >> $(CONFIG_FILE).tmp; \
  240. read -p "Enter the Azure LLM Embedding Deployment Name: " llm_embedding_deployment_name; \
  241. echo "embedding_deployment_name=\"$$llm_embedding_deployment_name\"" >> $(CONFIG_FILE).tmp; \
  242. read -p "Enter the Azure API Version: " llm_api_version; \
  243. echo "api_version=\"$$llm_api_version\"" >> $(CONFIG_FILE).tmp; \
  244. fi
  245. # Clean up all caches
  246. clean:
  247. @echo "$(YELLOW)Cleaning up caches...$(RESET)"
  248. @rm -rf openhands/.cache
  249. @echo "$(GREEN)Caches cleaned up successfully.$(RESET)"
  250. # Help
  251. help:
  252. @echo "$(BLUE)Usage: make [target]$(RESET)"
  253. @echo "Targets:"
  254. @echo " $(GREEN)build$(RESET) - Build project, including environment setup and dependencies."
  255. @echo " $(GREEN)lint$(RESET) - Run linters on the project."
  256. @echo " $(GREEN)setup-config$(RESET) - Setup the configuration for OpenHands by providing LLM API key,"
  257. @echo " LLM Model name, and workspace directory."
  258. @echo " $(GREEN)start-backend$(RESET) - Start the backend server for the OpenHands project."
  259. @echo " $(GREEN)start-frontend$(RESET) - Start the frontend server for the OpenHands project."
  260. @echo " $(GREEN)run$(RESET) - Run the OpenHands application, starting both backend and frontend servers."
  261. @echo " Backend Log file will be stored in the 'logs' directory."
  262. @echo " $(GREEN)help$(RESET) - Display this help message, providing information on available targets."
  263. # Phony targets
  264. .PHONY: build check-dependencies check-python check-npm check-docker check-poetry install-python-dependencies install-frontend-dependencies install-pre-commit-hooks lint start-backend start-frontend run run-wsl setup-config setup-config-prompts help