openloops 0.5.0 → 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 (224) hide show
  1. package/CHANGELOG.md +27 -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/services/authentication/repositories/revoked-token.repository.d.ts +3 -3
  26. package/dist/services/authentication/repositories/revoked-token.repository.js +7 -6
  27. package/dist/services/authentication/repositories/revoked-token.repository.mjs +7 -6
  28. package/dist/services/authentication/use-cases/is-token-revoked.use-case.js +2 -1
  29. package/dist/services/authentication/use-cases/is-token-revoked.use-case.mjs +2 -1
  30. package/dist/services/authentication/use-cases/login.use-case.js +2 -1
  31. package/dist/services/authentication/use-cases/login.use-case.mjs +2 -1
  32. package/dist/services/authentication/use-cases/revoke-token.use-case.js +5 -4
  33. package/dist/services/authentication/use-cases/revoke-token.use-case.mjs +5 -4
  34. package/dist/services/automations/repositories/automation-run.repository.d.ts +10 -10
  35. package/dist/services/automations/repositories/automation-run.repository.js +21 -18
  36. package/dist/services/automations/repositories/automation-run.repository.mjs +21 -18
  37. package/dist/services/automations/repositories/automation.repository.d.ts +14 -13
  38. package/dist/services/automations/repositories/automation.repository.js +30 -18
  39. package/dist/services/automations/repositories/automation.repository.mjs +30 -18
  40. package/dist/services/automations/runner/automation-runner.d.ts +3 -0
  41. package/dist/services/automations/runner/automation-runner.js +1 -1
  42. package/dist/services/automations/runner/automation-runner.mjs +1 -1
  43. package/dist/services/automations/use-cases/attach-automation-run-chat.use-case.js +3 -2
  44. package/dist/services/automations/use-cases/attach-automation-run-chat.use-case.mjs +3 -2
  45. package/dist/services/automations/use-cases/claim-due-automations.use-case.js +7 -6
  46. package/dist/services/automations/use-cases/claim-due-automations.use-case.mjs +7 -6
  47. package/dist/services/automations/use-cases/claim-queued-automation-runs.use-case.js +9 -8
  48. package/dist/services/automations/use-cases/claim-queued-automation-runs.use-case.mjs +9 -8
  49. package/dist/services/automations/use-cases/create-automation.use-case.js +4 -5
  50. package/dist/services/automations/use-cases/create-automation.use-case.mjs +2 -3
  51. package/dist/services/automations/use-cases/delete-automation.use-case.js +6 -9
  52. package/dist/services/automations/use-cases/delete-automation.use-case.mjs +4 -7
  53. package/dist/services/automations/use-cases/finish-automation-run.use-case.js +4 -3
  54. package/dist/services/automations/use-cases/finish-automation-run.use-case.mjs +4 -3
  55. package/dist/services/automations/use-cases/get-automation-by-id.use-case.js +5 -4
  56. package/dist/services/automations/use-cases/get-automation-by-id.use-case.mjs +3 -2
  57. package/dist/services/automations/use-cases/issue-automation-event-token.use-case.js +5 -5
  58. package/dist/services/automations/use-cases/issue-automation-event-token.use-case.mjs +3 -3
  59. package/dist/services/automations/use-cases/list-automation-runs-by-automation.use-case.js +3 -1
  60. package/dist/services/automations/use-cases/list-automation-runs-by-automation.use-case.mjs +3 -1
  61. package/dist/services/automations/use-cases/list-automation-runs-by-user.use-case.js +2 -0
  62. package/dist/services/automations/use-cases/list-automation-runs-by-user.use-case.mjs +2 -0
  63. package/dist/services/automations/use-cases/list-automations-by-user.use-case.js +2 -0
  64. package/dist/services/automations/use-cases/list-automations-by-user.use-case.mjs +2 -0
  65. package/dist/services/automations/use-cases/receive-automation-event.use-case.js +7 -6
  66. package/dist/services/automations/use-cases/receive-automation-event.use-case.mjs +7 -6
  67. package/dist/services/automations/use-cases/renew-automation-lease.use-case.js +2 -1
  68. package/dist/services/automations/use-cases/renew-automation-lease.use-case.mjs +2 -1
  69. package/dist/services/automations/use-cases/revoke-automation-event-token.use-case.js +5 -5
  70. package/dist/services/automations/use-cases/revoke-automation-event-token.use-case.mjs +3 -3
  71. package/dist/services/automations/use-cases/rotate-automation-event-tokens.use-case.js +8 -8
  72. package/dist/services/automations/use-cases/rotate-automation-event-tokens.use-case.mjs +6 -6
  73. package/dist/services/automations/use-cases/start-automation-run.use-case.js +8 -6
  74. package/dist/services/automations/use-cases/start-automation-run.use-case.mjs +8 -6
  75. package/dist/services/automations/use-cases/sync-automation-run.use-case.js +6 -5
  76. package/dist/services/automations/use-cases/sync-automation-run.use-case.mjs +6 -5
  77. package/dist/services/automations/use-cases/sync-automation-runs.use-case.js +3 -2
  78. package/dist/services/automations/use-cases/sync-automation-runs.use-case.mjs +3 -2
  79. package/dist/services/automations/use-cases/update-automation.use-case.js +8 -7
  80. package/dist/services/automations/use-cases/update-automation.use-case.mjs +6 -5
  81. package/dist/services/automations/utils/open-run.d.ts +2 -0
  82. package/dist/services/automations/utils/open-run.js +2 -2
  83. package/dist/services/automations/utils/open-run.mjs +2 -2
  84. package/dist/services/automations/utils/previous-run.js +3 -2
  85. package/dist/services/automations/utils/previous-run.mjs +3 -2
  86. package/dist/services/automations/utils/runner-scope.d.ts +3 -0
  87. package/dist/{common/decorators/admin-only/index.js → services/automations/utils/runner-scope.js} +15 -5
  88. package/dist/services/automations/utils/runner-scope.mjs +8 -0
  89. package/dist/services/chats/repositories/chat.repository.d.ts +7 -6
  90. package/dist/services/chats/repositories/chat.repository.js +16 -11
  91. package/dist/services/chats/repositories/chat.repository.mjs +16 -11
  92. package/dist/services/chats/use-cases/create-chat.use-case.js +4 -5
  93. package/dist/services/chats/use-cases/create-chat.use-case.mjs +2 -3
  94. package/dist/services/chats/use-cases/delete-chat.use-case.js +7 -9
  95. package/dist/services/chats/use-cases/delete-chat.use-case.mjs +5 -7
  96. package/dist/services/chats/use-cases/get-chat-by-id.use-case.js +3 -2
  97. package/dist/services/chats/use-cases/get-chat-by-id.use-case.mjs +3 -2
  98. package/dist/services/chats/use-cases/interrupt-execution.use-case.js +5 -7
  99. package/dist/services/chats/use-cases/interrupt-execution.use-case.mjs +3 -5
  100. package/dist/services/chats/use-cases/list-chats-by-props.use-case.js +3 -2
  101. package/dist/services/chats/use-cases/list-chats-by-props.use-case.mjs +3 -2
  102. package/dist/services/chats/use-cases/list-chats-by-user.use-case.js +2 -0
  103. package/dist/services/chats/use-cases/list-chats-by-user.use-case.mjs +2 -0
  104. package/dist/services/chats/use-cases/update-chat.use-case.js +5 -7
  105. package/dist/services/chats/use-cases/update-chat.use-case.mjs +3 -5
  106. package/dist/services/llmcalls/repositories/llmcall.repository.d.ts +7 -6
  107. package/dist/services/llmcalls/repositories/llmcall.repository.js +16 -11
  108. package/dist/services/llmcalls/repositories/llmcall.repository.mjs +16 -11
  109. package/dist/services/llmcalls/use-cases/create-llmcall.use-case.js +4 -5
  110. package/dist/services/llmcalls/use-cases/create-llmcall.use-case.mjs +2 -3
  111. package/dist/services/llmcalls/use-cases/delete-llmcall.use-case.js +6 -8
  112. package/dist/services/llmcalls/use-cases/delete-llmcall.use-case.mjs +4 -6
  113. package/dist/services/llmcalls/use-cases/get-llm-usage-by-chat.use-case.js +4 -3
  114. package/dist/services/llmcalls/use-cases/get-llm-usage-by-chat.use-case.mjs +2 -1
  115. package/dist/services/llmcalls/use-cases/get-llm-usage-by-message.use-case.js +4 -3
  116. package/dist/services/llmcalls/use-cases/get-llm-usage-by-message.use-case.mjs +2 -1
  117. package/dist/services/llmcalls/use-cases/get-llm-usage-stats.use-case.js +2 -1
  118. package/dist/services/llmcalls/use-cases/get-llm-usage-stats.use-case.mjs +2 -1
  119. package/dist/services/llmcalls/use-cases/get-llmcall-by-id.use-case.js +6 -4
  120. package/dist/services/llmcalls/use-cases/get-llmcall-by-id.use-case.mjs +4 -2
  121. package/dist/services/llmcalls/use-cases/list-llmcalls-by-props.use-case.js +3 -2
  122. package/dist/services/llmcalls/use-cases/list-llmcalls-by-props.use-case.mjs +3 -2
  123. package/dist/services/llmcalls/use-cases/update-llmcall.use-case.js +5 -7
  124. package/dist/services/llmcalls/use-cases/update-llmcall.use-case.mjs +3 -5
  125. package/dist/services/mcp-servers/repositories/mcpserver.repository.d.ts +8 -7
  126. package/dist/services/mcp-servers/repositories/mcpserver.repository.js +18 -12
  127. package/dist/services/mcp-servers/repositories/mcpserver.repository.mjs +18 -12
  128. package/dist/services/mcp-servers/use-cases/create-mcpserver.use-case.js +4 -5
  129. package/dist/services/mcp-servers/use-cases/create-mcpserver.use-case.mjs +2 -3
  130. package/dist/services/mcp-servers/use-cases/decide-mcpserver-trust.use-case.js +8 -9
  131. package/dist/services/mcp-servers/use-cases/decide-mcpserver-trust.use-case.mjs +6 -7
  132. package/dist/services/mcp-servers/use-cases/delete-mcpserver.use-case.js +6 -8
  133. package/dist/services/mcp-servers/use-cases/delete-mcpserver.use-case.mjs +4 -6
  134. package/dist/services/mcp-servers/use-cases/get-mcpserver-by-id.use-case.js +5 -4
  135. package/dist/services/mcp-servers/use-cases/get-mcpserver-by-id.use-case.mjs +3 -2
  136. package/dist/services/mcp-servers/use-cases/list-all-mcpservers-by-user.use-case.js +4 -3
  137. package/dist/services/mcp-servers/use-cases/list-all-mcpservers-by-user.use-case.mjs +2 -1
  138. package/dist/services/mcp-servers/use-cases/update-mcpserver.use-case.js +8 -11
  139. package/dist/services/mcp-servers/use-cases/update-mcpserver.use-case.mjs +6 -9
  140. package/dist/services/mcp-servers/use-cases/verify-mcpserver-connection.use-case.js +9 -9
  141. package/dist/services/mcp-servers/use-cases/verify-mcpserver-connection.use-case.mjs +7 -7
  142. package/dist/services/tasks/repositories/agent-action.repository.d.ts +4 -4
  143. package/dist/services/tasks/repositories/agent-action.repository.js +9 -7
  144. package/dist/services/tasks/repositories/agent-action.repository.mjs +9 -7
  145. package/dist/services/tasks/repositories/agent-task.repository.d.ts +5 -5
  146. package/dist/services/tasks/repositories/agent-task.repository.js +11 -9
  147. package/dist/services/tasks/repositories/agent-task.repository.mjs +11 -9
  148. package/dist/services/tasks/use-cases/create-agent-action.use-case.js +4 -5
  149. package/dist/services/tasks/use-cases/create-agent-action.use-case.mjs +2 -3
  150. package/dist/services/tasks/use-cases/create-agent-task.use-case.js +4 -5
  151. package/dist/services/tasks/use-cases/create-agent-task.use-case.mjs +2 -3
  152. package/dist/services/tasks/use-cases/get-agent-task-by-id.use-case.js +5 -4
  153. package/dist/services/tasks/use-cases/get-agent-task-by-id.use-case.mjs +3 -2
  154. package/dist/services/tasks/use-cases/list-agent-actions-by-task.use-case.js +3 -1
  155. package/dist/services/tasks/use-cases/list-agent-actions-by-task.use-case.mjs +3 -1
  156. package/dist/services/tasks/use-cases/list-agent-tasks-by-chat.use-case.js +3 -1
  157. package/dist/services/tasks/use-cases/list-agent-tasks-by-chat.use-case.mjs +3 -1
  158. package/dist/services/tasks/use-cases/update-agent-action.use-case.js +5 -7
  159. package/dist/services/tasks/use-cases/update-agent-action.use-case.mjs +3 -5
  160. package/dist/services/tasks/use-cases/update-agent-task.use-case.js +5 -7
  161. package/dist/services/tasks/use-cases/update-agent-task.use-case.mjs +3 -5
  162. package/dist/services/traces/repositories/agent-trace.repository.d.ts +3 -3
  163. package/dist/services/traces/repositories/agent-trace.repository.js +6 -5
  164. package/dist/services/traces/repositories/agent-trace.repository.mjs +6 -5
  165. package/dist/services/traces/use-cases/create-agent-traces.use-case.js +5 -9
  166. package/dist/services/traces/use-cases/create-agent-traces.use-case.mjs +3 -7
  167. package/dist/services/traces/use-cases/list-agent-traces-by-chat.use-case.js +3 -1
  168. package/dist/services/traces/use-cases/list-agent-traces-by-chat.use-case.mjs +3 -1
  169. package/dist/services/users/entities/user-acquisition.js +3 -16
  170. package/dist/services/users/entities/user-acquisition.mjs +4 -11
  171. package/dist/services/users/entities/user-marketing.js +3 -16
  172. package/dist/services/users/entities/user-marketing.mjs +4 -11
  173. package/dist/services/users/entities/user.entity.js +0 -3
  174. package/dist/services/users/entities/user.entity.mjs +1 -4
  175. package/dist/services/users/repositories/user.repository.d.ts +8 -7
  176. package/dist/services/users/repositories/user.repository.js +26 -12
  177. package/dist/services/users/repositories/user.repository.mjs +26 -12
  178. package/dist/services/users/use-cases/create-user.use-case.d.ts +0 -1
  179. package/dist/services/users/use-cases/create-user.use-case.js +5 -5
  180. package/dist/services/users/use-cases/create-user.use-case.mjs +3 -3
  181. package/dist/services/users/use-cases/delete-user.use-case.js +7 -9
  182. package/dist/services/users/use-cases/delete-user.use-case.mjs +5 -7
  183. package/dist/services/users/use-cases/get-user-by-id.use-case.js +6 -4
  184. package/dist/services/users/use-cases/get-user-by-id.use-case.mjs +4 -2
  185. package/dist/services/users/use-cases/list-users-by-props.use-case.js +3 -2
  186. package/dist/services/users/use-cases/list-users-by-props.use-case.mjs +3 -2
  187. package/dist/services/users/use-cases/update-user.use-case.js +5 -7
  188. package/dist/services/users/use-cases/update-user.use-case.mjs +3 -5
  189. package/dist/services/workspaces/repositories/workspace-item.repository.d.ts +8 -8
  190. package/dist/services/workspaces/repositories/workspace-item.repository.js +17 -14
  191. package/dist/services/workspaces/repositories/workspace-item.repository.mjs +17 -14
  192. package/dist/services/workspaces/use-cases/delete-workspace-item.use-case.js +6 -3
  193. package/dist/services/workspaces/use-cases/delete-workspace-item.use-case.mjs +4 -1
  194. package/dist/services/workspaces/use-cases/get-workspace-item.use-case.js +4 -3
  195. package/dist/services/workspaces/use-cases/get-workspace-item.use-case.mjs +2 -1
  196. package/dist/services/workspaces/use-cases/list-workspace-items.use-case.js +2 -0
  197. package/dist/services/workspaces/use-cases/list-workspace-items.use-case.mjs +2 -0
  198. package/dist/services/workspaces/use-cases/save-workspace-item.use-case.js +8 -8
  199. package/dist/services/workspaces/use-cases/save-workspace-item.use-case.mjs +6 -6
  200. package/dist/services/workspaces/use-cases/search-workspace-items.use-case.js +2 -0
  201. package/dist/services/workspaces/use-cases/search-workspace-items.use-case.mjs +2 -0
  202. package/docs/README.md +2 -1
  203. package/docs/authorization.md +77 -0
  204. package/docs/automations.md +4 -4
  205. package/docs/integration-guide.md +8 -10
  206. package/docs/llms.txt +1 -0
  207. package/docs/users.md +6 -4
  208. package/examples/automations/list-automation-runs.ts +2 -0
  209. package/examples/automations/run-automation-now.ts +3 -1
  210. package/examples/chats/delete-chat.ts +1 -1
  211. package/examples/users/create-user.ts +2 -1
  212. package/examples/users/list-users.ts +1 -0
  213. package/package.json +1 -1
  214. package/dist/common/decorators/admin-only/admin-only.d.ts +0 -2
  215. package/dist/common/decorators/admin-only/admin-only.js +0 -52
  216. package/dist/common/decorators/admin-only/admin-only.mjs +0 -28
  217. package/dist/common/decorators/admin-only/index.d.ts +0 -1
  218. package/dist/common/decorators/admin-only/index.mjs +0 -1
  219. package/dist/common/decorators/owner-only/index.d.ts +0 -1
  220. package/dist/common/decorators/owner-only/index.js +0 -22
  221. package/dist/common/decorators/owner-only/index.mjs +0 -1
  222. package/dist/common/decorators/owner-only/owner-only.d.ts +0 -2
  223. package/dist/common/decorators/owner-only/owner-only.js +0 -52
  224. package/dist/common/decorators/owner-only/owner-only.mjs +0 -28
