aiterm-steer-delivery 0.2.1 → 0.2.3
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 +17 -0
- package/README.ja.md +2 -0
- package/README.md +2 -0
- package/dist/claude-receiver.d.ts +14 -0
- package/dist/claude-receiver.js +103 -5
- 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,22 @@
|
|
|
1
1
|
# Changelog
|
|
2
2
|
|
|
3
|
+
## 0.2.3
|
|
4
|
+
|
|
5
|
+
- **`discardClaudeHookRequest`を公開する。** 製品が、toolの返りを誤り(`isError`)にする時に、返す前に呼ぶ。その呼び出しの置き場(`<state_root>/claude-parent-hooks/<tool_use_id>/`)を消し、消したかを返す。Claude Codeは誤りの返りで`PostToolUse`を走らせないので、配送の無い呼び出しの片付けが走らず、`request.json`だけの置き場が1日後の見回りまで残っていた(連携元の本番で、引数の検査で断った呼び出しの置き場が残っているのを見た。Claude Code 2.1.291)。
|
|
6
|
+
- 配送を結んだ置き場(`delivery.json`がある)は消さない。送り手が待っているか、届かなかった時に原因を調べる材料になる。今までどおり見回りが後で消す。
|
|
7
|
+
- Claude Codeでない呼び出し元、`toolUseId`の無い要求、置き場の無い番号、この製品の依頼の記録でないfileが入ったdirectoryでは、何もせず`false`を返す。
|
|
8
|
+
- 投げない。消せなかった置き場は、今までどおり見回りが消す。
|
|
9
|
+
- 呼ばない製品の動きは変わらない。Claude側で断られた呼び出し(引数がtoolの定義に合わない)と打ち切られた呼び出しは、製品へ返りが渡らないので、今までどおり1日後の見回りが消す。
|
|
10
|
+
|
|
11
|
+
## 0.2.2
|
|
12
|
+
|
|
13
|
+
Claude Codeの親への単発配送で、依頼ごとの置き場(`<state_root>/claude-parent-hooks/<tool_use_id>/`)が届いた後も残っていたのを片付けます。APIと記録の形は変わりません。
|
|
14
|
+
|
|
15
|
+
- **届いた依頼の置き場を、送り手が消す。** `submitClaudeParentAnswer`は、受信hookが本文を出し終えた記録(`emitted.json`)を確かめた後に、置き場ごと消す。今までは届いた子1つにつき置き場が1つ残り、Windowsの一時置き場では再起動でも消えなかった(18日前の分が残っていた)。消せなかった時も配送は成功のまま返す。届かなかった時(誤りで返す時)は消さず、原因を調べる材料として残す。
|
|
16
|
+
- **届かないまま残った置き場を、PreToolUseのhookが見回って消す。** 誤りで返った呼び出しや打ち切られた呼び出しはPostToolUseが走らず、`request.json`だけの置き場が残っていた。消すのは、最後に書かれてから1日たち、待っている配送が無い置き場(配送を結んでいない、依頼元のClaude processが居ない、出し終えている、依頼元を読めない)。依頼元が居て配送を結んであり、まだ出し終えていない置き場は、何日たっても残す。この製品の依頼の記録でないfileが入ったdirectoryには触れない。process表は、hookが親を記録するために読んだものを使い、読む回数は増えない。
|
|
17
|
+
- **会話終了の見回り(`closeClaudeParentSession`)が、読めない`request.json`で止まらない。** 今までは1つでも読めないと例外で終わり、その会話の残りの依頼を閉じなかった。読めない記録と、消えている途中の置き場は飛ばす。
|
|
18
|
+
- 依頼元のClaude processが居なくなってから1日より後に子が終わった配送は、置き場が消えているので、`CLAUDE_PARENT_PROCESS_CLOSED`ではなく`CLAUDE_PARENT_HOOK_UNAVAILABLE`で返る(どちらも届けていない。OSの再起動で一時置き場が消えた後と同じ)。
|
|
19
|
+
|
|
3
20
|
## 0.2.1
|
|
4
21
|
|
|
5
22
|
- `withCodexReceiver`の`runtime`に`env`を足す。Codex App Serverを起こす環境を渡せる(省略すると、今までどおりこのprocessの環境)。`CODEX_HOME`は常に`parent.codex_home`で上書きする。Aitermが、認証sessionへ引き継いだ環境のままCodexのログインの状態を公式App Serverへ聞くために使う。
|
package/README.ja.md
CHANGED
|
@@ -145,6 +145,8 @@ const { queued_submission_id } = await steer.submitCodexParentAnswer(PROFILE, pa
|
|
|
145
145
|
|
|
146
146
|
Claude Code は `claudeParentFromRequest`/`bindClaudeParentDelivery`/`submitClaudeParentAnswer`、Cursor は `cursorParentFromRequest`/`prepareCursorDelivery`/`submitCursorParentAnswer` で同じ流れになります。各 `…ParentFromRequest` は呼び出し元が別のクライアントなら `null` を返すので、順に試せます。Cursor では、hook が会話に結び付けられるよう、ツール結果に配送IDを入れます。`structuredContent` には `parent_delivery: { delivery_id }`、本文には `delivery_id=<uuid>` の行を書きます。
|
|
147
147
|
|
|
148
|
+
Claude Code は、ツールの返りが誤り(`isError`)の時に `PostToolUse` の hook を走らせません。誤りで返す時は、返す前に `discardClaudeHookRequest(clientName, request.params._meta, claudeHookRoot(PROFILE))` を呼び、その呼び出しの置き場を消します。配送を結んだ置き場は消しません。呼ばなくても、残った置き場は1日後に次の依頼の hook が消します。
|
|
149
|
+
|
|
148
150
|
**同じ会話へ何通も送る**(channel。例: Peertable がルームの発言を親へ届ける):
|
|
149
151
|
|
|
150
152
|
```js
|
package/README.md
CHANGED
|
@@ -145,6 +145,8 @@ const { queued_submission_id } = await steer.submitCodexParentAnswer(PROFILE, pa
|
|
|
145
145
|
|
|
146
146
|
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.
|
|
147
147
|
|
|
148
|
+
Claude Code does not run the `PostToolUse` hook when a tool result is an error (`isError`). When you return an error, call `discardClaudeHookRequest(clientName, request.params._meta, claudeHookRoot(PROFILE))` before returning, to remove that call's request directory. A directory with a bound delivery is kept. If you do not call it, the next request's hook removes the leftover after one day.
|
|
149
|
+
|
|
148
150
|
**Many messages to one conversation** (channels, e.g. Peertable's room messages to the parent):
|
|
149
151
|
|
|
150
152
|
```js
|
|
@@ -9,6 +9,20 @@ export declare const claudeParentSchema: z.ZodObject<{
|
|
|
9
9
|
}, z.core.$strict>;
|
|
10
10
|
export type ClaudeParent = z.infer<typeof claudeParentSchema>;
|
|
11
11
|
export declare function prepareClaudeHookRequest(input: unknown, root: string): void;
|
|
12
|
+
/**
|
|
13
|
+
* 届かないまま残った依頼の置き場を消し、消した数を返す。誤りで返った呼び出しや打ち切られた呼び出しはPostToolUseが走らず、
|
|
14
|
+
* 置き場が残る。Windowsの一時置き場は再起動でも消えない。
|
|
15
|
+
* 消すのは、最後に書かれてから1日たち、待っている配送が無い置き場だけ。依頼元のClaude processが居て、配送を結んであり、
|
|
16
|
+
* まだ出し終えていない置き場は、何日たっても残す(子が長く動いている配送)。1日は、届かなかった時に原因を調べる間。
|
|
17
|
+
*/
|
|
18
|
+
export declare function sweepClaudeHookRequests(root: string, rows: readonly RuntimeProcess[], now?: number): number;
|
|
19
|
+
/**
|
|
20
|
+
* 誤りで返す呼び出しの置き場を消し、消したかを返す。製品は、toolの返りを誤り(`isError`)にする時に、返す前に呼ぶ。
|
|
21
|
+
* Claude Codeは誤りの返りでPostToolUseを走らせないので、配送の無い呼び出しの片付け(runClaudeResultHook)が走らない。
|
|
22
|
+
* 配送を結んだ置き場は消さない(届かなかった時に原因を調べる材料。見回りが後で消す)。
|
|
23
|
+
* 片付けの失敗でtoolの返りを変えないよう、投げない。消せなかった置き場は見回りが消す。
|
|
24
|
+
*/
|
|
25
|
+
export declare function discardClaudeHookRequest(clientName: string | undefined, metadata: unknown, root: string): Promise<boolean>;
|
|
12
26
|
/** 起動時envのsession IDは/clearで古くなるため、実際のPreToolUseとの相関だけを使う。 */
|
|
13
27
|
export declare function claudeParentFromRequest(profile: ProductProfile, clientName: string | undefined, metadata: unknown, root: string): ClaudeParent | null;
|
|
14
28
|
export declare function verifyClaudeParent(profile: ProductProfile, parent: ClaudeParent): void;
|
package/dist/claude-receiver.js
CHANGED
|
@@ -38,7 +38,8 @@ function assertOpen(parent) {
|
|
|
38
38
|
export function prepareClaudeHookRequest(input, root) {
|
|
39
39
|
const event = z.object({ session_id: z.uuid(), tool_use_id: requestId, agent_id: z.string().optional() }).parse(input);
|
|
40
40
|
// shellを通す形のhookでは、直接の親はshellになる。shellはこのhookと一緒に終わるので、その先のClaude processを記録する。
|
|
41
|
-
const
|
|
41
|
+
const rows = readRuntimeProcesses();
|
|
42
|
+
const owner = hookOwnerProcess(rows, process.ppid);
|
|
42
43
|
if (!owner)
|
|
43
44
|
throw new ClaudeDeliveryError("CLAUDE_PARENT_PROCESS_UNAVAILABLE", "hookを起動した親processを確認できません");
|
|
44
45
|
const dir = path.join(root, event.tool_use_id);
|
|
@@ -47,6 +48,83 @@ export function prepareClaudeHookRequest(input, root) {
|
|
|
47
48
|
request_id: event.tool_use_id, session_id: event.session_id, agent_id: event.agent_id ?? null,
|
|
48
49
|
parent_pid: owner.pid, parent_started_identity: owner.started_identity,
|
|
49
50
|
});
|
|
51
|
+
// 記録を置いた後に見回る。process表は上で読んだものを使う。見回りの失敗で依頼を止めない。
|
|
52
|
+
try {
|
|
53
|
+
sweepClaudeHookRequests(root, rows);
|
|
54
|
+
}
|
|
55
|
+
catch { /* 次の依頼でまた見回る */ }
|
|
56
|
+
}
|
|
57
|
+
// 届いた依頼の置き場は送り手が消す。見回りが消すのは、届かないまま残った分。
|
|
58
|
+
const STALE_REQUEST_MS = 24 * 60 * 60 * 1000;
|
|
59
|
+
const REQUEST_FILES = new Set(["request.json", "delivery.json", "answer.json", "hook.json", "sending.json", "emitted.json", "failed.json", "closed.json"]);
|
|
60
|
+
/**
|
|
61
|
+
* 届かないまま残った依頼の置き場を消し、消した数を返す。誤りで返った呼び出しや打ち切られた呼び出しはPostToolUseが走らず、
|
|
62
|
+
* 置き場が残る。Windowsの一時置き場は再起動でも消えない。
|
|
63
|
+
* 消すのは、最後に書かれてから1日たち、待っている配送が無い置き場だけ。依頼元のClaude processが居て、配送を結んであり、
|
|
64
|
+
* まだ出し終えていない置き場は、何日たっても残す(子が長く動いている配送)。1日は、届かなかった時に原因を調べる間。
|
|
65
|
+
*/
|
|
66
|
+
export function sweepClaudeHookRequests(root, rows, now = Date.now()) {
|
|
67
|
+
let entries;
|
|
68
|
+
try {
|
|
69
|
+
entries = fs.readdirSync(root, { withFileTypes: true });
|
|
70
|
+
}
|
|
71
|
+
catch {
|
|
72
|
+
return 0;
|
|
73
|
+
}
|
|
74
|
+
let removed = 0;
|
|
75
|
+
for (const entry of entries) {
|
|
76
|
+
if (!entry.isDirectory())
|
|
77
|
+
continue;
|
|
78
|
+
const dir = path.join(root, entry.name);
|
|
79
|
+
try {
|
|
80
|
+
if (now - fs.statSync(dir).mtimeMs < STALE_REQUEST_MS)
|
|
81
|
+
continue;
|
|
82
|
+
const names = fs.readdirSync(dir);
|
|
83
|
+
// この製品の依頼の置き場に見えない物には触れない。
|
|
84
|
+
if (!names.every(name => REQUEST_FILES.has(name) || name.endsWith(".tmp")))
|
|
85
|
+
continue;
|
|
86
|
+
if (names.some(name => now - fs.statSync(path.join(dir, name)).mtimeMs < STALE_REQUEST_MS))
|
|
87
|
+
continue;
|
|
88
|
+
if (names.includes("delivery.json") && !names.includes("emitted.json")) {
|
|
89
|
+
// 依頼元を読めない置き場には、もう誰も届けられない。
|
|
90
|
+
let invocation;
|
|
91
|
+
try {
|
|
92
|
+
invocation = invocationSchema.parse(JSON.parse(fs.readFileSync(path.join(dir, "request.json"), "utf8")));
|
|
93
|
+
}
|
|
94
|
+
catch { /* 消す */ }
|
|
95
|
+
if (invocation && rows.find(row => row.pid === invocation.parent_pid)?.started_identity === invocation.parent_started_identity)
|
|
96
|
+
continue;
|
|
97
|
+
}
|
|
98
|
+
fs.rmSync(dir, { recursive: true, force: true });
|
|
99
|
+
removed += 1;
|
|
100
|
+
}
|
|
101
|
+
catch { /* 読めない・消せない置き場は次の見回りへ回す */ }
|
|
102
|
+
}
|
|
103
|
+
return removed;
|
|
104
|
+
}
|
|
105
|
+
/**
|
|
106
|
+
* 誤りで返す呼び出しの置き場を消し、消したかを返す。製品は、toolの返りを誤り(`isError`)にする時に、返す前に呼ぶ。
|
|
107
|
+
* Claude Codeは誤りの返りでPostToolUseを走らせないので、配送の無い呼び出しの片付け(runClaudeResultHook)が走らない。
|
|
108
|
+
* 配送を結んだ置き場は消さない(届かなかった時に原因を調べる材料。見回りが後で消す)。
|
|
109
|
+
* 片付けの失敗でtoolの返りを変えないよう、投げない。消せなかった置き場は見回りが消す。
|
|
110
|
+
*/
|
|
111
|
+
export async function discardClaudeHookRequest(clientName, metadata, root) {
|
|
112
|
+
if (clientName !== "claude-code")
|
|
113
|
+
return false;
|
|
114
|
+
const parsed = requestId.safeParse(metadata?.["claudecode/toolUseId"]);
|
|
115
|
+
if (!parsed.success)
|
|
116
|
+
return false;
|
|
117
|
+
const dir = path.join(root, parsed.data);
|
|
118
|
+
try {
|
|
119
|
+
const names = fs.readdirSync(dir);
|
|
120
|
+
if (names.includes("delivery.json") || !names.every(name => REQUEST_FILES.has(name) || name.endsWith(".tmp")))
|
|
121
|
+
return false;
|
|
122
|
+
await fs.promises.rm(dir, { recursive: true, force: true, maxRetries: 3, retryDelay: 100 });
|
|
123
|
+
return true;
|
|
124
|
+
}
|
|
125
|
+
catch {
|
|
126
|
+
return false;
|
|
127
|
+
}
|
|
50
128
|
}
|
|
51
129
|
/** 起動時envのsession IDは/clearで古くなるため、実際のPreToolUseとの相関だけを使う。 */
|
|
52
130
|
export function claudeParentFromRequest(profile, clientName, metadata, root) {
|
|
@@ -81,16 +159,30 @@ export function closeClaudeParentSession(input, root) {
|
|
|
81
159
|
const { session_id } = z.object({ session_id: z.uuid() }).parse(input);
|
|
82
160
|
if (!fs.existsSync(root))
|
|
83
161
|
return;
|
|
162
|
+
let failure;
|
|
84
163
|
for (const entry of fs.readdirSync(root, { withFileTypes: true })) {
|
|
85
164
|
if (!entry.isDirectory())
|
|
86
165
|
continue;
|
|
87
|
-
|
|
88
|
-
|
|
166
|
+
// 読めない記録(形の違う古い残り、送り手や見回りが消している途中の置き場)は飛ばす。1つで止まると、この会話の残りの依頼を閉じられない。
|
|
167
|
+
let invocation;
|
|
168
|
+
try {
|
|
169
|
+
invocation = invocationSchema.parse(JSON.parse(fs.readFileSync(path.join(root, entry.name, "request.json"), "utf8")));
|
|
170
|
+
}
|
|
171
|
+
catch {
|
|
172
|
+
continue;
|
|
173
|
+
}
|
|
174
|
+
if (invocation.session_id !== session_id)
|
|
89
175
|
continue;
|
|
90
|
-
|
|
91
|
-
if (invocation.session_id === session_id)
|
|
176
|
+
try {
|
|
92
177
|
writeJson0600(path.join(root, entry.name, "closed.json"), { session_id });
|
|
178
|
+
}
|
|
179
|
+
catch (error) {
|
|
180
|
+
if (error.code !== "ENOENT")
|
|
181
|
+
failure ??= error;
|
|
182
|
+
}
|
|
93
183
|
}
|
|
184
|
+
if (failure)
|
|
185
|
+
throw failure;
|
|
94
186
|
}
|
|
95
187
|
function assertParentAlive(invocation) {
|
|
96
188
|
if (processIdentity(invocation.parent_pid) !== invocation.parent_started_identity) {
|
|
@@ -140,6 +232,12 @@ export async function submitClaudeParentAnswer(profile, parent, deliveryId, text
|
|
|
140
232
|
return true;
|
|
141
233
|
throw gone;
|
|
142
234
|
});
|
|
235
|
+
// 届いた依頼の置き場を読む物は、もう無い。届かなかった時は消さず、原因を調べる材料として残す。
|
|
236
|
+
// 片付けの失敗を配送の失敗にしない(消せなかった置き場は、後の見回りが消す)。
|
|
237
|
+
try {
|
|
238
|
+
await fs.promises.rm(dir, { recursive: true, force: true, maxRetries: 3, retryDelay: 100 });
|
|
239
|
+
}
|
|
240
|
+
catch { /* 見回りへ回す */ }
|
|
143
241
|
return { queued_submission_id: null };
|
|
144
242
|
}
|
|
145
243
|
export async function runClaudeResultHook(profile, input, emit, root) {
|
package/dist/index.d.ts
CHANGED
|
@@ -9,7 +9,7 @@ export { codexHookConfigSchema, readCodexHookConfig, codexInputDirectory, hookIn
|
|
|
9
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
11
|
export { codexHookCommand, processCodexHome, mergeCodexParentHooks, planCodexParentHooks, writeCodexHookPlan, applyCodexHookPlan, verifyCodexHookRegistration, configureCodexSteer, type CodexHookMove, type CodexHookPlan, type CodexSteerAction, type CodexSteerResult, type CodexSteerRuntime, } from "./codex-setup.js";
|
|
12
|
-
export { claudeParentSchema, prepareClaudeHookRequest, claudeParentFromRequest, verifyClaudeParent, bindClaudeParentDelivery, closeClaudeParentSession, submitClaudeParentAnswer, runClaudeResultHook, type ClaudeParent, } from "./claude-receiver.js";
|
|
12
|
+
export { claudeParentSchema, prepareClaudeHookRequest, claudeParentFromRequest, verifyClaudeParent, bindClaudeParentDelivery, closeClaudeParentSession, submitClaudeParentAnswer, runClaudeResultHook, discardClaudeHookRequest, type ClaudeParent, } from "./claude-receiver.js";
|
|
13
13
|
export { sendClaudeInbox, type ClaudeInboxTarget, type ClaudeInboxSendOptions, type ClaudeInboxSendResult, } from "./claude-inbox.js";
|
|
14
14
|
export { cursorParentSchema, isCursorMcpClient, cursorHooksFile, cursorParentHooksRegistered, verifyCursorParent, cursorParentFromRequest, prepareCursorDelivery, submitCursorParentAnswer, handleCursorHook, receiveCursorAnswer, type CursorParent, type CursorReceiveResult, type CursorReceiveReader, } from "./cursor-receiver.js";
|
|
15
15
|
export { cursorReceiveProcess, runCursorReceive, stdoutOutput, waitProcessCommandLine, type WaitProcess, type ReceiveOutput } from "./cursor-receive.js";
|
package/dist/index.js
CHANGED
|
@@ -11,7 +11,7 @@ export { codexHookConfigSchema, readCodexHookConfig, codexInputDirectory, hookIn
|
|
|
11
11
|
export { codexParentFromRequest, withCodexReceiver, verifyCodexParent, submitCodexParentAnswer, } from "./codex-receiver.js";
|
|
12
12
|
export { runCodexResultHook } from "./codex-hooks.js";
|
|
13
13
|
export { codexHookCommand, processCodexHome, mergeCodexParentHooks, planCodexParentHooks, writeCodexHookPlan, applyCodexHookPlan, verifyCodexHookRegistration, configureCodexSteer, } from "./codex-setup.js";
|
|
14
|
-
export { claudeParentSchema, prepareClaudeHookRequest, claudeParentFromRequest, verifyClaudeParent, bindClaudeParentDelivery, closeClaudeParentSession, submitClaudeParentAnswer, runClaudeResultHook, } from "./claude-receiver.js";
|
|
14
|
+
export { claudeParentSchema, prepareClaudeHookRequest, claudeParentFromRequest, verifyClaudeParent, bindClaudeParentDelivery, closeClaudeParentSession, submitClaudeParentAnswer, runClaudeResultHook, discardClaudeHookRequest, } from "./claude-receiver.js";
|
|
15
15
|
export { sendClaudeInbox, } from "./claude-inbox.js";
|
|
16
16
|
export { cursorParentSchema, isCursorMcpClient, cursorHooksFile, cursorParentHooksRegistered, verifyCursorParent, cursorParentFromRequest, prepareCursorDelivery, submitCursorParentAnswer, handleCursorHook, receiveCursorAnswer, } from "./cursor-receiver.js";
|
|
17
17
|
export { cursorReceiveProcess, runCursorReceive, stdoutOutput, waitProcessCommandLine } from "./cursor-receive.js";
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "aiterm-steer-delivery",
|
|
3
|
-
"version": "0.2.
|
|
3
|
+
"version": "0.2.3",
|
|
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",
|