Next.js 前端 + FastAPI/LangChain 后端透明 agent平台 - 含 .gitignore, 敏感文件(sessions数据/.env/venv等)已排除 - 待后续添加 trajectory 自进化机制
142 lines
4.0 KiB
Python
142 lines
4.0 KiB
Python
"""
|
|
mini-OpenClaw 安装脚本
|
|
Python 版本 - 跨平台兼容
|
|
"""
|
|
|
|
import os
|
|
import subprocess
|
|
import sys
|
|
from pathlib import Path
|
|
|
|
def run_command(cmd, cwd=None, shell=True):
|
|
"""运行命令并返回结果"""
|
|
print(f"[执行] {cmd}")
|
|
try:
|
|
result = subprocess.run(
|
|
cmd,
|
|
cwd=cwd,
|
|
shell=shell,
|
|
capture_output=True,
|
|
text=True,
|
|
encoding='utf-8',
|
|
errors='ignore'
|
|
)
|
|
if result.returncode == 0:
|
|
print(f"[成功] 命令执行成功")
|
|
if result.stdout:
|
|
print(result.stdout[:500]) # 只显示前500字符
|
|
return True
|
|
else:
|
|
print(f"[失败] 返回码: {result.returncode}")
|
|
if result.stderr:
|
|
print(f"错误信息: {result.stderr[:500]}")
|
|
return False
|
|
except Exception as e:
|
|
print(f"[异常] {e}")
|
|
return False
|
|
|
|
def main():
|
|
print("=" * 60)
|
|
print("mini-OpenClaw 安装脚本")
|
|
print("=" * 60)
|
|
print()
|
|
|
|
# 获取项目根目录
|
|
root_dir = Path(__file__).parent
|
|
backend_dir = root_dir / "backend"
|
|
|
|
# 步骤 1: 创建后端虚拟环境
|
|
print("[1/4] 创建后端虚拟环境...")
|
|
venv_dir = backend_dir / "venv"
|
|
if venv_dir.exists():
|
|
print("[跳过] 虚拟环境已存在")
|
|
else:
|
|
if not run_command(f"python -m venv {venv_dir}"):
|
|
print("虚拟环境创建失败,请检查 Python 安装")
|
|
return False
|
|
|
|
# 步骤 2: 安装后端依赖
|
|
print()
|
|
print("[2/4] 安装后端依赖...")
|
|
|
|
# 确定激活脚本路径
|
|
if sys.platform == "win32":
|
|
activate_script = venv_dir / "Scripts" / "activate.bat"
|
|
pip_exe = venv_dir / "Scripts" / "pip.exe"
|
|
else:
|
|
activate_script = venv_dir / "bin" / "activate"
|
|
pip_exe = venv_dir / "bin" / "pip"
|
|
|
|
if not pip_exe.exists():
|
|
print(f"[错误] pip 不存在: {pip_exe}")
|
|
return False
|
|
|
|
# 升级 pip
|
|
run_command(
|
|
f'"{pip_exe}" install --upgrade pip -i https://pypi.tuna.tsinghua.edu.cn/simple',
|
|
cwd=backend_dir
|
|
)
|
|
|
|
# 安装依赖
|
|
requirements_file = backend_dir / "requirements.txt"
|
|
if not run_command(
|
|
f'"{pip_exe}" install -r "{requirements_file}" -i https://pypi.tuna.tsinghua.edu.cn/simple',
|
|
cwd=backend_dir
|
|
):
|
|
print("依赖安装失败")
|
|
return False
|
|
|
|
# 步骤 3: 验证后端安装
|
|
print()
|
|
print("[3/4] 验证后端安装...")
|
|
python_exe = venv_dir / "Scripts" / "python.exe" if sys.platform == "win32" else venv_dir / "bin" / "python"
|
|
|
|
packages = ["fastapi", "langchain", "llama_index"]
|
|
for pkg in packages:
|
|
if run_command(f'"{python_exe}" -c "import {pkg}; print(\'✓ {pkg}\')"', cwd=backend_dir):
|
|
print(f"[OK] {pkg}")
|
|
else:
|
|
print(f"[警告] {pkg} 导入失败")
|
|
|
|
# 步骤 4: 安装前端依赖
|
|
print()
|
|
print("[4/4] 安装前端依赖...")
|
|
node_modules = root_dir / "node_modules"
|
|
if node_modules.exists():
|
|
print("[跳过] node_modules 已存在")
|
|
else:
|
|
if not run_command(
|
|
"npm install --registry=https://registry.npmmirror.com",
|
|
cwd=root_dir
|
|
):
|
|
print("前端依赖安装失败")
|
|
return False
|
|
|
|
print()
|
|
print("=" * 60)
|
|
print("安装完成!")
|
|
print("=" * 60)
|
|
print()
|
|
print("启动方式:")
|
|
print(" 后端: start-backend.bat")
|
|
print(" 前端: start-frontend.bat")
|
|
print()
|
|
print("或者:")
|
|
print(" 后端: cd backend && venv\\Scripts\\activate.bat && uvicorn app:app --host 0.0.0.0 --port 8002")
|
|
print(" 前端: npm run dev")
|
|
|
|
return True
|
|
|
|
if __name__ == "__main__":
|
|
try:
|
|
success = main()
|
|
sys.exit(0 if success else 1)
|
|
except KeyboardInterrupt:
|
|
print("\n\n安装被用户中断")
|
|
sys.exit(1)
|
|
except Exception as e:
|
|
print(f"\n\n安装过程中发生错误: {e}")
|
|
import traceback
|
|
traceback.print_exc()
|
|
sys.exit(1)
|