@code-yeongyu/senpi
Version:
Coding agent CLI with read, bash, edit, write tools and session management
3 lines (2 loc) • 3.33 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 = () => {}; } }
var CHATGPT_SUBSCRIPTION_AUTH_CLAIM_PATH="https://api.openai.com/auth";function extractChatGptSubscriptionAccountId(token){return claimString(authClaims(token),"chatgpt_account_id")}function extractChatGptSubscriptionIdentity(token){let auth=authClaims(token),workspace=claimString(auth,"chatgpt_account_id"),user=claimString(auth,"chatgpt_user_id")??claimString(auth,"user_id");return claimString(auth,"chatgpt_account_user_id")??(user!==void 0&&workspace!==void 0?`${user}/${workspace}`:void 0)}function authClaims(token){try{let parts=token.split("."),encodedPayload=parts.length===3?parts[1]:void 0;if(!encodedPayload)return;let base64=encodedPayload.replaceAll("-","+").replaceAll("_","/"),payload=JSON.parse(atob(base64.padEnd(Math.ceil(base64.length/4)*4,"=")));if(!payload||typeof payload!="object")return;let auth=Reflect.get(payload,CHATGPT_SUBSCRIPTION_AUTH_CLAIM_PATH);return auth&&typeof auth=="object"?auth:void 0}catch{return}}function claimString(claims,key){let value=claims===void 0?void 0:Reflect.get(claims,key);return typeof value=="string"&&value.length>0?value:void 0}var MAX_SEQUENCE=(1n<<41n)-1n,lastOrdinaryTimestamp=-1,sequence;function fillRandomBytes(bytes){let crypto=globalThis.crypto;if(crypto?.getRandomValues){crypto.getRandomValues(bytes);return}for(let i=0;i<bytes.length;i++)bytes[i]=Math.floor(Math.random()*256)}function uuidv7(timestampMs){let requestedTimestamp=timestampMs??Date.now();if(!Number.isInteger(requestedTimestamp)||requestedTimestamp<0||requestedTimestamp>0xffffffffffff)throw new RangeError(`UUIDv7 timestamp must be an integer between 0 and ${0xffffffffffff}`);let effectiveTimestamp=timestampMs===void 0?Math.max(requestedTimestamp,lastOrdinaryTimestamp):timestampMs;timestampMs===void 0&&(lastOrdinaryTimestamp=effectiveTimestamp);let bytes=new Uint8Array(16);if(fillRandomBytes(bytes),sequence===void 0)sequence=BigInt(bytes[1])<<32n|BigInt(bytes[2])<<24n|BigInt(bytes[3])<<16n|BigInt(bytes[4])<<8n|BigInt(bytes[5]);else{if(sequence===MAX_SEQUENCE)throw new RangeError("UUIDv7 generator sequence exhausted");sequence++}let timestamp=BigInt(effectiveTimestamp);for(let index=5;index>=0;index--)bytes[index]=Number(timestamp>>BigInt((5-index)*8))&255;return bytes[6]=112|Number(sequence>>37n&0x0fn),bytes[7]=Number(sequence>>29n&0xffn),bytes[8]=128|Number(sequence>>23n&0x3fn),bytes[9]=Number(sequence>>15n&0xffn),bytes[10]=Number(sequence>>7n&0xffn),bytes[11]=Number((sequence&0x7fn)<<1n)|bytes[11]&1,formatUuid(bytes)}function formatUuid(bytes){let hex=Array.from(bytes,byte=>byte.toString(16).padStart(2,"0"));return`${hex.slice(0,4).join("")}-${hex.slice(4,6).join("")}-${hex.slice(6,8).join("")}-${hex.slice(8,10).join("")}-${hex.slice(10).join("")}`}var DEFAULT_WIRE_IDENTITY="senpi",wireIdentity=DEFAULT_WIRE_IDENTITY;function setWireIdentity(identity){identity?.trim()&&(wireIdentity=identity.trim())}function getWireIdentity(){return wireIdentity}export{extractChatGptSubscriptionAccountId,extractChatGptSubscriptionIdentity,uuidv7,setWireIdentity,getWireIdentity};