openclaw-watcher 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.claude/settings.local.json +7 -0
- package/.dockerignore +21 -0
- package/.env.example +31 -0
- package/.eslintrc.json +26 -0
- package/.prettierrc.json +9 -0
- package/CHANGELOG.md +93 -0
- package/Dockerfile +47 -0
- package/README.md +408 -0
- package/build.sh +33 -0
- package/dist/ai/ai-orchestrator.d.ts +11 -0
- package/dist/ai/ai-orchestrator.d.ts.map +1 -0
- package/dist/ai/ai-orchestrator.js +85 -0
- package/dist/ai/ai-orchestrator.js.map +1 -0
- package/dist/ai/cli-client.d.ts +17 -0
- package/dist/ai/cli-client.d.ts.map +1 -0
- package/dist/ai/cli-client.js +239 -0
- package/dist/ai/cli-client.js.map +1 -0
- package/dist/cli.d.ts +3 -0
- package/dist/cli.d.ts.map +1 -0
- package/dist/cli.js +33 -0
- package/dist/cli.js.map +1 -0
- package/dist/commands/config.d.ts +7 -0
- package/dist/commands/config.d.ts.map +1 -0
- package/dist/commands/config.js +52 -0
- package/dist/commands/config.js.map +1 -0
- package/dist/commands/init.d.ts +6 -0
- package/dist/commands/init.d.ts.map +1 -0
- package/dist/commands/init.js +205 -0
- package/dist/commands/init.js.map +1 -0
- package/dist/commands/start.d.ts +6 -0
- package/dist/commands/start.d.ts.map +1 -0
- package/dist/commands/start.js +49 -0
- package/dist/commands/start.js.map +1 -0
- package/dist/commands/status.d.ts +2 -0
- package/dist/commands/status.d.ts.map +1 -0
- package/dist/commands/status.js +48 -0
- package/dist/commands/status.js.map +1 -0
- package/dist/config/default.d.ts +5 -0
- package/dist/config/default.d.ts.map +1 -0
- package/dist/config/default.js +22 -0
- package/dist/config/default.js.map +1 -0
- package/dist/healthcheck/gateway-monitor.d.ts +19 -0
- package/dist/healthcheck/gateway-monitor.d.ts.map +1 -0
- package/dist/healthcheck/gateway-monitor.js +116 -0
- package/dist/healthcheck/gateway-monitor.js.map +1 -0
- package/dist/healthcheck/health-checker.d.ts +11 -0
- package/dist/healthcheck/health-checker.d.ts.map +1 -0
- package/dist/healthcheck/health-checker.js +60 -0
- package/dist/healthcheck/health-checker.js.map +1 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +39 -0
- package/dist/index.js.map +1 -0
- package/dist/recovery/auto-fixer.d.ts +16 -0
- package/dist/recovery/auto-fixer.d.ts.map +1 -0
- package/dist/recovery/auto-fixer.js +162 -0
- package/dist/recovery/auto-fixer.js.map +1 -0
- package/dist/recovery/change-recorder.d.ts +8 -0
- package/dist/recovery/change-recorder.d.ts.map +1 -0
- package/dist/recovery/change-recorder.js +41 -0
- package/dist/recovery/change-recorder.js.map +1 -0
- package/dist/setup/config-initializer.d.ts +13 -0
- package/dist/setup/config-initializer.d.ts.map +1 -0
- package/dist/setup/config-initializer.js +46 -0
- package/dist/setup/config-initializer.js.map +1 -0
- package/dist/setup/config-loader.d.ts +9 -0
- package/dist/setup/config-loader.d.ts.map +1 -0
- package/dist/setup/config-loader.js +17 -0
- package/dist/setup/config-loader.js.map +1 -0
- package/dist/setup/git-initializer.d.ts +15 -0
- package/dist/setup/git-initializer.d.ts.map +1 -0
- package/dist/setup/git-initializer.js +189 -0
- package/dist/setup/git-initializer.js.map +1 -0
- package/dist/setup/safe-config-generator.d.ts +9 -0
- package/dist/setup/safe-config-generator.d.ts.map +1 -0
- package/dist/setup/safe-config-generator.js +85 -0
- package/dist/setup/safe-config-generator.js.map +1 -0
- package/dist/types/index.d.ts +60 -0
- package/dist/types/index.d.ts.map +1 -0
- package/dist/types/index.js +2 -0
- package/dist/types/index.js.map +1 -0
- package/dist/utils/executor.d.ts +17 -0
- package/dist/utils/executor.d.ts.map +1 -0
- package/dist/utils/executor.js +57 -0
- package/dist/utils/executor.js.map +1 -0
- package/dist/utils/git-manager.d.ts +14 -0
- package/dist/utils/git-manager.d.ts.map +1 -0
- package/dist/utils/git-manager.js +116 -0
- package/dist/utils/git-manager.js.map +1 -0
- package/dist/utils/github-cli.d.ts +9 -0
- package/dist/utils/github-cli.d.ts.map +1 -0
- package/dist/utils/github-cli.js +31 -0
- package/dist/utils/github-cli.js.map +1 -0
- package/dist/utils/logger.d.ts +4 -0
- package/dist/utils/logger.d.ts.map +1 -0
- package/dist/utils/logger.js +26 -0
- package/dist/utils/logger.js.map +1 -0
- package/dist/utils/paths.d.ts +6 -0
- package/dist/utils/paths.d.ts.map +1 -0
- package/dist/utils/paths.js +19 -0
- package/dist/utils/paths.js.map +1 -0
- package/docker-compose.yml +43 -0
- package/nodemon.json +9 -0
- package/package.json +59 -0
- package/prompts/fix-openclaw.md +202 -0
- package/scripts/setup.sh +105 -0
- package/src/ai/ai-orchestrator.ts +95 -0
- package/src/ai/cli-client.ts +296 -0
- package/src/cli.ts +40 -0
- package/src/commands/config.ts +57 -0
- package/src/commands/init.ts +239 -0
- package/src/commands/start.ts +75 -0
- package/src/commands/status.ts +79 -0
- package/src/config/default.ts +25 -0
- package/src/healthcheck/gateway-monitor.ts +137 -0
- package/src/healthcheck/health-checker.ts +71 -0
- package/src/index.ts +48 -0
- package/src/recovery/auto-fixer.ts +184 -0
- package/src/recovery/change-recorder.ts +46 -0
- package/src/setup/config-initializer.ts +63 -0
- package/src/setup/config-loader.ts +25 -0
- package/src/setup/git-initializer.ts +203 -0
- package/src/setup/safe-config-generator.ts +100 -0
- package/src/types/index.ts +67 -0
- package/src/utils/executor.ts +75 -0
- package/src/utils/git-manager.ts +121 -0
- package/src/utils/github-cli.ts +37 -0
- package/src/utils/logger.ts +39 -0
- package/src/utils/paths.ts +25 -0
- package/tsconfig.json +29 -0
package/.dockerignore
ADDED
package/.env.example
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
# OpenClaw Configuration
|
|
2
|
+
OPENCLAW_GATEWAY_URL=http://localhost:10002
|
|
3
|
+
OPENCLAW_HEALTH_ENDPOINT=/health
|
|
4
|
+
OPENCLAW_CONFIG_PATH=~/.openclaw
|
|
5
|
+
|
|
6
|
+
# Health Check Configuration
|
|
7
|
+
HEALTH_CHECK_INTERVAL=30000
|
|
8
|
+
HEALTH_CHECK_TIMEOUT=10000
|
|
9
|
+
MAX_RETRY_ATTEMPTS=3
|
|
10
|
+
FAILURE_THRESHOLD=3
|
|
11
|
+
|
|
12
|
+
# AI Configuration
|
|
13
|
+
# Choose 'claude' or 'kimi' - both use CLI tools (claude code / kimi code)
|
|
14
|
+
AI_PROVIDER=claude
|
|
15
|
+
# No API keys needed - uses CLI tools directly
|
|
16
|
+
|
|
17
|
+
# Recovery Protection
|
|
18
|
+
MAX_RECOVERY_RETRIES=3
|
|
19
|
+
RECOVERY_COOLDOWN_MS=300000
|
|
20
|
+
|
|
21
|
+
# Change Tracking
|
|
22
|
+
USE_GIT_TRACKING=true
|
|
23
|
+
GIT_COMMIT_MESSAGE_PREFIX=[AutoFix]
|
|
24
|
+
BACKUP_CONFIG_BEFORE_CHANGE=true
|
|
25
|
+
|
|
26
|
+
# Watcher Home Directory (config + logs)
|
|
27
|
+
# WATCHER_HOME=~/.openclaw-watcher
|
|
28
|
+
|
|
29
|
+
# Logging
|
|
30
|
+
LOG_LEVEL=info
|
|
31
|
+
# LOG_FILE_PATH defaults to $WATCHER_HOME/logs/healthcheck.log
|
package/.eslintrc.json
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
{
|
|
2
|
+
"parser": "@typescript-eslint/parser",
|
|
3
|
+
"extends": [
|
|
4
|
+
"eslint:recommended",
|
|
5
|
+
"plugin:@typescript-eslint/recommended"
|
|
6
|
+
],
|
|
7
|
+
"plugins": ["@typescript-eslint"],
|
|
8
|
+
"parserOptions": {
|
|
9
|
+
"ecmaVersion": 2022,
|
|
10
|
+
"sourceType": "module",
|
|
11
|
+
"project": "./tsconfig.json"
|
|
12
|
+
},
|
|
13
|
+
"env": {
|
|
14
|
+
"node": true,
|
|
15
|
+
"es2022": true
|
|
16
|
+
},
|
|
17
|
+
"rules": {
|
|
18
|
+
"@typescript-eslint/no-explicit-any": "warn",
|
|
19
|
+
"@typescript-eslint/explicit-function-return-type": "off",
|
|
20
|
+
"@typescript-eslint/no-unused-vars": [
|
|
21
|
+
"error",
|
|
22
|
+
{ "argsIgnorePattern": "^_" }
|
|
23
|
+
],
|
|
24
|
+
"no-console": "warn"
|
|
25
|
+
}
|
|
26
|
+
}
|
package/.prettierrc.json
ADDED
package/CHANGELOG.md
ADDED
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
# Changelog
|
|
2
|
+
|
|
3
|
+
All notable changes to this project will be documented in this file.
|
|
4
|
+
|
|
5
|
+
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
|
|
6
|
+
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
|
|
7
|
+
|
|
8
|
+
## [2.0.0] - 2026-02-01
|
|
9
|
+
|
|
10
|
+
### 🚀 Major Architecture Upgrade
|
|
11
|
+
|
|
12
|
+
**核心改变**:AI 直接修复问题,而不是返回诊断建议
|
|
13
|
+
|
|
14
|
+
### Breaking Changes
|
|
15
|
+
|
|
16
|
+
- ❌ 移除 API 调用方式(Claude API、Kimi API)
|
|
17
|
+
- ✅ 改用 CLI 工具(Claude Code CLI、Kimi Code CLI)
|
|
18
|
+
- ❌ 移除配置项:`CLAUDE_API_KEY`、`KIMI_API_KEY`
|
|
19
|
+
- ✅ 新增配置项:`AI_TIMEOUT`(默认 5 分钟)
|
|
20
|
+
|
|
21
|
+
### Added
|
|
22
|
+
|
|
23
|
+
#### AI 直接修复能力
|
|
24
|
+
- 🤖 AI 使用完整工具集(Read、Edit、Write、Bash)直接修复问题
|
|
25
|
+
- ⚡ 迭代式修复:AI 会持续尝试直到问题解决
|
|
26
|
+
- ✅ AI 自主验证:修复后自动验证是否成功
|
|
27
|
+
- 📊 标准化摘要:AI 输出规范化的修复报告
|
|
28
|
+
|
|
29
|
+
#### 统一 System Prompt
|
|
30
|
+
- 📝 Claude Code 和 Kimi Code 共用同一个 System Prompt
|
|
31
|
+
- 🎯 保证两个 AI 的行为一致性
|
|
32
|
+
- 🔧 可自定义修复策略和指导方针
|
|
33
|
+
|
|
34
|
+
#### 增强的变更追踪
|
|
35
|
+
- 📝 AI 修复后自动提交到 Git
|
|
36
|
+
- 📊 JSON 日志记录完整的修复过程
|
|
37
|
+
- 🔍 包含:问题、原因、修复措施、修改文件、执行命令
|
|
38
|
+
|
|
39
|
+
### Changed
|
|
40
|
+
|
|
41
|
+
#### 工作流程优化
|
|
42
|
+
```
|
|
43
|
+
旧: 健康检查 → AI 返回诊断 → 程序解析 → 程序执行 → 可能失败
|
|
44
|
+
新: 健康检查 → AI 直接修复 → AI 验证 → AI 总结 → 程序记录 → 完成
|
|
45
|
+
```
|
|
46
|
+
|
|
47
|
+
#### 架构调整
|
|
48
|
+
- `ai/claude-client.ts` → `ai/cli-client.ts`(统一 CLI 客户端)
|
|
49
|
+
- `ai/kimi-client.ts` → 删除(合并到 cli-client.ts)
|
|
50
|
+
- `recovery/auto-fixer.ts` → 简化为只记录 AI 已完成的修复
|
|
51
|
+
- `recovery/config-manager.ts` → 已弃用(AI 直接修改配置)
|
|
52
|
+
|
|
53
|
+
### Improved
|
|
54
|
+
|
|
55
|
+
- 🎯 修复成功率提升:AI 可以迭代尝试多次
|
|
56
|
+
- ⚡ 修复速度提升:不需要往返通信
|
|
57
|
+
- 🔒 安全性提升:Git 完整记录所有变更
|
|
58
|
+
- 📊 可观察性提升:标准化的修复摘要
|
|
59
|
+
|
|
60
|
+
### Documentation
|
|
61
|
+
|
|
62
|
+
- ✨ 更新 README.md:强调 AI 直接修复能力
|
|
63
|
+
- 📖 更新 QUICKSTART.md:简化配置步骤
|
|
64
|
+
- 🎓 新增 `prompts/fix-openclaw.md`:AI System Prompt
|
|
65
|
+
- 🔧 更新所有示例和文档
|
|
66
|
+
|
|
67
|
+
---
|
|
68
|
+
|
|
69
|
+
## [1.0.0] - 2026-02-01
|
|
70
|
+
|
|
71
|
+
### Added(已废弃,见 v2.0.0)
|
|
72
|
+
|
|
73
|
+
- 🎉 Initial release(原始版本)
|
|
74
|
+
- ✨ 基于诊断 + 程序执行的修复方案(已废弃)
|
|
75
|
+
- 🤖 Claude CLI / Kimi API 集成(API 方式已废弃)
|
|
76
|
+
|
|
77
|
+
### Configuration
|
|
78
|
+
|
|
79
|
+
- Environment-based configuration
|
|
80
|
+
- Support for custom OpenClaw paths
|
|
81
|
+
- Configurable AI provider selection
|
|
82
|
+
- Adjustable health check parameters
|
|
83
|
+
- Git tracking toggle
|
|
84
|
+
- Backup system toggle
|
|
85
|
+
|
|
86
|
+
### Documentation
|
|
87
|
+
|
|
88
|
+
- Comprehensive README with examples
|
|
89
|
+
- Architecture diagrams
|
|
90
|
+
- Configuration guide
|
|
91
|
+
- Security considerations
|
|
92
|
+
- Troubleshooting guide
|
|
93
|
+
- Development setup instructions
|
package/Dockerfile
ADDED
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
FROM node:22-alpine AS builder
|
|
2
|
+
|
|
3
|
+
# Install pnpm
|
|
4
|
+
RUN npm install -g pnpm
|
|
5
|
+
|
|
6
|
+
WORKDIR /app
|
|
7
|
+
|
|
8
|
+
# Copy package files
|
|
9
|
+
COPY package.json pnpm-lock.yaml* ./
|
|
10
|
+
|
|
11
|
+
# Install dependencies
|
|
12
|
+
RUN pnpm install --frozen-lockfile
|
|
13
|
+
|
|
14
|
+
# Copy source code
|
|
15
|
+
COPY . .
|
|
16
|
+
|
|
17
|
+
# Build
|
|
18
|
+
RUN pnpm run build
|
|
19
|
+
|
|
20
|
+
# Production image
|
|
21
|
+
FROM node:22-alpine
|
|
22
|
+
|
|
23
|
+
# Install pnpm
|
|
24
|
+
RUN npm install -g pnpm
|
|
25
|
+
|
|
26
|
+
WORKDIR /app
|
|
27
|
+
|
|
28
|
+
# Copy package files
|
|
29
|
+
COPY package.json pnpm-lock.yaml* ./
|
|
30
|
+
|
|
31
|
+
# Install production dependencies only
|
|
32
|
+
RUN pnpm install --prod --frozen-lockfile
|
|
33
|
+
|
|
34
|
+
# Copy built files
|
|
35
|
+
COPY --from=builder /app/dist ./dist
|
|
36
|
+
|
|
37
|
+
# Create logs directory
|
|
38
|
+
RUN mkdir -p /app/logs
|
|
39
|
+
|
|
40
|
+
# Set environment
|
|
41
|
+
ENV NODE_ENV=production
|
|
42
|
+
|
|
43
|
+
# Expose port (if needed for metrics)
|
|
44
|
+
# EXPOSE 9090
|
|
45
|
+
|
|
46
|
+
# Run the service
|
|
47
|
+
CMD ["node", "dist/index.js"]
|
package/README.md
ADDED
|
@@ -0,0 +1,408 @@
|
|
|
1
|
+
# OpenClaw Watcher 🛡️
|
|
2
|
+
|
|
3
|
+
AI-powered automated health monitoring and self-healing CLI tool for OpenClaw Gateway.
|
|
4
|
+
|
|
5
|
+
[](https://www.npmjs.com/package/openclaw-watcher)
|
|
6
|
+
[](https://opensource.org/licenses/MIT)
|
|
7
|
+
|
|
8
|
+
## 🌟 What is OpenClaw Watcher?
|
|
9
|
+
|
|
10
|
+
OpenClaw Watcher is a CLI tool that automatically monitors your OpenClaw Gateway and uses AI (Claude Code or Kimi Code) to **directly diagnose and fix** issues when they occur. No more manual interventions or 3am wake-up calls!
|
|
11
|
+
|
|
12
|
+
### Key Features
|
|
13
|
+
|
|
14
|
+
- 🔍 **Automated Health Monitoring**: Continuous health checks of your OpenClaw Gateway
|
|
15
|
+
- 🤖 **AI-Powered Repair**: AI directly fixes issues using complete tool capabilities
|
|
16
|
+
- ⚡ **Iterative Fixing**: AI keeps trying until the problem is resolved
|
|
17
|
+
- 🔒 **Secure Config Management**: Automatic Git tracking with sensitive data protection
|
|
18
|
+
- 📊 **Complete Audit Trail**: Full history of all AI repairs in files
|
|
19
|
+
|
|
20
|
+
## 🚀 Quick Start
|
|
21
|
+
|
|
22
|
+
### Prerequisites
|
|
23
|
+
|
|
24
|
+
- Node.js >= 22
|
|
25
|
+
- OpenClaw Gateway installed
|
|
26
|
+
- **Claude Code CLI** or **Kimi Code CLI** (at least one)
|
|
27
|
+
|
|
28
|
+
```bash
|
|
29
|
+
# Install Claude Code CLI (recommended)
|
|
30
|
+
# https://github.com/anthropics/claude-code
|
|
31
|
+
|
|
32
|
+
# OR install Kimi Code CLI
|
|
33
|
+
# https://platform.moonshot.cn/docs/code
|
|
34
|
+
```
|
|
35
|
+
|
|
36
|
+
### Installation & Setup
|
|
37
|
+
|
|
38
|
+
```bash
|
|
39
|
+
# Initialize (interactive setup)
|
|
40
|
+
npx openclaw-watcher init
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
The interactive setup will guide you through:
|
|
44
|
+
|
|
45
|
+
1. OpenClaw configuration directory
|
|
46
|
+
2. Gateway URL and port
|
|
47
|
+
3. Health check settings
|
|
48
|
+
4. AI provider selection
|
|
49
|
+
5. Git repository initialization
|
|
50
|
+
|
|
51
|
+
### Start Monitoring
|
|
52
|
+
|
|
53
|
+
```bash
|
|
54
|
+
# Start monitoring
|
|
55
|
+
npx openclaw-watcher start
|
|
56
|
+
```
|
|
57
|
+
|
|
58
|
+
### Check Status
|
|
59
|
+
|
|
60
|
+
```bash
|
|
61
|
+
# View current status
|
|
62
|
+
npx openclaw-watcher status
|
|
63
|
+
```
|
|
64
|
+
|
|
65
|
+
## 📖 Full Documentation
|
|
66
|
+
|
|
67
|
+
- **[CLI Guide](./CLI_GUIDE.md)** - Complete CLI command reference
|
|
68
|
+
- **[Architecture](./ARCHITECTURE.md)** - System design and architecture
|
|
69
|
+
- **[Quick Start (Chinese)](./QUICKSTART.md)** - 中文快速开始指南
|
|
70
|
+
|
|
71
|
+
## 🎯 How It Works
|
|
72
|
+
|
|
73
|
+
```
|
|
74
|
+
┌─────────────────┐
|
|
75
|
+
│ Health Check │ Every 30s (configurable)
|
|
76
|
+
└────────┬────────┘
|
|
77
|
+
│ Fails 3x (configurable)
|
|
78
|
+
▼
|
|
79
|
+
┌─────────────────┐
|
|
80
|
+
│ AI Triggered │ Claude/Kimi Code CLI
|
|
81
|
+
└────────┬────────┘
|
|
82
|
+
│
|
|
83
|
+
▼
|
|
84
|
+
┌─────────────────┐
|
|
85
|
+
│ AI Diagnosis │ Read configs, logs, status
|
|
86
|
+
└────────┬────────┘
|
|
87
|
+
│
|
|
88
|
+
▼
|
|
89
|
+
┌─────────────────┐
|
|
90
|
+
│ AI Repairs │ Edit files, run commands
|
|
91
|
+
└────────┬────────┘
|
|
92
|
+
│
|
|
93
|
+
▼
|
|
94
|
+
┌─────────────────┐
|
|
95
|
+
│ AI Verifies │ Restart, check status
|
|
96
|
+
└────────┬────────┘
|
|
97
|
+
│
|
|
98
|
+
▼
|
|
99
|
+
┌─────────────────┐
|
|
100
|
+
│ Git Commit │ Auto-commit changes
|
|
101
|
+
└────────┬────────┘
|
|
102
|
+
│ (if GitHub sync enabled)
|
|
103
|
+
▼
|
|
104
|
+
┌─────────────────┐
|
|
105
|
+
│ GitHub Push │ Best-effort, never blocks
|
|
106
|
+
└─────────────────┘
|
|
107
|
+
```
|
|
108
|
+
|
|
109
|
+
## 🔒 Security Features
|
|
110
|
+
|
|
111
|
+
### Safe Configuration Management
|
|
112
|
+
|
|
113
|
+
OpenClaw Watcher creates two configuration files:
|
|
114
|
+
|
|
115
|
+
- `openclaw.json` - Real config with secrets (**NOT tracked in Git**)
|
|
116
|
+
- `openclaw.safe.json` - Template with redacted secrets (**tracked in Git**)
|
|
117
|
+
|
|
118
|
+
Example:
|
|
119
|
+
|
|
120
|
+
```json
|
|
121
|
+
// openclaw.json (real, gitignored)
|
|
122
|
+
{
|
|
123
|
+
"apiKey": "sk-ant-api03-xxx-real-key-xxx",
|
|
124
|
+
"token": "actual-secret-token"
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
// openclaw.safe.json (tracked in Git)
|
|
128
|
+
{
|
|
129
|
+
"apiKey": "<YOUR_API_KEY_HERE>",
|
|
130
|
+
"token": "<YOUR_TOKEN_HERE>"
|
|
131
|
+
}
|
|
132
|
+
```
|
|
133
|
+
|
|
134
|
+
### Pre-commit Hook
|
|
135
|
+
|
|
136
|
+
Automatically prevents:
|
|
137
|
+
|
|
138
|
+
- ❌ Committing `openclaw.json`
|
|
139
|
+
- ❌ Committing actual API keys/tokens in `openclaw.safe.json`
|
|
140
|
+
- ❌ Accidental sensitive data leaks
|
|
141
|
+
|
|
142
|
+
### Automatic Git Workflow
|
|
143
|
+
|
|
144
|
+
Every AI repair:
|
|
145
|
+
|
|
146
|
+
1. ✅ Updates `openclaw.safe.json` from `openclaw.json`
|
|
147
|
+
2. ✅ Redacts all sensitive data
|
|
148
|
+
3. ✅ Runs security checks
|
|
149
|
+
4. ✅ Commits to Git with detailed message
|
|
150
|
+
|
|
151
|
+
## 📋 CLI Commands
|
|
152
|
+
|
|
153
|
+
### `init` - Initialize
|
|
154
|
+
|
|
155
|
+
```bash
|
|
156
|
+
npx openclaw-watcher init [options]
|
|
157
|
+
```
|
|
158
|
+
|
|
159
|
+
Options:
|
|
160
|
+
|
|
161
|
+
- `--no-git` - Skip Git repository initialization
|
|
162
|
+
|
|
163
|
+
### `start` - Start Monitoring
|
|
164
|
+
|
|
165
|
+
```bash
|
|
166
|
+
npx openclaw-watcher start [options]
|
|
167
|
+
```
|
|
168
|
+
|
|
169
|
+
Options:
|
|
170
|
+
|
|
171
|
+
- `-d, --daemon` - Run as daemon process
|
|
172
|
+
|
|
173
|
+
### `status` - Show Status
|
|
174
|
+
|
|
175
|
+
```bash
|
|
176
|
+
npx openclaw-watcher status
|
|
177
|
+
```
|
|
178
|
+
|
|
179
|
+
Shows:
|
|
180
|
+
|
|
181
|
+
- Current configuration
|
|
182
|
+
- Repair history
|
|
183
|
+
- Last repair details
|
|
184
|
+
|
|
185
|
+
### `config` - Manage Config
|
|
186
|
+
|
|
187
|
+
```bash
|
|
188
|
+
npx openclaw-watcher config [options]
|
|
189
|
+
```
|
|
190
|
+
|
|
191
|
+
Options:
|
|
192
|
+
|
|
193
|
+
- `-s, --show` - Display current configuration
|
|
194
|
+
- `-e, --edit` - Edit configuration file
|
|
195
|
+
|
|
196
|
+
## 🛠️ Configuration
|
|
197
|
+
|
|
198
|
+
After initialization, config and logs are stored in `~/.openclaw-watcher/` (customizable via `WATCHER_HOME` env var):
|
|
199
|
+
|
|
200
|
+
```json
|
|
201
|
+
{
|
|
202
|
+
"monitor": {
|
|
203
|
+
"gatewayUrl": "http://localhost:10002",
|
|
204
|
+
"healthEndpoint": "/health",
|
|
205
|
+
"checkInterval": 30000,
|
|
206
|
+
"failureThreshold": 3
|
|
207
|
+
},
|
|
208
|
+
"ai": {
|
|
209
|
+
"provider": "claude",
|
|
210
|
+
"timeout": 300000
|
|
211
|
+
},
|
|
212
|
+
"recovery": {
|
|
213
|
+
"useGitTracking": true,
|
|
214
|
+
"useGitHubCli": false,
|
|
215
|
+
"openclawConfigPath": "~/.openclaw",
|
|
216
|
+
"maxRecoveryRetries": 3,
|
|
217
|
+
"recoveryCooldownMs": 300000
|
|
218
|
+
}
|
|
219
|
+
}
|
|
220
|
+
```
|
|
221
|
+
|
|
222
|
+
## 📊 Example Scenario
|
|
223
|
+
|
|
224
|
+
### Problem: Gateway Crashes
|
|
225
|
+
|
|
226
|
+
```
|
|
227
|
+
1. Health check fails 3 times consecutively
|
|
228
|
+
2. AI is triggered
|
|
229
|
+
3. AI reads error logs: "Port 10002 already in use"
|
|
230
|
+
4. AI diagnoses: Port conflict
|
|
231
|
+
5. AI fixes: Changes port to 10003 in openclaw.json
|
|
232
|
+
6. AI restarts: openclaw gateway restart
|
|
233
|
+
7. AI verifies: Gateway is now healthy
|
|
234
|
+
8. System commits: Updates openclaw.safe.json, commits to Git
|
|
235
|
+
```
|
|
236
|
+
|
|
237
|
+
### Git Commit Message
|
|
238
|
+
|
|
239
|
+
```
|
|
240
|
+
[AutoFix] Gateway failed to start - Port conflict
|
|
241
|
+
|
|
242
|
+
Root Cause: Port 10002 was already in use by another process
|
|
243
|
+
Fix Applied: Changed gateway port from 10002 to 10003 in openclaw.json
|
|
244
|
+
|
|
245
|
+
|
|
246
|
+
Files Modified:
|
|
247
|
+
- openclaw.json: Updated port configuration
|
|
248
|
+
|
|
249
|
+
Commands Executed:
|
|
250
|
+
- openclaw gateway restart
|
|
251
|
+
|
|
252
|
+
Applied by: AI Auto-Fix System
|
|
253
|
+
```
|
|
254
|
+
|
|
255
|
+
## 🌐 Git Repository
|
|
256
|
+
|
|
257
|
+
Watcher manages the entire `~/.openclaw` directory as a Git repository — not just repair history, but also agent configurations and any tracked files. Sensitive data (API keys, sessions, cache) is automatically excluded via `.gitignore`.
|
|
258
|
+
|
|
259
|
+
```
|
|
260
|
+
~/.openclaw/
|
|
261
|
+
├── .git/
|
|
262
|
+
│ └── hooks/
|
|
263
|
+
│ └── pre-commit # Security hook
|
|
264
|
+
├── .gitignore # Git ignore rules
|
|
265
|
+
├── README.md # Auto-generated docs
|
|
266
|
+
├── openclaw.json # Real config (NOT tracked)
|
|
267
|
+
├── openclaw.safe.json # Safe template (tracked)
|
|
268
|
+
├── agents/*/agent/auth-profiles.json # API keys (NOT tracked)
|
|
269
|
+
├── agents/*/sessions/ # Session history (NOT tracked)
|
|
270
|
+
├── memory/*.sqlite # Vector index (NOT tracked)
|
|
271
|
+
└── cache/ # Cache (NOT tracked)
|
|
272
|
+
```
|
|
273
|
+
|
|
274
|
+
### .gitignore Coverage
|
|
275
|
+
|
|
276
|
+
The auto-generated `.gitignore` excludes sensitive and temporary files:
|
|
277
|
+
|
|
278
|
+
| Pattern | Reason |
|
|
279
|
+
|---------|--------|
|
|
280
|
+
| `openclaw.json` | Contains API keys and tokens |
|
|
281
|
+
| `agents/*/agent/auth-profiles.json` | Agent authentication credentials |
|
|
282
|
+
| `agents/*/sessions/` | Session history (large + privacy) |
|
|
283
|
+
| `memory/*.sqlite` | Vector index databases (large) |
|
|
284
|
+
| `cache/` | Temporary cache files |
|
|
285
|
+
| `*.log` | Log files |
|
|
286
|
+
|
|
287
|
+
### GitHub Sync (Optional)
|
|
288
|
+
|
|
289
|
+
You can optionally push the repository to a private GitHub repo using the [GitHub CLI](https://cli.github.com):
|
|
290
|
+
|
|
291
|
+
```bash
|
|
292
|
+
# During init, answer "Yes" to "Sync repair history to GitHub?"
|
|
293
|
+
npx openclaw-watcher init
|
|
294
|
+
```
|
|
295
|
+
|
|
296
|
+
**Requirements**: `gh` CLI installed and authenticated (`gh auth login`).
|
|
297
|
+
|
|
298
|
+
**How it works**:
|
|
299
|
+
|
|
300
|
+
- During `init`: creates a private GitHub repo and pushes the initial commit
|
|
301
|
+
- During monitoring: auto-pushes to GitHub after each repair commit (best-effort — push failures never block recovery)
|
|
302
|
+
- Repo name is auto-generated: `openclaw-config-<hostname>`
|
|
303
|
+
|
|
304
|
+
If `gh` is not installed or not authenticated during init, the setup warns and continues — you can always configure GitHub sync later.
|
|
305
|
+
|
|
306
|
+
### View Repair History
|
|
307
|
+
|
|
308
|
+
```bash
|
|
309
|
+
cd ~/.openclaw
|
|
310
|
+
git log --oneline --grep="\[AutoFix\]"
|
|
311
|
+
```
|
|
312
|
+
|
|
313
|
+
### Rollback a Bad Fix
|
|
314
|
+
|
|
315
|
+
```bash
|
|
316
|
+
cd ~/.openclaw
|
|
317
|
+
git log --oneline -5
|
|
318
|
+
git revert <commit-hash>
|
|
319
|
+
openclaw gateway restart
|
|
320
|
+
```
|
|
321
|
+
|
|
322
|
+
## 🎓 Advanced Usage
|
|
323
|
+
|
|
324
|
+
### Run as System Service (systemd)
|
|
325
|
+
|
|
326
|
+
```bash
|
|
327
|
+
# Create service file
|
|
328
|
+
sudo vim /etc/systemd/system/openclaw-watcher.service
|
|
329
|
+
```
|
|
330
|
+
|
|
331
|
+
```ini
|
|
332
|
+
[Unit]
|
|
333
|
+
Description=OpenClaw Watcher
|
|
334
|
+
After=network.target
|
|
335
|
+
|
|
336
|
+
[Service]
|
|
337
|
+
Type=simple
|
|
338
|
+
User=openclaw
|
|
339
|
+
WorkingDirectory=/path/to/project
|
|
340
|
+
ExecStart=/usr/bin/npx openclaw-watcher start
|
|
341
|
+
Restart=always
|
|
342
|
+
|
|
343
|
+
[Install]
|
|
344
|
+
WantedBy=multi-user.target
|
|
345
|
+
```
|
|
346
|
+
|
|
347
|
+
```bash
|
|
348
|
+
sudo systemctl daemon-reload
|
|
349
|
+
sudo systemctl enable openclaw-watcher
|
|
350
|
+
sudo systemctl start openclaw-watcher
|
|
351
|
+
```
|
|
352
|
+
|
|
353
|
+
### Multiple Instances
|
|
354
|
+
|
|
355
|
+
```bash
|
|
356
|
+
# Instance 1
|
|
357
|
+
cd ~/project-1
|
|
358
|
+
npx openclaw-watcher init # Configure for instance 1
|
|
359
|
+
npx openclaw-watcher start
|
|
360
|
+
|
|
361
|
+
# Instance 2
|
|
362
|
+
cd ~/project-2
|
|
363
|
+
npx openclaw-watcher init # Configure for instance 2
|
|
364
|
+
npx openclaw-watcher start
|
|
365
|
+
```
|
|
366
|
+
|
|
367
|
+
## ❓ FAQ
|
|
368
|
+
|
|
369
|
+
### Q: What if AI can't fix the issue?
|
|
370
|
+
|
|
371
|
+
A: The system has built-in retry protection:
|
|
372
|
+
|
|
373
|
+
- **Max retries**: After 3 consecutive failed recovery attempts (configurable via `maxRecoveryRetries`), the system stops retrying and logs a warning requesting manual intervention.
|
|
374
|
+
- **Cooldown**: After each recovery attempt (success or failure), a 5-minute cooldown period (configurable via `recoveryCooldownMs`) prevents rapid-fire retries.
|
|
375
|
+
- **Fallback**: If the primary AI provider (e.g. Claude) fails, the system automatically tries the fallback provider (e.g. Kimi) before counting it as a failure.
|
|
376
|
+
|
|
377
|
+
All attempts are logged in detail for manual investigation.
|
|
378
|
+
|
|
379
|
+
### Q: Is it safe to let AI modify configs?
|
|
380
|
+
|
|
381
|
+
A: Yes! All changes are:
|
|
382
|
+
|
|
383
|
+
- ✅ Committed to Git (easy rollback)
|
|
384
|
+
- ✅ Verified after fixing
|
|
385
|
+
- ✅ Logged in detail
|
|
386
|
+
- ✅ Protected by pre-commit hooks
|
|
387
|
+
|
|
388
|
+
### Q: Can I review changes before they're applied?
|
|
389
|
+
|
|
390
|
+
A: Currently no - AI applies fixes immediately. However, all changes are in Git, so you can review and rollback if needed.
|
|
391
|
+
|
|
392
|
+
### Q: Does it work with other services besides OpenClaw?
|
|
393
|
+
|
|
394
|
+
A: Currently designed specifically for OpenClaw. Contributions welcome for other services!
|
|
395
|
+
|
|
396
|
+
## 🤝 Contributing
|
|
397
|
+
|
|
398
|
+
Issues and Pull Requests are welcome!
|
|
399
|
+
|
|
400
|
+
## 📄 License
|
|
401
|
+
|
|
402
|
+
MIT License
|
|
403
|
+
|
|
404
|
+
## 🔗 Links
|
|
405
|
+
|
|
406
|
+
- [OpenClaw Documentation](https://openclaw.io)
|
|
407
|
+
- [Claude Code CLI](https://github.com/anthropics/claude-code)
|
|
408
|
+
- [Kimi Code CLI](https://platform.moonshot.cn/docs/code)
|
package/build.sh
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
#!/bin/bash
|
|
2
|
+
|
|
3
|
+
set -e
|
|
4
|
+
|
|
5
|
+
echo "🔨 Building OpenClaw Watcher..."
|
|
6
|
+
|
|
7
|
+
# Clean previous build
|
|
8
|
+
echo "Cleaning previous build..."
|
|
9
|
+
rm -rf dist
|
|
10
|
+
|
|
11
|
+
# Install dependencies
|
|
12
|
+
echo "Installing dependencies..."
|
|
13
|
+
pnpm install
|
|
14
|
+
|
|
15
|
+
# Type check
|
|
16
|
+
echo "Running type check..."
|
|
17
|
+
pnpm run type-check
|
|
18
|
+
|
|
19
|
+
# Build
|
|
20
|
+
echo "Building..."
|
|
21
|
+
pnpm run build
|
|
22
|
+
|
|
23
|
+
# Make CLI executable
|
|
24
|
+
echo "Setting executable permissions..."
|
|
25
|
+
chmod +x dist/cli.js
|
|
26
|
+
|
|
27
|
+
echo "✅ Build completed successfully!"
|
|
28
|
+
echo ""
|
|
29
|
+
echo "Usage:"
|
|
30
|
+
echo " npx . init # Initialize configuration"
|
|
31
|
+
echo " npx . start # Start monitoring"
|
|
32
|
+
echo " npx . status # Check status"
|
|
33
|
+
echo " npx . config # Manage configuration"
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { AIClientConfig, DiagnosisResult, RecoveryConfig } from '../types';
|
|
2
|
+
export declare class AIOrchestrator {
|
|
3
|
+
private primaryClient;
|
|
4
|
+
private fallbackClient?;
|
|
5
|
+
private configPath;
|
|
6
|
+
constructor(aiConfig: AIClientConfig, recoveryConfig: RecoveryConfig);
|
|
7
|
+
initialize(): Promise<void>;
|
|
8
|
+
diagnoseAndFix(error: string): Promise<DiagnosisResult>;
|
|
9
|
+
private fallbackDiagnosis;
|
|
10
|
+
}
|
|
11
|
+
//# sourceMappingURL=ai-orchestrator.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"ai-orchestrator.d.ts","sourceRoot":"","sources":["../../src/ai/ai-orchestrator.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,cAAc,EAAE,eAAe,EAAE,cAAc,EAAE,MAAM,SAAS,CAAC;AAE1E,qBAAa,cAAc;IACzB,OAAO,CAAC,aAAa,CAAY;IACjC,OAAO,CAAC,cAAc,CAAC,CAAY;IACnC,OAAO,CAAC,UAAU,CAAS;gBAEf,QAAQ,EAAE,cAAc,EAAE,cAAc,EAAE,cAAc;IAW9D,UAAU,IAAI,OAAO,CAAC,IAAI,CAAC;IA2B3B,cAAc,CAAC,KAAK,EAAE,MAAM,GAAG,OAAO,CAAC,eAAe,CAAC;YAoB/C,iBAAiB;CA2BhC"}
|