@whoz-oss/coday-server 0.103.2 → 0.103.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +1 -1
- package/server.js +1 -1
package/package.json
CHANGED
package/server.js
CHANGED
|
@@ -579,7 +579,7 @@ Verify the memory system's overall health:
|
|
|
579
579
|
- List any remaining issues or concerns
|
|
580
580
|
- Provide recommendations for maintaining memory quality
|
|
581
581
|
|
|
582
|
-
This ensures the memory system stays efficient and valuable.`],createdBy:"system",createdAt:new Date("2024-01-01").toISOString(),webhookEnabled:!1,parameterFormat:""}];var yu=class extends Error{constructor(t,n){super(t);this.cause=n;this.name="ThreadRepositoryError"}},N4={input:0,output:0,cache_read:0,cache_write:0,price:0,iterations:0,priceThreshold:
|
|
582
|
+
This ensures the memory system stays efficient and valuable.`],createdBy:"system",createdAt:new Date("2024-01-01").toISOString(),webhookEnabled:!1,parameterFormat:""}];var yu=class extends Error{constructor(t,n){super(t);this.cause=n;this.name="ThreadRepositoryError"}},N4={input:0,output:0,cache_read:0,cache_write:0,price:0,iterations:0,priceThreshold:10,iterationsThreshold:100};function rn(r,e){return r.users.some(t=>t.userId===e)}function Vte(r,e,t=.7){if(!e||!r.length)return{messages:r,overflow:[]};let n=0,i=0,a=e*t;for(let c of r)i+=c.length,n+=i<a?1:0;if(i<e)return{messages:r,overflow:[]};let o=r.slice(0,n),s=r.slice(n);return{messages:o,overflow:s}}var eIe=[Rt.type,ln.type,kn.type,da.type,Di.type,Ii.type,_r.type,fo.type],bu=class r{id;projectId;name;summary;starring=[];username;users=[];createdDate;modifiedDate;runStatus="STOPPED";data={};usage={...N4};price=0;delegationDepth=0;parentThreadId;parentEventId;delegatedAgentName;delegatedTask;parentThread;messages;get messagesLength(){return this.messages.length}constructor(e){this.id=e.id,this.username=e.username??"",this.projectId=e.projectId??"",this.name=e.name??"",this.summary=e.summary??"",this.starring=e.starring??[],this.users=e.users??(e.username?[{userId:e.username}]:[]),this.createdDate=e.createdDate??new Date().toISOString(),this.modifiedDate=e.modifiedDate??this.createdDate,this.price=e.price??0,this.parentThreadId=e.parentThreadId,this.parentEventId=e.parentEventId,this.delegatedAgentName=e.delegatedAgentName,this.delegatedTask=e.delegatedTask;let t=e.messages??[];Array.isArray(t)?this.messages=t.filter(n=>eIe.includes(n.type)).map(n=>cv(n)).filter(n=>n!==void 0):this.messages=[]}async getMessages(e,t){if((!this.messages||!Array.isArray(this.messages))&&(this.messages=[]),!e){let l=this.getMessagesUpToFirstSummary();return{messages:this.cleanToolRequestResponseConsistency(l),compacted:!1}}let n=this.getMessagesUpToFirstSummary(),i=Math.floor(e*.5),a=n.map(l=>{if(l instanceof kn&&l.length>i){let f=Math.floor(e*.15),p=l.getTextOutput(),d=`[... truncated ${l.length-f} chars]
|
|
583
583
|
`+p.slice(-f);return new kn({...l,output:d})}return l}),{messages:o,overflow:s}=Vte(a.toReversed(),e);if(o=o.toReversed(),s=s.toReversed(),!t||s.length===0)return{messages:this.cleanToolRequestResponseConsistency(o),compacted:s.length>0};let c;try{c=await t(s);let l=s[s.length-1];if(l){let f=this.messages.indexOf(l);f!==-1&&this.messages.splice(f+1,0,c)}}catch(l){return console.error("[AiThread] Compaction failed, keeping recent messages only:",l),{messages:this.cleanToolRequestResponseConsistency(o),compacted:!0}}let u=this.cleanToolRequestResponseConsistency(o);return{messages:[c,...u],compacted:!0}}getAllMessages(){return this.cleanToolRequestResponseConsistency([...this.messages])}getMessagesUpToFirstSummary(){let e=[...this.messages].reverse(),t=[];for(let n of e){if(n instanceof da){t.push(n);break}t.push(n)}return t.reverse()}cleanToolRequestResponseConsistency(e){let t=[],n=new Set,i=new Set;for(let a of e)a instanceof ln&&a.toolRequestId?n.add(a.toolRequestId):a instanceof kn&&a.toolRequestId&&i.add(a.toolRequestId);for(let a of e){let o=!0;a instanceof ln?a.toolRequestId&&!i.has(a.toolRequestId)&&(console.debug(`[AiThread] Removing orphaned tool request: ${a.name} (ID: ${a.toolRequestId})`),o=!1):a instanceof kn&&(a.toolRequestId?n.has(a.toolRequestId)||(console.debug(`[AiThread] Removing orphaned tool response for missing request ID: ${a.toolRequestId}`),o=!1):(console.warn(`[AiThread] Removing tool response without tool reference (timestamp: ${a.timestamp})`),o=!1)),o&&t.push(a)}return t}resetUsageForRun(){this.usage={...N4}}addUsage(e){this.price+=e.price??0,this.usage.price+=e.price??0,this.usage.iterations+=1;let t=this.usage;t.input+=e.input??0,t.output+=e.output??0,t.cache_read+=e.cache_read??0,t.cache_write+=e.cache_write??0}add(e){this.messages.push(e)}findToolRequestById(e){return this.messages.find(t=>t instanceof ln&&t.toolRequestId===e)}findSimilarToolRequests(e){return this.messages.filter(t=>t!==e&&t instanceof ln&&t.name===e.name&&t.args===e.args)}findToolResponsesToRequests(e){return this.messages.filter(t=>t instanceof kn&&e.some(n=>n.toolRequestId===t.toolRequestId))}removeMessages(e){this.messages=this.messages.filter(t=>!e.includes(t))}addUserMessage(e,t){let n=this.messages[this.messages.length-1];n&&n instanceof Rt&&n.role==="user"&&n.name===e?n.content.push(t):this.add(new Rt({role:"user",content:[t],name:e})),this.modifiedDate=new Date().toISOString()}addAnswerEvent(e){this.add(e),this.modifiedDate=new Date().toISOString()}addDelegationEvent(e){this.messages.some(n=>n instanceof fo&&n.subThreadId===e.subThreadId)||this.add(e)}addAgentMessage(e,t){let n=this.messages[this.messages.length-1];n&&n instanceof Rt&&n.role==="assistant"&&n.name===e?n.content.push(t):this.add(new Rt({role:"assistant",content:[t],name:e})),this.modifiedDate=new Date().toISOString()}fork(e,t,n){let i=new r({id:crypto.randomUUID(),username:this.username,projectId:this.projectId,name:e,summary:"",createdDate:new Date().toISOString(),modifiedDate:new Date().toISOString(),price:0,messages:[],users:this.users.map(a=>({...a})),parentThreadId:this.id,parentEventId:n,delegatedAgentName:e,delegatedTask:t});return i.delegationDepth=this.delegationDepth+1,i.parentThread=this,i.runStatus="RUNNING",i}merge(e){this.price+=e.price,e.price=0}get totalPrice(){return this.price+(this.parentThread?.totalPrice??0)}getLastAgentName(){for(let e=this.messages.length-1;e>=0;e--){let t=this.messages[e];if(t instanceof Rt&&t.role==="assistant"&&t.name)return t.name}}getUserMessageCount(){return this.messages.filter(e=>e instanceof Rt&&e.role==="user"||e instanceof _r).length}getEventById(e){return this.messages.find(t=>t.timestamp===e)}truncateAtUserMessage(e){let t=this.messages.findIndex(i=>i.timestamp===e);if(t===-1)return!1;let n=this.messages[t];return!(n instanceof Rt)||n.role!=="user"||t===0?!1:(this.messages=this.messages.slice(0,t),this.modifiedDate=new Date().toISOString(),!0)}addToolRequests(e,t){t.forEach(n=>{!n.toolRequestId||!n.name||!n.args||this.add(n)})}addToolResponseEvents(e){e.forEach(t=>{if(!t.toolRequestId||!t.output)return;let n=this.findToolRequestById(t.toolRequestId);if(!n)return;let i=this.findSimilarToolRequests(n),a=this.findToolResponsesToRequests(i);this.removeMessages([...i,...a]),this.add(t)})}addToolCalls(e,t){t.forEach(n=>{!n.id||!n.name||!n.args||this.add(new ln({name:n.name,args:n.args,toolRequestId:n.id}))})}addToolResponses(e,t){t.forEach(n=>{if(!n.id||!n.response)return;let i=this.findToolRequestById(n.id);if(!i)return;let a=this.findSimilarToolRequests(i),o=this.findToolResponsesToRequests(a);this.removeMessages([...a,...o]),this.add(new kn({toolRequestId:n.id,output:n.response}))})}serialize(){return{id:this.id,username:this.username,projectId:this.projectId,name:this.name,summary:this.summary,createdDate:this.createdDate,modifiedDate:this.modifiedDate,price:this.price,starring:this.starring,users:this.users,parentThreadId:this.parentThreadId,parentEventId:this.parentEventId,delegatedAgentName:this.delegatedAgentName,delegatedTask:this.delegatedTask,messages:this.messages}}};var Vie=vr(Go(),1);var k0=class{constructor(e,t){this.aiProviderConfig=e;this.logger=t;this.apiKey=e.apiKey}models=[];apiKey;killed=!1;thinkingInterval=1e3;charsPerToken=3;username;activeThinkingIntervals=new Set;activeDelays=new Set;isShuttingDown=!1;mergeModels(e){let t=new Map(e.map(n=>[n.alias??n.name,n]));this.aiProviderConfig.models?.forEach(n=>{let i=n.alias??n.name,a=t.get(i);a?t.set(i,{...a,...n,price:n.price??a.price}):t.set(i,n)}),this.models=Array.from(t.values())}kill(){this.killed=!0,this.cleanup()}cleanup(){this.isShuttingDown=!0;for(let e of this.activeDelays)clearTimeout(e);this.activeDelays.clear();for(let e of this.activeThinkingIntervals)clearInterval(e);this.activeThinkingIntervals.clear()}async delay(e,t){if(this.isShuttingDown)throw new Error("Client is shutting down");return new Promise((n,i)=>{let a=setTimeout(()=>{this.activeDelays.delete(a),n()},e);this.activeDelays.add(a),this.isShuttingDown&&(clearTimeout(a),this.activeDelays.delete(a),i(new Error(`Delay interrupted by shutdown${t?`: ${t}`:""}`)))})}startThinkingInterval(){let e=setInterval(()=>this.interactor.thinking(),this.thinkingInterval);return this.activeThinkingIntervals.add(e),e}stopThinkingInterval(e){clearInterval(e),this.activeThinkingIntervals.delete(e)}getCompactor(e,t){return async n=>{if(this.interactor.debug(`\u{1F5DC}\uFE0F Starting compaction for ${n.length} messages (budget: ${t} chars)`),t<500)return this.interactor.warn(`\u26A0\uFE0F Budget too small for compaction (${t} chars). Using truncation marker.`),new da({summary:"[Previous conversation truncated due to context window constraints]"});let i=n.filter(d=>d instanceof Rt).map(d=>` - ${d.role}: ${d.getTextContent()}`).join(`
|
|
584
584
|
`);this.interactor.debug(`\u{1F4DD} Built transcript from ${n.filter(d=>d instanceof Rt).length} messages (${i.length} chars)`);let a=Math.max(100,Math.floor(t/20)),o=150,s=.2,c=Math.max(0,Math.floor((t-o-a)*(1-s)));this.interactor.debug(`\u{1F4CA} Compaction budget: summary=${a} chars, max transcript=${c} chars (overhead=${o}, margin=${Math.round(s*100)}%)`);let u=i,l=!1;i.length>c&&(u="..."+i.slice(-(c-3)),l=!0,this.interactor.debug(`\u2702\uFE0F Transcript truncated: ${i.length} \u2192 ${u.length} chars (removed ${i.length-u.length} chars from beginning)`));let f=`Here is a transcript of a conversation:
|
|
585
585
|
<transcript>${u}</transcript>
|