UNPKG

eve

Version:

Filesystem-first framework for durable backend AI agents that run anywhere.

1 lines • 1.77 kB
import{vercelOidc}from"#public/channels/auth.js";import{chatSdkChannel}from"#public/channels/chat-sdk/index.js";import{createMemoryState}from"#compiled/@chat-adapter/state-memory/index.js";import{createiMessageAdapter}from"#compiled/@photon-ai/chat-adapter-imessage/index.js";import{photonInboundContent}from"#public/channels/photon/inboundContent.js";function photonIMessageChannel(e){let t=e.webhookSecret??process.env.IMESSAGE_WEBHOOK_SECRET,n=createiMessageAdapter({credentials:e.credentials,...e.webhookVerifier?{webhookVerifier:e.webhookVerifier}:t?{webhookSecret:t}:{webhookVerifier:vercelOidc()}}),r=chatSdkChannel({adapters:{imessage:n},concurrency:`concurrent`,events:e.events,routes:{imessage:e.route??`/eve/v1/photon`},state:createMemoryState(),streaming:!1,turnPolicy:e.turnPolicy,userName:e.userName??`eve`}),i=e.onMessage??defaultOnMessage;return r.bot.onDirectMessage(async(e,t)=>{await dispatchMessage(r,i,e,t)}),r.bot.onNewMessage(/[\s\S]*/,async(e,t)=>{await dispatchMessage(r,i,e,t)}),r.channel}function defaultPhotonAuth(e){let t={};return e.author.userName!==void 0&&(t.user_name=e.author.userName),{attributes:t,authenticator:`photon-imessage`,issuer:`photon`,principalId:`photon:${e.author.userId}`,principalType:e.author.isBot?`service`:`user`,subject:e.author.userId}}async function defaultOnMessage(e,t){return{auth:defaultPhotonAuth(t)}}async function dispatchMessage(e,t,n,r){let i=await t({thread:n},r);if(i===null)return;await markReadBestEffort(e.bot.getAdapter(`imessage`),n,r);let a=photonInboundContent(r);a!==void 0&&await e.send({context:[...i.context??[]],message:a},{auth:i.auth,thread:n,title:i.title})}async function markReadBestEffort(e,t,n){try{await e.markRead(t.id,n.id)}catch{}}export{defaultPhotonAuth,photonIMessageChannel};