@code-yeongyu/senpi
Version:
Coding agent CLI with read, bash, edit, write tools and session management
3 lines (2 loc) • 4.05 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{ChildProcess}from"node:child_process";var KEEP_POWER_THROTTLING_ENV="SENPI_KEEP_POWER_THROTTLING",PROCESS_POWER_THROTTLING_CLASS=4,PROCESS_POWER_THROTTLING_CURRENT_VERSION=1,PROCESS_POWER_THROTTLING_EXECUTION_SPEED=1,POWER_THROTTLING_STATE_BYTES=12,PROCESS_SET_INFORMATION=512;function executionSpeedOptOutState(){let info=new Uint8Array(POWER_THROTTLING_STATE_BYTES),view=new DataView(info.buffer);return view.setUint32(0,PROCESS_POWER_THROTTLING_CURRENT_VERSION,!0),view.setUint32(4,PROCESS_POWER_THROTTLING_EXECUTION_SPEED,!0),view.setUint32(8,0,!0),info}function disableExecutionSpeedThrottling(kernel,pid){let handle=kernel.OpenProcess(PROCESS_SET_INFORMATION,0,pid);if(!handle)return{ok:!1,step:"OpenProcess",win32Error:kernel.GetLastError()};try{let info=executionSpeedOptOutState();return kernel.SetProcessInformation(handle,PROCESS_POWER_THROTTLING_CLASS,info,info.byteLength)===0?{ok:!1,step:"SetProcessInformation",win32Error:kernel.GetLastError()}:{ok:!0}}finally{kernel.CloseHandle(handle)}}function loadKernel32PowerThrottling(platform=process.platform){if(platform!=="win32")return;let ffi=process.getBuiltinModule("bun:ffi");if(ffi===void 0)return;let lib;try{lib=ffi.dlopen("kernel32.dll",{OpenProcess:{args:["u32","i32","u32"],returns:"ptr"},SetProcessInformation:{args:["ptr","i32","ptr","u32"],returns:"i32"},GetProcessInformation:{args:["ptr","i32","ptr","u32"],returns:"i32"},CloseHandle:{args:["ptr"],returns:"i32"},GetLastError:{args:[],returns:"u32"}})}catch{return}let call=(name,...args)=>{let fn=lib.symbols[name];if(fn===void 0)throw new Error(`kernel32 has no ${name}`);return fn(...args.map(arg=>arg instanceof Uint8Array?ffi.ptr(arg):arg))};return{OpenProcess:(access,inherit,pid)=>call("OpenProcess",access,inherit,pid),SetProcessInformation:(process2,infoClass,info,length)=>Number(call("SetProcessInformation",process2,infoClass,info,length)),GetProcessInformation:(process2,infoClass,info,length)=>Number(call("GetProcessInformation",process2,infoClass,info,length)),CloseHandle:handle=>Number(call("CloseHandle",handle)),GetLastError:()=>Number(call("GetLastError"))}}function defaultSurfaces(){let bun=globalThis.Bun;return{childProcessPrototype:ChildProcess.prototype,...bun===void 0?{}:{bun}}}function installWindowsPowerThrottlingOptOut(options={}){let platform=options.platform??process.platform;if(platform!=="win32")return{status:"skipped",reason:"not-windows"};if((options.env??process.env)[KEEP_POWER_THROTTLING_ENV]==="1")return{status:"skipped",reason:"opted-out"};let kernel=(options.load??loadKernel32PowerThrottling)(platform);if(kernel===void 0)return{status:"skipped",reason:"no-ffi"};let self=disableExecutionSpeedThrottling(kernel,options.pid??process.pid),surfaces=options.surfaces??defaultSurfaces(),optOut=pid=>{if(!(typeof pid!="number"||pid<=0))try{disableExecutionSpeedThrottling(kernel,pid)}catch{}},proto=surfaces.childProcessPrototype,originalSpawn=proto.spawn;proto.spawn=function(...args){let result=originalSpawn.apply(this,args);return optOut(this.pid),result};let bun=surfaces.bun,originalBunSpawn=bun?.spawn;return bun!==void 0&&originalBunSpawn!==void 0&&(bun.spawn=function(...args){let subprocess=originalBunSpawn.apply(this,args);return optOut(subprocess.pid),subprocess}),{status:"installed",self,optOutChild:optOut,restore:()=>{proto.spawn=originalSpawn,bun!==void 0&&originalBunSpawn!==void 0&&(bun.spawn=originalBunSpawn)}}}var installed;function ensureWindowsPowerThrottlingOptOut(){return installed??=installWindowsPowerThrottlingOptOut(),installed}function optOutChildPowerThrottling(pid){let install=ensureWindowsPowerThrottlingOptOut();install.status==="installed"&&install.optOutChild(pid)}export{ensureWindowsPowerThrottlingOptOut,optOutChildPowerThrottling};