NoteEditor.svelte 37 KB

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