@z40/magpie 1.0.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 (4) hide show
  1. package/README.md +31 -0
  2. package/install.js +216 -0
  3. package/package.json +39 -0
  4. package/run.js +77 -0
package/README.md ADDED
@@ -0,0 +1,31 @@
1
+ # @z40/magpie
2
+
3
+ Put your AI coding agent in your chat app. Send a message in Feishu or WeChat;
4
+ the agent runs locally in your own project directory; its output comes back to
5
+ the chat.
6
+
7
+ Agents: Claude Code, Codex, Cursor Agent, GitHub Copilot CLI, and any
8
+ ACP-speaking agent. Platforms: Feishu (Lark), WeChat Work, Weixin.
9
+
10
+ ## Install
11
+
12
+ ```bash
13
+ npm install -g @z40/magpie
14
+ ```
15
+
16
+ This downloads the prebuilt binary for your platform from the matching GitHub
17
+ release and installs it as `magpie`.
18
+
19
+ ## Usage
20
+
21
+ ```bash
22
+ magpie # first run creates ~/.magpie/config.toml
23
+ magpie --config /path/to/config.toml
24
+ ```
25
+
26
+ The first run prints a Web admin URL (`http://localhost:9820`) where you create
27
+ a project and paste your bot credentials.
28
+
29
+ ## Documentation
30
+
31
+ Full documentation: https://github.com/ChamberZ40/magpie
package/install.js ADDED
@@ -0,0 +1,216 @@
1
+ #!/usr/bin/env node
2
+
3
+ "use strict";
4
+
5
+ const { execSync } = require("child_process");
6
+ const fs = require("fs");
7
+ const path = require("path");
8
+ const https = require("https");
9
+ const http = require("http");
10
+ const zlib = require("zlib");
11
+
12
+ const PACKAGE = require("./package.json");
13
+ const VERSION = `v${PACKAGE.version}`;
14
+ const NAME = "magpie";
15
+
16
+ const GITHUB_REPO = "ChamberZ40/magpie";
17
+
18
+ const PLATFORM_MAP = {
19
+ darwin: "darwin",
20
+ linux: "linux",
21
+ win32: "windows",
22
+ };
23
+
24
+ const ARCH_MAP = {
25
+ x64: "amd64",
26
+ arm64: "arm64",
27
+ };
28
+
29
+ function getPlatformInfo() {
30
+ const platform = PLATFORM_MAP[process.platform];
31
+ const arch = ARCH_MAP[process.arch];
32
+ if (!platform || !arch) {
33
+ throw new Error(
34
+ `Unsupported platform: ${process.platform}/${process.arch}. ` +
35
+ `Supported: linux/darwin/windows x64/arm64`
36
+ );
37
+ }
38
+ const ext = platform === "windows" ? ".zip" : ".tar.gz";
39
+ const filename = `${NAME}-${VERSION}-${platform}-${arch}${ext}`;
40
+ return { platform, arch, ext, filename };
41
+ }
42
+
43
+ function getDownloadURLs(filename) {
44
+ return [
45
+ `https://github.com/${GITHUB_REPO}/releases/download/${VERSION}/${filename}`,
46
+ ];
47
+ }
48
+
49
+ function fetch(url, redirects = 5) {
50
+ return new Promise((resolve, reject) => {
51
+ if (redirects <= 0) return reject(new Error("Too many redirects"));
52
+ const mod = url.startsWith("https") ? https : http;
53
+ mod
54
+ .get(url, { headers: { "User-Agent": "magpie-npm" } }, (res) => {
55
+ if (res.statusCode >= 300 && res.statusCode < 400 && res.headers.location) {
56
+ return resolve(fetch(res.headers.location, redirects - 1));
57
+ }
58
+ if (res.statusCode !== 200) {
59
+ res.resume();
60
+ return reject(new Error(`HTTP ${res.statusCode} for ${url}`));
61
+ }
62
+ const chunks = [];
63
+ res.on("data", (c) => chunks.push(c));
64
+ res.on("end", () => resolve(Buffer.concat(chunks)));
65
+ res.on("error", reject);
66
+ })
67
+ .on("error", reject);
68
+ });
69
+ }
70
+
71
+ async function download(urls) {
72
+ for (const url of urls) {
73
+ try {
74
+ console.log(`[magpie] Downloading from ${url}`);
75
+ const data = await fetch(url);
76
+ console.log(`[magpie] Downloaded ${(data.length / 1024 / 1024).toFixed(1)} MB`);
77
+ return data;
78
+ } catch (err) {
79
+ console.warn(`[magpie] Failed: ${err.message}, trying next source...`);
80
+ }
81
+ }
82
+ throw new Error(
83
+ `[magpie] Could not download binary from any source.\n` +
84
+ ` Tried: ${urls.join(", ")}\n` +
85
+ ` You can download manually from https://github.com/${GITHUB_REPO}/releases`
86
+ );
87
+ }
88
+
89
+ function extractTarGz(buffer, destDir, binaryName) {
90
+ const tmpFile = path.join(destDir, "_tmp.tar.gz");
91
+ fs.writeFileSync(tmpFile, buffer);
92
+ try {
93
+ execSync(`tar xzf "${tmpFile}" -C "${destDir}"`, { stdio: "pipe" });
94
+ } finally {
95
+ fs.unlinkSync(tmpFile);
96
+ }
97
+ const extracted = fs.readdirSync(destDir).find((f) => f.startsWith(NAME) && !f.endsWith(".tar.gz"));
98
+ if (extracted && extracted !== binaryName) {
99
+ fs.renameSync(path.join(destDir, extracted), path.join(destDir, binaryName));
100
+ }
101
+ }
102
+
103
+ function extractZip(buffer, destDir, binaryName) {
104
+ const tmpFile = path.join(destDir, "_tmp.zip");
105
+ fs.writeFileSync(tmpFile, buffer);
106
+ try {
107
+ try {
108
+ execSync(`unzip -o "${tmpFile}" -d "${destDir}"`, { stdio: "pipe" });
109
+ } catch {
110
+ execSync(`powershell -Command "Expand-Archive -Force '${tmpFile}' '${destDir}'"`, {
111
+ stdio: "pipe",
112
+ });
113
+ }
114
+ } finally {
115
+ try { fs.unlinkSync(tmpFile); } catch {}
116
+ }
117
+ const extracted = fs.readdirSync(destDir).find((f) => f.startsWith(NAME) && f.endsWith(".exe"));
118
+ if (extracted && extracted !== binaryName) {
119
+ fs.renameSync(path.join(destDir, extracted), path.join(destDir, binaryName));
120
+ }
121
+ }
122
+
123
+ // parseVersion splits "1.2.3-beta.1" into { nums: [1,2,3], preTag: "beta", preNum: 1 }
124
+ function parseVersion(v) {
125
+ v = v.replace(/^v/, "").trim();
126
+ const [base, ...rest] = v.split("-");
127
+ const nums = base.split(".").map(Number);
128
+ const pre = rest.join("-");
129
+ const m = pre.match(/^([a-zA-Z]+)\.?(\d+)?$/);
130
+ return { nums, preTag: m ? m[1] : pre, preNum: m && m[2] ? parseInt(m[2], 10) : 0, hasPre: pre !== "" };
131
+ }
132
+
133
+ // isNewerOrEqual returns true if installed >= expected
134
+ function isNewerOrEqual(installed, expected) {
135
+ const a = parseVersion(installed);
136
+ const b = parseVersion(expected);
137
+ const len = Math.max(a.nums.length, b.nums.length);
138
+ for (let i = 0; i < len; i++) {
139
+ const av = a.nums[i] || 0;
140
+ const bv = b.nums[i] || 0;
141
+ if (av > bv) return true;
142
+ if (av < bv) return false;
143
+ }
144
+ if (!a.hasPre && b.hasPre) return true;
145
+ if (a.hasPre && !b.hasPre) return false;
146
+ if (!a.hasPre && !b.hasPre) return true;
147
+ // Both pre-release: compare tag then number (rc > beta, beta.10 > beta.9)
148
+ if (a.preTag !== b.preTag) return a.preTag > b.preTag;
149
+ return a.preNum >= b.preNum;
150
+ }
151
+
152
+ async function main() {
153
+ const { platform, arch, ext, filename } = getPlatformInfo();
154
+ console.log(`[magpie] Platform: ${platform}/${arch}`);
155
+
156
+ const binDir = path.join(__dirname, "bin");
157
+ fs.mkdirSync(binDir, { recursive: true });
158
+
159
+ const binaryName = platform === "windows" ? `${NAME}.exe` : NAME;
160
+ const binaryPath = path.join(binDir, binaryName);
161
+
162
+ if (fs.existsSync(binaryPath)) {
163
+ try {
164
+ const out = execSync(`"${binaryPath}" --version`, { encoding: "utf8", timeout: 5000 });
165
+ const expectedVer = VERSION.slice(1); // remove leading "v"
166
+ if (out.includes(expectedVer)) {
167
+ console.log(`[magpie] Binary ${VERSION} already installed, skipping.`);
168
+ return;
169
+ }
170
+ // Don't downgrade: if existing binary is newer, keep it
171
+ const match = out.match(/(\d+\.\d+\.\d+[^\s]*)/);
172
+ if (match && isNewerOrEqual(match[1], expectedVer)) {
173
+ console.log(`[magpie] Binary ${match[1]} is newer than ${VERSION}, skipping.`);
174
+ return;
175
+ }
176
+ console.log(`[magpie] Existing binary is outdated, upgrading to ${VERSION}...`);
177
+ fs.unlinkSync(binaryPath);
178
+ } catch {
179
+ console.log(`[magpie] Replacing existing binary with ${VERSION}...`);
180
+ fs.unlinkSync(binaryPath);
181
+ }
182
+ }
183
+
184
+ const urls = getDownloadURLs(filename);
185
+ const data = await download(urls);
186
+
187
+ if (ext === ".tar.gz") {
188
+ extractTarGz(data, binDir, binaryName);
189
+ } else {
190
+ extractZip(data, binDir, binaryName);
191
+ }
192
+
193
+ if (platform !== "windows") {
194
+ fs.chmodSync(binaryPath, 0o755);
195
+ }
196
+
197
+ if (platform === "darwin") {
198
+ try {
199
+ execSync(`xattr -d com.apple.quarantine "${binaryPath}"`, { stdio: "pipe" });
200
+ console.log(`[magpie] Removed macOS quarantine attribute`);
201
+ } catch {
202
+ // xattr fails if the attribute doesn't exist, which is fine
203
+ }
204
+ }
205
+
206
+ console.log(`[magpie] Installed to ${binaryPath}`);
207
+ }
208
+
209
+ main().catch((err) => {
210
+ console.error(err.message);
211
+ console.error(
212
+ "[magpie] Installation failed. You can install manually:\n" +
213
+ ` https://github.com/${GITHUB_REPO}/releases/tag/${VERSION}`
214
+ );
215
+ process.exit(1);
216
+ });
package/package.json ADDED
@@ -0,0 +1,39 @@
1
+ {
2
+ "name": "@z40/magpie",
3
+ "version": "1.0.0",
4
+ "description": "Bridge local AI coding agents (Claude Code, Codex, Cursor, GitHub Copilot, any ACP agent) to messaging platforms (Feishu/Lark, WeChat Work, Weixin)",
5
+ "keywords": [
6
+ "claude-code",
7
+ "codex",
8
+ "cursor",
9
+ "copilot",
10
+ "acp",
11
+ "ai-coding",
12
+ "feishu",
13
+ "lark",
14
+ "wechat-work",
15
+ "chatbot",
16
+ "bridge"
17
+ ],
18
+ "homepage": "https://github.com/ChamberZ40/magpie",
19
+ "repository": {
20
+ "type": "git",
21
+ "url": "git+https://github.com/ChamberZ40/magpie.git"
22
+ },
23
+ "license": "MIT",
24
+ "author": "ChamberZ40",
25
+ "publishConfig": {
26
+ "access": "public"
27
+ },
28
+ "bin": {
29
+ "magpie": "run.js"
30
+ },
31
+ "scripts": {
32
+ "postinstall": "node install.js"
33
+ },
34
+ "files": [
35
+ "install.js",
36
+ "run.js",
37
+ "README.md"
38
+ ]
39
+ }
package/run.js ADDED
@@ -0,0 +1,77 @@
1
+ #!/usr/bin/env node
2
+
3
+ "use strict";
4
+
5
+ const { execFileSync, execSync } = require("child_process");
6
+ const path = require("path");
7
+ const fs = require("fs");
8
+
9
+ const PACKAGE = require("./package.json");
10
+ const EXPECTED_VER = PACKAGE.version; // e.g. "1.1.0-beta.4"
11
+ const NAME = "magpie";
12
+ const binDir = path.join(__dirname, "bin");
13
+ const ext = process.platform === "win32" ? ".exe" : "";
14
+ const binaryPath = path.join(binDir, NAME + ext);
15
+
16
+ // parseVersion splits "1.2.3-beta.1" into { nums: [1,2,3], preTag: "beta", preNum: 1 }
17
+ function parseVersion(v) {
18
+ v = v.replace(/^v/, "").trim();
19
+ const [base, ...rest] = v.split("-");
20
+ const nums = base.split(".").map(Number);
21
+ const pre = rest.join("-");
22
+ const m = pre.match(/^([a-zA-Z]+)\.?(\d+)?$/);
23
+ return { nums, preTag: m ? m[1] : pre, preNum: m && m[2] ? parseInt(m[2], 10) : 0, hasPre: pre !== "" };
24
+ }
25
+
26
+ // isNewerOrEqual returns true if installed >= expected
27
+ function isNewerOrEqual(installed, expected) {
28
+ const a = parseVersion(installed);
29
+ const b = parseVersion(expected);
30
+ const len = Math.max(a.nums.length, b.nums.length);
31
+ for (let i = 0; i < len; i++) {
32
+ const av = a.nums[i] || 0;
33
+ const bv = b.nums[i] || 0;
34
+ if (av > bv) return true;
35
+ if (av < bv) return false;
36
+ }
37
+ // Same base: no pre-release >= any pre-release (1.2.3 >= 1.2.3-beta.1)
38
+ if (!a.hasPre && b.hasPre) return true;
39
+ if (a.hasPre && !b.hasPre) return false;
40
+ if (!a.hasPre && !b.hasPre) return true;
41
+ // Both pre-release: compare tag then number (rc > beta, beta.10 > beta.9)
42
+ if (a.preTag !== b.preTag) return a.preTag > b.preTag;
43
+ return a.preNum >= b.preNum;
44
+ }
45
+
46
+ function needsReinstall() {
47
+ if (!fs.existsSync(binaryPath)) return true;
48
+ try {
49
+ const out = execFileSync(binaryPath, ["--version"], { encoding: "utf8", timeout: 5000 });
50
+ if (out.includes(EXPECTED_VER)) return false;
51
+ // Extract version from output (e.g. "magpie 1.2.2-beta.1" or "1.2.2-beta.1")
52
+ const match = out.match(/(\d+\.\d+\.\d+[^\s]*)/);
53
+ if (match && isNewerOrEqual(match[1], EXPECTED_VER)) return false;
54
+ return true;
55
+ } catch {
56
+ return true;
57
+ }
58
+ }
59
+
60
+ if (needsReinstall()) {
61
+ console.log(`[magpie] Binary missing or outdated, installing v${EXPECTED_VER}...`);
62
+ try {
63
+ execSync("node " + JSON.stringify(path.join(__dirname, "install.js")), {
64
+ stdio: "inherit",
65
+ cwd: __dirname,
66
+ });
67
+ } catch {
68
+ console.error("[magpie] Auto-install failed. Run manually: npm uninstall -g @z40/magpie && npm install -g @z40/magpie");
69
+ process.exit(1);
70
+ }
71
+ }
72
+
73
+ try {
74
+ execFileSync(binaryPath, process.argv.slice(2), { stdio: "inherit" });
75
+ } catch (err) {
76
+ process.exit(err.status || 1);
77
+ }