@code-yeongyu/senpi
Version:
Coding agent CLI with read, bash, edit, write tools and session management
3 lines (2 loc) • 2.62 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{execFile}from"node:child_process";import{readFile}from"node:fs/promises";import{uptime as osUptimeSeconds}from"node:os";import{workerData}from"node:worker_threads";var BOOT_INSTANT_TOLERANCE_MS=12e4,wholeSeconds=ms=>Math.round(ms/1e3)*1e3;async function readKernelBootEpochSeconds(platform){try{if(platform==="linux"){let match=/^btime (\d+)$/m.exec(await readFile("/proc/stat","utf8"));return match?Number(match[1]):void 0}if(platform==="darwin"){let raw=await new Promise((resolve,reject)=>{execFile("/usr/sbin/sysctl",["-n","kern.boottime"],{encoding:"utf8",timeout:2e3},(error,stdout)=>error?reject(error):resolve(stdout))}),match=/sec = (\d+)/.exec(raw);return match?Number(match[1]):void 0}}catch{}}var kernelBootRecords=new Map;function kernelBootEpochSeconds(platform){let known=kernelBootRecords.get(platform);if(known!==void 0)return known;let read=readKernelBootEpochSeconds(platform).then(seconds=>(seconds===void 0&&kernelBootRecords.delete(platform),seconds));return kernelBootRecords.set(platform,read),read}async function processBootAtMs(now=Date.now,platform=process.platform){let bootSeconds=await kernelBootEpochSeconds(platform);return bootSeconds!==void 0?bootSeconds*1e3:wholeSeconds(now()-osUptimeSeconds()*1e3)}var floorToSecond=ms=>Math.floor(ms/1e3)*1e3,hostIdentity=workerData,OWN_START_AT_LOAD=typeof hostIdentity=="object"&&hostIdentity!==null&&"hostProcessStartedAtMs"in hostIdentity&&typeof hostIdentity.hostProcessStartedAtMs=="number"&&Number.isFinite(hostIdentity.hostProcessStartedAtMs)?hostIdentity.hostProcessStartedAtMs:floorToSecond(Date.now()-process.uptime()*1e3);function ownProcessStartedAtMs(now){return now===void 0?OWN_START_AT_LOAD:floorToSecond(now()-process.uptime()*1e3)}function sameBoot(bootAtMs,otherBootAtMs){return otherBootAtMs===void 0||!Number.isFinite(otherBootAtMs)?!1:Math.abs(bootAtMs-otherBootAtMs)<=BOOT_INSTANT_TOLERANCE_MS}var ownBootAtMs=options=>(options?.fresh===!0&&kernelBootRecords.delete(process.platform),processBootAtMs());async function sameBootAsOwn(otherBootAtMs,read=ownBootAtMs){return otherBootAtMs===void 0||!Number.isFinite(otherBootAtMs)?!1:sameBoot(await read(),otherBootAtMs)?!0:sameBoot(await read({fresh:!0}),otherBootAtMs)}export{BOOT_INSTANT_TOLERANCE_MS,processBootAtMs,ownProcessStartedAtMs,sameBoot,ownBootAtMs,sameBootAsOwn};