@blaxel/core 0.2.36-dev.185 → 0.2.36-dev.190
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/cjs/.tsbuildinfo +1 -0
- package/dist/{common → cjs/common}/settings.js +2 -2
- package/dist/{tools → cjs/types/tools}/index.d.ts +1 -0
- package/dist/esm/.tsbuildinfo +1 -0
- package/dist/esm/agents/index.js +104 -0
- package/dist/esm/authentication/apikey.js +20 -0
- package/dist/esm/authentication/clientcredentials.js +81 -0
- package/dist/esm/authentication/credentials.js +13 -0
- package/dist/esm/authentication/deviceMode.js +66 -0
- package/dist/esm/authentication/index.js +56 -0
- package/dist/esm/authentication/types.js +1 -0
- package/dist/esm/cache/index.js +20 -0
- package/dist/esm/client/authentication.js +11 -0
- package/dist/esm/client/client.gen.js +5 -0
- package/dist/esm/client/client.js +1 -0
- package/dist/esm/client/index.js +3 -0
- package/dist/esm/client/interceptors.js +14 -0
- package/dist/esm/client/sdk.gen.js +1649 -0
- package/dist/esm/client/types.gen.js +3 -0
- package/dist/esm/common/autoload.js +23 -0
- package/dist/esm/common/env.js +36 -0
- package/dist/esm/common/errors.js +14 -0
- package/dist/esm/common/internal.js +182 -0
- package/dist/esm/common/internal.test.js +37 -0
- package/dist/esm/common/logger.js +65 -0
- package/dist/esm/common/node.js +20 -0
- package/dist/esm/common/settings.js +167 -0
- package/dist/esm/index.browser.test.js +10 -0
- package/dist/esm/index.js +17 -0
- package/dist/esm/jobs/index.js +3 -0
- package/dist/esm/jobs/jobs.js +86 -0
- package/dist/esm/jobs/start.js +62 -0
- package/dist/esm/jobs/types.js +1 -0
- package/dist/esm/mcp/client.js +243 -0
- package/dist/esm/mcp/index.js +2 -0
- package/dist/esm/mcp/server.js +176 -0
- package/dist/esm/models/index.js +25 -0
- package/dist/esm/package.json +1 -0
- package/dist/esm/sandbox/action.js +79 -0
- package/dist/esm/sandbox/client/client.gen.js +3 -0
- package/dist/esm/sandbox/client/index.js +3 -0
- package/dist/esm/sandbox/client/sdk.gen.js +270 -0
- package/dist/esm/sandbox/client/types.gen.js +2 -0
- package/dist/esm/sandbox/filesystem/filesystem.js +272 -0
- package/dist/esm/sandbox/filesystem/index.js +2 -0
- package/dist/esm/sandbox/filesystem/types.js +1 -0
- package/dist/esm/sandbox/index.js +7 -0
- package/dist/esm/sandbox/network/index.js +1 -0
- package/dist/esm/sandbox/network/network.js +6 -0
- package/dist/esm/sandbox/preview.js +141 -0
- package/dist/esm/sandbox/process/index.js +1 -0
- package/dist/esm/sandbox/process/process.js +185 -0
- package/dist/esm/sandbox/sandbox.js +174 -0
- package/dist/esm/sandbox/session.js +119 -0
- package/dist/esm/sandbox/types.js +76 -0
- package/dist/esm/telemetry/telemetry.js +74 -0
- package/dist/esm/tools/index.js +44 -0
- package/dist/esm/tools/mcpTool.js +213 -0
- package/dist/esm/tools/types.js +1 -0
- package/dist/esm/tools/zodSchema.js +43 -0
- package/dist/esm/volume/index.js +109 -0
- package/package.json +17 -30
- /package/dist/{agents → cjs/agents}/index.js +0 -0
- /package/dist/{authentication → cjs/authentication}/apikey.js +0 -0
- /package/dist/{authentication → cjs/authentication}/clientcredentials.js +0 -0
- /package/dist/{authentication → cjs/authentication}/credentials.js +0 -0
- /package/dist/{authentication → cjs/authentication}/deviceMode.js +0 -0
- /package/dist/{authentication → cjs/authentication}/index.js +0 -0
- /package/dist/{authentication → cjs/authentication}/types.js +0 -0
- /package/dist/{cache → cjs/cache}/index.js +0 -0
- /package/dist/{client → cjs/client}/authentication.js +0 -0
- /package/dist/{client → cjs/client}/client.gen.js +0 -0
- /package/dist/{client → cjs/client}/client.js +0 -0
- /package/dist/{client → cjs/client}/index.js +0 -0
- /package/dist/{client → cjs/client}/interceptors.js +0 -0
- /package/dist/{client → cjs/client}/sdk.gen.js +0 -0
- /package/dist/{client → cjs/client}/types.gen.js +0 -0
- /package/dist/{common → cjs/common}/autoload.js +0 -0
- /package/dist/{common → cjs/common}/env.js +0 -0
- /package/dist/{common → cjs/common}/errors.js +0 -0
- /package/dist/{common → cjs/common}/internal.js +0 -0
- /package/dist/{common → cjs/common}/internal.test.js +0 -0
- /package/dist/{common → cjs/common}/logger.js +0 -0
- /package/dist/{common → cjs/common}/node.js +0 -0
- /package/dist/{index.browser.test.js → cjs/index.browser.test.js} +0 -0
- /package/dist/{index.js → cjs/index.js} +0 -0
- /package/dist/{jobs → cjs/jobs}/index.js +0 -0
- /package/dist/{jobs → cjs/jobs}/jobs.js +0 -0
- /package/dist/{jobs → cjs/jobs}/start.js +0 -0
- /package/dist/{jobs → cjs/jobs}/types.js +0 -0
- /package/dist/{mcp → cjs/mcp}/client.js +0 -0
- /package/dist/{mcp → cjs/mcp}/index.js +0 -0
- /package/dist/{mcp → cjs/mcp}/server.js +0 -0
- /package/dist/{models → cjs/models}/index.js +0 -0
- /package/dist/{sandbox → cjs/sandbox}/action.js +0 -0
- /package/dist/{sandbox → cjs/sandbox}/client/client.gen.js +0 -0
- /package/dist/{sandbox → cjs/sandbox}/client/index.js +0 -0
- /package/dist/{sandbox → cjs/sandbox}/client/sdk.gen.js +0 -0
- /package/dist/{sandbox → cjs/sandbox}/client/types.gen.js +0 -0
- /package/dist/{sandbox → cjs/sandbox}/filesystem/filesystem.js +0 -0
- /package/dist/{sandbox → cjs/sandbox}/filesystem/index.js +0 -0
- /package/dist/{sandbox → cjs/sandbox}/filesystem/types.js +0 -0
- /package/dist/{sandbox → cjs/sandbox}/index.js +0 -0
- /package/dist/{sandbox → cjs/sandbox}/network/index.js +0 -0
- /package/dist/{sandbox → cjs/sandbox}/network/network.js +0 -0
- /package/dist/{sandbox → cjs/sandbox}/preview.js +0 -0
- /package/dist/{sandbox → cjs/sandbox}/process/index.js +0 -0
- /package/dist/{sandbox → cjs/sandbox}/process/process.js +0 -0
- /package/dist/{sandbox → cjs/sandbox}/sandbox.js +0 -0
- /package/dist/{sandbox → cjs/sandbox}/session.js +0 -0
- /package/dist/{sandbox → cjs/sandbox}/types.js +0 -0
- /package/dist/{telemetry → cjs/telemetry}/telemetry.js +0 -0
- /package/dist/{tools → cjs/tools}/index.js +0 -0
- /package/dist/{tools → cjs/tools}/mcpTool.js +0 -0
- /package/dist/{tools → cjs/tools}/types.js +0 -0
- /package/dist/{tools → cjs/tools}/zodSchema.js +0 -0
- /package/dist/{agents → cjs/types/agents}/index.d.ts +0 -0
- /package/dist/{authentication → cjs/types/authentication}/apikey.d.ts +0 -0
- /package/dist/{authentication → cjs/types/authentication}/clientcredentials.d.ts +0 -0
- /package/dist/{authentication → cjs/types/authentication}/credentials.d.ts +0 -0
- /package/dist/{authentication → cjs/types/authentication}/deviceMode.d.ts +0 -0
- /package/dist/{authentication → cjs/types/authentication}/index.d.ts +0 -0
- /package/dist/{authentication → cjs/types/authentication}/types.d.ts +0 -0
- /package/dist/{cache → cjs/types/cache}/index.d.ts +0 -0
- /package/dist/{client → cjs/types/client}/authentication.d.ts +0 -0
- /package/dist/{client → cjs/types/client}/client.d.ts +0 -0
- /package/dist/{client → cjs/types/client}/client.gen.d.ts +0 -0
- /package/dist/{client → cjs/types/client}/index.d.ts +0 -0
- /package/dist/{client → cjs/types/client}/interceptors.d.ts +0 -0
- /package/dist/{client → cjs/types/client}/sdk.gen.d.ts +0 -0
- /package/dist/{client → cjs/types/client}/types.gen.d.ts +0 -0
- /package/dist/{common → cjs/types/common}/autoload.d.ts +0 -0
- /package/dist/{common → cjs/types/common}/env.d.ts +0 -0
- /package/dist/{common → cjs/types/common}/errors.d.ts +0 -0
- /package/dist/{common → cjs/types/common}/internal.d.ts +0 -0
- /package/dist/{common → cjs/types/common}/internal.test.d.ts +0 -0
- /package/dist/{common → cjs/types/common}/logger.d.ts +0 -0
- /package/dist/{common → cjs/types/common}/node.d.ts +0 -0
- /package/dist/{common → cjs/types/common}/settings.d.ts +0 -0
- /package/dist/{index.browser.test.d.ts → cjs/types/index.browser.test.d.ts} +0 -0
- /package/dist/{index.d.ts → cjs/types/index.d.ts} +0 -0
- /package/dist/{jobs → cjs/types/jobs}/index.d.ts +0 -0
- /package/dist/{jobs → cjs/types/jobs}/jobs.d.ts +0 -0
- /package/dist/{jobs → cjs/types/jobs}/start.d.ts +0 -0
- /package/dist/{jobs → cjs/types/jobs}/types.d.ts +0 -0
- /package/dist/{mcp → cjs/types/mcp}/client.d.ts +0 -0
- /package/dist/{mcp → cjs/types/mcp}/index.d.ts +0 -0
- /package/dist/{mcp → cjs/types/mcp}/server.d.ts +0 -0
- /package/dist/{models → cjs/types/models}/index.d.ts +0 -0
- /package/dist/{sandbox → cjs/types/sandbox}/action.d.ts +0 -0
- /package/dist/{sandbox → cjs/types/sandbox}/client/client.gen.d.ts +0 -0
- /package/dist/{sandbox → cjs/types/sandbox}/client/index.d.ts +0 -0
- /package/dist/{sandbox → cjs/types/sandbox}/client/sdk.gen.d.ts +0 -0
- /package/dist/{sandbox → cjs/types/sandbox}/client/types.gen.d.ts +0 -0
- /package/dist/{sandbox → cjs/types/sandbox}/filesystem/filesystem.d.ts +0 -0
- /package/dist/{sandbox → cjs/types/sandbox}/filesystem/index.d.ts +0 -0
- /package/dist/{sandbox → cjs/types/sandbox}/filesystem/types.d.ts +0 -0
- /package/dist/{sandbox → cjs/types/sandbox}/index.d.ts +0 -0
- /package/dist/{sandbox → cjs/types/sandbox}/network/index.d.ts +0 -0
- /package/dist/{sandbox → cjs/types/sandbox}/network/network.d.ts +0 -0
- /package/dist/{sandbox → cjs/types/sandbox}/preview.d.ts +0 -0
- /package/dist/{sandbox → cjs/types/sandbox}/process/index.d.ts +0 -0
- /package/dist/{sandbox → cjs/types/sandbox}/process/process.d.ts +0 -0
- /package/dist/{sandbox → cjs/types/sandbox}/sandbox.d.ts +0 -0
- /package/dist/{sandbox → cjs/types/sandbox}/session.d.ts +0 -0
- /package/dist/{sandbox → cjs/types/sandbox}/types.d.ts +0 -0
- /package/dist/{telemetry → cjs/types/telemetry}/telemetry.d.ts +0 -0
- /package/dist/{tools → cjs/types/tools}/mcpTool.d.ts +0 -0
- /package/dist/{tools → cjs/types/tools}/types.d.ts +0 -0
- /package/dist/{tools → cjs/types/tools}/zodSchema.d.ts +0 -0
- /package/dist/{volume → cjs/types/volume}/index.d.ts +0 -0
- /package/dist/{volume → cjs/volume}/index.js +0 -0
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
import { findFromCache } from "../cache/index.js";
|
|
2
|
+
import { getAgent } from "../client/index.js";
|
|
3
|
+
import { getForcedUrl, getGlobalUniqueHash } from "../common/internal.js";
|
|
4
|
+
import { logger } from "../common/logger.js";
|
|
5
|
+
import { settings } from "../common/settings.js";
|
|
6
|
+
import { startSpan } from '../telemetry/telemetry.js';
|
|
7
|
+
class BlAgent {
|
|
8
|
+
agentName;
|
|
9
|
+
constructor(agentName) {
|
|
10
|
+
this.agentName = agentName;
|
|
11
|
+
}
|
|
12
|
+
get fallbackUrl() {
|
|
13
|
+
if (this.externalUrl != this.url) {
|
|
14
|
+
return this.externalUrl;
|
|
15
|
+
}
|
|
16
|
+
return null;
|
|
17
|
+
}
|
|
18
|
+
get externalUrl() {
|
|
19
|
+
return new URL(`${settings.runUrl}/${settings.workspace}/agents/${this.agentName}`);
|
|
20
|
+
}
|
|
21
|
+
get internalUrl() {
|
|
22
|
+
const hash = getGlobalUniqueHash(settings.workspace, "agent", this.agentName);
|
|
23
|
+
return new URL(`${settings.runInternalProtocol}://bl-${settings.env}-${hash}.${settings.runInternalHostname}`);
|
|
24
|
+
}
|
|
25
|
+
get forcedUrl() {
|
|
26
|
+
return getForcedUrl('function', this.agentName);
|
|
27
|
+
}
|
|
28
|
+
get url() {
|
|
29
|
+
if (this.forcedUrl)
|
|
30
|
+
return this.forcedUrl;
|
|
31
|
+
if (settings.runInternalHostname)
|
|
32
|
+
return this.internalUrl;
|
|
33
|
+
return this.externalUrl;
|
|
34
|
+
}
|
|
35
|
+
async call(url, input) {
|
|
36
|
+
let body = input;
|
|
37
|
+
if (typeof body != "string") {
|
|
38
|
+
body = JSON.stringify(body);
|
|
39
|
+
}
|
|
40
|
+
const response = await fetch(url, {
|
|
41
|
+
method: "POST",
|
|
42
|
+
headers: {
|
|
43
|
+
...settings.headers,
|
|
44
|
+
"Content-Type": "application/json",
|
|
45
|
+
},
|
|
46
|
+
body,
|
|
47
|
+
});
|
|
48
|
+
return response;
|
|
49
|
+
}
|
|
50
|
+
async run(input) {
|
|
51
|
+
logger.debug(`Agent Calling: ${this.agentName}`);
|
|
52
|
+
const span = startSpan(this.agentName, {
|
|
53
|
+
attributes: {
|
|
54
|
+
"agent.name": this.agentName,
|
|
55
|
+
"agent.args": JSON.stringify(input),
|
|
56
|
+
"span.type": "agent.run",
|
|
57
|
+
},
|
|
58
|
+
isRoot: false,
|
|
59
|
+
});
|
|
60
|
+
try {
|
|
61
|
+
const response = await this.call(this.url, input);
|
|
62
|
+
span.setAttribute("agent.run.result", await response.text());
|
|
63
|
+
return await response.text();
|
|
64
|
+
}
|
|
65
|
+
catch (err) {
|
|
66
|
+
if (err instanceof Error) {
|
|
67
|
+
if (!this.fallbackUrl) {
|
|
68
|
+
span.setAttribute("agent.run.error", err.stack);
|
|
69
|
+
throw err;
|
|
70
|
+
}
|
|
71
|
+
try {
|
|
72
|
+
const response = await this.call(this.fallbackUrl, input);
|
|
73
|
+
span.setAttribute("agent.run.result", await response.text());
|
|
74
|
+
return await response.text();
|
|
75
|
+
}
|
|
76
|
+
catch (err) {
|
|
77
|
+
if (err instanceof Error) {
|
|
78
|
+
span.setAttribute("agent.run.error", err.stack);
|
|
79
|
+
}
|
|
80
|
+
throw err;
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
throw err;
|
|
84
|
+
}
|
|
85
|
+
finally {
|
|
86
|
+
span.end();
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
export const blAgent = (agentName) => {
|
|
91
|
+
return new BlAgent(agentName);
|
|
92
|
+
};
|
|
93
|
+
export const getAgentMetadata = async (agent) => {
|
|
94
|
+
const cacheData = await findFromCache("Agent", agent);
|
|
95
|
+
if (cacheData) {
|
|
96
|
+
return cacheData;
|
|
97
|
+
}
|
|
98
|
+
const { data } = await getAgent({
|
|
99
|
+
path: {
|
|
100
|
+
agentName: agent,
|
|
101
|
+
},
|
|
102
|
+
});
|
|
103
|
+
return data || null;
|
|
104
|
+
};
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { env } from "../common/env.js";
|
|
2
|
+
import { Credentials } from "./credentials.js";
|
|
3
|
+
export class ApiKey extends Credentials {
|
|
4
|
+
apiKey;
|
|
5
|
+
credentials;
|
|
6
|
+
constructor(credentials) {
|
|
7
|
+
super();
|
|
8
|
+
this.apiKey = credentials.apiKey || "";
|
|
9
|
+
this.credentials = credentials;
|
|
10
|
+
}
|
|
11
|
+
get workspace() {
|
|
12
|
+
return this.credentials.workspace || env.BL_WORKSPACE || "";
|
|
13
|
+
}
|
|
14
|
+
get authorization() {
|
|
15
|
+
return `Bearer ${this.apiKey}`;
|
|
16
|
+
}
|
|
17
|
+
get token() {
|
|
18
|
+
return this.apiKey;
|
|
19
|
+
}
|
|
20
|
+
}
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
import { jwtDecode } from "jwt-decode";
|
|
2
|
+
import { oauthToken } from "../client/authentication.js";
|
|
3
|
+
import { env } from "../common/env.js";
|
|
4
|
+
import { Credentials } from "./credentials.js";
|
|
5
|
+
export class ClientCredentials extends Credentials {
|
|
6
|
+
clientCredentials;
|
|
7
|
+
accessToken;
|
|
8
|
+
credentials;
|
|
9
|
+
currentPromise;
|
|
10
|
+
constructor(credentials) {
|
|
11
|
+
super();
|
|
12
|
+
this.clientCredentials = credentials.clientCredentials || "";
|
|
13
|
+
this.credentials = credentials;
|
|
14
|
+
this.accessToken = "";
|
|
15
|
+
this.currentPromise = null;
|
|
16
|
+
}
|
|
17
|
+
get workspace() {
|
|
18
|
+
return this.credentials.workspace || env.BL_WORKSPACE || "";
|
|
19
|
+
}
|
|
20
|
+
needRefresh() {
|
|
21
|
+
if (this.currentPromise)
|
|
22
|
+
return false;
|
|
23
|
+
if (this.accessToken) {
|
|
24
|
+
const decoded = jwtDecode(this.accessToken);
|
|
25
|
+
const { exp, iat } = decoded;
|
|
26
|
+
if (!exp || !iat)
|
|
27
|
+
return true;
|
|
28
|
+
const expDate = new Date(exp * 1000);
|
|
29
|
+
const iatDate = new Date(iat * 1000);
|
|
30
|
+
const nowDate = new Date();
|
|
31
|
+
const diff = expDate.getTime() - nowDate.getTime();
|
|
32
|
+
const iatDiff = expDate.getTime() - iatDate.getTime();
|
|
33
|
+
const ratio = diff / iatDiff;
|
|
34
|
+
return ratio < 0.5;
|
|
35
|
+
}
|
|
36
|
+
return true;
|
|
37
|
+
}
|
|
38
|
+
async authenticate() {
|
|
39
|
+
if (!this.needRefresh()) {
|
|
40
|
+
return this.currentPromise || Promise.resolve();
|
|
41
|
+
}
|
|
42
|
+
this.currentPromise = this.processWithRetry();
|
|
43
|
+
return this.currentPromise;
|
|
44
|
+
}
|
|
45
|
+
sleep(ms) {
|
|
46
|
+
return new Promise((resolve) => setTimeout(resolve, ms));
|
|
47
|
+
}
|
|
48
|
+
async processWithRetry(retry = 3) {
|
|
49
|
+
try {
|
|
50
|
+
return await this.process();
|
|
51
|
+
}
|
|
52
|
+
catch (error) {
|
|
53
|
+
if (retry > 0) {
|
|
54
|
+
await this.sleep(1000);
|
|
55
|
+
return this.processWithRetry(retry - 1);
|
|
56
|
+
}
|
|
57
|
+
throw error;
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
async process() {
|
|
61
|
+
const response = await oauthToken({
|
|
62
|
+
headers: {
|
|
63
|
+
Authorization: `Basic ${this.clientCredentials}`,
|
|
64
|
+
},
|
|
65
|
+
body: {
|
|
66
|
+
grant_type: "client_credentials",
|
|
67
|
+
},
|
|
68
|
+
});
|
|
69
|
+
if (response.error) {
|
|
70
|
+
throw new Error(response.error.error);
|
|
71
|
+
}
|
|
72
|
+
this.accessToken = response.data?.access_token || "";
|
|
73
|
+
this.currentPromise = null;
|
|
74
|
+
}
|
|
75
|
+
get authorization() {
|
|
76
|
+
return `Bearer ${this.accessToken}`;
|
|
77
|
+
}
|
|
78
|
+
get token() {
|
|
79
|
+
return this.accessToken;
|
|
80
|
+
}
|
|
81
|
+
}
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
import { jwtDecode } from "jwt-decode";
|
|
2
|
+
import { oauthToken } from "../client/authentication.js";
|
|
3
|
+
import { env } from "../common/env.js";
|
|
4
|
+
export class DeviceMode {
|
|
5
|
+
refreshToken;
|
|
6
|
+
deviceCode;
|
|
7
|
+
accessToken;
|
|
8
|
+
credentials;
|
|
9
|
+
currentPromise;
|
|
10
|
+
// private expireIn: number;
|
|
11
|
+
constructor(credentials) {
|
|
12
|
+
this.refreshToken = credentials.refresh_token || "";
|
|
13
|
+
this.deviceCode = credentials.device_code || "";
|
|
14
|
+
this.accessToken = credentials.access_token || "";
|
|
15
|
+
this.credentials = credentials;
|
|
16
|
+
this.currentPromise = null;
|
|
17
|
+
}
|
|
18
|
+
get workspace() {
|
|
19
|
+
return this.credentials.workspace || env.BL_WORKSPACE || "";
|
|
20
|
+
}
|
|
21
|
+
needRefresh() {
|
|
22
|
+
if (this.currentPromise)
|
|
23
|
+
return false;
|
|
24
|
+
if (this.accessToken) {
|
|
25
|
+
const decoded = jwtDecode(this.accessToken);
|
|
26
|
+
const { exp, iat } = decoded;
|
|
27
|
+
if (!exp || !iat)
|
|
28
|
+
return true;
|
|
29
|
+
const expDate = new Date(exp * 1000);
|
|
30
|
+
const iatDate = new Date(iat * 1000);
|
|
31
|
+
const nowDate = new Date();
|
|
32
|
+
const diff = expDate.getTime() - nowDate.getTime();
|
|
33
|
+
const iatDiff = expDate.getTime() - iatDate.getTime();
|
|
34
|
+
const ratio = diff / iatDiff;
|
|
35
|
+
return ratio < 0.5;
|
|
36
|
+
}
|
|
37
|
+
return true;
|
|
38
|
+
}
|
|
39
|
+
async authenticate() {
|
|
40
|
+
if (!this.needRefresh()) {
|
|
41
|
+
return this.currentPromise || Promise.resolve();
|
|
42
|
+
}
|
|
43
|
+
this.currentPromise = this.process();
|
|
44
|
+
return this.currentPromise;
|
|
45
|
+
}
|
|
46
|
+
async process() {
|
|
47
|
+
const response = await oauthToken({
|
|
48
|
+
body: {
|
|
49
|
+
grant_type: "refresh_token",
|
|
50
|
+
device_code: this.deviceCode,
|
|
51
|
+
refresh_token: this.refreshToken,
|
|
52
|
+
},
|
|
53
|
+
});
|
|
54
|
+
if (response.error) {
|
|
55
|
+
throw new Error(response.error.error);
|
|
56
|
+
}
|
|
57
|
+
this.accessToken = response.data?.access_token || "";
|
|
58
|
+
this.currentPromise = null;
|
|
59
|
+
}
|
|
60
|
+
get authorization() {
|
|
61
|
+
return `Bearer ${this.accessToken}`;
|
|
62
|
+
}
|
|
63
|
+
get token() {
|
|
64
|
+
return this.accessToken;
|
|
65
|
+
}
|
|
66
|
+
}
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
import yaml from 'yaml';
|
|
2
|
+
import { env } from "../common/env.js";
|
|
3
|
+
import { fs, os, path } from "../common/node.js";
|
|
4
|
+
import { ApiKey } from "./apikey.js";
|
|
5
|
+
import { ClientCredentials } from "./clientcredentials.js";
|
|
6
|
+
import { Credentials } from "./credentials.js";
|
|
7
|
+
import { DeviceMode } from "./deviceMode.js";
|
|
8
|
+
function getCredentials() {
|
|
9
|
+
if (env.BL_API_KEY) {
|
|
10
|
+
return {
|
|
11
|
+
apiKey: env.BL_API_KEY,
|
|
12
|
+
workspace: env.BL_WORKSPACE,
|
|
13
|
+
};
|
|
14
|
+
}
|
|
15
|
+
if (env.BL_CLIENT_CREDENTIALS) {
|
|
16
|
+
return {
|
|
17
|
+
clientCredentials: env.BL_CLIENT_CREDENTIALS,
|
|
18
|
+
workspace: env.BL_WORKSPACE,
|
|
19
|
+
};
|
|
20
|
+
}
|
|
21
|
+
if (os === null || fs === null || path === null) {
|
|
22
|
+
return null;
|
|
23
|
+
}
|
|
24
|
+
try {
|
|
25
|
+
const homeDir = os.homedir();
|
|
26
|
+
const config = fs.readFileSync(path.join(homeDir, ".blaxel/config.yaml"), "utf8");
|
|
27
|
+
const configJson = yaml.parse(config);
|
|
28
|
+
const workspaceName = env.BL_WORKSPACE || configJson.context.workspace;
|
|
29
|
+
const credentials = configJson.workspaces.find((wk) => wk.name === workspaceName)?.credentials;
|
|
30
|
+
if (!credentials) {
|
|
31
|
+
return null;
|
|
32
|
+
}
|
|
33
|
+
credentials.workspace = workspaceName;
|
|
34
|
+
return credentials;
|
|
35
|
+
}
|
|
36
|
+
catch {
|
|
37
|
+
// If any error (e.g., running in browser), just return null
|
|
38
|
+
return null;
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
export function authentication() {
|
|
42
|
+
const credentials = getCredentials();
|
|
43
|
+
if (!credentials) {
|
|
44
|
+
return new Credentials();
|
|
45
|
+
}
|
|
46
|
+
if (credentials.apiKey) {
|
|
47
|
+
return new ApiKey(credentials);
|
|
48
|
+
}
|
|
49
|
+
if (credentials.clientCredentials) {
|
|
50
|
+
return new ClientCredentials(credentials);
|
|
51
|
+
}
|
|
52
|
+
if (credentials.device_code) {
|
|
53
|
+
return new DeviceMode(credentials);
|
|
54
|
+
}
|
|
55
|
+
return new Credentials();
|
|
56
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import yaml from "yaml";
|
|
2
|
+
import { fs } from "../common/node.js";
|
|
3
|
+
const cache = new Map();
|
|
4
|
+
try {
|
|
5
|
+
if (fs) {
|
|
6
|
+
const cacheString = fs.readFileSync(".cache.yaml", "utf8");
|
|
7
|
+
const cacheData = yaml.parseAllDocuments(cacheString);
|
|
8
|
+
for (const doc of cacheData) {
|
|
9
|
+
const jsonDoc = doc.toJSON();
|
|
10
|
+
const cacheKey = `${jsonDoc.kind}/${jsonDoc.metadata.name}`;
|
|
11
|
+
cache.set(cacheKey, jsonDoc);
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
/* eslint-disable */
|
|
15
|
+
}
|
|
16
|
+
catch (error) { }
|
|
17
|
+
export async function findFromCache(resource, name) {
|
|
18
|
+
const cacheKey = `${resource}/${name}`;
|
|
19
|
+
return cache.get(cacheKey);
|
|
20
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './index.js';
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { settings } from "../common/settings.js";
|
|
2
|
+
export const interceptors = [
|
|
3
|
+
// Authentication interceptor
|
|
4
|
+
async (request, options) => {
|
|
5
|
+
if (options.authenticated === false) {
|
|
6
|
+
return request;
|
|
7
|
+
}
|
|
8
|
+
await settings.authenticate();
|
|
9
|
+
for (const header in settings.headers) {
|
|
10
|
+
request.headers.set(header, settings.headers[header]);
|
|
11
|
+
}
|
|
12
|
+
return request;
|
|
13
|
+
},
|
|
14
|
+
];
|