@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
package/dist/client.d.ts
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import type { AppthenHostContext, WindowAppthenHost } from './contract';
|
|
2
|
+
/**
|
|
3
|
+
* Web 侧统一客户端:把 `window.appthenHost` 的 request/subscribe 收敛成一组
|
|
4
|
+
* 低样板助手,供 Workbench / 扩展直接调用,避免各自手拼 envelope。
|
|
5
|
+
*
|
|
6
|
+
* 能力解析(host mode):native 宿主存在且当前模式允许 → 走 native;
|
|
7
|
+
* 否则若该能力配了 web 降级且当前模式允许 → 用浏览器原生 API 将就;
|
|
8
|
+
* 两样都没有 → 抛 `host_unavailable` / `web_unavailable`。见 `host-mode.ts`。
|
|
9
|
+
*
|
|
10
|
+
* 用法:
|
|
11
|
+
* ```ts
|
|
12
|
+
* const result = await appthenRequest('daemon.start', { ... });
|
|
13
|
+
* const serverUrl = await appthenRequest<string>('server.getUrl');
|
|
14
|
+
* ```
|
|
15
|
+
*/
|
|
16
|
+
export declare function getAppthenHost(): WindowAppthenHost | null;
|
|
17
|
+
export declare function nextRequestId(): string;
|
|
18
|
+
/**
|
|
19
|
+
* 发一次能力调用。成功 resolve 出 `result`;失败 throw `{ code, message, retryable }`。
|
|
20
|
+
* 宿主不存在(纯浏览器)时 reject,由调用方按「无此能力」处理。
|
|
21
|
+
*/
|
|
22
|
+
export declare function appthenRequest<T = unknown>(method: string, params?: unknown, context?: AppthenHostContext): Promise<T>;
|
|
23
|
+
/** 订阅 native → web 事件,返回取消函数。 */
|
|
24
|
+
export declare function appthenSubscribe<E = unknown>(eventType: string, listener: (payload: E) => void): () => void;
|
package/dist/client.js
ADDED
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.getAppthenHost = getAppthenHost;
|
|
4
|
+
exports.nextRequestId = nextRequestId;
|
|
5
|
+
exports.appthenRequest = appthenRequest;
|
|
6
|
+
exports.appthenSubscribe = appthenSubscribe;
|
|
7
|
+
const host_mode_1 = require("./host-mode");
|
|
8
|
+
const web_providers_1 = require("./web-providers");
|
|
9
|
+
/**
|
|
10
|
+
* Web 侧统一客户端:把 `window.appthenHost` 的 request/subscribe 收敛成一组
|
|
11
|
+
* 低样板助手,供 Workbench / 扩展直接调用,避免各自手拼 envelope。
|
|
12
|
+
*
|
|
13
|
+
* 能力解析(host mode):native 宿主存在且当前模式允许 → 走 native;
|
|
14
|
+
* 否则若该能力配了 web 降级且当前模式允许 → 用浏览器原生 API 将就;
|
|
15
|
+
* 两样都没有 → 抛 `host_unavailable` / `web_unavailable`。见 `host-mode.ts`。
|
|
16
|
+
*
|
|
17
|
+
* 用法:
|
|
18
|
+
* ```ts
|
|
19
|
+
* const result = await appthenRequest('daemon.start', { ... });
|
|
20
|
+
* const serverUrl = await appthenRequest<string>('server.getUrl');
|
|
21
|
+
* ```
|
|
22
|
+
*/
|
|
23
|
+
function getAppthenHost() {
|
|
24
|
+
if (typeof window === 'undefined')
|
|
25
|
+
return null;
|
|
26
|
+
return window.appthenHost ?? null;
|
|
27
|
+
}
|
|
28
|
+
let seqSeed = 0;
|
|
29
|
+
function nextRequestId() {
|
|
30
|
+
seqSeed += 1;
|
|
31
|
+
return `${Date.now().toString(36)}-${seqSeed}`;
|
|
32
|
+
}
|
|
33
|
+
/**
|
|
34
|
+
* 发一次能力调用。成功 resolve 出 `result`;失败 throw `{ code, message, retryable }`。
|
|
35
|
+
* 宿主不存在(纯浏览器)时 reject,由调用方按「无此能力」处理。
|
|
36
|
+
*/
|
|
37
|
+
async function appthenRequest(method, params, context) {
|
|
38
|
+
const ctx = context ?? {};
|
|
39
|
+
const host = getAppthenHost();
|
|
40
|
+
const useNative = Boolean(host) && (0, host_mode_1.hostModeAllowsNative)();
|
|
41
|
+
// 1) native 优先(除非显式 force web)
|
|
42
|
+
if (useNative && host) {
|
|
43
|
+
const req = {
|
|
44
|
+
id: nextRequestId(),
|
|
45
|
+
version: 1,
|
|
46
|
+
method,
|
|
47
|
+
params,
|
|
48
|
+
context: ctx,
|
|
49
|
+
};
|
|
50
|
+
const res = await host.request(req);
|
|
51
|
+
if (!res.ok)
|
|
52
|
+
throw res.error;
|
|
53
|
+
return res.result;
|
|
54
|
+
}
|
|
55
|
+
// 2) native 不可用或被强制关掉 → 查 web 降级
|
|
56
|
+
const provider = web_providers_1.webCapabilityProviders[method];
|
|
57
|
+
if (provider && (0, host_mode_1.hostModeAllowsWebFallback)()) {
|
|
58
|
+
return (await provider.run(params, ctx));
|
|
59
|
+
}
|
|
60
|
+
throw host
|
|
61
|
+
? { code: 'web_disabled', message: `能力「${method}」被强制走 native,但当前终端没有它。`, retryable: false }
|
|
62
|
+
: { code: 'host_unavailable', message: '当前环境没有 appthenHost(非客户端页面),且该能力没有 web 降级。', retryable: false };
|
|
63
|
+
}
|
|
64
|
+
/** 订阅 native → web 事件,返回取消函数。 */
|
|
65
|
+
function appthenSubscribe(eventType, listener) {
|
|
66
|
+
const host = getAppthenHost();
|
|
67
|
+
if (!host)
|
|
68
|
+
return () => undefined;
|
|
69
|
+
return host.subscribe(eventType, (event) => listener(event.payload));
|
|
70
|
+
}
|
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* appthenHost 统一契约(v1)
|
|
3
|
+
*
|
|
4
|
+
* 桌面端(Electron)与移动端(RN WebView)共用这一份契约与 `window.appthenHost`
|
|
5
|
+
* Web 侧入口。业务代码只认这里定义的接口,不关心底层是 Electron IPC 还是 RN
|
|
6
|
+
* `postMessage`;更不要在本模块暴露任何 RN / Electron 专属对象名。
|
|
7
|
+
*/
|
|
8
|
+
export interface AppthenHostContext {
|
|
9
|
+
tenantId?: string;
|
|
10
|
+
projectId?: string;
|
|
11
|
+
workspaceId?: string;
|
|
12
|
+
userGestureId?: string;
|
|
13
|
+
}
|
|
14
|
+
export interface AppthenHostRequest<M extends string = string> {
|
|
15
|
+
id: string;
|
|
16
|
+
version: 1;
|
|
17
|
+
method: M;
|
|
18
|
+
params?: unknown;
|
|
19
|
+
context: AppthenHostContext;
|
|
20
|
+
}
|
|
21
|
+
export interface AppthenHostResponseOk {
|
|
22
|
+
id: string;
|
|
23
|
+
ok: true;
|
|
24
|
+
result: unknown;
|
|
25
|
+
}
|
|
26
|
+
export interface AppthenHostResponseErr {
|
|
27
|
+
id: string;
|
|
28
|
+
ok: false;
|
|
29
|
+
error: {
|
|
30
|
+
code: string;
|
|
31
|
+
message: string;
|
|
32
|
+
retryable?: boolean;
|
|
33
|
+
};
|
|
34
|
+
}
|
|
35
|
+
export type AppthenHostResponse = AppthenHostResponseOk | AppthenHostResponseErr;
|
|
36
|
+
export interface AppthenHostEvent<E = unknown> {
|
|
37
|
+
eventType: string;
|
|
38
|
+
eventId: string;
|
|
39
|
+
correlationId?: string;
|
|
40
|
+
timestamp: number;
|
|
41
|
+
payload: E;
|
|
42
|
+
}
|
|
43
|
+
export type AppthenHostEventListener<E> = (event: AppthenHostEvent<E>) => void;
|
|
44
|
+
/** 握手响应(启动第一件事)。capabilities 由各端按自己实装的能力组动态声明。 */
|
|
45
|
+
export interface AppthenHostHandshakeResponse {
|
|
46
|
+
protocolVersion: 1;
|
|
47
|
+
hostKind: 'ios' | 'android' | 'desktop';
|
|
48
|
+
appVersion: string;
|
|
49
|
+
capabilities: string[];
|
|
50
|
+
deviceId: string;
|
|
51
|
+
}
|
|
52
|
+
/** `voice.speak` 入参:把一段文字播报出来(TTS)。 */
|
|
53
|
+
export interface VoiceSpeakParams {
|
|
54
|
+
text: string;
|
|
55
|
+
/** 可选:语音标识,缺省用系统默认。 */
|
|
56
|
+
voice?: string;
|
|
57
|
+
}
|
|
58
|
+
/** `voice.listen` 入参:开始一段语音听写,识别完返回最终文本(STT)。 */
|
|
59
|
+
export interface VoiceListenParams {
|
|
60
|
+
/** BCP-47 语言,缺省用系统默认。 */
|
|
61
|
+
language?: string;
|
|
62
|
+
}
|
|
63
|
+
/** `voice.listen` 出参。 */
|
|
64
|
+
export interface VoiceListenResult {
|
|
65
|
+
transcript: string;
|
|
66
|
+
isFinal: true;
|
|
67
|
+
language?: string;
|
|
68
|
+
}
|
|
69
|
+
/** `voice.onInterimTranscript` 事件:听写过程中滚动出现的中间结果。 */
|
|
70
|
+
export interface VoiceInterimTranscriptEvent {
|
|
71
|
+
transcript: string;
|
|
72
|
+
}
|
|
73
|
+
/** voice 域对外事件名约定。 */
|
|
74
|
+
export declare const voiceEventTypes: {
|
|
75
|
+
readonly onInterimTranscript: "voice.onInterimTranscript";
|
|
76
|
+
};
|
|
77
|
+
/**
|
|
78
|
+
* 全局 window 上的宿主入口
|
|
79
|
+
*
|
|
80
|
+
* WebView / 网页侧只认这一个接口,不关心宿主是 Electron 还是 RN iOS/Android。
|
|
81
|
+
* 桌面端虽然由 preload 的 contextBridge 注入,移动端由 injectedJavaScript 注入,
|
|
82
|
+
* 但暴露给业务的表面(request / subscribe)完全一致。
|
|
83
|
+
*/
|
|
84
|
+
export interface WindowAppthenHost {
|
|
85
|
+
request(req: AppthenHostRequest): Promise<AppthenHostResponse>;
|
|
86
|
+
subscribe<E>(eventType: string, listener: AppthenHostEventListener<E>): () => void;
|
|
87
|
+
}
|
|
88
|
+
declare global {
|
|
89
|
+
interface Window {
|
|
90
|
+
appthenHost?: WindowAppthenHost;
|
|
91
|
+
}
|
|
92
|
+
}
|
package/dist/contract.js
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* appthenHost 统一契约(v1)
|
|
4
|
+
*
|
|
5
|
+
* 桌面端(Electron)与移动端(RN WebView)共用这一份契约与 `window.appthenHost`
|
|
6
|
+
* Web 侧入口。业务代码只认这里定义的接口,不关心底层是 Electron IPC 还是 RN
|
|
7
|
+
* `postMessage`;更不要在本模块暴露任何 RN / Electron 专属对象名。
|
|
8
|
+
*/
|
|
9
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
10
|
+
exports.voiceEventTypes = void 0;
|
|
11
|
+
/** voice 域对外事件名约定。 */
|
|
12
|
+
exports.voiceEventTypes = {
|
|
13
|
+
onInterimTranscript: 'voice.onInterimTranscript',
|
|
14
|
+
};
|
|
@@ -0,0 +1,152 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* appthenHost 桌面端(Electron)RPC 契约。
|
|
3
|
+
*
|
|
4
|
+
* 这份契约是「SDK 作为两端对齐依据」的桌面侧部分:桌面 main 进程按这里的
|
|
5
|
+
* 方法名注册能力,桌面 Console 与 Workbench / 扩展用 `desktopClient` 调用,
|
|
6
|
+
* 移动端不注册这些方法(请求时得到 `method_not_found` / `host_unavailable`),
|
|
7
|
+
* 因此移动端页面通过 `hasHostCapability` / 握手 capabilities 感知宿主能力差异。
|
|
8
|
+
*
|
|
9
|
+
* 事件名 / 方法名 / DTO 都以本模块为准,别在别处再造一套。
|
|
10
|
+
*/
|
|
11
|
+
export type DaemonConnectionState = 'idle' | 'starting' | 'connecting' | 'connected' | 'disconnected' | 'errored';
|
|
12
|
+
export interface DaemonStatus {
|
|
13
|
+
state: 'stopped' | 'starting' | 'running' | 'errored';
|
|
14
|
+
connection: DaemonConnectionState;
|
|
15
|
+
pid: number | null;
|
|
16
|
+
command: string | null;
|
|
17
|
+
error: string | null;
|
|
18
|
+
exitCode: number | null;
|
|
19
|
+
logPath: string | null;
|
|
20
|
+
lastMessage: string | null;
|
|
21
|
+
recentLogs: string[];
|
|
22
|
+
startedAt: string | null;
|
|
23
|
+
connectedAt: string | null;
|
|
24
|
+
nodeId: string | null;
|
|
25
|
+
projectId?: string;
|
|
26
|
+
workspaceId?: string;
|
|
27
|
+
workspaceName?: string;
|
|
28
|
+
cwd?: string;
|
|
29
|
+
serverUrl?: string;
|
|
30
|
+
recentEvents?: DaemonLogEntry[];
|
|
31
|
+
}
|
|
32
|
+
export type DaemonLogCategory = 'connection' | 'message' | 'task' | 'error' | 'process' | 'log';
|
|
33
|
+
export interface DaemonLogEntry {
|
|
34
|
+
id: string;
|
|
35
|
+
timestamp: string;
|
|
36
|
+
source: 'stdout' | 'stderr' | 'desktop';
|
|
37
|
+
level: 'info' | 'warn' | 'error';
|
|
38
|
+
category: DaemonLogCategory;
|
|
39
|
+
message: string;
|
|
40
|
+
}
|
|
41
|
+
/** 定位某个 daemon 的最小维度(`daemon.status` / `daemon.stop` / `daemon.logs` 入参)。 */
|
|
42
|
+
export interface DaemonTarget {
|
|
43
|
+
projectId?: string;
|
|
44
|
+
workspaceId?: string;
|
|
45
|
+
cwd?: string;
|
|
46
|
+
}
|
|
47
|
+
export type DaemonStartReason = 'manual_start' | 'manual_restart' | 'environment_created' | 'conversation_ensure' | 'runtime_ensure';
|
|
48
|
+
/** `daemon.start` / `daemon.restart` 入参。 */
|
|
49
|
+
export interface DaemonStartParams extends DaemonTarget {
|
|
50
|
+
serverUrl: string;
|
|
51
|
+
projectId: string;
|
|
52
|
+
token: string;
|
|
53
|
+
workspaceId?: string;
|
|
54
|
+
workspaceName?: string;
|
|
55
|
+
cwd?: string;
|
|
56
|
+
reason?: DaemonStartReason;
|
|
57
|
+
}
|
|
58
|
+
/** `dialog.chooseDirectory` 出参。 */
|
|
59
|
+
export interface DirectoryPickerResult {
|
|
60
|
+
canceled: boolean;
|
|
61
|
+
path?: string;
|
|
62
|
+
}
|
|
63
|
+
/** 桌面端稳定的设备身份(`device.identity` 出参)。 */
|
|
64
|
+
export interface DesktopDeviceIdentity {
|
|
65
|
+
nodeId: string;
|
|
66
|
+
machineId: string;
|
|
67
|
+
}
|
|
68
|
+
/** `notifications.show` / `events.publish` 入参。 */
|
|
69
|
+
export interface DesktopNotification {
|
|
70
|
+
title: string;
|
|
71
|
+
body?: string;
|
|
72
|
+
route?: string;
|
|
73
|
+
tag?: string;
|
|
74
|
+
}
|
|
75
|
+
/** `updater.*` 状态(`updater:state` 事件载荷)。 */
|
|
76
|
+
export interface UpdateState {
|
|
77
|
+
status: 'idle' | 'checking' | 'available' | 'downloading' | 'downloaded' | 'not-available' | 'error';
|
|
78
|
+
version?: string;
|
|
79
|
+
percent?: number;
|
|
80
|
+
message?: string;
|
|
81
|
+
}
|
|
82
|
+
export declare const desktopRpcMethods: {
|
|
83
|
+
readonly appVersion: "app.version";
|
|
84
|
+
readonly serverGetUrl: "server.getUrl";
|
|
85
|
+
readonly dialogChooseDirectory: "dialog.chooseDirectory";
|
|
86
|
+
readonly daemonStatus: "daemon.status";
|
|
87
|
+
readonly daemonStatusAll: "daemon.statusAll";
|
|
88
|
+
readonly daemonStart: "daemon.start";
|
|
89
|
+
readonly daemonStop: "daemon.stop";
|
|
90
|
+
readonly daemonRestart: "daemon.restart";
|
|
91
|
+
readonly daemonLogs: "daemon.logs";
|
|
92
|
+
readonly logsOpenDirectory: "logs.openDirectory";
|
|
93
|
+
readonly notificationsShow: "notifications.show";
|
|
94
|
+
readonly eventsPublish: "events.publish";
|
|
95
|
+
readonly updaterCheck: "updater.check";
|
|
96
|
+
readonly updaterDownload: "updater.download";
|
|
97
|
+
readonly updaterInstall: "updater.install";
|
|
98
|
+
readonly navigationOpenExternal: "navigation.openExternal";
|
|
99
|
+
};
|
|
100
|
+
export declare const desktopRpcEvents: {
|
|
101
|
+
readonly daemonStatus: "daemon:status";
|
|
102
|
+
readonly notification: "events:notification";
|
|
103
|
+
readonly updaterState: "updater:state";
|
|
104
|
+
};
|
|
105
|
+
/** SDK 声明的桌面能力清单(握手 capabilities 下发,供页面探测宿主能力差异)。 */
|
|
106
|
+
export declare const desktopHostCapabilities: readonly ["app.version", "server.getUrl", "dialog.chooseDirectory", "daemon.status", "daemon.statusAll", "daemon.start", "daemon.stop", "daemon.restart", "daemon.logs", "logs.openDirectory", "notifications.show", "events.publish", "updater.check", "updater.download", "updater.install", "navigation.openExternal"];
|
|
107
|
+
export type DesktopRpcMethod = (typeof desktopHostCapabilities)[number];
|
|
108
|
+
/**
|
|
109
|
+
* 类型化桌面客户端:Console / Workbench / 扩展统一从这里调用,替代旧
|
|
110
|
+
* `window.appthenDesktop`。底层走 `window.appthenHost.request / subscribe`,
|
|
111
|
+
* 纯浏览器无宿主时按「无此能力」处理(thrown `host_unavailable`)。
|
|
112
|
+
*/
|
|
113
|
+
export declare const desktopClient: {
|
|
114
|
+
getVersion: () => Promise<string>;
|
|
115
|
+
server: {
|
|
116
|
+
getUrl: () => Promise<string>;
|
|
117
|
+
};
|
|
118
|
+
device: {
|
|
119
|
+
identity: () => Promise<DesktopDeviceIdentity>;
|
|
120
|
+
};
|
|
121
|
+
dialog: {
|
|
122
|
+
chooseDirectory: () => Promise<DirectoryPickerResult>;
|
|
123
|
+
};
|
|
124
|
+
daemon: {
|
|
125
|
+
status: (target?: DaemonTarget) => Promise<DaemonStatus>;
|
|
126
|
+
statusAll: () => Promise<DaemonStatus[]>;
|
|
127
|
+
start: (config: DaemonStartParams) => Promise<DaemonStatus>;
|
|
128
|
+
stop: (target?: DaemonTarget) => Promise<DaemonStatus>;
|
|
129
|
+
restart: (config: DaemonStartParams) => Promise<DaemonStatus>;
|
|
130
|
+
logs: (target: DaemonTarget, limit?: number) => Promise<string[]>;
|
|
131
|
+
/** 订阅 daemon 状态事件,返回取消函数。 */
|
|
132
|
+
onStatus: (listener: (status: DaemonStatus) => void) => (() => void);
|
|
133
|
+
};
|
|
134
|
+
logs: {
|
|
135
|
+
openDirectory: () => Promise<string>;
|
|
136
|
+
};
|
|
137
|
+
notifications: {
|
|
138
|
+
show: (payload: DesktopNotification) => Promise<void>;
|
|
139
|
+
};
|
|
140
|
+
events: {
|
|
141
|
+
publish: (payload: DesktopNotification) => Promise<void>;
|
|
142
|
+
/** 订阅应用内事件通知,返回取消函数。 */
|
|
143
|
+
onNotification: (listener: (payload: DesktopNotification) => void) => (() => void);
|
|
144
|
+
};
|
|
145
|
+
updater: {
|
|
146
|
+
check: () => Promise<void>;
|
|
147
|
+
download: () => Promise<void>;
|
|
148
|
+
install: () => Promise<void>;
|
|
149
|
+
/** 订阅更新状态,返回取消函数。 */
|
|
150
|
+
onState: (listener: (state: UpdateState) => void) => (() => void);
|
|
151
|
+
};
|
|
152
|
+
};
|
package/dist/desktop.js
ADDED
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* appthenHost 桌面端(Electron)RPC 契约。
|
|
4
|
+
*
|
|
5
|
+
* 这份契约是「SDK 作为两端对齐依据」的桌面侧部分:桌面 main 进程按这里的
|
|
6
|
+
* 方法名注册能力,桌面 Console 与 Workbench / 扩展用 `desktopClient` 调用,
|
|
7
|
+
* 移动端不注册这些方法(请求时得到 `method_not_found` / `host_unavailable`),
|
|
8
|
+
* 因此移动端页面通过 `hasHostCapability` / 握手 capabilities 感知宿主能力差异。
|
|
9
|
+
*
|
|
10
|
+
* 事件名 / 方法名 / DTO 都以本模块为准,别在别处再造一套。
|
|
11
|
+
*/
|
|
12
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
13
|
+
exports.desktopClient = exports.desktopHostCapabilities = exports.desktopRpcEvents = exports.desktopRpcMethods = void 0;
|
|
14
|
+
const client_1 = require("./client");
|
|
15
|
+
// ---- 方法名 / 事件名约定(一份,别在别处再造) ----
|
|
16
|
+
exports.desktopRpcMethods = {
|
|
17
|
+
appVersion: 'app.version',
|
|
18
|
+
serverGetUrl: 'server.getUrl',
|
|
19
|
+
dialogChooseDirectory: 'dialog.chooseDirectory',
|
|
20
|
+
daemonStatus: 'daemon.status',
|
|
21
|
+
daemonStatusAll: 'daemon.statusAll',
|
|
22
|
+
daemonStart: 'daemon.start',
|
|
23
|
+
daemonStop: 'daemon.stop',
|
|
24
|
+
daemonRestart: 'daemon.restart',
|
|
25
|
+
daemonLogs: 'daemon.logs',
|
|
26
|
+
logsOpenDirectory: 'logs.openDirectory',
|
|
27
|
+
notificationsShow: 'notifications.show',
|
|
28
|
+
eventsPublish: 'events.publish',
|
|
29
|
+
updaterCheck: 'updater.check',
|
|
30
|
+
updaterDownload: 'updater.download',
|
|
31
|
+
updaterInstall: 'updater.install',
|
|
32
|
+
navigationOpenExternal: 'navigation.openExternal',
|
|
33
|
+
};
|
|
34
|
+
exports.desktopRpcEvents = {
|
|
35
|
+
daemonStatus: 'daemon:status',
|
|
36
|
+
notification: 'events:notification',
|
|
37
|
+
updaterState: 'updater:state',
|
|
38
|
+
};
|
|
39
|
+
/** SDK 声明的桌面能力清单(握手 capabilities 下发,供页面探测宿主能力差异)。 */
|
|
40
|
+
exports.desktopHostCapabilities = [
|
|
41
|
+
exports.desktopRpcMethods.appVersion,
|
|
42
|
+
exports.desktopRpcMethods.serverGetUrl,
|
|
43
|
+
exports.desktopRpcMethods.dialogChooseDirectory,
|
|
44
|
+
exports.desktopRpcMethods.daemonStatus,
|
|
45
|
+
exports.desktopRpcMethods.daemonStatusAll,
|
|
46
|
+
exports.desktopRpcMethods.daemonStart,
|
|
47
|
+
exports.desktopRpcMethods.daemonStop,
|
|
48
|
+
exports.desktopRpcMethods.daemonRestart,
|
|
49
|
+
exports.desktopRpcMethods.daemonLogs,
|
|
50
|
+
exports.desktopRpcMethods.logsOpenDirectory,
|
|
51
|
+
exports.desktopRpcMethods.notificationsShow,
|
|
52
|
+
exports.desktopRpcMethods.eventsPublish,
|
|
53
|
+
exports.desktopRpcMethods.updaterCheck,
|
|
54
|
+
exports.desktopRpcMethods.updaterDownload,
|
|
55
|
+
exports.desktopRpcMethods.updaterInstall,
|
|
56
|
+
exports.desktopRpcMethods.navigationOpenExternal,
|
|
57
|
+
];
|
|
58
|
+
/**
|
|
59
|
+
* 类型化桌面客户端:Console / Workbench / 扩展统一从这里调用,替代旧
|
|
60
|
+
* `window.appthenDesktop`。底层走 `window.appthenHost.request / subscribe`,
|
|
61
|
+
* 纯浏览器无宿主时按「无此能力」处理(thrown `host_unavailable`)。
|
|
62
|
+
*/
|
|
63
|
+
exports.desktopClient = {
|
|
64
|
+
getVersion: () => (0, client_1.appthenRequest)(exports.desktopRpcMethods.appVersion),
|
|
65
|
+
server: {
|
|
66
|
+
getUrl: () => (0, client_1.appthenRequest)(exports.desktopRpcMethods.serverGetUrl),
|
|
67
|
+
},
|
|
68
|
+
device: {
|
|
69
|
+
identity: () => (0, client_1.appthenRequest)('device.identity'),
|
|
70
|
+
},
|
|
71
|
+
dialog: {
|
|
72
|
+
chooseDirectory: () => (0, client_1.appthenRequest)(exports.desktopRpcMethods.dialogChooseDirectory),
|
|
73
|
+
},
|
|
74
|
+
daemon: {
|
|
75
|
+
status: (target) => (0, client_1.appthenRequest)(exports.desktopRpcMethods.daemonStatus, target),
|
|
76
|
+
statusAll: () => (0, client_1.appthenRequest)(exports.desktopRpcMethods.daemonStatusAll),
|
|
77
|
+
start: (config) => (0, client_1.appthenRequest)(exports.desktopRpcMethods.daemonStart, config),
|
|
78
|
+
stop: (target) => (0, client_1.appthenRequest)(exports.desktopRpcMethods.daemonStop, target),
|
|
79
|
+
restart: (config) => (0, client_1.appthenRequest)(exports.desktopRpcMethods.daemonRestart, config),
|
|
80
|
+
logs: (target, limit) => (0, client_1.appthenRequest)(exports.desktopRpcMethods.daemonLogs, { target, limit }),
|
|
81
|
+
/** 订阅 daemon 状态事件,返回取消函数。 */
|
|
82
|
+
onStatus: (listener) => (0, client_1.appthenSubscribe)(exports.desktopRpcEvents.daemonStatus, listener),
|
|
83
|
+
},
|
|
84
|
+
logs: {
|
|
85
|
+
openDirectory: () => (0, client_1.appthenRequest)(exports.desktopRpcMethods.logsOpenDirectory),
|
|
86
|
+
},
|
|
87
|
+
notifications: {
|
|
88
|
+
show: (payload) => (0, client_1.appthenRequest)(exports.desktopRpcMethods.notificationsShow, payload),
|
|
89
|
+
},
|
|
90
|
+
events: {
|
|
91
|
+
publish: (payload) => (0, client_1.appthenRequest)(exports.desktopRpcMethods.eventsPublish, payload),
|
|
92
|
+
/** 订阅应用内事件通知,返回取消函数。 */
|
|
93
|
+
onNotification: (listener) => (0, client_1.appthenSubscribe)(exports.desktopRpcEvents.notification, listener),
|
|
94
|
+
},
|
|
95
|
+
updater: {
|
|
96
|
+
check: () => (0, client_1.appthenRequest)(exports.desktopRpcMethods.updaterCheck),
|
|
97
|
+
download: () => (0, client_1.appthenRequest)(exports.desktopRpcMethods.updaterDownload),
|
|
98
|
+
install: () => (0, client_1.appthenRequest)(exports.desktopRpcMethods.updaterInstall),
|
|
99
|
+
/** 订阅更新状态,返回取消函数。 */
|
|
100
|
+
onState: (listener) => (0, client_1.appthenSubscribe)(exports.desktopRpcEvents.updaterState, listener),
|
|
101
|
+
},
|
|
102
|
+
};
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
import type { AppthenHostContext } from './contract';
|
|
2
|
+
/**
|
|
3
|
+
* Native 侧可以回写给 Web 的通道。
|
|
4
|
+
* - 移动端:App 用 WebView ref 的 `injectJavaScript`
|
|
5
|
+
* - 桌面端:main 进程用 `webContents.executeJavaScript`
|
|
6
|
+
*/
|
|
7
|
+
export interface WebViewHostSink {
|
|
8
|
+
injectJavaScript(js: string): void;
|
|
9
|
+
}
|
|
10
|
+
export type MethodHandler = (params: unknown, context: AppthenHostContext) => unknown | Promise<unknown>;
|
|
11
|
+
export interface AppthenHostOptions {
|
|
12
|
+
hostKind: 'ios' | 'android' | 'desktop';
|
|
13
|
+
appVersion: string;
|
|
14
|
+
/** 稳定设备 id。移动端用 native 生成,桌面端用自己的 identity —— SDK 不负责生成。 */
|
|
15
|
+
deviceId: string;
|
|
16
|
+
/** 本端实装并对外声明的能力(handshake 下发)。 */
|
|
17
|
+
capabilities: string[];
|
|
18
|
+
/** 契约已声明、但 native 尚未接的能力:返回明确「未可用」诊断而非静默成功。 */
|
|
19
|
+
notYetImplemented: string[];
|
|
20
|
+
}
|
|
21
|
+
/**
|
|
22
|
+
* 宿主 bridge 的统一分发逻辑(桌面端 / 移动端共用一份)。
|
|
23
|
+
*
|
|
24
|
+
* - 解释 `appthen:request` 来信、按 method 路由到能力、把响应回写
|
|
25
|
+
* - 只做「能力分派」与「授权边界」,不持有会话 / 模型 / Workspace 状态
|
|
26
|
+
* - 能力由个端通过 `registerMethod` 挂入;`host.*` / `device.identity` 默认方法内建
|
|
27
|
+
* - `notYetImplemented` 里的能力返回明确诊断
|
|
28
|
+
*/
|
|
29
|
+
export declare class AppthenHost {
|
|
30
|
+
private readonly hostKind;
|
|
31
|
+
private readonly appVersion;
|
|
32
|
+
private readonly deviceId;
|
|
33
|
+
private readonly capabilities;
|
|
34
|
+
private sink;
|
|
35
|
+
private readonly handlers;
|
|
36
|
+
constructor(options: AppthenHostOptions);
|
|
37
|
+
/** 绑定回写 sink(App / main 进程在拿到 WebView/WebContents 后调用)。 */
|
|
38
|
+
attach(sink: WebViewHostSink): void;
|
|
39
|
+
/** 供各端能力注册专用方法(调用方自行保证 method 在 capabilities 里)。 */
|
|
40
|
+
registerMethod(method: string, handler: MethodHandler): void;
|
|
41
|
+
/**
|
|
42
|
+
* 处理收到的原文消息。
|
|
43
|
+
* 移动端:`react-native-webview` onMessage 的 `event.nativeEvent.data`。
|
|
44
|
+
* 桌面端:preload `ipcRenderer.send('appthen:toMain', raw)` 的 raw。
|
|
45
|
+
*/
|
|
46
|
+
handleIncoming(raw: string): void;
|
|
47
|
+
/**
|
|
48
|
+
* Native → Web 事件(通知点击、语音转写/播报、daemon 状态等)。
|
|
49
|
+
* 业务用 `window.appthenHost.subscribe(eventType, listener)` 订阅。
|
|
50
|
+
*/
|
|
51
|
+
emit(eventType: string, payload: unknown, correlationId?: string): void;
|
|
52
|
+
private registerDefaults;
|
|
53
|
+
private makeHandshake;
|
|
54
|
+
private handleRequest;
|
|
55
|
+
}
|
|
@@ -0,0 +1,121 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.AppthenHost = void 0;
|
|
4
|
+
const mkError = (id, code, message, retryable = false) => ({
|
|
5
|
+
id,
|
|
6
|
+
ok: false,
|
|
7
|
+
error: { code, message, retryable },
|
|
8
|
+
});
|
|
9
|
+
const mkOk = (id, result) => ({ id, ok: true, result });
|
|
10
|
+
/**
|
|
11
|
+
* 宿主 bridge 的统一分发逻辑(桌面端 / 移动端共用一份)。
|
|
12
|
+
*
|
|
13
|
+
* - 解释 `appthen:request` 来信、按 method 路由到能力、把响应回写
|
|
14
|
+
* - 只做「能力分派」与「授权边界」,不持有会话 / 模型 / Workspace 状态
|
|
15
|
+
* - 能力由个端通过 `registerMethod` 挂入;`host.*` / `device.identity` 默认方法内建
|
|
16
|
+
* - `notYetImplemented` 里的能力返回明确诊断
|
|
17
|
+
*/
|
|
18
|
+
class AppthenHost {
|
|
19
|
+
constructor(options) {
|
|
20
|
+
this.sink = null;
|
|
21
|
+
this.handlers = new Map();
|
|
22
|
+
this.hostKind = options.hostKind;
|
|
23
|
+
this.appVersion = options.appVersion;
|
|
24
|
+
this.deviceId = options.deviceId;
|
|
25
|
+
this.capabilities = [...options.capabilities];
|
|
26
|
+
this.registerDefaults(options.notYetImplemented);
|
|
27
|
+
}
|
|
28
|
+
/** 绑定回写 sink(App / main 进程在拿到 WebView/WebContents 后调用)。 */
|
|
29
|
+
attach(sink) {
|
|
30
|
+
this.sink = sink;
|
|
31
|
+
}
|
|
32
|
+
/** 供各端能力注册专用方法(调用方自行保证 method 在 capabilities 里)。 */
|
|
33
|
+
registerMethod(method, handler) {
|
|
34
|
+
this.handlers.set(method, handler);
|
|
35
|
+
}
|
|
36
|
+
/**
|
|
37
|
+
* 处理收到的原文消息。
|
|
38
|
+
* 移动端:`react-native-webview` onMessage 的 `event.nativeEvent.data`。
|
|
39
|
+
* 桌面端:preload `ipcRenderer.send('appthen:toMain', raw)` 的 raw。
|
|
40
|
+
*/
|
|
41
|
+
handleIncoming(raw) {
|
|
42
|
+
let payload;
|
|
43
|
+
try {
|
|
44
|
+
payload = JSON.parse(raw);
|
|
45
|
+
}
|
|
46
|
+
catch {
|
|
47
|
+
return; // 非 JSON / 注入之外的消息,忽略
|
|
48
|
+
}
|
|
49
|
+
if (payload?.type !== 'appthen:request' || !payload.req)
|
|
50
|
+
return;
|
|
51
|
+
void this.handleRequest(payload.req);
|
|
52
|
+
}
|
|
53
|
+
/**
|
|
54
|
+
* Native → Web 事件(通知点击、语音转写/播报、daemon 状态等)。
|
|
55
|
+
* 业务用 `window.appthenHost.subscribe(eventType, listener)` 订阅。
|
|
56
|
+
*/
|
|
57
|
+
emit(eventType, payload, correlationId) {
|
|
58
|
+
if (!this.sink)
|
|
59
|
+
return;
|
|
60
|
+
const event = {
|
|
61
|
+
eventType,
|
|
62
|
+
eventId: Math.random().toString(36).slice(2),
|
|
63
|
+
correlationId,
|
|
64
|
+
timestamp: Date.now(),
|
|
65
|
+
payload,
|
|
66
|
+
};
|
|
67
|
+
this.sink.injectJavaScript(`window.appthenHost && window.appthenHost.__dispatchEvent(${JSON.stringify(event)});`);
|
|
68
|
+
}
|
|
69
|
+
registerDefaults(notYetImplemented) {
|
|
70
|
+
this.handlers.set('host.handshake', () => this.makeHandshake());
|
|
71
|
+
this.handlers.set('host.info', () => ({
|
|
72
|
+
hostKind: this.hostKind,
|
|
73
|
+
appVersion: this.appVersion,
|
|
74
|
+
deviceId: this.deviceId,
|
|
75
|
+
capabilities: this.capabilities,
|
|
76
|
+
}));
|
|
77
|
+
this.handlers.set('device.identity', () => ({ deviceId: this.deviceId }));
|
|
78
|
+
this.handlers.set('device.registerPresence', (params) => params ?? { ok: true });
|
|
79
|
+
for (const method of notYetImplemented) {
|
|
80
|
+
this.handlers.set(method, () => {
|
|
81
|
+
throw {
|
|
82
|
+
code: 'not_yet_implemented',
|
|
83
|
+
message: `能力「${method}」的原生模块尚未接入(协同工作台后续版本提供)。`,
|
|
84
|
+
retryable: false,
|
|
85
|
+
};
|
|
86
|
+
});
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
makeHandshake() {
|
|
90
|
+
return {
|
|
91
|
+
protocolVersion: 1,
|
|
92
|
+
hostKind: this.hostKind,
|
|
93
|
+
appVersion: this.appVersion,
|
|
94
|
+
capabilities: [...this.capabilities],
|
|
95
|
+
deviceId: this.deviceId,
|
|
96
|
+
};
|
|
97
|
+
}
|
|
98
|
+
async handleRequest(req) {
|
|
99
|
+
const respond = (resp) => {
|
|
100
|
+
this.sink?.injectJavaScript(`window.appthenHost && window.appthenHost.__dispatchResponse(${JSON.stringify(resp)});`);
|
|
101
|
+
};
|
|
102
|
+
if (req.version !== 1) {
|
|
103
|
+
respond(mkError(req.id, 'unsupported_protocol', '不支持的协议版本。'));
|
|
104
|
+
return;
|
|
105
|
+
}
|
|
106
|
+
const handler = this.handlers.get(req.method);
|
|
107
|
+
if (!handler) {
|
|
108
|
+
respond(mkError(req.id, 'method_not_found', `未知方法 ${req.method}。`));
|
|
109
|
+
return;
|
|
110
|
+
}
|
|
111
|
+
try {
|
|
112
|
+
const result = await handler(req.params, req.context);
|
|
113
|
+
respond(mkOk(req.id, result));
|
|
114
|
+
}
|
|
115
|
+
catch (err) {
|
|
116
|
+
const e = (err ?? {});
|
|
117
|
+
respond(mkError(req.id, e.code ?? 'internal_error', e.message ?? '能力执行失败。', Boolean(e.retryable)));
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
exports.AppthenHost = AppthenHost;
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 主机模式(host mode):决定一次能力调用「优先走 native 宿主还是 web 降级」。
|
|
3
|
+
*
|
|
4
|
+
* 背景:同一份 `appthenHost` 契约,桌面端(Electron)+ 移动端(RN WebView)由
|
|
5
|
+
* native 宿主提供能力;纯浏览器没有 `window.appthenHost`,但部分能力(剪贴板、
|
|
6
|
+
* 通知、外链、TTS/STT、选目录等)可以用浏览器原生 API「将就着用」。
|
|
7
|
+
* 这份模块统一这套切换逻辑,避免各个消费点自己判 `window.appthenHost`。
|
|
8
|
+
*
|
|
9
|
+
* 优先级(高 → 低):
|
|
10
|
+
* 1. 运行时 `setHostMode(mode)` 覆盖;
|
|
11
|
+
* 2. 环境变量 `APPTHEN_HOST_MODE`(Node):取值 auto | native | web;
|
|
12
|
+
* 3. `globalThis.APPTHEN_HOST_MODE`(Vite 可用 `define` 注入 / 页面自设);
|
|
13
|
+
* 4. 默认 `'auto'`。
|
|
14
|
+
*
|
|
15
|
+
* Vite 的 `VITE_` 变量不会进入预编译的 CommonJS dist,web 侧如有需要请用
|
|
16
|
+
* `define` 注入 `globalThis.APPTHEN_HOST_MODE`,或直接调用 `setHostMode`。
|
|
17
|
+
*/
|
|
18
|
+
export type HostMode = 'auto' | 'native' | 'web';
|
|
19
|
+
/** 运行时强制覆盖主机模式(例如在桌面里临时切到 web 路径做回归测试)。 */
|
|
20
|
+
export declare function setHostMode(mode: HostMode): void;
|
|
21
|
+
/** 当前生效的主机模式(调用时读取,允许运行时切换)。 */
|
|
22
|
+
export declare function getHostMode(): HostMode;
|
|
23
|
+
/** web 降级是否在该模式下可用(`native` 模式返回 false)。 */
|
|
24
|
+
export declare function hostModeAllowsWebFallback(): boolean;
|
|
25
|
+
/** 当前是否允许优先使用 native 宿主(`web` 模式返回 false)。 */
|
|
26
|
+
export declare function hostModeAllowsNative(): boolean;
|