codexmate 0.0.31 → 0.0.33
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/README.md +92 -308
- package/README.zh.md +94 -318
- package/cli/local-bridge.js +227 -0
- package/cli/update.js +162 -0
- package/cli.js +357 -112
- package/lib/cli-sessions.js +16 -6
- package/lib/win-tray.js +119 -0
- package/package.json +2 -2
- package/web-ui/app.js +4 -0
- package/web-ui/logic.sessions.mjs +17 -1
- package/web-ui/modules/app.computed.session.mjs +51 -315
- package/web-ui/modules/app.methods.agents.mjs +19 -0
- package/web-ui/modules/app.methods.claude-config.mjs +71 -2
- package/web-ui/modules/app.methods.codex-config.mjs +20 -0
- package/web-ui/modules/app.methods.providers.mjs +53 -7
- package/web-ui/modules/app.methods.session-actions.mjs +1 -1
- package/web-ui/modules/app.methods.session-browser.mjs +29 -1
- package/web-ui/modules/app.methods.startup-claude.mjs +4 -0
- package/web-ui/modules/i18n.dict.mjs +21 -3
- package/web-ui/partials/index/layout-header.html +1 -2
- package/web-ui/partials/index/modal-config-template-agents.html +12 -1
- package/web-ui/partials/index/modals-basic.html +14 -3
- package/web-ui/partials/index/panel-config-claude.html +57 -85
- package/web-ui/partials/index/panel-config-codex.html +60 -226
- package/web-ui/partials/index/panel-dashboard.html +0 -33
- package/web-ui/partials/index/panel-docs.html +21 -53
- package/web-ui/partials/index/panel-sessions.html +37 -20
- package/web-ui/partials/index/panel-trash.html +33 -38
- package/web-ui/partials/index/panel-usage.html +71 -304
- package/web-ui/styles/controls-forms.css +11 -0
- package/web-ui/styles/docs-panel.css +57 -83
- package/web-ui/styles/layout-shell.css +26 -24
- package/web-ui/styles/modals-core.css +33 -0
- package/web-ui/styles/responsive.css +5 -67
- package/web-ui/styles/sessions-list.css +274 -8
- package/web-ui/styles/sessions-toolbar-trash.css +185 -15
- package/web-ui/styles/sessions-usage.css +336 -788
package/cli/local-bridge.js
CHANGED
|
@@ -1,4 +1,6 @@
|
|
|
1
1
|
const fs = require('fs');
|
|
2
|
+
const http = require('http');
|
|
3
|
+
const https = require('https');
|
|
2
4
|
const { URL } = require('url');
|
|
3
5
|
const {
|
|
4
6
|
readOpenaiBridgeSettings,
|
|
@@ -21,6 +23,8 @@ const { isValidHttpUrl, normalizeBaseUrl, joinApiUrl } = require('../lib/cli-uti
|
|
|
21
23
|
|
|
22
24
|
const BUILTIN_PROXY_PROVIDER_NAME = 'codexmate-proxy';
|
|
23
25
|
const BUILTIN_LOCAL_PROVIDER_NAME = 'local';
|
|
26
|
+
const CLAUDE_LOCAL_PROVIDER_NAME = 'claude-local';
|
|
27
|
+
const CLAUDE_LOCAL_EXCLUDED_KEY = 'claudeLocalExcluded';
|
|
24
28
|
const CIRCUIT_BREAKER_THRESHOLD = 3;
|
|
25
29
|
const CIRCUIT_BREAKER_COOLDOWN_MS = 5 * 60 * 1000;
|
|
26
30
|
|
|
@@ -51,6 +55,31 @@ function buildUpstreamPool(readConfigFn, openaiBridgeFile, excludedProviders) {
|
|
|
51
55
|
return { pool };
|
|
52
56
|
}
|
|
53
57
|
|
|
58
|
+
function buildClaudeUpstreamPool(claudeProvidersFile, excludedProviders) {
|
|
59
|
+
let raw;
|
|
60
|
+
try {
|
|
61
|
+
if (!fs.existsSync(claudeProvidersFile)) return { error: '暂无可用上游 provider,请先添加 Claude 提供商' };
|
|
62
|
+
raw = JSON.parse(fs.readFileSync(claudeProvidersFile, 'utf-8'));
|
|
63
|
+
} catch (e) { return { error: '读取 Claude 提供商配置失败' }; }
|
|
64
|
+
const providers = (raw && typeof raw.providers === 'object' && !Array.isArray(raw.providers))
|
|
65
|
+
? raw.providers : {};
|
|
66
|
+
const pool = [];
|
|
67
|
+
const excludedSet = new Set(
|
|
68
|
+
(Array.isArray(excludedProviders) ? excludedProviders : [])
|
|
69
|
+
.filter(n => typeof n === 'string' && n.trim())
|
|
70
|
+
.map(n => n.trim().toLowerCase())
|
|
71
|
+
);
|
|
72
|
+
for (const [name, p] of Object.entries(providers)) {
|
|
73
|
+
if (!p || typeof p !== 'object') continue;
|
|
74
|
+
if (excludedSet.has(name.toLowerCase())) continue;
|
|
75
|
+
const baseUrl = typeof p.baseUrl === 'string' ? p.baseUrl.trim() : '';
|
|
76
|
+
if (!baseUrl || !isValidHttpUrl(normalizeBaseUrl(baseUrl))) continue;
|
|
77
|
+
pool.push({ name, baseUrl: normalizeBaseUrl(baseUrl), apiKey: typeof p.apiKey === 'string' ? p.apiKey : '' });
|
|
78
|
+
}
|
|
79
|
+
if (pool.length === 0) return { error: '请先添加可用的 Claude 上游提供商' };
|
|
80
|
+
return { pool };
|
|
81
|
+
}
|
|
82
|
+
|
|
54
83
|
function resolveUpstreamAuth(entry, openaiBridgeFile, reqAuthToken) {
|
|
55
84
|
if (entry.authMethod === 'codexmate' || entry.requiresOpenaiAuth) {
|
|
56
85
|
const token = reqAuthToken || '';
|
|
@@ -69,6 +98,7 @@ function resolveUpstreamAuth(entry, openaiBridgeFile, reqAuthToken) {
|
|
|
69
98
|
function createLocalBridgeHttpHandler(options = {}) {
|
|
70
99
|
const readConfigFn = options.readConfigFn;
|
|
71
100
|
const openaiBridgeFile = options.openaiBridgeFile;
|
|
101
|
+
const claudeProvidersFile = options.claudeProvidersFile || '';
|
|
72
102
|
const expectedToken = typeof options.expectedToken === 'string' ? options.expectedToken.trim() : '';
|
|
73
103
|
const maxBodySize = Number.isFinite(options.maxBodySize) ? options.maxBodySize : 0;
|
|
74
104
|
const httpAgent = options.httpAgent;
|
|
@@ -128,10 +158,207 @@ function createLocalBridgeHttpHandler(options = {}) {
|
|
|
128
158
|
} catch (e) { return []; }
|
|
129
159
|
}
|
|
130
160
|
|
|
161
|
+
function streamClaudeUpstream(targetUrl, options) {
|
|
162
|
+
const parsed = new URL(targetUrl);
|
|
163
|
+
const transport = parsed.protocol === 'https:' ? https : http;
|
|
164
|
+
const bodyText = options.body || '';
|
|
165
|
+
const headers = {
|
|
166
|
+
'Accept': 'text/event-stream',
|
|
167
|
+
'Content-Type': 'application/json',
|
|
168
|
+
...(options.headers || {})
|
|
169
|
+
};
|
|
170
|
+
if (bodyText) {
|
|
171
|
+
headers['Content-Length'] = Buffer.byteLength(bodyText, 'utf-8');
|
|
172
|
+
}
|
|
173
|
+
const maxBytes = Number.isFinite(options.maxBytes) && options.maxBytes > 0 ? options.maxBytes : 0;
|
|
174
|
+
const res = options.res;
|
|
175
|
+
|
|
176
|
+
return new Promise((resolve) => {
|
|
177
|
+
let settled = false;
|
|
178
|
+
let upstreamReq = null;
|
|
179
|
+
const finish = (value) => { if (!settled) { settled = true; resolve(value); } };
|
|
180
|
+
const abortUpstream = () => { if (upstreamReq) try { upstreamReq.destroy(new Error('client aborted')); } catch (_) {} };
|
|
181
|
+
if (res && typeof res.once === 'function') res.once('close', abortUpstream);
|
|
182
|
+
|
|
183
|
+
upstreamReq = transport.request({
|
|
184
|
+
protocol: parsed.protocol,
|
|
185
|
+
hostname: parsed.hostname,
|
|
186
|
+
port: parsed.port || (parsed.protocol === 'https:' ? 443 : 80),
|
|
187
|
+
method: options.method || 'POST',
|
|
188
|
+
path: `${parsed.pathname}${parsed.search}`,
|
|
189
|
+
headers,
|
|
190
|
+
agent: parsed.protocol === 'https:' ? options.httpsAgent : options.httpAgent
|
|
191
|
+
}, (upstreamRes) => {
|
|
192
|
+
const status = upstreamRes.statusCode || 0;
|
|
193
|
+
if (status >= 400) {
|
|
194
|
+
const chunks = [];
|
|
195
|
+
let size = 0;
|
|
196
|
+
upstreamRes.on('data', (chunk) => {
|
|
197
|
+
if (!chunk) return;
|
|
198
|
+
if (maxBytes > 0) { size += chunk.length; if (size > maxBytes) { finish({ ok: false, status, error: 'response too large' }); return; } }
|
|
199
|
+
chunks.push(chunk);
|
|
200
|
+
});
|
|
201
|
+
upstreamRes.on('end', () => finish({ ok: false, status, error: chunks.length ? Buffer.concat(chunks).toString('utf-8') : 'Upstream error' }));
|
|
202
|
+
return;
|
|
203
|
+
}
|
|
204
|
+
// SSE: pipe directly to client
|
|
205
|
+
if (!res.headersSent) {
|
|
206
|
+
res.writeHead(200, {
|
|
207
|
+
'Content-Type': 'text/event-stream; charset=utf-8',
|
|
208
|
+
'Cache-Control': 'no-cache',
|
|
209
|
+
'Connection': 'keep-alive',
|
|
210
|
+
'X-Accel-Buffering': 'no'
|
|
211
|
+
});
|
|
212
|
+
if (typeof res.flushHeaders === 'function') res.flushHeaders();
|
|
213
|
+
}
|
|
214
|
+
upstreamRes.pipe(res);
|
|
215
|
+
upstreamRes.on('end', () => finish({ ok: true, status }));
|
|
216
|
+
upstreamRes.on('error', (err) => finish({ ok: false, status, error: err.message }));
|
|
217
|
+
});
|
|
218
|
+
upstreamReq.on('error', (err) => finish({ ok: false, error: err.message }));
|
|
219
|
+
upstreamReq.setTimeout(5 * 60 * 1000, () => { try { upstreamReq.destroy(new Error('timeout')); } catch (_) {} });
|
|
220
|
+
if (bodyText) upstreamReq.write(bodyText);
|
|
221
|
+
upstreamReq.end();
|
|
222
|
+
});
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
function readClaudeExcludedProviders() {
|
|
226
|
+
if (!claudeProvidersFile) return [];
|
|
227
|
+
try {
|
|
228
|
+
if (!fs.existsSync(claudeProvidersFile)) return [];
|
|
229
|
+
const raw = JSON.parse(fs.readFileSync(claudeProvidersFile, 'utf-8'));
|
|
230
|
+
return Array.isArray(raw.excludedProviders)
|
|
231
|
+
? raw.excludedProviders.filter(n => typeof n === 'string' && n.trim())
|
|
232
|
+
: [];
|
|
233
|
+
} catch (e) { return []; }
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
async function handleClaudeLocalBridge(req, res, parsedUrl) {
|
|
237
|
+
try {
|
|
238
|
+
const token = extractAuthorizationToken(req);
|
|
239
|
+
const remoteAddr = req && req.socket ? req.socket.remoteAddress : '';
|
|
240
|
+
const isLoopback = isLoopbackAddress(remoteAddr);
|
|
241
|
+
if (!isLoopback && !expectedToken) {
|
|
242
|
+
res.writeHead(403, { 'Content-Type': 'application/json; charset=utf-8' });
|
|
243
|
+
res.end(JSON.stringify({ error: 'Remote access is disabled (set CODEXMATE_HTTP_TOKEN)' }));
|
|
244
|
+
return;
|
|
245
|
+
}
|
|
246
|
+
if (!token && !isLoopback) {
|
|
247
|
+
res.writeHead(401, { 'Content-Type': 'application/json; charset=utf-8' });
|
|
248
|
+
res.end(JSON.stringify({ error: 'Unauthorized' }));
|
|
249
|
+
return;
|
|
250
|
+
}
|
|
251
|
+
if (!isLoopback && token && token !== expectedToken) {
|
|
252
|
+
res.writeHead(401, { 'Content-Type': 'application/json; charset=utf-8' });
|
|
253
|
+
res.end(JSON.stringify({ error: 'Unauthorized' }));
|
|
254
|
+
return;
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
const poolResult = buildClaudeUpstreamPool(claudeProvidersFile, readClaudeExcludedProviders());
|
|
258
|
+
if (poolResult.error) {
|
|
259
|
+
res.writeHead(503, { 'Content-Type': 'application/json; charset=utf-8' });
|
|
260
|
+
res.end(JSON.stringify({ error: poolResult.error }));
|
|
261
|
+
return;
|
|
262
|
+
}
|
|
263
|
+
const pool = poolResult.pool;
|
|
264
|
+
const { entry } = pickUpstream(pool);
|
|
265
|
+
|
|
266
|
+
const suffix = (parsedUrl.pathname || '').replace(/^\/bridge\/claude-local\/?/, '');
|
|
267
|
+
if (!suffix) {
|
|
268
|
+
if ((req.method || 'GET').toUpperCase() !== 'GET') {
|
|
269
|
+
res.writeHead(405, { 'Content-Type': 'application/json; charset=utf-8' });
|
|
270
|
+
res.end(JSON.stringify({ error: 'Method Not Allowed' }));
|
|
271
|
+
return;
|
|
272
|
+
}
|
|
273
|
+
res.writeHead(200, { 'Content-Type': 'application/json; charset=utf-8' });
|
|
274
|
+
res.end(JSON.stringify({ object: 'codexmate.claude_local_bridge', provider: entry.name, status: 'ok', pool: pool.map(p => p.name) }));
|
|
275
|
+
return;
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
// Proxy Anthropic Messages API requests
|
|
279
|
+
const bodyResult = await readRequestBody(req, maxBodySize);
|
|
280
|
+
if (bodyResult.error) {
|
|
281
|
+
res.writeHead(413, { 'Content-Type': 'application/json; charset=utf-8' });
|
|
282
|
+
res.end(JSON.stringify({ error: bodyResult.error }));
|
|
283
|
+
return;
|
|
284
|
+
}
|
|
285
|
+
|
|
286
|
+
let parsedBody;
|
|
287
|
+
try { parsedBody = bodyResult.body ? JSON.parse(bodyResult.body) : {}; } catch (_) { parsedBody = {}; }
|
|
288
|
+
const wantsStream = !!(parsedBody && parsedBody.stream);
|
|
289
|
+
const upstreamUrl = joinApiUrl(entry.baseUrl.replace(/\/+$/, ''), suffix);
|
|
290
|
+
const headers = { 'Content-Type': 'application/json' };
|
|
291
|
+
if (entry.apiKey) {
|
|
292
|
+
headers['x-api-key'] = entry.apiKey.startsWith('Bearer ') ? entry.apiKey.slice(7) : entry.apiKey;
|
|
293
|
+
}
|
|
294
|
+
if (token && !entry.apiKey) {
|
|
295
|
+
headers['x-api-key'] = token.startsWith('Bearer ') ? token.slice(7) : token;
|
|
296
|
+
}
|
|
297
|
+
headers['anthropic-version'] = '2023-06-01';
|
|
298
|
+
|
|
299
|
+
if (wantsStream) {
|
|
300
|
+
// Streaming proxy: pipe upstream SSE directly to client
|
|
301
|
+
const upstreamResult = await streamClaudeUpstream(upstreamUrl, {
|
|
302
|
+
method: req.method || 'POST',
|
|
303
|
+
body: bodyResult.body,
|
|
304
|
+
headers,
|
|
305
|
+
maxBytes: maxUpstreamBytes,
|
|
306
|
+
httpAgent,
|
|
307
|
+
httpsAgent,
|
|
308
|
+
res
|
|
309
|
+
});
|
|
310
|
+
if (!upstreamResult.ok) {
|
|
311
|
+
recordFailure(entry.name);
|
|
312
|
+
if (!res.headersSent) {
|
|
313
|
+
res.writeHead(upstreamResult.status || 502, { 'Content-Type': 'application/json; charset=utf-8' });
|
|
314
|
+
res.end(JSON.stringify({ error: upstreamResult.error || 'Upstream error' }));
|
|
315
|
+
}
|
|
316
|
+
return;
|
|
317
|
+
}
|
|
318
|
+
recordSuccess(entry.name);
|
|
319
|
+
return;
|
|
320
|
+
}
|
|
321
|
+
|
|
322
|
+
// Non-streaming proxy
|
|
323
|
+
const upstreamResult = await retryTransientRequest(() => proxyRequestJson(upstreamUrl, {
|
|
324
|
+
method: req.method || 'POST',
|
|
325
|
+
body: bodyResult.body || null,
|
|
326
|
+
headers,
|
|
327
|
+
maxBytes: maxUpstreamBytes,
|
|
328
|
+
httpAgent,
|
|
329
|
+
httpsAgent
|
|
330
|
+
}));
|
|
331
|
+
|
|
332
|
+
if (!upstreamResult.ok) {
|
|
333
|
+
recordFailure(entry.name);
|
|
334
|
+
res.writeHead(502, { 'Content-Type': 'application/json; charset=utf-8' });
|
|
335
|
+
res.end(JSON.stringify({ error: `Upstream request failed: ${upstreamResult.error}` }));
|
|
336
|
+
return;
|
|
337
|
+
}
|
|
338
|
+
|
|
339
|
+
recordSuccess(entry.name);
|
|
340
|
+
const statusCode = Number.isFinite(upstreamResult.status) ? upstreamResult.status : 200;
|
|
341
|
+
res.writeHead(statusCode, { 'Content-Type': 'application/json; charset=utf-8' });
|
|
342
|
+
res.end(upstreamResult.bodyText || '{}');
|
|
343
|
+
} catch (e) {
|
|
344
|
+
res.writeHead(500, { 'Content-Type': 'application/json; charset=utf-8' });
|
|
345
|
+
res.end(JSON.stringify({ error: e && e.message ? e.message : 'Internal Error' }));
|
|
346
|
+
}
|
|
347
|
+
}
|
|
348
|
+
|
|
131
349
|
const handler = (req, res) => {
|
|
132
350
|
let parsedUrl;
|
|
133
351
|
try { parsedUrl = new URL(req.url || '/', 'http://localhost'); } catch (_) { return false; }
|
|
134
352
|
const pathname = parsedUrl.pathname || '/';
|
|
353
|
+
|
|
354
|
+
// Claude local bridge: /bridge/claude-local/v1/messages
|
|
355
|
+
if (pathname.startsWith('/bridge/claude-local/')) {
|
|
356
|
+
if (!claudeProvidersFile) return false;
|
|
357
|
+
void handleClaudeLocalBridge(req, res, parsedUrl);
|
|
358
|
+
return true;
|
|
359
|
+
}
|
|
360
|
+
|
|
361
|
+
// Codex local bridge: /bridge/local/v1
|
|
135
362
|
if (!pathname.startsWith('/bridge/local/')) return false;
|
|
136
363
|
const suffix = pathname.replace(/^\/bridge\/local\/?/, '');
|
|
137
364
|
if (!suffix.startsWith('v1')) return false;
|
package/cli/update.js
ADDED
|
@@ -0,0 +1,162 @@
|
|
|
1
|
+
const fs = require('fs');
|
|
2
|
+
const path = require('path');
|
|
3
|
+
const https = require('https');
|
|
4
|
+
const { execSync, spawn } = require('child_process');
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* 快速更新命令实现
|
|
8
|
+
*/
|
|
9
|
+
async function cmdToolUpdate(args = []) {
|
|
10
|
+
const pkg = require('../package.json');
|
|
11
|
+
const currentVersion = pkg.version;
|
|
12
|
+
const isCheckOnly = args.includes('--check');
|
|
13
|
+
|
|
14
|
+
console.log(`[Update] 当前版本: v${currentVersion}`);
|
|
15
|
+
|
|
16
|
+
let latestVersion = '';
|
|
17
|
+
try {
|
|
18
|
+
latestVersion = await fetchLatestVersion();
|
|
19
|
+
} catch (err) {
|
|
20
|
+
console.error(`[Update] 获取最新版本失败: ${err.message}`);
|
|
21
|
+
return;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
if (!latestVersion) {
|
|
25
|
+
console.error('[Update] 无法获取最新版本信息');
|
|
26
|
+
return;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
if (latestVersion === currentVersion) {
|
|
30
|
+
console.log('[Update] 已是最新版本。');
|
|
31
|
+
return;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
console.log(`[Update] 发现新版本: v${latestVersion}`);
|
|
35
|
+
|
|
36
|
+
if (isCheckOnly) {
|
|
37
|
+
console.log('[Update] 请运行 "codexmate update" 进行更新。');
|
|
38
|
+
return;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
// 确定安装方式并执行更新
|
|
42
|
+
const installMethod = detectInstallMethod();
|
|
43
|
+
console.log(`[Update] 检测到安装方式: ${installMethod}`);
|
|
44
|
+
|
|
45
|
+
try {
|
|
46
|
+
switch (installMethod) {
|
|
47
|
+
case 'npm':
|
|
48
|
+
updateViaNpm();
|
|
49
|
+
break;
|
|
50
|
+
case 'git':
|
|
51
|
+
updateViaGit();
|
|
52
|
+
break;
|
|
53
|
+
case 'standalone':
|
|
54
|
+
updateViaStandalone(latestVersion);
|
|
55
|
+
break;
|
|
56
|
+
default:
|
|
57
|
+
console.log('[Update] 未知安装方式,请手动更新。');
|
|
58
|
+
return;
|
|
59
|
+
}
|
|
60
|
+
console.log('[Update] 更新指令已下达。');
|
|
61
|
+
} catch (err) {
|
|
62
|
+
console.error(`[Update] 更新失败: ${err.message}`);
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
async function fetchLatestVersion() {
|
|
67
|
+
return new Promise((resolve, reject) => {
|
|
68
|
+
const url = 'https://registry.npmjs.org/codexmate/latest';
|
|
69
|
+
https.get(url, (res) => {
|
|
70
|
+
let data = '';
|
|
71
|
+
res.on('data', (chunk) => { data += chunk; });
|
|
72
|
+
res.on('end', () => {
|
|
73
|
+
try {
|
|
74
|
+
const json = JSON.parse(data);
|
|
75
|
+
resolve(json.version || '');
|
|
76
|
+
} catch (e) {
|
|
77
|
+
reject(new Error('解析 NPM 响应失败'));
|
|
78
|
+
}
|
|
79
|
+
});
|
|
80
|
+
}).on('error', (err) => {
|
|
81
|
+
reject(err);
|
|
82
|
+
});
|
|
83
|
+
});
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
function detectInstallMethod() {
|
|
87
|
+
const cliPath = path.resolve(__dirname, '..');
|
|
88
|
+
|
|
89
|
+
// 1. Git 仓库检测
|
|
90
|
+
if (fs.existsSync(path.join(cliPath, '.git'))) {
|
|
91
|
+
return 'git';
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
// 2. Standalone 检测 (通常在 ~/.codexmate)
|
|
95
|
+
const installDir = process.env.CODEXMATE_INSTALL_DIR || path.join(require('os').homedir(), '.codexmate');
|
|
96
|
+
if (cliPath.toLowerCase().startsWith(path.resolve(installDir).toLowerCase())) {
|
|
97
|
+
return 'standalone';
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
// 3. NPM 全局安装检测
|
|
101
|
+
// 如果路径包含 node_modules,通常是 npm 安装
|
|
102
|
+
if (cliPath.includes('node_modules')) {
|
|
103
|
+
return 'npm';
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
return 'unknown';
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
function updateViaNpm() {
|
|
110
|
+
console.log('[Update] 正在通过 npm 更新...');
|
|
111
|
+
// 使用 spawn 运行,这样可以实时看到输出
|
|
112
|
+
const isWindows = process.platform === 'win32';
|
|
113
|
+
const npmCmd = isWindows ? 'npm.cmd' : 'npm';
|
|
114
|
+
|
|
115
|
+
// 注意:npm install -g 可能会因为权限问题失败
|
|
116
|
+
console.log(`[Update] 执行: ${npmCmd} install -g codexmate`);
|
|
117
|
+
|
|
118
|
+
// 我们不直接在这里结束进程,因为 npm 更新后会覆盖文件
|
|
119
|
+
// 但为了安全,我们提示用户
|
|
120
|
+
try {
|
|
121
|
+
execSync(`${npmCmd} install -g codexmate`, { stdio: 'inherit' });
|
|
122
|
+
console.log('[Update] NPM 更新完成,请重启程序。');
|
|
123
|
+
} catch (e) {
|
|
124
|
+
throw new Error('NPM 更新失败,请尝试使用 sudo 或管理员权限运行。');
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
function updateViaGit() {
|
|
129
|
+
console.log('[Update] 正在通过 Git 更新...');
|
|
130
|
+
try {
|
|
131
|
+
execSync('git pull', { stdio: 'inherit', cwd: path.resolve(__dirname, '..') });
|
|
132
|
+
execSync('npm install', { stdio: 'inherit', cwd: path.resolve(__dirname, '..') });
|
|
133
|
+
console.log('[Update] Git 更新完成。');
|
|
134
|
+
} catch (e) {
|
|
135
|
+
throw new Error('Git 更新失败,请检查网络或本地改动。');
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
function updateViaStandalone(version) {
|
|
140
|
+
console.log(`[Update] 正在更新 Standalone 版本至 v${version}...`);
|
|
141
|
+
// 对于 Standalone,最简单的方法是重新运行安装脚本
|
|
142
|
+
// 或者提示用户重新运行 curl 命令
|
|
143
|
+
console.log('[Update] 请运行以下命令进行快速更新:');
|
|
144
|
+
console.log('curl -fsSL https://raw.githubusercontent.com/SakuraByteCore/codexmate/main/scripts/install.sh | bash');
|
|
145
|
+
|
|
146
|
+
// 尝试自动化(实验性)
|
|
147
|
+
if (process.platform !== 'win32') {
|
|
148
|
+
console.log('[Update] 尝试自动执行安装脚本...');
|
|
149
|
+
try {
|
|
150
|
+
const script = 'curl -fsSL https://raw.githubusercontent.com/SakuraByteCore/codexmate/main/scripts/install.sh | bash';
|
|
151
|
+
execSync(script, { stdio: 'inherit' });
|
|
152
|
+
} catch (e) {
|
|
153
|
+
console.warn('[Update] 自动脚本执行失败,请手动运行。');
|
|
154
|
+
}
|
|
155
|
+
} else {
|
|
156
|
+
console.log('[Update] Windows 环境下请手动下载最新版本或使用 npm 安装。');
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
module.exports = {
|
|
161
|
+
cmdToolUpdate
|
|
162
|
+
};
|