iflow-bot Windows 修复方案
问题描述
iflow-bot 在 Windows 系统上存在以下问题:
- ACP 模式无法启动:
shell must be False 错误
- 进程检查失败:
os.kill(pid, 0) 在 Windows 上不支持
- iflow 命令执行失败:Windows 上的 .CMD 文件无法通过 subprocess 正确执行
修复内容
1. 修复 check_iflow_installed() 函数
问题:在 Windows 上,subprocess.run(["iflow", "--version"]) 无法执行 .CMD 文件
解决方案:添加 Windows 平台检测,在 Windows 上使用 shell=True
# 修改前
result = subprocess.run(
["iflow", "--version"],
capture_output=True,
text=True,
timeout=10,
)
# 修改后
import platform
kwargs = {}
if platform.system().lower() == "windows":
kwargs["shell"] = True
result = subprocess.run(
["iflow", "--version"],
capture_output=True,
text=True,
timeout=10,
**kwargs
)
2. 修复 _start_acp_server() 函数
问题:asyncio.create_subprocess_exec() 在 Windows 上无法执行 .CMD 文件
解决方案:在 Windows 上使用 create_subprocess_shell(),在 Unix 上保持原方式
# 修改后
import platform
if platform.system().lower() == "windows":
# 在 Windows 上使用 shell 启动 iflow 命令,确保 .CMD 文件能被正确执行
cmd = f'iflow --experimental-acp --stream --port {port}'
process = await asyncio.create_subprocess_shell(
cmd,
stdout=asyncio.subprocess.PIPE,
stderr=asyncio.subprocess.PIPE,
# Windows 上 start_new_session 不支持 shell=True
)
else:
# Unix 系统使用原来的 exec 方式
process = await asyncio.create_subprocess_exec(
"iflow",
"--experimental-acp",
"--stream",
"--port", str(port),
stdout=asyncio.subprocess.PIPE,
stderr=asyncio.subprocess.PIPE,
start_new_session=True,
)
3. 修复 os.kill(pid, 0) 兼容性问题
问题:os.kill(pid, 0) 在 Windows 上不支持,会产生 OSError: [WinError 87] 参数错误
解决方案:在 Windows 上使用 tasklist 命令检查进程是否存在
3.1 在 gateway_start 函数中
# 修改后
import platform
if platform.system().lower() == "windows":
# Windows 上使用 tasklist 检查进程是否存在
result = subprocess.run(
["tasklist", "/FI", f"PID eq {pid}"],
capture_output=True,
text=True,
timeout=5,
shell=True,
)
if str(pid) in result.stdout:
console.print(f"[yellow]Gateway already running (PID: {pid})[/yellow]")
console.print("Use [cyan]iflow-bot gateway restart[/cyan] to restart")
return
else:
# Unix 系统使用 os.kill(pid, 0)
os.kill(pid, 0) # 检查进程是否存在
console.print(f"[yellow]Gateway already running (PID: {pid})[/yellow]")
console.print("Use [cyan]iflow-bot gateway restart[/cyan] to restart")
return
3.2 在 status 函数中
# 修改后
import platform
if platform.system().lower() == "windows":
# Windows 上使用 tasklist 检查进程是否存在
result = subprocess.run(
["tasklist", "/FI", f"PID eq {pid}"],
capture_output=True,
text=True,
timeout=5,
shell=True,
)
if str(pid) in result.stdout:
console.print(f" Gateway: [green]运行中[/green] (PID: {pid})")
else:
console.print(" Gateway: [red]已停止[/red] (进程不存在)")
else:
# Unix 系统使用 os.kill(pid, 0)
os.kill(pid, 0)
console.print(f" Gateway: [green]运行中[/green] (PID: {pid})")
验证结果
环境信息
- Python: 3.14.0
- iflow-cli: 0.5.14
- iflow-bot: 0.2.3
- 平台: Windows (win32)
- 启用渠道: 飞书 (feishu)
测试结果
| 测试项 |
命令 |
结果 |
| 版本查看 |
iflow-bot version |
正常 |
| 状态查看 |
iflow-bot status |
正常 |
| Gateway 启动 |
iflow-bot gateway start |
正常 (PID: 9332) |
| Gateway 停止 |
iflow-bot gateway stop |
正常 |
| Gateway 重启 |
iflow-bot gateway restart |
正常 |
| Cron 任务列表 |
iflow-bot cron list |
正常 |
| ACP 模式启动 |
- |
成功 |
| 飞书渠道 |
- |
已启用 |
| 定时任务 |
- |
1 个任务加载正常 |
| 心跳服务 |
- |
运行正常 |
日志输出
启动 ACP 服务 (端口: 8090)...
ACP 服务已在运行 (端口 8090),复用现有进程
Gateway 启动中...
✓ Gateway 运行中!
渠道: feishu
定时任务: 1 个
心跳: 每 30 分钟
模式: ACP
验证结论
ACP 模式成功启动
所有命令正常工作 (start, stop, status, restart)
消息处理功能正常
进程管理功能正常
修复了所有 asyncio.create_subprocess_exec 调用问题
Windows 兼容性完全修复
注意事项
- 需要重新安装 iflow-bot:
pip install -e .
- Windows 上使用 Git Bash 或 PowerShell 运行
- 确保 iflow CLI 已正确安装并登录