NoteEditor.svelte 35 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262
  1. <script lang="ts">
  2. import { getContext, onDestroy, onMount, tick } from 'svelte';
  3. import { v4 as uuidv4 } from 'uuid';
  4. import heic2any from 'heic2any';
  5. import fileSaver from 'file-saver';
  6. const { saveAs } = fileSaver;
  7. import jsPDF from 'jspdf';
  8. import html2canvas from 'html2canvas-pro';
  9. const i18n = getContext('i18n');
  10. import { marked } from 'marked';
  11. import { toast } from 'svelte-sonner';
  12. import { goto } from '$app/navigation';
  13. import dayjs from '$lib/dayjs';
  14. import calendar from 'dayjs/plugin/calendar';
  15. import duration from 'dayjs/plugin/duration';
  16. import relativeTime from 'dayjs/plugin/relativeTime';
  17. dayjs.extend(calendar);
  18. dayjs.extend(duration);
  19. dayjs.extend(relativeTime);
  20. import { PaneGroup, Pane, PaneResizer } from 'paneforge';
  21. import { compressImage, copyToClipboard, splitStream } from '$lib/utils';
  22. import { WEBUI_API_BASE_URL, WEBUI_BASE_URL } from '$lib/constants';
  23. import { uploadFile } from '$lib/apis/files';
  24. import { chatCompletion, generateOpenAIChatCompletion } from '$lib/apis/openai';
  25. import { config, models, settings, showSidebar, socket, user } from '$lib/stores';
  26. import NotePanel from '$lib/components/notes/NotePanel.svelte';
  27. import MenuLines from '../icons/MenuLines.svelte';
  28. import ChatBubbleOval from '../icons/ChatBubbleOval.svelte';
  29. import Settings from './NoteEditor/Settings.svelte';
  30. import Chat from './NoteEditor/Chat.svelte';
  31. import AccessControlModal from '$lib/components/workspace/common/AccessControlModal.svelte';
  32. async function loadLocale(locales) {
  33. for (const locale of locales) {
  34. try {
  35. dayjs.locale(locale);
  36. break; // Stop after successfully loading the first available locale
  37. } catch (error) {
  38. console.error(`Could not load locale '${locale}':`, error);
  39. }
  40. }
  41. }
  42. // Assuming $i18n.languages is an array of language codes
  43. $: loadLocale($i18n.languages);
  44. import { deleteNoteById, getNoteById, updateNoteById } from '$lib/apis/notes';
  45. import RichTextInput from '../common/RichTextInput.svelte';
  46. import Spinner from '../common/Spinner.svelte';
  47. import MicSolid from '../icons/MicSolid.svelte';
  48. import VoiceRecording from '../chat/MessageInput/VoiceRecording.svelte';
  49. import DeleteConfirmDialog from '$lib/components/common/ConfirmDialog.svelte';
  50. import Calendar from '../icons/Calendar.svelte';
  51. import Users from '../icons/Users.svelte';
  52. import Image from '../common/Image.svelte';
  53. import FileItem from '../common/FileItem.svelte';
  54. import FilesOverlay from '../chat/MessageInput/FilesOverlay.svelte';
  55. import RecordMenu from './RecordMenu.svelte';
  56. import NoteMenu from './Notes/NoteMenu.svelte';
  57. import EllipsisHorizontal from '../icons/EllipsisHorizontal.svelte';
  58. import Sparkles from '../icons/Sparkles.svelte';
  59. import SparklesSolid from '../icons/SparklesSolid.svelte';
  60. import Tooltip from '../common/Tooltip.svelte';
  61. import Bars3BottomLeft from '../icons/Bars3BottomLeft.svelte';
  62. import ArrowUturnLeft from '../icons/ArrowUturnLeft.svelte';
  63. import ArrowUturnRight from '../icons/ArrowUturnRight.svelte';
  64. import Sidebar from '../common/Sidebar.svelte';
  65. import ArrowRight from '../icons/ArrowRight.svelte';
  66. import Cog6 from '../icons/Cog6.svelte';
  67. import AiMenu from './AIMenu.svelte';
  68. export let id: null | string = null;
  69. let editor = null;
  70. let note = null;
  71. const newNote = {
  72. title: '',
  73. data: {
  74. content: {
  75. json: null,
  76. html: '',
  77. md: ''
  78. },
  79. versions: [],
  80. files: null
  81. },
  82. // pages: [], // TODO: Implement pages for notes to allow users to create multiple pages in a note
  83. meta: null,
  84. access_control: {}
  85. };
  86. let files = [];
  87. let messages = [];
  88. let wordCount = 0;
  89. let charCount = 0;
  90. let versionIdx = null;
  91. let selectedModelId = null;
  92. let recording = false;
  93. let displayMediaRecord = false;
  94. let showPanel = false;
  95. let selectedPanel = 'chat';
  96. let showDeleteConfirm = false;
  97. let showAccessControlModal = false;
  98. let titleInputFocused = false;
  99. let titleGenerating = false;
  100. let dragged = false;
  101. let loading = false;
  102. let editing = false;
  103. let streaming = false;
  104. let stopResponseFlag = false;
  105. let inputElement = null;
  106. const init = async () => {
  107. loading = true;
  108. const res = await getNoteById(localStorage.token, id).catch((error) => {
  109. toast.error(`${error}`);
  110. return null;
  111. });
  112. messages = [];
  113. if (res) {
  114. note = res;
  115. files = res.data.files || [];
  116. } else {
  117. goto('/');
  118. return;
  119. }
  120. loading = false;
  121. };
  122. let debounceTimeout: NodeJS.Timeout | null = null;
  123. const changeDebounceHandler = () => {
  124. if (debounceTimeout) {
  125. clearTimeout(debounceTimeout);
  126. }
  127. debounceTimeout = setTimeout(async () => {
  128. const res = await updateNoteById(localStorage.token, id, {
  129. title: note?.title === '' ? $i18n.t('Untitled') : note.title,
  130. access_control: note?.access_control
  131. }).catch((e) => {
  132. toast.error(`${e}`);
  133. });
  134. }, 200);
  135. };
  136. $: if (id) {
  137. init();
  138. }
  139. function areContentsEqual(a, b) {
  140. return JSON.stringify(a) === JSON.stringify(b);
  141. }
  142. function insertNoteVersion(note) {
  143. const current = {
  144. json: note.data.content.json,
  145. html: note.data.content.html,
  146. md: note.data.content.md
  147. };
  148. const lastVersion = note.data.versions?.at(-1);
  149. if (!lastVersion || !areContentsEqual(lastVersion, current)) {
  150. note.data.versions = (note.data.versions ?? []).concat(current);
  151. return true;
  152. }
  153. return false;
  154. }
  155. const onEdited = async () => {
  156. if (!editor) return;
  157. editor.commands.setContent(note.data.content.html);
  158. };
  159. const generateTitleHandler = async () => {
  160. const content = note.data.content.md;
  161. const DEFAULT_TITLE_GENERATION_PROMPT_TEMPLATE = `### Task:
  162. Generate a concise, 3-5 word title with an emoji summarizing the content in the content's primary language.
  163. ### Guidelines:
  164. - The title should clearly represent the main theme or subject of the content.
  165. - Use emojis that enhance understanding of the topic, but avoid quotation marks or special formatting.
  166. - Write the title in the content's primary language.
  167. - Prioritize accuracy over excessive creativity; keep it clear and simple.
  168. - Your entire response must consist solely of the JSON object, without any introductory or concluding text.
  169. - The output must be a single, raw JSON object, without any markdown code fences or other encapsulating text.
  170. - Ensure no conversational text, affirmations, or explanations precede or follow the raw JSON output, as this will cause direct parsing failure.
  171. ### Output:
  172. JSON format: { "title": "your concise title here" }
  173. ### Examples:
  174. - { "title": "📉 Stock Market Trends" },
  175. - { "title": "🍪 Perfect Chocolate Chip Recipe" },
  176. - { "title": "Evolution of Music Streaming" },
  177. - { "title": "Remote Work Productivity Tips" },
  178. - { "title": "Artificial Intelligence in Healthcare" },
  179. - { "title": "🎮 Video Game Development Insights" }
  180. ### Content:
  181. <content>
  182. ${content}
  183. </content>`;
  184. const oldTitle = JSON.parse(JSON.stringify(note.title));
  185. note.title = '';
  186. titleGenerating = true;
  187. const res = await generateOpenAIChatCompletion(
  188. localStorage.token,
  189. {
  190. model: selectedModelId,
  191. stream: false,
  192. messages: [
  193. {
  194. role: 'user',
  195. content: DEFAULT_TITLE_GENERATION_PROMPT_TEMPLATE
  196. }
  197. ]
  198. },
  199. `${WEBUI_BASE_URL}/api`
  200. );
  201. if (res) {
  202. // Step 1: Safely extract the response string
  203. const response = res?.choices[0]?.message?.content ?? '';
  204. try {
  205. const jsonStartIndex = response.indexOf('{');
  206. const jsonEndIndex = response.lastIndexOf('}');
  207. if (jsonStartIndex !== -1 && jsonEndIndex !== -1) {
  208. const jsonResponse = response.substring(jsonStartIndex, jsonEndIndex + 1);
  209. const parsed = JSON.parse(jsonResponse);
  210. if (parsed && parsed.title) {
  211. note.title = parsed.title.trim();
  212. }
  213. }
  214. } catch (e) {
  215. console.error('Error parsing JSON response:', e);
  216. toast.error($i18n.t('Failed to generate title'));
  217. }
  218. }
  219. if (!note.title) {
  220. note.title = oldTitle;
  221. }
  222. titleGenerating = false;
  223. await tick();
  224. changeDebounceHandler();
  225. };
  226. async function enhanceNoteHandler() {
  227. if (selectedModelId === '') {
  228. toast.error($i18n.t('Please select a model.'));
  229. return;
  230. }
  231. const model = $models
  232. .filter((model) => model.id === selectedModelId && !(model?.info?.meta?.hidden ?? false))
  233. .find((model) => model.id === selectedModelId);
  234. if (!model) {
  235. selectedModelId = '';
  236. return;
  237. }
  238. editing = true;
  239. await enhanceCompletionHandler(model);
  240. editing = false;
  241. onEdited();
  242. versionIdx = null;
  243. }
  244. const stopResponseHandler = async () => {
  245. stopResponseFlag = true;
  246. console.log('stopResponse', stopResponseFlag);
  247. };
  248. function setContentByVersion(versionIdx) {
  249. if (!note.data.versions?.length) return;
  250. let idx = versionIdx;
  251. if (idx === null) idx = note.data.versions.length - 1; // latest
  252. const v = note.data.versions[idx];
  253. note.data.content.json = v.json;
  254. note.data.content.html = v.html;
  255. note.data.content.md = v.md;
  256. if (versionIdx === null) {
  257. const lastVersion = note.data.versions.at(-1);
  258. const currentContent = note.data.content;
  259. if (areContentsEqual(lastVersion, currentContent)) {
  260. // remove the last version
  261. note.data.versions = note.data.versions.slice(0, -1);
  262. }
  263. }
  264. }
  265. // Navigation
  266. function versionNavigateHandler(direction) {
  267. if (!note.data.versions || note.data.versions.length === 0) return;
  268. if (versionIdx === null) {
  269. // Get latest snapshots
  270. const lastVersion = note.data.versions.at(-1);
  271. const currentContent = note.data.content;
  272. if (!areContentsEqual(lastVersion, currentContent)) {
  273. // If the current content is different from the last version, insert a new version
  274. insertNoteVersion(note);
  275. versionIdx = note.data.versions.length - 1;
  276. } else {
  277. versionIdx = note.data.versions.length;
  278. }
  279. }
  280. if (direction === 'prev') {
  281. if (versionIdx > 0) versionIdx -= 1;
  282. } else if (direction === 'next') {
  283. if (versionIdx < note.data.versions.length - 1) versionIdx += 1;
  284. else versionIdx = null; // Reset to latest
  285. if (versionIdx === note.data.versions.length - 1) {
  286. // If we reach the latest version, reset to null
  287. versionIdx = null;
  288. }
  289. }
  290. setContentByVersion(versionIdx);
  291. }
  292. const uploadFileHandler = async (file) => {
  293. const tempItemId = uuidv4();
  294. const fileItem = {
  295. type: 'file',
  296. file: '',
  297. id: null,
  298. url: '',
  299. name: file.name,
  300. collection_name: '',
  301. status: 'uploading',
  302. size: file.size,
  303. error: '',
  304. itemId: tempItemId
  305. };
  306. if (fileItem.size == 0) {
  307. toast.error($i18n.t('You cannot upload an empty file.'));
  308. return null;
  309. }
  310. files = [...files, fileItem];
  311. try {
  312. // If the file is an audio file, provide the language for STT.
  313. let metadata = null;
  314. if (
  315. (file.type.startsWith('audio/') || file.type.startsWith('video/')) &&
  316. $settings?.audio?.stt?.language
  317. ) {
  318. metadata = {
  319. language: $settings?.audio?.stt?.language
  320. };
  321. }
  322. // During the file upload, file content is automatically extracted.
  323. const uploadedFile = await uploadFile(localStorage.token, file, metadata);
  324. if (uploadedFile) {
  325. console.log('File upload completed:', {
  326. id: uploadedFile.id,
  327. name: fileItem.name,
  328. collection: uploadedFile?.meta?.collection_name
  329. });
  330. if (uploadedFile.error) {
  331. console.warn('File upload warning:', uploadedFile.error);
  332. toast.warning(uploadedFile.error);
  333. }
  334. fileItem.status = 'uploaded';
  335. fileItem.file = uploadedFile;
  336. fileItem.id = uploadedFile.id;
  337. fileItem.collection_name =
  338. uploadedFile?.meta?.collection_name || uploadedFile?.collection_name;
  339. fileItem.url = `${WEBUI_API_BASE_URL}/files/${uploadedFile.id}`;
  340. files = files;
  341. } else {
  342. files = files.filter((item) => item?.itemId !== tempItemId);
  343. }
  344. } catch (e) {
  345. toast.error(`${e}`);
  346. files = files.filter((item) => item?.itemId !== tempItemId);
  347. }
  348. if (files.length > 0) {
  349. note.data.files = files;
  350. } else {
  351. note.data.files = null;
  352. }
  353. };
  354. const inputFilesHandler = async (inputFiles) => {
  355. console.log('Input files handler called with:', inputFiles);
  356. inputFiles.forEach(async (file) => {
  357. console.log('Processing file:', {
  358. name: file.name,
  359. type: file.type,
  360. size: file.size,
  361. extension: file.name.split('.').at(-1)
  362. });
  363. if (
  364. ($config?.file?.max_size ?? null) !== null &&
  365. file.size > ($config?.file?.max_size ?? 0) * 1024 * 1024
  366. ) {
  367. console.log('File exceeds max size limit:', {
  368. fileSize: file.size,
  369. maxSize: ($config?.file?.max_size ?? 0) * 1024 * 1024
  370. });
  371. toast.error(
  372. $i18n.t(`File size should not exceed {{maxSize}} MB.`, {
  373. maxSize: $config?.file?.max_size
  374. })
  375. );
  376. return;
  377. }
  378. if (file['type'].startsWith('image/')) {
  379. const compressImageHandler = async (imageUrl, settings = {}, config = {}) => {
  380. // Quick shortcut so we don’t do unnecessary work.
  381. const settingsCompression = settings?.imageCompression ?? false;
  382. const configWidth = config?.file?.image_compression?.width ?? null;
  383. const configHeight = config?.file?.image_compression?.height ?? null;
  384. // If neither settings nor config wants compression, return original URL.
  385. if (!settingsCompression && !configWidth && !configHeight) {
  386. return imageUrl;
  387. }
  388. // Default to null (no compression unless set)
  389. let width = null;
  390. let height = null;
  391. // If user/settings want compression, pick their preferred size.
  392. if (settingsCompression) {
  393. width = settings?.imageCompressionSize?.width ?? null;
  394. height = settings?.imageCompressionSize?.height ?? null;
  395. }
  396. // Apply config limits as an upper bound if any
  397. if (configWidth && (width === null || width > configWidth)) {
  398. width = configWidth;
  399. }
  400. if (configHeight && (height === null || height > configHeight)) {
  401. height = configHeight;
  402. }
  403. // Do the compression if required
  404. if (width || height) {
  405. return await compressImage(imageUrl, width, height);
  406. }
  407. return imageUrl;
  408. };
  409. let reader = new FileReader();
  410. reader.onload = async (event) => {
  411. let imageUrl = event.target.result;
  412. imageUrl = await compressImageHandler(imageUrl, $settings, $config);
  413. files = [
  414. ...files,
  415. {
  416. type: 'image',
  417. url: `${imageUrl}`
  418. }
  419. ];
  420. note.data.files = files;
  421. };
  422. reader.readAsDataURL(
  423. file['type'] === 'image/heic'
  424. ? await heic2any({ blob: file, toType: 'image/jpeg' })
  425. : file
  426. );
  427. } else {
  428. uploadFileHandler(file);
  429. }
  430. });
  431. };
  432. const downloadHandler = async (type) => {
  433. console.log('downloadHandler', type);
  434. if (type === 'txt') {
  435. const blob = new Blob([note.data.content.md], { type: 'text/plain' });
  436. saveAs(blob, `${note.title}.txt`);
  437. } else if (type === 'md') {
  438. const blob = new Blob([note.data.content.md], { type: 'text/markdown' });
  439. saveAs(blob, `${note.title}.md`);
  440. } else if (type === 'pdf') {
  441. await downloadPdf(note);
  442. }
  443. };
  444. const downloadPdf = async (note) => {
  445. try {
  446. // Define a fixed virtual screen size
  447. const virtualWidth = 1024; // Fixed width (adjust as needed)
  448. const virtualHeight = 1400; // Fixed height (adjust as needed)
  449. // STEP 1. Get a DOM node to render
  450. const html = note.data?.content?.html ?? '';
  451. let node;
  452. if (html instanceof HTMLElement) {
  453. node = html;
  454. } else {
  455. // If it's HTML string, render to a temporary hidden element
  456. node = document.createElement('div');
  457. node.innerHTML = html;
  458. document.body.appendChild(node);
  459. }
  460. // Render to canvas with predefined width
  461. const canvas = await html2canvas(node, {
  462. useCORS: true,
  463. scale: 2, // Keep at 1x to avoid unexpected enlargements
  464. width: virtualWidth, // Set fixed virtual screen width
  465. windowWidth: virtualWidth, // Ensure consistent rendering
  466. windowHeight: virtualHeight
  467. });
  468. // Remove hidden node if needed
  469. if (!(html instanceof HTMLElement)) {
  470. document.body.removeChild(node);
  471. }
  472. const imgData = canvas.toDataURL('image/png');
  473. // A4 page settings
  474. const pdf = new jsPDF('p', 'mm', 'a4');
  475. const imgWidth = 210; // A4 width in mm
  476. const pageHeight = 297; // A4 height in mm
  477. // Maintain aspect ratio
  478. const imgHeight = (canvas.height * imgWidth) / canvas.width;
  479. let heightLeft = imgHeight;
  480. let position = 0;
  481. pdf.addImage(imgData, 'PNG', 0, position, imgWidth, imgHeight);
  482. heightLeft -= pageHeight;
  483. // Handle additional pages
  484. while (heightLeft > 0) {
  485. position -= pageHeight;
  486. pdf.addPage();
  487. pdf.addImage(imgData, 'PNG', 0, position, imgWidth, imgHeight);
  488. heightLeft -= pageHeight;
  489. }
  490. pdf.save(`${note.title}.pdf`);
  491. } catch (error) {
  492. console.error('Error generating PDF', error);
  493. toast.error(`${error}`);
  494. }
  495. };
  496. const deleteNoteHandler = async (id) => {
  497. const res = await deleteNoteById(localStorage.token, id).catch((error) => {
  498. toast.error(`${error}`);
  499. return null;
  500. });
  501. if (res) {
  502. toast.success($i18n.t('Note deleted successfully'));
  503. goto('/notes');
  504. } else {
  505. toast.error($i18n.t('Failed to delete note'));
  506. }
  507. };
  508. const scrollToBottom = () => {
  509. const element = document.getElementById('note-content-container');
  510. if (element) {
  511. element.scrollTop = element?.scrollHeight;
  512. }
  513. };
  514. const enhanceCompletionHandler = async (model) => {
  515. stopResponseFlag = false;
  516. let enhancedContent = {
  517. json: null,
  518. html: '',
  519. md: ''
  520. };
  521. const systemPrompt = `Enhance existing notes using additional context provided from audio transcription or uploaded file content. Your task is to make the notes more useful and comprehensive by incorporating relevant information from the provided context.
  522. Input will be provided within <notes> and <context> XML tags, providing a structure for the existing notes and context respectively.
  523. # Output Format
  524. Provide the enhanced notes in markdown format. Use markdown syntax for headings, lists, and emphasis to improve clarity and presentation. Ensure that all integrated content from the context is accurately reflected. Return only the markdown formatted note.
  525. `;
  526. const [res, controller] = await chatCompletion(
  527. localStorage.token,
  528. {
  529. model: model.id,
  530. stream: true,
  531. messages: [
  532. {
  533. role: 'system',
  534. content: systemPrompt
  535. },
  536. {
  537. role: 'user',
  538. content:
  539. `<notes>${note.data.content.md}</notes>` +
  540. (files && files.length > 0
  541. ? `\n<context>${files.map((file) => `${file.name}: ${file?.file?.data?.content ?? 'Could not extract content'}\n`).join('')}</context>`
  542. : '')
  543. }
  544. ]
  545. },
  546. `${WEBUI_BASE_URL}/api`
  547. );
  548. await tick();
  549. streaming = true;
  550. if (res && res.ok) {
  551. const reader = res.body
  552. .pipeThrough(new TextDecoderStream())
  553. .pipeThrough(splitStream('\n'))
  554. .getReader();
  555. while (true) {
  556. const { value, done } = await reader.read();
  557. if (done || stopResponseFlag) {
  558. if (stopResponseFlag) {
  559. controller.abort('User: Stop Response');
  560. }
  561. editing = false;
  562. streaming = false;
  563. break;
  564. }
  565. try {
  566. let lines = value.split('\n');
  567. for (const line of lines) {
  568. if (line !== '') {
  569. console.log(line);
  570. if (line === 'data: [DONE]') {
  571. console.log(line);
  572. } else {
  573. let data = JSON.parse(line.replace(/^data: /, ''));
  574. console.log(data);
  575. if (data.choices && data.choices.length > 0) {
  576. const choice = data.choices[0];
  577. if (choice.delta && choice.delta.content) {
  578. enhancedContent.md += choice.delta.content;
  579. enhancedContent.html = marked.parse(enhancedContent.md);
  580. note.data.content.md = enhancedContent.md;
  581. note.data.content.html = enhancedContent.html;
  582. note.data.content.json = null;
  583. scrollToBottom();
  584. }
  585. }
  586. }
  587. }
  588. }
  589. } catch (error) {
  590. console.log(error);
  591. }
  592. }
  593. }
  594. streaming = false;
  595. };
  596. const onDragOver = (e) => {
  597. e.preventDefault();
  598. // Check if a file is being dragged.
  599. if (e.dataTransfer?.types?.includes('Files')) {
  600. dragged = true;
  601. } else {
  602. dragged = false;
  603. }
  604. };
  605. const onDragLeave = () => {
  606. dragged = false;
  607. };
  608. const onDrop = async (e) => {
  609. e.preventDefault();
  610. console.log(e);
  611. if (e.dataTransfer?.files) {
  612. const inputFiles = Array.from(e.dataTransfer?.files);
  613. if (inputFiles && inputFiles.length > 0) {
  614. console.log(inputFiles);
  615. inputFilesHandler(inputFiles);
  616. }
  617. }
  618. dragged = false;
  619. };
  620. const insertHandler = (content) => {
  621. insertNoteVersion(note);
  622. inputElement?.insertContent(content);
  623. };
  624. onMount(async () => {
  625. await tick();
  626. if ($settings?.models) {
  627. selectedModelId = $settings?.models[0];
  628. } else if ($config?.default_models) {
  629. selectedModelId = $config?.default_models.split(',')[0];
  630. } else {
  631. selectedModelId = '';
  632. }
  633. if (selectedModelId) {
  634. const model = $models
  635. .filter((model) => model.id === selectedModelId && !(model?.info?.meta?.hidden ?? false))
  636. .find((model) => model.id === selectedModelId);
  637. if (!model) {
  638. selectedModelId = '';
  639. }
  640. }
  641. const dropzoneElement = document.getElementById('note-editor');
  642. dropzoneElement?.addEventListener('dragover', onDragOver);
  643. dropzoneElement?.addEventListener('drop', onDrop);
  644. dropzoneElement?.addEventListener('dragleave', onDragLeave);
  645. });
  646. onDestroy(() => {
  647. console.log('destroy');
  648. const dropzoneElement = document.getElementById('note-editor');
  649. if (dropzoneElement) {
  650. dropzoneElement?.removeEventListener('dragover', onDragOver);
  651. dropzoneElement?.removeEventListener('drop', onDrop);
  652. dropzoneElement?.removeEventListener('dragleave', onDragLeave);
  653. }
  654. });
  655. </script>
  656. {#if note}
  657. <AccessControlModal
  658. bind:show={showAccessControlModal}
  659. bind:accessControl={note.access_control}
  660. accessRoles={['read', 'write']}
  661. onChange={() => {
  662. changeDebounceHandler();
  663. }}
  664. />
  665. {/if}
  666. <FilesOverlay show={dragged} />
  667. <DeleteConfirmDialog
  668. bind:show={showDeleteConfirm}
  669. title={$i18n.t('Delete note?')}
  670. on:confirm={() => {
  671. deleteNoteHandler(note.id);
  672. showDeleteConfirm = false;
  673. }}
  674. >
  675. <div class=" text-sm text-gray-500">
  676. {$i18n.t('This will delete')} <span class=" font-semibold">{note.title}</span>.
  677. </div>
  678. </DeleteConfirmDialog>
  679. <PaneGroup direction="horizontal" class="w-full h-full">
  680. <Pane defaultSize={70} minSize={30} class="h-full flex flex-col w-full relative">
  681. <div class="relative flex-1 w-full h-full flex justify-center pt-[11px]" id="note-editor">
  682. {#if loading}
  683. <div class=" absolute top-0 bottom-0 left-0 right-0 flex">
  684. <div class="m-auto">
  685. <Spinner className="size-5" />
  686. </div>
  687. </div>
  688. {:else}
  689. <div class=" w-full flex flex-col {loading ? 'opacity-20' : ''}">
  690. <div class="shrink-0 w-full flex justify-between items-center px-3.5 mb-1.5">
  691. <div class="w-full flex items-center">
  692. <div
  693. class="{$showSidebar
  694. ? 'md:hidden pl-0.5'
  695. : ''} flex flex-none items-center pr-1 -translate-x-1"
  696. >
  697. <button
  698. id="sidebar-toggle-button"
  699. class="cursor-pointer p-1.5 flex rounded-xl hover:bg-gray-100 dark:hover:bg-gray-850 transition"
  700. on:click={() => {
  701. showSidebar.set(!$showSidebar);
  702. }}
  703. aria-label="Toggle Sidebar"
  704. >
  705. <div class=" m-auto self-center">
  706. <MenuLines />
  707. </div>
  708. </button>
  709. </div>
  710. <input
  711. class="w-full text-2xl font-medium bg-transparent outline-hidden"
  712. type="text"
  713. bind:value={note.title}
  714. placeholder={titleGenerating ? $i18n.t('Generating...') : $i18n.t('Title')}
  715. disabled={(note?.user_id !== $user?.id && $user?.role !== 'admin') ||
  716. titleGenerating}
  717. required
  718. on:input={changeDebounceHandler}
  719. on:focus={() => {
  720. titleInputFocused = true;
  721. }}
  722. on:blur={(e) => {
  723. // check if target is generate button
  724. if (e.relatedTarget?.id === 'generate-title-button') {
  725. return;
  726. }
  727. titleInputFocused = false;
  728. changeDebounceHandler();
  729. }}
  730. />
  731. {#if titleInputFocused && !titleGenerating}
  732. <div
  733. class="flex self-center items-center space-x-1.5 z-10 translate-y-[0.5px] -translate-x-[0.5px] pl-2"
  734. >
  735. <Tooltip content={$i18n.t('Generate')}>
  736. <button
  737. class=" self-center dark:hover:text-white transition"
  738. id="generate-title-button"
  739. on:click={(e) => {
  740. e.preventDefault();
  741. e.stopImmediatePropagation();
  742. e.stopPropagation();
  743. generateTitleHandler();
  744. }}
  745. >
  746. <Sparkles strokeWidth="2" />
  747. </button>
  748. </Tooltip>
  749. </div>
  750. {/if}
  751. <div class="flex items-center gap-0.5 translate-x-1">
  752. {#if editor}
  753. <div>
  754. <div class="flex items-center gap-0.5 self-center min-w-fit" dir="ltr">
  755. <button
  756. class="self-center p-1 hover:enabled:bg-black/5 dark:hover:enabled:bg-white/5 dark:hover:enabled:text-white hover:enabled:text-black rounded-md transition disabled:cursor-not-allowed disabled:text-gray-500 disabled:hover:text-gray-500"
  757. on:click={() => {
  758. editor.chain().focus().undo().run();
  759. // versionNavigateHandler('prev');
  760. }}
  761. disabled={!editor.can().undo()}
  762. >
  763. <ArrowUturnLeft className="size-4" />
  764. </button>
  765. <button
  766. class="self-center p-1 hover:enabled:bg-black/5 dark:hover:enabled:bg-white/5 dark:hover:enabled:text-white hover:enabled:text-black rounded-md transition disabled:cursor-not-allowed disabled:text-gray-500 disabled:hover:text-gray-500"
  767. on:click={() => {
  768. editor.chain().focus().redo().run();
  769. // versionNavigateHandler('next');
  770. }}
  771. disabled={!editor.can().redo()}
  772. >
  773. <ArrowUturnRight className="size-4" />
  774. </button>
  775. </div>
  776. </div>
  777. {/if}
  778. <Tooltip placement="top" content={$i18n.t('Chat')} className="cursor-pointer">
  779. <button
  780. class="p-1.5 bg-transparent hover:bg-white/5 transition rounded-lg"
  781. on:click={() => {
  782. if (showPanel && selectedPanel === 'chat') {
  783. showPanel = false;
  784. } else {
  785. if (!showPanel) {
  786. showPanel = true;
  787. }
  788. selectedPanel = 'chat';
  789. }
  790. }}
  791. >
  792. <ChatBubbleOval />
  793. </button>
  794. </Tooltip>
  795. <Tooltip placement="top" content={$i18n.t('Settings')} className="cursor-pointer">
  796. <button
  797. class="p-1.5 bg-transparent hover:bg-white/5 transition rounded-lg"
  798. on:click={() => {
  799. if (showPanel && selectedPanel === 'settings') {
  800. showPanel = false;
  801. } else {
  802. if (!showPanel) {
  803. showPanel = true;
  804. }
  805. selectedPanel = 'settings';
  806. }
  807. }}
  808. >
  809. <Cog6 />
  810. </button>
  811. </Tooltip>
  812. <NoteMenu
  813. onDownload={(type) => {
  814. downloadHandler(type);
  815. }}
  816. onCopyLink={async () => {
  817. const baseUrl = window.location.origin;
  818. const res = await copyToClipboard(`${baseUrl}/notes/${note.id}`);
  819. if (res) {
  820. toast.success($i18n.t('Copied link to clipboard'));
  821. } else {
  822. toast.error($i18n.t('Failed to copy link'));
  823. }
  824. }}
  825. onCopyToClipboard={async () => {
  826. const res = await copyToClipboard(note.data.content.md).catch((error) => {
  827. toast.error(`${error}`);
  828. return null;
  829. });
  830. if (res) {
  831. toast.success($i18n.t('Copied to clipboard'));
  832. }
  833. }}
  834. onDelete={() => {
  835. showDeleteConfirm = true;
  836. }}
  837. >
  838. <div class="p-1 bg-transparent hover:bg-white/5 transition rounded-lg">
  839. <EllipsisHorizontal className="size-5" />
  840. </div>
  841. </NoteMenu>
  842. </div>
  843. </div>
  844. </div>
  845. <div class=" px-2.5">
  846. <div
  847. class=" flex w-full bg-transparent overflow-x-auto scrollbar-none"
  848. on:wheel={(e) => {
  849. if (e.deltaY !== 0) {
  850. e.preventDefault();
  851. e.currentTarget.scrollLeft += e.deltaY;
  852. }
  853. }}
  854. >
  855. <div
  856. class="flex gap-1 items-center text-xs font-medium text-gray-500 dark:text-gray-500 w-fit"
  857. >
  858. <button class=" flex items-center gap-1 w-fit py-1 px-1.5 rounded-lg min-w-fit">
  859. <Calendar className="size-3.5" strokeWidth="2" />
  860. <span>{dayjs(note.created_at / 1000000).calendar()}</span>
  861. </button>
  862. <button
  863. class=" flex items-center gap-1 w-fit py-1 px-1.5 rounded-lg min-w-fit"
  864. on:click={() => {
  865. showAccessControlModal = true;
  866. }}
  867. disabled={note?.user_id !== $user?.id && $user?.role !== 'admin'}
  868. >
  869. <Users className="size-3.5" strokeWidth="2" />
  870. <span> {note?.access_control ? $i18n.t('Private') : $i18n.t('Everyone')} </span>
  871. </button>
  872. {#if editor}
  873. <div class="flex items-center gap-1 px-1 min-w-fit">
  874. <div>
  875. {$i18n.t('{{count}} words', {
  876. count: wordCount
  877. })}
  878. </div>
  879. <div>
  880. {$i18n.t('{{count}} characters', {
  881. count: charCount
  882. })}
  883. </div>
  884. </div>
  885. {/if}
  886. </div>
  887. </div>
  888. </div>
  889. <div
  890. class=" flex-1 w-full h-full overflow-auto px-3.5 pb-20 relative pt-2.5"
  891. id="note-content-container"
  892. >
  893. {#if editing}
  894. <div
  895. class="w-full h-full fixed top-0 left-0 {streaming
  896. ? ''
  897. : ' backdrop-blur-xs bg-white/10 dark:bg-gray-900/10'} flex items-center justify-center z-10 cursor-not-allowed"
  898. ></div>
  899. {/if}
  900. {#if files && files.length > 0}
  901. <div class="mb-2.5 w-full flex gap-1 flex-wrap z-40">
  902. {#each files as file, fileIdx}
  903. <div class="w-fit">
  904. {#if file.type === 'image'}
  905. <Image
  906. src={file.url}
  907. imageClassName=" max-h-96 rounded-lg"
  908. dismissible={true}
  909. onDismiss={() => {
  910. files = files.filter((item, idx) => idx !== fileIdx);
  911. note.data.files = files.length > 0 ? files : null;
  912. }}
  913. />
  914. {:else}
  915. <FileItem
  916. item={file}
  917. dismissible={true}
  918. url={file.url}
  919. name={file.name}
  920. type={file.type}
  921. size={file?.size}
  922. loading={file.status === 'uploading'}
  923. on:dismiss={() => {
  924. files = files.filter((item) => item?.id !== file.id);
  925. note.data.files = files.length > 0 ? files : null;
  926. }}
  927. />
  928. {/if}
  929. </div>
  930. {/each}
  931. </div>
  932. {/if}
  933. <RichTextInput
  934. bind:this={inputElement}
  935. bind:editor
  936. className="input-prose-sm px-0.5"
  937. json={true}
  938. bind:value={note.data.content.json}
  939. html={note.data?.content?.html}
  940. documentId={`note:${note.id}`}
  941. collaboration={true}
  942. socket={$socket}
  943. user={$user}
  944. link={true}
  945. placeholder={$i18n.t('Write something...')}
  946. editable={versionIdx === null && !editing}
  947. onChange={(content) => {
  948. note.data.content.html = content.html;
  949. note.data.content.md = content.md;
  950. if (editor) {
  951. wordCount = editor.storage.characterCount.words();
  952. charCount = editor.storage.characterCount.characters();
  953. }
  954. }}
  955. />
  956. </div>
  957. </div>
  958. {/if}
  959. </div>
  960. <div class="absolute z-20 bottom-0 right-0 p-3.5 max-w-full w-full flex">
  961. <div class="flex gap-1 w-full min-w-full justify-between">
  962. {#if recording}
  963. <div class="flex-1 w-full">
  964. <VoiceRecording
  965. bind:recording
  966. className="p-1 w-full max-w-full"
  967. transcribe={false}
  968. displayMedia={displayMediaRecord}
  969. onCancel={() => {
  970. recording = false;
  971. displayMediaRecord = false;
  972. }}
  973. onConfirm={(data) => {
  974. if (data?.file) {
  975. uploadFileHandler(data?.file);
  976. }
  977. recording = false;
  978. displayMediaRecord = false;
  979. }}
  980. />
  981. </div>
  982. {:else}
  983. <RecordMenu
  984. onRecord={async () => {
  985. displayMediaRecord = false;
  986. try {
  987. let stream = await navigator.mediaDevices
  988. .getUserMedia({ audio: true })
  989. .catch(function (err) {
  990. toast.error(
  991. $i18n.t(`Permission denied when accessing microphone: {{error}}`, {
  992. error: err
  993. })
  994. );
  995. return null;
  996. });
  997. if (stream) {
  998. recording = true;
  999. const tracks = stream.getTracks();
  1000. tracks.forEach((track) => track.stop());
  1001. }
  1002. stream = null;
  1003. } catch {
  1004. toast.error($i18n.t('Permission denied when accessing microphone'));
  1005. }
  1006. }}
  1007. onCaptureAudio={async () => {
  1008. displayMediaRecord = true;
  1009. recording = true;
  1010. }}
  1011. onUpload={async () => {
  1012. const input = document.createElement('input');
  1013. input.type = 'file';
  1014. input.accept = 'audio/*';
  1015. input.multiple = false;
  1016. input.click();
  1017. input.onchange = async (e) => {
  1018. const files = e.target.files;
  1019. if (files && files.length > 0) {
  1020. await uploadFileHandler(files[0]);
  1021. }
  1022. };
  1023. }}
  1024. >
  1025. <Tooltip content={$i18n.t('Record')} placement="top">
  1026. <div
  1027. class="cursor-pointer p-2.5 flex rounded-full border border-gray-50 bg-white dark:border-none dark:bg-gray-850 hover:bg-gray-50 dark:hover:bg-gray-800 transition shadow-xl"
  1028. >
  1029. <MicSolid className="size-4.5" />
  1030. </div>
  1031. </Tooltip>
  1032. </RecordMenu>
  1033. <div
  1034. class="cursor-pointer flex gap-0.5 rounded-full border border-gray-50 dark:border-gray-850 dark:bg-gray-850 transition shadow-xl"
  1035. >
  1036. <Tooltip content={$i18n.t('Enhance')} placement="top">
  1037. {#if editing}
  1038. <button
  1039. class="p-2 flex justify-center items-center hover:bg-gray-50 dark:hover:bg-gray-800 rounded-full transition shrink-0"
  1040. on:click={() => {
  1041. stopResponseHandler();
  1042. }}
  1043. type="button"
  1044. >
  1045. <Spinner className="size-5" />
  1046. </button>
  1047. {:else}
  1048. <AiMenu
  1049. onEdit={() => {
  1050. enhanceNoteHandler();
  1051. }}
  1052. onChat={() => {
  1053. showPanel = true;
  1054. selectedPanel = 'chat';
  1055. }}
  1056. >
  1057. <div
  1058. class="cursor-pointer p-2.5 flex rounded-full border border-gray-50 bg-white dark:border-none dark:bg-gray-850 hover:bg-gray-50 dark:hover:bg-gray-800 transition shadow-xl"
  1059. >
  1060. <SparklesSolid />
  1061. </div>
  1062. </AiMenu>
  1063. {/if}
  1064. </Tooltip>
  1065. </div>
  1066. {/if}
  1067. </div>
  1068. </div>
  1069. </Pane>
  1070. <NotePanel bind:show={showPanel}>
  1071. {#if selectedPanel === 'chat'}
  1072. <Chat
  1073. bind:show={showPanel}
  1074. bind:selectedModelId
  1075. bind:messages
  1076. bind:note
  1077. bind:editing
  1078. bind:streaming
  1079. bind:stopResponseFlag
  1080. {files}
  1081. onInsert={insertHandler}
  1082. onStop={stopResponseHandler}
  1083. {onEdited}
  1084. insertNoteHandler={() => {
  1085. insertNoteVersion(note);
  1086. }}
  1087. scrollToBottomHandler={scrollToBottom}
  1088. />
  1089. {:else if selectedPanel === 'settings'}
  1090. <Settings bind:show={showPanel} bind:selectedModelId />
  1091. {/if}
  1092. </NotePanel>
  1093. </PaneGroup>