aiterm-steer-delivery 0.1.0 → 0.1.2
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 +11 -0
- package/README.md +2 -2
- package/dist/cli.js +2 -4
- package/dist/codex-binary.d.ts +10 -1
- package/dist/codex-binary.js +49 -4
- package/dist/codex-receiver.d.ts +7 -2
- package/dist/codex-receiver.js +3 -2
- package/dist/codex-setup.d.ts +21 -0
- package/dist/codex-setup.js +84 -14
- package/dist/index.d.ts +3 -3
- package/dist/index.js +2 -2
- package/package.json +43 -10
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,16 @@
|
|
|
1
1
|
# Changelog
|
|
2
2
|
|
|
3
|
+
## 0.1.2
|
|
4
|
+
|
|
5
|
+
- CodexのSteer(作業中のturnへの差し込み)を、Codex Desktopの無い端末とLinuxにも広げた(指揮官氏の判断 2026-09-29)。公式Desktopの同梱CLI(macOS・Windows・Linux)を先に探し、無ければ通常のCodex CLI(0.154以上)を使う。以前はmacOS・WindowsのDesktopだけで、Linuxは`unsupported`だった。
|
|
6
|
+
- 使っていたCodexが更新で消えた時の探し直しも、同じ順(Desktop、無ければCLI)で行う。
|
|
7
|
+
- npm版のCodex CLI(`node …/codex.js`とその中のnative本体)も、導入前から動いている「再起動が要るCodex」として照合する。
|
|
8
|
+
|
|
9
|
+
## 0.1.1
|
|
10
|
+
|
|
11
|
+
- Codex hookの解除(disable)で、自製品のhookのまとまりを抜くと後ろの他製品・利用者のhookの位置がずれ、Codexが位置の鍵で持つ承認と合わなくなって「modified」になり動かなくなっていた(紅蓮氏の報告、macbookでgrokbot-bridgeとpeertableのhookが止まった)。位置が動くhookの承認(trusted_hash・enabled)を、hooks.jsonを書き換える前に新しい位置へ写し、書き換え後に空いた位置の承認を消す。承認を新たに与えたり外したりはしない。公式Codexで確かめる試験を加えた。
|
|
12
|
+
- `verifyCodexParent`(CLIの`codex verify`)が、確認したthreadの`cwd`と`source`を返す。execの親かどうかの見分けに使える(call-bridgeの依頼)。
|
|
13
|
+
|
|
3
14
|
## 0.1.0
|
|
4
15
|
|
|
5
16
|
- Aiterm(aiterm-mcp 0.42.2)の親配送を、挙動を変えずにライブラリとして切り出した。Codexの公式キュー+同期hook、Claude CodeのasyncRewake hook、Cursorのhook+背景受信。
|
package/README.md
CHANGED
|
@@ -9,7 +9,7 @@ This is the delivery [Aiterm](https://github.com/kitepon/aiterm-mcp) uses to han
|
|
|
9
9
|
|
|
10
10
|
| Parent | How the parent is identified | How text arrives |
|
|
11
11
|
| --- | --- | --- |
|
|
12
|
-
| Codex | `_meta.threadId` of the MCP request | Official App Server queue (`thread/queue/add`).
|
|
12
|
+
| 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
13
|
| 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
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. |
|
|
15
15
|
| Others (Grok, …) | — | A background receiver process (`wait_process`). |
|
|
@@ -64,7 +64,7 @@ import { PROFILE } from "./profile.mjs";
|
|
|
64
64
|
await runClaudeHookMain(PROFILE);
|
|
65
65
|
```
|
|
66
66
|
|
|
67
|
-
Register them with `mergeClaudeParentHooks`, `mergeCursorParentHooks`, and `configureCodexSteer
|
|
67
|
+
Register them with `mergeClaudeParentHooks`, `mergeCursorParentHooks`, and `configureCodexSteer`. Codex Steer uses the Codex Desktop's bundled CLI when present (macOS, Windows, Linux) and otherwise the regular Codex CLI (0.154+).
|
|
68
68
|
|
|
69
69
|
## Non-Node products
|
|
70
70
|
|
package/dist/cli.js
CHANGED
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
// 製品の識別情報はJSONファイル(--profile)で渡す。state_root・config_rootは絶対pathの文字列にする。
|
|
4
4
|
//
|
|
5
5
|
// aiterm-steer-delivery --profile <file> codex parent --client <name> --meta <json>
|
|
6
|
-
// aiterm-steer-delivery --profile <file> codex verify --thread <uuid> [--codex-home <dir>]
|
|
6
|
+
// aiterm-steer-delivery --profile <file> codex verify --thread <uuid> [--codex-home <dir>] → thread{thread_id,cwd,source}
|
|
7
7
|
// aiterm-steer-delivery --profile <file> codex submit --thread <uuid> --delivery <uuid> --text-file <file|-> [--codex-home <dir>]
|
|
8
8
|
// aiterm-steer-delivery --profile <file> codex state --thread <uuid> --delivery <uuid> [--codex-home <dir>]
|
|
9
9
|
// aiterm-steer-delivery --profile <file> codex setup <enable|disable|status>
|
|
@@ -62,9 +62,7 @@ export async function runCli(argv) {
|
|
|
62
62
|
throw Object.assign(new Error("対応するのは codex だけです"), { delivery_code: "CLI_USAGE" });
|
|
63
63
|
switch (command) {
|
|
64
64
|
case "parent": return { parent: codexParentFromRequest(required(args, "--client"), JSON.parse(option(args, "--meta") ?? "null")) };
|
|
65
|
-
case "verify":
|
|
66
|
-
await verifyCodexParent(profile, parent(args));
|
|
67
|
-
return { verified: true };
|
|
65
|
+
case "verify": return { verified: true, thread: await verifyCodexParent(profile, parent(args)) };
|
|
68
66
|
case "submit": {
|
|
69
67
|
const target = parent(args);
|
|
70
68
|
const text = await readText(required(args, "--text-file"));
|
package/dist/codex-binary.d.ts
CHANGED
|
@@ -13,8 +13,17 @@ export declare function findDesktopBinary(): string;
|
|
|
13
13
|
export declare function findWindowsCodexCache(resources: string, cache: string): string;
|
|
14
14
|
/** Windowsの公式Codex Desktop(MSIX)が展開したCLI。 */
|
|
15
15
|
export declare function findWindowsCodexBinary(): string;
|
|
16
|
+
/** Linuxの公式Codex Desktop(deb・rpm)が同梱するCLI。 */
|
|
17
|
+
export declare function findLinuxDesktopBinary(candidates?: string[]): string;
|
|
18
|
+
/** Desktopの無い端末で使う、通常のCodex CLI。 */
|
|
19
|
+
export declare function findCliBinary(resolve?: () => string | null): string;
|
|
16
20
|
export type DesktopBinaryFinder = () => string;
|
|
17
|
-
export declare function platformDesktopFinder(): DesktopBinaryFinder;
|
|
21
|
+
export declare function platformDesktopFinder(platform?: NodeJS.Platform): DesktopBinaryFinder;
|
|
22
|
+
/**
|
|
23
|
+
* Steerのhookが公式キューを操作するのに使うCodex。公式Desktopの同梱CLIを先に探し、Desktopが無ければ
|
|
24
|
+
* 通常のCodex CLIを使う(どちらも同じ公式キューとhookを持つ)。
|
|
25
|
+
*/
|
|
26
|
+
export declare function findSteerBinary(desktop?: DesktopBinaryFinder, cli?: () => string): string;
|
|
18
27
|
export declare function currentCodexDesktopBinary(profile: ProductProfile, config: CodexHookConfig, options?: {
|
|
19
28
|
directory?: string;
|
|
20
29
|
find?: DesktopBinaryFinder;
|
package/dist/codex-binary.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
// Codexの実行ファイルとCODEX_HOMEの解決。Desktopが同梱するCLIは更新のたびに場所が変わることがあるので、
|
|
2
|
-
// setup
|
|
2
|
+
// setupで保存した場所が消えていたら、使う時点で同じ順(公式Desktop、無ければCodex CLI)で探し直す。探せなければ理由付きで止める。
|
|
3
3
|
import { spawnSync } from "node:child_process";
|
|
4
4
|
import { createHash } from "node:crypto";
|
|
5
5
|
import * as fs from "node:fs";
|
|
@@ -156,8 +156,53 @@ Join-Path $packages[0].InstallLocation 'app/resources'`, () => new CodexDelivery
|
|
|
156
156
|
throw new SetupError("codex_version_unsupported", "公式Codex Desktopの対応CLIを確認できません。");
|
|
157
157
|
return value;
|
|
158
158
|
}
|
|
159
|
-
|
|
160
|
-
|
|
159
|
+
/** CLIが対応する最低版(公式キューとhookの取込み)。 */
|
|
160
|
+
function assertSteerVersion(executable, subject) {
|
|
161
|
+
const result = spawnSync(executable, ["--version"], { encoding: "utf8", windowsHide: true, timeout: 15_000 });
|
|
162
|
+
if (result.error || result.status !== 0)
|
|
163
|
+
throw new SetupError("codex_binary_unavailable", `${subject}を起動できません`);
|
|
164
|
+
const match = /codex-cli (\d+)\.(\d+)\.(\d+)/.exec(result.stdout ?? "");
|
|
165
|
+
if (!match || (Number(match[1]) === 0 && Number(match[2]) < 154)) {
|
|
166
|
+
throw new SetupError("codex_version_unsupported", `Steerには公式キューとhookに対応するCodex CLI 0.154以上が必要です(${subject})`);
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
/** Linuxの公式Codex Desktop(deb・rpm)が同梱するCLI。 */
|
|
170
|
+
export function findLinuxDesktopBinary(candidates = ["/usr/lib/chatgpt/resources/codex", "/opt/ChatGPT/resources/codex"]) {
|
|
171
|
+
const found = candidates.filter(file => fs.existsSync(file));
|
|
172
|
+
if (found.length !== 1)
|
|
173
|
+
throw new SetupError("codex_desktop_not_identified", "Codex Desktopのインストール先を一つに特定できません");
|
|
174
|
+
assertSteerVersion(found[0], "Codex Desktopの同梱CLI");
|
|
175
|
+
return found[0];
|
|
176
|
+
}
|
|
177
|
+
/** Desktopの無い端末で使う、通常のCodex CLI。 */
|
|
178
|
+
export function findCliBinary(resolve = resolveCodexExecutable) {
|
|
179
|
+
const binary = resolve();
|
|
180
|
+
if (!binary)
|
|
181
|
+
throw new SetupError("codex_binary_unavailable", "Codex DesktopもCodex CLIも見つかりません");
|
|
182
|
+
assertSteerVersion(binary, "Codex CLI");
|
|
183
|
+
return binary;
|
|
184
|
+
}
|
|
185
|
+
export function platformDesktopFinder(platform = process.platform) {
|
|
186
|
+
return platform === "win32" ? findWindowsCodexBinary : platform === "darwin" ? findDesktopBinary : findLinuxDesktopBinary;
|
|
187
|
+
}
|
|
188
|
+
/**
|
|
189
|
+
* Steerのhookが公式キューを操作するのに使うCodex。公式Desktopの同梱CLIを先に探し、Desktopが無ければ
|
|
190
|
+
* 通常のCodex CLIを使う(どちらも同じ公式キューとhookを持つ)。
|
|
191
|
+
*/
|
|
192
|
+
export function findSteerBinary(desktop = platformDesktopFinder(), cli = () => findCliBinary()) {
|
|
193
|
+
try {
|
|
194
|
+
return desktop();
|
|
195
|
+
}
|
|
196
|
+
catch (desktopError) {
|
|
197
|
+
try {
|
|
198
|
+
return cli();
|
|
199
|
+
}
|
|
200
|
+
catch (cliError) {
|
|
201
|
+
if (cliError instanceof SetupError && cliError.code === "codex_binary_unavailable" && desktopError instanceof SetupError)
|
|
202
|
+
throw desktopError;
|
|
203
|
+
throw cliError;
|
|
204
|
+
}
|
|
205
|
+
}
|
|
161
206
|
}
|
|
162
207
|
export async function currentCodexDesktopBinary(profile, config, options = {}) {
|
|
163
208
|
const exists = options.exists ?? fs.existsSync;
|
|
@@ -165,7 +210,7 @@ export async function currentCodexDesktopBinary(profile, config, options = {}) {
|
|
|
165
210
|
return config.binary;
|
|
166
211
|
let binary;
|
|
167
212
|
try {
|
|
168
|
-
binary = (options.find ??
|
|
213
|
+
binary = (options.find ?? (() => findSteerBinary()))();
|
|
169
214
|
}
|
|
170
215
|
catch (error) {
|
|
171
216
|
throw new CodexDeliveryError("CODEX_DESKTOP_BINARY_MOVED", `Codex Desktopの更新で ${config.binary} が無くなり、新しい場所も特定できません(${error instanceof Error ? error.message : String(error)})。` +
|
package/dist/codex-receiver.d.ts
CHANGED
|
@@ -12,8 +12,13 @@ export interface CodexReceiverRuntime {
|
|
|
12
12
|
hook_directory?: string;
|
|
13
13
|
}
|
|
14
14
|
export declare function withCodexReceiver<T>(profile: ProductProfile, parent: CodexParent, action: (request: (method: string, params: unknown) => Promise<any>) => Promise<T>, runtime?: CodexReceiverRuntime): Promise<T>;
|
|
15
|
-
|
|
16
|
-
|
|
15
|
+
export interface CodexParentThread {
|
|
16
|
+
thread_id: string;
|
|
17
|
+
cwd: string | null;
|
|
18
|
+
source: unknown;
|
|
19
|
+
}
|
|
20
|
+
/** 送る前に、同じstoreの宛先と公式キューの対応を確認する。本文は保存・表示しない。確認したthreadのcwdとsourceを返す。 */
|
|
21
|
+
export declare function verifyCodexParent(profile: ProductProfile, parent: CodexParent, runtime?: CodexReceiverRuntime): Promise<CodexParentThread>;
|
|
17
22
|
/** 確定した本文を公式キューへ一度だけ入れる。受付IDを返す。受付の成否が不明な失敗はoutcome_unknownで区別する。 */
|
|
18
23
|
export declare function submitCodexParentAnswer(profile: ProductProfile, parent: CodexParent, deliveryId: string, text: string, runtime?: CodexReceiverRuntime): Promise<{
|
|
19
24
|
queued_submission_id: string | null;
|
package/dist/codex-receiver.js
CHANGED
|
@@ -100,9 +100,9 @@ export async function withCodexReceiver(profile, parent, action, runtime = {}) {
|
|
|
100
100
|
reader.close();
|
|
101
101
|
}
|
|
102
102
|
}
|
|
103
|
-
/** 送る前に、同じstore
|
|
103
|
+
/** 送る前に、同じstoreの宛先と公式キューの対応を確認する。本文は保存・表示しない。確認したthreadのcwdとsourceを返す。 */
|
|
104
104
|
export async function verifyCodexParent(profile, parent, runtime) {
|
|
105
|
-
await withCodexReceiver(profile, parent, async (request) => {
|
|
105
|
+
return await withCodexReceiver(profile, parent, async (request) => {
|
|
106
106
|
const response = await request("thread/read", { threadId: parent.thread_id, includeTurns: false });
|
|
107
107
|
if (response?.thread?.id !== parent.thread_id) {
|
|
108
108
|
throw new CodexDeliveryError("CODEX_PARENT_UNAVAILABLE", "同じCodex環境で親threadを確認できません");
|
|
@@ -117,6 +117,7 @@ export async function verifyCodexParent(profile, parent, runtime) {
|
|
|
117
117
|
assertCodexHookParentCurrent(profile, config);
|
|
118
118
|
assertCodexHooksReady(profile, await request("hooks/list", { cwds: [response.thread.cwd] }), config.command, path.join(parent.codex_home, "hooks.json"));
|
|
119
119
|
}
|
|
120
|
+
return { thread_id: parent.thread_id, cwd: typeof response.thread.cwd === "string" ? response.thread.cwd : null, source: response.thread.source ?? null };
|
|
120
121
|
}, runtime);
|
|
121
122
|
}
|
|
122
123
|
/** 確定した本文を公式キューへ一度だけ入れる。受付IDを返す。受付の成否が不明な失敗はoutcome_unknownで区別する。 */
|
package/dist/codex-setup.d.ts
CHANGED
|
@@ -7,7 +7,28 @@ export type CodexSteerResult = {
|
|
|
7
7
|
reason_code?: string;
|
|
8
8
|
};
|
|
9
9
|
export declare function codexHookCommand(node: string, hook: string, directory: string, platform?: NodeJS.Platform, powershell?: () => string): string;
|
|
10
|
+
/** hooks.jsonの書き換えで位置が動く他のhook。Codexは承認を「ファイル:イベント:まとまり:番号」の鍵で持つ。 */
|
|
11
|
+
export type CodexHookMove = {
|
|
12
|
+
event: string;
|
|
13
|
+
from: [number, number];
|
|
14
|
+
to: [number, number] | null;
|
|
15
|
+
};
|
|
16
|
+
export type CodexHookPlan = {
|
|
17
|
+
target: string;
|
|
18
|
+
current: any;
|
|
19
|
+
next: any;
|
|
20
|
+
changed: boolean;
|
|
21
|
+
moves: CodexHookMove[];
|
|
22
|
+
};
|
|
23
|
+
/** 自製品のhookを置換・解除した後のhooks.jsonと、他のhookの位置の動きを計算する(書き込まない)。 */
|
|
24
|
+
export declare function planCodexParentHooks(file: string, command: string | null, previousCommand?: string): CodexHookPlan;
|
|
25
|
+
export declare function writeCodexHookPlan(profile: ProductProfile, plan: CodexHookPlan): boolean;
|
|
10
26
|
export declare function mergeCodexParentHooks(profile: ProductProfile, file: string, command: string | null, previousCommand?: string): boolean;
|
|
27
|
+
/**
|
|
28
|
+
* 自製品のhookを書き換えて他のhookの位置が動く時、その承認(trusted_hash・enabled)を新しい位置へ写してから書き換え、
|
|
29
|
+
* 空いた位置の承認を消す。承認を新たに与えたり外したりはしない。位置が動かなければ公式APIを呼ばない。
|
|
30
|
+
*/
|
|
31
|
+
export declare function applyCodexHookPlan(profile: ProductProfile, plan: CodexHookPlan, codexHome: string, executable: string | undefined): Promise<boolean>;
|
|
11
32
|
export type CodexSteerRuntime = {
|
|
12
33
|
platform: string;
|
|
13
34
|
directory: string;
|
package/dist/codex-setup.js
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
import * as fs from "node:fs";
|
|
3
3
|
import * as path from "node:path";
|
|
4
4
|
import { isDeepStrictEqual } from "node:util";
|
|
5
|
-
import { currentCodexDesktopBinary,
|
|
5
|
+
import { currentCodexDesktopBinary, findSteerBinary, realCodexHome } from "./codex-binary.js";
|
|
6
6
|
import { withCodexReceiver } from "./codex-receiver.js";
|
|
7
7
|
import { SetupError } from "./errors.js";
|
|
8
8
|
import { setupNodeExecutable } from "./setup-node.js";
|
|
@@ -18,7 +18,8 @@ export function codexHookCommand(node, hook, directory, platform = process.platf
|
|
|
18
18
|
}
|
|
19
19
|
return [node, hook, directory].map(value => "'" + value.replace(/'/g, "'\"'\"'") + "'").join(" ");
|
|
20
20
|
}
|
|
21
|
-
|
|
21
|
+
/** 自製品のhookを置換・解除した後のhooks.jsonと、他のhookの位置の動きを計算する(書き込まない)。 */
|
|
22
|
+
export function planCodexParentHooks(file, command, previousCommand) {
|
|
22
23
|
if (!fs.existsSync(file)) {
|
|
23
24
|
try {
|
|
24
25
|
if (fs.lstatSync(file).isSymbolicLink())
|
|
@@ -43,16 +44,21 @@ export function mergeCodexParentHooks(profile, file, command, previousCommand) {
|
|
|
43
44
|
if (!object(current) || (current.hooks !== undefined && !object(current.hooks)))
|
|
44
45
|
throw new SetupError("codex_hook_config_invalid", "Codexのhooks設定を読めません");
|
|
45
46
|
const hooks = { ...current.hooks };
|
|
47
|
+
const moves = [];
|
|
46
48
|
for (const event of ["PostToolUse", "Stop"]) {
|
|
47
49
|
const groups = hooks[event] ?? [];
|
|
48
50
|
if (!Array.isArray(groups) || groups.some(group => !object(group) || !Array.isArray(group.hooks)))
|
|
49
51
|
throw new SetupError("codex_hook_config_invalid", `${event}のhook設定を読めません`);
|
|
52
|
+
const owned = (hook) => hook?.type === "command" && ((command !== null && hook.command === command) || (!!previousCommand && hook.command === previousCommand));
|
|
53
|
+
const before = new Map();
|
|
54
|
+
groups.forEach((group, g) => group.hooks.forEach((hook, h) => { if (!owned(hook))
|
|
55
|
+
before.set(hook, [g, h]); }));
|
|
50
56
|
// 既存の位置で置換する。末尾へ移すと同じ設定の再導入でも変更扱いになり、
|
|
51
57
|
// 稼働中のCodexへ不要な再起動を要求してしまう。
|
|
52
58
|
hooks[event] = [];
|
|
53
59
|
let insertionIndex = null;
|
|
54
60
|
for (const group of groups) {
|
|
55
|
-
const remaining = group.hooks.filter((hook) => !(hook
|
|
61
|
+
const remaining = group.hooks.filter((hook) => !owned(hook));
|
|
56
62
|
if (remaining.length !== group.hooks.length && insertionIndex === null)
|
|
57
63
|
insertionIndex = hooks[event].length;
|
|
58
64
|
if (remaining.length)
|
|
@@ -61,19 +67,74 @@ export function mergeCodexParentHooks(profile, file, command, previousCommand) {
|
|
|
61
67
|
if (command !== null)
|
|
62
68
|
hooks[event].splice(insertionIndex ?? hooks[event].length, 0, { ...(event === "PostToolUse" ? { matcher: ".*" } : {}),
|
|
63
69
|
hooks: [{ type: "command", command, timeout: 20, ...(event === "PostToolUse" ? { additionalContextLimit: 0 } : {}) }] });
|
|
70
|
+
// 他製品・利用者のhookの位置の動きを記録する(承認を新しい位置へ写すため)。
|
|
71
|
+
const after = new Map();
|
|
72
|
+
hooks[event].forEach((group, g) => group.hooks.forEach((hook, h) => after.set(hook, [g, h])));
|
|
73
|
+
for (const [hook, from] of before) {
|
|
74
|
+
const to = after.get(hook) ?? null;
|
|
75
|
+
if (!to || to[0] !== from[0] || to[1] !== from[1])
|
|
76
|
+
moves.push({ event, from, to });
|
|
77
|
+
}
|
|
64
78
|
if (!hooks[event].length)
|
|
65
79
|
delete hooks[event];
|
|
66
80
|
}
|
|
67
81
|
const next = { ...current, hooks };
|
|
68
|
-
|
|
82
|
+
return { target, current, next, changed: !isDeepStrictEqual(current, next), moves };
|
|
83
|
+
}
|
|
84
|
+
export function writeCodexHookPlan(profile, plan) {
|
|
85
|
+
if (!plan.changed)
|
|
69
86
|
return false;
|
|
70
|
-
if (fs.existsSync(target))
|
|
71
|
-
fs.copyFileSync(target, `${target}${profile.backup_suffix}`);
|
|
72
|
-
writeHookJson(target, next);
|
|
73
|
-
if (!isDeepStrictEqual(JSON.parse(fs.readFileSync(target, "utf8")), next))
|
|
87
|
+
if (fs.existsSync(plan.target))
|
|
88
|
+
fs.copyFileSync(plan.target, `${plan.target}${profile.backup_suffix}`);
|
|
89
|
+
writeHookJson(plan.target, plan.next);
|
|
90
|
+
if (!isDeepStrictEqual(JSON.parse(fs.readFileSync(plan.target, "utf8")), plan.next))
|
|
74
91
|
throw new SetupError("codex_hook_readback_failed", "Codex hookの読戻しが一致しません");
|
|
75
92
|
return true;
|
|
76
93
|
}
|
|
94
|
+
export function mergeCodexParentHooks(profile, file, command, previousCommand) {
|
|
95
|
+
return writeCodexHookPlan(profile, planCodexParentHooks(file, command, previousCommand));
|
|
96
|
+
}
|
|
97
|
+
const snake = (event) => event.replace(/([a-z0-9])([A-Z])/g, "$1_$2").toLowerCase();
|
|
98
|
+
/**
|
|
99
|
+
* 自製品のhookを書き換えて他のhookの位置が動く時、その承認(trusted_hash・enabled)を新しい位置へ写してから書き換え、
|
|
100
|
+
* 空いた位置の承認を消す。承認を新たに与えたり外したりはしない。位置が動かなければ公式APIを呼ばない。
|
|
101
|
+
*/
|
|
102
|
+
export async function applyCodexHookPlan(profile, plan, codexHome, executable) {
|
|
103
|
+
if (!plan.changed)
|
|
104
|
+
return false;
|
|
105
|
+
if (!plan.moves.length)
|
|
106
|
+
return writeCodexHookPlan(profile, plan);
|
|
107
|
+
return withCodexReceiver(profile, { thread_id: "00000000-0000-4000-8000-000000000000", codex_home: codexHome }, async (request) => {
|
|
108
|
+
const listed = await request("hooks/list", { cwds: [codexHome] });
|
|
109
|
+
const source = fs.realpathSync(plan.target);
|
|
110
|
+
const sample = (listed?.data?.[0]?.hooks ?? []).find((hook) => hook.sourcePath === source && typeof hook.key === "string");
|
|
111
|
+
if (!sample)
|
|
112
|
+
throw new SetupError("codex_hook_schema_unknown", "公式APIでhookの鍵を確認できません");
|
|
113
|
+
const prefix = String(sample.key).split(":").slice(0, -3).join(":");
|
|
114
|
+
const key = (event, [g, h]) => `${prefix}:${snake(event)}:${g}:${h}`;
|
|
115
|
+
const state = (await request("config/read", { includeLayers: false }))?.config?.hooks?.state ?? {};
|
|
116
|
+
const configFile = path.join(codexHome, "config.toml");
|
|
117
|
+
const field = (target, name, value) => ({ keyPath: `hooks.state.${JSON.stringify(target)}.${name}`, value, mergeStrategy: "replace" });
|
|
118
|
+
const copy = plan.moves.filter(move => move.to).flatMap(move => {
|
|
119
|
+
const saved = state[key(move.event, move.from)];
|
|
120
|
+
const target = key(move.event, move.to);
|
|
121
|
+
return saved && typeof saved === "object"
|
|
122
|
+
? [field(target, "trusted_hash", saved.trusted_hash ?? null), field(target, "enabled", saved.enabled ?? null)]
|
|
123
|
+
: [{ keyPath: `hooks.state.${JSON.stringify(target)}`, value: null, mergeStrategy: "replace" }];
|
|
124
|
+
});
|
|
125
|
+
if (copy.length)
|
|
126
|
+
await request("config/batchWrite", { edits: copy, filePath: configFile });
|
|
127
|
+
writeCodexHookPlan(profile, plan);
|
|
128
|
+
// 書き換え後に使われなくなった位置の承認を消す(他のhookへ写した値は残る)。
|
|
129
|
+
const used = new Set();
|
|
130
|
+
for (const event of ["PostToolUse", "Stop"])
|
|
131
|
+
(plan.next.hooks?.[event] ?? []).forEach((group, g) => group.hooks.forEach((_, h) => used.add(key(event, [g, h]))));
|
|
132
|
+
const vacated = plan.moves.map(move => key(move.event, move.from)).filter(old => !used.has(old) && state[old] !== undefined);
|
|
133
|
+
if (vacated.length)
|
|
134
|
+
await request("config/batchWrite", { edits: vacated.map(old => ({ keyPath: `hooks.state.${JSON.stringify(old)}`, value: null, mergeStrategy: "replace" })), filePath: configFile });
|
|
135
|
+
return true;
|
|
136
|
+
}, executable ? { executable } : {});
|
|
137
|
+
}
|
|
77
138
|
export async function verifyCodexHookRegistration(profile, config, approve, directory = codexHookDirectory(profile)) {
|
|
78
139
|
fs.accessSync(config.node, fs.constants.X_OK);
|
|
79
140
|
fs.accessSync(config.hook, fs.constants.R_OK);
|
|
@@ -101,7 +162,7 @@ export async function verifyCodexHookRegistration(profile, config, approve, dire
|
|
|
101
162
|
export async function configureCodexSteer(profile, action = "status", overrides) {
|
|
102
163
|
const directory = overrides.directory ?? codexHookDirectory(profile);
|
|
103
164
|
const runtime = { platform: process.platform, directory, codex_home: realCodexHome(),
|
|
104
|
-
node: process.execPath, findBinary:
|
|
165
|
+
node: process.execPath, findBinary: () => findSteerBinary(),
|
|
105
166
|
processes: readRuntimeProcesses, legacy: () => null,
|
|
106
167
|
disableLegacy: async () => ({ status: "disabled" }), verify: (config, approve) => verifyCodexHookRegistration(profile, config, approve, directory),
|
|
107
168
|
prepareDirectory: () => undefined, powershell: () => resolveWindowsPowerShell7(), ...overrides };
|
|
@@ -121,15 +182,13 @@ export async function configureCodexSteer(profile, action = "status", overrides)
|
|
|
121
182
|
}
|
|
122
183
|
if (action === "disable") {
|
|
123
184
|
if (previous?.enabled) {
|
|
124
|
-
|
|
185
|
+
await applyCodexHookPlan(profile, planCodexParentHooks(file, null, previous.command), previous.codex_home, await currentCodexDesktopBinary(profile, previous, { directory: runtime.directory }).catch(() => undefined));
|
|
125
186
|
save({ ...previous, enabled: false });
|
|
126
187
|
}
|
|
127
188
|
if (legacy?.enabled)
|
|
128
189
|
return runtime.disableLegacy();
|
|
129
190
|
return previous?.enabled ? { status: "restart_required", reason_code: "codex_restart_required" } : { status: "disabled" };
|
|
130
191
|
}
|
|
131
|
-
if (!["darwin", "win32"].includes(runtime.platform))
|
|
132
|
-
return { status: "unsupported", reason_code: "codex_steer_platform_unsupported" };
|
|
133
192
|
if (previous?.enabled && fs.realpathSync(previous.codex_home) !== fs.realpathSync(runtime.codex_home))
|
|
134
193
|
throw new SetupError("codex_steer_configuration_conflict", "別のCODEX_HOMEでSteerが有効です。先に既存の選択を解除してください");
|
|
135
194
|
const node = setupNodeExecutable(runtime.node);
|
|
@@ -137,17 +196,28 @@ export async function configureCodexSteer(profile, action = "status", overrides)
|
|
|
137
196
|
fs.accessSync(runtime.hook, fs.constants.R_OK);
|
|
138
197
|
const binary = runtime.findBinary();
|
|
139
198
|
const command = codexHookCommand(node, runtime.hook, runtime.directory, runtime.platform, runtime.powershell);
|
|
140
|
-
const changed =
|
|
199
|
+
const changed = await applyCodexHookPlan(profile, planCodexParentHooks(file, command, previous?.command), runtime.codex_home, binary);
|
|
141
200
|
// hook導入前から動いているCodexを再起動待ちとして記録する。今のDesktop、前回の登録、旧方式が起動したCodexを、
|
|
142
201
|
// 引用符付きのコマンド行も含めて照合する(Windowsは区切りと大文字小文字を揃える)。
|
|
143
202
|
const normalize = (value) => runtime.platform === "win32" ? value.replaceAll("/", "\\").toLowerCase() : value;
|
|
203
|
+
// npm版のCodex CLIは `node …/@openai/codex/bin/codex.js` と、その中のnative本体として動く。実体のpackageも照合する。
|
|
204
|
+
const packageRoot = (file) => {
|
|
205
|
+
let real = file;
|
|
206
|
+
try {
|
|
207
|
+
real = fs.realpathSync(file);
|
|
208
|
+
}
|
|
209
|
+
catch { /* 無ければそのまま */ }
|
|
210
|
+
return /[\\/]bin[\\/]codex\.js$/u.test(real) ? path.dirname(path.dirname(real)) : null;
|
|
211
|
+
};
|
|
212
|
+
const roots = [binary, legacy?.binary, previous?.binary].filter((value) => !!value).flatMap(file => packageRoot(file) ?? []).map(normalize);
|
|
144
213
|
const binaries = [binary, legacy?.binary, previous?.binary].filter((value) => !!value).map(normalize);
|
|
145
214
|
const stale = !previous?.enabled || changed || legacy?.enabled
|
|
146
215
|
? runtime.processes().filter(row => binaries.some(candidate => {
|
|
147
216
|
const command = normalize(row.command);
|
|
148
217
|
return (row.executable !== undefined && normalize(row.executable) === candidate) || command === candidate || command === `"${candidate}"`
|
|
149
218
|
|| command.startsWith(candidate + " ") || command.startsWith(`"${candidate}" `);
|
|
150
|
-
})
|
|
219
|
+
}) || roots.some(root => normalize(row.command).includes(root + (runtime.platform === "win32" ? "\\" : "/"))))
|
|
220
|
+
.map(({ pid, started_identity }) => ({ pid, started_identity }))
|
|
151
221
|
: previous.stale_processes;
|
|
152
222
|
const config = { schema: profile.codex_hook_schema, enabled: true,
|
|
153
223
|
codex_home: runtime.codex_home, binary, command, node, hook: runtime.hook, stale_processes: stale };
|
package/dist/index.d.ts
CHANGED
|
@@ -4,11 +4,11 @@ export { writeJson0600, writeHookJson, waitForFileState } from "./files.js";
|
|
|
4
4
|
export { readRuntimeProcesses, parsePosixProcessTable, type RuntimeProcess, type NativeProcessIdentity } from "./process.js";
|
|
5
5
|
export { resolveWindowsPowerShell7, quotePowerShell, windowsPowerShellSync, windowsStartProcessArgumentList } from "./windows.js";
|
|
6
6
|
export { setupNodeExecutable } from "./setup-node.js";
|
|
7
|
-
export { realCodexHome, resolveCodexExecutable, desktopBundledCodex, findDesktopBinary, findWindowsCodexCache, findWindowsCodexBinary, platformDesktopFinder, currentCodexDesktopBinary, type DesktopBinaryFinder, } from "./codex-binary.js";
|
|
7
|
+
export { realCodexHome, resolveCodexExecutable, desktopBundledCodex, findDesktopBinary, findWindowsCodexCache, findWindowsCodexBinary, findLinuxDesktopBinary, findCliBinary, findSteerBinary, platformDesktopFinder, currentCodexDesktopBinary, type DesktopBinaryFinder, } from "./codex-binary.js";
|
|
8
8
|
export { codexHookConfigSchema, readCodexHookConfig, codexInputDirectory, hookInputSchema, answerDigest, registerCodexHookInput, finishCodexHookSubmission, codexHookDeliveryState, ownedCodexHooks, assertCodexHooksReady, assertCodexHookParentCurrent, type CodexHookConfig, } from "./codex-hook-state.js";
|
|
9
|
-
export { codexParentFromRequest, withCodexReceiver, verifyCodexParent, submitCodexParentAnswer, type CodexParent, type CodexReceiverRuntime, } from "./codex-receiver.js";
|
|
9
|
+
export { codexParentFromRequest, withCodexReceiver, verifyCodexParent, submitCodexParentAnswer, type CodexParent, type CodexParentThread, type CodexReceiverRuntime, } from "./codex-receiver.js";
|
|
10
10
|
export { runCodexResultHook } from "./codex-hooks.js";
|
|
11
|
-
export { codexHookCommand, mergeCodexParentHooks, verifyCodexHookRegistration, configureCodexSteer, type CodexSteerAction, type CodexSteerResult, type CodexSteerRuntime, } from "./codex-setup.js";
|
|
11
|
+
export { codexHookCommand, mergeCodexParentHooks, planCodexParentHooks, writeCodexHookPlan, applyCodexHookPlan, verifyCodexHookRegistration, configureCodexSteer, type CodexHookMove, type CodexHookPlan, type CodexSteerAction, type CodexSteerResult, type CodexSteerRuntime, } from "./codex-setup.js";
|
|
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, type WaitProcess } from "./cursor-receive.js";
|
package/dist/index.js
CHANGED
|
@@ -6,11 +6,11 @@ export { writeJson0600, writeHookJson, waitForFileState } from "./files.js";
|
|
|
6
6
|
export { readRuntimeProcesses, parsePosixProcessTable } from "./process.js";
|
|
7
7
|
export { resolveWindowsPowerShell7, quotePowerShell, windowsPowerShellSync, windowsStartProcessArgumentList } from "./windows.js";
|
|
8
8
|
export { setupNodeExecutable } from "./setup-node.js";
|
|
9
|
-
export { realCodexHome, resolveCodexExecutable, desktopBundledCodex, findDesktopBinary, findWindowsCodexCache, findWindowsCodexBinary, platformDesktopFinder, currentCodexDesktopBinary, } from "./codex-binary.js";
|
|
9
|
+
export { realCodexHome, resolveCodexExecutable, desktopBundledCodex, findDesktopBinary, findWindowsCodexCache, findWindowsCodexBinary, findLinuxDesktopBinary, findCliBinary, findSteerBinary, platformDesktopFinder, currentCodexDesktopBinary, } from "./codex-binary.js";
|
|
10
10
|
export { codexHookConfigSchema, readCodexHookConfig, codexInputDirectory, hookInputSchema, answerDigest, registerCodexHookInput, finishCodexHookSubmission, codexHookDeliveryState, ownedCodexHooks, assertCodexHooksReady, assertCodexHookParentCurrent, } from "./codex-hook-state.js";
|
|
11
11
|
export { codexParentFromRequest, withCodexReceiver, verifyCodexParent, submitCodexParentAnswer, } from "./codex-receiver.js";
|
|
12
12
|
export { runCodexResultHook } from "./codex-hooks.js";
|
|
13
|
-
export { codexHookCommand, mergeCodexParentHooks, verifyCodexHookRegistration, configureCodexSteer, } from "./codex-setup.js";
|
|
13
|
+
export { codexHookCommand, mergeCodexParentHooks, planCodexParentHooks, writeCodexHookPlan, applyCodexHookPlan, verifyCodexHookRegistration, configureCodexSteer, } from "./codex-setup.js";
|
|
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 } from "./cursor-receive.js";
|
package/package.json
CHANGED
|
@@ -1,28 +1,61 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "aiterm-steer-delivery",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.2",
|
|
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",
|
|
7
|
-
"author": {
|
|
8
|
-
|
|
7
|
+
"author": {
|
|
8
|
+
"name": "Quo",
|
|
9
|
+
"url": "https://kitepon.dev"
|
|
10
|
+
},
|
|
11
|
+
"repository": {
|
|
12
|
+
"type": "git",
|
|
13
|
+
"url": "git+https://github.com/kitepon/aiterm-steer-delivery.git"
|
|
14
|
+
},
|
|
9
15
|
"homepage": "https://github.com/kitepon/aiterm-steer-delivery#readme",
|
|
10
16
|
"bugs": "https://github.com/kitepon/aiterm-steer-delivery/issues",
|
|
11
|
-
"keywords": [
|
|
12
|
-
|
|
17
|
+
"keywords": [
|
|
18
|
+
"mcp",
|
|
19
|
+
"codex",
|
|
20
|
+
"claude-code",
|
|
21
|
+
"cursor",
|
|
22
|
+
"steer",
|
|
23
|
+
"hooks",
|
|
24
|
+
"aiterm",
|
|
25
|
+
"ai-agents"
|
|
26
|
+
],
|
|
27
|
+
"engines": {
|
|
28
|
+
"node": ">=18"
|
|
29
|
+
},
|
|
13
30
|
"main": "./dist/index.js",
|
|
14
31
|
"types": "./dist/index.d.ts",
|
|
15
32
|
"exports": {
|
|
16
|
-
".": {
|
|
33
|
+
".": {
|
|
34
|
+
"types": "./dist/index.d.ts",
|
|
35
|
+
"default": "./dist/index.js"
|
|
36
|
+
},
|
|
17
37
|
"./package.json": "./package.json"
|
|
18
38
|
},
|
|
19
|
-
"bin": {
|
|
20
|
-
|
|
39
|
+
"bin": {
|
|
40
|
+
"aiterm-steer-delivery": "dist/cli.js"
|
|
41
|
+
},
|
|
42
|
+
"files": [
|
|
43
|
+
"dist/**/*.js",
|
|
44
|
+
"dist/**/*.d.ts",
|
|
45
|
+
"README.md",
|
|
46
|
+
"CHANGELOG.md",
|
|
47
|
+
"LICENSE"
|
|
48
|
+
],
|
|
21
49
|
"scripts": {
|
|
22
50
|
"build": "node -e \"require('fs').rmSync('dist',{recursive:true,force:true})\" && tsc",
|
|
23
51
|
"test": "npm run build && node --test test/*.test.mjs",
|
|
24
52
|
"prepublishOnly": "npm run build"
|
|
25
53
|
},
|
|
26
|
-
"dependencies": {
|
|
27
|
-
|
|
54
|
+
"dependencies": {
|
|
55
|
+
"zod": "^4.4.3"
|
|
56
|
+
},
|
|
57
|
+
"devDependencies": {
|
|
58
|
+
"@types/node": "^25.9.1",
|
|
59
|
+
"typescript": "^6.0.3"
|
|
60
|
+
}
|
|
28
61
|
}
|