eve
Version:
Filesystem-first framework for durable backend AI agents that run anywhere.
1 lines • 5.06 kB
JavaScript
import{expectObjectRecord,expectOnlyKnownKeys}from"#internal/authored-module.js";import{normalizeAuthorizationSpec}from"#runtime/connections/validate-authorization.js";const KNOWN_TOP_LEVEL_KEYS=[`approval`,`auth`,`description`,`headers`,`tools`,`url`],KNOWN_OPENAPI_TOP_LEVEL_KEYS=[`approval`,`auth`,`baseUrl`,`description`,`headers`,`operations`,`spec`],KNOWN_AUTHORIZATION_KEYS=[`completeAuthorization`,`getToken`,`principalType`,`startAuthorization`,`vercelConnect`];function normalizeMcpClientConnectionDefinition(n,i){let a=expectObjectRecord(n,i);expectOnlyKnownKeys(a,KNOWN_TOP_LEVEL_KEYS,i),validateUrl(a,i),validateDescription(a,i);let o=normalizeAuthorization(a,i),s=normalizeHeaders(a,i),c=normalizeToolFilter(a,i);if(o!==void 0&&s!==void 0&&typeof s!=`function`&&Object.keys(s).some(e=>e.toLowerCase()===`authorization`))throw Error(`${i} "headers" must not include an "Authorization" key when "auth" is also provided.`);let l={description:a.description,url:a.url};if(o!==void 0&&(l.auth=o),s!==void 0&&(l.headers=s),c!==void 0&&(l.tools=c),a.approval!==void 0){if(typeof a.approval!=`function`)throw Error(`${i} The "approval" field must be a function when provided.`);l.approval=a.approval}return l}function normalizeOpenApiConnectionDefinition(n,r){let a=expectObjectRecord(n,r);expectOnlyKnownKeys(a,KNOWN_OPENAPI_TOP_LEVEL_KEYS,r),validateSpec(a,r),validateBaseUrl(a,r),validateDescription(a,r);let o=normalizeAuthorization(a,r),s=normalizeHeaders(a,r),c=normalizeFilterField(a,`operations`,r);if(o!==void 0&&s!==void 0&&typeof s!=`function`&&Object.keys(s).some(e=>e.toLowerCase()===`authorization`))throw Error(`${r} "headers" must not include an "Authorization" key when "auth" is also provided.`);let l={description:a.description,spec:a.spec};if(a.baseUrl!==void 0&&(l.baseUrl=a.baseUrl),o!==void 0&&(l.auth=o),s!==void 0&&(l.headers=s),c!==void 0&&(l.operations=c),a.approval!==void 0){if(typeof a.approval!=`function`)throw Error(`${r} The "approval" field must be a function when provided.`);l.approval=a.approval}return l}function validateSpec(e,t){let n=e.spec;if(typeof n==`string`){if(!URL.canParse(n))throw Error(`${t} The "spec" field must be a valid URL when provided as a string.`);let e=new URL(n);if(e.protocol!==`https:`&&e.protocol!==`http:`)throw Error(`${t} The "spec" URL must use the http or https protocol, got "${e.protocol}".`);return}if(typeof n!=`object`||!n||Array.isArray(n))throw Error(`${t} The "spec" field must be a URL string or an inline OpenAPI document object.`)}function validateBaseUrl(e,t){if(e.baseUrl===void 0)return;if(typeof e.baseUrl!=`string`||!URL.canParse(e.baseUrl))throw Error(`${t} The "baseUrl" field must be a valid URL when provided.`);let n=new URL(e.baseUrl);if(n.protocol!==`https:`&&n.protocol!==`http:`)throw Error(`${t} The "baseUrl" field must use the http or https protocol, got "${n.protocol}".`)}function validateUrl(e,t){if(typeof e.url!=`string`||!URL.canParse(e.url))throw Error(`${t} The "url" field must be a valid URL.`);let n=new URL(e.url);if(n.protocol!==`https:`&&n.protocol!==`http:`)throw Error(`${t} The "url" field must use the http or https protocol, got "${n.protocol}".`)}function validateDescription(e,t){if(typeof e.description!=`string`||e.description.length===0)throw Error(`${t} The "description" field must be a non-empty string.`)}function normalizeAuthorization(r,i){if(r.auth===void 0)return;let o=expectObjectRecord(r.auth,`${i} The "auth" field must be an object with a "getToken" method.`);return expectOnlyKnownKeys(o,KNOWN_AUTHORIZATION_KEYS,`${i} The "auth" field`),normalizeAuthorizationSpec(o,i)}function normalizeHeaders(e,t){if(e.headers===void 0)return;if(typeof e.headers==`function`)return e.headers;if(typeof e.headers!=`object`||e.headers===null||Array.isArray(e.headers))throw Error(`${t} The "headers" field must be a plain object or a function.`);let n=e.headers;for(let[e,r]of Object.entries(n)){let n=typeof r;if(n!==`string`&&n!==`function`&&n!==`object`||n===`object`&&(r===null||typeof r.then!=`function`))throw Error(`${t} The "headers.${e}" value must be a string, Promise, or function.`)}return n}function normalizeToolFilter(e,t){return normalizeFilterField(e,`tools`,t)}function normalizeFilterField(e,t,n){let r=e[t];if(r===void 0)return;if(typeof r!=`object`||!r||Array.isArray(r))throw Error(`${n} The "${t}" field must specify either "allow" or "block".`);let i=r,a=`allow`in i,o=`block`in i;if(a&&o)throw Error(`${n} The "${t}" field must specify either "allow" or "block", not both.`);if(!a&&!o)throw Error(`${n} The "${t}" field must specify either "allow" or "block".`);return a?(validateStringArray(i.allow,`${n} The "${t}.allow"`),{allow:i.allow}):(validateStringArray(i.block,`${n} The "${t}.block"`),{block:i.block})}function validateStringArray(e,t){if(!Array.isArray(e))throw Error(`${t} field must be an array of strings.`);for(let n=0;n<e.length;n++)if(typeof e[n]!=`string`)throw Error(`${t}[${n}] must be a string.`)}export{normalizeMcpClientConnectionDefinition,normalizeOpenApiConnectionDefinition};