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.

73 lines
1.8 KiB

11 months ago
  1. const pluralize = require("pluralize");
  2. const {
  3. WorkspaceAgentInvocation,
  4. } = require("../../models/workspaceAgentInvocation");
  5. const { writeResponseChunk } = require("../helpers/chat/responses");
  6. async function grepAgents({
  7. uuid,
  8. response,
  9. message,
  10. workspace,
  11. user = null,
  12. thread = null,
  13. }) {
  14. const agentHandles = WorkspaceAgentInvocation.parseAgents(message);
  15. if (agentHandles.length > 0) {
  16. const { invocation: newInvocation } = await WorkspaceAgentInvocation.new({
  17. prompt: message,
  18. workspace: workspace,
  19. user: user,
  20. thread: thread,
  21. });
  22. if (!newInvocation) {
  23. writeResponseChunk(response, {
  24. id: uuid,
  25. type: "statusResponse",
  26. textResponse: `${pluralize(
  27. "Agent",
  28. agentHandles.length
  29. )} ${agentHandles.join(
  30. ", "
  31. )} could not be called. Chat will be handled as default chat.`,
  32. sources: [],
  33. close: true,
  34. animate: false,
  35. error: null,
  36. });
  37. return;
  38. }
  39. writeResponseChunk(response, {
  40. id: uuid,
  41. type: "agentInitWebsocketConnection",
  42. textResponse: null,
  43. sources: [],
  44. close: false,
  45. error: null,
  46. websocketUUID: newInvocation.uuid,
  47. });
  48. // Close HTTP stream-able chunk response method because we will swap to agents now.
  49. writeResponseChunk(response, {
  50. id: uuid,
  51. type: "statusResponse",
  52. textResponse: `${pluralize(
  53. "Agent",
  54. agentHandles.length
  55. )} ${agentHandles.join(
  56. ", "
  57. )} invoked.\nSwapping over to agent chat. Type /exit to exit agent execution loop early.`,
  58. sources: [],
  59. close: true,
  60. error: null,
  61. animate: true,
  62. });
  63. return true;
  64. }
  65. return false;
  66. }
  67. module.exports = { grepAgents };