@vincentbanana/banana-gui 0.1.1 → 0.3.0

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 CHANGED
@@ -6,9 +6,18 @@ Banana 的图形界面:本地起一个服务,用浏览器打开。黑白主
6
6
 
7
7
  ```bash
8
8
  npm install -g @vincentbanana/banana-gui
9
- banana-gui
10
9
  ```
11
10
 
11
+ 装完有两种启动方式,**同一份安装,不用额外装东西**:
12
+
13
+ ```bash
14
+ banana-gui # 浏览器版:启动后自动开浏览器,最轻
15
+ banana-gui --desktop # 桌面版:直接开独立窗口
16
+ banana-gui-desktop # 同上,等价的独立命令
17
+ ```
18
+
19
+ electron 是可选依赖(`optionalDependencies`),npm 安装时会自动带上;万一某个平台装不上,`banana-gui` 仍然照常工作,只是 `--desktop` 会提示你用浏览器版。
20
+
12
21
  首次启动会进配置向导(选 provider、贴 API key、选权限、测连接),配置与 CLI 共用 `~/.banana/config.json`。
13
22
 
14
23
  ## 用法
@@ -22,13 +31,48 @@ banana-gui --provider deepseek --model deepseek-chat
22
31
 
23
32
  ## 特性
24
33
 
25
- - 左侧会话列表:新建 / 切换 / 删除,切换即续上下文
26
- - 流式回复、工具调用实时状态(自绘图标:文件、终端、搜索、编辑…)
27
- - 危险操作弹出确认框,附变更预览(写文件前给 diff)
34
+ - **和 CLI 同一套内核**:agent 循环、工具、权限、斜杠命令全部来自 `@vincentbanana/bananacli`,两边行为完全一致
35
+ - 左侧会话列表:新建 / 切换 / 重命名 / 导出(Markdown、JSON)/ 删除
36
+ - 输入 `/` 调出命令面板(`/help` 看全部),输入 `@` 补全项目文件
37
+ - 流式回复、工具调用实时状态,点工具行可展开完整输出
38
+ - 危险操作弹窗确认,写文件前给 diff
28
39
  - 一键撤销上一轮文件改动
40
+ - 右侧项目文件面板:浏览目录、预览文件、一键插入引用
29
41
  - 粘贴或拖拽图片,随消息一起发送
30
- - 模型、权限、工作区、系统提示词在设置面板里改,改完立刻生效
31
- - 深浅色跟随系统,纯黑白,无渐变无彩色
42
+ - 模型、权限、API Key、工作区、系统提示词在设置里改,改完立刻生效
43
+ - 权限设置写清了每种模式的含义和风险,新手也不会懵
44
+ - 三态主题:跟随系统 / 浅色 / 深色,纯黑白无渐变
45
+ - 没配 API Key 也能打开界面,照着引导填完即可用
46
+
47
+ ## 两种方式对比
48
+
49
+ | | 启动命令 | 需要什么 | 说明 |
50
+ | --- | --- | --- | --- |
51
+ | 浏览器版 | `banana-gui` | 任何装了 Node 20+ 的系统 | 启动后自动打开浏览器,几十 MB |
52
+ | 桌面版 | `banana-gui --desktop` | 同一个 npm 包 | 独立窗口,约 200MB(内含 electron) |
53
+
54
+ 两者用的是同一套 agent 内核和同一份界面资源,功能完全一致,只是外壳不同。
55
+
56
+ ## 打包成安装包(可选)
57
+
58
+ 想出 `.exe` / `.dmg` / `AppImage` 的话,在源码仓库里构建:
59
+
60
+ ```bash
61
+ git clone <这个仓库>
62
+ cd banana-gui
63
+ npm install # 装 electron-builder
64
+ npm run dist:win # 也支持 dist:mac / dist:linux
65
+ ```
66
+
67
+ 产物在 `gui/release/`。
68
+
69
+ ## 配置
70
+
71
+ 首次启动会提示配置向导。GUI 与 CLI 共用 `~/.banana/config.json`:
72
+
73
+ - 也可以直接在界面的设置面板里填 API Key、切换模型和权限
74
+ - 环境变量优先级更高,例如 `DEEPSEEK_API_KEY`
75
+ - API Key 只写在你自己的机器上,不会进代码仓库
32
76
 
33
77
  ## 安全
34
78
 
