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.

97 lines
2.4 KiB

11 months ago
  1. const prisma = require("../utils/prisma");
  2. const { v4: uuidv4 } = require("uuid");
  3. const WorkspaceAgentInvocation = {
  4. // returns array of strings with their @ handle.
  5. // must start with @agent for now.
  6. parseAgents: function (promptString) {
  7. if (!promptString.startsWith("@agent")) return [];
  8. return promptString.split(/\s+/).filter((v) => v.startsWith("@"));
  9. },
  10. close: async function (uuid) {
  11. if (!uuid) return;
  12. try {
  13. await prisma.workspace_agent_invocations.update({
  14. where: { uuid: String(uuid) },
  15. data: { closed: true },
  16. });
  17. } catch {}
  18. },
  19. new: async function ({ prompt, workspace, user = null, thread = null }) {
  20. try {
  21. const invocation = await prisma.workspace_agent_invocations.create({
  22. data: {
  23. uuid: uuidv4(),
  24. workspace_id: workspace.id,
  25. prompt: String(prompt),
  26. user_id: user?.id,
  27. thread_id: thread?.id,
  28. },
  29. });
  30. return { invocation, message: null };
  31. } catch (error) {
  32. console.error(error.message);
  33. return { invocation: null, message: error.message };
  34. }
  35. },
  36. get: async function (clause = {}) {
  37. try {
  38. const invocation = await prisma.workspace_agent_invocations.findFirst({
  39. where: clause,
  40. });
  41. return invocation || null;
  42. } catch (error) {
  43. console.error(error.message);
  44. return null;
  45. }
  46. },
  47. getWithWorkspace: async function (clause = {}) {
  48. try {
  49. const invocation = await prisma.workspace_agent_invocations.findFirst({
  50. where: clause,
  51. include: {
  52. workspace: true,
  53. },
  54. });
  55. return invocation || null;
  56. } catch (error) {
  57. console.error(error.message);
  58. return null;
  59. }
  60. },
  61. delete: async function (clause = {}) {
  62. try {
  63. await prisma.workspace_agent_invocations.delete({
  64. where: clause,
  65. });
  66. return true;
  67. } catch (error) {
  68. console.error(error.message);
  69. return false;
  70. }
  71. },
  72. where: async function (clause = {}, limit = null, orderBy = null) {
  73. try {
  74. const results = await prisma.workspace_agent_invocations.findMany({
  75. where: clause,
  76. ...(limit !== null ? { take: limit } : {}),
  77. ...(orderBy !== null ? { orderBy } : {}),
  78. });
  79. return results;
  80. } catch (error) {
  81. console.error(error.message);
  82. return [];
  83. }
  84. },
  85. };
  86. module.exports = { WorkspaceAgentInvocation };