aiterm-steer-delivery 0.1.9 → 0.1.10
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/CHANGELOG.md +5 -0
- package/README.ja.md +197 -0
- package/README.md +145 -25
- package/dist/cursor-receiver.js +2 -1
- package/dist/hook-setup.d.ts +5 -0
- package/dist/hook-setup.js +14 -1
- package/dist/index.d.ts +1 -1
- package/dist/index.js +1 -1
- package/package.json +1 -1
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,10 @@
|
|
|
1
1
|
# Changelog
|
|
2
2
|
|
|
3
|
+
## 0.1.10
|
|
4
|
+
|
|
5
|
+
- Cursorのhookの持ち主を、名前の部分一致ではなくfile名の境目まで見て決める。Aitermの`cursor-parent-hook.js`が`gpt-connector-cursor-parent-hook.js`にも当たり、Aitermのsetupと解除がgpt-connectorのCursor hookを消していた(2026-10-03、main-server)。登録の確認(`cursorParentHooksRegistered`)も同じ見分けにした。`commandNamesFile`を公開する。
|
|
6
|
+
- Claudeのhookが同じ中身で登録済みなら、並びを変えない。後から他の道具がhookを足した後にsetupを流すと、自製品のentryが末尾へ移ってfileが書き換わっていた。
|
|
7
|
+
|
|
3
8
|
## 0.1.9
|
|
4
9
|
|
|
5
10
|
- `waitProcessCommandLine`を公開する(Aitermから移した)。`cursorReceiveProcess`・`channelReceiveProcess`の起動情報を、親のshellへ書ける1行にする(POSIXはsh、Windowsは既定のPowerShell)。Cursor CLIのmodelはtool結果のstructuredContentを読まないので、Cursor・Grokの親には背景で受信を起動するコマンドをtool結果の本文に書く必要がある(ラプラス氏の依頼、決裁箱のコネクタで使う)。
|
package/README.ja.md
ADDED
|
@@ -0,0 +1,197 @@
|
|
|
1
|
+
# aiterm-steer-delivery
|
|
2
|
+
|
|
3
|
+
[](https://github.com/kitepon/aiterm-steer-delivery/actions/workflows/ci.yml)
|
|
4
|
+
[](https://www.npmjs.com/package/aiterm-steer-delivery)
|
|
5
|
+
[](https://nodejs.org)
|
|
6
|
+
[](LICENSE)
|
|
7
|
+
|
|
8
|
+
**MCPサーバーから、そのツールを呼んだAIセッションへ話しかけられるようにするライブラリです。**
|
|
9
|
+
|
|
10
|
+
[English README](README.md)(英語版が正本です)
|
|
11
|
+
|
|
12
|
+
MCPサーバーは、ツールの呼び出しが返った後でも、**ツールを呼んだ親のAIセッション**へ文章を届けられます。
|
|
13
|
+
|
|
14
|
+
- 親が作業中なら、実行中のターンへ差し込みます。
|
|
15
|
+
- 親が手すきなら、同じ会話に新しいターンとして届きます。
|
|
16
|
+
|
|
17
|
+
対応する親は Codex、Claude Code、Cursor です。それ以外(Grok など)は、背景で動く受信プロセスで受け取ります。Linux、macOS、Windows で動きます。
|
|
18
|
+
|
|
19
|
+
## なぜ必要か
|
|
20
|
+
|
|
21
|
+
AIのセッションをいくつも同時に動かしていると、大事なことはセッションの間で起きます。サブエージェントが作業を終えたら親に知らせたいですし、あるセッションが共有の部屋に書いたら別のセッションに反応してほしいこともあります。ところがツールの呼び出しは一度返ったら終わりです。後から起きたことは、親がもう一度聞きに来ない限り届け先がありません。
|
|
22
|
+
|
|
23
|
+
後から出た文章を正しい会話へ届けるには、次の壁があります。
|
|
24
|
+
|
|
25
|
+
- **どの会話か。** 呼び出し元の会話の見分け方はクライアントごとに違い、hook からしか分からないものもあります。環境変数のセッションIDのように使えそうな値も、`/clear` で古くなります。
|
|
26
|
+
- **どう入れるか。** 公式の受け口はクライアントごとに別物です(キュー、非同期 hook、hook の出力、背景プロセス)。導入手順も失敗の仕方もそれぞれ違います。
|
|
27
|
+
- **OSごとの違い。** プロセスの見分け方、シェル、PowerShell の引用符、hook 入力の先頭に付く BOM、同じ端末に複数ある `CODEX_HOME` などです。
|
|
28
|
+
|
|
29
|
+
このパッケージは、[Aiterm](https://github.com/kitepon/aiterm-mcp) がサブエージェントの答えを親へ返すのに使っている配送の仕組みです。挙動を変えずに Aiterm から切り出したので、他の製品は会話の見分け、hook の登録、OSごとの対応を作るたびに書き直さずに済みます。
|
|
30
|
+
|
|
31
|
+
## 使っている製品
|
|
32
|
+
|
|
33
|
+
| 製品 | 届けているもの |
|
|
34
|
+
| --- | --- |
|
|
35
|
+
| [Aiterm](https://github.com/kitepon/aiterm-mcp) | サブエージェントの最終回答を、それを起動したセッションへ |
|
|
36
|
+
| [Peertable](https://github.com/kitepon/peertable) | ルームの発言と個別メッセージを、円卓の脇に座るオーナー自身のセッションへ |
|
|
37
|
+
| [gpt-connector](https://github.com/kitepon/gpt-connector) | ChatGPT の返答を、それを頼んだ Codex のスレッドへ |
|
|
38
|
+
|
|
39
|
+
## 仕組み
|
|
40
|
+
|
|
41
|
+
| 親 | 親の見分け方 | 文章の届き方 |
|
|
42
|
+
| --- | --- | --- |
|
|
43
|
+
| Codex | MCP 要求の `_meta.threadId` | 公式 App Server のキュー(`thread/queue/add`)。Steer を有効にすると、製品の同期 hook(`PostToolUse`/`Stop`)が実行中のターンへ取り込みます。無効なら公式キューが次のターンの切れ目で届けます。 |
|
|
44
|
+
| Claude Code | 製品の `PreToolUse` hook の記録と `_meta["claudecode/toolUseId"]` | `asyncRewake` hook が本文を stderr へ書いて exit 2 で終わり、セッションを起こします(作業中ならそのターンへ入ります)。 |
|
|
45
|
+
| Cursor(Desktop と CLI) | ツール結果に入れた ID を、製品の hook が実際の `conversation_id` に結び付けます | 作業中は次のツール返りの `additional_context`、手すきの時は背景の受信プロセス。 |
|
|
46
|
+
| その他(Grok など) | — | 背景の受信プロセス(`wait_process`)。 |
|
|
47
|
+
|
|
48
|
+
どの経路もクライアントの公式の仕組みを使います。端末へ文字を打ち込んだり、クライアントを改造したりはしません。
|
|
49
|
+
|
|
50
|
+
**届いたかどうか分からない送信は、自動で再送しません。** 親に届いたか確かめられない時は `outcome_unknown`(channel の状態では `unknown`)を返し、判断を呼び出し側に任せます。同じ文章が親に二度届く事故を避けるためです。
|
|
51
|
+
|
|
52
|
+
## 導入
|
|
53
|
+
|
|
54
|
+
```sh
|
|
55
|
+
npm install aiterm-steer-delivery
|
|
56
|
+
```
|
|
57
|
+
|
|
58
|
+
必要なもの:
|
|
59
|
+
|
|
60
|
+
- Node.js 18 以上。ESM のみです。実行時の依存は `zod` だけです。
|
|
61
|
+
- Codex の Steer(作業中のターンへの差し込み)は、Codex Desktop の同梱 CLI があればそれを使います(macOS・Windows・Linux)。無ければ通常の Codex CLI 0.154 以上を使います。
|
|
62
|
+
- Windows の Codex Steer の hook は PowerShell 7(`pwsh.exe`)で動きます。
|
|
63
|
+
|
|
64
|
+
## 使い方
|
|
65
|
+
|
|
66
|
+
### 1. 製品プロファイルを書く
|
|
67
|
+
|
|
68
|
+
製品ごとの違いは、1つのオブジェクトにまとめます。配送の仕組みはどの製品でも同じです。
|
|
69
|
+
|
|
70
|
+
```js
|
|
71
|
+
// profile.mjs
|
|
72
|
+
export const PROFILE = {
|
|
73
|
+
id: "peertable", display_name: "Peertable",
|
|
74
|
+
setup_command: "peertable parent connect", codex_steer_command: "peertable parent connect --target codex",
|
|
75
|
+
mcp_server: "peertable_parent", dispatch_tools: ["parent_join", "parent_leave"],
|
|
76
|
+
state_root: () => stateDir, config_root: () => configDir,
|
|
77
|
+
hooks: { codex: "peertable-parent-codex-hook.mjs", claude: "peertable-parent-claude-hook.mjs", cursor: "peertable-parent-cursor-hook.mjs" },
|
|
78
|
+
codex_client_name: "peertable_parent_delivery", codex_hook_schema: "peertable.codex-parent-hooks.v1",
|
|
79
|
+
backup_suffix: ".peertable-backup",
|
|
80
|
+
channels: { claude_expiry_notice: "..." }, // channel を使う製品だけ
|
|
81
|
+
};
|
|
82
|
+
```
|
|
83
|
+
|
|
84
|
+
| 項目 | 意味 |
|
|
85
|
+
| --- | --- |
|
|
86
|
+
| `id` | 製品ID。状態のスキーマ名や、自製品の hook の見分けに使います。 |
|
|
87
|
+
| `display_name` | エラー文に出す製品名。 |
|
|
88
|
+
| `setup_command`、`codex_steer_command` | 利用者が配送を設定する時に実行するコマンド。エラー文で案内します。 |
|
|
89
|
+
| `mcp_server` | 親に登録された MCP サーバー名。Claude Code の hook の matcher と、Cursor のツール名に使います。 |
|
|
90
|
+
| `dispatch_tools` | 親への配送を伴うツール。Claude Code の hook はこれらにだけ付きます。 |
|
|
91
|
+
| `state_root` | Claude Code と Cursor の配送記録を置く、利用者ごとの実行時の置き場。 |
|
|
92
|
+
| `config_root` | Codex の hook 設定と所有記録を置く、利用者ごとの永続設定の置き場。 |
|
|
93
|
+
| `hooks` | 製品の hook 入口ファイルの名前。自製品の hook の見分けに使うので、他の製品と重ならない名前にします。 |
|
|
94
|
+
| `codex_client_name`、`codex_hook_schema` | Codex App Server へ名乗るクライアント名と、Codex の hook 設定のスキーマ名。 |
|
|
95
|
+
| `backup_suffix` | 設定ファイルを書き換える前に残す控えの接尾辞。 |
|
|
96
|
+
| `channels` | 同じ会話へ何通も送る製品だけ設定します。`claude_expiry_notice` は、hook の期限(24時間)が近づいた時に Claude Code を起こして待機を張り直すための短い文です。 |
|
|
97
|
+
|
|
98
|
+
### 2. hook 入口ファイルを同梱する
|
|
99
|
+
|
|
100
|
+
hook 入口ファイルは製品の中に置きます。ファイル名で自製品の hook を見分けるので、製品同士が互いの hook を消すことはありません。中身は2行です。
|
|
101
|
+
|
|
102
|
+
```js
|
|
103
|
+
#!/usr/bin/env node
|
|
104
|
+
import { runClaudeHookMain } from "aiterm-steer-delivery";
|
|
105
|
+
import { PROFILE } from "./profile.mjs";
|
|
106
|
+
await runClaudeHookMain(PROFILE);
|
|
107
|
+
```
|
|
108
|
+
|
|
109
|
+
3つの hook ファイルには `runCodexHookMain`、`runClaudeHookMain`、`runCursorHookMain` を使います。背景の受信には `runCursorReceiveMain`(1回の回答)か `runChannelReceiveMain`(channel)を使います。
|
|
110
|
+
|
|
111
|
+
### 3. hook を登録する
|
|
112
|
+
|
|
113
|
+
製品の setup コマンドで次を呼びます。
|
|
114
|
+
|
|
115
|
+
- Claude Code: `mergeClaudeParentHooks(PROFILE, settingsFile, { command: nodePath, script: hookPath })`
|
|
116
|
+
- Cursor: `mergeCursorParentHooks(PROFILE, hooksFile, { command: nodePath, script: hookPath })`
|
|
117
|
+
- Codex: `configureCodexSteer(PROFILE, "enable", { hook: hookPath })`
|
|
118
|
+
|
|
119
|
+
他の製品や利用者の hook とその順序は保ち、書き換える前にファイルの控えを残します。解除は `removeClaudeParentHooks`、`removeCursorParentHooks`、`configureCodexSteer(PROFILE, "disable", …)` です。hook を入れる前から動いている Codex が残っていると、`configureCodexSteer` は `restart_required` を返します。
|
|
120
|
+
|
|
121
|
+
### 4. 届ける
|
|
122
|
+
|
|
123
|
+
届け方は2つあります。
|
|
124
|
+
|
|
125
|
+
**1回の依頼に1つの答え**(Aiterm の使い方): MCP 要求から親を見分け、配送IDを結び付け、確定した本文を一度だけ送ります。
|
|
126
|
+
|
|
127
|
+
```js
|
|
128
|
+
import * as steer from "aiterm-steer-delivery";
|
|
129
|
+
|
|
130
|
+
const parent = steer.codexParentFromRequest(clientName, request.params._meta);
|
|
131
|
+
await steer.verifyCodexParent(PROFILE, parent);
|
|
132
|
+
const { queued_submission_id } = await steer.submitCodexParentAnswer(PROFILE, parent, deliveryId, text);
|
|
133
|
+
```
|
|
134
|
+
|
|
135
|
+
Claude Code は `claudeParentFromRequest`/`bindClaudeParentDelivery`/`submitClaudeParentAnswer`、Cursor は `cursorParentFromRequest`/`prepareCursorDelivery`/`submitCursorParentAnswer` で同じ流れになります。各 `…ParentFromRequest` は呼び出し元が別のクライアントなら `null` を返すので、順に試せます。Cursor では、hook が会話に結び付けられるよう、ツール結果に配送IDを入れます。`structuredContent` には `parent_delivery: { delivery_id }`、本文には `delivery_id=<uuid>` の行を書きます。
|
|
136
|
+
|
|
137
|
+
**同じ会話へ何通も送る**(channel。例: Peertable がルームの発言を親へ届ける):
|
|
138
|
+
|
|
139
|
+
```js
|
|
140
|
+
const channel = steer.openChannel(PROFILE, parent); // parent は要求から得たもの。背景受信だけなら null
|
|
141
|
+
await steer.sendToChannel(PROFILE, channel.channel_id, randomUUID(), text);
|
|
142
|
+
steer.channelDeliveryState(PROFILE, channel.channel_id, id); // queued | sending | emitted | unknown | withdrawn
|
|
143
|
+
```
|
|
144
|
+
|
|
145
|
+
Claude Code は `Stop` のたびに待機を張り直します。Cursor はツール結果に入れた `steer.channelMarker(channel)` で channel を会話に結び付けます。手すきの Cursor/Grok の親は `steer.channelReceiveProcess(...)` を背景で動かし、結果に付く `next_wait_process` で受信を張り直します。`withdrawFromChannel` はまだどの受け口も取っていない本文を取り下げるので、新しい channel で送り直せます。`closeChannel` で channel を閉じます。
|
|
146
|
+
|
|
147
|
+
marker と受信コマンドは、`structuredContent` だけでなくツール結果の**本文**にも書いてください。Cursor CLI のモデルは structured content を読みません。`steer.waitProcessCommandLine(steer.channelReceiveProcess(script, channel.channel_id))` で、受信コマンドをシェルの1行にできます(POSIX sh、Windows では PowerShell)。
|
|
148
|
+
|
|
149
|
+
## エラーと配送の状態
|
|
150
|
+
|
|
151
|
+
配送のエラーは `SteerDeliveryError`(`CodexDeliveryError`、`ClaudeDeliveryError`、`CursorDeliveryError`、`ChannelError`)で、次を持ちます。
|
|
152
|
+
|
|
153
|
+
- `delivery_code`: 理由を表す決まった文字列。例: `CLAUDE_PARENT_HOOK_UNAVAILABLE`、`CHANNEL_CLOSED`
|
|
154
|
+
- `outcome_unknown`: 親に届いたかどうか分からない時に `true`。自動で再送しないでください。
|
|
155
|
+
|
|
156
|
+
setup の失敗は `SetupError` で、その `code` が製品の setup 結果の `reason_code` になります。
|
|
157
|
+
|
|
158
|
+
channel の配送状態:
|
|
159
|
+
|
|
160
|
+
| 状態 | 意味 |
|
|
161
|
+
| --- | --- |
|
|
162
|
+
| `queued` | channel の受信箱に保存済みで、まだどの受け口も取っていません。 |
|
|
163
|
+
| `sending` | 受け口が取り、出力しているところです。 |
|
|
164
|
+
| `emitted` | 親へ出力しました。 |
|
|
165
|
+
| `unknown` | 受け口が取った後に止まりました。親に届いたかどうか分かりません。 |
|
|
166
|
+
| `withdrawn` | 受け口が取る前に `withdrawFromChannel` で取り下げました。 |
|
|
167
|
+
|
|
168
|
+
Codex の channel では、`sendToChannel` が公式キューの `queued_submission_id` とともに `submitted` を返し、`channelDeliveryState` は `null` を返します。
|
|
169
|
+
|
|
170
|
+
既知の制限: `agent_id` を持つ Claude Code の会話(`--agent` で起動したもの、ネイティブのサブエージェント)は、親として対応していません。
|
|
171
|
+
|
|
172
|
+
## Node 以外の製品
|
|
173
|
+
|
|
174
|
+
`aiterm-steer-delivery --profile <profile.json> codex <parent|verify|submit|state|setup>` は、結果を1行の JSON で出します。JSON のプロファイルでは `state_root` と `config_root` を絶対パスで書きます。`codex setup enable` は、パッケージ同梱の Codex hook 入口をそのプロファイル用に登録します。
|
|
175
|
+
|
|
176
|
+
```sh
|
|
177
|
+
aiterm-steer-delivery --profile profile.json codex parent --client <name> --meta <json>
|
|
178
|
+
aiterm-steer-delivery --profile profile.json codex verify --thread <uuid> [--codex-home <dir>]
|
|
179
|
+
aiterm-steer-delivery --profile profile.json codex submit --thread <uuid> --delivery <uuid> --text-file <file|-> [--codex-home <dir>]
|
|
180
|
+
aiterm-steer-delivery --profile profile.json codex state --thread <uuid> --delivery <uuid> [--codex-home <dir>]
|
|
181
|
+
aiterm-steer-delivery --profile profile.json codex setup <enable|disable|status>
|
|
182
|
+
```
|
|
183
|
+
|
|
184
|
+
成功は `{"ok":true,...}`、失敗は `{"ok":false,"code":...,"message":...,"outcome_unknown":...}` で、exit code は 1 です。
|
|
185
|
+
|
|
186
|
+
## 開発
|
|
187
|
+
|
|
188
|
+
```sh
|
|
189
|
+
npm ci
|
|
190
|
+
npm test # tsc でビルドしてから node --test を実行します
|
|
191
|
+
```
|
|
192
|
+
|
|
193
|
+
CI は Ubuntu、macOS、Windows で試験します。`v<version>` のタグを push すると、provenance 付きで npm に公開されます。変更の記録は [CHANGELOG.md](CHANGELOG.md) にあります。
|
|
194
|
+
|
|
195
|
+
## ライセンス
|
|
196
|
+
|
|
197
|
+
[MIT](LICENSE)
|
package/README.md
CHANGED
|
@@ -1,50 +1,74 @@
|
|
|
1
1
|
# aiterm-steer-delivery
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
[](https://github.com/kitepon/aiterm-steer-delivery/actions/workflows/ci.yml)
|
|
4
|
+
[](https://www.npmjs.com/package/aiterm-steer-delivery)
|
|
5
|
+
[](https://nodejs.org)
|
|
6
|
+
[](LICENSE)
|
|
7
|
+
|
|
8
|
+
**Let your MCP server talk back to the AI session that called it.**
|
|
9
|
+
|
|
10
|
+
[日本語版 README](README.ja.md)
|
|
11
|
+
|
|
12
|
+
Your MCP server can deliver text to the **parent AI session that called its tool**, even after the tool call has returned:
|
|
4
13
|
|
|
5
14
|
- if the parent is busy, the text is steered into its running turn;
|
|
6
15
|
- if the parent is idle, it arrives in the same conversation as a normal new turn.
|
|
7
16
|
|
|
8
|
-
|
|
17
|
+
Works with Codex, Claude Code, and Cursor. Other clients (Grok and so on) receive through a background process. Linux, macOS, and Windows.
|
|
18
|
+
|
|
19
|
+
## Why
|
|
20
|
+
|
|
21
|
+
If you run many AI sessions at once, the useful work often happens between them: a sub-agent finishes and its parent should hear about it, or one session posts in a shared room and another should react. A tool call returns once. Anything that happens later has nowhere to go unless the parent comes back and asks.
|
|
22
|
+
|
|
23
|
+
Delivering that later text to the right conversation is the hard part:
|
|
24
|
+
|
|
25
|
+
- **Which conversation?** Each client identifies the calling conversation differently, and some only through their hooks. Values that look usable, such as a session id in the environment, go stale after `/clear`.
|
|
26
|
+
- **How does text get in?** Each client has its own official mechanism (a queue, async hooks, hook output, a background process), with its own setup and its own failure modes.
|
|
27
|
+
- **OS quirks.** Process identity, shells, PowerShell quoting, BOMs in hook input, different `CODEX_HOME`s on the same machine.
|
|
28
|
+
|
|
29
|
+
This package is the delivery [Aiterm](https://github.com/kitepon/aiterm-mcp) uses to hand a sub-agent's answer back to its parent. It was extracted from Aiterm without changing behavior, so other products do not have to rebuild conversation correlation, hook registration, and OS handling every time.
|
|
30
|
+
|
|
31
|
+
## Used by
|
|
32
|
+
|
|
33
|
+
| Product | What it delivers |
|
|
34
|
+
| --- | --- |
|
|
35
|
+
| [Aiterm](https://github.com/kitepon/aiterm-mcp) | A sub-agent's final answer, back to the session that launched it |
|
|
36
|
+
| [Peertable](https://github.com/kitepon/peertable) | Room messages and direct messages, to the owner's own session sitting beside the table |
|
|
37
|
+
| [gpt-connector](https://github.com/kitepon/gpt-connector) | A ChatGPT reply, back into the Codex thread that asked for it |
|
|
38
|
+
|
|
39
|
+
## How it works
|
|
9
40
|
|
|
10
41
|
| Parent | How the parent is identified | How text arrives |
|
|
11
42
|
| --- | --- | --- |
|
|
12
43
|
| Codex | `_meta.threadId` of the MCP request | Official App Server queue (`thread/queue/add`). With Steer enabled, your sync `PostToolUse`/`Stop` hooks pull it into the running turn; otherwise the official queue delivers it at the next turn boundary. |
|
|
13
44
|
| Claude Code | Your `PreToolUse` hook record + `_meta["claudecode/toolUseId"]` | An `asyncRewake` hook writes the text to stderr and exits 2, waking the session (or steering into the running turn). |
|
|
14
|
-
| Cursor | Your hook binds the id in your tool result to the real `conversation_id` | `additional_context` on the next tool return while busy; a background receiver process while idle. |
|
|
45
|
+
| Cursor (Desktop and CLI) | Your hook binds the id in your tool result to the real `conversation_id` | `additional_context` on the next tool return while busy; a background receiver process while idle. |
|
|
15
46
|
| Others (Grok, …) | — | A background receiver process (`wait_process`). |
|
|
16
47
|
|
|
17
|
-
|
|
48
|
+
Every route uses the client's official mechanism. Nothing types into a terminal or patches the client.
|
|
18
49
|
|
|
19
|
-
|
|
50
|
+
**Delivery never auto-retries a send whose outcome is unknown.** If the library cannot confirm whether text reached the parent, it reports `outcome_unknown` (or the channel state `unknown`) and leaves the decision to you, so the parent never gets the same message twice by accident.
|
|
20
51
|
|
|
21
|
-
|
|
52
|
+
## Install
|
|
22
53
|
|
|
23
|
-
```
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
const parent = steer.codexParentFromRequest(clientName, request.params._meta);
|
|
27
|
-
await steer.verifyCodexParent(PROFILE, parent);
|
|
28
|
-
const { queued_submission_id } = await steer.submitCodexParentAnswer(PROFILE, parent, deliveryId, text);
|
|
54
|
+
```sh
|
|
55
|
+
npm install aiterm-steer-delivery
|
|
29
56
|
```
|
|
30
57
|
|
|
31
|
-
|
|
58
|
+
Requirements:
|
|
32
59
|
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
steer.channelDeliveryState(PROFILE, channel.channel_id, id); // queued | sending | emitted | unknown | withdrawn
|
|
37
|
-
```
|
|
38
|
-
|
|
39
|
-
Claude Code re-arms its waiter at every `Stop`. Cursor binds the channel with `steer.channelMarker(channel)` placed in your tool result, and idle Cursor/Grok parents run `steer.channelReceiveProcess(...)` in the background; each result carries `next_wait_process` to re-arm.
|
|
60
|
+
- Node.js 18 or later. ESM only. The only runtime dependency is `zod`.
|
|
61
|
+
- Codex Steer (delivery into a running Codex turn) uses the Codex Desktop's bundled CLI when present (macOS, Windows, Linux), and otherwise the regular Codex CLI 0.154 or later.
|
|
62
|
+
- On Windows, Codex Steer hooks run through PowerShell 7 (`pwsh.exe`).
|
|
40
63
|
|
|
41
|
-
|
|
64
|
+
## Quick start
|
|
42
65
|
|
|
43
|
-
|
|
66
|
+
### 1. Write a product profile
|
|
44
67
|
|
|
45
68
|
Everything product-specific lives in one object. The delivery mechanism is the same for every product.
|
|
46
69
|
|
|
47
70
|
```js
|
|
71
|
+
// profile.mjs
|
|
48
72
|
export const PROFILE = {
|
|
49
73
|
id: "peertable", display_name: "Peertable",
|
|
50
74
|
setup_command: "peertable parent connect", codex_steer_command: "peertable parent connect --target codex",
|
|
@@ -57,7 +81,23 @@ export const PROFILE = {
|
|
|
57
81
|
};
|
|
58
82
|
```
|
|
59
83
|
|
|
60
|
-
|
|
84
|
+
| Field | Meaning |
|
|
85
|
+
| --- | --- |
|
|
86
|
+
| `id` | Product id. Used in state schema names and to tell your hooks apart. |
|
|
87
|
+
| `display_name` | Product name shown in error messages. |
|
|
88
|
+
| `setup_command`, `codex_steer_command` | The commands your users run to set up delivery. Error messages point to them. |
|
|
89
|
+
| `mcp_server` | Your MCP server's name as registered in the parent. Used for the Claude Code hook matcher and Cursor tool names. |
|
|
90
|
+
| `dispatch_tools` | The tools whose calls deliver to the parent. Claude Code hooks attach only to these. |
|
|
91
|
+
| `state_root` | Per-user runtime state for Claude Code and Cursor delivery records. |
|
|
92
|
+
| `config_root` | Per-user persistent config for Codex hook settings and ownership records. |
|
|
93
|
+
| `hooks` | File names of your hook entry files. They identify your hooks, so pick names no other product uses. |
|
|
94
|
+
| `codex_client_name`, `codex_hook_schema` | The client name you present to the Codex App Server, and your Codex hook config schema name. |
|
|
95
|
+
| `backup_suffix` | Suffix for the backup written before a config file is changed. |
|
|
96
|
+
| `channels` | Set this only if you send many messages to one conversation. `claude_expiry_notice` is a short text that wakes Claude Code to re-arm its waiter before the 24-hour hook limit. |
|
|
97
|
+
|
|
98
|
+
### 2. Ship the hook entry files
|
|
99
|
+
|
|
100
|
+
Hook entry files stay in your product, because their names identify your hooks and keep products from removing each other's hooks. Each is a two-line file:
|
|
61
101
|
|
|
62
102
|
```js
|
|
63
103
|
#!/usr/bin/env node
|
|
@@ -66,12 +106,92 @@ import { PROFILE } from "./profile.mjs";
|
|
|
66
106
|
await runClaudeHookMain(PROFILE);
|
|
67
107
|
```
|
|
68
108
|
|
|
69
|
-
|
|
109
|
+
Use `runCodexHookMain`, `runClaudeHookMain`, and `runCursorHookMain` for the three hook files. Background receivers use `runCursorReceiveMain` (one answer) or `runChannelReceiveMain` (channels).
|
|
110
|
+
|
|
111
|
+
### 3. Register the hooks
|
|
112
|
+
|
|
113
|
+
In your setup command:
|
|
114
|
+
|
|
115
|
+
- `mergeClaudeParentHooks(PROFILE, settingsFile, { command: nodePath, script: hookPath })` for Claude Code,
|
|
116
|
+
- `mergeCursorParentHooks(PROFILE, hooksFile, { command: nodePath, script: hookPath })` for Cursor,
|
|
117
|
+
- `configureCodexSteer(PROFILE, "enable", { hook: hookPath })` for Codex.
|
|
118
|
+
|
|
119
|
+
They keep other products' and the user's hooks and their order, and back up the file before writing. `removeClaudeParentHooks`, `removeCursorParentHooks`, and `configureCodexSteer(PROFILE, "disable", …)` undo them. `configureCodexSteer` returns `restart_required` when Codex processes that started before the hooks were installed are still running.
|
|
120
|
+
|
|
121
|
+
### 4. Deliver
|
|
122
|
+
|
|
123
|
+
There are two ways to deliver.
|
|
124
|
+
|
|
125
|
+
**One answer per request** (what Aiterm does): identify the parent from the MCP request, bind a delivery id, and submit the final text once.
|
|
126
|
+
|
|
127
|
+
```js
|
|
128
|
+
import * as steer from "aiterm-steer-delivery";
|
|
129
|
+
|
|
130
|
+
const parent = steer.codexParentFromRequest(clientName, request.params._meta);
|
|
131
|
+
await steer.verifyCodexParent(PROFILE, parent);
|
|
132
|
+
const { queued_submission_id } = await steer.submitCodexParentAnswer(PROFILE, parent, deliveryId, text);
|
|
133
|
+
```
|
|
134
|
+
|
|
135
|
+
Claude Code and Cursor follow the same shape with `claudeParentFromRequest` / `bindClaudeParentDelivery` / `submitClaudeParentAnswer` and `cursorParentFromRequest` / `prepareCursorDelivery` / `submitCursorParentAnswer`. Each `…ParentFromRequest` returns `null` when the caller is a different client, so you can try them in turn. For Cursor, put the delivery id in your tool result, as `parent_delivery: { delivery_id }` in `structuredContent` and as a `delivery_id=<uuid>` line in the text, so the hook can bind it to the conversation.
|
|
136
|
+
|
|
137
|
+
**Many messages to one conversation** (channels, e.g. Peertable's room messages to the parent):
|
|
138
|
+
|
|
139
|
+
```js
|
|
140
|
+
const channel = steer.openChannel(PROFILE, parent); // parent from the request; null for background-only
|
|
141
|
+
await steer.sendToChannel(PROFILE, channel.channel_id, randomUUID(), text);
|
|
142
|
+
steer.channelDeliveryState(PROFILE, channel.channel_id, id); // queued | sending | emitted | unknown | withdrawn
|
|
143
|
+
```
|
|
144
|
+
|
|
145
|
+
Claude Code re-arms its waiter at every `Stop`. Cursor binds the channel with `steer.channelMarker(channel)` placed in your tool result, and idle Cursor/Grok parents run `steer.channelReceiveProcess(...)` in the background; each result carries `next_wait_process` to re-arm. `withdrawFromChannel` takes back text no receiver has claimed yet, so you can resend it on a new channel; `closeChannel` ends one.
|
|
146
|
+
|
|
147
|
+
Put both the marker and the receiver command in the **text** of your tool result, not only in `structuredContent`: the Cursor CLI model does not read structured content. `steer.waitProcessCommandLine(steer.channelReceiveProcess(script, channel.channel_id))` gives the command as one shell line (POSIX sh, or PowerShell on Windows).
|
|
148
|
+
|
|
149
|
+
## Errors and delivery states
|
|
150
|
+
|
|
151
|
+
Delivery errors are `SteerDeliveryError` (`CodexDeliveryError`, `ClaudeDeliveryError`, `CursorDeliveryError`, `ChannelError`) with:
|
|
152
|
+
|
|
153
|
+
- `delivery_code`: a stable reason, for example `CLAUDE_PARENT_HOOK_UNAVAILABLE` or `CHANNEL_CLOSED`;
|
|
154
|
+
- `outcome_unknown`: `true` when the text may or may not have reached the parent. Do not resend automatically.
|
|
155
|
+
|
|
156
|
+
Setup failures are `SetupError`, whose `code` becomes your setup result's `reason_code`.
|
|
157
|
+
|
|
158
|
+
Channel delivery states:
|
|
159
|
+
|
|
160
|
+
| State | Meaning |
|
|
161
|
+
| --- | --- |
|
|
162
|
+
| `queued` | Saved in the channel inbox; no receiver has taken it yet. |
|
|
163
|
+
| `sending` | A receiver has claimed it and is writing it out. |
|
|
164
|
+
| `emitted` | Written to the parent. |
|
|
165
|
+
| `unknown` | The receiver stopped after claiming it; it may or may not have reached the parent. |
|
|
166
|
+
| `withdrawn` | Taken back with `withdrawFromChannel` before any receiver claimed it. |
|
|
167
|
+
|
|
168
|
+
On a Codex channel, `sendToChannel` returns `submitted` with the official queue's `queued_submission_id`, and `channelDeliveryState` returns `null`.
|
|
169
|
+
|
|
170
|
+
Known limit: Claude Code sessions that carry an `agent_id` (started with `--agent`, or native subagents) are not supported as parents.
|
|
70
171
|
|
|
71
172
|
## Non-Node products
|
|
72
173
|
|
|
73
174
|
`aiterm-steer-delivery --profile <profile.json> codex <parent|verify|submit|state|setup>` prints one JSON line. `state_root` and `config_root` are absolute paths in the JSON profile. `codex setup enable` registers the package's own Codex hook entry for your profile.
|
|
74
175
|
|
|
176
|
+
```sh
|
|
177
|
+
aiterm-steer-delivery --profile profile.json codex parent --client <name> --meta <json>
|
|
178
|
+
aiterm-steer-delivery --profile profile.json codex verify --thread <uuid> [--codex-home <dir>]
|
|
179
|
+
aiterm-steer-delivery --profile profile.json codex submit --thread <uuid> --delivery <uuid> --text-file <file|-> [--codex-home <dir>]
|
|
180
|
+
aiterm-steer-delivery --profile profile.json codex state --thread <uuid> --delivery <uuid> [--codex-home <dir>]
|
|
181
|
+
aiterm-steer-delivery --profile profile.json codex setup <enable|disable|status>
|
|
182
|
+
```
|
|
183
|
+
|
|
184
|
+
Success is `{"ok":true,...}`. Failure is `{"ok":false,"code":...,"message":...,"outcome_unknown":...}` with exit code 1.
|
|
185
|
+
|
|
186
|
+
## Development
|
|
187
|
+
|
|
188
|
+
```sh
|
|
189
|
+
npm ci
|
|
190
|
+
npm test # builds with tsc, then runs node --test
|
|
191
|
+
```
|
|
192
|
+
|
|
193
|
+
CI runs the tests on Ubuntu, macOS, and Windows. Pushing a `v<version>` tag publishes to npm with provenance. Changes are listed in [CHANGELOG.md](CHANGELOG.md).
|
|
194
|
+
|
|
75
195
|
## License
|
|
76
196
|
|
|
77
|
-
MIT
|
|
197
|
+
[MIT](LICENSE)
|
package/dist/cursor-receiver.js
CHANGED
|
@@ -6,6 +6,7 @@ import * as path from "node:path";
|
|
|
6
6
|
import { z } from "zod";
|
|
7
7
|
import { waitForFileState, withoutBom, writeJson0600 } from "./files.js";
|
|
8
8
|
import { CursorDeliveryError } from "./errors.js";
|
|
9
|
+
import { commandNamesFile } from "./hook-setup.js";
|
|
9
10
|
export const cursorParentSchema = z.object({
|
|
10
11
|
kind: z.literal("cursor"),
|
|
11
12
|
hook_root: z.string(),
|
|
@@ -33,7 +34,7 @@ export function cursorParentHooksRegistered(profile, document) {
|
|
|
33
34
|
if (entry === null || typeof entry !== "object")
|
|
34
35
|
return false;
|
|
35
36
|
const command = entry.command;
|
|
36
|
-
return typeof command === "string" && command
|
|
37
|
+
return typeof command === "string" && commandNamesFile(command, profile.hooks.cursor);
|
|
37
38
|
});
|
|
38
39
|
};
|
|
39
40
|
return owns("postToolUse") && owns("afterMCPExecution");
|
package/dist/hook-setup.d.ts
CHANGED
|
@@ -12,5 +12,10 @@ export declare function mergeClaudeParentHooks(profile: ProductProfile, file: st
|
|
|
12
12
|
/** hookを持たない旧版へ戻す前に、当製品の登録だけを除く。 */
|
|
13
13
|
export declare function removeClaudeParentHooks(profile: ProductProfile, file: string): "removed" | "unchanged";
|
|
14
14
|
export declare function cursorParentHookCommand(hook: HookRuntime): string;
|
|
15
|
+
/**
|
|
16
|
+
* commandが、その名前のfileを指しているか。名前の前はpathの区切りか引用符か空白、後ろは引用符か空白か末尾に限る。
|
|
17
|
+
* 部分一致で見ると、`cursor-parent-hook.js`が他製品の`gpt-connector-cursor-parent-hook.js`にも当たり、他製品のhookを消す。
|
|
18
|
+
*/
|
|
19
|
+
export declare function commandNamesFile(command: string, file: string): boolean;
|
|
15
20
|
export declare function mergeCursorParentHooks(profile: ProductProfile, file: string, hook: HookRuntime): "configured" | "unchanged";
|
|
16
21
|
export declare function removeCursorParentHooks(profile: ProductProfile, file: string): "removed" | "unchanged";
|
package/dist/hook-setup.js
CHANGED
|
@@ -42,6 +42,11 @@ export function mergeClaudeParentHooks(profile, file, hook) {
|
|
|
42
42
|
if (!Array.isArray(previous) || previous.some(group => !record(group) || !Array.isArray(group.hooks))) {
|
|
43
43
|
throw new SetupError("config_invalid", `Claudeの${event} hook形式を読めません`);
|
|
44
44
|
}
|
|
45
|
+
// 登録済みの中身が同じなら並びごと保つ。後から他製品が足したhookより後ろへ、自分のentryを移さない。
|
|
46
|
+
const owned = previous.flatMap(group => group.hooks.filter(entry => isClaudeParentHook(profile, entry)));
|
|
47
|
+
if (owned.length === additions.flatMap(group => group.hooks).length
|
|
48
|
+
&& additions.every(addition => previous.some(group => isDeepStrictEqual(group, addition))))
|
|
49
|
+
continue;
|
|
45
50
|
// 他製品のhookとmatcherはそのまま保ち、当製品の専用entryだけを更新する。
|
|
46
51
|
const retained = previous.map(group => ({ ...group, hooks: group.hooks.filter(entry => !isClaudeParentHook(profile, entry))
|
|
47
52
|
})).filter(group => group.hooks.length > 0);
|
|
@@ -120,8 +125,16 @@ export function cursorParentHookCommand(hook) {
|
|
|
120
125
|
const command = `${shellQuote(hook.command)} ${shellQuote(hook.script)}`;
|
|
121
126
|
return process.platform === "win32" ? `& ${command}` : command;
|
|
122
127
|
}
|
|
128
|
+
/**
|
|
129
|
+
* commandが、その名前のfileを指しているか。名前の前はpathの区切りか引用符か空白、後ろは引用符か空白か末尾に限る。
|
|
130
|
+
* 部分一致で見ると、`cursor-parent-hook.js`が他製品の`gpt-connector-cursor-parent-hook.js`にも当たり、他製品のhookを消す。
|
|
131
|
+
*/
|
|
132
|
+
export function commandNamesFile(command, file) {
|
|
133
|
+
const escaped = file.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
|
134
|
+
return new RegExp(`(?:^|[\\\\/'"\\s])${escaped}(?:['"\\s]|$)`, "u").test(command);
|
|
135
|
+
}
|
|
123
136
|
function ownsCursorParentHook(profile, hook) {
|
|
124
|
-
return record(hook) && typeof hook.command === "string" && hook.command
|
|
137
|
+
return record(hook) && typeof hook.command === "string" && commandNamesFile(hook.command, profile.hooks.cursor);
|
|
125
138
|
}
|
|
126
139
|
export function mergeCursorParentHooks(profile, file, hook) {
|
|
127
140
|
const target = existsSync(file) ? realpathSync(file) : file;
|
package/dist/index.d.ts
CHANGED
|
@@ -12,7 +12,7 @@ export { codexHookCommand, processCodexHome, mergeCodexParentHooks, planCodexPar
|
|
|
12
12
|
export { claudeParentSchema, prepareClaudeHookRequest, claudeParentFromRequest, verifyClaudeParent, bindClaudeParentDelivery, closeClaudeParentSession, submitClaudeParentAnswer, runClaudeResultHook, type ClaudeParent, } from "./claude-receiver.js";
|
|
13
13
|
export { cursorParentSchema, isCursorMcpClient, cursorHooksFile, cursorParentHooksRegistered, verifyCursorParent, cursorParentFromRequest, prepareCursorDelivery, submitCursorParentAnswer, handleCursorHook, receiveCursorAnswer, type CursorParent, type CursorReceiveResult, } from "./cursor-receiver.js";
|
|
14
14
|
export { cursorReceiveProcess, runCursorReceive, waitProcessCommandLine, type WaitProcess } from "./cursor-receive.js";
|
|
15
|
-
export { claudeParentHookEntries, mergeClaudeParentHooks, removeClaudeParentHooks, cursorParentHookCommand, mergeCursorParentHooks, removeCursorParentHooks, type HookRuntime, } from "./hook-setup.js";
|
|
15
|
+
export { claudeParentHookEntries, mergeClaudeParentHooks, removeClaudeParentHooks, cursorParentHookCommand, mergeCursorParentHooks, removeCursorParentHooks, commandNamesFile, type HookRuntime, } from "./hook-setup.js";
|
|
16
16
|
export { runCodexHookMain, runClaudeHookMain, runCursorHookMain, runCursorReceiveMain, isDirectExecution } from "./hook-main.js";
|
|
17
17
|
export { runChannelReceiveMain } from "./hook-main.js";
|
|
18
18
|
export { openChannel, readChannel, channelClosed, closeChannel, sendToChannel, channelDeliveryState, withdrawFromChannel, runClaudeChannelWaiter, closeClaudeSessionChannels, channelIdFromResult, channelMarker, handleCursorChannelHook, channelReceiveProcess, receiveFromChannel, ChannelError, type Channel, type ChannelKind, type ChannelDeliveryState, type ChannelReceiveResult, } from "./channel.js";
|
package/dist/index.js
CHANGED
|
@@ -14,7 +14,7 @@ export { codexHookCommand, processCodexHome, mergeCodexParentHooks, planCodexPar
|
|
|
14
14
|
export { claudeParentSchema, prepareClaudeHookRequest, claudeParentFromRequest, verifyClaudeParent, bindClaudeParentDelivery, closeClaudeParentSession, submitClaudeParentAnswer, runClaudeResultHook, } from "./claude-receiver.js";
|
|
15
15
|
export { cursorParentSchema, isCursorMcpClient, cursorHooksFile, cursorParentHooksRegistered, verifyCursorParent, cursorParentFromRequest, prepareCursorDelivery, submitCursorParentAnswer, handleCursorHook, receiveCursorAnswer, } from "./cursor-receiver.js";
|
|
16
16
|
export { cursorReceiveProcess, runCursorReceive, waitProcessCommandLine } from "./cursor-receive.js";
|
|
17
|
-
export { claudeParentHookEntries, mergeClaudeParentHooks, removeClaudeParentHooks, cursorParentHookCommand, mergeCursorParentHooks, removeCursorParentHooks, } from "./hook-setup.js";
|
|
17
|
+
export { claudeParentHookEntries, mergeClaudeParentHooks, removeClaudeParentHooks, cursorParentHookCommand, mergeCursorParentHooks, removeCursorParentHooks, commandNamesFile, } from "./hook-setup.js";
|
|
18
18
|
export { runCodexHookMain, runClaudeHookMain, runCursorHookMain, runCursorReceiveMain, isDirectExecution } from "./hook-main.js";
|
|
19
19
|
export { runChannelReceiveMain } from "./hook-main.js";
|
|
20
20
|
export { openChannel, readChannel, channelClosed, closeChannel, sendToChannel, channelDeliveryState, withdrawFromChannel, runClaudeChannelWaiter, closeClaudeSessionChannels, channelIdFromResult, channelMarker, handleCursorChannelHook, channelReceiveProcess, receiveFromChannel, ChannelError, } from "./channel.js";
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "aiterm-steer-delivery",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.10",
|
|
4
4
|
"description": "Aiterm's steer delivery as a library: deliver text to the parent AI session that called your MCP tool — steered into the running turn when it is busy, delivered normally when it is idle. Codex (official queue + sync hooks), Claude Code (asyncRewake hooks), Cursor (hooks + background receiver).",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"license": "MIT",
|