UNPKG

cerevox

Version:

TypeScript SDK for browser automation and secure command execution in highly available and scalable micro computer environments

132 lines 4.94 kB
"use strict"; var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) { var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d; if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc); else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r; return c > 3 && r && Object.defineProperty(target, key, r), r; }; var __metadata = (this && this.__metadata) || function (k, v) { if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v); }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.Session = exports.sessions = void 0; const base_1 = require("./base"); const terminal_1 = require("./terminal"); const code_runner_1 = require("./code-runner"); const file_system_1 = require("./file-system"); const ws_1 = __importDefault(require("ws")); const browser_1 = require("./browser"); const ai_1 = require("./ai"); const constants_1 = require("../utils/constants"); const sandbox_1 = require("./sandbox"); exports.sessions = {}; let Session = class Session extends base_1.BaseClass { constructor(sandbox, options = {}) { super(options.logLevel); this.sandbox = sandbox; this.ws = null; this.keepAliveTimer = null; this.isClosed = false; this.workspace = options.workspace || 'unknown'; this.terminal = new terminal_1.Terminal(this, options); this.files = new file_system_1.FileSystem(this); this.browser = new browser_1.Browser(this); this.ai = new ai_1.AI(this); this.codeRunner = new code_runner_1.CodeRunner(this); exports.sessions[sandbox.sandboxId] = this; } async connect() { const wsUrl = await this.browser.getCDPEndpoint(); return new Promise(resolve => { this.ws = new ws_1.default(wsUrl, { headers: { 'keep-alive-ms': this.sandbox.keepAliveMS.toString(), }, }); this.ws.on('open', () => { if (!this.isClosed) { this.keepAliveTimer = setTimeout(() => { this.keepAlive(); }, 10000); } else { this.ws.close(); } resolve(this.ws); }); this.ws.on('close', async () => { this.logger.debug('WebSocket disconnected'); clearInterval(this.keepAliveTimer); }); }); } keepAlive() { // 因为走的是 cloudflare,还不能用空的 ping/pong const ws = this.ws; if (ws.readyState === ws_1.default.OPEN) { // ws.ping(); ws.send(JSON.stringify({ type: 'heartbeat' })); } ws.once('message', data => { // console.log('pong', data.toString()); this.keepAliveTimer = setTimeout(() => { this.keepAlive(); }, 10000); }); } async isRunning() { return this.sandbox.isRunning(); } get id() { return this.sandbox.sandboxId; } getLogger() { return this.logger; } track(event, context) { context = { ...context, distinct_id: this.workspace, }; const logUrl = `/api/track?event=${event}&context=${encodeURIComponent(JSON.stringify(context))}`; return this.sandbox.request(logUrl, { method: 'GET', headers: { 'Content-Type': 'application/json', }, }); } async setTimeout(ms) { await this.sandbox.setTimeout(ms); } async close(forceToKill = false) { if (this.ws?.readyState === ws_1.default.OPEN) { this.ws?.close(); clearInterval(this.keepAliveTimer); } this.isClosed = true; if (forceToKill) { // 主进程 // await this.process?.kill(); try { if (await this.sandbox.isRunning()) { await this.sandbox.kill(); } } catch (error) { /* empty */ } } delete exports.sessions[this.id]; this.logger.debug(`Session ${this.id} closed`); } }; exports.Session = Session; exports.Session = Session = __decorate([ (0, base_1.Logger)({ VERSION: constants_1.VERSION }), __metadata("design:paramtypes", [sandbox_1.Sandbox, Object]) ], Session); //# sourceMappingURL=session.js.map