@tryhamster/gerbil 1.0.0 → 1.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +85 -34
- package/dist/browser/index.d.ts +12 -2
- package/dist/browser/index.d.ts.map +1 -1
- package/dist/browser/index.js +12 -5
- package/dist/browser/index.js.map +1 -1
- package/dist/cli.mjs +7 -7
- package/dist/cli.mjs.map +1 -1
- package/dist/frameworks/express.d.mts +1 -1
- package/dist/frameworks/express.mjs +1 -1
- package/dist/frameworks/fastify.d.mts +1 -1
- package/dist/frameworks/fastify.mjs +1 -1
- package/dist/frameworks/hono.d.mts +1 -1
- package/dist/frameworks/hono.mjs +1 -1
- package/dist/frameworks/next.d.mts +3 -3
- package/dist/frameworks/next.mjs +1 -1
- package/dist/frameworks/react.d.mts +1 -1
- package/dist/frameworks/trpc.d.mts +1 -1
- package/dist/frameworks/trpc.mjs +1 -1
- package/dist/{gerbil-BT9fCydo.d.mts → gerbil-BetB5xb0.d.mts} +3 -3
- package/dist/{gerbil-BT9fCydo.d.mts.map → gerbil-BetB5xb0.d.mts.map} +1 -1
- package/dist/gerbil-CTZUa8EZ.mjs +4 -0
- package/dist/{gerbil-BHrJJIa4.mjs → gerbil-DNniplr4.mjs} +2 -2
- package/dist/{gerbil-BHrJJIa4.mjs.map → gerbil-DNniplr4.mjs.map} +1 -1
- package/dist/gpu/hooks.d.mts +122 -2
- package/dist/gpu/hooks.d.mts.map +1 -1
- package/dist/gpu/hooks.mjs +187 -6
- package/dist/gpu/hooks.mjs.map +1 -1
- package/dist/gpu/index.d.mts +2 -2
- package/dist/gpu/index.mjs +2 -2
- package/dist/{gpu-33qCAtHW.mjs → gpu-DFuglcEx.mjs} +178 -3
- package/dist/gpu-DFuglcEx.mjs.map +1 -0
- package/dist/{index-jEAL2s-A.d.mts → index-DukkJRMj.d.mts} +104 -12
- package/dist/{index-jEAL2s-A.d.mts.map → index-DukkJRMj.d.mts.map} +1 -1
- package/dist/index.d.mts +3 -3
- package/dist/index.mjs +4 -4
- package/dist/integrations/ai-sdk.d.mts +1 -1
- package/dist/integrations/ai-sdk.mjs +1 -1
- package/dist/integrations/langchain.d.mts +1 -1
- package/dist/integrations/langchain.mjs +1 -1
- package/dist/integrations/llamaindex.d.mts +1 -1
- package/dist/integrations/llamaindex.mjs +1 -1
- package/dist/integrations/mcp.d.mts +3 -3
- package/dist/integrations/mcp.mjs +4 -4
- package/dist/{mcp-1DaMsaBc.mjs → mcp-D2vvH1Xc.mjs} +3 -3
- package/dist/{mcp-1DaMsaBc.mjs.map → mcp-D2vvH1Xc.mjs.map} +1 -1
- package/dist/moonshine-stt-17dpP1kr.mjs +4 -0
- package/dist/{moonshine-stt-v_P_Ci_m.mjs → moonshine-stt-4ojLtMq7.mjs} +30 -4
- package/dist/{moonshine-stt-v_P_Ci_m.mjs.map → moonshine-stt-4ojLtMq7.mjs.map} +1 -1
- package/dist/{one-liner-DnQn7HJK.mjs → one-liner-JhdIPxzF.mjs} +2 -2
- package/dist/{one-liner-DnQn7HJK.mjs.map → one-liner-JhdIPxzF.mjs.map} +1 -1
- package/dist/{repl-jV5gcJFA.mjs → repl-BDRkwPGX.mjs} +3 -3
- package/dist/skills/index.d.mts +3 -3
- package/dist/skills/index.mjs +3 -3
- package/dist/{skills-DX8D59UH.mjs → skills-CU694Dc8.mjs} +2 -2
- package/dist/{skills-DX8D59UH.mjs.map → skills-CU694Dc8.mjs.map} +1 -1
- package/dist/{types-D6FiR_oh.d.mts → types-LlyYILII.d.mts} +7 -3
- package/dist/types-LlyYILII.d.mts.map +1 -0
- package/package.json +1 -1
- package/dist/gerbil-DomNfIr1.mjs +0 -4
- package/dist/gpu-33qCAtHW.mjs.map +0 -1
- package/dist/moonshine-stt-BLyVoRpB.mjs +0 -4
- package/dist/types-D6FiR_oh.d.mts.map +0 -1
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","names":["BUILTIN_MODELS: Record<string, ModelConfig>","audioContext: AudioContext | null","MODEL_SIZES: Record<string, number>","chat: string","reason: string","state: SessionState","bytesDownloaded","e: any"],"sources":["../../src/core/models.ts","../../src/browser/pwa.ts","../../src/browser/audio.ts","../../src/browser/device-guards.ts","../../src/browser/download.ts"],"sourcesContent":["/**\n * Model Registry\n *\n * Supports built-in models and any HuggingFace model via hf:org/model syntax\n */\n\nimport type { ModelConfig, ModelSource } from \"./types.js\";\n\n// ============================================\n// Canonical default model\n// ============================================\n\n/**\n * The default model used everywhere a model id is not explicitly provided\n * (CLI flags, REPL, framework adapters, integrations, one-liner). This is the\n * e2e-validated model; reference this constant instead of hard-coding the id.\n */\nexport const DEFAULT_MODEL = \"qwen3.5-0.8b\";\n\n// ============================================\n// Built-in Models (curated & tested)\n// ============================================\n\n// Every entry is a standard HuggingFace safetensors repo whose architecture the\n// native WebGPU engine supports (Qwen2/Qwen3/Qwen3.5, LFM2 — see\n// src/gpu/architectures/index.ts). The engine quantizes weights to INT4 on load;\n// `size` is the bf16/fp16 download size. Only add a repo here whose architecture\n// has a graph generator in the registry.\nexport const BUILTIN_MODELS: Record<string, ModelConfig> = {\n \"qwen3.5-0.8b\": {\n id: \"qwen3.5-0.8b\",\n repo: \"Qwen/Qwen3.5-0.8B\",\n description:\n \"Qwen3.5 0.8B - Fast, multimodal (vision), 262k context, supports thinking (default)\",\n size: \"~1.6GB\",\n contextLength: 262_144,\n supportsThinking: true,\n supportsJson: true,\n supportsVision: true,\n family: \"qwen\",\n },\n \"qwen3.5-2b\": {\n id: \"qwen3.5-2b\",\n repo: \"Qwen/Qwen3.5-2B\",\n description:\n \"Qwen3.5 2B - Higher quality, multimodal (vision), 262k context, supports thinking\",\n size: \"~4GB\",\n contextLength: 262_144,\n supportsThinking: true,\n supportsJson: true,\n supportsVision: true,\n family: \"qwen\",\n },\n \"lfm2.5-1.2b-thinking\": {\n id: \"lfm2.5-1.2b-thinking\",\n repo: \"LiquidAI/LFM2.5-1.2B-Thinking\",\n description: \"LFM2.5 1.2B Thinking - Efficient reasoning model, 128k context\",\n size: \"~2.4GB\",\n contextLength: 128_000,\n supportsThinking: true,\n supportsJson: false,\n family: \"other\",\n },\n};\n\n// ============================================\n// Model Resolution\n// ============================================\n\n/**\n * Parse model identifier and resolve to source\n *\n * Supported formats:\n * - \"qwen3.5-0.8b\" (built-in)\n * - \"hf:org/model\" (HuggingFace shorthand)\n * - \"https://huggingface.co/org/model\" (full URL)\n * - \"file:./path/to/model\" (local path)\n */\nexport function resolveModel(modelId: string): ModelSource {\n // Built-in model\n if (BUILTIN_MODELS[modelId]) {\n return {\n type: \"builtin\",\n path: BUILTIN_MODELS[modelId].repo,\n };\n }\n\n // HuggingFace shorthand: hf:org/model\n if (modelId.startsWith(\"hf:\")) {\n const repo = modelId.slice(3);\n return {\n type: \"huggingface\",\n path: repo,\n };\n }\n\n // HuggingFace URL\n if (modelId.startsWith(\"https://huggingface.co/\")) {\n const repo = modelId.replace(\"https://huggingface.co/\", \"\");\n return {\n type: \"huggingface\",\n path: repo,\n };\n }\n\n // Local file\n if (modelId.startsWith(\"file:\")) {\n const path = modelId.slice(5);\n return {\n type: \"local\",\n path,\n };\n }\n\n // Assume it's a HuggingFace repo if it contains a slash\n if (modelId.includes(\"/\")) {\n return {\n type: \"huggingface\",\n path: modelId,\n };\n }\n\n // Unknown - treat as HuggingFace\n return {\n type: \"huggingface\",\n path: modelId,\n };\n}\n\n/**\n * Get model config (built-in only)\n */\nexport function getModelConfig(modelId: string): ModelConfig | null {\n return BUILTIN_MODELS[modelId] || null;\n}\n\n// Default context lengths for the families the native engine actually supports\n// (a graph generator exists in src/gpu/architectures). Other families fall back\n// to a conservative default.\nconst FAMILY_CONTEXT_DEFAULTS: Record<string, number> = {\n qwen: 32_768,\n other: 32_768, // LFM2 supports 128k but config.json is the real source of truth\n};\n\n/**\n * Create model config for an external HuggingFace model.\n *\n * Inference is restricted to families the engine can actually run — Qwen\n * (Qwen2/Qwen3/Qwen3.5) and LFM2 (Liquid). Everything else is left as \"other\"\n * with conservative capability flags so the REPL doesn't advertise features the\n * engine can't deliver.\n */\nexport function createExternalModelConfig(\n modelId: string,\n repo: string,\n contextLength?: number,\n): ModelConfig {\n const repoLower = repo.toLowerCase();\n\n // Only infer families that have a graph generator in the registry.\n let family: ModelConfig[\"family\"] = \"other\";\n if (repoLower.includes(\"qwen\")) {\n family = \"qwen\";\n }\n\n const isLiquid = repoLower.includes(\"lfm\") || repoLower.includes(\"liquid\");\n const isQwen = family === \"qwen\";\n\n return {\n id: modelId,\n repo,\n description: `External model: ${repo}`,\n size: \"Unknown\",\n contextLength: contextLength || FAMILY_CONTEXT_DEFAULTS[family] || 32_768,\n // Qwen3/Qwen3.5 and LFM2.5-Thinking expose thinking; nothing here is vision.\n supportsThinking: isQwen || isLiquid,\n supportsJson: isQwen,\n family,\n };\n}\n\n/**\n * Fetch context length from HuggingFace model config\n */\nexport async function fetchModelContextLength(repo: string): Promise<number | null> {\n try {\n const res = await fetch(`https://huggingface.co/${repo}/raw/main/config.json`);\n if (!res.ok) {\n return null;\n }\n\n const config = await res.json();\n\n // Different models use different field names\n return (\n config.max_position_embeddings ||\n config.n_positions ||\n config.max_seq_len ||\n config.sliding_window || // Some models use this\n config.context_length ||\n null\n );\n } catch {\n return null;\n }\n}\n\n/**\n * List all built-in models\n */\nexport function listBuiltinModels(): ModelConfig[] {\n return Object.values(BUILTIN_MODELS);\n}\n\n/**\n * Search HuggingFace models (placeholder - would need HF API)\n */\nexport async function searchModels(query: string): Promise<ModelConfig[]> {\n // TODO: Implement HuggingFace API search\n // For now, filter built-in models\n const q = query.toLowerCase();\n return listBuiltinModels().filter(\n (m) =>\n m.id.toLowerCase().includes(q) ||\n m.description.toLowerCase().includes(q) ||\n m.family.toLowerCase().includes(q),\n );\n}\n","/**\n * Mobile / PWA storage helpers.\n *\n * On-device models are large (a 4-bit 0.8B is ~400 MB; vision/larger models are\n * GBs). Mobile browsers — iOS Safari especially — wall a web origin off from the\n * real disk with TWO independent ceilings:\n *\n * 1. **Storage quota** (disk for the model cache). An *uninstalled* Safari tab\n * gets only ~1 GB, best-effort and evictable, regardless of how much free\n * disk the device has. Exceed it and every cache write fails → the model\n * re-downloads on every visit.\n * 2. **Tab memory** (RAM during load/inference) — a separate, smaller ceiling.\n *\n * The unlock for the storage ceiling is **persistent storage**, which iOS Safari\n * grants when the site is **installed to the Home Screen** (a PWA). Installed, the\n * quota jumps to a large fraction of actual disk and is never evicted — so models\n * cache once and stay. These helpers let an app surface that to its users and\n * request it, so on-device AI is actually practical on mobile.\n *\n * All functions are SSR/Node-safe (guarded; return conservative defaults).\n */\n\n/** True when the page is running as an installed/standalone PWA (Home Screen). */\nexport function isStandalone(): boolean {\n if (typeof window === \"undefined\") return false;\n // iOS Safari exposes navigator.standalone; everyone else uses display-mode.\n const iosStandalone = (navigator as { standalone?: boolean }).standalone === true;\n const displayStandalone =\n typeof window.matchMedia === \"function\" &&\n window.matchMedia(\"(display-mode: standalone)\").matches;\n return iosStandalone || displayStandalone;\n}\n\n/** True when running on iOS/iPadOS (where install is the quota unlock and the\n * install flow is manual: Share → Add to Home Screen). iPadOS masquerades as\n * macOS, so we also treat touch-capable WebKit-on-Mac as iOS. */\nexport function isIOS(): boolean {\n if (typeof navigator === \"undefined\") return false;\n const ua = navigator.userAgent || \"\";\n if (/iPhone|iPad|iPod/.test(ua)) return true;\n // iPadOS 13+ reports a Mac UA — detect via touch points + WebKit.\n const isMacWebKit = /Macintosh/.test(ua) && /AppleWebKit/.test(ua) && !/Chrome/.test(ua);\n return isMacWebKit && ((navigator as { maxTouchPoints?: number }).maxTouchPoints ?? 0) > 1;\n}\n\nexport type StorageStatus = {\n /** Total quota granted to this origin, in MB (best-effort estimate). */\n quotaMB: number;\n /** Bytes currently used by this origin, in MB. */\n usageMB: number;\n /** quota − usage, in MB. */\n availableMB: number;\n /** Storage is persistent (exempt from eviction). On iOS this is effectively\n * only true once the site is installed to the Home Screen. */\n persisted: boolean;\n /** Running as an installed/standalone PWA. */\n installed: boolean;\n /** Platform is iOS/iPadOS (install is the quota unlock here). */\n ios: boolean;\n};\n\n/** Snapshot of the origin's storage situation — quota, usage, persistence, and\n * whether the app is installed. Use it to decide whether to recommend install\n * before downloading a large model. */\nexport async function getStorageStatus(): Promise<StorageStatus> {\n const installed = isStandalone();\n const ios = isIOS();\n let quotaMB = 0;\n let usageMB = 0;\n let persisted = false;\n try {\n const est = await navigator.storage?.estimate?.();\n quotaMB = Math.round((est?.quota || 0) / 1e6);\n usageMB = Math.round((est?.usage || 0) / 1e6);\n } catch {\n /* estimate unsupported */\n }\n try {\n persisted = (await navigator.storage?.persisted?.()) ?? false;\n } catch {\n /* persisted unsupported */\n }\n return {\n quotaMB,\n usageMB,\n availableMB: Math.max(0, quotaMB - usageMB),\n persisted,\n installed,\n ios,\n };\n}\n\n/**\n * Request persistent storage (exempt from eviction). Returns whether the origin\n * is persistent afterwards. Browsers grant this based on engagement/installation;\n * on iOS Safari it is effectively granted only to an installed (Home Screen) PWA,\n * so call this AND guide users to install when it returns false on iOS.\n */\nexport async function requestPersistentStorage(): Promise<boolean> {\n try {\n if (await navigator.storage?.persisted?.()) return true;\n return (await navigator.storage?.persist?.()) ?? false;\n } catch {\n return false;\n }\n}\n\nexport type ModelFit = {\n /** The model likely fits in the currently-available quota. */\n fits: boolean;\n availableMB: number;\n /** Caching durably would benefit from installing to the Home Screen — true when\n * not installed on iOS, or when the model doesn't fit the current quota. */\n recommendInstall: boolean;\n};\n\n/**\n * Estimate whether a model of `sizeMB` will cache in the current quota, and\n * whether you should recommend installing to the Home Screen first. Pair with a\n * one-time \"Install for offline use\" prompt before a large download on mobile.\n */\nexport async function canCacheModel(sizeMB: number): Promise<ModelFit> {\n const s = await getStorageStatus();\n // Headroom: leave ~10% slack so we don't recommend a download that just barely\n // fits and then fails mid-write.\n const fits = s.availableMB >= sizeMB * 1.1;\n const recommendInstall = (!fits || (s.ios && !s.installed)) && !s.persisted;\n return { fits, availableMB: s.availableMB, recommendInstall };\n}\n\n/**\n * Platform-appropriate install guidance. iOS Safari has NO programmatic install\n * prompt — installation is manual (Share → Add to Home Screen), so apps should\n * show these instructions. Other platforms (Android/Chrome) fire\n * `beforeinstallprompt`, which apps can capture for a one-tap button.\n */\nexport function getInstallGuidance(): { installed: boolean; manual: boolean; steps: string } {\n const installed = isStandalone();\n if (installed) return { installed: true, manual: false, steps: \"Already installed.\" };\n if (isIOS()) {\n return {\n installed: false,\n manual: true,\n steps:\n \"Tap the Share button, then 'Add to Home Screen'. Installing unlocks durable storage so models download once instead of every visit.\",\n };\n }\n return {\n installed: false,\n manual: false,\n steps:\n \"Use your browser's Install option (or the install icon in the address bar) to add this app for offline use and durable model storage.\",\n };\n}\n","// ============================================\n// Audio Playback Utilities\n// ============================================\n\n/**\n * Play audio from Float32Array using Web Audio API\n *\n * @example\n * ```ts\n * import { playAudio } from \"@tryhamster/gerbil/browser\";\n *\n * const audio = new Float32Array([...]); // TTS output\n * const controller = await playAudio(audio, 24000);\n *\n * // Stop playback\n * controller.stop();\n * ```\n */\nexport async function playAudio(\n audio: Float32Array,\n sampleRate: number = 24000,\n): Promise<{ stop: () => void; onEnded: Promise<void> }> {\n const audioContext = new AudioContext();\n\n // Resume if suspended\n if (audioContext.state === \"suspended\") {\n await audioContext.resume();\n }\n\n const audioBuffer = audioContext.createBuffer(1, audio.length, sampleRate);\n const channelData = new Float32Array(audio);\n audioBuffer.copyToChannel(channelData, 0);\n\n const sourceNode = audioContext.createBufferSource();\n sourceNode.buffer = audioBuffer;\n sourceNode.connect(audioContext.destination);\n\n const onEnded = new Promise<void>((resolve) => {\n sourceNode.onended = () => {\n audioContext.close();\n resolve();\n };\n });\n\n sourceNode.start();\n\n return {\n stop: () => {\n sourceNode.stop();\n audioContext.close();\n },\n onEnded,\n };\n}\n\n/**\n * Create a reusable audio player for streaming TTS\n *\n * @example\n * ```ts\n * import { createAudioPlayer } from \"@tryhamster/gerbil/browser\";\n *\n * const player = createAudioPlayer(24000);\n *\n * // Queue audio chunks as they arrive\n * player.queue(chunk1);\n * player.queue(chunk2);\n *\n * // Stop and clear\n * player.stop();\n * ```\n */\nexport function createAudioPlayer(sampleRate: number = 24000): {\n queue: (audio: Float32Array) => void;\n stop: () => void;\n isPlaying: () => boolean;\n} {\n let audioContext: AudioContext | null = null;\n let nextStartTime = 0;\n let isActive = false;\n\n const ensureContext = async () => {\n if (!audioContext) {\n audioContext = new AudioContext();\n }\n if (audioContext.state === \"suspended\") {\n await audioContext.resume();\n }\n return audioContext;\n };\n\n return {\n queue: async (audio: Float32Array) => {\n const ctx = await ensureContext();\n isActive = true;\n\n const buffer = ctx.createBuffer(1, audio.length, sampleRate);\n const channelData = new Float32Array(audio);\n buffer.copyToChannel(channelData, 0);\n\n const source = ctx.createBufferSource();\n source.buffer = buffer;\n source.connect(ctx.destination);\n\n // Schedule seamlessly after previous chunk\n const startTime = Math.max(ctx.currentTime, nextStartTime);\n source.start(startTime);\n nextStartTime = startTime + buffer.duration;\n\n source.onended = () => {\n if (ctx.currentTime >= nextStartTime - 0.1) {\n isActive = false;\n }\n };\n },\n\n stop: () => {\n isActive = false;\n nextStartTime = 0;\n if (audioContext) {\n audioContext.close();\n audioContext = null;\n }\n },\n\n isPlaying: () => isActive,\n };\n}\n","// ============================================\n// iOS Model Guards & Device Capability Detection\n// ============================================\n\n// ============================================\n// Real native-engine model ids\n// ============================================\n//\n// The website loads models by their actual repo id (MLX 4-bit on-device builds,\n// or the upstream Qwen/Liquid repos on desktop). The guard matches on those ids\n// — NOT the old ONNX-era shorthands — so it must stay in sync with the real\n// checkpoints the engine can run. Matching is substring-based on a normalized\n// id so both `mlx-community/Qwen3.5-0.8B-4bit` and `Qwen/Qwen3.5-0.8B` resolve.\n\n/** Recommended safe model ids per modality (used as fallbacks on mobile). */\nconst SAFE_MOBILE_CHAT = \"mlx-community/Qwen3.5-0.8B-4bit\";\n\n/**\n * Approximate on-device (INT4) memory footprint in MB for the models the native\n * engine actually ships. Used for memory-aware selection and messaging.\n */\nexport const MODEL_SIZES: Record<string, number> = {\n // Chat models (INT4, on-device)\n \"qwen3.5-0.8b\": 650, // ~0.65GB\n \"qwen3.5-2b\": 1700, // ~1.7GB\n \"gemma-4-e2b\": 3600, // ~3.6GB\n \"lfm2.5-350m\": 300, // ~0.3GB\n // TTS models\n \"kokoro-82m\": 350,\n \"supertonic-66m\": 300,\n // STT models\n \"whisper-tiny\": 150,\n \"whisper-tiny.en\": 150,\n \"whisper-small\": 500,\n // Embedding models\n \"all-minilm-l6-v2\": 100,\n};\n\n/**\n * Normalize a repo/model id to a lowercase token stream for substring matching\n * (strips org prefixes' punctuation while preserving the model name tokens).\n */\nfunction normalizeId(modelId: string): string {\n return modelId.toLowerCase().replace(/[^a-z0-9]/g, \"-\");\n}\n\n/**\n * iOS (WKWebView) model classification keyed off the REAL native-engine ids.\n *\n * - blocked: too large for the WKWebView memory ceiling on iPhone — will crash.\n * gemma-4-e2b (~3.6GB) plus any vision checkpoint (the vision encoder pushes\n * the working set well past what an iPhone can hold).\n * - risky: borderline on iPhone — Qwen3.5-2B (~1.7GB) fits on newer devices but\n * warns and can OOM on older ones.\n * - everything else (Qwen3.5-0.8B ~0.65GB, LFM2.5-350M) is allowed everywhere.\n */\nconst IOS_MODEL_LIMITS = {\n /** Substrings (normalized) of ids that HARD-BLOCK on iPhone. */\n blocked: [\"gemma-4-e2b\", \"gemma-4-e4b\"],\n /** Substrings that mark a vision checkpoint (blocked on iPhone). */\n visionMarkers: [\"vision\", \"-vl-\", \"-vl\", \"vlm\", \"image-text\", \"-it-vision\"],\n /** Substrings of ids that WARN on iPhone (Qwen3.5-2B class). */\n risky: [\"qwen3.5-2b\", \"qwen3-5-2b\"],\n /** Maximum total memory budget in MB for iOS WKWebView. */\n maxBudgetMB: 1800,\n} as const;\n\n/**\n * Check if a model is safe to load on the current device.\n * Returns guidance specific to iOS memory constraints. Matches on the real\n * native-engine repo ids (MLX 4-bit / upstream Qwen / Liquid).\n */\nexport function isModelSafeForDevice(modelId: string): {\n safe: boolean;\n reason: string;\n recommendation?: string;\n maxSafeModel?: string;\n} {\n const ua = typeof navigator !== \"undefined\" ? navigator.userAgent : \"\";\n const isIPhone = /iPhone|iPod/.test(ua);\n const isIPad = /iPad/.test(ua);\n const isIOS = isIPhone || isIPad;\n const isIOSChrome = isIOS && /CriOS/.test(ua);\n const normalizedId = normalizeId(modelId);\n\n const isVision = IOS_MODEL_LIMITS.visionMarkers.some((m) =>\n normalizedId.includes(normalizeId(m)),\n );\n const isBlocked =\n IOS_MODEL_LIMITS.blocked.some((m) => normalizedId.includes(normalizeId(m))) || isVision;\n const isRisky = IOS_MODEL_LIMITS.risky.some((m) => normalizedId.includes(normalizeId(m)));\n\n // iPhone is the hard constraint (smallest WKWebView budget). iPad has more\n // headroom, so only the genuinely huge / vision checkpoints are blocked there.\n if (isIPhone) {\n if (isBlocked) {\n const browserNote = isIOSChrome ? \" (iOS Chrome uses WKWebView, same limits as Safari)\" : \"\";\n const why = isVision\n ? \"Vision checkpoints need a separate image encoder in memory\"\n : \"It is too large (~3.6GB)\";\n return {\n safe: false,\n reason: `Model ${modelId} will crash on iPhone${browserNote}. ${why}, which exceeds the WKWebView memory ceiling.`,\n recommendation: `Use ${SAFE_MOBILE_CHAT} (Qwen3.5-0.8B) on iPhone, or run larger models on desktop.`,\n maxSafeModel: SAFE_MOBILE_CHAT,\n };\n }\n if (isRisky) {\n return {\n safe: true,\n reason: `Model ${modelId} (~1.7GB) is borderline on iPhone and may run out of memory on older devices.`,\n recommendation: `If it crashes, fall back to ${SAFE_MOBILE_CHAT} (Qwen3.5-0.8B).`,\n maxSafeModel: SAFE_MOBILE_CHAT,\n };\n }\n return { safe: true, reason: \"Model is within iPhone memory limits.\" };\n }\n\n if (isIPad) {\n // iPad tolerates the 2B class; only block the huge / vision checkpoints.\n if (isBlocked) {\n const why = isVision\n ? \"Vision checkpoints need a separate image encoder in memory\"\n : \"It is too large (~3.6GB)\";\n return {\n safe: false,\n reason: `Model ${modelId} may crash on iPad. ${why}, which can exceed the WKWebView memory ceiling.`,\n recommendation: `Use ${SAFE_MOBILE_CHAT} (Qwen3.5-0.8B) or Qwen3.5-2B on iPad.`,\n maxSafeModel: \"mlx-community/Qwen3.5-2B-4bit\",\n };\n }\n return { safe: true, reason: \"Model is within iPad memory limits.\" };\n }\n\n // Android - block the huge / vision checkpoints, allow the rest.\n const isAndroid = /Android/.test(ua);\n if (isAndroid && isBlocked) {\n return {\n safe: false,\n reason: `Model ${modelId} is very large and may crash on Android devices.`,\n recommendation: `Use ${SAFE_MOBILE_CHAT} (Qwen3.5-0.8B) or Qwen3.5-2B on Android.`,\n maxSafeModel: \"mlx-community/Qwen3.5-2B-4bit\",\n };\n }\n\n // Desktop - all models are safe.\n return { safe: true, reason: \"Desktop browser has sufficient memory.\" };\n}\n\n/**\n * Get recommended models based on device memory and capabilities.\n * Helps prevent OOM crashes on low-memory mobile devices.\n */\nexport function getRecommendedModels(): {\n chat: string;\n tts: string;\n stt: string;\n embedding: string;\n reason: string;\n deviceMemory: number | null;\n isMobile: boolean;\n} {\n const ua = typeof navigator !== \"undefined\" ? navigator.userAgent : \"\";\n const deviceMemory = typeof navigator !== \"undefined\" ? (navigator as any).deviceMemory : null;\n const isMobile = /iPhone|iPad|iPod|Android|Mobile/.test(ua);\n\n // Estimate available memory (deviceMemory reports total GB, not available)\n // Mobile devices typically have less free memory due to OS overhead\n const effectiveMemory = deviceMemory ? (isMobile ? deviceMemory * 0.4 : deviceMemory * 0.6) : 4;\n const availableMB = effectiveMemory * 1024;\n\n let chat: string;\n let reason: string;\n\n if (availableMB < 600) {\n chat = \"LiquidAI/LFM2.5-350M\";\n reason = \"Very low memory device - using smallest model (LFM2.5-350M)\";\n } else if (isMobile && availableMB < 2200) {\n chat = SAFE_MOBILE_CHAT;\n reason = \"Mobile device - using Qwen3.5-0.8B to stay within the WKWebView memory limit\";\n } else if (availableMB < 2200) {\n chat = SAFE_MOBILE_CHAT;\n reason = \"Standard model for moderate memory (Qwen3.5-0.8B)\";\n } else {\n chat = \"mlx-community/Qwen3.5-2B-4bit\";\n reason = \"High memory available - using Qwen3.5-2B for better quality\";\n }\n\n return {\n chat,\n tts: \"kokoro-82m\",\n stt: \"whisper-tiny.en\",\n embedding: \"all-MiniLM-L6-v2\",\n reason,\n deviceMemory,\n isMobile,\n };\n}\n\n// ============================================\n// Session Phase Tracking (Reload Detection)\n// ============================================\n\ntype DownloadPhase = \"idle\" | \"downloading\" | \"caching\" | \"initializing\" | \"ready\" | \"error\";\n\nexport const SESSION_STORAGE_KEY = \"gerbil_session_phase\";\n\nexport type SessionState = {\n phase: DownloadPhase;\n modelId: string | null;\n sessionId: string;\n timestamp: number;\n bytesDownloaded?: number;\n totalBytes?: number;\n};\n\n/**\n * Generate a unique session ID for tracking across reloads.\n */\nfunction generateSessionId(): string {\n return `${Date.now()}-${Math.random().toString(36).slice(2, 9)}`;\n}\n\n/**\n * Get or create the current session ID.\n */\nfunction getSessionId(): string {\n if (typeof localStorage === \"undefined\") return generateSessionId();\n\n let sessionId = sessionStorage.getItem(\"gerbil_session_id\");\n if (!sessionId) {\n sessionId = generateSessionId();\n sessionStorage.setItem(\"gerbil_session_id\", sessionId);\n }\n return sessionId;\n}\n\n/**\n * Set the current download/initialization phase.\n * Used to detect if a reload happened during a critical operation.\n */\nexport function setDownloadPhase(\n phase: DownloadPhase,\n modelId?: string,\n progress?: { bytesDownloaded: number; totalBytes: number },\n): void {\n if (typeof localStorage === \"undefined\") return;\n\n const state: SessionState = {\n phase,\n modelId: modelId || null,\n sessionId: getSessionId(),\n timestamp: Date.now(),\n bytesDownloaded: progress?.bytesDownloaded,\n totalBytes: progress?.totalBytes,\n };\n\n localStorage.setItem(SESSION_STORAGE_KEY, JSON.stringify(state));\n}\n\n/**\n * Get the last known download phase from storage.\n */\nexport function getDownloadPhase(): SessionState | null {\n if (typeof localStorage === \"undefined\") return null;\n\n try {\n const raw = localStorage.getItem(SESSION_STORAGE_KEY);\n if (!raw) return null;\n return JSON.parse(raw) as SessionState;\n } catch {\n return null;\n }\n}\n\n/**\n * Detect if the page reloaded during a model download/initialization.\n * This typically indicates an iOS memory crash.\n *\n * @returns Detection result with recommended action\n */\nexport function detectMemoryCrash(): {\n crashed: boolean;\n phase?: DownloadPhase;\n modelId?: string;\n timeSinceCrash?: number;\n recommendation?: string;\n} {\n const lastState = getDownloadPhase();\n const currentSessionId = getSessionId();\n\n if (!lastState) {\n return { crashed: false };\n }\n\n // If session ID changed and we were in a critical phase, it's likely a crash/reload\n const criticalPhases: DownloadPhase[] = [\"downloading\", \"caching\", \"initializing\"];\n const wasInCriticalPhase = criticalPhases.includes(lastState.phase);\n const sessionChanged = lastState.sessionId !== currentSessionId;\n const timeSinceCrash = Date.now() - lastState.timestamp;\n\n // Only count as crash if it happened recently (within 5 minutes) and session changed\n const recentCrash = timeSinceCrash < 5 * 60 * 1000;\n\n if (wasInCriticalPhase && sessionChanged && recentCrash) {\n // Clear the state to prevent repeated detection\n localStorage.removeItem(SESSION_STORAGE_KEY);\n\n return {\n crashed: true,\n phase: lastState.phase,\n modelId: lastState.modelId || undefined,\n timeSinceCrash,\n recommendation:\n lastState.modelId && /2b|gemma|vision/i.test(lastState.modelId)\n ? \"The model was too large for your device. Try Qwen3.5-0.8B instead.\"\n : \"Your device ran out of memory. Try a smaller model or use a desktop browser.\",\n };\n }\n\n return { crashed: false };\n}\n\n/**\n * Clear session phase (call when model loads successfully).\n */\nexport function clearDownloadPhase(): void {\n if (typeof localStorage === \"undefined\") return;\n localStorage.removeItem(SESSION_STORAGE_KEY);\n}\n\n// ============================================\n// WebKit Submit-Granularity (group size) Probe\n// ============================================\n//\n// On WebKit/iOS WebGPU, decode speed is bound by GPU round-trips: the executor\n// groups `webkitGroupSize` dispatches per command buffer, then submits + drains\n// (queue.onSubmittedWorkDone) per group. Larger groups amortize the drains and\n// can be ~5x faster on iPad. BUT the safe ceiling is device-dependent in three\n// classes:\n// 1. Newer WebKit (iPad 26.5): high group is correct + fast.\n// 2. Older WebKit: high group yields zero/garbage logits (wrong output).\n// 3. iPhone (iOS 18.7): high group HARD-CRASHES the GPU process (page dies).\n//\n// The crash class is the hard one: a bad group kills the page before any code\n// can record the result. So we use a localStorage BREADCRUMB that OUTLIVES the\n// page kill: we persist the candidate we're about to try BEFORE running any\n// GPU-heavy work. If the page survives, a later promotion clears `trying` and\n// records it as known-good. If the page crashed, `trying` is still set on the\n// next load, so we KNOW that rung crashed and cap below it.\n//\n// The probe escalates UP from the safe floor (never starts optimistic — a\n// first-visit crash is a terrible UX) and persists per-device in localStorage.\n\nexport const WEBKIT_GROUP_PROBE_KEY = \"gerbil-webkit-group-v1\";\n\n/**\n * Candidate group-size ladder. We escalate UP one rung per page load. 128 is\n * treated as effectively \"batch-all\" for this model — kept simple on purpose.\n */\nexport const WEBKIT_GROUP_LADDER = [1, 8, 32, 64, 128] as const;\n\nexport type WebkitGroupProbe = {\n /** Largest group size proven crash-free AND correct on this device. Starts at 1. */\n knownGood: number;\n /** Candidate being attempted this page-load (null when not probing). */\n trying: number | null;\n /** True once we hit a crash/incorrect ceiling and should stop escalating. */\n capped: boolean;\n};\n\nconst DEFAULT_PROBE: WebkitGroupProbe = { knownGood: 1, trying: null, capped: false };\n\n/** Read the persisted WebKit group probe record (guarded; safe on node). */\nexport function readGroupProbe(): WebkitGroupProbe {\n if (typeof localStorage === \"undefined\") return { ...DEFAULT_PROBE };\n try {\n const raw = localStorage.getItem(WEBKIT_GROUP_PROBE_KEY);\n if (!raw) return { ...DEFAULT_PROBE };\n const parsed = JSON.parse(raw) as Partial<WebkitGroupProbe>;\n return {\n knownGood:\n typeof parsed.knownGood === \"number\" && parsed.knownGood >= 1 ? parsed.knownGood : 1,\n trying: typeof parsed.trying === \"number\" ? parsed.trying : null,\n capped: parsed.capped === true,\n };\n } catch {\n return { ...DEFAULT_PROBE };\n }\n}\n\n/** Persist the WebKit group probe record (guarded; no-op on node). */\nexport function writeGroupProbe(rec: WebkitGroupProbe): void {\n if (typeof localStorage === \"undefined\") return;\n try {\n localStorage.setItem(WEBKIT_GROUP_PROBE_KEY, JSON.stringify(rec));\n } catch {\n // Best-effort; private mode / quota can throw.\n }\n}\n\n/**\n * The validated non-phone sweet spot. iPad swept 1→7.9, 8→19, 32→24.8, 64→26.6,\n * 128→26.9 (peak), 256→26.2 tok/s — a plateau from ~64 up, so 128 is the best\n * stable target (more batching just costs memory). Non-phone WebKit jumps here\n * directly; the crash breadcrumb caps it down if a device can't sustain it.\n */\nconst NONPHONE_TARGET_GROUP = 128;\n\n/**\n * Resolve the WebKit group size to use this session, recording `trying` as a\n * side effect so a crash this load is detectable on the next load.\n *\n * Algorithm (only meaningful on WebKit; inert otherwise):\n * 1. Read the record (default {knownGood:1, trying:null, capped:false}).\n * 2. If `trying !== null` on entry → the previous load set it but never cleared\n * it → that load CRASHED at `trying`. Cap there, keep `knownGood`, clear\n * `trying`. Use `knownGood` this session.\n * 3. Else if !capped and there is a rung above `knownGood` → set `trying = next`,\n * persist BEFORE any GPU work, and use it (we're escalating).\n * 4. Else → use `knownGood`.\n *\n * @returns the group size to use this session.\n */\nexport function resolveWebkitGroupSize(args: {\n override?: number;\n isWebKit: boolean;\n /**\n * Memory-constrained devices (phones) are the \"crash class\": batching crashes\n * the GPU process, and discovering that costs one user-visible crash before the\n * breadcrumb caps it. When `conservative` is set we never auto-escalate — the\n * device stays at its proven floor (group=1 on first run) and never risks that\n * calibration crash. `?group=N` still lets such a device opt in explicitly.\n */\n conservative?: boolean;\n}): number {\n // Explicit ?group=N override wins and skips the probe entirely.\n if (args.override && args.override > 0) return args.override;\n // Non-WebKit (desktop / Dawn / node) never groups — floor of 1, no bookkeeping.\n if (!args.isWebKit) return 1;\n\n const rec = readGroupProbe();\n\n // Step 2: a stale `trying` means last load crashed at that rung.\n if (rec.trying !== null) {\n const crashedAt = rec.trying;\n const next: WebkitGroupProbe = {\n knownGood: rec.knownGood,\n trying: null,\n capped: true,\n };\n writeGroupProbe(next);\n console.log(\n `[engine] webkit group probe: previous load crashed at group=${crashedAt} → capping at knownGood=${next.knownGood}`,\n );\n return next.knownGood;\n }\n\n // Step 3: non-phone WebKit (iPad / Mac Safari) jumps straight to the validated\n // sweet spot (group=128) instead of climbing rung-by-rung — these devices\n // tolerate batching (iPad ran every rung to 256 coherently), so there's no\n // reason to crawl. The breadcrumb (Step 2) caps it down if a particular device\n // can't sustain it. Phones (conservative) NEVER escalate — they stay at the\n // proven floor (group=1) to avoid the one calibration crash batching costs on\n // the crash class.\n if (!rec.capped && !args.conservative && rec.knownGood < NONPHONE_TARGET_GROUP) {\n const next: WebkitGroupProbe = {\n knownGood: rec.knownGood,\n trying: NONPHONE_TARGET_GROUP,\n capped: false,\n };\n writeGroupProbe(next); // PERSIST BEFORE any GPU-heavy work runs\n console.log(\n `[engine] webkit group probe: trying target group=${NONPHONE_TARGET_GROUP} (knownGood=${rec.knownGood})`,\n );\n return NONPHONE_TARGET_GROUP;\n }\n\n // Step 4: nothing to escalate (capped or at top rung) — use the proven floor.\n console.log(\n `[engine] webkit group probe: knownGood=${rec.knownGood} trying=null capped=${rec.capped} → using ${rec.knownGood}`,\n );\n return rec.knownGood;\n}\n\n/**\n * Promote (or cap) the WebKit group probe after the first successful forward.\n *\n * Call this once per page-load, after the model has loaded AND a first forward\n * completed without the page dying. The breadcrumb already handles the crash\n * class (the page death leaves `trying` set for the next load); this handles the\n * wrong-output class and records success.\n *\n * @param correct true if the first forward produced non-corrupt output.\n * - correct → promote: knownGood = trying, trying = null.\n * - incorrect → cap: keep knownGood at the prior rung, trying = null, capped.\n */\nexport function promoteGroupProbe(correct: boolean): void {\n if (typeof localStorage === \"undefined\") return;\n const rec = readGroupProbe();\n // Nothing was being attempted this load — nothing to promote.\n if (rec.trying === null) return;\n\n if (correct) {\n writeGroupProbe({ knownGood: rec.trying, trying: null, capped: rec.capped });\n console.log(`[engine] webkit group probe: PROMOTED group=${rec.trying} to known-good`);\n } else {\n // Wrong-output class: this rung is incorrect (but did not crash). Keep the\n // prior knownGood and stop escalating.\n writeGroupProbe({ knownGood: rec.knownGood, trying: null, capped: true });\n console.log(\n `[engine] webkit group probe: group=${rec.trying} produced INCORRECT output → capping at knownGood=${rec.knownGood}`,\n );\n }\n}\n","import { setDownloadPhase } from \"./device-guards.js\";\n\n// ============================================\n// Chunked Resumable Downloader\n// ============================================\n\n/** Chunk size for downloads: 1.5MB (safe for iOS IndexedDB transactions) */\nexport const CHUNK_SIZE_BYTES = 1.5 * 1024 * 1024;\n\n/** IndexedDB database name for chunked downloads */\nexport const DOWNLOAD_DB_NAME = \"gerbil-model-chunks\";\nconst DOWNLOAD_DB_VERSION = 1;\n\n/**\n * Manifest stored in IndexedDB to track download progress.\n */\ntype DownloadManifest = {\n modelId: string;\n url: string;\n etag: string | null;\n totalBytes: number;\n chunkSize: number;\n completedChunks: number[];\n createdAt: number;\n updatedAt: number;\n};\n\n/**\n * Open (or create) the IndexedDB for chunked downloads.\n */\nasync function openDownloadDB(): Promise<IDBDatabase> {\n return new Promise((resolve, reject) => {\n const request = indexedDB.open(DOWNLOAD_DB_NAME, DOWNLOAD_DB_VERSION);\n\n request.onerror = () =>\n reject(new Error(`Failed to open download DB: ${request.error?.message}`));\n\n request.onsuccess = () => resolve(request.result);\n\n request.onupgradeneeded = (event) => {\n const db = (event.target as IDBOpenDBRequest).result;\n\n // Store for download manifests\n if (!db.objectStoreNames.contains(\"manifests\")) {\n db.createObjectStore(\"manifests\", { keyPath: \"modelId\" });\n }\n\n // Store for actual chunks (key: modelId-chunkIndex)\n if (!db.objectStoreNames.contains(\"chunks\")) {\n db.createObjectStore(\"chunks\");\n }\n };\n });\n}\n\n/**\n * Get download manifest for a model.\n */\nasync function getManifest(db: IDBDatabase, modelId: string): Promise<DownloadManifest | null> {\n return new Promise((resolve, reject) => {\n const tx = db.transaction(\"manifests\", \"readonly\");\n const store = tx.objectStore(\"manifests\");\n const request = store.get(modelId);\n\n request.onerror = () => reject(new Error(`Failed to get manifest: ${request.error?.message}`));\n request.onsuccess = () => resolve(request.result || null);\n });\n}\n\n/**\n * Save download manifest.\n */\nasync function saveManifest(db: IDBDatabase, manifest: DownloadManifest): Promise<void> {\n return new Promise((resolve, reject) => {\n const tx = db.transaction(\"manifests\", \"readwrite\");\n const store = tx.objectStore(\"manifests\");\n const request = store.put(manifest);\n\n request.onerror = () => reject(new Error(`Failed to save manifest: ${request.error?.message}`));\n request.onsuccess = () => resolve();\n });\n}\n\n/**\n * Save a single chunk.\n */\nasync function saveChunk(\n db: IDBDatabase,\n modelId: string,\n chunkIndex: number,\n data: ArrayBuffer,\n): Promise<void> {\n return new Promise((resolve, reject) => {\n const tx = db.transaction(\"chunks\", \"readwrite\");\n const store = tx.objectStore(\"chunks\");\n const key = `${modelId}-${chunkIndex}`;\n const request = store.put(data, key);\n\n request.onerror = () =>\n reject(new Error(`Failed to save chunk ${chunkIndex}: ${request.error?.message}`));\n request.onsuccess = () => resolve();\n });\n}\n\n/**\n * Get a single chunk.\n */\nasync function getChunk(\n db: IDBDatabase,\n modelId: string,\n chunkIndex: number,\n): Promise<ArrayBuffer | null> {\n return new Promise((resolve, reject) => {\n const tx = db.transaction(\"chunks\", \"readonly\");\n const store = tx.objectStore(\"chunks\");\n const key = `${modelId}-${chunkIndex}`;\n const request = store.get(key);\n\n request.onerror = () =>\n reject(new Error(`Failed to get chunk ${chunkIndex}: ${request.error?.message}`));\n request.onsuccess = () => resolve(request.result || null);\n });\n}\n\n/**\n * Delete all chunks and manifest for a model.\n */\nasync function clearModelData(db: IDBDatabase, modelId: string): Promise<void> {\n // Get manifest to know how many chunks to delete\n const manifest = await getManifest(db, modelId);\n\n return new Promise((resolve, reject) => {\n const tx = db.transaction([\"manifests\", \"chunks\"], \"readwrite\");\n\n // Delete manifest\n tx.objectStore(\"manifests\").delete(modelId);\n\n // Delete all chunks\n if (manifest) {\n const totalChunks = Math.ceil(manifest.totalBytes / manifest.chunkSize);\n const chunkStore = tx.objectStore(\"chunks\");\n for (let i = 0; i < totalChunks; i++) {\n chunkStore.delete(`${modelId}-${i}`);\n }\n }\n\n tx.oncomplete = () => resolve();\n tx.onerror = () => reject(new Error(`Failed to clear model data: ${tx.error?.message}`));\n });\n}\n\n/**\n * Chunked resumable downloader for large model files.\n * Downloads in 1.5MB chunks to avoid iOS memory pressure.\n */\nexport async function downloadModelChunked(\n url: string,\n modelId: string,\n options: {\n onProgress?: (info: {\n phase: string;\n bytesDownloaded: number;\n totalBytes: number;\n percent: number;\n }) => void;\n signal?: AbortSignal;\n } = {},\n): Promise<ArrayBuffer> {\n const { onProgress, signal } = options;\n\n // Update session phase\n setDownloadPhase(\"downloading\", modelId);\n\n const db = await openDownloadDB();\n\n try {\n // Check for existing manifest\n let manifest = await getManifest(db, modelId);\n\n // Fetch headers to get content-length and etag\n const headResponse = await fetch(url, { method: \"HEAD\", signal });\n if (!headResponse.ok) {\n throw new Error(`HEAD request failed: ${headResponse.status} ${headResponse.statusText}`);\n }\n\n const contentLength = parseInt(headResponse.headers.get(\"content-length\") || \"0\", 10);\n const etag = headResponse.headers.get(\"etag\");\n const acceptRanges = headResponse.headers.get(\"accept-ranges\");\n\n if (!contentLength) {\n throw new Error(\"Server did not provide content-length\");\n }\n\n // Check if we need to restart (etag mismatch means model updated)\n if (manifest && manifest.etag !== etag) {\n console.warn(`Model ${modelId} has been updated (etag mismatch). Clearing cached chunks.`);\n await clearModelData(db, modelId);\n manifest = null;\n }\n\n // Check if server supports range requests\n const supportsRange = acceptRanges === \"bytes\";\n\n if (!supportsRange) {\n // Fall back to regular download\n console.warn(`Server doesn't support range requests for ${modelId}. Using regular download.`);\n db.close();\n\n const response = await fetch(url, { signal });\n if (!response.ok) throw new Error(`Download failed: ${response.status}`);\n\n setDownloadPhase(\"caching\", modelId);\n const buffer = await response.arrayBuffer();\n setDownloadPhase(\"ready\", modelId);\n return buffer;\n }\n\n // Create or update manifest\n const totalChunks = Math.ceil(contentLength / CHUNK_SIZE_BYTES);\n\n if (!manifest) {\n manifest = {\n modelId,\n url,\n etag,\n totalBytes: contentLength,\n chunkSize: CHUNK_SIZE_BYTES,\n completedChunks: [],\n createdAt: Date.now(),\n updatedAt: Date.now(),\n };\n await saveManifest(db, manifest);\n }\n\n // Download missing chunks\n for (let i = 0; i < totalChunks; i++) {\n if (signal?.aborted) {\n throw new Error(\"Download aborted\");\n }\n\n // Skip already downloaded chunks\n if (manifest.completedChunks.includes(i)) {\n const bytesDownloaded = (manifest.completedChunks.length / totalChunks) * contentLength;\n onProgress?.({\n phase: \"resuming\",\n bytesDownloaded,\n totalBytes: contentLength,\n percent: Math.round((bytesDownloaded / contentLength) * 100),\n });\n continue;\n }\n\n const start = i * CHUNK_SIZE_BYTES;\n const end = Math.min(start + CHUNK_SIZE_BYTES - 1, contentLength - 1);\n\n // Download chunk with Range header\n const response = await fetch(url, {\n headers: { Range: `bytes=${start}-${end}` },\n signal,\n });\n\n if (response.status !== 206) {\n throw new Error(`Range request failed: ${response.status} (expected 206)`);\n }\n\n const chunkData = await response.arrayBuffer();\n\n // Save chunk to IndexedDB\n await saveChunk(db, modelId, i, chunkData);\n\n // Update manifest\n manifest.completedChunks.push(i);\n manifest.updatedAt = Date.now();\n await saveManifest(db, manifest);\n\n // Update session phase with progress\n const bytesDownloaded = manifest.completedChunks.length * CHUNK_SIZE_BYTES;\n setDownloadPhase(\"downloading\", modelId, { bytesDownloaded, totalBytes: contentLength });\n\n onProgress?.({\n phase: \"downloading\",\n bytesDownloaded: Math.min(bytesDownloaded, contentLength),\n totalBytes: contentLength,\n percent: Math.round((manifest.completedChunks.length / totalChunks) * 100),\n });\n\n // Null out reference to allow GC before next chunk\n // @ts-expect-error - intentional null for GC\n response.body = null;\n }\n\n // All chunks downloaded - reassemble\n setDownloadPhase(\"caching\", modelId);\n onProgress?.({\n phase: \"assembling\",\n bytesDownloaded: contentLength,\n totalBytes: contentLength,\n percent: 100,\n });\n\n // Assemble chunks into final buffer\n // We do this incrementally to avoid holding all chunks in memory at once\n const finalBuffer = new ArrayBuffer(contentLength);\n const finalView = new Uint8Array(finalBuffer);\n\n for (let i = 0; i < totalChunks; i++) {\n const chunk = await getChunk(db, modelId, i);\n if (!chunk) {\n throw new Error(`Missing chunk ${i} during assembly`);\n }\n\n const offset = i * CHUNK_SIZE_BYTES;\n finalView.set(new Uint8Array(chunk), offset);\n\n // Null out chunk reference immediately after copying\n // This helps GC reclaim memory on iOS\n }\n\n // Clean up - delete chunks now that we have the full file\n await clearModelData(db, modelId);\n db.close();\n\n setDownloadPhase(\"ready\", modelId);\n return finalBuffer;\n } catch (error) {\n setDownloadPhase(\"error\", modelId);\n db.close();\n throw error;\n }\n}\n\n/**\n * Check if a model has an incomplete download.\n */\nexport async function hasIncompleteDownload(modelId: string): Promise<{\n incomplete: boolean;\n bytesDownloaded?: number;\n totalBytes?: number;\n percent?: number;\n}> {\n try {\n const db = await openDownloadDB();\n const manifest = await getManifest(db, modelId);\n db.close();\n\n if (!manifest) {\n return { incomplete: false };\n }\n\n const totalChunks = Math.ceil(manifest.totalBytes / manifest.chunkSize);\n const completedChunks = manifest.completedChunks.length;\n\n if (completedChunks < totalChunks) {\n return {\n incomplete: true,\n bytesDownloaded: completedChunks * manifest.chunkSize,\n totalBytes: manifest.totalBytes,\n percent: Math.round((completedChunks / totalChunks) * 100),\n };\n }\n\n return { incomplete: false };\n } catch {\n return { incomplete: false };\n }\n}\n\n/**\n * Clear incomplete download data for a model.\n */\nexport async function clearIncompleteDownload(modelId: string): Promise<void> {\n try {\n const db = await openDownloadDB();\n await clearModelData(db, modelId);\n db.close();\n } catch {\n // Ignore errors\n }\n}\n\n/**\n * Check if there's enough storage quota for a model download.\n * Returns estimated available space and whether download should proceed.\n */\nexport async function checkStorageQuota(requiredMB: number = 500): Promise<{\n ok: boolean;\n availableMB: number;\n usedMB: number;\n quotaMB: number;\n message?: string;\n}> {\n if (typeof navigator === \"undefined\" || !navigator.storage?.estimate) {\n return {\n ok: true,\n availableMB: -1,\n usedMB: -1,\n quotaMB: -1,\n message: \"Storage API not available\",\n };\n }\n\n try {\n const { quota, usage } = await navigator.storage.estimate();\n const quotaMB = Math.round((quota || 0) / 1_000_000);\n const usedMB = Math.round((usage || 0) / 1_000_000);\n const availableMB = quotaMB - usedMB;\n\n if (availableMB < requiredMB) {\n return {\n ok: false,\n availableMB,\n usedMB,\n quotaMB,\n message: `Need ${requiredMB}MB but only ${availableMB}MB available. Clear browser data or free up space.`,\n };\n }\n\n return { ok: true, availableMB, usedMB, quotaMB };\n } catch (e: any) {\n return {\n ok: true, // Proceed optimistically if we can't check\n availableMB: -1,\n usedMB: -1,\n quotaMB: -1,\n message: `Storage check failed: ${e.message}`,\n };\n }\n}\n"],"mappings":";AA4BA,MAAaA,iBAA8C;CACzD,gBAAgB;EACd,IAAI;EACJ,MAAM;EACN,aACE;EACF,MAAM;EACN,eAAe;EACf,kBAAkB;EAClB,cAAc;EACd,gBAAgB;EAChB,QAAQ;EACT;CACD,cAAc;EACZ,IAAI;EACJ,MAAM;EACN,aACE;EACF,MAAM;EACN,eAAe;EACf,kBAAkB;EAClB,cAAc;EACd,gBAAgB;EAChB,QAAQ;EACT;CACD,wBAAwB;EACtB,IAAI;EACJ,MAAM;EACN,aAAa;EACb,MAAM;EACN,eAAe;EACf,kBAAkB;EAClB,cAAc;EACd,QAAQ;EACT;CACF;;;;;;;;;;;;;;;;;;;;;;;;;;ACxCD,SAAgB,eAAwB;AACtC,KAAI,OAAO,WAAW,YAAa,QAAO;CAE1C,MAAM,gBAAiB,UAAuC,eAAe;CAC7E,MAAM,oBACJ,OAAO,OAAO,eAAe,cAC7B,OAAO,WAAW,6BAA6B,CAAC;AAClD,QAAO,iBAAiB;;;;;AAM1B,SAAgB,QAAiB;AAC/B,KAAI,OAAO,cAAc,YAAa,QAAO;CAC7C,MAAM,KAAK,UAAU,aAAa;AAClC,KAAI,mBAAmB,KAAK,GAAG,CAAE,QAAO;AAGxC,QADoB,YAAY,KAAK,GAAG,IAAI,cAAc,KAAK,GAAG,IAAI,CAAC,SAAS,KAAK,GAAG,KAChE,U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
|
1
|
+
{"version":3,"file":"index.js","names":["BUILTIN_MODELS: Record<string, ModelConfig>","audioContext: AudioContext | null","MODEL_SIZES: Record<string, number>","chat: string","reason: string","state: SessionState","bytesDownloaded","e: any"],"sources":["../../src/core/models.ts","../../src/browser/pwa.ts","../../src/browser/audio.ts","../../src/browser/device-guards.ts","../../src/browser/download.ts"],"sourcesContent":["/**\n * Model Registry\n *\n * Supports built-in models and any HuggingFace model via hf:org/model syntax\n */\n\nimport type { ModelConfig, ModelSource } from \"./types.js\";\n\n// ============================================\n// Canonical default model\n// ============================================\n\n/**\n * The default model used everywhere a model id is not explicitly provided\n * (CLI flags, REPL, framework adapters, integrations, one-liner). This is the\n * e2e-validated model; reference this constant instead of hard-coding the id.\n */\nexport const DEFAULT_MODEL = \"qwen3.5-0.8b\";\n\n// ============================================\n// Built-in Models (curated & tested)\n// ============================================\n\n// Every entry is a standard HuggingFace safetensors repo whose architecture the\n// native WebGPU engine supports (Qwen2/Qwen3/Qwen3.5, LFM2 — see\n// src/gpu/architectures/index.ts). The engine quantizes weights to INT4 on load;\n// `size` is the bf16/fp16 download size. Only add a repo here whose architecture\n// has a graph generator in the registry.\nexport const BUILTIN_MODELS: Record<string, ModelConfig> = {\n \"qwen3.5-0.8b\": {\n id: \"qwen3.5-0.8b\",\n repo: \"Qwen/Qwen3.5-0.8B\",\n description:\n \"Qwen3.5 0.8B - Fast, multimodal (vision), 262k context, supports thinking (default)\",\n size: \"~1.6GB\",\n contextLength: 262_144,\n supportsThinking: true,\n supportsJson: true,\n supportsVision: true,\n family: \"qwen\",\n },\n \"qwen3.5-2b\": {\n id: \"qwen3.5-2b\",\n repo: \"Qwen/Qwen3.5-2B\",\n description:\n \"Qwen3.5 2B - Higher quality, multimodal (vision), 262k context, supports thinking\",\n size: \"~4GB\",\n contextLength: 262_144,\n supportsThinking: true,\n supportsJson: true,\n supportsVision: true,\n family: \"qwen\",\n },\n \"lfm2.5-1.2b-thinking\": {\n id: \"lfm2.5-1.2b-thinking\",\n repo: \"LiquidAI/LFM2.5-1.2B-Thinking\",\n description: \"LFM2.5 1.2B Thinking - Efficient reasoning model, 128k context\",\n size: \"~2.4GB\",\n contextLength: 128_000,\n supportsThinking: true,\n supportsJson: false,\n family: \"other\",\n },\n};\n\n// ============================================\n// Model Resolution\n// ============================================\n\n/**\n * Parse model identifier and resolve to source\n *\n * Supported formats:\n * - \"qwen3.5-0.8b\" (built-in)\n * - \"hf:org/model\" (HuggingFace shorthand)\n * - \"https://huggingface.co/org/model\" (full URL)\n * - \"file:./path/to/model\" (local path)\n */\nexport function resolveModel(modelId: string): ModelSource {\n // Built-in model\n if (BUILTIN_MODELS[modelId]) {\n return {\n type: \"builtin\",\n path: BUILTIN_MODELS[modelId].repo,\n };\n }\n\n // HuggingFace shorthand: hf:org/model\n if (modelId.startsWith(\"hf:\")) {\n const repo = modelId.slice(3);\n return {\n type: \"huggingface\",\n path: repo,\n };\n }\n\n // HuggingFace URL\n if (modelId.startsWith(\"https://huggingface.co/\")) {\n const repo = modelId.replace(\"https://huggingface.co/\", \"\");\n return {\n type: \"huggingface\",\n path: repo,\n };\n }\n\n // Local file\n if (modelId.startsWith(\"file:\")) {\n const path = modelId.slice(5);\n return {\n type: \"local\",\n path,\n };\n }\n\n // Assume it's a HuggingFace repo if it contains a slash\n if (modelId.includes(\"/\")) {\n return {\n type: \"huggingface\",\n path: modelId,\n };\n }\n\n // Unknown - treat as HuggingFace\n return {\n type: \"huggingface\",\n path: modelId,\n };\n}\n\n/**\n * Get model config (built-in only)\n */\nexport function getModelConfig(modelId: string): ModelConfig | null {\n return BUILTIN_MODELS[modelId] || null;\n}\n\n// Default context lengths for the families the native engine actually supports\n// (a graph generator exists in src/gpu/architectures). Other families fall back\n// to a conservative default.\nconst FAMILY_CONTEXT_DEFAULTS: Record<string, number> = {\n qwen: 32_768,\n other: 32_768, // LFM2 supports 128k but config.json is the real source of truth\n};\n\n/**\n * Create model config for an external HuggingFace model.\n *\n * Inference is restricted to families the engine can actually run — Qwen\n * (Qwen2/Qwen3/Qwen3.5) and LFM2 (Liquid). Everything else is left as \"other\"\n * with conservative capability flags so the REPL doesn't advertise features the\n * engine can't deliver.\n */\nexport function createExternalModelConfig(\n modelId: string,\n repo: string,\n contextLength?: number,\n): ModelConfig {\n const repoLower = repo.toLowerCase();\n\n // Only infer families that have a graph generator in the registry.\n let family: ModelConfig[\"family\"] = \"other\";\n if (repoLower.includes(\"qwen\")) {\n family = \"qwen\";\n }\n\n const isLiquid = repoLower.includes(\"lfm\") || repoLower.includes(\"liquid\");\n const isQwen = family === \"qwen\";\n\n return {\n id: modelId,\n repo,\n description: `External model: ${repo}`,\n size: \"Unknown\",\n contextLength: contextLength || FAMILY_CONTEXT_DEFAULTS[family] || 32_768,\n // Qwen3/Qwen3.5 and LFM2.5-Thinking expose thinking; nothing here is vision.\n supportsThinking: isQwen || isLiquid,\n supportsJson: isQwen,\n family,\n };\n}\n\n/**\n * Fetch context length from HuggingFace model config\n */\nexport async function fetchModelContextLength(repo: string): Promise<number | null> {\n try {\n const res = await fetch(`https://huggingface.co/${repo}/raw/main/config.json`);\n if (!res.ok) {\n return null;\n }\n\n const config = await res.json();\n\n // Different models use different field names\n return (\n config.max_position_embeddings ||\n config.n_positions ||\n config.max_seq_len ||\n config.sliding_window || // Some models use this\n config.context_length ||\n null\n );\n } catch {\n return null;\n }\n}\n\n/**\n * List all built-in models\n */\nexport function listBuiltinModels(): ModelConfig[] {\n return Object.values(BUILTIN_MODELS);\n}\n\n/**\n * Search HuggingFace models (placeholder - would need HF API)\n */\nexport async function searchModels(query: string): Promise<ModelConfig[]> {\n // TODO: Implement HuggingFace API search\n // For now, filter built-in models\n const q = query.toLowerCase();\n return listBuiltinModels().filter(\n (m) =>\n m.id.toLowerCase().includes(q) ||\n m.description.toLowerCase().includes(q) ||\n m.family.toLowerCase().includes(q),\n );\n}\n","/**\n * Mobile / PWA storage helpers.\n *\n * On-device models are large (a 4-bit 0.8B is ~400 MB; vision/larger models are\n * GBs). Mobile browsers — iOS Safari especially — wall a web origin off from the\n * real disk with TWO independent ceilings:\n *\n * 1. **Storage quota** (disk for the model cache). An *uninstalled* Safari tab\n * gets only ~1 GB, best-effort and evictable, regardless of how much free\n * disk the device has. Exceed it and every cache write fails → the model\n * re-downloads on every visit.\n * 2. **Tab memory** (RAM during load/inference) — a separate, smaller ceiling.\n *\n * The unlock for the storage ceiling is **persistent storage**, which iOS Safari\n * grants when the site is **installed to the Home Screen** (a PWA). Installed, the\n * quota jumps to a large fraction of actual disk and is never evicted — so models\n * cache once and stay. These helpers let an app surface that to its users and\n * request it, so on-device AI is actually practical on mobile.\n *\n * All functions are SSR/Node-safe (guarded; return conservative defaults).\n */\n\n/** True when the page is running as an installed/standalone PWA (Home Screen). */\nexport function isStandalone(): boolean {\n if (typeof window === \"undefined\") return false;\n // iOS Safari exposes navigator.standalone; everyone else uses display-mode.\n const iosStandalone = (navigator as { standalone?: boolean }).standalone === true;\n const displayStandalone =\n typeof window.matchMedia === \"function\" &&\n window.matchMedia(\"(display-mode: standalone)\").matches;\n return iosStandalone || displayStandalone;\n}\n\n/** True when running on iOS/iPadOS (where install is the quota unlock and the\n * install flow is manual: Share → Add to Home Screen). iPadOS masquerades as\n * macOS, so we also treat touch-capable WebKit-on-Mac as iOS. */\nexport function isIOS(): boolean {\n if (typeof navigator === \"undefined\") return false;\n const ua = navigator.userAgent || \"\";\n if (/iPhone|iPad|iPod/.test(ua)) return true;\n // iPadOS 13+ reports a Mac UA — detect via touch points + WebKit.\n const isMacWebKit = /Macintosh/.test(ua) && /AppleWebKit/.test(ua) && !/Chrome/.test(ua);\n return isMacWebKit && ((navigator as { maxTouchPoints?: number }).maxTouchPoints ?? 0) > 1;\n}\n\nexport type StorageStatus = {\n /** Total quota granted to this origin, in MB (best-effort estimate). */\n quotaMB: number;\n /** Bytes currently used by this origin, in MB. */\n usageMB: number;\n /** quota − usage, in MB. */\n availableMB: number;\n /** Storage is persistent (exempt from eviction). On iOS this is effectively\n * only true once the site is installed to the Home Screen. */\n persisted: boolean;\n /** Running as an installed/standalone PWA. */\n installed: boolean;\n /** Platform is iOS/iPadOS (install is the quota unlock here). */\n ios: boolean;\n};\n\n/** Snapshot of the origin's storage situation — quota, usage, persistence, and\n * whether the app is installed. Use it to decide whether to recommend install\n * before downloading a large model. */\nexport async function getStorageStatus(): Promise<StorageStatus> {\n const installed = isStandalone();\n const ios = isIOS();\n let quotaMB = 0;\n let usageMB = 0;\n let persisted = false;\n try {\n const est = await navigator.storage?.estimate?.();\n quotaMB = Math.round((est?.quota || 0) / 1e6);\n usageMB = Math.round((est?.usage || 0) / 1e6);\n } catch {\n /* estimate unsupported */\n }\n try {\n persisted = (await navigator.storage?.persisted?.()) ?? false;\n } catch {\n /* persisted unsupported */\n }\n return {\n quotaMB,\n usageMB,\n availableMB: Math.max(0, quotaMB - usageMB),\n persisted,\n installed,\n ios,\n };\n}\n\n/**\n * Request persistent storage (exempt from eviction). Returns whether the origin\n * is persistent afterwards. Browsers grant this based on engagement/installation;\n * on iOS Safari it is effectively granted only to an installed (Home Screen) PWA,\n * so call this AND guide users to install when it returns false on iOS.\n */\nexport async function requestPersistentStorage(): Promise<boolean> {\n try {\n if (await navigator.storage?.persisted?.()) return true;\n return (await navigator.storage?.persist?.()) ?? false;\n } catch {\n return false;\n }\n}\n\nexport type ModelFit = {\n /** The model likely fits in the currently-available quota. */\n fits: boolean;\n availableMB: number;\n /** Caching durably would benefit from installing to the Home Screen — true when\n * not installed on iOS, or when the model doesn't fit the current quota. */\n recommendInstall: boolean;\n};\n\n/**\n * Estimate whether a model of `sizeMB` will cache in the current quota, and\n * whether you should recommend installing to the Home Screen first. Pair with a\n * one-time \"Install for offline use\" prompt before a large download on mobile.\n */\nexport async function canCacheModel(sizeMB: number): Promise<ModelFit> {\n const s = await getStorageStatus();\n // Headroom: leave ~10% slack so we don't recommend a download that just barely\n // fits and then fails mid-write.\n const fits = s.availableMB >= sizeMB * 1.1;\n const recommendInstall = (!fits || (s.ios && !s.installed)) && !s.persisted;\n return { fits, availableMB: s.availableMB, recommendInstall };\n}\n\n/**\n * Platform-appropriate install guidance. iOS Safari has NO programmatic install\n * prompt — installation is manual (Share → Add to Home Screen), so apps should\n * show these instructions. Other platforms (Android/Chrome) fire\n * `beforeinstallprompt`, which apps can capture for a one-tap button.\n */\nexport function getInstallGuidance(): { installed: boolean; manual: boolean; steps: string } {\n const installed = isStandalone();\n if (installed) return { installed: true, manual: false, steps: \"Already installed.\" };\n if (isIOS()) {\n return {\n installed: false,\n manual: true,\n steps:\n \"Tap the Share button, then 'Add to Home Screen'. Installing unlocks durable storage so models download once instead of every visit.\",\n };\n }\n return {\n installed: false,\n manual: false,\n steps:\n \"Use your browser's Install option (or the install icon in the address bar) to add this app for offline use and durable model storage.\",\n };\n}\n","// ============================================\n// Audio Playback Utilities\n// ============================================\n\n/**\n * Play audio from Float32Array using Web Audio API\n *\n * @example\n * ```ts\n * import { playAudio } from \"@tryhamster/gerbil/browser\";\n *\n * const audio = new Float32Array([...]); // TTS output\n * const controller = await playAudio(audio, 24000);\n *\n * // Stop playback\n * controller.stop();\n * ```\n */\nexport async function playAudio(\n audio: Float32Array,\n sampleRate: number = 24000,\n): Promise<{ stop: () => void; onEnded: Promise<void> }> {\n const audioContext = new AudioContext();\n\n // Resume if suspended\n if (audioContext.state === \"suspended\") {\n await audioContext.resume();\n }\n\n const audioBuffer = audioContext.createBuffer(1, audio.length, sampleRate);\n const channelData = new Float32Array(audio);\n audioBuffer.copyToChannel(channelData, 0);\n\n const sourceNode = audioContext.createBufferSource();\n sourceNode.buffer = audioBuffer;\n sourceNode.connect(audioContext.destination);\n\n const onEnded = new Promise<void>((resolve) => {\n sourceNode.onended = () => {\n audioContext.close();\n resolve();\n };\n });\n\n sourceNode.start();\n\n return {\n stop: () => {\n sourceNode.stop();\n audioContext.close();\n },\n onEnded,\n };\n}\n\n/**\n * Create a reusable audio player for streaming TTS\n *\n * @example\n * ```ts\n * import { createAudioPlayer } from \"@tryhamster/gerbil/browser\";\n *\n * const player = createAudioPlayer(24000);\n *\n * // Queue audio chunks as they arrive\n * player.queue(chunk1);\n * player.queue(chunk2);\n *\n * // Stop and clear\n * player.stop();\n * ```\n */\nexport function createAudioPlayer(sampleRate: number = 24000): {\n queue: (audio: Float32Array) => void;\n stop: () => void;\n isPlaying: () => boolean;\n} {\n let audioContext: AudioContext | null = null;\n let nextStartTime = 0;\n let isActive = false;\n\n const ensureContext = async () => {\n if (!audioContext) {\n audioContext = new AudioContext();\n }\n if (audioContext.state === \"suspended\") {\n await audioContext.resume();\n }\n return audioContext;\n };\n\n return {\n queue: async (audio: Float32Array) => {\n const ctx = await ensureContext();\n isActive = true;\n\n const buffer = ctx.createBuffer(1, audio.length, sampleRate);\n const channelData = new Float32Array(audio);\n buffer.copyToChannel(channelData, 0);\n\n const source = ctx.createBufferSource();\n source.buffer = buffer;\n source.connect(ctx.destination);\n\n // Schedule seamlessly after previous chunk\n const startTime = Math.max(ctx.currentTime, nextStartTime);\n source.start(startTime);\n nextStartTime = startTime + buffer.duration;\n\n source.onended = () => {\n if (ctx.currentTime >= nextStartTime - 0.1) {\n isActive = false;\n }\n };\n },\n\n stop: () => {\n isActive = false;\n nextStartTime = 0;\n if (audioContext) {\n audioContext.close();\n audioContext = null;\n }\n },\n\n isPlaying: () => isActive,\n };\n}\n","// ============================================\n// iOS Model Guards & Device Capability Detection\n// ============================================\n\n// ============================================\n// Real native-engine model ids\n// ============================================\n//\n// The website loads models by their actual repo id (MLX 4-bit on-device builds,\n// or the upstream Qwen/Liquid repos on desktop). The guard matches on those ids\n// — NOT the old ONNX-era shorthands — so it must stay in sync with the real\n// checkpoints the engine can run. Matching is substring-based on a normalized\n// id so both `mlx-community/Qwen3.5-0.8B-4bit` and `Qwen/Qwen3.5-0.8B` resolve.\n\n/** Recommended safe model ids per modality (used as fallbacks on mobile). */\nconst SAFE_MOBILE_CHAT = \"mlx-community/Qwen3.5-0.8B-4bit\";\n\n/**\n * Approximate on-device (INT4) memory footprint in MB for the models the native\n * engine actually ships. Used for memory-aware selection and messaging.\n */\nexport const MODEL_SIZES: Record<string, number> = {\n // Chat models (INT4, on-device)\n \"qwen3.5-0.8b\": 650, // ~0.65GB\n \"qwen3.5-2b\": 1700, // ~1.7GB\n \"gemma-4-e2b\": 3600, // ~3.6GB\n \"lfm2.5-350m\": 300, // ~0.3GB\n // TTS models\n \"kokoro-82m\": 350,\n \"supertonic-66m\": 300,\n // STT models\n \"whisper-tiny\": 150,\n \"whisper-tiny.en\": 150,\n \"whisper-small\": 500,\n // Embedding models\n \"all-minilm-l6-v2\": 100,\n};\n\n/**\n * Normalize a repo/model id to a lowercase token stream for substring matching\n * (strips org prefixes' punctuation while preserving the model name tokens).\n */\nfunction normalizeId(modelId: string): string {\n return modelId.toLowerCase().replace(/[^a-z0-9]/g, \"-\");\n}\n\n/**\n * iOS (WKWebView) model classification keyed off the REAL native-engine ids.\n *\n * - blocked: too large for the WKWebView memory ceiling on iPhone — will crash.\n * gemma-4-e2b (~3.6GB) plus any vision checkpoint (the vision encoder pushes\n * the working set well past what an iPhone can hold).\n * - risky: Qwen3.5-2B (~1.7GB) OOM-crashes iPhone WKWebView in practice — now\n * treated as unsafe to auto-load (flagged `risky` so a UI can offer to force it).\n * - everything else (Qwen3.5-0.8B ~0.65GB, LFM2.5-350M) is allowed everywhere.\n */\nconst IOS_MODEL_LIMITS = {\n /** Substrings (normalized) of ids that HARD-BLOCK on iPhone. */\n blocked: [\"gemma-4-e2b\", \"gemma-4-e4b\"],\n /** Substrings that mark a vision checkpoint (blocked on iPhone). */\n visionMarkers: [\"vision\", \"-vl-\", \"-vl\", \"vlm\", \"image-text\", \"-it-vision\"],\n /** Substrings of ids that WARN on iPhone (Qwen3.5-2B class). */\n risky: [\"qwen3.5-2b\", \"qwen3-5-2b\"],\n /** Maximum total memory budget in MB for iOS WKWebView. */\n maxBudgetMB: 1800,\n} as const;\n\n/**\n * Check if a model is safe to load on the current device.\n * Returns guidance specific to iOS memory constraints. Matches on the real\n * native-engine repo ids (MLX 4-bit / upstream Qwen / Liquid).\n */\nexport function isModelSafeForDevice(modelId: string): {\n safe: boolean;\n /**\n * Borderline: may run on the newest hardware but is prone to OOM. Reported\n * unsafe (`safe: false`) so callers block by default; a UI can use `risky` to\n * offer an explicit \"load anyway\" on capable devices.\n */\n risky: boolean;\n reason: string;\n recommendation?: string;\n maxSafeModel?: string;\n} {\n const ua = typeof navigator !== \"undefined\" ? navigator.userAgent : \"\";\n const isIPhone = /iPhone|iPod/.test(ua);\n const isIPad = /iPad/.test(ua);\n const isIOS = isIPhone || isIPad;\n const isIOSChrome = isIOS && /CriOS/.test(ua);\n const normalizedId = normalizeId(modelId);\n\n const isVision = IOS_MODEL_LIMITS.visionMarkers.some((m) =>\n normalizedId.includes(normalizeId(m)),\n );\n const isBlocked =\n IOS_MODEL_LIMITS.blocked.some((m) => normalizedId.includes(normalizeId(m))) || isVision;\n const isRisky = IOS_MODEL_LIMITS.risky.some((m) => normalizedId.includes(normalizeId(m)));\n\n // iPhone is the hard constraint (smallest WKWebView budget). iPad has more\n // headroom, so only the genuinely huge / vision checkpoints are blocked there.\n if (isIPhone) {\n if (isBlocked) {\n const browserNote = isIOSChrome ? \" (iOS Chrome uses WKWebView, same limits as Safari)\" : \"\";\n const why = isVision\n ? \"Vision checkpoints need a separate image encoder in memory\"\n : \"It is too large (~3.6GB)\";\n return {\n safe: false,\n risky: false,\n reason: `Model ${modelId} will crash on iPhone${browserNote}. ${why}, which exceeds the WKWebView memory ceiling.`,\n recommendation: `Use ${SAFE_MOBILE_CHAT} (Qwen3.5-0.8B) on iPhone, or run larger models on desktop.`,\n maxSafeModel: SAFE_MOBILE_CHAT,\n };\n }\n if (isRisky) {\n return {\n safe: false,\n risky: true,\n reason: `Model ${modelId} (~1.7GB) exceeds the iPhone WKWebView memory budget and is likely to crash.`,\n recommendation: `Use ${SAFE_MOBILE_CHAT} (Qwen3.5-0.8B) on iPhone; run Qwen3.5-2B on iPad or desktop.`,\n maxSafeModel: SAFE_MOBILE_CHAT,\n };\n }\n return { safe: true, risky: false, reason: \"Model is within iPhone memory limits.\" };\n }\n\n if (isIPad) {\n // iPad tolerates the 2B class; only block the huge / vision checkpoints.\n if (isBlocked) {\n const why = isVision\n ? \"Vision checkpoints need a separate image encoder in memory\"\n : \"It is too large (~3.6GB)\";\n return {\n safe: false,\n risky: false,\n reason: `Model ${modelId} may crash on iPad. ${why}, which can exceed the WKWebView memory ceiling.`,\n recommendation: `Use ${SAFE_MOBILE_CHAT} (Qwen3.5-0.8B) or Qwen3.5-2B on iPad.`,\n maxSafeModel: \"mlx-community/Qwen3.5-2B-4bit\",\n };\n }\n return { safe: true, risky: false, reason: \"Model is within iPad memory limits.\" };\n }\n\n // Android - block the huge / vision checkpoints, allow the rest.\n const isAndroid = /Android/.test(ua);\n if (isAndroid && isBlocked) {\n return {\n safe: false,\n risky: false,\n reason: `Model ${modelId} is very large and may crash on Android devices.`,\n recommendation: `Use ${SAFE_MOBILE_CHAT} (Qwen3.5-0.8B) or Qwen3.5-2B on Android.`,\n maxSafeModel: \"mlx-community/Qwen3.5-2B-4bit\",\n };\n }\n\n // Desktop - all models are safe.\n return { safe: true, risky: false, reason: \"Desktop browser has sufficient memory.\" };\n}\n\n/**\n * Get recommended models based on device memory and capabilities.\n * Helps prevent OOM crashes on low-memory mobile devices.\n */\nexport function getRecommendedModels(): {\n chat: string;\n tts: string;\n stt: string;\n embedding: string;\n reason: string;\n deviceMemory: number | null;\n isMobile: boolean;\n} {\n const ua = typeof navigator !== \"undefined\" ? navigator.userAgent : \"\";\n const deviceMemory = typeof navigator !== \"undefined\" ? (navigator as any).deviceMemory : null;\n const isMobile = /iPhone|iPad|iPod|Android|Mobile/.test(ua);\n\n // Estimate available memory (deviceMemory reports total GB, not available)\n // Mobile devices typically have less free memory due to OS overhead\n const effectiveMemory = deviceMemory ? (isMobile ? deviceMemory * 0.4 : deviceMemory * 0.6) : 4;\n const availableMB = effectiveMemory * 1024;\n\n let chat: string;\n let reason: string;\n\n if (availableMB < 600) {\n chat = \"LiquidAI/LFM2.5-350M\";\n reason = \"Very low memory device - using smallest model (LFM2.5-350M)\";\n } else if (isMobile && availableMB < 2200) {\n chat = SAFE_MOBILE_CHAT;\n reason = \"Mobile device - using Qwen3.5-0.8B to stay within the WKWebView memory limit\";\n } else if (availableMB < 2200) {\n chat = SAFE_MOBILE_CHAT;\n reason = \"Standard model for moderate memory (Qwen3.5-0.8B)\";\n } else {\n chat = \"mlx-community/Qwen3.5-2B-4bit\";\n reason = \"High memory available - using Qwen3.5-2B for better quality\";\n }\n\n return {\n chat,\n tts: \"kokoro-82m\",\n stt: \"whisper-tiny.en\",\n embedding: \"all-MiniLM-L6-v2\",\n reason,\n deviceMemory,\n isMobile,\n };\n}\n\n// ============================================\n// Session Phase Tracking (Reload Detection)\n// ============================================\n\ntype DownloadPhase = \"idle\" | \"downloading\" | \"caching\" | \"initializing\" | \"ready\" | \"error\";\n\nexport const SESSION_STORAGE_KEY = \"gerbil_session_phase\";\n\nexport type SessionState = {\n phase: DownloadPhase;\n modelId: string | null;\n sessionId: string;\n timestamp: number;\n bytesDownloaded?: number;\n totalBytes?: number;\n};\n\n/**\n * Generate a unique session ID for tracking across reloads.\n */\nfunction generateSessionId(): string {\n return `${Date.now()}-${Math.random().toString(36).slice(2, 9)}`;\n}\n\n/**\n * Get or create the current session ID.\n */\nfunction getSessionId(): string {\n if (typeof localStorage === \"undefined\") return generateSessionId();\n\n let sessionId = sessionStorage.getItem(\"gerbil_session_id\");\n if (!sessionId) {\n sessionId = generateSessionId();\n sessionStorage.setItem(\"gerbil_session_id\", sessionId);\n }\n return sessionId;\n}\n\n/**\n * Set the current download/initialization phase.\n * Used to detect if a reload happened during a critical operation.\n */\nexport function setDownloadPhase(\n phase: DownloadPhase,\n modelId?: string,\n progress?: { bytesDownloaded: number; totalBytes: number },\n): void {\n if (typeof localStorage === \"undefined\") return;\n\n const state: SessionState = {\n phase,\n modelId: modelId || null,\n sessionId: getSessionId(),\n timestamp: Date.now(),\n bytesDownloaded: progress?.bytesDownloaded,\n totalBytes: progress?.totalBytes,\n };\n\n localStorage.setItem(SESSION_STORAGE_KEY, JSON.stringify(state));\n}\n\n/**\n * Get the last known download phase from storage.\n */\nexport function getDownloadPhase(): SessionState | null {\n if (typeof localStorage === \"undefined\") return null;\n\n try {\n const raw = localStorage.getItem(SESSION_STORAGE_KEY);\n if (!raw) return null;\n return JSON.parse(raw) as SessionState;\n } catch {\n return null;\n }\n}\n\n/**\n * Detect if the page reloaded during a model download/initialization.\n * This typically indicates an iOS memory crash.\n *\n * @returns Detection result with recommended action\n */\nexport function detectMemoryCrash(): {\n crashed: boolean;\n phase?: DownloadPhase;\n modelId?: string;\n timeSinceCrash?: number;\n recommendation?: string;\n} {\n const lastState = getDownloadPhase();\n const currentSessionId = getSessionId();\n\n if (!lastState) {\n return { crashed: false };\n }\n\n // If session ID changed and we were in a critical phase, it's likely a crash/reload\n const criticalPhases: DownloadPhase[] = [\"downloading\", \"caching\", \"initializing\"];\n const wasInCriticalPhase = criticalPhases.includes(lastState.phase);\n const sessionChanged = lastState.sessionId !== currentSessionId;\n const timeSinceCrash = Date.now() - lastState.timestamp;\n\n // Only count as crash if it happened recently (within 5 minutes) and session changed\n const recentCrash = timeSinceCrash < 5 * 60 * 1000;\n\n if (wasInCriticalPhase && sessionChanged && recentCrash) {\n // Clear the state to prevent repeated detection\n localStorage.removeItem(SESSION_STORAGE_KEY);\n\n return {\n crashed: true,\n phase: lastState.phase,\n modelId: lastState.modelId || undefined,\n timeSinceCrash,\n recommendation:\n lastState.modelId && /2b|gemma|vision/i.test(lastState.modelId)\n ? \"The model was too large for your device. Try Qwen3.5-0.8B instead.\"\n : \"Your device ran out of memory. Try a smaller model or use a desktop browser.\",\n };\n }\n\n return { crashed: false };\n}\n\n/**\n * Clear session phase (call when model loads successfully).\n */\nexport function clearDownloadPhase(): void {\n if (typeof localStorage === \"undefined\") return;\n localStorage.removeItem(SESSION_STORAGE_KEY);\n}\n\n// ============================================\n// WebKit Submit-Granularity (group size) Probe\n// ============================================\n//\n// On WebKit/iOS WebGPU, decode speed is bound by GPU round-trips: the executor\n// groups `webkitGroupSize` dispatches per command buffer, then submits + drains\n// (queue.onSubmittedWorkDone) per group. Larger groups amortize the drains and\n// can be ~5x faster on iPad. BUT the safe ceiling is device-dependent in three\n// classes:\n// 1. Newer WebKit (iPad 26.5): high group is correct + fast.\n// 2. Older WebKit: high group yields zero/garbage logits (wrong output).\n// 3. iPhone (iOS 18.7): high group HARD-CRASHES the GPU process (page dies).\n//\n// The crash class is the hard one: a bad group kills the page before any code\n// can record the result. So we use a localStorage BREADCRUMB that OUTLIVES the\n// page kill: we persist the candidate we're about to try BEFORE running any\n// GPU-heavy work. If the page survives, a later promotion clears `trying` and\n// records it as known-good. If the page crashed, `trying` is still set on the\n// next load, so we KNOW that rung crashed and cap below it.\n//\n// The probe escalates UP from the safe floor (never starts optimistic — a\n// first-visit crash is a terrible UX) and persists per-device in localStorage.\n\nexport const WEBKIT_GROUP_PROBE_KEY = \"gerbil-webkit-group-v1\";\n\n/**\n * Candidate group-size ladder. We escalate UP one rung per page load. 128 is\n * treated as effectively \"batch-all\" for this model — kept simple on purpose.\n */\nexport const WEBKIT_GROUP_LADDER = [1, 8, 32, 64, 128] as const;\n\nexport type WebkitGroupProbe = {\n /** Largest group size proven crash-free AND correct on this device. Starts at 1. */\n knownGood: number;\n /** Candidate being attempted this page-load (null when not probing). */\n trying: number | null;\n /** True once we hit a crash/incorrect ceiling and should stop escalating. */\n capped: boolean;\n};\n\nconst DEFAULT_PROBE: WebkitGroupProbe = { knownGood: 1, trying: null, capped: false };\n\n/** Read the persisted WebKit group probe record (guarded; safe on node). */\nexport function readGroupProbe(): WebkitGroupProbe {\n if (typeof localStorage === \"undefined\") return { ...DEFAULT_PROBE };\n try {\n const raw = localStorage.getItem(WEBKIT_GROUP_PROBE_KEY);\n if (!raw) return { ...DEFAULT_PROBE };\n const parsed = JSON.parse(raw) as Partial<WebkitGroupProbe>;\n return {\n knownGood:\n typeof parsed.knownGood === \"number\" && parsed.knownGood >= 1 ? parsed.knownGood : 1,\n trying: typeof parsed.trying === \"number\" ? parsed.trying : null,\n capped: parsed.capped === true,\n };\n } catch {\n return { ...DEFAULT_PROBE };\n }\n}\n\n/** Persist the WebKit group probe record (guarded; no-op on node). */\nexport function writeGroupProbe(rec: WebkitGroupProbe): void {\n if (typeof localStorage === \"undefined\") return;\n try {\n localStorage.setItem(WEBKIT_GROUP_PROBE_KEY, JSON.stringify(rec));\n } catch {\n // Best-effort; private mode / quota can throw.\n }\n}\n\n/**\n * The validated non-phone sweet spot. iPad swept 1→7.9, 8→19, 32→24.8, 64→26.6,\n * 128→26.9 (peak), 256→26.2 tok/s — a plateau from ~64 up, so 128 is the best\n * stable target (more batching just costs memory). Non-phone WebKit jumps here\n * directly; the crash breadcrumb caps it down if a device can't sustain it.\n */\nconst NONPHONE_TARGET_GROUP = 128;\n\n/**\n * Resolve the WebKit group size to use this session, recording `trying` as a\n * side effect so a crash this load is detectable on the next load.\n *\n * Algorithm (only meaningful on WebKit; inert otherwise):\n * 1. Read the record (default {knownGood:1, trying:null, capped:false}).\n * 2. If `trying !== null` on entry → the previous load set it but never cleared\n * it → that load CRASHED at `trying`. Cap there, keep `knownGood`, clear\n * `trying`. Use `knownGood` this session.\n * 3. Else if !capped and there is a rung above `knownGood` → set `trying = next`,\n * persist BEFORE any GPU work, and use it (we're escalating).\n * 4. Else → use `knownGood`.\n *\n * @returns the group size to use this session.\n */\nexport function resolveWebkitGroupSize(args: {\n override?: number;\n isWebKit: boolean;\n /**\n * Memory-constrained devices (phones) are the \"crash class\": batching crashes\n * the GPU process, and discovering that costs one user-visible crash before the\n * breadcrumb caps it. When `conservative` is set we never auto-escalate — the\n * device stays at its proven floor (group=1 on first run) and never risks that\n * calibration crash. `?group=N` still lets such a device opt in explicitly.\n */\n conservative?: boolean;\n}): number {\n // Explicit ?group=N override wins and skips the probe entirely.\n if (args.override && args.override > 0) return args.override;\n // Non-WebKit (desktop / Dawn / node) never groups — floor of 1, no bookkeeping.\n if (!args.isWebKit) return 1;\n\n const rec = readGroupProbe();\n\n // Step 2: a stale `trying` means last load crashed at that rung.\n if (rec.trying !== null) {\n const crashedAt = rec.trying;\n const next: WebkitGroupProbe = {\n knownGood: rec.knownGood,\n trying: null,\n capped: true,\n };\n writeGroupProbe(next);\n console.log(\n `[engine] webkit group probe: previous load crashed at group=${crashedAt} → capping at knownGood=${next.knownGood}`,\n );\n return next.knownGood;\n }\n\n // Step 3: non-phone WebKit (iPad / Mac Safari) jumps straight to the validated\n // sweet spot (group=128) instead of climbing rung-by-rung — these devices\n // tolerate batching (iPad ran every rung to 256 coherently), so there's no\n // reason to crawl. The breadcrumb (Step 2) caps it down if a particular device\n // can't sustain it. Phones (conservative) NEVER escalate — they stay at the\n // proven floor (group=1) to avoid the one calibration crash batching costs on\n // the crash class.\n if (!rec.capped && !args.conservative && rec.knownGood < NONPHONE_TARGET_GROUP) {\n const next: WebkitGroupProbe = {\n knownGood: rec.knownGood,\n trying: NONPHONE_TARGET_GROUP,\n capped: false,\n };\n writeGroupProbe(next); // PERSIST BEFORE any GPU-heavy work runs\n console.log(\n `[engine] webkit group probe: trying target group=${NONPHONE_TARGET_GROUP} (knownGood=${rec.knownGood})`,\n );\n return NONPHONE_TARGET_GROUP;\n }\n\n // Step 4: nothing to escalate (capped or at top rung) — use the proven floor.\n console.log(\n `[engine] webkit group probe: knownGood=${rec.knownGood} trying=null capped=${rec.capped} → using ${rec.knownGood}`,\n );\n return rec.knownGood;\n}\n\n/**\n * Promote (or cap) the WebKit group probe after the first successful forward.\n *\n * Call this once per page-load, after the model has loaded AND a first forward\n * completed without the page dying. The breadcrumb already handles the crash\n * class (the page death leaves `trying` set for the next load); this handles the\n * wrong-output class and records success.\n *\n * @param correct true if the first forward produced non-corrupt output.\n * - correct → promote: knownGood = trying, trying = null.\n * - incorrect → cap: keep knownGood at the prior rung, trying = null, capped.\n */\nexport function promoteGroupProbe(correct: boolean): void {\n if (typeof localStorage === \"undefined\") return;\n const rec = readGroupProbe();\n // Nothing was being attempted this load — nothing to promote.\n if (rec.trying === null) return;\n\n if (correct) {\n writeGroupProbe({ knownGood: rec.trying, trying: null, capped: rec.capped });\n console.log(`[engine] webkit group probe: PROMOTED group=${rec.trying} to known-good`);\n } else {\n // Wrong-output class: this rung is incorrect (but did not crash). Keep the\n // prior knownGood and stop escalating.\n writeGroupProbe({ knownGood: rec.knownGood, trying: null, capped: true });\n console.log(\n `[engine] webkit group probe: group=${rec.trying} produced INCORRECT output → capping at knownGood=${rec.knownGood}`,\n );\n }\n}\n","import { setDownloadPhase } from \"./device-guards.js\";\n\n// ============================================\n// Chunked Resumable Downloader\n// ============================================\n\n/** Chunk size for downloads: 1.5MB (safe for iOS IndexedDB transactions) */\nexport const CHUNK_SIZE_BYTES = 1.5 * 1024 * 1024;\n\n/** IndexedDB database name for chunked downloads */\nexport const DOWNLOAD_DB_NAME = \"gerbil-model-chunks\";\nconst DOWNLOAD_DB_VERSION = 1;\n\n/**\n * Manifest stored in IndexedDB to track download progress.\n */\ntype DownloadManifest = {\n modelId: string;\n url: string;\n etag: string | null;\n totalBytes: number;\n chunkSize: number;\n completedChunks: number[];\n createdAt: number;\n updatedAt: number;\n};\n\n/**\n * Open (or create) the IndexedDB for chunked downloads.\n */\nasync function openDownloadDB(): Promise<IDBDatabase> {\n return new Promise((resolve, reject) => {\n const request = indexedDB.open(DOWNLOAD_DB_NAME, DOWNLOAD_DB_VERSION);\n\n request.onerror = () =>\n reject(new Error(`Failed to open download DB: ${request.error?.message}`));\n\n request.onsuccess = () => resolve(request.result);\n\n request.onupgradeneeded = (event) => {\n const db = (event.target as IDBOpenDBRequest).result;\n\n // Store for download manifests\n if (!db.objectStoreNames.contains(\"manifests\")) {\n db.createObjectStore(\"manifests\", { keyPath: \"modelId\" });\n }\n\n // Store for actual chunks (key: modelId-chunkIndex)\n if (!db.objectStoreNames.contains(\"chunks\")) {\n db.createObjectStore(\"chunks\");\n }\n };\n });\n}\n\n/**\n * Get download manifest for a model.\n */\nasync function getManifest(db: IDBDatabase, modelId: string): Promise<DownloadManifest | null> {\n return new Promise((resolve, reject) => {\n const tx = db.transaction(\"manifests\", \"readonly\");\n const store = tx.objectStore(\"manifests\");\n const request = store.get(modelId);\n\n request.onerror = () => reject(new Error(`Failed to get manifest: ${request.error?.message}`));\n request.onsuccess = () => resolve(request.result || null);\n });\n}\n\n/**\n * Save download manifest.\n */\nasync function saveManifest(db: IDBDatabase, manifest: DownloadManifest): Promise<void> {\n return new Promise((resolve, reject) => {\n const tx = db.transaction(\"manifests\", \"readwrite\");\n const store = tx.objectStore(\"manifests\");\n const request = store.put(manifest);\n\n request.onerror = () => reject(new Error(`Failed to save manifest: ${request.error?.message}`));\n request.onsuccess = () => resolve();\n });\n}\n\n/**\n * Save a single chunk.\n */\nasync function saveChunk(\n db: IDBDatabase,\n modelId: string,\n chunkIndex: number,\n data: ArrayBuffer,\n): Promise<void> {\n return new Promise((resolve, reject) => {\n const tx = db.transaction(\"chunks\", \"readwrite\");\n const store = tx.objectStore(\"chunks\");\n const key = `${modelId}-${chunkIndex}`;\n const request = store.put(data, key);\n\n request.onerror = () =>\n reject(new Error(`Failed to save chunk ${chunkIndex}: ${request.error?.message}`));\n request.onsuccess = () => resolve();\n });\n}\n\n/**\n * Get a single chunk.\n */\nasync function getChunk(\n db: IDBDatabase,\n modelId: string,\n chunkIndex: number,\n): Promise<ArrayBuffer | null> {\n return new Promise((resolve, reject) => {\n const tx = db.transaction(\"chunks\", \"readonly\");\n const store = tx.objectStore(\"chunks\");\n const key = `${modelId}-${chunkIndex}`;\n const request = store.get(key);\n\n request.onerror = () =>\n reject(new Error(`Failed to get chunk ${chunkIndex}: ${request.error?.message}`));\n request.onsuccess = () => resolve(request.result || null);\n });\n}\n\n/**\n * Delete all chunks and manifest for a model.\n */\nasync function clearModelData(db: IDBDatabase, modelId: string): Promise<void> {\n // Get manifest to know how many chunks to delete\n const manifest = await getManifest(db, modelId);\n\n return new Promise((resolve, reject) => {\n const tx = db.transaction([\"manifests\", \"chunks\"], \"readwrite\");\n\n // Delete manifest\n tx.objectStore(\"manifests\").delete(modelId);\n\n // Delete all chunks\n if (manifest) {\n const totalChunks = Math.ceil(manifest.totalBytes / manifest.chunkSize);\n const chunkStore = tx.objectStore(\"chunks\");\n for (let i = 0; i < totalChunks; i++) {\n chunkStore.delete(`${modelId}-${i}`);\n }\n }\n\n tx.oncomplete = () => resolve();\n tx.onerror = () => reject(new Error(`Failed to clear model data: ${tx.error?.message}`));\n });\n}\n\n/**\n * Chunked resumable downloader for large model files.\n * Downloads in 1.5MB chunks to avoid iOS memory pressure.\n */\nexport async function downloadModelChunked(\n url: string,\n modelId: string,\n options: {\n onProgress?: (info: {\n phase: string;\n bytesDownloaded: number;\n totalBytes: number;\n percent: number;\n }) => void;\n signal?: AbortSignal;\n } = {},\n): Promise<ArrayBuffer> {\n const { onProgress, signal } = options;\n\n // Update session phase\n setDownloadPhase(\"downloading\", modelId);\n\n const db = await openDownloadDB();\n\n try {\n // Check for existing manifest\n let manifest = await getManifest(db, modelId);\n\n // Fetch headers to get content-length and etag\n const headResponse = await fetch(url, { method: \"HEAD\", signal });\n if (!headResponse.ok) {\n throw new Error(`HEAD request failed: ${headResponse.status} ${headResponse.statusText}`);\n }\n\n const contentLength = parseInt(headResponse.headers.get(\"content-length\") || \"0\", 10);\n const etag = headResponse.headers.get(\"etag\");\n const acceptRanges = headResponse.headers.get(\"accept-ranges\");\n\n if (!contentLength) {\n throw new Error(\"Server did not provide content-length\");\n }\n\n // Check if we need to restart (etag mismatch means model updated)\n if (manifest && manifest.etag !== etag) {\n console.warn(`Model ${modelId} has been updated (etag mismatch). Clearing cached chunks.`);\n await clearModelData(db, modelId);\n manifest = null;\n }\n\n // Check if server supports range requests\n const supportsRange = acceptRanges === \"bytes\";\n\n if (!supportsRange) {\n // Fall back to regular download\n console.warn(`Server doesn't support range requests for ${modelId}. Using regular download.`);\n db.close();\n\n const response = await fetch(url, { signal });\n if (!response.ok) throw new Error(`Download failed: ${response.status}`);\n\n setDownloadPhase(\"caching\", modelId);\n const buffer = await response.arrayBuffer();\n setDownloadPhase(\"ready\", modelId);\n return buffer;\n }\n\n // Create or update manifest\n const totalChunks = Math.ceil(contentLength / CHUNK_SIZE_BYTES);\n\n if (!manifest) {\n manifest = {\n modelId,\n url,\n etag,\n totalBytes: contentLength,\n chunkSize: CHUNK_SIZE_BYTES,\n completedChunks: [],\n createdAt: Date.now(),\n updatedAt: Date.now(),\n };\n await saveManifest(db, manifest);\n }\n\n // Download missing chunks\n for (let i = 0; i < totalChunks; i++) {\n if (signal?.aborted) {\n throw new Error(\"Download aborted\");\n }\n\n // Skip already downloaded chunks\n if (manifest.completedChunks.includes(i)) {\n const bytesDownloaded = (manifest.completedChunks.length / totalChunks) * contentLength;\n onProgress?.({\n phase: \"resuming\",\n bytesDownloaded,\n totalBytes: contentLength,\n percent: Math.round((bytesDownloaded / contentLength) * 100),\n });\n continue;\n }\n\n const start = i * CHUNK_SIZE_BYTES;\n const end = Math.min(start + CHUNK_SIZE_BYTES - 1, contentLength - 1);\n\n // Download chunk with Range header\n const response = await fetch(url, {\n headers: { Range: `bytes=${start}-${end}` },\n signal,\n });\n\n if (response.status !== 206) {\n throw new Error(`Range request failed: ${response.status} (expected 206)`);\n }\n\n const chunkData = await response.arrayBuffer();\n\n // Save chunk to IndexedDB\n await saveChunk(db, modelId, i, chunkData);\n\n // Update manifest\n manifest.completedChunks.push(i);\n manifest.updatedAt = Date.now();\n await saveManifest(db, manifest);\n\n // Update session phase with progress\n const bytesDownloaded = manifest.completedChunks.length * CHUNK_SIZE_BYTES;\n setDownloadPhase(\"downloading\", modelId, { bytesDownloaded, totalBytes: contentLength });\n\n onProgress?.({\n phase: \"downloading\",\n bytesDownloaded: Math.min(bytesDownloaded, contentLength),\n totalBytes: contentLength,\n percent: Math.round((manifest.completedChunks.length / totalChunks) * 100),\n });\n\n // Null out reference to allow GC before next chunk\n // @ts-expect-error - intentional null for GC\n response.body = null;\n }\n\n // All chunks downloaded - reassemble\n setDownloadPhase(\"caching\", modelId);\n onProgress?.({\n phase: \"assembling\",\n bytesDownloaded: contentLength,\n totalBytes: contentLength,\n percent: 100,\n });\n\n // Assemble chunks into final buffer\n // We do this incrementally to avoid holding all chunks in memory at once\n const finalBuffer = new ArrayBuffer(contentLength);\n const finalView = new Uint8Array(finalBuffer);\n\n for (let i = 0; i < totalChunks; i++) {\n const chunk = await getChunk(db, modelId, i);\n if (!chunk) {\n throw new Error(`Missing chunk ${i} during assembly`);\n }\n\n const offset = i * CHUNK_SIZE_BYTES;\n finalView.set(new Uint8Array(chunk), offset);\n\n // Null out chunk reference immediately after copying\n // This helps GC reclaim memory on iOS\n }\n\n // Clean up - delete chunks now that we have the full file\n await clearModelData(db, modelId);\n db.close();\n\n setDownloadPhase(\"ready\", modelId);\n return finalBuffer;\n } catch (error) {\n setDownloadPhase(\"error\", modelId);\n db.close();\n throw error;\n }\n}\n\n/**\n * Check if a model has an incomplete download.\n */\nexport async function hasIncompleteDownload(modelId: string): Promise<{\n incomplete: boolean;\n bytesDownloaded?: number;\n totalBytes?: number;\n percent?: number;\n}> {\n try {\n const db = await openDownloadDB();\n const manifest = await getManifest(db, modelId);\n db.close();\n\n if (!manifest) {\n return { incomplete: false };\n }\n\n const totalChunks = Math.ceil(manifest.totalBytes / manifest.chunkSize);\n const completedChunks = manifest.completedChunks.length;\n\n if (completedChunks < totalChunks) {\n return {\n incomplete: true,\n bytesDownloaded: completedChunks * manifest.chunkSize,\n totalBytes: manifest.totalBytes,\n percent: Math.round((completedChunks / totalChunks) * 100),\n };\n }\n\n return { incomplete: false };\n } catch {\n return { incomplete: false };\n }\n}\n\n/**\n * Clear incomplete download data for a model.\n */\nexport async function clearIncompleteDownload(modelId: string): Promise<void> {\n try {\n const db = await openDownloadDB();\n await clearModelData(db, modelId);\n db.close();\n } catch {\n // Ignore errors\n }\n}\n\n/**\n * Check if there's enough storage quota for a model download.\n * Returns estimated available space and whether download should proceed.\n */\nexport async function checkStorageQuota(requiredMB: number = 500): Promise<{\n ok: boolean;\n availableMB: number;\n usedMB: number;\n quotaMB: number;\n message?: string;\n}> {\n if (typeof navigator === \"undefined\" || !navigator.storage?.estimate) {\n return {\n ok: true,\n availableMB: -1,\n usedMB: -1,\n quotaMB: -1,\n message: \"Storage API not available\",\n };\n }\n\n try {\n const { quota, usage } = await navigator.storage.estimate();\n const quotaMB = Math.round((quota || 0) / 1_000_000);\n const usedMB = Math.round((usage || 0) / 1_000_000);\n const availableMB = quotaMB - usedMB;\n\n if (availableMB < requiredMB) {\n return {\n ok: false,\n availableMB,\n usedMB,\n quotaMB,\n message: `Need ${requiredMB}MB but only ${availableMB}MB available. Clear browser data or free up space.`,\n };\n }\n\n return { ok: true, availableMB, usedMB, quotaMB };\n } catch (e: any) {\n return {\n ok: true, // Proceed optimistically if we can't check\n availableMB: -1,\n usedMB: -1,\n quotaMB: -1,\n message: `Storage check failed: ${e.message}`,\n };\n }\n}\n"],"mappings":";AA4BA,MAAaA,iBAA8C;CACzD,gBAAgB;EACd,IAAI;EACJ,MAAM;EACN,aACE;EACF,MAAM;EACN,eAAe;EACf,kBAAkB;EAClB,cAAc;EACd,gBAAgB;EAChB,QAAQ;EACT;CACD,cAAc;EACZ,IAAI;EACJ,MAAM;EACN,aACE;EACF,MAAM;EACN,eAAe;EACf,kBAAkB;EAClB,cAAc;EACd,gBAAgB;EAChB,QAAQ;EACT;CACD,wBAAwB;EACtB,IAAI;EACJ,MAAM;EACN,aAAa;EACb,MAAM;EACN,eAAe;EACf,kBAAkB;EAClB,cAAc;EACd,QAAQ;EACT;CACF;;;;;;;;;;;;;;;;;;;;;;;;;;ACxCD,SAAgB,eAAwB;AACtC,KAAI,OAAO,WAAW,YAAa,QAAO;CAE1C,MAAM,gBAAiB,UAAuC,eAAe;CAC7E,MAAM,oBACJ,OAAO,OAAO,eAA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M,UAAU,KAAK,OAAO,SAAS,KAAK,IAAU;EACpD,MAAM,SAAS,KAAK,OAAO,SAAS,KAAK,IAAU;EACnD,MAAM,cAAc,UAAU;AAE9B,MAAI,cAAc,WAChB,QAAO;GACL,IAAI;GACJ;GACA;GACA;GACA,SAAS,QAAQ,WAAW,cAAc,YAAY;GACvD;AAGH,SAAO;GAAE,IAAI;GAAM;GAAa;GAAQ;GAAS;UAC1CC,GAAQ;AACf,SAAO;GACL,IAAI;GACJ,aAAa;GACb,QAAQ;GACR,SAAS;GACT,SAAS,yBAAyB,EAAE;GACrC"}
|
package/dist/cli.mjs
CHANGED
|
@@ -1,11 +1,11 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
2
|
import { t as __require } from "./chunk-B9cbKln6.mjs";
|
|
3
|
-
import { n as BUILTIN_MODELS, r as DEFAULT_MODEL, t as Gerbil } from "./gerbil-
|
|
3
|
+
import { n as BUILTIN_MODELS, r as DEFAULT_MODEL, t as Gerbil } from "./gerbil-DNniplr4.mjs";
|
|
4
4
|
import "./utils-DKO55ZmZ.mjs";
|
|
5
|
-
import "./one-liner-
|
|
5
|
+
import "./one-liner-JhdIPxzF.mjs";
|
|
6
6
|
import { n as isArchitectureSupported } from "./architectures-C1I5V3Dt.mjs";
|
|
7
|
-
import { A as useSkill, E as getSkillInfo, O as listSkills, a as summarize, f as explain, h as commit, s as review, y as loadProjectSkills } from "./skills-
|
|
8
|
-
import { r as startMCPServer } from "./mcp-
|
|
7
|
+
import { A as useSkill, E as getSkillInfo, O as listSkills, a as summarize, f as explain, h as commit, s as review, y as loadProjectSkills } from "./skills-CU694Dc8.mjs";
|
|
8
|
+
import { r as startMCPServer } from "./mcp-D2vvH1Xc.mjs";
|
|
9
9
|
import { a as getToolDefinitions, c as setToolContext, i as getTool, n as executeToolCall, o as loadProjectTools, r as formatToolsForPrompt, s as parseToolCall } from "./tools-DQ1mPUw5.mjs";
|
|
10
10
|
import fs, { existsSync, readFileSync, unlinkSync } from "node:fs";
|
|
11
11
|
import os, { tmpdir } from "node:os";
|
|
@@ -24,7 +24,7 @@ import TextInput from "ink-text-input";
|
|
|
24
24
|
import http from "node:http";
|
|
25
25
|
|
|
26
26
|
//#region package.json
|
|
27
|
-
var version = "1.0.
|
|
27
|
+
var version = "1.0.1";
|
|
28
28
|
|
|
29
29
|
//#endregion
|
|
30
30
|
//#region src/cli/repl/auto-update.ts
|
|
@@ -7701,7 +7701,7 @@ function App({ initialView = "menu" } = {}) {
|
|
|
7701
7701
|
return () => {
|
|
7702
7702
|
mounted = false;
|
|
7703
7703
|
if (gerbilRef.current) {
|
|
7704
|
-
import("./repl-
|
|
7704
|
+
import("./repl-BDRkwPGX.mjs").then(({ setCleanupPromise: setCleanupPromise$1 }) => {
|
|
7705
7705
|
setCleanupPromise$1(gerbilRef.current?.dispose(true) ?? Promise.resolve());
|
|
7706
7706
|
});
|
|
7707
7707
|
gerbilRef.current = null;
|
|
@@ -8997,7 +8997,7 @@ program.command("serve").description("Start Gerbil server (use --mcp or --http f
|
|
|
8997
8997
|
}
|
|
8998
8998
|
if (opts.mcp) await startMCPServer({ model: opts.model });
|
|
8999
8999
|
else {
|
|
9000
|
-
const { Gerbil: Gerbil$1 } = await import("./gerbil-
|
|
9000
|
+
const { Gerbil: Gerbil$1 } = await import("./gerbil-CTZUa8EZ.mjs");
|
|
9001
9001
|
const g = new Gerbil$1();
|
|
9002
9002
|
const spinner = ora("Loading model...").start();
|
|
9003
9003
|
await g.loadModel(opts.model);
|