"use strict";exports.id=454,exports.ids=[454],exports.modules={20454:(a,b,c)=>{c.a(a,async(a,d)=>{try{c.d(b,{startPaperFeedScheduler:()=>i});var e=c(37422),f=c(48945),g=a([e,f]);[e,f]=g.then?(await g)():g;let j=Number(process.env.PAPER_FEEDS_TRIGGER_HOUR_UTC??"9"),k=globalThis;async function h(a=new Date){let b=await (0,f.Eu)();if(a.getUTCHours()<j)return{triggered:0,skipped:0,reaped:b};let c=function(a=new Date){return new Date(Date.UTC(a.getUTCFullYear(),a.getUTCMonth(),a.getUTCDate()-1)).toISOString().slice(0,10)}(a),d=await e.z.researchProject.findMany({where:{paperFeedEnabled:!0,paperFeedAgentUuid:{not:null}},select:{uuid:!0,companyUuid:!0}}),g=0,i=0;for(let a of d)(await (0,f.uy)({companyUuid:a.companyUuid,researchProjectUuid:a.uuid,triggeredBy:"cron",feedDate:c})).reused?i++:g++;return{triggered:g,skipped:i,reaped:b}}function i(){if("1"===process.env.PAPER_FEEDS_DISABLE_SCHEDULER||k.__paperFeedScheduler)return;let a=setInterval(()=>{h().catch(()=>{})},6e5);h().catch(()=>{}),"function"==typeof a.unref&&a.unref(),k.__paperFeedScheduler={intervalId:a}}d()}catch(a){d(a)}})},35401:(a,b,c)=>{},48945:(a,b,c)=>{c.a(a,async(a,d)=>{try{c.d(b,{Eu:()=>j,uy:()=>h});var e=c(37422);c(35401);var f=c(90580),g=a([e,f]);async function h(a){let b,c=await e.z.researchProject.findFirst({where:{uuid:a.researchProjectUuid,companyUuid:a.companyUuid},select:{uuid:!0,name:!0,paperFeedAgentUuid:!0}});if(!c)throw Error("Research Project not found");if(!c.paperFeedAgentUuid)throw Error("No paper feed agent configured");let d=c.paperFeedAgentUuid,g=new Date(`${a.feedDate}T00:00:00.000Z`),h=await e.z.paperFeedRun.findUnique({where:{researchProjectUuid_feedDate:{researchProjectUuid:a.researchProjectUuid,feedDate:g}}});if(h){if("completed"===h.status||"failed"!==h.status)return{runUuid:h.uuid,reused:!0};await e.z.paperFeedRun.update({where:{uuid:h.uuid},data:{status:"pending",errorMessage:null,startedAt:new Date,completedAt:null,triggeredBy:a.triggeredBy,paperCount:0}}),b=h.uuid}else b=(await e.z.paperFeedRun.create({data:{companyUuid:a.companyUuid,researchProjectUuid:a.researchProjectUuid,agentUuid:d,feedDate:g,status:"pending",triggeredBy:a.triggeredBy}})).uuid;return await e.z.researchProject.update({where:{uuid:a.researchProjectUuid},data:{paperFeedActiveAgentUuid:d,paperFeedStartedAt:new Date}}),await f.vt({companyUuid:a.companyUuid,researchProjectUuid:a.researchProjectUuid,recipientType:"agent",recipientUuid:d,entityType:"paper_feed_run",entityUuid:b,entityTitle:c.name,projectName:c.name,action:"paper_feed_triggered",message:a.feedDate,actorType:"user",actorUuid:"system",actorName:"cron"===a.triggeredBy?"Synapse cron":"User"}),{runUuid:b,reused:!1}}async function i(a){let b=await e.z.paperFeedRun.findUnique({where:{uuid:a.feedRunUuid},select:{uuid:!0,companyUuid:!0,researchProjectUuid:!0,agentUuid:!0,feedDate:!0}});if(!b)throw Error("Paper feed run not found");let c=await e.z.paperFeedItem.count({where:{feedRunUuid:b.uuid}});await e.z.paperFeedRun.update({where:{uuid:b.uuid},data:{status:a.status,completedAt:new Date,paperCount:c,errorMessage:"failed"===a.status?a.errorMessage??null:null}}),await e.z.researchProject.update({where:{uuid:b.researchProjectUuid},data:{paperFeedActiveAgentUuid:null,paperFeedStartedAt:null,paperFeedLastRunAt:new Date}});let d=await e.z.researchProject.findUnique({where:{uuid:b.researchProjectUuid},select:{name:!0}}),g=await e.z.agent.findUnique({where:{uuid:b.agentUuid},select:{ownerUuid:!0,name:!0}});if(g?.ownerUuid){let e=b.feedDate.toISOString().slice(0,10),h="completed"===a.status?`Paper Feed for ${e} ready: ${c} relevant papers.`:`Paper Feed for ${e} failed${a.errorMessage?`: ${a.errorMessage}`:"."}`;await f.vt({companyUuid:b.companyUuid,researchProjectUuid:b.researchProjectUuid,recipientType:"user",recipientUuid:g.ownerUuid,entityType:"paper_feed_run",entityUuid:b.uuid,entityTitle:d?.name??"",projectName:d?.name??"",action:"completed"===a.status?"paper_feed_completed":"paper_feed_failed",message:h,actorType:"agent",actorUuid:b.agentUuid,actorName:g.name??"Agent"})}}async function j(){let a=new Date(Date.now()-18e5),b=await e.z.paperFeedRun.findMany({where:{status:"running",startedAt:{lt:a}},select:{uuid:!0}});for(let a of b)await i({feedRunUuid:a.uuid,status:"failed",errorMessage:"timeout"});return b.length}[e,f]=g.then?(await g)():g,d()}catch(a){d(a)}})}};