@madh-io/alfred-ai 0.10.79 → 0.10.80
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/bundle/index.js +3 -1
- package/package.json +1 -1
package/bundle/index.js
CHANGED
|
@@ -940,7 +940,9 @@ Result: ${o.display??JSON.stringify(o.data)}`:`\u274C Background task failed: ${
|
|
|
940
940
|
Error: ${o.error}`;await a.sendMessage(e.chatId,c)}}catch(t){let s=t instanceof Error?t.message:String(t);this.taskRepo.updateStatus(e.id,"failed",void 0,s),this.logger.error({taskId:e.id,err:t},"Background task failed");let r=this.adapters.get(e.platform);r&&await r.sendMessage(e.chatId,`\u274C Background task failed: ${e.description}
|
|
941
941
|
|
|
942
942
|
Error: ${s}`)}}}});import Wp from"node:crypto";var Br,sa=T(()=>{"use strict";_s();Br=class{static{m(this,"ProactiveScheduler")}actionRepo;skillRegistry;skillSandbox;llm;adapters;users;logger;pipeline;formatter;conversationManager;tickTimer;tickIntervalMs=6e4;constructor(e,t,s,r,n,o,i,a,c,d){this.actionRepo=e,this.skillRegistry=t,this.skillSandbox=s,this.llm=r,this.adapters=n,this.users=o,this.logger=i,this.pipeline=a,this.formatter=c,this.conversationManager=d}start(){this.tickTimer=setInterval(()=>this.tick(),this.tickIntervalMs),this.logger.info("Proactive scheduler started")}stop(){this.tickTimer&&(clearInterval(this.tickTimer),this.tickTimer=void 0),this.logger.info("Proactive scheduler stopped")}async tick(){try{let e=this.actionRepo.getDue();for(let t of e)try{await this.executeAction(t)}catch(s){this.logger.error({err:s,actionId:t.id},"Failed to execute scheduled action")}}catch(e){this.logger.error({err:e},"Error during proactive scheduler tick")}}async executeAction(e){let t=new Date().toISOString();this.logger.info({actionId:e.id,name:e.name},"Executing scheduled action");let s,r="text";if(e.skillName&&this.skillRegistry.has(e.skillName)){let u=this.skillRegistry.get(e.skillName);try{let p;try{p=JSON.parse(e.skillInput)}catch{p={},this.logger.warn({actionId:e.id},"Invalid skillInput JSON, using empty input")}let{context:f}=Ze(this.users,{userId:e.userId,platform:e.platform,chatId:e.chatId,chatType:"dm"}),g=await this.skillSandbox.execute(u,p,f);if(g.success){let h=g.display??JSON.stringify(g.data);if(this.formatter){let y=this.formatter.format(h,e.platform);s=y.text,r=y.parseMode}else s=h}else s=`\u274C Scheduled action "${e.name}" failed: ${g.error}`}catch(p){let f=p instanceof Error?p.message:String(p);s=`\u274C Scheduled action "${e.name}" failed: ${f}`}}else if(e.promptTemplate&&this.pipeline)try{let u=`scheduled-${e.id}`,p=this.users.findById(e.userId),f=p?.platformUserId??e.userId,g={id:`scheduled-${Wp.randomUUID()}`,platform:e.platform,chatId:u,chatType:"dm",userId:f,userName:p?.username??f,text:e.promptTemplate+"\n\n[Format: Use only Markdown (**, *, ~~, `, ```). Do NOT use HTML tags like <b>, <i>, <code>. The system converts Markdown to platform-specific formatting automatically.]",timestamp:new Date,metadata:{scheduled:!0,skipHistory:!0,tier:"fast"}},h=await this.pipeline.process(g),y=this.formatter?this.formatter.format(h.text,e.platform):{text:h.text,parseMode:"text"};s=y.text,r=y.parseMode;let b=this.adapters.get(e.platform);if(b&&h.attachments)for(let k of h.attachments)try{let S=k.mimeType.startsWith("image/"),M=k.mimeType==="audio/ogg"||k.mimeType==="audio/opus";S?await b.sendPhoto(e.chatId,k.data,k.fileName):M?await b.sendVoice(e.chatId,k.data):await b.sendFile(e.chatId,k.data,k.fileName)}catch(S){this.logger.warn({err:S,fileName:k.fileName,actionId:e.id},"Failed to send scheduled action attachment")}}catch(u){let p=u instanceof Error?u.message:String(u);this.logger.error({actionId:e.id,err:u},"Pipeline execution failed for scheduled action"),s=`Scheduled action "${e.name}" failed: ${p}`}else if(e.promptTemplate)try{s=(await this.llm.complete({messages:[{role:"user",content:e.promptTemplate}],maxTokens:1024,tier:"fast"})).content}catch(u){let p=u instanceof Error?u.message:String(u);this.logger.error({actionId:e.id,err:u},"LLM call failed for scheduled action"),s=`Scheduled action "${e.name}" failed: ${p}`}else this.logger.warn({actionId:e.id,skillName:e.skillName},"Unknown skill for scheduled action"),s=`Scheduled action "${e.name}" failed: unknown skill "${e.skillName}"`;let n=s.trim(),o=(e.promptTemplate??"").toLowerCase(),i=/nichts|silent|no\s*output|don't\s*respond|do\s*not\s*respond/i.test(o),a=/offline|down|fehler|error|warn|critical|alert|fail|nicht\s+(erreichbar|verf[uü]gbar|gefunden|online)|ausgefallen|stopped|unreachable|unavailable|⚠|❌|🚨|🔴/i.test(n)&&!/keine\s+(probleme|fehler|auff[aä]lligkeiten)/i.test(n);if(!n||n.length<3||i&&!a)this.logger.info({actionId:e.id,name:e.name},"Scheduled action produced no actionable output \u2014 skipping notification");else{let u=this.adapters.get(e.platform);if(u)try{if(await u.sendMessage(e.chatId,s,{parseMode:r!=="text"?r:void 0}),e.promptTemplate&&this.conversationManager){let p=this.conversationManager.getOrCreateConversation(e.platform,e.chatId,e.userId),f=`[Automated Scheduled Alert: ${e.name}]
|
|
943
|
-
${s}`;this.conversationManager.addMessage(p.id,"assistant",f)}}catch(p){this.logger.error({err:p,actionId:e.id},"Failed to send scheduled action result")}}if(e.promptTemplate&&this.conversationManager)try{let u=`scheduled-${e.id}`,p=this.conversationManager.getOrCreateConversation(e.platform,u,e.userId);this.conversationManager.pruneMessages(p.id,20)}catch{}let d=this.calculateNextRun(e);d?this.actionRepo.updateLastRun(e.id,t,d):(this.actionRepo.updateLastRun(e.id,t,null),this.actionRepo.setEnabled(e.id,!1))}calculateNextRun(e){let t=new Date;switch(e.scheduleType){case"interval":{let s=parseInt(e.scheduleValue,10);return isNaN(s)||s<=0?null:new Date(t.getTime()+s*6e4).toISOString()}case"once":return null;case"cron":return this.getNextCronDate(e.scheduleValue,t)?.toISOString()??null;default:return null}}getNextCronDate(e,t){let s=e.trim().split(/\s+/);if(s.length!==5)return null;let r=new Date(t.getTime()+6e4);r.setSeconds(0,0);for(let n=0;n<1440;n++){if(this.matchesCron(s,r))return r;r.setTime(r.getTime()+6e4)}return null}matchesCron(e,t){let s=t.getMinutes(),r=t.getHours(),n=t.getDate(),o=t.getMonth()+1,i=t.getDay();return this.matchCronField(e[0],s)&&this.matchCronField(e[1],r)&&this.matchCronField(e[2],n)&&this.matchCronField(e[3],o)&&this.matchCronField(e[4],i)}matchCronField(e,t){if(e==="*")return!0;let s=/^\*\/(\d+)$/.exec(e);if(s){let n=parseInt(s[1],10);return t%n===0}let r=parseInt(e,10);return isNaN(r)?!1:t===r}}});function bd(l,e){let t=e.split("."),s=l;for(let r of t){if(s==null)return;if(typeof s=="object"){if(r==="length"&&Array.isArray(s)){s=s.length;continue}s=s[r]}else return}return s}function kd(l,e,t,s){let r=Gp(l);if(s===null)return{triggered:!1,displayValue:r};switch(e){case"lt":case"gt":case"lte":case"gte":{let n=Ts(l),o=Ts(t);return n===null||o===null?{triggered:!1,displayValue:r}:{triggered:e==="lt"?n<o:e==="gt"?n>o:e==="lte"?n<=o:n>=o,displayValue:r}}case"eq":return{triggered:String(l)===String(t),displayValue:r};case"neq":return{triggered:String(l)!==String(t),displayValue:r};case"contains":return{triggered:String(l).includes(String(t??"")),displayValue:r};case"not_contains":return{triggered:!String(l).includes(String(t??"")),displayValue:r};case"changed":return{triggered:JSON.stringify(l)!==JSON.stringify(s),displayValue:r};case"increased":{let n=Ts(l),o=Ts(s);return n===null||o===null?{triggered:!1,displayValue:r}:{triggered:n>o,displayValue:r}}case"decreased":{let n=Ts(l),o=Ts(s);return n===null||o===null?{triggered:!1,displayValue:r}:{triggered:n<o,displayValue:r}}default:return{triggered:!1,displayValue:r}}}function Ts(l){if(typeof l=="number")return l;let e=parseFloat(String(l));return isNaN(e)?null:e}function Gp(l){return l==null?"null":typeof l=="object"?JSON.stringify(l):String(l)}var $d=T(()=>{"use strict";m(bd,"extractField");m(kd,"evaluateCondition");m(Ts,"toNumber");m(Gp,"formatValue")});var Vp,Wn,Sd=T(()=>{"use strict";$d();_s();Vp={lt:"<",gt:">",lte:"<=",gte:">=",eq:"=",neq:"!=",contains:"contains",not_contains:"not contains",changed:"changed",increased:"increased",decreased:"decreased"},Wn=class{static{m(this,"WatchEngine")}watchRepo;skillRegistry;skillSandbox;adapters;users;logger;timer=null;tickIntervalMs=6e4;constructor(e,t,s,r,n,o){this.watchRepo=e,this.skillRegistry=t,this.skillSandbox=s,this.adapters=r,this.users=n,this.logger=o}start(){this.timer=setInterval(()=>this.tick(),this.tickIntervalMs),this.logger.info("Watch engine started")}stop(){this.timer&&(clearInterval(this.timer),this.timer=null),this.logger.info("Watch engine stopped")}async tick(){try{let e=this.watchRepo.getDue();for(let t of e)try{await this.checkWatch(t)}catch(s){this.logger.error({err:s,watchId:t.id},"Failed to check watch")}}catch(e){this.logger.error({err:e},"Error during watch engine tick")}}async checkWatch(e){let t=new Date().toISOString();this.logger.debug({watchId:e.id,name:e.name,skill:e.skillName},"Checking watch");let s=this.skillRegistry.get(e.skillName);if(!s){this.logger.warn({watchId:e.id,skillName:e.skillName},"Unknown skill for watch"),this.watchRepo.updateAfterCheck(e.id,{lastCheckedAt:t,lastValue:e.lastValue});return}let{context:r}=Ze(this.users,{platformUserId:e.chatId,platform:e.platform,chatId:e.chatId,chatType:"dm"}),n=await this.skillSandbox.execute(s,e.skillParams,r);if(!n.success){this.logger.warn({watchId:e.id,error:n.error},"Watch skill execution failed"),this.watchRepo.updateAfterCheck(e.id,{lastCheckedAt:t,lastValue:e.lastValue});return}let o=bd(n.data,e.condition.field),i=e.lastValue!==null?JSON.parse(e.lastValue):null,{triggered:a,displayValue:c}=kd(o,e.condition.operator,e.condition.value,i),d=JSON.stringify(o);if(a&&this.isCooldownExpired(e)){let u=e.messageTemplate??this.formatAlert(e,c,n.data)
|
|
943
|
+
${s}`;this.conversationManager.addMessage(p.id,"assistant",f)}}catch(p){this.logger.error({err:p,actionId:e.id},"Failed to send scheduled action result")}}if(e.promptTemplate&&this.conversationManager)try{let u=`scheduled-${e.id}`,p=this.conversationManager.getOrCreateConversation(e.platform,u,e.userId);this.conversationManager.pruneMessages(p.id,20)}catch{}let d=this.calculateNextRun(e);d?this.actionRepo.updateLastRun(e.id,t,d):(this.actionRepo.updateLastRun(e.id,t,null),this.actionRepo.setEnabled(e.id,!1))}calculateNextRun(e){let t=new Date;switch(e.scheduleType){case"interval":{let s=parseInt(e.scheduleValue,10);return isNaN(s)||s<=0?null:new Date(t.getTime()+s*6e4).toISOString()}case"once":return null;case"cron":return this.getNextCronDate(e.scheduleValue,t)?.toISOString()??null;default:return null}}getNextCronDate(e,t){let s=e.trim().split(/\s+/);if(s.length!==5)return null;let r=new Date(t.getTime()+6e4);r.setSeconds(0,0);for(let n=0;n<1440;n++){if(this.matchesCron(s,r))return r;r.setTime(r.getTime()+6e4)}return null}matchesCron(e,t){let s=t.getMinutes(),r=t.getHours(),n=t.getDate(),o=t.getMonth()+1,i=t.getDay();return this.matchCronField(e[0],s)&&this.matchCronField(e[1],r)&&this.matchCronField(e[2],n)&&this.matchCronField(e[3],o)&&this.matchCronField(e[4],i)}matchCronField(e,t){if(e==="*")return!0;let s=/^\*\/(\d+)$/.exec(e);if(s){let n=parseInt(s[1],10);return t%n===0}let r=parseInt(e,10);return isNaN(r)?!1:t===r}}});function bd(l,e){let t=e.split("."),s=l;for(let r of t){if(s==null)return;if(typeof s=="object"){if(r==="length"&&Array.isArray(s)){s=s.length;continue}s=s[r]}else return}return s}function kd(l,e,t,s){let r=Gp(l);if(s===null)return{triggered:!1,displayValue:r};switch(e){case"lt":case"gt":case"lte":case"gte":{let n=Ts(l),o=Ts(t);return n===null||o===null?{triggered:!1,displayValue:r}:{triggered:e==="lt"?n<o:e==="gt"?n>o:e==="lte"?n<=o:n>=o,displayValue:r}}case"eq":return{triggered:String(l)===String(t),displayValue:r};case"neq":return{triggered:String(l)!==String(t),displayValue:r};case"contains":return{triggered:String(l).includes(String(t??"")),displayValue:r};case"not_contains":return{triggered:!String(l).includes(String(t??"")),displayValue:r};case"changed":return{triggered:JSON.stringify(l)!==JSON.stringify(s),displayValue:r};case"increased":{let n=Ts(l),o=Ts(s);return n===null||o===null?{triggered:!1,displayValue:r}:{triggered:n>o,displayValue:r}}case"decreased":{let n=Ts(l),o=Ts(s);return n===null||o===null?{triggered:!1,displayValue:r}:{triggered:n<o,displayValue:r}}default:return{triggered:!1,displayValue:r}}}function Ts(l){if(typeof l=="number")return l;let e=parseFloat(String(l));return isNaN(e)?null:e}function Gp(l){return l==null?"null":typeof l=="object"?JSON.stringify(l):String(l)}var $d=T(()=>{"use strict";m(bd,"extractField");m(kd,"evaluateCondition");m(Ts,"toNumber");m(Gp,"formatValue")});var Vp,Wn,Sd=T(()=>{"use strict";$d();_s();Vp={lt:"<",gt:">",lte:"<=",gte:">=",eq:"=",neq:"!=",contains:"contains",not_contains:"not contains",changed:"changed",increased:"increased",decreased:"decreased"},Wn=class{static{m(this,"WatchEngine")}watchRepo;skillRegistry;skillSandbox;adapters;users;logger;timer=null;tickIntervalMs=6e4;constructor(e,t,s,r,n,o){this.watchRepo=e,this.skillRegistry=t,this.skillSandbox=s,this.adapters=r,this.users=n,this.logger=o}start(){this.timer=setInterval(()=>this.tick(),this.tickIntervalMs),this.logger.info("Watch engine started")}stop(){this.timer&&(clearInterval(this.timer),this.timer=null),this.logger.info("Watch engine stopped")}async tick(){try{let e=this.watchRepo.getDue();for(let t of e)try{await this.checkWatch(t)}catch(s){this.logger.error({err:s,watchId:t.id},"Failed to check watch")}}catch(e){this.logger.error({err:e},"Error during watch engine tick")}}async checkWatch(e){let t=new Date().toISOString();this.logger.debug({watchId:e.id,name:e.name,skill:e.skillName},"Checking watch");let s=this.skillRegistry.get(e.skillName);if(!s){this.logger.warn({watchId:e.id,skillName:e.skillName},"Unknown skill for watch"),this.watchRepo.updateAfterCheck(e.id,{lastCheckedAt:t,lastValue:e.lastValue});return}let{context:r}=Ze(this.users,{platformUserId:e.chatId,platform:e.platform,chatId:e.chatId,chatType:"dm"}),n=await this.skillSandbox.execute(s,e.skillParams,r);if(!n.success){this.logger.warn({watchId:e.id,error:n.error},"Watch skill execution failed"),this.watchRepo.updateAfterCheck(e.id,{lastCheckedAt:t,lastValue:e.lastValue});return}let o=bd(n.data,e.condition.field),i=e.lastValue!==null?JSON.parse(e.lastValue):null,{triggered:a,displayValue:c}=kd(o,e.condition.operator,e.condition.value,i),d=JSON.stringify(o);if(a&&this.isCooldownExpired(e)){let u=e.messageTemplate??this.formatAlert(e,c,n.data);if(e.messageTemplate&&n.data&&typeof n.data=="object"){let f=this.formatResultContext(n.data,e.condition.field);f&&(u+=`
|
|
944
|
+
|
|
945
|
+
`+f)}let p=this.adapters.get(e.platform);if(p)try{await p.sendMessage(e.chatId,u),this.logger.info({watchId:e.id,name:e.name,value:c},"Watch alert sent")}catch(f){this.logger.error({err:f,watchId:e.id},"Failed to send watch alert")}this.watchRepo.updateAfterCheck(e.id,{lastCheckedAt:t,lastValue:d,lastTriggeredAt:t})}else this.watchRepo.updateAfterCheck(e.id,{lastCheckedAt:t,lastValue:d})}isCooldownExpired(e){if(!e.lastTriggeredAt)return!0;let t=e.cooldownMinutes*6e4;return Date.now()-new Date(e.lastTriggeredAt).getTime()>=t}formatAlert(e,t,s){let r=Vp[e.condition.operator]??e.condition.operator,n=e.condition.value!=null?` ${r} ${e.condition.value}`:` ${r}`,o=[`\u26A1 Watch Alert: ${e.name}`,`Bedingung erf\xFCllt: ${e.condition.field}${n}`,`Aktueller Wert: ${t}`];if(s&&typeof s=="object"){let i=s,a=this.formatResultContext(i,e.condition.field);a&&o.push("",a)}return o.join(`
|
|
944
946
|
`)}formatResultContext(e,t){if(Array.isArray(e.listings)&&e.listings.length>0){let n=e.listings.filter(i=>typeof i.price=="number").sort((i,a)=>i.price-a.price).slice(0,3);if(n.length===0)return null;let o=[`G\xFCnstigste ${n.length}:`];for(let i of n){let a=String(i.title??"").slice(0,60),c=typeof i.price=="number"?`${i.price}\xA0\u20AC`:"k.A.",d=i.location?` \u2014 ${i.location}`:"",u=i.url?`
|
|
945
947
|
${i.url}`:"";o.push(`\u2022 ${a} \u2014 ${c}${d}${u}`)}return typeof e.count=="number"&&o.push(`
|
|
946
948
|
Insgesamt: ${e.count} Inserate`),o.join(`
|