@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.
Files changed (126) hide show
  1. package/CHANGELOG.md +7 -0
  2. package/LICENSE +55 -0
  3. package/NOTICE +14 -0
  4. package/README.md +91 -3
  5. package/dist/THIRD_PARTY_NOTICES.txt +466 -0
  6. package/dist/host/src/bootstrap/client.d.ts +37 -0
  7. package/dist/host/src/bootstrap/client.js +215 -0
  8. package/dist/host/src/bootstrap/host.d.ts +79 -0
  9. package/dist/host/src/bootstrap/host.js +214 -0
  10. package/dist/host/src/bootstrap/protocol.d.ts +20 -0
  11. package/dist/host/src/bootstrap/protocol.js +72 -0
  12. package/dist/host/src/dev.d.ts +30 -0
  13. package/dist/host/src/dev.js +20 -0
  14. package/dist/host/src/entry/app.d.ts +10 -0
  15. package/dist/host/src/entry/app.js +9 -0
  16. package/dist/host/src/entry/auto.d.ts +16 -0
  17. package/dist/host/src/entry/auto.js +18747 -0
  18. package/dist/host/src/standalone/account-api.d.ts +32 -0
  19. package/dist/host/src/standalone/account-api.js +176 -0
  20. package/dist/host/src/standalone/connection-slot.d.ts +12 -0
  21. package/dist/host/src/standalone/connection-slot.js +28 -0
  22. package/dist/host/src/standalone/device-picker.d.ts +6 -0
  23. package/dist/host/src/standalone/device-picker.js +19 -0
  24. package/dist/host/src/standalone/locale.d.ts +4 -0
  25. package/dist/host/src/standalone/locale.js +35 -0
  26. package/dist/host/src/standalone/mount-host.d.ts +155 -0
  27. package/dist/host/src/standalone/mount-host.js +396 -0
  28. package/dist/host/src/standalone/mount-surface.d.ts +12 -0
  29. package/dist/host/src/standalone/mount-surface.js +69 -0
  30. package/dist/host/src/standalone/robot-setup.d.ts +61 -0
  31. package/dist/host/src/standalone/robot-setup.js +160 -0
  32. package/dist/lib/client/action-ownership.d.ts +31 -0
  33. package/dist/lib/client/action-ownership.js +182 -0
  34. package/dist/lib/client/direct.d.ts +149 -0
  35. package/dist/lib/client/direct.js +560 -0
  36. package/dist/lib/client/raydiculousx.d.ts +541 -0
  37. package/dist/lib/client/raydiculousx.js +694 -0
  38. package/dist/lib/client/runtime.d.ts +165 -0
  39. package/dist/lib/client/runtime.js +1 -0
  40. package/dist/lib/core/index.d.ts +16 -0
  41. package/dist/lib/core/index.js +9 -0
  42. package/dist/lib/core/rpc/command-channel.d.ts +345 -0
  43. package/dist/lib/core/rpc/command-channel.js +697 -0
  44. package/dist/lib/core/rpc/commands.d.ts +354 -0
  45. package/dist/lib/core/rpc/commands.js +134 -0
  46. package/dist/lib/core/rpc/request-policy.d.ts +29 -0
  47. package/dist/lib/core/rpc/request-policy.js +35 -0
  48. package/dist/lib/core/rpc/robot-api.d.ts +184 -0
  49. package/dist/lib/core/rpc/robot-api.js +34 -0
  50. package/dist/lib/core/signaling/ice-servers.d.ts +10 -0
  51. package/dist/lib/core/signaling/ice-servers.js +58 -0
  52. package/dist/lib/core/signaling/peer-negotiator.d.ts +164 -0
  53. package/dist/lib/core/signaling/peer-negotiator.js +366 -0
  54. package/dist/lib/core/signaling/signaling-client.d.ts +204 -0
  55. package/dist/lib/core/signaling/signaling-client.js +473 -0
  56. package/dist/lib/core/signaling/signaling-contract.d.ts +164 -0
  57. package/dist/lib/core/signaling/signaling-contract.js +35 -0
  58. package/dist/lib/core/signaling/signaling.d.ts +172 -0
  59. package/dist/lib/core/signaling/signaling.js +381 -0
  60. package/dist/lib/media/attach-video.d.ts +3 -0
  61. package/dist/lib/media/attach-video.js +243 -0
  62. package/dist/lib/media/direct-audio.d.ts +36 -0
  63. package/dist/lib/media/direct-audio.js +94 -0
  64. package/dist/lib/media/index.d.ts +5 -0
  65. package/dist/lib/media/index.js +3 -0
  66. package/dist/lib/media/pcm-types.d.ts +33 -0
  67. package/dist/lib/media/pcm-types.js +1 -0
  68. package/dist/lib/media/robot-audio.d.ts +64 -0
  69. package/dist/lib/media/robot-audio.js +82 -0
  70. package/dist/lib/media/robot-pcm-output.d.ts +16 -0
  71. package/dist/lib/media/robot-pcm-output.js +101 -0
  72. package/dist/lib/robot/action-upload.d.ts +32 -0
  73. package/dist/lib/robot/action-upload.js +143 -0
  74. package/dist/lib/robot/robot-commands.d.ts +148 -0
  75. package/dist/lib/robot/robot-commands.js +164 -0
  76. package/dist/lib/robot/robot-responses.d.ts +42 -0
  77. package/dist/lib/robot/robot-responses.js +226 -0
  78. package/dist/lib/robot/robot-tof.d.ts +61 -0
  79. package/dist/lib/robot/robot-tof.js +25 -0
  80. package/dist/lib/robot/robot-types.d.ts +293 -0
  81. package/dist/lib/robot/robot-types.js +17 -0
  82. package/dist/lib/shared/errors.d.ts +57 -0
  83. package/dist/lib/shared/errors.js +45 -0
  84. package/dist/lib/shared/events.d.ts +11 -0
  85. package/dist/lib/shared/events.js +40 -0
  86. package/dist/lib/shared/types.d.ts +26 -0
  87. package/dist/lib/shared/types.js +1 -0
  88. package/dist/lib/shared/validation.d.ts +29 -0
  89. package/dist/lib/shared/validation.js +87 -0
  90. package/dist/lib/version.d.ts +2 -0
  91. package/dist/lib/version.js +3 -0
  92. package/dist/raydiculousx-sdk.d.ts +26 -0
  93. package/dist/raydiculousx-sdk.js +17 -0
  94. package/docs/API.md +1106 -0
  95. package/docs/DIRECT_APPS.md +83 -0
  96. package/docs/HOST_HANDOFF.md +59 -0
  97. package/docs/HOST_PROTOCOL.md +43 -0
  98. package/docs/RELEASING.md +33 -0
  99. package/docs/assets/raydiculousx-mark.png +0 -0
  100. package/examples/README.md +10 -0
  101. package/examples/application-template/README.md +60 -0
  102. package/examples/application-template/index.html +12 -0
  103. package/examples/application-template/package.json +21 -0
  104. package/examples/application-template/src/app.config.ts +8 -0
  105. package/examples/application-template/src/app.ts +81 -0
  106. package/examples/application-template/src/dispatch.ts +29 -0
  107. package/examples/application-template/src/embed.ts +39 -0
  108. package/examples/application-template/src/env.d.ts +10 -0
  109. package/examples/application-template/src/host-origin.ts +9 -0
  110. package/examples/application-template/src/locale.ts +90 -0
  111. package/examples/application-template/src/styles.css +31 -0
  112. package/examples/application-template/tsconfig.json +24 -0
  113. package/examples/application-template/vite.config.mjs +16 -0
  114. package/examples/sdk-diagnostics/README.md +199 -0
  115. package/examples/sdk-diagnostics/assets/THIRD_PARTY_NOTICES.txt +567 -0
  116. package/examples/sdk-diagnostics/assets/auto-CnsixhmH.js +9 -0
  117. package/examples/sdk-diagnostics/assets/console-BjvPHSNK.css +1 -0
  118. package/examples/sdk-diagnostics/assets/console-BrwFsb65.js +9 -0
  119. package/examples/sdk-diagnostics/assets/favicon-BZQEit-G.ico +0 -0
  120. package/examples/sdk-diagnostics/assets/index-DXSJUMrS.js +2 -0
  121. package/examples/sdk-diagnostics/assets/messages-C1UCo5nT.js +1 -0
  122. package/examples/sdk-diagnostics/assets/robot-x3-D4wBvBN8.webp +0 -0
  123. package/examples/sdk-diagnostics/assets/styles-D3TrErcf.css +1 -0
  124. package/examples/sdk-diagnostics/index.html +15 -0
  125. package/host/README.md +34 -0
  126. package/package.json +80 -4