@@ -0,0 +1,71 @@
1
+ #!/usr/bin/env node
2
+ // 桌面版启动器:npm 安装后直接开一个独立窗口,不用浏览器。
3
+ //
4
+ // npm install -g @vincentbanana/banana-gui
5
+ // banana-gui-desktop 开窗口
6
+ // banana-gui-desktop --port 8080
7
+ //
8
+ // electron 是 optionalDependencies:装得上就能用,装不上会提示改用浏览器版
9
+ // (banana-gui)。本文件不直接依赖 electron 的 API,只负责把它拉起来。
10
+ import { spawn } from 'node:child_process';
11
+ import { createRequire } from 'node:module';
12
+ import { dirname, join } from 'node:path';
13
+ import { fileURLToPath } from 'node:url';
14
+
15
+ const here = dirname(fileURLToPath(import.meta.url));
16
+ const require_ = createRequire(import.meta.url);
17
+ const args = process.argv.slice(2);
18
+
19
+ if (args.includes('--help') || args.includes('-h')) {
20
+ console.log('banana-gui-desktop — Banana 的桌面版(独立窗口)');
21
+ console.log('');
22
+ console.log('用法: banana-gui-desktop [选项]');
23
+ console.log(' --port <端口> 指定端口(默认随机)');
24
+ console.log(' --cwd <目录> 指定工作区');
25
+ console.log(' --provider <名> 指定 provider');
26
+ console.log(' --model <名> 指定模型');
27
+ console.log(' --help 显示帮助');
28
+ console.log('');
29
+ console.log('提示:日常使用建议用 banana-gui(浏览器版,更轻)。');
30
+ process.exit(0);
31
+ }
32
+
33
+ /** electron 装在依赖里;require('electron') 返回的是可执行文件路径。 */
34
+ function electronBinary() {
35
+ for (const specifier of ['electron', '@vincentbanana/banana-gui/node_modules/electron']) {
36
+ try {
37
+ const resolved = require_.resolve(specifier);
38
+ // electron 主入口导出的是 exe 路径;解析失败时退回到包目录里的 dist
39
+ const binary = require_(resolved);
40
+ if (typeof binary === 'string') return binary;
41
+ return join(dirname(resolved), 'dist', process.platform === 'win32' ? 'electron.exe' : 'electron');
42
+ } catch {
43
+ /* 换下一个候选 */
44
+ }
45
+ }
46
+ return null;
47
+ }
48
+
49
+ const binary = electronBinary();
50
+ if (!binary) {
51
+ console.error('没找到 electron,桌面版暂时用不了。');
52
+ console.error('');
53
+ console.error('可以先继续用浏览器版,功能完全一样:');
54
+ console.error(' banana-gui');
55
+ console.error('');
56
+ console.error('想用桌面版,试着补装一次:');
57
+ console.error(' npm install -g electron');
58
+ process.exit(1);
59
+ }
60
+
61
+ const main = join(here, '..', 'desktop', 'main.cjs');
62
+ const child = spawn(binary, [main, ...args], { stdio: 'inherit' });
63
+ child.on('error', (error) => {
64
+ console.error('桌面版启动失败:' + error.message);
65
+ console.error('可以先用浏览器版:banana-gui');
66
+ process.exit(1);
67
+ });
68
+ child.on('exit', (code) => process.exit(code ?? 0));
69
+ for (const signal of ['SIGINT', 'SIGTERM']) {
70
+ process.on(signal, () => child.kill(signal));
71
+ }
@@ -4,8 +4,11 @@
4
4
  // 启动:banana-gui(首次会进配置向导)
5
5
  import { createRequire } from 'node:module';
6
6
  import { spawn } from 'node:child_process';
7
+ import { dirname, join } from 'node:path';
8
+ import { fileURLToPath } from 'node:url';
7
9
 
8
10
  const require = createRequire(import.meta.url);
11
+ const pkg = require('../package.json');
9
12
 
10
13
  function resolveCli() {
11
14
  // 依赖可能被 npm 提升到上层 node_modules,用 require 解析更稳;
@@ -17,23 +20,43 @@ function resolveCli() {
17
20
  }
18
21
  }
19
22
 
