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.
Files changed (51) hide show
  1. package/LICENSE +21 -0
  2. package/README.ko.md +134 -0
  3. package/README.md +161 -2
  4. package/adapters/antigravity/bin/agy-hook.mjs +24 -0
  5. package/adapters/antigravity/hooks.json +23 -0
  6. package/adapters/antigravity/mcp_config.json +8 -0
  7. package/adapters/antigravity/plugin.json +4 -0
  8. package/adapters/antigravity/rules/AGENTS.md +6 -0
  9. package/adapters/antigravity/skills/peer-bridge/SKILL.md +19 -0
  10. package/adapters/claude/.claude-plugin/plugin.json +5 -0
  11. package/adapters/claude/.mcp.json +8 -0
  12. package/adapters/claude/bin/cc-hook.mjs +20 -0
  13. package/adapters/claude/hooks/hooks.json +46 -0
  14. package/adapters/claude/skills/peer-bridge/SKILL.md +18 -0
  15. package/adapters/codex/bin/codex-hook.mjs +10 -0
  16. package/adapters/codex/config.toml.example +5 -0
  17. package/adapters/codex/hooks.json +44 -0
  18. package/adapters/codex/skills/peer-bridge/SKILL.md +12 -0
  19. package/adapters/gemini/bin/gemini-hook.mjs +10 -0
  20. package/adapters/gemini/settings.json +51 -0
  21. package/adapters/gemini/skills/peer-bridge/SKILL.md +12 -0
  22. package/adapters/paseo/SKILL.md +24 -0
  23. package/bin/pbridge-mcp.mjs +37 -0
  24. package/bin/pbridge-tui.mjs +7 -0
  25. package/bin/pbridge.mjs +5 -0
  26. package/bin/pbridged.mjs +16 -0
  27. package/package.json +42 -4
  28. package/src/adapters/antigravity/hooks.mjs +207 -0
  29. package/src/adapters/claude/hooks.mjs +204 -0
  30. package/src/adapters/codex/hooks.mjs +142 -0
  31. package/src/adapters/gemini/hooks.mjs +159 -0
  32. package/src/adapters/installer.mjs +328 -0
  33. package/src/cli/main.mjs +729 -0
  34. package/src/client/rpc.mjs +243 -0
  35. package/src/config.mjs +154 -0
  36. package/src/daemon/broker.mjs +1799 -0
  37. package/src/daemon/resolver.mjs +200 -0
  38. package/src/daemon/server.mjs +221 -0
  39. package/src/index.mjs +53 -0
  40. package/src/mcp/server.mjs +668 -0
  41. package/src/orchestrator/workflows.mjs +496 -0
  42. package/src/project.mjs +81 -0
  43. package/src/protocol.mjs +203 -0
  44. package/src/runtime/conv-api.mjs +101 -0
  45. package/src/runtime/host-discovery.mjs +635 -0
  46. package/src/runtime/native-claude.mjs +88 -0
  47. package/src/runtime/paseo.mjs +289 -0
  48. package/src/runtime/workers.mjs +751 -0
  49. package/src/storage/db.mjs +704 -0
  50. package/src/tui/render.mjs +472 -0
  51. package/src/tui/viewer.mjs +443 -0
