MCPcopy
hub / github.com/agent0ai/agent-zero / match

Method match

webui/js/transformers@3.0.2.js:217–217  ·  view source on GitHub ↗
(e)

Source from the content-addressed store, hash-verified

215/*!**************************!*\
216 !*** ./src/utils/hub.js ***!
217 \**************************/(e,t,n)=>{n.r(t),n.d(t,{getFile:()=>d,getModelFile:()=>h,getModelJSON:()=>m});var r=n(/*! fs */"?7a2c"),a=n(/*! path */"?a42a"),s=n(/*! ../env.js */"./src/env.js"),i=n(/*! ./core.js */"./src/utils/core.js");const o={txt:"text/plain",html:"text/html",css:"text/css",js:"text/javascript",json:"application/json",png:"image/png",jpg:"image/jpeg",jpeg:"image/jpeg",gif:"image/gif"};class l{constructor(e){if(this.filePath=e,this.headers=new Headers,this.exists=r.existsSync(e),this.exists){this.status=200,this.statusText="OK";let t=r.statSync(e);this.headers.set("content-length",t.size.toString()),this.updateContentType();let n=this;this.body=new ReadableStream({start(e){n.arrayBuffer().then((t=>{e.enqueue(new Uint8Array(t)),e.close()}))}})}else this.status=404,this.statusText="Not Found",this.body=null}updateContentType(){const e=this.filePath.toString().split(".").pop().toLowerCase();this.headers.set("content-type",o[e]??"application/octet-stream")}clone(){let e=new l(this.filePath);return e.exists=this.exists,e.status=this.status,e.statusText=this.statusText,e.headers=new Headers(this.headers),e}async arrayBuffer(){return(await r.promises.readFile(this.filePath)).buffer}async blob(){const e=await r.promises.readFile(this.filePath);return new Blob([e],{type:this.headers.get("content-type")})}async text(){return await r.promises.readFile(this.filePath,"utf8")}async json(){return JSON.parse(await this.text())}}function u(e,t=null,n=null){let r;try{r=new URL(e)}catch(e){return!1}return!(t&&!t.includes(r.protocol))&&!(n&&!n.includes(r.hostname))}async function d(e){if(s.env.useFS&&!u(e,["http:","https:","blob:"]))return new l(e);if("undefined"!=typeof process&&"node"===process?.release?.name){const t=!!process.env?.TESTING_REMOTELY,n=s.env.version,r=new Headers;r.set("User-Agent",`transformers.js/${n}; is_ci/${t};`);if(u(e,["http:","https:"],["huggingface.co","hf.co"])){const e=process.env?.HF_TOKEN??process.env?.HF_ACCESS_TOKEN;e&&r.set("Authorization",`Bearer ${e}`)}return fetch(e,{headers:r})}return fetch(e)}const c={400:"Bad request error occurred while trying to load file",401:"Unauthorized access to file",403:"Forbidden access to file",404:"Could not locate file",408:"Request timeout error occurred while trying to load file",500:"Internal server error error occurred while trying to load file",502:"Bad gateway error occurred while trying to load file",503:"Service unavailable error occurred while trying to load file",504:"Gateway timeout error occurred while trying to load file"};class p{constructor(e){this.path=e}async match(e){let t=a.join(this.path,e),n=new l(t);return n.exists?n:void 0}async put(e,t){const n=Buffer.from(await t.arrayBuffer());let s=a.join(this.path,e);try{await r.promises.mkdir(a.dirname(s),{recursive:!0}),await r.promises.writeFile(s,n)}catch(e){console.warn("An error occurred while writing the file to cache:",e)}}}async function h(e,t,n=!0,r={}){if(!s.env.allowLocalModels){if(r.local_files_only)throw Error("Invalid configuration detected: local models are disabled (`env.allowLocalModels=false`) but you have requested to only use local models (`local_files_only=true`).");if(!s.env.allowRemoteModels)throw Error("Invalid configuration detected: both local and remote models are disabled. Fix by setting `env.allowLocalModels` or `env.allowRemoteModels` to `true`.")}let a;if((0,i.dispatchCallback)(r.progress_callback,{status:"initiate",name:e,file:t}),!a&&s.env.useBrowserCache){if("undefined"==typeof caches)throw Error("Browser cache is not available in this environment.");try{a=await caches.open("transformers-cache")}catch(e){console.warn("An error occurred while opening the browser cache:",e)}}if(!a&&s.env.useFSCache&&(a=new p(r.cache_dir??s.env.cacheDir)),!a&&s.env.useCustomCache){if(!s.env.customCache)throw Error("`env.useCustomCache=true`, but `env.customCache` is not defined.");if(!s.env.customCache.match||!s.env.customCache.put)throw new Error("`env.customCache` must be an object which implements the `match` and `put` functions of the Web Cache API. For more information, see https://developer.mozilla.org/en-US/docs/Web/API/Cache");a=s.env.customCache}const o=r.revision??"main";let l,h,m=f(e,t),g=f(s.env.localModelPath,m),_=f(s.env.remoteHost,s.env.remotePathTemplate.replaceAll("{model}",e).replaceAll("{revision}",encodeURIComponent(o)),t),w="main"===o?m:f(e,o,t),y=a instanceof p?w:_,b=!1;a&&(h=await async function(e,...t){for(let n of t)try{let t=await e.match(n);if(t)return t}catch(e){continue}}(a,g,y));const v=void 0!==h;if(void 0===h){if(s.env.allowLocalModels){if(u(m,["http:","https:"])){if(r.local_files_only)throw new Error(`\`local_files_only=true\`, but attempted to load a remote file from: ${m}.`);if(!s.env.allowRemoteModels)throw new Error(`\`env.allowRemoteModels=false\`, but attempted to load a remote file from: ${m}.`)}else try{h=await d(g),l=g}catch(e){console.warn(`Unable to load from local path "${g}": "${e}"`)}}if(void 0===h||404===h.status){if(r.local_files_only||!s.env.allowRemoteModels){if(n)throw Error(`\`local_files_only=true\` or \`env.allowRemoteModels=false\` and file was not found locally at "${g}".`);return null}if(h=await d(_),200!==h.status)return function(e,t,n){if(!n)return null;const r=c[e]??`Error (${e}) occurred while trying to load file`;throw Error(`${r}: "${t}".`)}(h.status,_,n);l=y}b=a&&"undefined"!=typeof Response&&h instanceof Response&&200===h.status}(0,i.dispatchCallback)(r.progress_callback,{status:"download",name:e,file:t});const x={status:"progress",name:e,file:t};let M;return r.progress_callback?v&&"undefined"!=typeof navigator&&/firefox/i.test(navigator.userAgent)?(M=new Uint8Array(await h.arrayBuffer()),(0,i.dispatchCallback)(r.progress_callback,{...x,progress:100,loaded:M.length,total:M.length})):M=await async function(e,t){const n=e.headers.get("Content-Length");null===n&&console.warn("Unable to determine content-length from response headers. Will expand buffer when needed.");let r=parseInt(n??"0"),a=new Uint8Array(r),s=0;const i=e.body.getReader();async function o(){const{done:e,value:n}=await i.read();if(e)return;let l=s+n.length;if(l>r){r=l;let e=new Uint8Array(r);e.set(a),a=e}a.set(n,s),s=l;return t({progress:s/r*100,loaded:s,total:r}),o()}return await o(),a}(h,(e=>{(0,i.dispatchCallback)(r.progress_callback,{...x,...e})})):M=new Uint8Array(await h.arrayBuffer()),b&&l&&void 0===await a.match(l)&&await a.put(l,new Response(M,{headers:h.headers})).catch((e=>{console.warn(`Unable to add response to browser cache: ${e}.`)})),(0,i.dispatchCallback)(r.progress_callback,{status:"done",name:e,file:t}),M}async function m(e,t,n=!0,r={}){let a=await h(e,t,n,r);if(null===a)return{};let s=new TextDecoder("utf-8").decode(a);return JSON.parse(s)}function f(...e){return(e=e.map(((t,n)=>(n&&(t=t.replace(new RegExp("^/"),"")),n!==e.length-1&&(t=t.replace(new RegExp("/$"),"")),t)))).join("/")}},"./src/utils/image.js":
218/*!****************************!*\
219 !*** ./src/utils/image.js ***!
220 \****************************/(e,t,n)=>{n.r(t),n.d(t,{RawImage:()=>m});var r=n(/*! ./hub.js */"./src/utils/hub.js"),a=n(/*! ../env.js */"./src/env.js"),s=n(/*! ./tensor.js */"./src/utils/tensor.js"),i=n(/*! sharp */"?2b25");const o="undefined"!=typeof self,l=o&&"DedicatedWorkerGlobalScope"===self.constructor.name;let u,d,c;if(o)u=(e,t)=>{if(!self.OffscreenCanvas)throw new Error("OffscreenCanvas not supported by this browser.");return new self.OffscreenCanvas(e,t)},c=self.createImageBitmap,d=self.ImageData;else{if(!i)throw new Error("Unable to load image processing library.");c=async e=>{const t=(await e.metadata()).channels,{data:n,info:r}=await e.rotate().raw().toBuffer({resolveWithObject:!0}),a=new m(new Uint8ClampedArray(n),r.width,r.height,r.channels);return void 0!==t&&t!==r.channels&&a.convert(t),a}}const p={0:"nearest",1:"lanczos",2:"bilinear",3:"bicubic",4:"box",5:"hamming"},h=new Map([["png","image/png"],["jpg","image/jpeg"],["jpeg","image/jpeg"],["gif","image/gif"]]);class m{constructor(e,t,n,r){this.data=e,this.width=t,this.height=n,this.channels=r}get size(){return[this.width,this.height]}static async read(e){if(e instanceof m)return e;if("string"==typeof e||e instanceof URL)return await this.fromURL(e);throw new Error("Unsupported input type: "+typeof e)}static fromCanvas(e){if(!o)throw new Error("fromCanvas() is only supported in browser environments.");const t=e.getContext("2d").getImageData(0,0,e.width,e.height).data;return new m(t,e.width,e.height,4)}static async fromURL(e){const t=await(0,r.getFile)(e);if(200!==t.status)throw new Error(`Unable to read image from "${e}" (${t.status} ${t.statusText})`);const n=await t.blob();return this.fromBlob(n)}static async fromBlob(e){if(o){const t=await c(e),n=u(t.width,t.height).getContext("2d");return n.drawImage(t,0,0),new this(n.getImageData(0,0,t.width,t.height).data,t.width,t.height,4)}{const t=i(await e.arrayBuffer());return await c(t)}}static fromTensor(e,t="CHW"){if(3!==e.dims.length)throw new Error(`Tensor should have 3 dimensions, but has ${e.dims.length} dimensions.`);if("CHW"===t)e=e.transpose(1,2,0);else if("HWC"!==t)throw new Error(`Unsupported channel format: ${t}`);if(!(e.data instanceof Uint8ClampedArray||e.data instanceof Uint8Array))throw new Error(`Unsupported tensor type: ${e.type}`);switch(e.dims[2]){case 1:case 2:case 3:case 4:return new m(e.data,e.dims[1],e.dims[0],e.dims[2]);default:throw new Error(`Unsupported number of channels: ${e.dims[2]}`)}}grayscale(){if(1===this.channels)return this;const e=new Uint8ClampedArray(this.width*this.height*1);switch(this.channels){case 3:case 4:for(let t=0,n=0;t<this.data.length;t+=this.channels){const r=this.data[t],a=this.data[t+1],s=this.data[t+2];e[n++]=Math.round(.2989*r+.587*a+.114*s)}break;default:throw new Error(`Conversion failed due to unsupported number of channels: ${this.channels}`)}return this._update(e,this.width,this.height,1)}rgb(){if(3===this.channels)return this;const e=new Uint8ClampedArray(this.width*this.height*3);switch(this.channels){case 1:for(let t=0,n=0;t<this.data.length;++t)e[n++]=this.data[t],e[n++]=this.data[t],e[n++]=this.data[t];break;case 4:for(let t=0,n=0;t<this.data.length;t+=4)e[n++]=this.data[t],e[n++]=this.data[t+1],e[n++]=this.data[t+2];break;default:throw new Error(`Conversion failed due to unsupported number of channels: ${this.channels}`)}return this._update(e,this.width,this.height,3)}rgba(){if(4===this.channels)return this;const e=new Uint8ClampedArray(this.width*this.height*4);switch(this.channels){case 1:for(let t=0,n=0;t<this.data.length;++t)e[n++]=this.data[t],e[n++]=this.data[t],e[n++]=this.data[t],e[n++]=255;break;case 3:for(let t=0,n=0;t<this.data.length;t+=3)e[n++]=this.data[t],e[n++]=this.data[t+1],e[n++]=this.data[t+2],e[n++]=255;break;default:throw new Error(`Conversion failed due to unsupported number of channels: ${this.channels}`)}return this._update(e,this.width,this.height,4)}async resize(e,t,{resample:n=2}={}){let r=p[n]??n;if(o){const n=this.channels,r=this.toCanvas(),a=u(e,t).getContext("2d");a.drawImage(r,0,0,e,t);return new m(a.getImageData(0,0,e,t).data,e,t,4).convert(n)}{let n=this.toSharp();switch(r){case"box":case"hamming":"box"!==r&&"hamming"!==r||(console.warn(`Resampling method ${r} is not yet supported. Using bilinear instead.`),r="bilinear");case"nearest":case"bilinear":case"bicubic":n=n.affine([e/this.width,0,0,t/this.height],{interpolator:r});break;case"lanczos":n=n.resize({width:e,height:t,fit:"fill",kernel:"lanczos3"});break;default:throw new Error(`Resampling method ${r} is not supported.`)}return await c(n)}}async pad([e,t,n,r]){if(e=Math.max(e,0),t=Math.max(t,0),n=Math.max(n,0),r=Math.max(r,0),0===e&&0===t&&0===n&&0===r)return this;if(o){const a=this.channels,s=this.toCanvas(),i=this.width+e+t,o=this.height+n+r,l=u(i,o).getContext("2d");l.drawImage(s,0,0,this.width,this.height,e,n,i,o);return new m(l.getImageData(0,0,i,o).data,i,o,4).convert(a)}{const a=this.toSharp().extend({left:e,right:t,top:n,bottom:r});return await c(a)}}async crop([e,t,n,r]){if(e=Math.max(e,0),t=Math.max(t,0),n=Math.min(n,this.width-1),r=Math.min(r,this.height-1),0===e&&0===t&&n===this.width-1&&r===this.height-1)return this;const a=n-e+1,s=r-t+1;if(o){const n=this.channels,r=this.toCanvas(),i=u(a,s).getContext("2d");i.drawImage(r,e,t,a,s,0,0,a,s);return new m(i.getImageData(0,0,a,s).data,a,s,4).convert(n)}{const n=this.toSharp().extract({left:e,top:t,width:a,height:s});return await c(n)}}async center_crop(e,t){if(this.width===e&&this.height===t)return this;const n=(this.width-e)/2,r=(this.height-t)/2;if(o){const a=this.channels,s=this.toCanvas(),i=u(e,t).getContext("2d");let o=0,l=0,d=0,c=0;n>=0?o=n:d=-n,r>=0?l=r:c=-r,i.drawImage(s,o,l,e,t,d,c,e,t);return new m(i.getImageData(0,0,e,t).data,e,t,4).convert(a)}{let a=this.toSharp();if(n>=0&&r>=0)a=a.extract({left:Math.floor(n),top:Math.floor(r),width:e,height:t});else if(n<=0&&r<=0){const s=Math.floor(-r),i=Math.floor(-n);a=a.extend({top:s,left:i,right:e-this.width-i,bottom:t-this.height-s})}else{let s=[0,0],i=0;r<0?(s[0]=Math.floor(-r),s[1]=t-this.height-s[0]):i=Math.floor(r);let o=[0,0],l=0;n<0?(o[0]=Math.floor(-n),o[1]=e-this.width-o[0]):l=Math.floor(n),a=a.extend({top:s[0],bottom:s[1],left:o[0],right:o[1]}).extract({left:l,top:i,width:e,height:t})}return await c(a)}}async toBlob(e="image/png",t=1){if(!o)throw new Error("toBlob() is only supported in browser environments.");const n=this.toCanvas();return await n.convertToBlob({type:e,quality:t})}toTensor(e="CHW"){let t=new s.Tensor("uint8",new Uint8Array(this.data),[this.height,this.width,this.channels]);if("HWC"===e);else{if("CHW"!==e)throw new Error(`Unsupported channel format: ${e}`);t=t.permute(2,0,1)}return t}toCanvas(){if(!o)throw new Error("toCanvas() is only supported in browser environments.");const e=this.clone().rgba(),t=u(e.width,e.height),n=new d(e.data,e.width,e.height);return t.getContext("2d").putImageData(n,0,0),t}_update(e,t,n,r=null){return this.data=e,this.width=t,this.height=n,null!==r&&(this.channels=r),this}clone(){return new m(this.data.slice(),this.width,this.height,this.channels)}convert(e){if(this.channels===e)return this;switch(e){case 1:this.grayscale();break;case 3:this.rgb();break;case 4:this.rgba();break;default:throw new Error(`Conversion failed due to unsupported number of channels: ${this.channels}`)}return this}async save(e){if(!o){if(a.env.useFS){const t=this.toSharp();return await t.toFile(e)}throw new Error("Unable to save the image because filesystem is disabled in this environment.")}{if(l)throw new Error("Unable to save an image from a Web Worker.");const t=e.split(".").pop().toLowerCase(),n=h.get(t)??"image/png",r=await this.toBlob(n),a=URL.createObjectURL(r),s=document.createElement("a");s.href=a,s.download=e,s.click(),s.remove()}}toSharp(){if(o)throw new Error("toSharp() is only supported in server-side environments.");return i(this.data,{raw:{width:this.width,height:this.height,channels:this.channels}})}}},"./src/utils/maths.js":

Callers 15

update_requirementsFunction · 0.80
_validate_task_scheduleFunction · 0.80
format_errorFunction · 0.80
validate_skillFunction · 0.80
validate_skillFunction · 0.80
_xrandr_output_modesFunction · 0.80
matchesFunction · 0.80
save_dotenv_valueFunction · 0.80
skip_ignored_patternsFunction · 0.80
buildMarkdownPagesFunction · 0.80
prepareFootnotesFunction · 0.80

Calls 1

joinMethod · 0.45

Tested by

no test coverage detected