@blaxel/core 0.2.0-preview5 → 0.2.1-dev.46

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.
@@ -1,213 +0,0 @@
1
- "use strict";
2
- var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
- if (k2 === undefined) k2 = k;
4
- var desc = Object.getOwnPropertyDescriptor(m, k);
5
- if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
- desc = { enumerable: true, get: function() { return m[k]; } };
7
- }
8
- Object.defineProperty(o, k2, desc);
9
- }) : (function(o, m, k, k2) {
10
- if (k2 === undefined) k2 = k;
11
- o[k2] = m[k];
12
- }));
13
- var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
- Object.defineProperty(o, "default", { enumerable: true, value: v });
15
- }) : function(o, v) {
16
- o["default"] = v;
17
- });
18
- var __importStar = (this && this.__importStar) || (function () {
19
- var ownKeys = function(o) {
20
- ownKeys = Object.getOwnPropertyNames || function (o) {
21
- var ar = [];
22
- for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
23
- return ar;
24
- };
25
- return ownKeys(o);
26
- };
27
- return function (mod) {
28
- if (mod && mod.__esModule) return mod;
29
- var result = {};
30
- if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
31
- __setModuleDefault(result, mod);
32
- return result;
33
- };
34
- })();
35
- Object.defineProperty(exports, "__esModule", { value: true });
36
- exports.BlaxelWebsocketMcpServerTransport = void 0;
37
- const uuid_1 = require("uuid");
38
- const ws_1 = __importStar(require("ws"));
39
- const env_js_1 = require("../../common/env.js");
40
- const logger_js_1 = require("../../common/logger.js");
41
- const telemetry_js_1 = require("../../telemetry/telemetry.js");
42
- const spans = new Map();
43
- class BlaxelWebsocketMcpServerTransport {
44
- port;
45
- wss;
46
- clients = new Map();
47
- onclose;
48
- onerror;
49
- messageHandler;
50
- onconnection;
51
- ondisconnection;
52
- set onmessage(handler) {
53
- this.messageHandler = handler
54
- ? (msg, clientId) => {
55
- if (!("id" in msg)) {
56
- return handler(msg);
57
- }
58
- return handler({
59
- ...msg,
60
- id: clientId + ":" + msg.id,
61
- });
62
- }
63
- : undefined;
64
- }
65
- constructor(port) {
66
- this.port = port ?? parseInt(env_js_1.env.BL_SERVER_PORT ?? "8080", 10);
67
- this.wss = new ws_1.WebSocketServer({ port: this.port });
68
- }
69
- async start() {
70
- logger_js_1.logger.info("Starting WebSocket Server on port " + this.port);
71
- this.wss.on("connection", (ws) => {
72
- const clientId = (0, uuid_1.v4)();
73
- this.clients.set(clientId, {
74
- ws,
75
- });
76
- this.onconnection?.(clientId);
77
- ws.on("message", (data) => {
78
- const span = (0, telemetry_js_1.startSpan)("message", {
79
- attributes: {
80
- "mcp.client.id": clientId,
81
- "span.type": "mcp.message",
82
- },
83
- isRoot: false,
84
- });
85
- try {
86
- const msg = JSON.parse(data.toString());
87
- this.messageHandler?.(msg, clientId);
88
- if ("method" in msg && "id" in msg && "params" in msg) {
89
- span.setAttributes({
90
- "mcp.message.parsed": true,
91
- "mcp.method": msg.method,
92
- "mcp.messageId": msg.id,
93
- "mcp.toolName": msg.params?.name,
94
- });
95
- spans.set(clientId + ":" + msg.id, span);
96
- }
97
- // Handle msg.id safely
98
- const msgId = msg.id ? String(msg.id) : "";
99
- const [cId, parsedMsgId] = msgId.split(":");
100
- msg.id = parsedMsgId ? parseInt(parsedMsgId) : undefined;
101
- // Use optional chaining for safe access
102
- const client = this.clients.get(cId ?? "");
103
- if (client?.ws?.readyState === ws_1.default.OPEN) {
104
- const msgSpan = spans.get(cId + ":" + (msg.id ?? ""));
105
- try {
106
- client.ws.send(JSON.stringify(msg));
107
- if (msgSpan) {
108
- msgSpan.setAttributes({
109
- "mcp.message.response_sent": true,
110
- });
111
- }
112
- }
113
- catch (err) {
114
- if (msgSpan) {
115
- msgSpan.setStatus("error"); // Error status
116
- msgSpan.recordException(err);
117
- }
118
- throw err;
119
- }
120
- finally {
121
- if (msgSpan) {
122
- msgSpan.end();
123
- }
124
- }
125
- }
126
- else {
127
- this.clients.delete(cId);
128
- this.ondisconnection?.(cId);
129
- }
130
- }
131
- catch (err) {
132
- if (err instanceof Error) {
133
- span.setStatus("error"); // Error status
134
- span.recordException(err);
135
- this.onerror?.(err);
136
- }
137
- else {
138
- this.onerror?.(new Error(`Failed to parse message: ${String(err)}`));
139
- }
140
- span.end();
141
- }
142
- });
143
- ws.on("close", () => {
144
- this.clients.delete(clientId);
145
- this.ondisconnection?.(clientId);
146
- });
147
- ws.on("error", (err) => {
148
- this.onerror?.(err);
149
- });
150
- });
151
- return Promise.resolve();
152
- }
153
- async send(msg) {
154
- const [cId, msgId] = msg.id ? String(msg.id).split(":") : [];
155
- msg.id = parseInt(msgId);
156
- const data = JSON.stringify(msg);
157
- const deadClients = [];
158
- if (cId) {
159
- // Send to specific client
160
- const client = this.clients.get(cId);
161
- if (client?.ws?.readyState === ws_1.default.OPEN) {
162
- const msgSpan = spans.get(cId + ":" + msg.id);
163
- try {
164
- client.ws.send(data);
165
- if (msgSpan) {
166
- msgSpan.setAttributes({
167
- "mcp.message.response_sent": true,
168
- });
169
- }
170
- }
171
- catch (err) {
172
- if (msgSpan) {
173
- msgSpan.setStatus("error"); // Error status
174
- msgSpan.recordException(err);
175
- }
176
- throw err;
177
- }
178
- finally {
179
- if (msgSpan) {
180
- msgSpan.end();
181
- }
182
- }
183
- }
184
- else {
185
- this.clients.delete(cId);
186
- this.ondisconnection?.(cId);
187
- }
188
- }
189
- for (const [id, client] of this.clients.entries()) {
190
- if (client.ws.readyState !== ws_1.default.OPEN) {
191
- deadClients.push(id);
192
- }
193
- }
194
- // Cleanup dead clients
195
- deadClients.forEach((id) => {
196
- this.clients.delete(id);
197
- this.ondisconnection?.(id);
198
- });
199
- return Promise.resolve();
200
- }
201
- async broadcast(msg) {
202
- return this.send(msg);
203
- }
204
- async close() {
205
- return new Promise((resolve) => {
206
- this.wss.close(() => {
207
- this.clients.clear();
208
- resolve();
209
- });
210
- });
211
- }
212
- }
213
- exports.BlaxelWebsocketMcpServerTransport = BlaxelWebsocketMcpServerTransport;
@@ -1,21 +0,0 @@
1
- import { Sandbox } from "../client/index.js";
2
- import { SessionCreateOptions, SessionWithToken } from "./types.js";
3
- export declare class SandboxSessions {
4
- private sandbox;
5
- constructor(sandbox: Sandbox);
6
- get sandboxName(): string;
7
- create(options?: SessionCreateOptions): Promise<SessionWithToken>;
8
- list(): Promise<{
9
- name: string;
10
- url: string;
11
- token: string;
12
- expiresAt: string | Date;
13
- }[]>;
14
- get(name: string): Promise<{
15
- url: string;
16
- token: string;
17
- expiresAt: string | Date;
18
- }>;
19
- delete(name: string): Promise<import("../client/types.gen.js").Preview>;
20
- getToken(previewName: string): Promise<import("../client/types.gen.js").PreviewToken | null>;
21
- }
@@ -1,97 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.SandboxSessions = void 0;
4
- const index_js_1 = require("../client/index.js");
5
- const preview_js_1 = require("./preview.js");
6
- class SandboxSessions {
7
- sandbox;
8
- constructor(sandbox) {
9
- this.sandbox = sandbox;
10
- }
11
- get sandboxName() {
12
- return this.sandbox.metadata?.name ?? "";
13
- }
14
- async create(options = {}) {
15
- const expiresAt = options.expiresAt ?? new Date(Date.now() + 24 * 60 * 60 * 1000); // 1 day from now
16
- const body = {
17
- metadata: {
18
- name: "session-" + Date.now(),
19
- },
20
- spec: {
21
- port: 443,
22
- public: false,
23
- },
24
- };
25
- const { data } = await (0, index_js_1.createSandboxPreview)({
26
- path: {
27
- sandboxName: this.sandboxName,
28
- },
29
- body,
30
- throwOnError: true,
31
- });
32
- const preview = new preview_js_1.SandboxPreview(data);
33
- // Create a token for the preview with the given expiresAt
34
- const tokenObj = await preview.tokens.create(expiresAt);
35
- return {
36
- name: body.metadata.name,
37
- url: preview.spec?.url ?? "",
38
- token: tokenObj.value,
39
- expiresAt: typeof tokenObj.expiresAt === 'string' ? new Date(tokenObj.expiresAt) : tokenObj.expiresAt,
40
- };
41
- }
42
- async list() {
43
- const { data } = await (0, index_js_1.listSandboxPreviews)({
44
- path: {
45
- sandboxName: this.sandboxName,
46
- },
47
- throwOnError: true,
48
- });
49
- return await Promise.all(data.filter((preview) => preview.metadata?.name?.includes("session-")).map(async (preview) => {
50
- const token = await this.getToken(preview.metadata?.name ?? "");
51
- return {
52
- name: preview.metadata?.name ?? "",
53
- url: preview.spec?.url ?? "",
54
- token: token?.spec?.token ?? "",
55
- expiresAt: token?.spec?.expiresAt ?? new Date(),
56
- };
57
- }));
58
- }
59
- async get(name) {
60
- const { data } = await (0, index_js_1.getSandboxPreview)({
61
- path: {
62
- sandboxName: this.sandboxName,
63
- previewName: name,
64
- },
65
- throwOnError: true,
66
- });
67
- const token = await this.getToken(name);
68
- return {
69
- url: data.spec?.url ?? "",
70
- token: token?.spec?.token ?? "",
71
- expiresAt: token?.spec?.expiresAt ?? new Date(),
72
- };
73
- }
74
- async delete(name) {
75
- const { data } = await (0, index_js_1.deleteSandboxPreview)({
76
- path: {
77
- sandboxName: this.sandboxName,
78
- previewName: name,
79
- },
80
- throwOnError: true,
81
- });
82
- return data;
83
- }
84
- async getToken(previewName) {
85
- const { data } = await (0, index_js_1.listSandboxPreviewTokens)({
86
- path: {
87
- sandboxName: this.sandboxName,
88
- previewName,
89
- },
90
- throwOnError: true,
91
- });
92
- if (data.length === 0)
93
- return null;
94
- return data[0];
95
- }
96
- }
97
- exports.SandboxSessions = SandboxSessions;
@@ -1,15 +0,0 @@
1
- import { Sandbox } from "../client/types.gen";
2
- export interface SessionCreateOptions {
3
- expiresAt?: Date;
4
- }
5
- export interface SessionWithToken {
6
- name: string;
7
- url: string;
8
- token: string;
9
- expiresAt: Date;
10
- }
11
- export type SandboxConfiguration = {
12
- forceUrl?: string;
13
- headers?: Record<string, string>;
14
- params?: Record<string, string>;
15
- } & Sandbox;
@@ -1,2 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });