@myrialabs/clopen 0.4.38 → 0.4.40

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 (177) hide show
  1. package/backend/auth/permissions.ts +48 -0
  2. package/backend/chat/stream-manager.ts +1 -1
  3. package/backend/database/crypto/secret-columns.ts +9 -1
  4. package/backend/database/migrations/075_create_deploy_bindings.ts +54 -0
  5. package/backend/database/migrations/076_remove_memory_code_graph.ts +229 -0
  6. package/backend/database/migrations/077_create_integration_db_links.ts +150 -0
  7. package/backend/database/migrations/078_create_worktree_branch_tables.ts +158 -0
  8. package/backend/database/migrations/index.ts +28 -0
  9. package/backend/database/queries/db-client-connection-queries.ts +19 -0
  10. package/backend/database/queries/deploy-queries.ts +124 -0
  11. package/backend/database/queries/graph-queries.ts +291 -247
  12. package/backend/database/queries/index.ts +10 -0
  13. package/backend/database/queries/integration-db-link-queries.ts +168 -0
  14. package/backend/database/queries/integration-queries.ts +34 -6
  15. package/backend/database/queries/worktree-branch-queries.ts +331 -0
  16. package/backend/db-client/cloning.test.ts +161 -0
  17. package/backend/db-client/cloning.ts +672 -0
  18. package/backend/db-client/connection-url.test.ts +117 -0
  19. package/backend/db-client/connection-url.ts +126 -0
  20. package/backend/db-client/drivers/mongodb.ts +2 -14
  21. package/backend/db-client/drivers/mysql.ts +2 -8
  22. package/backend/db-client/drivers/postgres.ts +10 -13
  23. package/backend/db-client/drivers/redis.ts +2 -6
  24. package/backend/db-client/env/detect.ts +438 -0
  25. package/backend/db-client/env/diff.ts +94 -0
  26. package/backend/db-client/env/env.test.ts +574 -0
  27. package/backend/db-client/env/index.ts +15 -0
  28. package/backend/db-client/env/naming.ts +202 -0
  29. package/backend/db-client/env/service.ts +487 -0
  30. package/backend/db-client/integrations/index.ts +44 -0
  31. package/backend/db-client/integrations/links.ts +564 -0
  32. package/backend/db-client/integrations/projector.test.ts +280 -0
  33. package/backend/db-client/integrations/projector.ts +256 -0
  34. package/backend/db-client/integrations/supabase-service.ts +266 -0
  35. package/backend/db-client/providers/neon/adapter.ts +522 -0
  36. package/backend/db-client/providers/neon/api-types.ts +182 -0
  37. package/backend/db-client/providers/neon/client.ts +298 -0
  38. package/backend/db-client/providers/neon/endpoints.test.ts +201 -0
  39. package/backend/db-client/providers/neon/endpoints.ts +314 -0
  40. package/backend/db-client/providers/neon/list.test.ts +181 -0
  41. package/backend/db-client/providers/registry.ts +27 -0
  42. package/backend/db-client/providers/supabase/adapter.ts +523 -0
  43. package/backend/db-client/providers/supabase/api-types.ts +92 -0
  44. package/backend/db-client/providers/supabase/apply-migration.ts +104 -0
  45. package/backend/db-client/providers/supabase/client.ts +259 -0
  46. package/backend/db-client/providers/supabase/features.ts +502 -0
  47. package/backend/db-client/providers/supabase/local.test.ts +78 -0
  48. package/backend/db-client/providers/supabase/local.ts +137 -0
  49. package/backend/db-client/providers/types.ts +194 -0
  50. package/backend/deployments/bindings.test.ts +43 -0
  51. package/backend/deployments/bindings.ts +216 -0
  52. package/backend/deployments/index.ts +53 -0
  53. package/backend/deployments/log-streams.ts +214 -0
  54. package/backend/deployments/prompts.ts +70 -0
  55. package/backend/deployments/providers/vercel/adapter.ts +757 -0
  56. package/backend/deployments/providers/vercel/build-logs.test.ts +80 -0
  57. package/backend/deployments/providers/vercel/build-logs.ts +211 -0
  58. package/backend/deployments/providers/vercel/client.ts +203 -0
  59. package/backend/deployments/providers/vercel/list.test.ts +624 -0
  60. package/backend/deployments/providers/vercel/normalise.test.ts +115 -0
  61. package/backend/deployments/providers/vercel/normalise.ts +113 -0
  62. package/backend/deployments/providers/vercel/types.ts +104 -0
  63. package/backend/deployments/registry.ts +27 -0
  64. package/backend/deployments/service.ts +458 -0
  65. package/backend/deployments/types.ts +278 -0
  66. package/backend/env-files/discovery.ts +48 -0
  67. package/backend/env-files/edit.ts +213 -0
  68. package/backend/env-files/env-files.test.ts +515 -0
  69. package/backend/env-files/git.ts +27 -0
  70. package/backend/env-files/index.ts +44 -0
  71. package/backend/env-files/keys.ts +69 -0
  72. package/backend/env-files/parse.ts +157 -0
  73. package/backend/env-files/write.ts +229 -0
  74. package/backend/index.ts +4 -0
  75. package/backend/integrations/health.ts +18 -4
  76. package/backend/integrations/projections/index.ts +0 -0
  77. package/backend/integrations/projections/mcp-projector.ts +7 -3
  78. package/backend/integrations/projections/types.ts +28 -9
  79. package/backend/integrations/registry/index.ts +8 -1
  80. package/backend/integrations/registry/providers/context7.ts +1 -0
  81. package/backend/integrations/registry/providers/firecrawl.ts +1 -0
  82. package/backend/integrations/registry/providers/github.ts +1 -0
  83. package/backend/integrations/registry/providers/neon.ts +55 -0
  84. package/backend/integrations/registry/providers/supabase.ts +62 -0
  85. package/backend/integrations/registry/providers/vercel.ts +47 -0
  86. package/backend/integrations/registry/types.ts +2 -0
  87. package/backend/mcp/internal/servers/memory-graph/description.ts +3 -3
  88. package/backend/mcp/internal/servers/memory-graph/runner.ts +4 -9
  89. package/backend/mcp/internal/servers/memory-graph/schema.ts +0 -4
  90. package/backend/memory/README.md +57 -34
  91. package/backend/memory/compose.test.ts +3 -3
  92. package/backend/memory/compose.ts +6 -9
  93. package/backend/memory/config.ts +0 -5
  94. package/backend/memory/consolidate.ts +1 -2
  95. package/backend/memory/context.test.ts +8 -54
  96. package/backend/memory/context.ts +19 -25
  97. package/backend/memory/extract/episodic.ts +10 -12
  98. package/backend/memory/extract/index.ts +23 -52
  99. package/backend/memory/extract/scheduler.ts +3 -6
  100. package/backend/memory/index.ts +6 -5
  101. package/backend/memory/invalidate.ts +36 -48
  102. package/backend/memory/layout.ts +319 -130
  103. package/backend/memory/retention.ts +12 -42
  104. package/backend/memory/retrieval.eval.test.ts +2 -2
  105. package/backend/memory/retrieval.test.ts +228 -183
  106. package/backend/memory/retrieval.ts +22 -21
  107. package/backend/memory/revise.test.ts +62 -116
  108. package/backend/memory/revise.ts +0 -1
  109. package/backend/memory/view.ts +8 -5
  110. package/backend/snapshot/helpers.ts +35 -41
  111. package/backend/snapshot/snapshot-service.ts +90 -0
  112. package/backend/snapshot/turn-changes.test.ts +163 -0
  113. package/backend/snapshot/turn-changes.ts +131 -0
  114. package/backend/work/start-work.ts +4 -0
  115. package/backend/worktrees/branching/engine.ts +557 -0
  116. package/backend/worktrees/branching/index.ts +56 -0
  117. package/backend/worktrees/branching/naming.test.ts +118 -0
  118. package/backend/worktrees/branching/naming.ts +97 -0
  119. package/backend/worktrees/branching/projector.test.ts +229 -0
  120. package/backend/worktrees/branching/projector.ts +139 -0
  121. package/backend/worktrees/branching/providers/neon.ts +220 -0
  122. package/backend/worktrees/branching/registry.ts +27 -0
  123. package/backend/worktrees/branching/service.ts +620 -0
  124. package/backend/worktrees/branching/suggest.test.ts +154 -0
  125. package/backend/worktrees/branching/suggest.ts +202 -0
  126. package/backend/worktrees/branching/types.ts +88 -0
  127. package/backend/worktrees/index.ts +7 -0
  128. package/backend/worktrees/service.ts +48 -1
  129. package/backend/ws/db-client/accounts.ts +225 -0
  130. package/backend/ws/db-client/connections.ts +34 -3
  131. package/backend/ws/db-client/context.ts +37 -0
  132. package/backend/ws/db-client/env.ts +142 -0
  133. package/backend/ws/db-client/index.ts +22 -1
  134. package/backend/ws/db-client/supabase.ts +188 -0
  135. package/backend/ws/deployments/actions.ts +360 -0
  136. package/backend/ws/deployments/context.ts +43 -0
  137. package/backend/ws/deployments/crud.ts +152 -0
  138. package/backend/ws/deployments/index.ts +15 -0
  139. package/backend/ws/deployments/schemas.ts +183 -0
  140. package/backend/ws/index.ts +6 -2
  141. package/backend/ws/memory/crud.ts +4 -16
  142. package/backend/ws/projects/index.ts +6 -1
  143. package/backend/ws/projects/info.ts +70 -6
  144. package/backend/ws/projects/overview.test.ts +108 -0
  145. package/backend/ws/projects/overview.ts +300 -0
  146. package/backend/ws/snapshot/changes.ts +279 -0
  147. package/backend/ws/snapshot/index.ts +4 -3
  148. package/backend/ws/system/device-info.ts +307 -76
  149. package/backend/ws/worktrees/branching.ts +212 -0
  150. package/backend/ws/worktrees/crud.ts +33 -7
  151. package/backend/ws/worktrees/index.ts +14 -0
  152. package/dist/assets/{index-DSHIANSy.js → index-D4igjF6e.js} +1 -1
  153. package/dist/assets/{index-CyH7fSbR.js → index-DONNwiXm.js} +1 -1
  154. package/dist/assets/main-BrfXH1bL.js +1205 -0
  155. package/dist/assets/main-CT1PygsW.css +1 -0
  156. package/dist/index.html +2 -2
  157. package/package.json +1 -1
  158. package/shared/constants/tool-icons.ts +13 -0
  159. package/shared/types/db-client/connection.ts +6 -0
  160. package/shared/types/db-client/env.ts +192 -0
  161. package/shared/types/db-client/index.ts +3 -0
  162. package/shared/types/db-client/integrations.ts +204 -0
  163. package/shared/types/db-client/supabase.ts +131 -0
  164. package/shared/types/deployments.ts +382 -0
  165. package/shared/types/integrations.ts +9 -0
  166. package/shared/types/memory.ts +34 -51
  167. package/shared/types/ui/components.ts +1 -1
  168. package/shared/types/worktree-branching.ts +315 -0
  169. package/shared/utils/diff-calculator.test.ts +89 -0
  170. package/shared/utils/diff-calculator.ts +30 -65
  171. package/shared/utils/logger.ts +2 -0
  172. package/backend/memory/extract/languages.ts +0 -176
  173. package/backend/memory/extract/structural.test.ts +0 -215
  174. package/backend/memory/extract/structural.ts +0 -364
  175. package/backend/ws/snapshot/content.ts +0 -85
  176. package/dist/assets/main-D2IOjftb.css +0 -1
  177. package/dist/assets/main-DM-tHKbW.js +0 -1152
