@ismail-kattakath/mediapipe-react 0.1.2 → 0.1.3

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.
@@ -178,4 +178,4 @@ export {
178
178
  MediaPipeProvider,
179
179
  useMediaPipeContext
180
180
  };
181
- //# sourceMappingURL=chunk-73Z6K33X.mjs.map
181
+ //# sourceMappingURL=chunk-F4K45QYO.mjs.map
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/genai.ts","../src/index.tsx","../src/utils.ts"],"sourcesContent":["\"use client\";\n\nimport { useEffect, useState, useCallback, useRef } from \"react\";\nimport { useMediaPipeContext } from \"./index\";\n\n/**\n * The Web Worker logic for MediaPipe GenAI.\n * This is stringified so it can be easily initialized as a Blob URL if the file-based worker fails.\n */\nconst workerScript = `\nimport { LlmInference, FilesetResolver } from 'https://cdn.jsdelivr.net/npm/@mediapipe/tasks-genai';\n\nlet llmInference = null;\n\nasync function checkGpuSupport() {\n if (!('gpu' in navigator)) {\n throw new Error('WebGPU is not supported in this browser.');\n }\n const gpu = navigator.gpu;\n const adapter = await gpu.requestAdapter();\n if (!adapter) {\n throw new Error('No appropriate GPU adapter found.');\n }\n}\n\nasync function initInference(modelPath, wasmPath) {\n try {\n await checkGpuSupport();\n const genai = await FilesetResolver.forGenAiTasks(wasmPath);\n llmInference = await LlmInference.createFromOptions(genai, {\n baseOptions: { modelAssetPath: modelPath },\n });\n self.postMessage({ type: 'INIT_COMPLETE' });\n } catch (error) {\n self.postMessage({ type: 'ERROR', error: error.message || 'Unknown error during initialization' });\n }\n}\n\nself.onmessage = async (event) => {\n const { type, payload } = event.data;\n\n if (type === 'INIT') {\n const { modelPath, wasmPath } = payload;\n await initInference(modelPath, wasmPath);\n }\n\n if (type === 'GENERATE') {\n if (!llmInference) {\n self.postMessage({ type: 'ERROR', error: 'LLM Inference not initialized. Please ensure the model is loaded and WebGPU is supported.' });\n return;\n }\n\n try {\n const { prompt } = payload;\n llmInference.generateResponse(prompt, (partialText, done) => {\n self.postMessage({\n type: 'CHUNK',\n payload: { text: partialText, done }\n });\n });\n } catch (error) {\n self.postMessage({ type: 'ERROR', error: error.message || 'Error generating response' });\n }\n }\n};\n`;\n\nexport interface UseLlmOptions {\n modelPath?: string;\n wasmPath?: string;\n}\n\nexport function useLlm(options: UseLlmOptions = {}) {\n const context = useMediaPipeContext();\n const [output, setOutput] = useState(\"\");\n const [isLoading, setIsLoading] = useState(false);\n const [error, setError] = useState<string | null>(null);\n const [progress, setProgress] = useState(0);\n\n const workerRef = useRef<Worker | null>(null);\n\n // Use values from props if provided, otherwise fallback to context\n const modelPath = options.modelPath || context.modelPath;\n const wasmPath =\n options.wasmPath ||\n context.wasmPath ||\n \"https://cdn.jsdelivr.net/npm/@mediapipe/tasks-genai/wasm\";\n\n useEffect(() => {\n if (!context.isBrowser || !modelPath) return;\n\n // Early check for WebGPU support in the UI thread too\n if (!(\"gpu\" in navigator)) {\n setTimeout(() => setError(\"WebGPU is not supported in this browser.\"), 0);\n return;\n }\n\n let worker: Worker;\n\n try {\n // Attempt to load from the separate worker file (Vite/Next.js/Webpack friendly)\n worker = new Worker(new URL(\"./genai.worker\", import.meta.url), {\n type: \"module\",\n name: \"mediapipe-genai-worker\",\n });\n } catch (_e) {\n // Fallback to Blob-based worker if relative path fails\n console.warn(\"MediaPipe React: Falling back to Blob-based GenAI worker\");\n const blob = new Blob([workerScript], { type: \"application/javascript\" });\n worker = new Worker(URL.createObjectURL(blob));\n }\n\n workerRef.current = worker;\n\n worker.onmessage = (event) => {\n const { type, payload, error: workerError } = event.data;\n\n switch (type) {\n case \"INIT_COMPLETE\":\n setIsLoading(false);\n setProgress(100);\n break;\n case \"CHUNK\":\n setOutput((prev) => prev + payload.text);\n if (payload.done) {\n setIsLoading(false);\n }\n break;\n case \"ERROR\":\n setError(workerError || \"Worker encountered an error\");\n setIsLoading(false);\n break;\n }\n };\n\n setTimeout(() => {\n setIsLoading(true);\n setProgress(10); // Initial progress\n }, 0);\n\n worker.postMessage({\n type: \"INIT\",\n payload: { modelPath, wasmPath },\n });\n\n return () => {\n worker.terminate();\n };\n }, [context.isBrowser, modelPath, wasmPath]);\n\n const generate = useCallback((prompt: string) => {\n if (!workerRef.current) {\n setError(\"Worker not initialized\");\n return;\n }\n\n setOutput(\"\");\n setIsLoading(true);\n setError(null);\n\n workerRef.current.postMessage({\n type: \"GENERATE\",\n payload: { prompt },\n });\n }, []);\n\n return {\n output,\n isLoading,\n progress,\n error,\n generate,\n };\n}\n","\"use client\";\n\nimport React, { createContext, useContext, useMemo } from \"react\";\nimport { isBrowser } from \"./utils\";\n\nexport { useLlm } from \"./genai\";\nexport type { UseLlmOptions } from \"./genai\";\n\nexport interface MediaPipeContextType {\n wasmPath?: string;\n modelPath?: string;\n isBrowser: boolean;\n}\n\nconst MediaPipeContext = createContext<MediaPipeContextType | null>(null);\n\nexport interface MediaPipeProviderProps {\n children: React.ReactNode;\n wasmPath?: string;\n modelPath?: string;\n}\n\nexport const MediaPipeProvider: React.FC<MediaPipeProviderProps> = ({\n children,\n wasmPath,\n modelPath,\n}) => {\n const value = useMemo(\n () => ({\n wasmPath,\n modelPath,\n isBrowser,\n }),\n [wasmPath, modelPath],\n );\n\n return (\n <MediaPipeContext.Provider value={value}>\n {children}\n </MediaPipeContext.Provider>\n );\n};\n\nexport const useMediaPipeContext = () => {\n const context = useContext(MediaPipeContext);\n if (!context) {\n throw new Error(\n \"useMediaPipeContext must be used within a MediaPipeProvider\",\n );\n }\n return context;\n};\n","export const isBrowser = typeof window !== \"undefined\";\n"],"mappings":";AAEA,SAAS,WAAW,UAAU,aAAa,cAAc;;;ACAzD,SAAgB,eAAe,YAAY,eAAe;;;ACFnD,IAAM,YAAY,OAAO,WAAW;;;ADqCvC;AAvBJ,IAAM,mBAAmB,cAA2C,IAAI;AAQjE,IAAM,oBAAsD,CAAC;AAAA,EAClE;AAAA,EACA;AAAA,EACA;AACF,MAAM;AACJ,QAAM,QAAQ;AAAA,IACZ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C,CAAC;AAEL,SAAO;AAAA,IACL;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,EACF;AACF;","names":[]}
1
+ {"version":3,"sources":["../src/genai.ts","../src/index.tsx","../src/utils.ts"],"sourcesContent":["\"use client\";\n\nimport { useEffect, useState, useCallback, useRef } from \"react\";\nimport { useMediaPipeContext } from \"./index\";\n\n/**\n * The Web Worker logic for MediaPipe GenAI.\n * This is stringified so it can be easily initialized as a Blob URL if the file-based worker fails.\n */\nconst workerScript = `\nimport { LlmInference, FilesetResolver } from 'https://cdn.jsdelivr.net/npm/@mediapipe/tasks-genai';\n\nlet llmInference = null;\n\nasync function checkGpuSupport() {\n if (!('gpu' in navigator)) {\n throw new Error('WebGPU is not supported in this browser.');\n }\n const gpu = navigator.gpu;\n const adapter = await gpu.requestAdapter();\n if (!adapter) {\n throw new Error('No appropriate GPU adapter found.');\n }\n}\n\nasync function initInference(modelPath, wasmPath) {\n try {\n await checkGpuSupport();\n const genai = await FilesetResolver.forGenAiTasks(wasmPath);\n llmInference = await LlmInference.createFromOptions(genai, {\n baseOptions: { modelAssetPath: modelPath },\n });\n self.postMessage({ type: 'INIT_COMPLETE' });\n } catch (error) {\n self.postMessage({ type: 'ERROR', error: error.message || 'Unknown error during initialization' });\n }\n}\n\nself.onmessage = async (event) => {\n const { type, payload } = event.data;\n\n if (type === 'INIT') {\n const { modelPath, wasmPath } = payload;\n await initInference(modelPath, wasmPath);\n }\n\n if (type === 'GENERATE') {\n if (!llmInference) {\n self.postMessage({ type: 'ERROR', error: 'LLM Inference not initialized. Please ensure the model is loaded and WebGPU is supported.' });\n return;\n }\n\n try {\n const { prompt } = payload;\n llmInference.generateResponse(prompt, (partialText, done) => {\n self.postMessage({\n type: 'CHUNK',\n payload: { text: partialText, done }\n });\n });\n } catch (error) {\n self.postMessage({ type: 'ERROR', error: error.message || 'Error generating response' });\n }\n }\n};\n`;\n\nexport interface UseLlmOptions {\n modelPath?: string;\n wasmPath?: string;\n}\n\nexport function useLlm(options: UseLlmOptions = {}) {\n const context = useMediaPipeContext();\n const [output, setOutput] = useState(\"\");\n const [isLoading, setIsLoading] = useState(false);\n const [error, setError] = useState<string | null>(null);\n const [progress, setProgress] = useState(0);\n\n const workerRef = useRef<Worker | null>(null);\n\n // Use values from props if provided, otherwise fallback to context\n const modelPath = options.modelPath || context.modelPath;\n const wasmPath =\n options.wasmPath ||\n context.wasmPath ||\n \"https://cdn.jsdelivr.net/npm/@mediapipe/tasks-genai/wasm\";\n\n useEffect(() => {\n if (!context.isBrowser || !modelPath) return;\n\n // Early check for WebGPU support in the UI thread too\n if (!(\"gpu\" in navigator)) {\n setTimeout(() => setError(\"WebGPU is not supported in this browser.\"), 0);\n return;\n }\n\n let worker: Worker;\n\n try {\n // Attempt to load from the separate worker file (Vite/Next.js/Webpack friendly)\n worker = new Worker(new URL(\"./genai.worker\", import.meta.url), {\n type: \"module\",\n name: \"mediapipe-genai-worker\",\n });\n } catch (_e) {\n // Fallback to Blob-based worker if relative path fails\n console.warn(\"MediaPipe React: Falling back to Blob-based GenAI worker\");\n const blob = new Blob([workerScript], { type: \"application/javascript\" });\n worker = new Worker(URL.createObjectURL(blob));\n }\n\n workerRef.current = worker;\n\n worker.onmessage = (event) => {\n const { type, payload, error: workerError } = event.data;\n\n switch (type) {\n case \"INIT_COMPLETE\":\n setIsLoading(false);\n setProgress(100);\n break;\n case \"CHUNK\":\n setOutput((prev) => prev + payload.text);\n if (payload.done) {\n setIsLoading(false);\n }\n break;\n case \"ERROR\":\n setError(workerError || \"Worker encountered an error\");\n setIsLoading(false);\n break;\n }\n };\n\n setTimeout(() => {\n setIsLoading(true);\n setProgress(10); // Initial progress\n }, 0);\n\n worker.postMessage({\n type: \"INIT\",\n payload: { modelPath, wasmPath },\n });\n\n return () => {\n worker.terminate();\n };\n }, [context.isBrowser, modelPath, wasmPath]);\n\n const generate = useCallback((prompt: string) => {\n if (!workerRef.current) {\n setError(\"Worker not initialized\");\n return;\n }\n\n setOutput(\"\");\n setIsLoading(true);\n setError(null);\n\n workerRef.current.postMessage({\n type: \"GENERATE\",\n payload: { prompt },\n });\n }, []);\n\n return {\n output,\n isLoading,\n progress,\n error,\n generate,\n };\n}\n","\"use client\";\n\n// Dummy change to verify release workflow\n\nimport React, { createContext, useContext, useMemo } from \"react\";\nimport { isBrowser } from \"./utils\";\n\nexport { useLlm } from \"./genai\";\nexport type { UseLlmOptions } from \"./genai\";\n\nexport interface MediaPipeContextType {\n wasmPath?: string;\n modelPath?: string;\n isBrowser: boolean;\n}\n\nconst MediaPipeContext = createContext<MediaPipeContextType | null>(null);\n\nexport interface MediaPipeProviderProps {\n children: React.ReactNode;\n wasmPath?: string;\n modelPath?: string;\n}\n\nexport const MediaPipeProvider: React.FC<MediaPipeProviderProps> = ({\n children,\n wasmPath,\n modelPath,\n}) => {\n const value = useMemo(\n () => ({\n wasmPath,\n modelPath,\n isBrowser,\n }),\n [wasmPath, modelPath],\n );\n\n return (\n <MediaPipeContext.Provider value={value}>\n {children}\n </MediaPipeContext.Provider>\n );\n};\n\nexport const useMediaPipeContext = () => {\n const context = useContext(MediaPipeContext);\n if (!context) {\n throw new Error(\n \"useMediaPipeContext must be used within a MediaPipeProvider\",\n );\n }\n return context;\n};\n","export const isBrowser = typeof window !== \"undefined\";\n"],"mappings":";AAEA,SAAS,WAAW,UAAU,aAAa,cAAc;;;ACEzD,SAAgB,eAAe,YAAY,eAAe;;;ACJnD,IAAM,YAAY,OAAO,WAAW;;;ADuCvC;AAvBJ,IAAM,mBAAmB,cAA2C,IAAI;AAQjE,IAAM,oBAAsD,CAAC;AAAA,EAClE;AAAA,EACA;AAAA,EACA;AACF,MAAM;AACJ,QAAM,QAAQ;AAAA,IACZ,OAAO;AAAA,MACL;AAAA,MACA;AAAA,MACA;AAAA,IACF;AAAA,IACA,CAAC,UAAU,SAAS;AAAA,EACtB;AAEA,SACE,oBAAC,iBAAiB,UAAjB,EAA0B,OACxB,UACH;AAEJ;AAEO,IAAM,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= useLlm; exports.MediaPipeProvider = MediaPipeProvider; exports.useMediaPipeContext = useMediaPipeContext;
181
- //# sourceMappingURL=chunk-OURWDMTD.js.map
181
+ //# sourceMappingURL=chunk-LU5G37I6.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["/home/runner/work/mediapipe-react/mediapipe-react/packages/core/dist/chunk-LU5G37I6.js","../src/genai.ts","../src/index.tsx","../src/utils.ts"],"names":[],"mappings":"AAAA;ACEA,8BAAyD;ADAzD;AACA;AECA;AFCA;AACA;AGNO,IAAM,UAAA,EAAY,OAAO,OAAA,IAAW,WAAA;AHQ3C;AACA;AE8BI,+CAAA;AAvBJ,IAAM,iBAAA,EAAmB,kCAAA,IAA+C,CAAA;AAQjE,IAAM,kBAAA,EAAsD,CAAC;AAAA,EAClE,QAAA;AAAA,EACA,QAAA;AAAA,EACA;AACF,CAAA,EAAA,GAAM;AACJ,EAAA,MAAM,MAAA,EAAQ,4BAAA;AAAA,IACZ,CAAA,EAAA,GAAA,CAAO;AAAA,MACL,QAAA;AAAA,MACA,SAAA;AAAA,MACA;AAAA,IACF,CAAA,CAAA;AAAA,IACA,CAAC,QAAA,EAAU,SAAS;AAAA,EACtB,CAAA;AAEA,EAAA,uBACE,6BAAA,gBAAC,CAAiB,QAAA,EAAjB,EAA0B,KAAA,EACxB,SAAA,CACH,CAAA;AAEJ,CAAA;AAEO,IAAM,oBAAA,EAAsB,CAAA,EAAA,GAAM;AACvC,EAAA,MAAM,QAAA,EAAU,+BAAA,gBAA2B,CAAA;AAC3C,EAAA,GAAA,CAAI,CAAC,OAAA,EAAS;AACZ,IAAA,MAAM,IAAI,KAAA;AAAA,MACR;AAAA,IACF,CAAA;AAAA,EACF;AACA,EAAA,OAAO,OAAA;AACT,CAAA;AFjBA;AACA;AC5BA,IAAM,aAAA,EAAe,CAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAA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GAAM;AACf,MAAA,YAAA,CAAa,IAAI,CAAA;AACjB,MAAA,WAAA,CAAY,EAAE,CAAA;AAAA,IAChB,CAAA,EAAG,CAAC,CAAA;AAEJ,IAAA,MAAA,CAAO,WAAA,CAAY;AAAA,MACjB,IAAA,EAAM,MAAA;AAAA,MACN,OAAA,EAAS,EAAE,SAAA,EAAW,SAAS;AAAA,IACjC,CAAC,CAAA;AAED,IAAA,OAAO,CAAA,EAAA,GAAM;AACX,MAAA,MAAA,CAAO,SAAA,CAAU,CAAA;AAAA,IACnB,CAAA;AAAA,EACF,CAAA,EAAG,CAAC,OAAA,CAAQ,SAAA,EAAW,SAAA,EAAW,QAAQ,CAAC,CAAA;AAE3C,EAAA,MAAM,SAAA,EAAW,gCAAA,CAAa,MAAA,EAAA,GAAmB;AAC/C,IAAA,GAAA,CAAI,CAAC,SAAA,CAAU,OAAA,EAAS;AACtB,MAAA,QAAA,CAAS,wBAAwB,CAAA;AACjC,MAAA,MAAA;AAAA,IACF;AAEA,IAAA,SAAA,CAAU,EAAE,CAAA;AACZ,IAAA,YAAA,CAAa,IAAI,CAAA;AACjB,IAAA,QAAA,CAAS,IAAI,CAAA;AAEb,IAAA,SAAA,CAAU,OAAA,CAAQ,WAAA,CAAY;AAAA,MAC5B,IAAA,EAAM,UAAA;AAAA,MACN,OAAA,EAAS,EAAE,OAAO;AAAA,IACpB,CAAC,CAAA;AAAA,EACH,CAAA,EAAG,CAAC,CAAC,CAAA;AAEL,EAAA,OAAO;AAAA,IACL,MAAA;AAAA,IACA,SAAA;AAAA,IACA,QAAA;AAAA,IACA,KAAA;AAAA,IACA;AAAA,EACF,CAAA;AACF;ADCA;AACA;AACE;AACA;AACA;AACF,0HAAC","file":"/home/runner/work/mediapipe-react/mediapipe-react/packages/core/dist/chunk-LU5G37I6.js","sourcesContent":[null,"\"use client\";\n\nimport { useEffect, useState, useCallback, useRef } from \"react\";\nimport { useMediaPipeContext } from \"./index\";\n\n/**\n * The Web Worker logic for MediaPipe GenAI.\n * This is stringified so it can be easily initialized as a Blob URL if the file-based worker fails.\n */\nconst workerScript = `\nimport { LlmInference, FilesetResolver } from 'https://cdn.jsdelivr.net/npm/@mediapipe/tasks-genai';\n\nlet llmInference = null;\n\nasync function checkGpuSupport() {\n if (!('gpu' in navigator)) {\n throw new Error('WebGPU is not supported in this browser.');\n }\n const gpu = navigator.gpu;\n const adapter = await gpu.requestAdapter();\n if (!adapter) {\n throw new Error('No appropriate GPU adapter found.');\n }\n}\n\nasync function initInference(modelPath, wasmPath) {\n try {\n await checkGpuSupport();\n const genai = await FilesetResolver.forGenAiTasks(wasmPath);\n llmInference = await LlmInference.createFromOptions(genai, {\n baseOptions: { modelAssetPath: modelPath },\n });\n self.postMessage({ type: 'INIT_COMPLETE' });\n } catch (error) {\n self.postMessage({ type: 'ERROR', error: error.message || 'Unknown error during initialization' });\n }\n}\n\nself.onmessage = async (event) => {\n const { type, payload } = event.data;\n\n if (type === 'INIT') {\n const { modelPath, wasmPath } = payload;\n await initInference(modelPath, wasmPath);\n }\n\n if (type === 'GENERATE') {\n if (!llmInference) {\n self.postMessage({ type: 'ERROR', error: 'LLM Inference not initialized. Please ensure the model is loaded and WebGPU is supported.' });\n return;\n }\n\n try {\n const { prompt } = payload;\n llmInference.generateResponse(prompt, (partialText, done) => {\n self.postMessage({\n type: 'CHUNK',\n payload: { text: partialText, done }\n });\n });\n } catch (error) {\n self.postMessage({ type: 'ERROR', error: error.message || 'Error generating response' });\n }\n }\n};\n`;\n\nexport interface UseLlmOptions {\n modelPath?: string;\n wasmPath?: string;\n}\n\nexport function useLlm(options: UseLlmOptions = {}) {\n const context = useMediaPipeContext();\n const [output, setOutput] = useState(\"\");\n const [isLoading, setIsLoading] = useState(false);\n const [error, setError] = useState<string | null>(null);\n const [progress, setProgress] = useState(0);\n\n const workerRef = useRef<Worker | null>(null);\n\n // Use values from props if provided, otherwise fallback to context\n const modelPath = options.modelPath || context.modelPath;\n const wasmPath =\n options.wasmPath ||\n context.wasmPath ||\n \"https://cdn.jsdelivr.net/npm/@mediapipe/tasks-genai/wasm\";\n\n useEffect(() => {\n if (!context.isBrowser || !modelPath) return;\n\n // Early check for WebGPU support in the UI thread too\n if (!(\"gpu\" in navigator)) {\n setTimeout(() => setError(\"WebGPU is not supported in this browser.\"), 0);\n return;\n }\n\n let worker: Worker;\n\n try {\n // Attempt to load from the separate worker file (Vite/Next.js/Webpack friendly)\n worker = new Worker(new URL(\"./genai.worker\", import.meta.url), {\n type: \"module\",\n name: \"mediapipe-genai-worker\",\n });\n } catch (_e) {\n // Fallback to Blob-based worker if relative path fails\n console.warn(\"MediaPipe React: Falling back to Blob-based GenAI worker\");\n const blob = new Blob([workerScript], { type: \"application/javascript\" });\n worker = new Worker(URL.createObjectURL(blob));\n }\n\n workerRef.current = worker;\n\n worker.onmessage = (event) => {\n const { type, payload, error: workerError } = event.data;\n\n switch (type) {\n case \"INIT_COMPLETE\":\n setIsLoading(false);\n setProgress(100);\n break;\n case \"CHUNK\":\n setOutput((prev) => prev + payload.text);\n if (payload.done) {\n setIsLoading(false);\n }\n break;\n case \"ERROR\":\n setError(workerError || \"Worker encountered an error\");\n setIsLoading(false);\n break;\n }\n };\n\n setTimeout(() => {\n setIsLoading(true);\n setProgress(10); // Initial progress\n }, 0);\n\n worker.postMessage({\n type: \"INIT\",\n payload: { modelPath, wasmPath },\n });\n\n return () => {\n worker.terminate();\n };\n }, [context.isBrowser, modelPath, wasmPath]);\n\n const generate = useCallback((prompt: string) => {\n if (!workerRef.current) {\n setError(\"Worker not initialized\");\n return;\n }\n\n setOutput(\"\");\n setIsLoading(true);\n setError(null);\n\n workerRef.current.postMessage({\n type: \"GENERATE\",\n payload: { prompt },\n });\n }, []);\n\n return {\n output,\n isLoading,\n progress,\n error,\n generate,\n };\n}\n","\"use client\";\n\n// Dummy change to verify release workflow\n\nimport React, { createContext, useContext, useMemo } from \"react\";\nimport { isBrowser } from \"./utils\";\n\nexport { useLlm } from \"./genai\";\nexport type { UseLlmOptions } from \"./genai\";\n\nexport interface MediaPipeContextType {\n wasmPath?: string;\n modelPath?: string;\n isBrowser: boolean;\n}\n\nconst MediaPipeContext = createContext<MediaPipeContextType | null>(null);\n\nexport interface MediaPipeProviderProps {\n children: React.ReactNode;\n wasmPath?: string;\n modelPath?: string;\n}\n\nexport const MediaPipeProvider: React.FC<MediaPipeProviderProps> = ({\n children,\n wasmPath,\n modelPath,\n}) => {\n const value = useMemo(\n () => ({\n wasmPath,\n modelPath,\n isBrowser,\n }),\n [wasmPath, modelPath],\n );\n\n return (\n <MediaPipeContext.Provider value={value}>\n {children}\n </MediaPipeContext.Provider>\n );\n};\n\nexport const useMediaPipeContext = () => {\n const context = useContext(MediaPipeContext);\n if (!context) {\n throw new Error(\n \"useMediaPipeContext must be used within a MediaPipeProvider\",\n );\n }\n return context;\n};\n","export const isBrowser = typeof window !== \"undefined\";\n"]}
package/dist/genai.js CHANGED
@@ -1,8 +1,8 @@
1
1
  "use strict";Object.defineProperty(exports, "__esModule", {value: true});"use client";
2
2
 
3
3
 
4
- var _chunkOURWDMTDjs = require('./chunk-OURWDMTD.js');
4
+ var _chunkLU5G37I6js = require('./chunk-LU5G37I6.js');
5
5
 
6
6
 
7
- exports.useLlm = _chunkOURWDMTDjs.useLlm;
7
+ exports.useLlm = _chunkLU5G37I6js.useLlm;
8
8
  //# sourceMappingURL=genai.js.map
package/dist/genai.mjs CHANGED
@@ -1,7 +1,7 @@
1
1
  "use client";
2
2
  import {
3
3
  useLlm
4
- } from "./chunk-73Z6K33X.mjs";
4
+ } from "./chunk-F4K45QYO.mjs";
5
5
  export {
6
6
  useLlm
7
7
  };
