@bolloon/bolloon-agent 0.1.37 → 0.1.38

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.
@@ -0,0 +1,153 @@
1
+ /**
2
+ * Postinstall 脚本
3
+ * npm 安装后自动执行
4
+ *
5
+ * 主要任务:
6
+ * 1. 确保 bin 目录存在且可执行
7
+ * 2. 初始化本地配置
8
+ * 3. 检查依赖完整性
9
+ */
10
+
11
+ import * as fs from 'fs';
12
+ import * as path from 'path';
13
+ import { fileURLToPath } from 'url';
14
+
15
+ const __filename = fileURLToPath(import.meta.url);
16
+ const __dirname = path.dirname(__filename);
17
+ const rootDir = path.join(__dirname, '..');
18
+
19
+ const RESET = '\x1b[0m';
20
+ const GREEN = '\x1b[32m';
21
+ const YELLOW = '\x1b[33m';
22
+ const CYAN = '\x1b[36m';
23
+
24
+ function log(msg, color = RESET) {
25
+ console.log(color + msg + RESET);
26
+ }
27
+
28
+ function initUserDirs() {
29
+ // 创建用户数据目录
30
+ const home = process.env.HOME || process.env.USERPROFILE || '/tmp';
31
+ const bolloonDir = path.join(home, '.bolloon');
32
+
33
+ const dirs = [
34
+ bolloonDir,
35
+ path.join(bolloonDir, 'sessions'),
36
+ path.join(bolloonDir, 'peer-store'),
37
+ ];
38
+
39
+ for (const dir of dirs) {
40
+ if (!fs.existsSync(dir)) {
41
+ fs.mkdirSync(dir, { recursive: true });
42
+ log(` ✓ 创建目录: ${dir}`, GREEN);
43
+ }
44
+ }
45
+
46
+ // 初始化配置文件
47
+ const configPath = path.join(bolloonDir, 'config.json');
48
+ if (!fs.existsSync(configPath)) {
49
+ const defaultConfig = {
50
+ version: '0.1.12',
51
+ initializedAt: new Date().toISOString(),
52
+ defaults: {
53
+ port: 54188,
54
+ theme: 'dark',
55
+ autoConnect: true,
56
+ },
57
+ providers: {
58
+ minimax: { enabled: false },
59
+ openai: { enabled: false },
60
+ anthropic: { enabled: false },
61
+ }
62
+ };
63
+ fs.writeFileSync(configPath, JSON.stringify(defaultConfig, null, 2));
64
+ log(` ✓ 创建配置: ${configPath}`, GREEN);
65
+ }
66
+
67
+ return bolloonDir;
68
+ }
69
+
70
+ function checkNativeDeps() {
71
+ // 检查必要的原生依赖
72
+ const nativeDeps = [
73
+ 'libp2p',
74
+ '@diap/sdk',
75
+ ];
76
+
77
+ let allOk = true;
78
+
79
+ for (const dep of nativeDeps) {
80
+ const depPath = path.join(rootDir, 'node_modules', dep);
81
+ if (!fs.existsSync(depPath)) {
82
+ log(` ⚠ 缺少依赖: ${dep}`, YELLOW);
83
+ allOk = false;
84
+ }
85
+ }
86
+
87
+ return allOk;
88
+ }
89
+
90
+ function setupPlatform() {
91
+ const platform = process.platform;
92
+
93
+ log(`\n 平台: ${platform}`, CYAN);
94
+
95
+ if (platform === 'win32') {
96
+ // Windows: 确保 .cmd 文件存在
97
+ const binDir = path.join(rootDir, 'bin');
98
+ const cmdPath = path.join(binDir, 'bolloon.cmd');
99
+
100
+ if (fs.existsSync(binDir) && !fs.existsSync(cmdPath)) {
101
+ const cmdContent = `@echo off
102
+ node "%~dp0..\\dist\\cli.js" %*
103
+ `;
104
+ fs.writeFileSync(cmdPath, cmdContent);
105
+ log(' ✓ Windows 入口已创建', GREEN);
106
+ }
107
+ } else {
108
+ // Unix/Linux/Mac: 确保 bin 文件可执行
109
+ const binDir = path.join(rootDir, 'bin');
110
+ const binPath = path.join(binDir, 'bolloon.js');
111
+
112
+ if (fs.existsSync(binPath)) {
113
+ try {
114
+ fs.chmodSync(binPath, 0o755);
115
+ log(' ✓ bin/bolloon.js 已设为可执行', GREEN);
116
+ } catch (err) {
117
+ log(` ⚠ 无法设置执行权限: ${err.message}`, YELLOW);
118
+ }
119
+ }
120
+ }
121
+ }
122
+
123
+ function main() {
124
+ console.log('\n📦 Bolloon 安装后处理...\n');
125
+
126
+ try {
127
+ // 1. 初始化用户目录
128
+ const bolloonDir = initUserDirs();
129
+ log(` ✓ 用户数据目录: ${bolloonDir}`, GREEN);
130
+
131
+ // 2. 检查依赖
132
+ const depsOk = checkNativeDeps();
133
+ if (!depsOk) {
134
+ log('\n ⚠ 部分依赖缺失,建议运行: npm install', YELLOW);
135
+ }
136
+
137
+ // 3. 平台特定设置
138
+ setupPlatform();
139
+
140
+ console.log('\n✅ 安装完成!\n');
141
+ console.log(' 使用方式:');
142
+ console.log(' bolloon # 启动 GUI');
143
+ console.log(' bolloon --web # 启动 Web UI');
144
+ console.log(' bolloon --cli # 命令行模式');
145
+ console.log(' bolloon --help # 显示帮助\n');
146
+ console.log(` 配置文件: ${path.join(bolloonDir, 'config.json')}\n`);
147
+ } catch (err) {
148
+ console.error('\n❌ 安装后处理失败:', err.message);
149
+ console.error(' 这通常不影响基本功能,继续安装...\n');
150
+ }
151
+ }
152
+
153
+ main();