@appthen/host-sdk 0.0.0-stage → 1.0.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/dist/client.d.ts +24 -0
- package/dist/client.js +70 -0
- package/dist/contract.d.ts +92 -0
- package/dist/contract.js +14 -0
- package/dist/desktop.d.ts +152 -0
- package/dist/desktop.js +102 -0
- package/dist/dispatcher.d.ts +55 -0
- package/dist/dispatcher.js +121 -0
- package/dist/host-mode.d.ts +26 -0
- package/dist/host-mode.js +62 -0
- package/dist/index.d.ts +16 -0
- package/dist/index.js +36 -0
- package/dist/index.mjs +565 -0
- package/dist/injectedScript.d.ts +17 -0
- package/dist/injectedScript.js +82 -0
- package/dist/web-providers.d.ts +21 -0
- package/dist/web-providers.js +247 -0
- package/package.json +23 -3
- package/src/client.ts +87 -0
- package/src/contract.ts +109 -0
- package/src/desktop.ts +209 -0
- package/src/dispatcher.ts +167 -0
- package/src/host-mode.ts +62 -0
- package/src/index.ts +16 -0
- package/src/injectedScript.ts +79 -0
- package/src/web-providers.ts +278 -0
- package/tsconfig.build.json +12 -0
- package/tsconfig.json +16 -0
- package/README.md +0 -3
|
@@ -0,0 +1,278 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* web 降级提供者(web capability providers)
|
|
3
|
+
*
|
|
4
|
+
* 部分能力在纯浏览器里可以用原生 Web API「将就着用」(native 体验更好但缺了也能跑):
|
|
5
|
+
* 剪贴板、系统通知、外链打开、目录/文件选择、TTS/STT、系统分享。
|
|
6
|
+
* 每个提供者实现与 native 方法**同一份返回形状**(见 desktop.ts / contract.ts 的 DTO),
|
|
7
|
+
* 由 `client.ts` 的 resolver 按主机模式决定调用 native 还是这里。
|
|
8
|
+
*
|
|
9
|
+
* 说明:SDK 开启 `types:[]`(无 node / 全局 DOM 类型污染),所以浏览器全局构造
|
|
10
|
+
* (Notification / SpeechSynthesisUtterance / SpeechRecognition 等)都经 `globalThis`
|
|
11
|
+
* 取,避免与两端各自的全局声明打架。
|
|
12
|
+
*
|
|
13
|
+
* 明确**不做** web 降级的能力(native-only):`push.register`(厂商通道)、
|
|
14
|
+
* `daemon.*` / `updater.*`(本机进程)、`voice.wakeup`(常驻唤醒)、`device.identity`。
|
|
15
|
+
*/
|
|
16
|
+
|
|
17
|
+
import type { AppthenHostContext, VoiceListenResult } from './contract';
|
|
18
|
+
|
|
19
|
+
export interface WebCapabilityProvider {
|
|
20
|
+
/** 浏览器里执行能力调用,返回与 native 方法一致的出参形状。 */
|
|
21
|
+
run(params: unknown, context?: AppthenHostContext): Promise<unknown>;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
function hasWindow(): boolean {
|
|
25
|
+
return typeof window !== 'undefined';
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
function requireWindow(what: string): Window {
|
|
29
|
+
if (!hasWindow()) {
|
|
30
|
+
throw { code: 'web_unavailable', message: `能力需要浏览器环境(${what})。`, retryable: false };
|
|
31
|
+
}
|
|
32
|
+
return window;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/** 内联创建一个临时的 `<input type=file>` 并从文件选择中解析出结果。 */
|
|
36
|
+
function pickWithInput(options: {
|
|
37
|
+
webkitdirectory?: boolean;
|
|
38
|
+
multiple?: boolean;
|
|
39
|
+
accept?: string;
|
|
40
|
+
}): Promise<{ canceled: boolean; files: Array<{ name: string; relativePath?: string; size: number }> }> {
|
|
41
|
+
return new Promise((resolve) => {
|
|
42
|
+
const input = document.createElement('input');
|
|
43
|
+
input.type = 'file';
|
|
44
|
+
if (options.webkitdirectory) input.setAttribute('webkitdirectory', '');
|
|
45
|
+
if (options.multiple) input.multiple = true;
|
|
46
|
+
if (options.accept) input.accept = options.accept;
|
|
47
|
+
input.style.position = 'fixed';
|
|
48
|
+
input.style.opacity = '0';
|
|
49
|
+
input.style.pointerEvents = 'none';
|
|
50
|
+
document.body.appendChild(input);
|
|
51
|
+
const cleanup = () => {
|
|
52
|
+
if (input.parentNode) input.parentNode.removeChild(input);
|
|
53
|
+
};
|
|
54
|
+
input.onchange = () => {
|
|
55
|
+
const files = Array.from(input.files ?? []);
|
|
56
|
+
cleanup();
|
|
57
|
+
resolve({
|
|
58
|
+
canceled: false,
|
|
59
|
+
files: files.map((f) => ({
|
|
60
|
+
name: f.name,
|
|
61
|
+
size: f.size,
|
|
62
|
+
relativePath:
|
|
63
|
+
'webkitRelativePath' in f && f.webkitRelativePath ? f.webkitRelativePath : undefined,
|
|
64
|
+
})),
|
|
65
|
+
});
|
|
66
|
+
};
|
|
67
|
+
input.oncancel = () => {
|
|
68
|
+
cleanup();
|
|
69
|
+
resolve({ canceled: true, files: [] });
|
|
70
|
+
};
|
|
71
|
+
input.click();
|
|
72
|
+
});
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
const globalWindow = (): unknown =>
|
|
76
|
+
typeof globalThis !== 'undefined' ? globalThis : ({} as unknown);
|
|
77
|
+
|
|
78
|
+
/** 进行中的 web 语音识别会话(供 `voice.stopListening` 中止)。 */
|
|
79
|
+
let activeWebRecognition: { stop(): void } | null = null;
|
|
80
|
+
|
|
81
|
+
export const webCapabilityProviders: Record<string, WebCapabilityProvider> = {
|
|
82
|
+
'navigation.openExternal': {
|
|
83
|
+
async run(params) {
|
|
84
|
+
const { url } = (params ?? {}) as { url?: string };
|
|
85
|
+
if (!url || typeof url !== 'string') {
|
|
86
|
+
throw { code: 'invalid_arg', message: 'navigation.openExternal 需要 url。', retryable: false };
|
|
87
|
+
}
|
|
88
|
+
const win = requireWindow('window.open');
|
|
89
|
+
win.open(url, '_blank', 'noopener,noreferrer');
|
|
90
|
+
return { ok: true };
|
|
91
|
+
},
|
|
92
|
+
},
|
|
93
|
+
|
|
94
|
+
'navigation.openDeepLink': {
|
|
95
|
+
async run(params) {
|
|
96
|
+
const { url } = (params ?? {}) as { url?: string };
|
|
97
|
+
if (!url || typeof url !== 'string') {
|
|
98
|
+
throw { code: 'invalid_arg', message: 'navigation.openDeepLink 需要 url。', retryable: false };
|
|
99
|
+
}
|
|
100
|
+
// web 只能对标准 http(s) 外链降级;app scheme 深链在浏览器里无对应宿主。
|
|
101
|
+
if (/^https?:\/\//i.test(url)) {
|
|
102
|
+
const win = requireWindow('window.open');
|
|
103
|
+
win.open(url, '_blank', 'noopener,noreferrer');
|
|
104
|
+
return { ok: true, open: true };
|
|
105
|
+
}
|
|
106
|
+
return { ok: true, open: false };
|
|
107
|
+
},
|
|
108
|
+
},
|
|
109
|
+
|
|
110
|
+
'device.registerPresence': {
|
|
111
|
+
async run(params) {
|
|
112
|
+
// 桌面端 native 才做真实的 daemon 侧登记;web 视为无害占位。
|
|
113
|
+
return params ?? { ok: true };
|
|
114
|
+
},
|
|
115
|
+
},
|
|
116
|
+
|
|
117
|
+
'notifications.show': {
|
|
118
|
+
async run(params) {
|
|
119
|
+
const NotificationCtor = (globalWindow() as { Notification?: typeof Notification }).Notification;
|
|
120
|
+
if (!NotificationCtor) {
|
|
121
|
+
throw { code: 'web_unavailable', message: '当前浏览器不支持系统通知。', retryable: false };
|
|
122
|
+
}
|
|
123
|
+
const { title, body, tag } = (params ?? {}) as { title?: string; body?: string; tag?: string };
|
|
124
|
+
if (!title) throw { code: 'invalid_arg', message: 'notifications.show 需要 title。', retryable: false };
|
|
125
|
+
if (NotificationCtor.permission === 'denied') {
|
|
126
|
+
throw { code: 'web_unavailable', message: '系统通知权限被拒绝。', retryable: true };
|
|
127
|
+
}
|
|
128
|
+
if (NotificationCtor.permission === 'default') await NotificationCtor.requestPermission();
|
|
129
|
+
if (NotificationCtor.permission !== 'granted') {
|
|
130
|
+
throw { code: 'web_unavailable', message: '未获得系统通知权限。', retryable: true };
|
|
131
|
+
}
|
|
132
|
+
new NotificationCtor(title, { body, tag });
|
|
133
|
+
return { ok: true };
|
|
134
|
+
},
|
|
135
|
+
},
|
|
136
|
+
|
|
137
|
+
'clipboard.read': {
|
|
138
|
+
async run() {
|
|
139
|
+
const win = requireWindow('navigator.clipboard');
|
|
140
|
+
if (!win.navigator?.clipboard?.readText) {
|
|
141
|
+
throw { code: 'web_unavailable', message: '当前浏览器不支持剪贴板读取。', retryable: false };
|
|
142
|
+
}
|
|
143
|
+
const text = await win.navigator.clipboard.readText();
|
|
144
|
+
return { text };
|
|
145
|
+
},
|
|
146
|
+
},
|
|
147
|
+
|
|
148
|
+
'clipboard.write': {
|
|
149
|
+
async run(params) {
|
|
150
|
+
const win = requireWindow('navigator.clipboard');
|
|
151
|
+
const { text } = (params ?? {}) as { text?: string };
|
|
152
|
+
if (typeof text !== 'string') {
|
|
153
|
+
throw { code: 'invalid_arg', message: 'clipboard.write 需要 text。', retryable: false };
|
|
154
|
+
}
|
|
155
|
+
if (!win.navigator?.clipboard?.writeText) {
|
|
156
|
+
throw { code: 'web_unavailable', message: '当前浏览器不支持剪贴板写入。', retryable: false };
|
|
157
|
+
}
|
|
158
|
+
await win.navigator.clipboard.writeText(text);
|
|
159
|
+
return { ok: true };
|
|
160
|
+
},
|
|
161
|
+
},
|
|
162
|
+
|
|
163
|
+
'dialog.chooseDirectory': {
|
|
164
|
+
async run() {
|
|
165
|
+
const result = await pickWithInput({ webkitdirectory: true });
|
|
166
|
+
return { canceled: result.canceled, paths: result.files.map((f) => f.relativePath ?? f.name) };
|
|
167
|
+
},
|
|
168
|
+
},
|
|
169
|
+
|
|
170
|
+
'files.pick': {
|
|
171
|
+
async run(params) {
|
|
172
|
+
const { multiple } = (params ?? {}) as { multiple?: boolean };
|
|
173
|
+
const result = await pickWithInput({ multiple: Boolean(multiple) });
|
|
174
|
+
return { canceled: result.canceled, files: result.files };
|
|
175
|
+
},
|
|
176
|
+
},
|
|
177
|
+
|
|
178
|
+
'voice.speak': {
|
|
179
|
+
async run(params) {
|
|
180
|
+
const win = requireWindow('speechSynthesis');
|
|
181
|
+
const { text } = (params ?? {}) as { text?: string };
|
|
182
|
+
if (!text) throw { code: 'invalid_arg', message: 'voice.speak 需要 text。', retryable: false };
|
|
183
|
+
if (!win.speechSynthesis) {
|
|
184
|
+
throw { code: 'web_unavailable', message: '当前浏览器不支持语音播报。', retryable: false };
|
|
185
|
+
}
|
|
186
|
+
const UtteranceCtor = (globalWindow() as { SpeechSynthesisUtterance?: typeof SpeechSynthesisUtterance })
|
|
187
|
+
.SpeechSynthesisUtterance;
|
|
188
|
+
if (!UtteranceCtor) {
|
|
189
|
+
throw { code: 'web_unavailable', message: '当前浏览器不支持语音播报。', retryable: false };
|
|
190
|
+
}
|
|
191
|
+
win.speechSynthesis.speak(new UtteranceCtor(text));
|
|
192
|
+
return { ok: true };
|
|
193
|
+
},
|
|
194
|
+
},
|
|
195
|
+
|
|
196
|
+
'voice.stopSpeaking': {
|
|
197
|
+
async run() {
|
|
198
|
+
const win = requireWindow('speechSynthesis');
|
|
199
|
+
win.speechSynthesis?.cancel?.();
|
|
200
|
+
return { ok: true };
|
|
201
|
+
},
|
|
202
|
+
},
|
|
203
|
+
|
|
204
|
+
'voice.listen': {
|
|
205
|
+
async run(params): Promise<VoiceListenResult> {
|
|
206
|
+
interface WebSpeechRecognition {
|
|
207
|
+
lang?: string;
|
|
208
|
+
interimResults: boolean;
|
|
209
|
+
start(): void;
|
|
210
|
+
stop(): void;
|
|
211
|
+
onresult: ((e: { results: ArrayLike<ArrayLike<{ transcript: string }>> }) => void) | null;
|
|
212
|
+
onerror: ((e: { error?: string }) => void) | null;
|
|
213
|
+
onend: (() => void) | null;
|
|
214
|
+
}
|
|
215
|
+
type WebSpeechRecognitionCtor = new () => WebSpeechRecognition;
|
|
216
|
+
const globals = globalWindow() as {
|
|
217
|
+
SpeechRecognition?: WebSpeechRecognitionCtor;
|
|
218
|
+
webkitSpeechRecognition?: WebSpeechRecognitionCtor;
|
|
219
|
+
};
|
|
220
|
+
const RecCtor = globals.SpeechRecognition ?? globals.webkitSpeechRecognition;
|
|
221
|
+
if (!RecCtor) {
|
|
222
|
+
throw { code: 'web_unavailable', message: '当前浏览器不支持语音识别。', retryable: false };
|
|
223
|
+
}
|
|
224
|
+
const { language } = (params ?? {}) as { language?: string };
|
|
225
|
+
const rec = new RecCtor();
|
|
226
|
+
rec.interimResults = false;
|
|
227
|
+
if (language) rec.lang = language;
|
|
228
|
+
activeWebRecognition = rec;
|
|
229
|
+
return new Promise<VoiceListenResult>((resolve, reject) => {
|
|
230
|
+
rec.onresult = (event) => {
|
|
231
|
+
const transcript = Array.from(event.results).map((r) => r[0]?.transcript).join('');
|
|
232
|
+
rec.stop();
|
|
233
|
+
resolve({ transcript, isFinal: true, language });
|
|
234
|
+
};
|
|
235
|
+
rec.onerror = (event) => {
|
|
236
|
+
rec.stop();
|
|
237
|
+
reject({ code: 'web_recognition_error', message: `语音识别失败:${event?.error ?? 'unknown'}`, retryable: true });
|
|
238
|
+
};
|
|
239
|
+
rec.onend = () => {
|
|
240
|
+
if (activeWebRecognition === rec) activeWebRecognition = null;
|
|
241
|
+
resolve({ transcript: '', isFinal: true, language });
|
|
242
|
+
};
|
|
243
|
+
rec.start();
|
|
244
|
+
});
|
|
245
|
+
},
|
|
246
|
+
},
|
|
247
|
+
|
|
248
|
+
'voice.stopListening': {
|
|
249
|
+
async run() {
|
|
250
|
+
const rec = activeWebRecognition;
|
|
251
|
+
activeWebRecognition = null;
|
|
252
|
+
if (rec) {
|
|
253
|
+
try {
|
|
254
|
+
rec.stop();
|
|
255
|
+
} catch {
|
|
256
|
+
// 会话已结束,忽略
|
|
257
|
+
}
|
|
258
|
+
}
|
|
259
|
+
return { ok: true };
|
|
260
|
+
},
|
|
261
|
+
},
|
|
262
|
+
|
|
263
|
+
'share': {
|
|
264
|
+
async run(params) {
|
|
265
|
+
const win = requireWindow('navigator.share');
|
|
266
|
+
if (!win.navigator?.share) {
|
|
267
|
+
throw { code: 'web_unavailable', message: '当前浏览器不支持系统分享。', retryable: false };
|
|
268
|
+
}
|
|
269
|
+
const payload = (params ?? {}) as { title?: string; text?: string; url?: string };
|
|
270
|
+
await win.navigator.share({
|
|
271
|
+
title: payload.title,
|
|
272
|
+
text: payload.text,
|
|
273
|
+
url: payload.url,
|
|
274
|
+
} satisfies ShareData);
|
|
275
|
+
return { ok: true };
|
|
276
|
+
},
|
|
277
|
+
},
|
|
278
|
+
};
|
package/tsconfig.json
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
{
|
|
2
|
+
"compilerOptions": {
|
|
3
|
+
"strict": true,
|
|
4
|
+
"noEmit": true,
|
|
5
|
+
"skipLibCheck": true,
|
|
6
|
+
"module": "ESNext",
|
|
7
|
+
"moduleResolution": "Bundler",
|
|
8
|
+
"target": "ES2020",
|
|
9
|
+
"lib": ["ES2020", "DOM"],
|
|
10
|
+
"types": [],
|
|
11
|
+
"esModuleInterop": true,
|
|
12
|
+
"isolatedModules": true,
|
|
13
|
+
"forceConsistentCasingInFileNames": true
|
|
14
|
+
},
|
|
15
|
+
"include": ["src"]
|
|
16
|
+
}
|
package/README.md
DELETED