@@ -0,0 +1,458 @@
1
+ /**
2
+ * The Deployments surface, provider-agnostic.
3
+ *
4
+ * Every route in `backend/ws/deployments/` goes through here, and nothing here
5
+ * mentions Vercel. The service resolves which connected accounts can serve the
6
+ * current project, assembles the adapter context (account + decoded credential
7
+ * + binding), and hands the call on.
8
+ *
9
+ * The credential is decoded HERE and dies here. No function in this file
10
+ * returns it, and the shapes the routes serialise (`DeploySource`,
11
+ * `Deployment`) have nowhere to put it.
12
+ *
13
+ * Outward-facing actions go through `runAction`, which is the single place that
14
+ * checks a capability before calling a provider. A route that called
15
+ * `adapter.promote` directly would work for Vercel and crash for the first
16
+ * provider that does not have one.
17
+ */
18
+
19
+ import {
20
+ deployBindingQueries,
21
+ integrationAccountQueries,
22
+ parseDeployConfig,
23
+ type IntegrationAccountRow
24
+ } from '$backend/database/queries';
25
+ import { getProvider } from '$backend/integrations';
26
+ import type {
27
+ BuildLogBundle,
28
+ DeployAction,
29
+ DeployActionResult,
30
+ DeployBindingConfig,
31
+ Deployment,
32
+ DeploymentDetail,
33
+ DeploymentDraft,
34
+ DeploymentPage,
35
+ DeployProjectStatus,
36
+ DeploySource,
37
+ DeployTarget,
38
+ NewProjectDraft
39
+ } from '$shared/types/deployments';
40
+ import { getDeployAdapter } from './registry';
41
+ import {
42
+ readDeployBinding,
43
+ resolveDeployBinding,
44
+ suggestTargets,
45
+ toDeployBindingInfo
46
+ } from './bindings';
47
+ import type {
48
+ AdapterDeployInfo,
49
+ DeployContext,
50
+ DeploymentQuery,
51
+ DeployProviderAdapter,
52
+ RepositoryAccess
53
+ } from './types';
54
+ import { debug } from '$shared/utils/logger';
55
+
56
+ /**
57
+ * Accounts that can serve deployments here.
58
+ *
59
+ * Three filters, in order of what they mean: the account offers `deployments`,
60
+ * it is switched on, and there is code registered that knows how to talk to it.
61
+ * The third is why a provider declaring the capability before its adapter
62
+ * exists is harmless — it simply does not appear.
63
+ */
64
+ function accountsFor(projectId: string): { account: IntegrationAccountRow; adapter: DeployProviderAdapter }[] {
65
+ return integrationAccountQueries
66
+ .getAll()
67
+ .filter((account) => account.is_enabled === 1)
68
+ .filter((account) => integrationAccountQueries.capabilitiesOf(account).includes('deployments'))
69
+ // A project-bound account belongs to that project alone; a null binding
70
+ // means "available everywhere". Same rule the webhook gateway uses.
71
+ .filter((account) => account.project_id === null || account.project_id === projectId)
72
+ .flatMap((account) => {
73
+ const adapter = getDeployAdapter(account.provider);
74
+ return adapter ? [{ account, adapter }] : [];
75
+ });
76
+ }
77
+
78
+ export const deployService = {
79
+ /**
80
+ * Every source the panel can show, with its binding already resolved.
81
+ *
82
+ * Detection runs here, which is why this is async: opening the panel on a
83
+ * project that has never been bound is the moment we have both the account
84
+ * and the working tree in hand, and asking the user to bind manually at that
85
+ * point would be asking them to retype a file already in their repository.
86
+ */
87
+ async listSources(projectId: string, projectRoot: string): Promise<DeploySource[]> {
88
+ const sources: DeploySource[] = [];
89
+
90
+ for (const { account, adapter } of accountsFor(projectId)) {
91
+ const provider = getProvider(account.provider);
92
+ const credentials = integrationAccountQueries.credentialsOf(account);
93
+
94
+ let binding = readDeployBinding(projectId, account.id);
95
+ if (!binding) {
96
+ try {
97
+ binding = await resolveDeployBinding(adapter, account, projectId, projectRoot);
98
+ } catch (error) {
99
+ debug.warn('deployments', `Binding detection failed for ${account.provider}:`, error);
100
+ }
101
+ }
102
+
103
+ // Only offered when there is nothing bound yet. Listing every project
104
+ // in every team is several requests, and spending them to populate a
105
+ // picker nobody opened is the kind of cost that shows up as a rate
106
+ // limit during someone else's build.
107
+ const suggested = binding
108
+ ? []
109
+ : await suggestTargets(adapter, { projectId, account, credentials }, projectRoot).catch((error) => {
110
+ debug.warn('deployments', 'Could not list deploy targets:', error);
111
+ return [] as DeployTarget[];
112
+ });
113
+
114
+ sources.push({
115
+ accountId: account.id,
116
+ provider: account.provider,
117
+ providerName: provider?.name ?? account.provider,
118
+ label: account.label,
119
+ // Resolved once per source: every view that wants to say whose
120
+ // account this is would otherwise ask again.
121
+ viewer: adapter.viewer
122
+ ? await adapter.viewer({ projectId, account, credentials }).catch(() => null)
123
+ : null,
124
+ capabilities: adapter.capabilities,
125
+ binding: toDeployBindingInfo(binding),
126
+ suggestedTargets: suggested,
127
+ status: account.status,
128
+ statusDetail: account.status_detail
129
+ });
130
+ }
131
+
132
+ return sources;
133
+ },
134
+
135
+ /**
136
+ * Adapter + context for one account, or a clear error.
137
+ *
138
+ * "Not bound" is separated from "not connected" because they need different
139
+ * answers: one is a project picker, the other is a connect dialog.
140
+ */
141
+ require(projectId: string, accountId: string): { adapter: DeployProviderAdapter; context: DeployContext } {
142
+ const account = integrationAccountQueries.getById(accountId);
143
+ if (!account) throw new Error('That integration account is no longer connected');
144
+ if (account.is_enabled !== 1) {
145
+ throw new Error(`${account.label} is switched off in Settings → Integrations`);
146
+ }
147
+
148
+ const adapter = getDeployAdapter(account.provider);
149
+ if (!adapter) throw new Error(`No deployment provider is registered for ${account.provider}`);
150
+
151
+ const binding = readDeployBinding(projectId, account.id);
152
+ if (!binding) {
153
+ throw new Error('This project is not pointed at a deployment project yet — pick one first.');
154
+ }
155
+
156
+ return {
157
+ adapter,
158
+ context: {
159
+ projectId,
160
+ account,
161
+ credentials: integrationAccountQueries.credentialsOf(account),
162
+ binding
163
+ }
164
+ };
165
+ },
166
+
167
+ /** Targets for the picker, listed on demand rather than with every source. */
168
+ async listTargets(projectId: string, accountId: string, projectRoot: string): Promise<DeployTarget[]> {
169
+ const account = integrationAccountQueries.getById(accountId);
170
+ if (!account) throw new Error('That integration account is no longer connected');
171
+
172
+ const adapter = getDeployAdapter(account.provider);
173
+ if (!adapter) throw new Error(`No deployment provider is registered for ${account.provider}`);
174
+
175
+ return suggestTargets(
176
+ adapter,
177
+ { projectId, account, credentials: integrationAccountQueries.credentialsOf(account) },
178
+ projectRoot
179
+ );
180
+ },
181
+
182
+ list(projectId: string, accountId: string, query: DeploymentQuery): Promise<DeploymentPage> {
183
+ const { adapter, context } = this.require(projectId, accountId);
184
+ return adapter.list(context, query);
185
+ },
186
+
187
+ get(projectId: string, accountId: string, id: string): Promise<DeploymentDetail> {
188
+ const { adapter, context } = this.require(projectId, accountId);
189
+ return adapter.get(context, id);
190
+ },
191
+
192
+ currentProduction(projectId: string, accountId: string): Promise<Deployment | null> {
193
+ const { adapter, context } = this.require(projectId, accountId);
194
+ if (!adapter.currentProduction) return Promise.resolve(null);
195
+ return adapter.currentProduction(context);
196
+ },
197
+
198
+ fetchLogs(projectId: string, accountId: string, id: string): Promise<BuildLogBundle> {
199
+ const { adapter, context } = this.require(projectId, accountId);
200
+ if (!adapter.capabilities.logs || !adapter.fetchLogs) {
201
+ throw new Error(`${context.account.label} does not expose build logs`);
202
+ }
203
+ return adapter.fetchLogs(context, id);
204
+ },
205
+
206
+ /**
207
+ * Run one outward-facing action.
208
+ *
209
+ * The capability check lives here rather than in the route so every entry
210
+ * point inherits it, and the error names the provider rather than the
211
+ * method: "Coolify cannot promote a deployment" is actionable, while
212
+ * "adapter.promote is not a function" is a bug report.
213
+ */
214
+ async runAction(
215
+ projectId: string,
216
+ accountId: string,
217
+ action: DeployAction,
218
+ id: string
219
+ ): Promise<DeployActionResult> {
220
+ const { adapter, context } = this.require(projectId, accountId);
221
+ const label = context.account.label;
222
+
223
+ switch (action) {
224
+ case 'redeploy': {
225
+ if (!adapter.capabilities.redeploy || !adapter.redeploy) {
226
+ throw new Error(`${label} cannot redeploy a build`);
227
+ }
228
+ const result = await adapter.redeploy(context, id);
229
+ return { action, status: 'done', deploymentId: result.deploymentId, message: result.message };
230
+ }
231
+ case 'cancel': {
232
+ if (!adapter.capabilities.cancel || !adapter.cancel) {
233
+ throw new Error(`${label} cannot cancel a build`);
234
+ }
235
+ const result = await adapter.cancel(context, id);
236
+ return { action, status: 'done', deploymentId: null, message: result.message };
237
+ }
238
+ case 'rollback': {
239
+ if (!adapter.capabilities.rollback || !adapter.rollback) {
240
+ throw new Error(`${label} cannot roll back a deployment`);
241
+ }
242
+ const result = await adapter.rollback(context, id);
243
+ return { action, status: result.status, deploymentId: null, message: result.message };
244
+ }
245
+ case 'promote': {
246
+ if (!adapter.capabilities.promote || !adapter.promote) {
247
+ throw new Error(`${label} cannot promote a deployment`);
248
+ }
249
+ const result = await adapter.promote(context, id);
250
+ return { action, status: result.status, deploymentId: null, message: result.message };
251
+ }
252
+ case 'delete': {
253
+ if (!adapter.capabilities.deleteDeployment || !adapter.deleteDeployment) {
254
+ throw new Error(`${label} cannot delete a deployment`);
255
+ }
256
+ const result = await adapter.deleteDeployment(context, id);
257
+ return { action, status: 'done', deploymentId: null, message: result.message };
258
+ }
259
+ }
260
+ },
261
+
262
+ /** Project-level state, including a traffic move still in flight. */
263
+ async projectStatus(projectId: string, accountId: string): Promise<DeployProjectStatus> {
264
+ const { adapter, context } = this.require(projectId, accountId);
265
+ if (!adapter.projectStatus) return { paused: false, aliasRequest: null };
266
+ return adapter.projectStatus(context);
267
+ },
268
+
269
+ /** What the deploy dialog needs before it opens. */
270
+ deployInfo(projectId: string, accountId: string): Promise<AdapterDeployInfo> {
271
+ const { adapter, context } = this.require(projectId, accountId);
272
+ if (!adapter.capabilities.createDeployment || !adapter.deployInfo) {
273
+ return Promise.resolve({
274
+ gitRepo: null,
275
+ productionBranch: null,
276
+ canDeploy: false,
277
+ reason: `${context.account.label} cannot start a build from here.`
278
+ });
279
+ }
280
+ return adapter.deployInfo(context);
281
+ },
282
+
283
+ /**
284
+ * Start a new build.
285
+ *
286
+ * Kept out of `runAction` because it does not act on an existing deployment
287
+ * — it takes a branch and an environment, and the one thing a project that
288
+ * has never been deployed does not have is a deployment to act on.
289
+ */
290
+ async createDeployment(
291
+ projectId: string,
292
+ accountId: string,
293
+ draft: DeploymentDraft
294
+ ): Promise<DeployActionResult> {
295
+ const { adapter, context } = this.require(projectId, accountId);
296
+ if (!adapter.capabilities.createDeployment || !adapter.createDeployment) {
297
+ throw new Error(`${context.account.label} cannot start a build`);
298
+ }
299
+
300
+ const ref = draft.ref.trim();
301
+ if (!ref) throw new Error('Pick a branch to build from');
302
+
303
+ const result = await adapter.createDeployment(context, { ref, production: draft.production });
304
+ return {
305
+ action: 'redeploy',
306
+ status: 'done',
307
+ deploymentId: result.deploymentId,
308
+ message: result.message
309
+ };
310
+ },
311
+
312
+ /**
313
+ * Create a remote project and point this one at it.
314
+ *
315
+ * Binding straight away is the point: a user who just created a project for
316
+ * this repository has already answered "which project is this", and making
317
+ * them answer it again in a picker is the setup step that makes an
318
+ * integration feel like homework.
319
+ */
320
+ async createProject(
321
+ projectId: string,
322
+ accountId: string,
323
+ draft: NewProjectDraft
324
+ ): Promise<DeployTarget> {
325
+ const account = integrationAccountQueries.getById(accountId);
326
+ if (!account) throw new Error('That integration account is no longer connected');
327
+
328
+ const adapter = getDeployAdapter(account.provider);
329
+ if (!adapter) throw new Error(`No deployment provider is registered for ${account.provider}`);
330
+ if (!adapter.capabilities.createProject || !adapter.createProject) {
331
+ throw new Error(`${account.label} cannot create a project`);
332
+ }
333
+
334
+ const name = draft.name.trim();
335
+ if (!name) throw new Error('Give the project a name');
336
+
337
+ const credentials = integrationAccountQueries.credentialsOf(account);
338
+ const target = await adapter.createProject({ projectId, account, credentials }, { ...draft, name });
339
+
340
+ deployBindingQueries.upsert({
341
+ projectId,
342
+ accountId,
343
+ locator: target.id,
344
+ displayName: target.name,
345
+ teamId: target.teamId,
346
+ detected: false,
347
+ config: parseDeployConfig(deployBindingQueries.get(projectId, accountId)?.config_json ?? null)
348
+ });
349
+
350
+ return target;
351
+ },
352
+
353
+ /** Whether a repository could be attached, and what to do when it cannot. */
354
+ async repositoryAccess(projectId: string, accountId: string, gitRepo: string): Promise<RepositoryAccess> {
355
+ const account = integrationAccountQueries.getById(accountId);
356
+ if (!account) throw new Error('That integration account is no longer connected');
357
+
358
+ const adapter = getDeployAdapter(account.provider);
359
+ // UNBOUND on purpose: this reads a property of the account, and the
360
+ // new-project form needs the answer before any project exists to bind to.
361
+ if (!adapter?.capabilities.connectRepository || !adapter.repositoryAccess) {
362
+ return {
363
+ state: 'blocked',
364
+ reason: `${account.label} cannot attach a repository to a project.`,
365
+ actionUrl: null
366
+ };
367
+ }
368
+
369
+ return adapter.repositoryAccess(
370
+ { projectId, account, credentials: integrationAccountQueries.credentialsOf(account) },
371
+ gitRepo
372
+ );
373
+ },
374
+
375
+ /**
376
+ * Attach a git repository to the bound remote project.
377
+ *
378
+ * The way out of the one dead end this surface has: a remote project with no
379
+ * repository cannot be built from at all, and previously the only answer was
380
+ * "go to the provider's dashboard".
381
+ */
382
+ async connectRepository(
383
+ projectId: string,
384
+ accountId: string,
385
+ repo: { gitRepo: string; gitProvider: string }
386
+ ): Promise<void> {
387
+ const { adapter, context } = this.require(projectId, accountId);
388
+ if (!adapter.capabilities.connectRepository || !adapter.connectRepository) {
389
+ throw new Error(`${context.account.label} cannot attach a repository to an existing project`);
390
+ }
391
+ if (!repo.gitRepo.trim()) throw new Error('Name the repository to connect');
392
+
393
+ await adapter.connectRepository(context, {
394
+ gitRepo: repo.gitRepo.trim(),
395
+ gitProvider: repo.gitProvider || 'github'
396
+ });
397
+ },
398
+
399
+ /** Point this project at a different remote project. */
400
+ async setBinding(
401
+ projectId: string,
402
+ accountId: string,
403
+ locator: string,
404
+ teamId: string | null
405
+ ): Promise<void> {
406
+ const account = integrationAccountQueries.getById(accountId);
407
+ if (!account) throw new Error('That integration account is no longer connected');
408
+
409
+ const adapter = getDeployAdapter(account.provider);
410
+ if (!adapter) throw new Error(`No deployment provider is registered for ${account.provider}`);
411
+
412
+ const credentials = integrationAccountQueries.credentialsOf(account);
413
+ const described = await adapter
414
+ .describeTarget({ projectId, account, credentials }, locator, teamId)
415
+ .catch(() => null);
416
+
417
+ const existing = deployBindingQueries.get(projectId, accountId);
418
+ deployBindingQueries.upsert({
419
+ projectId,
420
+ accountId,
421
+ locator,
422
+ displayName: described?.name ?? locator,
423
+ teamId,
424
+ // Chosen, not detected — the distinction is what lets the binding bar
425
+ // say "from your linked project" only when that is actually true.
426
+ detected: false,
427
+ config: parseDeployConfig(existing?.config_json ?? null)
428
+ });
429
+ },
430
+
431
+ setConfig(projectId: string, accountId: string, config: DeployBindingConfig): void {
432
+ if (!deployBindingQueries.get(projectId, accountId)) {
433
+ throw new Error('This project is not pointed at a deployment project yet');
434
+ }
435
+ deployBindingQueries.setConfig(projectId, accountId, config);
436
+ },
437
+
438
+ /**
439
+ * Health for the hub.
440
+ *
441
+ * Returns null when this account has nothing to say — a Vercel account with
442
+ * `deployments` switched off — so the hub falls back to the MCP probe rather
443
+ * than reporting a state this surface never measured.
444
+ */
445
+ async probe(accountId: string, projectId: string | null) {
446
+ const account = integrationAccountQueries.getById(accountId);
447
+ if (!account) return null;
448
+ if (!integrationAccountQueries.capabilitiesOf(account).includes('deployments')) return null;
449
+
450
+ const adapter = getDeployAdapter(account.provider);
451
+ if (!adapter) return null;
452
+
453
+ const credentials = integrationAccountQueries.credentialsOf(account);
454
+ const binding = projectId ? readDeployBinding(projectId, accountId) : null;
455
+
456
+ return adapter.probe({ projectId: projectId ?? '', account, credentials }, binding);
457
+ }
458
+ };