Brunobkr/llama.cpp_AlgMor24_github
ΩFFFΣLLIa • llama.cpp • AlgMor24 ██████╗ ███████╗███████╗███████╗██╗ ██╗ ██╗ █████╗ ██╔═══██╗██╔════╝██╔════╝██╔════╝██║ ██║ ██║██╔══██╗ ██║ ██║█████╗ █████╗ █████╗ ██║ ██║ ██║███████║ ██║ ██║██╔══╝ ██╔══╝ ██╔══╝ ██║ ██║ ██║██╔══██║ ╚██████╔╝██║ ██║ ███████╗███████╗███████╗██║██║ ██║ ╚═════╝ ╚═╝ ╚═╝ ╚══════╝╚══════╝╚══════╝╚═╝╚═╝ ╚═╝ High-Performance LLM / VLM Inference & Autonomous Agentic Ecosystem… See the full description on the dataset page: https://huggingface.co/datasets/Brunobkr/llama.cpp_AlgMor24_github.
03.1k
1/*2 Copyright 2020 Google LLC3 4 Use of this source code is governed by an MIT-style5 license that can be found in the LICENSE file or at6 https://opensource.org/licenses/MIT.7*/8 9import {assert} from 'workbox-core/_private/assert.js';10import {cacheMatchIgnoreParams} from 'workbox-core/_private/cacheMatchIgnoreParams.js';11import {Deferred} from 'workbox-core/_private/Deferred.js';12import {executeQuotaErrorCallbacks} from 'workbox-core/_private/executeQuotaErrorCallbacks.js';13import {getFriendlyURL} from 'workbox-core/_private/getFriendlyURL.js';14import {logger} from 'workbox-core/_private/logger.js';15import {timeout} from 'workbox-core/_private/timeout.js';16import {WorkboxError} from 'workbox-core/_private/WorkboxError.js';17import {18 HandlerCallbackOptions,19 MapLikeObject,20 WorkboxPlugin,21 WorkboxPluginCallbackParam,22} from 'workbox-core/types.js';23 24import {Strategy} from './Strategy.js';25import './_version.js';26 27function toRequest(input: RequestInfo) {28 return typeof input === 'string' ? new Request(input) : input;29}30 31/**32 * A class created every time a Strategy instance calls33 * {@link workbox-strategies.Strategy~handle} or34 * {@link workbox-strategies.Strategy~handleAll} that wraps all fetch and35 * cache actions around plugin callbacks and keeps track of when the strategy36 * is "done" (i.e. all added `event.waitUntil()` promises have resolved).37 *38 * @memberof workbox-strategies39 */40class StrategyHandler {41 public request!: Request;42 public url?: URL;43 public event: ExtendableEvent;44 public params?: any;45 46 private _cacheKeys: Record<string, Request> = {};47 48 private readonly _strategy: Strategy;49 private readonly _extendLifetimePromises: Promise<any>[];50 private readonly _handlerDeferred: Deferred<any>;51 private readonly _plugins: WorkboxPlugin[];52 private readonly _pluginStateMap: Map<WorkboxPlugin, MapLikeObject>;53 54 /**55 * Creates a new instance associated with the passed strategy and event56 * that's handling the request.57 *58 * The constructor also initializes the state that will be passed to each of59 * the plugins handling this request.60 *61 * @param {workbox-strategies.Strategy} strategy62 * @param {Object} options63 * @param {Request|string} options.request A request to run this strategy for.64 * @param {ExtendableEvent} options.event The event associated with the65 * request.66 * @param {URL} [options.url]67 * @param {*} [options.params] The return value from the68 * {@link workbox-routing~matchCallback} (if applicable).69 */70 constructor(strategy: Strategy, options: HandlerCallbackOptions) {71 /**72 * The request the strategy is performing (passed to the strategy's73 * `handle()` or `handleAll()` method).74 * @name request75 * @instance76 * @type {Request}77 * @memberof workbox-strategies.StrategyHandler78 */79 /**80 * The event associated with this request.81 * @name event82 * @instance83 * @type {ExtendableEvent}84 * @memberof workbox-strategies.StrategyHandler85 */86 /**87 * A `URL` instance of `request.url` (if passed to the strategy's88 * `handle()` or `handleAll()` method).89 * Note: the `url` param will be present if the strategy was invoked90 * from a workbox `Route` object.91 * @name url92 * @instance93 * @type {URL|undefined}94 * @memberof workbox-strategies.StrategyHandler95 */96 /**97 * A `param` value (if passed to the strategy's98 * `handle()` or `handleAll()` method).99 * Note: the `param` param will be present if the strategy was invoked100 * from a workbox `Route` object and the101 * {@link workbox-routing~matchCallback} returned102 * a truthy value (it will be that value).103 * @name params104 * @instance105 * @type {*|undefined}106 * @memberof workbox-strategies.StrategyHandler107 */108 if (process.env.NODE_ENV !== 'production') {109 assert!.isInstance(options.event, ExtendableEvent, {110 moduleName: 'workbox-strategies',111 className: 'StrategyHandler',112 funcName: 'constructor',113 paramName: 'options.event',114 });115 }116 117 Object.assign(this, options);118 119 this.event = options.event;120 this._strategy = strategy;121 this._handlerDeferred = new Deferred();122 this._extendLifetimePromises = [];123 124 // Copy the plugins list (since it's mutable on the strategy),125 // so any mutations don't affect this handler instance.126 this._plugins = [...strategy.plugins];127 this._pluginStateMap = new Map();128 for (const plugin of this._plugins) {129 this._pluginStateMap.set(plugin, {});130 }131 132 this.event.waitUntil(this._handlerDeferred.promise);133 }134 135 /**136 * Fetches a given request (and invokes any applicable plugin callback137 * methods) using the `fetchOptions` (for non-navigation requests) and138 * `plugins` defined on the `Strategy` object.139 *140 * The following plugin lifecycle methods are invoked when using this method:141 * - `requestWillFetch()`142 * - `fetchDidSucceed()`143 * - `fetchDidFail()`144 *145 * @param {Request|string} input The URL or request to fetch.146 * @return {Promise<Response>}147 */148 async fetch(input: RequestInfo): Promise<Response> {149 const {event} = this;150 let request: Request = toRequest(input);151 152 if (153 request.mode === 'navigate' &&154 event instanceof FetchEvent &&155 event.preloadResponse156 ) {157 const possiblePreloadResponse = (await event.preloadResponse) as158 | Response159 | undefined;160 if (possiblePreloadResponse) {161 if (process.env.NODE_ENV !== 'production') {162 logger.log(163 `Using a preloaded navigation response for ` +164 `'${getFriendlyURL(request.url)}'`,165 );166 }167 return possiblePreloadResponse;168 }169 }170 171 // If there is a fetchDidFail plugin, we need to save a clone of the172 // original request before it's either modified by a requestWillFetch173 // plugin or before the original request's body is consumed via fetch().174 const originalRequest = this.hasCallback('fetchDidFail')175 ? request.clone()176 : null;177 178 try {179 for (const cb of this.iterateCallbacks('requestWillFetch')) {180 request = await cb({request: request.clone(), event});181 }182 } catch (err) {183 if (err instanceof Error) {184 throw new WorkboxError('plugin-error-request-will-fetch', {185 thrownErrorMessage: err.message,186 });187 }188 }189 190 // The request can be altered by plugins with `requestWillFetch` making191 // the original request (most likely from a `fetch` event) different192 // from the Request we make. Pass both to `fetchDidFail` to aid debugging.193 const pluginFilteredRequest: Request = request.clone();194 195 try {196 let fetchResponse: Response;197 198 // See https://github.com/GoogleChrome/workbox/issues/1796199 fetchResponse = await fetch(200 request,201 request.mode === 'navigate' ? undefined : this._strategy.fetchOptions,202 );203 204 if (process.env.NODE_ENV !== 'production') {205 logger.debug(206 `Network request for ` +207 `'${getFriendlyURL(request.url)}' returned a response with ` +208 `status '${fetchResponse.status}'.`,209 );210 }211 212 for (const callback of this.iterateCallbacks('fetchDidSucceed')) {213 fetchResponse = await callback({214 event,215 request: pluginFilteredRequest,216 response: fetchResponse,217 });218 }219 return fetchResponse;220 } catch (error) {221 if (process.env.NODE_ENV !== 'production') {222 logger.log(223 `Network request for ` +224 `'${getFriendlyURL(request.url)}' threw an error.`,225 error,226 );227 }228 229 // `originalRequest` will only exist if a `fetchDidFail` callback230 // is being used (see above).231 if (originalRequest) {232 await this.runCallbacks('fetchDidFail', {233 error: error as Error,234 event,235 originalRequest: originalRequest.clone(),236 request: pluginFilteredRequest.clone(),237 });238 }239 throw error;240 }241 }242 243 /**244 * Calls `this.fetch()` and (in the background) runs `this.cachePut()` on245 * the response generated by `this.fetch()`.246 *247 * The call to `this.cachePut()` automatically invokes `this.waitUntil()`,248 * so you do not have to manually call `waitUntil()` on the event.249 *250 * @param {Request|string} input The request or URL to fetch and cache.251 * @return {Promise<Response>}252 */253 async fetchAndCachePut(input: RequestInfo): Promise<Response> {254 const response = await this.fetch(input);255 const responseClone = response.clone();256 257 void this.waitUntil(this.cachePut(input, responseClone));258 259 return response;260 }261 262 /**263 * Matches a request from the cache (and invokes any applicable plugin264 * callback methods) using the `cacheName`, `matchOptions`, and `plugins`265 * defined on the strategy object.266 *267 * The following plugin lifecycle methods are invoked when using this method:268 * - cacheKeyWillBeUsed()269 * - cachedResponseWillBeUsed()270 *271 * @param {Request|string} key The Request or URL to use as the cache key.272 * @return {Promise<Response|undefined>} A matching response, if found.273 */274 async cacheMatch(key: RequestInfo): Promise<Response | undefined> {275 const request: Request = toRequest(key);276 let cachedResponse: Response | undefined;277 const {cacheName, matchOptions} = this._strategy;278 279 const effectiveRequest = await this.getCacheKey(request, 'read');280 const multiMatchOptions = {...matchOptions, ...{cacheName}};281 282 cachedResponse = await caches.match(effectiveRequest, multiMatchOptions);283 284 if (process.env.NODE_ENV !== 'production') {285 if (cachedResponse) {286 logger.debug(`Found a cached response in '${cacheName}'.`);287 } else {288 logger.debug(`No cached response found in '${cacheName}'.`);289 }290 }291 292 for (const callback of this.iterateCallbacks('cachedResponseWillBeUsed')) {293 cachedResponse =294 (await callback({295 cacheName,296 matchOptions,297 cachedResponse,298 request: effectiveRequest,299 event: this.event,300 })) || undefined;301 }302 return cachedResponse;303 }304 305 /**306 * Puts a request/response pair in the cache (and invokes any applicable307 * plugin callback methods) using the `cacheName` and `plugins` defined on308 * the strategy object.309 *310 * The following plugin lifecycle methods are invoked when using this method:311 * - cacheKeyWillBeUsed()312 * - cacheWillUpdate()313 * - cacheDidUpdate()314 *315 * @param {Request|string} key The request or URL to use as the cache key.316 * @param {Response} response The response to cache.317 * @return {Promise<boolean>} `false` if a cacheWillUpdate caused the response318 * not be cached, and `true` otherwise.319 */320 async cachePut(key: RequestInfo, response: Response): Promise<boolean> {321 const request: Request = toRequest(key);322 323 // Run in the next task to avoid blocking other cache reads.324 // https://github.com/w3c/ServiceWorker/issues/1397325 await timeout(0);326 327 const effectiveRequest = await this.getCacheKey(request, 'write');328 329 if (process.env.NODE_ENV !== 'production') {330 if (effectiveRequest.method && effectiveRequest.method !== 'GET') {331 throw new WorkboxError('attempt-to-cache-non-get-request', {332 url: getFriendlyURL(effectiveRequest.url),333 method: effectiveRequest.method,334 });335 }336 337 // See https://github.com/GoogleChrome/workbox/issues/2818338 const vary = response.headers.get('Vary');339 if (vary) {340 logger.debug(341 `The response for ${getFriendlyURL(effectiveRequest.url)} ` +342 `has a 'Vary: ${vary}' header. ` +343 `Consider setting the {ignoreVary: true} option on your strategy ` +344 `to ensure cache matching and deletion works as expected.`,345 );346 }347 }348 349 if (!response) {350 if (process.env.NODE_ENV !== 'production') {351 logger.error(352 `Cannot cache non-existent response for ` +353 `'${getFriendlyURL(effectiveRequest.url)}'.`,354 );355 }356 357 throw new WorkboxError('cache-put-with-no-response', {358 url: getFriendlyURL(effectiveRequest.url),359 });360 }361 362 const responseToCache = await this._ensureResponseSafeToCache(response);363 364 if (!responseToCache) {365 if (process.env.NODE_ENV !== 'production') {366 logger.debug(367 `Response '${getFriendlyURL(effectiveRequest.url)}' ` +368 `will not be cached.`,369 responseToCache,370 );371 }372 return false;373 }374 375 const {cacheName, matchOptions} = this._strategy;376 const cache = await self.caches.open(cacheName);377 378 const hasCacheUpdateCallback = this.hasCallback('cacheDidUpdate');379 const oldResponse = hasCacheUpdateCallback380 ? await cacheMatchIgnoreParams(381 // TODO(philipwalton): the `__WB_REVISION__` param is a precaching382 // feature. Consider into ways to only add this behavior if using383 // precaching.384 cache,385 effectiveRequest.clone(),386 ['__WB_REVISION__'],387 matchOptions,388 )389 : null;390 391 if (process.env.NODE_ENV !== 'production') {392 logger.debug(393 `Updating the '${cacheName}' cache with a new Response ` +394 `for ${getFriendlyURL(effectiveRequest.url)}.`,395 );396 }397 398 try {399 await cache.put(400 effectiveRequest,401 hasCacheUpdateCallback ? responseToCache.clone() : responseToCache,402 );403 } catch (error) {404 if (error instanceof Error) {405 // See https://developer.mozilla.org/en-US/docs/Web/API/DOMException#exception-QuotaExceededError406 if (error.name === 'QuotaExceededError') {407 await executeQuotaErrorCallbacks();408 }409 throw error;410 }411 }412 413 for (const callback of this.iterateCallbacks('cacheDidUpdate')) {414 await callback({415 cacheName,416 oldResponse,417 newResponse: responseToCache.clone(),418 request: effectiveRequest,419 event: this.event,420 });421 }422 423 return true;424 }425 426 /**427 * Checks the list of plugins for the `cacheKeyWillBeUsed` callback, and428 * executes any of those callbacks found in sequence. The final `Request`429 * object returned by the last plugin is treated as the cache key for cache430 * reads and/or writes. If no `cacheKeyWillBeUsed` plugin callbacks have431 * been registered, the passed request is returned unmodified432 *433 * @param {Request} request434 * @param {string} mode435 * @return {Promise<Request>}436 */437 async getCacheKey(438 request: Request,439 mode: 'read' | 'write',440 ): Promise<Request> {441 const key = `${request.url} | ${mode}`;442 if (!this._cacheKeys[key]) {443 let effectiveRequest = request;444 445 for (const callback of this.iterateCallbacks('cacheKeyWillBeUsed')) {446 effectiveRequest = toRequest(447 await callback({448 mode,449 request: effectiveRequest,450 event: this.event,451 // params has a type any can't change right now.452 params: this.params, // eslint-disable-line453 }),454 );455 }456 457 this._cacheKeys[key] = effectiveRequest;458 }459 return this._cacheKeys[key];460 }461 462 /**463 * Returns true if the strategy has at least one plugin with the given464 * callback.465 *466 * @param {string} name The name of the callback to check for.467 * @return {boolean}468 */469 hasCallback<C extends keyof WorkboxPlugin>(name: C): boolean {470 for (const plugin of this._strategy.plugins) {471 if (name in plugin) {472 return true;473 }474 }475 return false;476 }477 478 /**479 * Runs all plugin callbacks matching the given name, in order, passing the480 * given param object (merged ith the current plugin state) as the only481 * argument.482 *483 * Note: since this method runs all plugins, it's not suitable for cases484 * where the return value of a callback needs to be applied prior to calling485 * the next callback. See486 * {@link workbox-strategies.StrategyHandler#iterateCallbacks}487 * below for how to handle that case.488 *489 * @param {string} name The name of the callback to run within each plugin.490 * @param {Object} param The object to pass as the first (and only) param491 * when executing each callback. This object will be merged with the492 * current plugin state prior to callback execution.493 */494 async runCallbacks<C extends keyof NonNullable<WorkboxPlugin>>(495 name: C,496 param: Omit<WorkboxPluginCallbackParam[C], 'state'>,497 ): Promise<void> {498 for (const callback of this.iterateCallbacks(name)) {499 // TODO(philipwalton): not sure why `any` is needed. It seems like500 // this should work with `as WorkboxPluginCallbackParam[C]`.501 await callback(param as any);502 }503 }504 505 /**506 * Accepts a callback and returns an iterable of matching plugin callbacks,507 * where each callback is wrapped with the current handler state (i.e. when508 * you call each callback, whatever object parameter you pass it will509 * be merged with the plugin's current state).510 *511 * @param {string} name The name fo the callback to run512 * @return {Array<Function>}513 */514 *iterateCallbacks<C extends keyof WorkboxPlugin>(515 name: C,516 ): Generator<NonNullable<WorkboxPlugin[C]>> {517 for (const plugin of this._strategy.plugins) {518 if (typeof plugin[name] === 'function') {519 const state = this._pluginStateMap.get(plugin);520 const statefulCallback = (521 param: Omit<WorkboxPluginCallbackParam[C], 'state'>,522 ) => {523 const statefulParam = {...param, state};524 525 // TODO(philipwalton): not sure why `any` is needed. It seems like526 // this should work with `as WorkboxPluginCallbackParam[C]`.527 return plugin[name]!(statefulParam as any);528 };529 yield statefulCallback as NonNullable<WorkboxPlugin[C]>;530 }531 }532 }533 534 /**535 * Adds a promise to the536 * [extend lifetime promises]{@link https://w3c.github.io/ServiceWorker/#extendableevent-extend-lifetime-promises}537 * of the event associated with the request being handled (usually a538 * `FetchEvent`).539 *540 * Note: you can await541 * {@link workbox-strategies.StrategyHandler~doneWaiting}542 * to know when all added promises have settled.543 *544 * @param {Promise} promise A promise to add to the extend lifetime promises545 * of the event that triggered the request.546 */547 waitUntil<T>(promise: Promise<T>): Promise<T> {548 this._extendLifetimePromises.push(promise);549 return promise;550 }551 552 /**553 * Returns a promise that resolves once all promises passed to554 * {@link workbox-strategies.StrategyHandler~waitUntil}555 * have settled.556 *557 * Note: any work done after `doneWaiting()` settles should be manually558 * passed to an event's `waitUntil()` method (not this handler's559 * `waitUntil()` method), otherwise the service worker thread may be killed560 * prior to your work completing.561 */562 async doneWaiting(): Promise<void> {563 while (this._extendLifetimePromises.length) {564 const promises = this._extendLifetimePromises.splice(0);565 const result = await Promise.allSettled(promises);566 const firstRejection = result.find((i) => i.status === 'rejected');567 if (firstRejection) {568 throw (firstRejection as PromiseRejectedResult).reason;569 }570 }571 }572 573 /**574 * Stops running the strategy and immediately resolves any pending575 * `waitUntil()` promises.576 */577 destroy(): void {578 this._handlerDeferred.resolve(null);579 }580 581 /**582 * This method will call cacheWillUpdate on the available plugins (or use583 * status === 200) to determine if the Response is safe and valid to cache.584 *585 * @param {Request} options.request586 * @param {Response} options.response587 * @return {Promise<Response|undefined>}588 *589 * @private590 */591 async _ensureResponseSafeToCache(592 response: Response,593 ): Promise<Response | undefined> {594 let responseToCache: Response | undefined = response;595 let pluginsUsed = false;596 597 for (const callback of this.iterateCallbacks('cacheWillUpdate')) {598 responseToCache =599 (await callback({600 request: this.request,601 response: responseToCache,602 event: this.event,603 })) || undefined;604 pluginsUsed = true;605 606 if (!responseToCache) {607 break;608 }609 }610 611 if (!pluginsUsed) {612 if (responseToCache && responseToCache.status !== 200) {613 responseToCache = undefined;614 }615 if (process.env.NODE_ENV !== 'production') {616 if (responseToCache) {617 if (responseToCache.status !== 200) {618 if (responseToCache.status === 0) {619 logger.warn(620 `The response for '${this.request.url}' ` +621 `is an opaque response. The caching strategy that you're ` +622 `using will not cache opaque responses by default.`,623 );624 } else {625 logger.debug(626 `The response for '${this.request.url}' ` +627 `returned a status code of '${response.status}' and won't ` +628 `be cached as a result.`,629 );630 }631 }632 }633 }634 }635 636 return responseToCache;637 }638}639 640export {StrategyHandler};641 