@@ -0,0 +1,101 @@
1
+ import { RobotAudio } from "./robot-audio.js";
2
+ /**
3
+ * 创建当前页面的 PCM 排程驱动,将音频送入已有 WebRTC 发送端。
4
+ *
5
+ * @param options - 现有机器人连接和真实音频能力;见 {@link RobotAudioOptions}。
6
+ * @param signal - 驱动生命周期信号;取消会清空排程并异步释放音轨和 AudioContext。
7
+ * @returns AudioContext 已恢复且发送音轨已替换的 24 kHz 单声道驱动;不表示设备已经发声。
8
+ * 排程缓冲上限为 30 秒,默认发送增益为 1,新增音频最早在本地音频时钟 20 ms 后开始。
9
+ * @throws AudioContext 在 4000 ms 内未恢复、缺少能力或发送音轨、浏览器媒体 API 失败、初始化期间取消时拒绝 Promise。
10
+ * 进入恢复 AudioContext 或替换音轨阶段后的失败会先尝试释放驱动;清理失败也会拒绝。
11
+ * @remarks 不连接本机扬声器,也不关闭共享机器人连接。取消信号会发起异步关闭;
12
+ * 调用方应 await 返回驱动的 close 以确认清理完成。音轨替换和清理没有额外超时。
13
+ * 返回驱动的 write 和 setOutputGain 不执行业务层的样本范围校验或增益裁剪,调用约束见 {@link PcmSink}。
14
+ */
15
+ export async function openRobotPcmOutput(options, signal) {
16
+ signal.throwIfAborted();
17
+ const audio = new RobotAudio(options);
18
+ const context = new AudioContext({ sampleRate: 24000 });
19
+ const destination = context.createMediaStreamDestination();
20
+ const gain = context.createGain();
21
+ gain.connect(destination);
22
+ const track = destination.stream.getAudioTracks()[0];
23
+ const sources = new Set();
24
+ let end = 0;
25
+ let closed = false;
26
+ let closing = null;
27
+ const clear = () => {
28
+ for (const source of sources) {
29
+ source.stop();
30
+ source.disconnect();
31
+ }
32
+ sources.clear();
33
+ end = context.currentTime;
34
+ };
35
+ const close = () => {
36
+ if (closing)
37
+ return closing;
38
+ closed = true;
39
+ clear();
40
+ signal.removeEventListener('abort', abort);
41
+ closing = (async () => {
42
+ await audio.dispose();
43
+ track.stop();
44
+ await context.close();
45
+ })();
46
+ return closing;
47
+ };
48
+ const abort = () => {
49
+ void close();
50
+ };
51
+ signal.addEventListener('abort', abort, { once: true });
52
+ try {
53
+ // 页面用户激活可解锁 AudioContext;失败时明确报错。
54
+ let timeout;
55
+ try {
56
+ await Promise.race([context.resume(), new Promise((_, reject) => {
57
+ timeout = setTimeout(() => reject(new Error('请先在 页面中启用音频播放')), 4000);
58
+ })]);
59
+ }
60
+ finally {
61
+ clearTimeout(timeout);
62
+ }
63
+ signal.throwIfAborted();
64
+ if (!await audio.setOutgoingAudioTrack(track)) {
65
+ throw new Error('机器人音频能力或 WebRTC 音轨尚未就绪');
66
+ }
67
+ signal.throwIfAborted();
68
+ return {
69
+ write(samples) {
70
+ if (closed || context.state !== 'running')
71
+ throw new Error('页面音频已暂停');
72
+ if (Math.max(end, context.currentTime) - context.currentTime + samples.length / 24000 > 30) {
73
+ throw new Error('页面音频缓冲超过 30 秒');
74
+ }
75
+ const buffer = context.createBuffer(1, samples.length, 24000);
76
+ buffer.copyToChannel(new Float32Array(samples), 0);
77
+ const source = context.createBufferSource();
78
+ source.buffer = buffer;
79
+ source.connect(gain);
80
+ sources.add(source);
81
+ source.onended = () => {
82
+ sources.delete(source);
83
+ source.disconnect();
84
+ };
85
+ const start = Math.max(end, context.currentTime + 0.02);
86
+ source.start(start);
87
+ end = start + buffer.duration;
88
+ },
89
+ clear,
90
+ setOutputGain(volume) {
91
+ gain.gain.setTargetAtTime(volume, context.currentTime, 0.015);
92
+ },
93
+ playing: () => !closed && end > context.currentTime,
94
+ close,
95
+ };
96
+ }
97
+ catch (error) {
98
+ await close();
99
+ throw error;
100
+ }
101
+ }
@@ -0,0 +1,32 @@
1
+ /** 自定义动作上传选项。上传和控制共用 WebRTC 连接,各自使用独立通道。 */
2
+ export interface ActionUploadOptions {
3
+ /** 动作保持秒数,须为有限正数。省略时使用设备默认值,上限由设备校验。 */
4
+ durationS?: number;
5
+ /** 整次上传的等待上限,单位毫秒,默认 120000,范围 1~2147483647。 */
6
+ timeoutMs?: number;
7
+ /** 中断本次上传。已发送 end 时设备仍有可能完成注册。 */
8
+ signal?: AbortSignal;
9
+ }
10
+ /** 设备已完成自定义动作注册时返回的 blob result。 */
11
+ export interface ActionUploadResult {
12
+ /** 设备回执帧类型。 */
13
+ op: 'result';
14
+ /** 设备已校验并注册文件。 */
15
+ ok: true;
16
+ /** 已注册的动作名。 */
17
+ name: string;
18
+ /** 可传给 doSkill 的技能名。 */
19
+ skill: string;
20
+ /** 文件长度,单位字节。 */
21
+ sizeBytes: number;
22
+ /** 设备登记的动作保持时长,单位秒。 */
23
+ durationS: number;
24
+ }
25
+ /** 一个已协商的 blob 通道同一时刻只处理一份文件。 */
26
+ export declare class ActionUploader {
27
+ private active;
28
+ private mayReceiveOrphanEnd;
29
+ private readonly channel;
30
+ constructor(channel: RTCDataChannel);
31
+ upload(name: string, file: Blob, options: ActionUploadOptions, lifetime: AbortSignal): Promise<ActionUploadResult>;
32
+ }
@@ -0,0 +1,143 @@
1
+ import { positiveTimeout, RaydiculousXError } from "../shared/errors.js";
2
+ const CHUNK_BYTES = 8 * 1024;
3
+ const MAX_BUFFERED_BYTES = 256 * 1024;
4
+ /** 一个已协商的 blob 通道同一时刻只处理一份文件。 */
5
+ export class ActionUploader {
6
+ active = false;
7
+ mayReceiveOrphanEnd = false;
8
+ channel;
9
+ constructor(channel) {
10
+ this.channel = channel;
11
+ }
12
+ async upload(name, file, options, lifetime) {
13
+ const channel = this.channel;
14
+ if (this.active)
15
+ throw new RaydiculousXError('BUSY', '当前连接正在上传动作');
16
+ if (channel.readyState === 'closing' || channel.readyState === 'closed') {
17
+ throw new RaydiculousXError('UNSUPPORTED', '当前连接没有可用的动作上传通道');
18
+ }
19
+ if (typeof name !== 'string' || !/^[a-z][a-z0-9_-]{0,31}$/.test(name)) {
20
+ throw new RaydiculousXError('INVALID_ARGUMENT', '动作名须以小写字母开头,限 1~32 个小写字母、数字、下划线或连字符');
21
+ }
22
+ if (!file || typeof file.arrayBuffer !== 'function' || !Number.isSafeInteger(file.size)
23
+ || file.size <= 0 || file.size > 16 * 1024 * 1024) {
24
+ throw new RaydiculousXError('INVALID_ARGUMENT', '需要非空且不超过 16 MiB 的动作文件');
25
+ }
26
+ const { durationS } = options;
27
+ if (durationS !== undefined && (!Number.isFinite(durationS) || durationS <= 0)) {
28
+ throw new RaydiculousXError('INVALID_ARGUMENT', 'durationS 必须为有限正数');
29
+ }
30
+ const timeoutMs = positiveTimeout(options.timeoutMs ?? 120_000, 'timeoutMs');
31
+ const signal = options.signal ? AbortSignal.any([lifetime, options.signal]) : lifetime;
32
+ if (signal.aborted)
33
+ throw new RaydiculousXError('ABORTED', '上传已取消');
34
+ if (!globalThis.crypto?.subtle)
35
+ throw new RaydiculousXError('UNSUPPORTED', '上传需要 Web Crypto SHA-256');
36
+ this.active = true;
37
+ const listeners = new AbortController();
38
+ const eventOptions = { signal: listeners.signal };
39
+ const previousThreshold = channel.bufferedAmountLowThreshold;
40
+ let began = false;
41
+ let ended = false;
42
+ let fileBytes;
43
+ let header;
44
+ let offset = 0;
45
+ let timer;
46
+ try {
47
+ channel.bufferedAmountLowThreshold = MAX_BUFFERED_BYTES / 2;
48
+ return await new Promise((resolve, reject) => {
49
+ const finish = (result, closePartial = false) => {
50
+ if (listeners.signal.aborted)
51
+ return;
52
+ listeners.abort();
53
+ // 协议没有取消帧和请求 ID。中断有歧义时关闭 blob,触发设备清理并隔离迟到回执。
54
+ if (began && closePartial)
55
+ channel.close();
56
+ if (result instanceof Error)
57
+ reject(result);
58
+ else
59
+ resolve(result);
60
+ };
61
+ const sendAvailable = () => {
62
+ if (!fileBytes || !header || ended || channel.readyState !== 'open' || listeners.signal.aborted)
63
+ return;
64
+ try {
65
+ if (!began) {
66
+ began = true;
67
+ channel.send(header);
68
+ }
69
+ while (offset < fileBytes.length && channel.bufferedAmount < MAX_BUFFERED_BYTES) {
70
+ const chunk = fileBytes.subarray(offset, offset + CHUNK_BYTES);
71
+ const frame = new Uint8Array(4 + chunk.length);
72
+ new DataView(frame.buffer).setUint32(0, offset / CHUNK_BYTES, false);
73
+ frame.set(chunk, 4);
74
+ channel.send(frame);
75
+ offset += chunk.length;
76
+ }
77
+ if (offset === fileBytes.length) {
78
+ ended = true;
79
+ channel.send('{"op":"end"}');
80
+ }
81
+ }
82
+ catch {
83
+ finish(new RaydiculousXError('CONNECTION_FAILED', '无法发送动作文件', 'transport'), true);
84
+ }
85
+ };
86
+ channel.addEventListener('open', sendAvailable, eventOptions);
87
+ channel.addEventListener('bufferedamountlow', sendAvailable, eventOptions);
88
+ channel.addEventListener('close', () => finish(new RaydiculousXError(began ? 'CONNECTION_FAILED' : 'UNSUPPORTED', '动作上传通道已关闭', 'transport')), eventOptions);
89
+ channel.addEventListener('error', () => finish(new RaydiculousXError('CONNECTION_FAILED', '动作上传通道出错', 'transport'), true), eventOptions);
90
+ signal.addEventListener('abort', () => finish(new RaydiculousXError('ABORTED', '上传已取消'), true), eventOptions);
91
+ channel.addEventListener('message', ({ data }) => {
92
+ if (!began)
93
+ return;
94
+ try {
95
+ const result = JSON.parse(data);
96
+ if (!result || result.op !== 'result')
97
+ throw new Error();
98
+ // begin 被拒时,已在队列里的 end 可能晚到;设备会为它单独回一条空上传错误。
99
+ if (this.mayReceiveOrphanEnd && result.ok === false && result.name == null
100
+ && result.error === 'no upload in progress') {
101
+ this.mayReceiveOrphanEnd = false;
102
+ return;
103
+ }
104
+ this.mayReceiveOrphanEnd = false;
105
+ if (result.ok === false && typeof result.error === 'string') {
106
+ this.mayReceiveOrphanEnd = ended;
107
+ finish(new RaydiculousXError('UPLOAD_REJECTED', result.error, 'daemon'));
108
+ return;
109
+ }
110
+ if (!ended || result.ok !== true || result.name !== name || result.skill !== name
111
+ || result.sizeBytes !== file.size || !Number.isFinite(result.durationS) || result.durationS <= 0) {
112
+ throw new Error();
113
+ }
114
+ finish(result);
115
+ }
116
+ catch {
117
+ finish(new RaydiculousXError('INVALID_RESPONSE', '无效的动作上传回执'), true);
118
+ }
119
+ }, eventOptions);
120
+ timer = setTimeout(() => finish(new RaydiculousXError('REQUEST_TIMEOUT', '动作上传超时'), true), timeoutMs);
121
+ const prepare = async () => {
122
+ const buffer = await file.arrayBuffer();
123
+ if (listeners.signal.aborted)
124
+ return;
125
+ const digest = await crypto.subtle.digest('SHA-256', buffer);
126
+ if (listeners.signal.aborted)
127
+ return;
128
+ const sha256 = Array.from(new Uint8Array(digest), byte => byte.toString(16).padStart(2, '0')).join('');
129
+ header = JSON.stringify({ op: 'begin', name, size: file.size, sha256, durationS });
130
+ fileBytes = new Uint8Array(buffer);
131
+ sendAvailable();
132
+ };
133
+ void prepare().catch(() => finish(new RaydiculousXError('INVALID_ARGUMENT', '读取或校验动作文件失败')));
134
+ });
135
+ }
136
+ finally {
137
+ listeners.abort();
138
+ clearTimeout(timer);
139
+ channel.bufferedAmountLowThreshold = previousThreshold;
140
+ this.active = false;
141
+ }
142
+ }
143
+ }
@@ -0,0 +1,148 @@
1
+ /** SDK、连接核心与上位机共用的方法名;发送前仍校验参数及请求归属。 */
2
+ export declare const ROBOT_METHODS: Readonly<{
3
+ /** 发送一次速度设定。 */
4
+ readonly move: "robot.move";
5
+ /** 提交完整文本给板端智能体,回执表示受理。 */
6
+ readonly invoke: "robot.invoke";
7
+ /** 请求设备中断当前音频播放。 */
8
+ readonly stopAudio: "robot.audio.stop";
9
+ /** 列出设备上的用户动作。 */
10
+ readonly listActions: "robot.actions.list";
11
+ /** 按名称删除用户动作。 */
12
+ readonly deleteAction: "robot.actions.delete";
13
+ /** 读取设备配置文件快照。 */
14
+ readonly getConfig: "robot.config";
15
+ /** 切换行走、轮滑或舵机直控模式。 */
16
+ readonly setMode: "robot.setMode";
17
+ /** 给关节上电并回到 home 姿态。 */
18
+ readonly init: "robot.init";
19
+ /** 交给或收回策略驱动。 */
20
+ readonly enable: "robot.enable";
21
+ /** 设置头部关节目标。 */
22
+ readonly head: "robot.head";
23
+ /** 指定躯干坐标系中的注视点。 */
24
+ readonly look: "robot.look";
25
+ /** 设置嘴部开合。 */
26
+ readonly mouth: "robot.mouth";
27
+ /** 设置或解除身体姿态保持。 */
28
+ readonly pose: "robot.pose";
29
+ /** 读取板端舵机状态及当前关节角。 */
30
+ readonly getServoState: "robot.servo.state";
31
+ /** 在舵机直控模式下设置关节目标。 */
32
+ readonly setServoJoints: "robot.servo.set";
33
+ /** 切断关节力矩。 */
34
+ readonly relax: "robot.relax";
35
+ /** 请求停车。 */
36
+ readonly stop: "robot.stop";
37
+ /** 复位设备端允许远程恢复的故障。 */
38
+ readonly reset: "robot.reset";
39
+ /** 请求预置技能,wait 决定设备侧等待语义。 */
40
+ readonly doSkill: "robot.do";
41
+ /** 查询执行器状态快照。 */
42
+ readonly getStatus: "robot.status";
43
+ /** 查询设备协议与能力。 */
44
+ readonly getCapabilities: "robot.capabilities";
45
+ /** 订阅当前连接的头部 ToF 深度流。 */
46
+ readonly subscribeTof: "robot.tof.subscribe";
47
+ /** 停止当前连接的头部 ToF 深度流。 */
48
+ readonly unsubscribeTof: "robot.tof.unsubscribe";
49
+ /** 启动板端追光程序。 */
50
+ readonly startLightFollow: "robot.lightFollow.start";
51
+ /** 停止板端追光程序。 */
52
+ readonly stopLightFollow: "robot.lightFollow.stop";
53
+ /** 查询板端追光程序的安装和运行状态。 */
54
+ readonly getLightFollowStatus: "robot.lightFollow.status";
55
+ }>;
56
+ /** 直控接口使用的 15 个关节名,顺序与设备遥测一致。 */
57
+ export declare const SERVO_JOINT_NAMES: readonly ["left_hip_yaw", "left_hip_roll", "left_hip_pitch", "left_knee", "left_ankle", "neck_pitch", "head_pitch", "head_yaw", "head_roll", "mouth", "right_hip_yaw", "right_hip_roll", "right_hip_pitch", "right_knee", "right_ankle"];
58
+ /** `robot.servo.state` 和 `robot.servo.set` 回执使用的关节名。 */
59
+ export type ServoJointName = typeof SERVO_JOINT_NAMES[number];
60
+ /** 舵机写入接受的腿部遥测名及对应的标准关节名。 */
61
+ declare const servoJointAliases: {
62
+ readonly L_hip_yaw: "left_hip_yaw";
63
+ readonly L_hip_roll: "left_hip_roll";
64
+ readonly L_hip_pitch: "left_hip_pitch";
65
+ readonly L_knee: "left_knee";
66
+ readonly L_ankle: "left_ankle";
67
+ readonly R_hip_yaw: "right_hip_yaw";
68
+ readonly R_hip_roll: "right_hip_roll";
69
+ readonly R_hip_pitch: "right_hip_pitch";
70
+ readonly R_knee: "right_knee";
71
+ readonly R_ankle: "right_ankle";
72
+ };
73
+ /** 写关节目标时额外接受的腿部遥测关节名。 */
74
+ export type ServoJointAlias = keyof typeof servoJointAliases;
75
+ /** 舵机直控的单次写入参数。 */
76
+ export interface SetServoJointsParams {
77
+ /** 1~15 个关节的绝对目标角,单位 rad,必须为有限数。每个关节只能指定一次,行程由设备校验。 */
78
+ joints: Partial<Record<ServoJointName | ServoJointAlias, number>>;
79
+ }
80
+ /** 策略驱动开关。 */
81
+ export interface EnableParams {
82
+ /** true 交给策略驱动,false 收回策略并保持力矩。 */
83
+ on: boolean;
84
+ }
85
+ /** 头部关节目标。各项省略时由上位机按 0 处理。 */
86
+ export interface HeadParams {
87
+ /** 颈部俯仰,rad。上位机裁剪到 −1.571~1.047。 */
88
+ neck_pitch?: number;
89
+ /** 头部俯仰,rad。上位机裁剪到 ±1.571。 */
90
+ head_pitch?: number;
91
+ /** 头部左右转,rad。上位机裁剪到 ±2.967。 */
92
+ head_yaw?: number;
93
+ /** 头部侧倾,rad。上位机裁剪到 ±0.436。 */
94
+ head_roll?: number;
95
+ }
96
+ /** 躯干坐标系中的注视点,由设备求解头部角度。 */
97
+ export interface LookParams {
98
+ /** 向前,单位 m,须为有限数。 */
99
+ x: number;
100
+ /** 向左,单位 m,须为有限数。 */
101
+ y: number;
102
+ /** 向上,单位 m,须为有限数。 */
103
+ z: number;
104
+ /** 颈部俯仰,rad,省略时由设备按 0 处理。 */
105
+ neck_pitch?: number;
106
+ }
107
+ /** 嘴部开合目标。 */
108
+ export interface MouthParams {
109
+ /** 0 闭合、1 全开,须为有限数,由设备裁剪到 0~1。 */
110
+ open: number;
111
+ }
112
+ /** 身体姿态目标。省略的高度和倾角由设备按 0 处理。 */
113
+ export interface PoseParams {
114
+ /** 高度偏移,单位 m。上位机裁剪到 −0.025~0.010。 */
115
+ z?: number;
116
+ /** 左右倾角,rad。上位机裁剪到 ±0.2618。 */
117
+ roll?: number;
118
+ /** 前后倾角,rad。上位机裁剪到 ±0.2618。 */
119
+ pitch?: number;
120
+ /** 默认 true,保持姿态并抑制移动;false 解除保持并回中位。 */
121
+ active?: boolean;
122
+ }
123
+ /** 追光启动参数。 */
124
+ export interface LightFollowStartParams {
125
+ /** 是否允许前进。false 仅允许转向。省略时使用上位机配置,出厂默认 true。 */
126
+ allow_forward?: boolean;
127
+ }
128
+ /** SDK 发出的 Web 运动指令固定优先级。 */
129
+ export declare const WEB_PRIORITY = 3;
130
+ /** 判断设备方法是否参与运动优先级仲裁。 */
131
+ export declare function usesMotionPriority(method: string): boolean;
132
+ /** 新控制方法共用的参数校验,设备保留裁剪和执行状态判断。 */
133
+ export declare function controlParamsError(method: string, params: unknown): string | undefined;
134
+ /** robot.setMode 的参数。 */
135
+ export interface SetModeParams {
136
+ /** 目标运动模式。walk 为行走,roller 为轮滑,direct 为舵机直控;设备校验模式名称。 */
137
+ mode: string;
138
+ }
139
+ /** 单次速度设定;无自动续发,实际速度由上位机裁剪。 */
140
+ export interface MoveParams {
141
+ /** 前进/后退速度,单位 m/s,必须为有限数。 */
142
+ vx: number;
143
+ /** 横向速度,单位 m/s,必须为有限数;省略时发送 0。 */
144
+ vy?: number;
145
+ /** 偏航角速度,单位 rad/s,必须为有限数。 */
146
+ vyaw: number;
147
+ }
148
+ export {};
@@ -0,0 +1,164 @@
1
+ /** SDK、连接核心与上位机共用的方法名;发送前仍校验参数及请求归属。 */
2
+ export const ROBOT_METHODS = Object.freeze({
3
+ /** 发送一次速度设定。 */
4
+ move: 'robot.move',
5
+ /** 提交完整文本给板端智能体,回执表示受理。 */
6
+ invoke: 'robot.invoke',
7
+ /** 请求设备中断当前音频播放。 */
8
+ stopAudio: 'robot.audio.stop',
9
+ /** 列出设备上的用户动作。 */
10
+ listActions: 'robot.actions.list',
11
+ /** 按名称删除用户动作。 */
12
+ deleteAction: 'robot.actions.delete',
13
+ /** 读取设备配置文件快照。 */
14
+ getConfig: 'robot.config',
15
+ /** 切换行走、轮滑或舵机直控模式。 */
16
+ setMode: 'robot.setMode',
17
+ /** 给关节上电并回到 home 姿态。 */
18
+ init: 'robot.init',
19
+ /** 交给或收回策略驱动。 */
20
+ enable: 'robot.enable',
21
+ /** 设置头部关节目标。 */
22
+ head: 'robot.head',
23
+ /** 指定躯干坐标系中的注视点。 */
24
+ look: 'robot.look',
25
+ /** 设置嘴部开合。 */
26
+ mouth: 'robot.mouth',
27
+ /** 设置或解除身体姿态保持。 */
28
+ pose: 'robot.pose',
29
+ /** 读取板端舵机状态及当前关节角。 */
30
+ getServoState: 'robot.servo.state',
31
+ /** 在舵机直控模式下设置关节目标。 */
32
+ setServoJoints: 'robot.servo.set',
33
+ /** 切断关节力矩。 */
34
+ relax: 'robot.relax',
35
+ /** 请求停车。 */
36
+ stop: 'robot.stop',
37
+ /** 复位设备端允许远程恢复的故障。 */
38
+ reset: 'robot.reset',
39
+ /** 请求预置技能,wait 决定设备侧等待语义。 */
40
+ doSkill: 'robot.do',
41
+ /** 查询执行器状态快照。 */
42
+ getStatus: 'robot.status',
43
+ /** 查询设备协议与能力。 */
44
+ getCapabilities: 'robot.capabilities',
45
+ /** 订阅当前连接的头部 ToF 深度流。 */
46
+ subscribeTof: 'robot.tof.subscribe',
47
+ /** 停止当前连接的头部 ToF 深度流。 */
48
+ unsubscribeTof: 'robot.tof.unsubscribe',
49
+ /** 启动板端追光程序。 */
50
+ startLightFollow: 'robot.lightFollow.start',
51
+ /** 停止板端追光程序。 */
52
+ stopLightFollow: 'robot.lightFollow.stop',
53
+ /** 查询板端追光程序的安装和运行状态。 */
54
+ getLightFollowStatus: 'robot.lightFollow.status',
55
+ });
56
+ /** 直控接口使用的 15 个关节名,顺序与设备遥测一致。 */
57
+ export const SERVO_JOINT_NAMES = Object.freeze([
58
+ 'left_hip_yaw', 'left_hip_roll', 'left_hip_pitch', 'left_knee', 'left_ankle',
59
+ 'neck_pitch', 'head_pitch', 'head_yaw', 'head_roll', 'mouth',
60
+ 'right_hip_yaw', 'right_hip_roll', 'right_hip_pitch', 'right_knee', 'right_ankle',
61
+ ]);
62
+ /** 舵机写入接受的腿部遥测名及对应的标准关节名。 */
63
+ const servoJointAliases = {
64
+ L_hip_yaw: 'left_hip_yaw', L_hip_roll: 'left_hip_roll', L_hip_pitch: 'left_hip_pitch',
65
+ L_knee: 'left_knee', L_ankle: 'left_ankle',
66
+ R_hip_yaw: 'right_hip_yaw', R_hip_roll: 'right_hip_roll', R_hip_pitch: 'right_hip_pitch',
67
+ R_knee: 'right_knee', R_ankle: 'right_ankle',
68
+ };
69
+ const servoJointNames = new Set(SERVO_JOINT_NAMES);
70
+ function servoJointsError(joints) {
71
+ if (joints === null || typeof joints !== 'object' || Array.isArray(joints)) {
72
+ return 'robot.servo.set 需要 joints 对象';
73
+ }
74
+ const entries = Object.entries(joints);
75
+ if (entries.length === 0 || entries.length > SERVO_JOINT_NAMES.length) {
76
+ return 'robot.servo.set 需要 1~15 个关节';
77
+ }
78
+ const seen = new Set();
79
+ for (const [rawName, angle] of entries) {
80
+ const name = rawName.trim();
81
+ const joint = Object.hasOwn(servoJointAliases, name) ? servoJointAliases[name] : name;
82
+ if (!servoJointNames.has(joint))
83
+ return `未知舵机关节 ${rawName}`;
84
+ if (seen.has(joint))
85
+ return `舵机关节 ${joint} 重复指定`;
86
+ if (typeof angle !== 'number' || !Number.isFinite(angle))
87
+ return `舵机关节 ${joint} 的角度必须为有限数`;
88
+ seen.add(joint);
89
+ }
90
+ return undefined;
91
+ }
92
+ const controlFields = {
93
+ [ROBOT_METHODS.enable]: { on: 'boolean' },
94
+ [ROBOT_METHODS.head]: { neck_pitch: 'number?', head_pitch: 'number?', head_yaw: 'number?', head_roll: 'number?' },
95
+ [ROBOT_METHODS.look]: { x: 'number', y: 'number', z: 'number', neck_pitch: 'number?' },
96
+ [ROBOT_METHODS.mouth]: { open: 'number' },
97
+ [ROBOT_METHODS.pose]: { z: 'number?', roll: 'number?', pitch: 'number?', active: 'boolean?' },
98
+ };
99
+ /** SDK 发出的 Web 运动指令固定优先级。 */
100
+ export const WEB_PRIORITY = 3;
101
+ const motionPriorityMethods = new Set([
102
+ ROBOT_METHODS.move, ROBOT_METHODS.head, ROBOT_METHODS.look, ROBOT_METHODS.pose,
103
+ ]);
104
+ /** 判断设备方法是否参与运动优先级仲裁。 */
105
+ export function usesMotionPriority(method) {
106
+ return motionPriorityMethods.has(method);
107
+ }
108
+ /** 新控制方法共用的参数校验,设备保留裁剪和执行状态判断。 */
109
+ export function controlParamsError(method, params) {
110
+ const object = params !== null && typeof params === 'object' && !Array.isArray(params)
111
+ ? params : undefined;
112
+ if (object?.priority !== undefined && !usesMotionPriority(method))
113
+ return `${method} 不接受 priority`;
114
+ if (method === ROBOT_METHODS.init || method === ROBOT_METHODS.getServoState || method === ROBOT_METHODS.relax
115
+ || method === ROBOT_METHODS.unsubscribeTof || method === ROBOT_METHODS.stopLightFollow
116
+ || method === ROBOT_METHODS.getLightFollowStatus) {
117
+ return params == null || (object && Object.keys(object).length === 0) ? undefined : `${method} 不接受参数`;
118
+ }
119
+ if (method === ROBOT_METHODS.startLightFollow) {
120
+ if (params == null)
121
+ return undefined;
122
+ if (!object || Object.keys(object).some(key => key !== 'allow_forward')) {
123
+ return `${method} 只接受 allow_forward 参数`;
124
+ }
125
+ if (object.allow_forward !== undefined && typeof object.allow_forward !== 'boolean') {
126
+ return `${method} 的 allow_forward 必须是布尔值`;
127
+ }
128
+ return undefined;
129
+ }
130
+ if (method === ROBOT_METHODS.subscribeTof) {
131
+ if (params == null)
132
+ return undefined;
133
+ if (!object || Object.keys(object).some(key => key !== 'hz'))
134
+ return 'robot.tof.subscribe 只接受 hz 参数';
135
+ const hz = object.hz;
136
+ return hz === undefined || (typeof hz === 'number' && Number.isInteger(hz) && hz >= 1 && hz <= 15)
137
+ ? undefined : 'robot.tof.subscribe 的 hz 必须是 1~15 的整数';
138
+ }
139
+ if (method === ROBOT_METHODS.setServoJoints) {
140
+ if (!object || Object.keys(object).length !== 1 || !Object.hasOwn(object, 'joints')) {
141
+ return 'robot.servo.set 只接受 joints 参数';
142
+ }
143
+ return servoJointsError(object.joints);
144
+ }
145
+ if (method === ROBOT_METHODS.invoke) {
146
+ return object && Object.keys(object).length === 1 && typeof object.text === 'string' && object.text.trim()
147
+ ? undefined : 'robot.invoke 只接受非空 text 参数';
148
+ }
149
+ if (!Object.hasOwn(controlFields, method))
150
+ return undefined;
151
+ if (!object && !(params === undefined && (method === ROBOT_METHODS.head || method === ROBOT_METHODS.pose))) {
152
+ return `${method} 需要对象参数`;
153
+ }
154
+ for (const [field, rule] of Object.entries(controlFields[method])) {
155
+ const value = object?.[field];
156
+ if (value === undefined && rule.endsWith('?'))
157
+ continue;
158
+ const valid = rule.startsWith('number') ? typeof value === 'number' && Number.isFinite(value)
159
+ : typeof value === 'boolean';
160
+ if (!valid)
161
+ return `${method} 的 ${field} 参数无效`;
162
+ }
163
+ return undefined;
164
+ }
@@ -0,0 +1,42 @@
1
+ import type { TofSubscribeResult, TofUnsubscribeResult } from './robot-tof.ts';
2
+ import type { ActionListResult, DeleteActionResult, RobotConfigResult, AcceptedResult, ControlResult, SkillResult, RobotStatus, RobotCapabilities, RobotTelemetry, RobotFault, HeadResult, LightFollowResult, LightFollowStatus, LookResult, MouthResult, PoseResult, ServoStateResult, SetServoJointsResult } from './robot-types.ts';
3
+ /** 校验用户动作列表,保留设备返回的文件元数据。 */
4
+ export declare function readActionList(value: unknown): ActionListResult;
5
+ /** 校验删除回执对应的动作名与文件大小。 */
6
+ export declare function readDeletedAction(value: unknown, name: string): DeleteActionResult;
7
+ /** 校验只读配置快照,保留设备上报的缺失文件、读取或解析错误。 */
8
+ export declare function readRobotConfig(value: unknown): RobotConfigResult;
9
+ /** 校验 move 和 stop 的 accepted:true 回执;非法结构抛出 INVALID_RESPONSE。 */
10
+ export declare function readAccepted(value: unknown): AcceptedResult;
11
+ /** 校验受理状态和设备给出的原因,保留指令专有字段。 */
12
+ export declare function readControlResult(value: unknown, method: string): ControlResult & Record<string, unknown>;
13
+ /** 读取设备采用的头部目标。 */
14
+ export declare function readHead(value: unknown): HeadResult;
15
+ /** 读取设备求解出的头部目标和可达性。 */
16
+ export declare function readLook(value: unknown): LookResult;
17
+ /** 读取设备采用的开合值。 */
18
+ export declare function readMouth(value: unknown): MouthResult;
19
+ /** 读取设备采用的姿态和保持状态。 */
20
+ export declare function readPose(value: unknown): PoseResult;
21
+ /** 校验板端报告的模式、使能和关节角。 */
22
+ export declare function readServoState(value: unknown): ServoStateResult;
23
+ /** 校验直控写入的受理状态和目标值。 */
24
+ export declare function readSetServoJoints(value: unknown): SetServoJointsResult;
25
+ /** 校验技能受理及可选完成字段,将 reason:null 归一为省略;非法结构抛出 INVALID_RESPONSE。 */
26
+ export declare function readSkill(value: unknown): SkillResult;
27
+ /** 校验执行器状态及故障原因,保留附加字段;非法结构抛出 INVALID_RESPONSE。 */
28
+ export declare function readStatus(value: unknown): RobotStatus;
29
+ /** 校验 v1 的 15 自由度能力清单及媒体标志;非法结构抛出 INVALID_RESPONSE。 */
30
+ export declare function readCapabilities(value: unknown): RobotCapabilities;
31
+ /** 校验追光启停回执和对应的运行状态。 */
32
+ export declare function readLightFollowResult(value: unknown, running: boolean): LightFollowResult;
33
+ /** 校验追光安装和运行状态回执。 */
34
+ export declare function readLightFollowStatus(value: unknown): LightFollowStatus;
35
+ /** 校验头部 ToF 订阅回执。 */
36
+ export declare function readTofSubscription(value: unknown): TofSubscribeResult;
37
+ /** 校验头部 ToF 退订回执。 */
38
+ export declare function readTofUnsubscription(value: unknown): TofUnsubscribeResult;
39
+ /** 校验 robot.state 的状态、15 关节数组、设备上报的速度分量和时间戳。 */
40
+ export declare function readTelemetry(value: unknown): RobotTelemetry;
41
+ /** 校验 state:FAULT 及字符串原因的故障通知;非法结构抛出 INVALID_RESPONSE。 */
42
+ export declare function readFault(value: unknown): RobotFault;