openloops 0.4.5 → 0.6.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (250) hide show
  1. package/CHANGELOG.md +52 -0
  2. package/README.md +2 -1
  3. package/dist/common/base/base.entity.d.ts +3 -9
  4. package/dist/common/base/base.entity.js +17 -96
  5. package/dist/common/base/base.entity.mjs +19 -92
  6. package/dist/common/base/base.use-case.d.ts +2 -0
  7. package/dist/common/base/index.d.ts +1 -0
  8. package/dist/common/base/index.js +3 -1
  9. package/dist/common/base/index.mjs +1 -0
  10. package/dist/common/base/share.d.ts +12 -0
  11. package/dist/common/base/share.js +45 -0
  12. package/dist/common/base/share.mjs +21 -0
  13. package/dist/common/decorators/index.d.ts +0 -2
  14. package/dist/common/decorators/index.js +1 -5
  15. package/dist/common/decorators/index.mjs +0 -2
  16. package/dist/common/repositories/index.d.ts +1 -0
  17. package/dist/common/repositories/index.js +3 -1
  18. package/dist/common/repositories/index.mjs +1 -0
  19. package/dist/common/repositories/mongo-db.repository.d.ts +31 -11
  20. package/dist/common/repositories/mongo-db.repository.js +121 -40
  21. package/dist/common/repositories/mongo-db.repository.mjs +121 -40
  22. package/dist/common/repositories/scope.d.ts +19 -0
  23. package/dist/common/repositories/scope.js +63 -0
  24. package/dist/common/repositories/scope.mjs +40 -0
  25. package/dist/db/indexes/automations.js +1 -1
  26. package/dist/db/indexes/automations.mjs +1 -1
  27. package/dist/db/migrations/automations.d.ts +1 -0
  28. package/dist/db/migrations/automations.js +32 -5
  29. package/dist/db/migrations/automations.mjs +32 -5
  30. package/dist/services/authentication/repositories/revoked-token.repository.d.ts +3 -3
  31. package/dist/services/authentication/repositories/revoked-token.repository.js +7 -6
  32. package/dist/services/authentication/repositories/revoked-token.repository.mjs +7 -6
  33. package/dist/services/authentication/use-cases/is-token-revoked.use-case.js +2 -1
  34. package/dist/services/authentication/use-cases/is-token-revoked.use-case.mjs +2 -1
  35. package/dist/services/authentication/use-cases/login.use-case.js +2 -1
  36. package/dist/services/authentication/use-cases/login.use-case.mjs +2 -1
  37. package/dist/services/authentication/use-cases/revoke-token.use-case.js +5 -4
  38. package/dist/services/authentication/use-cases/revoke-token.use-case.mjs +5 -4
  39. package/dist/services/automations/entities/automation.entity.d.ts +9 -9
  40. package/dist/services/automations/entities/automation.entity.js +21 -20
  41. package/dist/services/automations/entities/automation.entity.mjs +21 -20
  42. package/dist/services/automations/entities/trigger.d.ts +16 -8
  43. package/dist/services/automations/entities/trigger.js +28 -17
  44. package/dist/services/automations/entities/trigger.mjs +27 -16
  45. package/dist/services/automations/repositories/automation-run.repository.d.ts +10 -10
  46. package/dist/services/automations/repositories/automation-run.repository.js +21 -18
  47. package/dist/services/automations/repositories/automation-run.repository.mjs +21 -18
  48. package/dist/services/automations/repositories/automation.repository.d.ts +14 -13
  49. package/dist/services/automations/repositories/automation.repository.js +34 -22
  50. package/dist/services/automations/repositories/automation.repository.mjs +34 -22
  51. package/dist/services/automations/runner/automation-runner.d.ts +3 -0
  52. package/dist/services/automations/runner/automation-runner.js +2 -2
  53. package/dist/services/automations/runner/automation-runner.mjs +2 -2
  54. package/dist/services/automations/use-cases/attach-automation-run-chat.use-case.js +3 -2
  55. package/dist/services/automations/use-cases/attach-automation-run-chat.use-case.mjs +3 -2
  56. package/dist/services/automations/use-cases/claim-due-automations.use-case.js +11 -10
  57. package/dist/services/automations/use-cases/claim-due-automations.use-case.mjs +11 -10
  58. package/dist/services/automations/use-cases/claim-queued-automation-runs.use-case.js +10 -9
  59. package/dist/services/automations/use-cases/claim-queued-automation-runs.use-case.mjs +10 -9
  60. package/dist/services/automations/use-cases/create-automation.use-case.d.ts +2 -2
  61. package/dist/services/automations/use-cases/create-automation.use-case.js +8 -8
  62. package/dist/services/automations/use-cases/create-automation.use-case.mjs +6 -6
  63. package/dist/services/automations/use-cases/delete-automation.use-case.js +6 -9
  64. package/dist/services/automations/use-cases/delete-automation.use-case.mjs +4 -7
  65. package/dist/services/automations/use-cases/finish-automation-run.use-case.js +4 -3
  66. package/dist/services/automations/use-cases/finish-automation-run.use-case.mjs +4 -3
  67. package/dist/services/automations/use-cases/get-automation-by-id.use-case.js +5 -4
  68. package/dist/services/automations/use-cases/get-automation-by-id.use-case.mjs +3 -2
  69. package/dist/services/automations/use-cases/issue-automation-event-token.use-case.js +7 -7
  70. package/dist/services/automations/use-cases/issue-automation-event-token.use-case.mjs +5 -5
  71. package/dist/services/automations/use-cases/list-automation-runs-by-automation.use-case.js +3 -1
  72. package/dist/services/automations/use-cases/list-automation-runs-by-automation.use-case.mjs +3 -1
  73. package/dist/services/automations/use-cases/list-automation-runs-by-user.use-case.js +2 -0
  74. package/dist/services/automations/use-cases/list-automation-runs-by-user.use-case.mjs +2 -0
  75. package/dist/services/automations/use-cases/list-automations-by-user.use-case.js +2 -0
  76. package/dist/services/automations/use-cases/list-automations-by-user.use-case.mjs +2 -0
  77. package/dist/services/automations/use-cases/receive-automation-event.use-case.js +10 -8
  78. package/dist/services/automations/use-cases/receive-automation-event.use-case.mjs +10 -8
  79. package/dist/services/automations/use-cases/renew-automation-lease.use-case.js +2 -1
  80. package/dist/services/automations/use-cases/renew-automation-lease.use-case.mjs +2 -1
  81. package/dist/services/automations/use-cases/revoke-automation-event-token.use-case.js +5 -5
  82. package/dist/services/automations/use-cases/revoke-automation-event-token.use-case.mjs +3 -3
  83. package/dist/services/automations/use-cases/rotate-automation-event-tokens.use-case.js +10 -10
  84. package/dist/services/automations/use-cases/rotate-automation-event-tokens.use-case.mjs +8 -8
  85. package/dist/services/automations/use-cases/start-automation-run.use-case.js +8 -6
  86. package/dist/services/automations/use-cases/start-automation-run.use-case.mjs +8 -6
  87. package/dist/services/automations/use-cases/sync-automation-run.use-case.js +6 -5
  88. package/dist/services/automations/use-cases/sync-automation-run.use-case.mjs +6 -5
  89. package/dist/services/automations/use-cases/sync-automation-runs.use-case.js +3 -2
  90. package/dist/services/automations/use-cases/sync-automation-runs.use-case.mjs +3 -2
  91. package/dist/services/automations/use-cases/update-automation.use-case.d.ts +2 -2
  92. package/dist/services/automations/use-cases/update-automation.use-case.js +16 -12
  93. package/dist/services/automations/use-cases/update-automation.use-case.mjs +14 -10
  94. package/dist/services/automations/utils/automation-fields.d.ts +3 -4
  95. package/dist/services/automations/utils/automation-fields.js +3 -5
  96. package/dist/services/automations/utils/automation-fields.mjs +4 -6
  97. package/dist/services/automations/utils/open-run.d.ts +2 -0
  98. package/dist/services/automations/utils/open-run.js +2 -2
  99. package/dist/services/automations/utils/open-run.mjs +2 -2
  100. package/dist/services/automations/utils/previous-run.js +3 -2
  101. package/dist/services/automations/utils/previous-run.mjs +3 -2
  102. package/dist/services/automations/utils/runner-scope.d.ts +3 -0
  103. package/dist/{common/decorators/admin-only/index.js → services/automations/utils/runner-scope.js} +15 -5
  104. package/dist/services/automations/utils/runner-scope.mjs +8 -0
  105. package/dist/services/chats/repositories/chat.repository.d.ts +7 -6
  106. package/dist/services/chats/repositories/chat.repository.js +16 -11
  107. package/dist/services/chats/repositories/chat.repository.mjs +16 -11
  108. package/dist/services/chats/use-cases/create-chat.use-case.js +4 -5
  109. package/dist/services/chats/use-cases/create-chat.use-case.mjs +2 -3
  110. package/dist/services/chats/use-cases/delete-chat.use-case.js +7 -9
  111. package/dist/services/chats/use-cases/delete-chat.use-case.mjs +5 -7
  112. package/dist/services/chats/use-cases/get-chat-by-id.use-case.js +3 -2
  113. package/dist/services/chats/use-cases/get-chat-by-id.use-case.mjs +3 -2
  114. package/dist/services/chats/use-cases/interrupt-execution.use-case.js +5 -7
  115. package/dist/services/chats/use-cases/interrupt-execution.use-case.mjs +3 -5
  116. package/dist/services/chats/use-cases/list-chats-by-props.use-case.js +3 -2
  117. package/dist/services/chats/use-cases/list-chats-by-props.use-case.mjs +3 -2
  118. package/dist/services/chats/use-cases/list-chats-by-user.use-case.js +2 -0
  119. package/dist/services/chats/use-cases/list-chats-by-user.use-case.mjs +2 -0
  120. package/dist/services/chats/use-cases/update-chat.use-case.js +5 -7
  121. package/dist/services/chats/use-cases/update-chat.use-case.mjs +3 -5
  122. package/dist/services/llmcalls/repositories/llmcall.repository.d.ts +7 -6
  123. package/dist/services/llmcalls/repositories/llmcall.repository.js +16 -11
  124. package/dist/services/llmcalls/repositories/llmcall.repository.mjs +16 -11
  125. package/dist/services/llmcalls/use-cases/create-llmcall.use-case.js +4 -5
  126. package/dist/services/llmcalls/use-cases/create-llmcall.use-case.mjs +2 -3
  127. package/dist/services/llmcalls/use-cases/delete-llmcall.use-case.js +6 -8
  128. package/dist/services/llmcalls/use-cases/delete-llmcall.use-case.mjs +4 -6
  129. package/dist/services/llmcalls/use-cases/get-llm-usage-by-chat.use-case.js +4 -3
  130. package/dist/services/llmcalls/use-cases/get-llm-usage-by-chat.use-case.mjs +2 -1
  131. package/dist/services/llmcalls/use-cases/get-llm-usage-by-message.use-case.js +4 -3
  132. package/dist/services/llmcalls/use-cases/get-llm-usage-by-message.use-case.mjs +2 -1
  133. package/dist/services/llmcalls/use-cases/get-llm-usage-stats.use-case.js +2 -1
  134. package/dist/services/llmcalls/use-cases/get-llm-usage-stats.use-case.mjs +2 -1
  135. package/dist/services/llmcalls/use-cases/get-llmcall-by-id.use-case.js +6 -4
  136. package/dist/services/llmcalls/use-cases/get-llmcall-by-id.use-case.mjs +4 -2
  137. package/dist/services/llmcalls/use-cases/list-llmcalls-by-props.use-case.js +3 -2
  138. package/dist/services/llmcalls/use-cases/list-llmcalls-by-props.use-case.mjs +3 -2
  139. package/dist/services/llmcalls/use-cases/update-llmcall.use-case.js +5 -7
  140. package/dist/services/llmcalls/use-cases/update-llmcall.use-case.mjs +3 -5
  141. package/dist/services/mcp-servers/repositories/mcpserver.repository.d.ts +8 -7
  142. package/dist/services/mcp-servers/repositories/mcpserver.repository.js +18 -12
  143. package/dist/services/mcp-servers/repositories/mcpserver.repository.mjs +18 -12
  144. package/dist/services/mcp-servers/use-cases/create-mcpserver.use-case.js +4 -5
  145. package/dist/services/mcp-servers/use-cases/create-mcpserver.use-case.mjs +2 -3
  146. package/dist/services/mcp-servers/use-cases/decide-mcpserver-trust.use-case.js +8 -9
  147. package/dist/services/mcp-servers/use-cases/decide-mcpserver-trust.use-case.mjs +6 -7
  148. package/dist/services/mcp-servers/use-cases/delete-mcpserver.use-case.js +6 -8
  149. package/dist/services/mcp-servers/use-cases/delete-mcpserver.use-case.mjs +4 -6
  150. package/dist/services/mcp-servers/use-cases/get-mcpserver-by-id.use-case.js +5 -4
  151. package/dist/services/mcp-servers/use-cases/get-mcpserver-by-id.use-case.mjs +3 -2
  152. package/dist/services/mcp-servers/use-cases/list-all-mcpservers-by-user.use-case.js +4 -3
  153. package/dist/services/mcp-servers/use-cases/list-all-mcpservers-by-user.use-case.mjs +2 -1
  154. package/dist/services/mcp-servers/use-cases/update-mcpserver.use-case.js +8 -11
  155. package/dist/services/mcp-servers/use-cases/update-mcpserver.use-case.mjs +6 -9
  156. package/dist/services/mcp-servers/use-cases/verify-mcpserver-connection.use-case.js +9 -9
  157. package/dist/services/mcp-servers/use-cases/verify-mcpserver-connection.use-case.mjs +7 -7
  158. package/dist/services/tasks/repositories/agent-action.repository.d.ts +4 -4
  159. package/dist/services/tasks/repositories/agent-action.repository.js +9 -7
  160. package/dist/services/tasks/repositories/agent-action.repository.mjs +9 -7
  161. package/dist/services/tasks/repositories/agent-task.repository.d.ts +5 -5
  162. package/dist/services/tasks/repositories/agent-task.repository.js +11 -9
  163. package/dist/services/tasks/repositories/agent-task.repository.mjs +11 -9
  164. package/dist/services/tasks/use-cases/create-agent-action.use-case.js +4 -5
  165. package/dist/services/tasks/use-cases/create-agent-action.use-case.mjs +2 -3
  166. package/dist/services/tasks/use-cases/create-agent-task.use-case.js +4 -5
  167. package/dist/services/tasks/use-cases/create-agent-task.use-case.mjs +2 -3
  168. package/dist/services/tasks/use-cases/get-agent-task-by-id.use-case.js +5 -4
  169. package/dist/services/tasks/use-cases/get-agent-task-by-id.use-case.mjs +3 -2
  170. package/dist/services/tasks/use-cases/list-agent-actions-by-task.use-case.js +3 -1
  171. package/dist/services/tasks/use-cases/list-agent-actions-by-task.use-case.mjs +3 -1
  172. package/dist/services/tasks/use-cases/list-agent-tasks-by-chat.use-case.js +3 -1
  173. package/dist/services/tasks/use-cases/list-agent-tasks-by-chat.use-case.mjs +3 -1
  174. package/dist/services/tasks/use-cases/update-agent-action.use-case.js +5 -7
  175. package/dist/services/tasks/use-cases/update-agent-action.use-case.mjs +3 -5
  176. package/dist/services/tasks/use-cases/update-agent-task.use-case.js +5 -7
  177. package/dist/services/tasks/use-cases/update-agent-task.use-case.mjs +3 -5
  178. package/dist/services/traces/repositories/agent-trace.repository.d.ts +3 -3
  179. package/dist/services/traces/repositories/agent-trace.repository.js +6 -5
  180. package/dist/services/traces/repositories/agent-trace.repository.mjs +6 -5
  181. package/dist/services/traces/use-cases/create-agent-traces.use-case.js +5 -9
  182. package/dist/services/traces/use-cases/create-agent-traces.use-case.mjs +3 -7
  183. package/dist/services/traces/use-cases/list-agent-traces-by-chat.use-case.js +3 -1
  184. package/dist/services/traces/use-cases/list-agent-traces-by-chat.use-case.mjs +3 -1
  185. package/dist/services/users/entities/user-acquisition.js +3 -16
  186. package/dist/services/users/entities/user-acquisition.mjs +4 -11
  187. package/dist/services/users/entities/user-marketing.js +3 -16
  188. package/dist/services/users/entities/user-marketing.mjs +4 -11
  189. package/dist/services/users/entities/user.entity.js +0 -3
  190. package/dist/services/users/entities/user.entity.mjs +1 -4
  191. package/dist/services/users/repositories/user.repository.d.ts +8 -7
  192. package/dist/services/users/repositories/user.repository.js +26 -12
  193. package/dist/services/users/repositories/user.repository.mjs +26 -12
  194. package/dist/services/users/use-cases/create-user.use-case.d.ts +0 -1
  195. package/dist/services/users/use-cases/create-user.use-case.js +5 -5
  196. package/dist/services/users/use-cases/create-user.use-case.mjs +3 -3
  197. package/dist/services/users/use-cases/delete-user.use-case.js +7 -9
  198. package/dist/services/users/use-cases/delete-user.use-case.mjs +5 -7
  199. package/dist/services/users/use-cases/get-user-by-id.use-case.js +6 -4
  200. package/dist/services/users/use-cases/get-user-by-id.use-case.mjs +4 -2
  201. package/dist/services/users/use-cases/list-users-by-props.use-case.js +3 -2
  202. package/dist/services/users/use-cases/list-users-by-props.use-case.mjs +3 -2
  203. package/dist/services/users/use-cases/update-user.use-case.js +5 -7
  204. package/dist/services/users/use-cases/update-user.use-case.mjs +3 -5
  205. package/dist/services/workspaces/repositories/workspace-item.repository.d.ts +8 -8
  206. package/dist/services/workspaces/repositories/workspace-item.repository.js +17 -14
  207. package/dist/services/workspaces/repositories/workspace-item.repository.mjs +17 -14
  208. package/dist/services/workspaces/use-cases/delete-workspace-item.use-case.js +6 -3
  209. package/dist/services/workspaces/use-cases/delete-workspace-item.use-case.mjs +4 -1
  210. package/dist/services/workspaces/use-cases/get-workspace-item.use-case.js +4 -3
  211. package/dist/services/workspaces/use-cases/get-workspace-item.use-case.mjs +2 -1
  212. package/dist/services/workspaces/use-cases/list-workspace-items.use-case.js +2 -0
  213. package/dist/services/workspaces/use-cases/list-workspace-items.use-case.mjs +2 -0
  214. package/dist/services/workspaces/use-cases/save-workspace-item.use-case.js +8 -8
  215. package/dist/services/workspaces/use-cases/save-workspace-item.use-case.mjs +6 -6
  216. package/dist/services/workspaces/use-cases/search-workspace-items.use-case.js +2 -0
  217. package/dist/services/workspaces/use-cases/search-workspace-items.use-case.mjs +2 -0
  218. package/docs/README.md +3 -2
  219. package/docs/agent.md +2 -2
  220. package/docs/authorization.md +77 -0
  221. package/docs/automations.md +143 -41
  222. package/docs/integration-guide.md +8 -10
  223. package/docs/llms.txt +1 -0
  224. package/docs/mcp-servers.md +4 -4
  225. package/docs/users.md +6 -4
  226. package/examples/automations/create-automation.ts +6 -5
  227. package/examples/automations/create-event-automation.ts +1 -1
  228. package/examples/automations/list-automation-runs.ts +2 -0
  229. package/examples/automations/list-automations.ts +3 -2
  230. package/examples/automations/preview-automation-runs.ts +1 -1
  231. package/examples/automations/revoke-automation-event-token.ts +1 -1
  232. package/examples/automations/run-automation-now.ts +3 -1
  233. package/examples/automations/update-automation.ts +4 -5
  234. package/examples/chats/delete-chat.ts +1 -1
  235. package/examples/mcps/internal-mcp.ts +3 -3
  236. package/examples/run-agent/run-agent-with-identity.ts +1 -1
  237. package/examples/users/create-user.ts +2 -1
  238. package/examples/users/list-users.ts +1 -0
  239. package/package.json +1 -1
  240. package/dist/common/decorators/admin-only/admin-only.d.ts +0 -2
  241. package/dist/common/decorators/admin-only/admin-only.js +0 -52
  242. package/dist/common/decorators/admin-only/admin-only.mjs +0 -28
  243. package/dist/common/decorators/admin-only/index.d.ts +0 -1
  244. package/dist/common/decorators/admin-only/index.mjs +0 -1
  245. package/dist/common/decorators/owner-only/index.d.ts +0 -1
  246. package/dist/common/decorators/owner-only/index.js +0 -22
  247. package/dist/common/decorators/owner-only/index.mjs +0 -1
  248. package/dist/common/decorators/owner-only/owner-only.d.ts +0 -2
  249. package/dist/common/decorators/owner-only/owner-only.js +0 -52
  250. package/dist/common/decorators/owner-only/owner-only.mjs +0 -28
