@sciagent/cli 1.0.84 → 1.0.85

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/bin/sciagent.js CHANGED
@@ -3,17 +3,27 @@
3
3
  /**
4
4
  * SciAgent CLI 薄壳脚本
5
5
  * 自动识别平台并调用对应的预编译二进制文件
6
+ *
7
+ * 二进制查找策略:
8
+ * 1. %LOCALAPPDATA%\sciagent\bin\ (Windows) 或 ~/.sciagent/bin/ (Linux/Mac)
9
+ * - 带严格版本校验(.version文件必须匹配CURRENT_VERSION)
10
+ * - 版本不匹配时自动从极狐GitLab下载正确版本
11
+ * 2. 本地开发目录(仅开发模式)
12
+ * 3. 未找到时自动从极狐GitLab下载
13
+ *
14
+ * 注意:不再从npm optionalDependencies中查找二进制,因为:
15
+ * - npm缓存中的旧包可能包含旧版二进制
16
+ * - npm可能修改package.json版本号来匹配请求,但二进制文件仍是旧的
17
+ * - 超过250MB的包无法发布到npm
6
18
  */
7
19
 
8
20
  const { spawn, execSync } = require('child_process');
9
21
  const path = require('path');
10
22
  const fs = require('fs');
11
23
  const os = require('os');
12
- const https = require('https');
13
- const http = require('http');
14
24
 
15
25
  // 当前版本号 - 与 postinstall.js 和 package.json 保持同步
16
- const CURRENT_VERSION = '1.0.84';
26
+ const CURRENT_VERSION = '1.0.85';
17
27
 
18
28
  // 极狐GitLab下载配置
19
29
  const JIHULAB_URL = 'https://jihulab.com';
@@ -50,6 +60,15 @@ function compareVersions(a, b) {
50
60
  return 0;
51
61
  }
52
62
 
63
+ /**
64
+ * 获取二进制安装目录
65
+ */
66
+ function getInstallDir(platform) {
67
+ return platform === 'win32'
68
+ ? path.join(process.env.LOCALAPPDATA || os.homedir(), 'sciagent', 'bin')
69
+ : path.join(os.homedir(), '.sciagent', 'bin');
70
+ }
71
+
53
72
  /**
54
73
  * 获取当前平台的二进制文件路径
55
74
  */
@@ -69,102 +88,66 @@ function getBinaryPath() {
69
88
  process.exit(1);
70
89
  }
71
90
 
72
- // 构建包名
73
- const packageName = `@sciagent/cli-${platform}-${arch}`;
74
-
75
- // 尝试从node_modules中查找
76
91
  const binName = platform === 'win32' ? 'sciagent.exe' : 'sciagent';
