MCPcopy Index your code
hub / github.com/ChinaSiro/claude-code-sourcemap / mb

Function mb

package/cli.js:7679–7679  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

7677`;if(z.length+O.length>=this.MAX_CHUNK_BYTES){await this.appendToFile(q,z);for(let w of Y)w();Y.length=0,z=""}z+=O,Y.push(A)}if(z.length>0){await this.appendToFile(q,z);for(let $ of Y)$()}}for(let[q,K]of this.writeQueues)if(K.length===0)this.writeQueues.delete(q)}resetSessionFile(){this.sessionFile=null,this.pendingEntries=[]}reAppendSessionMetadata(q=!1){if(!this.sessionFile)return;let K=N8();if(!K)return;let z=FRY(this.sessionFile).split(`
7678`);if(!q){let $=z.findLast((A)=>A.startsWith('{"type":"custom-title"'));if($){let A=UT($,"customTitle");if(A!==void 0)this.currentSessionTitle=A||void 0}}let Y=z.findLast(($)=>$.startsWith('{"type":"tag"'));if(Y){let $=UT(Y,"tag");if($!==void 0)this.currentSessionTag=$||void 0}if(this.currentSessionLastPrompt)jZ(this.sessionFile,{type:"last-prompt",lastPrompt:this.currentSessionLastPrompt,sessionId:K});if(this.currentSessionTitle)jZ(this.sessionFile,{type:"custom-title",customTitle:this.currentSessionTitle,sessionId:K});if(this.currentSessionTag)jZ(this.sessionFile,{type:"tag",tag:this.currentSessionTag,sessionId:K});if(this.currentSessionAgentName)jZ(this.sessionFile,{type:"agent-name",agentName:this.currentSessionAgentName,sessionId:K});if(this.currentSessionAgentColor)jZ(this.sessionFile,{type:"agent-color",agentColor:this.currentSessionAgentColor,sessionId:K});if(this.currentSessionAgentSetting)jZ(this.sessionFile,{type:"agent-setting",agentSetting:this.currentSessionAgentSetting,sessionId:K});if(this.currentSessionMode)jZ(this.sessionFile,{type:"mode",mode:this.currentSessionMode,sessionId:K});if(this.currentSessionWorktree!==void 0)jZ(this.sessionFile,{type:"worktree-state",worktreeSession:this.currentSessionWorktree,sessionId:K});if(this.currentSessionPrNumber!==void 0&&this.currentSessionPrUrl&&this.currentSessionPrRepository)jZ(this.sessionFile,{type:"pr-link",sessionId:K,prNumber:this.currentSessionPrNumber,prUrl:this.currentSessionPrUrl,prRepository:this.currentSessionPrRepository,timestamp:new Date().toISOString()})}async flush(){if(this.flushTimer)clearTimeout(this.flushTimer),this.flushTimer=null;if(this.activeDrain)await this.activeDrain;if(await this.drainWriteQueue(),this.pendingWriteCount===0)return;return new Promise((q)=>{this.flushResolvers.push(q)})}async removeMessageByUuid(q){return this.trackWrite(async()=>{if(this.sessionFile===null)return;try{let K=0,_=await fRY(this.sessionFile,"r+");try{let{size:$}=await _.stat();if(K=$,$===0)return;let A=Math.min($,ue),O=$-A,w=Buffer.allocUnsafe(A),{bytesRead:j}=await _.read(w,0,A,O),H=w.subarray(0,j),J=`"uuid":"${q}"`,M=H.lastIndexOf(J);if(M>=0){let X=H.lastIndexOf(10,M);if(X>=0||O===0){let P=X+1,W=H.indexOf(10,M+J.length),D=W>=0?W+1:j,f=O+P,G=j-D;if(await _.truncate(f),G>0)await _.write(H,D,G,f);return}}}finally{await _.close()}if(K>vRY){N(`Skipping tombstone removal: session file too large (${m4(K)})`,{level:"warn"});return}let Y=(await dS6(this.sessionFile,{encoding:"utf-8"})).split(`
7679`).filter(($)=>{if(!$.trim())return!0;try{return r8($).uuid!==q}catch{return!0}});await gS6(this.sessionFile,Y.join(`
7680`),{encoding:"utf8"})}catch{}})}shouldSkipPersistence(){let q=i6(process.env.TEST_ENABLE_SESSION_PERSISTENCE);return LBK()==="test"&&!q||Z7()?.cleanupPeriodDays===0||ey()||i6(process.env.CLAUDE_CODE_SKIP_PROMPT_HISTORY)}async materializeSessionFile(){if(this.shouldSkipPersistence())return;if(this.ensureCurrentSessionFile(),this.reAppendSessionMetadata(),this.pendingEntries.length>0){let q=this.pendingEntries;this.pendingEntries=[];for(let K of q)await this.appendEntry(K)}}async insertMessageChain(q,K=!1,_,z,Y){return this.trackWrite(async()=>{let $=z??null;if(this.sessionFile===null&&q.some((j)=>j.type==="user"||j.type==="assistant"))await this.materializeSessionFile();let A;try{A=await oM()}catch{A=void 0}let O=N8(),w=t36().get(O);for(let j of q){let H=bD(j),J=$;if(j.type==="user"&&"sourceToolAssistantUUID"in j&&j.sourceToolAssistantUUID)J=j.sourceToolAssistantUUID;let M={parentUuid:H?null:J,logicalParentUuid:H?$:void 0,isSidechain:K,teamName:Y?.teamName,agentName:Y?.agentName,promptId:j.type==="user"?Kx6()??void 0:void 0,agentId:_,...j,userType:HQ8(),entrypoint:yRY(),cwd:Z8(),sessionId:O,version:TRY,gitBranch:A,slug:w};if(await this.appendEntry(M),FS6(j))$=j.uuid}if(!K){let j=wq8(q);if(j){let H=j.replace(/\n/g," ").trim();this.currentSessionLastPrompt=H.length>200?H.slice(0,200).trim()+"…":H}}})}async insertFileHistorySnapshot(q,K,_){return this.trackWrite(async()=>{let z={type:"file-history-snapshot",messageId:q,snapshot:K,isSnapshotUpdate:_};await this.appendEntry(z)})}async insertQueueOperation(q){return this.trackWrite(async()=>{await this.appendEntry(q)})}async insertAttributionSnapshot(q){return this.trackWrite(async()=>{await this.appendEntry(q)})}async insertContentReplacement(q,K){return this.trackWrite(async()=>{let _={type:"content-replacement",sessionId:N8(),agentId:K,replacements:q};await this.appendEntry(_)})}async appendEntry(q,K=N8()){if(this.shouldSkipPersistence())return;let _=N8(),z=K===_,Y;if(z){if(this.sessionFile===null){this.pendingEntries.push(q);return}Y=this.sessionFile}else{let $=await this.getExistingSessionFile(K);if(!$){H6(Error(`appendEntry: session file not found for other session ${K}`));return}Y=$}if(q.type==="summary")this.enqueueWrite(Y,q);else if(q.type==="custom-title")this.enqueueWrite(Y,q);else if(q.type==="ai-title")this.enqueueWrite(Y,q);else if(q.type==="last-prompt")this.enqueueWrite(Y,q);else if(q.type==="task-summary")this.enqueueWrite(Y,q);else if(q.type==="tag")this.enqueueWrite(Y,q);else if(q.type==="agent-name")this.enqueueWrite(Y,q);else if(q.type==="agent-color")this.enqueueWrite(Y,q);else if(q.type==="agent-setting")this.enqueueWrite(Y,q);else if(q.type==="pr-link")this.enqueueWrite(Y,q);else if(q.type==="file-history-snapshot")this.enqueueWrite(Y,q);else if(q.type==="attribution-snapshot")this.enqueueWrite(Y,q);else if(q.type==="speculation-accept")this.enqueueWrite(Y,q);else if(q.type==="mode")this.enqueueWrite(Y,q);else if(q.type==="worktree-state")this.enqueueWrite(Y,q);else if(q.type==="content-replacement"){let $=q.agentId?c0(q.agentId):Y;this.enqueueWrite($,q)}else if(q.type==="marble-origami-commit")this.enqueueWrite(Y,q);else if(q.type==="marble-origami-snapshot")this.enqueueWrite(Y,q);else{let $=await US6(K);if(q.type==="queue-operation")this.enqueueWrite(Y,q);else{let A=q.isSidechain&&q.agentId!==void 0,O=A?c0(pO(q.agentId)):Y,w=!$.has(q.uuid);if(A||w){if(this.enqueueWrite(O,q),!A){if($.add(q.uuid),Oa(q))await this.persistToRemote(K,q)}}}}}ensureCurrentSessionFile(){if(this.sessionFile===null)this.sessionFile=Y$();return this.sessionFile}existingSessionFiles=new Map;async getExistingSessionFile(q){let K=this.existingSessionFiles.get(q);if(K)return K;let _=JT(q);try{return await C97(_),this.existingSessionFiles.set(q,_),_}catch(z){if(e3(z))return null;throw z}}async persistToRemote(q,K){if(gL8())return;if(this.internalEventWriter){try{await this.internalEventWriter("transcript",K,{...bD(K)&&{isCompaction:!0},...K.agentId&&{agentId:K.agentId}})}catch{d("tengu_session_persistence_failed",{}),N("Failed to write transcript as internal event")}return}if(!i6("true")||!this.remoteIngressUrl)return;if(!await W5K(q,K,this.remoteIngressUrl))d("tengu_session_persistence_failed",{}),eK(1,"other")}setRemoteIngressUrl(q){if(this.remoteIngressUrl=q,N(`Remote persistence enabled with URL: ${q}`),q)this.FLUSH_INTERVAL_MS=kBK}setInternalEventWriter(q){this.internalEventWriter=q,N("CCR v2 internal event writer registered for transcript persistence"),this.FLUSH_INTERVAL_MS=kBK}setInternalEventReader(q){this.internalEventReader=q,N("CCR v2 internal event reader registered for session resume")}setInternalSubagentEventReader(q){this.internalSubagentEventReader=q,N("CCR v2 subagent event reader registered for session resume")}getInternalEventReader(){return this.internalEventReader}getInternalSubagentEventReader(){return this.internalSubagentEventReader}}async function fN(q,K,_,z){let Y=Bq8(q,z),$=N8(),A=await US6($),O=[],w=_,j=!1;for(let J of Y)if(A.has(J.uuid)){if(!j&&FS6(J))w=J.uuid}else O.push(J),j=!0;if(O.length>0)await dY().insertMessageChain(O,!1,void 0,w,K);return O.findLast(FS6)?.uuid??w??null}async function SQ(q,K,_){await dY().insertMessageChain(Bq8(q),!0,K,_)}async function tI1(q){await dY().insertQueueOperation(q)}async function p97(q){await dY().removeMessageByUuid(q)}async function O68(q,K,_){await dY().insertFileHistorySnapshot(q,K,_)}async function SRY(q){await dY().insertAttributionSnapshot(q)}async function DH6(q,K){await dY().insertContentReplacement(q,K)}async function mb(){dY().resetSessionFile()}function vJ6(){let q=dY();q.sessionFile=Y$(),q.reAppendSessionMetadata(!0)}async function CRY(q){let K=N8();if(!K)return;await dY().appendEntry({type:"marble-origami-commit",sessionId:K,...q})}async function bRY(q){let K=N8();if(!K)return;await dY().appendEntry({type:"marble-origami-snapshot",sessionId:K,...q})}async function Hd(){await dY().flush()}async function B97(q,K){Of(EX(q));let _=dY();try{let z=await D5K(q,K)||[],Y=_2(r1());await BS6(Y,{recursive:!0,mode:448});let $=JT(q),A=z.map((O)=>g6(O)+`
7681`).join("");return await gS6($,A,{encoding:"utf8",mode:384}),N(`Hydrated ${z.length} entries from remote`),z.length>0}catch(z){return N(`Error hydrating session from remote: ${z}`),c8("error","hydrate_remote_session_fail"),!1}finally{_.setRemoteIngressUrl(K)}}async function g97(q){let K=Date.now();Of(EX(q));let _=dY(),z=_.getInternalEventReader();if(!z)return N("No internal event reader registered for CCR v2 resume"),!1;try{let Y=await z();if(!Y)return N("Failed to read internal events for resume"),c8("error","hydrate_ccr_v2_read_fail"),!1;let $=_2(r1());await BS6($,{recursive:!0,mode:448});let A=JT(q),O=Y.map((H)=>g6(H.payload)+`
7682`).join("");await gS6(A,O,{encoding:"utf8",mode:384}),N(`Hydrated ${Y.length} foreground entries from CCR v2 internal events`);let w=0,j=_.getInternalSubagentEventReader();if(j){let H=await j();if(H&&H.length>0){w=H.length;let J=new Map;for(let M of H){let X=M.agent_id||"";if(!X)continue;let P=J.get(X);if(!P)P=[],J.set(X,P);P.push(M.payload)}for(let[M,X]of J){let P=c0(pO(M));await BS6(Iq8(P),{recursive:!0,mode:448});let W=X.map((D)=>g6(D)+`

Callers 5

xq7Function · 0.85
g$7Function · 0.85
GA7Function · 0.85
M6Function · 0.85
OaYFunction · 0.85

Calls 2

dYFunction · 0.85
resetSessionFileMethod · 0.45

Tested by

no test coverage detected