@@ -0,0 +1,243 @@
1
+ import fs from 'node:fs';
2
+ import net from 'node:net';
3
+ import path from 'node:path';
4
+ import { spawn } from 'node:child_process';
5
+ import { fileURLToPath } from 'node:url';
6
+ import { ensureHomeDir, getConfig, isProcessAlive } from '../config.mjs';
7
+ import { RpcError, createLineSplitter, encodeFrame } from '../protocol.mjs';
8
+
9
+ const __dirname = path.dirname(fileURLToPath(import.meta.url));
10
+ const DAEMON_BIN = path.resolve(__dirname, '../../bin/pbridged.mjs');
11
+
12
+ export function callRpc(method, params = {}, options = {}) {
13
+ const config = getConfig(options.config || {});
14
+ const timeoutMs = Number(options.timeoutMs ?? 60_000);
15
+ const id = Math.floor(Math.random() * 1e9);
16
+
17
+ return new Promise((resolve, reject) => {
18
+ const socket = net.createConnection(config.sockPath);
19
+ let settled = false;
20
+
21
+ const finish = (err, val) => {
22
+ if (settled) return;
23
+ settled = true;
24
+ clearTimeout(timer);
25
+ socket.destroy();
26
+ if (err) reject(err);
27
+ else resolve(val);
28
+ };
29
+
30
+ const timer =
31
+ timeoutMs > 0
32
+ ? setTimeout(() => {
33
+ finish(new RpcError('RPC_TIMEOUT', `RPC "${method}" timed out after ${timeoutMs}ms`));
34
+ }, timeoutMs)
35
+ : null;
36
+
37
+ socket.on('error', (err) => {
38
+ finish(
39
+ new RpcError(
40
+ 'DAEMON_UNREACHABLE',
41
+ `Cannot connect to pbridged at ${config.sockPath}: ${err.message}`,
42
+ ),
43
+ );
44
+ });
45
+
46
+ socket.on('connect', () => {
47
+ socket.write(encodeFrame({ jsonrpc: '2.0', id, method, params }));
48
+ });
49
+
50
+ socket.on(
51
+ 'data',
52
+ createLineSplitter((line) => {
53
+ let msg;
54
+ try {
55
+ msg = JSON.parse(line);
56
+ } catch {
57
+ return;
58
+ }
59
+ if (msg.id !== id) return;
60
+ if (msg.error) {
61
+ finish(new RpcError(msg.error.code || 'RPC_ERROR', msg.error.message, msg.error.data));
62
+ } else {
63
+ finish(null, msg.result);
64
+ }
65
+ }),
66
+ );
67
+ });
68
+ }
69
+
70
+ export function subscribeChannel(params = {}, onPush, options = {}) {
71
+ const config = getConfig(options.config || {});
72
+ const id = Math.floor(Math.random() * 1e9);
73
+ const socket = net.createConnection(config.sockPath);
74
+
75
+ const ready = new Promise((resolve, reject) => {
76
+ let settled = false;
77
+ socket.on('error', (err) => {
78
+ if (!settled) {
79
+ settled = true;
80
+ reject(new RpcError('DAEMON_UNREACHABLE', err.message));
81
+ }
82
+ });
83
+ socket.on('connect', () => {
84
+ socket.write(encodeFrame({ jsonrpc: '2.0', id, method: 'channel.subscribe', params }));
85
+ });
86
+ socket.on(
87
+ 'data',
88
+ createLineSplitter((line) => {
89
+ let msg;
90
+ try {
91
+ msg = JSON.parse(line);
92
+ } catch {
93
+ return;
94
+ }
95
+ if (!settled && msg.id === id) {
96
+ settled = true;
97
+ if (msg.error) reject(new RpcError(msg.error.code, msg.error.message, msg.error.data));
98
+ else resolve(msg.result);
99
+ return;
100
+ }
101
+ if (msg.method === 'channel.push' && typeof onPush === 'function') {
102
+ onPush(msg.params);
103
+ }
104
+ }),
105
+ );
106
+ });
107
+
108
+ return {
109
+ ready,
110
+ close: () => socket.destroy(),
111
+ };
112
+ }
113
+
114
+ export function subscribeMonitor(params = {}, onPush, options = {}) {
115
+ const config = getConfig(options.config || {});
116
+ const onDisconnect = typeof options.onDisconnect === 'function' ? options.onDisconnect : null;
117
+ const id = Math.floor(Math.random() * 1e9);
118
+ const socket = net.createConnection(config.sockPath);
119
+
120
+ let settled = false;
121
+ let closed = false;
122
+ const notifyDisconnect = () => {
123
+ if (!settled || closed) return;
124
+ closed = true;
125
+ if (onDisconnect) onDisconnect();
126
+ };
127
+
128
+ const ready = new Promise((resolve, reject) => {
129
+ socket.on('error', (err) => {
130
+ if (!settled) {
131
+ settled = true;
132
+ reject(new RpcError('DAEMON_UNREACHABLE', err.message));
133
+ } else {
134
+ notifyDisconnect();
135
+ }
136
+ });
137
+ socket.on('close', () => {
138
+ notifyDisconnect();
139
+ });
140
+ socket.on('connect', () => {
141
+ socket.write(encodeFrame({ jsonrpc: '2.0', id, method: 'monitor.subscribe', params }));
142
+ });
143
+ socket.on(
144
+ 'data',
145
+ createLineSplitter((line) => {
146
+ let msg;
147
+ try {
148
+ msg = JSON.parse(line);
149
+ } catch {
150
+ return;
151
+ }
152
+ if (!settled && msg.id === id) {
153
+ settled = true;
154
+ if (msg.error) reject(new RpcError(msg.error.code, msg.error.message, msg.error.data));
155
+ else resolve(msg.result);
156
+ return;
157
+ }
158
+ if (msg.method === 'channel.push' && typeof onPush === 'function') {
159
+ onPush(msg.params);
160
+ }
161
+ }),
162
+ );
163
+ });
164
+
165
+ return {
166
+ ready,
167
+ close: () => {
168
+ closed = true;
169
+ socket.destroy();
170
+ },
171
+ };
172
+ }
173
+
174
+ export async function isDaemonRunning(options = {}) {
175
+ try {
176
+ const res = await callRpc('daemon.ping', {}, { ...options, timeoutMs: 1500 });
177
+ return Boolean(res?.pong);
178
+ } catch {
179
+ return false;
180
+ }
181
+ }
182
+
183
+ export async function startDaemonDetached(options = {}) {
184
+ const config = getConfig(options.config || {});
185
+ if (await isDaemonRunning({ config })) {
186
+ return callRpc('daemon.status', {}, { config, timeoutMs: 3000 });
187
+ }
188
+
189
+ ensureHomeDir(config.homeDir);
190
+ const logFd = fs.openSync(config.logPath, 'a');
191
+ const child = spawn(config.nodeBin, [DAEMON_BIN], {
192
+ detached: true,
193
+ windowsHide: true,
194
+ stdio: ['ignore', logFd, logFd],
195
+ env: {
196
+ ...process.env,
197
+ PBRIDGE_HOME: config.homeDir,
198
+ PBRIDGE_CLAUDE_DIR: config.claudeDir,
199
+ PBRIDGE_CODEX_DIR: config.codexDir,
200
+ },
201
+ });
202
+ child.unref();
203
+ fs.closeSync(logFd);
204
+
205
+ const deadline = Date.now() + 5000;
206
+ while (Date.now() < deadline) {
207
+ if (await isDaemonRunning({ config })) {
208
+ return callRpc('daemon.status', {}, { config, timeoutMs: 3000 });
209
+ }
210
+ await new Promise((r) => setTimeout(r, 100));
211
+ }
212
+
213
+ throw new Error(`Timed out waiting for pbridged to start. Check logs at ${config.logPath}`);
214
+ }
215
+
216
+ export async function stopDaemon(options = {}) {
217
+ const config = getConfig(options.config || {});
218
+ if (await isDaemonRunning({ config })) {
219
+ await callRpc('daemon.shutdown', {}, { config, timeoutMs: 3000 }).catch(() => {});
220
+ const deadline = Date.now() + 5000;
221
+ while (Date.now() < deadline) {
222
+ if (!(await isDaemonRunning({ config }))) return { stopped: true };
223
+ await new Promise((r) => setTimeout(r, 100));
224
+ }
225
+ }
226
+
227
+ if (fs.existsSync(config.pidPath)) {
228
+ const pid = Number(fs.readFileSync(config.pidPath, 'utf8').trim());
229
+ if (pid && isProcessAlive(pid)) {
230
+ try {
231
+ if (process.platform === 'win32') process.kill(pid);
232
+ else process.kill(pid, 'SIGTERM');
233
+ } catch {
234
+ // ignore
235
+ }
236
+ }
237
+ fs.rmSync(config.pidPath, { force: true });
238
+ }
239
+ if (fs.existsSync(config.sockPath)) {
240
+ fs.rmSync(config.sockPath, { force: true });
241
+ }
242
+ return { stopped: true };
243
+ }
package/src/config.mjs ADDED
@@ -0,0 +1,154 @@
1
+ import crypto from 'node:crypto';
2
+ import fs from 'node:fs';
3
+ import os from 'node:os';
4
+ import path from 'node:path';
5
+
6
+ export function isNamedPipePath(sockPath) {
7
+ return (
8
+ typeof sockPath === 'string' &&
9
+ (sockPath.startsWith('\\\\.\\pipe\\') || sockPath.startsWith('//./pipe/'))
10
+ );
11
+ }
12
+
13
+ export function defaultSockPath(homeDir, platform = process.platform) {
14
+ if (platform === 'win32') {
15
+ const hash = crypto
16
+ .createHash('sha1')
17
+ .update(String(homeDir || '').toLowerCase())
18
+ .digest('hex')
19
+ .slice(0, 12);
20
+ return `\\\\.\\pipe\\peer-bridge-${hash}`;
21
+ }
22
+ return path.join(homeDir, 'broker.sock');
23
+ }
24
+
25
+ export function resolveRuntimeBin(
26
+ name,
27
+ {
28
+ explicit = null,
29
+ envVal = null,
30
+ platform = process.platform,
31
+ envPath = process.env.PATH || '',
32
+ homeDir = os.homedir(),
33
+ } = {},
34
+ ) {
35
+ if (explicit) return explicit;
36
+ if (envVal) return envVal;
37
+
38
+ const isWin = platform === 'win32';
39
+ const sep = isWin ? ';' : ':';
40
+ const pathDirs = String(envPath || '')
41
+ .split(sep)
42
+ .map((d) => d.trim())
43
+ .filter(Boolean);
44
+
45
+ const fallbackDirs = isWin
46
+ ? [
47
+ process.env.APPDATA ? path.join(process.env.APPDATA, 'npm') : null,
48
+ process.env.LOCALAPPDATA ? path.join(process.env.LOCALAPPDATA, 'Programs') : null,
49
+ path.join(homeDir, '.local', 'bin'),
50
+ ].filter(Boolean)
51
+ : [path.join(homeDir, '.local', 'bin'), '/opt/homebrew/bin', '/usr/local/bin', '/usr/bin'];
52
+
53
+ const searchDirs = [...new Set([...pathDirs, ...fallbackDirs])];
54
+ const exts = isWin ? ['.cmd', '.exe', '.bat', ''] : [''];
55
+
56
+ for (const dir of searchDirs) {
57
+ for (const ext of exts) {
58
+ const candidate = path.join(dir, `${name}${ext}`);
59
+ try {
60
+ if (fs.existsSync(candidate) && fs.statSync(candidate).isFile()) {
61
+ return candidate;
62
+ }
63
+ } catch {
64
+ // ignore inaccessible paths
65
+ }
66
+ }
67
+ }
68
+
69
+ return name;
70
+ }
71
+
72
+ export function getConfig(overrides = {}) {
73
+ const platform = overrides.platform || process.platform;
74
+ const envPath = overrides.envPath !== undefined ? overrides.envPath : process.env.PATH || '';
75
+ const homeDir =
76
+ platform === 'win32' && overrides.homeDir
77
+ ? String(overrides.homeDir)
78
+ : path.resolve(
79
+ overrides.homeDir ||
80
+ process.env.PBRIDGE_HOME ||
81
+ path.join(os.homedir(), '.peer-bridge'),
82
+ );
83
+ const claudeDir = path.resolve(
84
+ overrides.claudeDir ||
85
+ process.env.PBRIDGE_CLAUDE_DIR ||
86
+ path.join(os.homedir(), '.claude'),
87
+ );
88
+ const codexDir = path.resolve(
89
+ overrides.codexDir ||
90
+ process.env.PBRIDGE_CODEX_DIR ||
91
+ path.join(os.homedir(), '.codex'),
92
+ );
93
+
94
+ const binOpts = { platform, envPath };
95
+
96
+ return {
97
+ homeDir,
98
+ dbPath: path.join(homeDir, 'state.db'),
99
+ sockPath: overrides.sockPath || defaultSockPath(homeDir, platform),
100
+ pidPath: path.join(homeDir, 'pbridged.pid'),
101
+ logPath: path.join(homeDir, 'pbridged.log'),
102
+ claudeDir,
103
+ codexDir,
104
+ nodeBin: overrides.nodeBin || process.env.PBRIDGE_NODE_BIN || process.execPath,
105
+ claudeBin: resolveRuntimeBin('claude', {
106
+ ...binOpts,
107
+ explicit: overrides.claudeBin,
108
+ envVal: process.env.PBRIDGE_CLAUDE_BIN,
109
+ }),
110
+ agyBin: resolveRuntimeBin('agy', {
111
+ ...binOpts,
112
+ explicit: overrides.agyBin,
113
+ envVal: process.env.PBRIDGE_AGY_BIN,
114
+ }),
115
+ codexBin: resolveRuntimeBin('codex', {
116
+ ...binOpts,
117
+ explicit: overrides.codexBin,
118
+ envVal: process.env.PBRIDGE_CODEX_BIN,
119
+ }),
120
+ geminiBin: resolveRuntimeBin('gemini', {
121
+ ...binOpts,
122
+ explicit: overrides.geminiBin,
123
+ envVal: process.env.PBRIDGE_GEMINI_BIN,
124
+ }),
125
+ paseoBin: resolveRuntimeBin('paseo', {
126
+ ...binOpts,
127
+ explicit: overrides.paseoBin,
128
+ envVal: process.env.PBRIDGE_PASEO_BIN,
129
+ }),
130
+ paseoHost: overrides.paseoHost || process.env.PBRIDGE_PASEO_HOST || process.env.PASEO_HOST || null,
131
+ paseoHome: overrides.paseoHome || process.env.PBRIDGE_PASEO_HOME || null,
132
+ tmuxBin: resolveRuntimeBin('tmux', {
133
+ ...binOpts,
134
+ explicit: overrides.tmuxBin,
135
+ envVal: process.env.PBRIDGE_TMUX_BIN,
136
+ }),
137
+ defaultMaxHops: Number(overrides.defaultMaxHops ?? process.env.PBRIDGE_MAX_HOPS ?? 6),
138
+ defaultWaitTimeoutMs: Number(overrides.defaultWaitTimeoutMs ?? 600_000),
139
+ };
140
+ }
141
+
142
+ export function ensureHomeDir(homeDir) {
143
+ fs.mkdirSync(homeDir, { recursive: true, mode: 0o700 });
144
+ }
145
+
146
+ export function isProcessAlive(pid) {
147
+ if (!Number.isInteger(pid) || pid <= 0) return false;
148
+ try {
149
+ process.kill(pid, 0);
150
+ return true;
151
+ } catch (err) {
152
+ return err?.code === 'EPERM';
153
+ }
154
+ }