package/dist/index.js CHANGED
@@ -3,10 +3,10 @@
3
3
 
4
4
 
5
5
 
6
- var _chunkOURWDMTDjs = require('./chunk-OURWDMTD.js');
6
+ var _chunkLU5G37I6js = require('./chunk-LU5G37I6.js');
7
7
 
8
8
 
9
9
 
10
10
 
11
- exports.MediaPipeProvider = _chunkOURWDMTDjs.MediaPipeProvider; exports.useLlm = _chunkOURWDMTDjs.useLlm; exports.useMediaPipeContext = _chunkOURWDMTDjs.useMediaPipeContext;
11
+ exports.MediaPipeProvider = _chunkLU5G37I6js.MediaPipeProvider; exports.useLlm = _chunkLU5G37I6js.useLlm; exports.useMediaPipeContext = _chunkLU5G37I6js.useMediaPipeContext;
12
12
  //# sourceMappingURL=index.js.map
package/dist/index.mjs CHANGED
@@ -3,7 +3,7 @@ import {
3
3
  MediaPipeProvider,
4
4
  useLlm,
5
5
  useMediaPipeContext
6
- } from "./chunk-73Z6K33X.mjs";
6
+ } from "./chunk-F4K45QYO.mjs";
7
7
  export {
8
8
  MediaPipeProvider,
9
9
  useLlm,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ismail-kattakath/mediapipe-react",
3
- "version": "0.1.2",
3
+ "version": "0.1.3",
4
4
  "license": "MIT",
5
5
  "repository": {
6
6
  "type": "git",
@@ -1 +0,0 @@
1
- {"version":3,"sources":["/home/runner/work/mediapipe-react/mediapipe-react/packages/core/dist/chunk-OURWDMTD.js","../src/genai.ts","../src/index.tsx","../src/utils.ts"],"names":[],"mappings":"AAAA;ACEA,8BAAyD;ADAzD;AACA;AEDA;AFGA;AACA;AGNO,IAAM,UAAA,EAAY,OAAO,OAAA,IAAW,WAAA;AHQ3C;AACA;AE4BI,+CAAA;AAvBJ,IAAM,iBAAA,EAAmB,kCAAA,IAA+C,CAAA;AAQjE,IAAM,kBAAA,EAAsD,CAAC;AAAA,EAClE,QAAA;AAAA,EACA,QAAA;AAAA,EACA;AACF,CAAA,EAAA,GAAM;AACJ,EAAA,MAAM,MAAA,EAAQ,4BAAA;AAAA,IACZ,CAAA,EAAA,GAAA,CAAO;AAAA,MACL,QAAA;AAAA,MACA,SAAA;AAAA,MACA;AAAA,IACF,CAAA,CAAA;AAAA,IACA,CAAC,QAAA,EAAU,SAAS;AAAA,EACtB,CAAA;AAEA,EAAA,uBACE,6BAAA,gBAAC,CAAiB,QAAA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AA,EAAM,UAAA;AAAA,MACN,OAAA,EAAS,EAAE,OAAO;AAAA,IACpB,CAAC,CAAA;AAAA,EACH,CAAA,EAAG,CAAC,CAAC,CAAA;AAEL,EAAA,OAAO;AAAA,IACL,MAAA;AAAA,IACA,SAAA;AAAA,IACA,QAAA;AAAA,IACA,KAAA;AAAA,IACA;AAAA,EACF,CAAA;AACF;ADCA;AACA;AACE;AACA;AACA;AACF,0HAAC","file":"/home/runner/work/mediapipe-react/mediapipe-react/packages/core/dist/chunk-OURWDMTD.js","sourcesContent":[null,"\"use client\";\n\nimport { useEffect, useState, useCallback, useRef } from \"react\";\nimport { useMediaPipeContext } from \"./index\";\n\n/**\n * The Web Worker logic for MediaPipe GenAI.\n * This is stringified so it can be easily initialized as a Blob URL if the file-based worker fails.\n */\nconst workerScript = `\nimport { LlmInference, FilesetResolver } from 'https://cdn.jsdelivr.net/npm/@mediapipe/tasks-genai';\n\nlet llmInference = null;\n\nasync function checkGpuSupport() {\n if (!('gpu' in navigator)) {\n throw new Error('WebGPU is not supported in this browser.');\n }\n const gpu = navigator.gpu;\n const adapter = await gpu.requestAdapter();\n if (!adapter) {\n throw new Error('No appropriate GPU adapter found.');\n }\n}\n\nasync function initInference(modelPath, wasmPath) {\n try {\n await checkGpuSupport();\n const genai = await FilesetResolver.forGenAiTasks(wasmPath);\n llmInference = await LlmInference.createFromOptions(genai, {\n baseOptions: { modelAssetPath: modelPath },\n });\n self.postMessage({ type: 'INIT_COMPLETE' });\n } catch (error) {\n self.postMessage({ type: 'ERROR', error: error.message || 'Unknown error during initialization' });\n }\n}\n\nself.onmessage = async (event) => {\n const { type, payload } = event.data;\n\n if (type === 'INIT') {\n const { modelPath, wasmPath } = payload;\n await initInference(modelPath, wasmPath);\n }\n\n if (type === 'GENERATE') {\n if (!llmInference) {\n self.postMessage({ type: 'ERROR', error: 'LLM Inference not initialized. Please ensure the model is loaded and WebGPU is supported.' });\n return;\n }\n\n try {\n const { prompt } = payload;\n llmInference.generateResponse(prompt, (partialText, done) => {\n self.postMessage({\n type: 'CHUNK',\n payload: { text: partialText, done }\n });\n });\n } catch (error) {\n self.postMessage({ type: 'ERROR', error: error.message || 'Error generating response' });\n }\n }\n};\n`;\n\nexport interface UseLlmOptions {\n modelPath?: string;\n wasmPath?: string;\n}\n\nexport function useLlm(options: UseLlmOptions = {}) {\n const context = useMediaPipeContext();\n const [output, setOutput] = useState(\"\");\n const [isLoading, setIsLoading] = useState(false);\n const [error, setError] = useState<string | null>(null);\n const [progress, setProgress] = useState(0);\n\n const workerRef = useRef<Worker | null>(null);\n\n // Use values from props if provided, otherwise fallback to context\n const modelPath = options.modelPath || context.modelPath;\n const wasmPath =\n options.wasmPath ||\n context.wasmPath ||\n \"https://cdn.jsdelivr.net/npm/@mediapipe/tasks-genai/wasm\";\n\n useEffect(() => {\n if (!context.isBrowser || !modelPath) return;\n\n // Early check for WebGPU support in the UI thread too\n if (!(\"gpu\" in navigator)) {\n setTimeout(() => setError(\"WebGPU is not supported in this browser.\"), 0);\n return;\n }\n\n let worker: Worker;\n\n try {\n // Attempt to load from the separate worker file (Vite/Next.js/Webpack friendly)\n worker = new Worker(new URL(\"./genai.worker\", import.meta.url), {\n type: \"module\",\n name: \"mediapipe-genai-worker\",\n });\n } catch (_e) {\n // Fallback to Blob-based worker if relative path fails\n console.warn(\"MediaPipe React: Falling back to Blob-based GenAI worker\");\n const blob = new Blob([workerScript], { type: \"application/javascript\" });\n worker = new Worker(URL.createObjectURL(blob));\n }\n\n workerRef.current = worker;\n\n worker.onmessage = (event) => {\n const { type, payload, error: workerError } = event.data;\n\n switch (type) {\n case \"INIT_COMPLETE\":\n setIsLoading(false);\n setProgress(100);\n break;\n case \"CHUNK\":\n setOutput((prev) => prev + payload.text);\n if (payload.done) {\n setIsLoading(false);\n }\n break;\n case \"ERROR\":\n setError(workerError || \"Worker encountered an error\");\n setIsLoading(false);\n break;\n }\n };\n\n setTimeout(() => {\n setIsLoading(true);\n setProgress(10); // Initial progress\n }, 0);\n\n worker.postMessage({\n type: \"INIT\",\n payload: { modelPath, wasmPath },\n });\n\n return () => {\n worker.terminate();\n };\n }, [context.isBrowser, modelPath, wasmPath]);\n\n const generate = useCallback((prompt: string) => {\n if (!workerRef.current) {\n setError(\"Worker not initialized\");\n return;\n }\n\n setOutput(\"\");\n setIsLoading(true);\n setError(null);\n\n workerRef.current.postMessage({\n type: \"GENERATE\",\n payload: { prompt },\n });\n }, []);\n\n return {\n output,\n isLoading,\n progress,\n error,\n generate,\n };\n}\n","\"use client\";\n\nimport React, { createContext, useContext, useMemo } from \"react\";\nimport { isBrowser } from \"./utils\";\n\nexport { useLlm } from \"./genai\";\nexport type { UseLlmOptions } from \"./genai\";\n\nexport interface MediaPipeContextType {\n wasmPath?: string;\n modelPath?: string;\n isBrowser: boolean;\n}\n\nconst MediaPipeContext = createContext<MediaPipeContextType | null>(null);\n\nexport interface MediaPipeProviderProps {\n children: React.ReactNode;\n wasmPath?: string;\n modelPath?: string;\n}\n\nexport const MediaPipeProvider: React.FC<MediaPipeProviderProps> = ({\n children,\n wasmPath,\n modelPath,\n}) => {\n const value = useMemo(\n () => ({\n wasmPath,\n modelPath,\n isBrowser,\n }),\n [wasmPath, modelPath],\n );\n\n return (\n <MediaPipeContext.Provider value={value}>\n {children}\n </MediaPipeContext.Provider>\n );\n};\n\nexport const useMediaPipeContext = () => {\n const context = useContext(MediaPipeContext);\n if (!context) {\n throw new Error(\n \"useMediaPipeContext must be used within a MediaPipeProvider\",\n );\n }\n return context;\n};\n","export const isBrowser = typeof window !== \"undefined\";\n"]}