imtoagent 0.2.0 → 0.2.2

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/imtoagent CHANGED
@@ -13,7 +13,6 @@
13
13
 
14
14
  import * as fs from 'fs';
15
15
  import * as path from 'path';
16
- import * as readline from 'readline';
17
16
  import { getDataDir } from '../modules/utils/paths';
18
17
 
19
18
  const PID_FILE = '/tmp/imtoagent.pid';
@@ -46,13 +45,7 @@ switch (command) {
46
45
  case 'help':
47
46
  case '--help':
48
47
  case '-h':
49
- // 无命令 + 无配置 → 自动引导进入 setup
50
- if (!command && !fs.existsSync(path.join(getDataDir(), 'config.json'))) {
51
- console.log('⚠️ 未检测到配置文件,请先完成初始配置\n');
52
- await cmdSetup();
53
- } else {
54
- printHelp();
55
- }
48
+ printHelp();
56
49
  break;
57
50
  default:
58
51
  console.error(`❌ 未知命令: ${command}`);
@@ -64,21 +57,30 @@ switch (command) {
64
57
  // Help
65
58
  // ================================================================
66
59
  function printHelp() {
60
+ const dataDir = getDataDir();
61
+ const hasConfig = fs.existsSync(path.join(dataDir, 'config.json'));
62
+
67
63
  console.log(`
68
64
  imtoagent — IM ↔ Agent 统一网关
69
65
 
70
66
  用法:
71
- imtoagent setup 交互式配置向导
67
+ imtoagent setup 交互式配置向导(首次使用)
72
68
  imtoagent start 后台启动网关
73
69
  imtoagent stop 停止网关
74
70
  imtoagent status 查看运行状态
75
71
  imtoagent restore 热重载恢复
76
72
  imtoagent daemon 前台守护模式(自动重启 + 日志,适合 launchd/systemd 托管)
77
73
 
78
- 数据目录: ${getDataDir()}
74
+ 数据目录: ${dataDir}
79
75
  `);
76
+
77
+ if (!hasConfig) {
78
+ console.log('💡 首次使用,请先运行: imtoagent setup');
79
+ console.log();
80
+ }
80
81
  }
81
82
 
83
+
82
84
  // ================================================================
83
85
  // setup — 交互式向导
84
86
  // ================================================================
@@ -127,40 +129,12 @@ async function cmdStart() {
127
129
  }
128
130
  }
129
131
  if (missingBackends.length > 0) {
130
- console.error(`\n⚠️ 以下后端已配置但未安装:`);
131
- for (const name of missingBackends) {
132
- const b = BACKEND_DEFS.find((d) => d.type === name);
133
- console.error(` ❌ ${name} ${b ? `(${b.label})` : ''}`);
134
- }
135
-
136
- // 交互式询问是否自动安装
137
- const rl = readline.createInterface({
138
- input: process.stdin,
139
- output: process.stdout,
140
- });
141
-
142
- const answer = await new Promise<string>((resolve) => {
143
- rl.question('\n🔧 是否自动安装缺失的后端?[Y/n]: ', resolve);
144
- });
145
- rl.close();
146
-
147
- if (answer.trim().toLowerCase() !== 'n') {
148
- const { installBackend } = await import('../modules/utils/backend-check');
149
- let allInstalled = true;
150
-
151
- for (const name of missingBackends) {
152
- const ok = await installBackend(name as 'claude' | 'codex' | 'opencode');
153
- if (!ok) allInstalled = false;
154
- }
155
-
156
- if (allInstalled) {
157
- console.log('\n✅ 所有后端安装完成,正在启动网关...\n');
158
- } else {
159
- console.error('\n⚠️ 部分后端安装失败。网关仍可启动,但使用未安装的后端时会报错。\n');
160
- }
161
- } else {
162
- console.error('\n跳过安装。网关仍可启动,但使用未安装的后端时会报错。\n');
132
+ console.error(`\n⚠️ 以下后端已配置但未安装,网关启动后发消息会报错:`);
133
+ for (const b of missingBackends) {
134
+ console.error(` ❌ ${b}`);
163
135
  }
136
+ console.error(`\n请先安装缺失的后端,或运行 "imtoagent setup" 修改配置。\n`);
137
+ // 不强制退出,允许用户先启动网关再慢慢装后端
164
138
  }
165
139
  } catch {
166
140
  // 检查失败不影响启动
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "imtoagent",
3
- "version": "0.2.0",
3
+ "version": "0.2.2",
4
4
  "description": "IM ↔ Agent 统一网关 — 飞书/Telegram/微信/企业微信对接 Claude Code/Codex/OpenCode",
5
5
  "type": "module",
6
6
  "bin": {
@@ -15,7 +15,7 @@
15
15
  "README.md"
16
16
  ],
17
17
  "scripts": {
18
- "postinstall": "bun run scripts/postinstall.ts",
18
+ "postinstall": "node scripts/postinstall.cjs",
19
19
  "start": "bun run index.ts"
20
20
  },
21
21
  "dependencies": {
@@ -29,7 +29,7 @@
29
29
  },
30
30
  "repository": {
31
31
  "type": "git",
32
- "url": "https://github.com/YOUR_USERNAME/imtoagent.git"
32
+ "url": "git+https://github.com/YOUR_USERNAME/imtoagent.git"
33
33
  },
34
34
  "keywords": [
35
35
  "im",
@@ -0,0 +1,90 @@
1
+ #!/usr/bin/env node
2
+ // ================================================================
3
+ // postinstall.js — npm 安装后引导脚本
4
+ // ================================================================
5
+ // 用 Node.js 运行(不依赖 bun),兼容所有安装环境
6
+ // ================================================================
7
+
8
+ const fs = require('fs');
9
+ const path = require('path');
10
+ const { execSync } = require('child_process');
11
+
12
+ const HOME = process.env.HOME || process.env.USERPROFILE || '';
13
+ const DATA_DIR = path.join(HOME, '.imtoagent');
14
+
15
+ // 检测 bun 路径
16
+ function findBun() {
17
+ // 1. PATH 中有 bun
18
+ try {
19
+ const r = execSync('which bun', { encoding: 'utf-8', timeout: 3000 }).trim();
20
+ if (r) return r;
21
+ } catch {}
22
+ // 2. 常见安装路径
23
+ const candidates = [
24
+ path.join(HOME, '.bun', 'bin', 'bun'),
25
+ '/usr/local/bin/bun',
26
+ '/opt/homebrew/bin/bun',
27
+ ];
28
+ for (const p of candidates) {
29
+ if (fs.existsSync(p)) return p;
30
+ }
31
+ return null;
32
+ }
33
+
34
+ try {
35
+ const configExists = fs.existsSync(path.join(DATA_DIR, 'config.json'));
36
+
37
+ if (configExists) {
38
+ console.log(`
39
+ ✅ imtoagent 升级成功!
40
+ 数据目录: ${DATA_DIR}
41
+ 配置文件保持不变,无需重新配置。
42
+ 运行 "imtoagent start" 启动网关。
43
+ `);
44
+ } else {
45
+ console.log(`
46
+ +----------------------------------------------------------+
47
+ | |
48
+ | 🎉 imtoagent 安装成功! |
49
+ | |
50
+ | 首次使用需要配置 IM 凭证和模型供应商 |
51
+ | |
52
+ +----------------------------------------------------------+
53
+ `);
54
+
55
+ if (process.stdin.isTTY) {
56
+ const readline = require('readline');
57
+ const rl = readline.createInterface({ input: process.stdin, output: process.stdout });
58
+
59
+ rl.question('是否立即运行配置向导?[Y/n]: ', (answer) => {
60
+ rl.close();
61
+ const yes = (answer || '').trim().toLowerCase();
62
+ if (yes === '' || yes === 'y' || yes === 'yes') {
63
+ const bunPath = findBun();
64
+ if (!bunPath) {
65
+ console.error('\n⚠️ bun 未安装,请先安装: https://bun.sh');
66
+ console.error(' 安装后运行: imtoagent setup\n');
67
+ process.exit(0);
68
+ }
69
+ console.log('\n🚀 启动配置向导...\n');
70
+ const pkgDir = path.resolve(__dirname, '..');
71
+ execSync(`${bunPath} bin/imtoagent setup`, {
72
+ cwd: pkgDir,
73
+ stdio: 'inherit',
74
+ env: { ...process.env },
75
+ });
76
+ } else {
77
+ console.log('\n稍后运行 "imtoagent setup" 即可开始配置。');
78
+ }
79
+ });
80
+ } else {
81
+ console.log(' 运行 "imtoagent setup" 开始配置');
82
+ console.log(' 然后运行 "imtoagent start" 启动网关\n');
83
+ }
84
+ }
85
+ } catch (e) {
86
+ // 静默失败,不影响安装
87
+ if (e.message && !e.message.includes('readline')) {
88
+ console.error(`[postinstall] 提示失败: ${e.message}`);
89
+ }
90
+ }