Advertisement
Guest User

ChatGPT with memory

a guest
Dec 18th, 2022
1,759
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 14.87 KB | None | 0 0
  1. // ==UserScript==
  2. // u/name chatgpt with memory
  3. // u/namespace http://tampermonkey.net/
  4. // u/version 0.1
  5. // u/description chatgpt with memory
  6. // u/author simoNCode
  7. // u/match https://chat.openai.com/chat
  8. // u/icon https://www.google.com/s2/favicons?sz=64&domain=openai.com
  9. // u/grant none
  10. // u/require https://code.jquery.com/jquery-3.6.0.min.js
  11. // ==/UserScript==
  12.  
  13. const generateUUID = () => {
  14. return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {
  15. var r = Math.random() * 16 | 0, v = c == 'x' ? r : (r & 0x3 | 0x8);
  16. return v.toString(16);
  17. });
  18. }
  19.  
  20. // GET THREADS
  21. const _threads = () => {
  22. return localStorage.getItem('chatgpt-threads') != null ? JSON.parse(localStorage.getItem('chatgpt-threads')): [];
  23. }
  24.  
  25.  
  26. const _setThreads = (threads) => {
  27. localStorage.setItem('chatgpt-threads', JSON.stringify(threads));
  28. }
  29.  
  30. const _getActive = (active) => {
  31. let threads = _threads();
  32. return threads.filter(t => t.active)[0];
  33. }
  34.  
  35. const _updateConvIdActive = (active) => {
  36. let threads = _threads();
  37. threads.filter(t => t.id == active.id)[0].conv_id = active.conv_id;
  38. threads.filter(t => t.id == active.id)[0].parent_id = active.parent_id;
  39. threads.filter(t => t.id == active.id)[0].messages = active.messages;
  40. _setThreads(threads);
  41. }
  42.  
  43. const _updateMsgsActive = (msgs) => {
  44. let threads = _threads();
  45. threads.filter(t => t.active)[0].messages = threads.filter(t => t.active)[0].messages.concat(msgs);
  46. _setThreads(threads);
  47. }
  48.  
  49. const _init = () => {
  50. let threads = _threads();
  51. if (threads.length == 0) {
  52. let btnObj = {
  53. id: generateUUID(),
  54. conv_id: 0,
  55. parent_id: 0,
  56. active: true,
  57. title: 'Chat',
  58. messages: []
  59. };
  60. threads.push(btnObj);
  61. _setThreads(threads);
  62. }
  63. _draw();
  64. }
  65.  
  66. let nav = $('nav');
  67. let newThreadBtn = $('nav>a:nth-child(1)');
  68.  
  69. nav.css('overflow-y', 'visible !important');
  70. newThreadBtn.click((e) => {
  71. e.preventDefault();
  72. let threads = _threads();
  73. for (let t of threads) {
  74. t.active = false;
  75. }
  76.  
  77. let btnObj = {
  78. id: generateUUID(),
  79. conv_id: 0,
  80. parent_id: 0,
  81. active: true,
  82. title: 'Chat',
  83. messages: []
  84. };
  85. threads.push(btnObj);
  86. localStorage.setItem('chatgpt-threads', JSON.stringify(threads));
  87. _draw();
  88. });
  89.  
  90. // BUILD THE MENU BUTTON
  91. let _button = (id, conv_id, active, title) => {
  92. let svgHistory = $('<?xml version="1.0" encoding="UTF-8"?><svg alt="Chat History" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="20px" height="20px" viewBox="0 0 20 20" version="1.1"><g id="surface1"><path style=" stroke:none;fill-rule:nonzero;fill:rgb(255%,255%,255%);fill-opacity:1;" d="M 2.777344 9.921875 L 2.777344 19.046875 L 13.332031 19.046875 L 15.199219 17.144531 L 17.0625 15.238281 L 17.0625 0.792969 L 2.777344 0.792969 Z M 16.269531 8.136719 L 16.269531 14.683594 L 12.699219 14.683594 L 12.699219 18.253906 L 3.570312 18.253906 L 3.570312 1.585938 L 16.269531 1.585938 Z M 15.476562 15.59375 C 15.476562 15.675781 15.039062 16.152344 14.484375 16.667969 L 13.492188 17.578125 L 13.492188 16.546875 C 13.492188 15.675781 13.691406 15.476562 14.484375 15.476562 C 15.039062 15.476562 15.476562 15.515625 15.476562 15.59375 Z M 15.476562 15.59375 "/><path style=" stroke:none;fill-rule:nonzero;fill:rgb(255%,255%,255%);fill-opacity:1;" d="M 5.953125 5.554688 C 5.953125 5.753906 6.230469 5.953125 6.546875 5.953125 C 6.863281 5.953125 7.144531 5.753906 7.144531 5.554688 C 7.144531 5.316406 6.863281 5.160156 6.546875 5.160156 C 6.230469 5.160156 5.953125 5.316406 5.953125 5.554688 Z M 5.953125 5.554688 "/><path style=" stroke:none;fill-rule:nonzero;fill:rgb(255%,255%,255%);fill-opacity:1;" d="M 8.332031 5.554688 C 8.332031 5.792969 9.523438 5.953125 11.109375 5.953125 C 12.699219 5.953125 13.890625 5.792969 13.890625 5.554688 C 13.890625 5.316406 12.699219 5.160156 11.109375 5.160156 C 9.523438 5.160156 8.332031 5.316406 8.332031 5.554688 Z M 8.332031 5.554688 "/><path style=" stroke:none;fill-rule:nonzero;fill:rgb(255%,255%,255%);fill-opacity:1;" d="M 5.953125 7.9375 C 5.953125 8.136719 6.230469 8.332031 6.546875 8.332031 C 6.863281 8.332031 7.144531 8.136719 7.144531 7.9375 C 7.144531 7.699219 6.863281 7.539062 6.546875 7.539062 C 6.230469 7.539062 5.953125 7.699219 5.953125 7.9375 Z M 5.953125 7.9375 "/><path style=" stroke:none;fill-rule:nonzero;fill:rgb(255%,255%,255%);fill-opacity:1;" d="M 8.332031 7.9375 C 8.332031 8.175781 9.523438 8.332031 11.109375 8.332031 C 12.699219 8.332031 13.890625 8.175781 13.890625 7.9375 C 13.890625 7.699219 12.699219 7.539062 11.109375 7.539062 C 9.523438 7.539062 8.332031 7.699219 8.332031 7.9375 Z M 8.332031 7.9375 "/><path style=" stroke:none;fill-rule:nonzero;fill:rgb(255%,255%,255%);fill-opacity:1;" d="M 5.953125 10.316406 C 5.953125 10.515625 6.230469 10.714844 6.546875 10.714844 C 6.863281 10.714844 7.144531 10.515625 7.144531 10.316406 C 7.144531 10.078125 6.863281 9.921875 6.546875 9.921875 C 6.230469 9.921875 5.953125 10.078125 5.953125 10.316406 Z M 5.953125 10.316406 "/><path style=" stroke:none;fill-rule:nonzero;fill:rgb(255%,255%,255%);fill-opacity:1;" d="M 8.332031 10.316406 C 8.332031 10.554688 9.523438 10.714844 11.109375 10.714844 C 12.699219 10.714844 13.890625 10.554688 13.890625 10.316406 C 13.890625 10.078125 12.699219 9.921875 11.109375 9.921875 C 9.523438 9.921875 8.332031 10.078125 8.332031 10.316406 Z M 8.332031 10.316406 "/><path style=" stroke:none;fill-rule:nonzero;fill:rgb(255%,255%,255%);fill-opacity:1;" d="M 5.953125 12.699219 C 5.953125 12.898438 6.230469 13.09375 6.546875 13.09375 C 6.863281 13.09375 7.144531 12.898438 7.144531 12.699219 C 7.144531 12.460938 6.863281 12.300781 6.546875 12.300781 C 6.230469 12.300781 5.953125 12.460938 5.953125 12.699219 Z M 5.953125 12.699219 "/><path style=" stroke:none;fill-rule:nonzero;fill:rgb(255%,255%,255%);fill-opacity:1;" d="M 8.332031 12.699219 C 8.332031 12.9375 9.523438 13.09375 11.109375 13.09375 C 12.699219 13.09375 13.890625 12.9375 13.890625 12.699219 C 13.890625 12.460938 12.699219 12.300781 11.109375 12.300781 C 9.523438 12.300781 8.332031 12.460938 8.332031 12.699219 Z M 8.332031 12.699219 "/></g></svg>');
  93. let svgEdit = $('<?xml alt="Edit chat name" version="1.0" encoding="UTF-8"?><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="20px" height="20px" viewBox="0 0 20 20" version="1.1"><g id="surface1"><path style="fill:none;stroke-width:30;stroke-linecap:butt;stroke-linejoin:miter;stroke:rgb(255%,255%,255%);stroke-opacity:1;stroke-miterlimit:4;" d="M 70.078125 422.34375 L 444.375 48.046875 L 551.953125 155.625 L 177.65625 529.921875 L 48.046875 551.953125 Z M 70.078125 422.34375 " transform="matrix(0.0333333,0,0,0.0333333,0,0)"/><path style="fill:none;stroke-width:25;stroke-linecap:butt;stroke-linejoin:miter;stroke:rgb(255%,255%,255%);stroke-opacity:1;stroke-miterlimit:4;" d="M 70.546875 417.773438 L 181.171875 528.398438 " transform="matrix(0.0333333,0,0,0.0333333,0,0)"/><path style="fill:none;stroke-width:25;stroke-linecap:butt;stroke-linejoin:miter;stroke:rgb(255%,255%,255%);stroke-opacity:1;stroke-miterlimit:4;" d="M 491.484375 108.398438 L 124.804688 475.078125 " transform="matrix(0.0333333,0,0,0.0333333,0,0)"/><path style="fill:none;stroke-width:25;stroke-linecap:butt;stroke-linejoin:miter;stroke:rgb(255%,255%,255%);stroke-opacity:1;stroke-miterlimit:4;" d="M 54.257812 507.304688 L 95.15625 546.796875 " transform="matrix(0.0333333,0,0,0.0333333,0,0)"/></g></svg>');
  94. svgHistory.click((e) => {
  95. e.preventDefault();
  96. let threads_ = _threads();
  97. let msgs = threads_.filter(el => el.id == id)[0].messages;
  98. let popupContainer = $('<div>');
  99. popupContainer.attr('id', 'pupup_history');
  100. popupContainer.css({position: 'absolute', width: '100%', height: '100%', left: 0, top: 0, backgroundColor: 'rgba(0,0,0,.5)', zIndex: 999998});
  101. let close = $('<div>');
  102. close.css({ position: 'absolute', fontSize: '30px', color: 'white', top: '10px', right: '10px', zIndex: 999999, cursor: 'pointer' });
  103. close.html('X');
  104. close.click(() => {
  105. $('#pupup_history').remove();
  106. });
  107. let popup = $('<div>');
  108. popup.css({overflow: 'auto', position: 'absolute', width: '80%', height: '80%', left: '10%', top: '10%', backgroundColor: '#343541', zIndex: 999999, border: '3px solid #000000', borderRadius: '5px', padding: '20px'});
  109. for (let msg of msgs) {
  110. let msgDiv = $('<p>').css({marginBottom: '5px', marginTop: '5px'}).html(msg.replace(/\n/g, '<br />'));
  111. popup.append(msgDiv).append('<hr>');
  112. }
  113. popupContainer.append(close);
  114. popupContainer.append(popup);
  115. $($($($('body').children()[0]).children()[1])).prepend(popupContainer)
  116.  
  117. });
  118. svgEdit.click(() => {
  119. let newT = prompt("Enter the new chat name (length max 20):", title);
  120. if (newT != null && newT.trim().length < 20) {
  121. let threads_ = _threads();
  122. threads_.filter(el => el.id == id)[0].title = newT;
  123. _setThreads(threads_);
  124. _draw();
  125. } else {
  126. alert('Max length 20');
  127. }
  128. });
  129. let svgDelete = $('<?xml alt="Delete this chat" version="1.0" encoding="UTF-8"?><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="20px" height="20px" viewBox="0 0 20 20" version="1.1"><g id="surface1"><path style=" stroke:none;fill-rule:nonzero;fill:rgb(255%,255%,255%);fill-opacity:1;" d="M 16.964844 0.742188 L 12.695312 0.742188 L 12.695312 0.640625 C 12.695312 0.347656 12.460938 0.109375 12.167969 0.109375 L 7.8125 0.109375 C 7.519531 0.109375 7.28125 0.347656 7.28125 0.640625 L 7.28125 0.742188 L 3.011719 0.742188 C 2.71875 0.742188 2.484375 0.980469 2.484375 1.273438 L 2.484375 3.363281 C 2.484375 3.65625 2.71875 3.894531 3.011719 3.894531 L 16.964844 3.894531 C 17.257812 3.894531 17.496094 3.65625 17.496094 3.363281 L 17.496094 1.273438 C 17.496094 0.980469 17.257812 0.742188 16.964844 0.742188 Z M 16.964844 0.742188 "/><path style=" stroke:none;fill-rule:nonzero;fill:rgb(255%,255%,255%);fill-opacity:1;" d="M 16.167969 4.964844 L 3.808594 4.964844 C 3.515625 4.964844 3.277344 5.203125 3.277344 5.496094 L 3.277344 19.339844 C 3.277344 19.628906 3.515625 19.867188 3.808594 19.867188 L 16.167969 19.867188 C 16.460938 19.867188 16.699219 19.628906 16.699219 19.339844 L 16.699219 5.496094 C 16.699219 5.203125 16.460938 4.964844 16.167969 4.964844 Z M 7.472656 17.960938 C 7.472656 18.253906 7.238281 18.492188 6.945312 18.492188 L 6.453125 18.492188 C 6.160156 18.492188 5.921875 18.253906 5.921875 17.960938 L 5.921875 6.871094 C 5.921875 6.578125 6.160156 6.339844 6.453125 6.339844 L 6.945312 6.339844 C 7.238281 6.339844 7.472656 6.578125 7.472656 6.871094 Z M 10.765625 17.960938 C 10.765625 18.253906 10.527344 18.492188 10.234375 18.492188 L 9.742188 18.492188 C 9.449219 18.492188 9.214844 18.253906 9.214844 17.960938 L 9.214844 6.871094 C 9.214844 6.578125 9.449219 6.339844 9.742188 6.339844 L 10.234375 6.339844 C 10.527344 6.339844 10.765625 6.578125 10.765625 6.871094 Z M 14.054688 17.960938 C 14.054688 18.253906 13.820312 18.492188 13.527344 18.492188 L 13.035156 18.492188 C 12.742188 18.492188 12.503906 18.253906 12.503906 17.960938 L 12.503906 6.871094 C 12.503906 6.578125 12.742188 6.339844 13.035156 6.339844 L 13.527344 6.339844 C 13.820312 6.339844 14.054688 6.578125 14.054688 6.871094 Z M 14.054688 17.960938 "/></g></svg>');
  130. svgDelete.click(() => {
  131. if (confirm(`Delete the chat: ${title}?`)) {
  132. let threads_ = _threads();
  133. let newThreads = threads_.filter(el => el.id !== id);
  134. _setThreads(newThreads);
  135. if (newThreads.length == 0) {
  136. _init();
  137. } else {
  138. _draw();
  139. }
  140. }
  141. });
  142. let a = $(`<a data-id="${id}" conv-id="${conv_id}" ${active ? 'style="background-color: rgba(255,255,255,.2)"': ''} class="flex py-3 px-3 items-center gap-3 rounded-md hover:bg-gray-500/10 transition-colors duration-200 text-white cursor-pointer text-sm flex-shrink-0 border border-white/20"></a>`);
  143. a.append(svgHistory);
  144. a.append(svgEdit);
  145. a.append(svgDelete);
  146. a.append(title);
  147. return a;
  148. }
  149.  
  150. let _draw = (id) => {
  151. nav.parent().css('overflow', 'auto');
  152. // BUILD THE LEFT MENU
  153. var children = nav.children();
  154. children = children.not(':eq(0)');
  155. children.remove();
  156. let threads = _threads();
  157. for (let t of threads) {
  158. let btn = _button(t.id,t.conv_id, t.active, t.title);
  159. btn.click(() => {
  160. let threads_ = _threads();
  161. for (let t2 of threads_) {
  162. t2.active = false;
  163. }
  164. threads_.filter(el => el.id == t.id)[0].active = true;
  165. _setThreads(threads_);
  166. _draw();
  167. });
  168. if (t.active) {
  169. $('textarea').attr('placeholder', `The chat is now referring to: ${t.title}`);
  170. }
  171. newThreadBtn.after(btn);
  172. }
  173. // BUILD THE CHAT
  174.  
  175. }
  176.  
  177. _init();
  178.  
  179.  
  180. // ############## DISABLE MODERATION
  181.  
  182. window.originalFetch = window.fetch
  183. // Then override fetch function with your new function
  184. window.fetch = async (... args) => {
  185. let active = _getActive();
  186. let body = JSON.parse(args[1].body);
  187. if (args.length > 0 && args[0].includes('moderations')) {
  188. let lastMessage = active.messages.length > 0 ? active.messages[active.messages.length - 1]: "";
  189. let msgToAdd = body.input.replace(lastMessage, "");
  190. active.messages.push(msgToAdd);
  191. _updateConvIdActive(active);
  192. body.input = "HI!";
  193. args[1].body = JSON.stringify(body);
  194. }
  195. if (args.length > 0 && args[0].includes('conversation')) {
  196. if (active.conv_id != 0) {
  197. body.conversation_id = active.conv_id;
  198. }
  199. if (active.parent_id != 0) {
  200. body.parent_message_id = active.parent_id;
  201. }
  202. args[1].body = JSON.stringify(body);
  203. }
  204. let response = await window.originalFetch(...args);
  205. let responseClone = response.clone();
  206. let reader = responseClone.body.getReader();
  207.  
  208. reader.read().then(function processText({ done, value }) {
  209. let valueDecoded = new TextDecoder().decode(value);
  210. if (args.length > 0 && args[0].includes('conversation')) {
  211. let json = valueDecoded.split('data: ')[1];
  212. if (json != undefined) {
  213. json = JSON.parse(json);
  214. let conversation_id = json.conversation_id;
  215. active.conv_id = conversation_id;
  216. let parent_message_id = json.message.id;
  217. active.parent_id = parent_message_id;
  218. _updateConvIdActive(active);
  219. }
  220. }
  221.  
  222. });
  223. return response;
  224. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement