UNPKG

nano-ai-pdf

Version:

This package helps you summarize pdfs using Gemini nano on edge or on browser, making it compliant safe, faster and free

136 lines 6.44 kB
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; import { PdfSummarizerError, } from "./types"; /** * Store the most recent download progress percentage. */ let currentDownloadProgress = 0; /** * Optional user-provided callback for progress updates. */ let progressCallback = null; /** * Allows consumer to get the latest download progress value. */ export function getDownloadProgress() { return currentDownloadProgress; } /** * Allows consumer to set a callback function to receive real-time progress updates. */ export function onDownloadProgressUpdate(callback) { progressCallback = callback; } /** * Checks if Chrome's built-in Summarizer API is available * * @returns boolean indicating if the API is available */ export function isSummarizerSupported() { const isSupported = typeof Summarizer !== "undefined"; console.log(`🔍 Summarizer API support check: ${isSupported ? "✅ Supported" : "❌ Not supported"}`); return isSupported; } /** * Checks the availability status of the Chrome Summarizer model */ export function checkModelAvailability() { return __awaiter(this, void 0, void 0, function* () { console.log("🔍 Checking Chrome Summarizer model availability..."); if (!isSummarizerSupported()) { console.error("❌ Summarizer API is not available. Please use Chrome 138+ with experimental flags enabled."); throw new PdfSummarizerError("Summarizer API is not available. Please check your Chrome version and ensure experimental flags are enabled.", "API_NOT_SUPPORTED"); } try { const availability = yield Summarizer.availability(); switch (availability) { case "available": console.log("✅ Summarizer model is ready for immediate use"); break; case "downloadable": console.log("⏳ Summarizer model needs to be downloaded on first use"); break; case "downloading": console.log("📥 Summarizer model is currently downloading"); break; case "unavailable": console.log("❌ Summarizer model is not available on this device"); break; default: console.log(`⚠️ Unknown availability status: ${availability}`); } return availability; } catch (error) { const errorMessage = error instanceof Error ? error.message : "Unknown error"; console.error(`❌ Failed to check model availability: ${errorMessage}`); throw new PdfSummarizerError(`Failed to check model availability: ${errorMessage}`, "AVAILABILITY_CHECK_FAILED"); } }); } /** * Validates user activation requirement for Chrome AI APIs */ export function validateUserActivation() { var _a; console.log("🔍 Checking user activation status..."); if (!((_a = navigator.userActivation) === null || _a === void 0 ? void 0 : _a.isActive)) { console.error("❌ User activation is required to create a summarizer session"); throw new PdfSummarizerError("User activation is required. This function must be called in response to a user gesture (click, tap, keypress).", "USER_ACTIVATION_REQUIRED"); } console.log("✅ User activation detected"); } /** * Creates a Chrome Summarizer instance with the specified configuration * and tracks download progress. */ export function createSummarizer(options) { return __awaiter(this, void 0, void 0, function* () { var _a, _b, _c; console.log("🔧 Creating Chrome Summarizer instance..."); console.log("⚙️ Configuration:", JSON.stringify(options, null, 2)); validateUserActivation(); const availability = yield checkModelAvailability(); if (availability === "unavailable") { throw new PdfSummarizerError("Summarizer model is not available on this device. Please check hardware requirements.", "MODEL_UNAVAILABLE"); } try { const summarizerOptions = { type: (_a = options.type) !== null && _a !== void 0 ? _a : "key-points", format: (_b = options.format) !== null && _b !== void 0 ? _b : "markdown", length: (_c = options.length) !== null && _c !== void 0 ? _c : "medium", monitor: (monitor) => { console.log("📥 Model download monitor attached"); monitor.addEventListener("downloadprogress", (event) => { const progress = Math.round((event.loaded / event.total) * 100); currentDownloadProgress = progress; console.log(`📥 Model download progress: ${progress}%`); // If a callback is registered, call it if (progressCallback) { progressCallback(progress); } }); }, }; if (options.sharedContext) { summarizerOptions.sharedContext = options.sharedContext; } const summarizer = yield Summarizer.create(summarizerOptions); console.log("✅ Chrome Summarizer instance created successfully"); return summarizer; } catch (error) { const errorMessage = error instanceof Error ? error.message : "Unknown error"; console.error(`❌ Failed to create summarizer: ${errorMessage}`); throw new PdfSummarizerError(`Failed to create summarizer: ${errorMessage}`, "SUMMARIZER_CREATION_FAILED"); } }); } //# sourceMappingURL=chrome-summarizer.js.map