@ragana/agentmesh 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.
@@ -0,0 +1,122 @@
1
+ #!/usr/bin/env node
2
+ // agent-mesh — the one command people install (npx @ragana/agentmesh ...).
3
+ //
4
+ // agent-mesh setup guided: pick a handle, enter an invite code
5
+ // (hidden), then turn on human approval
6
+ // agent-mesh mcp [--home DIR] run the MCP server (for Claude and others)
7
+ // agent-mesh <command> ... any client command: send, inbox, requests,
8
+ // accept, invite, backup, ... (see: agent-mesh help)
9
+ //
10
+ // --home defaults to the only identity in ~/.agent-mesh (or AGENT_MESH_HOME),
11
+ // so most commands need no flags. --broker defaults to the public broker
12
+ // (or AGENT_MESH_BROKER).
13
+ 'use strict';
14
+
15
+ const fs = require('node:fs');
16
+ const os = require('node:os');
17
+ const path = require('node:path');
18
+ const { spawn } = require('node:child_process');
19
+
20
+ const ROOT = path.join(__dirname, '..');
21
+ const CLIENT = path.join(ROOT, 'client', 'agent.js');
22
+ const MCP = path.join(ROOT, 'mcp', 'server.js');
23
+ const DEFAULT_BROKER = process.env.AGENT_MESH_BROKER || 'https://tyhypscgrzwsqeuaqqdi.supabase.co/functions/v1/mesh';
24
+ const MESH_DIR = path.join(os.homedir(), '.agent-mesh');
25
+
26
+ const argv = process.argv.slice(2);
27
+ const cmd = argv[0];
28
+ const has = (flag) => argv.includes(flag);
29
+
30
+ function identities() {
31
+ try {
32
+ return fs.readdirSync(MESH_DIR).filter((d) => fs.existsSync(path.join(MESH_DIR, d, 'state.json')));
33
+ } catch { return []; }
34
+ }
35
+
36
+ // --home: explicit flag > AGENT_MESH_HOME > the only identity in ~/.agent-mesh.
37
+ function defaultHome() {
38
+ if (process.env.AGENT_MESH_HOME) return process.env.AGENT_MESH_HOME;
39
+ const ids = identities();
40
+ if (ids.length === 1) return path.join(MESH_DIR, ids[0]);
41
+ if (ids.length > 1) {
42
+ console.error(`error: several identities in ${MESH_DIR} (${ids.join(', ')}); pass --home ${MESH_DIR}/<handle>`);
43
+ } else {
44
+ console.error(`error: no identity yet. Run: agent-mesh setup`);
45
+ }
46
+ process.exit(2);
47
+ }
48
+
49
+ function run(script, args) {
50
+ const child = spawn(process.execPath, [script, ...args], { stdio: 'inherit' });
51
+ return new Promise((resolve) => child.on('exit', (code) => resolve(code ?? 1)));
52
+ }
53
+
54
+ // Prompts on a terminal; the hidden one doesn't echo.
55
+ function ask(question, hidden) {
56
+ return new Promise((resolve) => {
57
+ const stdin = process.stdin;
58
+ process.stdout.write(question);
59
+ let buf = '';
60
+ const done = () => { stdin.off('data', onData); stdin.setRawMode(false); stdin.pause(); process.stdout.write('\n'); resolve(buf); };
61
+ const onData = (chunk) => {
62
+ for (const ch of chunk) {
63
+ if (ch === '\r' || ch === '\n') return done();
64
+ if (ch === '\u0003') { stdin.setRawMode(false); process.stdout.write('\n'); process.exit(130); }
65
+ if (ch === '\u007f' || ch === '\b') { if (buf) { buf = buf.slice(0, -1); if (!hidden) process.stdout.write('\b \b'); } continue; }
66
+ buf += ch;
67
+ if (!hidden) process.stdout.write(ch);
68
+ }
69
+ };
70
+ stdin.setRawMode(true); stdin.setEncoding('utf8'); stdin.resume(); stdin.on('data', onData);
71
+ });
72
+ }
73
+
74
+ async function setup() {
75
+ if (!process.stdin.isTTY) {
76
+ console.error('error: setup is interactive; run it in a terminal');
77
+ process.exit(2);
78
+ }
79
+ const i = argv.indexOf('--broker');
80
+ const broker = i > 0 && argv[i + 1] ? argv[i + 1] : DEFAULT_BROKER;
81
+ console.log(`Agent Mesh setup (broker: ${broker})\n`);
82
+ let handle;
83
+ for (;;) {
84
+ handle = (await ask('Pick a handle (lowercase letters, digits, - or _): ')).trim();
85
+ if (/^[a-z0-9][a-z0-9_-]{0,63}$/.test(handle)) break;
86
+ console.log(' not a valid handle, try again');
87
+ }
88
+ const home = path.join(MESH_DIR, handle);
89
+ if (fs.existsSync(path.join(home, 'state.json'))) {
90
+ console.error(`error: ${home} already holds an identity`);
91
+ process.exit(1);
92
+ }
93
+ const invite = (await ask('Invite code (hidden): ', true)).replace(/\s+/g, '');
94
+ if (!invite) { console.error('error: no invite code entered'); process.exit(1); }
95
+ const code = await run(CLIENT, ['register', '--broker', broker, '--invite', invite, '--handle', handle, '--home', home]);
96
+ if (code !== 0) process.exit(code);
97
+ console.log('\nNext: a passphrase that only you know. Your agent will need it to accept');
98
+ console.log('anyone, unblock, rotate keys or delete the account. Never give it to an AI agent.\n');
99
+ const ac = await run(CLIENT, ['approval', 'set', '--home', home]);
100
+ if (ac !== 0) console.log('(You can do this later: agent-mesh approval set)');
101
+ console.log(`\nDone. You are @${handle}. Back up your identity now:`);
102
+ console.log(' agent-mesh backup --out ~/agent-mesh-backup.json');
103
+ console.log('Connect Claude Code:');
104
+ console.log(` claude mcp add --scope user agent-mesh -- npx -y @ragana/agentmesh mcp --home ${home}`);
105
+ }
106
+
107
+ (async () => {
108
+ if (!cmd || cmd === 'help' || cmd === '--help' || cmd === '-h') {
109
+ console.log('usage: agent-mesh setup | agent-mesh mcp [--home DIR] | agent-mesh <client command> [...]\n');
110
+ process.exit(await run(CLIENT, []) === 2 ? 0 : 1);
111
+ }
112
+ if (cmd === 'setup') return setup();
113
+ if (cmd === 'mcp') {
114
+ const args = argv.slice(1);
115
+ if (!args.includes('--home')) args.push('--home', defaultHome());
116
+ process.exit(await run(MCP, args));
117
+ }
118
+ const args = [...argv];
119
+ if (cmd === 'register' && !has('--broker')) args.push('--broker', DEFAULT_BROKER);
120
+ if (cmd !== 'register' && cmd !== 'restore' && !has('--home')) args.push('--home', defaultHome());
121
+ process.exit(await run(CLIENT, args));
122
+ })();