UNPKG

eve

Version:

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

6 lines • 8.91 kB
import{A as e,Ft as t,R as n,j as r}from"./dist-DYDVZ-FS.js";import i from"crypto";import{lookup as a}from"dns/promises";import{request as o}from"https";import{BlockList as s,isIP as c}from"net";import{pipeline as l}from"stream";import{createBrotliDecompress as u,createGunzip as d,createInflate as f}from"zlib";function ee(e){return n(e)?e:typeof e==`object`&&e&&`card`in e?e.card:null}function te(e){return typeof e==`object`&&e&&`files`in e?e.files??[]:[]}function ne(e){return typeof e==`object`&&e&&`attachments`in e?e.attachments??[]:[]}var p=class extends Error{adapter;code;constructor(e,t,n){super(e),this.name=`AdapterError`,this.adapter=t,this.code=n}},m=class extends p{retryAfter;constructor(e,t){super(`Rate limited by ${e}${t?`, retry after ${t}s`:``}`,e,`RATE_LIMITED`),this.name=`AdapterRateLimitError`,this.retryAfter=t}},h=class extends p{constructor(e,t){super(t||`Authentication failed for ${e}`,e,`AUTH_FAILED`),this.name=`AuthenticationError`}},g=class extends p{constructor(e,t){super(t,e,`VALIDATION_ERROR`),this.name=`ValidationError`}},_=class extends p{originalError;constructor(e,t,n){super(t||`Network error communicating with ${e}`,e,`NETWORK_ERROR`),this.name=`NetworkError`,this.originalError=n}};async function v(e,t){let{platform:n,throwOnUnsupported:r=!0}=t;if(Buffer.isBuffer(e))return e;if(e instanceof ArrayBuffer)return Buffer.from(e);if(e instanceof Blob){let t=await e.arrayBuffer();return Buffer.from(t)}if(r)throw new g(n,`Unsupported file data type`);return null}var y={slack:{primary:`primary`,danger:`danger`},gchat:{primary:`primary`,danger:`danger`},teams:{primary:`positive`,danger:`destructive`},discord:{primary:`primary`,danger:`danger`}};function b(e){return t=>r(t,e)}function x(e,t){if(e)return y[t][e]}function re(e,t={}){let{boldFormat:n=`*`,lineBreak:r=` `,platform:i}=t,a=i?b(i):e=>e,o=[];e.title&&o.push(`${n}${a(e.title)}${n}`),e.subtitle&&o.push(a(e.subtitle));for(let t of e.children){let e=S(t,a);e&&o.push(e)}return o.join(r)}function S(n,r){switch(n.type){case`text`:return r(n.content);case`link`:return`${r(n.label)} (${n.url})`;case`fields`:return n.children.map(e=>`${r(e.label)}: ${r(e.value)}`).join(` `);case`actions`:return null;case`section`:return n.children.map(e=>S(e,r)).filter(Boolean).join(` `);case`table`:return t(n.headers,n.rows);case`divider`:return`---`;default:return e(n)}}var C=`aes-256-gcm`,w=12,T=16,ie=/^[0-9a-fA-F]{64}$/;function ae(e,t){let n=i.randomBytes(w),r=i.createCipheriv(C,t,n,{authTagLength:T}),a=Buffer.concat([r.update(e,`utf8`),r.final()]),o=r.getAuthTag();return{iv:n.toString(`base64`),data:a.toString(`base64`),tag:o.toString(`base64`)}}function oe(e,t){let n=Buffer.from(e.iv,`base64`),r=Buffer.from(e.data,`base64`),a=Buffer.from(e.tag,`base64`),o=i.createDecipheriv(C,t,n,{authTagLength:T});return o.setAuthTag(a),Buffer.concat([o.update(r),o.final()]).toString(`utf8`)}function se(e){if(!e||typeof e!=`object`)return!1;let t=e;return typeof t.iv==`string`&&typeof t.data==`string`&&typeof t.tag==`string`}function ce(e){let t=e.trim(),n=ie.test(t),r=Buffer.from(t,n?`hex`:`base64`);if(r.length!==32)throw Error(`Encryption key must decode to exactly 32 bytes (received ${r.length}). Use a 64-char hex string or 44-char base64 string.`);return r}var E=26214400,le=5,D=3e4,O=new Set([301,302,303,307,308]),k=/^\[|\]$/g,A=[[`0.0.0.0`,8],[`10.0.0.0`,8],[`100.64.0.0`,10],[`127.0.0.0`,8],[`169.254.0.0`,16],[`172.16.0.0`,12],[`192.0.0.0`,24],[`192.0.2.0`,24],[`192.88.99.0`,24],[`192.168.0.0`,16],[`198.18.0.0`,15],[`198.51.100.0`,24],[`203.0.113.0`,24],[`224.0.0.0`,4],[`240.0.0.0`,4]],j=[[`::`,3],[`2001::`,23],[`2001:db8::`,32],[`2002::`,16],[`3fff::`,20],[`4000::`,2],[`8000::`,1]],M={br:u,deflate:f,gzip:d,"x-gzip":d};function N(e,t){let n=new s;for(let[r,i]of e)n.addSubnet(r,i,t);return n}var P=N(A,`ipv4`),F=N(j,`ipv6`);function I(e,t){return t!==4&&t!==6?!0:t===6?F.check(e,`ipv6`):P.check(e,`ipv4`)}function L(e){return new _(e,`Refusing to fetch an internal attachment URL`)}function R(e,t=a){return(n,r,i)=>{t(n,{...r,all:!0}).then(t=>{if(t.length===0){i(new _(e,`Could not resolve the attachment host`),``,0);return}if(t.some(({address:e,family:t})=>I(e,t))){i(L(e),``,0);return}if(r.all){i(null,t);return}let[n]=t;i(null,n.address,n.family)},e=>{i(e instanceof Error?e:Error(String(e)),``,0)})}}function z(e,t,n){let r=e instanceof URL?e:new URL(e),i=r.hostname.replace(k,``),a=c(i);if(a&&I(i,a))throw L(t);if(r.protocol!==`https:`||n&&!n.some(e=>{let t=e.toLowerCase();return i===t||i.endsWith(`.${t}`)}))throw new _(t,`Refusing to fetch an untrusted attachment URL`);return r}function B(e){let t=R(e);return(e,n,r)=>new Promise((i,a)=>{let s=o(e,{agent:!1,headers:r,lookup:t,signal:n},i);s.on(`error`,a),s.end()})}async function V(e,t,n=E,r){if(r?.aborted)throw e.destroy(),new _(t,`Timed out fetching the attachment`);let i=()=>e.destroy(ue(r));r?.addEventListener(`abort`,i,{once:!0});try{return await U(e,t,n)}finally{r?.removeEventListener(`abort`,i)}}function ue(e){let t=e?.reason;return t instanceof Error?t:Error(String(t))}function H(e,t,n){return new Promise((r,i)=>{let a=()=>i(new _(n,`Timed out fetching the attachment`));t.aborted?a():t.addEventListener(`abort`,a,{once:!0}),e.then(e=>{t.removeEventListener(`abort`,a),t.aborted&&e.destroy(),r(e)},e=>{t.removeEventListener(`abort`,a),i(e)})})}async function U(e,t,n){let r=e.headers[`content-encoding`]?.trim().toLowerCase()||`identity`,i=r===`identity`?void 0:M[r];if(r!==`identity`&&!i)throw e.destroy(),new _(t,`Unsupported attachment encoding: ${r}`);let a=i?NaN:Number(e.headers[`content-length`]);if(Number.isFinite(a)&&a>n)throw e.destroy(),new _(t,`Attachment exceeds the download limit`);let o=i?l(e,i(),()=>void 0):e;if(Number.isFinite(a)){let n=Buffer.allocUnsafe(a),r=0;for await(let i of o){let o=Buffer.isBuffer(i)?i:Buffer.from(i);if(r+o.length>a)throw e.destroy(),new _(t,`Attachment body exceeds its declared length`);o.copy(n,r),r+=o.length}return r===a?n:n.subarray(0,r)}let s=[],c=0;for await(let r of o){let i=Buffer.isBuffer(r)?r:Buffer.from(r);if(c+=i.length,c>n)throw e.destroy(),new _(t,`Attachment exceeds the download limit`);s.push(i)}return Buffer.concat(s,c)}async function de(e,t){let{adapter:n,headers:r,hosts:i,limit:a=E,onResponse:o,redirects:s=le,timeoutMs:c=D,transport:l}=t,u=l??B(n),d=AbortSignal.timeout(c),f=z(e,n,i);try{for(let e=0;e<=s;e+=1){let t=await H(u(f,d,{"accept-encoding":`gzip, deflate, br`,"user-agent":`Vercel.ChatSDK`,...typeof r==`function`?r(f):r}),d,n),c=t.statusCode??0;if(O.has(c)){let r=t.headers.location;if(t.destroy(),!r)throw new _(n,`Attachment redirect has no location`);if(e===s)throw new _(n,`Too many attachment redirects`);f=z(new URL(r,f),n,i);continue}if(c<200||c>=300)throw t.destroy(),new _(n,`Failed to fetch file: ${c} ${t.statusMessage??``}`.trim());if(o)try{o(t)}catch(e){throw t.destroy(),e}return await V(t,n,a,d)}throw new _(n,`Too many attachment redirects`)}catch(e){throw d.aborted&&!(e instanceof _)?new _(n,`Timed out fetching the attachment`,e instanceof Error?e:void 0):e}}var W=`http://`,G=`https://`;function K(e){let t=e.charCodeAt(0);return t>=65&&t<=90||t>=97&&t<=122}function q(e){let t=e.charCodeAt(0);return t>=48&&t<=57}function J(e){return e!==void 0&&(K(e)||q(e)||e===`_`)}function Y(e){return K(e)||q(e)||e===`.`||e===`-`}function X(e){return e===`<`||e===`>`||e.trim()===``}function Z(e,t,n){return e.slice(t,t+n.length).toLowerCase()===n}function fe(e,t,n){let r=0;if(Z(e,t,G)?r=G.length:Z(e,t,W)&&(r=W.length),r===0||t+r>=n)return t;let i=t+r;for(;i<n&&!X(e[i]);)i+=1;return i}function pe(e,t,n){if(!(K(e[t])||q(e[t]))||t>0&&Y(e[t-1]))return t;let r=t;for(;r<n&&Y(e[r]);)r+=1;let i=e[r];if(i!==`/`&&i!==`?`&&i!==`#`)return t;let a=e.slice(t,r),o=a.lastIndexOf(`.`),s=a.slice(o+1);if(o<=0||s.length<2)return t;for(let e of s)if(!K(e))return t;for(r+=1;r<n&&!X(e[r]);)r+=1;return r}function Q(e,t,n){if(e[t]!=="`")return t;let r=e.startsWith("```",t),i=r?"```":"`",a=t+i.length,o=e.indexOf(i,a);if(o===-1||o>=n)return t;if(!r){let n=e.indexOf(` `,a);if(n!==-1&&n<o)return t}return o+i.length}function $(e,t,n,r,i){let a=``,o=t;for(;o<n;){let t=Q(e,o,n);if(t>o){a+=e.slice(o,t),o=t;continue}if(i&&e[o]===`<`){let t=o+1;for(;t<n&&e[t]!==`>`&&e[t]!==` `&&e[t]!==`\r`;)t+=1;if(e[t]===`>`){a+=e.slice(o,t+1),o=t+1;continue}a+=$(e,o,t,r,!1),o=t;continue}let s=fe(e,o,n);if(s>o){a+=e.slice(o,s),o=s;continue}let c=pe(e,o,n);if(c>o){a+=e.slice(o,c),o=c;continue}if(e[o]===`@`&&e[o-1]!==`<`&&!J(e[o-1])&&J(e[o+1])){let t=o+2;for(;t<n&&J(e[t]);)t+=1;let i=e.slice(o,t);a+=r(i,i.slice(1)),o=t;continue}a+=e[o],o+=1}return a}function me(e,t){return $(e,0,e.length,t,!0)}function he(e,t=` `){let n=``,r=0,i=e.length;for(;r<i;){let a=Q(e,r,i);if(a>r){n+=t,r=a;continue}n+=e[r],r+=1}return n}export{me as _,re as a,oe as c,ee as d,te as f,he as g,x as h,g as i,de as l,se as m,h as n,b as o,ne as p,_ as r,ce as s,m as t,ae as u,v};