77
-
78
- // 方法0: ~/.sciagent/bin/ 或 %LOCALAPPDATA%\sciagent\bin 查找(postinstall下载位置)
79
- const homeBinDir = platform === 'win32'
80
- ? path.join(process.env.LOCALAPPDATA || os.homedir(), 'sciagent', 'bin')
81
- : path.join(os.homedir(), '.sciagent', 'bin');
82
- const homeBinPath = path.join(homeBinDir, binName);
83
- if (fs.existsSync(homeBinPath)) {
84
- const stats = fs.statSync(homeBinPath);
92
+ const installDir = getInstallDir(platform);
93
+ const binPath = path.join(installDir, binName);
94
+ const versionFile = path.join(installDir, '.version');
95
+
96
+ // 检查已安装的二进制
97
+ if (fs.existsSync(binPath)) {
98
+ const stats = fs.statSync(binPath);
85
99
  if (stats.size > 10 * 1024 * 1024) {
86
- // 版本检查:不匹配时自动下载新版本
87
- const versionFile = path.join(homeBinDir, '.version');
100
+ // 读取版本文件
88
101
  if (fs.existsSync(versionFile)) {
89
102
  const installedVersion = fs.readFileSync(versionFile, 'utf8').trim();
90
103
  const cmp = compareVersions(installedVersion, CURRENT_VERSION);
91
- if (cmp < 0) {
92
- // 安装版本低于预期版本 自动下载新版本
93
- console.log(`[INFO] Binary version outdated: installed=${installedVersion}, expected=${CURRENT_VERSION}`);
94
- console.log(`[INFO] Downloading new version...`);
95
- try { fs.unlinkSync(homeBinPath); fs.unlinkSync(versionFile); } catch (e) {}
104
+
105
+ if (cmp === 0) {
106
+ // 版本精确匹配 直接使用
107
+ console.log(`[INFO] SciAgent v${installedVersion} (installed)`);
108
+ return binPath;
109
+ } else if (cmp < 0) {
110
+ // 版本过低 → 自动下载新版本
111
+ console.log(`[INFO] SciAgent version outdated: v${installedVersion} → v${CURRENT_VERSION}`);
112
+ console.log(`[INFO] Downloading new version from JiHuLab...`);
113
+ try { fs.unlinkSync(binPath); fs.unlinkSync(versionFile); } catch (e) {}
96
114
  const downloaded = downloadBinary(platform, arch, CURRENT_VERSION);
97
115
  if (downloaded) return downloaded;
98
116
  console.error(`[ERROR] Failed to download v${CURRENT_VERSION}. Please run: npm install -g @sciagent/cli`);
99
117
  process.exit(1);
100
- } else if (cmp > 0) {
101
- // 安装版本高于预期用户手动装了新版,允许运行
102
- console.log(`[INFO] Binary version ${installedVersion} is newer than npm package version ${CURRENT_VERSION}, using it.`);
118
+ } else {
119
+ // 版本更高允许运行(用户可能手动安装了新版)
120
+ console.log(`[INFO] SciAgent v${installedVersion} (newer than package v${CURRENT_VERSION})`);
121
+ return binPath;
103
122
  }
104
- // cmp === 0 → 版本匹配,正常使用
105
123
  } else {
106
- // 没有 .version 文件 → 自动下载新版本
107
- console.log(`[INFO] Binary version unknown (no .version file), downloading v${CURRENT_VERSION}...`);
108
- try { fs.unlinkSync(homeBinPath); } catch (e) {}
124
+ // 没有.version文件 → 可能是旧安装,重新下载
125
+ console.log(`[INFO] SciAgent binary found but version unknown, downloading v${CURRENT_VERSION}...`);
126
+ try { fs.unlinkSync(binPath); } catch (e) {}
109
127
  const downloaded = downloadBinary(platform, arch, CURRENT_VERSION);
110
128
  if (downloaded) return downloaded;
111
129
  console.error(`[ERROR] Failed to download v${CURRENT_VERSION}. Please run: npm install -g @sciagent/cli`);
112
130
  process.exit(1);
113
131
  }
114
- return homeBinPath;
115
- }
116
- }
117
-
118
- // 方法1: 从optionalDependencies安装的包中查找
119
- // ⚠️ 重要:npm包中的二进制可能是旧版本(npm缓存/250MB限制导致无法发布新版)
120
- // 必须严格校验版本,版本不匹配则跳过并触发下载
121
- try {
122
- const packagePath = require.resolve(`${packageName}/bin/${binName}`);
123
- if (fs.existsSync(packagePath)) {
124
- const stats = fs.statSync(packagePath);
125
- if (stats.size > 10 * 1024 * 1024) {
126
- // 严格版本校验:读取npm包的package.json
127
- try {
128
- const pkgJsonPath = require.resolve(`${packageName}/package.json`);
129
- const pkgJson = JSON.parse(fs.readFileSync(pkgJsonPath, 'utf8'));
130
- const pkgVersion = pkgJson.version;
131
- const cmp = compareVersions(pkgVersion, CURRENT_VERSION);
132
- if (cmp !== 0) {
133
- // 版本不匹配(无论高低)→ 跳过,下载正确版本
134
- console.log(`[INFO] npm package binary version mismatch: ${packageName}@${pkgVersion} != ${CURRENT_VERSION}, will download correct version`);
135
- // 不return,继续到下载逻辑
136
- } else {
137
- // 版本精确匹配,使用
138
- console.log(`[INFO] Using npm package binary: ${packageName}@${pkgVersion}`);
139
- return packagePath;
140
- }
141
- } catch (e) {
142
- // 无法读取package.json,跳过
143
- console.log(`[INFO] Cannot read package version from ${packageName}, skipping npm binary`);
144
- }
145
- } else {
146
- console.log(`[INFO] npm package binary too small (${(stats.size / 1024).toFixed(0)} KB), likely a stub`);
147
- }
132
+ } else {
133
+ // 文件太小,损坏
134
+ console.log(`[INFO] SciAgent binary too small (${(stats.size / 1024).toFixed(0)} KB), removing...`);
135
+ try { fs.unlinkSync(binPath); } catch (e) {}
148
136
  }
149
- } catch (e) {
150
- // 包未安装,继续尝试其他方法
151
137
  }
152
138
 
153
- // 方法2: 从本地packages目录查找(开发模式)
139
+ // 开发模式:从本地packages目录查找
154
140
  const localPath = path.join(__dirname, '..', 'packages', `sciagent-${platform}-${arch}`, 'bin', binName);
155
141
  if (fs.existsSync(localPath)) {
156
- return localPath;
157
- }
158
-
159
- // 方法3: 从当前目录的bin查找
160
- const binPath = path.join(__dirname, '..', 'bin', platform, arch, binName);
161
- if (fs.existsSync(binPath)) {
162
- return binPath;
142
+ const stats = fs.statSync(localPath);
143
+ if (stats.size > 10 * 1024 * 1024) {
144
+ console.log(`[INFO] SciAgent (dev mode)`);
145
+ return localPath;
146
+ }
163
147
  }
164
148
 
165
- // 未找到匹配版本的二进制文件 - 自动从极狐GitLab下载
166
- console.log(`⚠️ SciAgent binary not found or version mismatch for ${platform}-${arch}`);
167
- console.log(` Attempting to download v${CURRENT_VERSION} from JiHuLab...`);
149
+ // 未找到二进制文件 - 自动从极狐GitLab下载
150
+ console.log(`[INFO] SciAgent binary not found, downloading v${CURRENT_VERSION} from JiHuLab...`);
168
151
 
169
152
  const downloaded = downloadBinary(platform, arch, CURRENT_VERSION);
170
153
  if (downloaded && fs.existsSync(downloaded)) {
@@ -190,9 +173,7 @@ function downloadBinary(platform, arch, version) {
190
173
  `?access_token=${JIHULAB_DOWNLOAD_TOKEN}`
191
174
  );
192
175
 
193
- const installDir = platform === 'win32'
194
- ? path.join(process.env.LOCALAPPDATA || os.homedir(), 'sciagent', 'bin')
195
- : path.join(os.homedir(), '.sciagent', 'bin');
176
+ const installDir = getInstallDir(platform);
196
177
  fs.mkdirSync(installDir, { recursive: true });
197
178
  const targetPath = path.join(installDir, binName);
198
179
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@sciagent/cli",
3
- "version": "1.0.84",
3
+ "version": "1.0.85",
4
4
  "description": "SciAgent CLI - AI Research Assistant",
5
5
  "main": "index.js",
6
6
  "files": [
@@ -15,12 +15,12 @@
15
15
  "postinstall": "node scripts/postinstall.js"
16
16
  },
17
17
  "optionalDependencies": {
18
- "@sciagent/cli-linux-x64": "1.0.84",
19
- "@sciagent/cli-linux-arm64": "1.0.84",
20
- "@sciagent/cli-darwin-x64": "1.0.84",
21
- "@sciagent/cli-darwin-arm64": "1.0.84",
22
- "@sciagent/cli-win32-x64": "1.0.84",
23
- "@sciagent/cli-win32-arm64": "1.0.84"
18
+ "@sciagent/cli-linux-x64": "1.0.85",
19
+ "@sciagent/cli-linux-arm64": "1.0.85",
20
+ "@sciagent/cli-darwin-x64": "1.0.85",
21
+ "@sciagent/cli-darwin-arm64": "1.0.85",
22
+ "@sciagent/cli-win32-x64": "1.0.85",
23
+ "@sciagent/cli-win32-arm64": "1.0.85"
24
24
  },
25
25
  "keywords": [
26
26
  "ai",
@@ -27,7 +27,7 @@ const ARCH_MAP = {
27
27
  };
28
28
 
29
29
  // 当前版本号 - 每次发布时同步更新
30
- const CURRENT_VERSION = '1.0.84';
30
+ const CURRENT_VERSION = '1.0.85';
31
31
 
32
32
  // GitHub Releases 回退版本列表(当当前版本的 release 不存在时尝试)
33
33
  const GITHUB_FALLBACK_VERSIONS = ['1.0.50', '1.0.48', '1.0.46', '1.0.40', '1.0.38', '1.0.36'];