Update dependencies and refactor useMessage hook
This commit is contained in:
		
							parent
							
								
									01d27fd1c2
								
							
						
					
					
						commit
						2381588e72
					
				
							
								
								
									
										10
									
								
								package.json
									
									
									
									
									
								
							
							
						
						
									
										10
									
								
								package.json
									
									
									
									
									
								
							| @ -18,8 +18,7 @@ | |||||||
|     "@ant-design/cssinjs": "^1.18.4", |     "@ant-design/cssinjs": "^1.18.4", | ||||||
|     "@headlessui/react": "^1.7.18", |     "@headlessui/react": "^1.7.18", | ||||||
|     "@heroicons/react": "^2.1.1", |     "@heroicons/react": "^2.1.1", | ||||||
|     "@langchain/community": "^0.0.21", |     "@langchain/community": "^0.0.41", | ||||||
|     "@langchain/core": "^0.1.22", |  | ||||||
|     "@mantine/form": "^7.5.0", |     "@mantine/form": "^7.5.0", | ||||||
|     "@mantine/hooks": "^7.5.3", |     "@mantine/hooks": "^7.5.3", | ||||||
|     "@plasmohq/storage": "^1.9.0", |     "@plasmohq/storage": "^1.9.0", | ||||||
| @ -33,8 +32,9 @@ | |||||||
|     "html-to-text": "^9.0.5", |     "html-to-text": "^9.0.5", | ||||||
|     "i18next": "^23.10.1", |     "i18next": "^23.10.1", | ||||||
|     "i18next-browser-languagedetector": "^7.2.0", |     "i18next-browser-languagedetector": "^7.2.0", | ||||||
|     "langchain": "^0.1.9", |     "langchain": "^0.1.28", | ||||||
|     "lucide-react": "^0.350.0", |     "lucide-react": "^0.350.0", | ||||||
|  |     "pdfjs-dist": "^4.0.379", | ||||||
|     "property-information": "^6.4.1", |     "property-information": "^6.4.1", | ||||||
|     "react": "18.2.0", |     "react": "18.2.0", | ||||||
|     "react-dom": "18.2.0", |     "react-dom": "18.2.0", | ||||||
| @ -62,6 +62,10 @@ | |||||||
|     "prettier": "3.2.4", |     "prettier": "3.2.4", | ||||||
|     "tailwindcss": "^3.4.1", |     "tailwindcss": "^3.4.1", | ||||||
|     "typescript": "5.3.3", |     "typescript": "5.3.3", | ||||||
|  |     "vite-plugin-top-level-await": "^1.4.1", | ||||||
|     "wxt": "^0.17.7" |     "wxt": "^0.17.7" | ||||||
|  |   }, | ||||||
|  |   "resolutions": { | ||||||
|  |     "@langchain/core": "0.1.45" | ||||||
|   } |   } | ||||||
| } | } | ||||||
|  | |||||||
| @ -16,7 +16,7 @@ import { | |||||||
|   type MessageContent, |   type MessageContent, | ||||||
|   SystemMessage |   SystemMessage | ||||||
| } from "@langchain/core/messages" | } from "@langchain/core/messages" | ||||||
| import { getHtmlOfCurrentTab } from "~/libs/get-html" | import { getDataFromCurrentTab } from "~/libs/get-html" | ||||||
| import { PageAssistHtmlLoader } from "~/loader/html" | import { PageAssistHtmlLoader } from "~/loader/html" | ||||||
| import { RecursiveCharacterTextSplitter } from "langchain/text_splitter" | import { RecursiveCharacterTextSplitter } from "langchain/text_splitter" | ||||||
| import { OllamaEmbeddings } from "@langchain/community/embeddings/ollama" | import { OllamaEmbeddings } from "@langchain/community/embeddings/ollama" | ||||||
| @ -167,7 +167,7 @@ export const useMessage = () => { | |||||||
|       let isAlreadyExistEmbedding: MemoryVectorStore |       let isAlreadyExistEmbedding: MemoryVectorStore | ||||||
|       let embedURL: string, embedHTML: string |       let embedURL: string, embedHTML: string | ||||||
|       if (messages.length === 0) { |       if (messages.length === 0) { | ||||||
|         const { html, url } = await getHtmlOfCurrentTab() |         const { content: html, url, type } = await getDataFromCurrentTab() | ||||||
|         embedHTML = html |         embedHTML = html | ||||||
|         embedURL = url |         embedURL = url | ||||||
|         setCurrentURL(url) |         setCurrentURL(url) | ||||||
|  | |||||||
| @ -1,15 +1,46 @@ | |||||||
|  | import { pdfDist } from "./pdfjs" | ||||||
| 
 | 
 | ||||||
| const _getHtml = () => { | export const getPdf = async (data: ArrayBuffer) => { | ||||||
|  |   const pdf = pdfDist.getDocument({ | ||||||
|  |     data, | ||||||
|  |     useWorkerFetch: false, | ||||||
|  |     isEvalSupported: false, | ||||||
|  |     useSystemFonts: true, | ||||||
|  |   }); | ||||||
|  | 
 | ||||||
|  |   pdf.onPassword = (callback: any) => { | ||||||
|  |     const password = prompt("Enter the password: ") | ||||||
|  |     if (!password) { | ||||||
|  |       throw new Error("Password required to open the PDF."); | ||||||
|  |     } | ||||||
|  |     callback(password); | ||||||
|  |   }; | ||||||
|  | 
 | ||||||
|  | 
 | ||||||
|  |   const pdfDocument = await pdf.promise; | ||||||
|  | 
 | ||||||
|  | 
 | ||||||
|  |   return pdfDocument | ||||||
|  | 
 | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | const _getHtml = async () => { | ||||||
|   const url = window.location.href |   const url = window.location.href | ||||||
|  |   // check the content type
 | ||||||
|  |   if (document.contentType === "application/pdf") { | ||||||
|  | 
 | ||||||
|  | 
 | ||||||
|  |     return { url, content: "", type: "pdf" } | ||||||
|  |   } | ||||||
|   const html = Array.from(document.querySelectorAll("script")).reduce( |   const html = Array.from(document.querySelectorAll("script")).reduce( | ||||||
|     (acc, script) => { |     (acc, script) => { | ||||||
|       return acc.replace(script.outerHTML, "") |       return acc.replace(script.outerHTML, "") | ||||||
|     }, |     }, | ||||||
|     document.documentElement.outerHTML |     document.documentElement.outerHTML | ||||||
|   ) |   ) | ||||||
|   return { url, html } |   return { url, content: html, type: "html" } | ||||||
| } | } | ||||||
| export const getHtmlOfCurrentTab = async () => { | export const getDataFromCurrentTab = async () => { | ||||||
|   const result = new Promise((resolve) => { |   const result = new Promise((resolve) => { | ||||||
|     chrome.tabs.query({ active: true, currentWindow: true }, async (tabs) => { |     chrome.tabs.query({ active: true, currentWindow: true }, async (tabs) => { | ||||||
|       const tab = tabs[0] |       const tab = tabs[0] | ||||||
| @ -25,9 +56,41 @@ export const getHtmlOfCurrentTab = async () => { | |||||||
|     }) |     }) | ||||||
|   }) as Promise<{ |   }) as Promise<{ | ||||||
|     url: string |     url: string | ||||||
|     html: string |     content: string | ||||||
|  |     type: string | ||||||
|   }> |   }> | ||||||
| 
 | 
 | ||||||
|   return result | 
 | ||||||
|  |   const { content, type, url } = await result | ||||||
|  | 
 | ||||||
|  |   if (type === "pdf") { | ||||||
|  |     const res = await fetch(url) | ||||||
|  |     const data = await res.arrayBuffer() | ||||||
|  |     let pdfHtml: string[] = [] | ||||||
|  |     const pdf = await getPdf(data) | ||||||
|  | 
 | ||||||
|  |     for (let i = 1; i <= pdf.numPages; i += 1) { | ||||||
|  |       const page = await pdf.getPage(i); | ||||||
|  |       const content = await page.getTextContent(); | ||||||
|  | 
 | ||||||
|  |       if (content?.items.length === 0) { | ||||||
|  |         continue; | ||||||
|  |       } | ||||||
|  | 
 | ||||||
|  |       const text = content?.items.map((item: any) => item.str).join("\n") | ||||||
|  |         .replace(/\x00/g, "").trim(); | ||||||
|  |       pdfHtml.push(`<div class="pdf-page">${text}</div>`) | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  | 
 | ||||||
|  |     return { | ||||||
|  |       url, | ||||||
|  |       content: pdfHtml.join(""), | ||||||
|  |       type: "html" | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |   } | ||||||
|  | 
 | ||||||
|  |   return { url, content, type } | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
|  | |||||||
							
								
								
									
										8
									
								
								src/libs/pdfjs.ts
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										8
									
								
								src/libs/pdfjs.ts
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,8 @@ | |||||||
|  | import * as pdfDist from "pdfjs-dist" | ||||||
|  | import * as pdfWorker from "pdfjs-dist/build/pdf.worker.mjs"; | ||||||
|  | 
 | ||||||
|  | pdfDist.GlobalWorkerOptions.workerSrc = pdfWorker | ||||||
|  | 
 | ||||||
|  | export { | ||||||
|  |     pdfDist | ||||||
|  | } | ||||||
| @ -1,10 +1,24 @@ | |||||||
| import { defineConfig } from "wxt" | import { defineConfig } from "wxt" | ||||||
| import react from "@vitejs/plugin-react" | import react from "@vitejs/plugin-react" | ||||||
|  | import topLevelAwait from "vite-plugin-top-level-await" | ||||||
| 
 | 
 | ||||||
| // See https://wxt.dev/api/config.html
 | // See https://wxt.dev/api/config.html
 | ||||||
| export default defineConfig({ | export default defineConfig({ | ||||||
|   vite: () => ({ |   vite: () => ({ | ||||||
|     plugins: [react()], |     plugins: [react(), | ||||||
|  |       topLevelAwait({ | ||||||
|  |         promiseExportName: '__tla', | ||||||
|  |         promiseImportName: i => `__tla_${i}`, | ||||||
|  |       }), | ||||||
|  |     ], | ||||||
|  |     build: { | ||||||
|  |       rollupOptions: { | ||||||
|  |         external: [ | ||||||
|  |           "langchain", | ||||||
|  |           "@langchain/community", | ||||||
|  |         ] | ||||||
|  |       } | ||||||
|  |     } | ||||||
|   }), |   }), | ||||||
|   entrypointsDir: "entries", |   entrypointsDir: "entries", | ||||||
|   srcDir: "src", |   srcDir: "src", | ||||||
| @ -16,7 +30,7 @@ export default defineConfig({ | |||||||
|     default_locale: 'en', |     default_locale: 'en', | ||||||
|     action: {}, |     action: {}, | ||||||
|     author: "n4ze3m", |     author: "n4ze3m", | ||||||
|     host_permissions: ["http://*/*", "https://*/*"], |     host_permissions: ["http://*/*", "https://*/*", "file://*/*"], | ||||||
|     commands: { |     commands: { | ||||||
|       _execute_action: { |       _execute_action: { | ||||||
|         suggested_key: { |         suggested_key: { | ||||||
|  | |||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user