@blaxel/core 0.2.49-dev.214 → 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 (247) 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/jobs/start.js +1 -32
  31. package/dist/cjs/sandbox/action.js +2 -1
  32. package/dist/cjs/sandbox/index.js +1 -0
  33. package/dist/cjs/sandbox/interpreter.js +400 -0
  34. package/dist/cjs/sandbox/sandbox.js +10 -67
  35. package/dist/cjs/tools/mcpTool.js +2 -6
  36. package/dist/cjs/types/client/responseInterceptor.d.ts +12 -0
  37. package/dist/cjs/types/client/sdk.gen.d.ts +41 -1
  38. package/dist/cjs/types/client/types.gen.d.ts +599 -16
  39. package/dist/cjs/types/common/internal.d.ts +0 -2
  40. package/dist/cjs/types/common/settings.d.ts +0 -1
  41. package/dist/cjs/types/common/version.d.ts +2 -0
  42. package/dist/cjs/types/common/webhook.d.ts +89 -0
  43. package/dist/cjs/types/jobs/executions.d.ts +42 -0
  44. package/dist/cjs/types/sandbox/client/sdk.gen.d.ts +1 -1
  45. package/dist/cjs/types/sandbox/index.d.ts +1 -0
  46. package/dist/cjs/types/sandbox/interpreter.d.ts +71 -0
  47. package/dist/cjs/types/sandbox/sandbox.d.ts +6 -12
  48. package/dist/cjs/types/sandbox/types.d.ts +0 -3
  49. package/dist/cjs-browser/.tsbuildinfo +1 -1
  50. package/dist/cjs-browser/agents/index.js +2 -2
  51. package/dist/cjs-browser/authentication/clientcredentials.js +1 -4
  52. package/dist/cjs-browser/client/responseInterceptor.js +53 -0
  53. package/dist/cjs-browser/client/sdk.gen.js +143 -3
  54. package/dist/cjs-browser/common/autoload.js +7 -0
  55. package/dist/cjs-browser/common/env.js +3 -0
  56. package/dist/cjs-browser/common/internal.js +0 -40
  57. package/dist/cjs-browser/common/settings.js +2 -5
  58. package/dist/cjs-browser/common/version.js +6 -0
  59. package/dist/cjs-browser/common/webhook.js +101 -0
  60. package/dist/cjs-browser/jobs/executions.js +119 -0
  61. package/dist/cjs-browser/jobs/jobs.js +2 -2
  62. package/dist/cjs-browser/jobs/start.js +1 -32
  63. package/dist/cjs-browser/sandbox/action.js +2 -1
  64. package/dist/cjs-browser/sandbox/index.js +1 -0
  65. package/dist/cjs-browser/sandbox/interpreter.js +400 -0
  66. package/dist/cjs-browser/sandbox/sandbox.js +10 -67
  67. package/dist/cjs-browser/tools/mcpTool.js +2 -6
  68. package/dist/cjs-browser/types/client/responseInterceptor.d.ts +12 -0
  69. package/dist/cjs-browser/types/client/sdk.gen.d.ts +41 -1
  70. package/dist/cjs-browser/types/client/types.gen.d.ts +599 -16
  71. package/dist/cjs-browser/types/common/internal.d.ts +0 -2
  72. package/dist/cjs-browser/types/common/settings.d.ts +0 -1
  73. package/dist/cjs-browser/types/common/version.d.ts +2 -0
  74. package/dist/cjs-browser/types/common/webhook.d.ts +89 -0
  75. package/dist/cjs-browser/types/jobs/executions.d.ts +42 -0
  76. package/dist/cjs-browser/types/sandbox/client/sdk.gen.d.ts +1 -1
  77. package/dist/cjs-browser/types/sandbox/index.d.ts +1 -0
  78. package/dist/cjs-browser/types/sandbox/interpreter.d.ts +71 -0
  79. package/dist/cjs-browser/types/sandbox/sandbox.d.ts +6 -12
  80. package/dist/cjs-browser/types/sandbox/types.d.ts +0 -3
  81. package/dist/client/authentication.d.ts +25 -0
  82. package/dist/client/authentication.js +15 -0
  83. package/dist/client/client.d.ts +2 -0
  84. package/dist/client/client.gen.d.ts +12 -0
  85. package/dist/client/client.gen.js +8 -0
  86. package/dist/{cjs/sandbox/websocket/index.js → client/client.js} +1 -1
  87. package/dist/client/index.d.ts +2 -0
  88. package/dist/client/index.js +19 -0
  89. package/dist/client/interceptors.d.ts +3 -0
  90. package/dist/client/interceptors.js +17 -0
  91. package/dist/client/sdk.gen.d.ts +499 -0
  92. package/dist/client/sdk.gen.js +1751 -0
  93. package/dist/client/types.gen.d.ts +4546 -0
  94. package/dist/client/types.gen.js +4 -0
  95. package/dist/common/autoload.d.ts +3 -0
  96. package/dist/common/autoload.js +27 -0
  97. package/dist/common/env.d.ts +5 -0
  98. package/dist/common/env.js +42 -0
  99. package/dist/common/errors.d.ts +1 -0
  100. package/dist/common/errors.js +17 -0
  101. package/dist/common/internal.d.ts +4 -0
  102. package/dist/common/internal.js +188 -0
  103. package/dist/common/internal.test.d.ts +1 -0
  104. package/dist/common/internal.test.js +39 -0
  105. package/dist/common/logger.d.ts +25 -0
  106. package/dist/common/logger.js +69 -0
  107. package/dist/common/node.d.ts +5 -0
  108. package/dist/common/node.js +26 -0
  109. package/dist/common/settings.d.ts +31 -0
  110. package/dist/common/settings.js +170 -0
  111. package/dist/esm/.tsbuildinfo +1 -1
  112. package/dist/esm/agents/index.js +3 -3
  113. package/dist/esm/authentication/clientcredentials.js +1 -4
  114. package/dist/esm/client/responseInterceptor.js +49 -0
  115. package/dist/esm/client/sdk.gen.js +132 -0
  116. package/dist/esm/common/autoload.js +7 -0
  117. package/dist/esm/common/env.js +3 -0
  118. package/dist/esm/common/internal.js +0 -38
  119. package/dist/esm/common/settings.js +2 -5
  120. package/dist/esm/common/version.js +3 -0
  121. package/dist/esm/common/webhook.js +97 -0
  122. package/dist/esm/jobs/executions.js +114 -0
  123. package/dist/esm/jobs/jobs.js +3 -3
  124. package/dist/esm/jobs/start.js +1 -32
  125. package/dist/esm/sandbox/action.js +3 -2
  126. package/dist/esm/sandbox/index.js +1 -0
  127. package/dist/esm/sandbox/interpreter.js +396 -0
  128. package/dist/esm/sandbox/sandbox.js +10 -67
  129. package/dist/esm/tools/mcpTool.js +3 -7
  130. package/dist/esm-browser/.tsbuildinfo +1 -1
  131. package/dist/esm-browser/agents/index.js +3 -3
  132. package/dist/esm-browser/authentication/clientcredentials.js +1 -4
  133. package/dist/esm-browser/client/responseInterceptor.js +49 -0
  134. package/dist/esm-browser/client/sdk.gen.js +132 -0
  135. package/dist/esm-browser/common/autoload.js +7 -0
  136. package/dist/esm-browser/common/env.js +3 -0
  137. package/dist/esm-browser/common/internal.js +0 -38
  138. package/dist/esm-browser/common/settings.js +2 -5
  139. package/dist/esm-browser/common/version.js +3 -0
  140. package/dist/esm-browser/common/webhook.js +97 -0
  141. package/dist/esm-browser/jobs/executions.js +114 -0
  142. package/dist/esm-browser/jobs/jobs.js +3 -3
  143. package/dist/esm-browser/jobs/start.js +1 -32
  144. package/dist/esm-browser/sandbox/action.js +3 -2
  145. package/dist/esm-browser/sandbox/index.js +1 -0
  146. package/dist/esm-browser/sandbox/interpreter.js +396 -0
  147. package/dist/esm-browser/sandbox/sandbox.js +10 -67
  148. package/dist/esm-browser/tools/mcpTool.js +3 -7
  149. package/dist/index.browser.test.d.ts +1 -0
  150. package/dist/index.browser.test.js +45 -0
  151. package/dist/index.d.ts +17 -0
  152. package/dist/index.js +33 -0
  153. package/dist/jobs/index.d.ts +3 -0
  154. package/dist/jobs/index.js +19 -0
  155. package/dist/jobs/jobs.d.ts +13 -0
  156. package/dist/jobs/jobs.js +90 -0
  157. package/dist/jobs/start.d.ts +1 -0
  158. package/dist/jobs/start.js +66 -0
  159. package/dist/jobs/types.d.ts +3 -0
  160. package/dist/jobs/types.js +2 -0
  161. package/dist/mcp/client.d.ts +28 -0
  162. package/dist/mcp/client.js +247 -0
  163. package/dist/{cjs-browser/types/sandbox/websocket → mcp}/index.d.ts +1 -0
  164. package/dist/{cjs-browser/sandbox/websocket → mcp}/index.js +1 -0
  165. package/dist/mcp/server.d.ts +24 -0
  166. package/dist/mcp/server.js +213 -0
  167. package/dist/models/index.d.ts +8 -0
  168. package/dist/models/index.js +31 -0
  169. package/dist/sandbox/action.d.ts +19 -0
  170. package/dist/sandbox/action.js +84 -0
  171. package/dist/sandbox/client/client.gen.d.ts +12 -0
  172. package/dist/sandbox/client/client.gen.js +6 -0
  173. package/dist/sandbox/client/index.d.ts +2 -0
  174. package/dist/sandbox/client/index.js +19 -0
  175. package/dist/sandbox/client/sdk.gen.d.ts +101 -0
  176. package/dist/sandbox/client/sdk.gen.js +289 -0
  177. package/dist/sandbox/client/types.gen.d.ts +578 -0
  178. package/dist/sandbox/client/types.gen.js +3 -0
  179. package/dist/{cjs/types/sandbox/filesystem/filesystem-ws.d.ts → sandbox/filesystem/filesystem.d.ts} +6 -18
  180. package/dist/sandbox/filesystem/filesystem.js +276 -0
  181. package/dist/sandbox/filesystem/index.d.ts +2 -0
  182. package/dist/sandbox/filesystem/index.js +18 -0
  183. package/dist/sandbox/filesystem/types.d.ts +15 -0
  184. package/dist/sandbox/filesystem/types.js +2 -0
  185. package/dist/sandbox/index.d.ts +4 -0
  186. package/dist/sandbox/index.js +36 -0
  187. package/dist/sandbox/network/index.d.ts +1 -0
  188. package/dist/sandbox/network/index.js +17 -0
  189. package/dist/sandbox/network/network.d.ts +5 -0
  190. package/dist/sandbox/network/network.js +10 -0
  191. package/dist/sandbox/preview.d.ts +37 -0
  192. package/dist/sandbox/preview.js +148 -0
  193. package/dist/sandbox/process/index.d.ts +1 -0
  194. package/dist/sandbox/process/index.js +17 -0
  195. package/dist/sandbox/process/process.d.ts +24 -0
  196. package/dist/sandbox/process/process.js +189 -0
  197. package/dist/sandbox/sandbox.d.ts +33 -0
  198. package/dist/sandbox/sandbox.js +178 -0
  199. package/dist/sandbox/session.d.ts +27 -0
  200. package/dist/sandbox/session.js +123 -0
  201. package/dist/sandbox/types.d.ts +51 -0
  202. package/dist/sandbox/types.js +81 -0
  203. package/dist/telemetry/telemetry.d.ts +62 -0
  204. package/dist/telemetry/telemetry.js +80 -0
  205. package/dist/tools/index.d.ts +11 -0
  206. package/dist/tools/index.js +52 -0
  207. package/dist/tools/mcpTool.d.ts +28 -0
  208. package/dist/tools/mcpTool.js +218 -0
  209. package/dist/tools/types.d.ts +8 -0
  210. package/dist/tools/types.js +2 -0
  211. package/dist/tools/zodSchema.d.ts +9 -0
  212. package/dist/tools/zodSchema.js +50 -0
  213. package/dist/volume/index.d.ts +23 -0
  214. package/dist/volume/index.js +113 -0
  215. package/package.json +2 -2
  216. package/dist/cjs/sandbox/codegen/codegen-ws.js +0 -30
  217. package/dist/cjs/sandbox/filesystem/filesystem-ws.js +0 -106
  218. package/dist/cjs/sandbox/network/network-ws.js +0 -12
  219. package/dist/cjs/sandbox/process/process-ws.js +0 -139
  220. package/dist/cjs/sandbox/websocket/client.js +0 -269
  221. package/dist/cjs/types/sandbox/codegen/codegen-ws.d.ts +0 -10
  222. package/dist/cjs/types/sandbox/network/network-ws.d.ts +0 -7
  223. package/dist/cjs/types/sandbox/process/process-ws.d.ts +0 -27
  224. package/dist/cjs/types/sandbox/websocket/client.d.ts +0 -48
  225. package/dist/cjs/types/sandbox/websocket/index.d.ts +0 -1
  226. package/dist/cjs-browser/sandbox/codegen/codegen-ws.js +0 -30
  227. package/dist/cjs-browser/sandbox/filesystem/filesystem-ws.js +0 -106
  228. package/dist/cjs-browser/sandbox/network/network-ws.js +0 -12
  229. package/dist/cjs-browser/sandbox/process/process-ws.js +0 -139
  230. package/dist/cjs-browser/sandbox/websocket/client.js +0 -269
  231. package/dist/cjs-browser/types/sandbox/codegen/codegen-ws.d.ts +0 -10
  232. package/dist/cjs-browser/types/sandbox/filesystem/filesystem-ws.d.ts +0 -35
  233. package/dist/cjs-browser/types/sandbox/network/network-ws.d.ts +0 -7
  234. package/dist/cjs-browser/types/sandbox/process/process-ws.d.ts +0 -27
  235. package/dist/cjs-browser/types/sandbox/websocket/client.d.ts +0 -48
  236. package/dist/esm/sandbox/codegen/codegen-ws.js +0 -26
  237. package/dist/esm/sandbox/filesystem/filesystem-ws.js +0 -102
  238. package/dist/esm/sandbox/network/network-ws.js +0 -8
  239. package/dist/esm/sandbox/process/process-ws.js +0 -135
  240. package/dist/esm/sandbox/websocket/client.js +0 -265
  241. package/dist/esm/sandbox/websocket/index.js +0 -1
  242. package/dist/esm-browser/sandbox/codegen/codegen-ws.js +0 -26
  243. package/dist/esm-browser/sandbox/filesystem/filesystem-ws.js +0 -102
  244. package/dist/esm-browser/sandbox/network/network-ws.js +0 -8
  245. package/dist/esm-browser/sandbox/process/process-ws.js +0 -135
  246. package/dist/esm-browser/sandbox/websocket/client.js +0 -265
  247. package/dist/esm-browser/sandbox/websocket/index.js +0 -1
