agent-window 1.0.0

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.
@@ -0,0 +1,70 @@
1
+ {
2
+ "BOT_TOKEN": "your-discord-bot-token",
3
+ "CLAUDE_CODE_OAUTH_TOKEN": "your-claude-oauth-token",
4
+ "PROJECT_DIR": "/path/to/your/project",
5
+ "ALLOWED_CHANNELS": "channel-id-1,channel-id-2",
6
+
7
+ "_comment_backend": "=== Backend Configuration ===",
8
+ "backend": {
9
+ "type": "claude-code",
10
+ "configDir": "~/.claude"
11
+ },
12
+
13
+ "_comment_workspace": "=== Workspace Configuration ===",
14
+ "workspace": {
15
+ "containerName": "claude-discord-bot",
16
+ "dockerImage": "claude-sandbox",
17
+ "portMappings": ["5173:5173"]
18
+ },
19
+
20
+ "_comment_cli": "=== CLI Configuration ===",
21
+ "cli": {
22
+ "command": "claude",
23
+ "maxTurns": 50,
24
+ "taskTimeout": 3600000
25
+ },
26
+
27
+ "_comment_permissions": "=== Permission Configuration ===",
28
+ "permissions": {
29
+ "hookEnabled": true,
30
+ "pollInterval": 300,
31
+ "timeout": 120000
32
+ },
33
+
34
+ "_comment_docker": "=== Docker Configuration ===",
35
+ "docker": {
36
+ "checkTimeout": 5000,
37
+ "containerPaths": {
38
+ "workspace": "/workspace",
39
+ "configDir": "/home/claude/.claude",
40
+ "hookDir": "/permission-hook",
41
+ "pendingDir": "/pending-permissions",
42
+ "settingsFile": "/permission-hook/settings.json"
43
+ }
44
+ },
45
+
46
+ "_comment_ui": "=== UI Configuration ===",
47
+ "ui": {
48
+ "statusUpdateThrottle": 500,
49
+ "theme": {
50
+ "success": "0x9ece6a",
51
+ "error": "0xf7768e",
52
+ "info": "0x7aa2f7",
53
+ "warning": "0xf0ad4e"
54
+ }
55
+ },
56
+
57
+ "_comment_platform": "=== Platform Configuration (for multi-platform support) ===",
58
+ "platform": {
59
+ "type": "discord",
60
+ "messageMaxLength": 1900,
61
+ "retryDelay": 500,
62
+ "previewLengths": {
63
+ "command": 500,
64
+ "fileContent": 300,
65
+ "editPreview": 150,
66
+ "jsonInput": 400,
67
+ "response": 200
68
+ }
69
+ }
70
+ }
@@ -0,0 +1,31 @@
1
+ # AgentBridge - Docker Compose
2
+ #
3
+ # Usage:
4
+ # docker-compose up -d # Start in background
5
+ # docker-compose logs -f # View logs
6
+ # docker-compose down # Stop
7
+
8
+ version: '3.8'
9
+
10
+ services:
11
+ agent-bridge:
12
+ build:
13
+ context: .
14
+ dockerfile: Dockerfile
15
+ container_name: agent-bridge
16
+ restart: unless-stopped
17
+ volumes:
18
+ - ./config/config.json:/app/config/config.json:ro
19
+ - /var/run/docker.sock:/var/run/docker.sock
20
+ - ~/.claude:/root/.claude:ro
21
+ - ./hooks:/app/hooks:ro
22
+ - ./.pending-permissions:/app/.pending-permissions
23
+ - ${PROJECT_DIR:-./workspace}:/workspace
24
+ environment:
25
+ - NODE_ENV=production
26
+ networks:
27
+ - agent-bridge-network
28
+
29
+ networks:
30
+ agent-bridge-network:
31
+ driver: bridge
@@ -0,0 +1,174 @@
1
+ # Discord CLI Bot - 开发文档
2
+
3
+ ## 项目定位
4
+
5
+ 一个将 **AI 编程助手 CLI 工具**(Claude Code、Codex、OpenCode 等)接入 **Discord** 的通用框架。让团队成员可以通过 Discord 频道与 AI 编程助手交互,支持代码审查、自动化任务、项目协作等场景。
6
+
7
+ ## 项目目标
8
+
9
+ 1. **多 CLI 支持**:统一接口适配多种 AI CLI 工具
10
+ - Claude Code(已实现)
11
+ - OpenAI Codex CLI(待实现)
12
+ - OpenCode(待实现)
13
+ - 未来可扩展其他 CLI/API
14
+
15
+ 2. **安全隔离**:Docker 沙箱执行,CLI 只能访问指定项目目录
16
+
17
+ 3. **权限控制**:危险操作需用户通过 Discord 按钮审批
18
+
19
+ 4. **多实例部署**:支持多个独立 bot 实例,可配置不同服务器/频道
20
+
21
+ ## 当前状态
22
+
23
+ ### 已完成
24
+ - [x] Discord 消息交互(@mention 触发)
25
+ - [x] Docker 沙箱隔离执行
26
+ - [x] 权限审批系统(按钮 UI)
27
+ - [x] Session 管理(频道绑定、恢复对话)
28
+ - [x] 任务计时(排除权限等待时间)
29
+ - [x] 自动重连 + pm2 托管
30
+ - [x] Playwright 预装(支持浏览器自动化)
31
+ - [x] 端口映射(dev server 可从宿主机访问)
32
+ - [x] Bash 命令分类审批(安全命令自动通过)
33
+
34
+ ### 待实现
35
+ - [ ] 多 CLI 后端抽象层
36
+ - [ ] Codex CLI 适配器
37
+ - [ ] OpenCode CLI 适配器
38
+ - [ ] 多实例配置管理
39
+ - [ ] API 后端支持(预留)
40
+
41
+ ## 架构设计
42
+
43
+ ### 目标架构
44
+
45
+ ```
46
+ discord-cli-bot/
47
+ ├── core/ # 核心模块(共享)
48
+ │ ├── discord.js # Discord 客户端、消息路由
49
+ │ ├── permission.js # 权限审批 UI 系统
50
+ │ ├── session.js # Session 管理
51
+ │ └── status.js # 状态显示、计时
52
+ ├── backends/ # CLI 后端适配器
53
+ │ ├── base.js # 抽象基类接口
54
+ │ ├── claude-cli.js # Claude Code 适配器
55
+ │ ├── codex-cli.js # Codex CLI 适配器
56
+ │ └── opencode-cli.js # OpenCode 适配器
57
+ ├── sandbox/ # Docker 沙箱
58
+ │ ├── docker.js # 容器管理
59
+ │ └── Dockerfile # 基础镜像
60
+ ├── hooks/ # CLI 钩子
61
+ │ └── hook.mjs # PreToolUse 权限钩子
62
+ ├── config/ # 配置
63
+ │ └── config.json # 实例配置
64
+ ├── instances/ # 多实例配置(未来)
65
+ │ ├── claude.json
66
+ │ ├── codex.json
67
+ │ └── opencode.json
68
+ └── cli.js # 启动入口
69
+ ```
70
+
71
+ ### 后端适配器接口
72
+
73
+ ```javascript
74
+ // backends/base.js
75
+ class BaseBackend {
76
+ constructor(config) { }
77
+
78
+ // 启动 CLI 进程
79
+ async start(prompt, options) { }
80
+
81
+ // 解析 JSON 流事件
82
+ parseEvent(line) { }
83
+
84
+ // 恢复 session
85
+ async resume(sessionId, prompt) { }
86
+
87
+ // 获取 CLI 命令和参数
88
+ getCommand() { }
89
+ getArgs(options) { }
90
+ }
91
+ ```
92
+
93
+ ### CLI JSON 流格式对比
94
+
95
+ | 事件 | Claude Code | Codex CLI |
96
+ |-----|-------------|-----------|
97
+ | 开始 | `{"type":"system","subtype":"init"}` | `{"type":"thread.started"}` |
98
+ | 消息 | `{"type":"assistant","message":{...}}` | `{"type":"item.completed","item":{"type":"agent_message"}}` |
99
+ | 工具调用 | `{"type":"assistant","message":{"content":[{"type":"tool_use"}]}}` | `{"type":"item.started","item":{"type":"command_execution"}}` |
100
+ | 结果 | `{"type":"result"}` | `{"type":"turn.completed"}` |
101
+ | 恢复 | `--resume <id>` | `codex exec resume <id>` |
102
+
103
+ ## 配置说明
104
+
105
+ ### config/config.json
106
+
107
+ ```json
108
+ {
109
+ "BOT_TOKEN": "Discord Bot Token",
110
+ "CLAUDE_CODE_OAUTH_TOKEN": "Claude OAuth Token(可选,也可用 API Key)",
111
+ "PROJECT_DIR": "要操作的项目目录绝对路径",
112
+ "ALLOWED_CHANNELS": "允许的频道ID,逗号分隔"
113
+ }
114
+ ```
115
+
116
+ ### 环境变量(优先级低于配置文件)
117
+
118
+ - `DISCORD_BOT_TOKEN`
119
+ - `ANTHROPIC_API_KEY`
120
+
121
+ ## 开发指南
122
+
123
+ ### 添加新的 CLI 后端
124
+
125
+ 1. 在 `backends/` 创建适配器文件(如 `codex-cli.js`)
126
+ 2. 继承 `BaseBackend` 实现接口
127
+ 3. 处理 JSON 流事件映射
128
+ 4. 在配置中指定 `backend: "codex-cli"`
129
+
130
+ ### 权限钩子机制
131
+
132
+ Claude Code 支持 PreToolUse Hook,用于拦截工具调用:
133
+
134
+ ```
135
+ CLI 调用工具 → Hook 拦截 → 写入 .request.json →
136
+ Bot 检测到请求 → 发送 Discord 按钮 →
137
+ 用户点击 → 写入 .response.json → Hook 读取 →
138
+ CLI 继续执行
139
+ ```
140
+
141
+ 其他 CLI 如果不支持 Hook,需要用 `--auto-edit` 等模式或包装脚本。
142
+
143
+ ## 关键文件说明
144
+
145
+ | 文件 | 说明 |
146
+ |-----|-----|
147
+ | `src/bot.js` | 主程序,包含 Discord 交互、权限处理、Docker 执行 |
148
+ | `hooks/hook.mjs` | Claude PreToolUse 钩子,处理权限请求 |
149
+ | `sandbox/Dockerfile` | Docker 镜像,预装 Playwright 和依赖 |
150
+ | `config/config.json` | 实例配置(不提交到 git) |
151
+
152
+ ## 运行方式
153
+
154
+ ```bash
155
+ # 开发模式
156
+ node src/bot.js
157
+
158
+ # 生产模式(pm2 托管)
159
+ pm2 start src/bot.js --name discord-bot --exp-backoff-restart-delay=1000
160
+
161
+ # 查看日志
162
+ pm2 logs discord-bot
163
+
164
+ # 重启
165
+ pm2 restart discord-bot
166
+ ```
167
+
168
+ ## 相关资源
169
+
170
+ - [Claude Code 文档](https://docs.anthropic.com/en/docs/claude-code)
171
+ - [Codex CLI 文档](https://developers.openai.com/codex/cli/)
172
+ - [Discord.js 文档](https://discord.js.org/)
173
+ - [cmux - 多 CLI 并行工具](https://github.com/manaflow-ai/cmux)(架构参考)
174
+ - [claude-code-discord](https://github.com/zebbern/claude-code-discord)(Discord 集成参考)
@@ -0,0 +1,149 @@
1
+ # 交接说明 - Discord CLI Bot
2
+
3
+ ## 项目背景
4
+
5
+ 这是一个将 AI 编程助手 CLI(Claude Code、Codex、OpenCode 等)接入 Discord 的机器人框架。
6
+
7
+ **原始需求**:用户希望通过 Discord 频道与 Claude Code 交互,实现:
8
+ - 团队协作:多人可通过 Discord 与 AI 助手对话
9
+ - 安全隔离:CLI 在 Docker 沙箱中执行,只能访问指定项目
10
+ - 权限控制:危险操作(删除文件、执行命令等)需要用户通过 Discord 按钮审批
11
+ - 无缝体验:支持对话恢复、任务计时、自动重连
12
+
13
+ **迁移原因**:原本 bot 代码在 `cognitive-prototype` 项目的 `.claude/hooks/discord-bot/` 目录下,现在独立出来成为单独项目,便于:
14
+ 1. 独立版本管理
15
+ 2. 扩展支持其他 CLI(Codex、OpenCode)
16
+ 3. 多实例部署
17
+
18
+ ## 当前状态
19
+
20
+ ### 已完成功能
21
+ - ✅ Discord 消息交互(@mention 触发)
22
+ - ✅ Docker 沙箱隔离执行
23
+ - ✅ 权限审批系统(Discord 按钮 UI)
24
+ - ✅ Session 管理(频道绑定、对话恢复)
25
+ - ✅ 任务计时(排除权限等待时间)
26
+ - ✅ 网络断开自动重连 + pm2 崩溃重启
27
+ - ✅ Playwright 预装(支持浏览器自动化测试)
28
+ - ✅ 端口映射 5173(Vite dev server 可从宿主机访问)
29
+ - ✅ Bash 命令智能审批(安全命令自动通过,危险命令需审批)
30
+
31
+ ### 待实现功能
32
+ - ⬚ 多 CLI 后端抽象层
33
+ - ⬚ Codex CLI 适配器
34
+ - ⬚ OpenCode CLI 适配器
35
+ - ⬚ 多实例配置管理
36
+
37
+ ## 立即需要做的事情
38
+
39
+ **Bot 已停止,需要从新项目启动!**
40
+
41
+ ```bash
42
+ # 1. 进入项目目录
43
+ cd /Users/cyrus/Documents/project/discord-cli-bot
44
+
45
+ # 2. 安装依赖
46
+ npm install
47
+
48
+ # 3. 构建 Docker 镜像(含 Playwright + 图形库依赖)
49
+ cd sandbox && docker build -t claude-sandbox . && cd ..
50
+
51
+ # 4. 启动 bot(pm2 托管,崩溃自动重启)
52
+ pm2 start src/bot.js --name discord-bot --exp-backoff-restart-delay=1000
53
+
54
+ # 5. 查看日志确认启动成功
55
+ pm2 logs discord-bot --lines 20
56
+ ```
57
+
58
+ ## 配置文件
59
+
60
+ 配置已就绪:`config/config.json`
61
+
62
+ ```json
63
+ {
64
+ "BOT_TOKEN": "Discord Bot Token",
65
+ "CLAUDE_CODE_OAUTH_TOKEN": "Claude OAuth Token",
66
+ "PROJECT_DIR": "/Users/cyrus/Documents/project/cognitive-prototype/interactive-prototype-cognitiveOS",
67
+ "ALLOWED_CHANNELS": "1462723721188868096"
68
+ }
69
+ ```
70
+
71
+ - `PROJECT_DIR`:Claude 可操作的项目目录(挂载到容器 /workspace)
72
+ - `ALLOWED_CHANNELS`:允许触发 bot 的 Discord 频道 ID
73
+
74
+ ## 项目结构
75
+
76
+ ```
77
+ discord-cli-bot/
78
+ ├── src/
79
+ │ └── bot.js # 主程序(Discord 交互、权限处理、Docker 执行)
80
+ ├── sandbox/
81
+ │ └── Dockerfile # Docker 镜像(node:20-slim + Playwright + 图形库)
82
+ ├── hooks/
83
+ │ └── hook.mjs # Claude PreToolUse 钩子(权限拦截)
84
+ ├── config/
85
+ │ ├── config.json # 实例配置(已配置,勿提交 git)
86
+ │ └── config.example.json # 配置示例
87
+ ├── README.md # 使用说明 + 踩坑记录
88
+ ├── DEVELOPMENT.md # 开发文档 + 架构设计
89
+ ├── HANDOVER.md # 本文档
90
+ ├── package.json
91
+ └── .gitignore
92
+ ```
93
+
94
+ ## 关键技术点
95
+
96
+ ### 1. Docker 执行模式
97
+ - 持久容器:`claude-discord-bot`,启动后保持运行
98
+ - 每次请求通过 `docker exec` 执行 Claude CLI
99
+ - 挂载:项目目录 → /workspace,~/.claude → /home/claude/.claude
100
+
101
+ ### 2. 权限审批流程
102
+ ```
103
+ Claude 调用工具 → hooks/hook.mjs 拦截 →
104
+ 写入 .pending-permissions/xxx.request.json →
105
+ Bot 检测到 → 发送 Discord 按钮卡片 →
106
+ 用户点击 Allow/Deny →
107
+ 写入 xxx.response.json → Hook 读取 →
108
+ Claude 继续/中止
109
+ ```
110
+
111
+ ### 3. Session 恢复
112
+ - Session 数据在 `/Users/cyrus/.claude`(挂载到容器)
113
+ - 频道绑定在 `.discord-channel-sessions.json`
114
+ - 用户发消息自动恢复上次对话
115
+
116
+ ## 常用命令
117
+
118
+ ```bash
119
+ # 查看状态
120
+ pm2 status
121
+
122
+ # 查看日志
123
+ pm2 logs discord-bot
124
+
125
+ # 重启
126
+ pm2 restart discord-bot
127
+
128
+ # 停止
129
+ pm2 stop discord-bot
130
+
131
+ # 进入容器调试
132
+ docker exec -it claude-discord-bot bash
133
+ ```
134
+
135
+ ## 相关文档
136
+
137
+ - `README.md` - 8 个踩坑记录(Discord 按钮超时、Docker localhost、Playwright 依赖等)
138
+ - `DEVELOPMENT.md` - 架构设计、后端适配器接口、CLI JSON 格式对比
139
+
140
+ ## Discord 使用方式
141
+
142
+ ```
143
+ @bot <消息> # 继续上次对话
144
+ @bot -n <消息> # 开始新对话
145
+ @bot -r <id> <消息> # 恢复指定 session
146
+ @bot /sessions # 查看历史 session
147
+ @bot /status # 查看 bot 状态
148
+ @bot /help # 帮助
149
+ ```
@@ -0,0 +1,26 @@
1
+ /**
2
+ * PM2 Ecosystem Configuration for AgentBridge
3
+ *
4
+ * Start: pm2 start ecosystem.config.cjs
5
+ * Stop: pm2 stop agent-bridge
6
+ * Logs: pm2 logs agent-bridge
7
+ */
8
+
9
+ module.exports = {
10
+ apps: [{
11
+ name: 'agent-bridge',
12
+ script: 'src/bot.js',
13
+ watch: false,
14
+ autorestart: true,
15
+ max_restarts: 10,
16
+ min_uptime: '10s',
17
+ max_memory_restart: '500M',
18
+ env: {
19
+ NODE_ENV: 'production'
20
+ },
21
+ kill_timeout: 5000,
22
+ wait_ready: false,
23
+ log_date_format: 'YYYY-MM-DD HH:mm:ss',
24
+ merge_logs: true,
25
+ }]
26
+ };