Open-source, secure environment with real-world tools for enterprise-grade agents.
Python agentaiai-agentai-agentscode-interpretercopilotdevelopmentdevtoolsgptgpt-4javascriptllmnextjsopenaipythonreactsoftwaretypescript
feat(js-sdk): JS SDK logging v2 (#107)
Pavel 'Strajk' Dolecek8728e7f9827e
diff --git a/.changeset/gorgeous-bags-cry.md b/.changeset/gorgeous-bags-cry.mdnew file mode 100644index 000000000..53de25b23--- /dev/null+++ b/.changeset/gorgeous-bags-cry.md@@ -0,0 +1,5 @@+---+"@e2b/sdk": patch+---++Improve sdk loggingdiff --git a/packages/js-sdk/src/session/index.ts b/packages/js-sdk/src/session/index.tsindex 0501974c6..f8a7f9ddd 100644--- a/packages/js-sdk/src/session/index.ts+++ b/packages/js-sdk/src/session/index.ts@@ -108,6 +108,7 @@ export class Session extends SessionConnection {* @returns new watcher*/watchDir: (path: string) => {+ this.logger.debug?.(`Watching directory "${path}"`)const npath = normalizePath(path)return new FilesystemWatcher(this, npath)},@@ -124,6 +125,7 @@ export class Session extends SessionConnection {rootdir = '',terminalID = id(12),}) => {+ this.logger.debug?.(`Starting terminal "${terminalID}"`)const { promise: terminalExited, resolve: triggerExit } = createDeferredPromise()const output = new TerminalOutput()@@ -146,10 +148,11 @@ export class Session extends SessionConnection {this.unsubscribe(onExitSubID),this.unsubscribe(onDataSubID),])+ this.logger.debug?.(`Terminal "${terminalID}" exited`)const errMsg = formatSettledErrors(results)if (errMsg) {- this.logger.error(errMsg)+ this.logger.error?.(errMsg)}onExit?.()@@ -188,6 +191,7 @@ export class Session extends SessionConnection {if (!cmd) {throw new Error('cmd is required')}+ this.logger.debug?.(`Starting process "${processID}"`)const { promise: processExited, resolve: triggerExit } = createDeferredPromise()@@ -221,10 +225,11 @@ export class Session extends SessionConnection {onStdoutSubID ? this.unsubscribe(onStdoutSubID) : undefined,onStderrSubID ? this.unsubscribe(onStderrSubID) : undefined,])+ this.logger.debug?.(`Process "${processID}" exited`)const errMsg = formatSettledErrors(results)if (errMsg) {- this.logger.error(errMsg)+ this.logger.error?.(errMsg)}onExit?.()@@ -251,14 +256,14 @@ export class Session extends SessionConnection {override async open() {await super.open()- const portsHander = this.onScanPorts+ const portsHandler = this.onScanPorts? (ports: { State: string; Ip: string; Port: number }[]) =>this.onScanPorts?.(ports.map(p => ({ ip: p.Ip, port: p.Port, state: p.State }))): undefinedawait this.handleSubscriptions(- portsHander- ? this.subscribe(codeSnippetService, portsHander, 'scanOpenedPorts')+ portsHandler+ ? this.subscribe(codeSnippetService, portsHandler, 'scanOpenedPorts'): undefined,)diff --git a/packages/js-sdk/src/session/sessionConnection.ts b/packages/js-sdk/src/session/sessionConnection.tsindex 3250b1edb..5697d01ab 100644--- a/packages/js-sdk/src/session/sessionConnection.ts+++ b/packages/js-sdk/src/session/sessionConnection.ts@@ -8,7 +8,6 @@ import {WS_RECONNECT_INTERVAL,WS_ROUTE,} from '../constants'-import Logger from '../utils/logger'import { assertFulfilled, formatSettledErrors } from '../utils/promise'import wait from '../utils/wait'import { codeSnippetService } from './codeSnippet'@@ -32,10 +31,17 @@ interface Subscriber {handler: SubscriptionHandler}+interface Logger {+ debug?: (message: string, ...args: unknown[]) => void+ info?: (message: string, ...args: unknown[]) => void+ warn?: (message: string, ...args: unknown[]) => void+ error?: (message: string, ...args: unknown[]) => void+}+export interface SessionConnectionOpts {id: stringapiKey: string- debug?: boolean+ logger?: Logger__debug_hostname?: string__debug_port?: number__debug_devEnv?: 'remote' | 'local'@@ -61,9 +67,12 @@ export class SessionConnection {'API key is required, please visit https://e2b.dev/docs to get your API key',)}-- this.logger = new Logger('Session', opts.debug)- this.logger.log(`Session for code snippet "${opts.id}" initialized`)+ this.logger = opts.logger ?? {+ // by default, we log to the console, only warnings and errors+ warn: console.warn,+ error: console.error,+ }+ this.logger.info?.(`Session for code snippet "${opts.id}" initialized`)}/**@@ -105,16 +114,16 @@ export class SessionConnection {*/async close() {if (this.isOpen) {- this.logger.log('Closing', this.session)+ this.logger.debug?.(`Closing session "${this.session?.sessionID}"`)this.isOpen = false- this.logger.log('Unsubscribing...')+ this.logger.debug?.('Unsubscribing...')const results = await Promise.allSettled(this.subscribers.map(s => this.unsubscribe(s.subID)),)results.forEach(r => {if (r.status === 'rejected') {- this.logger.log(`Failed to unsubscribe: "${r.reason}"`)+ this.logger.warn?.(`Failed to unsubscribe: "${r.reason}"`)}})@@ -122,7 +131,7 @@ export class SessionConnection {this.rpc.ws?.terminate?.()// This is the browser WebSocket way of closing connectionthis.rpc.ws?.close?.()- this.logger.log('Disconected from the session')+ this.logger.info?.('Disconnected from the session')}}@@ -137,6 +146,7 @@ export class SessionConnection {} else {this.isOpen = true}+ this.logger.debug?.('Opening session...')if (!this.opts.__debug_hostname) {try {@@ -146,7 +156,7 @@ export class SessionConnection {editEnabled: false,})this.session = res.data- this.logger.log('Aquired session:', this.session)+ this.logger.debug?.(`Acquired session "${this.session.sessionID}"`)this.refresh(this.session.sessionID)} catch (e) {@@ -181,8 +191,12 @@ export class SessionConnection {const protocol = this.opts.__debug_devEnv === 'local' ? 'ws' : 'wss'const sessionURL = `${protocol}://${hostname}${WS_ROUTE}`- this.rpc.onError(e => {- this.logger.log('Error in WS session:', this.session, e)+ this.rpc.onError(err => {+ this.logger.warn?.(+ `Error in WS session "${this.session?.sessionID}": ${+ err.message ?? err.code ?? err.toString()+ }. Trying to reconnect...`,+ )})let isFinished = false@@ -203,24 +217,28 @@ export class SessionConnection {})this.rpc.onOpen(() => {- this.logger.log('Connected to session:', this.session)+ this.logger.debug?.(`Connected to session "${this.session?.sessionID}"`)resolveOpening?.()})this.rpc.onClose(async e => {- this.logger.log('Closing WS connection to session:', this.session, e)+ this.logger.debug?.(`Closing WS connection to session "${this.session?.sessionID}"`)if (this.isOpen) {await wait(WS_RECONNECT_INTERVAL)- this.logger.log('Reconnecting to session:', this.session)+ this.logger.debug?.(`Reconnecting to session "${this.session?.sessionID}"`)try {// When the WS connection closes the subscribers in devbookd are removed.// We want to delete the subscriber handlers here so there are no orphans.this.subscribers = []await this.rpc.connect(sessionURL)- this.logger.log('Reconnected to session:', this.session)+ this.logger.debug?.(`Reconnected to session "${this.session?.sessionID}"`)// eslint-disable-next-line @typescript-eslint/no-explicit-any- } catch (e: any) {- this.logger.log('Failed reconnecting to session:', this.session, e)+ } catch (err: any) {+ this.logger.warn?.(+ `Failed reconnecting to session "${this.session?.sessionID}": ${+ err.message ?? err.code ?? err.toString()+ }`,+ )}} else {rejectOpening?.()@@ -230,12 +248,16 @@ export class SessionConnection {this.rpc.onNotification.push(this.handleNotification.bind(this))try {- this.logger.log('Connection to session:', this.session)+ this.logger.debug?.(`Connection to session "${this.session?.sessionID}"`)await this.rpc.connect(sessionURL)// eslint-disable-next-line @typescript-eslint/no-explicit-any- } catch (e: any) {- this.logger.log('Error connecting to session', this.session, e)+ } catch (err: any) {+ this.logger.warn?.(+ `Error connecting to session "${this.session?.sessionID}": ${+ err.message ?? err.code ?? err.toString()+ }`,+ )}await openingPromise@@ -244,6 +266,7 @@ export class SessionConnection {// eslint-disable-next-line @typescript-eslint/no-explicit-anyasync call(service: Service, method: string, params?: any[]) {+ this.logger.debug?.(`Calling "${service}_${method}" with params:`, params)return this.rpc.call(`${service}_${method}`, params)}@@ -279,7 +302,7 @@ export class SessionConnection {await this.call(subscription.service, 'unsubscribe', [subscription.subID])this.subscribers = this.subscribers.filter(s => s !== subscription)- this.logger.log(`Unsubscribed '${subID}' from '${subscription.service}'`)+ this.logger.debug?.(`Unsubscribed '${subID}' from '${subscription.service}'`)}// eslint-disable-next-line @typescript-eslint/no-explicit-any,@typescript-eslint/member-ordering@@ -307,7 +330,7 @@ export class SessionConnection {service,subID,})- this.logger.log(+ this.logger.debug?.(// eslint-disable-next-line prettier/prettier`Subscribed to "${service}_${method}"${params.length > 0 ? ' with params [' + params.join(', ') + '] and' : ''} with id "${subID}"`,@@ -317,26 +340,29 @@ export class SessionConnection {}private handleNotification(data: IRpcNotification) {+ this.logger.debug?.('Handling notification:', data)this.subscribers.filter(s => s.subID === data.params?.subscription).forEach(s => s.handler(data.params?.result))}private async refresh(sessionID: string) {- this.logger.log(`Started refreshing session "${sessionID}"`)+ this.logger.debug?.(`Started refreshing session "${sessionID}"`)try {// eslint-disable-next-line no-constant-conditionwhile (true) {if (!this.isOpen) {- this.logger.log('Cannot refresh session - it was closed', this.session)+ this.logger.warn?.(+ `Cannot refresh session ${this.session?.sessionID} - it was closed`,+ )return}await wait(SESSION_REFRESH_PERIOD)try {- this.logger.log(`Refreshed session "${sessionID}"`)+ this.logger.debug?.(`Refreshed session "${sessionID}"`)await refreshSession({api_key: this.opts.apiKey,sessionID,@@ -345,19 +371,19 @@ export class SessionConnection {if (e instanceof refreshSession.Error) {const error = e.getActualType()if (error.status === 404) {- this.logger.error(+ this.logger.warn?.(`Error refreshing session - (${error.status}): ${error.data.message}`,)return}- this.logger.error(+ this.logger.warn?.(`Refreshing session "${sessionID}" failed - (${error.status})`,)}}}} finally {- this.logger.log(`Stopped refreshing session "${sessionID}"`)+ this.logger.debug?.(`Stopped refreshing session "${sessionID}"`)this.close()}}diff --git a/packages/js-sdk/src/utils/logger.ts b/packages/js-sdk/src/utils/logger.tsdeleted file mode 100644index 052558307..000000000--- a/packages/js-sdk/src/utils/logger.ts+++ /dev/null@@ -1,23 +0,0 @@-/* eslint-disable @typescript-eslint/no-explicit-any */-type LogID = string | (() => string)--class Logger {- constructor(public readonly logID: LogID, public readonly isEnabled = false) {}-- error(...args: any[]) {- console.error(`\x1b[31m[${this.id()} ERROR]\x1b[0m`, ...args)- }-- log(...args: any[]) {- if (this.isEnabled) {- console.log(`\x1b[36m[${this.id()}]\x1b[0m`, ...args)- }- }-- private id() {- if (typeof this.logID === 'function') return this.logID()- return this.logID- }-}--export default Loggerdiff --git a/packages/js-sdk/test/logging.test.mjs b/packages/js-sdk/test/logging.test.mjsnew file mode 100644index 000000000..a0a5587a9--- /dev/null+++ b/packages/js-sdk/test/logging.test.mjs@@ -0,0 +1,61 @@+import { Session } from '../src'+import { expect, test, vi } from 'vitest'++test('no logs in console during very basic scenario', async () => {+ const consoleSpy = {+ debug: vi.spyOn(console, "debug"),+ info: vi.spyOn(console, "info"),+ warn: vi.spyOn(console, "warn"),+ error: vi.spyOn(console, "error"),+ }++ const session = await Session.create({ id: "Nodejs", apiKey: process.env.E2B_API_KEY });+ await session.filesystem.write("test.txt", "Hello World");+ await session.close()++ expect(consoleSpy.debug).toHaveBeenCalledTimes(0);+ expect(consoleSpy.info).toHaveBeenCalledTimes(0);+ expect(consoleSpy.error).toHaveBeenCalledTimes(0);+})++test('warn logs in console during convoluted scenario', async () => {+ const consoleSpy = {+ debug: vi.spyOn(console, "debug"),+ info: vi.spyOn(console, "info"),+ warn: vi.spyOn(console, "warn"),+ error: vi.spyOn(console, "error"),+ }++ const session = await Session.create({ id: "Nodejs", apiKey: process.env.E2B_API_KEY });+ await session.close() // Note that we are intentionally closing and then trying to write+ const warnsAmount = consoleSpy.warn.mock.calls.length++ // void to explicitly not awaiting, we wanna check if logging is happening correctly during retries+ void session.filesystem.read('/etc/hosts') // this should trigger retries+ setTimeout(() => {+ expect(consoleSpy.warn.mock.calls.length).toBeGreaterThan(warnsAmount) // should have logged a warning+ }, 2000) // wait for some retries to occur+})++test('verbose & info logs in console when opted-in', async () => {+ const consoleSpy = {+ info: vi.spyOn(console, "info"),+ warn: vi.spyOn(console, "warn"),+ error: vi.spyOn(console, "error"),+ }++ const logger = {+ info: console.info,+ warn: console.warn,+ error: console.error,+ }++ const session = await Session.create({ id: "Nodejs", apiKey: process.env.E2B_API_KEY, logger });+ await session.filesystem.write("test.txt", "Hello World");+ await session.close()++ expect(consoleSpy.info).toHaveBeenCalled();+ expect(consoleSpy.error).toHaveBeenCalledTimes(0);+})++