aiterm-steer-delivery 0.1.0

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.
Files changed (44) hide show
  1. package/CHANGELOG.md +8 -0
  2. package/LICENSE +22 -0
  3. package/README.md +75 -0
  4. package/dist/channel.d.ts +106 -0
  5. package/dist/channel.js +484 -0
  6. package/dist/claude-receiver.d.ts +20 -0
  7. package/dist/claude-receiver.js +171 -0
  8. package/dist/cli.d.ts +4 -0
  9. package/dist/cli.js +98 -0
  10. package/dist/codex-binary.d.ts +22 -0
  11. package/dist/codex-binary.js +176 -0
  12. package/dist/codex-hook-state.d.ts +45 -0
  13. package/dist/codex-hook-state.js +119 -0
  14. package/dist/codex-hook.d.ts +2 -0
  15. package/dist/codex-hook.js +13 -0
  16. package/dist/codex-hooks.d.ts +7 -0
  17. package/dist/codex-hooks.js +114 -0
  18. package/dist/codex-receiver.d.ts +20 -0
  19. package/dist/codex-receiver.js +145 -0
  20. package/dist/codex-setup.d.ts +35 -0
  21. package/dist/codex-setup.js +160 -0
  22. package/dist/cursor-receive.d.ts +9 -0
  23. package/dist/cursor-receive.js +31 -0
  24. package/dist/cursor-receiver.d.ts +29 -0
  25. package/dist/cursor-receiver.js +242 -0
  26. package/dist/errors.d.ts +17 -0
  27. package/dist/errors.js +26 -0
  28. package/dist/files.d.ts +4 -0
  29. package/dist/files.js +79 -0
  30. package/dist/hook-main.d.ts +16 -0
  31. package/dist/hook-main.js +139 -0
  32. package/dist/hook-setup.d.ts +16 -0
  33. package/dist/hook-setup.js +222 -0
  34. package/dist/index.d.ts +18 -0
  35. package/dist/index.js +20 -0
  36. package/dist/process.d.ts +17 -0
  37. package/dist/process.js +79 -0
  38. package/dist/profile.d.ts +42 -0
  39. package/dist/profile.js +14 -0
  40. package/dist/setup-node.d.ts +1 -0
  41. package/dist/setup-node.js +16 -0
  42. package/dist/windows.d.ts +13 -0
  43. package/dist/windows.js +76 -0
  44. package/package.json +28 -0
