dsh-oc-desktop 0.3.1
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/assets/icon-app.png +0 -0
- package/assets/icon-dark.png +0 -0
- package/assets/icon-light.png +0 -0
- package/assets/icon.ico +0 -0
- package/assets/icon.png +0 -0
- package/assets/splash-whale.png +0 -0
- package/assets/tray.ico +0 -0
- package/assets/tray.png +0 -0
- package/assets/whale.svg +8 -0
- package/bin/dsh-desktop.cjs +52 -0
- package/launcher/apply-update.js +69 -0
- package/launcher/events.js +118 -0
- package/launcher/main.js +800 -0
- package/launcher/preload.js +148 -0
- package/launcher/session-window.js +22 -0
- package/launcher/splash.html +27 -0
- package/launcher/updater.js +188 -0
- package/package.json +19 -0
- package/scripts/test.mjs +15 -0
|
Binary file
|
|
Binary file
|
|
Binary file
|
package/assets/icon.ico
ADDED
|
Binary file
|
package/assets/icon.png
ADDED
|
Binary file
|
|
Binary file
|
package/assets/tray.ico
ADDED
|
Binary file
|
package/assets/tray.png
ADDED
|
Binary file
|
package/assets/whale.svg
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="50.000000" height="50.000000" viewBox="0 0 50 50" fill="none">
|
|
2
|
+
<style>
|
|
3
|
+
@media (prefers-color-scheme: dark) {
|
|
4
|
+
path { fill: #fff; }
|
|
5
|
+
}
|
|
6
|
+
</style>
|
|
7
|
+
<path id="path" d="M48.8354 10.0479C48.3232 9.79199 48.1025 10.2798 47.8032 10.5278C47.7007 10.6079 47.6143 10.7119 47.5273 10.8076C46.7793 11.624 45.9048 12.1597 44.7622 12.0957C43.0923 12 41.666 12.5356 40.4058 13.8398C40.1377 12.2319 39.2476 11.272 37.8926 10.6558C37.1836 10.3359 36.4668 10.0156 35.9702 9.31982C35.6235 8.82373 35.5293 8.27197 35.356 7.72754C35.2456 7.3999 35.1353 7.06396 34.7651 7.00781C34.3633 6.94385 34.2056 7.2876 34.0479 7.57568C33.418 8.75195 33.1733 10.0479 33.1973 11.3599C33.2524 14.312 34.4736 16.6641 36.8999 18.3359C37.1758 18.5278 37.2466 18.7197 37.1597 19C36.9946 19.5757 36.7974 20.1357 36.624 20.7119C36.5137 21.0801 36.3486 21.1597 35.9624 21C34.6309 20.4321 33.481 19.5918 32.4644 18.5757C30.7393 16.8721 29.1792 14.9917 27.2334 13.52C26.7764 13.1758 26.3193 12.856 25.8467 12.5518C23.8618 10.584 26.1069 8.96777 26.627 8.77588C27.1704 8.57568 26.8159 7.8877 25.0591 7.896C23.3022 7.90381 21.6953 8.50391 19.647 9.30371C19.3477 9.42383 19.0322 9.51172 18.7095 9.58398C16.8501 9.22363 14.9199 9.14355 12.9033 9.37598C9.10596 9.80762 6.07275 11.6396 3.84326 14.7681C1.16455 18.5278 0.53418 22.7998 1.30664 27.2559C2.11768 31.9521 4.46582 35.8398 8.07373 38.8799C11.8159 42.0322 16.1255 43.5762 21.041 43.2803C24.0269 43.104 27.3516 42.6963 31.1016 39.4561C32.0469 39.936 33.0396 40.1279 34.686 40.272C35.9546 40.3921 37.1758 40.208 38.1211 40.0078C39.6021 39.688 39.4995 38.2881 38.9639 38.0322C34.623 35.9678 35.5762 36.8081 34.71 36.1279C36.9155 33.4639 40.2402 30.6958 41.54 21.728C41.6426 21.0161 41.5557 20.5679 41.54 19.9917C41.5322 19.6396 41.6108 19.5039 42.0049 19.4639C43.0923 19.3359 44.1479 19.0317 45.1167 18.4878C47.9292 16.9199 49.064 14.3438 49.3315 11.2559C49.3711 10.7837 49.3237 10.2959 48.8354 10.0479ZM24.3262 37.8398C20.1196 34.4639 18.0791 33.3521 17.2358 33.3999C16.4482 33.4482 16.5898 34.3682 16.7632 34.9678C16.9443 35.5601 17.1812 35.9683 17.5117 36.4878C17.7402 36.832 17.8979 37.3442 17.2832 37.728C15.9282 38.584 13.5728 37.4399 13.4624 37.3838C10.7207 35.7358 8.42822 33.5601 6.81348 30.584C5.25342 27.7197 4.34766 24.6479 4.19775 21.3677C4.1582 20.5757 4.38672 20.2959 5.15869 20.1519C6.17529 19.96 7.22314 19.9199 8.23926 20.0718C12.5327 20.7119 16.1885 22.6719 19.2529 25.7759C21.002 27.5439 22.3252 29.6558 23.6885 31.7202C25.1377 33.9121 26.6978 36 28.6831 37.7119C29.3843 38.312 29.9434 38.7681 30.479 39.104C28.8643 39.2881 26.1699 39.3281 24.3262 37.8398ZM26.3433 24.6001C26.3433 24.248 26.6191 23.9678 26.9658 23.9678C27.0444 23.9678 27.1152 23.9839 27.1782 24.0078C27.2651 24.04 27.3438 24.0879 27.4067 24.1602C27.5171 24.272 27.5801 24.4321 27.5801 24.6001C27.5801 24.9521 27.3042 25.2319 26.9575 25.2319C26.6108 25.2319 26.3433 24.9521 26.3433 24.6001ZM32.6064 27.8799C32.2046 28.0479 31.8027 28.1919 31.4165 28.208C30.8179 28.2397 30.1641 27.9922 29.8096 27.688C29.2583 27.2158 28.8643 26.9521 28.6987 26.1279C28.6279 25.7759 28.6675 25.2319 28.7305 24.9199C28.8721 24.248 28.7144 23.8159 28.2495 23.4238C27.8716 23.104 27.3911 23.0161 26.8633 23.0161C26.666 23.0161 26.4849 22.9277 26.3511 22.856C26.1304 22.7441 25.9492 22.4639 26.1226 22.1201C26.1777 22.0078 26.4458 21.7358 26.5088 21.688C27.2256 21.272 28.0527 21.4077 28.8169 21.7197C29.5259 22.0161 30.0615 22.5601 30.834 23.3281C31.6216 24.2559 31.7632 24.5117 32.2124 25.208C32.5669 25.752 32.8901 26.312 33.1104 26.9521C33.2446 27.3521 33.0713 27.6802 32.6064 27.8799Z" fill="#000" fill-opacity="1.000000" fill-rule="nonzero"/>
|
|
8
|
+
</svg>
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
'use strict';
|
|
3
|
+
// dsh-desktop: boot the desktop shell (Electron launcher).
|
|
4
|
+
const { spawn } = require('node:child_process');
|
|
5
|
+
const fs = require('node:fs');
|
|
6
|
+
const path = require('node:path');
|
|
7
|
+
|
|
8
|
+
function resolveElectron() {
|
|
9
|
+
try {
|
|
10
|
+
const p = require('electron');
|
|
11
|
+
if (typeof p === 'string' && p.length > 0) return p;
|
|
12
|
+
} catch { /* fall through to explicit package lookup */ }
|
|
13
|
+
for (const dir of [
|
|
14
|
+
path.join(__dirname, '..', 'node_modules', 'electron'),
|
|
15
|
+
path.join(process.cwd(), 'node_modules', 'electron')
|
|
16
|
+
]) {
|
|
17
|
+
try {
|
|
18
|
+
const pkg = JSON.parse(fs.readFileSync(path.join(dir, 'package.json'), 'utf8'));
|
|
19
|
+
if (pkg && typeof pkg.version === 'string') {
|
|
20
|
+
const p = require(dir);
|
|
21
|
+
if (typeof p === 'string' && p.length > 0) return p;
|
|
22
|
+
}
|
|
23
|
+
} catch { /* keep scanning */ }
|
|
24
|
+
}
|
|
25
|
+
return null;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
function main() {
|
|
29
|
+
const args = process.argv.slice(2);
|
|
30
|
+
const electron = resolveElectron();
|
|
31
|
+
if (electron === null) {
|
|
32
|
+
console.error('dsh-oc-desktop: electron not found. Run "npm install" in the desktop package or the profile.');
|
|
33
|
+
process.exit(1);
|
|
34
|
+
}
|
|
35
|
+
const mainPath = path.join(__dirname, '..', 'launcher', 'main.js');
|
|
36
|
+
if (!fs.existsSync(mainPath)) {
|
|
37
|
+
console.error('dsh-oc-desktop: launcher/main.js missing, reinstall the package.');
|
|
38
|
+
process.exit(1);
|
|
39
|
+
}
|
|
40
|
+
const child = spawn(electron, [mainPath, ...args], { stdio: 'inherit', windowsHide: true });
|
|
41
|
+
child.on('error', (err) => { console.error(`dsh-oc-desktop: failed to start electron: ${err.message}`); process.exit(1); });
|
|
42
|
+
child.on('exit', (code, signal) => {
|
|
43
|
+
if (signal) {
|
|
44
|
+
try { process.kill(process.pid, signal); } catch { /* unsupported signal — fall through */ }
|
|
45
|
+
process.exit(1);
|
|
46
|
+
}
|
|
47
|
+
process.exit(code ?? 0);
|
|
48
|
+
});
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
if (require.main === module) main();
|
|
52
|
+
module.exports = { resolveElectron };
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
// apply-update.js - 应用更新(替换文件树)
|
|
3
|
+
//
|
|
4
|
+
// 由主进程以 `--dsh-apply-update` 重启自身后执行:此刻旧进程已退出,
|
|
5
|
+
// 除 exe 本体(当前进程占用)外的文件均可直接替换。
|
|
6
|
+
//
|
|
7
|
+
// 替换策略:
|
|
8
|
+
// 1. staging 内除 exe 外的文件 → 逐个 rename 到正式位置(同卷,原子)
|
|
9
|
+
// 2. exe 本体 → 写入 .dsh-apply.cmd:
|
|
10
|
+
// timeout 2s(等当前进程完全退出)→ move 新 exe 覆盖旧 exe
|
|
11
|
+
// → rmdir staging → start 新 exe → 删除自身
|
|
12
|
+
// cmd 进程不锁定 exe,可以完成最后一步(绿色版无安装器场景的标准做法)
|
|
13
|
+
//
|
|
14
|
+
// 零依赖:仅 node 内置模块。
|
|
15
|
+
|
|
16
|
+
const fs = require('node:fs');
|
|
17
|
+
const path = require('node:path');
|
|
18
|
+
|
|
19
|
+
// appRoot 应用根目录(win-unpacked)
|
|
20
|
+
// staging 待应用更新目录(位于 appRoot 内,如 .dsh-update-staging)
|
|
21
|
+
// returns { ok, error?, exeChanged, cmdPath }
|
|
22
|
+
// ok=false 时不做任何修改(调用方负责清理/提示)
|
|
23
|
+
function applyUpdate(appRoot, staging) {
|
|
24
|
+
if (!fs.existsSync(staging)) return { ok: false, error: 'staging 不存在: ' + staging };
|
|
25
|
+
const top = fs.readdirSync(staging, { withFileTypes: true });
|
|
26
|
+
const exeEntry = top.find((e) => e.isFile() && /\.exe$/i.test(e.name));
|
|
27
|
+
const exeName = exeEntry ? exeEntry.name : null;
|
|
28
|
+
const exeChanged = !!exeName;
|
|
29
|
+
|
|
30
|
+
// 1. 替换除 exe 外的全部文件
|
|
31
|
+
const walk = (dir, rel) => {
|
|
32
|
+
for (const e of fs.readdirSync(dir, { withFileTypes: true })) {
|
|
33
|
+
const src = path.join(dir, e.name);
|
|
34
|
+
const r = rel ? rel + '/' + e.name : e.name;
|
|
35
|
+
if (e.isFile() && exeName && r === exeName) continue; // exe 留给 cmd 处理
|
|
36
|
+
if (e.isDirectory()) walk(src, r);
|
|
37
|
+
else if (e.isFile()) {
|
|
38
|
+
const dst = path.join(appRoot, r);
|
|
39
|
+
fs.mkdirSync(path.dirname(dst), { recursive: true });
|
|
40
|
+
try { fs.renameSync(src, dst); }
|
|
41
|
+
catch { /* 目标可能被占用,尝试覆盖语义 */ fs.copyFileSync(src, dst); fs.rmSync(src, { force: true }); }
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
};
|
|
45
|
+
walk(staging, '');
|
|
46
|
+
|
|
47
|
+
// 1b. 清理 staging 中已被搬空的目录(自底向上;顶层因 exe 文件未移走而保留)
|
|
48
|
+
const prune = (dir) => {
|
|
49
|
+
for (const e of fs.readdirSync(dir, { withFileTypes: true })) {
|
|
50
|
+
if (e.isDirectory()) prune(path.join(dir, e.name));
|
|
51
|
+
}
|
|
52
|
+
try { if (fs.readdirSync(dir).length === 0) fs.rmdirSync(dir); } catch { /* ignore */ }
|
|
53
|
+
};
|
|
54
|
+
prune(staging);
|
|
55
|
+
|
|
56
|
+
// 2. 生成 apply.cmd(exe 延迟替换 + 清理 + 重启)
|
|
57
|
+
const q = (s) => '"' + String(s).replace(/"/g, '""') + '"';
|
|
58
|
+
const cmdPath = path.join(appRoot, '.dsh-apply.cmd');
|
|
59
|
+
const lines = ['@echo off', 'timeout /t 2 /nobreak >nul'];
|
|
60
|
+
if (exeName) lines.push(`move /y ${q(path.join(staging, exeName))} ${q(path.join(appRoot, exeName))} >nul`);
|
|
61
|
+
lines.push(`rmdir /s /q ${q(staging)}`);
|
|
62
|
+
lines.push(`start "" ${q(path.join(appRoot, exeName || path.basename(process.execPath)))}`);
|
|
63
|
+
lines.push('del "%~f0"');
|
|
64
|
+
fs.writeFileSync(cmdPath, lines.join('\r\n') + '\r\n', 'ascii');
|
|
65
|
+
|
|
66
|
+
return { ok: true, exeChanged, cmdPath };
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
module.exports = { applyUpdate };
|
|
@@ -0,0 +1,118 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
// events.js - 业务事件监听:主进程直连引擎 events.mux WebSocket
|
|
3
|
+
//
|
|
4
|
+
// 引擎(dsh-client-connection)暴露两条 WebSocket 下行流:
|
|
5
|
+
// /api/events.mux 业务事件(mux 帧)
|
|
6
|
+
// /api/events.host 宿主事件
|
|
7
|
+
// 本模块连接 mux 流,解析帧并分类为可通知的业务事件:
|
|
8
|
+
//
|
|
9
|
+
// WS 文本消息 = ServerRequest envelope:
|
|
10
|
+
// { type:'server-request', rpcId, method, payload }
|
|
11
|
+
// payload = mux 帧(discriminatedUnion by type):
|
|
12
|
+
// approval/requested { sessionId, approvalId, toolName, callId?, reason? } ← 审批请求
|
|
13
|
+
// question/requested { sessionId, questions:[{question, header?, ...}] } ← 用户提问
|
|
14
|
+
// session/event { sessionId, event } → event.type==='turn/end' ← 任务轮次结束
|
|
15
|
+
// session/subscribed / session/queue / session/jobs / session/projection / stream/error 等(暂不通知)
|
|
16
|
+
//
|
|
17
|
+
// 说明:审批/提问是宿主级广播帧,任何连接都能收到;session/event 是否对所有会话
|
|
18
|
+
// 广播取决于服务端订阅策略,真机验证时以日志记录的帧类型为准(见 main.js)。
|
|
19
|
+
//
|
|
20
|
+
// 零依赖:Node 内置全局 WebSocket(Node 22+/Electron 43)。
|
|
21
|
+
|
|
22
|
+
const RECONNECT_BASE_MS = 1000;
|
|
23
|
+
const RECONNECT_MAX_MS = 15000;
|
|
24
|
+
|
|
25
|
+
// 解析一条 WS 文本消息 → mux 帧;非法/非 server-request 返回 null(静默忽略)。
|
|
26
|
+
function parseFrame(raw) {
|
|
27
|
+
let msg;
|
|
28
|
+
try { msg = JSON.parse(String(raw)); } catch { return null; }
|
|
29
|
+
if (!msg || msg.type !== 'server-request' || !msg.payload || typeof msg.payload.type !== 'string') return null;
|
|
30
|
+
return msg.payload;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
// 把 mux 帧分类为可通知的业务事件;不关心的返回 null。
|
|
34
|
+
// 返回 { kind, title, body }。
|
|
35
|
+
function classifyFrame(frame) {
|
|
36
|
+
if (!frame || typeof frame.type !== 'string') return null;
|
|
37
|
+
switch (frame.type) {
|
|
38
|
+
case 'approval/requested': {
|
|
39
|
+
const reason = frame.reason ? `(${frame.reason})` : '';
|
|
40
|
+
const body = `工具「${frame.toolName || '未知'}」请求执行 ${reason}`.trim();
|
|
41
|
+
return { kind: 'approval', title: '需要审批', body };
|
|
42
|
+
}
|
|
43
|
+
case 'question/requested': {
|
|
44
|
+
const q = frame.questions && frame.questions[0];
|
|
45
|
+
if (!q) return null;
|
|
46
|
+
return { kind: 'question', title: '有提问需要回答', body: q.question || q.header || '请回答一个问题' };
|
|
47
|
+
}
|
|
48
|
+
case 'session/event': {
|
|
49
|
+
if (frame.event && frame.event.type === 'turn/end') {
|
|
50
|
+
const data = frame.event.data;
|
|
51
|
+
let reason = '';
|
|
52
|
+
if (data && data.reason != null) {
|
|
53
|
+
// reason 可能是字符串或对象(如 {code,message});对象安全序列化,避免 [object Object]
|
|
54
|
+
reason = typeof data.reason === 'string' ? data.reason : JSON.stringify(data.reason);
|
|
55
|
+
}
|
|
56
|
+
return { kind: 'turn-end', title: '任务完成', body: `对话轮次已结束${reason ? `(${reason})` : ''}` };
|
|
57
|
+
}
|
|
58
|
+
return null;
|
|
59
|
+
}
|
|
60
|
+
default:
|
|
61
|
+
return null;
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
// 连接引擎事件流。
|
|
66
|
+
// baseUrl 引擎地址,形如 http://127.0.0.1:1234
|
|
67
|
+
// onNotify ({title, body}) 收到可通知业务事件时回调
|
|
68
|
+
// onFrame (frame) 收到任意 mux 帧时回调(诊断用,观察服务端推送内容)
|
|
69
|
+
// onState ('connected' | 'reconnecting') 连接状态回调(可选)
|
|
70
|
+
// 自动重连(指数退避);返回 { stop() }。
|
|
71
|
+
function connectEvents(baseUrl, { onNotify, onFrame, onState } = {}) {
|
|
72
|
+
if (typeof WebSocket !== 'function') {
|
|
73
|
+
console.error('[events] 全局 WebSocket 不可用(Node 需 ≥22)');
|
|
74
|
+
return { stop() { /* noop */ } };
|
|
75
|
+
}
|
|
76
|
+
const wsUrl = String(baseUrl).replace(/^http/i, 'ws') + '/api/events.mux';
|
|
77
|
+
let ws = null;
|
|
78
|
+
let stopped = false;
|
|
79
|
+
let timer = null;
|
|
80
|
+
let attempt = 0;
|
|
81
|
+
|
|
82
|
+
const scheduleReconnect = () => {
|
|
83
|
+
if (stopped) return;
|
|
84
|
+
const delay = Math.min(RECONNECT_BASE_MS * 2 ** attempt, RECONNECT_MAX_MS);
|
|
85
|
+
attempt += 1;
|
|
86
|
+
if (onState) { try { onState('reconnecting'); } catch { /* ignore */ } }
|
|
87
|
+
timer = setTimeout(open, delay);
|
|
88
|
+
};
|
|
89
|
+
|
|
90
|
+
const open = () => {
|
|
91
|
+
if (stopped) return;
|
|
92
|
+
let socket;
|
|
93
|
+
try { socket = new WebSocket(wsUrl); } catch { scheduleReconnect(); return; }
|
|
94
|
+
ws = socket;
|
|
95
|
+
socket.onopen = () => { attempt = 0; if (onState) { try { onState('connected'); } catch { /* ignore */ } } };
|
|
96
|
+
socket.onmessage = (ev) => {
|
|
97
|
+
const frame = parseFrame(ev.data);
|
|
98
|
+
if (!frame) return;
|
|
99
|
+
if (onFrame) { try { onFrame(frame); } catch { /* ignore */ } }
|
|
100
|
+
const info = classifyFrame(frame);
|
|
101
|
+
if (info && onNotify) { try { onNotify(info); } catch { /* ignore */ } }
|
|
102
|
+
};
|
|
103
|
+
socket.onclose = () => { if (!stopped) scheduleReconnect(); };
|
|
104
|
+
socket.onerror = () => { try { socket.close(); } catch { /* ignore */ } };
|
|
105
|
+
};
|
|
106
|
+
|
|
107
|
+
open();
|
|
108
|
+
return {
|
|
109
|
+
stop() {
|
|
110
|
+
stopped = true;
|
|
111
|
+
clearTimeout(timer);
|
|
112
|
+
if (ws) { try { ws.close(); } catch { /* ignore */ } }
|
|
113
|
+
ws = null;
|
|
114
|
+
},
|
|
115
|
+
};
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
module.exports = { parseFrame, classifyFrame, connectEvents };
|