@myrialabs/clopen 0.4.37 → 0.4.39

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 (193) hide show
  1. package/README.md +2 -2
  2. package/backend/auth/permissions.ts +39 -0
  3. package/backend/bootstrap.ts +16 -0
  4. package/backend/chat/stream-manager.ts +1 -1
  5. package/backend/database/crypto/envelope.test.ts +93 -0
  6. package/backend/database/crypto/envelope.ts +127 -0
  7. package/backend/database/crypto/index.ts +22 -0
  8. package/backend/database/crypto/master-key.ts +129 -0
  9. package/backend/database/crypto/secret-columns.ts +151 -0
  10. package/backend/database/migrations/071_create_notes_tables.ts +90 -0
  11. package/backend/database/migrations/072_encrypt_stored_secrets.ts +107 -0
  12. package/backend/database/migrations/073_create_integration_tables.ts +119 -0
  13. package/backend/database/migrations/074_create_work_tables.ts +94 -0
  14. package/backend/database/migrations/075_create_deploy_bindings.ts +54 -0
  15. package/backend/database/migrations/076_remove_memory_code_graph.ts +229 -0
  16. package/backend/database/migrations/077_create_integration_db_links.ts +150 -0
  17. package/backend/database/migrations/index.ts +49 -0
  18. package/backend/database/queries/db-client-connection-queries.ts +42 -12
  19. package/backend/database/queries/deploy-queries.ts +124 -0
  20. package/backend/database/queries/engine-queries.ts +23 -10
  21. package/backend/database/queries/graph-queries.ts +291 -247
  22. package/backend/database/queries/index.ts +10 -1
  23. package/backend/database/queries/integration-db-link-queries.ts +168 -0
  24. package/backend/database/queries/integration-queries.ts +325 -0
  25. package/backend/database/queries/mcp-server-queries.ts +38 -11
  26. package/backend/database/queries/note-queries.test.ts +190 -0
  27. package/backend/database/queries/note-queries.ts +269 -0
  28. package/backend/database/queries/profile-queries.ts +1 -1
  29. package/backend/database/queries/ssh-connection-queries.ts +19 -8
  30. package/backend/database/queries/work-queries.ts +210 -0
  31. package/backend/db-client/integrations/index.ts +37 -0
  32. package/backend/db-client/integrations/links.ts +533 -0
  33. package/backend/db-client/integrations/projector.test.ts +280 -0
  34. package/backend/db-client/integrations/projector.ts +256 -0
  35. package/backend/db-client/integrations/supabase-service.ts +266 -0
  36. package/backend/db-client/providers/registry.ts +27 -0
  37. package/backend/db-client/providers/supabase/adapter.ts +500 -0
  38. package/backend/db-client/providers/supabase/api-types.ts +92 -0
  39. package/backend/db-client/providers/supabase/apply-migration.ts +104 -0
  40. package/backend/db-client/providers/supabase/client.ts +259 -0
  41. package/backend/db-client/providers/supabase/features.ts +502 -0
  42. package/backend/db-client/providers/supabase/local.test.ts +78 -0
  43. package/backend/db-client/providers/supabase/local.ts +137 -0
  44. package/backend/db-client/providers/types.ts +137 -0
  45. package/backend/deployments/bindings.test.ts +43 -0
  46. package/backend/deployments/bindings.ts +216 -0
  47. package/backend/deployments/index.ts +53 -0
  48. package/backend/deployments/log-streams.ts +214 -0
  49. package/backend/deployments/prompts.ts +70 -0
  50. package/backend/deployments/providers/vercel/adapter.ts +757 -0
  51. package/backend/deployments/providers/vercel/build-logs.test.ts +80 -0
  52. package/backend/deployments/providers/vercel/build-logs.ts +211 -0
  53. package/backend/deployments/providers/vercel/client.ts +203 -0
  54. package/backend/deployments/providers/vercel/list.test.ts +624 -0
  55. package/backend/deployments/providers/vercel/normalise.test.ts +115 -0
  56. package/backend/deployments/providers/vercel/normalise.ts +113 -0
  57. package/backend/deployments/providers/vercel/types.ts +104 -0
  58. package/backend/deployments/registry.ts +27 -0
  59. package/backend/deployments/service.ts +458 -0
  60. package/backend/deployments/types.ts +278 -0
  61. package/backend/engine/docs/artifacts.md +22 -9
  62. package/backend/files/os-clipboard.test.ts +209 -0
  63. package/backend/files/os-clipboard.ts +567 -0
  64. package/backend/http/integration-hooks.ts +42 -0
  65. package/backend/http/notes-images.test.ts +228 -0
  66. package/backend/http/notes-images.ts +196 -0
  67. package/backend/index.ts +13 -0
  68. package/backend/integrations/accounts.ts +229 -0
  69. package/backend/integrations/health.ts +146 -0
  70. package/backend/integrations/index.ts +36 -0
  71. package/backend/integrations/projections/index.ts +0 -0
  72. package/backend/integrations/projections/mcp-projector.test.ts +204 -0
  73. package/backend/integrations/projections/mcp-projector.ts +163 -0
  74. package/backend/integrations/projections/types.ts +63 -0
  75. package/backend/integrations/registry/define.ts +38 -0
  76. package/backend/integrations/registry/index.ts +60 -0
  77. package/backend/integrations/registry/providers/context7.ts +44 -0
  78. package/backend/integrations/registry/providers/firecrawl.ts +41 -0
  79. package/backend/integrations/registry/providers/github.ts +78 -0
  80. package/backend/integrations/registry/providers/supabase.ts +61 -0
  81. package/backend/integrations/registry/providers/vercel.ts +46 -0
  82. package/backend/integrations/registry/types.ts +96 -0
  83. package/backend/integrations/webhooks/gateway.ts +160 -0
  84. package/backend/integrations/webhooks/verify.ts +65 -0
  85. package/backend/mcp/README.md +25 -3
  86. package/backend/mcp/external/config.ts +35 -1
  87. package/backend/mcp/index.ts +1 -1
  88. package/backend/mcp/internal/config.ts +1 -1
  89. package/backend/mcp/internal/servers/memory-graph/description.ts +3 -3
  90. package/backend/mcp/internal/servers/memory-graph/runner.ts +4 -9
  91. package/backend/mcp/internal/servers/memory-graph/schema.ts +0 -4
  92. package/backend/memory/README.md +57 -34
  93. package/backend/memory/compose.test.ts +3 -3
  94. package/backend/memory/compose.ts +6 -9
  95. package/backend/memory/config.ts +0 -5
  96. package/backend/memory/consolidate.ts +1 -2
  97. package/backend/memory/context.test.ts +8 -54
  98. package/backend/memory/context.ts +19 -25
  99. package/backend/memory/extract/episodic.ts +10 -12
  100. package/backend/memory/extract/index.ts +23 -52
  101. package/backend/memory/extract/scheduler.ts +3 -6
  102. package/backend/memory/index.ts +6 -5
  103. package/backend/memory/invalidate.ts +36 -48
  104. package/backend/memory/layout.ts +319 -130
  105. package/backend/memory/retention.ts +12 -42
  106. package/backend/memory/retrieval.eval.test.ts +2 -2
  107. package/backend/memory/retrieval.test.ts +228 -183
  108. package/backend/memory/retrieval.ts +22 -21
  109. package/backend/memory/revise.test.ts +62 -116
  110. package/backend/memory/revise.ts +0 -1
  111. package/backend/memory/view.ts +8 -5
  112. package/backend/notes/access.test.ts +82 -0
  113. package/backend/notes/access.ts +27 -0
  114. package/backend/notes/storage.test.ts +77 -0
  115. package/backend/notes/storage.ts +114 -0
  116. package/backend/permissions/service.ts +2 -2
  117. package/backend/profiles/service.ts +1 -1
  118. package/backend/projects/project-cleanup.ts +6 -0
  119. package/backend/snapshot/helpers.ts +35 -41
  120. package/backend/snapshot/snapshot-service.ts +90 -0
  121. package/backend/snapshot/turn-changes.test.ts +163 -0
  122. package/backend/snapshot/turn-changes.ts +131 -0
  123. package/backend/work/bindings.ts +124 -0
  124. package/backend/work/index.ts +42 -0
  125. package/backend/work/prompts.ts +150 -0
  126. package/backend/work/providers/github/actions-logs.ts +180 -0
  127. package/backend/work/providers/github/adapter.test.ts +88 -0
  128. package/backend/work/providers/github/adapter.ts +1255 -0
  129. package/backend/work/providers/github/client.ts +280 -0
  130. package/backend/work/providers/github/list-range.test.ts +158 -0
  131. package/backend/work/providers/github/token-kind.test.ts +42 -0
  132. package/backend/work/pull-requests.ts +242 -0
  133. package/backend/work/registry.ts +27 -0
  134. package/backend/work/service.ts +436 -0
  135. package/backend/work/start-work.test.ts +71 -0
  136. package/backend/work/start-work.ts +232 -0
  137. package/backend/work/types.ts +180 -0
  138. package/backend/ws/db-client/accounts.ts +225 -0
  139. package/backend/ws/db-client/connections.ts +34 -3
  140. package/backend/ws/db-client/context.ts +37 -0
  141. package/backend/ws/db-client/index.ts +20 -1
  142. package/backend/ws/db-client/supabase.ts +188 -0
  143. package/backend/ws/deployments/actions.ts +360 -0
  144. package/backend/ws/deployments/context.ts +43 -0
  145. package/backend/ws/deployments/crud.ts +152 -0
  146. package/backend/ws/deployments/index.ts +15 -0
  147. package/backend/ws/deployments/schemas.ts +183 -0
  148. package/backend/ws/files/clipboard.ts +78 -0
  149. package/backend/ws/files/index.ts +5 -0
  150. package/backend/ws/git/diff-budget.ts +70 -11
  151. package/backend/ws/index.ts +16 -2
  152. package/backend/ws/integrations/crud.ts +219 -0
  153. package/backend/ws/integrations/index.ts +22 -0
  154. package/backend/ws/mcp/crud.ts +25 -1
  155. package/backend/ws/memory/crud.ts +4 -16
  156. package/backend/ws/notes/index.ts +278 -0
  157. package/backend/ws/profiles/crud.ts +1 -1
  158. package/backend/ws/snapshot/changes.ts +279 -0
  159. package/backend/ws/snapshot/index.ts +4 -3
  160. package/backend/ws/work/actions.ts +206 -0
  161. package/backend/ws/work/context.ts +51 -0
  162. package/backend/ws/work/crud.ts +347 -0
  163. package/backend/ws/work/index.ts +17 -0
  164. package/backend/ws/work/pull-requests.ts +177 -0
  165. package/backend/ws/work/schemas.ts +226 -0
  166. package/dist/assets/{index-sJmS_CCm.js → index-BrF6nqld.js} +1 -1
  167. package/dist/assets/{index-Bik9TCBi.js → index-C8Gi-b6U.js} +1 -1
  168. package/dist/assets/main-Bd3gb79F.css +1 -0
  169. package/dist/assets/main-Bo1hD7mt.js +1191 -0
  170. package/dist/index.html +2 -2
  171. package/package.json +1 -1
  172. package/shared/constants/engine-tools.ts +1 -1
  173. package/shared/constants/tool-icons.ts +42 -0
  174. package/shared/types/database/schema.ts +41 -0
  175. package/shared/types/db-client/connection.ts +6 -0
  176. package/shared/types/db-client/index.ts +2 -0
  177. package/shared/types/db-client/integrations.ts +174 -0
  178. package/shared/types/db-client/supabase.ts +131 -0
  179. package/shared/types/deployments.ts +382 -0
  180. package/shared/types/integrations.ts +140 -0
  181. package/shared/types/memory.ts +34 -51
  182. package/shared/types/stores/settings.ts +8 -0
  183. package/shared/types/ui/components.ts +1 -1
  184. package/shared/types/work.ts +426 -0
  185. package/shared/utils/diff-calculator.test.ts +89 -0
  186. package/shared/utils/diff-calculator.ts +30 -65
  187. package/shared/utils/logger.ts +4 -0
  188. package/backend/memory/extract/languages.ts +0 -176
  189. package/backend/memory/extract/structural.test.ts +0 -215
  190. package/backend/memory/extract/structural.ts +0 -364
  191. package/backend/ws/snapshot/content.ts +0 -85
  192. package/dist/assets/main-DLqolBI2.css +0 -1
  193. package/dist/assets/main-Df1I3hnw.js +0 -1096
