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
@@ -8,39 +8,42 @@ import { getDatabaseName } from "../../../common/utils/database-name.js";
8
8
  let AutomationRunRepository = class {
9
9
  _ = new MongoRepository({
10
10
  database: getDatabaseName(),
11
- collection: "automation_runs"
11
+ collection: "automation_runs",
12
+ scoped: true
12
13
  });
13
- async create(run) {
14
- const id = await this._.createOne({ payload: run.toDocument() });
15
- id && run.setId(id);
14
+ async create(run, scope) {
15
+ const { _id, ownerId, createdAt, createdBy } = await this._.createOne({ payload: run.toDocument(), scope });
16
+ Object.assign(run, { _id, ownerId, createdAt, createdBy });
16
17
  return run;
17
18
  }
18
- async update(run) {
19
+ async update(run, scope) {
19
20
  return await this._.updateOne({
21
+ scope,
20
22
  where: { _id: run._id },
21
23
  payload: run.toFlatDocument()
22
24
  });
23
25
  }
24
- async findById(id) {
25
- return await this._.findOne({ where: { _id: this._.parseId(id) } });
26
+ async findById(id, scope) {
27
+ return await this._.findOne({ scope, where: { _id: this._.parseId(id) } });
26
28
  }
27
- async findByAutomation(automationId, options) {
28
- return await this._.findMany({ where: { automationId: new ObjectId(automationId) }, options });
29
+ async findByAutomation(automationId, scope, options) {
30
+ return await this._.findMany({ scope, where: { automationId: new ObjectId(automationId) }, options });
29
31
  }
30
- async findByOwner(ownerId, options, status) {
31
- return await this._.findMany({ where: { ownerId: new ObjectId(ownerId), ...status ? { status } : {} }, options });
32
+ async findByOwner(ownerId, scope, options, status) {
33
+ return await this._.findMany({ scope, where: { ownerId: new ObjectId(ownerId), ...status ? { status } : {} }, options });
32
34
  }
33
- async findByStatus(status, limit) {
34
- return await this._.findMany({ where: { status }, options: { page: 1, limit, sortBy: { createdAt: 1 } } });
35
+ async findByStatus(status, limit, scope) {
36
+ return await this._.findMany({ scope, where: { status }, options: { page: 1, limit, sortBy: { createdAt: 1 } } });
35
37
  }
36
- async findByIdempotencyKey(automationId, idempotencyKey) {
37
- return await this._.findOne({ where: { automationId: new ObjectId(automationId), idempotencyKey } });
38
+ async findByIdempotencyKey(automationId, idempotencyKey, scope) {
39
+ return await this._.findOne({ scope, where: { automationId: new ObjectId(automationId), idempotencyKey } });
38
40
  }
39
- async countQueued(automationId) {
40
- return await this._.count({ where: { automationId: new ObjectId(automationId), status: "queued" } });
41
+ async countQueued(automationId, scope) {
42
+ return await this._.count({ scope, where: { automationId: new ObjectId(automationId), status: "queued" } });
41
43
  }
42
- async claimQueued(id, fields) {
44
+ async claimQueued(id, fields, scope) {
43
45
  return await this._.updateOne({
46
+ scope,
44
47
  where: { _id: this._.parseId(id), status: "queued" },
45
48
  payload: { ...fields, status: "running" }
46
49
  });
@@ -1,4 +1,4 @@
1
- import { MongoRepository, QueryOptions } from '../../../common/repositories/index.js';
1
+ import { MongoRepository, QueryOptions, Scope } from '../../../common/repositories/index.js';
2
2
  import { Automation, AutomationProps, AutomationRunStatus } from '../entities';
3
3
  export interface ClaimParams {
4
4
  id: string;
@@ -9,19 +9,20 @@ export interface ClaimParams {
9
9
  }
10
10
  export declare class AutomationRepository {
11
11
  readonly _: MongoRepository<AutomationProps>;
12
- create(automation: Automation): Promise<Automation>;
13
- update(automation: Automation, options?: {
12
+ create(automation: Automation, scope: Scope): Promise<Automation>;
13
+ update(automation: Automation, scope: Scope, options?: {
14
14
  expectedVersion?: number;
15
15
  }): Promise<AutomationProps | null>;
16
- findById(id: string): Promise<AutomationProps | null>;
17
- findByOwner(ownerId: string, options?: QueryOptions): Promise<AutomationProps[]>;
18
- deleteById(id: string): Promise<number>;
19
- findDue(now: Date, limit: number): Promise<AutomationProps[]>;
20
- claim(params: ClaimParams): Promise<AutomationProps | null>;
21
- claimManual(params: Omit<ClaimParams, 'nextRunAt'>): Promise<AutomationProps | null>;
22
- setActiveRun(id: string, workerId: string, runId: string): Promise<AutomationProps | null>;
23
- settleRun(id: string, runId: string, status: AutomationRunStatus, at: Date): Promise<AutomationProps | null>;
24
- renewLease(id: string, workerId: string, until: Date): Promise<AutomationProps | null>;
25
- release(id: string, workerId: string, result: Pick<AutomationProps, 'lastRunAt' | 'lastRunStatus' | 'activeRunId'>): Promise<AutomationProps | null>;
16
+ findById(id: string, scope: Scope): Promise<AutomationProps | null>;
17
+ findByOwner(ownerId: string, scope: Scope, options?: QueryOptions): Promise<AutomationProps[]>;
18
+ softDeleteById(id: string, scope: Scope): Promise<AutomationProps | null>;
19
+ deleteById(id: string, scope: Scope): Promise<number>;
20
+ findDue(now: Date, limit: number, scope: Scope): Promise<AutomationProps[]>;
21
+ claim(params: ClaimParams, scope: Scope): Promise<AutomationProps | null>;
22
+ claimManual(params: Omit<ClaimParams, 'nextRunAt'>, scope: Scope): Promise<AutomationProps | null>;
23
+ setActiveRun(id: string, workerId: string, runId: string, scope: Scope): Promise<AutomationProps | null>;
24
+ settleRun(id: string, runId: string, status: AutomationRunStatus, at: Date, scope: Scope): Promise<AutomationProps | null>;
25
+ renewLease(id: string, workerId: string, until: Date, scope: Scope): Promise<AutomationProps | null>;
26
+ release(id: string, workerId: string, result: Pick<AutomationProps, 'lastRunAt' | 'lastRunStatus' | 'activeRunId'>, scope: Scope): Promise<AutomationProps | null>;
26
27
  private unlocked;
27
28
  }
@@ -36,75 +36,87 @@ var import_database_name = require("../../../common/utils/database-name.js");
36
36
  let AutomationRepository = class {
37
37
  _ = new import_repositories.MongoRepository({
38
38
  database: (0, import_database_name.getDatabaseName)(),
39
- collection: "automations"
39
+ collection: "automations",
40
+ scoped: true
40
41
  });
41
- async create(automation) {
42
- const id = await this._.createOne({ payload: automation.toDocument() });
43
- id && automation.setId(id);
42
+ async create(automation, scope) {
43
+ const { _id, ownerId, createdAt, createdBy } = await this._.createOne({ payload: automation.toDocument(), scope });
44
+ Object.assign(automation, { _id, ownerId, createdAt, createdBy });
44
45
  return automation;
45
46
  }
46
- async update(automation, options) {
47
+ async update(automation, scope, options) {
47
48
  if (options && typeof options.expectedVersion !== "number") throw new Error("[AutomationRepository] The expected version is required to check for conflicts");
48
49
  return await this._.updateOne({
50
+ scope,
49
51
  where: { _id: automation._id },
50
52
  payload: automation.toFlatDocument(),
51
53
  expectedVersion: options?.expectedVersion
52
54
  });
53
55
  }
54
- async findById(id) {
55
- return await this._.findOne({ where: { _id: this._.parseId(id) } });
56
+ async findById(id, scope) {
57
+ return await this._.findOne({ scope, where: { _id: this._.parseId(id) } });
56
58
  }
57
- async findByOwner(ownerId, options) {
58
- return await this._.findMany({ where: { ownerId: new import_mongodb.ObjectId(ownerId) }, options });
59
+ async findByOwner(ownerId, scope, options) {
60
+ return await this._.findMany({ scope, where: { ownerId: new import_mongodb.ObjectId(ownerId) }, options });
59
61
  }
60
- async deleteById(id) {
61
- return await this._.deleteOne({ where: { _id: this._.parseId(id) } });
62
+ async softDeleteById(id, scope) {
63
+ return await this._.softDelete({ scope, where: { _id: this._.parseId(id) }, payload: { toUnset: { "triggers.schedule.nextRunAt": "" } } });
62
64
  }
63
- async findDue(now, limit) {
65
+ async deleteById(id, scope) {
66
+ return await this._.deleteOne({ scope, where: { _id: this._.parseId(id) } });
67
+ }
68
+ async findDue(now, limit, scope) {
64
69
  return await this._.findMany({
65
- where: { enabled: true, nextRunAt: { $lte: now }, ...this.unlocked(now) },
66
- options: { page: 1, limit, sortBy: { nextRunAt: 1 } }
70
+ scope,
71
+ where: { "triggers.schedule.enabled": true, "triggers.schedule.nextRunAt": { $lte: now }, ...this.unlocked(now) },
72
+ options: { page: 1, limit, sortBy: { "triggers.schedule.nextRunAt": 1 } }
67
73
  });
68
74
  }
69
- async claim(params) {
75
+ async claim(params, scope) {
70
76
  const { id, workerId, now, leaseMs, nextRunAt } = params;
71
77
  return await this._.updateOne({
72
- where: { _id: this._.parseId(id), enabled: true, nextRunAt: { $lte: now }, ...this.unlocked(now) },
78
+ scope,
79
+ where: { _id: this._.parseId(id), "triggers.schedule.enabled": true, "triggers.schedule.nextRunAt": { $lte: now }, ...this.unlocked(now) },
73
80
  payload: {
74
81
  lockedBy: workerId,
75
82
  lockedUntil: new Date(now.getTime() + leaseMs),
76
- ...nextRunAt ? { nextRunAt } : { toUnset: { nextRunAt: "" } }
83
+ ...nextRunAt ? { "triggers.schedule.nextRunAt": nextRunAt } : { toUnset: { "triggers.schedule.nextRunAt": "" } }
77
84
  }
78
85
  });
79
86
  }
80
- async claimManual(params) {
87
+ async claimManual(params, scope) {
81
88
  const { id, workerId, now, leaseMs } = params;
82
89
  return await this._.updateOne({
90
+ scope,
83
91
  where: { _id: this._.parseId(id), ...this.unlocked(now) },
84
92
  payload: { lockedBy: workerId, lockedUntil: new Date(now.getTime() + leaseMs) }
85
93
  });
86
94
  }
87
- async setActiveRun(id, workerId, runId) {
95
+ async setActiveRun(id, workerId, runId, scope) {
88
96
  return await this._.updateOne({
97
+ scope,
89
98
  where: { _id: this._.parseId(id), lockedBy: workerId },
90
99
  payload: { activeRunId: this._.parseId(runId) }
91
100
  });
92
101
  }
93
- async settleRun(id, runId, status, at) {
102
+ async settleRun(id, runId, status, at, scope) {
94
103
  return await this._.updateOne({
104
+ scope,
95
105
  where: { _id: this._.parseId(id), activeRunId: this._.parseId(runId) },
96
106
  payload: { lastRunStatus: status, lastRunAt: at, toUnset: { activeRunId: "" } }
97
107
  });
98
108
  }
99
- async renewLease(id, workerId, until) {
109
+ async renewLease(id, workerId, until, scope) {
100
110
  return await this._.updateOne({
111
+ scope,
101
112
  where: { _id: this._.parseId(id), lockedBy: workerId },
102
113
  payload: { lockedUntil: until }
103
114
  });
104
115
  }
105
- async release(id, workerId, result) {
116
+ async release(id, workerId, result, scope) {
106
117
  const { activeRunId, lastRunAt, lastRunStatus } = result;
107
118
  return await this._.updateOne({
119
+ scope,
108
120
  where: { _id: this._.parseId(id), lockedBy: workerId },
109
121
  payload: {
110
122
  ...lastRunAt ? { lastRunAt } : {},
@@ -8,75 +8,87 @@ import { getDatabaseName } from "../../../common/utils/database-name.js";
8
8
  let AutomationRepository = class {
9
9
  _ = new MongoRepository({
10
10
  database: getDatabaseName(),
11
- collection: "automations"
11
+ collection: "automations",
12
+ scoped: true
12
13
  });
13
- async create(automation) {
14
- const id = await this._.createOne({ payload: automation.toDocument() });
15
- id && automation.setId(id);
14
+ async create(automation, scope) {
15
+ const { _id, ownerId, createdAt, createdBy } = await this._.createOne({ payload: automation.toDocument(), scope });
16
+ Object.assign(automation, { _id, ownerId, createdAt, createdBy });
16
17
  return automation;
17
18
  }
18
- async update(automation, options) {
19
+ async update(automation, scope, options) {
19
20
  if (options && typeof options.expectedVersion !== "number") throw new Error("[AutomationRepository] The expected version is required to check for conflicts");
20
21
  return await this._.updateOne({
22
+ scope,
21
23
  where: { _id: automation._id },
22
24
  payload: automation.toFlatDocument(),
23
25
  expectedVersion: options?.expectedVersion
24
26
  });
25
27
  }
26
- async findById(id) {
27
- return await this._.findOne({ where: { _id: this._.parseId(id) } });
28
+ async findById(id, scope) {
29
+ return await this._.findOne({ scope, where: { _id: this._.parseId(id) } });
28
30
  }
29
- async findByOwner(ownerId, options) {
30
- return await this._.findMany({ where: { ownerId: new ObjectId(ownerId) }, options });
31
+ async findByOwner(ownerId, scope, options) {
32
+ return await this._.findMany({ scope, where: { ownerId: new ObjectId(ownerId) }, options });
31
33
  }
32
- async deleteById(id) {
33
- return await this._.deleteOne({ where: { _id: this._.parseId(id) } });
34
+ async softDeleteById(id, scope) {
35
+ return await this._.softDelete({ scope, where: { _id: this._.parseId(id) }, payload: { toUnset: { "triggers.schedule.nextRunAt": "" } } });
34
36
  }
35
- async findDue(now, limit) {
37
+ async deleteById(id, scope) {
38
+ return await this._.deleteOne({ scope, where: { _id: this._.parseId(id) } });
39
+ }
40
+ async findDue(now, limit, scope) {
36
41
  return await this._.findMany({
37
- where: { enabled: true, nextRunAt: { $lte: now }, ...this.unlocked(now) },
38
- options: { page: 1, limit, sortBy: { nextRunAt: 1 } }
42
+ scope,
43
+ where: { "triggers.schedule.enabled": true, "triggers.schedule.nextRunAt": { $lte: now }, ...this.unlocked(now) },
44
+ options: { page: 1, limit, sortBy: { "triggers.schedule.nextRunAt": 1 } }
39
45
  });
40
46
  }
41
- async claim(params) {
47
+ async claim(params, scope) {
42
48
  const { id, workerId, now, leaseMs, nextRunAt } = params;
43
49
  return await this._.updateOne({
44
- where: { _id: this._.parseId(id), enabled: true, nextRunAt: { $lte: now }, ...this.unlocked(now) },
50
+ scope,
51
+ where: { _id: this._.parseId(id), "triggers.schedule.enabled": true, "triggers.schedule.nextRunAt": { $lte: now }, ...this.unlocked(now) },
45
52
  payload: {
46
53
  lockedBy: workerId,
47
54
  lockedUntil: new Date(now.getTime() + leaseMs),
48
- ...nextRunAt ? { nextRunAt } : { toUnset: { nextRunAt: "" } }
55
+ ...nextRunAt ? { "triggers.schedule.nextRunAt": nextRunAt } : { toUnset: { "triggers.schedule.nextRunAt": "" } }
49
56
  }
50
57
  });
51
58
  }
52
- async claimManual(params) {
59
+ async claimManual(params, scope) {
53
60
  const { id, workerId, now, leaseMs } = params;
54
61
  return await this._.updateOne({
62
+ scope,
55
63
  where: { _id: this._.parseId(id), ...this.unlocked(now) },
56
64
  payload: { lockedBy: workerId, lockedUntil: new Date(now.getTime() + leaseMs) }
57
65
  });
58
66
  }
59
- async setActiveRun(id, workerId, runId) {
67
+ async setActiveRun(id, workerId, runId, scope) {
60
68
  return await this._.updateOne({
69
+ scope,
61
70
  where: { _id: this._.parseId(id), lockedBy: workerId },
62
71
  payload: { activeRunId: this._.parseId(runId) }
63
72
  });
64
73
  }
65
- async settleRun(id, runId, status, at) {
74
+ async settleRun(id, runId, status, at, scope) {
66
75
  return await this._.updateOne({
76
+ scope,
67
77
  where: { _id: this._.parseId(id), activeRunId: this._.parseId(runId) },
68
78
  payload: { lastRunStatus: status, lastRunAt: at, toUnset: { activeRunId: "" } }
69
79
  });
70
80
  }
71
- async renewLease(id, workerId, until) {
81
+ async renewLease(id, workerId, until, scope) {
72
82
  return await this._.updateOne({
83
+ scope,
73
84
  where: { _id: this._.parseId(id), lockedBy: workerId },
74
85
  payload: { lockedUntil: until }
75
86
  });
76
87
  }
77
- async release(id, workerId, result) {
88
+ async release(id, workerId, result, scope) {
78
89
  const { activeRunId, lastRunAt, lastRunStatus } = result;
79
90
  return await this._.updateOne({
91
+ scope,
80
92
  where: { _id: this._.parseId(id), lockedBy: workerId },
81
93
  payload: {
82
94
  ...lastRunAt ? { lastRunAt } : {},
@@ -2,6 +2,7 @@ import { CurrentUser } from '../../../common/base/index.js';
2
2
  import { AgentOptions } from '../../../harness/agent.js';
3
3
  import { Chat } from '../../chats/entities/chat.entity.js';
4
4
  import { AutomationProps, AutomationRunProps } from '../entities';
5
+ import { Scope } from '../../../common/repositories/index.js';
5
6
  export interface AutomationContext {
6
7
  automation: AutomationProps;
7
8
  currentUser: CurrentUser;
@@ -37,6 +38,8 @@ export declare class AutomationRunner {
37
38
  tick(): Promise<void>;
38
39
  runNow(params: {
39
40
  automationId: string;
41
+ currentUser?: CurrentUser;
42
+ scope?: Scope;
40
43
  }): Promise<{
41
44
  run: AutomationRunProps;
42
45
  }>;
@@ -97,7 +97,7 @@ class AutomationRunner {
97
97
  for (const { automation, run } of queued.claimed) this.track(this.execute(automation, run));
98
98
  }
99
99
  async runNow(params) {
100
- const { data } = await import_tsyringe.container.resolve(import_start_automation_run.StartAutomationRunUseCase).execute({ automationId: params.automationId, workerId: this.workerId, leaseMs: this.options.leaseMs ?? (0, import_settings.leaseMs)() });
100
+ const { data } = await import_tsyringe.container.resolve(import_start_automation_run.StartAutomationRunUseCase).execute({ automationId: params.automationId, currentUser: params.currentUser, scope: params.scope, workerId: this.workerId, leaseMs: this.options.leaseMs ?? (0, import_settings.leaseMs)() });
101
101
  this.track(this.execute(data.automation, data.run));
102
102
  return { run: data.run };
103
103
  }
@@ -162,7 +162,7 @@ class AutomationRunner {
162
162
  input: { message: (0, import_event_message.buildMessage)(automation.instructions, run.payload), chatId },
163
163
  options: { executionMode: "autonomous", ...model },
164
164
  currentUser,
165
- currentSession: import_base.CurrentSession.fromJSON({ location: { timezone: automation.trigger?.timezone } }),
165
+ currentSession: import_base.CurrentSession.fromJSON({ location: { timezone: currentUser.preferences?.timezone?.ianaIdentifier } }),
166
166
  serviceToken: process.env.INTERNAL_MCP_TOKEN ? import_secret_manager.secretManager.get("INTERNAL_MCP_TOKEN") : void 0
167
167
  });
168
168
  const chat = result.data;
@@ -74,7 +74,7 @@ class AutomationRunner {
74
74
  for (const { automation, run } of queued.claimed) this.track(this.execute(automation, run));
75
75
  }
76
76
  async runNow(params) {
77
- const { data } = await container.resolve(StartAutomationRunUseCase).execute({ automationId: params.automationId, workerId: this.workerId, leaseMs: this.options.leaseMs ?? defaultLeaseMs() });
77
+ const { data } = await container.resolve(StartAutomationRunUseCase).execute({ automationId: params.automationId, currentUser: params.currentUser, scope: params.scope, workerId: this.workerId, leaseMs: this.options.leaseMs ?? defaultLeaseMs() });
78
78
  this.track(this.execute(data.automation, data.run));
79
79
  return { run: data.run };
80
80
  }
@@ -139,7 +139,7 @@ class AutomationRunner {
139
139
  input: { message: buildMessage(automation.instructions, run.payload), chatId },
140
140
  options: { executionMode: "autonomous", ...model },
141
141
  currentUser,
142
- currentSession: CurrentSession.fromJSON({ location: { timezone: automation.trigger?.timezone } }),
142
+ currentSession: CurrentSession.fromJSON({ location: { timezone: currentUser.preferences?.timezone?.ianaIdentifier } }),
143
143
  serviceToken: process.env.INTERNAL_MCP_TOKEN ? secretManager.get("INTERNAL_MCP_TOKEN") : void 0
144
144
  });
145
145
  const chat = result.data;
@@ -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_entities = require("../entities");
36
36
  var import_repositories = require("../repositories");
37
+ var import_runner_scope = require("../utils/runner-scope");
37
38
  let AttachAutomationRunChatUseCase = class extends import_base.BaseUseCase {
38
39
  constructor(automationRunRepository) {
39
40
  super();
@@ -42,9 +43,9 @@ let AttachAutomationRunChatUseCase = class extends import_base.BaseUseCase {
42
43
  automationRunRepository;
43
44
  async execute(params) {
44
45
  const { runId, chatId } = params;
45
- const run = await this.automationRunRepository.findById(runId);
46
+ const run = await this.automationRunRepository.findById(runId, import_runner_scope.runnerScope);
46
47
  if (!run) return { data: null };
47
- const saved = await this.automationRunRepository.update(import_entities.AutomationRun.fromJSON({ ...run, chatId }));
48
+ const saved = await this.automationRunRepository.update(import_entities.AutomationRun.fromJSON({ ...run, chatId }), import_runner_scope.runnerScope);
48
49
  return { data: saved };
49
50
  }
50
51
  };
@@ -6,6 +6,7 @@ import { autoInjectable, inject } from "tsyringe";
6
6
  import { BaseUseCase } from "../../../common/base/base.use-case.js";
7
7
  import { AutomationRun } from "../entities";
8
8
  import { AutomationRunRepository } from "../repositories";
9
+ import { runnerScope } from "../utils/runner-scope";
9
10
  let AttachAutomationRunChatUseCase = class extends BaseUseCase {
10
11
  constructor(automationRunRepository) {
11
12
  super();
@@ -14,9 +15,9 @@ let AttachAutomationRunChatUseCase = class extends BaseUseCase {
14
15
  automationRunRepository;
15
16
  async execute(params) {
16
17
  const { runId, chatId } = params;
17
- const run = await this.automationRunRepository.findById(runId);
18
+ const run = await this.automationRunRepository.findById(runId, runnerScope);
18
19
  if (!run) return { data: null };
19
- const saved = await this.automationRunRepository.update(AutomationRun.fromJSON({ ...run, chatId }));
20
+ const saved = await this.automationRunRepository.update(AutomationRun.fromJSON({ ...run, chatId }), runnerScope);
20
21
  return { data: saved };
21
22
  }
22
23
  };
@@ -39,6 +39,7 @@ var import_open_run = require("../utils/open-run");
39
39
  var import_settings = require("../utils/settings");
40
40
  var import_previous_run = require("../utils/previous-run");
41
41
  var import_sync_automation_run = require("./sync-automation-run.use-case");
42
+ var import_runner_scope = require("../utils/runner-scope");
42
43
  const DEFAULT_LIMIT = 10;
43
44
  let ClaimDueAutomationsUseCase = class extends import_base.BaseUseCase {
44
45
  constructor(automationRepository, automationRunRepository, syncRun) {
@@ -56,32 +57,32 @@ let ClaimDueAutomationsUseCase = class extends import_base.BaseUseCase {
56
57
  if (!workerId) throw new Error("The id of the worker is required");
57
58
  const now = params.now ?? /* @__PURE__ */ new Date();
58
59
  const leaseMs = params.leaseMs ?? (0, import_settings.leaseMs)();
59
- const due = await this.automationRepository.findDue(now, params.limit ?? DEFAULT_LIMIT);
60
+ const due = await this.automationRepository.findDue(now, params.limit ?? DEFAULT_LIMIT, import_runner_scope.runnerScope);
60
61
  const claimed = [];
61
62
  const skipped = [];
62
63
  for (const candidate of due) {
63
64
  const id = String(candidate._id);
64
- const scheduledFor = candidate.nextRunAt ? new Date(candidate.nextRunAt) : void 0;
65
- const trigger = candidate.trigger;
66
- if (trigger?.type !== "schedule") continue;
65
+ const schedule = candidate.triggers?.schedule;
66
+ if (!schedule) continue;
67
+ const scheduledFor = schedule.nextRunAt ? new Date(schedule.nextRunAt) : void 0;
67
68
  let nextRunAt = null;
68
69
  try {
69
- nextRunAt = (0, import_next_run.computeNextRun)(trigger.recurrence, trigger.timezone, now);
70
+ nextRunAt = (0, import_next_run.computeNextRun)(schedule.recurrence, schedule.timezone, now);
70
71
  } catch (error) {
71
72
  this.log.error({ error: error?.message ?? error, automationId: id }, "[AUTOMATIONS] The recurrence of an automation cannot be calculated, skipping it");
72
73
  continue;
73
74
  }
74
- const automation = await this.automationRepository.claim({ id, workerId, now, leaseMs, nextRunAt: nextRunAt ?? void 0 });
75
+ const automation = await this.automationRepository.claim({ id, workerId, now, leaseMs, nextRunAt: nextRunAt ?? void 0 }, import_runner_scope.runnerScope);
75
76
  if (!automation) continue;
76
77
  const pending = await (0, import_previous_run.findPausedRun)(this.automationRunRepository, this.syncRun, automation, now);
77
78
  if (pending) {
78
- const run2 = await (0, import_open_run.openRun)(this.automationRunRepository, { automation, trigger: "schedule", status: "skipped", workerId, now, scheduledFor, skipReason: "The previous run is still waiting for the user" });
79
- await this.automationRepository.release(id, workerId, { activeRunId: pending._id });
79
+ const run2 = await (0, import_open_run.openRun)(this.automationRunRepository, { scope: import_runner_scope.runnerScope, automation, trigger: "schedule", status: "skipped", workerId, now, scheduledFor, skipReason: "The previous run is still waiting for the user" });
80
+ await this.automationRepository.release(id, workerId, { activeRunId: pending._id }, import_runner_scope.runnerScope);
80
81
  skipped.push({ automation, run: run2.toJSON() });
81
82
  continue;
82
83
  }
83
- const run = await (0, import_open_run.openRun)(this.automationRunRepository, { automation, trigger: "schedule", status: "running", workerId, now, scheduledFor });
84
- await this.automationRepository.setActiveRun(id, workerId, String(run._id));
84
+ const run = await (0, import_open_run.openRun)(this.automationRunRepository, { scope: import_runner_scope.runnerScope, automation, trigger: "schedule", status: "running", workerId, now, scheduledFor });
85
+ await this.automationRepository.setActiveRun(id, workerId, String(run._id), import_runner_scope.runnerScope);
85
86
  claimed.push({ automation, run: run.toJSON() });
86
87
  }
87
88
  return { data: { claimed, skipped } };
@@ -11,6 +11,7 @@ import { openRun } from "../utils/open-run";
11
11
  import { leaseMs as defaultLeaseMs } from "../utils/settings";
12
12
  import { findPausedRun } from "../utils/previous-run";
13
13
  import { SyncAutomationRunUseCase } from "./sync-automation-run.use-case";
14
+ import { runnerScope } from "../utils/runner-scope";
14
15
  const DEFAULT_LIMIT = 10;
15
16
  let ClaimDueAutomationsUseCase = class extends BaseUseCase {
16
17
  constructor(automationRepository, automationRunRepository, syncRun) {
@@ -28,32 +29,32 @@ let ClaimDueAutomationsUseCase = class extends BaseUseCase {
28
29
  if (!workerId) throw new Error("The id of the worker is required");
29
30
  const now = params.now ?? /* @__PURE__ */ new Date();
30
31
  const leaseMs = params.leaseMs ?? defaultLeaseMs();
31
- const due = await this.automationRepository.findDue(now, params.limit ?? DEFAULT_LIMIT);
32
+ const due = await this.automationRepository.findDue(now, params.limit ?? DEFAULT_LIMIT, runnerScope);
32
33
  const claimed = [];
33
34
  const skipped = [];
34
35
  for (const candidate of due) {
35
36
  const id = String(candidate._id);
36
- const scheduledFor = candidate.nextRunAt ? new Date(candidate.nextRunAt) : void 0;
37
- const trigger = candidate.trigger;
38
- if (trigger?.type !== "schedule") continue;
37
+ const schedule = candidate.triggers?.schedule;
38
+ if (!schedule) continue;
39
+ const scheduledFor = schedule.nextRunAt ? new Date(schedule.nextRunAt) : void 0;
39
40
  let nextRunAt = null;
40
41
  try {
41
- nextRunAt = computeNextRun(trigger.recurrence, trigger.timezone, now);
42
+ nextRunAt = computeNextRun(schedule.recurrence, schedule.timezone, now);
42
43
  } catch (error) {
43
44
  this.log.error({ error: error?.message ?? error, automationId: id }, "[AUTOMATIONS] The recurrence of an automation cannot be calculated, skipping it");
44
45
  continue;
45
46
  }
46
- const automation = await this.automationRepository.claim({ id, workerId, now, leaseMs, nextRunAt: nextRunAt ?? void 0 });
47
+ const automation = await this.automationRepository.claim({ id, workerId, now, leaseMs, nextRunAt: nextRunAt ?? void 0 }, runnerScope);
47
48
  if (!automation) continue;
48
49
  const pending = await findPausedRun(this.automationRunRepository, this.syncRun, automation, now);
49
50
  if (pending) {
50
- const run2 = await openRun(this.automationRunRepository, { automation, trigger: "schedule", status: "skipped", workerId, now, scheduledFor, skipReason: "The previous run is still waiting for the user" });
51
- await this.automationRepository.release(id, workerId, { activeRunId: pending._id });
51
+ const run2 = await openRun(this.automationRunRepository, { scope: runnerScope, automation, trigger: "schedule", status: "skipped", workerId, now, scheduledFor, skipReason: "The previous run is still waiting for the user" });
52
+ await this.automationRepository.release(id, workerId, { activeRunId: pending._id }, runnerScope);
52
53
  skipped.push({ automation, run: run2.toJSON() });
53
54
  continue;
54
55
  }
55
- const run = await openRun(this.automationRunRepository, { automation, trigger: "schedule", status: "running", workerId, now, scheduledFor });
56
- await this.automationRepository.setActiveRun(id, workerId, String(run._id));
56
+ const run = await openRun(this.automationRunRepository, { scope: runnerScope, automation, trigger: "schedule", status: "running", workerId, now, scheduledFor });
57
+ await this.automationRepository.setActiveRun(id, workerId, String(run._id), runnerScope);
57
58
  claimed.push({ automation, run: run.toJSON() });
58
59
  }
59
60
  return { data: { claimed, skipped } };
@@ -37,6 +37,7 @@ var import_repositories = require("../repositories");
37
37
  var import_previous_run = require("../utils/previous-run");
38
38
  var import_settings = require("../utils/settings");
39
39
  var import_sync_automation_run = require("./sync-automation-run.use-case");
40
+ var import_runner_scope = require("../utils/runner-scope");
40
41
  const SCAN_LIMIT = 200;
41
42
  let ClaimQueuedAutomationRunsUseCase = class extends import_base.BaseUseCase {
42
43
  constructor(automationRepository, automationRunRepository, syncRun) {
@@ -54,7 +55,7 @@ let ClaimQueuedAutomationRunsUseCase = class extends import_base.BaseUseCase {
54
55
  const now = params.now ?? /* @__PURE__ */ new Date();
55
56
  const leaseMs = params.leaseMs ?? (0, import_settings.leaseMs)();
56
57
  const limit = params.limit ?? SCAN_LIMIT;
57
- const queued = await this.automationRunRepository.findByStatus("queued", SCAN_LIMIT);
58
+ const queued = await this.automationRunRepository.findByStatus("queued", SCAN_LIMIT, import_runner_scope.runnerScope);
58
59
  const claimed = [];
59
60
  const skipped = [];
60
61
  const seen = /* @__PURE__ */ new Set();
@@ -63,18 +64,18 @@ let ClaimQueuedAutomationRunsUseCase = class extends import_base.BaseUseCase {
63
64
  const automationId = String(run.automationId);
64
65
  if (seen.has(automationId)) continue;
65
66
  seen.add(automationId);
66
- const existing = await this.automationRepository.findById(automationId);
67
- const reason = !existing ? "The automation no longer exists" : !existing.enabled ? "The automation is turned off" : void 0;
67
+ const existing = await this.automationRepository.findById(automationId, import_runner_scope.runnerScope);
68
+ const reason = !existing ? "The automation no longer exists" : !existing.triggers?.event?.enabled ? "The event trigger of the automation is turned off" : void 0;
68
69
  if (reason) {
69
- const settled = await this.automationRunRepository.update(import_entities.AutomationRun.fromJSON({ ...run, status: "skipped", finishedAt: now, skipReason: reason }));
70
+ const settled = await this.automationRunRepository.update(import_entities.AutomationRun.fromJSON({ ...run, status: "skipped", finishedAt: now, skipReason: reason }), import_runner_scope.runnerScope);
70
71
  if (existing && settled) skipped.push({ automation: existing, run: settled });
71
72
  continue;
72
73
  }
73
- const automation = await this.automationRepository.claimManual({ id: automationId, workerId, now, leaseMs });
74
+ const automation = await this.automationRepository.claimManual({ id: automationId, workerId, now, leaseMs }, import_runner_scope.runnerScope);
74
75
  if (!automation) continue;
75
76
  const paused = await (0, import_previous_run.findPausedRun)(this.automationRunRepository, this.syncRun, automation, now);
76
77
  if (paused) {
77
- await this.automationRepository.release(automationId, workerId, { activeRunId: paused._id });
78
+ await this.automationRepository.release(automationId, workerId, { activeRunId: paused._id }, import_runner_scope.runnerScope);
78
79
  continue;
79
80
  }
80
81
  const started = await this.automationRunRepository.claimQueued(String(run._id), {
@@ -83,12 +84,12 @@ let ClaimQueuedAutomationRunsUseCase = class extends import_base.BaseUseCase {
83
84
  instructions: automation.instructions,
84
85
  modelProvider: automation.modelProvider,
85
86
  modelName: automation.modelName
86
- });
87
+ }, import_runner_scope.runnerScope);
87
88
  if (!started) {
88
- await this.automationRepository.release(automationId, workerId, {});
89
+ await this.automationRepository.release(automationId, workerId, {}, import_runner_scope.runnerScope);
89
90
  continue;
90
91
  }
91
- await this.automationRepository.setActiveRun(automationId, workerId, String(started._id));
92
+ await this.automationRepository.setActiveRun(automationId, workerId, String(started._id), import_runner_scope.runnerScope);
92
93
  claimed.push({ automation, run: started });
93
94
  }
94
95
  return { data: { claimed, skipped } };