dsh-mobile-plugin 0.5.57 → 0.5.58
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/lib/client.js +92 -2
- package/lib/mobile/host-web.js +3 -6
- package/lib/mobile/permissions.js +0 -3
- package/lib/routes/mobile-api.js +0 -10
- package/package.json +1 -1
- package/lib/mobile/chat-embed.js +0 -112
package/lib/client.js
CHANGED
|
@@ -4,8 +4,8 @@
|
|
|
4
4
|
*
|
|
5
5
|
* 五件事:桌面侧边栏的展开态与「读过了」报给宿主;手机端的已读回执应用回桌面绿点;
|
|
6
6
|
* 设置里那一项「移动端」(一张薄卡,一颗「打开配置页面」);在侧边栏最底下那一行挂一颗
|
|
7
|
-
*
|
|
8
|
-
*
|
|
7
|
+
* 手机图标(点一下等于那张卡上的按钮);聊天嵌入页里按注入的目标打开会话。方向是单向的
|
|
8
|
+
* (桌面 → 宿主 → 手机),手机上的折叠只存在 App 本地,不写回。
|
|
9
9
|
*
|
|
10
10
|
* 本文件不经任何构建(package.json 直接收录 lib 下的 .js),所以不能写 JSX,统一用
|
|
11
11
|
* `React.createElement` 的别名 `h`;客户端插件按约定写成惰性工厂,定时器与页签在物化时才起。
|
|
@@ -2107,6 +2107,95 @@ window.__ModuleLoader__.load({
|
|
|
2107
2107
|
: null));
|
|
2108
2108
|
}
|
|
2109
2109
|
|
|
2110
|
+
/* ── 聊天嵌入页:把网页定位到顶栏那条会话 ───────────────────────────────
|
|
2111
|
+
* 官方整页没选工作区时停在欢迎页。App 在页面启动前写下目标会话号,
|
|
2112
|
+
* 这里照 liveshare 的 viewer-session 做:等会话清单出现它,再用 uiWorkspace 打开。
|
|
2113
|
+
* 打开会让清单再通知一次,所以订阅回调里必须先退订再打开,且只开一次。
|
|
2114
|
+
* 回报仍走 App 认的那一个 kind;没有目标、或服务没就位都不回报,免得被当成失败盖住整页。
|
|
2115
|
+
*/
|
|
2116
|
+
const CHAT_EMBED_KIND = 'dsh-chat-embed';
|
|
2117
|
+
/** 等清单出现目标的最长时间:九秒够页面把工作区与会话清单拉起来。 */
|
|
2118
|
+
const CHAT_EMBED_WAIT_MS = 9000;
|
|
2119
|
+
function openEmbeddedSession(ctx) {
|
|
2120
|
+
const target = globalThis.__DSH_CHAT_EMBED_TARGET__;
|
|
2121
|
+
const sessionId = target && typeof target.sessionId === 'string' ? target.sessionId : '';
|
|
2122
|
+
if (!sessionId || !ctx || typeof ctx.inject !== 'function') return;
|
|
2123
|
+
// 本插件是 immediately 那一档,apply 时 ctx.get 还看不见宿主稍后才提供的 sessions
|
|
2124
|
+
// (绿点两条同步当初就是被这点坑过,才统一改走 ctx.inject)。liveshare 的 viewer
|
|
2125
|
+
// 插件同样把 sessions 与 uiWorkspace 列进 inject 才读清单、打开会话。这里等两者
|
|
2126
|
+
// 就位,拿到的是带服务的子 ctx;没就位就一直不回调,安静降级,不回报也不抛。
|
|
2127
|
+
ctx.inject(['sessions', 'uiWorkspace'], (scope) => {
|
|
2128
|
+
armEmbeddedSession(scope, sessionId);
|
|
2129
|
+
});
|
|
2130
|
+
}
|
|
2131
|
+
|
|
2132
|
+
/** 清单与导航就位后:等目标出现,退订再打开一次,并按原 kind 回报 App。 */
|
|
2133
|
+
function armEmbeddedSession(scope, sessionId) {
|
|
2134
|
+
const sessions = scope.sessions;
|
|
2135
|
+
const uiWorkspace = scope.uiWorkspace;
|
|
2136
|
+
if (!sessions || !sessions.list || typeof sessions.list.subscribe !== 'function'
|
|
2137
|
+
|| typeof sessions.list.getSnapshot !== 'function'
|
|
2138
|
+
|| !uiWorkspace || typeof uiWorkspace.openSession !== 'function') return;
|
|
2139
|
+
const win = typeof window !== 'undefined' ? window : null;
|
|
2140
|
+
|
|
2141
|
+
const post = (located) => {
|
|
2142
|
+
try {
|
|
2143
|
+
const bridge = win && win.ReactNativeWebView;
|
|
2144
|
+
if (bridge && typeof bridge.postMessage === 'function') {
|
|
2145
|
+
bridge.postMessage(JSON.stringify({ kind: CHAT_EMBED_KIND, sessionId, located: Boolean(located) }));
|
|
2146
|
+
}
|
|
2147
|
+
} catch { /* 桌面网页里没有 WebView 桥:回报不到也不影响会话本身 */ }
|
|
2148
|
+
};
|
|
2149
|
+
const listed = () => {
|
|
2150
|
+
try {
|
|
2151
|
+
const snapshot = sessions.list.getSnapshot();
|
|
2152
|
+
return !!(snapshot && snapshot.byId && snapshot.byId[sessionId]);
|
|
2153
|
+
} catch {
|
|
2154
|
+
return false;
|
|
2155
|
+
}
|
|
2156
|
+
};
|
|
2157
|
+
|
|
2158
|
+
scope.effect(() => {
|
|
2159
|
+
let done = false;
|
|
2160
|
+
let opened = false;
|
|
2161
|
+
let unsubscribe = null;
|
|
2162
|
+
let timer = null;
|
|
2163
|
+
const finish = (located) => {
|
|
2164
|
+
if (done) return;
|
|
2165
|
+
done = true;
|
|
2166
|
+
if (timer !== null && win && typeof win.clearTimeout === 'function') win.clearTimeout(timer);
|
|
2167
|
+
if (typeof unsubscribe === 'function') unsubscribe();
|
|
2168
|
+
if (located && !opened) {
|
|
2169
|
+
opened = true;
|
|
2170
|
+
try {
|
|
2171
|
+
uiWorkspace.openSession(sessionId);
|
|
2172
|
+
} catch {
|
|
2173
|
+
located = false;
|
|
2174
|
+
}
|
|
2175
|
+
}
|
|
2176
|
+
post(located);
|
|
2177
|
+
};
|
|
2178
|
+
const onListed = () => {
|
|
2179
|
+
if (done || !listed()) return;
|
|
2180
|
+
// 退订要赶在打开之前:打开会再次通知清单,留着监听器会重进自己直到栈溢出。
|
|
2181
|
+
finish(true);
|
|
2182
|
+
};
|
|
2183
|
+
if (listed()) {
|
|
2184
|
+
finish(true);
|
|
2185
|
+
return () => {};
|
|
2186
|
+
}
|
|
2187
|
+
unsubscribe = sessions.list.subscribe(onListed);
|
|
2188
|
+
if (win && typeof win.setTimeout === 'function') {
|
|
2189
|
+
timer = win.setTimeout(() => finish(false), CHAT_EMBED_WAIT_MS);
|
|
2190
|
+
}
|
|
2191
|
+
return () => {
|
|
2192
|
+
done = true;
|
|
2193
|
+
if (timer !== null && win && typeof win.clearTimeout === 'function') win.clearTimeout(timer);
|
|
2194
|
+
if (typeof unsubscribe === 'function') unsubscribe();
|
|
2195
|
+
};
|
|
2196
|
+
}, 'mobile-plugin: 打开聊天嵌入页目标会话');
|
|
2197
|
+
}
|
|
2198
|
+
|
|
2110
2199
|
return {
|
|
2111
2200
|
// slots:注册设置项与侧边栏那颗图标,由 @deepseek-ai/dsh-client-runtime 提供(它 require
|
|
2112
2201
|
// dsh-client-ui-slots 的 SlotCore 再以 `super(ctx, "slots")` 暴露服务)。
|
|
@@ -2118,6 +2207,7 @@ window.__ModuleLoader__.load({
|
|
|
2118
2207
|
const t = ctx.locale.bind(NS);
|
|
2119
2208
|
installPluginPages(ctx, t);
|
|
2120
2209
|
captureSessions(ctx);
|
|
2210
|
+
openEmbeddedSession(ctx);
|
|
2121
2211
|
startDesktopReadSync(ctx);
|
|
2122
2212
|
startReadReceiptSync(ctx);
|
|
2123
2213
|
startClientHalfReport(ctx);
|
package/lib/mobile/host-web.js
CHANGED
|
@@ -51,12 +51,9 @@ function launchTokenOf(ctx) {
|
|
|
51
51
|
async function handleHostWeb(ctx, _req, res) {
|
|
52
52
|
const port = hostPortOf(ctx);
|
|
53
53
|
const token = port === null ? null : launchTokenOf(ctx);
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
if (port === null) return sendJson(res, 200, { ok: true, available: false, reason: 'no-port', chatEmbed });
|
|
58
|
-
if (token === null) return sendJson(res, 200, { ok: true, available: false, reason: 'no-launch-token', chatEmbed });
|
|
59
|
-
sendJson(res, 200, { ok: true, available: true, port, token, pluginPages: true, chatEmbed });
|
|
54
|
+
if (port === null) return sendJson(res, 200, { ok: true, available: false, reason: 'no-port' });
|
|
55
|
+
if (token === null) return sendJson(res, 200, { ok: true, available: false, reason: 'no-launch-token' });
|
|
56
|
+
sendJson(res, 200, { ok: true, available: true, port, token, pluginPages: true });
|
|
60
57
|
}
|
|
61
58
|
|
|
62
59
|
module.exports = {
|
|
@@ -342,9 +342,6 @@ const MAP = Object.freeze({
|
|
|
342
342
|
'/api/mobile/v1/relay/reconnect': V.MANAGE,
|
|
343
343
|
'/api/mobile/v1/relay/restart-sidecar': V.MANAGE,
|
|
344
344
|
'/api/mobile/v1/host-web': V.MANAGE,
|
|
345
|
-
// 聊天嵌入页吐的是带宿主一次性启动令牌的整页,敏感度和 /host-web 同一档;
|
|
346
|
-
// 不登记会在这里解析成 unknown、被 guarded fail-closed 成 403,手机端整页打不开。
|
|
347
|
-
'/api/mobile/v1/host-web/chat-page': V.MANAGE,
|
|
348
345
|
|
|
349
346
|
// ── config:宿主配置面(模型 / 渠道 / 插件 / 密钥 / 预设)─────────
|
|
350
347
|
'/api/mobile/v1/credentials/set': V.CONFIG,
|
package/lib/routes/mobile-api.js
CHANGED
|
@@ -111,9 +111,6 @@ const extensionData = require('../mobile/extension-data.js');
|
|
|
111
111
|
const extensionDev = require('../mobile/extension-dev.js');
|
|
112
112
|
const extensionCatalog = require('../mobile/extension-catalog.js');
|
|
113
113
|
const sessionDelivery = require('../mobile/session-delivery.js');
|
|
114
|
-
// 聊天嵌入页:把宿主注入好的官方网页版整页取回来、加工后交给 App WebView 直用。
|
|
115
|
-
// 它读 ctx.webServer 的端口与连接服务的启动令牌,所以 handler 从路由层现取 ctx。
|
|
116
|
-
const chatEmbed = require('../mobile/chat-embed.js');
|
|
117
114
|
// 体检接口要自报身份:版本号只有一个来源,就是包自己的 package.json(与 relay/status.js 同)。
|
|
118
115
|
const pkg = require('../../package.json');
|
|
119
116
|
// 「这一代运行时少挂了什么」(见 host-capabilities.cjs):由 host.js 在启动时落账。
|
|
@@ -816,13 +813,6 @@ function registerMobileRoutes(deps) {
|
|
|
816
813
|
// (`mode=webapp` 那一档才允许拨回宿主自己的 web 端口)。
|
|
817
814
|
route('/api/mobile/v1/host-web', deps.hostWeb)
|
|
818
815
|
|
|
819
|
-
// 聊天嵌入页(见 mobile/chat-embed.js):返回官方网页版整页加工后的 HTML,App 以
|
|
820
|
-
// source={{html, baseUrl}} 直接塞进 WebView。成功 { ok:true, html },失败 { ok:false,
|
|
821
|
-
// reason },一律 200 —— 令牌或端口缺一格时 App 要靠 reason 退回「请在电脑上打开网页版」,
|
|
822
|
-
// 抛成 500 只会让这次能力探测变成一句看不懂的错。与上面那条一样走 route() 即包 guarded:
|
|
823
|
-
// 页面里带着宿主的一次性启动令牌,只有已配对设备拿得到。
|
|
824
|
-
route('/api/mobile/v1/host-web/chat-page', chatEmbed.handleChatPage)
|
|
825
|
-
|
|
826
816
|
if (deps.remoteComputers) {
|
|
827
817
|
registerRoute({ kind: 'exact', path: '/api/mobile/v1/remote-computers/pair', handler: deps.remoteComputers.handlePair })
|
|
828
818
|
registerUpgradeRoute({ path: '/api/mobile/v1/remote-computers/tunnel', handler: deps.remoteComputers.handleUpgrade })
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "dsh-mobile-plugin",
|
|
3
|
-
"version": "0.5.
|
|
3
|
+
"version": "0.5.58",
|
|
4
4
|
"description": "Mobile API (/api/mobile/v1) and relay client for the DSH mobile app — desktop profile build (dsh 0.2.x, peers resolved by the host)",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"icon": "./icon.png",
|
package/lib/mobile/chat-embed.js
DELETED
|
@@ -1,112 +0,0 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* 聊天嵌入页:把宿主注入好的官方网页版整页取回来,加工成 App WebView 能直接用的形态。
|
|
5
|
-
*
|
|
6
|
-
* 不拼官方前端路径、不自己调 renderIndex:宿主已经把注入完成的整页挂在自己的 web 端口上,
|
|
7
|
-
* 插件只拿端口与一次性启动令牌走一遍那道登录,把结果原样取回。页面长什么样、以后怎么变,
|
|
8
|
-
* 都由宿主自己负责 —— 插件只改三件与自身形态有关的事:把 base 指到根、藏掉官方外壳、
|
|
9
|
-
* 按 sessionId 把会话切过去。
|
|
10
|
-
*
|
|
11
|
-
* 这一页以 `source={{html, baseUrl}}` 的方式进 App 的 WebView,baseUrl 是预览管道映射出来的
|
|
12
|
-
* 手机环回地址。把 `<base href="./">` 改成 `<base href="/">` 才让官方那堆 `./assets/...`
|
|
13
|
-
* 与 `plugins/...` 落到该地址的根,再由字节管道原样转发给宿主的 web 端口;不改就会按文档
|
|
14
|
-
* 所在路径解析,整页资源全 404。
|
|
15
|
-
*/
|
|
16
|
-
|
|
17
|
-
const { sendJson } = require('../shared/http-util');
|
|
18
|
-
|
|
19
|
-
/** 页面加工脚本派回 App 的消息标记,App 据此分辨这是定位回报而不是业务消息。 */
|
|
20
|
-
const TAG = 'dsh-chat-embed';
|
|
21
|
-
|
|
22
|
-
/**
|
|
23
|
-
* 注入脚本:藏外壳 + 定位会话 + 回报。
|
|
24
|
-
*
|
|
25
|
-
* 官方前端没有按会话跳转的 hash 路由(构建产物里只有 `#dsh-mobile-plugins` 这类面板入口),
|
|
26
|
-
* 所以走现成的 DOM 入口:会话行的 `data-row-key="session:<id>"` 是 stable 钩子(见
|
|
27
|
-
* dsh-client-ui-workspace 的 rows/Rows.tsx),点它等于桌面端用户点开那条会话。手机窄屏下侧栏
|
|
28
|
-
* 默认收成抽屉,先点 `[data-sidebar-toggle]` 展开再找那一行;找不到就把顶层结构与定位结果
|
|
29
|
-
* 原样报回去,让真机上能据此收紧选择器,而不是猜一个宽泛选择器把正文一起藏掉。
|
|
30
|
-
*
|
|
31
|
-
* 藏外壳只认两处确切 DOM:会话顶栏 `header[data-window-drag]`,与侧栏列(从展开按钮上溯到
|
|
32
|
-
* frame 的直接子元素)。同时把 frame 的侧栏轨道清零,否则列虽然 display:none,网格仍留一条
|
|
33
|
-
* 空轨,正文会被顶到屏幕右边。
|
|
34
|
-
*
|
|
35
|
-
* sessionId 先剔掉会闭合脚本的符号再塞进字面量:注入脚本里不能出现 `</script`。
|
|
36
|
-
*/
|
|
37
|
-
function embedScript(sessionId) {
|
|
38
|
-
const safe = String(sessionId || '').replace(/[<>&"']/g, '');
|
|
39
|
-
return [
|
|
40
|
-
'<script>(function(){',
|
|
41
|
-
'var d=document,sid=' + JSON.stringify(safe) + ',n=0,MAX=60;',
|
|
42
|
-
'function post(o){if(window.ReactNativeWebView&&window.ReactNativeWebView.postMessage){try{window.ReactNativeWebView.postMessage(JSON.stringify(o));}catch(e){}}}',
|
|
43
|
-
'function top(){var r=d.getElementById("root"),a=[];if(r){for(var i=0;i<r.children.length&&i<12;i++){var e=r.children[i];a.push({tag:e.tagName,id:e.id||"",cls:String(e.className||"").slice(0,80)});}}return a;}',
|
|
44
|
-
'function report(ok){post({kind:' + JSON.stringify(TAG) + ',sessionId:sid,located:!!ok,top:top()});}',
|
|
45
|
-
'function frame(){var r=d.getElementById("root");if(!r)return null;var f=r.querySelector("[style*=\\"grid-template-columns\\"]");return f||r.firstElementChild;}',
|
|
46
|
-
'function hide(){var f=frame(),i,hs=d.querySelectorAll("header[data-window-drag],[data-shell-leading],[data-sidebar-mask]");for(i=0;i<hs.length;i++)hs[i].style.setProperty("display","none","important");var t=d.querySelector("[data-sidebar-toggle]"),col=t;while(col&&f&&col.parentElement!==f)col=col.parentElement;if(col&&col!==f)col.style.setProperty("display","none","important");if(f&&f.style&&typeof f.style.gridTemplateColumns==="string")f.style.gridTemplateColumns=f.style.gridTemplateColumns.replace(/^\\s*\\S+/,"0px");}',
|
|
47
|
-
'function tick(){n++;if(!sid){hide();report(false);return;}var q=String.fromCharCode(34),sel="[data-row-key="+q+"session:"+sid+q+"]",row=d.querySelector(sel);',
|
|
48
|
-
'if(row){try{row.click();}catch(e){}var c=d.querySelector("[data-sidebar-toggle]");if(c)try{c.click();}catch(e){}hide();report(true);return;}',
|
|
49
|
-
'var t=d.querySelector("[data-sidebar-toggle]");if(t&&n===1)try{t.click();}catch(e){}',
|
|
50
|
-
'if(n<MAX)setTimeout(tick,150);else{hide();report(false);}}',
|
|
51
|
-
'function start(){if(d.readyState==="loading")d.addEventListener("DOMContentLoaded",function(){setTimeout(tick,240);});else setTimeout(tick,240);}',
|
|
52
|
-
'start();})();</script>',
|
|
53
|
-
].join('\n');
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
/**
|
|
57
|
-
* 把官方那页改造成嵌入页。返回 null 表示这页不是宿主注入过的形态,不该往外吐。
|
|
58
|
-
*/
|
|
59
|
-
function decorate(html, sessionId) {
|
|
60
|
-
if (typeof html !== 'string' || html.indexOf('__DSH_BOOT_READY__') < 0) return null;
|
|
61
|
-
let out = html;
|
|
62
|
-
if (/<base href="\.\/">/i.test(out)) out = out.replace(/<base href="\.\/">/i, '<base href="/">');
|
|
63
|
-
else if (!/<base href="\/"/i.test(out)) out = out.replace(/(<head(?:\s[^>]*)?>)/i, '$1<base href="/">');
|
|
64
|
-
const tail = embedScript(sessionId);
|
|
65
|
-
const close = /<\/body\s*>/i;
|
|
66
|
-
out = close.test(out) ? out.replace(close, tail + '</body>') : out + tail;
|
|
67
|
-
return out;
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
/** 用启动令牌走一遍官方那道登录,把宿主注入好的整页取回来并加工。 */
|
|
71
|
-
async function fetchBootstrappedIndex(ctx, sessionId) {
|
|
72
|
-
const port = ctx && ctx.webServer ? ctx.webServer.port : null;
|
|
73
|
-
if (!Number.isInteger(port) || port <= 0 || port > 65535) return { error: 'no-web-port' };
|
|
74
|
-
const connection = typeof ctx?.get === 'function' ? ctx.get('connection') : null;
|
|
75
|
-
const token = connection && connection.browserAuth ? connection.browserAuth.launchToken : null;
|
|
76
|
-
if (typeof token !== 'string' || token === '') return { error: 'no-launch-token' };
|
|
77
|
-
|
|
78
|
-
const origin = `http://127.0.0.1:${port}`;
|
|
79
|
-
const timeout = AbortSignal.timeout(5000);
|
|
80
|
-
const login = await fetch(`${origin}/?token=${encodeURIComponent(token)}`, { redirect: 'manual', signal: timeout });
|
|
81
|
-
const setCookie = login.headers.get('set-cookie');
|
|
82
|
-
const cookie = typeof setCookie === 'string' && setCookie ? setCookie.split(';', 1)[0] : '';
|
|
83
|
-
const page = await fetch(`${origin}/`, {
|
|
84
|
-
headers: cookie ? { cookie } : {},
|
|
85
|
-
redirect: 'manual',
|
|
86
|
-
signal: timeout,
|
|
87
|
-
});
|
|
88
|
-
if (!page.ok) return { error: `index-http-${page.status}` };
|
|
89
|
-
const decorated = decorate(await page.text(), sessionId);
|
|
90
|
-
if (!decorated) return { error: 'index-not-bootstrapped' };
|
|
91
|
-
return { html: decorated };
|
|
92
|
-
}
|
|
93
|
-
|
|
94
|
-
/** `GET /api/mobile/v1/host-web/chat-page`(移动端守卫之后的 handler)。 */
|
|
95
|
-
async function handleChatPage(ctx, req, res) {
|
|
96
|
-
let sessionId = '';
|
|
97
|
-
try {
|
|
98
|
-
const url = new URL(String(req?.url || ''), 'http://localhost');
|
|
99
|
-
sessionId = String(url.searchParams.get('sessionId') || '');
|
|
100
|
-
} catch { /* query 残缺就当没带 sessionId,页面照常出,只是不定位 */ }
|
|
101
|
-
let got;
|
|
102
|
-
try {
|
|
103
|
-
got = await fetchBootstrappedIndex(ctx, sessionId);
|
|
104
|
-
} catch (error) {
|
|
105
|
-
got = { error: String(error?.message || error) };
|
|
106
|
-
}
|
|
107
|
-
// 失败一律 200 + ok:false:这条是能力探测,抛上去只会变成 500,App 反而看不出哪一格缺了。
|
|
108
|
-
if (got.error) return sendJson(res, 200, { ok: false, reason: got.error });
|
|
109
|
-
return sendJson(res, 200, { ok: true, html: got.html });
|
|
110
|
-
}
|
|
111
|
-
|
|
112
|
-
module.exports = { handleChatPage, decorate, embedScript, fetchBootstrappedIndex, TAG };
|