peer-bridge 0.0.0-stage → 0.6.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.
- package/LICENSE +21 -0
- package/README.ko.md +134 -0
- package/README.md +161 -2
- package/adapters/antigravity/bin/agy-hook.mjs +24 -0
- package/adapters/antigravity/hooks.json +23 -0
- package/adapters/antigravity/mcp_config.json +8 -0
- package/adapters/antigravity/plugin.json +4 -0
- package/adapters/antigravity/rules/AGENTS.md +6 -0
- package/adapters/antigravity/skills/peer-bridge/SKILL.md +19 -0
- package/adapters/claude/.claude-plugin/plugin.json +5 -0
- package/adapters/claude/.mcp.json +8 -0
- package/adapters/claude/bin/cc-hook.mjs +20 -0
- package/adapters/claude/hooks/hooks.json +46 -0
- package/adapters/claude/skills/peer-bridge/SKILL.md +18 -0
- package/adapters/codex/bin/codex-hook.mjs +10 -0
- package/adapters/codex/config.toml.example +5 -0
- package/adapters/codex/hooks.json +44 -0
- package/adapters/codex/skills/peer-bridge/SKILL.md +12 -0
- package/adapters/gemini/bin/gemini-hook.mjs +10 -0
- package/adapters/gemini/settings.json +51 -0
- package/adapters/gemini/skills/peer-bridge/SKILL.md +12 -0
- package/adapters/paseo/SKILL.md +24 -0
- package/bin/pbridge-mcp.mjs +37 -0
- package/bin/pbridge-tui.mjs +7 -0
- package/bin/pbridge.mjs +5 -0
- package/bin/pbridged.mjs +16 -0
- package/package.json +42 -4
- package/src/adapters/antigravity/hooks.mjs +207 -0
- package/src/adapters/claude/hooks.mjs +204 -0
- package/src/adapters/codex/hooks.mjs +142 -0
- package/src/adapters/gemini/hooks.mjs +159 -0
- package/src/adapters/installer.mjs +328 -0
- package/src/cli/main.mjs +729 -0
- package/src/client/rpc.mjs +243 -0
- package/src/config.mjs +154 -0
- package/src/daemon/broker.mjs +1799 -0
- package/src/daemon/resolver.mjs +200 -0
- package/src/daemon/server.mjs +221 -0
- package/src/index.mjs +53 -0
- package/src/mcp/server.mjs +668 -0
- package/src/orchestrator/workflows.mjs +496 -0
- package/src/project.mjs +81 -0
- package/src/protocol.mjs +203 -0
- package/src/runtime/conv-api.mjs +101 -0
- package/src/runtime/host-discovery.mjs +635 -0
- package/src/runtime/native-claude.mjs +88 -0
- package/src/runtime/paseo.mjs +289 -0
- package/src/runtime/workers.mjs +751 -0
- package/src/storage/db.mjs +704 -0
- package/src/tui/render.mjs +472 -0
- package/src/tui/viewer.mjs +443 -0
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
---
|
|
2
|
+
name: peer-bridge
|
|
3
|
+
description: Cross-runtime peer messaging, task contracts, and workflow orchestration for Gemini CLI via peer-bridge.
|
|
4
|
+
---
|
|
5
|
+
|
|
6
|
+
# Peer-Bridge for Gemini CLI
|
|
7
|
+
|
|
8
|
+
Use the `peer-bridge` MCP tools (`list_peers`, `send_peer_message`, `delegate_task`, `complete_task`, `spawn_peer`, `close_peer`, `run_workflow`) to collaborate with Antigravity (`agy:*`), Claude Code (`cc:*`), Codex (`codex:*`), and Paseo (`paseo:*`) peers.
|
|
9
|
+
|
|
10
|
+
- Incoming `<peer-message>` and `<task-contract>` messages arrive automatically via Gemini CLI hooks (`SessionStart`, `BeforeAgent`, `AfterTool`, `AfterAgent`).
|
|
11
|
+
- When completing a `<task-contract request-id="...">`, call `complete_task` (or `pbridge task complete <request_id> "<summary>"`).
|
|
12
|
+
- Install into a project with `pbridge install gemini --project <dir>`.
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
---
|
|
2
|
+
name: peer-bridge-paseo
|
|
3
|
+
description: Bridge Paseo daemon agents into peer-bridge for cross-runtime messaging, task contracts, and workflow orchestration.
|
|
4
|
+
---
|
|
5
|
+
|
|
6
|
+
# Paseo Daemon Adapter (`runtime: "paseo"`)
|
|
7
|
+
|
|
8
|
+
`peer-bridge` integrates with the local or remote Paseo daemon (`paseo` CLI / `send_agent_prompt` semantics).
|
|
9
|
+
|
|
10
|
+
## Discover & Sync Existing Paseo Agents
|
|
11
|
+
```bash
|
|
12
|
+
pbridge paseo sync [--cwd <dir>] [--host <host>] [--all] [--json]
|
|
13
|
+
```
|
|
14
|
+
Synced agents appear as `paseo:<name>` (`origin="attached"`, `exec_mode="external"`). Messages sent to `paseo:<name>` are forwarded via `paseo send --behavior steer`.
|
|
15
|
+
|
|
16
|
+
## Spawn a Managed Paseo Worker
|
|
17
|
+
```bash
|
|
18
|
+
pbridge spawn --runtime paseo --name builder --provider codex/gpt-5.4 --cwd . --prompt "Implement module X"
|
|
19
|
+
```
|
|
20
|
+
Turn 1 launches the agent via `paseo run --label peer-bridge=spawned`; subsequent turns reuse its `agentId` via `paseo send --behavior steer`.
|
|
21
|
+
|
|
22
|
+
## Safe Lifecycle Closure
|
|
23
|
+
- Attached Paseo agents require `--policy detach` (or `--force`) to prevent accidentally stopping or deleting user-managed agents.
|
|
24
|
+
- Spawned Paseo workers stop gracefully on `close --policy archive|graceful` and hard-delete on `close --policy delete`.
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
import { startMcpServer } from '../src/mcp/server.mjs';
|
|
3
|
+
|
|
4
|
+
function parseFlags(argv) {
|
|
5
|
+
const flags = {};
|
|
6
|
+
for (let i = 0; i < argv.length; i++) {
|
|
7
|
+
const arg = argv[i];
|
|
8
|
+
if (arg.startsWith('--')) {
|
|
9
|
+
const eq = arg.indexOf('=');
|
|
10
|
+
if (eq !== -1) {
|
|
11
|
+
flags[arg.slice(2, eq)] = arg.slice(eq + 1);
|
|
12
|
+
} else {
|
|
13
|
+
const key = arg.slice(2);
|
|
14
|
+
const next = argv[i + 1];
|
|
15
|
+
if (next !== undefined && !next.startsWith('-')) {
|
|
16
|
+
flags[key] = next;
|
|
17
|
+
i++;
|
|
18
|
+
} else {
|
|
19
|
+
flags[key] = true;
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
return flags;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
const flags = parseFlags(process.argv.slice(2));
|
|
28
|
+
const runtime = flags.runtime || 'agy';
|
|
29
|
+
const enableChannel = Boolean(flags.channel) || runtime === 'cc';
|
|
30
|
+
|
|
31
|
+
startMcpServer({
|
|
32
|
+
runtime,
|
|
33
|
+
enableChannel,
|
|
34
|
+
peerName: typeof flags.name === 'string' ? flags.name : null,
|
|
35
|
+
sessionId: typeof flags.session === 'string' ? flags.session : null,
|
|
36
|
+
cwd: typeof flags.cwd === 'string' ? flags.cwd : process.cwd(),
|
|
37
|
+
});
|
package/bin/pbridge.mjs
ADDED
package/bin/pbridged.mjs
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
import { getConfig } from '../src/config.mjs';
|
|
3
|
+
import { BrokerServer } from '../src/daemon/server.mjs';
|
|
4
|
+
|
|
5
|
+
const config = getConfig();
|
|
6
|
+
const server = new BrokerServer({ config });
|
|
7
|
+
|
|
8
|
+
await server.start();
|
|
9
|
+
|
|
10
|
+
const shutdown = async () => {
|
|
11
|
+
await server.stop();
|
|
12
|
+
process.exit(0);
|
|
13
|
+
};
|
|
14
|
+
|
|
15
|
+
process.once('SIGINT', shutdown);
|
|
16
|
+
process.once('SIGTERM', shutdown);
|
package/package.json
CHANGED
|
@@ -1,6 +1,44 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "peer-bridge",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.6.0",
|
|
4
|
+
"description": "Zero-dependency local message broker, multi-AI orchestrator, MCP/Channel server, and standalone TUI viewer bridging Claude Code, Antigravity, Codex CLI, Gemini CLI, and Paseo sessions",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"license": "MIT",
|
|
7
|
+
"author": "Hwanyong Yoo",
|
|
8
|
+
"keywords": [
|
|
9
|
+
"ai-agents",
|
|
10
|
+
"claude-code",
|
|
11
|
+
"antigravity",
|
|
12
|
+
"codex",
|
|
13
|
+
"gemini-cli",
|
|
14
|
+
"paseo",
|
|
15
|
+
"mcp",
|
|
16
|
+
"multi-agent",
|
|
17
|
+
"orchestrator",
|
|
18
|
+
"tui"
|
|
19
|
+
],
|
|
20
|
+
"engines": {
|
|
21
|
+
"node": ">=22.13.0"
|
|
22
|
+
},
|
|
23
|
+
"files": [
|
|
24
|
+
"bin/",
|
|
25
|
+
"src/",
|
|
26
|
+
"adapters/",
|
|
27
|
+
"README.md",
|
|
28
|
+
"README.ko.md",
|
|
29
|
+
"LICENSE"
|
|
30
|
+
],
|
|
31
|
+
"bin": {
|
|
32
|
+
"pbridge": "./bin/pbridge.mjs",
|
|
33
|
+
"pbridged": "./bin/pbridged.mjs",
|
|
34
|
+
"pbridge-mcp": "./bin/pbridge-mcp.mjs",
|
|
35
|
+
"pbridge-tui": "./bin/pbridge-tui.mjs"
|
|
36
|
+
},
|
|
37
|
+
"exports": {
|
|
38
|
+
".": "./src/index.mjs"
|
|
39
|
+
},
|
|
40
|
+
"scripts": {
|
|
41
|
+
"test": "node --test test/unit/*.test.mjs",
|
|
42
|
+
"test:e2e": "node --test test/e2e/*.test.mjs"
|
|
43
|
+
}
|
|
44
|
+
}
|
|
@@ -0,0 +1,207 @@
|
|
|
1
|
+
import {
|
|
2
|
+
callRpc,
|
|
3
|
+
isDaemonRunning,
|
|
4
|
+
startDaemonDetached,
|
|
5
|
+
} from '../../client/rpc.mjs';
|
|
6
|
+
import { getConfig } from '../../config.mjs';
|
|
7
|
+
|
|
8
|
+
function parseInput(rawInput) {
|
|
9
|
+
if (!rawInput) return {};
|
|
10
|
+
if (typeof rawInput === 'object') return rawInput;
|
|
11
|
+
try {
|
|
12
|
+
return JSON.parse(String(rawInput));
|
|
13
|
+
} catch {
|
|
14
|
+
return {};
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
function sanitizeTitleToName(title) {
|
|
19
|
+
if (!title) return null;
|
|
20
|
+
const trimmed = String(title).trim();
|
|
21
|
+
if (!trimmed) return null;
|
|
22
|
+
const cleaned = trimmed
|
|
23
|
+
.replace(/^peer-bridge:/i, '')
|
|
24
|
+
.replace(/\s+/g, '-')
|
|
25
|
+
.replace(/[^a-zA-Z0-9._-가-힣]/g, '')
|
|
26
|
+
.slice(0, 40);
|
|
27
|
+
return cleaned || null;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
async function fetchSyncedTitle({ lsAddress, csrfToken, conversationId }) {
|
|
31
|
+
if (!lsAddress || !csrfToken || !conversationId) return null;
|
|
32
|
+
try {
|
|
33
|
+
const res = await fetch(
|
|
34
|
+
`http://${lsAddress}/product.v1.ConversationService/GetConversation`,
|
|
35
|
+
{
|
|
36
|
+
method: 'POST',
|
|
37
|
+
headers: {
|
|
38
|
+
'Content-Type': 'application/json',
|
|
39
|
+
'Connect-Protocol-Version': '1',
|
|
40
|
+
'x-antigravity-csrf-token': csrfToken,
|
|
41
|
+
},
|
|
42
|
+
body: JSON.stringify({ conversationId }),
|
|
43
|
+
signal: AbortSignal.timeout(400),
|
|
44
|
+
},
|
|
45
|
+
);
|
|
46
|
+
if (!res.ok) return null;
|
|
47
|
+
const json = await res.json();
|
|
48
|
+
const conv = json?.conversation || json;
|
|
49
|
+
return sanitizeTitleToName(conv?.annotations?.title || conv?.title);
|
|
50
|
+
} catch {
|
|
51
|
+
return null;
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
export async function runAntigravityHook(eventName, rawInput, options = {}) {
|
|
56
|
+
const input = parseInput(rawInput);
|
|
57
|
+
const event = eventName || input.hookEventName || '';
|
|
58
|
+
const config = getConfig(options.config || {});
|
|
59
|
+
const autoStartDaemon =
|
|
60
|
+
options.autoStartDaemon !== undefined
|
|
61
|
+
? Boolean(options.autoStartDaemon)
|
|
62
|
+
: process.env.PBRIDGE_AUTO_START !== '0';
|
|
63
|
+
|
|
64
|
+
const conversationId = input.conversationId || process.env.ANTIGRAVITY_CONVERSATION_ID || null;
|
|
65
|
+
const cwd =
|
|
66
|
+
input.cwd ||
|
|
67
|
+
(Array.isArray(input.workspacePaths) ? input.workspacePaths[0] : null) ||
|
|
68
|
+
process.cwd();
|
|
69
|
+
const lsAddress = options.lsAddress ?? process.env.ANTIGRAVITY_LS_ADDRESS ?? null;
|
|
70
|
+
const csrfToken = options.csrfToken ?? process.env.ANTIGRAVITY_CSRF_TOKEN ?? null;
|
|
71
|
+
const projectId = options.projectId ?? process.env.ANTIGRAVITY_PROJECT_ID ?? null;
|
|
72
|
+
|
|
73
|
+
const ensureDaemonOrSkip = async () => {
|
|
74
|
+
if (await isDaemonRunning({ config })) return true;
|
|
75
|
+
if (!autoStartDaemon) return false;
|
|
76
|
+
try {
|
|
77
|
+
await startDaemonDetached({ config });
|
|
78
|
+
return true;
|
|
79
|
+
} catch {
|
|
80
|
+
return false;
|
|
81
|
+
}
|
|
82
|
+
};
|
|
83
|
+
|
|
84
|
+
const registerOrHeartbeat = async (state, summary = undefined) => {
|
|
85
|
+
if (!conversationId) return;
|
|
86
|
+
try {
|
|
87
|
+
await callRpc(
|
|
88
|
+
'peer.heartbeat',
|
|
89
|
+
{
|
|
90
|
+
session_id: conversationId,
|
|
91
|
+
runtime: 'agy',
|
|
92
|
+
state,
|
|
93
|
+
ls_address: lsAddress,
|
|
94
|
+
csrf_token: csrfToken,
|
|
95
|
+
project_id: projectId,
|
|
96
|
+
cwd,
|
|
97
|
+
pid: process.ppid,
|
|
98
|
+
...(summary ? { summary } : {}),
|
|
99
|
+
},
|
|
100
|
+
{ config, timeoutMs: 2500 },
|
|
101
|
+
);
|
|
102
|
+
} catch {
|
|
103
|
+
const syncedTitle = await fetchSyncedTitle({ lsAddress, csrfToken, conversationId });
|
|
104
|
+
const name =
|
|
105
|
+
syncedTitle ||
|
|
106
|
+
process.env.PBRIDGE_PEER_NAME ||
|
|
107
|
+
`agy-${String(conversationId).slice(0, 8)}`;
|
|
108
|
+
await callRpc(
|
|
109
|
+
'peer.register',
|
|
110
|
+
{
|
|
111
|
+
runtime: 'agy',
|
|
112
|
+
name,
|
|
113
|
+
session_id: conversationId,
|
|
114
|
+
origin: 'attached',
|
|
115
|
+
exec_mode: 'interactive',
|
|
116
|
+
state,
|
|
117
|
+
cwd,
|
|
118
|
+
ls_address: lsAddress,
|
|
119
|
+
csrf_token: csrfToken,
|
|
120
|
+
project_id: projectId,
|
|
121
|
+
pid: process.ppid,
|
|
122
|
+
},
|
|
123
|
+
{ config, timeoutMs: 2500 },
|
|
124
|
+
).catch(() => {});
|
|
125
|
+
}
|
|
126
|
+
};
|
|
127
|
+
|
|
128
|
+
if (event === 'SessionStart') {
|
|
129
|
+
if (!conversationId || !(await ensureDaemonOrSkip())) return {};
|
|
130
|
+
const syncedTitle = await fetchSyncedTitle({ lsAddress, csrfToken, conversationId });
|
|
131
|
+
const name =
|
|
132
|
+
syncedTitle ||
|
|
133
|
+
process.env.PBRIDGE_PEER_NAME ||
|
|
134
|
+
`agy-${String(conversationId).slice(0, 8)}`;
|
|
135
|
+
await callRpc(
|
|
136
|
+
'peer.register',
|
|
137
|
+
{
|
|
138
|
+
runtime: 'agy',
|
|
139
|
+
name,
|
|
140
|
+
session_id: conversationId,
|
|
141
|
+
origin: 'attached',
|
|
142
|
+
exec_mode: 'interactive',
|
|
143
|
+
state: 'idle',
|
|
144
|
+
cwd,
|
|
145
|
+
ls_address: lsAddress,
|
|
146
|
+
csrf_token: csrfToken,
|
|
147
|
+
project_id: projectId,
|
|
148
|
+
pid: process.ppid,
|
|
149
|
+
},
|
|
150
|
+
{ config, timeoutMs: 3000 },
|
|
151
|
+
).catch(() => {});
|
|
152
|
+
|
|
153
|
+
const polled = await callRpc(
|
|
154
|
+
'message.poll',
|
|
155
|
+
{ session_id: conversationId, runtime: 'agy' },
|
|
156
|
+
{ config, timeoutMs: 2500 },
|
|
157
|
+
).catch(() => ({ count: 0, messages: [] }));
|
|
158
|
+
|
|
159
|
+
if (polled.count > 0) {
|
|
160
|
+
const joined = polled.messages.map((m) => m.formatted_xml).join('\n\n');
|
|
161
|
+
return { injectSteps: [{ userMessage: joined }] };
|
|
162
|
+
}
|
|
163
|
+
return {};
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
if (event === 'PostInvocation') {
|
|
167
|
+
if (!conversationId || !(await ensureDaemonOrSkip())) return {};
|
|
168
|
+
await registerOrHeartbeat('busy', input.response || input.summary || undefined);
|
|
169
|
+
const polled = await callRpc(
|
|
170
|
+
'message.poll',
|
|
171
|
+
{ session_id: conversationId, runtime: 'agy' },
|
|
172
|
+
{ config, timeoutMs: 2500 },
|
|
173
|
+
).catch(() => ({ count: 0, messages: [] }));
|
|
174
|
+
|
|
175
|
+
if (polled.count > 0) {
|
|
176
|
+
const joined = polled.messages.map((m) => m.formatted_xml).join('\n\n');
|
|
177
|
+
return {
|
|
178
|
+
injectSteps: [{ userMessage: joined }],
|
|
179
|
+
terminationBehavior: 'force_continue',
|
|
180
|
+
};
|
|
181
|
+
}
|
|
182
|
+
return {};
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
if (event === 'Stop') {
|
|
186
|
+
if (!conversationId || !(await ensureDaemonOrSkip())) {
|
|
187
|
+
return { decision: 'allow' };
|
|
188
|
+
}
|
|
189
|
+
await registerOrHeartbeat('idle', input.response || input.summary || undefined);
|
|
190
|
+
if (input.terminationReason === 'ERROR') {
|
|
191
|
+
return { decision: 'allow' };
|
|
192
|
+
}
|
|
193
|
+
const polled = await callRpc(
|
|
194
|
+
'message.poll',
|
|
195
|
+
{ session_id: conversationId, runtime: 'agy' },
|
|
196
|
+
{ config, timeoutMs: 2500 },
|
|
197
|
+
).catch(() => ({ count: 0, messages: [] }));
|
|
198
|
+
|
|
199
|
+
if (polled.count > 0) {
|
|
200
|
+
const joined = polled.messages.map((m) => m.formatted_xml).join('\n\n');
|
|
201
|
+
return { decision: 'continue', reason: joined };
|
|
202
|
+
}
|
|
203
|
+
return { decision: 'allow' };
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
return {};
|
|
207
|
+
}
|
|
@@ -0,0 +1,204 @@
|
|
|
1
|
+
import {
|
|
2
|
+
callRpc,
|
|
3
|
+
isDaemonRunning,
|
|
4
|
+
startDaemonDetached,
|
|
5
|
+
} from '../../client/rpc.mjs';
|
|
6
|
+
import { getConfig } from '../../config.mjs';
|
|
7
|
+
import { parseAddress } from '../../protocol.mjs';
|
|
8
|
+
import {
|
|
9
|
+
matchNativeClaudePeers,
|
|
10
|
+
scanNativeClaudePeers,
|
|
11
|
+
} from '../../runtime/native-claude.mjs';
|
|
12
|
+
|
|
13
|
+
function parseInput(rawInput) {
|
|
14
|
+
if (!rawInput) return {};
|
|
15
|
+
if (typeof rawInput === 'object') return rawInput;
|
|
16
|
+
try {
|
|
17
|
+
return JSON.parse(String(rawInput));
|
|
18
|
+
} catch {
|
|
19
|
+
return {};
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
function allowPreToolUse(reason) {
|
|
24
|
+
return {
|
|
25
|
+
hookSpecificOutput: {
|
|
26
|
+
hookEventName: 'PreToolUse',
|
|
27
|
+
permissionDecision: 'allow',
|
|
28
|
+
...(reason ? { permissionDecisionReason: reason } : {}),
|
|
29
|
+
},
|
|
30
|
+
};
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
function denyPreToolUse(reason, additionalContext) {
|
|
34
|
+
return {
|
|
35
|
+
hookSpecificOutput: {
|
|
36
|
+
hookEventName: 'PreToolUse',
|
|
37
|
+
permissionDecision: 'deny',
|
|
38
|
+
permissionDecisionReason: reason,
|
|
39
|
+
...(additionalContext !== undefined ? { additionalContext } : {}),
|
|
40
|
+
},
|
|
41
|
+
};
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
export async function runClaudeHook(eventName, rawInput, options = {}) {
|
|
45
|
+
const input = parseInput(rawInput);
|
|
46
|
+
const event = eventName || input.hook_event_name || '';
|
|
47
|
+
const config = getConfig(options.config || {});
|
|
48
|
+
const autoStartDaemon =
|
|
49
|
+
options.autoStartDaemon !== undefined
|
|
50
|
+
? Boolean(options.autoStartDaemon)
|
|
51
|
+
: process.env.PBRIDGE_AUTO_START !== '0';
|
|
52
|
+
|
|
53
|
+
const ensureDaemonOrSkip = async () => {
|
|
54
|
+
if (await isDaemonRunning({ config })) return true;
|
|
55
|
+
if (!autoStartDaemon) return false;
|
|
56
|
+
try {
|
|
57
|
+
await startDaemonDetached({ config });
|
|
58
|
+
return true;
|
|
59
|
+
} catch {
|
|
60
|
+
return false;
|
|
61
|
+
}
|
|
62
|
+
};
|
|
63
|
+
|
|
64
|
+
if (event === 'SessionStart') {
|
|
65
|
+
if (!(await ensureDaemonOrSkip())) return {};
|
|
66
|
+
const sessionId = input.session_id || null;
|
|
67
|
+
const name =
|
|
68
|
+
process.env.PBRIDGE_PEER_NAME ||
|
|
69
|
+
(sessionId ? `cc-${String(sessionId).slice(0, 8)}` : 'claude');
|
|
70
|
+
await callRpc(
|
|
71
|
+
'peer.register',
|
|
72
|
+
{
|
|
73
|
+
runtime: 'cc',
|
|
74
|
+
name,
|
|
75
|
+
session_id: sessionId,
|
|
76
|
+
origin: 'attached',
|
|
77
|
+
exec_mode: 'interactive',
|
|
78
|
+
state: 'idle',
|
|
79
|
+
cwd: input.cwd || process.cwd(),
|
|
80
|
+
pid: process.ppid,
|
|
81
|
+
},
|
|
82
|
+
{ config, timeoutMs: 3000 },
|
|
83
|
+
).catch(() => {});
|
|
84
|
+
return {};
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
if (event === 'PreToolUse') {
|
|
88
|
+
if (input.tool_name && input.tool_name !== 'SendMessage') {
|
|
89
|
+
return {};
|
|
90
|
+
}
|
|
91
|
+
const ti = input.tool_input || {};
|
|
92
|
+
const target = String(ti.to ?? ti.recipient ?? '').trim();
|
|
93
|
+
const body = String(ti.message ?? ti.content ?? '').trim();
|
|
94
|
+
if (!target || !body) {
|
|
95
|
+
return allowPreToolUse();
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
if (!(await ensureDaemonOrSkip())) {
|
|
99
|
+
return allowPreToolUse('peer-bridge daemon unreachable; falling back to native SendMessage.');
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
let res;
|
|
103
|
+
try {
|
|
104
|
+
res = await callRpc(
|
|
105
|
+
'peer.resolve',
|
|
106
|
+
{ target, check_native_claude: true },
|
|
107
|
+
{ config, timeoutMs: 3000 },
|
|
108
|
+
);
|
|
109
|
+
} catch {
|
|
110
|
+
return allowPreToolUse('peer-bridge resolve failed; falling back to native SendMessage.');
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
if (!res.ok && res.code === 'AMBIGUOUS_TARGET') {
|
|
114
|
+
return denyPreToolUse(`[peer-bridge AMBIGUOUS TARGET] ${res.message}`);
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
if (!res.ok && res.code === 'NATIVE_ONLY') {
|
|
118
|
+
return allowPreToolUse(res.message);
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
if (res.ok) {
|
|
122
|
+
// Preserve native Claude SendMessage for unprefixed attached cc teammates present in ~/.claude
|
|
123
|
+
if (
|
|
124
|
+
!target.includes(':') &&
|
|
125
|
+
res.peer?.runtime === 'cc' &&
|
|
126
|
+
res.peer?.origin === 'attached'
|
|
127
|
+
) {
|
|
128
|
+
const nativePeers = scanNativeClaudePeers(config.claudeDir, {
|
|
129
|
+
skipPidCheck: Boolean(options.skipNativePidCheck),
|
|
130
|
+
});
|
|
131
|
+
if (matchNativeClaudePeers(nativePeers, target).length > 0) {
|
|
132
|
+
return allowPreToolUse(
|
|
133
|
+
`Native Claude peer "${target}" matched; allowing native SendMessage.`,
|
|
134
|
+
);
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
const receipt = await callRpc(
|
|
139
|
+
'message.send',
|
|
140
|
+
{
|
|
141
|
+
to: res.peer.handle,
|
|
142
|
+
from: input.session_id || 'cc:session',
|
|
143
|
+
body,
|
|
144
|
+
thread_id: ti.thread_id || undefined,
|
|
145
|
+
reply_expected:
|
|
146
|
+
ti.reply_expected !== undefined ? Boolean(ti.reply_expected) : true,
|
|
147
|
+
},
|
|
148
|
+
{ config, timeoutMs: 10_000 },
|
|
149
|
+
);
|
|
150
|
+
|
|
151
|
+
return denyPreToolUse(
|
|
152
|
+
`[peer-bridge DELIVERED — NOT AN ERROR] Message to "${target}" resolved to "${res.peer.handle}" and delivered via peer-bridge (msg_id=${receipt.msg_id}, status=${receipt.delivery_status}). Native SendMessage was intentionally skipped. Do NOT retry SendMessage or report an error.`,
|
|
153
|
+
JSON.stringify({ peer_bridge_delivery: receipt }),
|
|
154
|
+
);
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
// NOT_FOUND
|
|
158
|
+
const parsed = parseAddress(target);
|
|
159
|
+
if (parsed.runtime && parsed.runtime !== 'cc') {
|
|
160
|
+
return denyPreToolUse(`[peer-bridge PEER NOT FOUND] ${res.message}`);
|
|
161
|
+
}
|
|
162
|
+
return allowPreToolUse();
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
if (event === 'PostToolUse') {
|
|
166
|
+
if (input.tool_name && input.tool_name !== 'ListAgents' && input.tool_name !== 'TeamStatus') {
|
|
167
|
+
return {};
|
|
168
|
+
}
|
|
169
|
+
if (!(await ensureDaemonOrSkip())) return {};
|
|
170
|
+
try {
|
|
171
|
+
const listed = await callRpc('peer.list', { include_offline: false }, { config, timeoutMs: 2500 });
|
|
172
|
+
const external = (listed.peers || []).filter((p) => p.runtime !== 'cc' || p.origin === 'spawned');
|
|
173
|
+
if (!external.length) return {};
|
|
174
|
+
const summary = external
|
|
175
|
+
.map((p) => `${p.handle} (${p.state}, ${p.exec_mode})`)
|
|
176
|
+
.join(', ');
|
|
177
|
+
return {
|
|
178
|
+
hookSpecificOutput: {
|
|
179
|
+
hookEventName: 'PostToolUse',
|
|
180
|
+
additionalContext: `Active peer-bridge peers: ${summary}`,
|
|
181
|
+
},
|
|
182
|
+
};
|
|
183
|
+
} catch {
|
|
184
|
+
return {};
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
if (event === 'Stop') {
|
|
189
|
+
if (!input.session_id || !(await ensureDaemonOrSkip())) return {};
|
|
190
|
+
await callRpc(
|
|
191
|
+
'peer.heartbeat',
|
|
192
|
+
{
|
|
193
|
+
session_id: input.session_id,
|
|
194
|
+
runtime: 'cc',
|
|
195
|
+
state: 'idle',
|
|
196
|
+
...(input.last_assistant_message ? { summary: input.last_assistant_message } : {}),
|
|
197
|
+
},
|
|
198
|
+
{ config, timeoutMs: 2000 },
|
|
199
|
+
).catch(() => {});
|
|
200
|
+
return {};
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
return {};
|
|
204
|
+
}
|