From ebd73e1c698b83f0e99fa12714e32c6648b0ec6d Mon Sep 17 00:00:00 2001 From: hotwa Date: Sun, 22 Mar 2026 16:02:40 +0800 Subject: [PATCH] feat: add macOS LaunchAgent install/uninstall scripts - Add install-launchagent.sh for auto-start on boot - Add uninstall-launchagent.sh for service removal - Update README.md with deployment instructions Co-Authored-By: Claude Opus 4.6 --- README.md | 62 +++++++++++++++++++++++ install-launchagent.sh | 107 +++++++++++++++++++++++++++++++++++++++ uninstall-launchagent.sh | 37 ++++++++++++++ 3 files changed, 206 insertions(+) create mode 100755 install-launchagent.sh create mode 100755 uninstall-launchagent.sh diff --git a/README.md b/README.md index 3e67335..2f3403d 100755 --- a/README.md +++ b/README.md @@ -78,3 +78,65 @@ curl -s http://127.0.0.1:11435/api/chat \ npm run test \`\`\` 将会进行 `xml-toolcall` 解析用例和端到端整合测试。 + +--- + +## macOS 开机自启动部署 + +本项目提供了 LaunchAgent 安装脚本,可将代理服务配置为开机自动启动。 + +### 安装步骤 + +1. 确保已完成前面的「获取代码与安装」和「环境配置」步骤 + +2. 运行安装脚本: +\`\`\`bash +./install-launchagent.sh +\`\`\` + +3. 验证服务状态: +\`\`\`bash +# 检查服务是否运行 +launchctl list com.openclaw.ollama-proxy + +# 测试代理响应 +curl http://127.0.0.1:11435/ +\`\`\` + +### 常用命令 + +| 操作 | 命令 | +|------|------| +| 查看状态 | `launchctl list com.openclaw.ollama-proxy` | +| 停止服务 | `launchctl bootout gui/$(id -u) ~/Library/LaunchAgents/com.openclaw.ollama-proxy.plist` | +| 启动服务 | `launchctl bootstrap gui/$(id -u) ~/Library/LaunchAgents/com.openclaw.ollama-proxy.plist` | +| 查看日志 | `tail -f ~/Library/Logs/OpenClawOllamaProxy/stdout.log` | +| 查看错误日志 | `tail -f ~/Library/Logs/OpenClawOllamaProxy/stderr.log` | + +### 卸载服务 + +\`\`\`bash +./uninstall-launchagent.sh +\`\`\` + +### 多机器部署 + +如需在多台机器上部署,只需在每台机器上执行以下步骤: + +\`\`\`bash +# 1. 克隆代码 +git clone ssh://git@gitea.jmsu.top:2222/lingyuzeng/openclaw-ollama-toolcall-proxy.git +cd openclaw-ollama-toolcall-proxy + +# 2. 安装依赖 +npm install + +# 3. 配置环境变量 +cp .env.example .env +# 编辑 .env 配置目标 Ollama 地址 + +# 4. 安装开机自启动服务 +./install-launchagent.sh +\`\`\` + +日志位置:`~/Library/Logs/OpenClawOllamaProxy/` diff --git a/install-launchagent.sh b/install-launchagent.sh new file mode 100755 index 0000000..bcc9b17 --- /dev/null +++ b/install-launchagent.sh @@ -0,0 +1,107 @@ +#!/bin/bash + +# OpenClaw Ollama Toolcall Proxy - macOS LaunchAgent 安装脚本 +# 将服务配置为开机自动启动 + +set -e + +# 配置 +PROJECT_DIR="$(cd "$(dirname "$0")" && pwd)" +SERVICE_NAME="com.openclaw.ollama-proxy" +PLIST_PATH="$HOME/Library/LaunchAgents/${SERVICE_NAME}.plist" +LOG_DIR="$HOME/Library/Logs/OpenClawOllamaProxy" + +# 颜色输出 +RED='\033[0;31m' +GREEN='\033[0;32m' +YELLOW='\033[1;33m' +NC='\033[0m' # No Color + +echo -e "${GREEN}=== OpenClaw Ollama Proxy LaunchAgent 安装 ===${NC}" +echo "" + +# 检查 Node.js +if ! command -v node &> /dev/null; then + echo -e "${RED}错误: 未找到 Node.js,请先安装 Node.js${NC}" + exit 1 +fi + +# 获取 node 和 npx 的完整路径 +NODE_PATH="$(which node)" +NPX_PATH="$(which npx)" + +echo -e "${YELLOW}项目目录:${NC} $PROJECT_DIR" +echo -e "${YELLOW}Node 路径:${NC} $NODE_PATH" +echo -e "${YELLOW}NPX 路径:${NC} $NPX_PATH" +echo "" + +# 创建日志目录 +mkdir -p "$LOG_DIR" +echo -e "${GREEN}✓${NC} 创建日志目录: $LOG_DIR" + +# 构建 plist 文件 +cat > "$PLIST_PATH" << EOF + + + + + Label + ${SERVICE_NAME} + ProgramArguments + + ${NPX_PATH} + tsx + ${PROJECT_DIR}/src/index.ts + + WorkingDirectory + ${PROJECT_DIR} + RunAtLoad + + KeepAlive + + StandardOutPath + ${LOG_DIR}/stdout.log + StandardErrorPath + ${LOG_DIR}/stderr.log + EnvironmentVariables + + PATH + /opt/homebrew/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin + + + +EOF + +echo -e "${GREEN}✓${NC} 生成 plist: $PLIST_PATH" + +# 卸载旧服务(如果存在) +if launchctl list "$SERVICE_NAME" &> /dev/null; then + launchctl bootout gui/$(id -u) "$PLIST_PATH" 2>/dev/null || true + echo -e "${GREEN}✓${NC} 卸载旧服务" +fi + +# 加载新服务 +launchctl bootstrap gui/$(id -u) "$PLIST_PATH" +echo -e "${GREEN}✓${NC} 加载新服务" + +# 等待服务启动 +sleep 1 + +# 检查服务状态 +if launchctl list "$SERVICE_NAME" &> /dev/null; then + echo "" + echo -e "${GREEN}=== 安装成功 ===${NC}" + echo "" + echo "服务名称: $SERVICE_NAME" + echo "监听地址: http://0.0.0.0:11435" + echo "日志目录: $LOG_DIR" + echo "" + echo "常用命令:" + echo " 查看状态: launchctl list $SERVICE_NAME" + echo " 停止服务: launchctl bootout gui/\$(id -u) $PLIST_PATH" + echo " 启动服务: launchctl bootstrap gui/\$(id -u) $PLIST_PATH" + echo " 查看日志: tail -f $LOG_DIR/stdout.log" +else + echo -e "${RED}服务启动失败,请检查日志: $LOG_DIR/stderr.log${NC}" + exit 1 +fi diff --git a/uninstall-launchagent.sh b/uninstall-launchagent.sh new file mode 100755 index 0000000..5c2f0c0 --- /dev/null +++ b/uninstall-launchagent.sh @@ -0,0 +1,37 @@ +#!/bin/bash + +# OpenClaw Ollama Toolcall Proxy - macOS LaunchAgent 卸载脚本 + +set -e + +SERVICE_NAME="com.openclaw.ollama-proxy" +PLIST_PATH="$HOME/Library/LaunchAgents/${SERVICE_NAME}.plist" +LOG_DIR="$HOME/Library/Logs/OpenClawOllamaProxy" + +RED='\033[0;31m' +GREEN='\033[0;32m' +YELLOW='\033[1;33m' +NC='\033[0m' + +echo -e "${YELLOW}=== OpenClaw Ollama Proxy LaunchAgent 卸载 ===${NC}" +echo "" + +# 停止并卸载服务 +if launchctl list "$SERVICE_NAME" &> /dev/null; then + launchctl bootout gui/$(id -u) "$PLIST_PATH" + echo -e "${GREEN}✓${NC} 停止并卸载服务" +else + echo -e "${YELLOW}服务未运行${NC}" +fi + +# 删除 plist 文件 +if [ -f "$PLIST_PATH" ]; then + rm "$PLIST_PATH" + echo -e "${GREEN}✓${NC} 删除 plist 文件: $PLIST_PATH" +fi + +echo "" +echo -e "${GREEN}=== 卸载完成 ===${NC}" +echo "" +echo "日志目录保留在: $LOG_DIR" +echo "如需删除日志: rm -rf $LOG_DIR"