import{n as e}from"./chunk-DR8-3Aex.js";import{n as t,t as n}from"./memoize-DNnuA2aU.js";import{W as r,cn as i}from"./state-sIHsFpDu.js";import{S as a,_ as o,g as s,nc as c,rc as l}from"./paths-Ci1iwL5Y.js";import{B as u,U as d,_ as ee,h as te,z as ne}from"./debug-otxXY3j-.js";import{n as re,r as f}from"./analytics-DqMQntaB.js";import{b as p,x as ie}from"./teammate-CbskW5G9.js";import{Ak as m,Bk as h,Bl as g,Bn as ae,Ck as oe,Dk as _,Ek as v,Eo as y,Fl as b,Hk as se,Hn as ce,Kc as le,Lk as ue,Mk as de,Ok as x,Pl as fe,Rk as S,Rl as C,Sk as w,Tk as T,Uk as E,Uo as D,Vn as O,Vo as k,Wk as A,aO as j,bk as M,bl as N,ck as P,dt as F,ft as I,iO as L,jk as R,kk as z,ml as B,pl as pe,qc as me,vk as V,vl as H,wk as he,yl as ge}from"./loadAgentsDir-C_wFhytF.js";import{Hn as _e,zn as U}from"./prompt-6ZKyqv6z.js";import{X as ve}from"./rcDebugLog-C42A0pX1.js";import{writeFile as W}from"fs/promises";function ye(){return o(`tengu_session_memory`,!1)}function G(){return s(`tengu_sm_config`,{})}function be(e,t){let n=0,r=t==null;for(let i of e){if(!r){i.uuid===t&&(r=!0);continue}if(i.type===`assistant`){let e=i.message.content;Array.isArray(e)&&(n+=p(e,e=>e.type===`tool_use`))}}return n}function K(e){let t=h(e);if(!v()){if(!oe(t))return!1;z()}let n=he(t),r=be(e,X)>=w(),i=k(e);if(n&&r||n&&!i){let t=e[e.length-1];return t?.uuid&&(X=t.uuid),!0}return!1}async function q(e){let t=te(),n=ae();await t.mkdir(n,{mode:448});let r=O();try{await W(r,``,{encoding:`utf-8`,mode:384,flag:`wx`}),await W(r,await N(),{encoding:`utf-8`,mode:384})}catch(e){if(u(e)!==`EEXIST`)throw e}e.readFileState.delete(r);let i=await le.call({file_path:r},e),a=``,o=i.data;return o.type===`text`&&(a=o.file.content),f(`tengu_session_memory_file_read`,{content_length:a.length}),{memoryPath:r,currentMemory:a}}function xe(){if(r())return;let e=B();process.env.USER_TYPE===`ant`&&f(`tengu_session_memory_init`,{auto_compact_enabled:e}),e&&j($)}async function Se(e,t){if(e.length===0)return{success:!1,error:`No messages to summarize`};x();try{let n=b(t),{memoryPath:r,currentMemory:i}=await q(n),a=await H(i,r),{tools:o,mainLoopModel:s}=t.options,[c,l,u]=await Promise.all([F(o,s),E(),se()]),d=ve(c);return await g({promptMessages:[y({content:a})],cacheSafeParams:{systemPrompt:d,userContext:l,systemContext:u,toolUseContext:n,forkContextMessages:e},canUseTool:J(r),querySource:`session_memory`,forkLabel:`session_memory_manual`,overrides:{readFileState:n.readFileState}}),f(`tengu_session_memory_manual_extraction`,{}),m(h(e)),Y(e),{success:!0,memoryPath:r}}catch(e){return{success:!1,error:ne(e)}}finally{_()}}function J(e){return async(t,n)=>{if(t.name===`Edit`&&typeof n==`object`&&n&&`file_path`in n){let t=n.file_path;if(typeof t==`string`&&t===e)return{behavior:`allow`,updatedInput:n}}return{behavior:`deny`,message:`only ${U} on ${e} is allowed`,decisionReason:{type:`other`,reason:`only ${U} on ${e} is allowed`}}}}function Y(e){if(!k(e)){let t=e[e.length-1];t?.uuid&&R(t.uuid)}}var X,Z,Q,$,Ce=e((()=>{n(),i(),I(),A(),_e(),me(),ie(),C(),ee(),L(),D(),ce(),c(),P(),S(),re(),pe(),ge(),T(),d(),a(),Z=t(()=>{let e=G();de({minimumMessageTokensToInit:e.minimumMessageTokensToInit&&e.minimumMessageTokensToInit>0?e.minimumMessageTokensToInit:V.minimumMessageTokensToInit,minimumTokensBetweenUpdate:e.minimumTokensBetweenUpdate&&e.minimumTokensBetweenUpdate>0?e.minimumTokensBetweenUpdate:V.minimumTokensBetweenUpdate,toolCallsBetweenUpdates:e.toolCallsBetweenUpdates&&e.toolCallsBetweenUpdates>0?e.toolCallsBetweenUpdates:V.toolCallsBetweenUpdates})}),Q=!1,$=l(async function(e){let{messages:t,toolUseContext:n,querySource:r}=e;if(r!==`repl_main_thread`)return;{let{isPoorModeActive:e}=await import(`./poorMode-DzEL0cu0.js`);if(e())return}if(!ye()){process.env.USER_TYPE===`ant`&&!Q&&(Q=!0,f(`tengu_session_memory_gate_disabled`,{}));return}if(Z(),!K(t))return;x();let i=b(n),{memoryPath:a,currentMemory:o}=await q(i);await g({promptMessages:[y({content:await H(o,a)})],cacheSafeParams:fe(e),canUseTool:J(a),querySource:`session_memory`,forkLabel:`session_memory`,overrides:{readFileState:i.readFileState}});let s=t[t.length-1],c=s?ue(s):void 0,l=M();f(`tengu_session_memory_extraction`,{input_tokens:c?.input_tokens,output_tokens:c?.output_tokens,cache_read_input_tokens:c?.cache_read_input_tokens??void 0,cache_creation_input_tokens:c?.cache_creation_input_tokens??void 0,config_min_message_tokens_to_init:l.minimumMessageTokensToInit,config_min_tokens_between_update:l.minimumTokensBetweenUpdate,config_tool_calls_between_updates:l.toolCallsBetweenUpdates}),m(h(t)),Y(t),_()})}));export{K as a,Se as i,xe as n,Ce as r,J as t};