openloops 0.4.5 → 0.6.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (250) hide show
  1. package/CHANGELOG.md +52 -0
  2. package/README.md +2 -1
  3. package/dist/common/base/base.entity.d.ts +3 -9
  4. package/dist/common/base/base.entity.js +17 -96
  5. package/dist/common/base/base.entity.mjs +19 -92
  6. package/dist/common/base/base.use-case.d.ts +2 -0
  7. package/dist/common/base/index.d.ts +1 -0
  8. package/dist/common/base/index.js +3 -1
  9. package/dist/common/base/index.mjs +1 -0
  10. package/dist/common/base/share.d.ts +12 -0
  11. package/dist/common/base/share.js +45 -0
  12. package/dist/common/base/share.mjs +21 -0
  13. package/dist/common/decorators/index.d.ts +0 -2
  14. package/dist/common/decorators/index.js +1 -5
  15. package/dist/common/decorators/index.mjs +0 -2
  16. package/dist/common/repositories/index.d.ts +1 -0
  17. package/dist/common/repositories/index.js +3 -1
  18. package/dist/common/repositories/index.mjs +1 -0
  19. package/dist/common/repositories/mongo-db.repository.d.ts +31 -11
  20. package/dist/common/repositories/mongo-db.repository.js +121 -40
  21. package/dist/common/repositories/mongo-db.repository.mjs +121 -40
  22. package/dist/common/repositories/scope.d.ts +19 -0
  23. package/dist/common/repositories/scope.js +63 -0
  24. package/dist/common/repositories/scope.mjs +40 -0
  25. package/dist/db/indexes/automations.js +1 -1
  26. package/dist/db/indexes/automations.mjs +1 -1
  27. package/dist/db/migrations/automations.d.ts +1 -0
  28. package/dist/db/migrations/automations.js +32 -5
  29. package/dist/db/migrations/automations.mjs +32 -5
  30. package/dist/services/authentication/repositories/revoked-token.repository.d.ts +3 -3
  31. package/dist/services/authentication/repositories/revoked-token.repository.js +7 -6
  32. package/dist/services/authentication/repositories/revoked-token.repository.mjs +7 -6
  33. package/dist/services/authentication/use-cases/is-token-revoked.use-case.js +2 -1
  34. package/dist/services/authentication/use-cases/is-token-revoked.use-case.mjs +2 -1
  35. package/dist/services/authentication/use-cases/login.use-case.js +2 -1
  36. package/dist/services/authentication/use-cases/login.use-case.mjs +2 -1
  37. package/dist/services/authentication/use-cases/revoke-token.use-case.js +5 -4
  38. package/dist/services/authentication/use-cases/revoke-token.use-case.mjs +5 -4
  39. package/dist/services/automations/entities/automation.entity.d.ts +9 -9
  40. package/dist/services/automations/entities/automation.entity.js +21 -20
  41. package/dist/services/automations/entities/automation.entity.mjs +21 -20
  42. package/dist/services/automations/entities/trigger.d.ts +16 -8
  43. package/dist/services/automations/entities/trigger.js +28 -17
  44. package/dist/services/automations/entities/trigger.mjs +27 -16
  45. package/dist/services/automations/repositories/automation-run.repository.d.ts +10 -10
  46. package/dist/services/automations/repositories/automation-run.repository.js +21 -18
  47. package/dist/services/automations/repositories/automation-run.repository.mjs +21 -18
  48. package/dist/services/automations/repositories/automation.repository.d.ts +14 -13
  49. package/dist/services/automations/repositories/automation.repository.js +34 -22
  50. package/dist/services/automations/repositories/automation.repository.mjs +34 -22
  51. package/dist/services/automations/runner/automation-runner.d.ts +3 -0
  52. package/dist/services/automations/runner/automation-runner.js +2 -2
  53. package/dist/services/automations/runner/automation-runner.mjs +2 -2
  54. package/dist/services/automations/use-cases/attach-automation-run-chat.use-case.js +3 -2
  55. package/dist/services/automations/use-cases/attach-automation-run-chat.use-case.mjs +3 -2
  56. package/dist/services/automations/use-cases/claim-due-automations.use-case.js +11 -10
  57. package/dist/services/automations/use-cases/claim-due-automations.use-case.mjs +11 -10
  58. package/dist/services/automations/use-cases/claim-queued-automation-runs.use-case.js +10 -9
  59. package/dist/services/automations/use-cases/claim-queued-automation-runs.use-case.mjs +10 -9
  60. package/dist/services/automations/use-cases/create-automation.use-case.d.ts +2 -2
  61. package/dist/services/automations/use-cases/create-automation.use-case.js +8 -8
  62. package/dist/services/automations/use-cases/create-automation.use-case.mjs +6 -6
  63. package/dist/services/automations/use-cases/delete-automation.use-case.js +6 -9
  64. package/dist/services/automations/use-cases/delete-automation.use-case.mjs +4 -7
  65. package/dist/services/automations/use-cases/finish-automation-run.use-case.js +4 -3
  66. package/dist/services/automations/use-cases/finish-automation-run.use-case.mjs +4 -3
  67. package/dist/services/automations/use-cases/get-automation-by-id.use-case.js +5 -4
  68. package/dist/services/automations/use-cases/get-automation-by-id.use-case.mjs +3 -2
  69. package/dist/services/automations/use-cases/issue-automation-event-token.use-case.js +7 -7
  70. package/dist/services/automations/use-cases/issue-automation-event-token.use-case.mjs +5 -5
  71. package/dist/services/automations/use-cases/list-automation-runs-by-automation.use-case.js +3 -1
  72. package/dist/services/automations/use-cases/list-automation-runs-by-automation.use-case.mjs +3 -1
  73. package/dist/services/automations/use-cases/list-automation-runs-by-user.use-case.js +2 -0
  74. package/dist/services/automations/use-cases/list-automation-runs-by-user.use-case.mjs +2 -0
  75. package/dist/services/automations/use-cases/list-automations-by-user.use-case.js +2 -0
  76. package/dist/services/automations/use-cases/list-automations-by-user.use-case.mjs +2 -0
  77. package/dist/services/automations/use-cases/receive-automation-event.use-case.js +10 -8
  78. package/dist/services/automations/use-cases/receive-automation-event.use-case.mjs +10 -8
  79. package/dist/services/automations/use-cases/renew-automation-lease.use-case.js +2 -1
  80. package/dist/services/automations/use-cases/renew-automation-lease.use-case.mjs +2 -1
  81. package/dist/services/automations/use-cases/revoke-automation-event-token.use-case.js +5 -5
  82. package/dist/services/automations/use-cases/revoke-automation-event-token.use-case.mjs +3 -3
  83. package/dist/services/automations/use-cases/rotate-automation-event-tokens.use-case.js +10 -10
  84. package/dist/services/automations/use-cases/rotate-automation-event-tokens.use-case.mjs +8 -8
  85. package/dist/services/automations/use-cases/start-automation-run.use-case.js +8 -6
  86. package/dist/services/automations/use-cases/start-automation-run.use-case.mjs +8 -6
  87. package/dist/services/automations/use-cases/sync-automation-run.use-case.js +6 -5
  88. package/dist/services/automations/use-cases/sync-automation-run.use-case.mjs +6 -5
  89. package/dist/services/automations/use-cases/sync-automation-runs.use-case.js +3 -2
  90. package/dist/services/automations/use-cases/sync-automation-runs.use-case.mjs +3 -2
  91. package/dist/services/automations/use-cases/update-automation.use-case.d.ts +2 -2
  92. package/dist/services/automations/use-cases/update-automation.use-case.js +16 -12
  93. package/dist/services/automations/use-cases/update-automation.use-case.mjs +14 -10
  94. package/dist/services/automations/utils/automation-fields.d.ts +3 -4
  95. package/dist/services/automations/utils/automation-fields.js +3 -5
  96. package/dist/services/automations/utils/automation-fields.mjs +4 -6
  97. package/dist/services/automations/utils/open-run.d.ts +2 -0
  98. package/dist/services/automations/utils/open-run.js +2 -2
  99. package/dist/services/automations/utils/open-run.mjs +2 -2
  100. package/dist/services/automations/utils/previous-run.js +3 -2
  101. package/dist/services/automations/utils/previous-run.mjs +3 -2
  102. package/dist/services/automations/utils/runner-scope.d.ts +3 -0
  103. package/dist/{common/decorators/admin-only/index.js → services/automations/utils/runner-scope.js} +15 -5
  104. package/dist/services/automations/utils/runner-scope.mjs +8 -0
  105. package/dist/services/chats/repositories/chat.repository.d.ts +7 -6
  106. package/dist/services/chats/repositories/chat.repository.js +16 -11
  107. package/dist/services/chats/repositories/chat.repository.mjs +16 -11
  108. package/dist/services/chats/use-cases/create-chat.use-case.js +4 -5
  109. package/dist/services/chats/use-cases/create-chat.use-case.mjs +2 -3
  110. package/dist/services/chats/use-cases/delete-chat.use-case.js +7 -9
  111. package/dist/services/chats/use-cases/delete-chat.use-case.mjs +5 -7
  112. package/dist/services/chats/use-cases/get-chat-by-id.use-case.js +3 -2
  113. package/dist/services/chats/use-cases/get-chat-by-id.use-case.mjs +3 -2
  114. package/dist/services/chats/use-cases/interrupt-execution.use-case.js +5 -7
  115. package/dist/services/chats/use-cases/interrupt-execution.use-case.mjs +3 -5
  116. package/dist/services/chats/use-cases/list-chats-by-props.use-case.js +3 -2
  117. package/dist/services/chats/use-cases/list-chats-by-props.use-case.mjs +3 -2
  118. package/dist/services/chats/use-cases/list-chats-by-user.use-case.js +2 -0
  119. package/dist/services/chats/use-cases/list-chats-by-user.use-case.mjs +2 -0
  120. package/dist/services/chats/use-cases/update-chat.use-case.js +5 -7
  121. package/dist/services/chats/use-cases/update-chat.use-case.mjs +3 -5
  122. package/dist/services/llmcalls/repositories/llmcall.repository.d.ts +7 -6
  123. package/dist/services/llmcalls/repositories/llmcall.repository.js +16 -11
  124. package/dist/services/llmcalls/repositories/llmcall.repository.mjs +16 -11
  125. package/dist/services/llmcalls/use-cases/create-llmcall.use-case.js +4 -5
  126. package/dist/services/llmcalls/use-cases/create-llmcall.use-case.mjs +2 -3
  127. package/dist/services/llmcalls/use-cases/delete-llmcall.use-case.js +6 -8
  128. package/dist/services/llmcalls/use-cases/delete-llmcall.use-case.mjs +4 -6
  129. package/dist/services/llmcalls/use-cases/get-llm-usage-by-chat.use-case.js +4 -3
  130. package/dist/services/llmcalls/use-cases/get-llm-usage-by-chat.use-case.mjs +2 -1
  131. package/dist/services/llmcalls/use-cases/get-llm-usage-by-message.use-case.js +4 -3
  132. package/dist/services/llmcalls/use-cases/get-llm-usage-by-message.use-case.mjs +2 -1
  133. package/dist/services/llmcalls/use-cases/get-llm-usage-stats.use-case.js +2 -1
  134. package/dist/services/llmcalls/use-cases/get-llm-usage-stats.use-case.mjs +2 -1
  135. package/dist/services/llmcalls/use-cases/get-llmcall-by-id.use-case.js +6 -4
  136. package/dist/services/llmcalls/use-cases/get-llmcall-by-id.use-case.mjs +4 -2
  137. package/dist/services/llmcalls/use-cases/list-llmcalls-by-props.use-case.js +3 -2
  138. package/dist/services/llmcalls/use-cases/list-llmcalls-by-props.use-case.mjs +3 -2
  139. package/dist/services/llmcalls/use-cases/update-llmcall.use-case.js +5 -7
  140. package/dist/services/llmcalls/use-cases/update-llmcall.use-case.mjs +3 -5
  141. package/dist/services/mcp-servers/repositories/mcpserver.repository.d.ts +8 -7
  142. package/dist/services/mcp-servers/repositories/mcpserver.repository.js +18 -12
  143. package/dist/services/mcp-servers/repositories/mcpserver.repository.mjs +18 -12
  144. package/dist/services/mcp-servers/use-cases/create-mcpserver.use-case.js +4 -5
  145. package/dist/services/mcp-servers/use-cases/create-mcpserver.use-case.mjs +2 -3
  146. package/dist/services/mcp-servers/use-cases/decide-mcpserver-trust.use-case.js +8 -9
  147. package/dist/services/mcp-servers/use-cases/decide-mcpserver-trust.use-case.mjs +6 -7
  148. package/dist/services/mcp-servers/use-cases/delete-mcpserver.use-case.js +6 -8
  149. package/dist/services/mcp-servers/use-cases/delete-mcpserver.use-case.mjs +4 -6
  150. package/dist/services/mcp-servers/use-cases/get-mcpserver-by-id.use-case.js +5 -4
  151. package/dist/services/mcp-servers/use-cases/get-mcpserver-by-id.use-case.mjs +3 -2
  152. package/dist/services/mcp-servers/use-cases/list-all-mcpservers-by-user.use-case.js +4 -3
  153. package/dist/services/mcp-servers/use-cases/list-all-mcpservers-by-user.use-case.mjs +2 -1
  154. package/dist/services/mcp-servers/use-cases/update-mcpserver.use-case.js +8 -11
  155. package/dist/services/mcp-servers/use-cases/update-mcpserver.use-case.mjs +6 -9
  156. package/dist/services/mcp-servers/use-cases/verify-mcpserver-connection.use-case.js +9 -9
  157. package/dist/services/mcp-servers/use-cases/verify-mcpserver-connection.use-case.mjs +7 -7
  158. package/dist/services/tasks/repositories/agent-action.repository.d.ts +4 -4
  159. package/dist/services/tasks/repositories/agent-action.repository.js +9 -7
  160. package/dist/services/tasks/repositories/agent-action.repository.mjs +9 -7
  161. package/dist/services/tasks/repositories/agent-task.repository.d.ts +5 -5
  162. package/dist/services/tasks/repositories/agent-task.repository.js +11 -9
  163. package/dist/services/tasks/repositories/agent-task.repository.mjs +11 -9
  164. package/dist/services/tasks/use-cases/create-agent-action.use-case.js +4 -5
  165. package/dist/services/tasks/use-cases/create-agent-action.use-case.mjs +2 -3
  166. package/dist/services/tasks/use-cases/create-agent-task.use-case.js +4 -5
  167. package/dist/services/tasks/use-cases/create-agent-task.use-case.mjs +2 -3
  168. package/dist/services/tasks/use-cases/get-agent-task-by-id.use-case.js +5 -4
  169. package/dist/services/tasks/use-cases/get-agent-task-by-id.use-case.mjs +3 -2
  170. package/dist/services/tasks/use-cases/list-agent-actions-by-task.use-case.js +3 -1
  171. package/dist/services/tasks/use-cases/list-agent-actions-by-task.use-case.mjs +3 -1
  172. package/dist/services/tasks/use-cases/list-agent-tasks-by-chat.use-case.js +3 -1
  173. package/dist/services/tasks/use-cases/list-agent-tasks-by-chat.use-case.mjs +3 -1
  174. package/dist/services/tasks/use-cases/update-agent-action.use-case.js +5 -7
  175. package/dist/services/tasks/use-cases/update-agent-action.use-case.mjs +3 -5
  176. package/dist/services/tasks/use-cases/update-agent-task.use-case.js +5 -7
  177. package/dist/services/tasks/use-cases/update-agent-task.use-case.mjs +3 -5
  178. package/dist/services/traces/repositories/agent-trace.repository.d.ts +3 -3
  179. package/dist/services/traces/repositories/agent-trace.repository.js +6 -5
  180. package/dist/services/traces/repositories/agent-trace.repository.mjs +6 -5
  181. package/dist/services/traces/use-cases/create-agent-traces.use-case.js +5 -9
  182. package/dist/services/traces/use-cases/create-agent-traces.use-case.mjs +3 -7
  183. package/dist/services/traces/use-cases/list-agent-traces-by-chat.use-case.js +3 -1
  184. package/dist/services/traces/use-cases/list-agent-traces-by-chat.use-case.mjs +3 -1
  185. package/dist/services/users/entities/user-acquisition.js +3 -16
  186. package/dist/services/users/entities/user-acquisition.mjs +4 -11
  187. package/dist/services/users/entities/user-marketing.js +3 -16
  188. package/dist/services/users/entities/user-marketing.mjs +4 -11
  189. package/dist/services/users/entities/user.entity.js +0 -3
  190. package/dist/services/users/entities/user.entity.mjs +1 -4
  191. package/dist/services/users/repositories/user.repository.d.ts +8 -7
  192. package/dist/services/users/repositories/user.repository.js +26 -12
  193. package/dist/services/users/repositories/user.repository.mjs +26 -12
  194. package/dist/services/users/use-cases/create-user.use-case.d.ts +0 -1
  195. package/dist/services/users/use-cases/create-user.use-case.js +5 -5
  196. package/dist/services/users/use-cases/create-user.use-case.mjs +3 -3
  197. package/dist/services/users/use-cases/delete-user.use-case.js +7 -9
  198. package/dist/services/users/use-cases/delete-user.use-case.mjs +5 -7
  199. package/dist/services/users/use-cases/get-user-by-id.use-case.js +6 -4
  200. package/dist/services/users/use-cases/get-user-by-id.use-case.mjs +4 -2
  201. package/dist/services/users/use-cases/list-users-by-props.use-case.js +3 -2
  202. package/dist/services/users/use-cases/list-users-by-props.use-case.mjs +3 -2
  203. package/dist/services/users/use-cases/update-user.use-case.js +5 -7
  204. package/dist/services/users/use-cases/update-user.use-case.mjs +3 -5
  205. package/dist/services/workspaces/repositories/workspace-item.repository.d.ts +8 -8
  206. package/dist/services/workspaces/repositories/workspace-item.repository.js +17 -14
  207. package/dist/services/workspaces/repositories/workspace-item.repository.mjs +17 -14
  208. package/dist/services/workspaces/use-cases/delete-workspace-item.use-case.js +6 -3
  209. package/dist/services/workspaces/use-cases/delete-workspace-item.use-case.mjs +4 -1
  210. package/dist/services/workspaces/use-cases/get-workspace-item.use-case.js +4 -3
  211. package/dist/services/workspaces/use-cases/get-workspace-item.use-case.mjs +2 -1
  212. package/dist/services/workspaces/use-cases/list-workspace-items.use-case.js +2 -0
  213. package/dist/services/workspaces/use-cases/list-workspace-items.use-case.mjs +2 -0
  214. package/dist/services/workspaces/use-cases/save-workspace-item.use-case.js +8 -8
  215. package/dist/services/workspaces/use-cases/save-workspace-item.use-case.mjs +6 -6
  216. package/dist/services/workspaces/use-cases/search-workspace-items.use-case.js +2 -0
  217. package/dist/services/workspaces/use-cases/search-workspace-items.use-case.mjs +2 -0
  218. package/docs/README.md +3 -2
  219. package/docs/agent.md +2 -2
  220. package/docs/authorization.md +77 -0
  221. package/docs/automations.md +143 -41
  222. package/docs/integration-guide.md +8 -10
  223. package/docs/llms.txt +1 -0
  224. package/docs/mcp-servers.md +4 -4
  225. package/docs/users.md +6 -4
  226. package/examples/automations/create-automation.ts +6 -5
  227. package/examples/automations/create-event-automation.ts +1 -1
  228. package/examples/automations/list-automation-runs.ts +2 -0
  229. package/examples/automations/list-automations.ts +3 -2
  230. package/examples/automations/preview-automation-runs.ts +1 -1
  231. package/examples/automations/revoke-automation-event-token.ts +1 -1
  232. package/examples/automations/run-automation-now.ts +3 -1
  233. package/examples/automations/update-automation.ts +4 -5
  234. package/examples/chats/delete-chat.ts +1 -1
  235. package/examples/mcps/internal-mcp.ts +3 -3
  236. package/examples/run-agent/run-agent-with-identity.ts +1 -1
  237. package/examples/users/create-user.ts +2 -1
  238. package/examples/users/list-users.ts +1 -0
  239. package/package.json +1 -1
  240. package/dist/common/decorators/admin-only/admin-only.d.ts +0 -2
  241. package/dist/common/decorators/admin-only/admin-only.js +0 -52
  242. package/dist/common/decorators/admin-only/admin-only.mjs +0 -28
  243. package/dist/common/decorators/admin-only/index.d.ts +0 -1
  244. package/dist/common/decorators/admin-only/index.mjs +0 -1
  245. package/dist/common/decorators/owner-only/index.d.ts +0 -1
  246. package/dist/common/decorators/owner-only/index.js +0 -22
  247. package/dist/common/decorators/owner-only/index.mjs +0 -1
  248. package/dist/common/decorators/owner-only/owner-only.d.ts +0 -2
  249. package/dist/common/decorators/owner-only/owner-only.js +0 -52
  250. package/dist/common/decorators/owner-only/owner-only.mjs +0 -28
