vibelogger
Version:
AI-Native Logging for LLM Agent Development - TypeScript/Node.js Implementation
140 lines • 4.47 kB
JavaScript
/**
* Environment detection and information gathering utilities
*
* Collects runtime environment information for log entries,
* similar to Python's EnvironmentInfo.collect()
*/
import { platform, type, arch } from 'os';
/**
* Environment information collector
*/
export class EnvironmentCollector {
static cachedInfo = null;
/**
* Collect current environment information
* Caches result for performance since environment doesn't change during runtime
*/
static collect() {
if (this.cachedInfo === null) {
this.cachedInfo = {
node_version: this.getNodeVersion(),
os: platform(),
platform: type(),
architecture: arch(),
runtime: this.detectRuntime()
};
}
return this.cachedInfo;
}
/**
* Force refresh of cached environment information
*/
static refresh() {
this.cachedInfo = null;
return this.collect();
}
/**
* Get Node.js version
*/
static getNodeVersion() {
try {
return process.version || 'unknown';
}
catch {
return 'unknown';
}
}
/**
* Detect the current JavaScript runtime environment
*/
static detectRuntime() {
try {
// Check for Node.js
if (typeof process !== 'undefined' && process.versions?.node) {
return 'node';
}
// Check for Deno
if (typeof globalThis.Deno !== 'undefined') {
return 'deno';
}
// Check for Bun
if (typeof globalThis.Bun !== 'undefined') {
return 'bun';
}
// Check for browser
if (typeof globalThis !== 'undefined' && 'window' in globalThis) {
return 'browser';
}
return 'unknown';
}
catch {
return 'unknown';
}
}
/**
* Get additional runtime information
*/
static getExtendedInfo() {
const info = {};
try {
// Node.js specific information
if (typeof process !== 'undefined') {
info.processId = process.pid;
info.nodeEnv = process.env.NODE_ENV || 'development';
info.platform_version = process.platform;
if (process.versions) {
info.v8_version = process.versions.v8;
info.uv_version = process.versions.uv;
info.openssl_version = process.versions.openssl;
}
if (process.memoryUsage) {
const memory = process.memoryUsage();
info.memory_usage = {
rss: memory.rss,
heapTotal: memory.heapTotal,
heapUsed: memory.heapUsed,
external: memory.external
};
}
}
// Browser specific information
if (typeof globalThis !== 'undefined' && 'window' in globalThis && 'navigator' in globalThis) {
const navigator = globalThis.navigator;
info.user_agent = navigator?.userAgent;
info.browser_language = navigator?.language;
info.browser_platform = navigator?.platform;
}
// Deno specific information
if (typeof globalThis.Deno !== 'undefined') {
const Deno = globalThis.Deno;
info.deno_version = Deno.version?.deno;
info.typescript_version = Deno.version?.typescript;
}
// Bun specific information
if (typeof globalThis.Bun !== 'undefined') {
const Bun = globalThis.Bun;
info.bun_version = Bun.version;
}
}
catch (error) {
info.environment_error = String(error);
}
return info;
}
/**
* Check if running in specific environment
*/
static isNode() {
return this.detectRuntime() === 'node';
}
static isBrowser() {
return this.detectRuntime() === 'browser';
}
static isDeno() {
return this.detectRuntime() === 'deno';
}
static isBun() {
return this.detectRuntime() === 'bun';
}
}
//# sourceMappingURL=environment.js.map