@mastra/factory 0.2.2-alpha.1 → 0.2.2-alpha.2

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 (155) hide show
  1. package/CHANGELOG.md +10 -0
  2. package/dist/auth.js +401 -334
  3. package/dist/auth.js.map +1 -1
  4. package/dist/capabilities/connection.js +1 -1
  5. package/dist/capabilities/intake.js +1 -1
  6. package/dist/capabilities/version-control.js +1 -1
  7. package/dist/factory.js +441 -39622
  8. package/dist/factory.js.map +1 -1
  9. package/dist/index.js +2 -39633
  10. package/dist/integrations/base.js +1 -1
  11. package/dist/integrations/github/config.js +36 -22
  12. package/dist/integrations/github/config.js.map +1 -1
  13. package/dist/integrations/github/factory-session.js +33 -37
  14. package/dist/integrations/github/factory-session.js.map +1 -1
  15. package/dist/integrations/github/integration.js +1037 -3672
  16. package/dist/integrations/github/integration.js.map +1 -1
  17. package/dist/integrations/github/issue-triage.js +71 -71
  18. package/dist/integrations/github/issue-triage.js.map +1 -1
  19. package/dist/integrations/github/pat.js +47 -36
  20. package/dist/integrations/github/pat.js.map +1 -1
  21. package/dist/integrations/github/provenance.js +72 -101
  22. package/dist/integrations/github/provenance.js.map +1 -1
  23. package/dist/integrations/github/routes.js +1272 -1881
  24. package/dist/integrations/github/routes.js.map +1 -1
  25. package/dist/integrations/github/rules.js +203 -471
  26. package/dist/integrations/github/rules.js.map +1 -1
  27. package/dist/integrations/github/sandbox.js +400 -345
  28. package/dist/integrations/github/sandbox.js.map +1 -1
  29. package/dist/integrations/github/session-subscriptions.js +165 -223
  30. package/dist/integrations/github/session-subscriptions.js.map +1 -1
  31. package/dist/integrations/github/subscriptions.js +43 -51
  32. package/dist/integrations/github/subscriptions.js.map +1 -1
  33. package/dist/integrations/github/token-refresh.js +15 -18
  34. package/dist/integrations/github/token-refresh.js.map +1 -1
  35. package/dist/integrations/github/webhook.js +359 -343
  36. package/dist/integrations/github/webhook.js.map +1 -1
  37. package/dist/integrations/linear/agent-tools.js +77 -100
  38. package/dist/integrations/linear/agent-tools.js.map +1 -1
  39. package/dist/integrations/linear/integration.js +611 -1309
  40. package/dist/integrations/linear/integration.js.map +1 -1
  41. package/dist/integrations/linear/routes.js +227 -230
  42. package/dist/integrations/linear/routes.js.map +1 -1
  43. package/dist/integrations/linear/rules.js +126 -346
  44. package/dist/integrations/linear/rules.js.map +1 -1
  45. package/dist/integrations/linear/storage.js +1 -1
  46. package/dist/integrations/platform/api-client.js +131 -135
  47. package/dist/integrations/platform/api-client.js.map +1 -1
  48. package/dist/integrations/platform/github/event-worker.js +275 -573
  49. package/dist/integrations/platform/github/event-worker.js.map +1 -1
  50. package/dist/integrations/platform/github/integration.js +845 -4022
  51. package/dist/integrations/platform/github/integration.js.map +1 -1
  52. package/dist/integrations/platform/linear/integration.js +457 -1309
  53. package/dist/integrations/platform/linear/integration.js.map +1 -1
  54. package/dist/integrations/workos/integration.js +80 -85
  55. package/dist/integrations/workos/integration.js.map +1 -1
  56. package/dist/routes/config.d.ts.map +1 -1
  57. package/dist/routes/config.js +933 -912
  58. package/dist/routes/config.js.map +1 -1
  59. package/dist/routes/custom-provider-source.js +110 -93
  60. package/dist/routes/custom-provider-source.js.map +1 -1
  61. package/dist/routes/fs.js +401 -349
  62. package/dist/routes/fs.js.map +1 -1
  63. package/dist/routes/intake.js +165 -165
  64. package/dist/routes/intake.js.map +1 -1
  65. package/dist/routes/oauth.js +447 -25166
  66. package/dist/routes/oauth.js.map +1 -1
  67. package/dist/routes/projects.js +437 -376
  68. package/dist/routes/projects.js.map +1 -1
  69. package/dist/routes/provider-credentials.js +76 -58
  70. package/dist/routes/provider-credentials.js.map +1 -1
  71. package/dist/routes/route.js +12 -8
  72. package/dist/routes/route.js.map +1 -1
  73. package/dist/routes/skills.js +160 -173
  74. package/dist/routes/skills.js.map +1 -1
  75. package/dist/routes/surface.js +250 -28615
  76. package/dist/routes/surface.js.map +1 -1
  77. package/dist/routes/tenant-credentials.js +141 -166
  78. package/dist/routes/tenant-credentials.js.map +1 -1
  79. package/dist/routes/work-items.js +654 -862
  80. package/dist/routes/work-items.js.map +1 -1
  81. package/dist/rules/binding-context.js +23 -35
  82. package/dist/rules/binding-context.js.map +1 -1
  83. package/dist/rules/defaults.js +200 -309
  84. package/dist/rules/defaults.js.map +1 -1
  85. package/dist/rules/dispatcher.js +438 -683
  86. package/dist/rules/dispatcher.js.map +1 -1
  87. package/dist/rules/index.js +5 -599
  88. package/dist/rules/processor.js +344 -596
  89. package/dist/rules/processor.js.map +1 -1
  90. package/dist/rules/resolve.js +21 -18
  91. package/dist/rules/resolve.js.map +1 -1
  92. package/dist/rules/start-coordinator.js +168 -157
  93. package/dist/rules/start-coordinator.js.map +1 -1
  94. package/dist/rules/tools.js +50 -85
  95. package/dist/rules/tools.js.map +1 -1
  96. package/dist/rules/transition-service.js +183 -454
  97. package/dist/rules/transition-service.js.map +1 -1
  98. package/dist/rules/types.js +33 -29
  99. package/dist/rules/types.js.map +1 -1
  100. package/dist/rules/validation.js +266 -281
  101. package/dist/rules/validation.js.map +1 -1
  102. package/dist/sandbox/fleet.js +272 -250
  103. package/dist/sandbox/fleet.js.map +1 -1
  104. package/dist/sandbox/reattach.js +13 -6
  105. package/dist/sandbox/reattach.js.map +1 -1
  106. package/dist/server-error.js +12 -16
  107. package/dist/server-error.js.map +1 -1
  108. package/dist/skills/service.js +30 -35
  109. package/dist/skills/service.js.map +1 -1
  110. package/dist/spa-static.js +91 -65
  111. package/dist/spa-static.js.map +1 -1
  112. package/dist/state-signing.js +73 -44
  113. package/dist/state-signing.js.map +1 -1
  114. package/dist/storage/domains/audit/agent-audit.js +60 -59
  115. package/dist/storage/domains/audit/agent-audit.js.map +1 -1
  116. package/dist/storage/domains/audit/base.js +169 -112
  117. package/dist/storage/domains/audit/base.js.map +1 -1
  118. package/dist/storage/domains/audit/domain.js +155 -153
  119. package/dist/storage/domains/audit/domain.js.map +1 -1
  120. package/dist/storage/domains/credentials/base.js +259 -243
  121. package/dist/storage/domains/credentials/base.js.map +1 -1
  122. package/dist/storage/domains/custom-providers/base.js +138 -143
  123. package/dist/storage/domains/custom-providers/base.js.map +1 -1
  124. package/dist/storage/domains/intake/base.js +75 -57
  125. package/dist/storage/domains/intake/base.js.map +1 -1
  126. package/dist/storage/domains/integrations/base.js +310 -245
  127. package/dist/storage/domains/integrations/base.js.map +1 -1
  128. package/dist/storage/domains/memory-settings/base.js +117 -102
  129. package/dist/storage/domains/memory-settings/base.js.map +1 -1
  130. package/dist/storage/domains/model-packs/base.js +89 -88
  131. package/dist/storage/domains/model-packs/base.js.map +1 -1
  132. package/dist/storage/domains/projects/base.js +97 -92
  133. package/dist/storage/domains/projects/base.js.map +1 -1
  134. package/dist/storage/domains/queue-health/base.js +111 -76
  135. package/dist/storage/domains/queue-health/base.js.map +1 -1
  136. package/dist/storage/domains/source-control/base.js +777 -706
  137. package/dist/storage/domains/source-control/base.js.map +1 -1
  138. package/dist/storage/domains/source-control/inmemory.js +322 -348
  139. package/dist/storage/domains/source-control/inmemory.js.map +1 -1
  140. package/dist/storage/domains/work-items/base.js +1266 -1148
  141. package/dist/storage/domains/work-items/base.js.map +1 -1
  142. package/dist/storage/domains/work-items/metrics.js +202 -184
  143. package/dist/storage/domains/work-items/metrics.js.map +1 -1
  144. package/dist/timing.js +29 -22
  145. package/dist/timing.js.map +1 -1
  146. package/dist/workspace.js +236 -477
  147. package/dist/workspace.js.map +1 -1
  148. package/package.json +8 -8
  149. package/dist/capabilities/connection.js.map +0 -1
  150. package/dist/capabilities/intake.js.map +0 -1
  151. package/dist/capabilities/version-control.js.map +0 -1
  152. package/dist/index.js.map +0 -1
  153. package/dist/integrations/base.js.map +0 -1
  154. package/dist/integrations/linear/storage.js.map +0 -1
  155. package/dist/rules/index.js.map +0 -1
