@mastra/factory 0.0.0-esbuild-bundle-worker-20260807173433

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 (329) hide show
  1. package/CHANGELOG.md +1008 -0
  2. package/LICENSE.md +30 -0
  3. package/README.md +27 -0
  4. package/dist/auth.d.ts +183 -0
  5. package/dist/auth.d.ts.map +1 -0
  6. package/dist/auth.js +507 -0
  7. package/dist/auth.js.map +1 -0
  8. package/dist/capabilities/connection.d.ts +9 -0
  9. package/dist/capabilities/connection.d.ts.map +1 -0
  10. package/dist/capabilities/connection.js +1 -0
  11. package/dist/capabilities/intake.d.ts +141 -0
  12. package/dist/capabilities/intake.d.ts.map +1 -0
  13. package/dist/capabilities/intake.js +1 -0
  14. package/dist/capabilities/version-control.d.ts +247 -0
  15. package/dist/capabilities/version-control.d.ts.map +1 -0
  16. package/dist/capabilities/version-control.js +1 -0
  17. package/dist/factory.d.ts +159 -0
  18. package/dist/factory.d.ts.map +1 -0
  19. package/dist/factory.js +491 -0
  20. package/dist/factory.js.map +1 -0
  21. package/dist/index.d.ts +14 -0
  22. package/dist/index.d.ts.map +1 -0
  23. package/dist/index.js +7 -0
  24. package/dist/integrations/base.d.ts +254 -0
  25. package/dist/integrations/base.d.ts.map +1 -0
  26. package/dist/integrations/base.js +1 -0
  27. package/dist/integrations/github/config.d.ts +60 -0
  28. package/dist/integrations/github/config.d.ts.map +1 -0
  29. package/dist/integrations/github/config.js +42 -0
  30. package/dist/integrations/github/config.js.map +1 -0
  31. package/dist/integrations/github/integration.d.ts +220 -0
  32. package/dist/integrations/github/integration.d.ts.map +1 -0
  33. package/dist/integrations/github/integration.js +1162 -0
  34. package/dist/integrations/github/integration.js.map +1 -0
  35. package/dist/integrations/github/issue-triage.d.ts +27 -0
  36. package/dist/integrations/github/issue-triage.d.ts.map +1 -0
  37. package/dist/integrations/github/issue-triage.js +82 -0
  38. package/dist/integrations/github/issue-triage.js.map +1 -0
  39. package/dist/integrations/github/pat.d.ts +38 -0
  40. package/dist/integrations/github/pat.d.ts.map +1 -0
  41. package/dist/integrations/github/pat.js +59 -0
  42. package/dist/integrations/github/pat.js.map +1 -0
  43. package/dist/integrations/github/provenance.d.ts +27 -0
  44. package/dist/integrations/github/provenance.d.ts.map +1 -0
  45. package/dist/integrations/github/provenance.js +75 -0
  46. package/dist/integrations/github/provenance.js.map +1 -0
  47. package/dist/integrations/github/reconcile-worker.d.ts +41 -0
  48. package/dist/integrations/github/reconcile-worker.d.ts.map +1 -0
  49. package/dist/integrations/github/reconcile-worker.js +125 -0
  50. package/dist/integrations/github/reconcile-worker.js.map +1 -0
  51. package/dist/integrations/github/routes.d.ts +69 -0
  52. package/dist/integrations/github/routes.d.ts.map +1 -0
  53. package/dist/integrations/github/routes.js +1335 -0
  54. package/dist/integrations/github/routes.js.map +1 -0
  55. package/dist/integrations/github/rules.d.ts +105 -0
  56. package/dist/integrations/github/rules.d.ts.map +1 -0
  57. package/dist/integrations/github/rules.js +551 -0
  58. package/dist/integrations/github/rules.js.map +1 -0
  59. package/dist/integrations/github/sandbox-release.d.ts +53 -0
  60. package/dist/integrations/github/sandbox-release.d.ts.map +1 -0
  61. package/dist/integrations/github/sandbox-release.js +115 -0
  62. package/dist/integrations/github/sandbox-release.js.map +1 -0
  63. package/dist/integrations/github/sandbox.d.ts +281 -0
  64. package/dist/integrations/github/sandbox.d.ts.map +1 -0
  65. package/dist/integrations/github/sandbox.js +547 -0
  66. package/dist/integrations/github/sandbox.js.map +1 -0
  67. package/dist/integrations/github/session-subscriptions.d.ts +26 -0
  68. package/dist/integrations/github/session-subscriptions.d.ts.map +1 -0
  69. package/dist/integrations/github/session-subscriptions.js +192 -0
  70. package/dist/integrations/github/session-subscriptions.js.map +1 -0
  71. package/dist/integrations/github/subscriptions.d.ts +54 -0
  72. package/dist/integrations/github/subscriptions.d.ts.map +1 -0
  73. package/dist/integrations/github/subscriptions.js +62 -0
  74. package/dist/integrations/github/subscriptions.js.map +1 -0
  75. package/dist/integrations/github/token-refresh.d.ts +12 -0
  76. package/dist/integrations/github/token-refresh.d.ts.map +1 -0
  77. package/dist/integrations/github/token-refresh.js +24 -0
  78. package/dist/integrations/github/token-refresh.js.map +1 -0
  79. package/dist/integrations/github/webhook.d.ts +107 -0
  80. package/dist/integrations/github/webhook.d.ts.map +1 -0
  81. package/dist/integrations/github/webhook.js +371 -0
  82. package/dist/integrations/github/webhook.js.map +1 -0
  83. package/dist/integrations/linear/agent-tools.d.ts +34 -0
  84. package/dist/integrations/linear/agent-tools.d.ts.map +1 -0
  85. package/dist/integrations/linear/agent-tools.js +85 -0
  86. package/dist/integrations/linear/agent-tools.js.map +1 -0
  87. package/dist/integrations/linear/integration.d.ts +235 -0
  88. package/dist/integrations/linear/integration.d.ts.map +1 -0
  89. package/dist/integrations/linear/integration.js +676 -0
  90. package/dist/integrations/linear/integration.js.map +1 -0
  91. package/dist/integrations/linear/routes.d.ts +61 -0
  92. package/dist/integrations/linear/routes.d.ts.map +1 -0
  93. package/dist/integrations/linear/routes.js +240 -0
  94. package/dist/integrations/linear/routes.js.map +1 -0
  95. package/dist/integrations/linear/rules.d.ts +44 -0
  96. package/dist/integrations/linear/rules.d.ts.map +1 -0
  97. package/dist/integrations/linear/rules.js +133 -0
  98. package/dist/integrations/linear/rules.js.map +1 -0
  99. package/dist/integrations/linear/storage.d.ts +65 -0
  100. package/dist/integrations/linear/storage.d.ts.map +1 -0
  101. package/dist/integrations/linear/storage.js +1 -0
  102. package/dist/integrations/platform/api-client.d.ts +26 -0
  103. package/dist/integrations/platform/api-client.d.ts.map +1 -0
  104. package/dist/integrations/platform/api-client.js +156 -0
  105. package/dist/integrations/platform/api-client.js.map +1 -0
  106. package/dist/integrations/platform/github/event-worker.d.ts +44 -0
  107. package/dist/integrations/platform/github/event-worker.d.ts.map +1 -0
  108. package/dist/integrations/platform/github/event-worker.js +349 -0
  109. package/dist/integrations/platform/github/event-worker.js.map +1 -0
  110. package/dist/integrations/platform/github/integration.d.ts +64 -0
  111. package/dist/integrations/platform/github/integration.d.ts.map +1 -0
  112. package/dist/integrations/platform/github/integration.js +1006 -0
  113. package/dist/integrations/platform/github/integration.js.map +1 -0
  114. package/dist/integrations/platform/linear/integration.d.ts +46 -0
  115. package/dist/integrations/platform/linear/integration.d.ts.map +1 -0
  116. package/dist/integrations/platform/linear/integration.js +483 -0
  117. package/dist/integrations/platform/linear/integration.js.map +1 -0
  118. package/dist/integrations/slack/connect-route.d.ts +46 -0
  119. package/dist/integrations/slack/connect-route.d.ts.map +1 -0
  120. package/dist/integrations/slack/connect-route.js +186 -0
  121. package/dist/integrations/slack/connect-route.js.map +1 -0
  122. package/dist/integrations/slack/integration.d.ts +62 -0
  123. package/dist/integrations/slack/integration.d.ts.map +1 -0
  124. package/dist/integrations/slack/integration.js +68 -0
  125. package/dist/integrations/slack/integration.js.map +1 -0
  126. package/dist/integrations/slack/slack.d.ts +200 -0
  127. package/dist/integrations/slack/slack.d.ts.map +1 -0
  128. package/dist/integrations/slack/slack.js +411 -0
  129. package/dist/integrations/slack/slack.js.map +1 -0
  130. package/dist/integrations/workos/integration.d.ts +39 -0
  131. package/dist/integrations/workos/integration.d.ts.map +1 -0
  132. package/dist/integrations/workos/integration.js +91 -0
  133. package/dist/integrations/workos/integration.js.map +1 -0
  134. package/dist/routes/config.d.ts +235 -0
  135. package/dist/routes/config.d.ts.map +1 -0
  136. package/dist/routes/config.js +972 -0
  137. package/dist/routes/config.js.map +1 -0
  138. package/dist/routes/custom-provider-source.d.ts +52 -0
  139. package/dist/routes/custom-provider-source.d.ts.map +1 -0
  140. package/dist/routes/custom-provider-source.js +124 -0
  141. package/dist/routes/custom-provider-source.js.map +1 -0
  142. package/dist/routes/fs.d.ts +151 -0
  143. package/dist/routes/fs.d.ts.map +1 -0
  144. package/dist/routes/fs.js +708 -0
  145. package/dist/routes/fs.js.map +1 -0
  146. package/dist/routes/intake.d.ts +23 -0
  147. package/dist/routes/intake.d.ts.map +1 -0
  148. package/dist/routes/intake.js +179 -0
  149. package/dist/routes/intake.js.map +1 -0
  150. package/dist/routes/oauth.d.ts +43 -0
  151. package/dist/routes/oauth.d.ts.map +1 -0
  152. package/dist/routes/oauth.js +463 -0
  153. package/dist/routes/oauth.js.map +1 -0
  154. package/dist/routes/projects.d.ts +19 -0
  155. package/dist/routes/projects.d.ts.map +1 -0
  156. package/dist/routes/projects.js +461 -0
  157. package/dist/routes/projects.js.map +1 -0
  158. package/dist/routes/provider-credentials.d.ts +81 -0
  159. package/dist/routes/provider-credentials.d.ts.map +1 -0
  160. package/dist/routes/provider-credentials.js +86 -0
  161. package/dist/routes/provider-credentials.js.map +1 -0
  162. package/dist/routes/route.d.ts +48 -0
  163. package/dist/routes/route.d.ts.map +1 -0
  164. package/dist/routes/route.js +15 -0
  165. package/dist/routes/route.js.map +1 -0
  166. package/dist/routes/skills.d.ts +30 -0
  167. package/dist/routes/skills.d.ts.map +1 -0
  168. package/dist/routes/skills.js +168 -0
  169. package/dist/routes/skills.js.map +1 -0
  170. package/dist/routes/surface.d.ts +107 -0
  171. package/dist/routes/surface.d.ts.map +1 -0
  172. package/dist/routes/surface.js +297 -0
  173. package/dist/routes/surface.js.map +1 -0
  174. package/dist/routes/tenant-credentials.d.ts +74 -0
  175. package/dist/routes/tenant-credentials.d.ts.map +1 -0
  176. package/dist/routes/tenant-credentials.js +156 -0
  177. package/dist/routes/tenant-credentials.js.map +1 -0
  178. package/dist/routes/work-items.d.ts +40 -0
  179. package/dist/routes/work-items.d.ts.map +1 -0
  180. package/dist/routes/work-items.js +695 -0
  181. package/dist/routes/work-items.js.map +1 -0
  182. package/dist/rules/binding-context.d.ts +5 -0
  183. package/dist/rules/binding-context.d.ts.map +1 -0
  184. package/dist/rules/binding-context.js +28 -0
  185. package/dist/rules/binding-context.js.map +1 -0
  186. package/dist/rules/defaults.d.ts +9 -0
  187. package/dist/rules/defaults.d.ts.map +1 -0
  188. package/dist/rules/defaults.js +322 -0
  189. package/dist/rules/defaults.js.map +1 -0
  190. package/dist/rules/dispatcher.d.ts +43 -0
  191. package/dist/rules/dispatcher.d.ts.map +1 -0
  192. package/dist/rules/dispatcher.js +547 -0
  193. package/dist/rules/dispatcher.js.map +1 -0
  194. package/dist/rules/index.d.ts +7 -0
  195. package/dist/rules/index.d.ts.map +1 -0
  196. package/dist/rules/index.js +5 -0
  197. package/dist/rules/processor.d.ts +54 -0
  198. package/dist/rules/processor.d.ts.map +1 -0
  199. package/dist/rules/processor.js +378 -0
  200. package/dist/rules/processor.js.map +1 -0
  201. package/dist/rules/resolve.d.ts +17 -0
  202. package/dist/rules/resolve.d.ts.map +1 -0
  203. package/dist/rules/resolve.js +30 -0
  204. package/dist/rules/resolve.js.map +1 -0
  205. package/dist/rules/start-coordinator.d.ts +60 -0
  206. package/dist/rules/start-coordinator.d.ts.map +1 -0
  207. package/dist/rules/start-coordinator.js +175 -0
  208. package/dist/rules/start-coordinator.js.map +1 -0
  209. package/dist/rules/tools.d.ts +10 -0
  210. package/dist/rules/tools.d.ts.map +1 -0
  211. package/dist/rules/tools.js +59 -0
  212. package/dist/rules/tools.js.map +1 -0
  213. package/dist/rules/transition-service.d.ts +49 -0
  214. package/dist/rules/transition-service.d.ts.map +1 -0
  215. package/dist/rules/transition-service.js +232 -0
  216. package/dist/rules/transition-service.js.map +1 -0
  217. package/dist/rules/types.d.ts +261 -0
  218. package/dist/rules/types.d.ts.map +1 -0
  219. package/dist/rules/types.js +43 -0
  220. package/dist/rules/types.js.map +1 -0
  221. package/dist/rules/validation.d.ts +11 -0
  222. package/dist/rules/validation.d.ts.map +1 -0
  223. package/dist/rules/validation.js +308 -0
  224. package/dist/rules/validation.js.map +1 -0
  225. package/dist/sandbox/fleet.d.ts +222 -0
  226. package/dist/sandbox/fleet.d.ts.map +1 -0
  227. package/dist/sandbox/fleet.js +322 -0
  228. package/dist/sandbox/fleet.js.map +1 -0
  229. package/dist/sandbox/reattach.d.ts +3 -0
  230. package/dist/sandbox/reattach.d.ts.map +1 -0
  231. package/dist/sandbox/reattach.js +16 -0
  232. package/dist/sandbox/reattach.js.map +1 -0
  233. package/dist/server-error.d.ts +12 -0
  234. package/dist/server-error.d.ts.map +1 -0
  235. package/dist/server-error.js +16 -0
  236. package/dist/server-error.js.map +1 -0
  237. package/dist/session/factory-session.d.ts +116 -0
  238. package/dist/session/factory-session.d.ts.map +1 -0
  239. package/dist/session/factory-session.js +162 -0
  240. package/dist/session/factory-session.js.map +1 -0
  241. package/dist/skills/service.d.ts +48 -0
  242. package/dist/skills/service.d.ts.map +1 -0
  243. package/dist/skills/service.js +40 -0
  244. package/dist/skills/service.js.map +1 -0
  245. package/dist/spa-static.d.ts +33 -0
  246. package/dist/spa-static.d.ts.map +1 -0
  247. package/dist/spa-static.js +104 -0
  248. package/dist/spa-static.js.map +1 -0
  249. package/dist/state-signing.d.ts +59 -0
  250. package/dist/state-signing.d.ts.map +1 -0
  251. package/dist/state-signing.js +83 -0
  252. package/dist/state-signing.js.map +1 -0
  253. package/dist/storage/domains/audit/agent-audit.d.ts +27 -0
  254. package/dist/storage/domains/audit/agent-audit.d.ts.map +1 -0
  255. package/dist/storage/domains/audit/agent-audit.js +66 -0
  256. package/dist/storage/domains/audit/agent-audit.js.map +1 -0
  257. package/dist/storage/domains/audit/base.d.ts +140 -0
  258. package/dist/storage/domains/audit/base.d.ts.map +1 -0
  259. package/dist/storage/domains/audit/base.js +184 -0
  260. package/dist/storage/domains/audit/base.js.map +1 -0
  261. package/dist/storage/domains/audit/domain.d.ts +76 -0
  262. package/dist/storage/domains/audit/domain.d.ts.map +1 -0
  263. package/dist/storage/domains/audit/domain.js +269 -0
  264. package/dist/storage/domains/audit/domain.js.map +1 -0
  265. package/dist/storage/domains/channel-identity/base.d.ts +96 -0
  266. package/dist/storage/domains/channel-identity/base.d.ts.map +1 -0
  267. package/dist/storage/domains/channel-identity/base.js +149 -0
  268. package/dist/storage/domains/channel-identity/base.js.map +1 -0
  269. package/dist/storage/domains/credentials/base.d.ts +134 -0
  270. package/dist/storage/domains/credentials/base.d.ts.map +1 -0
  271. package/dist/storage/domains/credentials/base.js +273 -0
  272. package/dist/storage/domains/credentials/base.js.map +1 -0
  273. package/dist/storage/domains/custom-providers/base.d.ts +57 -0
  274. package/dist/storage/domains/custom-providers/base.d.ts.map +1 -0
  275. package/dist/storage/domains/custom-providers/base.js +145 -0
  276. package/dist/storage/domains/custom-providers/base.js.map +1 -0
  277. package/dist/storage/domains/intake/base.d.ts +38 -0
  278. package/dist/storage/domains/intake/base.d.ts.map +1 -0
  279. package/dist/storage/domains/intake/base.js +80 -0
  280. package/dist/storage/domains/intake/base.js.map +1 -0
  281. package/dist/storage/domains/integrations/base.d.ts +126 -0
  282. package/dist/storage/domains/integrations/base.d.ts.map +1 -0
  283. package/dist/storage/domains/integrations/base.js +317 -0
  284. package/dist/storage/domains/integrations/base.js.map +1 -0
  285. package/dist/storage/domains/memory-settings/base.d.ts +62 -0
  286. package/dist/storage/domains/memory-settings/base.d.ts.map +1 -0
  287. package/dist/storage/domains/memory-settings/base.js +126 -0
  288. package/dist/storage/domains/memory-settings/base.js.map +1 -0
  289. package/dist/storage/domains/model-packs/base.d.ts +49 -0
  290. package/dist/storage/domains/model-packs/base.d.ts.map +1 -0
  291. package/dist/storage/domains/model-packs/base.js +96 -0
  292. package/dist/storage/domains/model-packs/base.js.map +1 -0
  293. package/dist/storage/domains/projects/base.d.ts +58 -0
  294. package/dist/storage/domains/projects/base.d.ts.map +1 -0
  295. package/dist/storage/domains/projects/base.js +111 -0
  296. package/dist/storage/domains/projects/base.js.map +1 -0
  297. package/dist/storage/domains/queue-health/base.d.ts +52 -0
  298. package/dist/storage/domains/queue-health/base.d.ts.map +1 -0
  299. package/dist/storage/domains/queue-health/base.js +122 -0
  300. package/dist/storage/domains/queue-health/base.js.map +1 -0
  301. package/dist/storage/domains/source-control/base.d.ts +360 -0
  302. package/dist/storage/domains/source-control/base.d.ts.map +1 -0
  303. package/dist/storage/domains/source-control/base.js +914 -0
  304. package/dist/storage/domains/source-control/base.js.map +1 -0
  305. package/dist/storage/domains/source-control/inmemory.d.ts +175 -0
  306. package/dist/storage/domains/source-control/inmemory.d.ts.map +1 -0
  307. package/dist/storage/domains/source-control/inmemory.js +380 -0
  308. package/dist/storage/domains/source-control/inmemory.js.map +1 -0
  309. package/dist/storage/domains/work-items/base.d.ts +412 -0
  310. package/dist/storage/domains/work-items/base.d.ts.map +1 -0
  311. package/dist/storage/domains/work-items/base.js +1320 -0
  312. package/dist/storage/domains/work-items/base.js.map +1 -0
  313. package/dist/storage/domains/work-items/metrics.d.ts +101 -0
  314. package/dist/storage/domains/work-items/metrics.d.ts.map +1 -0
  315. package/dist/storage/domains/work-items/metrics.js +221 -0
  316. package/dist/storage/domains/work-items/metrics.js.map +1 -0
  317. package/dist/timing.d.ts +15 -0
  318. package/dist/timing.d.ts.map +1 -0
  319. package/dist/timing.js +34 -0
  320. package/dist/timing.js.map +1 -0
  321. package/dist/workspace.d.ts +24 -0
  322. package/dist/workspace.d.ts.map +1 -0
  323. package/dist/workspace.js +287 -0
  324. package/dist/workspace.js.map +1 -0
  325. package/factory-skills/configure-factory-rules/SKILL.md +67 -0
  326. package/factory-skills/factory-plan/SKILL.md +61 -0
  327. package/factory-skills/factory-review/SKILL.md +157 -0
  328. package/factory-skills/factory-triage/SKILL.md +99 -0
  329. package/package.json +83 -0
