展示 HN:TermHub – 为 AI 代理构建的开源终端控制网关
Show HN: TermHub – Open-source terminal control gateway built for AI Agents

原始链接: https://github.com/duo121/termhub

## Termhub:AI驱动的终端控制 Termhub 是一款原生AI工具,旨在简化与终端会话的交互。它使AI能够智能管理终端——打开窗口/标签页,启动会话,发送命令,并仅捕获*新的*输出。这种“发送-捕获增量循环”是高效AI工作流程的关键。 **主要特性:** * **AI驱动自动化:** 基于AI检查和需求自动化终端任务。 * **跨平台:** 支持macOS (iTerm2, Terminal) 和 Windows (Windows Terminal, CMD)。 * **提供SDK:** 提供JavaScript SDK,用于对终端进行程序化控制。 * **核心命令:** 包括 `open`、`list`、`resolve/find`、`send`、`press`、`capture`、`focus`、`close`、`doctor` 和 `spec`。 * **会话管理:** 使用会话ID或句柄进行精确的目标定位。 * **增量捕获:** 仅捕获命令*发送后*的输出,提高效率。 **安装:** 可通过npm (`@duo121/termhub`)、Homebrew (macOS) 或从GitHub Releases直接下载。 Termhub简化了将终端交互集成到AI应用程序中,为自动化任务提供了一个强大而灵活的解决方案。

Hacker News 新闻 | 过去 | 评论 | 提问 | 展示 | 招聘 | 提交 登录 Show HN: TermHub – 为 AI 代理构建的开源终端控制网关 (github.com/duo121) 4 分,由 duo121 发布 2 小时前 | 隐藏 | 过去 | 收藏 | 1 条评论 帮助 duo121 2 小时前 | 下一个 [–] 为 LLM 和 AI 代理提供原生桥接,以完全控制本地终端 (iTerm2/Windows Terminal):创建标签页,分割窗格,切换会话,执行命令,以编程方式捕获实时输出。 构建强大的本地 AI 终端自动化,无需笨拙的解决方法。 GitHub: https://github.com/duo121/termhub 回复 指南 | 常见问题 | 列表 | API | 安全 | 法律 | 申请 YC | 联系 搜索:
相关文章

原文

中文说明

termhub cover

iterm2

termhub is an AI-native terminal control tool.

It is designed for this closed loop:

  1. AI inspects what terminal sessions are open.
  2. AI opens a window or tab when needed.
  3. AI launches or targets a Codex session.
  4. AI sends the task into that session.
  5. AI captures only the new output produced after send and returns it to the user.
  • Command: termhub
  • Alias: thub
  • npm package: @duo121/termhub
  • macOS backends: iTerm2, Terminal
  • Windows backends: Windows Terminal, Command Prompt (CMD)
npm install -g @duo121/termhub

Or Homebrew (macOS):

brew tap duo121/termhub https://github.com/duo121/termhub
brew install duo121/termhub/termhub

Install from GitHub Releases (without npm):

  • termhub_<version>_macos-arm64.tar.gz
  • termhub_<version>_windows-x64.zip

After extraction:

chmod +x termhub
./termhub --version
termhub --help
termhub spec
termhub list

Use spec as machine-readable truth and --help as human-readable truth. Both now include a currentSession hint near the top that you can copy directly into --session for AI handoff.

termhub now ships an SDK preview entry:

import { createTermhubClient } from "@duo121/termhub/sdk";

Core SDK capabilities:

  • Open/close terminal targets.
  • Find/resolve terminal sessions.
  • Send keyboard text and key events (key / combo / sequence).
  • Mouse click simulation on terminal target (mouseClick) on macOS.

Platform notes:

  • macOS (iTerm2 / Terminal): keyboard + mouse click are supported.
  • Windows (Windows Terminal / CMD): keyboard control is supported; mouseClick currently returns unsupported.

SDK quick example:

import { createTermhubClient } from "@duo121/termhub/sdk";

const client = createTermhubClient({ app: "iterm2" });

const opened = await client.open({ scope: "tab" });
await client.send({ session: opened.target.handle, text: "echo hello from sdk" });
await client.press({ session: opened.target.handle, key: "enter" });
const output = await client.capture({ session: opened.target.handle, lines: 20 });

console.log(output.text);
Top-Level Command What It Does Common Secondary Flags
open Open terminal window or tab --app --window --tab --dry-run
list List running windows/tabs/sessions --app --compact
resolve / find Narrow fuzzy target to one exact session --title --title-contains --session --current-tab
send Send text and optionally await/capture output delta in one step --text --stdin --no-enter --await-output --dry-run
press Send real key/combo/sequence events --key --combo --sequence --repeat --delay
capture Read visible output or delta since latest send checkpoint --session --lines --since-last-send --app
focus Bring target window/session to front --session --app --dry-run
close Close target tab or window --session --app --dry-run
doctor Check platform/backend/automation readiness --app --compact
spec Print machine-readable JSON contract --compact
  1. Always resolve (or find) to one exact target before mutating commands.
  2. Use --app when multiple backends are active.
  3. Use --dry-run before risky operations.
  4. Use send --no-enter only when you plan a separate real key submit.
  5. Never fake submit by appending literal newlines inside --text or stdin.

press supports exactly one input mode:

  • --key <key>
  • --combo <combo> (for example ctrl+c, cmd+k)
  • --sequence <steps> (for example esc,down*5,enter)

Extra controls:

  • --repeat <n>: only for --key and --combo
  • --delay <ms>: delay between repeated or sequenced key events

Examples:

termhub press --session <id|handle> --key enter
termhub press --session <id|handle> --combo ctrl+c
termhub press --session <id|handle> --sequence "esc,down*3,enter" --delay 60

Open a new iTerm2 window:

termhub open --app iterm2 --window

List all iTerm2 tabs:

termhub list --app iterm2

Close a specific tab by title:

termhub resolve --title Task1
termhub find --title Task1
termhub close --session <resolved-handle-or-session-id>

Read current Terminal tab (last 50 lines):

termhub resolve --app terminal --current-window --current-tab --current-session
termhub capture --app terminal --session terminal:session:<window-id>:<tab-index> --lines 50

Run command in Windows Terminal tab titled API:

termhub resolve --app windows-terminal --title API
termhub send --app windows-terminal --session windows-terminal:session:<window-handle>:<tab-index> --text "npm test"

Send-To-Capture Delta Loop

termhub now supports a built-in session checkpoint loop so AI can capture only the new output produced after send.

Basic flow:

termhub send --session <id|handle> --text "npm test" --await-output 1200

How it works:

  • send stores a checkpoint for that exact session before writing input.
  • send --await-output <ms> waits and returns only delta output produced after that send.
  • capture --since-last-send remains available when you want a separate explicit read step.

Concurrency:

  • Checkpoints are session-scoped, so two AI agents can use different sessions in parallel without conflict.
  • State files are stored under ~/.termhub/state by default.
  • --session accepts native session id or namespaced handle.
  • Windows focus/send/capture/close rely on PowerShell + UI Automation.
  • Windows capture is best-effort based on visible text accessibility.
联系我们 contact @ memedata.com