You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

291 lines
7.5 KiB

11 months ago
  1. const prisma = require("../utils/prisma");
  2. const WorkspaceChats = {
  3. new: async function ({
  4. workspaceId,
  5. prompt,
  6. response = {},
  7. user = null,
  8. threadId = null,
  9. include = true,
  10. apiSessionId = null,
  11. }) {
  12. try {
  13. const chat = await prisma.workspace_chats.create({
  14. data: {
  15. workspaceId,
  16. prompt,
  17. response: JSON.stringify(response),
  18. user_id: user?.id || null,
  19. thread_id: threadId,
  20. api_session_id: apiSessionId,
  21. include,
  22. },
  23. });
  24. return { chat, message: null };
  25. } catch (error) {
  26. console.error(error.message);
  27. return { chat: null, message: error.message };
  28. }
  29. },
  30. forWorkspaceByUser: async function (
  31. workspaceId = null,
  32. userId = null,
  33. limit = null,
  34. orderBy = null
  35. ) {
  36. if (!workspaceId || !userId) return [];
  37. try {
  38. const chats = await prisma.workspace_chats.findMany({
  39. where: {
  40. workspaceId,
  41. user_id: userId,
  42. thread_id: null, // this function is now only used for the default thread on workspaces and users
  43. api_session_id: null, // do not include api-session chats in the frontend for anyone.
  44. include: true,
  45. },
  46. ...(limit !== null ? { take: limit } : {}),
  47. ...(orderBy !== null ? { orderBy } : { orderBy: { id: "asc" } }),
  48. });
  49. return chats;
  50. } catch (error) {
  51. console.error(error.message);
  52. return [];
  53. }
  54. },
  55. forWorkspaceByApiSessionId: async function (
  56. workspaceId = null,
  57. apiSessionId = null,
  58. limit = null,
  59. orderBy = null
  60. ) {
  61. if (!workspaceId || !apiSessionId) return [];
  62. try {
  63. const chats = await prisma.workspace_chats.findMany({
  64. where: {
  65. workspaceId,
  66. user_id: null,
  67. api_session_id: String(apiSessionId),
  68. thread_id: null,
  69. },
  70. ...(limit !== null ? { take: limit } : {}),
  71. ...(orderBy !== null ? { orderBy } : { orderBy: { id: "asc" } }),
  72. });
  73. return chats;
  74. } catch (error) {
  75. console.error(error.message);
  76. return [];
  77. }
  78. },
  79. forWorkspace: async function (
  80. workspaceId = null,
  81. limit = null,
  82. orderBy = null
  83. ) {
  84. if (!workspaceId) return [];
  85. try {
  86. const chats = await prisma.workspace_chats.findMany({
  87. where: {
  88. workspaceId,
  89. thread_id: null, // this function is now only used for the default thread on workspaces
  90. api_session_id: null, // do not include api-session chats in the frontend for anyone.
  91. include: true,
  92. },
  93. ...(limit !== null ? { take: limit } : {}),
  94. ...(orderBy !== null ? { orderBy } : { orderBy: { id: "asc" } }),
  95. });
  96. return chats;
  97. } catch (error) {
  98. console.error(error.message);
  99. return [];
  100. }
  101. },
  102. markHistoryInvalid: async function (workspaceId = null, user = null) {
  103. if (!workspaceId) return;
  104. try {
  105. await prisma.workspace_chats.updateMany({
  106. where: {
  107. workspaceId,
  108. user_id: user?.id,
  109. thread_id: null, // this function is now only used for the default thread on workspaces
  110. },
  111. data: {
  112. include: false,
  113. },
  114. });
  115. return;
  116. } catch (error) {
  117. console.error(error.message);
  118. }
  119. },
  120. markThreadHistoryInvalid: async function (
  121. workspaceId = null,
  122. user = null,
  123. threadId = null
  124. ) {
  125. if (!workspaceId || !threadId) return;
  126. try {
  127. await prisma.workspace_chats.updateMany({
  128. where: {
  129. workspaceId,
  130. thread_id: threadId,
  131. user_id: user?.id,
  132. },
  133. data: {
  134. include: false,
  135. },
  136. });
  137. return;
  138. } catch (error) {
  139. console.error(error.message);
  140. }
  141. },
  142. get: async function (clause = {}, limit = null, orderBy = null) {
  143. try {
  144. const chat = await prisma.workspace_chats.findFirst({
  145. where: clause,
  146. ...(limit !== null ? { take: limit } : {}),
  147. ...(orderBy !== null ? { orderBy } : {}),
  148. });
  149. return chat || null;
  150. } catch (error) {
  151. console.error(error.message);
  152. return null;
  153. }
  154. },
  155. delete: async function (clause = {}) {
  156. try {
  157. await prisma.workspace_chats.deleteMany({
  158. where: clause,
  159. });
  160. return true;
  161. } catch (error) {
  162. console.error(error.message);
  163. return false;
  164. }
  165. },
  166. where: async function (
  167. clause = {},
  168. limit = null,
  169. orderBy = null,
  170. offset = null
  171. ) {
  172. try {
  173. const chats = await prisma.workspace_chats.findMany({
  174. where: clause,
  175. ...(limit !== null ? { take: limit } : {}),
  176. ...(offset !== null ? { skip: offset } : {}),
  177. ...(orderBy !== null ? { orderBy } : {}),
  178. });
  179. return chats;
  180. } catch (error) {
  181. console.error(error.message);
  182. return [];
  183. }
  184. },
  185. count: async function (clause = {}) {
  186. try {
  187. const count = await prisma.workspace_chats.count({
  188. where: clause,
  189. });
  190. return count;
  191. } catch (error) {
  192. console.error(error.message);
  193. return 0;
  194. }
  195. },
  196. whereWithData: async function (
  197. clause = {},
  198. limit = null,
  199. offset = null,
  200. orderBy = null
  201. ) {
  202. const { Workspace } = require("./workspace");
  203. const { User } = require("./user");
  204. try {
  205. const results = await this.where(clause, limit, orderBy, offset);
  206. for (const res of results) {
  207. const workspace = await Workspace.get({ id: res.workspaceId });
  208. res.workspace = workspace
  209. ? { name: workspace.name, slug: workspace.slug }
  210. : { name: "deleted workspace", slug: null };
  211. const user = res.user_id ? await User.get({ id: res.user_id }) : null;
  212. res.user = user
  213. ? { username: user.username }
  214. : { username: res.api_session_id !== null ? "API" : "unknown user" };
  215. }
  216. return results;
  217. } catch (error) {
  218. console.error(error.message);
  219. return [];
  220. }
  221. },
  222. updateFeedbackScore: async function (chatId = null, feedbackScore = null) {
  223. if (!chatId) return;
  224. try {
  225. await prisma.workspace_chats.update({
  226. where: {
  227. id: Number(chatId),
  228. },
  229. data: {
  230. feedbackScore:
  231. feedbackScore === null ? null : Number(feedbackScore) === 1,
  232. },
  233. });
  234. return;
  235. } catch (error) {
  236. console.error(error.message);
  237. }
  238. },
  239. // Explicit update of settings + key validations.
  240. // Only use this method when directly setting a key value
  241. // that takes no user input for the keys being modified.
  242. _update: async function (id = null, data = {}) {
  243. if (!id) throw new Error("No workspace chat id provided for update");
  244. try {
  245. await prisma.workspace_chats.update({
  246. where: { id },
  247. data,
  248. });
  249. return true;
  250. } catch (error) {
  251. console.error(error.message);
  252. return false;
  253. }
  254. },
  255. bulkCreate: async function (chatsData) {
  256. // TODO: Replace with createMany when we update prisma to latest version
  257. // The version of prisma that we are currently using does not support createMany with SQLite
  258. try {
  259. const createdChats = [];
  260. for (const chatData of chatsData) {
  261. const chat = await prisma.workspace_chats.create({
  262. data: chatData,
  263. });
  264. createdChats.push(chat);
  265. }
  266. return { chats: createdChats, message: null };
  267. } catch (error) {
  268. console.error(error.message);
  269. return { chats: null, message: error.message };
  270. }
  271. },
  272. };
  273. module.exports = { WorkspaceChats };