@@ -32,8 +32,9 @@ __export(get_workspace_item_use_case_exports, {
32
32
  module.exports = __toCommonJS(get_workspace_item_use_case_exports);
33
33
  var import_tsyringe = require("tsyringe");
34
34
  var import_base = require("../../../common/base/base.use-case.js");
35
+ var import_repositories = require("../../../common/repositories/index.js");
35
36
  var import_base2 = require("../../../common/base/base.entity.js");
36
- var import_repositories = require("../repositories");
37
+ var import_repositories2 = require("../repositories");
37
38
  let GetWorkspaceItemUseCase = class extends import_base.BaseUseCase {
38
39
  constructor(workspaceItemRepository) {
39
40
  super();
@@ -51,7 +52,7 @@ let GetWorkspaceItemUseCase = class extends import_base.BaseUseCase {
51
52
  { ownerId, chatId: import_base2.BaseEntity.toObjectId(chatId), taskId: void 0, name }
52
53
  ];
53
54
  for (const lookup of lookups) {
54
- const data = await this.workspaceItemRepository?.findByName(lookup, select);
55
+ const data = await this.workspaceItemRepository?.findByName(lookup, params.scope ?? import_repositories.Scope.canView(currentUser), select);
55
56
  if (data) return { data };
56
57
  }
57
58
  return { data: null };
@@ -59,7 +60,7 @@ let GetWorkspaceItemUseCase = class extends import_base.BaseUseCase {
59
60
  };
60
61
  GetWorkspaceItemUseCase = __decorateClass([
61
62
  (0, import_tsyringe.autoInjectable)(),
62
- __decorateParam(0, (0, import_tsyringe.inject)(import_repositories.WorkspaceItemRepository))
63
+ __decorateParam(0, (0, import_tsyringe.inject)(import_repositories2.WorkspaceItemRepository))
63
64
  ], GetWorkspaceItemUseCase);
64
65
  // Annotate the CommonJS export names for ESM import in node:
65
66
  0 && (module.exports = {
@@ -4,6 +4,7 @@ import {
4
4
  } from "../../../chunk-CF3WOMB3.mjs";
5
5
  import { autoInjectable, inject } from "tsyringe";
6
6
  import { BaseUseCase } from "../../../common/base/base.use-case.js";
7
+ import { Scope } from "../../../common/repositories/index.js";
7
8
  import { BaseEntity } from "../../../common/base/base.entity.js";
8
9
  import { WorkspaceItemRepository } from "../repositories";
9
10
  let GetWorkspaceItemUseCase = class extends BaseUseCase {
@@ -23,7 +24,7 @@ let GetWorkspaceItemUseCase = class extends BaseUseCase {
23
24
  { ownerId, chatId: BaseEntity.toObjectId(chatId), taskId: void 0, name }
24
25
  ];
25
26
  for (const lookup of lookups) {
26
- const data = await this.workspaceItemRepository?.findByName(lookup, select);
27
+ const data = await this.workspaceItemRepository?.findByName(lookup, params.scope ?? Scope.canView(currentUser), select);
27
28
  if (data) return { data };
28
29
  }
29
30
  return { data: null };
@@ -31,6 +31,7 @@ __export(list_workspace_items_use_case_exports, {
31
31
  });
32
32
  module.exports = __toCommonJS(list_workspace_items_use_case_exports);
33
33
  var import_tsyringe = require("tsyringe");
34
+ var import_repositories = require("../../../common/repositories/index.js");
34
35
  var import_base = require("../../../common/base/base.use-case.js");
35
36
  var import_base2 = require("../../../common/base/base.entity.js");
36
37
  var import_repositories2 = require("../repositories");
@@ -53,6 +54,7 @@ let ListWorkspaceItemsUseCase = class extends import_base.BaseUseCase {
53
54
  };
54
55
  const result = await this.workspaceItemRepository?.findManyPaginated(
55
56
  where,
57
+ params.scope ?? import_repositories.Scope.canView(currentUser),
56
58
  { content: 0 },
57
59
  { limit: DEFAULT_LIMIT, sortBy: { _id: -1 }, ...options }
58
60
  );
@@ -3,6 +3,7 @@ import {
3
3
  __decorateParam
4
4
  } from "../../../chunk-CF3WOMB3.mjs";
5
5
  import { autoInjectable, inject } from "tsyringe";
6
+ import { Scope } from "../../../common/repositories/index.js";
6
7
  import { BaseUseCase } from "../../../common/base/base.use-case.js";
7
8
  import { BaseEntity } from "../../../common/base/base.entity.js";
8
9
  import { WorkspaceItemRepository } from "../repositories";
@@ -25,6 +26,7 @@ let ListWorkspaceItemsUseCase = class extends BaseUseCase {
25
26
  };
26
27
  const result = await this.workspaceItemRepository?.findManyPaginated(
27
28
  where,
29
+ params.scope ?? Scope.canView(currentUser),
28
30
  { content: 0 },
29
31
  { limit: DEFAULT_LIMIT, sortBy: { _id: -1 }, ...options }
30
32
  );
@@ -32,8 +32,9 @@ __export(save_workspace_item_use_case_exports, {
32
32
  module.exports = __toCommonJS(save_workspace_item_use_case_exports);
33
33
  var import_tsyringe = require("tsyringe");
34
34
  var import_base = require("../../../common/base/base.use-case.js");
35
+ var import_repositories = require("../../../common/repositories/index.js");
35
36
  var import_base2 = require("../../../common/base/base.entity.js");
36
- var import_repositories = require("../repositories");
37
+ var import_repositories2 = require("../repositories");
37
38
  var import_entities = require("../entities");
38
39
  let SaveWorkspaceItemUseCase = class extends import_base.BaseUseCase {
39
40
  constructor(workspaceItemRepository) {
@@ -49,26 +50,25 @@ let SaveWorkspaceItemUseCase = class extends import_base.BaseUseCase {
49
50
  const ownerId = import_base2.BaseEntity.toObjectId(currentUser?.userId);
50
51
  const item = import_entities.WorkspaceItem.fromJSON({ ...payload });
51
52
  item.refreshSize();
53
+ const scope = params.scope ?? import_repositories.Scope.canEdit(currentUser);
52
54
  const existing = await this.workspaceItemRepository?.findByName(
53
55
  { ownerId, chatId: item.chatId, taskId: item.taskId, name: item.name },
56
+ scope,
54
57
  { _id: 1 }
55
58
  );
56
59
  if (existing?._id) {
57
60
  item.setId(existing._id);
58
- item.setUpdatedBy(currentUser);
59
- const result = await this.workspaceItemRepository?.update(item);
60
- if (!result) throw new Error("Workspace item update failed");
61
+ const result = await this.workspaceItemRepository?.update(item, scope);
62
+ if (!result) return this.throwHttpError({ statusCode: 404, errorMessage: "{{resource_not_found}}" });
61
63
  return { data: result };
62
64
  }
63
- item.setCreatedBy(currentUser);
64
- item.setOwner(currentUser);
65
- const created = await this.workspaceItemRepository?.create(item);
65
+ const created = await this.workspaceItemRepository?.create(item, scope);
66
66
  return { data: created.toJSON() };
67
67
  }
68
68
  };
69
69
  SaveWorkspaceItemUseCase = __decorateClass([
70
70
  (0, import_tsyringe.autoInjectable)(),
71
- __decorateParam(0, (0, import_tsyringe.inject)(import_repositories.WorkspaceItemRepository))
71
+ __decorateParam(0, (0, import_tsyringe.inject)(import_repositories2.WorkspaceItemRepository))
72
72
  ], SaveWorkspaceItemUseCase);
73
73
  // Annotate the CommonJS export names for ESM import in node:
74
74
  0 && (module.exports = {
@@ -4,6 +4,7 @@ import {
4
4
  } from "../../../chunk-CF3WOMB3.mjs";
5
5
  import { autoInjectable, inject } from "tsyringe";
6
6
  import { BaseUseCase } from "../../../common/base/base.use-case.js";
7
+ import { Scope } from "../../../common/repositories/index.js";
7
8
  import { BaseEntity } from "../../../common/base/base.entity.js";
8
9
  import { WorkspaceItemRepository } from "../repositories";
9
10
  import { WorkspaceItem } from "../entities";
@@ -21,20 +22,19 @@ let SaveWorkspaceItemUseCase = class extends BaseUseCase {
21
22
  const ownerId = BaseEntity.toObjectId(currentUser?.userId);
22
23
  const item = WorkspaceItem.fromJSON({ ...payload });
23
24
  item.refreshSize();
25
+ const scope = params.scope ?? Scope.canEdit(currentUser);
24
26
  const existing = await this.workspaceItemRepository?.findByName(
25
27
  { ownerId, chatId: item.chatId, taskId: item.taskId, name: item.name },
28
+ scope,
26
29
  { _id: 1 }
27
30
  );
28
31
  if (existing?._id) {
29
32
  item.setId(existing._id);
30
- item.setUpdatedBy(currentUser);
31
- const result = await this.workspaceItemRepository?.update(item);
32
- if (!result) throw new Error("Workspace item update failed");
33
+ const result = await this.workspaceItemRepository?.update(item, scope);
34
+ if (!result) return this.throwHttpError({ statusCode: 404, errorMessage: "{{resource_not_found}}" });
33
35
  return { data: result };
34
36
  }
35
- item.setCreatedBy(currentUser);
36
- item.setOwner(currentUser);
37
- const created = await this.workspaceItemRepository?.create(item);
37
+ const created = await this.workspaceItemRepository?.create(item, scope);
38
38
  return { data: created.toJSON() };
39
39
  }
40
40
  };
@@ -31,6 +31,7 @@ __export(search_workspace_items_use_case_exports, {
31
31
  });
32
32
  module.exports = __toCommonJS(search_workspace_items_use_case_exports);
33
33
  var import_tsyringe = require("tsyringe");
34
+ var import_repositories = require("../../../common/repositories/index.js");
34
35
  var import_helpers = require("../../../common/helpers/index.js");
35
36
  var import_base = require("../../../common/base/base.use-case.js");
36
37
  var import_base2 = require("../../../common/base/base.entity.js");
@@ -57,6 +58,7 @@ let SearchWorkspaceItemsUseCase = class extends import_base.BaseUseCase {
57
58
  };
58
59
  const result = await this.workspaceItemRepository?.findManyPaginated(
59
60
  where,
61
+ params.scope ?? import_repositories.Scope.canView(currentUser),
60
62
  { content: 0 },
61
63
  { limit: DEFAULT_LIMIT, sortBy: { _id: -1 }, ...options }
62
64
  );
@@ -3,6 +3,7 @@ import {
3
3
  __decorateParam
4
4
  } from "../../../chunk-CF3WOMB3.mjs";
5
5
  import { autoInjectable, inject } from "tsyringe";
6
+ import { Scope } from "../../../common/repositories/index.js";
6
7
  import { escapeRegex } from "../../../common/helpers/index.js";
7
8
  import { BaseUseCase } from "../../../common/base/base.use-case.js";
8
9
  import { BaseEntity } from "../../../common/base/base.entity.js";
@@ -29,6 +30,7 @@ let SearchWorkspaceItemsUseCase = class extends BaseUseCase {
29
30
  };
30
31
  const result = await this.workspaceItemRepository?.findManyPaginated(
31
32
  where,
33
+ params.scope ?? Scope.canView(currentUser),
32
34
  { content: 0 },
33
35
  { limit: DEFAULT_LIMIT, sortBy: { _id: -1 }, ...options }
34
36
  );
package/docs/README.md CHANGED
@@ -6,7 +6,8 @@ Start with the [README](../README.md) for what `openloops` is and how to run it.
6
6
 
7
7
  | Page | What it covers |
8
8
  | --- | --- |
9
- | [Integration guide](integration-guide.md) | What to write in a backend: which endpoint calls what, the authorization you own and a security checklist. |
9
+ | [Integration guide](integration-guide.md) | What to write in a backend: which endpoint calls what, what you still own and a security checklist. |
10
+ | [Authorization](authorization.md) | How the use cases check who owns a record, the scopes and how to migrate from 0.5. |
10
11
 
11
12
  ## Run an agent
12
13
 
@@ -14,7 +15,7 @@ Start with the [README](../README.md) for what `openloops` is and how to run it.
14
15
  | --- | --- |
15
16
  | [The `Agent`](agent.md) | Options, running a turn, the result and its statuses, confirmations, autonomous mode, hooks and interruption. |
16
17
  | [Configuration](configuration.md) | Environment variables, choosing the model and what is recorded. |
17
- | [Automations](automations.md) | Tasks that the agent runs by itself, on a schedule or when an event arrives: triggers, tokens, the queue, the history of runs and the runner that executes them. If you used `agentic-jobs`, read [the migration steps](automations.md#migrating-from-agentic-jobs). |
18
+ | [Automations](automations.md) | Tasks that the agent runs by itself, on a schedule, when an event arrives or both: triggers, tokens, the queue, the history of runs and the runner that executes them. If you used `agentic-jobs`, read [the migration steps](automations.md#migrating-from-agentic-jobs). Updating to 0.5.0 breaks the shape of the triggers, read [the migration to 0.5.0](automations.md#migrating-to-050). |
18
19
  | [Chats](chats.md) | The chat record, its messages and their `source`, listing, reading and deleting chats. |
19
20
 
20
21
  ## Shape how the agent works
package/docs/agent.md CHANGED
@@ -15,7 +15,7 @@ const agent = new Agent({
15
15
  tools: [],
16
16
  guards: [],
17
17
  identity: {
18
- name: 'Sofi',
18
+ name: 'Ava',
19
19
  gender: 'Female',
20
20
  role: 'Personal assistant and daily companion',
21
21
  },
@@ -199,7 +199,7 @@ await agent.run({ input: { chatId, message }, currentUser })
199
199
  stop()
200
200
  ```
201
201
 
202
- The listener receives every change of the activity of the run while it is in progress. It gets no start or end marker: the run is over when `agent.run()` resolves. This is the only place where the real activity is available: since 0.3.0 it is not saved with the chat, and `state.currentActivity` is deprecated (see [chats](chats.md#statecurrentactivity-is-deprecated)). It does not receive a value that repeats the previous one. With `AgiLoop` the value is a short sentence written by the model in the language of the conversation, such as `Buscando tus gastos de vuelos de agosto`, shown from the answer that asked for the actions until the next answer. When the model gives none, the listener hears nothing and the client keeps what it shows. See [loops](loops.md#agiloop).
202
+ The listener receives every change of the activity of the run while it is in progress. It gets no start or end marker: the run is over when `agent.run()` resolves. This is the only place where the real activity is available: since 0.3.0 it is not saved with the chat, and `state.currentActivity` is deprecated (see [chats](chats.md#statecurrentactivity-is-deprecated)). It does not receive a value that repeats the previous one. With `AgiLoop` the value is a short sentence written by the model in the language of the conversation, such as `Searching your flight expenses for August`, shown from the answer that asked for the actions until the next answer. When the model gives none, the listener hears nothing and the client keeps what it shows. See [loops](loops.md#agiloop).
203
203
 
204
204
  - The listener belongs to that `Agent` instance and to the chat of its current run. Nothing is shared between instances, so concurrent runs in the same process, even of different users, never hear each other. As always, create one `Agent` per run and do not share it between concurrent requests.
205
205
  - It is called synchronously from the loop, so keep it fast and do not await inside it. An error thrown by a listener is logged and ignored, the run goes on and the other listeners still run.
@@ -0,0 +1,77 @@
1
+ # Authorization
2
+
3
+ > **Breaking change.** Since this version every call that creates, reads or writes a record needs the `currentUser`, or a `scope` you build yourself. The calls that worked without one, or that created a record for the user in the payload, now fail with 401. Read [what changes when you create records](#creating-records) and the [migration steps](#migrating-from-05).
4
+
5
+ Every use case that reads or writes a record of a user checks who owns it. The check is not in a controller, it is in the repository, inside the same query that reads or writes the record, so it does not cost an extra read and cannot be forgotten.
6
+
7
+ ## The scope
8
+
9
+ A `Scope` tells the repository on whose behalf it works. Use cases build one from the `currentUser` you pass:
10
+
11
+ | Scope | Used for | Reaches |
12
+ | --- | --- | --- |
13
+ | `Scope.canView(currentUser)` | Reading. | The records of the user. |
14
+ | `Scope.canEdit(currentUser)` | Creating and updating. | The records of the user. |
15
+ | `Scope.canManage(currentUser)` | Deleting, issuing and revoking tokens. | The records of the user. |
16
+ | `Scope.asAdmin(currentUser)` | Actions of an administrator. | Every record. It fails with 403 if the user is not an admin. |
17
+ | `Scope.system('reason')` | Workers, webhooks and flows without a user. | Every record. The reason is required. |
18
+
19
+ An admin does not get more access by being an admin. A use case called with an admin as `currentUser` still works on the records of that admin. To work on any record, build the scope yourself and pass it:
20
+
21
+ ```typescript
22
+ import { Scope } from 'openloops/base'
23
+
24
+ await deleteChat({ id, currentUser, scope: Scope.asAdmin(currentUser) })
25
+ ```
26
+
27
+ Every use case accepts `scope`. Use it only from code you wrote, never from a value that comes from a request.
28
+
29
+ ## Creating records
30
+
31
+ Every `create...` use case (`createChat`, `createMcpServer`, `createAutomation`, `createUser`, `createLLMCall`, `createAgentTask`, `createAgentAction`, `createAgentTraces`, `saveWorkspaceItem`) now takes the owner and the author from the scope, not from the payload:
32
+
33
+ - With a user scope, the new record belongs to the `currentUser`. `ownerId`, `createdBy`, `createdAt` and the other fields of control are set by the repository, and the ones you put in the payload are discarded. You cannot create a record for another user by sending his `ownerId`.
34
+ - To create a record for someone else, or without a user (a sign up, a webhook, a worker), pass a trusted scope: `Scope.system('reason')` or `Scope.asAdmin(currentUser)`. The payload is kept as you send it, and `ownerId` is the one you send. With `system` there is no `createdBy`, unless you send it.
35
+ - Without `currentUser` and without `scope` the call fails with 401. Before, `createChat` and the others created the record without an owner.
36
+ - The record that returns has its `_id`, `ownerId`, `createdAt` and `createdBy`.
37
+
38
+ ```typescript
39
+ await createChat({ payload, currentUser })
40
+
41
+ await createUser({ payload, scope: Scope.system('sign up') })
42
+
43
+ await createAutomation({ payload: { ...payload, ownerId: otherUserId }, scope: Scope.asAdmin(currentUser) })
44
+ ```
45
+
46
+ ## What changes in your endpoints
47
+
48
+ - A record that is not the user's is answered as if it did not exist. The functions that update or delete throw a 404 `HttpError` with the message `{{resource_not_found}}`, `getChatById` returns `{}` in `data`, and the other getters return `null` or throw the same 404. Nobody can learn that an id exists by getting a 403.
49
+ - A call without `currentUser` and without `scope` fails with 401, it does not read everything.
50
+ - You do not need to load the record to check `ownerId` before calling a function that takes an id.
51
+
52
+ ## What the repository does with the fields of control
53
+
54
+ When a user writes, the repository removes `ownerId`, `sharedWith`, `createdAt`, `createdBy`, `updatedAt`, `updatedBy`, `deletedAt`, `deletedBy`, `archivedAt`, `archivedBy` and `__v` from the payload, and fills them in itself with the user of the scope. In the users collection it also removes `role`, `marketing.drawNumber` and `acquisition.referrerCode`. A `system` or `admin` scope keeps the payload as it is, because the code that uses it is trusted.
55
+
56
+ So `createUser` no longer has `sanitize`. Pass the scope that says who is creating the account:
57
+
58
+ ```typescript
59
+ await createUser({ payload, scope: Scope.system('sign up') })
60
+ ```
61
+
62
+ ## Sharing
63
+
64
+ `sharedWith` is a list of `{ userId, permission }` where `permission` is `read`, `write` or `manage`. Nothing reads it yet: while no one is shared, the three user scopes only reach the records of the owner.
65
+
66
+ ## Migrating from 0.5
67
+
68
+ - Pass `currentUser`, or a `scope`, to every function that reads or writes the records of a user: the ones that take an id (`getChatById`, `updateChat`, `deleteChat`, `getMcpServerById`, `updateMcpServer`, `deleteMcpServer`, `decideMcpServerTrust`, `verifyMcpServerConnection`, `getUserById`, `updateUser`, `deleteUser`, `updateAutomation`, `deleteAutomation`), the ones that create (see [creating records](#creating-records)), the ones that list and the usage reports (`getLLMUsageByChat`, `getLLMUsageByMessage` and `getLLMUsageStats`). Many of them accepted a call without a user before and did not check the owner. `getAutomationById`, `listAutomationRunsByAutomation`, `startAutomationRun` and `runner.runNow` did not read the user at all.
69
+ - You do not need to pass anything to `login`, `revokeToken`, `isTokenRevoked`, `previewAutomationRuns` and `receiveAutomationEvent`, which do not belong to a user, nor to the functions of the runner (`claimDueAutomations`, `claimQueuedAutomationRuns`, `finishAutomationRun`, `syncAutomationRun`, `syncAutomationRuns`, `attachAutomationRunChat` and `renewAutomationLease`), which use their own system scope.
70
+ - Pass `currentUser` to `getAutomationById`, `listAutomationRunsByAutomation`, `startAutomationRun` and `runner.runNow`. They did not need it before.
71
+ - Replace `sanitize: false` in `createUser` with a `scope`, as shown above.
72
+ - Where an admin used to act on the record of another user, pass `scope: Scope.asAdmin(currentUser)`.
73
+ - `issueAutomationEventToken`, `revokeAutomationEventToken` and `rotateAutomationEventTokens` are for the owner. They used to accept an admin.
74
+ - Expect a 404 where you got a 403 for the records of other users, and remove the ownership checks you wrote before calling a function with an id.
75
+ - Hard delete is for `Scope.asAdmin` or `Scope.system` only.
76
+ - `BaseEntity` no longer has `setOwner`, `setCreatedBy`, `setUpdatedBy`, `setDeletedBy` or `setArchivedBy`, and the `AdminOnly` and `OwnerOnly` decorators are gone. The repository does that work.
77
+ - `sharedWith` is now a list of `{ userId, permission }` and not a list of ids.