package/CHANGELOG.md ADDED
@@ -0,0 +1,8 @@
1
+ # Changelog
2
+
3
+ ## 0.1.0
4
+
5
+ - Aiterm(aiterm-mcp 0.42.2)の親配送を、挙動を変えずにライブラリとして切り出した。Codexの公式キュー+同期hook、Claude CodeのasyncRewake hook、Cursorのhook+背景受信。
6
+ - 製品ごとの違い(名前、保存場所、hook入口、案内文)を`ProductProfile`にまとめた。
7
+ - 同じ会話へ何通も送るchannelを追加した。Claude CodeはStop hookで待機を張り直す。Cursor・Grok等は背景の受信processで受け取り、`next_wait_process`で張り直す。
8
+ - Node以外の製品向けにCLI(`aiterm-steer-delivery`、Codex)を追加した。
package/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 kitepon
4
+ Copyright (c) 2026 kitepon-rgb (Windows Codex中継の移植部分)
5
+
6
+ Permission is hereby granted, free of charge, to any person obtaining a copy
7
+ of this software and associated documentation files (the "Software"), to deal
8
+ in the Software without restriction, including without limitation the rights
9
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10
+ copies of the Software, and to permit persons to whom the Software is
11
+ furnished to do so, subject to the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be included in all
14
+ copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,75 @@
1
+ # aiterm-steer-delivery
2
+
3
+ Aiterm's steer delivery as a library. Your MCP server can deliver text to the **parent AI session that called it**:
4
+
5
+ - if the parent is busy, the text is steered into its running turn;
6
+ - if the parent is idle, it arrives in the same conversation as a normal new turn.
7
+
8
+ This 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 products no longer have to rebuild conversation correlation, hook registration, and OS quirks each time.
9
+
10
+ | Parent | How the parent is identified | How text arrives |
11
+ | --- | --- | --- |
12
+ | Codex | `_meta.threadId` of the MCP request | Official App Server queue (`thread/queue/add`). On macOS/Windows your sync `PostToolUse`/`Stop` hooks pull it into the running turn; otherwise the official queue delivers it at the next turn boundary. |
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
+ | 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
+ | Others (Grok, …) | — | A background receiver process (`wait_process`). |
16
+
17
+ Delivery never auto-retries a send whose outcome is unknown (`outcome_unknown` / `unknown`).
18
+
19
+ ## Two ways to deliver
20
+
21
+ **One answer per request** (what Aiterm does): identify the parent from the MCP request, bind a delivery id, and submit the final text once.
22
+
23
+ ```js
24
+ import * as steer from "aiterm-steer-delivery";
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);
29
+ ```
30
+
31
+ **Many messages to one conversation** (channels, e.g. Peertable's room messages to the parent):
32
+
33
+ ```js
34
+ const channel = steer.openChannel(PROFILE, parent); // parent from the request; null for background-only
35
+ await steer.sendToChannel(PROFILE, channel.channel_id, randomUUID(), text);
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.
40
+
41
+ ## Product profile
42
+
43
+ Everything product-specific lives in one object. The delivery mechanism is the same for every product.
44
+
45
+ ```js
46
+ export const PROFILE = {
47
+ id: "peertable", display_name: "Peertable",
48
+ setup_command: "peertable parent connect", codex_steer_command: "peertable parent connect --target codex",
49
+ mcp_server: "peertable_parent", dispatch_tools: ["parent_join", "parent_leave"],
50
+ state_root: () => stateDir, config_root: () => configDir,
51
+ hooks: { codex: "peertable-parent-codex-hook.mjs", claude: "peertable-parent-claude-hook.mjs", cursor: "peertable-parent-cursor-hook.mjs" },
52
+ codex_client_name: "peertable_parent_delivery", codex_hook_schema: "peertable.codex-parent-hooks.v1",
53
+ backup_suffix: ".peertable-backup",
54
+ channels: { claude_expiry_notice: "..." }, // only for channel products
55
+ };
56
+ ```
57
+
58
+ Hook entry files stay in your product (their names identify your hooks, so products never remove each other's hooks). Each is a two-line file:
59
+
60
+ ```js
61
+ #!/usr/bin/env node
62
+ import { runClaudeHookMain } from "aiterm-steer-delivery";
63
+ import { PROFILE } from "./profile.mjs";
64
+ await runClaudeHookMain(PROFILE);
65
+ ```
66
+
67
+ Register them with `mergeClaudeParentHooks`, `mergeCursorParentHooks`, and `configureCodexSteer` (Codex Steer hooks are macOS/Windows, same as Aiterm).
68
+
69
+ ## Non-Node products
70
+
71
+ `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.
72
+
73
+ ## License
74
+
75
+ MIT
@@ -0,0 +1,106 @@
1
+ import { z } from "zod";
2
+ import { type ProductProfile } from "./profile.js";
3
+ import { SteerDeliveryError } from "./errors.js";
4
+ import { type CodexParent, type CodexReceiverRuntime } from "./codex-receiver.js";
5
+ import type { ClaudeParent } from "./claude-receiver.js";
6
+ import type { WaitProcess } from "./cursor-receive.js";
7
+ export type ChannelKind = "codex" | "claude" | "cursor" | "background";
8
+ declare const channelSchema: z.ZodObject<{
9
+ schema: z.ZodLiteral<"aiterm-steer.channel.v1">;
10
+ channel_id: z.ZodUUID;
11
+ kind: z.ZodEnum<{
12
+ codex: "codex";
13
+ claude: "claude";
14
+ cursor: "cursor";
15
+ background: "background";
16
+ }>;
17
+ created_at: z.ZodString;
18
+ codex: z.ZodOptional<z.ZodObject<{
19
+ thread_id: z.ZodUUID;
20
+ codex_home: z.ZodString;
21
+ }, z.core.$strict>>;
22
+ claude: z.ZodOptional<z.ZodObject<{
23
+ session_id: z.ZodUUID;
24
+ parent_pid: z.ZodNumber;
25
+ parent_started_identity: z.ZodString;
26
+ }, z.core.$strict>>;
27
+ }, z.core.$strict>;
28
+ export type Channel = z.infer<typeof channelSchema>;
29
+ export declare class ChannelError extends SteerDeliveryError {
30
+ }
31
+ export type ChannelDeliveryState = "queued" | "sending" | "emitted" | "unknown" | "withdrawn";
32
+ /**
33
+ * 親の会話にchannelを開く。parentは単発配送と同じ方法で特定したもの(codexParentFromRequest、claudeParentFromRequest、
34
+ * cursorParentFromRequest)。Grok等の特定できない親は null を渡し、背景受信だけのchannelにする。
35
+ */
36
+ export declare function openChannel(profile: ProductProfile, parent: CodexParent | ClaudeParent | {
37
+ kind: "cursor";
38
+ } | null): Channel;
39
+ export declare function readChannel(profile: ProductProfile, channelId: string): Channel;
40
+ export declare function channelClosed(profile: ProductProfile, channelId: string): boolean;
41
+ /** channelを閉じる。未取得の本文は残し、待機中の受け口は次の確認で終わる。 */
42
+ export declare function closeChannel(profile: ProductProfile, channelId: string, reason?: string): void;
43
+ /**
44
+ * 確定した本文を送る。Codexは公式キューへ入れて受付IDを返す(state=submitted)。
45
+ * それ以外はchannelの受信箱へ保存し(state=queued)、受け口が出力した時点でemittedになる。deliveryIdは送る本文ごとに新しいUUID。
46
+ */
47
+ export declare function sendToChannel(profile: ProductProfile, channelId: string, deliveryId: string, text: string, runtime?: CodexReceiverRuntime): Promise<{
48
+ state: "submitted" | "queued";
49
+ queued_submission_id: string | null;
50
+ }>;
51
+ /** 送った本文の現在の状態。Codexは公式キューの受付までを表す(submitted)。 */
52
+ export declare function channelDeliveryState(profile: ProductProfile, channelId: string, deliveryId: string): ChannelDeliveryState | null;
53
+ /**
54
+ * まだどの受け口も取っていない本文を受信箱から取り下げる。取り下げられたらtrue(親へは出ていない)。
55
+ * 受け口が先に取っていたらfalse。channelを張り直す時に、未取得の本文を新しいchannelへ送り直すために使う。
56
+ */
57
+ export declare function withdrawFromChannel(profile: ProductProfile, channelId: string, deliveryId: string): boolean;
58
+ /**
59
+ * Claude Codeのhook(PostToolUse・Stop、asyncRewake)から呼ぶ。その会話のchannelに届いた本文を待ち、
60
+ * 出力したら2(親を起こす)、待つものが無い・他の待機が生きている・会話が終わったなら0を返す。
61
+ */
62
+ export declare function runClaudeChannelWaiter(profile: ProductProfile, input: unknown, emit: (text: string) => Promise<void> | void, options?: {
63
+ wait_ms?: number;
64
+ poll_ms?: number;
65
+ }): Promise<0 | 2>;
66
+ /** Claude CodeのSessionEndで、その会話のchannelを閉じる。未取得の本文は残る。 */
67
+ export declare function closeClaudeSessionChannels(profile: ProductProfile, input: unknown): void;
68
+ /** tool結果(structuredContentのsteer_channel.channel_id、または本文の`steer_channel=<uuid>`行)からchannel IDを読む。 */
69
+ export declare function channelIdFromResult(value: unknown, depth?: number): string | null;
70
+ /** tool結果へ載せる印。CursorのhookはこれでchannelをCursorの実際の会話へ結ぶ。 */
71
+ export declare function channelMarker(channel: Channel): {
72
+ structured: {
73
+ steer_channel: {
74
+ channel_id: string;
75
+ };
76
+ };
77
+ text: string;
78
+ };
79
+ /**
80
+ * Cursorの公式hook(afterMCPExecution・postToolUse・postToolUseFailure)から呼ぶ。
81
+ * dispatch toolの結果にあるchannelをその会話へ結び、tool返りのhookでは結ばれたchannelの本文を差し込み用に返す。
82
+ */
83
+ export declare function handleCursorChannelHook(profile: ProductProfile, event: Record<string, unknown>, emit: (text: string) => Promise<void> | void): Promise<boolean>;
84
+ /** 親の背景process APIへそのまま渡す起動情報。scriptは製品のchannel受信入口の絶対path。 */
85
+ export declare function channelReceiveProcess(script: string, channelId: string, executable?: string): WaitProcess;
86
+ export type ChannelReceiveResult = {
87
+ outcome: "delivered";
88
+ channel_id: string;
89
+ deliveries: {
90
+ delivery_id: string;
91
+ text: string;
92
+ }[];
93
+ } | {
94
+ outcome: "timeout" | "closed";
95
+ channel_id: string;
96
+ };
97
+ /**
98
+ * 次に届いた本文を受け取って終わる。受け取った本文はこの時点でemittedになる(出力は呼び出し側が行う)。
99
+ * hookが先に差し込んだ本文は飛ばして、その次を待つ。
100
+ */
101
+ export declare function receiveFromChannel(profile: ProductProfile, channelId: string, options?: {
102
+ wait_ms?: number;
103
+ poll_ms?: number;
104
+ emit?: (result: ChannelReceiveResult) => Promise<void> | void;
105
+ }): Promise<ChannelReceiveResult>;
106
+ export {};