@@ -32,8 +32,9 @@ __export(revoke_automation_event_token_use_case_exports, {
32
32
  module.exports = __toCommonJS(revoke_automation_event_token_use_case_exports);
33
33
  var import_tsyringe = require("tsyringe");
34
34
  var import_base = require("../../../common/base/base.use-case.js");
35
+ var import_repositories = require("../../../common/repositories/index.js");
35
36
  var import_authentication = require("../../authentication/index.js");
36
- var import_repositories = require("../repositories");
37
+ var import_repositories2 = require("../repositories");
37
38
  var import_event_token = require("../utils/event-token");
38
39
  let RevokeAutomationEventTokenUseCase = class extends import_base.BaseUseCase {
39
40
  constructor(automationRepository) {
@@ -43,9 +44,8 @@ let RevokeAutomationEventTokenUseCase = class extends import_base.BaseUseCase {
43
44
  automationRepository;
44
45
  async execute(params) {
45
46
  const { id, token, currentUser } = params;
46
- const automation = await this.automationRepository?.findById(id);
47
- if (!automation) throw new Error("Automation not found");
48
- if (!currentUser?.isAdmin() && String(automation.ownerId) !== currentUser?.userId) this.throwError403();
47
+ const automation = await this.automationRepository?.findById(id, params.scope ?? import_repositories.Scope.canManage(currentUser));
48
+ if (!automation) return this.throwHttpError({ statusCode: 404, errorMessage: "{{resource_not_found}}" });
49
49
  const claims = (0, import_event_token.readEventToken)(token, { ignoreExpiration: true });
50
50
  if (claims.sub !== id) throw new Error("The token does not belong to this automation");
51
51
  await (0, import_authentication.revokeToken)({
@@ -57,7 +57,7 @@ let RevokeAutomationEventTokenUseCase = class extends import_base.BaseUseCase {
57
57
  };
58
58
  RevokeAutomationEventTokenUseCase = __decorateClass([
59
59
  (0, import_tsyringe.autoInjectable)(),
60
- __decorateParam(0, (0, import_tsyringe.inject)(import_repositories.AutomationRepository))
60
+ __decorateParam(0, (0, import_tsyringe.inject)(import_repositories2.AutomationRepository))
61
61
  ], RevokeAutomationEventTokenUseCase);
62
62
  // Annotate the CommonJS export names for ESM import in node:
63
63
  0 && (module.exports = {
@@ -4,6 +4,7 @@ import {
4
4
  } from "../../../chunk-CF3WOMB3.mjs";
5
5
  import { autoInjectable, inject } from "tsyringe";
6
6
  import { BaseUseCase } from "../../../common/base/base.use-case.js";
7
+ import { Scope } from "../../../common/repositories/index.js";
7
8
  import { revokeToken } from "../../authentication/index.js";
8
9
  import { AutomationRepository } from "../repositories";
9
10
  import { EVENT_TOKEN_AUDIENCE, readEventToken } from "../utils/event-token";
@@ -15,9 +16,8 @@ let RevokeAutomationEventTokenUseCase = class extends BaseUseCase {
15
16
  automationRepository;
16
17
  async execute(params) {
17
18
  const { id, token, currentUser } = params;
18
- const automation = await this.automationRepository?.findById(id);
19
- if (!automation) throw new Error("Automation not found");
20
- if (!currentUser?.isAdmin() && String(automation.ownerId) !== currentUser?.userId) this.throwError403();
19
+ const automation = await this.automationRepository?.findById(id, params.scope ?? Scope.canManage(currentUser));
20
+ if (!automation) return this.throwHttpError({ statusCode: 404, errorMessage: "{{resource_not_found}}" });
21
21
  const claims = readEventToken(token, { ignoreExpiration: true });
22
22
  if (claims.sub !== id) throw new Error("The token does not belong to this automation");
23
23
  await revokeToken({
@@ -32,8 +32,9 @@ __export(rotate_automation_event_tokens_use_case_exports, {
32
32
  module.exports = __toCommonJS(rotate_automation_event_tokens_use_case_exports);
33
33
  var import_tsyringe = require("tsyringe");
34
34
  var import_base = require("../../../common/base/base.use-case.js");
35
+ var import_repositories = require("../../../common/repositories/index.js");
35
36
  var import_entities = require("../entities");
36
- var import_repositories = require("../repositories");
37
+ var import_repositories2 = require("../repositories");
37
38
  let RotateAutomationEventTokensUseCase = class extends import_base.BaseUseCase {
38
39
  constructor(automationRepository) {
39
40
  super();
@@ -42,20 +43,19 @@ let RotateAutomationEventTokensUseCase = class extends import_base.BaseUseCase {
42
43
  automationRepository;
43
44
  async execute(params) {
44
45
  const { id, currentUser } = params;
45
- const existing = await this.automationRepository?.findById(id);
46
- if (!existing) throw new Error("Automation not found");
47
- if (!currentUser?.isAdmin() && String(existing.ownerId) !== currentUser?.userId) this.throwError403();
48
- if (existing.trigger?.type !== "event") throw new Error("Only an automation with an event trigger has tokens");
49
- const automation = import_entities.Automation.fromJSON({ ...existing, trigger: { ...existing.trigger, tokenVersion: existing.trigger.tokenVersion + 1 } });
50
- automation.setUpdatedBy(currentUser);
51
- const result = await this.automationRepository?.update(automation, { expectedVersion: existing.__v });
52
- if (!result) throw new Error("Automation not found or update failed");
46
+ const scope = params.scope ?? import_repositories.Scope.canManage(currentUser);
47
+ const existing = await this.automationRepository?.findById(id, scope);
48
+ if (!existing) return this.throwHttpError({ statusCode: 404, errorMessage: "{{resource_not_found}}" });
49
+ if (!existing.triggers?.event) throw new Error("Only an automation with an event trigger has tokens");
50
+ const automation = import_entities.Automation.fromJSON({ ...existing, eventTokenVersion: existing.eventTokenVersion + 1 });
51
+ const result = await this.automationRepository?.update(automation, scope, { expectedVersion: existing.__v });
52
+ if (!result) return this.throwHttpError({ statusCode: 404, errorMessage: "{{resource_not_found}}" });
53
53
  return { data: result };
54
54
  }
55
55
  };
56
56
  RotateAutomationEventTokensUseCase = __decorateClass([
57
57
  (0, import_tsyringe.autoInjectable)(),
58
- __decorateParam(0, (0, import_tsyringe.inject)(import_repositories.AutomationRepository))
58
+ __decorateParam(0, (0, import_tsyringe.inject)(import_repositories2.AutomationRepository))
59
59
  ], RotateAutomationEventTokensUseCase);
60
60
  // Annotate the CommonJS export names for ESM import in node:
61
61
  0 && (module.exports = {
@@ -4,6 +4,7 @@ import {
4
4
  } from "../../../chunk-CF3WOMB3.mjs";
5
5
  import { autoInjectable, inject } from "tsyringe";
6
6
  import { BaseUseCase } from "../../../common/base/base.use-case.js";
7
+ import { Scope } from "../../../common/repositories/index.js";
7
8
  import { Automation } from "../entities";
8
9
  import { AutomationRepository } from "../repositories";
9
10
  let RotateAutomationEventTokensUseCase = class extends BaseUseCase {
@@ -14,14 +15,13 @@ let RotateAutomationEventTokensUseCase = class extends BaseUseCase {
14
15
  automationRepository;
15
16
  async execute(params) {
16
17
  const { id, currentUser } = params;
17
- const existing = await this.automationRepository?.findById(id);
18
- if (!existing) throw new Error("Automation not found");
19
- if (!currentUser?.isAdmin() && String(existing.ownerId) !== currentUser?.userId) this.throwError403();
20
- if (existing.trigger?.type !== "event") throw new Error("Only an automation with an event trigger has tokens");
21
- const automation = Automation.fromJSON({ ...existing, trigger: { ...existing.trigger, tokenVersion: existing.trigger.tokenVersion + 1 } });
22
- automation.setUpdatedBy(currentUser);
23
- const result = await this.automationRepository?.update(automation, { expectedVersion: existing.__v });
24
- if (!result) throw new Error("Automation not found or update failed");
18
+ const scope = params.scope ?? Scope.canManage(currentUser);
19
+ const existing = await this.automationRepository?.findById(id, scope);
20
+ if (!existing) return this.throwHttpError({ statusCode: 404, errorMessage: "{{resource_not_found}}" });
21
+ if (!existing.triggers?.event) throw new Error("Only an automation with an event trigger has tokens");
22
+ const automation = Automation.fromJSON({ ...existing, eventTokenVersion: existing.eventTokenVersion + 1 });
23
+ const result = await this.automationRepository?.update(automation, scope, { expectedVersion: existing.__v });
24
+ if (!result) return this.throwHttpError({ statusCode: 404, errorMessage: "{{resource_not_found}}" });
25
25
  return { data: result };
26
26
  }
27
27
  };
@@ -35,6 +35,8 @@ var import_base = require("../../../common/base/base.use-case.js");
35
35
  var import_repositories = require("../repositories");
36
36
  var import_open_run = require("../utils/open-run");
37
37
  var import_settings = require("../utils/settings");
38
+ var import_runner_scope = require("../utils/runner-scope");
39
+ var import_repositories2 = require("../../../common/repositories/index.js");
38
40
  let StartAutomationRunUseCase = class extends import_base.BaseUseCase {
39
41
  constructor(automationRepository, automationRunRepository) {
40
42
  super();
@@ -44,15 +46,15 @@ let StartAutomationRunUseCase = class extends import_base.BaseUseCase {
44
46
  automationRepository;
45
47
  automationRunRepository;
46
48
  async execute(params) {
47
- const { automationId, workerId } = params;
49
+ const { automationId, workerId, currentUser } = params;
48
50
  if (!automationId || !workerId) throw new Error("The automation and the worker are required");
49
51
  const now = params.now ?? /* @__PURE__ */ new Date();
50
- const existing = await this.automationRepository.findById(automationId);
51
- if (!existing) throw new Error("Automation not found");
52
- const automation = await this.automationRepository.claimManual({ id: automationId, workerId, now, leaseMs: params.leaseMs ?? (0, import_settings.leaseMs)() });
52
+ const existing = await this.automationRepository.findById(automationId, params.scope ?? import_repositories2.Scope.canEdit(currentUser));
53
+ if (!existing) return this.throwHttpError({ statusCode: 404, errorMessage: "{{resource_not_found}}" });
54
+ const automation = await this.automationRepository.claimManual({ id: automationId, workerId, now, leaseMs: params.leaseMs ?? (0, import_settings.leaseMs)() }, import_runner_scope.runnerScope);
53
55
  if (!automation) throw new Error("The automation is already running");
54
- const run = await (0, import_open_run.openRun)(this.automationRunRepository, { automation, trigger: "manual", status: "running", workerId, now });
55
- await this.automationRepository.setActiveRun(automationId, workerId, String(run._id));
56
+ const run = await (0, import_open_run.openRun)(this.automationRunRepository, { scope: import_runner_scope.runnerScope, automation, trigger: "manual", status: "running", workerId, now });
57
+ await this.automationRepository.setActiveRun(automationId, workerId, String(run._id), import_runner_scope.runnerScope);
56
58
  return { data: { automation, run: run.toJSON() } };
57
59
  }
58
60
  };
@@ -7,6 +7,8 @@ import { BaseUseCase } from "../../../common/base/base.use-case.js";
7
7
  import { AutomationRepository, AutomationRunRepository } from "../repositories";
8
8
  import { openRun } from "../utils/open-run";
9
9
  import { leaseMs as defaultLeaseMs } from "../utils/settings";
10
+ import { runnerScope } from "../utils/runner-scope";
11
+ import { Scope } from "../../../common/repositories/index.js";
10
12
  let StartAutomationRunUseCase = class extends BaseUseCase {
11
13
  constructor(automationRepository, automationRunRepository) {
12
14
  super();
@@ -16,15 +18,15 @@ let StartAutomationRunUseCase = class extends BaseUseCase {
16
18
  automationRepository;
17
19
  automationRunRepository;
18
20
  async execute(params) {
19
- const { automationId, workerId } = params;
21
+ const { automationId, workerId, currentUser } = params;
20
22
  if (!automationId || !workerId) throw new Error("The automation and the worker are required");
21
23
  const now = params.now ?? /* @__PURE__ */ new Date();
22
- const existing = await this.automationRepository.findById(automationId);
23
- if (!existing) throw new Error("Automation not found");
24
- const automation = await this.automationRepository.claimManual({ id: automationId, workerId, now, leaseMs: params.leaseMs ?? defaultLeaseMs() });
24
+ const existing = await this.automationRepository.findById(automationId, params.scope ?? Scope.canEdit(currentUser));
25
+ if (!existing) return this.throwHttpError({ statusCode: 404, errorMessage: "{{resource_not_found}}" });
26
+ const automation = await this.automationRepository.claimManual({ id: automationId, workerId, now, leaseMs: params.leaseMs ?? defaultLeaseMs() }, runnerScope);
25
27
  if (!automation) throw new Error("The automation is already running");
26
- const run = await openRun(this.automationRunRepository, { automation, trigger: "manual", status: "running", workerId, now });
27
- await this.automationRepository.setActiveRun(automationId, workerId, String(run._id));
28
+ const run = await openRun(this.automationRunRepository, { scope: runnerScope, automation, trigger: "manual", status: "running", workerId, now });
29
+ await this.automationRepository.setActiveRun(automationId, workerId, String(run._id), runnerScope);
28
30
  return { data: { automation, run: run.toJSON() } };
29
31
  }
30
32
  };
@@ -36,6 +36,7 @@ var import_base = require("../../../common/base/base.use-case.js");
36
36
  var import_chats = require("../../chats/index.js");
37
37
  var import_entities = require("../entities");
38
38
  var import_repositories = require("../repositories");
39
+ var import_runner_scope = require("../utils/runner-scope");
39
40
  const LOST_WORKER_MESSAGE = "The worker that was running this automation was lost before it finished";
40
41
  let SyncAutomationRunUseCase = class extends import_base.BaseUseCase {
41
42
  constructor(automationRepository, automationRunRepository) {
@@ -47,7 +48,7 @@ let SyncAutomationRunUseCase = class extends import_base.BaseUseCase {
47
48
  automationRunRepository;
48
49
  async execute(params) {
49
50
  const now = params.now ?? /* @__PURE__ */ new Date();
50
- const run = await this.automationRunRepository?.findById(params.runId);
51
+ const run = await this.automationRunRepository?.findById(params.runId, import_runner_scope.runnerScope);
51
52
  if (!run) return { data: null };
52
53
  if (run.status === "paused") return { data: await this.syncPaused(run, now) };
53
54
  if (run.status === "running") return { data: await this.syncRunning(run, now) };
@@ -55,22 +56,22 @@ let SyncAutomationRunUseCase = class extends import_base.BaseUseCase {
55
56
  }
56
57
  async syncPaused(run, now) {
57
58
  if (!run.chatId) return run;
58
- const { data: chat } = await (0, import_chats.getChatById)({ id: String(run.chatId) });
59
+ const { data: chat } = await (0, import_chats.getChatById)({ id: String(run.chatId), scope: import_runner_scope.runnerScope });
59
60
  const status = chat?.state?.status;
60
61
  if (status === "idle") return await this.settle(run, "completed", now);
61
62
  if (status === "failed") return await this.settle(run, "failed", now, { code: chat?.state?.lastError?.code, message: chat?.state?.lastError?.message ?? "The run failed" });
62
63
  return run;
63
64
  }
64
65
  async syncRunning(run, now) {
65
- const automation = run.automationId ? await this.automationRepository?.findById(String(run.automationId)) : null;
66
+ const automation = run.automationId ? await this.automationRepository?.findById(String(run.automationId), import_runner_scope.runnerScope) : null;
66
67
  const lockedUntil = automation?.lockedUntil ? new Date(automation.lockedUntil).getTime() : 0;
67
68
  if (automation && lockedUntil > now.getTime()) return run;
68
69
  return await this.settle(run, "failed", now, { code: "WORKER_LOST", message: LOST_WORKER_MESSAGE });
69
70
  }
70
71
  async settle(run, status, now, error) {
71
72
  const updated = import_entities.AutomationRun.fromJSON({ ...run, status, finishedAt: now, ...error ? { error } : {} });
72
- const saved = await this.automationRunRepository?.update(updated);
73
- if (run.automationId && run._id) await this.automationRepository?.settleRun(String(run.automationId), String(run._id), status, now);
73
+ const saved = await this.automationRunRepository?.update(updated, import_runner_scope.runnerScope);
74
+ if (run.automationId && run._id) await this.automationRepository?.settleRun(String(run.automationId), String(run._id), status, now, import_runner_scope.runnerScope);
74
75
  return saved ?? updated.toJSON();
75
76
  }
76
77
  };
@@ -7,6 +7,7 @@ import { BaseUseCase } from "../../../common/base/base.use-case.js";
7
7
  import { getChatById } from "../../chats/index.js";
8
8
  import { AutomationRun } from "../entities";
9
9
  import { AutomationRepository, AutomationRunRepository } from "../repositories";
10
+ import { runnerScope } from "../utils/runner-scope";
10
11
  const LOST_WORKER_MESSAGE = "The worker that was running this automation was lost before it finished";
11
12
  let SyncAutomationRunUseCase = class extends BaseUseCase {
12
13
  constructor(automationRepository, automationRunRepository) {
@@ -18,7 +19,7 @@ let SyncAutomationRunUseCase = class extends BaseUseCase {
18
19
  automationRunRepository;
19
20
  async execute(params) {
20
21
  const now = params.now ?? /* @__PURE__ */ new Date();
21
- const run = await this.automationRunRepository?.findById(params.runId);
22
+ const run = await this.automationRunRepository?.findById(params.runId, runnerScope);
22
23
  if (!run) return { data: null };
23
24
  if (run.status === "paused") return { data: await this.syncPaused(run, now) };
24
25
  if (run.status === "running") return { data: await this.syncRunning(run, now) };
@@ -26,22 +27,22 @@ let SyncAutomationRunUseCase = class extends BaseUseCase {
26
27
  }
27
28
  async syncPaused(run, now) {
28
29
  if (!run.chatId) return run;
29
- const { data: chat } = await getChatById({ id: String(run.chatId) });
30
+ const { data: chat } = await getChatById({ id: String(run.chatId), scope: runnerScope });
30
31
  const status = chat?.state?.status;
31
32
  if (status === "idle") return await this.settle(run, "completed", now);
32
33
  if (status === "failed") return await this.settle(run, "failed", now, { code: chat?.state?.lastError?.code, message: chat?.state?.lastError?.message ?? "The run failed" });
33
34
  return run;
34
35
  }
35
36
  async syncRunning(run, now) {
36
- const automation = run.automationId ? await this.automationRepository?.findById(String(run.automationId)) : null;
37
+ const automation = run.automationId ? await this.automationRepository?.findById(String(run.automationId), runnerScope) : null;
37
38
  const lockedUntil = automation?.lockedUntil ? new Date(automation.lockedUntil).getTime() : 0;
38
39
  if (automation && lockedUntil > now.getTime()) return run;
39
40
  return await this.settle(run, "failed", now, { code: "WORKER_LOST", message: LOST_WORKER_MESSAGE });
40
41
  }
41
42
  async settle(run, status, now, error) {
42
43
  const updated = AutomationRun.fromJSON({ ...run, status, finishedAt: now, ...error ? { error } : {} });
43
- const saved = await this.automationRunRepository?.update(updated);
44
- if (run.automationId && run._id) await this.automationRepository?.settleRun(String(run.automationId), String(run._id), status, now);
44
+ const saved = await this.automationRunRepository?.update(updated, runnerScope);
45
+ if (run.automationId && run._id) await this.automationRepository?.settleRun(String(run.automationId), String(run._id), status, now, runnerScope);
45
46
  return saved ?? updated.toJSON();
46
47
  }
47
48
  };
@@ -34,6 +34,7 @@ var import_tsyringe = require("tsyringe");
34
34
  var import_base = require("../../../common/base/base.use-case.js");
35
35
  var import_repositories = require("../repositories");
36
36
  var import_sync_automation_run = require("./sync-automation-run.use-case");
37
+ var import_runner_scope = require("../utils/runner-scope");
37
38
  const DEFAULT_LIMIT = 100;
38
39
  let SyncAutomationRunsUseCase = class extends import_base.BaseUseCase {
39
40
  constructor(automationRunRepository, syncRun) {
@@ -46,8 +47,8 @@ let SyncAutomationRunsUseCase = class extends import_base.BaseUseCase {
46
47
  async execute(params) {
47
48
  const limit = params.limit ?? DEFAULT_LIMIT;
48
49
  const open = [
49
- ...await this.automationRunRepository?.findByStatus("paused", limit) ?? [],
50
- ...await this.automationRunRepository?.findByStatus("running", limit) ?? []
50
+ ...await this.automationRunRepository?.findByStatus("paused", limit, import_runner_scope.runnerScope) ?? [],
51
+ ...await this.automationRunRepository?.findByStatus("running", limit, import_runner_scope.runnerScope) ?? []
51
52
  ];
52
53
  let settled = 0;
53
54
  for (const run of open) {
@@ -6,6 +6,7 @@ import { autoInjectable, inject } from "tsyringe";
6
6
  import { BaseUseCase } from "../../../common/base/base.use-case.js";
7
7
  import { AutomationRunRepository } from "../repositories";
8
8
  import { SyncAutomationRunUseCase } from "./sync-automation-run.use-case";
9
+ import { runnerScope } from "../utils/runner-scope";
9
10
  const DEFAULT_LIMIT = 100;
10
11
  let SyncAutomationRunsUseCase = class extends BaseUseCase {
11
12
  constructor(automationRunRepository, syncRun) {
@@ -18,8 +19,8 @@ let SyncAutomationRunsUseCase = class extends BaseUseCase {
18
19
  async execute(params) {
19
20
  const limit = params.limit ?? DEFAULT_LIMIT;
20
21
  const open = [
21
- ...await this.automationRunRepository?.findByStatus("paused", limit) ?? [],
22
- ...await this.automationRunRepository?.findByStatus("running", limit) ?? []
22
+ ...await this.automationRunRepository?.findByStatus("paused", limit, runnerScope) ?? [],
23
+ ...await this.automationRunRepository?.findByStatus("running", limit, runnerScope) ?? []
23
24
  ];
24
25
  let settled = 0;
25
26
  for (const run of open) {
@@ -1,9 +1,9 @@
1
1
  import { BaseUseCase, BaseUseCaseParams, BaseUseCaseOutput } from '../../../common/base/base.use-case.js';
2
- import { AutomationProps } from '../entities';
2
+ import { AutomationPayload, AutomationProps } from '../entities';
3
3
  import { AutomationRepository } from '../repositories';
4
4
  export interface Params extends BaseUseCaseParams {
5
5
  id: string;
6
- payload?: AutomationProps;
6
+ payload?: AutomationPayload;
7
7
  }
8
8
  export interface Output extends BaseUseCaseOutput {
9
9
  data: AutomationProps;
@@ -32,9 +32,10 @@ __export(update_automation_use_case_exports, {
32
32
  module.exports = __toCommonJS(update_automation_use_case_exports);
33
33
  var import_tsyringe = require("tsyringe");
34
34
  var import_base = require("../../../common/base/base.use-case.js");
35
+ var import_repositories = require("../../../common/repositories/index.js");
35
36
  var import_mongo_db = require("../../../common/repositories/mongo-db.repository.js");
36
37
  var import_entities = require("../entities");
37
- var import_repositories = require("../repositories");
38
+ var import_repositories2 = require("../repositories");
38
39
  var import_automation_fields = require("../utils/automation-fields");
39
40
  let UpdateAutomationUseCase = class extends import_base.BaseUseCase {
40
41
  constructor(automationRepository) {
@@ -45,10 +46,11 @@ let UpdateAutomationUseCase = class extends import_base.BaseUseCase {
45
46
  async execute(params) {
46
47
  const { id, payload, currentUser } = params;
47
48
  if (!payload || Object.keys(payload).length === 0) throw new Error("Payload is required for updating an automation");
48
- const existing = await this.automationRepository?.findById(id);
49
- if (!existing) throw new Error("Automation not found or update failed");
49
+ const scope = params.scope ?? import_repositories.Scope.canEdit(currentUser);
50
+ const existing = await this.automationRepository?.findById(id, scope);
51
+ if (!existing) return this.throwHttpError({ statusCode: 404, errorMessage: "{{resource_not_found}}" });
50
52
  if (payload.__v !== void 0 && payload.__v !== existing.__v) throw new import_mongo_db.VersionConflictError();
51
- const changes = (0, import_automation_fields.readAutomationFields)(payload, { complete: false, trigger: existing.trigger });
53
+ const changes = (0, import_automation_fields.readAutomationFields)(payload, { complete: false, triggers: existing.triggers });
52
54
  const { modelProvider, modelName, ...rest } = changes;
53
55
  const provider = modelProvider === void 0 ? existing.modelProvider : modelProvider;
54
56
  const name = modelName === void 0 ? existing.modelName : modelName;
@@ -60,22 +62,24 @@ let UpdateAutomationUseCase = class extends import_base.BaseUseCase {
60
62
  modelName: name ?? void 0,
61
63
  _id: id
62
64
  });
63
- automation.setUpdatedBy(currentUser);
64
65
  if (!provider) automation.unset(["modelProvider", "modelName"]);
65
- const schedulingChanged = rest.trigger !== void 0 || rest.enabled !== void 0;
66
- const missingNextRun = automation.enabled && automation.isScheduled() && !automation.nextRunAt;
67
- if (schedulingChanged || missingNextRun) {
66
+ const removed = ["schedule", "event"].filter((key) => existing.triggers?.[key] && !automation.triggers?.[key]);
67
+ if (removed.length) automation.toUnset = Object.fromEntries(removed.map((key) => [`triggers.${key}`, ""]));
68
+ if (removed.includes("event")) automation.eventTokenVersion = existing.eventTokenVersion + 1;
69
+ const schedule = automation.triggers?.schedule;
70
+ const scheduleChanged = payload.triggers?.schedule !== void 0;
71
+ if (schedule && (scheduleChanged || schedule.enabled && !schedule.nextRunAt)) {
68
72
  automation.scheduleNextRun();
69
- if (automation.enabled && automation.isScheduled() && !automation.nextRunAt) throw new Error("The date of the recurrence has already passed");
73
+ if (schedule.enabled && !schedule.nextRunAt) throw new Error("The date of the recurrence has already passed");
70
74
  }
71
- const result = await this.automationRepository?.update(automation, { expectedVersion: existing.__v });
72
- if (!result) throw new Error("Automation not found or update failed");
75
+ const result = await this.automationRepository?.update(automation, scope, { expectedVersion: existing.__v });
76
+ if (!result) return this.throwHttpError({ statusCode: 404, errorMessage: "{{resource_not_found}}" });
73
77
  return { data: result };
74
78
  }
75
79
  };
76
80
  UpdateAutomationUseCase = __decorateClass([
77
81
  (0, import_tsyringe.autoInjectable)(),
78
- __decorateParam(0, (0, import_tsyringe.inject)(import_repositories.AutomationRepository))
82
+ __decorateParam(0, (0, import_tsyringe.inject)(import_repositories2.AutomationRepository))
79
83
  ], UpdateAutomationUseCase);
80
84
  // Annotate the CommonJS export names for ESM import in node:
81
85
  0 && (module.exports = {
@@ -4,6 +4,7 @@ import {
4
4
  } from "../../../chunk-CF3WOMB3.mjs";
5
5
  import { autoInjectable, inject } from "tsyringe";
6
6
  import { BaseUseCase } from "../../../common/base/base.use-case.js";
7
+ import { Scope } from "../../../common/repositories/index.js";
7
8
  import { VersionConflictError } from "../../../common/repositories/mongo-db.repository.js";
8
9
  import { Automation } from "../entities";
9
10
  import { AutomationRepository } from "../repositories";
@@ -17,10 +18,11 @@ let UpdateAutomationUseCase = class extends BaseUseCase {
17
18
  async execute(params) {
18
19
  const { id, payload, currentUser } = params;
19
20
  if (!payload || Object.keys(payload).length === 0) throw new Error("Payload is required for updating an automation");
20
- const existing = await this.automationRepository?.findById(id);
21
- if (!existing) throw new Error("Automation not found or update failed");
21
+ const scope = params.scope ?? Scope.canEdit(currentUser);
22
+ const existing = await this.automationRepository?.findById(id, scope);
23
+ if (!existing) return this.throwHttpError({ statusCode: 404, errorMessage: "{{resource_not_found}}" });
22
24
  if (payload.__v !== void 0 && payload.__v !== existing.__v) throw new VersionConflictError();
23
- const changes = readAutomationFields(payload, { complete: false, trigger: existing.trigger });
25
+ const changes = readAutomationFields(payload, { complete: false, triggers: existing.triggers });
24
26
  const { modelProvider, modelName, ...rest } = changes;
25
27
  const provider = modelProvider === void 0 ? existing.modelProvider : modelProvider;
26
28
  const name = modelName === void 0 ? existing.modelName : modelName;
@@ -32,16 +34,18 @@ let UpdateAutomationUseCase = class extends BaseUseCase {
32
34
  modelName: name ?? void 0,
33
35
  _id: id
34
36
  });
35
- automation.setUpdatedBy(currentUser);
36
37
  if (!provider) automation.unset(["modelProvider", "modelName"]);
37
- const schedulingChanged = rest.trigger !== void 0 || rest.enabled !== void 0;
38
- const missingNextRun = automation.enabled && automation.isScheduled() && !automation.nextRunAt;
39
- if (schedulingChanged || missingNextRun) {
38
+ const removed = ["schedule", "event"].filter((key) => existing.triggers?.[key] && !automation.triggers?.[key]);
39
+ if (removed.length) automation.toUnset = Object.fromEntries(removed.map((key) => [`triggers.${key}`, ""]));
40
+ if (removed.includes("event")) automation.eventTokenVersion = existing.eventTokenVersion + 1;
41
+ const schedule = automation.triggers?.schedule;
42
+ const scheduleChanged = payload.triggers?.schedule !== void 0;
43
+ if (schedule && (scheduleChanged || schedule.enabled && !schedule.nextRunAt)) {
40
44
  automation.scheduleNextRun();
41
- if (automation.enabled && automation.isScheduled() && !automation.nextRunAt) throw new Error("The date of the recurrence has already passed");
45
+ if (schedule.enabled && !schedule.nextRunAt) throw new Error("The date of the recurrence has already passed");
42
46
  }
43
- const result = await this.automationRepository?.update(automation, { expectedVersion: existing.__v });
44
- if (!result) throw new Error("Automation not found or update failed");
47
+ const result = await this.automationRepository?.update(automation, scope, { expectedVersion: existing.__v });
48
+ if (!result) return this.throwHttpError({ statusCode: 404, errorMessage: "{{resource_not_found}}" });
45
49
  return { data: result };
46
50
  }
47
51
  };
@@ -1,14 +1,13 @@
1
- import { AutomationTrigger } from '../entities/trigger';
1
+ import { AutomationTriggers } from '../entities/trigger';
2
2
  export interface AutomationFields {
3
3
  name?: string;
4
4
  instructions?: string;
5
- enabled?: boolean;
6
- trigger?: AutomationTrigger;
5
+ triggers?: AutomationTriggers;
7
6
  modelProvider?: string | null;
8
7
  modelName?: string | null;
9
8
  }
10
9
  export declare function readAutomationFields(payload: Record<string, any> | undefined, options: {
11
10
  complete: boolean;
12
- trigger?: AutomationTrigger;
11
+ triggers?: AutomationTriggers;
13
12
  }): AutomationFields;
14
13
  export declare function assertModel(provider?: string | null, name?: string | null): void;
@@ -40,11 +40,9 @@ function readAutomationFields(payload, options) {
40
40
  const has = (key) => input[key] !== void 0;
41
41
  if (options.complete || has("name")) fields.name = text(input.name, "name", MAX_NAME_CHARS);
42
42
  if (options.complete || has("instructions")) fields.instructions = text(input.instructions, "instructions", MAX_INSTRUCTIONS_CHARS);
43
- if (options.complete || has("trigger")) fields.trigger = (0, import_trigger.readTrigger)(input.trigger, options.trigger);
44
- if (has("enabled")) {
45
- if (typeof input.enabled !== "boolean") throw new Error("The field enabled must be true or false");
46
- fields.enabled = input.enabled;
47
- }
43
+ if (has("trigger")) throw new Error("The field trigger was replaced by triggers");
44
+ if (has("enabled")) throw new Error("The field enabled belongs to each trigger, send it inside triggers.schedule or triggers.event");
45
+ if (options.complete || has("triggers")) fields.triggers = (0, import_trigger.readTriggers)(input.triggers, options.triggers);
48
46
  if (has("modelProvider")) fields.modelProvider = optionalText(input.modelProvider, "model provider");
49
47
  if (has("modelName")) fields.modelName = optionalText(input.modelName, "model name");
50
48
  return fields;
@@ -1,5 +1,5 @@
1
1
  import "../../../chunk-CF3WOMB3.mjs";
2
- import { readTrigger } from "../entities/trigger";
2
+ import { readTriggers } from "../entities/trigger";
3
3
  const MAX_NAME_CHARS = 200;
4
4
  const MAX_INSTRUCTIONS_CHARS = 2e4;
5
5
  const text = (value, label, max) => {
@@ -17,11 +17,9 @@ function readAutomationFields(payload, options) {
17
17
  const has = (key) => input[key] !== void 0;
18
18
  if (options.complete || has("name")) fields.name = text(input.name, "name", MAX_NAME_CHARS);
19
19
  if (options.complete || has("instructions")) fields.instructions = text(input.instructions, "instructions", MAX_INSTRUCTIONS_CHARS);
20
- if (options.complete || has("trigger")) fields.trigger = readTrigger(input.trigger, options.trigger);
21
- if (has("enabled")) {
22
- if (typeof input.enabled !== "boolean") throw new Error("The field enabled must be true or false");
23
- fields.enabled = input.enabled;
24
- }
20
+ if (has("trigger")) throw new Error("The field trigger was replaced by triggers");
21
+ if (has("enabled")) throw new Error("The field enabled belongs to each trigger, send it inside triggers.schedule or triggers.event");
22
+ if (options.complete || has("triggers")) fields.triggers = readTriggers(input.triggers, options.triggers);
25
23
  if (has("modelProvider")) fields.modelProvider = optionalText(input.modelProvider, "model provider");
26
24
  if (has("modelName")) fields.modelName = optionalText(input.modelName, "model name");
27
25
  return fields;
@@ -1,6 +1,8 @@
1
+ import { Scope } from '../../../common/repositories/index.js';
1
2
  import { AutomationProps, AutomationRun, AutomationRunStatus, AutomationRunTrigger } from '../entities';
2
3
  import { AutomationRunRepository } from '../repositories';
3
4
  export interface OpenRunParams {
5
+ scope: Scope;
4
6
  automation: AutomationProps;
5
7
  trigger: AutomationRunTrigger;
6
8
  status: Extract<AutomationRunStatus, 'queued' | 'running' | 'skipped'>;
@@ -23,7 +23,7 @@ __export(open_run_exports, {
23
23
  module.exports = __toCommonJS(open_run_exports);
24
24
  var import_entities = require("../entities");
25
25
  async function openRun(repository, params) {
26
- const { automation, trigger, status, workerId, now, scheduledFor, skipReason, payload, idempotencyKey } = params;
26
+ const { scope, automation, trigger, status, workerId, now, scheduledFor, skipReason, payload, idempotencyKey } = params;
27
27
  const run = import_entities.AutomationRun.fromJSON({
28
28
  automationId: automation._id,
29
29
  ownerId: automation.ownerId,
@@ -40,7 +40,7 @@ async function openRun(repository, params) {
40
40
  idempotencyKey,
41
41
  createdAt: now
42
42
  });
43
- await repository.create(run);
43
+ await repository.create(run, scope);
44
44
  return run;
45
45
  }
46
46
  // Annotate the CommonJS export names for ESM import in node:
@@ -1,7 +1,7 @@
1
1
  import "../../../chunk-CF3WOMB3.mjs";
2
2
  import { AutomationRun } from "../entities";
3
3
  async function openRun(repository, params) {
4
- const { automation, trigger, status, workerId, now, scheduledFor, skipReason, payload, idempotencyKey } = params;
4
+ const { scope, automation, trigger, status, workerId, now, scheduledFor, skipReason, payload, idempotencyKey } = params;
5
5
  const run = AutomationRun.fromJSON({
6
6
  automationId: automation._id,
7
7
  ownerId: automation.ownerId,
@@ -18,7 +18,7 @@ async function openRun(repository, params) {
18
18
  idempotencyKey,
19
19
  createdAt: now
20
20
  });
21
- await repository.create(run);
21
+ await repository.create(run, scope);
22
22
  return run;
23
23
  }
24
24
  export {
@@ -23,13 +23,14 @@ __export(previous_run_exports, {
23
23
  module.exports = __toCommonJS(previous_run_exports);
24
24
  var import_entities = require("../entities");
25
25
  var import_sync_automation_run = require("../use-cases/sync-automation-run.use-case");
26
+ var import_runner_scope = require("./runner-scope");
26
27
  async function findPausedRun(runs, syncRun, automation, now) {
27
28
  if (!automation.activeRunId) return null;
28
- const previous = await runs.findById(String(automation.activeRunId));
29
+ const previous = await runs.findById(String(automation.activeRunId), import_runner_scope.runnerScope);
29
30
  if (!previous) return null;
30
31
  if (previous.status === "running") {
31
32
  const lost = import_entities.AutomationRun.fromJSON({ ...previous, status: "failed", finishedAt: now, error: { code: "WORKER_LOST", message: import_sync_automation_run.LOST_WORKER_MESSAGE } });
32
- await runs.update(lost);
33
+ await runs.update(lost, import_runner_scope.runnerScope);
33
34
  return null;
34
35
  }
35
36
  if (previous.status !== "paused") return null;
@@ -1,13 +1,14 @@
1
1
  import "../../../chunk-CF3WOMB3.mjs";
2
2
  import { AutomationRun } from "../entities";
3
3
  import { LOST_WORKER_MESSAGE } from "../use-cases/sync-automation-run.use-case";
4
+ import { runnerScope } from "./runner-scope";
4
5
  async function findPausedRun(runs, syncRun, automation, now) {
5
6
  if (!automation.activeRunId) return null;
6
- const previous = await runs.findById(String(automation.activeRunId));
7
+ const previous = await runs.findById(String(automation.activeRunId), runnerScope);
7
8
  if (!previous) return null;
8
9
  if (previous.status === "running") {
9
10
  const lost = AutomationRun.fromJSON({ ...previous, status: "failed", finishedAt: now, error: { code: "WORKER_LOST", message: LOST_WORKER_MESSAGE } });
10
- await runs.update(lost);
11
+ await runs.update(lost, runnerScope);
11
12
  return null;
12
13
  }
13
14
  if (previous.status !== "paused") return null;
@@ -0,0 +1,3 @@
1
+ import { Scope } from '../../../common/repositories/index.js';
2
+ export declare const runnerScope: Scope;
3
+ export declare const eventScope: Scope;