@@ -0,0 +1,15 @@
1
+ import { Agent } from "../client/index.js";
2
+ declare class BlAgent {
3
+ agentName: string;
4
+ constructor(agentName: string);
5
+ get fallbackUrl(): import("url").URL | null;
6
+ get externalUrl(): import("url").URL;
7
+ get internalUrl(): import("url").URL;
8
+ get forcedUrl(): import("url").URL | null;
9
+ get url(): import("url").URL;
10
+ call(url: URL, input: Record<string, unknown> | string | undefined): Promise<Response>;
11
+ run(input: Record<string, unknown> | string | undefined): Promise<string>;
12
+ }
13
+ export declare const blAgent: (agentName: string) => BlAgent;
14
+ export declare const getAgentMetadata: (agent: string) => Promise<Agent | null>;
15
+ export {};
@@ -0,0 +1,109 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.getAgentMetadata = exports.blAgent = void 0;
4
+ const index_js_1 = require("../cache/index.js");
5
+ const index_js_2 = require("../client/index.js");
6
+ const internal_js_1 = require("../common/internal.js");
7
+ const logger_js_1 = require("../common/logger.js");
8
+ const settings_js_1 = require("../common/settings.js");
9
+ const telemetry_js_1 = require("../telemetry/telemetry.js");
10
+ class BlAgent {
11
+ agentName;
12
+ constructor(agentName) {
13
+ this.agentName = agentName;
14
+ }
15
+ get fallbackUrl() {
16
+ if (this.externalUrl != this.url) {
17
+ return this.externalUrl;
18
+ }
19
+ return null;
20
+ }
21
+ get externalUrl() {
22
+ return new URL(`${settings_js_1.settings.runUrl}/${settings_js_1.settings.workspace}/agents/${this.agentName}`);
23
+ }
24
+ get internalUrl() {
25
+ const hash = (0, internal_js_1.getGlobalUniqueHash)(settings_js_1.settings.workspace, "agent", this.agentName);
26
+ return new URL(`${settings_js_1.settings.runInternalProtocol}://bl-${settings_js_1.settings.env}-${hash}.${settings_js_1.settings.runInternalHostname}`);
27
+ }
28
+ get forcedUrl() {
29
+ return (0, internal_js_1.getForcedUrl)('function', this.agentName);
30
+ }
31
+ get url() {
32
+ if (this.forcedUrl)
33
+ return this.forcedUrl;
34
+ if (settings_js_1.settings.runInternalHostname)
35
+ return this.internalUrl;
36
+ return this.externalUrl;
37
+ }
38
+ async call(url, input) {
39
+ let body = input;
40
+ if (typeof body != "string") {
41
+ body = JSON.stringify(body);
42
+ }
43
+ const response = await fetch(url, {
44
+ method: "POST",
45
+ headers: {
46
+ ...settings_js_1.settings.headers,
47
+ "Content-Type": "application/json",
48
+ },
49
+ body,
50
+ });
51
+ return response;
52
+ }
53
+ async run(input) {
54
+ logger_js_1.logger.debug(`Agent Calling: ${this.agentName}`);
55
+ const span = (0, telemetry_js_1.startSpan)(this.agentName, {
56
+ attributes: {
57
+ "agent.name": this.agentName,
58
+ "agent.args": JSON.stringify(input),
59
+ "span.type": "agent.run",
60
+ },
61
+ isRoot: false,
62
+ });
63
+ try {
64
+ const response = await this.call(this.url, input);
65
+ span.setAttribute("agent.run.result", await response.text());
66
+ return await response.text();
67
+ }
68
+ catch (err) {
69
+ if (err instanceof Error) {
70
+ if (!this.fallbackUrl) {
71
+ span.setAttribute("agent.run.error", err.stack);
72
+ throw err;
73
+ }
74
+ try {
75
+ const response = await this.call(this.fallbackUrl, input);
76
+ span.setAttribute("agent.run.result", await response.text());
77
+ return await response.text();
78
+ }
79
+ catch (err) {
80
+ if (err instanceof Error) {
81
+ span.setAttribute("agent.run.error", err.stack);
82
+ }
83
+ throw err;
84
+ }
85
+ }
86
+ throw err;
87
+ }
88
+ finally {
89
+ span.end();
90
+ }
91
+ }
92
+ }
93
+ const blAgent = (agentName) => {
94
+ return new BlAgent(agentName);
95
+ };
96
+ exports.blAgent = blAgent;
97
+ const getAgentMetadata = async (agent) => {
98
+ const cacheData = await (0, index_js_1.findFromCache)("Agent", agent);
99
+ if (cacheData) {
100
+ return cacheData;
101
+ }
102
+ const { data } = await (0, index_js_2.getAgent)({
103
+ path: {
104
+ agentName: agent,
105
+ },
106
+ });
107
+ return data || null;
108
+ };
109
+ exports.getAgentMetadata = getAgentMetadata;
@@ -0,0 +1,10 @@
1
+ import { Credentials } from "./credentials.js";
2
+ import { CredentialsType } from "./types.js";
3
+ export declare class ApiKey extends Credentials {
4
+ private apiKey;
5
+ private credentials;
6
+ constructor(credentials: CredentialsType);
7
+ get workspace(): string;
8
+ get authorization(): string;
9
+ get token(): string;
10
+ }
@@ -0,0 +1,24 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.ApiKey = void 0;
4
+ const env_js_1 = require("../common/env.js");
5
+ const credentials_js_1 = require("./credentials.js");
6
+ class ApiKey extends credentials_js_1.Credentials {
7
+ apiKey;
8
+ credentials;
9
+ constructor(credentials) {
10
+ super();
11
+ this.apiKey = credentials.apiKey || "";
12
+ this.credentials = credentials;
13
+ }
14
+ get workspace() {
15
+ return this.credentials.workspace || env_js_1.env.BL_WORKSPACE || "";
16
+ }
17
+ get authorization() {
18
+ return `Bearer ${this.apiKey}`;
19
+ }
20
+ get token() {
21
+ return this.apiKey;
22
+ }
23
+ }
24
+ exports.ApiKey = ApiKey;
@@ -0,0 +1,17 @@
1
+ import { Credentials } from "./credentials.js";
2
+ import { CredentialsType } from "./types.js";
3
+ export declare class ClientCredentials extends Credentials {
4
+ private clientCredentials;
5
+ private accessToken;
6
+ private credentials;
7
+ private currentPromise;
8
+ constructor(credentials: CredentialsType);
9
+ get workspace(): string;
10
+ needRefresh(): boolean;
11
+ authenticate(): Promise<void>;
12
+ private sleep;
13
+ processWithRetry(retry?: number): Promise<void>;
14
+ process(): Promise<void>;
15
+ get authorization(): string;
16
+ get token(): string;
17
+ }
@@ -0,0 +1,85 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.ClientCredentials = void 0;
4
+ const jwt_decode_1 = require("jwt-decode");
5
+ const authentication_js_1 = require("../client/authentication.js");
6
+ const env_js_1 = require("../common/env.js");
7
+ const credentials_js_1 = require("./credentials.js");
8
+ class ClientCredentials extends credentials_js_1.Credentials {
9
+ clientCredentials;
10
+ accessToken;
11
+ credentials;
12
+ currentPromise;
13
+ constructor(credentials) {
14
+ super();
15
+ this.clientCredentials = credentials.clientCredentials || "";
16
+ this.credentials = credentials;
17
+ this.accessToken = "";
18
+ this.currentPromise = null;
19
+ }
20
+ get workspace() {
21
+ return this.credentials.workspace || env_js_1.env.BL_WORKSPACE || "";
22
+ }
23
+ needRefresh() {
24
+ if (this.currentPromise)
25
+ return false;
26
+ if (this.accessToken) {
27
+ const decoded = (0, jwt_decode_1.jwtDecode)(this.accessToken);
28
+ const { exp, iat } = decoded;
29
+ if (!exp || !iat)
30
+ return true;
31
+ const expDate = new Date(exp * 1000);
32
+ const iatDate = new Date(iat * 1000);
33
+ const nowDate = new Date();
34
+ const diff = expDate.getTime() - nowDate.getTime();
35
+ const iatDiff = expDate.getTime() - iatDate.getTime();
36
+ const ratio = diff / iatDiff;
37
+ return ratio < 0.5;
38
+ }
39
+ return true;
40
+ }
41
+ async authenticate() {
42
+ if (!this.needRefresh()) {
43
+ return this.currentPromise || Promise.resolve();
44
+ }
45
+ this.currentPromise = this.processWithRetry();
46
+ return this.currentPromise;
47
+ }
48
+ sleep(ms) {
49
+ return new Promise((resolve) => setTimeout(resolve, ms));
50
+ }
51
+ async processWithRetry(retry = 3) {
52
+ try {
53
+ return await this.process();
54
+ }
55
+ catch (error) {
56
+ if (retry > 0) {
57
+ await this.sleep(1000);
58
+ return this.processWithRetry(retry - 1);
59
+ }
60
+ throw error;
61
+ }
62
+ }
63
+ async process() {
64
+ const response = await (0, authentication_js_1.oauthToken)({
65
+ headers: {
66
+ Authorization: `Basic ${this.clientCredentials}`,
67
+ },
68
+ body: {
69
+ grant_type: "client_credentials",
70
+ },
71
+ });
72
+ if (response.error) {
73
+ throw new Error(response.error.error);
74
+ }
75
+ this.accessToken = response.data?.access_token || "";
76
+ this.currentPromise = null;
77
+ }
78
+ get authorization() {
79
+ return `Bearer ${this.accessToken}`;
80
+ }
81
+ get token() {
82
+ return this.accessToken;
83
+ }
84
+ }
85
+ exports.ClientCredentials = ClientCredentials;
@@ -0,0 +1,6 @@
1
+ export declare class Credentials {
2
+ authenticate(): Promise<void>;
3
+ get workspace(): string;
4
+ get authorization(): string;
5
+ get token(): string;
6
+ }
@@ -0,0 +1,17 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.Credentials = void 0;
4
+ const env_js_1 = require("../common/env.js");
5
+ class Credentials {
6
+ async authenticate() { }
7
+ get workspace() {
8
+ return env_js_1.env.BL_WORKSPACE || "";
9
+ }
10
+ get authorization() {
11
+ return "";
12
+ }
13
+ get token() {
14
+ return "";
15
+ }
16
+ }
17
+ exports.Credentials = Credentials;
@@ -0,0 +1,15 @@
1
+ import { CredentialsType } from "./types.js";
2
+ export declare class DeviceMode {
3
+ private refreshToken;
4
+ private deviceCode;
5
+ private accessToken;
6
+ private credentials;
7
+ private currentPromise;
8
+ constructor(credentials: CredentialsType);
9
+ get workspace(): string;
10
+ needRefresh(): boolean;
11
+ authenticate(): Promise<void>;
12
+ process(): Promise<void>;
13
+ get authorization(): string;
14
+ get token(): string;
15
+ }
@@ -0,0 +1,70 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.DeviceMode = void 0;
4
+ const jwt_decode_1 = require("jwt-decode");
5
+ const authentication_js_1 = require("../client/authentication.js");
6
+ const env_js_1 = require("../common/env.js");
7
+ class DeviceMode {
8
+ refreshToken;
9
+ deviceCode;
10
+ accessToken;
11
+ credentials;
12
+ currentPromise;
13
+ // private expireIn: number;
14
+ constructor(credentials) {
15
+ this.refreshToken = credentials.refresh_token || "";
16
+ this.deviceCode = credentials.device_code || "";
17
+ this.accessToken = credentials.access_token || "";
18
+ this.credentials = credentials;
19
+ this.currentPromise = null;
20
+ }
21
+ get workspace() {
22
+ return this.credentials.workspace || env_js_1.env.BL_WORKSPACE || "";
23
+ }
24
+ needRefresh() {
25
+ if (this.currentPromise)
26
+ return false;
27
+ if (this.accessToken) {
28
+ const decoded = (0, jwt_decode_1.jwtDecode)(this.accessToken);
29
+ const { exp, iat } = decoded;
30
+ if (!exp || !iat)
31
+ return true;
32
+ const expDate = new Date(exp * 1000);
33
+ const iatDate = new Date(iat * 1000);
34
+ const nowDate = new Date();
35
+ const diff = expDate.getTime() - nowDate.getTime();
36
+ const iatDiff = expDate.getTime() - iatDate.getTime();
37
+ const ratio = diff / iatDiff;
38
+ return ratio < 0.5;
39
+ }
40
+ return true;
41
+ }
42
+ async authenticate() {
43
+ if (!this.needRefresh()) {
44
+ return this.currentPromise || Promise.resolve();
45
+ }
46
+ this.currentPromise = this.process();
47
+ return this.currentPromise;
48
+ }
49
+ async process() {
50
+ const response = await (0, authentication_js_1.oauthToken)({
51
+ body: {
52
+ grant_type: "refresh_token",
53
+ device_code: this.deviceCode,
54
+ refresh_token: this.refreshToken,
55
+ },
56
+ });
57
+ if (response.error) {
58
+ throw new Error(response.error.error);
59
+ }
60
+ this.accessToken = response.data?.access_token || "";
61
+ this.currentPromise = null;
62
+ }
63
+ get authorization() {
64
+ return `Bearer ${this.accessToken}`;
65
+ }
66
+ get token() {
67
+ return this.accessToken;
68
+ }
69
+ }
70
+ exports.DeviceMode = DeviceMode;
@@ -0,0 +1,3 @@
1
+ import { Credentials } from "./credentials.js";
2
+ import { DeviceMode } from "./deviceMode.js";
3
+ export declare function authentication(): Credentials | DeviceMode;
@@ -0,0 +1,62 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.authentication = authentication;
7
+ const yaml_1 = __importDefault(require("yaml"));
8
+ const env_js_1 = require("../common/env.js");
9
+ const node_js_1 = require("../common/node.js");
10
+ const apikey_js_1 = require("./apikey.js");
11
+ const clientcredentials_js_1 = require("./clientcredentials.js");
12
+ const credentials_js_1 = require("./credentials.js");
13
+ const deviceMode_js_1 = require("./deviceMode.js");
14
+ function getCredentials() {
15
+ if (env_js_1.env.BL_API_KEY) {
16
+ return {
17
+ apiKey: env_js_1.env.BL_API_KEY,
18
+ workspace: env_js_1.env.BL_WORKSPACE,
19
+ };
20
+ }
21
+ if (env_js_1.env.BL_CLIENT_CREDENTIALS) {
22
+ return {
23
+ clientCredentials: env_js_1.env.BL_CLIENT_CREDENTIALS,
24
+ workspace: env_js_1.env.BL_WORKSPACE,
25
+ };
26
+ }
27
+ if (node_js_1.os === null || node_js_1.fs === null || node_js_1.path === null) {
28
+ return null;
29
+ }
30
+ try {
31
+ const homeDir = node_js_1.os.homedir();
32
+ const config = node_js_1.fs.readFileSync(node_js_1.path.join(homeDir, ".blaxel/config.yaml"), "utf8");
33
+ const configJson = yaml_1.default.parse(config);
34
+ const workspaceName = env_js_1.env.BL_WORKSPACE || configJson.context.workspace;
35
+ const credentials = configJson.workspaces.find((wk) => wk.name === workspaceName)?.credentials;
36
+ if (!credentials) {
37
+ return null;
38
+ }
39
+ credentials.workspace = workspaceName;
40
+ return credentials;
41
+ }
42
+ catch {
43
+ // If any error (e.g., running in browser), just return null
44
+ return null;
45
+ }
46
+ }
47
+ function authentication() {
48
+ const credentials = getCredentials();
49
+ if (!credentials) {
50
+ return new credentials_js_1.Credentials();
51
+ }
52
+ if (credentials.apiKey) {
53
+ return new apikey_js_1.ApiKey(credentials);
54
+ }
55
+ if (credentials.clientCredentials) {
56
+ return new clientcredentials_js_1.ClientCredentials(credentials);
57
+ }
58
+ if (credentials.device_code) {
59
+ return new deviceMode_js_1.DeviceMode(credentials);
60
+ }
61
+ return new credentials_js_1.Credentials();
62
+ }
@@ -0,0 +1,9 @@
1
+ export type CredentialsType = {
2
+ apiKey?: string;
3
+ clientCredentials?: string;
4
+ refresh_token?: string;
5
+ access_token?: string;
6
+ device_code?: string;
7
+ expires_in?: number;
8
+ workspace?: string;
9
+ };
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -0,0 +1 @@
1
+ export declare function findFromCache(resource: string, name: string): Promise<unknown | null>;
@@ -0,0 +1,26 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.findFromCache = findFromCache;
7
+ const yaml_1 = __importDefault(require("yaml"));
8
+ const node_js_1 = require("../common/node.js");
9
+ const cache = new Map();
10
+ try {
11
+ if (node_js_1.fs) {
12
+ const cacheString = node_js_1.fs.readFileSync(".cache.yaml", "utf8");
13
+ const cacheData = yaml_1.default.parseAllDocuments(cacheString);
14
+ for (const doc of cacheData) {
15
+ const jsonDoc = doc.toJSON();
16
+ const cacheKey = `${jsonDoc.kind}/${jsonDoc.metadata.name}`;
17
+ cache.set(cacheKey, jsonDoc);
18
+ }
19
+ }
20
+ /* eslint-disable */
21
+ }
22
+ catch (error) { }
23
+ async function findFromCache(resource, name) {
24
+ const cacheKey = `${resource}/${name}`;
25
+ return cache.get(cacheKey);
26
+ }