UNPKG

@code-yeongyu/senpi

Version:

Coding agent CLI with read, bash, edit, write tools and session management

6 lines (5 loc) • 5.18 kB
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{StringDecoder}from"node:string_decoder";import{isBigIntObject,isBooleanObject,isNumberObject,isStringObject}from"node:util/types";var LegacyRecordDepthError=class extends Error{constructor(record,cause){let type=typeof record=="object"&&record!==null?Reflect.get(record,"type"):void 0,recordType=typeof type=="string"?type:"record";super(`${recordType} nests deeper than this runtime's JSON engine limit`,{cause}),this.name="LegacyRecordDepthError",this.recordType=recordType}};function isStackOverflow(cause){return cause instanceof RangeError&&cause.message.startsWith("Maximum call stack size exceeded")}var OMITTED=Symbol("omitted");function jsonStep(input,key){let value=input;if(typeof value=="object"&&value!==null||typeof value=="function"||typeof value=="bigint"){let toJSON=value.toJSON;typeof toJSON=="function"&&(value=toJSON.call(value,key))}if(typeof value=="object"&&value!==null){if(isNumberObject(value))value=+value;else if(isStringObject(value))value=String(value);else if(isBooleanObject(value))value=Boolean.prototype.valueOf.call(value);else if(isBigIntObject(value))throw new TypeError("Do not know how to serialize a BigInt")}if(typeof value=="bigint")throw new TypeError("Do not know how to serialize a BigInt");return typeof value=="number"?Number.isFinite(value)?Object.is(value,-0)?0:value:null:typeof value>"u"||typeof value=="function"||typeof value=="symbol"?OMITTED:value}function jsonNormalValue(input){let ancestors=new Set,stack=[],enter=(input2,key)=>{let value=jsonStep(input2,key);if(typeof value!="object"||value===null)return value;if(ancestors.has(value))throw new TypeError("Converting circular structure to JSON");if(ancestors.add(value),Array.isArray(value)){let out2=[];return stack.push({source:value,out:out2,keys:void 0,length:value.length,index:0}),out2}let keys=Object.keys(value),out={};return stack.push({source:value,out,keys,length:keys.length,index:0}),out},root=enter(input,"");for(;stack.length>0;){let frame=stack[stack.length-1];if(frame.index===frame.length){ancestors.delete(frame.source),stack.pop();continue}let index=frame.index++;if(frame.keys===void 0){let child2=enter(frame.source[index],String(index));frame.out.push(child2===OMITTED?null:child2);continue}let name=frame.keys[index],child=enter(frame.source[name],name);child!==OMITTED&&Object.defineProperty(frame.out,name,{value:child,enumerable:!0,configurable:!0,writable:!0})}return root===OMITTED?void 0:root}function normalJsonText(root){let parts=[],stack=[],open=value=>{typeof value!="object"||value===null?parts.push(JSON.stringify(value)):Array.isArray(value)?(parts.push("["),stack.push({value,index:0})):(parts.push("{"),stack.push({value,keys:Object.keys(value),index:0}))};for(open(root);stack.length>0;){let frame=stack[stack.length-1],{keys}=frame,length=keys===void 0?frame.value.length:keys.length;if(frame.index===length){parts.push(keys===void 0?"]":"}"),stack.pop();continue}let index=frame.index++;if(index>0&&parts.push(","),keys===void 0)open(frame.value[index]);else{let name=keys[index];parts.push(JSON.stringify(name),":"),open(frame.value[name])}}return parts.join("")}function legacyJsonText(value){try{return JSON.stringify(value)}catch(cause){if(!isStackOverflow(cause))throw cause;let normal=jsonNormalValue(value);return normal===void 0?void 0:normalJsonText(normal)}}function serializeJsonLine(value){return`${legacyJsonText(value)} `}var MAX_RPC_LINE_CHARACTERS=16*1024*1024;function attachJsonlLineReader(stream,onLine,options={}){let decoder=new StringDecoder("utf8"),maxLineLength=options.maxLineLength??Number.POSITIVE_INFINITY;if(!(maxLineLength>0))throw new Error("maxLineLength must be greater than zero.");let buffer="",discardingOversizedLine=!1,consume=text=>{let offset=0;for(;offset<text.length;){if(discardingOversizedLine){let newlineIndex2=text.indexOf(` `,offset);if(newlineIndex2===-1)return;discardingOversizedLine=!1,offset=newlineIndex2+1;continue}let newlineIndex=text.indexOf(` `,offset);if(newlineIndex===-1){let remainder=text.slice(offset);buffer.length+remainder.length>maxLineLength?(buffer="",discardingOversizedLine=!0,options.onOversizedLine?.()):buffer+=remainder;return}let segment=text.slice(offset,newlineIndex);if(buffer.length+segment.length>maxLineLength)options.onOversizedLine?.();else{let line=buffer+segment;onLine(line.endsWith("\r")?line.slice(0,-1):line)}buffer="",offset=newlineIndex+1}},onData=chunk=>{consume(typeof chunk=="string"?chunk:decoder.write(chunk))},onEnd=()=>{consume(decoder.end()),!discardingOversizedLine&&buffer.length>0&&onLine(buffer.endsWith("\r")?buffer.slice(0,-1):buffer),buffer="",discardingOversizedLine=!1};return stream.on("data",onData),stream.on("end",onEnd),()=>{stream.off("data",onData),stream.off("end",onEnd)}}export{LegacyRecordDepthError,isStackOverflow,jsonNormalValue,serializeJsonLine,MAX_RPC_LINE_CHARACTERS,attachJsonlLineReader};