openclawapi 1.3.10 → 1.3.12

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 (2) hide show
  1. package/cli.js +83 -57
  2. package/package.json +1 -1
package/cli.js CHANGED
@@ -381,7 +381,7 @@ async function activate(paths, type) {
381
381
 
382
382
  // ============ 测试连接 ============
383
383
  async function testConnection(paths) {
384
- console.log(chalk.cyan('🧪 测试 API 连接\n'));
384
+ console.log(chalk.cyan('🧪 测试 OpenClaw Gateway 连接\n'));
385
385
 
386
386
  const config = readConfig(paths.openclawConfig);
387
387
 
@@ -415,68 +415,35 @@ async function testConnection(paths) {
415
415
  return;
416
416
  }
417
417
 
418
+ // 获取 Gateway 配置
419
+ const gatewayPort = config.gateway?.port || 18789;
420
+ const gatewayToken = config.gateway?.auth?.token || '';
421
+
418
422
  console.log(chalk.gray(`当前激活: ${typeLabel}`));
419
- console.log(chalk.gray(`节点: ${provider.baseUrl}`));
420
- console.log(chalk.gray(`模型: ${provider.models?.[0]?.id || 'N/A'}\n`));
423
+ console.log(chalk.gray(`中转节点: ${provider.baseUrl}`));
424
+ console.log(chalk.gray(`模型: ${primary}`));
425
+ console.log(chalk.gray(`Gateway: http://localhost:${gatewayPort}\n`));
426
+
427
+ // 步骤1: 先重启 Gateway 使配置生效
428
+ console.log(chalk.cyan('步骤 1/2: 重启 Gateway 使配置生效...'));
429
+ await restartGateway();
421
430
 
422
- // 直接测试中转 API
423
- console.log(chalk.cyan(`正在测试 ${typeLabel} API...`));
431
+ // 步骤2: 通过本地 Gateway 端口测试
432
+ console.log(chalk.cyan(`\n步骤 2/2: 通过 Gateway 测试 ${typeLabel}...`));
424
433
 
425
434
  try {
426
435
  const startTime = Date.now();
436
+ const result = await testGatewayApi(gatewayPort, gatewayToken, primary);
437
+ const latency = Date.now() - startTime;
427
438
 
428
- if (type === 'claude') {
429
- const result = await testClaudeApi(provider.baseUrl, provider.apiKey, provider.models?.[0]?.id);
430
- const latency = Date.now() - startTime;
431
-
432
- if (result.success) {
433
- console.log(chalk.green(`\n✅ ${typeLabel} API 连接成功!`));
434
- console.log(chalk.cyan(` 响应时间: ${latency}ms`));
435
- console.log(chalk.yellow(` 模型回复: ${result.message}`));
436
-
437
- // 询问是否重启 gateway
438
- const { restart } = await inquirer.prompt([{
439
- type: 'confirm',
440
- name: 'restart',
441
- message: '是否重启 OpenClaw Gateway 使配置生效?',
442
- default: true
443
- }]);
444
-
445
- if (restart) {
446
- await restartGateway();
447
- } else {
448
- console.log(chalk.gray(`\n💡 稍后可手动运行: openclaw gateway restart`));
449
- }
450
- } else {
451
- console.log(chalk.red(`\n❌ ${typeLabel} API 连接失败`));
452
- console.log(chalk.red(` 错误: ${result.error}`));
453
- }
439
+ if (result.success) {
440
+ console.log(chalk.green(`\n✅ 测试成功!配置已生效`));
441
+ console.log(chalk.cyan(` 响应时间: ${latency}ms`));
442
+ console.log(chalk.yellow(` 模型回复: ${result.message}`));
454
443
  } else {
455
- const result = await testCodexApi(provider.baseUrl, provider.apiKey, provider.models?.[0]?.id);
456
- const latency = Date.now() - startTime;
457
-
458
- if (result.success) {
459
- console.log(chalk.green(`\n✅ ${typeLabel} API 连接成功!`));
460
- console.log(chalk.cyan(` 响应时间: ${latency}ms`));
461
- console.log(chalk.yellow(` 模型回复: ${result.message}`));
462
-
463
- // 询问是否重启 gateway
464
- const { restart } = await inquirer.prompt([{
465
- type: 'confirm',
466
- name: 'restart',
467
- message: '是否重启 OpenClaw Gateway 使配置生效?',
468
- default: true
469
- }]);
470
-
471
- if (restart) {
472
- await restartGateway();
473
- } else {
474
- console.log(chalk.gray(`\n💡 稍后可手动运行: openclaw gateway restart`));
475
- }
476
- } else {
477
- console.log(chalk.red(`\n❌ ${typeLabel} API 连接失败`));
478
- console.log(chalk.red(` 错误: ${result.error}`));
479
- }
444
+ console.log(chalk.red(`\n❌ Gateway 连接失败`));
445
+ console.log(chalk.red(` 错误: ${result.error}`));
446
+ console.log(chalk.gray(`\n💡 请确保 Gateway 正在运行: openclaw gateway`));
480
447
  }
481
448
  } catch (error) {
482
449
  console.log(chalk.red(`❌ 测试失败: ${error.message}`));
@@ -527,7 +494,66 @@ async function restartGateway() {
527
494
  });
528
495
  }
529
496
 
530
- // Claude API 测试
497
+ // Gateway API 测试 - 通过本地 Gateway 端口测试
498
+ function testGatewayApi(port, token, model) {
499
+ return new Promise((resolve) => {
500
+ const postData = JSON.stringify({
501
+ model: model,
502
+ messages: [{ role: 'user', content: '你是什么模型?' }]
503
+ });
504
+
505
+ const options = {
506
+ hostname: '127.0.0.1',
507
+ port: port,
508
+ path: '/v1/chat/completions',
509
+ method: 'POST',
510
+ timeout: 60000,
511
+ headers: {
512
+ 'Content-Type': 'application/json',
513
+ 'Authorization': `Bearer ${token}`,
514
+ 'Content-Length': Buffer.byteLength(postData)
515
+ }
516
+ };
517
+
518
+ const req = http.request(options, (res) => {
519
+ let data = '';
520
+ res.on('data', chunk => data += chunk);
521
+ res.on('end', () => {
522
+ try {
523
+ const json = JSON.parse(data);
524
+ // OpenAI 格式响应
525
+ if (json.choices && json.choices[0]?.message?.content) {
526
+ resolve({ success: true, message: json.choices[0].message.content });
527
+ } else if (json.error) {
528
+ resolve({ success: false, error: json.error.message || JSON.stringify(json.error) });
529
+ } else {
530
+ resolve({ success: false, error: `HTTP ${res.statusCode}: ${data.substring(0, 300)}` });
531
+ }
532
+ } catch {
533
+ resolve({ success: false, error: `HTTP ${res.statusCode}: ${data.substring(0, 300)}` });
534
+ }
535
+ });
536
+ });
537
+
538
+ req.on('timeout', () => {
539
+ req.destroy();
540
+ resolve({ success: false, error: '请求超时 (60s)' });
541
+ });
542
+
543
+ req.on('error', (e) => {
544
+ if (e.code === 'ECONNREFUSED') {
545
+ resolve({ success: false, error: 'Gateway 未运行,请先启动: openclaw gateway' });
546
+ } else {
547
+ resolve({ success: false, error: e.message });
548
+ }
549
+ });
550
+
551
+ req.write(postData);
552
+ req.end();
553
+ });
554
+ }
555
+
556
+ // Claude API 测试 (直接测试中转,备用)
531
557
  function testClaudeApi(baseUrl, apiKey, model) {
532
558
  return new Promise((resolve) => {
533
559
  const urlObj = new URL(baseUrl);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "openclawapi",
3
- "version": "1.3.10",
3
+ "version": "1.3.12",
4
4
  "description": "跨平台 OpenClaw/Clawdbot 配置管理工具 - 管理中转地址、模型切换、API Keys、测速优化",
5
5
  "main": "cli.js",
6
6
  "bin": {