Open-source, secure environment with real-world tools for enterprise-grade agents.
Python agentaiai-agentai-agentscode-interpretercopilotdevelopmentdevtoolsgptgpt-4javascriptllmnextjsopenaipythonreactsoftwaretypescript
Add logs pretty printing and tail by default
Tomas Valenta5c59a7a7d22d
diff --git a/packages/cli/src/commands/sandbox/logs.ts b/packages/cli/src/commands/sandbox/logs.tsindex e0411a5b2..6cb498046 100644--- a/packages/cli/src/commands/sandbox/logs.ts+++ b/packages/cli/src/commands/sandbox/logs.ts@@ -1,29 +1,67 @@import * as commander from 'commander'import * as e2b from 'e2b'+import * as util from 'util'import { ensureAPIKey } from 'src/api'-import { asBold } from 'src/utils/format'+import { asBold, asTimestamp, asPrimary, asDim } from 'src/utils/format'+import { listSandboxes } from './list'const getSandboxLogs = e2b.withAPIKey(e2b.api.path('/sandboxes/{sandboxID}/logs').method('get').create(),)+const maxRuntime = 24 * 60 * 60 * 1000 // 24 hours in milliseconds+export const logsCommand = new commander.Command('logs')- .description('show logs for running sandbox')+ .description('show logs for sandbox').argument('<sandboxID>', `show longs for sandbox specified by ${asBold('<sandboxID>')}`).alias('lg').action(async (sandboxID: string) => {try {const apiKey = ensureAPIKey()+ const runningSandboxes = listSandboxes({ apiKey })++ console.log(util.inspect({+ apiKey,+ sandboxID,+ }, { colors: true, depth: null, maxArrayLength: Infinity, sorted: true, compact: true, breakLength: Infinity }))++ const startTime = new Date().getTime()- let offset = 0+ let start: number | undefined// eslint-disable-next-line no-constant-conditionwhile (true) {- const logs = await listSandboxLogs({ apiKey, sandboxID, offset })- offset = logs.logsOffset+ const currentTime = new Date().getTime()+ const elapsedTime = currentTime - startTime // Time elapsed in milliseconds++ // Check if 24 hours (in milliseconds) have passed+ if (elapsedTime >= maxRuntime) {+ console.log('Stopped printing logs after 24 hours.')+ break+ }++ const [info, logs] = await Promise.all([+ start === undefined ? runningSandboxes : undefined,+ listSandboxLogs({ apiKey, sandboxID, start }),+ ])++ if (info) {+ const sandbox = info.find(s => s.sandboxID === sandboxID)++ printSandboxInfo({+ isRunning: !!sandbox,+ envID: logs.envID,+ sandboxID: logs.sandboxID,+ })+ }for (const log of logs.logs) {- process.stdout.write(`${log}\n`)+ printLog(log.timestamp, log.line)+ }++ const lastLog = logs.logs[logs.logs.length - 1]+ if (lastLog) {+ start = new Date(lastLog.timestamp).getTime()}}} catch (err: any) {@@ -32,11 +70,35 @@ export const logsCommand = new commander.Command('logs')}})+function printSandboxInfo({ envID, sandboxID, isRunning }: {+ envID: string,+ sandboxID: string,+ isRunning: boolean,+}) {+ process.stdout.write(`Logs for ${asBold(isRunning ? 'running' : 'closed')} sandbox ${asPrimary(sandboxID)} created from template ${asDim(envID)}:\n\n`)+}++function printLog(timestamp: string, line: string) {+ const log = JSON.parse(line)++ delete log['TraceID']+ delete log['envID']+ delete log['instanceID']+ delete log['sandboxID']+ delete log['source_type']+ delete log['teamID']+ delete log['source']+ delete log['service']++ const time = `[${timestamp}]`+ console.log(`${asTimestamp(time)} ` + util.inspect(log, { colors: true, depth: null, maxArrayLength: Infinity, sorted: true, compact: true, breakLength: Infinity }))+}+export async function listSandboxLogs({apiKey,sandboxID,- offset,-}: { apiKey: string, sandboxID: string, offset: number }): Promise<e2b.components['schemas']['SandboxLogs']> {- const response = await getSandboxLogs(apiKey, { sandboxID, offset })+ start,+}: { apiKey: string, sandboxID: string, start?: number }): Promise<e2b.components['schemas']['SandboxLogs']> {+ const response = await getSandboxLogs(apiKey, { sandboxID, start })return response.data}diff --git a/packages/cli/src/utils/format.ts b/packages/cli/src/utils/format.tsindex 559248f57..229bc4f69 100644--- a/packages/cli/src/utils/format.ts+++ b/packages/cli/src/utils/format.ts@@ -41,6 +41,10 @@ export function asPrimary(content: string) {return chalk.default.hex(primaryColor)(content)}+export function asTimestamp(content: string) {+ return chalk.default.blue(content)+}+export function asSandboxTemplate(pathInTemplate?: string) {return chalk.default.green(pathInTemplate)}