import{Ah as C,Gh as D,Hh as j,Mh as x,cP as E,eP as c,nO as M}from"./cli-0.5.4-k5fz4zd3.mjs";import"./cli-0.5.4-wbqf44ym.mjs";import"./cli-0.5.4-9ewg9gfs.mjs";import"./cli-0.5.4-we3mspgg.mjs";import"./cli-0.5.4-jzhazry0.mjs";import"./cli-0.5.4-82sw3hrw.mjs";import"./cli-0.5.4-31v143kt.mjs";import"./cli-0.5.4-1cy7wd6s.mjs";import"./cli-0.5.4-5e0pk96f.mjs";import"./cli-0.5.4-z154a7bp.mjs";import"./cli-0.5.4-ng5p0zjs.mjs";import"./cli-0.5.4-5hp7ks3t.mjs";import"./cli-0.5.4-8hdwp78f.mjs";import"./cli-0.5.4-mr9aznxx.mjs";import"./cli-0.5.4-jevfk5q7.mjs";import"./cli-0.5.4-27dzbzdy.mjs";import"./cli-0.5.4-a5bkdqqb.mjs";import"./cli-0.5.4-j2hnw539.mjs";import"./cli-0.5.4-9w59nnw4.mjs";import"./cli-0.5.4-z5d3q8kh.mjs";import"./cli-0.5.4-c0r12dqp.mjs";import"./cli-0.5.4-kqh172jk.mjs";import"./cli-0.5.4-vv4jbgkc.mjs";import"./cli-0.5.4-s7k1f0wc.mjs";import"./cli-0.5.4-0h0yz2k5.mjs";import"./cli-0.5.4-942dm5q1.mjs";import"./cli-0.5.4-rcmp5h4y.mjs";import"./cli-0.5.4-xdcww3x8.mjs";import"./cli-0.5.4-fq32ny1h.mjs";import"./cli-0.5.4-h1m67ngp.mjs";import"./cli-0.5.4-49erze3e.mjs";import"./cli-0.5.4-jytjks37.mjs";import"./cli-0.5.4-mn22gk7g.mjs";import"./cli-0.5.4-5t3jj85n.mjs";import"./cli-0.5.4-5shjmmkz.mjs";import"./cli-0.5.4-zb412kkm.mjs";import"./cli-0.5.4-prfn3sxd.mjs";import"./cli-0.5.4-6xmgebmk.mjs";import"./cli-0.5.4-vjgejcrz.mjs";import"./cli-0.5.4-jqt00ywx.mjs";import"./cli-0.5.4-9e7rkd8e.mjs";import"./cli-0.5.4-jp4wtw87.mjs";import"./cli-0.5.4-hambpq58.mjs";import"./cli-0.5.4-syvgmg9g.mjs";import"./cli-0.5.4-w8ttefr5.mjs";import"./cli-0.5.4-j7kkqxv6.mjs";import"./cli-0.5.4-qrwczx7j.mjs";import"./cli-0.5.4-v2z6p9td.mjs";import{X8 as U,X_ as w,_8 as N,f$ as S}from"./cli-0.5.4-cbq01zg3.mjs";import"./cli-0.5.4-37v5fhf1.mjs";import"./cli-0.5.4-7grzwf6q.mjs";import"./cli-0.5.4-9qmek5dq.mjs";import"./cli-0.5.4-9c6r1fjg.mjs";import"./cli-0.5.4-2dw33ms6.mjs";import"./cli-0.5.4-x1fzfxmm.mjs";import"./cli-0.5.4-e822fzaq.mjs";import{Nea as v,Pea as k}from"./cli-0.5.4-09dvrzcr.mjs";import{sfa as O,xfa as I}from"./cli-0.5.4-end8wj9r.mjs";import"./cli-0.5.4-4x206j04.mjs";import"./cli-0.5.4-yd3rd9qt.mjs";import"./cli-0.5.4-sgcaw17t.mjs";import{ima as b,ria as z,xia as F}from"./cli-0.5.4-jmc3bzbn.mjs";import"./cli-0.5.4-959vxhhb.mjs";import{Zma as T}from"./cli-0.5.4-aacw1src.mjs";import{randomUUID as p}from"crypto";import{mkdir as y,readFile as f,writeFile as u}from"fs/promises";function g(Q){let G=Q?.message?.content;if(!G)return"Branched conversation";let X=typeof G==="string"?G:G.find((Z)=>Z.type==="text")?.text;if(!X)return"Branched conversation";return X.replace(/\s+/g," ").trim().slice(0,100)||"Branched conversation"}async function d(Q){let G=p(),X=z(),Z=x(F()),Y=j(G),V=D();await y(Z,{recursive:!0,mode:448});let K;try{K=await f(V)}catch{throw Error("No conversation to branch")}if(K.length===0)throw Error("No conversation to branch");let _=U(K),W=_.filter((A)=>C(A)&&!A.isSidechain),H=_.filter((A)=>A.type==="content-replacement"&&A.sessionId===X).flatMap((A)=>A.replacements);if(W.length===0)throw Error("No messages to branch");let $=null,q=[],B=[];for(let A of W){let L={...A,sessionId:G,parentUuid:$,isSidechain:!1,forkedFrom:{sessionId:X,messageUuid:A.uuid}},J={...A,sessionId:G};if(B.push(J),q.push(O(L)),A.type!=="progress")$=A.uuid}if(H.length>0){let A={type:"content-replacement",sessionId:G,replacements:H};q.push(O(A))}return await u(Y,q.join(`
`)+`
`,{encoding:"utf8",mode:384}),{sessionId:G,title:Q,forkPath:Y,serializedMessages:B,contentReplacementRecords:H}}async function m(Q){let G=`${Q} (Branch)`;if((await E(G,{exact:!0})).length===0)return G;let Z=await E(`${Q} (Branch`),Y=new Set([1]),V=new RegExp(`^${w(Q)} \\(Branch(?: (\\d+))?\\)$`);for(let _ of Z){let W=_.customTitle?.match(V);if(W)if(W[1])Y.add(parseInt(W[1],10));else Y.add(1)}let K=2;while(Y.has(K))K++;return`${Q} (Branch ${K})`}async function AA(Q,G,X){let Z=X?.trim()||void 0,Y=z();try{let{sessionId:V,title:K,forkPath:_,serializedMessages:W,contentReplacementRecords:H}=await d(Z),$=new Date,q=g(W.find((P)=>P.type==="user")),A=await m(K??q);await M(V,A,_),v("tengu_conversation_forked",{message_count:W.length,has_custom_title:!!K});let L={date:$.toISOString().split("T")[0],messages:W,fullPath:_,value:$.getTime(),created:$,modified:$,firstPrompt:q,messageCount:W.length,isSidechain:!1,sessionId:V,customTitle:A,contentReplacements:H},J=K?` "${K}"`:"",R=`
To resume the original: claude -r ${Y}`,h=`Branched conversation${J}. You are now in the branch.${R}`;if(G.resume)await G.resume(V,L,"fork"),Q(h,{display:"system"});else Q(`Branched conversation${J}. Resume with: /resume ${V}`);return null}catch(V){let K=V instanceof Error?V.message:"Unknown error occurred";return Q(`Failed to branch conversation: ${K}`),null}}var l=T(()=>{b();k();N();c();I();S()});l();export{g as deriveFirstPrompt,AA as call};
