@bolloon/bolloon-agent 0.2.7 → 0.2.9
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/dist/agents/agent-manifest-protocol.js +52 -0
- package/dist/agents/peer-manifest-loader.js +210 -0
- package/dist/agents/pi-sdk.js +24 -41
- package/dist/agents/session-store.js +17 -3
- package/dist/agents/shell-guard.js +2 -2
- package/dist/agents/workflow-pivot-loop.js +12 -3
- package/dist/bootstrap/chat-archiver.js +276 -0
- package/dist/bootstrap/context-collector.js +6 -3
- package/dist/bootstrap/lifecycle-hooks.js +15 -1
- package/dist/bootstrap/memory-compressor.js +170 -0
- package/dist/bootstrap/persona-loader.js +94 -0
- package/dist/network/p2p-outbox.js +161 -0
- package/dist/network/peer-fs.js +420 -0
- package/dist/network/peer-resource-bridge.js +216 -0
- package/dist/web/client.js +29 -27
- package/dist/web/components/p2p/index.js +17 -5
- package/dist/web/components/p2p/p2p-modal.js +9 -2
- package/dist/web/server.js +660 -20
- package/dist/web/util/safe-name.js +32 -0
- package/package.json +5 -2
- package/scripts/build-cli.js +216 -0
- package/scripts/build-web.ts +125 -0
- package/scripts/postinstall.js +153 -0
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 通用名兜底. fallback 默认 '(未命名)'. 中文场景.
|
|
3
|
+
*/
|
|
4
|
+
export function safeChannelName(input, fallback) {
|
|
5
|
+
return safeNameInternal(input, fallback ?? '(未命名)', ['undefined', 'null', 'NaN']);
|
|
6
|
+
}
|
|
7
|
+
/**
|
|
8
|
+
* 英文场景兜底, 默认 'Unknown'.
|
|
9
|
+
*/
|
|
10
|
+
export function safePeerName(input, fallback) {
|
|
11
|
+
return safeNameInternal(input, fallback ?? 'Unknown', ['undefined', 'null', 'NaN']);
|
|
12
|
+
}
|
|
13
|
+
/**
|
|
14
|
+
* 兜底任意字段, 由 caller 指定默认 + 无效字面量集合.
|
|
15
|
+
*/
|
|
16
|
+
export function safeName(input, fallback, invalidLiterals = ['undefined', 'null', 'NaN']) {
|
|
17
|
+
return safeNameInternal(input, fallback, invalidLiterals);
|
|
18
|
+
}
|
|
19
|
+
function safeNameInternal(input, fallback, invalidLiterals) {
|
|
20
|
+
if (input === undefined || input === null)
|
|
21
|
+
return fallback;
|
|
22
|
+
if (typeof input === 'number' && Number.isNaN(input))
|
|
23
|
+
return fallback;
|
|
24
|
+
const s = String(input).trim();
|
|
25
|
+
if (!s)
|
|
26
|
+
return fallback;
|
|
27
|
+
for (const lit of invalidLiterals) {
|
|
28
|
+
if (s === lit)
|
|
29
|
+
return fallback;
|
|
30
|
+
}
|
|
31
|
+
return s;
|
|
32
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@bolloon/bolloon-agent",
|
|
3
|
-
"version": "0.2.
|
|
3
|
+
"version": "0.2.9",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"description": "P2P AI Document Agent - 全局安装后执行 `bolloon` 启动产品",
|
|
6
6
|
"main": "dist/cli-entry.js",
|
|
@@ -12,7 +12,10 @@
|
|
|
12
12
|
"license": "MIT",
|
|
13
13
|
"files": [
|
|
14
14
|
"dist",
|
|
15
|
-
"bin"
|
|
15
|
+
"bin",
|
|
16
|
+
"scripts/postinstall.js",
|
|
17
|
+
"scripts/build-cli.js",
|
|
18
|
+
"scripts/build-web.ts"
|
|
16
19
|
],
|
|
17
20
|
"bin": {
|
|
18
21
|
"bolloon": "./bin/bolloon-cli.cjs"
|
|
@@ -0,0 +1,216 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* CLI 构建脚本
|
|
3
|
+
*
|
|
4
|
+
* 生成 bin/bolloon.js 和 bin/bolloon.cmd 入口文件
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
import * as fs from 'fs';
|
|
8
|
+
import * as path from 'path';
|
|
9
|
+
import { fileURLToPath } from 'url';
|
|
10
|
+
|
|
11
|
+
const __filename = fileURLToPath(import.meta.url);
|
|
12
|
+
const __dirname = path.dirname(__filename);
|
|
13
|
+
const rootDir = path.join(__dirname, '..');
|
|
14
|
+
|
|
15
|
+
const binDir = path.join(rootDir, 'bin');
|
|
16
|
+
if (!fs.existsSync(binDir)) {
|
|
17
|
+
fs.mkdirSync(binDir, { recursive: true });
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
// Windows 批处理入口
|
|
21
|
+
const winContent = `@echo off
|
|
22
|
+
set "BOLLOON_ROOT=%~dp0"
|
|
23
|
+
set "BOLLOON_ROOT=%BOLLOON_ROOT:~0,-1%"
|
|
24
|
+
|
|
25
|
+
REM 确定入口文件
|
|
26
|
+
set "ENTRY=%BOLLOON_ROOT%\\dist\\index.js"
|
|
27
|
+
if not exist "%ENTRY%" (
|
|
28
|
+
set "ENTRY=%BOLLOON_ROOT%\\src\\index.ts"
|
|
29
|
+
)
|
|
30
|
+
|
|
31
|
+
node "%ENTRY%" %*
|
|
32
|
+
`;
|
|
33
|
+
|
|
34
|
+
fs.writeFileSync(path.join(binDir, 'bolloon.cmd'), winContent);
|
|
35
|
+
|
|
36
|
+
// Unix/Linux/Mac 入口脚本
|
|
37
|
+
const unixContent = `#!/usr/bin/env node
|
|
38
|
+
const path = require("path");
|
|
39
|
+
const { spawn } = require("child_process");
|
|
40
|
+
const fs = require("fs");
|
|
41
|
+
|
|
42
|
+
const RESET = "\\x1b[0m";
|
|
43
|
+
const BOLD = "\\x1b[1m";
|
|
44
|
+
const CYAN = "\\x1b[36m";
|
|
45
|
+
const GREEN = "\\x1b[32m";
|
|
46
|
+
const MAGENTA = "\\x1b[35m";
|
|
47
|
+
|
|
48
|
+
function log(msg, color) {
|
|
49
|
+
console.log((color || RESET) + msg + RESET);
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
function printBanner() {
|
|
53
|
+
console.log("\\n" + CYAN + BOLD + [
|
|
54
|
+
" ╔═══════════════════════════════════════════╗",
|
|
55
|
+
" ║ 🤖 Bolloon Agent ║",
|
|
56
|
+
" ║ P2P AI Document Processor ║",
|
|
57
|
+
" ╚═══════════════════════════════════════════╝"
|
|
58
|
+
].join("\\n") + RESET + "\\n");
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
function getMainEntry() {
|
|
62
|
+
const distDir = path.dirname(require.main.filename);
|
|
63
|
+
const distIndex = path.join(distDir, "index.js");
|
|
64
|
+
if (fs.existsSync(distIndex)) {
|
|
65
|
+
return distIndex;
|
|
66
|
+
}
|
|
67
|
+
return path.join(process.cwd(), "src", "index.ts");
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
function parseArgs() {
|
|
71
|
+
const args = process.argv.slice(2);
|
|
72
|
+
if (args.length === 0) {
|
|
73
|
+
return { mode: "gui", args: [] };
|
|
74
|
+
}
|
|
75
|
+
const first = args[0];
|
|
76
|
+
switch (first) {
|
|
77
|
+
case "-v":
|
|
78
|
+
case "--version":
|
|
79
|
+
return { mode: "version", args: [] };
|
|
80
|
+
case "-h":
|
|
81
|
+
case "--help":
|
|
82
|
+
return { mode: "help", args: [] };
|
|
83
|
+
case "-g":
|
|
84
|
+
case "--gui":
|
|
85
|
+
return { mode: "gui", args: args.slice(1) };
|
|
86
|
+
case "-w":
|
|
87
|
+
case "--web":
|
|
88
|
+
return { mode: "web", args: args.slice(1) };
|
|
89
|
+
case "-c":
|
|
90
|
+
case "--cli":
|
|
91
|
+
return { mode: "cli", args: args.slice(1) };
|
|
92
|
+
default:
|
|
93
|
+
return { mode: "passthrough", args };
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
async function startElectron(additionalArgs) {
|
|
98
|
+
try {
|
|
99
|
+
const electron = require("electron");
|
|
100
|
+
const distDir = path.dirname(require.main.filename);
|
|
101
|
+
let mainPath = path.join(distDir, "electron.js");
|
|
102
|
+
if (!fs.existsSync(mainPath)) {
|
|
103
|
+
mainPath = path.join(process.cwd(), "src", "electron.ts");
|
|
104
|
+
}
|
|
105
|
+
log("启动 Electron...", CYAN);
|
|
106
|
+
const child = spawn(electron, [mainPath, ...additionalArgs], {
|
|
107
|
+
stdio: "inherit",
|
|
108
|
+
env: { ...process.env, NODE_ENV: "development" }
|
|
109
|
+
});
|
|
110
|
+
child.on("error", (err) => {
|
|
111
|
+
log("Electron 启动失败: " + err.message, MAGENTA);
|
|
112
|
+
process.exit(1);
|
|
113
|
+
});
|
|
114
|
+
child.on("exit", (code) => process.exit(code || 0));
|
|
115
|
+
} catch (err) {
|
|
116
|
+
log("Electron 不可用,切换到 Web 模式...", CYAN);
|
|
117
|
+
await startWebServer(additionalArgs);
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
async function startWebServer(additionalArgs) {
|
|
122
|
+
const mainPath = getMainEntry();
|
|
123
|
+
const webArgs = ["--web", ...additionalArgs];
|
|
124
|
+
log("启动 Web 服务...", CYAN);
|
|
125
|
+
const child = spawn(process.execPath, [mainPath, ...webArgs], { stdio: "inherit" });
|
|
126
|
+
child.on("error", (err) => {
|
|
127
|
+
log("Web 服务启动失败: " + err.message, MAGENTA);
|
|
128
|
+
process.exit(1);
|
|
129
|
+
});
|
|
130
|
+
child.on("exit", (code) => process.exit(code || 0));
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
async function startCLI(additionalArgs) {
|
|
134
|
+
const mainPath = getMainEntry();
|
|
135
|
+
log("启动命令行界面...", CYAN);
|
|
136
|
+
const child = spawn(process.execPath, [mainPath, ...additionalArgs], { stdio: "inherit" });
|
|
137
|
+
child.on("error", (err) => {
|
|
138
|
+
log("CLI 启动失败: " + err.message, MAGENTA);
|
|
139
|
+
process.exit(1);
|
|
140
|
+
});
|
|
141
|
+
child.on("exit", (code) => process.exit(code || 0));
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
async function main() {
|
|
145
|
+
const { mode, args } = parseArgs();
|
|
146
|
+
switch (mode) {
|
|
147
|
+
case "version":
|
|
148
|
+
console.log("Bolloon Agent v0.1.1");
|
|
149
|
+
break;
|
|
150
|
+
case "help":
|
|
151
|
+
printBanner();
|
|
152
|
+
console.log(BOLD + "用法:" + RESET + " bolloon [选项] [命令] [参数]");
|
|
153
|
+
console.log(BOLD + "选项:" + RESET + " --gui, -g 启动图形界面");
|
|
154
|
+
console.log(" --web, -w 启动 Web UI");
|
|
155
|
+
console.log(" --cli, -c 启动命令行界面");
|
|
156
|
+
console.log(" --version, -v 显示版本");
|
|
157
|
+
console.log(" --help, -h 显示帮助");
|
|
158
|
+
console.log(BOLD + "示例:" + RESET + " bolloon # 启动图形界面");
|
|
159
|
+
console.log(" bolloon --web # 启动 Web UI");
|
|
160
|
+
console.log(" bolloon --read file # 读取文档");
|
|
161
|
+
break;
|
|
162
|
+
case "gui":
|
|
163
|
+
printBanner();
|
|
164
|
+
await startElectron(args);
|
|
165
|
+
break;
|
|
166
|
+
case "web":
|
|
167
|
+
printBanner();
|
|
168
|
+
await startWebServer(args);
|
|
169
|
+
break;
|
|
170
|
+
case "cli":
|
|
171
|
+
await startCLI(args);
|
|
172
|
+
break;
|
|
173
|
+
case "passthrough":
|
|
174
|
+
const mainPath = getMainEntry();
|
|
175
|
+
const child = spawn(process.execPath, [mainPath, ...args], { stdio: "inherit" });
|
|
176
|
+
child.on("error", (err) => {
|
|
177
|
+
log("执行失败: " + err.message, MAGENTA);
|
|
178
|
+
process.exit(1);
|
|
179
|
+
});
|
|
180
|
+
child.on("exit", (code) => process.exit(code || 0));
|
|
181
|
+
break;
|
|
182
|
+
default:
|
|
183
|
+
log("未知模式: " + mode, MAGENTA);
|
|
184
|
+
printBanner();
|
|
185
|
+
console.log("输入 --help 查看帮助");
|
|
186
|
+
process.exit(1);
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
main().catch((err) => {
|
|
191
|
+
console.error("Fatal error:", err);
|
|
192
|
+
process.exit(1);
|
|
193
|
+
});
|
|
194
|
+
`;
|
|
195
|
+
|
|
196
|
+
fs.writeFileSync(path.join(binDir, 'bolloon.cjs'), unixContent);
|
|
197
|
+
|
|
198
|
+
// 确保 bin/bolloon.js 存在(npm link 需要)
|
|
199
|
+
// 优先用符号链接(POSIX),Windows 上若权限不足则退化为复制文件
|
|
200
|
+
const jsSymlink = path.join(binDir, 'bolloon.js');
|
|
201
|
+
if (fs.existsSync(jsSymlink)) fs.unlinkSync(jsSymlink);
|
|
202
|
+
try {
|
|
203
|
+
fs.symlinkSync('bolloon.cjs', jsSymlink);
|
|
204
|
+
} catch (err) {
|
|
205
|
+
if (err && err.code === 'EPERM') {
|
|
206
|
+
fs.copyFileSync(path.join(binDir, 'bolloon.cjs'), jsSymlink);
|
|
207
|
+
console.warn(' ⚠ symlink 不支持(Windows),已退化为文件复制');
|
|
208
|
+
} else {
|
|
209
|
+
throw err;
|
|
210
|
+
}
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
console.log("✓ CLI 构建完成");
|
|
214
|
+
console.log(" bin/bolloon.cjs - CommonJS 入口");
|
|
215
|
+
console.log(" bin/bolloon.js - 符号链接 -> bolloon.cjs");
|
|
216
|
+
console.log(" bin/bolloon.cmd - Windows 入口");
|
|
@@ -0,0 +1,125 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Web 构建脚本
|
|
3
|
+
*/
|
|
4
|
+
import * as fs from 'fs/promises';
|
|
5
|
+
import * as path from 'path';
|
|
6
|
+
import { execSync } from 'child_process';
|
|
7
|
+
import * as esbuild from 'esbuild';
|
|
8
|
+
|
|
9
|
+
const ROOT = path.resolve(import.meta.dirname, '..');
|
|
10
|
+
const DIST_WEB = path.join(ROOT, 'dist', 'web');
|
|
11
|
+
|
|
12
|
+
async function main() {
|
|
13
|
+
console.log('[build-web] 开始构建...');
|
|
14
|
+
|
|
15
|
+
// 重要: 不能 rm -rf 整个 dist/web/, 否则会删掉 build:main 编译出来的
|
|
16
|
+
// dist/web/server.js. 只清理 web 静态资源, 保留 server.js.
|
|
17
|
+
for (const f of ['index.html', 'api-config.html', 'style.css', 'client.js', 'components']) {
|
|
18
|
+
await fs.rm(path.join(DIST_WEB, f), { recursive: true, force: true });
|
|
19
|
+
}
|
|
20
|
+
await fs.mkdir(DIST_WEB, { recursive: true });
|
|
21
|
+
await fs.mkdir(path.join(DIST_WEB, 'components', 'p2p'), { recursive: true });
|
|
22
|
+
|
|
23
|
+
// 编译 TypeScript 模块
|
|
24
|
+
console.log('[build-web] 编译 TypeScript 模块...');
|
|
25
|
+
const moduleFiles = [
|
|
26
|
+
'src/web/components/p2p/types.ts',
|
|
27
|
+
'src/web/components/p2p/p2p-store-memory.ts',
|
|
28
|
+
'src/web/components/p2p/p2p-identity.ts',
|
|
29
|
+
'src/web/components/p2p/p2p-connection.ts',
|
|
30
|
+
'src/web/components/p2p/p2p-messages.ts',
|
|
31
|
+
'src/web/components/p2p/p2p-manager.ts',
|
|
32
|
+
];
|
|
33
|
+
|
|
34
|
+
try {
|
|
35
|
+
execSync(`npx tsc --outDir dist/web/components/p2p --declaration false --skipLibCheck --target ES2022 --module ESNext --moduleResolution bundler ${moduleFiles.join(' ')}`, {
|
|
36
|
+
cwd: ROOT,
|
|
37
|
+
stdio: 'inherit'
|
|
38
|
+
});
|
|
39
|
+
} catch {
|
|
40
|
+
console.log('[build-web] TypeScript 编译有警告,继续...');
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
// 编译 P2P Modal UI(esbuild)
|
|
44
|
+
console.log('[build-web] 编译 P2P Modal UI...');
|
|
45
|
+
await esbuild.build({
|
|
46
|
+
entryPoints: [path.join(ROOT, 'src/web/components/p2p/index.ts')],
|
|
47
|
+
outfile: path.join(DIST_WEB, 'components/p2p/index.js'),
|
|
48
|
+
format: 'esm',
|
|
49
|
+
target: 'es2022',
|
|
50
|
+
platform: 'browser',
|
|
51
|
+
minify: false,
|
|
52
|
+
});
|
|
53
|
+
|
|
54
|
+
// 2026-06-15: 编译 message-renderer.ts (对话显示 UI 模块)
|
|
55
|
+
// esbuild 编译 .ts → dist/web/ui/message-renderer.js (ESM, 浏览器侧 <script type="module"> 加载)
|
|
56
|
+
console.log('[build-web] 编译 message-renderer.ts...');
|
|
57
|
+
await fs.mkdir(path.join(DIST_WEB, 'ui'), { recursive: true });
|
|
58
|
+
await esbuild.build({
|
|
59
|
+
entryPoints: [path.join(ROOT, 'src/web/ui/message-renderer.ts')],
|
|
60
|
+
outfile: path.join(DIST_WEB, 'ui/message-renderer.js'),
|
|
61
|
+
format: 'esm',
|
|
62
|
+
target: 'es2022',
|
|
63
|
+
platform: 'browser',
|
|
64
|
+
minify: false,
|
|
65
|
+
});
|
|
66
|
+
|
|
67
|
+
// 2026-06-15: 编译 step-timeline.ts (气泡内 4 状态步骤条)
|
|
68
|
+
console.log('[build-web] 编译 step-timeline.ts...');
|
|
69
|
+
await esbuild.build({
|
|
70
|
+
entryPoints: [path.join(ROOT, 'src/web/ui/step-timeline.ts')],
|
|
71
|
+
outfile: path.join(DIST_WEB, 'ui/step-timeline.js'),
|
|
72
|
+
format: 'esm',
|
|
73
|
+
target: 'es2022',
|
|
74
|
+
platform: 'browser',
|
|
75
|
+
minify: false,
|
|
76
|
+
});
|
|
77
|
+
|
|
78
|
+
// 编译主客户端入口 (classic script, 非 module; 由 index.html 以 /client.js 加载)
|
|
79
|
+
console.log('[build-web] 编译 client.ts...');
|
|
80
|
+
await esbuild.build({
|
|
81
|
+
entryPoints: [path.join(ROOT, 'src/web/client.ts')],
|
|
82
|
+
outfile: path.join(DIST_WEB, 'client.js'),
|
|
83
|
+
format: 'iife',
|
|
84
|
+
target: 'es2022',
|
|
85
|
+
platform: 'browser',
|
|
86
|
+
minify: false,
|
|
87
|
+
});
|
|
88
|
+
|
|
89
|
+
// 复制静态文件
|
|
90
|
+
console.log('[build-web] 复制静态文件...');
|
|
91
|
+
await fs.copyFile(path.join(ROOT, 'src/web/index.html'), path.join(DIST_WEB, 'index.html'));
|
|
92
|
+
await fs.copyFile(path.join(ROOT, 'src/web/api-config.html'), path.join(DIST_WEB, 'api-config.html'));
|
|
93
|
+
await fs.copyFile(path.join(ROOT, 'src/web/style.css'), path.join(DIST_WEB, 'style.css'));
|
|
94
|
+
// 复制 PWA manifest (index.html 里有 <link rel="manifest">, 否则浏览器会 404)
|
|
95
|
+
await fs.copyFile(path.join(ROOT, 'src/web/manifest.json'), path.join(DIST_WEB, 'manifest.json'));
|
|
96
|
+
// 复制 icons 目录 (manifest.json 里引用了 favicon 等)
|
|
97
|
+
await fs.cp(path.join(ROOT, 'src/web/icons'), path.join(DIST_WEB, 'icons'), { recursive: true });
|
|
98
|
+
|
|
99
|
+
// 复制 system-prompt layer .md 文件到 dist/llm/system-prompt/layers/
|
|
100
|
+
// 原因: tsc 不复制 .md, src/llm/system-prompt/layers/{core,role,channel,tool}/*.md
|
|
101
|
+
// 在 dist 里完全不存在, registry.ts 用 __dirname + 'layers' 读它们时 100% ENOENT.
|
|
102
|
+
// 这里全量复制 25 个 .md, 不过滤 'never' 层 — registry.matchesContext 已做过滤.
|
|
103
|
+
const srcLayers = path.join(ROOT, 'src/llm/system-prompt/layers');
|
|
104
|
+
const dstLayers = path.join(ROOT, 'dist/llm/system-prompt/layers');
|
|
105
|
+
await fs.cp(srcLayers, dstLayers, { recursive: true });
|
|
106
|
+
console.log('[build-web] system-prompt layers copied →', path.relative(ROOT, dstLayers));
|
|
107
|
+
|
|
108
|
+
// 复制 components/ 下的独立 ESM 模块 (非 p2p 那些, 已被 esbuild 单独编译)
|
|
109
|
+
// 例如 wallet-viem.mjs: 浏览器侧 viem 封装, 被 index.html 引用
|
|
110
|
+
const extraComponentsDir = path.join(ROOT, 'src/web/components');
|
|
111
|
+
for (const entry of await fs.readdir(extraComponentsDir)) {
|
|
112
|
+
if (entry === 'p2p') continue;
|
|
113
|
+
const src = path.join(extraComponentsDir, entry);
|
|
114
|
+
const dest = path.join(DIST_WEB, 'components', entry);
|
|
115
|
+
const stat = await fs.stat(src);
|
|
116
|
+
if (stat.isFile()) {
|
|
117
|
+
await fs.mkdir(path.dirname(dest), { recursive: true });
|
|
118
|
+
await fs.copyFile(src, dest);
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
console.log('[build-web] 完成!');
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
main().catch(console.error);
|
|
@@ -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();
|