UNPKG

@code-yeongyu/senpi

Version:

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

3 lines (2 loc) • 6.23 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{jsonNormalValue}from"./chunk-SF4MQDKZ.js";import{isBigIntObject,isBooleanObject,isNumberObject,isStringObject}from"node:util/types";function normalizeJsonValue(input){let ancestors=new Set,changed=!1,visit=(input2,key)=>{let value2=input2;if(typeof value2=="object"&&value2!==null||typeof value2=="function"||typeof value2=="bigint"){let toJSON=value2.toJSON;typeof toJSON=="function"&&(value2=toJSON.call(value2,key),changed=!0)}if(typeof value2=="object"&&value2!==null){if(isNumberObject(value2))value2=+value2,changed=!0;else if(isStringObject(value2))value2=String(value2),changed=!0;else if(isBooleanObject(value2))value2=Boolean.prototype.valueOf.call(value2),changed=!0;else if(isBigIntObject(value2))throw new TypeError("Do not know how to serialize a BigInt")}if(typeof value2=="bigint")throw new TypeError("Do not know how to serialize a BigInt");if(typeof value2=="number"){let normalized=Number.isFinite(value2)?Object.is(value2,-0)?0:value2:null;return Object.is(normalized,value2)||(changed=!0),normalized}if(typeof value2>"u"||typeof value2=="function"||typeof value2=="symbol"){changed=!0;return}if(typeof value2!="object"||value2===null)return value2;if(ancestors.has(value2))throw new TypeError("Converting circular structure to JSON");ancestors.add(value2);try{if("toJSON"in value2&&(changed=!0),Array.isArray(value2)){let array=[],length=value2.length;for(let index=0;index<length;index++){let descriptor=Object.getOwnPropertyDescriptor(value2,String(index));(!descriptor||descriptor.get)&&(changed=!0),array.push(visit(value2[index],String(index))??null)}return array}let result={},names=Object.keys(value2);(Object.getPrototypeOf(value2)!==Object.prototype||Reflect.ownKeys(value2).length!==names.length)&&(changed=!0);for(let name of names){Object.getOwnPropertyDescriptor(value2,name)?.get&&(changed=!0);let child=visit(value2[name],name);child!==void 0&&Object.defineProperty(result,name,{value:child,enumerable:!0,configurable:!0,writable:!0})}return result}finally{ancestors.delete(value2)}},value;try{value=visit(input,"")}catch(error){if(!(error instanceof RangeError))throw error;if(value=jsonNormalValue(input),value===void 0)throw new SyntaxError("JSON input has no representation");return{value,changed:!0}}if(value===void 0)throw new SyntaxError("JSON input has no representation");return{value,changed}}var MEDIA_BEARING_RECORD_TYPES=new Set(["tool_execution_end","message_start","message_end","turn_end","agent_end","entry_appended"]),MEDIA_BEARING_RESPONSE_COMMANDS=new Set(["get_messages","get_entries","get_tree","get_state","open_session","claim_session"]);function base64ByteLength(data){let padding=data.endsWith("==")?2:data.endsWith("=")?1:0;return Math.max(0,Math.floor(data.length*3/4)-padding)}function isPlainObject(value){return typeof value=="object"&&value!==null&&!Array.isArray(value)}function isInlineImage(value){return isPlainObject(value)&&value.type==="image"&&typeof value.data=="string"}function omitContentImages(content,toolCallId,persist){let replaced;for(let index=0;index<content.length;index++){let block=content[index];if(!isInlineImage(block))continue;replaced??=[...content];let ref={toolCallId,contentIndex:index};replaced[index]={type:"image_ref",mimeType:typeof block.mimeType=="string"?block.mimeType:"application/octet-stream",byteLength:base64ByteLength(block.data),ref,...persist?.(ref,block)}}return replaced??content}function omitToolResultImages(message,persist){let content=message.content,toolCallId=typeof message.toolCallId=="string"?message.toolCallId:"";if(!Array.isArray(content))return message;let omitted=omitContentImages(content,toolCallId,persist);return omitted===content?message:{...message,content:omitted}}function enter(value,key,persist){if(Array.isArray(value))return{key,value,index:0};if(!isPlainObject(value))return;let scrubbed=value.role==="toolResult"?omitToolResultImages(value,persist):value,entries=Object.entries(scrubbed).filter(([name,child])=>(name!=="content"||scrubbed===value)&&(isPlainObject(child)||Array.isArray(child)));return scrubbed===value?{key,value,scrubbed,entries,index:0}:{key,value,scrubbed,entries,index:0,replaced:{...scrubbed}}}function walk(root,persist){let first=enter(root,0,persist);if(!first)return root;let stack=[first],ancestors=new Set([first.value]);for(;;){let top=stack[stack.length-1];if("entries"in top?top.index<top.entries.length:top.index<top.value.length){let[key,child]="entries"in top?top.entries[top.index]:[top.index,top.value[top.index]];top.index++;let frame=enter(child,key,persist);if(!frame)continue;if(ancestors.has(frame.value))throw new TypeError("Converting circular structure to JSON");ancestors.add(frame.value),stack.push(frame);continue}stack.pop(),ancestors.delete(top.value);let result=top.replaced??top.value,parent=stack[stack.length-1];if(!parent)return result;result!==top.value&&("entries"in parent?(parent.replaced??={...parent.scrubbed})[top.key]=result:(parent.replaced??=[...parent.value])[top.key]=result)}}function omitToolExecutionEndImages(record,persist){let result=record.result;if(!isPlainObject(result)||!Array.isArray(result.content))return record;let toolCallId=typeof record.toolCallId=="string"?record.toolCallId:"",omitted=omitContentImages(result.content,toolCallId,persist);return omitted===result.content?record:{...record,result:{...result,content:omitted}}}function carriesMedia(record){return typeof record.type!="string"?!1:MEDIA_BEARING_RECORD_TYPES.has(record.type)?!0:record.type==="response"&&typeof record.command=="string"?MEDIA_BEARING_RESPONSE_COMMANDS.has(record.command):!1}function omitNormalInlineMedia(record,persist){let typed=record;if(!carriesMedia(typed))return record;let scrubbed=typed.type==="tool_execution_end"?omitToolExecutionEndImages(typed,persist):typed,walked=walk(scrubbed,persist);return walked===typed?record:walked}export{normalizeJsonValue,base64ByteLength,carriesMedia,omitNormalInlineMedia};