@@ -1,396 +1,457 @@
1
- // src/routes/projects.ts
1
+ import { Route } from "./route.js";
2
2
  import { registerApiRoute } from "@mastra/core/server";
3
-
4
- // src/routes/route.ts
5
- var Route = class {
6
- deps;
7
- constructor(deps) {
8
- this.deps = deps;
9
- }
10
- };
11
-
12
- // src/routes/projects.ts
13
- var UUID_RE = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i;
14
- var MAX_NAME_LENGTH = 200;
15
- var MAX_DESCRIPTION_LENGTH = 2e3;
16
- var MAX_SETUP_COMMAND_LENGTH = 2e3;
17
- var MAX_BRANCH_LENGTH = 255;
18
- var MAX_SANDBOX_PROVIDER_LENGTH = 100;
19
- var MAX_SANDBOX_WORKDIR_LENGTH = 1e3;
20
- var CONTROL_CHAR_RE = /[\0-\x08\x0b\x0c\x0e-\x1f\x7f]/;
3
+ //#region src/routes/projects.ts
4
+ const UUID_RE = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i;
5
+ const MAX_NAME_LENGTH = 200;
6
+ const MAX_DESCRIPTION_LENGTH = 2e3;
7
+ const MAX_SETUP_COMMAND_LENGTH = 2e3;
8
+ const MAX_BRANCH_LENGTH = 255;
9
+ const MAX_SANDBOX_PROVIDER_LENGTH = 100;
10
+ const MAX_SANDBOX_WORKDIR_LENGTH = 1e3;
11
+ const CONTROL_CHAR_RE = /[\0-\x08\x0b\x0c\x0e-\x1f\x7f]/;
21
12
  function loose(context) {
22
- return context;
13
+ return context;
23
14
  }
24
15
  async function readJson(context) {
25
- try {
26
- return await context.req.json();
27
- } catch {
28
- return void 0;
29
- }
16
+ try {
17
+ return await context.req.json();
18
+ } catch {
19
+ return;
20
+ }
30
21
  }
31
22
  function parseCreateInput(value) {
32
- if (!value || typeof value !== "object") return null;
33
- const input = value;
34
- if (typeof input.name !== "string") return null;
35
- const name = input.name.trim();
36
- if (!name || name.length > MAX_NAME_LENGTH) return null;
37
- if (input.description !== void 0 && input.description !== null && typeof input.description !== "string")
38
- return null;
39
- const description = typeof input.description === "string" ? input.description.trim() || null : null;
40
- if (description && description.length > MAX_DESCRIPTION_LENGTH) return null;
41
- return { name, description };
23
+ if (!value || typeof value !== "object") return null;
24
+ const input = value;
25
+ if (typeof input.name !== "string") return null;
26
+ const name = input.name.trim();
27
+ if (!name || name.length > MAX_NAME_LENGTH) return null;
28
+ if (input.description !== void 0 && input.description !== null && typeof input.description !== "string") return null;
29
+ const description = typeof input.description === "string" ? input.description.trim() || null : null;
30
+ if (description && description.length > MAX_DESCRIPTION_LENGTH) return null;
31
+ return {
32
+ name,
33
+ description
34
+ };
42
35
  }
43
36
  function parseUpdateInput(value) {
44
- if (!value || typeof value !== "object") return null;
45
- const input = value;
46
- const patch = {};
47
- if (input.name !== void 0) {
48
- if (typeof input.name !== "string") return null;
49
- const name = input.name.trim();
50
- if (!name || name.length > MAX_NAME_LENGTH) return null;
51
- patch.name = name;
52
- }
53
- if (input.description !== void 0) {
54
- if (input.description !== null && typeof input.description !== "string") return null;
55
- const description = typeof input.description === "string" ? input.description.trim() || null : null;
56
- if (description && description.length > MAX_DESCRIPTION_LENGTH) return null;
57
- patch.description = description;
58
- }
59
- if (input.defaultModelId !== void 0) {
60
- const defaultModelId = parseOptionalString(input.defaultModelId, { maxLength: MAX_NAME_LENGTH, nullable: true });
61
- if (defaultModelId === false) return null;
62
- patch.defaultModelId = defaultModelId ?? null;
63
- }
64
- return Object.keys(patch).length > 0 ? patch : null;
37
+ if (!value || typeof value !== "object") return null;
38
+ const input = value;
39
+ const patch = {};
40
+ if (input.name !== void 0) {
41
+ if (typeof input.name !== "string") return null;
42
+ const name = input.name.trim();
43
+ if (!name || name.length > MAX_NAME_LENGTH) return null;
44
+ patch.name = name;
45
+ }
46
+ if (input.description !== void 0) {
47
+ if (input.description !== null && typeof input.description !== "string") return null;
48
+ const description = typeof input.description === "string" ? input.description.trim() || null : null;
49
+ if (description && description.length > MAX_DESCRIPTION_LENGTH) return null;
50
+ patch.description = description;
51
+ }
52
+ if (input.defaultModelId !== void 0) {
53
+ const defaultModelId = parseOptionalString(input.defaultModelId, {
54
+ maxLength: MAX_NAME_LENGTH,
55
+ nullable: true
56
+ });
57
+ if (defaultModelId === false) return null;
58
+ patch.defaultModelId = defaultModelId ?? null;
59
+ }
60
+ return Object.keys(patch).length > 0 ? patch : null;
65
61
  }
66
62
  function parseConnectionInput(value) {
67
- if (!value || typeof value !== "object") return null;
68
- const input = value;
69
- if (typeof input.integrationId !== "string" || !input.integrationId.trim()) return null;
70
- if (typeof input.installationId !== "string" || !UUID_RE.test(input.installationId)) return null;
71
- return { integrationId: input.integrationId.trim(), installationId: input.installationId };
63
+ if (!value || typeof value !== "object") return null;
64
+ const input = value;
65
+ if (typeof input.integrationId !== "string" || !input.integrationId.trim()) return null;
66
+ if (typeof input.installationId !== "string" || !UUID_RE.test(input.installationId)) return null;
67
+ return {
68
+ integrationId: input.integrationId.trim(),
69
+ installationId: input.installationId
70
+ };
72
71
  }
73
72
  function parseOptionalString(value, { maxLength, nullable = false }) {
74
- if (value === void 0) return void 0;
75
- if (value === null) return nullable ? null : false;
76
- if (typeof value !== "string") return false;
77
- const normalized = value.trim();
78
- if (!normalized) return nullable ? null : false;
79
- if (normalized.length > maxLength || CONTROL_CHAR_RE.test(normalized)) return false;
80
- return normalized;
73
+ if (value === void 0) return void 0;
74
+ if (value === null) return nullable ? null : false;
75
+ if (typeof value !== "string") return false;
76
+ const normalized = value.trim();
77
+ if (!normalized) return nullable ? null : false;
78
+ if (normalized.length > maxLength || CONTROL_CHAR_RE.test(normalized)) return false;
79
+ return normalized;
81
80
  }
82
81
  function parseRepositoryLinkInput(value) {
83
- if (!value || typeof value !== "object") return null;
84
- const input = value;
85
- if (typeof input.repositoryId !== "string" || !UUID_RE.test(input.repositoryId)) return null;
86
- const branch = parseOptionalString(input.branch, { maxLength: MAX_BRANCH_LENGTH, nullable: true });
87
- const sandboxProvider = parseOptionalString(input.sandboxProvider, { maxLength: MAX_SANDBOX_PROVIDER_LENGTH });
88
- const sandboxWorkdir = parseOptionalString(input.sandboxWorkdir, { maxLength: MAX_SANDBOX_WORKDIR_LENGTH });
89
- const setupCommand = parseOptionalString(input.setupCommand, { maxLength: MAX_SETUP_COMMAND_LENGTH, nullable: true });
90
- if (branch === false || typeof sandboxProvider !== "string" || typeof sandboxWorkdir !== "string" || setupCommand === false)
91
- return null;
92
- return {
93
- repositoryId: input.repositoryId,
94
- branch: branch ?? null,
95
- sandboxProvider,
96
- sandboxWorkdir,
97
- setupCommand: setupCommand ?? null
98
- };
82
+ if (!value || typeof value !== "object") return null;
83
+ const input = value;
84
+ if (typeof input.repositoryId !== "string" || !UUID_RE.test(input.repositoryId)) return null;
85
+ const branch = parseOptionalString(input.branch, {
86
+ maxLength: MAX_BRANCH_LENGTH,
87
+ nullable: true
88
+ });
89
+ const sandboxProvider = parseOptionalString(input.sandboxProvider, { maxLength: MAX_SANDBOX_PROVIDER_LENGTH });
90
+ const sandboxWorkdir = parseOptionalString(input.sandboxWorkdir, { maxLength: MAX_SANDBOX_WORKDIR_LENGTH });
91
+ const setupCommand = parseOptionalString(input.setupCommand, {
92
+ maxLength: MAX_SETUP_COMMAND_LENGTH,
93
+ nullable: true
94
+ });
95
+ if (branch === false || typeof sandboxProvider !== "string" || typeof sandboxWorkdir !== "string" || setupCommand === false) return null;
96
+ return {
97
+ repositoryId: input.repositoryId,
98
+ branch: branch ?? null,
99
+ sandboxProvider,
100
+ sandboxWorkdir,
101
+ setupCommand: setupCommand ?? null
102
+ };
99
103
  }
