@blaxel/core 0.2.49-dev.213 → 0.2.49-dev1

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 (243) hide show
  1. package/dist/agents/index.d.ts +15 -0
  2. package/dist/agents/index.js +109 -0
  3. package/dist/authentication/apikey.d.ts +10 -0
  4. package/dist/authentication/apikey.js +24 -0
  5. package/dist/authentication/clientcredentials.d.ts +17 -0
  6. package/dist/authentication/clientcredentials.js +85 -0
  7. package/dist/authentication/credentials.d.ts +6 -0
  8. package/dist/authentication/credentials.js +17 -0
  9. package/dist/authentication/deviceMode.d.ts +15 -0
  10. package/dist/authentication/deviceMode.js +70 -0
  11. package/dist/authentication/index.d.ts +3 -0
  12. package/dist/authentication/index.js +62 -0
  13. package/dist/authentication/types.d.ts +9 -0
  14. package/dist/authentication/types.js +2 -0
  15. package/dist/cache/index.d.ts +1 -0
  16. package/dist/cache/index.js +26 -0
  17. package/dist/cjs/.tsbuildinfo +1 -1
  18. package/dist/cjs/agents/index.js +2 -2
  19. package/dist/cjs/authentication/clientcredentials.js +1 -4
  20. package/dist/cjs/client/responseInterceptor.js +53 -0
  21. package/dist/cjs/client/sdk.gen.js +143 -3
  22. package/dist/cjs/common/autoload.js +7 -0
  23. package/dist/cjs/common/env.js +3 -0
  24. package/dist/cjs/common/internal.js +0 -40
  25. package/dist/cjs/common/settings.js +2 -5
  26. package/dist/cjs/common/version.js +6 -0
  27. package/dist/cjs/common/webhook.js +101 -0
  28. package/dist/cjs/jobs/executions.js +119 -0
  29. package/dist/cjs/jobs/jobs.js +2 -2
  30. package/dist/cjs/sandbox/action.js +2 -1
  31. package/dist/cjs/sandbox/index.js +1 -0
  32. package/dist/cjs/sandbox/interpreter.js +400 -0
  33. package/dist/cjs/sandbox/sandbox.js +10 -67
  34. package/dist/cjs/tools/mcpTool.js +2 -6
  35. package/dist/cjs/types/client/responseInterceptor.d.ts +12 -0
  36. package/dist/cjs/types/client/sdk.gen.d.ts +41 -1
  37. package/dist/cjs/types/client/types.gen.d.ts +599 -16
  38. package/dist/cjs/types/common/internal.d.ts +0 -2
  39. package/dist/cjs/types/common/settings.d.ts +0 -1
  40. package/dist/cjs/types/common/version.d.ts +2 -0
  41. package/dist/cjs/types/common/webhook.d.ts +89 -0
  42. package/dist/cjs/types/jobs/executions.d.ts +42 -0
  43. package/dist/cjs/types/sandbox/client/sdk.gen.d.ts +1 -1
  44. package/dist/cjs/types/sandbox/index.d.ts +1 -0
  45. package/dist/cjs/types/sandbox/interpreter.d.ts +71 -0
  46. package/dist/cjs/types/sandbox/sandbox.d.ts +6 -12
  47. package/dist/cjs/types/sandbox/types.d.ts +0 -3
  48. package/dist/cjs-browser/.tsbuildinfo +1 -1
  49. package/dist/cjs-browser/agents/index.js +2 -2
  50. package/dist/cjs-browser/authentication/clientcredentials.js +1 -4
  51. package/dist/cjs-browser/client/responseInterceptor.js +53 -0
  52. package/dist/cjs-browser/client/sdk.gen.js +143 -3
  53. package/dist/cjs-browser/common/autoload.js +7 -0
  54. package/dist/cjs-browser/common/env.js +3 -0
  55. package/dist/cjs-browser/common/internal.js +0 -40
  56. package/dist/cjs-browser/common/settings.js +2 -5
  57. package/dist/cjs-browser/common/version.js +6 -0
  58. package/dist/cjs-browser/common/webhook.js +101 -0
  59. package/dist/cjs-browser/jobs/executions.js +119 -0
  60. package/dist/cjs-browser/jobs/jobs.js +2 -2
  61. package/dist/cjs-browser/sandbox/action.js +2 -1
  62. package/dist/cjs-browser/sandbox/index.js +1 -0
  63. package/dist/cjs-browser/sandbox/interpreter.js +400 -0
  64. package/dist/cjs-browser/sandbox/sandbox.js +10 -67
  65. package/dist/cjs-browser/tools/mcpTool.js +2 -6
  66. package/dist/cjs-browser/types/client/responseInterceptor.d.ts +12 -0
  67. package/dist/cjs-browser/types/client/sdk.gen.d.ts +41 -1
  68. package/dist/cjs-browser/types/client/types.gen.d.ts +599 -16
  69. package/dist/cjs-browser/types/common/internal.d.ts +0 -2
  70. package/dist/cjs-browser/types/common/settings.d.ts +0 -1
  71. package/dist/cjs-browser/types/common/version.d.ts +2 -0
  72. package/dist/cjs-browser/types/common/webhook.d.ts +89 -0
  73. package/dist/cjs-browser/types/jobs/executions.d.ts +42 -0
  74. package/dist/cjs-browser/types/sandbox/client/sdk.gen.d.ts +1 -1
  75. package/dist/cjs-browser/types/sandbox/index.d.ts +1 -0
  76. package/dist/cjs-browser/types/sandbox/interpreter.d.ts +71 -0
  77. package/dist/cjs-browser/types/sandbox/sandbox.d.ts +6 -12
  78. package/dist/cjs-browser/types/sandbox/types.d.ts +0 -3
  79. package/dist/client/authentication.d.ts +25 -0
  80. package/dist/client/authentication.js +15 -0
  81. package/dist/client/client.d.ts +2 -0
  82. package/dist/client/client.gen.d.ts +12 -0
  83. package/dist/client/client.gen.js +8 -0
  84. package/dist/{cjs/sandbox/websocket/index.js → client/client.js} +1 -1
  85. package/dist/client/index.d.ts +2 -0
  86. package/dist/client/index.js +19 -0
  87. package/dist/client/interceptors.d.ts +3 -0
  88. package/dist/client/interceptors.js +17 -0
  89. package/dist/client/sdk.gen.d.ts +499 -0
  90. package/dist/client/sdk.gen.js +1751 -0
  91. package/dist/client/types.gen.d.ts +4546 -0
  92. package/dist/client/types.gen.js +4 -0
  93. package/dist/common/autoload.d.ts +3 -0
  94. package/dist/common/autoload.js +27 -0
  95. package/dist/common/env.d.ts +5 -0
  96. package/dist/common/env.js +42 -0
  97. package/dist/common/errors.d.ts +1 -0
  98. package/dist/common/errors.js +17 -0
  99. package/dist/common/internal.d.ts +4 -0
  100. package/dist/common/internal.js +188 -0
  101. package/dist/common/internal.test.d.ts +1 -0
  102. package/dist/common/internal.test.js +39 -0
  103. package/dist/common/logger.d.ts +25 -0
  104. package/dist/common/logger.js +69 -0
  105. package/dist/common/node.d.ts +5 -0
  106. package/dist/common/node.js +26 -0
  107. package/dist/common/settings.d.ts +31 -0
  108. package/dist/common/settings.js +170 -0
  109. package/dist/esm/.tsbuildinfo +1 -1
  110. package/dist/esm/agents/index.js +3 -3
  111. package/dist/esm/authentication/clientcredentials.js +1 -4
  112. package/dist/esm/client/responseInterceptor.js +49 -0
  113. package/dist/esm/client/sdk.gen.js +132 -0
  114. package/dist/esm/common/autoload.js +7 -0
  115. package/dist/esm/common/env.js +3 -0
  116. package/dist/esm/common/internal.js +0 -38
  117. package/dist/esm/common/settings.js +2 -5
  118. package/dist/esm/common/version.js +3 -0
  119. package/dist/esm/common/webhook.js +97 -0
  120. package/dist/esm/jobs/executions.js +114 -0
  121. package/dist/esm/jobs/jobs.js +3 -3
  122. package/dist/esm/sandbox/action.js +3 -2
  123. package/dist/esm/sandbox/index.js +1 -0
  124. package/dist/esm/sandbox/interpreter.js +396 -0
  125. package/dist/esm/sandbox/sandbox.js +10 -67
  126. package/dist/esm/tools/mcpTool.js +3 -7
  127. package/dist/esm-browser/.tsbuildinfo +1 -1
  128. package/dist/esm-browser/agents/index.js +3 -3
  129. package/dist/esm-browser/authentication/clientcredentials.js +1 -4
  130. package/dist/esm-browser/client/responseInterceptor.js +49 -0
  131. package/dist/esm-browser/client/sdk.gen.js +132 -0
  132. package/dist/esm-browser/common/autoload.js +7 -0
  133. package/dist/esm-browser/common/env.js +3 -0
  134. package/dist/esm-browser/common/internal.js +0 -38
  135. package/dist/esm-browser/common/settings.js +2 -5
  136. package/dist/esm-browser/common/version.js +3 -0
  137. package/dist/esm-browser/common/webhook.js +97 -0
  138. package/dist/esm-browser/jobs/executions.js +114 -0
  139. package/dist/esm-browser/jobs/jobs.js +3 -3
  140. package/dist/esm-browser/sandbox/action.js +3 -2
  141. package/dist/esm-browser/sandbox/index.js +1 -0
  142. package/dist/esm-browser/sandbox/interpreter.js +396 -0
  143. package/dist/esm-browser/sandbox/sandbox.js +10 -67
  144. package/dist/esm-browser/tools/mcpTool.js +3 -7
  145. package/dist/index.browser.test.d.ts +1 -0
  146. package/dist/index.browser.test.js +45 -0
  147. package/dist/index.d.ts +17 -0
  148. package/dist/index.js +33 -0
  149. package/dist/jobs/index.d.ts +3 -0
  150. package/dist/jobs/index.js +19 -0
  151. package/dist/jobs/jobs.d.ts +13 -0
  152. package/dist/jobs/jobs.js +90 -0
  153. package/dist/jobs/start.d.ts +1 -0
  154. package/dist/jobs/start.js +66 -0
  155. package/dist/jobs/types.d.ts +3 -0
  156. package/dist/jobs/types.js +2 -0
  157. package/dist/mcp/client.d.ts +28 -0
  158. package/dist/mcp/client.js +247 -0
  159. package/dist/{cjs-browser/types/sandbox/websocket → mcp}/index.d.ts +1 -0
  160. package/dist/{cjs-browser/sandbox/websocket → mcp}/index.js +1 -0
  161. package/dist/mcp/server.d.ts +24 -0
  162. package/dist/mcp/server.js +213 -0
  163. package/dist/models/index.d.ts +8 -0
  164. package/dist/models/index.js +31 -0
  165. package/dist/sandbox/action.d.ts +19 -0
  166. package/dist/sandbox/action.js +84 -0
  167. package/dist/sandbox/client/client.gen.d.ts +12 -0
  168. package/dist/sandbox/client/client.gen.js +6 -0
  169. package/dist/sandbox/client/index.d.ts +2 -0
  170. package/dist/sandbox/client/index.js +19 -0
  171. package/dist/sandbox/client/sdk.gen.d.ts +101 -0
  172. package/dist/sandbox/client/sdk.gen.js +289 -0
  173. package/dist/sandbox/client/types.gen.d.ts +578 -0
  174. package/dist/sandbox/client/types.gen.js +3 -0
  175. package/dist/{cjs/types/sandbox/filesystem/filesystem-ws.d.ts → sandbox/filesystem/filesystem.d.ts} +6 -18
  176. package/dist/sandbox/filesystem/filesystem.js +276 -0
  177. package/dist/sandbox/filesystem/index.d.ts +2 -0
  178. package/dist/sandbox/filesystem/index.js +18 -0
  179. package/dist/sandbox/filesystem/types.d.ts +15 -0
  180. package/dist/sandbox/filesystem/types.js +2 -0
  181. package/dist/sandbox/index.d.ts +4 -0
  182. package/dist/sandbox/index.js +36 -0
  183. package/dist/sandbox/network/index.d.ts +1 -0
  184. package/dist/sandbox/network/index.js +17 -0
  185. package/dist/sandbox/network/network.d.ts +5 -0
  186. package/dist/sandbox/network/network.js +10 -0
  187. package/dist/sandbox/preview.d.ts +37 -0
  188. package/dist/sandbox/preview.js +148 -0
  189. package/dist/sandbox/process/index.d.ts +1 -0
  190. package/dist/sandbox/process/index.js +17 -0
  191. package/dist/sandbox/process/process.d.ts +24 -0
  192. package/dist/sandbox/process/process.js +189 -0
  193. package/dist/sandbox/sandbox.d.ts +33 -0
  194. package/dist/sandbox/sandbox.js +178 -0
  195. package/dist/sandbox/session.d.ts +27 -0
  196. package/dist/sandbox/session.js +123 -0
  197. package/dist/sandbox/types.d.ts +51 -0
  198. package/dist/sandbox/types.js +81 -0
  199. package/dist/telemetry/telemetry.d.ts +62 -0
  200. package/dist/telemetry/telemetry.js +80 -0
  201. package/dist/tools/index.d.ts +11 -0
  202. package/dist/tools/index.js +52 -0
  203. package/dist/tools/mcpTool.d.ts +28 -0
  204. package/dist/tools/mcpTool.js +218 -0
  205. package/dist/tools/types.d.ts +8 -0
  206. package/dist/tools/types.js +2 -0
  207. package/dist/tools/zodSchema.d.ts +9 -0
  208. package/dist/tools/zodSchema.js +50 -0
  209. package/dist/volume/index.d.ts +23 -0
  210. package/dist/volume/index.js +113 -0
  211. package/package.json +2 -2
  212. package/dist/cjs/sandbox/codegen/codegen-ws.js +0 -30
  213. package/dist/cjs/sandbox/filesystem/filesystem-ws.js +0 -106
  214. package/dist/cjs/sandbox/network/network-ws.js +0 -12
  215. package/dist/cjs/sandbox/process/process-ws.js +0 -139
  216. package/dist/cjs/sandbox/websocket/client.js +0 -269
  217. package/dist/cjs/types/sandbox/codegen/codegen-ws.d.ts +0 -10
  218. package/dist/cjs/types/sandbox/network/network-ws.d.ts +0 -7
  219. package/dist/cjs/types/sandbox/process/process-ws.d.ts +0 -27
  220. package/dist/cjs/types/sandbox/websocket/client.d.ts +0 -48
  221. package/dist/cjs/types/sandbox/websocket/index.d.ts +0 -1
  222. package/dist/cjs-browser/sandbox/codegen/codegen-ws.js +0 -30
  223. package/dist/cjs-browser/sandbox/filesystem/filesystem-ws.js +0 -106
  224. package/dist/cjs-browser/sandbox/network/network-ws.js +0 -12
  225. package/dist/cjs-browser/sandbox/process/process-ws.js +0 -139
  226. package/dist/cjs-browser/sandbox/websocket/client.js +0 -269
  227. package/dist/cjs-browser/types/sandbox/codegen/codegen-ws.d.ts +0 -10
  228. package/dist/cjs-browser/types/sandbox/filesystem/filesystem-ws.d.ts +0 -35
  229. package/dist/cjs-browser/types/sandbox/network/network-ws.d.ts +0 -7
  230. package/dist/cjs-browser/types/sandbox/process/process-ws.d.ts +0 -27
  231. package/dist/cjs-browser/types/sandbox/websocket/client.d.ts +0 -48
  232. package/dist/esm/sandbox/codegen/codegen-ws.js +0 -26
  233. package/dist/esm/sandbox/filesystem/filesystem-ws.js +0 -102
  234. package/dist/esm/sandbox/network/network-ws.js +0 -8
  235. package/dist/esm/sandbox/process/process-ws.js +0 -135
  236. package/dist/esm/sandbox/websocket/client.js +0 -265
  237. package/dist/esm/sandbox/websocket/index.js +0 -1
  238. package/dist/esm-browser/sandbox/codegen/codegen-ws.js +0 -26
  239. package/dist/esm-browser/sandbox/filesystem/filesystem-ws.js +0 -102
  240. package/dist/esm-browser/sandbox/network/network-ws.js +0 -8
  241. package/dist/esm-browser/sandbox/process/process-ws.js +0 -135
  242. package/dist/esm-browser/sandbox/websocket/client.js +0 -265
  243. package/dist/esm-browser/sandbox/websocket/index.js +0 -1