@@ -0,0 +1,914 @@
1
+ import { FactoryStorageDomain, UniqueViolationError } from "@mastra/core/storage";
2
+ //#region src/storage/domains/source-control/base.ts
3
+ const FACTORY_PROJECTS = "factory_projects";
4
+ const INSTALLATIONS = "source_control_installations";
5
+ const REPOSITORIES = "source_control_repositories";
6
+ const CONNECTIONS = "factory_project_source_control_connections";
7
+ const PROJECT_REPOSITORIES = "factory_project_repositories";
8
+ const SANDBOXES = "source_control_project_repository_sandboxes";
9
+ const SANDBOX_POOL = "source_control_sandbox_pool";
10
+ const WORKTREES = "source_control_worktrees";
11
+ const SESSIONS = "source_control_sessions";
12
+ const SOURCE_CONTROL_SCHEMAS = [
13
+ {
14
+ name: INSTALLATIONS,
15
+ columns: {
16
+ id: { type: "uuid-pk" },
17
+ integration_id: { type: "text" },
18
+ org_id: { type: "text" },
19
+ connected_by_user_id: { type: "text" },
20
+ external_id: { type: "text" },
21
+ account_name: {
22
+ type: "text",
23
+ nullable: true
24
+ },
25
+ account_type: {
26
+ type: "text",
27
+ nullable: true
28
+ },
29
+ provider_metadata: { type: "json" },
30
+ created_at: { type: "timestamp" }
31
+ },
32
+ uniqueIndexes: [{
33
+ name: "source_control_installations_integration_org_external_unique",
34
+ columns: [
35
+ "integration_id",
36
+ "org_id",
37
+ "external_id"
38
+ ]
39
+ }]
40
+ },
41
+ {
42
+ name: REPOSITORIES,
43
+ columns: {
44
+ id: { type: "uuid-pk" },
45
+ installation_id: { type: "text" },
46
+ external_id: { type: "text" },
47
+ slug: { type: "text" },
48
+ default_branch: {
49
+ type: "text",
50
+ default: "main"
51
+ },
52
+ provider_metadata: { type: "json" },
53
+ created_at: { type: "timestamp" },
54
+ updated_at: { type: "timestamp" }
55
+ },
56
+ uniqueIndexes: [{
57
+ name: "source_control_repositories_installation_external_unique",
58
+ columns: ["installation_id", "external_id"]
59
+ }],
60
+ indexes: [{
61
+ name: "source_control_repositories_installation_slug_idx",
62
+ columns: ["installation_id", "slug"]
63
+ }]
64
+ },
65
+ {
66
+ name: CONNECTIONS,
67
+ columns: {
68
+ id: { type: "uuid-pk" },
69
+ factory_project_id: { type: "text" },
70
+ integration_id: { type: "text" },
71
+ installation_id: { type: "text" },
72
+ created_by_user_id: { type: "text" },
73
+ created_at: { type: "timestamp" }
74
+ },
75
+ uniqueIndexes: [{
76
+ name: "factory_project_source_control_connections_project_integration_installation_unique",
77
+ columns: [
78
+ "factory_project_id",
79
+ "integration_id",
80
+ "installation_id"
81
+ ]
82
+ }],
83
+ indexes: [{
84
+ name: "factory_project_source_control_connections_project_idx",
85
+ columns: ["factory_project_id"]
86
+ }]
87
+ },
88
+ {
89
+ name: PROJECT_REPOSITORIES,
90
+ columns: {
91
+ id: { type: "uuid-pk" },
92
+ connection_id: { type: "text" },
93
+ repository_id: { type: "text" },
94
+ created_by_user_id: { type: "text" },
95
+ branch: {
96
+ type: "text",
97
+ nullable: true
98
+ },
99
+ sandbox_provider: { type: "text" },
100
+ sandbox_workdir: { type: "text" },
101
+ setup_command: {
102
+ type: "text",
103
+ nullable: true
104
+ },
105
+ created_at: { type: "timestamp" },
106
+ updated_at: { type: "timestamp" }
107
+ },
108
+ uniqueIndexes: [{
109
+ name: "factory_project_repositories_connection_repository_unique",
110
+ columns: ["connection_id", "repository_id"]
111
+ }],
112
+ indexes: [{
113
+ name: "factory_project_repositories_connection_idx",
114
+ columns: ["connection_id"]
115
+ }]
116
+ },
117
+ {
118
+ name: SANDBOXES,
119
+ columns: {
120
+ id: { type: "uuid-pk" },
121
+ project_repository_id: { type: "text" },
122
+ user_id: { type: "text" },
123
+ sandbox_id: {
124
+ type: "text",
125
+ nullable: true
126
+ },
127
+ sandbox_workdir: { type: "text" },
128
+ materialized_at: {
129
+ type: "timestamp",
130
+ nullable: true
131
+ },
132
+ created_at: { type: "timestamp" }
133
+ },
134
+ uniqueIndexes: [{
135
+ name: "source_control_project_repository_sandboxes_link_user_unique",
136
+ columns: ["project_repository_id", "user_id"]
137
+ }]
138
+ },
139
+ {
140
+ name: SANDBOX_POOL,
141
+ columns: {
142
+ id: { type: "uuid-pk" },
143
+ org_id: { type: "text" },
144
+ project_repository_id: { type: "text" },
145
+ user_id: { type: "text" },
146
+ sandbox_id: { type: "text" },
147
+ sandbox_workdir: { type: "text" },
148
+ released_at: { type: "timestamp" }
149
+ },
150
+ uniqueIndexes: [{
151
+ name: "source_control_sandbox_pool_sandbox_unique",
152
+ columns: ["sandbox_id"]
153
+ }],
154
+ indexes: [{
155
+ name: "source_control_sandbox_pool_repository_user_idx",
156
+ columns: ["project_repository_id", "user_id"]
157
+ }]
158
+ },
159
+ {
160
+ name: WORKTREES,
161
+ columns: {
162
+ id: { type: "uuid-pk" },
163
+ project_repository_id: { type: "text" },
164
+ user_id: { type: "text" },
165
+ branch: { type: "text" },
166
+ base_branch: { type: "text" },
167
+ worktree_path: { type: "text" },
168
+ created_at: { type: "timestamp" }
169
+ },
170
+ uniqueIndexes: [{
171
+ name: "source_control_worktrees_project_repository_user_branch_unique",
172
+ columns: [
173
+ "project_repository_id",
174
+ "user_id",
175
+ "branch"
176
+ ]
177
+ }]
178
+ },
179
+ {
180
+ name: SESSIONS,
181
+ columns: {
182
+ id: { type: "uuid-pk" },
183
+ session_id: { type: "text" },
184
+ project_repository_id: { type: "text" },
185
+ org_id: { type: "text" },
186
+ user_id: { type: "text" },
187
+ branch: { type: "text" },
188
+ base_branch: { type: "text" },
189
+ title: {
190
+ type: "text",
191
+ nullable: true
192
+ },
193
+ sandbox_id: {
194
+ type: "text",
195
+ nullable: true
196
+ },
197
+ sandbox_workdir: {
198
+ type: "text",
199
+ nullable: true
200
+ },
201
+ materialized_at: {
202
+ type: "timestamp",
203
+ nullable: true
204
+ },
205
+ created_at: { type: "timestamp" },
206
+ updated_at: { type: "timestamp" }
207
+ },
208
+ uniqueIndexes: [{
209
+ name: "source_control_sessions_session_id_unique",
210
+ columns: ["session_id"]
211
+ }, {
212
+ name: "source_control_sessions_repository_user_branch_unique",
213
+ columns: [
214
+ "project_repository_id",
215
+ "user_id",
216
+ "branch"
217
+ ]
218
+ }]
219
+ }
220
+ ];
221
+ function toInstallation(row) {
222
+ return {
223
+ id: row.id,
224
+ integrationId: row.integration_id,
225
+ orgId: row.org_id,
226
+ connectedByUserId: row.connected_by_user_id,
227
+ externalId: row.external_id,
228
+ accountName: row.account_name,
229
+ accountType: row.account_type,
230
+ providerMetadata: row.provider_metadata,
231
+ createdAt: row.created_at
232
+ };
233
+ }
234
+ function toRepository(row) {
235
+ return {
236
+ id: row.id,
237
+ installationId: row.installation_id,
238
+ externalId: row.external_id,
239
+ slug: row.slug,
240
+ defaultBranch: row.default_branch,
241
+ providerMetadata: row.provider_metadata,
242
+ createdAt: row.created_at,
243
+ updatedAt: row.updated_at
244
+ };
245
+ }
246
+ function toConnection(row) {
247
+ return {
248
+ id: row.id,
249
+ factoryProjectId: row.factory_project_id,
250
+ integrationId: row.integration_id,
251
+ installationId: row.installation_id,
252
+ createdByUserId: row.created_by_user_id,
253
+ createdAt: row.created_at
254
+ };
255
+ }
256
+ function toProjectRepository(row) {
257
+ return {
258
+ id: row.id,
259
+ connectionId: row.connection_id,
260
+ repositoryId: row.repository_id,
261
+ createdByUserId: row.created_by_user_id,
262
+ branch: row.branch,
263
+ sandboxProvider: row.sandbox_provider,
264
+ sandboxWorkdir: row.sandbox_workdir,
265
+ setupCommand: row.setup_command,
266
+ createdAt: row.created_at,
267
+ updatedAt: row.updated_at
268
+ };
269
+ }
270
+ function toSandbox(row) {
271
+ return {
272
+ id: row.id,
273
+ projectRepositoryId: row.project_repository_id,
274
+ userId: row.user_id,
275
+ sandboxId: row.sandbox_id,
276
+ sandboxWorkdir: row.sandbox_workdir,
277
+ materializedAt: row.materialized_at,
278
+ createdAt: row.created_at
279
+ };
280
+ }
281
+ function toPooledSandbox(row) {
282
+ return {
283
+ id: row.id,
284
+ orgId: row.org_id,
285
+ projectRepositoryId: row.project_repository_id,
286
+ userId: row.user_id,
287
+ sandboxId: row.sandbox_id,
288
+ sandboxWorkdir: row.sandbox_workdir,
289
+ releasedAt: row.released_at
290
+ };
291
+ }
292
+ function toWorktree(row) {
293
+ return {
294
+ id: row.id,
295
+ projectRepositoryId: row.project_repository_id,
296
+ userId: row.user_id,
297
+ branch: row.branch,
298
+ baseBranch: row.base_branch,
299
+ worktreePath: row.worktree_path,
300
+ createdAt: row.created_at
301
+ };
302
+ }
303
+ function toSession(row) {
304
+ return {
305
+ id: row.id,
306
+ sessionId: row.session_id,
307
+ projectRepositoryId: row.project_repository_id,
308
+ orgId: row.org_id,
309
+ userId: row.user_id,
310
+ branch: row.branch,
311
+ title: row.title,
312
+ baseBranch: row.base_branch,
313
+ sandboxId: row.sandbox_id,
314
+ sandboxWorkdir: row.sandbox_workdir,
315
+ materializedAt: row.materialized_at,
316
+ createdAt: row.created_at,
317
+ updatedAt: row.updated_at
318
+ };
319
+ }
320
+ var SourceControlStorage = class extends FactoryStorageDomain {
321
+ constructor() {
322
+ super("source-control");
323
+ }
324
+ async init() {
325
+ await this.ensureCollections(SOURCE_CONTROL_SCHEMAS);
326
+ }
327
+ async dangerouslyClearAll() {
328
+ await this.ops.deleteMany(SESSIONS, {});
329
+ await this.ops.deleteMany(WORKTREES, {});
330
+ await this.ops.deleteMany(SANDBOX_POOL, {});
331
+ await this.ops.deleteMany(SANDBOXES, {});
332
+ await this.ops.deleteMany(PROJECT_REPOSITORIES, {});
333
+ await this.ops.deleteMany(CONNECTIONS, {});
334
+ await this.ops.deleteMany(REPOSITORIES, {});
335
+ await this.ops.deleteMany(INSTALLATIONS, {});
336
+ }
337
+ forIntegration(integrationId) {
338
+ if (!integrationId.trim()) throw new Error("[SourceControlStorage] integrationId must not be empty.");
339
+ const db = () => this.ops;
340
+ const getInstallation = async (args) => {
341
+ const row = await db().findOne(INSTALLATIONS, {
342
+ id: args.id,
343
+ integration_id: integrationId,
344
+ org_id: args.orgId
345
+ });
346
+ return row ? toInstallation(row) : null;
347
+ };
348
+ const requireInstallation = async (args) => {
349
+ const installation = await getInstallation(args);
350
+ if (!installation) throw new Error("Source-control installation not found for this organization and integration.");
351
+ return installation;
352
+ };
353
+ const getRepository = async (args) => {
354
+ const row = await db().findOne(REPOSITORIES, { id: args.id });
355
+ if (!row || !await getInstallation({
356
+ orgId: args.orgId,
357
+ id: row.installation_id
358
+ })) return null;
359
+ return toRepository(row);
360
+ };
361
+ const requireRepository = async (args) => {
362
+ const repository = await getRepository(args);
363
+ if (!repository) throw new Error("Source-control repository not found for this organization and integration.");
364
+ return repository;
365
+ };
366
+ const getConnection = async (args) => {
367
+ const row = await db().findOne(CONNECTIONS, {
368
+ id: args.id,
369
+ integration_id: integrationId
370
+ });
371
+ if (!row) return null;
372
+ if (!await db().findOne(FACTORY_PROJECTS, {
373
+ id: row.factory_project_id,
374
+ org_id: args.orgId
375
+ }) || !await getInstallation({
376
+ orgId: args.orgId,
377
+ id: row.installation_id
378
+ })) return null;
379
+ return toConnection(row);
380
+ };
381
+ const requireConnection = async (args) => {
382
+ const connection = await getConnection(args);
383
+ if (!connection) throw new Error("Project source-control connection not found for this organization and integration.");
384
+ return connection;
385
+ };
386
+ const getProjectRepository = async (args) => {
387
+ const row = await db().findOne(PROJECT_REPOSITORIES, { id: args.id });
388
+ if (!row || !await getConnection({
389
+ orgId: args.orgId,
390
+ id: row.connection_id
391
+ })) return null;
392
+ return toProjectRepository(row);
393
+ };
394
+ const getProjectRepositoryById = async (id) => {
395
+ const row = await db().findOne(PROJECT_REPOSITORIES, { id });
396
+ if (!row) return null;
397
+ return await db().findOne(CONNECTIONS, {
398
+ id: row.connection_id,
399
+ integration_id: integrationId
400
+ }) ? toProjectRepository(row) : null;
401
+ };
402
+ const requireProjectRepositoryById = async (id) => {
403
+ const projectRepository = await getProjectRepositoryById(id);
404
+ if (!projectRepository) throw new Error("Project repository not found for this integration.");
405
+ return projectRepository;
406
+ };
407
+ const getSandbox = async (id) => {
408
+ const row = await db().findOne(SANDBOXES, { id });
409
+ if (!row || !await getProjectRepositoryById(row.project_repository_id)) return null;
410
+ return toSandbox(row);
411
+ };
412
+ const requireSandbox = async (id) => {
413
+ const sandbox = await getSandbox(id);
414
+ if (!sandbox) throw new Error("Project-repository sandbox not found for this integration.");
415
+ return sandbox;
416
+ };
417
+ return {
418
+ integrationId,
419
+ installations: {
420
+ list: async ({ orgId }) => (await db().findMany(INSTALLATIONS, {
421
+ integration_id: integrationId,
422
+ org_id: orgId
423
+ })).map(toInstallation),
424
+ get: getInstallation,
425
+ findByExternalId: async ({ orgId, externalId }) => {
426
+ const row = await db().findOne(INSTALLATIONS, {
427
+ integration_id: integrationId,
428
+ org_id: orgId,
429
+ external_id: externalId
430
+ });
431
+ return row ? toInstallation(row) : null;
432
+ },
433
+ upsert: async (input) => {
434
+ return toInstallation(await db().upsertOne(INSTALLATIONS, [
435
+ "integration_id",
436
+ "org_id",
437
+ "external_id"
438
+ ], {
439
+ integration_id: integrationId,
440
+ org_id: input.orgId,
441
+ connected_by_user_id: input.connectedByUserId,
442
+ external_id: input.externalId,
443
+ account_name: input.accountName ?? null,
444
+ account_type: input.accountType ?? null,
445
+ provider_metadata: input.providerMetadata ?? {},
446
+ created_at: /* @__PURE__ */ new Date()
447
+ }));
448
+ },
449
+ delete: async ({ orgId, id }) => {
450
+ if (!await getInstallation({
451
+ orgId,
452
+ id
453
+ })) return false;
454
+ await db().deleteMany(INSTALLATIONS, {
455
+ id,
456
+ integration_id: integrationId,
457
+ org_id: orgId
458
+ });
459
+ return true;
460
+ }
461
+ },
462
+ repositories: {
463
+ list: async ({ orgId, installationId }) => {
464
+ await requireInstallation({
465
+ orgId,
466
+ id: installationId
467
+ });
468
+ return (await db().findMany(REPOSITORIES, { installation_id: installationId })).map(toRepository);
469
+ },
470
+ get: getRepository,
471
+ findByExternalId: async ({ orgId, externalId }) => {
472
+ const rows = await db().findMany(REPOSITORIES, { external_id: externalId });
473
+ for (const row of rows) if (await getInstallation({
474
+ orgId,
475
+ id: row.installation_id
476
+ })) return toRepository(row);
477
+ return null;
478
+ },
479
+ findBySlug: async ({ orgId, installationId, slug }) => {
480
+ await requireInstallation({
481
+ orgId,
482
+ id: installationId
483
+ });
484
+ const row = await db().findOne(REPOSITORIES, {
485
+ installation_id: installationId,
486
+ slug
487
+ });
488
+ return row ? toRepository(row) : null;
489
+ },
490
+ upsert: async ({ orgId, input }) => {
491
+ await requireInstallation({
492
+ orgId,
493
+ id: input.installationId
494
+ });
495
+ const now = /* @__PURE__ */ new Date();
496
+ return toRepository(await db().upsertOne(REPOSITORIES, ["installation_id", "external_id"], {
497
+ installation_id: input.installationId,
498
+ external_id: input.externalId,
499
+ slug: input.slug,
500
+ default_branch: input.defaultBranch,
501
+ provider_metadata: input.providerMetadata ?? {},
502
+ created_at: now,
503
+ updated_at: now
504
+ }));
505
+ },
506
+ migrateInstallation: async ({ orgId, id, newInstallationId }) => {
507
+ const existing = await getRepository({
508
+ orgId,
509
+ id
510
+ });
511
+ if (!existing) throw new Error(`Repository ${id} not found in organization ${orgId}`);
512
+ await requireInstallation({
513
+ orgId,
514
+ id: newInstallationId
515
+ });
516
+ try {
517
+ await db().updateMany(REPOSITORIES, { id }, {
518
+ installation_id: newInstallationId,
519
+ updated_at: /* @__PURE__ */ new Date()
520
+ });
521
+ await db().updateMany(CONNECTIONS, { installation_id: existing.installationId }, { installation_id: newInstallationId });
522
+ const updated = await getRepository({
523
+ orgId,
524
+ id
525
+ });
526
+ if (!updated) throw new Error("Repository disappeared after update");
527
+ return updated;
528
+ } catch (error) {
529
+ if (!(error instanceof UniqueViolationError)) throw error;
530
+ const conflictRow = await db().findOne(REPOSITORIES, {
531
+ installation_id: newInstallationId,
532
+ external_id: existing.externalId
533
+ });
534
+ if (!conflictRow) throw error;
535
+ return toRepository(conflictRow);
536
+ }
537
+ }
538
+ },
539
+ connections: {
540
+ list: async ({ orgId, factoryProjectId }) => {
541
+ if (!await db().findOne(FACTORY_PROJECTS, {
542
+ id: factoryProjectId,
543
+ org_id: orgId
544
+ })) return [];
545
+ return (await db().findMany(CONNECTIONS, {
546
+ factory_project_id: factoryProjectId,
547
+ integration_id: integrationId
548
+ })).map(toConnection);
549
+ },
550
+ get: getConnection,
551
+ create: async (input) => {
552
+ if (!await db().findOne(FACTORY_PROJECTS, {
553
+ id: input.factoryProjectId,
554
+ org_id: input.orgId
555
+ })) throw new Error("Factory project not found for this organization.");
556
+ await requireInstallation({
557
+ orgId: input.orgId,
558
+ id: input.installationId
559
+ });
560
+ try {
561
+ return toConnection(await db().insertOne(CONNECTIONS, {
562
+ factory_project_id: input.factoryProjectId,
563
+ integration_id: integrationId,
564
+ installation_id: input.installationId,
565
+ created_by_user_id: input.createdByUserId,
566
+ created_at: /* @__PURE__ */ new Date()
567
+ }));
568
+ } catch (error) {
569
+ if (!(error instanceof UniqueViolationError)) throw error;
570
+ const row = await db().findOne(CONNECTIONS, {
571
+ factory_project_id: input.factoryProjectId,
572
+ integration_id: integrationId,
573
+ installation_id: input.installationId
574
+ });
575
+ if (!row) throw error;
576
+ return toConnection(row);
577
+ }
578
+ },
579
+ delete: async ({ orgId, id }) => {
580
+ if (!await getConnection({
581
+ orgId,
582
+ id
583
+ })) return false;
584
+ const projectRepositories = await db().findMany(PROJECT_REPOSITORIES, { connection_id: id });
585
+ for (const projectRepository of projectRepositories) {
586
+ await db().deleteMany(SESSIONS, { project_repository_id: projectRepository.id });
587
+ await db().deleteMany(WORKTREES, { project_repository_id: projectRepository.id });
588
+ await db().deleteMany(SANDBOX_POOL, { project_repository_id: projectRepository.id });
589
+ await db().deleteMany(SANDBOXES, { project_repository_id: projectRepository.id });
590
+ }
591
+ await db().deleteMany(PROJECT_REPOSITORIES, { connection_id: id });
592
+ await db().deleteMany(CONNECTIONS, {
593
+ id,
594
+ integration_id: integrationId
595
+ });
596
+ return true;
597
+ }
598
+ },
599
+ projectRepositories: {
600
+ list: async ({ orgId, connectionId }) => {
601
+ await requireConnection({
602
+ orgId,
603
+ id: connectionId
604
+ });
605
+ return (await db().findMany(PROJECT_REPOSITORIES, { connection_id: connectionId })).map(toProjectRepository);
606
+ },
607
+ listConfiguredExternalKeys: async () => {
608
+ const keys = /* @__PURE__ */ new Map();
609
+ const connections = await db().findMany(CONNECTIONS, { integration_id: integrationId });
610
+ for (const connection of connections) {
611
+ const installation = await db().findOne(INSTALLATIONS, {
612
+ id: connection.installation_id,
613
+ integration_id: integrationId
614
+ });
615
+ if (!installation) continue;
616
+ const links = await db().findMany(PROJECT_REPOSITORIES, { connection_id: connection.id });
617
+ for (const link of links) {
618
+ const repository = await db().findOne(REPOSITORIES, { id: link.repository_id });
619
+ if (!repository) continue;
620
+ keys.set(`${installation.external_id}\u0000${repository.external_id}`, {
621
+ installationExternalId: installation.external_id,
622
+ repositoryExternalId: repository.external_id
623
+ });
624
+ }
625
+ }
626
+ return [...keys.values()];
627
+ },
628
+ listByExternalRepository: async ({ installationExternalId, repositoryExternalId }) => {
629
+ const targets = [];
630
+ const installations = await db().findMany(INSTALLATIONS, {
631
+ integration_id: integrationId,
632
+ external_id: installationExternalId
633
+ });
634
+ for (const installation of installations) {
635
+ const repository = await db().findOne(REPOSITORIES, {
636
+ installation_id: installation.id,
637
+ external_id: repositoryExternalId
638
+ });
639
+ if (!repository) continue;
640
+ const links = await db().findMany(PROJECT_REPOSITORIES, { repository_id: repository.id });
641
+ for (const link of links) {
642
+ const connection = await db().findOne(CONNECTIONS, {
643
+ id: link.connection_id,
644
+ integration_id: integrationId,
645
+ installation_id: installation.id
646
+ });
647
+ if (!connection) continue;
648
+ if (!await db().findOne(FACTORY_PROJECTS, {
649
+ id: connection.factory_project_id,
650
+ org_id: installation.org_id
651
+ })) continue;
652
+ targets.push({
653
+ orgId: installation.org_id,
654
+ factoryProjectId: connection.factory_project_id,
655
+ projectRepository: toProjectRepository(link)
656
+ });
657
+ }
658
+ }
659
+ return targets;
660
+ },
661
+ get: getProjectRepository,
662
+ link: async (input) => {
663
+ const connection = await requireConnection({
664
+ orgId: input.orgId,
665
+ id: input.connectionId
666
+ });
667
+ if ((await requireRepository({
668
+ orgId: input.orgId,
669
+ id: input.repositoryId
670
+ })).installationId !== connection.installationId) throw new Error("Repository does not belong to the connection installation.");
671
+ const now = /* @__PURE__ */ new Date();
672
+ return toProjectRepository(await db().upsertOne(PROJECT_REPOSITORIES, ["connection_id", "repository_id"], {
673
+ connection_id: input.connectionId,
674
+ repository_id: input.repositoryId,
675
+ created_by_user_id: input.createdByUserId,
676
+ branch: input.branch ?? null,
677
+ sandbox_provider: input.sandboxProvider,
678
+ sandbox_workdir: input.sandboxWorkdir,
679
+ setup_command: input.setupCommand ?? null,
680
+ created_at: now,
681
+ updated_at: now
682
+ }));
683
+ },
684
+ update: async ({ orgId, id, input }) => {
685
+ if (!await getProjectRepository({
686
+ orgId,
687
+ id
688
+ })) return null;
689
+ const patch = { updated_at: /* @__PURE__ */ new Date() };
690
+ if (input.branch !== void 0) patch.branch = input.branch;
691
+ if (input.sandboxProvider !== void 0) patch.sandbox_provider = input.sandboxProvider;
692
+ if (input.sandboxWorkdir !== void 0) patch.sandbox_workdir = input.sandboxWorkdir;
693
+ if (input.setupCommand !== void 0) patch.setup_command = input.setupCommand;
694
+ await db().updateMany(PROJECT_REPOSITORIES, { id }, patch);
695
+ return getProjectRepository({
696
+ orgId,
697
+ id
698
+ });
699
+ },
700
+ unlink: async ({ orgId, id }) => {
701
+ if (!await getProjectRepository({
702
+ orgId,
703
+ id
704
+ })) return false;
705
+ await db().deleteMany(SESSIONS, { project_repository_id: id });
706
+ await db().deleteMany(WORKTREES, { project_repository_id: id });
707
+ await db().deleteMany(SANDBOX_POOL, { project_repository_id: id });
708
+ await db().deleteMany(SANDBOXES, { project_repository_id: id });
709
+ await db().deleteMany(PROJECT_REPOSITORIES, { id });
710
+ return true;
711
+ }
712
+ },
713
+ sandboxes: {
714
+ getOrCreate: async ({ projectRepository, userId }) => {
715
+ await requireProjectRepositoryById(projectRepository.id);
716
+ const where = {
717
+ project_repository_id: projectRepository.id,
718
+ user_id: userId
719
+ };
720
+ const existing = await db().findOne(SANDBOXES, where);
721
+ if (existing) return toSandbox(existing);
722
+ try {
723
+ return toSandbox(await db().insertOne(SANDBOXES, {
724
+ ...where,
725
+ sandbox_id: null,
726
+ sandbox_workdir: projectRepository.sandboxWorkdir,
727
+ materialized_at: null,
728
+ created_at: /* @__PURE__ */ new Date()
729
+ }));
730
+ } catch (error) {
731
+ if (!(error instanceof UniqueViolationError)) throw error;
732
+ const row = await db().findOne(SANDBOXES, where);
733
+ if (!row) throw error;
734
+ return toSandbox(row);
735
+ }
736
+ },
737
+ getById: ({ id }) => getSandbox(id),
738
+ setWorkdir: async ({ id, sandboxWorkdir }) => {
739
+ await requireSandbox(id);
740
+ await db().updateMany(SANDBOXES, { id }, {
741
+ sandbox_workdir: sandboxWorkdir,
742
+ materialized_at: null
743
+ });
744
+ },
745
+ setSandboxId: async ({ id, sandboxId }) => {
746
+ await requireSandbox(id);
747
+ await db().updateMany(SANDBOXES, { id }, { sandbox_id: sandboxId });
748
+ },
749
+ clearBinding: async ({ id }) => {
750
+ await requireSandbox(id);
751
+ await db().updateMany(SANDBOXES, { id }, {
752
+ sandbox_id: null,
753
+ materialized_at: null
754
+ });
755
+ },
756
+ markMaterialized: async ({ id }) => {
757
+ await requireSandbox(id);
758
+ await db().updateMany(SANDBOXES, { id }, { materialized_at: /* @__PURE__ */ new Date() });
759
+ }
760
+ },
761
+ sandboxPool: {
762
+ release: async (input) => {
763
+ if (!await getProjectRepositoryById(input.projectRepositoryId)) return;
764
+ try {
765
+ await db().insertOne(SANDBOX_POOL, {
766
+ org_id: input.orgId,
767
+ project_repository_id: input.projectRepositoryId,
768
+ user_id: input.userId,
769
+ sandbox_id: input.sandboxId,
770
+ sandbox_workdir: input.sandboxWorkdir,
771
+ released_at: /* @__PURE__ */ new Date()
772
+ });
773
+ } catch (error) {
774
+ if (!(error instanceof UniqueViolationError)) throw error;
775
+ }
776
+ },
777
+ claim: async ({ projectRepositoryId }) => {
778
+ if (!await getProjectRepositoryById(projectRepositoryId)) return null;
779
+ const rows = await db().findMany(SANDBOX_POOL, { project_repository_id: projectRepositoryId });
780
+ rows.sort((left, right) => right.released_at.getTime() - left.released_at.getTime());
781
+ for (const row of rows) if (await db().deleteMany(SANDBOX_POOL, { id: row.id }) === 1) return toPooledSandbox(row);
782
+ return null;
783
+ }
784
+ },
785
+ worktrees: {
786
+ upsert: async (input) => {
787
+ await requireProjectRepositoryById(input.projectRepositoryId);
788
+ await db().upsertOne(WORKTREES, [
789
+ "project_repository_id",
790
+ "user_id",
791
+ "branch"
792
+ ], {
793
+ project_repository_id: input.projectRepositoryId,
794
+ user_id: input.userId,
795
+ branch: input.branch,
796
+ base_branch: input.baseBranch,
797
+ worktree_path: input.worktreePath,
798
+ created_at: /* @__PURE__ */ new Date()
799
+ });
800
+ },
801
+ list: async ({ projectRepositoryId, userId }) => {
802
+ if (!await getProjectRepositoryById(projectRepositoryId)) return [];
803
+ return (await db().findMany(WORKTREES, {
804
+ project_repository_id: projectRepositoryId,
805
+ user_id: userId
806
+ })).map(toWorktree);
807
+ },
808
+ get: async ({ projectRepositoryId, userId, branch }) => {
809
+ if (!await getProjectRepositoryById(projectRepositoryId)) return null;
810
+ const row = await db().findOne(WORKTREES, {
811
+ project_repository_id: projectRepositoryId,
812
+ user_id: userId,
813
+ branch
814
+ });
815
+ return row ? toWorktree(row) : null;
816
+ },
817
+ findByPath: async ({ projectRepositoryId, userId, worktreePath }) => {
818
+ if (!await getProjectRepositoryById(projectRepositoryId)) return null;
819
+ const row = await db().findOne(WORKTREES, {
820
+ project_repository_id: projectRepositoryId,
821
+ user_id: userId,
822
+ worktree_path: worktreePath
823
+ });
824
+ return row ? toWorktree(row) : null;
825
+ },
826
+ delete: async ({ projectRepositoryId, userId, branch }) => {
827
+ await requireProjectRepositoryById(projectRepositoryId);
828
+ await db().deleteMany(WORKTREES, {
829
+ project_repository_id: projectRepositoryId,
830
+ user_id: userId,
831
+ branch
832
+ });
833
+ }
834
+ },
835
+ sessions: {
836
+ list: async ({ projectRepositoryId, userId }) => {
837
+ if (!await getProjectRepositoryById(projectRepositoryId)) return [];
838
+ return (await db().findMany(SESSIONS, {
839
+ project_repository_id: projectRepositoryId,
840
+ user_id: userId
841
+ })).map(toSession);
842
+ },
843
+ getBySessionId: async (sessionId) => {
844
+ const row = await db().findOne(SESSIONS, { session_id: sessionId });
845
+ return row && await getProjectRepositoryById(row.project_repository_id) ? toSession(row) : null;
846
+ },
847
+ getForBranch: async ({ projectRepositoryId, userId, branch }) => {
848
+ if (!await getProjectRepositoryById(projectRepositoryId)) return null;
849
+ const row = await db().findOne(SESSIONS, {
850
+ project_repository_id: projectRepositoryId,
851
+ user_id: userId,
852
+ branch
853
+ });
854
+ return row ? toSession(row) : null;
855
+ },
856
+ create: async (input) => {
857
+ await requireProjectRepositoryById(input.projectRepositoryId);
858
+ const existing = await db().findOne(SESSIONS, {
859
+ project_repository_id: input.projectRepositoryId,
860
+ user_id: input.userId,
861
+ branch: input.branch
862
+ });
863
+ if (existing) return toSession(existing);
864
+ const now = /* @__PURE__ */ new Date();
865
+ try {
866
+ return toSession(await db().insertOne(SESSIONS, {
867
+ session_id: input.sessionId,
868
+ project_repository_id: input.projectRepositoryId,
869
+ org_id: input.orgId,
870
+ user_id: input.userId,
871
+ branch: input.branch,
872
+ title: input.title ?? null,
873
+ base_branch: input.baseBranch,
874
+ sandbox_id: null,
875
+ sandbox_workdir: null,
876
+ materialized_at: null,
877
+ created_at: now,
878
+ updated_at: now
879
+ }));
880
+ } catch (error) {
881
+ if (!(error instanceof UniqueViolationError)) throw error;
882
+ const row = await db().findOne(SESSIONS, {
883
+ project_repository_id: input.projectRepositoryId,
884
+ user_id: input.userId,
885
+ branch: input.branch
886
+ });
887
+ if (!row) throw error;
888
+ return toSession(row);
889
+ }
890
+ },
891
+ setSandbox: async ({ id, sandboxId, sandboxWorkdir }) => {
892
+ await db().updateMany(SESSIONS, { id }, {
893
+ sandbox_id: sandboxId,
894
+ sandbox_workdir: sandboxWorkdir,
895
+ updated_at: /* @__PURE__ */ new Date()
896
+ });
897
+ },
898
+ markMaterialized: async ({ id }) => {
899
+ await db().updateMany(SESSIONS, { id }, {
900
+ materialized_at: /* @__PURE__ */ new Date(),
901
+ updated_at: /* @__PURE__ */ new Date()
902
+ });
903
+ },
904
+ delete: async (id) => {
905
+ await db().deleteMany(SESSIONS, { id });
906
+ }
907
+ }
908
+ };
909
+ }
910
+ };
911
+ //#endregion
912
+ export { SOURCE_CONTROL_SCHEMAS, SourceControlStorage };
913
+
914
+ //# sourceMappingURL=base.js.map