@@ -0,0 +1,360 @@
1
+ /**
2
+ * Acting on a deployment, and following one.
3
+ *
4
+ * deployments:action redeploy, cancel, roll back or promote
5
+ * deployments:send-logs a failed build's log, as chat-ready text
6
+ * deployments:follow-start begin streaming a running build's log
7
+ * deployments:follow-stop stop one stream
8
+ *
9
+ * `deployments:action` is the only route in this surface that changes anything
10
+ * outward-facing. The CONFIRMATION lives in the client, because that is where a
11
+ * human is: the dialog names the environment and the hostname that will change
12
+ * before this route is ever called. What the server owes in return is an
13
+ * audit-visible log line and an honest status — `pending` when the provider
14
+ * moves traffic asynchronously, never `done` on a promise.
15
+ *
16
+ * Nothing here is reachable by an agent. This surface exposes no MCP tool, so a
17
+ * build is only ever triggered by a person pressing a button.
18
+ */
19
+
20
+ import { t } from 'elysia';
21
+ import { createRouter } from '$shared/utils/ws-server';
22
+ import { gitService } from '$backend/git/git-service';
23
+ import { repoPathOf } from '$backend/deployments/bindings';
24
+ import {
25
+ buildDeployFailurePrompt,
26
+ deployService,
27
+ startBuildLogStream,
28
+ stopBuildLogStream,
29
+ stopBuildLogStreamsForUser
30
+ } from '$backend/deployments';
31
+ import { getProvider } from '$backend/integrations';
32
+ import { integrationAccountQueries } from '$backend/database/queries';
33
+ import type { DeployAction } from '$shared/types/deployments';
34
+ import { ws } from '$backend/utils/ws';
35
+ import { debug } from '$shared/utils/logger';
36
+ import { resolveDeployProject } from './context';
37
+ import { announceDeploymentsChanged } from './crud';
38
+ import {
39
+ ACTION_RESULT_SCHEMA,
40
+ DEPLOY_ACTION_SCHEMA,
41
+ PROJECT_STATUS_SCHEMA,
42
+ DEPLOY_INFO_SCHEMA,
43
+ DEPLOY_TARGET_SCHEMA,
44
+ NEW_PROJECT_DEFAULTS_SCHEMA
45
+ } from './schemas';
46
+
47
+ const PROJECT_FIELD = { projectId: t.Optional(t.String()) };
48
+
49
+ /**
50
+ * `owner/repo` for a working tree, or null.
51
+ *
52
+ * Soft on every failure: a project that is not a repository, or has no remote,
53
+ * simply has nothing to suggest — which is a normal state, not an error worth
54
+ * failing a dialog over.
55
+ */
56
+ /**
57
+ * The branch checked out right now, or null.
58
+ *
59
+ * Detached HEAD answers null rather than a commit hash: a hash is a valid ref
60
+ * to build, but offering one as a default would silently deploy a commit the
61
+ * user has not named — and on a detached HEAD they almost certainly meant a
62
+ * branch they have yet to pick.
63
+ */
64
+ async function localBranch(root: string): Promise<string | null> {
65
+ try {
66
+ const info = await gitService.getBranches(root);
67
+ return info.detached ? null : info.current || null;
68
+ } catch {
69
+ return null;
70
+ }
71
+ }
72
+
73
+ async function localRepoPath(root: string): Promise<string | null> {
74
+ try {
75
+ const remotes = await gitService.getRemotes(root);
76
+ const origin = remotes.find((remote) => remote.name === 'origin') ?? remotes[0];
77
+ return origin ? repoPathOf(origin.fetchUrl || origin.pushUrl) : null;
78
+ } catch {
79
+ return null;
80
+ }
81
+ }
82
+
83
+ export const deploymentsActionsHandler = createRouter()
84
+ .http('deployments:action', {
85
+ data: t.Object({
86
+ ...PROJECT_FIELD,
87
+ accountId: t.String({ minLength: 1 }),
88
+ deploymentId: t.String({ minLength: 1 }),
89
+ action: DEPLOY_ACTION_SCHEMA
90
+ }),
91
+ response: ACTION_RESULT_SCHEMA
92
+ }, async ({ data, conn }) => {
93
+ const { projectId, project } = resolveDeployProject(conn, data.projectId);
94
+ const action = data.action as DeployAction;
95
+
96
+ // Logged before the call, not after: if the provider hangs or the process
97
+ // dies mid-request, the record that someone asked for a production change
98
+ // is the thing worth having.
99
+ debug.log(
100
+ 'deployments',
101
+ `${ws.getUserId(conn)} requested ${action} on ${data.deploymentId} for project ${project.name}`
102
+ );
103
+
104
+ const result = await deployService.runAction(projectId, data.accountId, action, data.deploymentId);
105
+ announceDeploymentsChanged(projectId);
106
+ return result;
107
+ })
108
+
109
+ /**
110
+ * A failed build's log, as the text the chat will send.
111
+ *
112
+ * The text is handed BACK to the client rather than injected into a session
113
+ * here, for the reason the Issues surface hands its prompts back: the chat
114
+ * pipeline is browser-owned, and a second injection path would race it.
115
+ */
116
+ .http('deployments:send-logs', {
117
+ data: t.Object({
118
+ ...PROJECT_FIELD,
119
+ accountId: t.String({ minLength: 1 }),
120
+ deploymentId: t.String({ minLength: 1 })
121
+ }),
122
+ response: t.Object({ prompt: t.String() })
123
+ }, async ({ data, conn }) => {
124
+ const { projectId, project } = resolveDeployProject(conn, data.projectId);
125
+
126
+ const [deployment, bundle] = await Promise.all([
127
+ deployService.get(projectId, data.accountId, data.deploymentId),
128
+ deployService.fetchLogs(projectId, data.accountId, data.deploymentId)
129
+ ]);
130
+
131
+ const account = integrationAccountQueries.getById(data.accountId);
132
+ const providerName = account ? getProvider(account.provider)?.name ?? account.provider : 'the provider';
133
+
134
+ return {
135
+ prompt: buildDeployFailurePrompt(deployment, bundle, {
136
+ projectName: project.name,
137
+ providerName
138
+ })
139
+ };
140
+ })
141
+
142
+ .http('deployments:follow-start', {
143
+ data: t.Object({
144
+ ...PROJECT_FIELD,
145
+ accountId: t.String({ minLength: 1 }),
146
+ deploymentId: t.String({ minLength: 1 })
147
+ }),
148
+ response: t.Object({ streamId: t.String(), backlog: t.String() })
149
+ }, async ({ data, conn }) => {
150
+ const { projectId } = resolveDeployProject(conn, data.projectId);
151
+ const userId = ws.getUserId(conn);
152
+
153
+ const started = await startBuildLogStream(projectId, data.accountId, data.deploymentId, userId);
154
+ // The follow belongs to the socket that asked for it: closing the tab must
155
+ // close the request against the provider, not leave it open for the life
156
+ // of the process with nothing listening.
157
+ ws.addCleanup(conn, () => stopBuildLogStreamsForUser(userId));
158
+ return started;
159
+ })
160
+
161
+ .http('deployments:follow-stop', {
162
+ data: t.Object({ streamId: t.String({ minLength: 1 }) }),
163
+ response: t.Object({ ok: t.Boolean() })
164
+ }, async ({ data, conn }) => {
165
+ stopBuildLogStream(data.streamId, ws.getUserId(conn));
166
+ return { ok: true };
167
+ })
168
+
169
+ /**
170
+ * Start a new build.
171
+ *
172
+ * Its own route rather than a case of `deployments:action`, because it acts
173
+ * on a BRANCH rather than on an existing deployment — and a project that has
174
+ * never been deployed has no deployment to act on. That gap is what made the
175
+ * panel a dead end for exactly the project a user most wants to get live.
176
+ */
177
+ .http('deployments:deploy', {
178
+ data: t.Object({
179
+ ...PROJECT_FIELD,
180
+ accountId: t.String({ minLength: 1 }),
181
+ ref: t.String({ minLength: 1, maxLength: 255 }),
182
+ production: t.Boolean()
183
+ }),
184
+ response: ACTION_RESULT_SCHEMA
185
+ }, async ({ data, conn }) => {
186
+ const { projectId, project } = resolveDeployProject(conn, data.projectId);
187
+
188
+ debug.log(
189
+ 'deployments',
190
+ `${ws.getUserId(conn)} started a ${data.production ? 'production' : 'preview'} build of ${data.ref} for ${project.name}`
191
+ );
192
+
193
+ const result = await deployService.createDeployment(projectId, data.accountId, {
194
+ ref: data.ref,
195
+ production: data.production
196
+ });
197
+ announceDeploymentsChanged(projectId);
198
+ return result;
199
+ })
200
+
201
+ /**
202
+ * What the deploy dialog needs, composed from both sides.
203
+ *
204
+ * The adapter answers for the REMOTE project; the suggested repository comes
205
+ * from the LOCAL one. Composing here rather than in the adapter keeps the
206
+ * adapter free of any notion that a Clopen project has a working tree.
207
+ */
208
+ .http('deployments:project-status', {
209
+ data: t.Object({
210
+ ...PROJECT_FIELD,
211
+ accountId: t.String({ minLength: 1 })
212
+ }),
213
+ response: PROJECT_STATUS_SCHEMA
214
+ }, async ({ data, conn }) => {
215
+ const { projectId } = resolveDeployProject(conn, data.projectId);
216
+ return deployService.projectStatus(projectId, data.accountId);
217
+ })
218
+
219
+ .http('deployments:deploy-info', {
220
+ data: t.Object({
221
+ ...PROJECT_FIELD,
222
+ accountId: t.String({ minLength: 1 })
223
+ }),
224
+ response: DEPLOY_INFO_SCHEMA
225
+ }, async ({ data, conn }) => {
226
+ const { projectId, root } = resolveDeployProject(conn, data.projectId);
227
+
228
+ // Both run at once. They are independent — one asks the provider about
229
+ // the remote project, the other reads a file in the working tree — and
230
+ // running them in series was half of why this dialog felt slow.
231
+ const [info, suggestedRepo, currentBranch] = await Promise.all([
232
+ deployService.deployInfo(projectId, data.accountId),
233
+ localRepoPath(root),
234
+ localBranch(root)
235
+ ]);
236
+
237
+ // Only asked when it could matter. A project that already builds has
238
+ // nothing to connect, and the check costs another request.
239
+ const access =
240
+ !info.canDeploy && suggestedRepo
241
+ ? await deployService
242
+ .repositoryAccess(projectId, data.accountId, suggestedRepo)
243
+ .catch(() => ({ state: 'unknown' as const, reason: null, actionUrl: null }))
244
+ : { state: 'unavailable' as const, reason: null, actionUrl: null };
245
+
246
+ return {
247
+ ...info,
248
+ suggestedRepo,
249
+ connectState: access.state,
250
+ connectHint: access.reason,
251
+ connectUrl: access.actionUrl,
252
+ currentBranch
253
+ };
254
+ })
255
+
256
+ /**
257
+ * Attach a git repository to the bound remote project.
258
+ *
259
+ * Outward-facing in the sense that matters here: it changes how the remote
260
+ * project behaves for everyone, and it enables automatic deploys on push. The
261
+ * dialog that calls it names the repository first.
262
+ */
263
+ /** Whether a repository could be attached — asked before anything offers it. */
264
+ .http('deployments:repo-access', {
265
+ data: t.Object({
266
+ ...PROJECT_FIELD,
267
+ accountId: t.String({ minLength: 1 }),
268
+ gitRepo: t.String({ minLength: 1, maxLength: 255 })
269
+ }),
270
+ response: t.Object({
271
+ state: t.Union([t.Literal('ready'), t.Literal('blocked'), t.Literal('unknown')]),
272
+ reason: t.Union([t.String(), t.Null()]),
273
+ actionUrl: t.Union([t.String(), t.Null()])
274
+ })
275
+ }, async ({ data, conn }) => {
276
+ const { projectId } = resolveDeployProject(conn, data.projectId);
277
+ return deployService.repositoryAccess(projectId, data.accountId, data.gitRepo);
278
+ })
279
+
280
+ .http('deployments:connect-repo', {
281
+ data: t.Object({
282
+ ...PROJECT_FIELD,
283
+ accountId: t.String({ minLength: 1 }),
284
+ gitRepo: t.String({ minLength: 1, maxLength: 255 }),
285
+ gitProvider: t.Optional(t.Union([t.String(), t.Null()]))
286
+ }),
287
+ response: t.Object({ ok: t.Boolean() })
288
+ }, async ({ data, conn }) => {
289
+ const { projectId, project } = resolveDeployProject(conn, data.projectId);
290
+
291
+ debug.log(
292
+ 'deployments',
293
+ `${ws.getUserId(conn)} connected ${data.gitRepo} to the deploy target for ${project.name}`
294
+ );
295
+
296
+ await deployService.connectRepository(projectId, data.accountId, {
297
+ gitRepo: data.gitRepo,
298
+ gitProvider: data.gitProvider ?? 'github'
299
+ });
300
+ announceDeploymentsChanged(projectId);
301
+ return { ok: true };
302
+ })
303
+
304
+ /**
305
+ * What to prefill the new-project form with.
306
+ *
307
+ * Read from the LOCAL project, because it already answers both questions: a
308
+ * project called `calc-2` that pushes to `acme/calc-2` does not need the user
309
+ * to retype either. Failures are soft — an empty form is a working form.
310
+ */
311
+ .http('deployments:new-project-defaults', {
312
+ data: t.Object({ ...PROJECT_FIELD }),
313
+ response: NEW_PROJECT_DEFAULTS_SCHEMA
314
+ }, async ({ data, conn }) => {
315
+ const { project, root } = resolveDeployProject(conn, data.projectId);
316
+ const gitRepo = await localRepoPath(root);
317
+
318
+ return {
319
+ // Vercel project names are lowercase and hyphenated; offering a name
320
+ // that will be rejected is worse than offering none.
321
+ suggestedName: project.name.toLowerCase().replace(/[^a-z0-9._-]+/g, '-').replace(/^-+|-+$/g, ''),
322
+ gitRepo,
323
+ gitProvider: gitRepo ? 'github' : null
324
+ };
325
+ })
326
+
327
+ .http('deployments:create-project', {
328
+ data: t.Object({
329
+ ...PROJECT_FIELD,
330
+ accountId: t.String({ minLength: 1 }),
331
+ name: t.String({ minLength: 1, maxLength: 100 }),
332
+ gitRepo: t.Optional(t.Union([t.String(), t.Null()])),
333
+ gitProvider: t.Optional(t.Union([t.String(), t.Null()])),
334
+ framework: t.Optional(t.Union([t.String(), t.Null()]))
335
+ }),
336
+ response: DEPLOY_TARGET_SCHEMA
337
+ }, async ({ data, conn }) => {
338
+ const { projectId, project } = resolveDeployProject(conn, data.projectId);
339
+
340
+ debug.log('deployments', `${ws.getUserId(conn)} created a deploy project for ${project.name}`);
341
+
342
+ const target = await deployService.createProject(projectId, data.accountId, {
343
+ name: data.name,
344
+ gitRepo: data.gitRepo ?? null,
345
+ gitProvider: data.gitProvider ?? null,
346
+ framework: data.framework ?? null
347
+ });
348
+ announceDeploymentsChanged(projectId);
349
+ return target;
350
+ })
351
+
352
+ .emit('deployments:log-chunk', t.Object({
353
+ streamId: t.String(),
354
+ deploymentId: t.String(),
355
+ data: t.String(),
356
+ /** Stage the provider reports, pushed the moment it changes. */
357
+ phase: t.Optional(t.String()),
358
+ done: t.Optional(t.Boolean()),
359
+ error: t.Optional(t.Union([t.String(), t.Null()]))
360
+ }));
@@ -0,0 +1,43 @@
1
+ /**
2
+ * Which project a deployments route acts on.
3
+ *
4
+ * The surface lives in More Tools, and More Tools is global — a user looking at
5
+ * project A should be able to watch project B's build without leaving their
6
+ * session. So every route takes an OPTIONAL `projectId` and falls back to the
7
+ * connection's current one, rather than reading the connection and nothing else.
8
+ *
9
+ * Access is still checked per call. `projectId` arriving from the client is a
10
+ * request, not a permission: `requireProjectAccess` is what decides.
11
+ *
12
+ * Unlike the Issues surface this never resolves a worktree. A binding belongs
13
+ * to the project, detection reads the project's own `.vercel/project.json` and
14
+ * git remotes, and a worktree is a copy that carries both — so resolving one
15
+ * would add a failure mode (a tree that was just deleted) and buy nothing.
16
+ */
17
+
18
+ import type { WSConnection } from '$shared/utils/ws-server';
19
+ import type { Project } from '$shared/types/database/schema';
20
+ import { ws } from '$backend/utils/ws';
21
+ import { requireProjectAccess } from '../access';
22
+
23
+ export interface DeployProjectContext {
24
+ projectId: string;
25
+ project: Project;
26
+ /** The tree detection reads from. Always the project root. */
27
+ root: string;
28
+ /** True when this is the project the user is currently working in. */
29
+ isCurrent: boolean;
30
+ }
31
+
32
+ export function resolveDeployProject(conn: WSConnection, projectId?: string): DeployProjectContext {
33
+ const current = ws.getProjectId(conn);
34
+ const target = (projectId ?? '').trim() || current;
35
+ const project = requireProjectAccess(conn, target);
36
+
37
+ return {
38
+ projectId: target,
39
+ project,
40
+ root: project.path,
41
+ isCurrent: target === current
42
+ };
43
+ }
@@ -0,0 +1,152 @@
1
+ /**
2
+ * Reading deployments.
3
+ *
4
+ * deployments:sources which connected accounts serve a project
5
+ * deployments:list deployments for one source
6
+ * deployments:get one deployment, with aliases and timings
7
+ * deployments:targets remote projects this account can be pointed at
8
+ * deployments:set-binding point a project at a different remote project
9
+ * deployments:set-config per-binding behaviour (environment, auto-refresh)
10
+ * deployments:logs build logs as one budgeted bundle
11
+ *
12
+ * NOT admin-gated, unlike `integrations:*`. The distinction is what the route
13
+ * can reach: an integrations route changes a credential every project and every
14
+ * engine depends on, while these act inside one project the caller was already
15
+ * granted access to — the same reasoning that leaves `git:push` open.
16
+ *
17
+ * Every route takes an optional `projectId`, because this surface is global.
18
+ */
19
+
20
+ import { t } from 'elysia';
21
+ import { createRouter } from '$shared/utils/ws-server';
22
+ import { deployService } from '$backend/deployments';
23
+ import type { DeploymentEnvironmentFilter } from '$shared/types/deployments';
24
+ import { ws } from '$backend/utils/ws';
25
+ import { resolveDeployProject } from './context';
26
+ import {
27
+ DEPLOYMENT_DETAIL_SCHEMA,
28
+ DEPLOYMENT_SCHEMA,
29
+ DEPLOY_CONFIG_SCHEMA,
30
+ DEPLOY_SOURCE_SCHEMA,
31
+ DEPLOY_TARGET_SCHEMA,
32
+ ENVIRONMENT_FILTER_SCHEMA,
33
+ LOG_BUNDLE_SCHEMA,
34
+ RATE_LIMIT_SCHEMA
35
+ } from './schemas';
36
+
37
+ /**
38
+ * Tell a project's viewers that one of its sources changed.
39
+ *
40
+ * Project-scoped rather than global: a binding belongs to one project, and two
41
+ * people looking at different projects have no reason to refetch because of
42
+ * each other.
43
+ */
44
+ export function announceDeploymentsChanged(projectId: string): void {
45
+ ws.emit.project(projectId, 'deployments:changed', { projectId });
46
+ }
47
+
48
+ /** Present on every route in this surface. Empty means "the current project". */
49
+ const PROJECT_FIELD = { projectId: t.Optional(t.String()) };
50
+
51
+ export const deploymentsCrudHandler = createRouter()
52
+ .http('deployments:sources', {
53
+ data: t.Object({ ...PROJECT_FIELD }),
54
+ response: t.Array(DEPLOY_SOURCE_SCHEMA)
55
+ }, async ({ data, conn }) => {
56
+ const { projectId, root } = resolveDeployProject(conn, data.projectId);
57
+ return deployService.listSources(projectId, root);
58
+ })
59
+
60
+ .http('deployments:list', {
61
+ data: t.Object({
62
+ ...PROJECT_FIELD,
63
+ accountId: t.String({ minLength: 1 }),
64
+ environment: ENVIRONMENT_FILTER_SCHEMA,
65
+ branch: t.Optional(t.String()),
66
+ cursor: t.Optional(t.String())
67
+ }),
68
+ response: t.Object({
69
+ items: t.Array(DEPLOYMENT_SCHEMA),
70
+ hasMore: t.Boolean(),
71
+ nextCursor: t.Union([t.String(), t.Null()]),
72
+ rateLimit: t.Union([RATE_LIMIT_SCHEMA, t.Null()])
73
+ })
74
+ }, async ({ data, conn }) => {
75
+ const { projectId } = resolveDeployProject(conn, data.projectId);
76
+ return deployService.list(projectId, data.accountId, {
77
+ environment: data.environment as DeploymentEnvironmentFilter,
78
+ ...(data.branch && { branch: data.branch }),
79
+ ...(data.cursor && { cursor: data.cursor })
80
+ });
81
+ })
82
+
83
+ .http('deployments:get', {
84
+ data: t.Object({
85
+ ...PROJECT_FIELD,
86
+ accountId: t.String({ minLength: 1 }),
87
+ deploymentId: t.String({ minLength: 1 })
88
+ }),
89
+ response: DEPLOYMENT_DETAIL_SCHEMA
90
+ }, async ({ data, conn }) => {
91
+ const { projectId } = resolveDeployProject(conn, data.projectId);
92
+ return deployService.get(projectId, data.accountId, data.deploymentId);
93
+ })
94
+
95
+ .http('deployments:targets', {
96
+ data: t.Object({
97
+ ...PROJECT_FIELD,
98
+ accountId: t.String({ minLength: 1 })
99
+ }),
100
+ response: t.Array(DEPLOY_TARGET_SCHEMA)
101
+ }, async ({ data, conn }) => {
102
+ const { projectId, root } = resolveDeployProject(conn, data.projectId);
103
+ return deployService.listTargets(projectId, data.accountId, root);
104
+ })
105
+
106
+ .http('deployments:set-binding', {
107
+ data: t.Object({
108
+ ...PROJECT_FIELD,
109
+ accountId: t.String({ minLength: 1 }),
110
+ locator: t.String({ minLength: 1 }),
111
+ teamId: t.Optional(t.Union([t.String(), t.Null()]))
112
+ }),
113
+ response: t.Object({ ok: t.Boolean() })
114
+ }, async ({ data, conn }) => {
115
+ const { projectId } = resolveDeployProject(conn, data.projectId);
116
+ await deployService.setBinding(projectId, data.accountId, data.locator, data.teamId ?? null);
117
+ announceDeploymentsChanged(projectId);
118
+ return { ok: true };
119
+ })
120
+
121
+ .http('deployments:set-config', {
122
+ data: t.Object({
123
+ ...PROJECT_FIELD,
124
+ accountId: t.String({ minLength: 1 }),
125
+ config: DEPLOY_CONFIG_SCHEMA
126
+ }),
127
+ response: t.Object({ ok: t.Boolean() })
128
+ }, async ({ data, conn }) => {
129
+ const { projectId } = resolveDeployProject(conn, data.projectId);
130
+ deployService.setConfig(projectId, data.accountId, {
131
+ environment: data.config.environment as DeploymentEnvironmentFilter,
132
+ autoRefresh: data.config.autoRefresh
133
+ });
134
+ announceDeploymentsChanged(projectId);
135
+ return { ok: true };
136
+ })
137
+
138
+ .http('deployments:logs', {
139
+ data: t.Object({
140
+ ...PROJECT_FIELD,
141
+ accountId: t.String({ minLength: 1 }),
142
+ deploymentId: t.String({ minLength: 1 })
143
+ }),
144
+ response: LOG_BUNDLE_SCHEMA
145
+ }, async ({ data, conn }) => {
146
+ const { projectId } = resolveDeployProject(conn, data.projectId);
147
+ return deployService.fetchLogs(projectId, data.accountId, data.deploymentId);
148
+ })
149
+
150
+ .emit('deployments:changed', t.Object({
151
+ projectId: t.String()
152
+ }));
@@ -0,0 +1,15 @@
1
+ /**
2
+ * Deployments surface routes.
3
+ *
4
+ * Importing `$backend/deployments` is what registers the providers, so merging
5
+ * this router into the app is also what makes Vercel exist. That is deliberate:
6
+ * a surface with no routes has no adapters to register either.
7
+ */
8
+
9
+ import { createRouter } from '$shared/utils/ws-server';
10
+ import { deploymentsCrudHandler } from './crud';
11
+ import { deploymentsActionsHandler } from './actions';
12
+
13
+ export const deploymentsRouter = createRouter()
14
+ .merge(deploymentsCrudHandler)
15
+ .merge(deploymentsActionsHandler);