@code-yeongyu/senpi
Version:
Coding agent CLI with read, bash, edit, write tools and session management
3 lines (2 loc) • 2.86 kB
JavaScript
import { createRequire as __piCreateRequire } from "node:module"; const require = __piCreateRequire(import.meta.url); { const __piWorkerThreads = require("node:worker_threads"); if (typeof __piWorkerThreads.markAsUncloneable !== "function") { __piWorkerThreads.markAsUncloneable = () => {}; } }
import{readFileSync}from"node:fs";var RUSAGE_INFO_V2=2,RI_PHYS_FOOTPRINT_OFFSET=72,RUSAGE_BUFFER_BYTES=256,PROCESS_QUERY_LIMITED_INFORMATION=4096,MEMORY_COUNTERS_EX_BYTES=80,PRIVATE_USAGE_OFFSET=72,platformReader;function readOwnFootprint(){return readProcessFootprint(process.pid)??{bytes:process.memoryUsage.rss(),measure:"rss"}}function readProcessFootprint(pid){if(!Number.isInteger(pid)||pid<=0)return;let reading=loadPlatformReader()?.(pid);return reading!==void 0||pid!==process.pid?reading:{bytes:process.memoryUsage.rss(),measure:"rss"}}function parseProcStatusRssAnon(status){let match=/^RssAnon:\s+(\d+)\s+kB$/m.exec(status);return match?.[1]===void 0?void 0:Number(match[1])*1024}function loadPlatformReader(){return platformReader===void 0&&(platformReader=createPlatformReader()??null),platformReader??void 0}function createPlatformReader(){if(process.platform==="linux")return linuxReader;if(process.platform!=="darwin"&&process.platform!=="win32")return;let ffi=process.getBuiltinModule("bun:ffi");if(ffi!==void 0)try{return process.platform==="darwin"?darwinReader(ffi):windowsReader(ffi)}catch{return}}function linuxReader(pid){let status;try{status=readFileSync(`/proc/${pid}/status`,"utf8")}catch{return}let bytes=parseProcStatusRssAnon(status);return bytes===void 0?void 0:{bytes,measure:"rss_anon"}}function darwinReader({dlopen,FFIType,ptr}){let library=dlopen("libSystem.B.dylib",{proc_pid_rusage:{args:[FFIType.i32,FFIType.i32,FFIType.ptr],returns:FFIType.i32}}),buffer=new Uint8Array(RUSAGE_BUFFER_BYTES),view=new DataView(buffer.buffer);return pid=>{if(buffer.fill(0),library.symbols.proc_pid_rusage(pid,RUSAGE_INFO_V2,ptr(buffer))===0)return{bytes:Number(view.getBigUint64(RI_PHYS_FOOTPRINT_OFFSET,!0)),measure:"phys_footprint"}}}function windowsReader({dlopen,FFIType,ptr}){let library=dlopen("kernel32.dll",{OpenProcess:{args:[FFIType.u32,FFIType.i32,FFIType.u32],returns:FFIType.ptr},K32GetProcessMemoryInfo:{args:[FFIType.ptr,FFIType.ptr,FFIType.u32],returns:FFIType.i32},CloseHandle:{args:[FFIType.ptr],returns:FFIType.i32}}),counters=new Uint8Array(MEMORY_COUNTERS_EX_BYTES),view=new DataView(counters.buffer);return pid=>{let handle=library.symbols.OpenProcess(PROCESS_QUERY_LIMITED_INFORMATION,0,pid);if(handle)try{return counters.fill(0),library.symbols.K32GetProcessMemoryInfo(handle,ptr(counters),counters.byteLength)===0?void 0:{bytes:Number(view.getBigUint64(PRIVATE_USAGE_OFFSET,!0)),measure:"private_usage"}}finally{library.symbols.CloseHandle(handle)}}}export{readOwnFootprint,readProcessFootprint,parseProcStatusRssAnon};