@honor-claw/yoyo 1.1.2 → 1.1.4-beta.1

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 (66) hide show
  1. package/index.ts +25 -25
  2. package/package.json +5 -5
  3. package/src/apis/claw-cloud.ts +124 -124
  4. package/src/apis/helpers.ts +10 -10
  5. package/src/apis/honor-auth.ts +158 -158
  6. package/src/apis/http-client.ts +239 -239
  7. package/src/apis/index.ts +8 -8
  8. package/src/apis/types.ts +77 -73
  9. package/src/cloud-channel/channel.ts +117 -117
  10. package/src/cloud-channel/client.ts +3 -3
  11. package/src/cloud-channel/index.ts +4 -4
  12. package/src/cloud-channel/message-handler.ts +50 -42
  13. package/src/cloud-channel/session-manager.ts +14 -9
  14. package/src/cloud-channel/types.ts +115 -115
  15. package/src/commands/env/impl.ts +58 -58
  16. package/src/commands/env/index.ts +1 -1
  17. package/src/commands/index.ts +30 -30
  18. package/src/commands/login/impl.ts +30 -30
  19. package/src/commands/login/index.ts +1 -1
  20. package/src/commands/logout/index.ts +1 -1
  21. package/src/commands/status/index.ts +194 -194
  22. package/src/gateway-client/client.ts +90 -90
  23. package/src/gateway-client/device/auth.ts +57 -57
  24. package/src/gateway-client/device/builder.ts +105 -105
  25. package/src/gateway-client/device/helpers.ts +40 -40
  26. package/src/gateway-client/device/identity.ts +251 -251
  27. package/src/gateway-client/device/index.ts +40 -40
  28. package/src/gateway-client/device/types.ts +57 -57
  29. package/src/gateway-client/index.ts +5 -5
  30. package/src/gateway-client/protocol-client.ts +49 -34
  31. package/src/honor-auth/browser.ts +2 -2
  32. package/src/honor-auth/callback-server.ts +109 -109
  33. package/src/honor-auth/cloud.ts +57 -57
  34. package/src/honor-auth/config.ts +43 -43
  35. package/src/honor-auth/index.ts +3 -3
  36. package/src/honor-auth/token-manager.ts +90 -90
  37. package/src/honor-auth/types.ts +50 -50
  38. package/src/index.ts +10 -10
  39. package/src/modules/claw-configs/config-manager.ts +409 -409
  40. package/src/modules/claw-configs/hosts.ts +48 -48
  41. package/src/modules/claw-configs/index.ts +8 -8
  42. package/src/modules/claw-configs/provider.ts +394 -394
  43. package/src/modules/claw-configs/types.ts +34 -34
  44. package/src/modules/device/device-info.ts +1 -1
  45. package/src/modules/device/index.ts +3 -3
  46. package/src/modules/device/providers/base.ts +32 -32
  47. package/src/modules/device/providers/pad.ts +107 -107
  48. package/src/modules/device/providers/windows.ts +130 -130
  49. package/src/modules/device/registry.ts +48 -43
  50. package/src/modules/login/impl.ts +31 -26
  51. package/src/modules/login/index.ts +6 -6
  52. package/src/schemas.ts +23 -23
  53. package/src/services/connection/impl.ts +339 -339
  54. package/src/services/connection/status-tracker/events.ts +127 -127
  55. package/src/services/connection/status-tracker/index.ts +31 -31
  56. package/src/services/connection/status-tracker/storage.ts +133 -133
  57. package/src/services/connection/status-tracker/tracker.ts +370 -370
  58. package/src/services/connection/status-tracker/types.ts +131 -131
  59. package/src/services/connection/types.ts +20 -20
  60. package/src/types.ts +64 -64
  61. package/src/utils/id.ts +8 -8
  62. package/src/utils/jwt.ts +37 -37
  63. package/src/utils/logger.ts +20 -20
  64. package/src/utils/proxy.ts +58 -58
  65. package/src/utils/version.ts +29 -29
  66. package/src/utils/ws.ts +21 -21
