index.ts 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. import { WEBUI_API_BASE_URL } from '$lib/constants';
  2. export const getGravatarUrl = async (email: string) => {
  3. let error = null;
  4. const res = await fetch(`${WEBUI_API_BASE_URL}/utils/gravatar?email=${email}`, {
  5. method: 'GET',
  6. headers: {
  7. 'Content-Type': 'application/json'
  8. }
  9. })
  10. .then(async (res) => {
  11. if (!res.ok) throw await res.json();
  12. return res.json();
  13. })
  14. .catch((err) => {
  15. console.log(err);
  16. error = err;
  17. return null;
  18. });
  19. return res;
  20. };
  21. export const downloadChatAsPDF = async (chat: object) => {
  22. let error = null;
  23. const blob = await fetch(`${WEBUI_API_BASE_URL}/utils/pdf`, {
  24. method: 'POST',
  25. headers: {
  26. 'Content-Type': 'application/json'
  27. },
  28. body: JSON.stringify({
  29. title: chat.title,
  30. messages: chat.messages
  31. })
  32. })
  33. .then(async (res) => {
  34. if (!res.ok) throw await res.json();
  35. return res.blob();
  36. })
  37. .catch((err) => {
  38. console.log(err);
  39. error = err;
  40. return null;
  41. });
  42. return blob;
  43. };
  44. export const getHTMLFromMarkdown = async (md: string) => {
  45. let error = null;
  46. const res = await fetch(`${WEBUI_API_BASE_URL}/utils/markdown`, {
  47. method: 'POST',
  48. headers: {
  49. 'Content-Type': 'application/json'
  50. },
  51. body: JSON.stringify({
  52. md: md
  53. })
  54. })
  55. .then(async (res) => {
  56. if (!res.ok) throw await res.json();
  57. return res.json();
  58. })
  59. .catch((err) => {
  60. console.log(err);
  61. error = err;
  62. return null;
  63. });
  64. return res.html;
  65. };
  66. export const downloadDatabase = async (token: string) => {
  67. let error = null;
  68. const res = await fetch(`${WEBUI_API_BASE_URL}/utils/db/download`, {
  69. method: 'GET',
  70. headers: {
  71. 'Content-Type': 'application/json',
  72. Authorization: `Bearer ${token}`
  73. }
  74. })
  75. .then(async (response) => {
  76. if (!response.ok) {
  77. throw await response.json();
  78. }
  79. return response.blob();
  80. })
  81. .then((blob) => {
  82. const url = window.URL.createObjectURL(blob);
  83. const a = document.createElement('a');
  84. a.href = url;
  85. a.download = 'webui.db';
  86. document.body.appendChild(a);
  87. a.click();
  88. window.URL.revokeObjectURL(url);
  89. })
  90. .catch((err) => {
  91. console.log(err);
  92. error = err.detail;
  93. return null;
  94. });
  95. if (error) {
  96. throw error;
  97. }
  98. };