@peer-agent/cli 0.0.1-beta.40

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 ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 梁音
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,85 @@
1
+ # @peer-agent/cli
2
+
3
+ Install the Peer Agent terminal CLI with npm or pnpm. This package is a thin installer:
4
+
5
+ 1. `postinstall` downloads `peer` + `peer-credential-helper` for your platform from the matching GitHub Release
6
+ 2. the `peer` bin launches the vendor binary (helper must stay next to `peer`)
7
+
8
+ > Monorepo note: the TUI **source** package is `@peer-agent/tui` (`apps/tui`).
9
+ > This package (`@peer-agent/cli`) is the **public npm installer** only.
10
+
11
+ ## Install
12
+
13
+ ```bash
14
+ npm install -g @peer-agent/cli
15
+ # or
16
+ pnpm add -g @peer-agent/cli
17
+ ```
18
+
19
+ ```bash
20
+ peer --version
21
+ peer
22
+ ```
23
+
24
+ Requires **Node.js 20+**.
25
+
26
+ ## What gets installed
27
+
28
+ | File | Role |
29
+ |------|------|
30
+ | `vendor/peer` | CLI / TUI binary |
31
+ | `vendor/peer-credential-helper` | credential vault helper (must be same directory as `peer`) |
32
+
33
+ Version is pinned to the npm package version (e.g. `@peer-agent/cli@0.0.1-beta.38` downloads the `v0.0.1-beta.38` GitHub Release asset).
34
+
35
+ ## Supported platforms (today)
36
+
37
+ | Platform | Archive | Notes |
38
+ |----------|---------|-------|
39
+ | macOS arm64 | `peer-darwin-arm64.tar.gz` | First-class |
40
+ | other | — | postinstall fails with a clear error until multi-platform CLI ships |
41
+
42
+ ## Environment variables
43
+
44
+ | Variable | Effect |
45
+ |----------|--------|
46
+ | `PEER_AGENT_SKIP_DOWNLOAD=1` | Skip binary download (CI publish / offline) |
47
+ | `PEER_AGENT_FORCE_DOWNLOAD=1` | Force download even inside the monorepo checkout |
48
+ | `PEER_AGENT_RELEASE_BASE` | Override GitHub Releases base URL (tests / mirrors) |
49
+
50
+ ## Monorepo developers
51
+
52
+ Inside this repository, `postinstall` **does not** download binaries by default (it detects `apps/tui`). Build the real CLI with:
53
+
54
+ ```bash
55
+ pnpm --filter @peer-agent/tui build
56
+ ./apps/tui/dist/peer --version
57
+ ```
58
+
59
+ To force the installer path locally:
60
+
61
+ ```bash
62
+ PEER_AGENT_FORCE_DOWNLOAD=1 node packages/npm-cli/scripts/postinstall.mjs
63
+ ```
64
+
65
+ ## Manual install (no npm)
66
+
67
+ From the GitHub Release for the same version:
68
+
69
+ ```bash
70
+ tar -xzf peer-darwin-arm64.tar.gz
71
+ export PATH="$PWD/peer-darwin-arm64:$PATH"
72
+ peer --version
73
+ ```
74
+
75
+ ## Uninstall
76
+
77
+ ```bash
78
+ npm uninstall -g @peer-agent/cli
79
+ ```
80
+
81
+ User data under `~/.peer-agent` is **not** removed (shared with Desktop).
82
+
83
+ ## License
84
+
85
+ MIT — same as the Peer Agent monorepo.
package/bin/peer.js ADDED
@@ -0,0 +1,56 @@
1
+ #!/usr/bin/env node
2
+ /**
3
+ * npm bin shim — exec the vendor peer binary downloaded by postinstall.
4
+ * peer-credential-helper must live next to peer (same vendor/ directory).
5
+ */
6
+ import { spawn } from 'node:child_process';
7
+ import { existsSync } from 'node:fs';
8
+ import { dirname, join } from 'node:path';
9
+ import { fileURLToPath } from 'node:url';
10
+
11
+ const root = join(dirname(fileURLToPath(import.meta.url)), '..');
12
+ const vendor = join(root, 'vendor');
13
+ const binary = join(vendor, process.platform === 'win32' ? 'peer.exe' : 'peer');
14
+ const helper = join(
15
+ vendor,
16
+ process.platform === 'win32' ? 'peer-credential-helper.exe' : 'peer-credential-helper',
17
+ );
18
+
19
+ if (!existsSync(binary) || !existsSync(helper)) {
20
+ console.error(
21
+ [
22
+ '[@peer-agent/cli] CLI binaries are missing.',
23
+ ` expected: ${binary}`,
24
+ ` expected: ${helper}`,
25
+ '',
26
+ 'postinstall should have downloaded them from GitHub Releases.',
27
+ 'Try reinstalling:',
28
+ ' npm i -g @peer-agent/cli',
29
+ 'or set PEER_AGENT_RELEASE_URL to a direct archive URL and reinstall.',
30
+ '',
31
+ 'Manual fallback: download peer-*-*.tar.gz from',
32
+ ' https://github.com/yinLiangDream/Peer-Agent/releases',
33
+ 'and keep peer + peer-credential-helper in the same directory.',
34
+ ].join('\n'),
35
+ );
36
+ process.exit(1);
37
+ }
38
+
39
+ const child = spawn(binary, process.argv.slice(2), {
40
+ stdio: 'inherit',
41
+ env: process.env,
42
+ windowsHide: true,
43
+ });
44
+
45
+ child.on('error', (error) => {
46
+ console.error(`[@peer-agent/cli] failed to start peer: ${error.message}`);
47
+ process.exit(1);
48
+ });
49
+
50
+ child.on('exit', (code, signal) => {
51
+ if (signal) {
52
+ process.kill(process.pid, signal);
53
+ return;
54
+ }
55
+ process.exit(code ?? 1);
56
+ });
@@ -0,0 +1,196 @@
1
+ import { spawnSync } from 'node:child_process';
2
+ import {
3
+ chmodSync,
4
+ copyFileSync,
5
+ createWriteStream,
6
+ existsSync,
7
+ mkdirSync,
8
+ mkdtempSync,
9
+ readdirSync,
10
+ readFileSync,
11
+ rmSync,
12
+ statSync,
13
+ writeFileSync,
14
+ } from 'node:fs';
15
+ import { tmpdir } from 'node:os';
16
+ import { join } from 'node:path';
17
+ import { pipeline } from 'node:stream/promises';
18
+ import { peerBinaryPath, helperBinaryPath, vendorDir } from './paths.mjs';
19
+ import { releaseAssetUrl, resolvePlatformTarget } from './platform.mjs';
20
+
21
+ /**
22
+ * @param {{
23
+ * version: string,
24
+ * root?: string,
25
+ * platform?: string,
26
+ * arch?: string,
27
+ * fetchImpl?: typeof fetch,
28
+ * skipDownload?: boolean,
29
+ * force?: boolean,
30
+ * log?: (msg: string) => void,
31
+ * }} options
32
+ */
33
+ export async function installBinary(options) {
34
+ const log = options.log ?? ((msg) => console.log(msg));
35
+ const root = options.root;
36
+ if (!root) throw new Error('installBinary: root is required');
37
+ const version = options.version;
38
+ if (!version) throw new Error('installBinary: version is required');
39
+
40
+ if (options.skipDownload || process.env.PEER_AGENT_SKIP_DOWNLOAD === '1') {
41
+ log('[@peer-agent/cli] PEER_AGENT_SKIP_DOWNLOAD=1 — skipping binary download');
42
+ return { skipped: true, reason: 'skip-download' };
43
+ }
44
+
45
+ const target = resolvePlatformTarget(options.platform, options.arch);
46
+ if (!target) {
47
+ throw new Error(
48
+ `Unsupported platform ${options.platform ?? process.platform}/${options.arch ?? process.arch}. ` +
49
+ 'Peer Agent npm CLI currently supports platforms published on GitHub Releases.',
50
+ );
51
+ }
52
+ if (!target.supported) {
53
+ throw new Error(
54
+ `Platform ${target.os}-${target.arch} is recognized but not yet published as a Release asset ` +
55
+ `(expected ${target.archive}). Download a manual archive from GitHub Releases, or wait for multi-platform CLI builds.`,
56
+ );
57
+ }
58
+
59
+ const vendor = vendorDir(root);
60
+ const peerPath = peerBinaryPath(root);
61
+ const helperPath = helperBinaryPath(root);
62
+ const markerPath = join(vendor, '.peer-agent-version');
63
+
64
+ if (
65
+ !options.force &&
66
+ existsSync(peerPath) &&
67
+ existsSync(helperPath) &&
68
+ existsSync(markerPath) &&
69
+ readFileSync(markerPath, 'utf8').trim() === version
70
+ ) {
71
+ log(`[@peer-agent/cli] vendor binaries already present for ${version}`);
72
+ return { skipped: true, reason: 'already-installed', peerPath, helperPath };
73
+ }
74
+
75
+ const url = process.env.PEER_AGENT_RELEASE_URL || releaseAssetUrl(version, target);
76
+ log(`[@peer-agent/cli] downloading ${url}`);
77
+
78
+ const fetchImpl = options.fetchImpl ?? globalThis.fetch;
79
+ if (typeof fetchImpl !== 'function') {
80
+ throw new Error('fetch is not available; Node.js 20+ is required');
81
+ }
82
+
83
+ const response = await fetchImpl(url, {
84
+ headers: { 'User-Agent': `@peer-agent/cli/${version}` },
85
+ redirect: 'follow',
86
+ });
87
+ if (!response.ok) {
88
+ throw new Error(
89
+ `Failed to download CLI archive (${response.status} ${response.statusText}): ${url}\n` +
90
+ 'Check that this version published peer-darwin-arm64.tar.gz on GitHub Releases.',
91
+ );
92
+ }
93
+
94
+ const tmp = mkdtempSync(join(tmpdir(), 'peer-agent-npm-'));
95
+ const archivePath = join(tmp, target.archive);
96
+ try {
97
+ const body = response.body;
98
+ if (!body) throw new Error('Empty response body from GitHub Releases');
99
+ await pipeline(body, createWriteStream(archivePath));
100
+
101
+ const extractDir = join(tmp, 'extract');
102
+ mkdirSync(extractDir, { recursive: true });
103
+ extractArchive(archivePath, extractDir, target.archive);
104
+
105
+ const found = findBinaries(extractDir);
106
+ if (!found.peer || !found.helper) {
107
+ throw new Error(
108
+ `Archive did not contain peer + peer-credential-helper (found peer=${Boolean(found.peer)} helper=${Boolean(found.helper)})`,
109
+ );
110
+ }
111
+
112
+ mkdirSync(vendor, { recursive: true });
113
+ copyFileSync(found.peer, peerPath);
114
+ copyFileSync(found.helper, helperPath);
115
+ try {
116
+ chmodSync(peerPath, 0o755);
117
+ chmodSync(helperPath, 0o755);
118
+ } catch {
119
+ // Windows no-op
120
+ }
121
+ writeFileSync(markerPath, `${version}\n`);
122
+
123
+ log(`[@peer-agent/cli] installed peer + peer-credential-helper → ${vendor}`);
124
+ return { skipped: false, peerPath, helperPath, url };
125
+ } finally {
126
+ rmSync(tmp, { recursive: true, force: true });
127
+ }
128
+ }
129
+
130
+ /**
131
+ * @param {string} archivePath
132
+ * @param {string} extractDir
133
+ * @param {string} archiveName
134
+ */
135
+ function extractArchive(archivePath, extractDir, archiveName) {
136
+ if (archiveName.endsWith('.tar.gz') || archiveName.endsWith('.tgz')) {
137
+ const result = spawnSync('tar', ['-xzf', archivePath, '-C', extractDir], {
138
+ encoding: 'utf8',
139
+ });
140
+ if (result.status !== 0) {
141
+ throw new Error(`tar extract failed: ${result.stderr || result.stdout || result.status}`);
142
+ }
143
+ return;
144
+ }
145
+ if (archiveName.endsWith('.zip')) {
146
+ // Prefer system unzip when present (stage-2 windows).
147
+ const result = spawnSync('unzip', ['-o', archivePath, '-d', extractDir], {
148
+ encoding: 'utf8',
149
+ });
150
+ if (result.status !== 0) {
151
+ throw new Error(
152
+ `unzip extract failed: ${result.stderr || result.stdout || result.status}. ` +
153
+ 'Install unzip or use a tar.gz asset.',
154
+ );
155
+ }
156
+ return;
157
+ }
158
+ throw new Error(`Unsupported archive format: ${archiveName}`);
159
+ }
160
+
161
+ /**
162
+ * Walk extract tree for peer + helper (archive may nest peer-darwin-arm64/).
163
+ * @param {string} dir
164
+ * @returns {{ peer: string | null, helper: string | null }}
165
+ */
166
+ export function findBinaries(dir) {
167
+ let peer = null;
168
+ let helper = null;
169
+ const stack = [dir];
170
+ while (stack.length > 0) {
171
+ const current = stack.pop();
172
+ if (!current) break;
173
+ let entries;
174
+ try {
175
+ entries = readdirSync(current, { withFileTypes: true });
176
+ } catch {
177
+ continue;
178
+ }
179
+ for (const entry of entries) {
180
+ const full = join(current, entry.name);
181
+ if (entry.isDirectory()) {
182
+ stack.push(full);
183
+ continue;
184
+ }
185
+ if (!entry.isFile()) continue;
186
+ if (entry.name === 'peer' || entry.name === 'peer.exe') peer = full;
187
+ if (entry.name === 'peer-credential-helper' || entry.name === 'peer-credential-helper.exe') {
188
+ helper = full;
189
+ }
190
+ }
191
+ }
192
+ // Prefer executable-looking files when duplicates exist
193
+ if (peer && !statSync(peer).isFile()) peer = null;
194
+ if (helper && !statSync(helper).isFile()) helper = null;
195
+ return { peer, helper };
196
+ }
package/lib/paths.mjs ADDED
@@ -0,0 +1,24 @@
1
+ import { dirname, join } from 'node:path';
2
+ import { fileURLToPath } from 'node:url';
3
+
4
+ /** Absolute package root for packages/npm-cli. */
5
+ export function packageRoot(fromUrl = import.meta.url) {
6
+ // lib/paths.mjs → package root is parent of lib/
7
+ return join(dirname(fileURLToPath(fromUrl)), '..');
8
+ }
9
+
10
+ /** Directory that holds peer + peer-credential-helper after postinstall. */
11
+ export function vendorDir(root = packageRoot()) {
12
+ return join(root, 'vendor');
13
+ }
14
+
15
+ export function peerBinaryPath(root = packageRoot()) {
16
+ return join(vendorDir(root), process.platform === 'win32' ? 'peer.exe' : 'peer');
17
+ }
18
+
19
+ export function helperBinaryPath(root = packageRoot()) {
20
+ return join(
21
+ vendorDir(root),
22
+ process.platform === 'win32' ? 'peer-credential-helper.exe' : 'peer-credential-helper',
23
+ );
24
+ }
@@ -0,0 +1,68 @@
1
+ /**
2
+ * Map Node process.platform / process.arch to the GitHub Release CLI archive name.
3
+ *
4
+ * Stage-1 release assets currently ship only peer-darwin-arm64.tar.gz.
5
+ * Platform keys below include forward-looking names so installers can fail with a
6
+ * precise "not yet published" message instead of inventing a wrong URL.
7
+ */
8
+
9
+ /** @typedef {{ os: string, arch: string, archive: string, supported: boolean }} PlatformTarget */
10
+
11
+ const TARGETS = {
12
+ 'darwin-arm64': {
13
+ os: 'darwin',
14
+ arch: 'arm64',
15
+ archive: 'peer-darwin-arm64.tar.gz',
16
+ supported: true,
17
+ },
18
+ 'darwin-x64': {
19
+ os: 'darwin',
20
+ arch: 'x64',
21
+ archive: 'peer-darwin-x64.tar.gz',
22
+ supported: false,
23
+ },
24
+ 'linux-arm64': {
25
+ os: 'linux',
26
+ arch: 'arm64',
27
+ archive: 'peer-linux-arm64.tar.gz',
28
+ supported: false,
29
+ },
30
+ 'linux-x64': {
31
+ os: 'linux',
32
+ arch: 'x64',
33
+ archive: 'peer-linux-x64.tar.gz',
34
+ supported: false,
35
+ },
36
+ 'win32-x64': {
37
+ os: 'win32',
38
+ arch: 'x64',
39
+ archive: 'peer-win32-x64.zip',
40
+ supported: false,
41
+ },
42
+ };
43
+
44
+ /**
45
+ * @param {string} [platform]
46
+ * @param {string} [arch]
47
+ * @returns {PlatformTarget | null}
48
+ */
49
+ export function resolvePlatformTarget(platform = process.platform, arch = process.arch) {
50
+ const key = `${platform}-${arch}`;
51
+ return TARGETS[key] ?? null;
52
+ }
53
+
54
+ /**
55
+ * @param {string} version semver without leading v
56
+ * @param {PlatformTarget} target
57
+ * @param {{ owner?: string, repo?: string }} [opts]
58
+ */
59
+ export function releaseAssetUrl(version, target, opts = {}) {
60
+ const owner = opts.owner ?? process.env.PEER_AGENT_GITHUB_OWNER ?? 'yinLiangDream';
61
+ const repo = opts.repo ?? process.env.PEER_AGENT_GITHUB_REPO ?? 'Peer-Agent';
62
+ const tag = version.startsWith('v') ? version : `v${version}`;
63
+ return `https://github.com/${owner}/${repo}/releases/download/${tag}/${target.archive}`;
64
+ }
65
+
66
+ export function listKnownTargets() {
67
+ return Object.values(TARGETS);
68
+ }
package/package.json ADDED
@@ -0,0 +1,50 @@
1
+ {
2
+ "name": "@peer-agent/cli",
3
+ "version": "0.0.1-beta.40",
4
+ "description": "Peer Agent CLI — install via npm; downloads the platform peer binary from GitHub Releases.",
5
+ "publishConfig": {
6
+ "access": "public"
7
+ },
8
+ "license": "MIT",
9
+ "type": "module",
10
+ "bin": {
11
+ "peer": "bin/peer.js"
12
+ },
13
+ "scripts": {
14
+ "postinstall": "node scripts/postinstall.mjs",
15
+ "test": "node --test test/*.test.mjs"
16
+ },
17
+ "files": [
18
+ "bin",
19
+ "lib",
20
+ "scripts",
21
+ "README.md",
22
+ "LICENSE"
23
+ ],
24
+ "engines": {
25
+ "node": ">=20"
26
+ },
27
+ "repository": {
28
+ "type": "git",
29
+ "url": "git+https://github.com/yinLiangDream/Peer-Agent.git",
30
+ "directory": "packages/npm-cli"
31
+ },
32
+ "homepage": "https://github.com/yinLiangDream/Peer-Agent#readme",
33
+ "bugs": {
34
+ "url": "https://github.com/yinLiangDream/Peer-Agent/issues"
35
+ },
36
+ "keywords": [
37
+ "peer-agent",
38
+ "cli",
39
+ "ai-agent",
40
+ "tui"
41
+ ],
42
+ "os": [
43
+ "darwin",
44
+ "linux"
45
+ ],
46
+ "cpu": [
47
+ "arm64",
48
+ "x64"
49
+ ]
50
+ }
@@ -0,0 +1,43 @@
1
+ #!/usr/bin/env node
2
+ /**
3
+ * postinstall — download peer + peer-credential-helper for the current platform
4
+ * from the matching GitHub Release (package version == release tag without v).
5
+ */
6
+ import { existsSync, readFileSync } from 'node:fs';
7
+ import { dirname, join } from 'node:path';
8
+ import { fileURLToPath } from 'node:url';
9
+ import { installBinary } from '../lib/install-binary.mjs';
10
+
11
+ const root = join(dirname(fileURLToPath(import.meta.url)), '..');
12
+ const pkg = JSON.parse(readFileSync(join(root, 'package.json'), 'utf8'));
13
+ const version = pkg.version;
14
+
15
+ // Monorepo checkout: packages/npm-cli lives next to apps/tui — developers build
16
+ // peer locally; do not hit GitHub on every pnpm install unless forced.
17
+ const monorepoMarker = join(root, '../../apps/tui/package.json');
18
+ const inMonorepo = existsSync(monorepoMarker);
19
+ const forceDownload = process.env.PEER_AGENT_FORCE_DOWNLOAD === '1';
20
+ if (inMonorepo && !forceDownload) {
21
+ console.log(
22
+ '[@peer-agent/cli] monorepo install detected — skipping binary download ' +
23
+ '(set PEER_AGENT_FORCE_DOWNLOAD=1 to fetch Release assets anyway)',
24
+ );
25
+ process.exit(0);
26
+ }
27
+
28
+ try {
29
+ await installBinary({
30
+ version,
31
+ root,
32
+ log: (msg) => console.log(msg),
33
+ });
34
+ } catch (error) {
35
+ const message = error instanceof Error ? error.message : String(error);
36
+ console.error(`[@peer-agent/cli] postinstall failed: ${message}`);
37
+ console.error(
38
+ '[@peer-agent/cli] You can still install manually from GitHub Releases: ' +
39
+ `https://github.com/yinLiangDream/Peer-Agent/releases/tag/v${version}`,
40
+ );
41
+ // Fail install so global users notice missing binaries instead of a broken `peer`.
42
+ process.exit(1);
43
+ }