100
104
  function parseRepositoryUpdateInput(value) {
101
- if (!value || typeof value !== "object") return null;
102
- const input = value;
103
- const patch = {};
104
- const branch = parseOptionalString(input.branch, { maxLength: MAX_BRANCH_LENGTH, nullable: true });
105
- const sandboxProvider = parseOptionalString(input.sandboxProvider, { maxLength: MAX_SANDBOX_PROVIDER_LENGTH });
106
- const sandboxWorkdir = parseOptionalString(input.sandboxWorkdir, { maxLength: MAX_SANDBOX_WORKDIR_LENGTH });
107
- const setupCommand = parseOptionalString(input.setupCommand, { maxLength: MAX_SETUP_COMMAND_LENGTH, nullable: true });
108
- if (branch === false || sandboxProvider === false || sandboxProvider === null || sandboxWorkdir === false || sandboxWorkdir === null || setupCommand === false)
109
- return null;
110
- if (branch !== void 0) patch.branch = branch;
111
- if (sandboxProvider !== void 0) patch.sandboxProvider = sandboxProvider;
112
- if (sandboxWorkdir !== void 0) patch.sandboxWorkdir = sandboxWorkdir;
113
- if (setupCommand !== void 0) patch.setupCommand = setupCommand;
114
- return Object.keys(patch).length > 0 ? patch : null;
105
+ if (!value || typeof value !== "object") return null;
106
+ const input = value;
107
+ const patch = {};
108
+ const branch = parseOptionalString(input.branch, {
109
+ maxLength: MAX_BRANCH_LENGTH,
110
+ nullable: true
111
+ });
112
+ const sandboxProvider = parseOptionalString(input.sandboxProvider, { maxLength: MAX_SANDBOX_PROVIDER_LENGTH });
113
+ const sandboxWorkdir = parseOptionalString(input.sandboxWorkdir, { maxLength: MAX_SANDBOX_WORKDIR_LENGTH });
114
+ const setupCommand = parseOptionalString(input.setupCommand, {
115
+ maxLength: MAX_SETUP_COMMAND_LENGTH,
116
+ nullable: true
117
+ });
118
+ if (branch === false || sandboxProvider === false || sandboxProvider === null || sandboxWorkdir === false || sandboxWorkdir === null || setupCommand === false) return null;
119
+ if (branch !== void 0) patch.branch = branch;
120
+ if (sandboxProvider !== void 0) patch.sandboxProvider = sandboxProvider;
121
+ if (sandboxWorkdir !== void 0) patch.sandboxWorkdir = sandboxWorkdir;
122
+ if (setupCommand !== void 0) patch.setupCommand = setupCommand;
123
+ return Object.keys(patch).length > 0 ? patch : null;
115
124
  }
