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
@@ -1,6 +1,6 @@
1
1
  # Automations
2
2
 
3
- An automation is a task that your agent runs by itself, without anyone in the chat. You describe what it must do and what starts it, a schedule or an event that arrives from another system, and `openloops` runs it in [autonomous mode](agent.md#autonomous-mode), in a new chat each time, and keeps a history of every run.
3
+ An automation is a task that your agent runs by itself, without anyone in the chat. You describe what it must do and what starts it, a schedule, an event that arrives from another system or both, and `openloops` runs it in [autonomous mode](agent.md#autonomous-mode), in a new chat each time, and keeps a history of every run.
4
4
 
5
5
  ```typescript
6
6
  import {
@@ -29,10 +29,11 @@ const result = await createAutomation({
29
29
  payload: {
30
30
  name: 'Morning report',
31
31
  instructions: 'Summarize my open tickets and send the summary to my email',
32
- trigger: {
33
- type: 'schedule',
34
- recurrence: { type: 'weekly', days: [1, 2, 3, 4, 5], time: '08:00' },
35
- timezone: 'America/Lima',
32
+ triggers: {
33
+ schedule: {
34
+ recurrence: { type: 'weekly', days: [1, 2, 3, 4, 5], time: '08:00' },
35
+ timezone: 'America/New_York',
36
+ },
36
37
  },
37
38
  modelProvider: 'anthropic',
38
39
  modelName: 'claude-haiku-5-5',
@@ -40,27 +41,45 @@ const result = await createAutomation({
40
41
  currentUser,
41
42
  })
42
43
 
43
- result.data.nextRunAt
44
+ result.data.triggers.schedule.nextRunAt
44
45
  ```
45
46
 
46
47
  | Field | Required | What it is |
47
48
  | --- | --- | --- |
48
49
  | `name` | yes | A name to recognize the automation. |
49
50
  | `instructions` | yes | What the agent must do. It is sent to the agent as the message of the user, so write it as you would write it in a chat. If the task needs to notify someone, say so here and give the agent a tool for it. |
50
- | `trigger` | yes | What starts it. A `schedule` or an `event`. See below. |
51
- | `enabled` | no | `true` by default. An automation that is off never runs, and can be turned on again. |
51
+ | `triggers` | yes | What starts it: a `schedule`, an `event` or both, at least one. See below. |
52
52
  | `modelProvider`, `modelName` | no | The model of the automation, both or none. Without them, the default model of the owner is used, then the fallbacks. See [configuration](configuration.md#which-model-a-chat-uses). |
53
53
 
54
- `nextRunAt` is calculated when a scheduled automation is created and it is what you show to the user. An automation with an event trigger has no `nextRunAt`. The fields that the system manages (`nextRunAt`, `lastRunAt`, `lastRunStatus`, `lockedBy`, `lockedUntil`, `activeRunId`) are ignored if they come in the payload.
54
+ `triggers.schedule.nextRunAt` is calculated when the schedule is created and it is what you show to the user. An automation without a schedule has no `nextRunAt`. The fields that the system manages (`nextRunAt` inside the schedule, `eventTokenVersion`, `lastRunAt`, `lastRunStatus`, `lockedBy`, `lockedUntil`, `activeRunId`) are ignored if they come in the payload.
55
55
 
56
56
  ## Triggers
57
57
 
58
- An automation has one trigger. Its type is chosen when it is created and cannot be changed afterwards. To change it, create another automation.
58
+ An automation has at most one trigger of each type, so `triggers` has two optional keys and at least one of them must be present.
59
59
 
60
- | Type | Fields | Starts |
60
+ | Trigger | Fields | Starts |
61
61
  | --- | --- | --- |
62
- | `schedule` | `recurrence`, `timezone` | On the dates of the recurrence. |
63
- | `event` | `timezone` (optional) | When an external system sends an event to it. See [events](#events). |
62
+ | `schedule` | `enabled`, `recurrence`, `timezone` | On the dates of the recurrence. |
63
+ | `event` | `enabled` | When an external system sends an event to it. See [events](#events). |
64
+
65
+ ```typescript
66
+ await createAutomation({
67
+ payload: {
68
+ name: 'Ticket review',
69
+ instructions: 'Review the open tickets and report what needs attention',
70
+ triggers: {
71
+ schedule: { recurrence: { type: 'daily', time: '08:00' }, timezone: 'America/New_York' },
72
+ event: {},
73
+ },
74
+ },
75
+ currentUser,
76
+ })
77
+ ```
78
+
79
+ - **Every trigger has its own `enabled`**, `true` by default. There is no switch for the whole automation. A trigger that is off never starts a run and can be turned on again, and the other trigger keeps working.
80
+ - **The `timezone` belongs to the schedule.** It only decides when the schedule runs. The agent gets the timezone of the owner of the automation, whatever started the run.
81
+ - **`nextRunAt`** lives inside `triggers.schedule`, and only a schedule has it.
82
+ - **Both triggers share the queue and the lock of the automation**: its runs happen one at a time, whether they come from the schedule, from an event or from a [manual run](#running-by-hand).
64
83
 
65
84
  You can always start any automation by hand with [`runNow`](#running-by-hand), whatever its trigger.
66
85
 
@@ -84,7 +103,7 @@ The recurrence of a `schedule` trigger is stored as a structured value, so a scr
84
103
  ```typescript
85
104
  const { data } = await previewAutomationRuns({
86
105
  recurrence: { type: 'weekly', days: [1, 5], time: '08:00' },
87
- timezone: 'America/Lima',
106
+ timezone: 'America/New_York',
88
107
  count: 5,
89
108
  })
90
109
 
@@ -96,9 +115,13 @@ It returns the next dates (at most 20) without saving anything, so a screen can
96
115
  ## Managing automations
97
116
 
98
117
  ```typescript
99
- await updateAutomation({ id, payload: { enabled: false }, currentUser })
118
+ await updateAutomation({ id, payload: { triggers: { schedule: { enabled: false } } }, currentUser })
119
+
120
+ await updateAutomation({ id, payload: { triggers: { event: { enabled: true } } }, currentUser })
100
121
 
101
- await updateAutomation({ id, payload: { enabled: true }, currentUser })
122
+ await updateAutomation({ id, payload: { triggers: { event: {} } }, currentUser })
123
+
124
+ await updateAutomation({ id, payload: { triggers: { event: null } }, currentUser })
102
125
 
103
126
  const automation = await getAutomationById({ id, currentUser })
104
127
 
@@ -107,12 +130,15 @@ const automations = await listAutomationsByUser({ options: { page: 1, limit: 10
107
130
  await deleteAutomation({ id, currentUser })
108
131
  ```
109
132
 
110
- - **Turning an automation off** is how the user pauses it for a while. It has no next run until it is turned on again. Turning it on counts the next run from that moment, it does not catch up with what was missed.
111
- - **Changing the trigger of a schedule** (its recurrence or timezone, you can send only the one that changes) **or turning it on** calculates `nextRunAt` again from now.
112
- - **A run that is in progress is not interrupted** by turning the automation off or by deleting it.
133
+ - **The triggers are merged key by key.** Send only the trigger that changes, and inside it only the fields that change. What you do not send is kept. To pause the whole automation, turn off both triggers.
134
+ - **Turning the schedule off** is how the user pauses it for a while. It has no next run until it is turned on again. Turning it on counts the next run from that moment, it does not catch up with what was missed.
135
+ - **Changing the schedule** (its recurrence or timezone, you can send only the one that changes) **or turning it on** calculates `nextRunAt` again from now. Changing only the event does not move it.
136
+ - **Adding a trigger** is sending its key. A schedule that does not exist yet needs its `recurrence` and `timezone`.
137
+ - **Removing a trigger** is sending `null` in its key. The last trigger cannot be removed. Removing the event is not the same as turning it off. Turning it off keeps `eventTokenVersion`, so the tokens issued before work again when it is turned on. Removing it counts as a rotation: `eventTokenVersion` goes up by one, so no token issued before works, not even when the event is added again.
138
+ - **A run that is in progress is not interrupted** by turning a trigger off, removing it or deleting the automation.
113
139
  - **`__v`** in the payload works as in the other use cases: if the automation changed since it was read, the update fails with a `VersionConflictError`.
114
140
  - **Deleting** is a soft delete. An admin can delete for good with `hardDelete: true`.
115
- - **The system never turns an automation off by itself.** An automation that fails keeps trying on every occurrence, so watch the history.
141
+ - **The system never turns a trigger off by itself.** An automation that fails keeps trying on every occurrence, so watch the history.
116
142
 
117
143
  ## Events
118
144
 
@@ -123,7 +149,7 @@ const { data: automation } = await createAutomation({
123
149
  payload: {
124
150
  name: 'Cost anomaly analysis',
125
151
  instructions: 'Find the cause of the cost alert in AWS and send a summary to the channel #costs',
126
- trigger: { type: 'event' },
152
+ triggers: { event: {} },
127
153
  },
128
154
  currentUser,
129
155
  })
@@ -170,13 +196,13 @@ The use case answers without running the agent. The run is created as `queued` a
170
196
  4. If the automation already has `AUTOMATIONS_MAX_QUEUED_RUNS` runs waiting (10 by default), the event is recorded as a `skipped` run with its payload and the status `skipped`, and it is not run.
171
197
  5. Otherwise it saves a `queued` run and returns the status `queued`.
172
198
 
173
- All refusals of the token are an `AutomationEventError` with the status 401 and the same message, so the caller learns nothing. The `reason` (`invalid`, `expired`, `revoked`, `not_found`, `disabled`) is for your logs.
199
+ All refusals of the token are an `AutomationEventError` with the status 401 and the same message, so the caller learns nothing. The `reason` (`invalid`, `expired`, `revoked`, `not_found`, `disabled`) is for your logs. `disabled` means the event trigger is turned off.
174
200
 
175
201
  ### The queue
176
202
 
177
203
  - Events of one automation run **one at a time and in order**, like everything else in an automation. While one runs, the next ones wait as `queued`.
178
204
  - If the run in progress is `paused` waiting for the user, the queue waits too, until the user answers.
179
- - A queued event whose automation was turned off or deleted before it started is marked `skipped`, and `onRunFinished` is called for it when the automation still exists.
205
+ - A queued event whose event trigger was turned off or removed, or whose automation was deleted, before it started is marked `skipped`, and `onRunFinished` is called for it when the automation still exists.
180
206
  - A queued run keeps the instructions of the automation as they were when the event arrived, but starts with the instructions and the model it has when the run begins.
181
207
 
182
208
  ### What the agent receives
@@ -185,31 +211,41 @@ The instructions are the first message, as in any run. When the run comes from a
185
211
 
186
212
  ### The token
187
213
 
188
- The token is a JWT signed with `JWT_SECRET`. It is not stored: the owner or an admin can ask for it as many times as they want, and every one is different and valid.
214
+ The token is a JWT signed with `JWT_SECRET`. It is not stored: the owner can ask for it as many times as they want, and every one is different and valid.
189
215
 
190
216
  | Claim | Value |
191
217
  | --- | --- |
192
218
  | `aud` | `automation-event`. A session token of a user is refused, and this one does not log anybody in. |
193
219
  | `sub` | The id of the automation. It must be the one of the URL. |
194
- | `v` | The version of the tokens of the automation. |
220
+ | `v` | The `eventTokenVersion` of the automation when the token was issued. |
195
221
  | `jti` | A unique id, used to revoke it. |
196
222
  | `exp` | Only if you pass `expiresIn` when you issue it, such as `'30m'`, `'12h'`, `'30d'` or `'2w'`. Without it the token does not expire. |
197
223
 
224
+ The `eventTokenVersion` is a field of the automation, not of the event trigger, so it is not lost when the event is removed. It starts at 1, it only goes up, and it is the only thing that decides which tokens are valid as a group: a token is accepted only if its `v` is equal to the current `eventTokenVersion`, so every lower version is blocked. Three operations touch tokens, and only one of them changes the version:
225
+
226
+ | Operation | What it does | Changes `eventTokenVersion` | Effect on the tokens already issued |
227
+ | --- | --- | --- | --- |
228
+ | `issueAutomationEventToken({ id, expiresIn?, currentUser })` | Signs a new token with the current `eventTokenVersion` and a new `jti`. It does not store it and it does not write the automation. | No | None. Every token issued before keeps working, so you can have one per system that sends events. |
229
+ | `revokeAutomationEventToken({ id, token, currentUser })` | Saves the `jti` of the token you pass in `revoked_tokens`. | No | Only that token stops working. The rest are valid. |
230
+ | `rotateAutomationEventTokens({ id, currentUser })` | Adds 1 to `eventTokenVersion` and saves the automation. It does not return a token. | Yes | Every token issued before stops working, because its `v` is not the current version any more. Issue a new one afterwards. |
231
+
232
+ So issuing again does **not** replace the previous token. To "regenerate" a token and kill the old ones, rotate first and then issue. The three are only for the owner of the automation or an admin, and the event trigger must exist: without it they fail with an error.
233
+
198
234
  Four ways to take a token out of use:
199
235
 
200
236
  - **Let it expire,** if it was issued with `expiresIn`. A token that expires stops working without notice, and the refused events create no run, so watch the `expired` reasons in your logs.
201
237
  - **Revoke one token.** If a token leaked, pass it to `revokeAutomationEventToken({ id, token, currentUser })`. Its `jti` goes to the collection `revoked_tokens` and only that token stops working. The entry is deleted by itself when the token would have expired. The tokens are not stored, so there is no list to choose from: you need the leaked token in your hands.
202
238
  - **Rotate.** `rotateAutomationEventTokens({ id, currentUser })` increases the version and **every** token issued before stops working. Use it when you do not have the leaked token, or you want to start again. Issue the new token afterwards.
203
- - **Turn the automation off.** Its events are refused with the reason `disabled`.
239
+ - **Turn the event trigger off.** Its events are refused with the reason `disabled`, and the tokens work again when it is turned on. If you remove the event trigger instead, the tokens are refused with the reason `invalid` until an event is added again, see [managing automations](#managing-automations).
204
240
 
205
- Revoking and rotating, like issuing, are only for the owner of the automation or an admin. A revoked token is checked after its signature, so an invalid token never reaches the database.
241
+ A revoked token is checked after its signature, so an invalid token never reaches the database.
206
242
 
207
243
  ## The history
208
244
 
209
245
  Every execution is a run.
210
246
 
211
247
  ```typescript
212
- const byAutomation = await listAutomationRunsByAutomation({ automationId, options: { page: 1, limit: 20 } })
248
+ const byAutomation = await listAutomationRunsByAutomation({ automationId, options: { page: 1, limit: 20 }, currentUser })
213
249
 
214
250
  const byUser = await listAutomationRunsByUser({ status: 'failed', currentUser })
215
251
  ```
@@ -232,7 +268,7 @@ const byUser = await listAutomationRunsByUser({ status: 'failed', currentUser })
232
268
  | `completed` | The agent finished its task. |
233
269
  | `paused` | The agent needs the user: it asked for the approval of a tool or an MCP server, or asked a question. The chat is waiting. When the user answers in the chat and it finishes, the run becomes `completed` or `failed`. |
234
270
  | `failed` | It failed. `error` says why: the model did not answer in time, a guardrail blocked the instructions, the owner no longer exists, or the worker was lost. |
235
- | `skipped` | It was not started and `skipReason` says why: a scheduled run was due but the previous one was still `paused`, an event found the queue full, or the automation was turned off or deleted while the event waited. It is recorded so you can notify the user. |
271
+ | `skipped` | It was not started and `skipReason` says why: a scheduled run was due but the previous one was still `paused`, an event found the queue full, or the event trigger was turned off or removed, or the automation deleted, while the event waited. It is recorded so you can notify the user. |
236
272
 
237
273
  ## The runner
238
274
 
@@ -278,7 +314,7 @@ A new `Agent` is created for every run, so the options are the same but nothing
278
314
 
279
315
  - It runs as the owner of the automation, which must exist.
280
316
  - It starts a new chat, in autonomous mode, with the model of the automation, and the instructions as the first message. The chat is recorded in the run as soon as it exists.
281
- - It gives the agent the timezone of the automation, so the local time reaches the prompts.
317
+ - It gives the agent the timezone of the owner, taken from their preferences, so the local time reaches the prompts. The timezone of the schedule is not used for this.
282
318
  - Guards of the agent apply to the instructions. If one blocks them, the run is `failed`.
283
319
 
284
320
  ### Internal MCP servers in an automation
@@ -294,10 +330,10 @@ An automation runs without the session of its owner, so there is no token of the
294
330
  ### Running by hand
295
331
 
296
332
  ```typescript
297
- const { run } = await runner.runNow({ automationId })
333
+ const { run } = await runner.runNow({ automationId, currentUser })
298
334
  ```
299
335
 
300
- It starts the run at once and returns while it keeps running in the background. It does not change the next scheduled run. It is refused if the automation is already running. Any process that has a runner can call it, it does not need to be the one that called `start()`.
336
+ It starts the run at once, only if the automation is of `currentUser`, and returns while it keeps running in the background. It does not change the next scheduled run. It is refused if the automation is already running. Any process that has a runner can call it, it does not need to be the one that called `start()`.
301
337
 
302
338
  ### Several instances
303
339
 
@@ -318,7 +354,7 @@ Someone answers a paused run in the chat, outside the runner. The runner checks
318
354
 
319
355
  The collections are `automations` and `automation_runs`. `ensureIndexes` from `openloops/db` creates the indexes of the queries of the runner and of the lists (see [configuration](configuration.md#indexes)). They are:
320
356
 
321
- - `automations`: `{ enabled: 1, nextRunAt: 1 }` and `{ ownerId: 1, createdAt: -1 }`.
357
+ - `automations`: `{ 'triggers.schedule.enabled': 1, 'triggers.schedule.nextRunAt': 1 }` and `{ ownerId: 1, createdAt: -1 }`.
322
358
  - `automation_runs`: `{ automationId: 1, createdAt: -1 }`, `{ ownerId: 1, createdAt: -1 }`, `{ status: 1, createdAt: 1 }` and a unique index on `{ automationId: 1, idempotencyKey: 1 }` for the runs that have a key, which makes the deduplication of events safe when two requests arrive together.
323
359
  - `revoked_tokens`: a unique index on `jti` and a TTL index on `expiresAt`. It belongs to [authentication](authentication.md#revoked-tokens).
324
360
 
@@ -338,7 +374,7 @@ Before 0.4.0 this module was called `agentic-jobs`. The old names are not kept a
338
374
  | `AGENTIC_JOBS_POLL_INTERVAL_MS`, `AGENTIC_JOBS_LEASE_MS` | `AUTOMATIONS_POLL_INTERVAL_MS`, `AUTOMATIONS_LEASE_MS` |
339
375
 
340
376
  The old variables are **not read anymore and nothing warns you**: if you had set them, the runner now uses the default values until you rename them.
341
- 3. **Change the shape of what you send and read.** `recurrence` and `timezone` are no longer fields of the automation, they live inside `trigger`.
377
+ 3. **Change the shape of what you send and read.** `recurrence` and `timezone` are no longer fields of the automation, they live inside `triggers.schedule` (see also [Migrating to 0.5.0](#migrating-to-050)).
342
378
 
343
379
  Before:
344
380
 
@@ -348,7 +384,7 @@ Before 0.4.0 this module was called `agentic-jobs`. The old names are not kept a
348
384
  name: 'Morning report',
349
385
  instructions: 'Summarize my open tickets',
350
386
  recurrence: { type: 'daily', time: '08:00' },
351
- timezone: 'America/Lima',
387
+ timezone: 'America/New_York',
352
388
  },
353
389
  currentUser,
354
390
  })
@@ -361,18 +397,19 @@ Before 0.4.0 this module was called `agentic-jobs`. The old names are not kept a
361
397
  payload: {
362
398
  name: 'Morning report',
363
399
  instructions: 'Summarize my open tickets',
364
- trigger: {
365
- type: 'schedule',
366
- recurrence: { type: 'daily', time: '08:00' },
367
- timezone: 'America/Lima',
400
+ triggers: {
401
+ schedule: {
402
+ recurrence: { type: 'daily', time: '08:00' },
403
+ timezone: 'America/New_York',
404
+ },
368
405
  },
369
406
  },
370
407
  currentUser,
371
408
  })
372
409
  ```
373
410
 
374
- To change the recurrence or the timezone of an existing automation, send `trigger: { recurrence }` or `trigger: { timezone }`, only the one that changes. When you read an automation, its schedule is in `automation.trigger`.
375
- 4. **Migrate the data**, once per database. It renames the collections, moves `recurrence` and `timezone` into a `schedule` trigger, renames `jobId` to `automationId`, drops the old index and creates the new ones. It can be run again without changing anything, and it refuses to rename a collection if the new name already exists.
411
+ To change the recurrence or the timezone of an existing automation, send `triggers: { schedule: { recurrence } }` or `triggers: { schedule: { timezone } }`, only the one that changes. When you read an automation, its schedule is in `automation.triggers.schedule`.
412
+ 4. **Migrate the data**, once per database. It renames the collections, moves `recurrence` and `timezone` into the schedule trigger, renames `jobId` to `automationId`, drops the old index and creates the new ones. It can be run again without changing anything, and it refuses to rename a collection if the new name already exists.
376
413
 
377
414
  ```typescript
378
415
  import { migrateAutomations } from 'openloops/db'
@@ -383,6 +420,71 @@ Before 0.4.0 this module was called `agentic-jobs`. The old names are not kept a
383
420
  With `dryRun` it only counts what it would change, so run it first. In a checkout of this repository you can run `yarn migrate:automations --dry-run` and `yarn migrate:automations`, which read `OPENLOOPS_DB_URI` and `OPENLOOPS_DB_NAME`.
384
421
  5. **Start the runner of the new version.**
385
422
 
423
+ ## Migrating to 0.5.0
424
+
425
+ Version 0.5.0 lets an automation have a schedule and an event at the same time. It breaks the shape of the automation, and there are no aliases for the old one. Follow these steps in order.
426
+
427
+ 1. **Stop the runner of the old version**, so no run is in progress while the data changes.
428
+ 2. **Change what you send.** `trigger` is now `triggers`, an object with the optional keys `schedule` and `event`. `enabled` moved from the automation to each trigger, and `timezone` moved from the trigger to the schedule. The event trigger has no `timezone`.
429
+
430
+ Before:
431
+
432
+ ```typescript
433
+ await createAutomation({
434
+ payload: {
435
+ name: 'Morning report',
436
+ instructions: 'Summarize my open tickets',
437
+ enabled: true,
438
+ trigger: { type: 'schedule', recurrence: { type: 'daily', time: '08:00' }, timezone: 'America/New_York' },
439
+ },
440
+ currentUser,
441
+ })
442
+
443
+ await createAutomation({ payload: { name: 'Alarms', instructions: 'Analyze the alarm', trigger: { type: 'event' } }, currentUser })
444
+ ```
445
+
446
+ Now:
447
+
448
+ ```typescript
449
+ await createAutomation({
450
+ payload: {
451
+ name: 'Morning report',
452
+ instructions: 'Summarize my open tickets',
453
+ triggers: { schedule: { enabled: true, recurrence: { type: 'daily', time: '08:00' }, timezone: 'America/New_York' } },
454
+ },
455
+ currentUser,
456
+ })
457
+
458
+ await createAutomation({ payload: { name: 'Alarms', instructions: 'Analyze the alarm', triggers: { event: {} } }, currentUser })
459
+ ```
460
+
461
+ Sending `trigger` or `enabled` in the payload of `createAutomation` or `updateAutomation` fails with an error that says where the field went, so nothing is ignored silently.
462
+ 3. **Change what you read.**
463
+
464
+ | Before | Now |
465
+ | --- | --- |
466
+ | `automation.trigger.type` | the keys present in `automation.triggers` |
467
+ | `automation.enabled` | `automation.triggers.schedule?.enabled` and `automation.triggers.event?.enabled` |
468
+ | `automation.nextRunAt` | `automation.triggers.schedule?.nextRunAt` |
469
+ | `automation.trigger.recurrence`, `automation.trigger.timezone` | `automation.triggers.schedule.recurrence`, `automation.triggers.schedule.timezone` |
470
+ | `automation.trigger.tokenVersion` | `automation.eventTokenVersion` |
471
+
472
+ The `trigger` of a run (`schedule`, `event` or `manual`) does not change.
473
+ 4. **Know the changes of behavior.**
474
+ - The agent of every run now gets the timezone of the owner, not the one saved in the trigger. Before, a schedule passed its own timezone and an event trigger only had one if you had set it.
475
+ - Turning off an automation used to stop its schedule and its events. Now each trigger has its own switch, so turn off both to pause everything.
476
+ - The events that are queued when an event trigger is turned off are marked `skipped` with the reason `The event trigger of the automation is turned off`.
477
+ 5. **Migrate the data**, once per database, with the same `migrateAutomations` as before. It also moves `trigger` into `triggers`: the root `enabled` goes into the trigger that the automation had, `nextRunAt` goes into the schedule, `tokenVersion` goes to `eventTokenVersion` in the automation (1 when it had no event), the timezone of an event trigger is dropped, and the old index `enabled_1_nextRunAt_1` is replaced by the new one. It can be run again without changing anything, and it also migrates databases that still have the agentic jobs. Run it with `dryRun` first, and take a backup before the real run.
478
+
479
+ ```typescript
480
+ import { migrateAutomations } from 'openloops/db'
481
+
482
+ const report = await migrateAutomations(db, { dryRun: true })
483
+ ```
484
+
485
+ The report has a new `triggersMoved` count. In a checkout of this repository you can run `yarn migrate:automations --dry-run` and `yarn migrate:automations`.
486
+ 6. **Start the runner of the new version.** Until the data is migrated the runner finds no automation due, because it looks for the schedule inside `triggers`.
487
+
386
488
  ## Limits
387
489
 
388
490
  How many automations a user can have, how often they can run and how much they can spend is not decided here. That belongs to the module of usage limits.
@@ -19,16 +19,13 @@ This project uses the `openloops` package. Before writing code that uses it, rea
19
19
  The package has no controllers: the authorization of every endpoint is the responsibility of this backend.
20
20
  ```
21
21
 
22
- ## The rule that matters most: authorization is yours
22
+ ## The rule that matters most: always pass the user
23
23
 
24
- The use cases trust the `currentUser` you pass and the ids you give them.
24
+ The use cases check who owns every record, in the repository, with the `currentUser` you pass. Read [authorization](authorization.md) once.
25
25
 
26
- - Functions that list by user (`listChatsByUser`, `listMcpServersByUser`, `listAutomationsByUser`, `listAutomationRunsByUser`) only return records of that user.
27
- - Functions that take an **id** (`getChatById`, `updateChat`, `deleteChat`, `getMcpServerById`, `updateMcpServer`, `deleteMcpServer`, `decideMcpServerTrust`, `getAutomationById`, `updateAutomation`, `deleteAutomation`, `listAutomationRunsByAutomation`, `runner.runNow`) **do not check who owns the record**. The use cases that issue, revoke or rotate the tokens of an automation do check that the user is the owner or an admin.
28
-
29
- So, before calling any of the second kind, load the record and check that `ownerId` is the user of the request. Do it in one place, a middleware or a helper, and never pass an id from a request straight to a use case.
30
-
31
- Also note that `getChatById` returns `{}` in `data` when the chat does not exist, so check for `data._id`.
26
+ - A record that is not the user's is answered as if it did not exist: a 404 `HttpError` from the functions that update or delete, `{}` in `data` from `getChatById` (so check for `data._id`), and `null` from the other getters.
27
+ - A call without `currentUser` fails with 401. Your endpoint is still the one that authenticates the request and builds the `currentUser` from the token.
28
+ - An administrator acts on the records of other users only with `scope: Scope.asAdmin(currentUser)`, and a worker or a webhook with `Scope.system('reason')`. Never build a scope from a value that comes from a request.
32
29
 
33
30
  ## Which endpoint calls what
34
31
 
@@ -46,7 +43,7 @@ Also note that `getChatById` returns `{}` in `data` when the chat does not exist
46
43
  | Receive an event from another system | `receiveAutomationEvent`, public endpoint, authenticated only by the token of the automation |
47
44
  | Tokens of an event automation | `issueAutomationEventToken`, `revokeAutomationEventToken`, `rotateAutomationEventTokens` |
48
45
  | History of runs | `listAutomationRunsByAutomation`, `listAutomationRunsByUser` |
49
- | "Run now" | `runner.runNow({ automationId })` |
46
+ | "Run now" | `runner.runNow({ automationId, currentUser })` |
50
47
  | Sign in, users | `login`, `createUser`, `getUserById`, `updateUser`, `deleteUser` |
51
48
 
52
49
  ## Running the agent for a message
@@ -123,7 +120,8 @@ runner.start()
123
120
 
124
121
  ## Security checklist
125
122
 
126
- - Check ownership before every call that takes an id.
123
+ - Pass the `currentUser` of the request to every call, and build it from the verified token.
124
+ - Never take a `scope` from the body or the query of a request.
127
125
  - Mask `headers` and `history` of MCP servers.
128
126
  - Add input guards for what your product must never receive, such as card numbers.
129
127
  - Mark your own destructive tools with `destructive: true`. The agent asks for confirmation before running them. Do not rely on a tool being treated as destructive by default, because it is not.
package/docs/llms.txt CHANGED
@@ -7,6 +7,7 @@ Start with the integration guide, then read only the pages you need. All pages a
7
7
  ## Start here
8
8
 
9
9
  - [Integration guide](integration-guide.md): what to write in a backend, which endpoint calls what, and the security checklist
10
+ - [Authorization](authorization.md): the scope that every use case uses to check who owns a record, and how to migrate from 0.5
10
11
  - [README](../README.md): what it is, the quick start and the package structure
11
12
 
12
13
  ## Run an agent
@@ -152,11 +152,11 @@ const agent = new Agent({
152
152
  loop: new AgiLoop(),
153
153
  internalMcps: [
154
154
  {
155
- name: 'sofi',
156
- url: 'https://api.sofi.cash/mcp',
155
+ name: 'budget',
156
+ url: 'https://api.example.com/mcp',
157
157
  headers: {
158
158
  Authorization: 'Bearer {{token}}',
159
- 'X-App': 'sofi-agent',
159
+ 'X-App': 'budget-agent',
160
160
  },
161
161
  },
162
162
  ],
@@ -167,7 +167,7 @@ agent.addInternalMcp({ name: 'other', url: 'https://other.example.com/mcp' })
167
167
  await agent.run({
168
168
  input: { message: 'how much did I spend on food this month?' },
169
169
  currentUser,
170
- mcpTokens: { sofi: sessionToken },
170
+ mcpTokens: { budget: sessionToken },
171
171
  })
172
172
  ```
173
173
 
package/docs/users.md CHANGED
@@ -30,17 +30,17 @@ const result = await createUser({
30
30
  password: 'a-real-password',
31
31
  role: UserRole.USER,
32
32
  },
33
- sanitize: false,
33
+ scope: Scope.system('sign up'),
34
34
  })
35
35
  ```
36
36
 
37
37
  `email` and `password` are required. Passwords are hashed before being stored and are never returned.
38
38
 
39
- ### `sanitize`
39
+ ### The scope
40
40
 
41
- By default `createUser` removes the fields only admins can set, such as `role`, unless the `currentUser` is an admin. This is what protects you from someone escalating privileges through the payload.
41
+ When a user creates the account, `createUser` removes the fields only a trusted scope can set, such as `role`. This is what protects you from someone escalating privileges through the payload.
42
42
 
43
- If you send `role`, pass `sanitize: false`, otherwise it is discarded. Do it only when your code builds the payload itself, with the fields you choose. Never pass the raw body of a request from an API.
43
+ If you send `role`, pass a trusted scope, otherwise it is discarded. Do it only when your code builds the payload itself, with the fields you choose, and never with the raw body of a request. See [authorization](authorization.md).
44
44
 
45
45
  ## Getting a user
46
46
 
@@ -60,6 +60,8 @@ const result = await ListUsersByProps({
60
60
  })
61
61
  ```
62
62
 
63
+ `ListUsersByProps` is for admins. The `currentUser` must be an admin, or you pass your own `scope`, because it reads the users of other people.
64
+
63
65
  ## Updating a user
64
66
 
65
67
  ```typescript
@@ -10,10 +10,11 @@ async function createSchedule () {
10
10
  payload: {
11
11
  name: 'Morning report',
12
12
  instructions: 'Summarize the news about the bitcoin price and keep the summary short',
13
- trigger: {
14
- type: 'schedule',
15
- recurrence: { type: 'weekly', days: [1, 2, 3, 4, 5], time: '08:00' },
16
- timezone: 'America/Lima',
13
+ triggers: {
14
+ schedule: {
15
+ recurrence: { type: 'weekly', days: [1, 2, 3, 4, 5], time: '08:00' },
16
+ timezone: 'America/New_York',
17
+ },
17
18
  },
18
19
  modelProvider: 'anthropic',
19
20
  modelName: 'claude-haiku-5-5',
@@ -21,7 +22,7 @@ async function createSchedule () {
21
22
  currentUser,
22
23
  })
23
24
 
24
- logger.info({ id: result.data._id, nextRunAt: result.data.nextRunAt }, '[Demo] Created automation')
25
+ logger.info({ id: result.data._id, nextRunAt: result.data.triggers?.schedule?.nextRunAt }, '[Demo] Created automation')
25
26
  }
26
27
 
27
28
  createSchedule()
@@ -10,7 +10,7 @@ async function createEventAutomation () {
10
10
  payload: {
11
11
  name: 'Cost anomaly analysis',
12
12
  instructions: 'Find the cause of the cost alert in AWS and send a short summary to the channel #costs',
13
- trigger: { type: 'event' },
13
+ triggers: { event: {} },
14
14
  },
15
15
  currentUser,
16
16
  })
@@ -4,10 +4,12 @@ import { listAutomationRunsByAutomation, listAutomationRunsByUser } from 'openlo
4
4
 
5
5
  async function listRunsOfAnAutomation () {
6
6
  const logger = new Logger()
7
+ const currentUser = await CurrentUser.asyncFromDB('6ab56894eb20887d1ef7942c')
7
8
 
8
9
  const result = await listAutomationRunsByAutomation({
9
10
  automationId: '6ad0c9a6d3ea4199c9b7ba10',
10
11
  options: { page: 1, limit: 20 },
12
+ currentUser,
11
13
  })
12
14
 
13
15
  logger.info(result.data.map(run => ({ status: run.status, startedAt: run.startedAt, chatId: run.chatId, error: run.error })), '[Demo] Runs of the automation')
@@ -16,8 +16,9 @@ async function listAutomations () {
16
16
 
17
17
  logger.info(result.data.map(automation => ({
18
18
  name: automation.name,
19
- enabled: automation.enabled,
20
- nextRunAt: automation.nextRunAt,
19
+ scheduleEnabled: automation.triggers?.schedule?.enabled,
20
+ eventEnabled: automation.triggers?.event?.enabled,
21
+ nextRunAt: automation.triggers?.schedule?.nextRunAt,
21
22
  lastRunStatus: automation.lastRunStatus,
22
23
  })), '[Demo] Automations')
23
24
  }
@@ -6,7 +6,7 @@ async function previewRuns () {
6
6
 
7
7
  const result = await previewAutomationRuns({
8
8
  recurrence: { type: 'monthly', day: 'last', time: '18:00' },
9
- timezone: 'America/Lima',
9
+ timezone: 'America/New_York',
10
10
  count: 5,
11
11
  })
12
12
 
@@ -17,7 +17,7 @@ async function rotateAll () {
17
17
 
18
18
  const result = await rotateAutomationEventTokens({ id: '6ad0c9a6d3ea4199c9b7ba10', currentUser })
19
19
 
20
- logger.info({ trigger: result.data.trigger }, '[Demo] Every token issued before stopped working, issue a new one')
20
+ logger.info({ eventTokenVersion: result.data.eventTokenVersion }, '[Demo] Every token issued before stopped working, issue a new one')
21
21
  }
22
22
 
23
23
  revokeLeakedToken()
@@ -1,15 +1,17 @@
1
1
  import { Logger } from 'openloops/common'
2
+ import { CurrentUser } from 'openloops/base'
2
3
  import { createAutomationRunner } from 'openloops/automations'
3
4
  import { webSearchTool } from 'openloops/tools'
4
5
 
5
6
  async function runNow () {
6
7
  const logger = new Logger()
8
+ const currentUser = await CurrentUser.asyncFromDB('6ab56894eb20887d1ef7942c')
7
9
 
8
10
  const runner = createAutomationRunner({
9
11
  agent: { tools: [webSearchTool] },
10
12
  })
11
13
 
12
- const { run } = await runner.runNow({ automationId: '6ad0c9a6d3ea4199c9b7ba10' })
14
+ const { run } = await runner.runNow({ automationId: '6ad0c9a6d3ea4199c9b7ba10', currentUser })
13
15
 
14
16
  logger.info({ runId: run._id, status: run.status }, '[Demo] The run started in the background')
15
17
 
@@ -8,11 +8,11 @@ async function pause () {
8
8
 
9
9
  const result = await updateAutomation({
10
10
  id: '6ad0c9a6d3ea4199c9b7ba10',
11
- payload: { enabled: false },
11
+ payload: { triggers: { schedule: { enabled: false } } },
12
12
  currentUser,
13
13
  })
14
14
 
15
- logger.info({ enabled: result.data.enabled }, '[Demo] The automation is off')
15
+ logger.info({ enabled: result.data.triggers?.schedule?.enabled }, '[Demo] The automation is off')
16
16
  }
17
17
 
18
18
  async function resume () {
@@ -22,13 +22,12 @@ async function resume () {
22
22
  const result = await updateAutomation({
23
23
  id: '6ad0c9a6d3ea4199c9b7ba10',
24
24
  payload: {
25
- enabled: true,
26
- trigger: { recurrence: { type: 'daily', time: '07:30' } },
25
+ triggers: { schedule: { enabled: true, recurrence: { type: 'daily', time: '07:30' } } },
27
26
  },
28
27
  currentUser,
29
28
  })
30
29
 
31
- logger.info({ nextRunAt: result.data.nextRunAt }, '[Demo] The automation is on again')
30
+ logger.info({ nextRunAt: result.data.triggers?.schedule?.nextRunAt }, '[Demo] The automation is on again')
32
31
  }
33
32
 
34
33
  pause()
@@ -20,7 +20,7 @@ async function hardDeleteChat () {
20
20
 
21
21
  const result = await deleteChat({
22
22
  id: '6abc454162624f9e01929cd8',
23
- hardDelete: true,
23
+ hardDelete: true, // only admins can perform this action
24
24
  currentUser,
25
25
  })
26
26
 
@@ -13,8 +13,8 @@ async function runAgentWithInternalMcp (sessionToken: string) {
13
13
  loop: new AgiLoop(),
14
14
  internalMcps: [
15
15
  {
16
- name: 'sofi',
17
- url: 'https://api.sofi.cash/mcp',
16
+ name: 'budget',
17
+ url: 'https://api.example.com/mcp',
18
18
  headers: { Authorization: 'Bearer {{token}}' },
19
19
  },
20
20
  ],
@@ -26,7 +26,7 @@ async function runAgentWithInternalMcp (sessionToken: string) {
26
26
  message: 'how much did I spend on food this month?',
27
27
  },
28
28
  currentUser,
29
- mcpTokens: { sofi: sessionToken },
29
+ mcpTokens: { budget: sessionToken },
30
30
  })
31
31
 
32
32
  logger.debug(result, '[Demo] Result')