@@ -7,37 +7,40 @@ import { getDatabaseName } from "../../../common/utils/database-name.js";
7
7
  let WorkspaceItemRepository = class {
8
8
  _ = new MongoRepository({
9
9
  database: getDatabaseName(),
10
- collection: "workspace_items"
10
+ collection: "workspace_items",
11
+ scoped: true
11
12
  });
12
- async create(item) {
13
- const id = await this._.createOne({ payload: item.toDocument() });
14
- id && item.setId(id);
13
+ async create(item, scope) {
14
+ const { _id, ownerId, createdAt, createdBy } = await this._.createOne({ payload: item.toDocument(), scope });
15
+ Object.assign(item, { _id, ownerId, createdAt, createdBy });
15
16
  return item;
16
17
  }
17
- async update(item) {
18
+ async update(item, scope) {
18
19
  return await this._.updateOne({
20
+ scope,
19
21
  where: { _id: item._id },
20
22
  payload: item.toDocument()
21
23
  });
22
24
  }
23
- async findById(id, select) {
24
- return await this._.findOne({ where: { _id: this._.parseId(id) }, select });
25
+ async findById(id, scope, select) {
26
+ return await this._.findOne({ scope, where: { _id: this._.parseId(id) }, select });
25
27
  }
26
- async findByName(lookup, select) {
28
+ async findByName(lookup, scope, select) {
27
29
  const { ownerId, chatId, taskId, name } = lookup;
28
30
  return await this._.findOne({
31
+ scope,
29
32
  where: { ...ownerId ? { ownerId } : {}, chatId, taskId: taskId ?? null, name },
30
33
  select
31
34
  });
32
35
  }
33
- async findMany(where, select, options) {
34
- return await this._.findMany({ where, select, options });
36
+ async findMany(where, scope, select, options) {
37
+ return await this._.findMany({ scope, where, select, options });
35
38
  }
36
- async findManyPaginated(where, select, options) {
37
- return await this._.findManyPaginated({ where, select, options });
39
+ async findManyPaginated(where, scope, select, options) {
40
+ return await this._.findManyPaginated({ scope, where, select, options });
38
41
  }
39
- async deleteById(id) {
40
- return await this._.deleteOne({ where: { _id: this._.parseId(id) } });
42
+ async deleteById(id, scope) {
43
+ return await this._.deleteOne({ scope, where: { _id: this._.parseId(id) } });
41
44
  }
42
45
  };
43
46
  WorkspaceItemRepository = __decorateClass([
@@ -32,8 +32,9 @@ __export(delete_workspace_item_use_case_exports, {
32
32
  module.exports = __toCommonJS(delete_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 DeleteWorkspaceItemUseCase = class extends import_base.BaseUseCase {
38
39
  constructor(workspaceItemRepository) {
39
40
  super();
@@ -44,6 +45,7 @@ let DeleteWorkspaceItemUseCase = class extends import_base.BaseUseCase {
44
45
  const { chatId, name, taskId, currentUser } = params;
45
46
  if (!chatId || !name) throw new Error("chatId and name are required");
46
47
  if (!currentUser) this.throwError403();
48
+ const scope = params.scope ?? import_repositories.Scope.canManage(currentUser);
47
49
  const existing = await this.workspaceItemRepository?.findByName(
48
50
  {
49
51
  ownerId: import_base2.BaseEntity.toObjectId(currentUser?.userId),
@@ -51,16 +53,17 @@ let DeleteWorkspaceItemUseCase = class extends import_base.BaseUseCase {
51
53
  taskId: import_base2.BaseEntity.toObjectId(taskId),
52
54
  name
53
55
  },
56
+ scope,
54
57
  { _id: 1 }
55
58
  );
56
59
  if (!existing?._id) return { data: { deletedCount: 0 } };
57
- const deletedCount = await this.workspaceItemRepository?.deleteById(existing._id.toString());
60
+ const deletedCount = await this.workspaceItemRepository?.deleteById(existing._id.toString(), scope);
58
61
  return { data: { deletedCount: deletedCount ?? 0 } };
59
62
  }
60
63
  };
61
64
  DeleteWorkspaceItemUseCase = __decorateClass([
62
65
  (0, import_tsyringe.autoInjectable)(),
63
- __decorateParam(0, (0, import_tsyringe.inject)(import_repositories.WorkspaceItemRepository))
66
+ __decorateParam(0, (0, import_tsyringe.inject)(import_repositories2.WorkspaceItemRepository))
64
67
  ], DeleteWorkspaceItemUseCase);
65
68
  // Annotate the CommonJS export names for ESM import in node:
66
69
  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 DeleteWorkspaceItemUseCase = class extends BaseUseCase {
@@ -16,6 +17,7 @@ let DeleteWorkspaceItemUseCase = class extends BaseUseCase {
16
17
  const { chatId, name, taskId, currentUser } = params;
17
18
  if (!chatId || !name) throw new Error("chatId and name are required");
18
19
  if (!currentUser) this.throwError403();
20
+ const scope = params.scope ?? Scope.canManage(currentUser);
19
21
  const existing = await this.workspaceItemRepository?.findByName(
20
22
  {
21
23
  ownerId: BaseEntity.toObjectId(currentUser?.userId),
@@ -23,10 +25,11 @@ let DeleteWorkspaceItemUseCase = class extends BaseUseCase {
23
25
  taskId: BaseEntity.toObjectId(taskId),
24
26
  name
25
27
  },
28
+ scope,
26
29
  { _id: 1 }
27
30
  );
28
31
  if (!existing?._id) return { data: { deletedCount: 0 } };
29
- const deletedCount = await this.workspaceItemRepository?.deleteById(existing._id.toString());
32
+ const deletedCount = await this.workspaceItemRepository?.deleteById(existing._id.toString(), scope);
30
33
  return { data: { deletedCount: deletedCount ?? 0 } };
31
34
  }
32
35
  };
@@ -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
 
@@ -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.
@@ -211,7 +211,7 @@ The instructions are the first message, as in any run. When the run comes from a
211
211
 
212
212
  ### The token
213
213
 
214
- 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.
215
215
 
216
216
  | Claim | Value |
217
217
  | --- | --- |
@@ -245,7 +245,7 @@ A revoked token is checked after its signature, so an invalid token never reache
245
245
  Every execution is a run.
246
246
 
247
247
  ```typescript
248
- const byAutomation = await listAutomationRunsByAutomation({ automationId, options: { page: 1, limit: 20 } })
248
+ const byAutomation = await listAutomationRunsByAutomation({ automationId, options: { page: 1, limit: 20 }, currentUser })
249
249
 
250
250
  const byUser = await listAutomationRunsByUser({ status: 'failed', currentUser })
251
251
  ```
@@ -330,10 +330,10 @@ An automation runs without the session of its owner, so there is no token of the
330
330
  ### Running by hand
331
331
 
332
332
  ```typescript
333
- const { run } = await runner.runNow({ automationId })
333
+ const { run } = await runner.runNow({ automationId, currentUser })
334
334
  ```
335
335
 
336
- 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()`.
337
337
 
338
338
  ### Several instances
339
339
 
@@ -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
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
@@ -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')
@@ -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
 
@@ -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
 
@@ -1,4 +1,5 @@
1
1
  import { createUser, UserRole } from 'openloops/users'
2
+ import { Scope } from 'openloops/base'
2
3
  import { Logger } from 'openloops/common'
3
4
 
4
5
  async function createUserExample () {
@@ -12,7 +13,7 @@ async function createUserExample () {
12
13
  password: 'openloops!',
13
14
  role: UserRole.USER
14
15
  },
15
- sanitize: false
16
+ scope: Scope.system('demo')
16
17
  })
17
18
 
18
19
  logger.info(result, '[Demo] Created user:')
@@ -6,6 +6,7 @@ async function listUsers () {
6
6
  const logger = new Logger()
7
7
  const currentUser = await CurrentUser.asyncFromDB('6ab56894eb20887d1ef7942c')
8
8
 
9
+ // only admins can list the users of other people
9
10
  const result = await ListUsersByProps({
10
11
  where: { firstName: 'Jose' },
11
12
  options: { page: 1, limit: 10 },
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "openloops",
3
- "version": "0.5.0",
3
+ "version": "0.6.0",
4
4
  "description": "Open source harness for AI agents: a TypeScript runtime with persistence, tools, MCP trust, approvals and autonomous automations, where the loop is pluggable",
5
5
  "author": {
6
6
  "name": "Jose Barcos",
@@ -1,2 +0,0 @@
1
- export declare function AdminOnly(target: any, propertyKey: string): void;
2
- export declare function filterAdminOnlyProperties(object: Record<string, any>): Record<string, any>;