116
125
  var ProjectRoutes = class extends Route {
117
- #versionControlIntegrationIds;
118
- constructor(deps) {
119
- super(deps);
120
- this.#versionControlIntegrationIds = new Set(deps.versionControlIntegrationIds ?? []);
121
- }
122
- async #projects() {
123
- await this.deps.projects.ensureReady();
124
- return this.deps.projects;
125
- }
126
- async #sourceControl() {
127
- await this.deps.sourceControl.ensureReady();
128
- return this.deps.sourceControl;
129
- }
130
- async #handles() {
131
- const storage = await this.#sourceControl();
132
- return [...this.#versionControlIntegrationIds].map((integrationId) => storage.forIntegration(integrationId));
133
- }
134
- async #project(orgId, id) {
135
- return (await this.#projects()).get({ orgId, id });
136
- }
137
- async #findConnection({ orgId, projectId, id }) {
138
- for (const handle of await this.#handles()) {
139
- const connection = await handle.connections.get({ orgId, id });
140
- if (connection?.factoryProjectId === projectId) return { handle, connection };
141
- }
142
- return null;
143
- }
144
- async #findProjectRepository({ orgId, projectId, id }) {
145
- for (const handle of await this.#handles()) {
146
- const projectRepository = await handle.projectRepositories.get({ orgId, id });
147
- if (!projectRepository) continue;
148
- const connection = await handle.connections.get({ orgId, id: projectRepository.connectionId });
149
- if (connection?.factoryProjectId === projectId) return { handle, connection, projectRepository };
150
- }
151
- return null;
152
- }
153
- async #repositoryPayload(handle, orgId, projectRepository) {
154
- const repository = await handle.repositories.get({ orgId, id: projectRepository.repositoryId });
155
- return { ...projectRepository, repository };
156
- }
157
- async #resolveTenant(context) {
158
- await this.deps.auth.ensureUser(context);
159
- const tenant = this.deps.auth.tenant(context);
160
- if (!tenant) return { response: context.json({ error: "unauthorized" }, 401) };
161
- if (!tenant.orgId) {
162
- return {
163
- response: context.json(
164
- { error: "organization_required", message: "Factory projects require an organization." },
165
- 403
166
- )
167
- };
168
- }
169
- return { orgId: tenant.orgId, userId: tenant.userId };
170
- }
171
- routes() {
172
- return [
173
- registerApiRoute("/web/factory/projects", {
174
- method: "GET",
175
- requiresAuth: false,
176
- handler: async (routeContext) => {
177
- const context = loose(routeContext);
178
- const tenant = await this.#resolveTenant(context);
179
- if ("response" in tenant) return tenant.response;
180
- return context.json({ projects: await (await this.#projects()).list({ orgId: tenant.orgId }) });
181
- }
182
- }),
183
- registerApiRoute("/web/factory/projects", {
184
- method: "POST",
185
- requiresAuth: false,
186
- handler: async (routeContext) => {
187
- const context = loose(routeContext);
188
- const tenant = await this.#resolveTenant(context);
189
- if ("response" in tenant) return tenant.response;
190
- const input = parseCreateInput(await readJson(context));
191
- if (!input) return context.json({ error: "invalid_project" }, 400);
192
- const project = await (await this.#projects()).create({ orgId: tenant.orgId, userId: tenant.userId, input });
193
- return context.json({ project }, 201);
194
- }
195
- }),
196
- registerApiRoute("/web/factory/projects/:id", {
197
- method: "GET",
198
- requiresAuth: false,
199
- handler: async (routeContext) => {
200
- const context = loose(routeContext);
201
- const tenant = await this.#resolveTenant(context);
202
- if ("response" in tenant) return tenant.response;
203
- const id = context.req.param("id");
204
- if (!id || !UUID_RE.test(id)) return context.json({ error: "Project not found" }, 404);
205
- const project = await this.#project(tenant.orgId, id);
206
- return project ? context.json({ project }) : context.json({ error: "Project not found" }, 404);
207
- }
208
- }),
209
- registerApiRoute("/web/factory/projects/:id", {
210
- method: "PATCH",
211
- requiresAuth: false,
212
- handler: async (routeContext) => {
213
- const context = loose(routeContext);
214
- const tenant = await this.#resolveTenant(context);
215
- if ("response" in tenant) return tenant.response;
216
- const id = context.req.param("id");
217
- if (!id || !UUID_RE.test(id)) return context.json({ error: "Project not found" }, 404);
218
- const input = parseUpdateInput(await readJson(context));
219
- if (!input) return context.json({ error: "invalid_project" }, 400);
220
- const project = await (await this.#projects()).update({ orgId: tenant.orgId, id, input });
221
- return project ? context.json({ project }) : context.json({ error: "Project not found" }, 404);
222
- }
223
- }),
224
- registerApiRoute("/web/factory/projects/:id", {
225
- method: "DELETE",
226
- requiresAuth: false,
227
- handler: async (routeContext) => {
228
- const context = loose(routeContext);
229
- const tenant = await this.#resolveTenant(context);
230
- if ("response" in tenant) return tenant.response;
231
- const id = context.req.param("id");
232
- if (!id || !UUID_RE.test(id)) return context.json({ error: "Project not found" }, 404);
233
- if (!await this.#project(tenant.orgId, id)) return context.json({ error: "Project not found" }, 404);
234
- for (const handle of await this.#handles()) {
235
- for (const connection of await handle.connections.list({ orgId: tenant.orgId, factoryProjectId: id })) {
236
- await handle.connections.delete({ orgId: tenant.orgId, id: connection.id });
237
- }
238
- }
239
- await (await this.#projects()).delete({ orgId: tenant.orgId, id });
240
- return context.body(null, 204);
241
- }
242
- }),
243
- registerApiRoute("/web/factory/projects/:id/source-control-connections", {
244
- method: "GET",
245
- requiresAuth: false,
246
- handler: async (routeContext) => {
247
- const context = loose(routeContext);
248
- const tenant = await this.#resolveTenant(context);
249
- if ("response" in tenant) return tenant.response;
250
- const projectId = context.req.param("id");
251
- if (!projectId || !UUID_RE.test(projectId) || !await this.#project(tenant.orgId, projectId))
252
- return context.json({ error: "Project not found" }, 404);
253
- const connections = [];
254
- for (const handle of await this.#handles()) {
255
- for (const connection of await handle.connections.list({
256
- orgId: tenant.orgId,
257
- factoryProjectId: projectId
258
- })) {
259
- const installation = await handle.installations.get({
260
- orgId: tenant.orgId,
261
- id: connection.installationId
262
- });
263
- if (!installation) continue;
264
- const links = await handle.projectRepositories.list({ orgId: tenant.orgId, connectionId: connection.id });
265
- connections.push({
266
- ...connection,
267
- installation,
268
- repositories: await Promise.all(links.map((link) => this.#repositoryPayload(handle, tenant.orgId, link)))
269
- });
270
- }
271
- }
272
- return context.json({ connections });
273
- }
274
- }),
275
- registerApiRoute("/web/factory/projects/:id/source-control-connections", {
276
- method: "POST",
277
- requiresAuth: false,
278
- handler: async (routeContext) => {
279
- const context = loose(routeContext);
280
- const tenant = await this.#resolveTenant(context);
281
- if ("response" in tenant) return tenant.response;
282
- const projectId = context.req.param("id");
283
- if (!projectId || !UUID_RE.test(projectId) || !await this.#project(tenant.orgId, projectId))
284
- return context.json({ error: "Project not found" }, 404);
285
- const input = parseConnectionInput(await readJson(context));
286
- if (!input) return context.json({ error: "invalid_source_control_connection" }, 400);
287
- if (!this.#versionControlIntegrationIds.has(input.integrationId))
288
- return context.json({ error: "Source-control integration not found" }, 404);
289
- const handle = (await this.#sourceControl()).forIntegration(input.integrationId);
290
- if (!await handle.installations.get({ orgId: tenant.orgId, id: input.installationId }))
291
- return context.json({ error: "Source-control installation not found" }, 404);
292
- const connection = await handle.connections.create({
293
- orgId: tenant.orgId,
294
- factoryProjectId: projectId,
295
- installationId: input.installationId,
296
- createdByUserId: tenant.userId
297
- });
298
- return context.json({ connection }, 201);
299
- }
300
- }),
301
- registerApiRoute("/web/factory/projects/:id/source-control-connections/:connectionId", {
302
- method: "DELETE",
303
- requiresAuth: false,
304
- handler: async (routeContext) => {
305
- const context = loose(routeContext);
306
- const tenant = await this.#resolveTenant(context);
307
- if ("response" in tenant) return tenant.response;
308
- const projectId = context.req.param("id");
309
- const connectionId = context.req.param("connectionId");
310
- if (!projectId || !UUID_RE.test(projectId) || !connectionId || !UUID_RE.test(connectionId))
311
- return context.json({ error: "Source-control connection not found" }, 404);
312
- const found = await this.#findConnection({ orgId: tenant.orgId, projectId, id: connectionId });
313
- if (!found) return context.json({ error: "Source-control connection not found" }, 404);
314
- await found.handle.connections.delete({ orgId: tenant.orgId, id: connectionId });
315
- return context.body(null, 204);
316
- }
317
- }),
318
- registerApiRoute("/web/factory/projects/:id/source-control-connections/:connectionId/repositories", {
319
- method: "POST",
320
- requiresAuth: false,
321
- handler: async (routeContext) => {
322
- const context = loose(routeContext);
323
- const tenant = await this.#resolveTenant(context);
324
- if ("response" in tenant) return tenant.response;
325
- const projectId = context.req.param("id");
326
- const connectionId = context.req.param("connectionId");
327
- if (!projectId || !UUID_RE.test(projectId) || !connectionId || !UUID_RE.test(connectionId))
328
- return context.json({ error: "Source-control connection not found" }, 404);
329
- const found = await this.#findConnection({ orgId: tenant.orgId, projectId, id: connectionId });
330
- if (!found) return context.json({ error: "Source-control connection not found" }, 404);
331
- const input = parseRepositoryLinkInput(await readJson(context));
332
- if (!input) return context.json({ error: "invalid_project_repository" }, 400);
333
- const repository = await found.handle.repositories.get({ orgId: tenant.orgId, id: input.repositoryId });
334
- if (!repository || repository.installationId !== found.connection.installationId)
335
- return context.json({ error: "Source-control repository not found" }, 404);
336
- const projectRepository = await found.handle.projectRepositories.link({
337
- orgId: tenant.orgId,
338
- connectionId,
339
- createdByUserId: tenant.userId,
340
- ...input
341
- });
342
- return context.json(
343
- { projectRepository: await this.#repositoryPayload(found.handle, tenant.orgId, projectRepository) },
344
- 201
345
- );
346
- }
347
- }),
348
- registerApiRoute("/web/factory/projects/:id/repositories/:projectRepositoryId", {
349
- method: "PATCH",
350
- requiresAuth: false,
351
- handler: async (routeContext) => {
352
- const context = loose(routeContext);
353
- const tenant = await this.#resolveTenant(context);
354
- if ("response" in tenant) return tenant.response;
355
- const projectId = context.req.param("id");
356
- const projectRepositoryId = context.req.param("projectRepositoryId");
357
- if (!projectId || !UUID_RE.test(projectId) || !projectRepositoryId || !UUID_RE.test(projectRepositoryId))
358
- return context.json({ error: "Project repository not found" }, 404);
359
- const found = await this.#findProjectRepository({ orgId: tenant.orgId, projectId, id: projectRepositoryId });
360
- if (!found) return context.json({ error: "Project repository not found" }, 404);
361
- const input = parseRepositoryUpdateInput(await readJson(context));
362
- if (!input) return context.json({ error: "invalid_project_repository" }, 400);
363
- const projectRepository = await found.handle.projectRepositories.update({
364
- orgId: tenant.orgId,
365
- id: projectRepositoryId,
366
- input
367
- });
368
- return context.json({
369
- projectRepository: await this.#repositoryPayload(found.handle, tenant.orgId, projectRepository)
370
- });
371
- }
372
- }),
373
- registerApiRoute("/web/factory/projects/:id/repositories/:projectRepositoryId", {
374
- method: "DELETE",
375
- requiresAuth: false,
376
- handler: async (routeContext) => {
377
- const context = loose(routeContext);
378
- const tenant = await this.#resolveTenant(context);
379
- if ("response" in tenant) return tenant.response;
380
- const projectId = context.req.param("id");
381
- const projectRepositoryId = context.req.param("projectRepositoryId");
382
- if (!projectId || !UUID_RE.test(projectId) || !projectRepositoryId || !UUID_RE.test(projectRepositoryId))
383
- return context.json({ error: "Project repository not found" }, 404);
384
- const found = await this.#findProjectRepository({ orgId: tenant.orgId, projectId, id: projectRepositoryId });
385
- if (!found) return context.json({ error: "Project repository not found" }, 404);
386
- await found.handle.projectRepositories.unlink({ orgId: tenant.orgId, id: projectRepositoryId });
387
- return context.body(null, 204);
388
- }
389
- })
390
- ];
391
- }
392
- };
393
- export {
394
- ProjectRoutes
126
+ #versionControlIntegrationIds;
127
+ constructor(deps) {
128
+ super(deps);
129
+ this.#versionControlIntegrationIds = new Set(deps.versionControlIntegrationIds ?? []);
130
+ }
131
+ async #projects() {
132
+ await this.deps.projects.ensureReady();
133
+ return this.deps.projects;
134
+ }
135
+ async #sourceControl() {
136
+ await this.deps.sourceControl.ensureReady();
137
+ return this.deps.sourceControl;
138
+ }
139
+ async #handles() {
140
+ const storage = await this.#sourceControl();
141
+ return [...this.#versionControlIntegrationIds].map((integrationId) => storage.forIntegration(integrationId));
142
+ }
143
+ async #project(orgId, id) {
144
+ return (await this.#projects()).get({
145
+ orgId,
146
+ id
147
+ });
148
+ }
149
+ async #findConnection({ orgId, projectId, id }) {
150
+ for (const handle of await this.#handles()) {
151
+ const connection = await handle.connections.get({
152
+ orgId,
153
+ id
154
+ });
155
+ if (connection?.factoryProjectId === projectId) return {
156
+ handle,
157
+ connection
158
+ };
159
+ }
160
+ return null;
161
+ }
162
+ async #findProjectRepository({ orgId, projectId, id }) {
163
+ for (const handle of await this.#handles()) {
164
+ const projectRepository = await handle.projectRepositories.get({
165
+ orgId,
166
+ id
167
+ });
168
+ if (!projectRepository) continue;
169
+ const connection = await handle.connections.get({
170
+ orgId,
171
+ id: projectRepository.connectionId
172
+ });
173
+ if (connection?.factoryProjectId === projectId) return {
174
+ handle,
175
+ connection,
176
+ projectRepository
177
+ };
178
+ }
179
+ return null;
180
+ }
181
+ async #repositoryPayload(handle, orgId, projectRepository) {
182
+ const repository = await handle.repositories.get({
183
+ orgId,
184
+ id: projectRepository.repositoryId
185
+ });
186
+ return {
187
+ ...projectRepository,
188
+ repository
189
+ };
190
+ }
191
+ async #resolveTenant(context) {
192
+ await this.deps.auth.ensureUser(context);
193
+ const tenant = this.deps.auth.tenant(context);
194
+ if (!tenant) return { response: context.json({ error: "unauthorized" }, 401) };
195
+ if (!tenant.orgId) return { response: context.json({
196
+ error: "organization_required",
197
+ message: "Factory projects require an organization."
198
+ }, 403) };
199
+ return {
200
+ orgId: tenant.orgId,
201
+ userId: tenant.userId
202
+ };
203
+ }
204
+ routes() {
205
+ return [
206
+ registerApiRoute("/web/factory/projects", {
207
+ method: "GET",
208
+ requiresAuth: false,
209
+ handler: async (routeContext) => {
210
+ const context = loose(routeContext);
211
+ const tenant = await this.#resolveTenant(context);
212
+ if ("response" in tenant) return tenant.response;
213
+ return context.json({ projects: await (await this.#projects()).list({ orgId: tenant.orgId }) });
214
+ }
215
+ }),
216
+ registerApiRoute("/web/factory/projects", {
217
+ method: "POST",
218
+ requiresAuth: false,
219
+ handler: async (routeContext) => {
220
+ const context = loose(routeContext);
221
+ const tenant = await this.#resolveTenant(context);
222
+ if ("response" in tenant) return tenant.response;
223
+ const input = parseCreateInput(await readJson(context));
224
+ if (!input) return context.json({ error: "invalid_project" }, 400);
225
+ const project = await (await this.#projects()).create({
226
+ orgId: tenant.orgId,
227
+ userId: tenant.userId,
228
+ input
229
+ });
230
+ return context.json({ project }, 201);
231
+ }
232
+ }),
233
+ registerApiRoute("/web/factory/projects/:id", {
234
+ method: "GET",
235
+ requiresAuth: false,
236
+ handler: async (routeContext) => {
237
+ const context = loose(routeContext);
238
+ const tenant = await this.#resolveTenant(context);
239
+ if ("response" in tenant) return tenant.response;
240
+ const id = context.req.param("id");
241
+ if (!id || !UUID_RE.test(id)) return context.json({ error: "Project not found" }, 404);
242
+ const project = await this.#project(tenant.orgId, id);
243
+ return project ? context.json({ project }) : context.json({ error: "Project not found" }, 404);
244
+ }
245
+ }),
246
+ registerApiRoute("/web/factory/projects/:id", {
247
+ method: "PATCH",
248
+ requiresAuth: false,
249
+ handler: async (routeContext) => {
250
+ const context = loose(routeContext);
251
+ const tenant = await this.#resolveTenant(context);
252
+ if ("response" in tenant) return tenant.response;
253
+ const id = context.req.param("id");
254
+ if (!id || !UUID_RE.test(id)) return context.json({ error: "Project not found" }, 404);
255
+ const input = parseUpdateInput(await readJson(context));
256
+ if (!input) return context.json({ error: "invalid_project" }, 400);
257
+ const project = await (await this.#projects()).update({
258
+ orgId: tenant.orgId,
259
+ id,
260
+ input
261
+ });
262
+ return project ? context.json({ project }) : context.json({ error: "Project not found" }, 404);
263
+ }
264
+ }),
265
+ registerApiRoute("/web/factory/projects/:id", {
266
+ method: "DELETE",
267
+ requiresAuth: false,
268
+ handler: async (routeContext) => {
269
+ const context = loose(routeContext);
270
+ const tenant = await this.#resolveTenant(context);
271
+ if ("response" in tenant) return tenant.response;
272
+ const id = context.req.param("id");
273
+ if (!id || !UUID_RE.test(id)) return context.json({ error: "Project not found" }, 404);
274
+ if (!await this.#project(tenant.orgId, id)) return context.json({ error: "Project not found" }, 404);
275
+ for (const handle of await this.#handles()) for (const connection of await handle.connections.list({
276
+ orgId: tenant.orgId,
277
+ factoryProjectId: id
278
+ })) await handle.connections.delete({
279
+ orgId: tenant.orgId,
280
+ id: connection.id
281
+ });
282
+ await (await this.#projects()).delete({
283
+ orgId: tenant.orgId,
284
+ id
285
+ });
286
+ return context.body(null, 204);
287
+ }
288
+ }),
289
+ registerApiRoute("/web/factory/projects/:id/source-control-connections", {
290
+ method: "GET",
291
+ requiresAuth: false,
292
+ handler: async (routeContext) => {
293
+ const context = loose(routeContext);
294
+ const tenant = await this.#resolveTenant(context);
295
+ if ("response" in tenant) return tenant.response;
296
+ const projectId = context.req.param("id");
297
+ if (!projectId || !UUID_RE.test(projectId) || !await this.#project(tenant.orgId, projectId)) return context.json({ error: "Project not found" }, 404);
298
+ const connections = [];
299
+ for (const handle of await this.#handles()) for (const connection of await handle.connections.list({
300
+ orgId: tenant.orgId,
301
+ factoryProjectId: projectId
302
+ })) {
303
+ const installation = await handle.installations.get({
304
+ orgId: tenant.orgId,
305
+ id: connection.installationId
306
+ });
307
+ if (!installation) continue;
308
+ const links = await handle.projectRepositories.list({
309
+ orgId: tenant.orgId,
310
+ connectionId: connection.id
311
+ });
312
+ connections.push({
313
+ ...connection,
314
+ installation,
315
+ repositories: await Promise.all(links.map((link) => this.#repositoryPayload(handle, tenant.orgId, link)))
316
+ });
317
+ }
318
+ return context.json({ connections });
319
+ }
320
+ }),
321
+ registerApiRoute("/web/factory/projects/:id/source-control-connections", {
322
+ method: "POST",
323
+ requiresAuth: false,
324
+ handler: async (routeContext) => {
325
+ const context = loose(routeContext);
326
+ const tenant = await this.#resolveTenant(context);
327
+ if ("response" in tenant) return tenant.response;
328
+ const projectId = context.req.param("id");
329
+ if (!projectId || !UUID_RE.test(projectId) || !await this.#project(tenant.orgId, projectId)) return context.json({ error: "Project not found" }, 404);
330
+ const input = parseConnectionInput(await readJson(context));
331
+ if (!input) return context.json({ error: "invalid_source_control_connection" }, 400);
332
+ if (!this.#versionControlIntegrationIds.has(input.integrationId)) return context.json({ error: "Source-control integration not found" }, 404);
333
+ const handle = (await this.#sourceControl()).forIntegration(input.integrationId);
334
+ if (!await handle.installations.get({
335
+ orgId: tenant.orgId,
336
+ id: input.installationId
337
+ })) return context.json({ error: "Source-control installation not found" }, 404);
338
+ const connection = await handle.connections.create({
339
+ orgId: tenant.orgId,
340
+ factoryProjectId: projectId,
341
+ installationId: input.installationId,
342
+ createdByUserId: tenant.userId
343
+ });
344
+ return context.json({ connection }, 201);
345
+ }
346
+ }),
347
+ registerApiRoute("/web/factory/projects/:id/source-control-connections/:connectionId", {
348
+ method: "DELETE",
349
+ requiresAuth: false,
350
+ handler: async (routeContext) => {
351
+ const context = loose(routeContext);
352
+ const tenant = await this.#resolveTenant(context);
353
+ if ("response" in tenant) return tenant.response;
354
+ const projectId = context.req.param("id");
355
+ const connectionId = context.req.param("connectionId");
356
+ if (!projectId || !UUID_RE.test(projectId) || !connectionId || !UUID_RE.test(connectionId)) return context.json({ error: "Source-control connection not found" }, 404);
357
+ const found = await this.#findConnection({
358
+ orgId: tenant.orgId,
359
+ projectId,
360
+ id: connectionId
361
+ });
362
+ if (!found) return context.json({ error: "Source-control connection not found" }, 404);
363
+ await found.handle.connections.delete({
364
+ orgId: tenant.orgId,
365
+ id: connectionId
366
+ });
367
+ return context.body(null, 204);
368
+ }
369
+ }),
370
+ registerApiRoute("/web/factory/projects/:id/source-control-connections/:connectionId/repositories", {
371
+ method: "POST",
372
+ requiresAuth: false,
373
+ handler: async (routeContext) => {
374
+ const context = loose(routeContext);
375
+ const tenant = await this.#resolveTenant(context);
376
+ if ("response" in tenant) return tenant.response;
377
+ const projectId = context.req.param("id");
378
+ const connectionId = context.req.param("connectionId");
379
+ if (!projectId || !UUID_RE.test(projectId) || !connectionId || !UUID_RE.test(connectionId)) return context.json({ error: "Source-control connection not found" }, 404);
380
+ const found = await this.#findConnection({
381
+ orgId: tenant.orgId,
382
+ projectId,
383
+ id: connectionId
384
+ });
385
+ if (!found) return context.json({ error: "Source-control connection not found" }, 404);
386
+ const input = parseRepositoryLinkInput(await readJson(context));
387
+ if (!input) return context.json({ error: "invalid_project_repository" }, 400);
388
+ const repository = await found.handle.repositories.get({
389
+ orgId: tenant.orgId,
390
+ id: input.repositoryId
391
+ });
392
+ if (!repository || repository.installationId !== found.connection.installationId) return context.json({ error: "Source-control repository not found" }, 404);
393
+ const projectRepository = await found.handle.projectRepositories.link({
394
+ orgId: tenant.orgId,
395
+ connectionId,
396
+ createdByUserId: tenant.userId,
397
+ ...input
398
+ });
399
+ return context.json({ projectRepository: await this.#repositoryPayload(found.handle, tenant.orgId, projectRepository) }, 201);
400
+ }
401
+ }),
402
+ registerApiRoute("/web/factory/projects/:id/repositories/:projectRepositoryId", {
403
+ method: "PATCH",
404
+ requiresAuth: false,
405
+ handler: async (routeContext) => {
406
+ const context = loose(routeContext);
407
+ const tenant = await this.#resolveTenant(context);
408
+ if ("response" in tenant) return tenant.response;
409
+ const projectId = context.req.param("id");
410
+ const projectRepositoryId = context.req.param("projectRepositoryId");
411
+ if (!projectId || !UUID_RE.test(projectId) || !projectRepositoryId || !UUID_RE.test(projectRepositoryId)) return context.json({ error: "Project repository not found" }, 404);
412
+ const found = await this.#findProjectRepository({
413
+ orgId: tenant.orgId,
414
+ projectId,
415
+ id: projectRepositoryId
416
+ });
417
+ if (!found) return context.json({ error: "Project repository not found" }, 404);
418
+ const input = parseRepositoryUpdateInput(await readJson(context));
419
+ if (!input) return context.json({ error: "invalid_project_repository" }, 400);
420
+ const projectRepository = await found.handle.projectRepositories.update({
421
+ orgId: tenant.orgId,
422
+ id: projectRepositoryId,
423
+ input
424
+ });
425
+ return context.json({ projectRepository: await this.#repositoryPayload(found.handle, tenant.orgId, projectRepository) });
426
+ }
427
+ }),
428
+ registerApiRoute("/web/factory/projects/:id/repositories/:projectRepositoryId", {
429
+ method: "DELETE",
430
+ requiresAuth: false,
431
+ handler: async (routeContext) => {
432
+ const context = loose(routeContext);
433
+ const tenant = await this.#resolveTenant(context);
434
+ if ("response" in tenant) return tenant.response;
435
+ const projectId = context.req.param("id");
436
+ const projectRepositoryId = context.req.param("projectRepositoryId");
437
+ if (!projectId || !UUID_RE.test(projectId) || !projectRepositoryId || !UUID_RE.test(projectRepositoryId)) return context.json({ error: "Project repository not found" }, 404);
438
+ const found = await this.#findProjectRepository({
439
+ orgId: tenant.orgId,
440
+ projectId,
441
+ id: projectRepositoryId
442
+ });
443
+ if (!found) return context.json({ error: "Project repository not found" }, 404);
444
+ await found.handle.projectRepositories.unlink({
445
+ orgId: tenant.orgId,
446
+ id: projectRepositoryId
447
+ });
448
+ return context.body(null, 204);
449
+ }
450
+ })
451
+ ];
452
+ }
395
453
  };
454
+ //#endregion
455
+ export { ProjectRoutes };
456
+
396
457
  //# sourceMappingURL=projects.js.map