SuperAPI AI 工具集

claude-code-runner

编程与开发

查看项目

更新于 10-13 16:19

技能介绍

Claude Code Runner 是一个用于以编程方式在非交互环境中运行 Claude Code 的包装技能。它使用 PTY(伪终端)来处理需要 TTY 的操作,并自动回应确认提示。

主要能力

  • 基于 PTY 的执行:可在非 TTY 环境中工作,例如容器、CI/CD、后台进程。
  • 自动回应提示:自动回答“Do you want to...”之类的确认。
  • 用户切换:以指定的非 root 用户身份运行。
  • 文件同步:将项目复制到临时目录,执行后将变更同步回原目录。
  • 超时处理:可配置超时并正确清理。
  • 输出捕获:捕获并返回完整的 stdout/stderr。

安装

克隆技能

git clone https://github.com/lhl09120/claude-code-runner-en.git

使脚本可执行

chmod +x claude-code-runner-en/scripts/run_claude.py

使用方式

基本用法

from claude_code_runner import run_claude_code

result = run_claude_code( workdir='/path/to/project', prompt='Refactor the authentication module to use JWT tokens', user='lighthouse', timeout=300 )

print(result)

通过命令行

python3 scripts/run_claude.py /path/to/project "Your task description here"

高级选项

result = run_claude_code( workdir='/root/repo/my-project', prompt=''' 1. Review the codebase 2. Identify security vulnerabilities 3. Fix any issues found 4. Add appropriate tests ''', user='developer', timeout=600 # 10 分钟 )

API 参考

run_claude_code(workdir, prompt, user='lighthouse', timeout=300)

在 PTY 环境中执行一个 Claude Code 任务。

参数:

  • workdir(str):包含项目的工作目录。
  • prompt(str):自然语言任务描述。
  • user(str):运行所用的用户(默认:'lighthouse')。
  • timeout(int):超时时间,单位为秒(默认:300)。

返回:

  • str:合并后的 stdout 与 stderr 输出。

行为:

  1. 将项目复制到临时目录。
  2. 将所有权更改为指定用户。
  3. 通过 PTY 执行 Claude Code。
  4. 自动回应确认提示。
  5. 将变更同步回原目录。
  6. 清理临时文件。

适用场景

  1. 自动化代码审查

result = run_claude_code( workdir='/root/repo/project', prompt='Review this codebase and identify potential bugs or improvements' )

  1. 重构任务

result = run_claude_code( workdir='/root/repo/legacy-app', prompt='Refactor the database layer to use SQLAlchemy ORM instead of raw SQL' )

  1. 添加功能

result = run_claude_code( workdir='/root/repo/api-service', prompt=''' Add a new REST endpoint for user profile management: - GET /api/users/{id}/profile - PUT /api/users/{id}/profile - Include validation and error handling - Add unit tests ''' )

  1. 修复缺陷

result = run_claude_code( workdir='/root/repo/web-app', prompt='Fix the memory leak in the WebSocket connection handler' )

环境要求

  • Python 3.8+
  • 已安装 Claude Code 并位于 PATH 中
  • 类 Unix 环境(Linux/macOS)
  • root 或 sudo 权限(用于用户切换)

配置

环境变量

  • CLAUDE_CODE_USER:默认运行所用的用户(默认:'lighthouse')。
  • CLAUDE_CODE_TIMEOUT:默认超时秒数(默认:300)。

自定义

编辑 scripts/run_claude.py 以自定义:

  • 自动回应关键词
  • 临时目录位置
  • 同步行为
  • 输出格式

故障排查

“Permission denied” 错误

确保脚本以足够权限运行,以便:

  • 创建临时目录
  • 更改文件所有权
  • 切换到目标用户

找不到 Claude Code

确保 Claude Code 已安装并在系统 PATH 中:

which claude

任务超时

为长时间运行的任务增加超时:

run_claude_code(workdir, prompt, timeout=600) # 10 分钟

交互式提示未被自动回应

将新的提示模式加入自动回应逻辑:

if b'new prompt text' in output: os.write(master_fd, b'y\n')

限制

  • 需要类 Unix 环境(使用 PTY)。
  • 需要 root/sudo 进行用户切换。
  • Claude Code 必须单独安装。
  • 可能无法处理所有交互式提示的边界情况。

许可证

MIT License

Copyright (c) 2026 lhl09120

特此免费授予任何获得本软件及相关文档文件(“软件”)副本的人不受限制地处理本软件的权利,包括但不限于使用、复制、修改、合并、发布、分发、再许可和/或销售本软件副本的权利,并允许向其提供本软件的人这样做,但须符合以下条件:

上述版权声明和本许可声明应包含在本软件的所有副本或实质性部分中。

详见 LICENSE 文件。

更新日志

v1.0.0(2026-02-27)

  • 初始版本
  • 基于 PTY 的 Claude Code 执行
  • 自动回应确认提示
  • 文件同步
  • 用户切换支持