@raydiculousx/ts-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/CHANGELOG.md +7 -0
- package/LICENSE +55 -0
- package/NOTICE +14 -0
- package/README.md +91 -3
- package/dist/THIRD_PARTY_NOTICES.txt +466 -0
- package/dist/host/src/bootstrap/client.d.ts +37 -0
- package/dist/host/src/bootstrap/client.js +215 -0
- package/dist/host/src/bootstrap/host.d.ts +79 -0
- package/dist/host/src/bootstrap/host.js +214 -0
- package/dist/host/src/bootstrap/protocol.d.ts +20 -0
- package/dist/host/src/bootstrap/protocol.js +72 -0
- package/dist/host/src/dev.d.ts +30 -0
- package/dist/host/src/dev.js +20 -0
- package/dist/host/src/entry/app.d.ts +10 -0
- package/dist/host/src/entry/app.js +9 -0
- package/dist/host/src/entry/auto.d.ts +16 -0
- package/dist/host/src/entry/auto.js +18747 -0
- package/dist/host/src/standalone/account-api.d.ts +32 -0
- package/dist/host/src/standalone/account-api.js +176 -0
- package/dist/host/src/standalone/connection-slot.d.ts +12 -0
- package/dist/host/src/standalone/connection-slot.js +28 -0
- package/dist/host/src/standalone/device-picker.d.ts +6 -0
- package/dist/host/src/standalone/device-picker.js +19 -0
- package/dist/host/src/standalone/locale.d.ts +4 -0
- package/dist/host/src/standalone/locale.js +35 -0
- package/dist/host/src/standalone/mount-host.d.ts +155 -0
- package/dist/host/src/standalone/mount-host.js +396 -0
- package/dist/host/src/standalone/mount-surface.d.ts +12 -0
- package/dist/host/src/standalone/mount-surface.js +69 -0
- package/dist/host/src/standalone/robot-setup.d.ts +61 -0
- package/dist/host/src/standalone/robot-setup.js +160 -0
- package/dist/lib/client/action-ownership.d.ts +31 -0
- package/dist/lib/client/action-ownership.js +182 -0
- package/dist/lib/client/direct.d.ts +149 -0
- package/dist/lib/client/direct.js +560 -0
- package/dist/lib/client/raydiculousx.d.ts +541 -0
- package/dist/lib/client/raydiculousx.js +694 -0
- package/dist/lib/client/runtime.d.ts +165 -0
- package/dist/lib/client/runtime.js +1 -0
- package/dist/lib/core/index.d.ts +16 -0
- package/dist/lib/core/index.js +9 -0
- package/dist/lib/core/rpc/command-channel.d.ts +345 -0
- package/dist/lib/core/rpc/command-channel.js +697 -0
- package/dist/lib/core/rpc/commands.d.ts +354 -0
- package/dist/lib/core/rpc/commands.js +134 -0
- package/dist/lib/core/rpc/request-policy.d.ts +29 -0
- package/dist/lib/core/rpc/request-policy.js +35 -0
- package/dist/lib/core/rpc/robot-api.d.ts +184 -0
- package/dist/lib/core/rpc/robot-api.js +34 -0
- package/dist/lib/core/signaling/ice-servers.d.ts +10 -0
- package/dist/lib/core/signaling/ice-servers.js +58 -0
- package/dist/lib/core/signaling/peer-negotiator.d.ts +164 -0
- package/dist/lib/core/signaling/peer-negotiator.js +366 -0
- package/dist/lib/core/signaling/signaling-client.d.ts +204 -0
- package/dist/lib/core/signaling/signaling-client.js +473 -0
- package/dist/lib/core/signaling/signaling-contract.d.ts +164 -0
- package/dist/lib/core/signaling/signaling-contract.js +35 -0
- package/dist/lib/core/signaling/signaling.d.ts +172 -0
- package/dist/lib/core/signaling/signaling.js +381 -0
- package/dist/lib/media/attach-video.d.ts +3 -0
- package/dist/lib/media/attach-video.js +243 -0
- package/dist/lib/media/direct-audio.d.ts +36 -0
- package/dist/lib/media/direct-audio.js +94 -0
- package/dist/lib/media/index.d.ts +5 -0
- package/dist/lib/media/index.js +3 -0
- package/dist/lib/media/pcm-types.d.ts +33 -0
- package/dist/lib/media/pcm-types.js +1 -0
- package/dist/lib/media/robot-audio.d.ts +64 -0
- package/dist/lib/media/robot-audio.js +82 -0
- package/dist/lib/media/robot-pcm-output.d.ts +16 -0
- package/dist/lib/media/robot-pcm-output.js +101 -0
- package/dist/lib/robot/action-upload.d.ts +32 -0
- package/dist/lib/robot/action-upload.js +143 -0
- package/dist/lib/robot/robot-commands.d.ts +148 -0
- package/dist/lib/robot/robot-commands.js +164 -0
- package/dist/lib/robot/robot-responses.d.ts +42 -0
- package/dist/lib/robot/robot-responses.js +226 -0
- package/dist/lib/robot/robot-tof.d.ts +61 -0
- package/dist/lib/robot/robot-tof.js +25 -0
- package/dist/lib/robot/robot-types.d.ts +293 -0
- package/dist/lib/robot/robot-types.js +17 -0
- package/dist/lib/shared/errors.d.ts +57 -0
- package/dist/lib/shared/errors.js +45 -0
- package/dist/lib/shared/events.d.ts +11 -0
- package/dist/lib/shared/events.js +40 -0
- package/dist/lib/shared/types.d.ts +26 -0
- package/dist/lib/shared/types.js +1 -0
- package/dist/lib/shared/validation.d.ts +29 -0
- package/dist/lib/shared/validation.js +87 -0
- package/dist/lib/version.d.ts +2 -0
- package/dist/lib/version.js +3 -0
- package/dist/raydiculousx-sdk.d.ts +26 -0
- package/dist/raydiculousx-sdk.js +17 -0
- package/docs/API.md +1106 -0
- package/docs/DIRECT_APPS.md +83 -0
- package/docs/HOST_HANDOFF.md +59 -0
- package/docs/HOST_PROTOCOL.md +43 -0
- package/docs/RELEASING.md +33 -0
- package/docs/assets/raydiculousx-mark.png +0 -0
- package/examples/README.md +10 -0
- package/examples/application-template/README.md +60 -0
- package/examples/application-template/index.html +12 -0
- package/examples/application-template/package.json +21 -0
- package/examples/application-template/src/app.config.ts +8 -0
- package/examples/application-template/src/app.ts +81 -0
- package/examples/application-template/src/dispatch.ts +29 -0
- package/examples/application-template/src/embed.ts +39 -0
- package/examples/application-template/src/env.d.ts +10 -0
- package/examples/application-template/src/host-origin.ts +9 -0
- package/examples/application-template/src/locale.ts +90 -0
- package/examples/application-template/src/styles.css +31 -0
- package/examples/application-template/tsconfig.json +24 -0
- package/examples/application-template/vite.config.mjs +16 -0
- package/examples/sdk-diagnostics/README.md +199 -0
- package/examples/sdk-diagnostics/assets/THIRD_PARTY_NOTICES.txt +567 -0
- package/examples/sdk-diagnostics/assets/auto-CnsixhmH.js +9 -0
- package/examples/sdk-diagnostics/assets/console-BjvPHSNK.css +1 -0
- package/examples/sdk-diagnostics/assets/console-BrwFsb65.js +9 -0
- package/examples/sdk-diagnostics/assets/favicon-BZQEit-G.ico +0 -0
- package/examples/sdk-diagnostics/assets/index-DXSJUMrS.js +2 -0
- package/examples/sdk-diagnostics/assets/messages-C1UCo5nT.js +1 -0
- package/examples/sdk-diagnostics/assets/robot-x3-D4wBvBN8.webp +0 -0
- package/examples/sdk-diagnostics/assets/styles-D3TrErcf.css +1 -0
- package/examples/sdk-diagnostics/index.html +15 -0
- package/host/README.md +34 -0
- package/package.json +80 -4
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import type { DeviceInfo } from '../../../lib/core/index.ts';
|
|
2
|
+
import type { HostLoginInput, HostRegisterInput, MountAppShellOptions } from './mount-host.ts';
|
|
3
|
+
/** 已绑定设备的展示信息;不包含账号凭据。 */
|
|
4
|
+
export interface BoundDevice {
|
|
5
|
+
/** 绑定记录 ID,用于解绑;没有序列号的设备也可按记录解绑。 */
|
|
6
|
+
id: string;
|
|
7
|
+
/** 机器人序列号;缺失时为空字符串,不允许连接。 */
|
|
8
|
+
sn: string;
|
|
9
|
+
/** 绑定记录中的名称。 */
|
|
10
|
+
name: string;
|
|
11
|
+
}
|
|
12
|
+
export declare class AccountApiError extends Error {
|
|
13
|
+
readonly status: number;
|
|
14
|
+
readonly code: number | undefined;
|
|
15
|
+
constructor(message: string, status: number, code?: number);
|
|
16
|
+
}
|
|
17
|
+
/** 与 Host 使用同一手机号、平台标识和 SHA256(password + oa) 认证合同。 */
|
|
18
|
+
export declare function passwordLogin(input: HostLoginInput, options: MountAppShellOptions): Promise<{
|
|
19
|
+
jti: string;
|
|
20
|
+
}>;
|
|
21
|
+
/** 与 Host 自助注册对齐:手机号、6–32 位密码及确认密码,提交摘要而非明文。 */
|
|
22
|
+
export declare function registerAccount(input: HostRegisterInput, options: MountAppShellOptions): Promise<{
|
|
23
|
+
code: string;
|
|
24
|
+
}>;
|
|
25
|
+
/** 按 Host 合同绑定设备;凭据仅放请求头,绑定请求不使用浏览器缓存。 */
|
|
26
|
+
export declare function bindDevice(jti: string, serialNo: string, signal: AbortSignal, endpoint?: string): Promise<void>;
|
|
27
|
+
/** 按绑定记录 ID 解绑;与 Host 使用同一 PUT 接口,成功响应可以没有正文。 */
|
|
28
|
+
export declare function unbindDevice(jti: string, id: string, signal: AbortSignal, endpoint?: string): Promise<void>;
|
|
29
|
+
/** 逐页读取已绑定设备;离线设备和缺少序列号的记录也保留展示。 */
|
|
30
|
+
export declare function listBoundDevices(jti: string, signal: AbortSignal, endpoint?: string): Promise<BoundDevice[]>;
|
|
31
|
+
/** 绑定关系决定列表,信令只决定在线状态;不显示未绑定的在线机器人。 */
|
|
32
|
+
export declare function mergeBoundDevices(bound: readonly BoundDevice[], online: readonly DeviceInfo[]): Array<BoundDevice & DeviceInfo>;
|
|
@@ -0,0 +1,176 @@
|
|
|
1
|
+
export class AccountApiError extends Error {
|
|
2
|
+
status;
|
|
3
|
+
code;
|
|
4
|
+
constructor(message, status, code) {
|
|
5
|
+
super(message);
|
|
6
|
+
this.status = status;
|
|
7
|
+
this.code = code;
|
|
8
|
+
}
|
|
9
|
+
}
|
|
10
|
+
async function request(url, init, fallback, allowEmpty = false) {
|
|
11
|
+
const response = await fetch(url, {
|
|
12
|
+
...init,
|
|
13
|
+
credentials: 'same-origin',
|
|
14
|
+
headers: { Accept: 'application/json', 'X-Request-Id': crypto.randomUUID(), ...init.headers },
|
|
15
|
+
});
|
|
16
|
+
const content = await response.text();
|
|
17
|
+
if (allowEmpty && response.ok && !content.trim())
|
|
18
|
+
return undefined;
|
|
19
|
+
const body = (() => { try {
|
|
20
|
+
return JSON.parse(content);
|
|
21
|
+
}
|
|
22
|
+
catch {
|
|
23
|
+
return null;
|
|
24
|
+
} })();
|
|
25
|
+
const success = body && body.success !== false && (body.code === undefined || body.code === 0 || body.code === 200);
|
|
26
|
+
if (!response.ok || !success) {
|
|
27
|
+
throw new AccountApiError(body?.message || `${fallback}(HTTP ${response.status})`, response.status, body?.code);
|
|
28
|
+
}
|
|
29
|
+
return body.data;
|
|
30
|
+
}
|
|
31
|
+
async function encodePassword(password) {
|
|
32
|
+
if (!globalThis.crypto?.subtle)
|
|
33
|
+
throw new Error('登录和注册需要 HTTPS 或 localhost 安全上下文');
|
|
34
|
+
const bytes = await crypto.subtle.digest('SHA-256', new TextEncoder().encode(password + 'oa'));
|
|
35
|
+
return Array.from(new Uint8Array(bytes), byte => byte.toString(16).padStart(2, '0')).join('');
|
|
36
|
+
}
|
|
37
|
+
/** 与 Host 使用同一手机号、平台标识和 SHA256(password + oa) 认证合同。 */
|
|
38
|
+
export async function passwordLogin(input, options) {
|
|
39
|
+
const password = await encodePassword(input.password);
|
|
40
|
+
const data = await request(options.loginUrl ?? '/api/oauth/tenantLogin', {
|
|
41
|
+
method: 'POST',
|
|
42
|
+
headers: { 'Content-Type': 'application/json' },
|
|
43
|
+
body: JSON.stringify({ platformCode: options.platformCode ?? 'little_dinosaur', phone: input.phone.trim(), password }),
|
|
44
|
+
signal: AbortSignal.any([input.signal, AbortSignal.timeout(15_000)]),
|
|
45
|
+
}, '登录失败');
|
|
46
|
+
const jti = data?.token ?? data?.jti ?? data?.access_token;
|
|
47
|
+
if (typeof jti !== 'string' || !jti.trim())
|
|
48
|
+
throw new Error('登录失败,请检查账号和密码');
|
|
49
|
+
return { jti };
|
|
50
|
+
}
|
|
51
|
+
/** 与 Host 自助注册对齐:手机号、6–32 位密码及确认密码,提交摘要而非明文。 */
|
|
52
|
+
export async function registerAccount(input, options) {
|
|
53
|
+
const phone = input.phone.trim();
|
|
54
|
+
if (!/^1[3-9]\d{9}$/.test(phone))
|
|
55
|
+
throw new Error('请输入 11 位有效手机号');
|
|
56
|
+
if (input.password.length < 6 || input.password.length > 32)
|
|
57
|
+
throw new Error('密码长度需为 6–32 位');
|
|
58
|
+
if (input.password !== input.confirmPassword)
|
|
59
|
+
throw new Error('两次输入的密码不一致');
|
|
60
|
+
const password = await encodePassword(input.password);
|
|
61
|
+
try {
|
|
62
|
+
const data = await request(options.registerUrl ?? '/api/tenant/register', {
|
|
63
|
+
method: 'POST',
|
|
64
|
+
headers: { 'Content-Type': 'application/json' },
|
|
65
|
+
body: JSON.stringify({ phone, password, platformKey: options.platformCode ?? 'little_dinosaur' }),
|
|
66
|
+
signal: AbortSignal.any([input.signal, AbortSignal.timeout(15_000)]),
|
|
67
|
+
}, '注册失败,请稍后重试');
|
|
68
|
+
return { code: typeof data?.code === 'string' ? data.code : '' };
|
|
69
|
+
}
|
|
70
|
+
catch (error) {
|
|
71
|
+
if (error instanceof AccountApiError && error.status === 404) {
|
|
72
|
+
throw new AccountApiError('注册服务暂未开放,请稍后再试或联系管理员', error.status, error.code);
|
|
73
|
+
}
|
|
74
|
+
if (error instanceof AccountApiError && error.status >= 500) {
|
|
75
|
+
throw new AccountApiError(`服务异常(${error.status}),请稍后重试`, error.status, error.code);
|
|
76
|
+
}
|
|
77
|
+
throw error;
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
/** 按 Host 合同绑定设备;凭据仅放请求头,绑定请求不使用浏览器缓存。 */
|
|
81
|
+
export async function bindDevice(jti, serialNo, signal, endpoint = '/api/deviceManage/manage/qrBind') {
|
|
82
|
+
const serial = serialNo.trim();
|
|
83
|
+
if (!serial)
|
|
84
|
+
throw new Error('请输入设备序列号');
|
|
85
|
+
if (!/^[A-Za-z0-9_-]{4,32}$/.test(serial)) {
|
|
86
|
+
throw new Error('序列号应为 4–32 位字母、数字、下划线或短横线');
|
|
87
|
+
}
|
|
88
|
+
const url = new URL(endpoint, globalThis.location?.href ?? 'http://localhost');
|
|
89
|
+
url.searchParams.set('serialNo', serial);
|
|
90
|
+
try {
|
|
91
|
+
await request(url.href, {
|
|
92
|
+
method: 'GET', cache: 'no-store',
|
|
93
|
+
headers: { jti, Authorization: `Bearer ${jti}` },
|
|
94
|
+
signal: AbortSignal.any([signal, AbortSignal.timeout(15_000)]),
|
|
95
|
+
}, '绑定失败,请重试');
|
|
96
|
+
}
|
|
97
|
+
catch (error) {
|
|
98
|
+
if (!(error instanceof AccountApiError) || error.status === 401 || error.code === 401)
|
|
99
|
+
throw error;
|
|
100
|
+
let message = error.message;
|
|
101
|
+
if (/其他租户|已被绑定|不属于|已绑定/.test(message))
|
|
102
|
+
message = '该设备已被其他账号绑定,请先在原账号解绑';
|
|
103
|
+
else if (/不存在|未找到|已注销/.test(message))
|
|
104
|
+
message = '设备不存在或已注销,请核对机身上的序列号';
|
|
105
|
+
else if (/参数|格式|不能为空|长度/.test(message))
|
|
106
|
+
message = '序列号格式不正确,请检查后重试';
|
|
107
|
+
throw new AccountApiError(message, error.status, error.code);
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
/** 按绑定记录 ID 解绑;与 Host 使用同一 PUT 接口,成功响应可以没有正文。 */
|
|
111
|
+
export async function unbindDevice(jti, id, signal, endpoint = '/api/tenantDevice/unbind') {
|
|
112
|
+
if (!id.trim())
|
|
113
|
+
throw new Error('缺少设备记录,请刷新设备列表');
|
|
114
|
+
const url = new URL(endpoint, globalThis.location?.href ?? 'http://localhost');
|
|
115
|
+
url.pathname = `${url.pathname.replace(/\/$/, '')}/${encodeURIComponent(id)}`;
|
|
116
|
+
try {
|
|
117
|
+
await request(url.href, {
|
|
118
|
+
method: 'PUT',
|
|
119
|
+
headers: { jti, Authorization: `Bearer ${jti}` },
|
|
120
|
+
signal: AbortSignal.any([signal, AbortSignal.timeout(15_000)]),
|
|
121
|
+
}, '解绑失败,请重试', true);
|
|
122
|
+
}
|
|
123
|
+
catch (error) {
|
|
124
|
+
if (error instanceof AccountApiError && (error.status === 403 || error.code === 403)) {
|
|
125
|
+
throw new AccountApiError('只能解绑自己账号下的设备', error.status, error.code);
|
|
126
|
+
}
|
|
127
|
+
throw error;
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
/** 逐页读取已绑定设备;离线设备和缺少序列号的记录也保留展示。 */
|
|
131
|
+
export async function listBoundDevices(jti, signal, endpoint = '/api/tenantDevice/list') {
|
|
132
|
+
const devices = new Map();
|
|
133
|
+
const timeout = AbortSignal.any([signal, AbortSignal.timeout(15_000)]);
|
|
134
|
+
let received = 0;
|
|
135
|
+
for (let pageNum = 1;; pageNum++) {
|
|
136
|
+
const url = new URL(endpoint, globalThis.location?.href ?? 'http://localhost');
|
|
137
|
+
url.searchParams.set('pageNum', String(pageNum));
|
|
138
|
+
url.searchParams.set('pageSize', '100');
|
|
139
|
+
const data = await request(url.href, {
|
|
140
|
+
headers: { jti, Authorization: `Bearer ${jti}` }, signal: timeout,
|
|
141
|
+
}, '设备列表加载失败');
|
|
142
|
+
const records = Array.isArray(data) ? data : data?.records;
|
|
143
|
+
if (!Array.isArray(records))
|
|
144
|
+
throw new Error('设备列表响应格式不正确,请重试');
|
|
145
|
+
const before = devices.size;
|
|
146
|
+
for (const record of records) {
|
|
147
|
+
if (!record || (typeof record.id !== 'string' && typeof record.id !== 'number')) {
|
|
148
|
+
throw new Error('设备列表响应格式不正确,请重试');
|
|
149
|
+
}
|
|
150
|
+
const id = String(record.id);
|
|
151
|
+
const sn = typeof record.serialNo === 'string' ? record.serialNo.trim() : '';
|
|
152
|
+
const name = typeof record.deviceName === 'string' ? record.deviceName.trim() : '';
|
|
153
|
+
devices.set(id, { id, sn, name: name || sn || `设备 ${id}` });
|
|
154
|
+
}
|
|
155
|
+
received += records.length;
|
|
156
|
+
if (records.length && devices.size === before)
|
|
157
|
+
throw new Error('设备列表分页未前进,请重试');
|
|
158
|
+
if (!Array.isArray(data) && !records.length && typeof data?.total === 'number' && received < data.total) {
|
|
159
|
+
throw new Error('设备列表分页不完整,请重试');
|
|
160
|
+
}
|
|
161
|
+
if (Array.isArray(data) || !records.length ||
|
|
162
|
+
(typeof data?.total === 'number' && received >= data.total) ||
|
|
163
|
+
(typeof data?.pages === 'number' && pageNum >= data.pages))
|
|
164
|
+
break;
|
|
165
|
+
// 未提供分页总量时继续到空页,不能因服务端限制每页条数而漏掉后续设备。
|
|
166
|
+
}
|
|
167
|
+
return [...devices.values()];
|
|
168
|
+
}
|
|
169
|
+
/** 绑定关系决定列表,信令只决定在线状态;不显示未绑定的在线机器人。 */
|
|
170
|
+
export function mergeBoundDevices(bound, online) {
|
|
171
|
+
const bySn = new Map(online.map(device => [device.sn, device]));
|
|
172
|
+
return bound.map(device => {
|
|
173
|
+
const status = device.sn ? bySn.get(device.sn) : undefined;
|
|
174
|
+
return { ...device, online: status?.online === true, connectedAt: status?.connectedAt ?? '' };
|
|
175
|
+
});
|
|
176
|
+
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
/** 持有发现连接;关闭失败时保留屏障,禁止创建第二条同账号连接。 */
|
|
2
|
+
export declare class ConnectionSlot<T extends {
|
|
3
|
+
disconnectSignaling(): Promise<void>;
|
|
4
|
+
}> {
|
|
5
|
+
private connection;
|
|
6
|
+
private closing;
|
|
7
|
+
get current(): T | null;
|
|
8
|
+
/** 工厂只在旧连接已确认关闭后调用,不因失败丢失旧连接引用。 */
|
|
9
|
+
open(create: () => T): T;
|
|
10
|
+
/** 并发关闭复用同一屏障;失败保留拒绝结果,交接方不能绕过。 */
|
|
11
|
+
close(): Promise<void>;
|
|
12
|
+
}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
/** 持有发现连接;关闭失败时保留屏障,禁止创建第二条同账号连接。 */
|
|
2
|
+
export class ConnectionSlot {
|
|
3
|
+
connection = null;
|
|
4
|
+
closing = null;
|
|
5
|
+
get current() {
|
|
6
|
+
return this.connection;
|
|
7
|
+
}
|
|
8
|
+
/** 工厂只在旧连接已确认关闭后调用,不因失败丢失旧连接引用。 */
|
|
9
|
+
open(create) {
|
|
10
|
+
if (this.connection || this.closing)
|
|
11
|
+
throw new Error('旧发现连接尚未确认关闭');
|
|
12
|
+
this.connection = create();
|
|
13
|
+
return this.connection;
|
|
14
|
+
}
|
|
15
|
+
/** 并发关闭复用同一屏障;失败保留拒绝结果,交接方不能绕过。 */
|
|
16
|
+
close() {
|
|
17
|
+
if (this.closing)
|
|
18
|
+
return this.closing;
|
|
19
|
+
const connection = this.connection;
|
|
20
|
+
if (!connection)
|
|
21
|
+
return Promise.resolve();
|
|
22
|
+
this.closing = Promise.resolve().then(() => connection.disconnectSignaling()).then(() => {
|
|
23
|
+
this.connection = null;
|
|
24
|
+
this.closing = null;
|
|
25
|
+
});
|
|
26
|
+
return this.closing;
|
|
27
|
+
}
|
|
28
|
+
}
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import type { AuthOkPayload, DeviceInfo, SignalingModule } from '../../../lib/core/index.ts';
|
|
2
|
+
/**
|
|
3
|
+
* 首次发现需要跨越两次异步边界;每次结果都必须仍属于当前设备选择界面。
|
|
4
|
+
* 设备推送可能提前展示列表,用户随后启动应用,不能再让晚到查询覆盖业务 iframe。
|
|
5
|
+
*/
|
|
6
|
+
export declare function loadDevicePicker(module: Pick<SignalingModule, 'connectSignaling' | 'listDevices'>, isCurrent: () => boolean, show: (devices: DeviceInfo[]) => void, report: (error: unknown) => Promise<void>, onAuthenticated?: (auth: AuthOkPayload) => void): Promise<void>;
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 首次发现需要跨越两次异步边界;每次结果都必须仍属于当前设备选择界面。
|
|
3
|
+
* 设备推送可能提前展示列表,用户随后启动应用,不能再让晚到查询覆盖业务 iframe。
|
|
4
|
+
*/
|
|
5
|
+
export async function loadDevicePicker(module, isCurrent, show, report, onAuthenticated) {
|
|
6
|
+
try {
|
|
7
|
+
const auth = await module.connectSignaling();
|
|
8
|
+
if (!isCurrent())
|
|
9
|
+
return;
|
|
10
|
+
onAuthenticated?.(auth);
|
|
11
|
+
const devices = await module.listDevices();
|
|
12
|
+
if (isCurrent())
|
|
13
|
+
show(devices);
|
|
14
|
+
}
|
|
15
|
+
catch (error) {
|
|
16
|
+
if (isCurrent())
|
|
17
|
+
await report(error);
|
|
18
|
+
}
|
|
19
|
+
}
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
const localeKey = 'microbotx.locale';
|
|
2
|
+
const localeEvent = 'microbotx:locale';
|
|
3
|
+
let current = 'zh-CN';
|
|
4
|
+
export function getLocale() {
|
|
5
|
+
try {
|
|
6
|
+
const saved = localStorage.getItem(localeKey);
|
|
7
|
+
if (saved === 'en' || saved === 'zh-CN')
|
|
8
|
+
return saved;
|
|
9
|
+
}
|
|
10
|
+
catch {
|
|
11
|
+
// Storage is optional in embedded browsers.
|
|
12
|
+
}
|
|
13
|
+
return current;
|
|
14
|
+
}
|
|
15
|
+
export function setLocale(locale) {
|
|
16
|
+
current = locale;
|
|
17
|
+
try {
|
|
18
|
+
localStorage.setItem(localeKey, locale);
|
|
19
|
+
}
|
|
20
|
+
catch {
|
|
21
|
+
// The current page still uses the selected language.
|
|
22
|
+
}
|
|
23
|
+
window.dispatchEvent(new CustomEvent(localeEvent, { detail: locale }));
|
|
24
|
+
}
|
|
25
|
+
export function watchLocale(listener, signal) {
|
|
26
|
+
window.addEventListener(localeEvent, event => {
|
|
27
|
+
if (event instanceof CustomEvent && (event.detail === 'en' || event.detail === 'zh-CN'))
|
|
28
|
+
current = event.detail;
|
|
29
|
+
listener();
|
|
30
|
+
}, { signal });
|
|
31
|
+
window.addEventListener('storage', event => {
|
|
32
|
+
if (event.key === localeKey || event.key === null)
|
|
33
|
+
listener();
|
|
34
|
+
}, { signal });
|
|
35
|
+
}
|
|
@@ -0,0 +1,155 @@
|
|
|
1
|
+
import type { DeviceInfo } from '../../../lib/core/index.ts';
|
|
2
|
+
import type { DirectConnectionOptions } from '../../../lib/client/direct.ts';
|
|
3
|
+
import type { BoundDevice } from './account-api.ts';
|
|
4
|
+
import type { HostLocale } from './locale.ts';
|
|
5
|
+
import type { RobotSetupAction, RobotSetupViewState } from './robot-setup.ts';
|
|
6
|
+
/** 登录表单输入;只交给认证回调,永远不进入业务 iframe。 */
|
|
7
|
+
export interface HostLoginInput {
|
|
8
|
+
/** 手机号或部署方定义的账号。 */
|
|
9
|
+
phone: string;
|
|
10
|
+
/** 仅本次认证使用的原始密码,调用方不得记录到日志。 */
|
|
11
|
+
password: string;
|
|
12
|
+
/** 页面销毁时取消认证请求。 */
|
|
13
|
+
signal: AbortSignal;
|
|
14
|
+
}
|
|
15
|
+
/** 注册输入;确认密码仅用于本地校验,不发送给后端。 */
|
|
16
|
+
export interface HostRegisterInput extends HostLoginInput {
|
|
17
|
+
/** 必须与 password 完全一致。 */
|
|
18
|
+
confirmPassword: string;
|
|
19
|
+
}
|
|
20
|
+
/** 外壳内部的视图状态;不含账号凭据。 */
|
|
21
|
+
export interface HostViewState {
|
|
22
|
+
/** 登录外壳当前显示语言。 */
|
|
23
|
+
locale: HostLocale;
|
|
24
|
+
/** 当前步骤。 */
|
|
25
|
+
phase: 'login' | 'register' | 'devices' | 'app';
|
|
26
|
+
/** 正在启动的应用名称。 */
|
|
27
|
+
appName: string;
|
|
28
|
+
/** 当前应用使用的机器人名称;未进入应用时为空。 */
|
|
29
|
+
robotName: string | null;
|
|
30
|
+
/** 当前操作尚未完成,应禁用重复提交。 */
|
|
31
|
+
busy: boolean;
|
|
32
|
+
/** 当前步骤的状态或错误提示。 */
|
|
33
|
+
status: string;
|
|
34
|
+
/** status 是否为需要用户处理的错误。 */
|
|
35
|
+
error: boolean;
|
|
36
|
+
/** 当前账号可见的设备;不可从中取得登录凭据。 */
|
|
37
|
+
devices: readonly (BoundDevice & DeviceInfo)[];
|
|
38
|
+
/** 刚创建的账号,用于自动登录失败时提示手动登录;不包含密码。 */
|
|
39
|
+
registeredAccount: {
|
|
40
|
+
phone: string;
|
|
41
|
+
code: string;
|
|
42
|
+
} | null;
|
|
43
|
+
/** 业务应用启动前的设备准备状态;未处在准备阶段时为空。 */
|
|
44
|
+
robotSetup: RobotSetupViewState | null;
|
|
45
|
+
}
|
|
46
|
+
/** UI 可触发的操作;错误由控制器写入视图状态。 */
|
|
47
|
+
export interface HostViewActions {
|
|
48
|
+
/** 保存并切换界面语言,保留当前表单和连接。 */
|
|
49
|
+
setLocale(locale: HostLocale): void;
|
|
50
|
+
/** 提交手机号、密码;不保存密码。 */
|
|
51
|
+
login(input: Pick<HostLoginInput, 'phone' | 'password'>): Promise<void>;
|
|
52
|
+
/** 创建账号并自动登录;失败由控制器显示,已创建账号不会自动重复注册。 */
|
|
53
|
+
register(input: Omit<HostRegisterInput, 'signal'>): Promise<void>;
|
|
54
|
+
/** 切换登录或注册表单;请求进行中忽略切换。 */
|
|
55
|
+
showAuthPage(phase: 'login' | 'register'): void;
|
|
56
|
+
/** 启动当前列表中的在线机器人。 */
|
|
57
|
+
connect(sn: string): Promise<void>;
|
|
58
|
+
/** 重新查询设备,或重试发现连接。 */
|
|
59
|
+
refresh(): Promise<void>;
|
|
60
|
+
/** 按序列号绑定并刷新设备;失败时拒绝,由绑定表单呈现错误。 */
|
|
61
|
+
bind(serialNo: string): Promise<void>;
|
|
62
|
+
/** 确认后按绑定记录 ID 解绑并刷新;只能在设备列表中执行,失败时拒绝。 */
|
|
63
|
+
unbind(id: string): Promise<void>;
|
|
64
|
+
/** 等待连接释放后退出账号。 */
|
|
65
|
+
logout(): Promise<void>;
|
|
66
|
+
/** 等待业务清理后回到设备选择。 */
|
|
67
|
+
leave(): Promise<void>;
|
|
68
|
+
/** 刷新启动引导中的设备状态。 */
|
|
69
|
+
checkRobot(): Promise<void>;
|
|
70
|
+
/** 手动执行当前允许的启动准备操作。 */
|
|
71
|
+
prepareRobot(action: RobotSetupAction): Promise<void>;
|
|
72
|
+
}
|
|
73
|
+
/** UI 渲染器持有的资源;不参与机器人通信。 */
|
|
74
|
+
export interface HostView {
|
|
75
|
+
/** 由控制器插入业务 iframe 的稳定 DOM 容器。 */
|
|
76
|
+
appContainer: HTMLElement;
|
|
77
|
+
/** 更新 UI;不读取或修改机器人连接。 */
|
|
78
|
+
render(state: HostViewState): void;
|
|
79
|
+
/** 卸载 UI 并清理监听器。 */
|
|
80
|
+
dispose(): void;
|
|
81
|
+
}
|
|
82
|
+
/** SDK 内部的视图接口;连接控制器不依赖 React。 */
|
|
83
|
+
export type HostViewFactory = (container: HTMLElement, actions: HostViewActions) => HostView;
|
|
84
|
+
/** 单应用外壳配置;根 SDK 导出不会加载外壳。 */
|
|
85
|
+
export interface MountAppShellOptions {
|
|
86
|
+
/** 接管的容器;默认 document.body。启动时移除占位内容,销毁后在容器未被重新接管时恢复。 */
|
|
87
|
+
container?: HTMLElement;
|
|
88
|
+
/** 布局模式;默认 viewport,在 body 下独立全屏显示并锁定页面滚动。container 按指定容器内容区显示,容器需有明确高度。 */
|
|
89
|
+
layout?: 'viewport' | 'container';
|
|
90
|
+
/** 标题栏显示的应用名称。 */
|
|
91
|
+
appName: string;
|
|
92
|
+
/** 英文界面的应用名称;省略时沿用 appName。应用身份仍使用 appId 或 appName。 */
|
|
93
|
+
appNameEn?: string;
|
|
94
|
+
/** 稳定应用身份;省略时使用 appName。 */
|
|
95
|
+
appId?: string;
|
|
96
|
+
/** 可信 HTTP(S) 业务入口,默认当前 URL;由外壳添加 embedded=1,不在 URL 放凭据。 */
|
|
97
|
+
appUrl?: string | URL;
|
|
98
|
+
/** 不含 token 的 WS(S) 信令地址;默认当前站点的 /ws,自动匹配 ws/wss。 */
|
|
99
|
+
signalingUrl?: string;
|
|
100
|
+
/**
|
|
101
|
+
* 执行自定义认证。
|
|
102
|
+
* @param input - 本次登录的账号、密码和取消信号,见 {@link HostLoginInput}。
|
|
103
|
+
* @returns 鉴权成功后解析为包含非空访问凭据 jti 的对象。
|
|
104
|
+
* @throws 认证失败时应拒绝 Promise,由外壳呈现错误。
|
|
105
|
+
* @remarks 省略该回调时使用平台手机号和密码认证。
|
|
106
|
+
*/
|
|
107
|
+
authenticate?: (input: HostLoginInput) => Promise<{
|
|
108
|
+
/** 用于设备发现与业务建连的非空访问凭据。 */
|
|
109
|
+
jti: string;
|
|
110
|
+
}>;
|
|
111
|
+
/** 默认认证实现使用的接口地址,默认 /api/oauth/tenantLogin;自定义 authenticate 时不使用。 */
|
|
112
|
+
loginUrl?: string;
|
|
113
|
+
/** 注册接口;默认 /api/tenant/register,沿用 Host 的手机号及密码摘要合同。 */
|
|
114
|
+
registerUrl?: string;
|
|
115
|
+
/** 已绑定设备接口;默认 /api/tenantDevice/list,自动携带登录凭据并读取分页。 */
|
|
116
|
+
deviceListUrl?: string;
|
|
117
|
+
/** 绑定设备接口;默认 /api/deviceManage/manage/qrBind,无需终端用户配置。 */
|
|
118
|
+
bindDeviceUrl?: string;
|
|
119
|
+
/** 解绑接口前缀;默认 /api/tenantDevice/unbind,SDK 自动附加编码后的记录 ID。 */
|
|
120
|
+
unbindDeviceUrl?: string;
|
|
121
|
+
/** 默认认证的 platformCode,默认 little_dinosaur。 */
|
|
122
|
+
platformCode?: string;
|
|
123
|
+
/** 交给业务 SDK 的 TURN/STUN 配置。省略时先使用 auth.ok 下发的配置,再从 ICE 接口获取。空数组表示不配置 ICE 服务器。 */
|
|
124
|
+
iceServers?: RTCIceServer[];
|
|
125
|
+
/** 平台临时 ICE 凭证接口,默认同源 /api/campus-websocket/api/signaling/ice-servers。仅填写可信平台地址。 */
|
|
126
|
+
iceServersUrl?: string;
|
|
127
|
+
/** 可公开给业务应用的配置,不允许放账号凭据。 */
|
|
128
|
+
config?: DirectConnectionOptions['config'];
|
|
129
|
+
/** 应用外观;默认 light。 */
|
|
130
|
+
theme?: 'light' | 'dark';
|
|
131
|
+
/** 是否向业务 iframe 委派麦克风权限;默认 false,浏览器仍须用户授权。 */
|
|
132
|
+
enableMicrophone?: boolean;
|
|
133
|
+
/** 是否向业务 iframe 委派摄像头权限;默认 false,浏览器仍须用户授权。 */
|
|
134
|
+
enableCamera?: boolean;
|
|
135
|
+
}
|
|
136
|
+
/** 外壳资源句柄;创建句柄不代表已登录或已连接机器人。 */
|
|
137
|
+
export interface MountedAppShell {
|
|
138
|
+
/**
|
|
139
|
+
* 退出设备准备或业务应用,重新打开设备选择页。
|
|
140
|
+
* @returns 清理和返回设备选择页的流程结束时完成。退出进行中复用同一 Promise,两个阶段都未启动时直接完成。
|
|
141
|
+
* @throws 准备连接释放、业务退出或返回设备选择页失败时拒绝 Promise。
|
|
142
|
+
*/
|
|
143
|
+
leave(): Promise<void>;
|
|
144
|
+
/**
|
|
145
|
+
* 取消认证与设备查询,释放准备连接、业务会话和发现连接,最后移除界面。
|
|
146
|
+
* @returns 活动资源释放、外壳界面移除后完成。
|
|
147
|
+
* @throws 准备连接、业务会话或发现连接清理失败时拒绝 Promise。
|
|
148
|
+
*/
|
|
149
|
+
dispose(): Promise<void>;
|
|
150
|
+
}
|
|
151
|
+
/**
|
|
152
|
+
* 挂载登录、选设备、业务 iframe 和结束按钮。
|
|
153
|
+
* 选定设备时关闭发现连接,准备连接释放后再向业务 iframe 交接建连凭据。
|
|
154
|
+
*/
|
|
155
|
+
export declare function createHostController(options: MountAppShellOptions, render: HostViewFactory): MountedAppShell;
|