23
+ /** electron 是可选依赖:装上了就能开独立窗口,没装就走浏览器。 */
24
+ function electronBinary() {
25
+ for (const specifier of ['electron', '@vincentbanana/banana-gui/node_modules/electron']) {
26
+ try {
27
+ const resolved = require.resolve(specifier);
28
+ const binary = require(resolved);
29
+ if (typeof binary === 'string') return binary;
30
+ } catch {
31
+ /* 换下一个候选 */
32
+ }
33
+ }
34
+ return null;
35
+ }
36
+
20
37
  const args = process.argv.slice(2);
21
38
  const passthrough = ['--port', '--cwd', '--provider', '--model', '--base-url', '--api-key', '--setup'];
22
39
 
23
- if (args.some((arg) => arg === '--help' || arg === '-h' || args.length === 0 || arg === '--version' || arg === '-v')) {
24
- if (args.includes('--version') || args.includes('-v')) {
25
- console.log('0.1.1');
26
- process.exit(0);
27
- }
40
+ if (args.includes('--version') || args.includes('-v')) {
41
+ console.log(pkg.version);
42
+ process.exit(0);
43
+ }
44
+
45
+ if (args.includes('--help') || args.includes('-h')) {
28
46
  console.log('banana-gui — Banana 的图形界面');
29
47
  console.log('');
30
48
  console.log('用法: banana-gui [选项]');
49
+ console.log(' --desktop 用独立窗口打开(需要已安装 electron)');
31
50
  console.log(' --port <端口> 指定端口(默认随机)');
32
51
  console.log(' --cwd <目录> 指定工作区');
33
52
  console.log(' --provider <名> 指定 provider');
34
53
  console.log(' --model <名> 指定模型');
35
54
  console.log(' --open 启动后打开浏览器(默认已开启)');
55
+ console.log(' --version 显示版本');
36
56
  console.log(' --help 显示帮助');
57
+ console.log('');
58
+ console.log('提示:默认用浏览器打开。想要独立窗口就加 --desktop,');
59
+ console.log(' 或直接运行 banana-gui-desktop。');
37
60
  process.exit(0);
38
61
  }
39
62
 
@@ -43,7 +66,42 @@ if (!entry) {
43
66
  process.exit(1);
44
67
  }
45
68
 
46
- const cliArgs = ['--ui'];
69
+ // --desktop:交给 electron 开独立窗口,本进程不参与
70
+ if (args.includes('--desktop')) {
71
+ const binary = electronBinary();
72
+ if (!binary) {
73
+ console.error('没装 electron,用不了独立窗口。补装一次即可:npm install -g electron');
74
+ console.error('(不装也没关系,直接运行 banana-gui 用浏览器版,功能完全一样)');
75
+ process.exit(1);
76
+ }
77
+ const rest = args.filter((arg) => arg !== '--desktop');
78
+ const desktop = spawn(binary, [join(dirname(fileURLToPath(import.meta.url)), '..', 'desktop', 'main.cjs'), ...rest], {
79
+ stdio: 'inherit',
80
+ });
81
+ desktop.on('exit', (code) => process.exit(code ?? 0));
82
+ for (const signal of ['SIGINT', 'SIGTERM']) process.on(signal, () => desktop.kill(signal));
83
+ } else {
84
+ startBrowser(entry);
85
+ }
86
+
87
+ function startBrowser(entry) {
88
+ const cliArgs = ['--ui'];
89
+ let forwarding = false;
90
+ for (const arg of args) {
91
+ if (arg === '--open') {
92
+ forwarding = false;
93
+ continue;
94
+ }
95
+ forwarding = passthrough.includes(arg) || forwarding;
96
+ cliArgs.push(arg);
97
+ }
98
+
99
+ const child = spawn(process.execPath, [entry, ...cliArgs], { stdio: 'inherit' });
100
+ child.on('exit', (code) => process.exit(code ?? 0));
101
+ for (const signal of ['SIGINT', 'SIGTERM']) {
102
+ process.on(signal, () => child.kill(signal));
103
+ }
104
+ }
47
105
  let forwarding = false;
48
106
  for (const arg of args) {
49
107
  if (arg === '--open') {
@@ -0,0 +1,136 @@
1
+ // Banana 桌面版主进程:拉起本地 GUI 服务,再用窗口加载它。
2
+ // 界面资源仍来自本包 ui/,agent 内核来自 @vincentbanana/bananacli。
3
+ const { app, BrowserWindow, Menu, shell, dialog } = require('electron');
4
+ const { spawn } = require('node:child_process');
5
+ const { createRequire } = require('node:module');
6
+
7
+ const require_ = createRequire(__filename);
8
+ let child = null;
9
+ let win = null;
10
+
11
+ // electron 自带的 node 运行 CLI 内核;ELECTRON_RUN_AS_NODE 让 electron.exe 当纯 node 用,
12
+ // 否则会再开一个 Electron 窗口,而不是执行脚本。
13
+ const PASSTHROUGH = ['--port', '--cwd', '--provider', '--model', '--base-url', '--api-key'];
14
+
15
+ function serverArgs() {
16
+ const argv = process.argv.slice(2).filter((arg) => arg.startsWith('--'));
17
+ const extra = [];
18
+ for (const arg of argv) {
19
+ if (PASSTHROUGH.includes(arg)) {
20
+ extra.push(arg);
21
+ const value = process.argv[process.argv.indexOf(arg) + 1];
22
+ if (value && !value.startsWith('--')) extra.push(value);
23
+ }
24
+ }
25
+ // 端口交给 CLI 随机分配,避免多开时互相抢占
26
+ return ['--ui', '--no-open', ...extra];
27
+ }
28
+
29
+ function startServer() {
30
+ let entry;
31
+ try {
32
+ // 依赖可能被提升到上层 node_modules;只能解析包根(子路径受 exports 限制)
33
+ entry = require_.resolve('@vincentbanana/bananacli');
34
+ } catch (error) {
35
+ throw new Error('找不到依赖 @vincentbanana/bananacli,请重新安装:npm install -g @vincentbanana/banana-gui');
36
+ }
37
+ child = spawn(process.execPath, [entry, ...serverArgs()], {
38
+ stdio: ['ignore', 'pipe', 'pipe'],
39
+ env: { ...process.env, ELECTRON_RUN_AS_NODE: '1' },
40
+ });
41
+ return new Promise((resolve, reject) => {
42
+ let buffer = '';
43
+ const onData = (chunk) => {
44
+ buffer += chunk.toString('utf8');
45
+ const match = buffer.match(/http:\/\/127\.0\.0\.1:\d+\/t\/[a-f0-9]+\//);
46
+ if (match) {
47
+ child.stdout.off('data', onData);
48
+ resolve(match[0]);
49
+ }
50
+ };
51
+ child.stdout.on('data', onData);
52
+ child.stderr.on('data', (chunk) => process.stderr.write(`[banana] ${chunk}`));
53
+ child.on('error', reject);
54
+ child.on('exit', (code) => {
55
+ if (!win) return;
56
+ dialog.showErrorBox('Banana 已退出', `本地服务意外结束(退出码 ${code})。`);
57
+ app.quit();
58
+ });
59
+ setTimeout(() => reject(new Error('启动超时:本地服务没有在 30 秒内就绪')), 30_000);
60
+ });
61
+ }
62
+
63
+ async function createWindow() {
64
+ const url = await startServer();
65
+ win = new BrowserWindow({
66
+ width: 1180,
67
+ height: 820,
68
+ minWidth: 720,
69
+ minHeight: 520,
70
+ backgroundColor: '#ffffff',
71
+ title: 'Banana',
72
+ autoHideMenuBar: true,
73
+ webPreferences: { contextIsolation: true, nodeIntegration: false, sandbox: true },
74
+ });
75
+ await win.loadURL(url);
76
+ win.on('closed', () => {
77
+ win = null;
78
+ });
79
+ // 外部链接交给系统浏览器,窗口只负责本地界面
80
+ win.webContents.setWindowOpenHandler(({ url: target }) => {
81
+ shell.openExternal(target);
82
+ return { action: 'deny' };
83
+ });
84
+ }
85
+
86
+ const menuTemplate = [
87
+ ...(process.platform === 'darwin' ? [{ role: 'appMenu' }] : []),
88
+ {
89
+ label: '文件',
90
+ submenu: [
91
+ {
92
+ label: '新建对话',
93
+ accelerator: 'CmdOrCtrl+N',
94
+ click: () => win?.webContents.executeJavaScript("document.getElementById('newChat')?.click()"),
95
+ },
96
+ { type: 'separator' },
97
+ process.platform === 'darwin' ? { role: 'close' } : { role: 'quit', label: '退出' },
98
+ ],
99
+ },
100
+ { role: 'editMenu', label: '编辑' },
101
+ {
102
+ label: '视图',
103
+ submenu: [
104
+ {
105
+ label: '项目文件',
106
+ accelerator: 'CmdOrCtrl+Shift+F',
107
+ click: () => win?.webContents.executeJavaScript("document.getElementById('filesBtn')?.click()"),
108
+ },
109
+ { type: 'separator' },
110
+ { role: 'reload', label: '重新加载' },
111
+ { role: 'toggleDevTools', label: '开发者工具' },
112
+ { role: 'togglefullscreen', label: '全屏' },
113
+ ],
114
+ },
115
+ { role: 'windowMenu', label: '窗口' },
116
+ ];
117
+
118
+ app.whenReady().then(() => {
119
+ Menu.setApplicationMenu(Menu.buildFromTemplate(menuTemplate));
120
+ createWindow().catch((error) => {
121
+ dialog.showErrorBox('Banana 启动失败', String(error?.message ?? error));
122
+ app.quit();
123
+ });
124
+ app.on('activate', () => {
125
+ if (BrowserWindow.getAllWindows().length === 0) createWindow();
126
+ });
127
+ });
128
+
129
+ app.on('window-all-closed', () => {
130
+ if (process.platform !== 'darwin') app.quit();
131
+ });
132
+
133
+ app.on('before-quit', () => {
134
+ child?.kill();
135
+ child = null;
136
+ });
package/package.json CHANGED
@@ -1,34 +1,78 @@
1
1
  {
2
2
  "name": "@vincentbanana/banana-gui",
3
- "version": "0.1.1",
3
+ "version": "0.3.0",
4
4
  "description": "Banana 的图形界面:本地服务 + 浏览器。黑白主题,自绘图标,无第三方 UI 依赖",
5
5
  "type": "module",
6
6
  "engines": {
7
7
  "node": ">=20.0.0"
8
8
  },
9
9
  "bin": {
10
- "banana-gui": "bin/banana-gui.mjs"
10
+ "banana-gui": "bin/banana-gui.mjs",
11
+ "banana-gui-desktop": "bin/banana-gui-desktop.mjs"
11
12
  },
12
13
  "files": [
13
14
  "bin",
14
15
  "ui",
16
+ "desktop",
15
17
  "README.md"
16
18
  ],
17
- "scripts": {
18
- "sync": "node ../scripts/sync-gui.mjs",
19
- "test": "node test/gui-package.test.mjs"
20
- },
21
- "dependencies": {
22
- "@vincentbanana/bananacli": "^0.2.0"
23
- },
24
19
  "keywords": [
25
20
  "ai",
26
21
  "agent",
27
22
  "gui",
28
23
  "web-ui",
29
24
  "llm",
30
- "terminal"
25
+ "terminal",
26
+ "desktop",
27
+ "electron"
31
28
  ],
29
+ "scripts": {
30
+ "sync": "node ../scripts/sync-gui.mjs",
31
+ "test": "node test/gui-package.test.mjs",
32
+ "desktop": "electron desktop/main.cjs",
33
+ "pack": "electron-builder --dir",
34
+ "dist": "electron-builder",
35
+ "dist:win": "electron-builder --win",
36
+ "dist:mac": "electron-builder --mac",
37
+ "dist:linux": "electron-builder --linux"
38
+ },
39
+ "build": {
40
+ "appId": "dev.banana.gui",
41
+ "productName": "Banana",
42
+ "files": [
43
+ "desktop/**/*",
44
+ "ui/**/*",
45
+ "bin/**/*",
46
+ "package.json"
47
+ ],
48
+ "npmRebuild": false,
49
+ "mac": {
50
+ "category": "public.app-category.developer-tools",
51
+ "target": [
52
+ "dmg"
53
+ ]
54
+ },
55
+ "win": {
56
+ "target": [
57
+ "nsis"
58
+ ]
59
+ },
60
+ "linux": {
61
+ "target": [
62
+ "AppImage"
63
+ ],
64
+ "category": "Development"
65
+ }
66
+ },
67
+ "optionalDependencies": {
68
+ "electron": "^33.0.0"
69
+ },
70
+ "devDependencies": {
71
+ "electron-builder": "^25.0.0"
72
+ },
73
+ "dependencies": {
74
+ "@vincentbanana/bananacli": "^0.3.0"
75
+ },
32
76
  "license": "MIT",
33
77
  "publishConfig": {
34
78
  "access": "public"