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.

230 lines
6.4 KiB

11 months ago
  1. const { EmbedConfig } = require("../../../models/embedConfig");
  2. const { EmbedChats } = require("../../../models/embedChats");
  3. const { validApiKey } = require("../../../utils/middleware/validApiKey");
  4. function apiEmbedEndpoints(app) {
  5. if (!app) return;
  6. app.get("/v1/embed", [validApiKey], async (request, response) => {
  7. /*
  8. #swagger.tags = ['Embed']
  9. #swagger.description = 'List all active embeds'
  10. #swagger.responses[200] = {
  11. content: {
  12. "application/json": {
  13. schema: {
  14. type: 'object',
  15. example: {
  16. embeds: [
  17. {
  18. "id": 1,
  19. "uuid": "embed-uuid-1",
  20. "enabled": true,
  21. "chat_mode": "query",
  22. "createdAt": "2023-04-01T12:00:00Z",
  23. "workspace": {
  24. "id": 1,
  25. "name": "Workspace 1"
  26. },
  27. "chat_count": 10
  28. },
  29. {
  30. "id": 2,
  31. "uuid": "embed-uuid-2",
  32. "enabled": false,
  33. "chat_mode": "chat",
  34. "createdAt": "2023-04-02T14:30:00Z",
  35. "workspace": {
  36. "id": 1,
  37. "name": "Workspace 1"
  38. },
  39. "chat_count": 10
  40. }
  41. ]
  42. }
  43. }
  44. }
  45. }
  46. }
  47. #swagger.responses[403] = {
  48. schema: {
  49. "$ref": "#/definitions/InvalidAPIKey"
  50. }
  51. }
  52. */
  53. try {
  54. const embeds = await EmbedConfig.whereWithWorkspace();
  55. const filteredEmbeds = embeds.map((embed) => ({
  56. id: embed.id,
  57. uuid: embed.uuid,
  58. enabled: embed.enabled,
  59. chat_mode: embed.chat_mode,
  60. createdAt: embed.createdAt,
  61. workspace: {
  62. id: embed.workspace.id,
  63. name: embed.workspace.name,
  64. },
  65. chat_count: embed._count.embed_chats,
  66. }));
  67. response.status(200).json({ embeds: filteredEmbeds });
  68. } catch (e) {
  69. console.error(e.message, e);
  70. response.sendStatus(500).end();
  71. }
  72. });
  73. app.get(
  74. "/v1/embed/:embedUuid/chats",
  75. [validApiKey],
  76. async (request, response) => {
  77. /*
  78. #swagger.tags = ['Embed']
  79. #swagger.description = 'Get all chats for a specific embed'
  80. #swagger.parameters['embedUuid'] = {
  81. in: 'path',
  82. description: 'UUID of the embed',
  83. required: true,
  84. type: 'string'
  85. }
  86. #swagger.responses[200] = {
  87. content: {
  88. "application/json": {
  89. schema: {
  90. type: 'object',
  91. example: {
  92. chats: [
  93. {
  94. "id": 1,
  95. "session_id": "session-uuid-1",
  96. "prompt": "Hello",
  97. "response": "Hi there!",
  98. "createdAt": "2023-04-01T12:00:00Z"
  99. },
  100. {
  101. "id": 2,
  102. "session_id": "session-uuid-2",
  103. "prompt": "How are you?",
  104. "response": "I'm doing well, thank you!",
  105. "createdAt": "2023-04-02T14:30:00Z"
  106. }
  107. ]
  108. }
  109. }
  110. }
  111. }
  112. }
  113. #swagger.responses[403] = {
  114. schema: {
  115. "$ref": "#/definitions/InvalidAPIKey"
  116. }
  117. }
  118. #swagger.responses[404] = {
  119. description: "Embed not found",
  120. }
  121. */
  122. try {
  123. const { embedUuid } = request.params;
  124. const embed = await EmbedConfig.get({ uuid: String(embedUuid) });
  125. if (!embed) {
  126. return response.status(404).json({ error: "Embed not found" });
  127. }
  128. const chats = await EmbedChats.where({ embed_id: embed.id });
  129. const formattedChats = chats.map((chat) => ({
  130. id: chat.id,
  131. session_id: chat.session_id,
  132. prompt: chat.prompt,
  133. response: chat.response,
  134. createdAt: chat.createdAt,
  135. }));
  136. response.status(200).json({ chats: formattedChats });
  137. } catch (e) {
  138. console.error(e.message, e);
  139. response.sendStatus(500).end();
  140. }
  141. }
  142. );
  143. app.get(
  144. "/v1/embed/:embedUuid/chats/:sessionUuid",
  145. [validApiKey],
  146. async (request, response) => {
  147. /*
  148. #swagger.tags = ['Embed']
  149. #swagger.description = 'Get chats for a specific embed and session'
  150. #swagger.parameters['embedUuid'] = {
  151. in: 'path',
  152. description: 'UUID of the embed',
  153. required: true,
  154. type: 'string'
  155. }
  156. #swagger.parameters['sessionUuid'] = {
  157. in: 'path',
  158. description: 'UUID of the session',
  159. required: true,
  160. type: 'string'
  161. }
  162. #swagger.responses[200] = {
  163. content: {
  164. "application/json": {
  165. schema: {
  166. type: 'object',
  167. example: {
  168. chats: [
  169. {
  170. "id": 1,
  171. "prompt": "Hello",
  172. "response": "Hi there!",
  173. "createdAt": "2023-04-01T12:00:00Z"
  174. }
  175. ]
  176. }
  177. }
  178. }
  179. }
  180. }
  181. #swagger.responses[403] = {
  182. schema: {
  183. "$ref": "#/definitions/InvalidAPIKey"
  184. }
  185. }
  186. #swagger.responses[404] = {
  187. description: "Embed or session not found",
  188. }
  189. */
  190. try {
  191. const { embedUuid, sessionUuid } = request.params;
  192. const embed = await EmbedConfig.get({ uuid: String(embedUuid) });
  193. if (!embed) {
  194. return response.status(404).json({ error: "Embed not found" });
  195. }
  196. const chats = await EmbedChats.where({
  197. embed_id: embed.id,
  198. session_id: String(sessionUuid),
  199. });
  200. if (!chats || chats.length === 0) {
  201. return response
  202. .status(404)
  203. .json({ error: "No chats found for this session" });
  204. }
  205. const formattedChats = chats.map((chat) => ({
  206. id: chat.id,
  207. prompt: chat.prompt,
  208. response: chat.response,
  209. createdAt: chat.createdAt,
  210. }));
  211. response.status(200).json({ chats: formattedChats });
  212. } catch (e) {
  213. console.error(e.message, e);
  214. response.sendStatus(500).end();
  215. }
  216. }
  217. );
  218. }
  219. module.exports = { apiEmbedEndpoints };