dsh-mobile-plugin 0.5.59 → 0.5.60
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 +2 -0
- package/docs/chat-embed.md +123 -0
- package/lib/client.js +31 -0
- package/lib/mobile/session-send.js +37 -2
- package/lib/panel/index.html +8 -8
- package/lib/relay/lan-proxy.js +55 -1
- package/lib/remote/host.cjs +26 -0
- package/lib/remote/instance-hint.cjs +66 -0
- package/lib/routes/mobile-api.js +4 -0
- package/package.json +2 -1
package/README.md
CHANGED
|
@@ -0,0 +1,123 @@
|
|
|
1
|
+
# 聊天嵌入页:保留网页顶部栏
|
|
2
|
+
|
|
3
|
+
DSH 网页插件把按钮放在网页顶部时,可以向 `dsh-mobile-plugin` 的客户端服务声明:在手机 App 的聊天网页中保留顶部栏。声明只在插件需要时生效,插件卸载或设置关闭后撤销。
|
|
4
|
+
|
|
5
|
+
它作用于 App 为聊天网页隐藏的网页页头和顶部前导区。侧栏、侧栏占位与 App 原生顶栏照旧;用户自己隐藏的区域仍然隐藏。这里的“保留”只撤去 App 自己的顶部隐藏规则,不会强制覆盖用户样式。
|
|
6
|
+
|
|
7
|
+
## 服务契约
|
|
8
|
+
|
|
9
|
+
移动端插件的客户端部分加载后,始终提供 Cordis 服务 `mobileChatEmbed`。它不依赖会话、工作区或设备列表服务就绪。
|
|
10
|
+
|
|
11
|
+
```ts
|
|
12
|
+
interface MobileChatEmbed {
|
|
13
|
+
isEmbedded(): boolean;
|
|
14
|
+
keepTopBar(): () => void;
|
|
15
|
+
}
|
|
16
|
+
```
|
|
17
|
+
|
|
18
|
+
- `isEmbedded()`:当前文档有受支持的 App 聊天桥时返回 `true`。普通电脑网页、没有桥的新旧 App 页面都返回 `false`,不会改变页面。
|
|
19
|
+
- `keepTopBar()`:新建一份保留声明,返回撤销函数。每次调用独立登记,重复调用同一撤销函数不会重复释放。
|
|
20
|
+
- 至少有一份有效声明时,App 撤去自己的顶部隐藏规则;最后一份撤销后恢复 App 默认隐藏。一个插件不能撤销另一个插件的声明。
|
|
21
|
+
|
|
22
|
+
没有安装或启用移动端插件时,这个服务不存在。通过 `ctx.inject(['mobileChatEmbed'], ...)` 等待它,只让这项增强依赖它;不要把 `mobileChatEmbed` 放进整个插件导出的必选 `inject`,否则插件的其他功能也会等待移动端插件。
|
|
23
|
+
|
|
24
|
+
## 最小 Client 示例
|
|
25
|
+
|
|
26
|
+
这是可放进 Client 入口的完整模块。把 `id` 改成自己的包名;已有 Client 模块时,把 `apply` 中的注入块合进去即可。工厂只返回插件定义,声明在 `apply` 中创建。
|
|
27
|
+
|
|
28
|
+
```js
|
|
29
|
+
window.__ModuleLoader__.load({
|
|
30
|
+
id: '@example/chat-toolbar',
|
|
31
|
+
factory() {
|
|
32
|
+
return {
|
|
33
|
+
apply(ctx) {
|
|
34
|
+
ctx.inject(['mobileChatEmbed'], (scope) => {
|
|
35
|
+
scope.effect(() => {
|
|
36
|
+
if (!scope.mobileChatEmbed.isEmbedded()) return;
|
|
37
|
+
return scope.mobileChatEmbed.keepTopBar();
|
|
38
|
+
}, 'chat-toolbar: keep embedded top bar');
|
|
39
|
+
});
|
|
40
|
+
},
|
|
41
|
+
};
|
|
42
|
+
},
|
|
43
|
+
});
|
|
44
|
+
```
|
|
45
|
+
|
|
46
|
+
`scope.effect` 持有返回的撤销函数。消费插件卸载、移动端服务撤下或这段注入作用域结束时,Cordis 会清理它;服务重新可用时再建立声明。移动端服务提供方卸载时也会释放自身登记、撤回对 App 的控制。
|
|
47
|
+
|
|
48
|
+
不要把声明放在组件每次渲染里,也不要在工厂顶层调用。声明应跟随需要顶部栏的插件功能或作用域,而不是跟随当前会话。
|
|
49
|
+
|
|
50
|
+
## 带运行时设置的完整示例
|
|
51
|
+
|
|
52
|
+
设置关闭时不登记;设置变化时先撤销旧声明,再按当前设置重新登记。不能只在 `apply` 开始时读一次开关。
|
|
53
|
+
|
|
54
|
+
下面使用 DSH `0.2.2-rc.3` 的真实设置 API:`configForms.get(entryId)`、`getSnapshot()` 和 `subscribe()`。示例假设自己的 Host 插件已经向设置服务公开 `chat-toolbar` 命名空间,其中有布尔字段 `keepWebTopBar`。把模块 `id`、`ENTRY_ID` 和字段名换成自己的;`ENTRY_ID` 是 Host 插件条目的设置命名空间,不一定等于 npm 包名。
|
|
55
|
+
|
|
56
|
+
```js
|
|
57
|
+
window.__ModuleLoader__.load({
|
|
58
|
+
id: '@example/chat-toolbar',
|
|
59
|
+
factory() {
|
|
60
|
+
const ENTRY_ID = 'chat-toolbar';
|
|
61
|
+
|
|
62
|
+
return {
|
|
63
|
+
apply(ctx) {
|
|
64
|
+
ctx.inject(['mobileChatEmbed', 'configForms'], (scope) => {
|
|
65
|
+
scope.effect(() => {
|
|
66
|
+
const embed = scope.mobileChatEmbed;
|
|
67
|
+
if (!embed.isEmbedded()) return;
|
|
68
|
+
|
|
69
|
+
const form = scope.configForms.get(ENTRY_ID);
|
|
70
|
+
let release = () => {};
|
|
71
|
+
|
|
72
|
+
const sync = () => {
|
|
73
|
+
release();
|
|
74
|
+
release = () => {};
|
|
75
|
+
|
|
76
|
+
const snapshot = form.getSnapshot();
|
|
77
|
+
if (snapshot.status !== 'ready') return;
|
|
78
|
+
if (snapshot.value?.keepWebTopBar !== true) return;
|
|
79
|
+
release = embed.keepTopBar();
|
|
80
|
+
};
|
|
81
|
+
|
|
82
|
+
const unsubscribe = form.subscribe(sync);
|
|
83
|
+
sync();
|
|
84
|
+
|
|
85
|
+
return () => {
|
|
86
|
+
unsubscribe();
|
|
87
|
+
release();
|
|
88
|
+
};
|
|
89
|
+
}, 'chat-toolbar: follow embedded top bar preference');
|
|
90
|
+
});
|
|
91
|
+
},
|
|
92
|
+
};
|
|
93
|
+
},
|
|
94
|
+
});
|
|
95
|
+
```
|
|
96
|
+
|
|
97
|
+
设置尚未就绪、不可用或开关为 `false` 时,示例不持有声明。设置页继续通过这个共享表单的 `set('keepWebTopBar', value)` 保存选择;表单接受新值后,订阅回调会更新声明。已有自己的设置订阅时,保留相同的“先撤销,再按真值登记”流程即可。
|
|
98
|
+
|
|
99
|
+
表单由 `configForms` 服务所有,消费方只清理自己的订阅和声明。这里把两者都放在注入作用域的 `scope.effect` 中,清理时先退订,再撤销当前声明。两个服务中的任意一个卸载,或消费插件卸载,都会结束该作用域;缺少它们只暂停这项顶部栏增强。
|
|
100
|
+
|
|
101
|
+
## 加载与页面生命周期
|
|
102
|
+
|
|
103
|
+
- 插件和移动端服务可以先后加载。`ctx.inject` 会在服务可用时建立作用域,不需要广播、轮询或自己猜加载顺序。
|
|
104
|
+
- 声明可以在页面启动后任意时刻到达,没有九秒截止时间。晚到时可能先看到顶部隐藏,声明生效后再恢复;顶部元素稍后才挂载也会遵循当前声明。
|
|
105
|
+
- 在同一个网页文档里切换会话,声明继续有效,不需要每次切会话重复申请。
|
|
106
|
+
- 网页重载、重试或其他操作建立新文档后,原声明结束,由消费插件再次执行 `apply` 重建。不要跨文档缓存服务或撤销函数。
|
|
107
|
+
- 所有声明撤销后恢复的是 App 的默认顶部隐藏规则。用户样式、其他插件自己控制的显示状态,以及侧栏和原生顶栏的既有行为继续保留。
|
|
108
|
+
|
|
109
|
+
## App 桥约定
|
|
110
|
+
|
|
111
|
+
App 在 document-start 阶段向聊天网页提供 `globalThis.__DSH_MOBILE_CHAT_EMBED__`,形状为:
|
|
112
|
+
|
|
113
|
+
```ts
|
|
114
|
+
interface MobileChatEmbedBridge {
|
|
115
|
+
version: 1;
|
|
116
|
+
surface: 'chat';
|
|
117
|
+
setTopBarVisible(visible: boolean): void;
|
|
118
|
+
}
|
|
119
|
+
```
|
|
120
|
+
|
|
121
|
+
`mobileChatEmbed.isEmbedded()` 检查桥的版本、聊天 surface 和可调用的方法。普通网页及没有这份桥的新旧 App 页面会得到 `false`;`keepTopBar()` 在这些页面也没有显示副作用。
|
|
122
|
+
|
|
123
|
+
网页插件通过 Cordis 服务声明需求,由移动端插件合并多份声明后调用桥。直接调用 `setTopBarVisible(false)` 会绕开合并,覆盖其他插件仍在持有的保留需求,因此消费方应只使用服务和它返回的撤销函数。
|
package/lib/client.js
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* 移动端插件提供电脑插件页面目录;标准页面保留宿主上下文并支持直接导航。
|
|
3
|
+
* 聊天嵌入服务合并顶部栏显示声明,随提供方与消费方生命周期释放。
|
|
3
4
|
* 本机和局域网入口直接访问管理页,公网入口保留两小时窗口。
|
|
4
5
|
*
|
|
5
6
|
* 五件事:桌面侧边栏的展开态与「读过了」报给宿主;手机端的已读回执应用回桌面绿点;
|
|
@@ -2304,6 +2305,35 @@ window.__ModuleLoader__.load({
|
|
|
2304
2305
|
};
|
|
2305
2306
|
}
|
|
2306
2307
|
|
|
2308
|
+
/** 每个提供方独立持有声明;旧撤销函数不能碰到重载后的新声明。 */
|
|
2309
|
+
function provideMobileChatEmbed(ctx) {
|
|
2310
|
+
const candidate = globalThis.__DSH_MOBILE_CHAT_EMBED__;
|
|
2311
|
+
const bridge = candidate && candidate.version === 1 && candidate.surface === 'chat'
|
|
2312
|
+
&& typeof candidate.setTopBarVisible === 'function' ? candidate : null;
|
|
2313
|
+
const claims = new Set();
|
|
2314
|
+
let disposed = false;
|
|
2315
|
+
const service = {
|
|
2316
|
+
isEmbedded: () => Boolean(bridge && !disposed),
|
|
2317
|
+
keepTopBar() {
|
|
2318
|
+
if (!bridge || disposed) return () => {};
|
|
2319
|
+
const claim = {};
|
|
2320
|
+
claims.add(claim);
|
|
2321
|
+
if (claims.size === 1) bridge.setTopBarVisible(true);
|
|
2322
|
+
return () => {
|
|
2323
|
+
if (disposed || !claims.delete(claim)) return;
|
|
2324
|
+
if (claims.size === 0) bridge.setTopBarVisible(false);
|
|
2325
|
+
};
|
|
2326
|
+
},
|
|
2327
|
+
};
|
|
2328
|
+
ctx.effect(() => () => {
|
|
2329
|
+
disposed = true;
|
|
2330
|
+
const hadClaims = claims.size > 0;
|
|
2331
|
+
claims.clear();
|
|
2332
|
+
if (bridge && hadClaims) bridge.setTopBarVisible(false);
|
|
2333
|
+
}, 'mobile-plugin: chat embed top bar');
|
|
2334
|
+
ctx.provide('mobileChatEmbed', service);
|
|
2335
|
+
}
|
|
2336
|
+
|
|
2307
2337
|
return {
|
|
2308
2338
|
// slots:注册设置项与侧边栏那颗图标,由 @deepseek-ai/dsh-client-runtime 提供(它 require
|
|
2309
2339
|
// dsh-client-ui-slots 的 SlotCore 再以 `super(ctx, "slots")` 暴露服务)。
|
|
@@ -2311,6 +2341,7 @@ window.__ModuleLoader__.load({
|
|
|
2311
2341
|
// 这两个服务名对应 package.json 里 dsh.client.inject 注入的两个包。
|
|
2312
2342
|
inject: ['slots', 'locale'],
|
|
2313
2343
|
apply(ctx) {
|
|
2344
|
+
provideMobileChatEmbed(ctx);
|
|
2314
2345
|
ctx.effect(() => ctx.locale.register(NS, DICT), 'mobile-plugin: settings dictionaries');
|
|
2315
2346
|
const t = ctx.locale.bind(NS);
|
|
2316
2347
|
installPluginPages(ctx, t);
|
|
@@ -26,6 +26,7 @@ const {
|
|
|
26
26
|
const { promptErrorObject, MODEL_UNAVAILABLE_ERROR } = require('./history-derive.js');
|
|
27
27
|
const { withIdempotency, requestIdOf } = require('./idempotency.js');
|
|
28
28
|
const { invoke, UNAVAILABLE: GATEWAY_UNAVAILABLE } = require('./host-bridge/transport.js');
|
|
29
|
+
const { selectSessionModel } = require('./host-bridge/session-model.js');
|
|
29
30
|
const { recordSessionRead } = require('./session-last-turn.js');
|
|
30
31
|
const { imageSizeOf } = require('./local-images.js');
|
|
31
32
|
const attachmentStore = require('./attachment-store.js');
|
|
@@ -47,6 +48,22 @@ function singleTextOf(content) {
|
|
|
47
48
|
return block.text;
|
|
48
49
|
}
|
|
49
50
|
|
|
51
|
+
/**
|
|
52
|
+
* 本次发送要临时使用的模型(`{provider, model, reasoningEffort?}`):App 选完模型不再单独
|
|
53
|
+
* 发切换请求,而是随这一条消息带过来,由这里在**真正提交 prompt 之前**落到会话上。
|
|
54
|
+
*
|
|
55
|
+
* 形状不认识(缺 provider / model)就当没带:老 App 不带这一格,行为与以前完全一致。
|
|
56
|
+
*/
|
|
57
|
+
function modelSelectionOf(body) {
|
|
58
|
+
const value = body && body.modelSelection;
|
|
59
|
+
if (!value || typeof value !== 'object') return null;
|
|
60
|
+
const provider = typeof value.provider === 'string' ? value.provider.trim() : '';
|
|
61
|
+
const model = typeof value.model === 'string' ? value.model.trim() : '';
|
|
62
|
+
if (!provider || !model) return null;
|
|
63
|
+
const reasoningEffort = typeof value.reasoningEffort === 'string' && value.reasoningEffort ? value.reasoningEffort : undefined;
|
|
64
|
+
return { provider, model, ...(reasoningEffort ? { reasoningEffort } : {}) };
|
|
65
|
+
}
|
|
66
|
+
|
|
50
67
|
/**
|
|
51
68
|
* 经网关执行 slash 命令(commands.execute,桌面 Web 端同一条派发路径:命令注册表命中才执行,
|
|
52
69
|
* 绝不经 session.prompt 发给模型;agent 由 agentId 经同一查找器解析,支持冷会话自动 resume)。
|
|
@@ -72,8 +89,10 @@ async function executeCommand(ctx, sessionId, line) {
|
|
|
72
89
|
* POST /api/mobile/v1/session/send(token)
|
|
73
90
|
* 兼容两种格式:
|
|
74
91
|
* 旧:{sessionId, text}
|
|
75
|
-
* 新:{sessionId, content:[{type:'text',text} | {type:'image',mediaType,data(base64),name?}], mode?:'queue'|'steer'
|
|
76
|
-
*
|
|
92
|
+
* 新:{sessionId, content:[{type:'text',text} | {type:'image',mediaType,data(base64),name?}], mode?:'queue'|'steer',
|
|
93
|
+
* modelSelection?:{provider,model,reasoningEffort?}}
|
|
94
|
+
* `modelSelection` 是本次发送要用的模型(App 侧选模型不再单独发切换请求);提交 prompt 之前先
|
|
95
|
+
* 落到会话上。恰为单个文本块且以 / 开头时派发为 slash 命令(commands.execute,不发给模型,也不应用模型);
|
|
77
96
|
* 其余原样透传给 harness 的 session.prompt(图片块需模型支持,不支持时错误原样返回)。
|
|
78
97
|
*/
|
|
79
98
|
function promptOnce(ctx, clientMessageId, payload) {
|
|
@@ -151,6 +170,22 @@ async function handleSend(ctx, req, res) {
|
|
|
151
170
|
return sendJson(res, 200, { ok: true, accepted: true, handledAs: 'command' });
|
|
152
171
|
}
|
|
153
172
|
}
|
|
173
|
+
// ── 本次发送携带的模型 ─────────────────────────────────────────
|
|
174
|
+
// App 侧选模型只改本地,切换请求随这一条消息一起到。**必须在 prompt 之前**落到会话上,
|
|
175
|
+
// 这样这一轮的第一步用的就是用户刚选的模型(`agent.model.select` 只作用于尚未准备的那一步)。
|
|
176
|
+
// 模型不可用 / 无该能力时明确回 400,让 App 报出来 —— 不静默沿用会话里原来的模型。
|
|
177
|
+
const modelSelection = modelSelectionOf(body);
|
|
178
|
+
if (modelSelection) {
|
|
179
|
+
const applied = await selectSessionModel(ctx, { sessionId: body.sessionId, ...modelSelection });
|
|
180
|
+
const result = applied && applied.result ? applied.result : null;
|
|
181
|
+
if (!result || result.ok !== true) {
|
|
182
|
+
const failure = result && result.error
|
|
183
|
+
? result.error
|
|
184
|
+
: { code: 'session/model-unavailable', message: '所选模型当前不可用', details: modelSelection };
|
|
185
|
+
return sendJson(res, 400, { ok: false, error: continuation ? continuationError(failure) : failure });
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
|
|
154
189
|
let promptResponse;
|
|
155
190
|
try {
|
|
156
191
|
promptResponse = await promptOnce(ctx, typeof body.clientMessageId === 'string' ? body.clientMessageId : '', {
|