eve
Version:
Filesystem-first framework for durable backend AI agents that run anywhere.
1 lines • 1.38 kB
JavaScript
import{z}from"#compiled/zod/index.js";import{createEveCallbackRoutePath}from"#protocol/routes.js";import{isReservedIpAddress}from"#shared/network-address.js";const sessionCallbackSchema=z.object({callId:z.string().min(1),subagentName:z.string().min(1),token:z.string().min(1),url:z.string().min(1)}).strict().superRefine((e,t)=>{let r;try{r=new URL(e.url)}catch{t.addIssue({code:`custom`,message:`Callback url must be absolute.`,path:[`url`]});return}readCallbackUrlToken(r)!==e.token&&t.addIssue({code:`custom`,message:`Callback url token must match callback token.`,path:[`url`]}),isReservedIpAddress(r.hostname)&&t.addIssue({code:`custom`,message:`Callback url host must not be a private or reserved address.`,path:[`url`]})});function parseSessionCallback(e){let t=sessionCallbackSchema.safeParse(e);return t.success?{callback:t.data,ok:!0}:{cause:t.error,message:formatSessionCallbackParseError(t.error),ok:!1}}function readCallbackUrlToken(e){let n=createEveCallbackRoutePath(``);if(!e.pathname.startsWith(n))return null;let r=e.pathname.slice(n.length);if(r.length===0||r.includes(`/`))return null;try{return decodeURIComponent(r)}catch{return null}}function formatSessionCallbackParseError(e){return`Invalid callback metadata: ${e.issues.map(e=>`${e.path.length===0?`callback`:`callback.${e.path.join(`.`)}`}: ${e.message}`).join(`; `)}`}export{parseSessionCallback};