@@ -1,239 +1,239 @@
1
- /**
2
- * HTTP 请求客户端封装
3
- * 基于 undici 实现
4
- */
5
-
6
- import { request, type Dispatcher, ProxyAgent } from "undici";
7
- import { getProxyUrl, shouldUseProxy } from "../utils/proxy.js";
8
-
9
- /**
10
- * HTTP 客户端配置
11
- */
12
- export interface HttpClientOptions {
13
- defaultHeaders?: Record<string, string>;
14
- defaultTimeout?: number;
15
- /**
16
- * 代理地址,例如:http://127.0.0.1:7890
17
- * 如果不设置,则使用环境变量 HTTP_PROXY 或 HTTPS_PROXY
18
- */
19
- proxy?: string;
20
- }
21
-
22
- /**
23
- * HTTP 请求配置
24
- */
25
- export interface HttpRequestConfig {
26
- url: string;
27
- method?: "GET" | "POST" | "PUT" | "DELETE" | "PATCH";
28
- headers?: Record<string, string>;
29
- body?: string | object | null;
30
- timeout?: number;
31
- query?: Record<string, string | number | boolean | undefined>;
32
- /**
33
- * 是否禁用代理(默认 false)
34
- */
35
- disableProxy?: boolean;
36
- }
37
-
38
- /**
39
- * HTTP 响应
40
- */
41
- export interface HttpResponse<T = unknown> {
42
- data: T;
43
- status: number;
44
- headers: Record<string, string | string[] | undefined>;
45
- }
46
-
47
- /**
48
- * HTTP 错误
49
- */
50
- export class HttpError extends Error {
51
- constructor(public status: number, public data: unknown, message: string) {
52
- super(message);
53
- this.name = "HttpError";
54
- }
55
- }
56
-
57
- /**
58
- * HTTP 客户端类
59
- */
60
- export class HttpClient {
61
- private baseUrl: string;
62
- private defaultHeaders: Record<string, string>;
63
- private defaultTimeout: number;
64
- private proxyAgent?: ProxyAgent;
65
-
66
- constructor(baseUrl: string, options?: HttpClientOptions) {
67
- this.baseUrl = baseUrl.replace(/\/$/, "");
68
- this.defaultHeaders = options?.defaultHeaders || {};
69
- this.defaultTimeout = options?.defaultTimeout || 30000;
70
-
71
- // 初始化代理 agent,使用统一的代理配置获取逻辑
72
- const proxyUrl = getProxyUrl(options?.proxy);
73
-
74
- if (proxyUrl) {
75
- this.proxyAgent = new ProxyAgent(proxyUrl);
76
- }
77
- }
78
-
79
- /**
80
- * 执行 HTTP 请求
81
- */
82
- async request<T = unknown>(
83
- config: HttpRequestConfig
84
- ): Promise<HttpResponse<T>> {
85
- const {
86
- method = "GET",
87
- headers = {},
88
- body,
89
- timeout = this.defaultTimeout,
90
- query,
91
- } = config;
92
-
93
- // 构建 URL
94
- let url = config.url;
95
- if (!url.startsWith("http://") && !url.startsWith("https://")) {
96
- url = `${this.baseUrl}${url.startsWith("/") ? "" : "/"}${url}`;
97
- }
98
-
99
- // 添加查询参数
100
- if (query && Object.keys(query).length > 0) {
101
- const params = new URLSearchParams();
102
- Object.entries(query).forEach(([key, value]) => {
103
- if (value !== undefined) {
104
- params.append(key, String(value));
105
- }
106
- });
107
- const queryString = params.toString();
108
- if (queryString) {
109
- url += (url.includes("?") ? "&" : "?") + queryString;
110
- }
111
- }
112
-
113
- // 合并请求头
114
- const mergedHeaders: Record<string, string> = {
115
- ...this.defaultHeaders,
116
- ...headers,
117
- };
118
-
119
- // 设置 Content-Type
120
- if (body && !mergedHeaders["Content-Type"]) {
121
- mergedHeaders["Content-Type"] = "application/json";
122
- }
123
-
124
- // 准备请求体
125
- let requestBody: string | undefined;
126
- if (body !== null && body !== undefined) {
127
- if (typeof body === "string") {
128
- requestBody = body;
129
- } else {
130
- requestBody = JSON.stringify(body);
131
- }
132
- }
133
-
134
- try {
135
- const dispatcher: Dispatcher = shouldUseProxy(
136
- getProxyUrl(),
137
- config.disableProxy
138
- ) && this.proxyAgent
139
- ? this.proxyAgent
140
- : undefined;
141
-
142
- const response = await request(url, {
143
- method,
144
- headers: mergedHeaders as HeadersInit,
145
- body: requestBody,
146
- headersTimeout: timeout,
147
- bodyTimeout: timeout,
148
- dispatcher,
149
- });
150
-
151
- // 读取响应体
152
- const responseBody = await response.body.text();
153
- let data: T;
154
-
155
- try {
156
- data = responseBody ? JSON.parse(responseBody) : ({} as T);
157
- } catch {
158
- data = responseBody as unknown as T;
159
- }
160
-
161
- // 检查响应状态
162
- if (response.statusCode >= 200 && response.statusCode < 300) {
163
- return {
164
- data,
165
- status: response.statusCode,
166
- headers: response.headers as Record<
167
- string,
168
- string | string[] | undefined
169
- >,
170
- };
171
- } else {
172
- throw new HttpError(
173
- response.statusCode,
174
- data,
175
- `HTTP request failed with status ${response.statusCode}`
176
- );
177
- }
178
- } catch (error) {
179
- if (error instanceof HttpError) {
180
- throw error;
181
- }
182
- throw new Error(
183
- `HTTP request failed: ${
184
- error instanceof Error ? error.message : String(error)
185
- }`
186
- );
187
- }
188
- }
189
-
190
- /**
191
- * GET 请求
192
- */
193
- async get<T = unknown>(
194
- url: string,
195
- config?: Omit<HttpRequestConfig, "url" | "method">
196
- ): Promise<HttpResponse<T>> {
197
- return this.request<T>({ ...config, url, method: "GET" });
198
- }
199
-
200
- /**
201
- * POST 请求
202
- */
203
- async post<T = unknown>(
204
- url: string,
205
- config?: Omit<HttpRequestConfig, "url" | "method">
206
- ): Promise<HttpResponse<T>> {
207
- return this.request<T>({ ...config, url, method: "POST" });
208
- }
209
-
210
- /**
211
- * PUT 请求
212
- */
213
- async put<T = unknown>(
214
- url: string,
215
- config?: Omit<HttpRequestConfig, "url" | "method">
216
- ): Promise<HttpResponse<T>> {
217
- return this.request<T>({ ...config, url, method: "PUT" });
218
- }
219
-
220
- /**
221
- * DELETE 请求
222
- */
223
- async delete<T = unknown>(
224
- url: string,
225
- config?: Omit<HttpRequestConfig, "url" | "method">
226
- ): Promise<HttpResponse<T>> {
227
- return this.request<T>({ ...config, url, method: "DELETE" });
228
- }
229
-
230
- /**
231
- * PATCH 请求
232
- */
233
- async patch<T = unknown>(
234
- url: string,
235
- config?: Omit<HttpRequestConfig, "url" | "method">
236
- ): Promise<HttpResponse<T>> {
237
- return this.request<T>({ ...config, url, method: "PATCH" });
238
- }
239
- }
1
+ /**
2
+ * HTTP 请求客户端封装
3
+ * 基于 undici 实现
4
+ */
5
+
6
+ import { request, type Dispatcher, ProxyAgent } from "undici";
7
+ import { getProxyUrl, shouldUseProxy } from "../utils/proxy.js";
8
+
9
+ /**
10
+ * HTTP 客户端配置
11
+ */
12
+ export interface HttpClientOptions {
13
+ defaultHeaders?: Record<string, string>;
14
+ defaultTimeout?: number;
15
+ /**
16
+ * 代理地址,例如:http://127.0.0.1:7890
17
+ * 如果不设置,则使用环境变量 HTTP_PROXY 或 HTTPS_PROXY
18
+ */
19
+ proxy?: string;
20
+ }
21
+
22
+ /**
23
+ * HTTP 请求配置
24
+ */
25
+ export interface HttpRequestConfig {
26
+ url: string;
27
+ method?: "GET" | "POST" | "PUT" | "DELETE" | "PATCH";
28
+ headers?: Record<string, string>;
29
+ body?: string | object | null;
30
+ timeout?: number;
31
+ query?: Record<string, string | number | boolean | undefined>;
32
+ /**
33
+ * 是否禁用代理(默认 false)
34
+ */
35
+ disableProxy?: boolean;
36
+ }
37
+
38
+ /**
39
+ * HTTP 响应
40
+ */
41
+ export interface HttpResponse<T = unknown> {
42
+ data: T;
43
+ status: number;
44
+ headers: Record<string, string | string[] | undefined>;
45
+ }
46
+
47
+ /**
48
+ * HTTP 错误
49
+ */
50
+ export class HttpError extends Error {
51
+ constructor(public status: number, public data: unknown, message: string) {
52
+ super(message);
53
+ this.name = "HttpError";
54
+ }
55
+ }
56
+
57
+ /**
58
+ * HTTP 客户端类
59
+ */
60
+ export class HttpClient {
61
+ private baseUrl: string;
62
+ private defaultHeaders: Record<string, string>;
63
+ private defaultTimeout: number;
64
+ private proxyAgent?: ProxyAgent;
65
+
66
+ constructor(baseUrl: string, options?: HttpClientOptions) {
67
+ this.baseUrl = baseUrl.replace(/\/$/, "");
68
+ this.defaultHeaders = options?.defaultHeaders || {};
69
+ this.defaultTimeout = options?.defaultTimeout || 30000;
70
+
71
+ // 初始化代理 agent,使用统一的代理配置获取逻辑
72
+ const proxyUrl = getProxyUrl(options?.proxy);
73
+
74
+ if (proxyUrl) {
75
+ this.proxyAgent = new ProxyAgent(proxyUrl);
76
+ }
77
+ }
78
+
79
+ /**
80
+ * 执行 HTTP 请求
81
+ */
82
+ async request<T = unknown>(
83
+ config: HttpRequestConfig
84
+ ): Promise<HttpResponse<T>> {
85
+ const {
86
+ method = "GET",
87
+ headers = {},
88
+ body,
89
+ timeout = this.defaultTimeout,
90
+ query,
91
+ } = config;
92
+
93
+ // 构建 URL
94
+ let url = config.url;
95
+ if (!url.startsWith("http://") && !url.startsWith("https://")) {
96
+ url = `${this.baseUrl}${url.startsWith("/") ? "" : "/"}${url}`;
97
+ }
98
+
99
+ // 添加查询参数
100
+ if (query && Object.keys(query).length > 0) {
101
+ const params = new URLSearchParams();
102
+ Object.entries(query).forEach(([key, value]) => {
103
+ if (value !== undefined) {
104
+ params.append(key, String(value));
105
+ }
106
+ });
107
+ const queryString = params.toString();
108
+ if (queryString) {
109
+ url += (url.includes("?") ? "&" : "?") + queryString;
110
+ }
111
+ }
112
+
113
+ // 合并请求头
114
+ const mergedHeaders: Record<string, string> = {
115
+ ...this.defaultHeaders,
116
+ ...headers,
117
+ };
118
+
119
+ // 设置 Content-Type
120
+ if (body && !mergedHeaders["Content-Type"]) {
121
+ mergedHeaders["Content-Type"] = "application/json";
122
+ }
123
+
124
+ // 准备请求体
125
+ let requestBody: string | undefined;
126
+ if (body !== null && body !== undefined) {
127
+ if (typeof body === "string") {
128
+ requestBody = body;
129
+ } else {
130
+ requestBody = JSON.stringify(body);
131
+ }
132
+ }
133
+
134
+ try {
135
+ const dispatcher: Dispatcher = shouldUseProxy(
136
+ getProxyUrl(),
137
+ config.disableProxy
138
+ ) && this.proxyAgent
139
+ ? this.proxyAgent
140
+ : undefined;
141
+
142
+ const response = await request(url, {
143
+ method,
144
+ headers: mergedHeaders as HeadersInit,
145
+ body: requestBody,
146
+ headersTimeout: timeout,
147
+ bodyTimeout: timeout,
148
+ dispatcher,
149
+ });
150
+
151
+ // 读取响应体
152
+ const responseBody = await response.body.text();
153
+ let data: T;
154
+
155
+ try {
156
+ data = responseBody ? JSON.parse(responseBody) : ({} as T);
157
+ } catch {
158
+ data = responseBody as unknown as T;
159
+ }
160
+
161
+ // 检查响应状态
162
+ if (response.statusCode >= 200 && response.statusCode < 300) {
163
+ return {
164
+ data,
165
+ status: response.statusCode,
166
+ headers: response.headers as Record<
167
+ string,
168
+ string | string[] | undefined
169
+ >,
170
+ };
171
+ } else {
172
+ throw new HttpError(
173
+ response.statusCode,
174
+ data,
175
+ `HTTP request failed with status ${response.statusCode}`
176
+ );
177
+ }
178
+ } catch (error) {
179
+ if (error instanceof HttpError) {
180
+ throw error;
181
+ }
182
+ throw new Error(
183
+ `HTTP request failed: ${
184
+ error instanceof Error ? error.message : String(error)
185
+ }`
186
+ );
187
+ }
188
+ }
189
+
190
+ /**
191
+ * GET 请求
192
+ */
193
+ async get<T = unknown>(
194
+ url: string,
195
+ config?: Omit<HttpRequestConfig, "url" | "method">
196
+ ): Promise<HttpResponse<T>> {
197
+ return this.request<T>({ ...config, url, method: "GET" });
198
+ }
199
+
200
+ /**
201
+ * POST 请求
202
+ */
203
+ async post<T = unknown>(
204
+ url: string,
205
+ config?: Omit<HttpRequestConfig, "url" | "method">
206
+ ): Promise<HttpResponse<T>> {
207
+ return this.request<T>({ ...config, url, method: "POST" });
208
+ }
209
+
210
+ /**
211
+ * PUT 请求
212
+ */
213
+ async put<T = unknown>(
214
+ url: string,
215
+ config?: Omit<HttpRequestConfig, "url" | "method">
216
+ ): Promise<HttpResponse<T>> {
217
+ return this.request<T>({ ...config, url, method: "PUT" });
218
+ }
219
+
220
+ /**
221
+ * DELETE 请求
222
+ */
223
+ async delete<T = unknown>(
224
+ url: string,
225
+ config?: Omit<HttpRequestConfig, "url" | "method">
226
+ ): Promise<HttpResponse<T>> {
227
+ return this.request<T>({ ...config, url, method: "DELETE" });
228
+ }
229
+
230
+ /**
231
+ * PATCH 请求
232
+ */
233
+ async patch<T = unknown>(
234
+ url: string,
235
+ config?: Omit<HttpRequestConfig, "url" | "method">
236
+ ): Promise<HttpResponse<T>> {
237
+ return this.request<T>({ ...config, url, method: "PATCH" });
238
+ }
239
+ }
package/src/apis/index.ts CHANGED
@@ -1,8 +1,8 @@
1
- /**
2
- * Claw Cloud API 模块
3
- */
4
- export { createClawCloudClient, ClawCloudClient } from './claw-cloud.js';
5
- export { HttpClient, HttpError, type HttpRequestConfig, type HttpResponse } from './http-client.js';
6
- export { createHonorAuthClient, HonorAuthClient } from './honor-auth.js';
7
- export * from './types.js';
8
- export * from './helpers.js';
1
+ /**
2
+ * Claw Cloud API 模块
3
+ */
4
+ export { createClawCloudClient, ClawCloudClient } from './claw-cloud.js';
5
+ export { HttpClient, HttpError, type HttpRequestConfig, type HttpResponse } from './http-client.js';
6
+ export { createHonorAuthClient, HonorAuthClient } from './honor-auth.js';
7
+ export * from './types.js';
8
+ export * from './helpers.js';
package/src/apis/types.ts CHANGED
@@ -1,73 +1,77 @@
1
- /**
2
- * 设备注册接口相关类型定义
3
- */
4
- import type { ClawDeviceInfo, DeviceRole } from "../types.js";
5
-
6
- /**
7
- * 设备注册请求体
8
- */
9
- export interface DeviceRegistryRequest {
10
- businessTag: "YOYO_CLAW";
11
- role: DeviceRole;
12
- deviceInfo: Omit<ClawDeviceInfo, "role">;
13
- }
14
-
15
- export interface HttpApiWrapper<T = any> {
16
- code: string;
17
- success: boolean;
18
- cnMessage: string;
19
- data?: T;
20
- }
21
-
22
- /**
23
- * 设备注册响应体
24
- */
25
- export type DeviceRegistryResponse = HttpApiWrapper;
26
-
27
- /**
28
- * 设备注册请求头
29
- */
30
- export interface DeviceRegistryHeaders {
31
- "x-trace-id": string;
32
- "x-udid"?: string;
33
- "x-device-id"?: string;
34
- /**
35
- * token信息
36
- */
37
- "x-jwt-token"?: string;
38
- /**
39
- * 用户id加密后的信息
40
- */
41
- authorization?: string;
42
- /**
43
- * 端用,claw侧不需要
44
- */
45
- // authorization?: string;
46
- // debug mode
47
- "x-agw-userId"?: string;
48
- }
49
-
50
- /**
51
- * 用户登出请求体
52
- */
53
- export interface UserLogoutRequest {
54
- businessTag: "YOYO_CLAW";
55
- deviceInfo: Omit<ClawDeviceInfo, "role">;
56
- }
57
-
58
- /**
59
- * 用户登出响应体
60
- */
61
- export type UserLogoutResponse = HttpApiWrapper;
62
-
63
- /**
64
- * 用户登出请求头
65
- */
66
- export interface UserLogoutHeaders {
67
- "x-trace-id": string;
68
- "x-udid"?: string;
69
- "x-device-id"?: string;
70
- "x-jwt-token"?: string;
71
- authorization?: string;
72
- "x-agw-userId"?: string;
73
- }
1
+ /**
2
+ * 设备注册接口相关类型定义
3
+ */
4
+ import type { ClawDeviceInfo, DeviceRole } from "../types.js";
5
+
6
+ /**
7
+ * 设备注册请求体
8
+ */
9
+ export interface DeviceRegistryRequest {
10
+ businessTag: "YOYO_CLAW";
11
+ role: DeviceRole;
12
+ deviceInfo: Omit<ClawDeviceInfo, "role"> & {
13
+ manufacture: string;
14
+ };
15
+ }
16
+
17
+ export interface HttpApiWrapper<T = unknown> {
18
+ code: string;
19
+ success: boolean;
20
+ cnMessage: string;
21
+ data?: T;
22
+ }
23
+
24
+ /**
25
+ * 设备注册响应体
26
+ */
27
+ export type DeviceRegistryResponse = HttpApiWrapper;
28
+
29
+ /**
30
+ * 设备注册请求头
31
+ */
32
+ export interface DeviceRegistryHeaders {
33
+ "x-trace-id": string;
34
+ "x-udid"?: string;
35
+ "x-device-id"?: string;
36
+ /**
37
+ * token信息
38
+ */
39
+ "x-jwt-token"?: string;
40
+ /**
41
+ * 用户id加密后的信息
42
+ */
43
+ authorization?: string;
44
+ /**
45
+ * 端用,claw侧不需要
46
+ */
47
+ // authorization?: string;
48
+ // debug mode
49
+ "x-agw-userId"?: string;
50
+ }
51
+
52
+ /**
53
+ * 用户登出请求体
54
+ */
55
+ export interface UserLogoutRequest {
56
+ businessTag: "YOYO_CLAW";
57
+ deviceInfo: Omit<ClawDeviceInfo, "role"> & {
58
+ manufacture: string;
59
+ };
60
+ }
61
+
62
+ /**
63
+ * 用户登出响应体
64
+ */
65
+ export type UserLogoutResponse = HttpApiWrapper;
66
+
67
+ /**
68
+ * 用户登出请求头
69
+ */
70
+ export interface UserLogoutHeaders {
71
+ "x-trace-id": string;
72
+ "x-udid"?: string;
73
+ "x-device-id"?: string;
74
+ "x-jwt-token"?: string;
75
+ authorization?: string;
76
+ "x-agw-userId"?: string;
77
+ }