@@ -1,102 +0,0 @@
1
- import { SandboxAction } from "../action.js";
2
- import { SandboxFileSystem } from "./filesystem.js";
3
- export class SandboxFileSystemWebSocket extends SandboxAction {
4
- process;
5
- wsClient;
6
- httpClient;
7
- constructor(sandbox, process, wsClient) {
8
- super(sandbox);
9
- this.process = process;
10
- this.wsClient = wsClient;
11
- // Create HTTP client for fallback operations
12
- this.httpClient = new SandboxFileSystem(sandbox, process);
13
- }
14
- async mkdir(path, permissions = "0755") {
15
- path = this.formatPath(path);
16
- const data = await this.wsClient.send("filesystem:create", {
17
- path,
18
- isDirectory: true,
19
- permissions,
20
- });
21
- return data;
22
- }
23
- async write(path, content) {
24
- path = this.formatPath(path);
25
- const data = await this.wsClient.send("filesystem:create", {
26
- path,
27
- content,
28
- isDirectory: false,
29
- });
30
- return data;
31
- }
32
- async writeBinary(path, content) {
33
- return this.httpClient.writeBinary(path, content);
34
- }
35
- async writeTree(files, destinationPath = null) {
36
- const path = this.formatPath(destinationPath ?? "");
37
- const filesMap = files.reduce((acc, file) => {
38
- acc[file.path] = file.content;
39
- return acc;
40
- }, {});
41
- const data = await this.wsClient.send("filesystem:tree:create", {
42
- path,
43
- files: filesMap,
44
- });
45
- return data;
46
- }
47
- async read(path) {
48
- path = this.formatPath(path);
49
- const data = await this.wsClient.send("filesystem:get", { path });
50
- return data.content;
51
- }
52
- async readBinary(path) {
53
- // Binary downloads are better suited for HTTP
54
- // Fall back to HTTP client for binary operations
55
- return this.httpClient.readBinary(path);
56
- }
57
- async download(src, destinationPath, options = {}) {
58
- // File downloads are better suited for HTTP
59
- // Fall back to HTTP client
60
- return this.httpClient.download(src, destinationPath, options);
61
- }
62
- async rm(path, recursive = false) {
63
- path = this.formatPath(path);
64
- const data = await this.wsClient.send("filesystem:delete", {
65
- path,
66
- recursive,
67
- });
68
- return data;
69
- }
70
- async ls(path) {
71
- path = this.formatPath(path);
72
- const data = await this.wsClient.send("filesystem:get", { path });
73
- return data;
74
- }
75
- async cp(source, destination, { maxWait = 180000 } = {}) {
76
- // Copy operation is typically done via process execution
77
- // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
78
- let process = await this.process.exec({
79
- command: `cp -r ${source} ${destination}`,
80
- });
81
- // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment, @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-unsafe-argument
82
- process = await this.process.wait(process.pid, { maxWait, interval: 100 });
83
- // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
84
- if (process.status === "failed") {
85
- // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
86
- throw new Error(`Could not copy ${source} to ${destination} cause: ${process.logs}`);
87
- }
88
- return {
89
- message: "Files copied",
90
- source,
91
- destination,
92
- };
93
- }
94
- watch(path, callback, options) {
95
- // File watching uses HTTP streaming which is already optimized
96
- // Fall back to HTTP client
97
- return this.httpClient.watch(path, callback, options);
98
- }
99
- formatPath(path) {
100
- return path;
101
- }
102
- }
@@ -1,8 +0,0 @@
1
- import { SandboxAction } from "../action.js";
2
- export class SandboxNetworkWebSocket extends SandboxAction {
3
- wsClient;
4
- constructor(sandbox, wsClient) {
5
- super(sandbox);
6
- this.wsClient = wsClient;
7
- }
8
- }
@@ -1,135 +0,0 @@
1
- import { SandboxAction } from "../action.js";
2
- import { SandboxProcess } from "./process.js";
3
- export class SandboxProcessWebSocket extends SandboxAction {
4
- wsClient;
5
- httpClient;
6
- constructor(sandbox, wsClient) {
7
- super(sandbox);
8
- this.wsClient = wsClient;
9
- // Create HTTP client for fallback operations
10
- this.httpClient = new SandboxProcess(sandbox);
11
- }
12
- streamLogs(identifier, options) {
13
- const streamId = this.wsClient.sendStream("process:logs:stream:start", { identifier }, (data) => {
14
- // Handle streaming log data
15
- // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
16
- if (data && data.log) {
17
- // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
18
- const log = String(data.log);
19
- // Parse log format: "stdout:" or "stderr:" prefix
20
- if (log.startsWith('stdout:')) {
21
- const stdout = log.slice(7);
22
- options.onStdout?.(stdout);
23
- options.onLog?.(stdout);
24
- }
25
- else if (log.startsWith('stderr:')) {
26
- const stderr = log.slice(7);
27
- options.onStderr?.(stderr);
28
- options.onLog?.(stderr);
29
- }
30
- else {
31
- options.onLog?.(log);
32
- }
33
- }
34
- }, () => {
35
- // Stream ended
36
- });
37
- return {
38
- close: () => this.wsClient.cancelStream(streamId),
39
- };
40
- }
41
- async exec(process) {
42
- let onLog;
43
- if ('onLog' in process && process.onLog) {
44
- onLog = process.onLog;
45
- delete process.onLog;
46
- }
47
- // Store original wait_for_completion setting
48
- const shouldWaitForCompletion = process.waitForCompletion;
49
- // Always start process without wait_for_completion to avoid server-side blocking
50
- if (shouldWaitForCompletion && onLog) {
51
- process.waitForCompletion = false;
52
- }
53
- const data = await this.wsClient.send("process:execute", process);
54
- let result = data;
55
- // Handle wait_for_completion with parallel log streaming
56
- if (shouldWaitForCompletion && onLog) {
57
- const streamControl = this.streamLogs(result.pid, { onLog });
58
- try {
59
- // Wait for process completion
60
- result = await this.wait(result.pid, { interval: 500, maxWait: 1000 * 60 * 60 });
61
- }
62
- finally {
63
- // Clean up log streaming
64
- if (streamControl) {
65
- streamControl.close();
66
- }
67
- }
68
- }
69
- else {
70
- // For non-blocking execution, set up log streaming immediately if requested
71
- if (onLog) {
72
- const streamControl = this.streamLogs(result.pid, { onLog });
73
- return {
74
- ...result,
75
- close() {
76
- if (streamControl) {
77
- streamControl.close();
78
- }
79
- },
80
- };
81
- }
82
- }
83
- return { ...result, close: () => { } };
84
- }
85
- async wait(identifier, { maxWait = 60000, interval = 1000 } = {}) {
86
- const startTime = Date.now();
87
- let status = "running";
88
- let data = await this.get(identifier);
89
- while (status === "running") {
90
- await new Promise((resolve) => setTimeout(resolve, interval));
91
- try {
92
- data = await this.get(identifier);
93
- status = data.status ?? "running";
94
- }
95
- catch {
96
- break;
97
- }
98
- if (Date.now() - startTime > maxWait) {
99
- throw new Error("Process did not finish in time");
100
- }
101
- }
102
- return data;
103
- }
104
- async get(identifier) {
105
- const data = await this.wsClient.send("process:get", { identifier });
106
- return data;
107
- }
108
- async list() {
109
- const data = await this.wsClient.send("process:list", {});
110
- return data.processes || [];
111
- }
112
- async stop(identifier) {
113
- const data = await this.wsClient.send("process:stop", { identifier });
114
- return data;
115
- }
116
- async kill(identifier) {
117
- const data = await this.wsClient.send("process:kill", { identifier });
118
- return data;
119
- }
120
- async logs(identifier, type = "all") {
121
- const data = await this.wsClient.send("process:logs", {
122
- identifier,
123
- });
124
- if (type === "all") {
125
- return data.logs || "";
126
- }
127
- else if (type === "stdout") {
128
- return data.stdout || "";
129
- }
130
- else if (type === "stderr") {
131
- return data.stderr || "";
132
- }
133
- throw new Error("Unsupported log type");
134
- }
135
- }
@@ -1,265 +0,0 @@
1
- import { v4 as uuidv4 } from "uuid";
2
- import { getWebSocket } from "../../common/node.js";
3
- import { settings } from "../../common/settings.js";
4
- export class WebSocketClient {
5
- ws = null;
6
- WebSocketClass = null;
7
- url;
8
- reconnect;
9
- reconnectInterval;
10
- maxReconnectAttempts;
11
- reconnectAttempts = 0;
12
- pendingRequests = new Map();
13
- streamHandlers = new Map();
14
- isClosing = false;
15
- connectionPromise = null;
16
- heartbeatInterval = null;
17
- reconnectTimeout = null;
18
- lastPongReceived = Date.now();
19
- constructor(options) {
20
- this.url = options.url;
21
- this.reconnect = options.reconnect ?? true;
22
- this.reconnectInterval = options.reconnectInterval ?? 5000;
23
- this.maxReconnectAttempts = options.maxReconnectAttempts ?? 5;
24
- }
25
- async connect() {
26
- if (this.connectionPromise) {
27
- return this.connectionPromise;
28
- }
29
- this.connectionPromise = this.initializeConnection();
30
- return this.connectionPromise;
31
- }
32
- async initializeConnection() {
33
- return new Promise((resolve, reject) => {
34
- // Get WebSocket class and connect
35
- void (async () => {
36
- try {
37
- // Get WebSocket class if not already loaded
38
- if (!this.WebSocketClass) {
39
- // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
40
- this.WebSocketClass = await getWebSocket();
41
- }
42
- // Convert http/https URL to ws/wss
43
- let wsUrl = this.url;
44
- if (wsUrl.startsWith("http://")) {
45
- wsUrl = wsUrl.replace("http://", "ws://");
46
- }
47
- else if (wsUrl.startsWith("https://")) {
48
- wsUrl = wsUrl.replace("https://", "wss://");
49
- }
50
- // Add /ws endpoint if not present
51
- if (!wsUrl.endsWith("/ws")) {
52
- wsUrl = `${wsUrl}/ws`;
53
- }
54
- wsUrl = `${wsUrl}?token=${settings.token}`;
55
- // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment, @typescript-eslint/no-unsafe-call
56
- this.ws = new this.WebSocketClass(wsUrl);
57
- // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
58
- this.ws.onopen = () => {
59
- this.reconnectAttempts = 0;
60
- this.startHeartbeat();
61
- resolve();
62
- };
63
- this.ws.onmessage = (event) => {
64
- this.handleMessage(event);
65
- };
66
- this.ws.onerror = (error) => {
67
- console.error("WebSocket error:", error);
68
- reject(new Error("WebSocket connection error"));
69
- };
70
- // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
71
- this.ws.onclose = () => {
72
- this.stopHeartbeat();
73
- this.connectionPromise = null;
74
- if (!this.isClosing && this.reconnect && this.reconnectAttempts < this.maxReconnectAttempts) {
75
- this.reconnectAttempts++;
76
- this.reconnectTimeout = setTimeout(() => {
77
- this.connect().catch(console.error);
78
- }, this.reconnectInterval);
79
- // Allow process to exit even if reconnect timeout is pending
80
- if (this.reconnectTimeout.unref) {
81
- this.reconnectTimeout.unref();
82
- }
83
- }
84
- else {
85
- // Reject all pending requests
86
- this.pendingRequests.forEach(({ reject }) => {
87
- reject(new Error("WebSocket connection closed"));
88
- });
89
- this.pendingRequests.clear();
90
- }
91
- };
92
- // Handle pong messages for heartbeat
93
- // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
94
- if (typeof this.ws.on === 'function') {
95
- // eslint-disable-next-line @typescript-eslint/no-unsafe-call, @typescript-eslint/no-unsafe-member-access
96
- this.ws.on('pong', () => {
97
- this.lastPongReceived = Date.now();
98
- });
99
- }
100
- }
101
- catch (error) {
102
- reject(error instanceof Error ? error : new Error(String(error)));
103
- }
104
- })();
105
- });
106
- }
107
- startHeartbeat() {
108
- // Send ping every 30 seconds
109
- this.heartbeatInterval = setInterval(() => {
110
- // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
111
- if (this.ws && this.WebSocketClass && this.ws.readyState === this.WebSocketClass.OPEN) {
112
- // Check if we received a pong recently (within 60 seconds)
113
- if (Date.now() - this.lastPongReceived > 60000) {
114
- console.warn("WebSocket heartbeat timeout, closing connection");
115
- // eslint-disable-next-line @typescript-eslint/no-unsafe-call, @typescript-eslint/no-unsafe-member-access
116
- this.ws.close();
117
- return;
118
- }
119
- // Send ping
120
- // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
121
- if (typeof this.ws.ping === 'function') {
122
- // eslint-disable-next-line @typescript-eslint/no-unsafe-call, @typescript-eslint/no-unsafe-member-access
123
- this.ws.ping();
124
- }
125
- }
126
- }, 30000);
127
- // Allow process to exit even if heartbeat interval is active
128
- if (this.heartbeatInterval.unref) {
129
- this.heartbeatInterval.unref();
130
- }
131
- }
132
- stopHeartbeat() {
133
- if (this.heartbeatInterval) {
134
- clearInterval(this.heartbeatInterval);
135
- this.heartbeatInterval = null;
136
- }
137
- }
138
- handleMessage(event) {
139
- try {
140
- // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment, @typescript-eslint/no-unsafe-member-access
141
- const response = JSON.parse(String(event.data));
142
- // Check if this is a streaming response
143
- if (response.stream) {
144
- const streamHandler = this.streamHandlers.get(response.id);
145
- if (streamHandler) {
146
- // Call the data handler with the response data
147
- streamHandler.onData(response.data);
148
- // If stream is done, call end handler and clean up
149
- if (response.done) {
150
- streamHandler.onEnd();
151
- this.streamHandlers.delete(response.id);
152
- }
153
- }
154
- return;
155
- }
156
- // Regular request-response handling
157
- const pending = this.pendingRequests.get(response.id);
158
- if (pending) {
159
- this.pendingRequests.delete(response.id);
160
- if (response.success) {
161
- pending.resolve(response.data);
162
- }
163
- else {
164
- pending.reject(new Error(response.error || "Unknown error"));
165
- }
166
- }
167
- }
168
- catch (error) {
169
- console.error("Failed to parse WebSocket message:", error);
170
- }
171
- }
172
- async send(operation, data = {}) {
173
- // Ensure we're connected
174
- // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
175
- if (!this.ws || !this.WebSocketClass || this.ws.readyState !== this.WebSocketClass.OPEN) {
176
- await this.connect();
177
- }
178
- return new Promise((resolve, reject) => {
179
- const id = uuidv4();
180
- const message = {
181
- id,
182
- operation,
183
- data,
184
- };
185
- // Store the promise handlers
186
- this.pendingRequests.set(id, { resolve, reject });
187
- // Set a timeout for the request (60 seconds)
188
- setTimeout(() => {
189
- if (this.pendingRequests.has(id)) {
190
- this.pendingRequests.delete(id);
191
- reject(new Error("Request timeout"));
192
- }
193
- }, 60000);
194
- // Send the message
195
- // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
196
- if (this.ws && this.WebSocketClass && this.ws.readyState === this.WebSocketClass.OPEN) {
197
- // eslint-disable-next-line @typescript-eslint/no-unsafe-call, @typescript-eslint/no-unsafe-member-access
198
- this.ws.send(JSON.stringify(message));
199
- }
200
- else {
201
- this.pendingRequests.delete(id);
202
- reject(new Error("WebSocket not connected"));
203
- }
204
- });
205
- }
206
- sendStream(operation, data, onData, onEnd) {
207
- // Ensure we're connected
208
- // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
209
- if (!this.ws || !this.WebSocketClass || this.ws.readyState !== this.WebSocketClass.OPEN) {
210
- throw new Error("WebSocket not connected");
211
- }
212
- const id = uuidv4();
213
- const message = {
214
- id,
215
- operation,
216
- data,
217
- };
218
- // Store the stream handlers
219
- this.streamHandlers.set(id, { onData, onEnd });
220
- // Send the message
221
- // eslint-disable-next-line @typescript-eslint/no-unsafe-call, @typescript-eslint/no-unsafe-member-access
222
- this.ws.send(JSON.stringify(message));
223
- return id;
224
- }
225
- cancelStream(id) {
226
- this.streamHandlers.delete(id);
227
- }
228
- close() {
229
- this.isClosing = true;
230
- this.reconnect = false;
231
- this.stopHeartbeat();
232
- // Clear reconnect timeout if any
233
- if (this.reconnectTimeout) {
234
- clearTimeout(this.reconnectTimeout);
235
- this.reconnectTimeout = null;
236
- }
237
- if (this.ws) {
238
- // In Node.js (ws package), use terminate() to forcefully close the connection
239
- // This immediately closes the socket without waiting for the close handshake
240
- // In browser, terminate() doesn't exist, so we fall back to close()
241
- // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
242
- if (typeof this.ws.terminate === 'function') {
243
- // Node.js ws package - force immediate close
244
- // eslint-disable-next-line @typescript-eslint/no-unsafe-call, @typescript-eslint/no-unsafe-member-access
245
- this.ws.terminate();
246
- }
247
- else {
248
- // Browser WebSocket - graceful close
249
- // eslint-disable-next-line @typescript-eslint/no-unsafe-call, @typescript-eslint/no-unsafe-member-access
250
- this.ws.close();
251
- }
252
- this.ws = null;
253
- }
254
- // Reject all pending requests
255
- this.pendingRequests.forEach(({ reject }) => {
256
- reject(new Error("WebSocket client closed"));
257
- });
258
- this.pendingRequests.clear();
259
- this.connectionPromise = null;
260
- }
261
- get isConnected() {
262
- // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
263
- return this.ws !== null && this.WebSocketClass !== null && this.ws.readyState === this.WebSocketClass.OPEN;
264
- }
265
- }
@@ -1 +0,0 @@
1
- export * from "./client.js";