NoteEditor.svelte 38 KB

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