@catamorphic/fastify-plugin 0.0.0-stage → 0.1.0-alpha.31

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 (160) hide show
  1. package/LICENSE.txt +202 -0
  2. package/NOTICE.txt +32 -0
  3. package/README.md +143 -2
  4. package/dist/app-presentation-tools.d.ts +4 -0
  5. package/dist/app-presentation-tools.d.ts.map +1 -0
  6. package/dist/app.d.ts +27 -0
  7. package/dist/app.d.ts.map +1 -0
  8. package/dist/chunk-baagvvm3.js +164 -0
  9. package/dist/chunk-baagvvm3.js.map +10 -0
  10. package/dist/http-identity.d.ts +52 -0
  11. package/dist/http-identity.d.ts.map +1 -0
  12. package/dist/hung-up.d.ts +7 -0
  13. package/dist/hung-up.d.ts.map +1 -0
  14. package/dist/index.d.ts +11 -0
  15. package/dist/index.d.ts.map +1 -0
  16. package/dist/index.js +10884 -0
  17. package/dist/index.js.map +56 -0
  18. package/dist/mcp-shared.d.ts +117 -0
  19. package/dist/mcp-shared.d.ts.map +1 -0
  20. package/dist/mcp.d.ts +6 -0
  21. package/dist/mcp.d.ts.map +1 -0
  22. package/dist/mcp.js +13 -0
  23. package/dist/mcp.js.map +9 -0
  24. package/dist/plugin.d.ts +113 -0
  25. package/dist/plugin.d.ts.map +1 -0
  26. package/dist/project-capabilities.d.ts +25 -0
  27. package/dist/project-capabilities.d.ts.map +1 -0
  28. package/dist/project-mcp-program.d.ts +15 -0
  29. package/dist/project-mcp-program.d.ts.map +1 -0
  30. package/dist/project-mcp-surface.d.ts +16 -0
  31. package/dist/project-mcp-surface.d.ts.map +1 -0
  32. package/dist/request-logging.d.ts +18 -0
  33. package/dist/request-logging.d.ts.map +1 -0
  34. package/dist/routes/agent-capabilities.d.ts +4 -0
  35. package/dist/routes/agent-capabilities.d.ts.map +1 -0
  36. package/dist/routes/agent.d.ts +4 -0
  37. package/dist/routes/agent.d.ts.map +1 -0
  38. package/dist/routes/apps-mcp.d.ts +4 -0
  39. package/dist/routes/apps-mcp.d.ts.map +1 -0
  40. package/dist/routes/apps.d.ts +12 -0
  41. package/dist/routes/apps.d.ts.map +1 -0
  42. package/dist/routes/client-runners.d.ts +11 -0
  43. package/dist/routes/client-runners.d.ts.map +1 -0
  44. package/dist/routes/code-hosts.d.ts +11 -0
  45. package/dist/routes/code-hosts.d.ts.map +1 -0
  46. package/dist/routes/connection-mcp.d.ts +10 -0
  47. package/dist/routes/connection-mcp.d.ts.map +1 -0
  48. package/dist/routes/connections.d.ts +4 -0
  49. package/dist/routes/connections.d.ts.map +1 -0
  50. package/dist/routes/documents.d.ts +11 -0
  51. package/dist/routes/documents.d.ts.map +1 -0
  52. package/dist/routes/environments.d.ts +4 -0
  53. package/dist/routes/environments.d.ts.map +1 -0
  54. package/dist/routes/git-gateway.d.ts +21 -0
  55. package/dist/routes/git-gateway.d.ts.map +1 -0
  56. package/dist/routes/http-gateway.d.ts +13 -0
  57. package/dist/routes/http-gateway.d.ts.map +1 -0
  58. package/dist/routes/me.d.ts +12 -0
  59. package/dist/routes/me.d.ts.map +1 -0
  60. package/dist/routes/memberships.d.ts +12 -0
  61. package/dist/routes/memberships.d.ts.map +1 -0
  62. package/dist/routes/model-gateway.d.ts +12 -0
  63. package/dist/routes/model-gateway.d.ts.map +1 -0
  64. package/dist/routes/notifications.d.ts +4 -0
  65. package/dist/routes/notifications.d.ts.map +1 -0
  66. package/dist/routes/parse.d.ts +5 -0
  67. package/dist/routes/parse.d.ts.map +1 -0
  68. package/dist/routes/personal-environment.d.ts +13 -0
  69. package/dist/routes/personal-environment.d.ts.map +1 -0
  70. package/dist/routes/plugins.d.ts +4 -0
  71. package/dist/routes/plugins.d.ts.map +1 -0
  72. package/dist/routes/project-mcp.d.ts +25 -0
  73. package/dist/routes/project-mcp.d.ts.map +1 -0
  74. package/dist/routes/projects.d.ts +4 -0
  75. package/dist/routes/projects.d.ts.map +1 -0
  76. package/dist/routes/publications.d.ts +15 -0
  77. package/dist/routes/publications.d.ts.map +1 -0
  78. package/dist/routes/run-errors.d.ts +8 -0
  79. package/dist/routes/run-errors.d.ts.map +1 -0
  80. package/dist/routes/runs.d.ts +4 -0
  81. package/dist/routes/runs.d.ts.map +1 -0
  82. package/dist/routes/session-artifacts.d.ts +27 -0
  83. package/dist/routes/session-artifacts.d.ts.map +1 -0
  84. package/dist/routes/session-mailboxes.d.ts +4 -0
  85. package/dist/routes/session-mailboxes.d.ts.map +1 -0
  86. package/dist/routes/session-previews.d.ts +40 -0
  87. package/dist/routes/session-previews.d.ts.map +1 -0
  88. package/dist/routes/session-terminals.d.ts +15 -0
  89. package/dist/routes/session-terminals.d.ts.map +1 -0
  90. package/dist/routes/triggers.d.ts +20 -0
  91. package/dist/routes/triggers.d.ts.map +1 -0
  92. package/dist/routes/watchers.d.ts +4 -0
  93. package/dist/routes/watchers.d.ts.map +1 -0
  94. package/dist/routes/webhooks.d.ts +11 -0
  95. package/dist/routes/webhooks.d.ts.map +1 -0
  96. package/dist/routes/workflow-enablements.d.ts +4 -0
  97. package/dist/routes/workflow-enablements.d.ts.map +1 -0
  98. package/dist/routes/workflows.d.ts +4 -0
  99. package/dist/routes/workflows.d.ts.map +1 -0
  100. package/dist/schemas.d.ts +17776 -0
  101. package/dist/schemas.d.ts.map +1 -0
  102. package/dist/session-artifact-tools.d.ts +5 -0
  103. package/dist/session-artifact-tools.d.ts.map +1 -0
  104. package/dist/session-event-stream.d.ts +45 -0
  105. package/dist/session-event-stream.d.ts.map +1 -0
  106. package/dist/spa.d.ts +24 -0
  107. package/dist/spa.d.ts.map +1 -0
  108. package/dist/telemetry.d.ts +4 -0
  109. package/dist/telemetry.d.ts.map +1 -0
  110. package/package.json +73 -4
  111. package/src/app-presentation-tools.ts +46 -0
  112. package/src/app.ts +115 -0
  113. package/src/http-identity.ts +145 -0
  114. package/src/hung-up.ts +13 -0
  115. package/src/index.ts +25 -0
  116. package/src/mcp-shared.ts +331 -0
  117. package/src/mcp.ts +10 -0
  118. package/src/plugin.ts +332 -0
  119. package/src/project-capabilities.ts +153 -0
  120. package/src/project-mcp-program.ts +607 -0
  121. package/src/project-mcp-surface.ts +881 -0
  122. package/src/request-logging.ts +47 -0
  123. package/src/routes/agent-capabilities.ts +109 -0
  124. package/src/routes/agent.ts +1566 -0
  125. package/src/routes/apps-mcp.ts +303 -0
  126. package/src/routes/apps.ts +692 -0
  127. package/src/routes/client-runners.ts +251 -0
  128. package/src/routes/code-hosts.ts +247 -0
  129. package/src/routes/connection-mcp.ts +135 -0
  130. package/src/routes/connections.ts +494 -0
  131. package/src/routes/documents.ts +538 -0
  132. package/src/routes/environments.ts +72 -0
  133. package/src/routes/git-gateway.ts +106 -0
  134. package/src/routes/http-gateway.ts +141 -0
  135. package/src/routes/me.ts +122 -0
  136. package/src/routes/memberships.ts +173 -0
  137. package/src/routes/model-gateway.ts +134 -0
  138. package/src/routes/notifications.ts +101 -0
  139. package/src/routes/parse.ts +32 -0
  140. package/src/routes/personal-environment.ts +146 -0
  141. package/src/routes/plugins.ts +341 -0
  142. package/src/routes/project-mcp.ts +394 -0
  143. package/src/routes/projects.ts +708 -0
  144. package/src/routes/publications.ts +222 -0
  145. package/src/routes/run-errors.ts +86 -0
  146. package/src/routes/runs.ts +253 -0
  147. package/src/routes/session-artifacts.ts +293 -0
  148. package/src/routes/session-mailboxes.ts +80 -0
  149. package/src/routes/session-previews.ts +224 -0
  150. package/src/routes/session-terminals.ts +209 -0
  151. package/src/routes/triggers.ts +241 -0
  152. package/src/routes/watchers.ts +94 -0
  153. package/src/routes/webhooks.ts +171 -0
  154. package/src/routes/workflow-enablements.ts +331 -0
  155. package/src/routes/workflows.ts +323 -0
  156. package/src/schemas.ts +2735 -0
  157. package/src/session-artifact-tools.ts +170 -0
  158. package/src/session-event-stream.ts +122 -0
  159. package/src/spa.ts +135 -0
  160. package/src/telemetry.ts +93 -0
@@ -0,0 +1,708 @@
1
+ import {
2
+ assertMayManageRolePolicy,
3
+ assertProjectPermission,
4
+ DeploymentBlockedError,
5
+ hasProjectPermission,
6
+ type Identity,
7
+ type Project,
8
+ ProjectFileConflictError,
9
+ ProjectFileNotFoundError,
10
+ ProjectFileNotTextError,
11
+ ProjectNotFoundError,
12
+ type WorkflowSummary,
13
+ } from "@catamorphic/core";
14
+ import type { FastifyInstance } from "fastify";
15
+ import type { ZodTypeProvider } from "fastify-type-provider-zod";
16
+ import { z } from "zod";
17
+ import type { RouteContext } from "../app.js";
18
+ import { resolveIdentity } from "../http-identity.js";
19
+ import {
20
+ CommitSchema,
21
+ CreateProjectSchema,
22
+ DeployRequestSchema,
23
+ DeployResponseSchema,
24
+ DiffEntrySchema,
25
+ DiscardResponseSchema,
26
+ ErrorSchema,
27
+ FileContentSchema,
28
+ FileEntrySchema,
29
+ ListSchema,
30
+ PaginationQuerySchema,
31
+ ProjectDetailSchema,
32
+ ProjectFileParamsSchema,
33
+ ProjectIdParamsSchema,
34
+ ProjectSchema,
35
+ PullRequestSchema,
36
+ PullResponseSchema,
37
+ RefQuerySchema,
38
+ RepoStatusSchema,
39
+ ResolveConflictsSchema,
40
+ UpdateProjectSchema,
41
+ WriteFileSchema,
42
+ } from "../schemas.js";
43
+
44
+ /**
45
+ * HTTP-facing shape of a project. Drops `tenantId` because the header already
46
+ * scopes the caller and the dashboard never renders it.
47
+ */
48
+ function toDto(project: Project) {
49
+ return {
50
+ id: project.id,
51
+ name: project.name,
52
+ storageType: project.storageType,
53
+ remoteUrl: project.remoteUrl,
54
+ remoteOwnership: project.remoteOwnership,
55
+ remoteDivergedAt: project.remoteDivergedAt,
56
+ defaultBranch: project.defaultBranch,
57
+ createdAt: project.createdAt,
58
+ updatedAt: project.updatedAt,
59
+ };
60
+ }
61
+
62
+ export function registerProjectRoutes(app: FastifyInstance, ctx: RouteContext) {
63
+ const typed = app.withTypeProvider<ZodTypeProvider>();
64
+
65
+ typed.route({
66
+ method: "POST",
67
+ url: "/projects",
68
+ schema: {
69
+ body: CreateProjectSchema,
70
+ response: { 201: ProjectSchema, 400: ErrorSchema, 503: ErrorSchema },
71
+ },
72
+ handler: async (request, reply) => {
73
+ if (!ctx.core)
74
+ return reply.status(503).send({ error: "Service not configured" });
75
+ const identity = resolveIdentity(request);
76
+ const project = await ctx.core.projects.create(identity, request.body);
77
+ return reply.status(201).send(toDto(project));
78
+ },
79
+ });
80
+
81
+ typed.route({
82
+ method: "GET",
83
+ url: "/projects",
84
+ schema: {
85
+ querystring: PaginationQuerySchema,
86
+ response: { 200: ListSchema(ProjectSchema) },
87
+ },
88
+ handler: async (request, reply) => {
89
+ if (!ctx.core) return reply.send({ items: [], total: 0 });
90
+ const identity = resolveIdentity(request);
91
+ const result = await ctx.core.projects.list(identity, request.query);
92
+ return reply.send({
93
+ items: result.items.map(toDto),
94
+ total: result.total,
95
+ });
96
+ },
97
+ });
98
+
99
+ typed.route({
100
+ method: "GET",
101
+ url: "/projects/:projectId",
102
+ schema: {
103
+ params: ProjectIdParamsSchema,
104
+ response: {
105
+ 200: ProjectDetailSchema,
106
+ 404: ErrorSchema,
107
+ 503: ErrorSchema,
108
+ },
109
+ },
110
+ handler: async (request, reply) => {
111
+ if (!ctx.core)
112
+ return reply.status(503).send({ error: "Service not configured" });
113
+ const identity = resolveIdentity(request);
114
+ const { projectId } = request.params;
115
+
116
+ try {
117
+ const project = await ctx.core.projects.getOverview({
118
+ identity,
119
+ projectId,
120
+ });
121
+ const summary = await safeListWorkflows(ctx.core, identity, projectId);
122
+ return reply.send({
123
+ ...toDto(project),
124
+ workflows: summary.workflows,
125
+ files: summary.files,
126
+ });
127
+ } catch (err) {
128
+ if (err instanceof ProjectNotFoundError) {
129
+ return reply.status(404).send({ error: "Project not found" });
130
+ }
131
+ throw err;
132
+ }
133
+ },
134
+ });
135
+
136
+ typed.route({
137
+ method: "PATCH",
138
+ url: "/projects/:projectId",
139
+ schema: {
140
+ params: ProjectIdParamsSchema,
141
+ body: UpdateProjectSchema,
142
+ response: { 200: ProjectSchema, 404: ErrorSchema, 503: ErrorSchema },
143
+ },
144
+ handler: async (request, reply) => {
145
+ if (!ctx.core)
146
+ return reply.status(503).send({ error: "Service not configured" });
147
+ const identity = resolveIdentity(request);
148
+ try {
149
+ const project = await ctx.core.projects.update(
150
+ identity,
151
+ request.params.projectId,
152
+ request.body,
153
+ );
154
+ return reply.send(toDto(project));
155
+ } catch (err) {
156
+ if (err instanceof ProjectNotFoundError) {
157
+ return reply.status(404).send({ error: "Project not found" });
158
+ }
159
+ throw err;
160
+ }
161
+ },
162
+ });
163
+
164
+ typed.route({
165
+ method: "DELETE",
166
+ url: "/projects/:projectId",
167
+ schema: {
168
+ params: ProjectIdParamsSchema,
169
+ response: {
170
+ 200: z.object({ deleted: z.boolean() }),
171
+ 404: ErrorSchema,
172
+ 503: ErrorSchema,
173
+ },
174
+ },
175
+ handler: async (request, reply) => {
176
+ if (!ctx.core)
177
+ return reply.status(503).send({ error: "Service not configured" });
178
+ const identity = resolveIdentity(request);
179
+ try {
180
+ await ctx.core.projects.delete(identity, request.params.projectId);
181
+ return reply.send({ deleted: true });
182
+ } catch (err) {
183
+ if (err instanceof ProjectNotFoundError) {
184
+ return reply.status(404).send({ error: "Project not found" });
185
+ }
186
+ throw err;
187
+ }
188
+ },
189
+ });
190
+
191
+ typed.route({
192
+ method: "GET",
193
+ url: "/projects/:projectId/files",
194
+ schema: {
195
+ params: ProjectIdParamsSchema,
196
+ response: {
197
+ 200: FileEntrySchema.array(),
198
+ 404: ErrorSchema,
199
+ 503: ErrorSchema,
200
+ },
201
+ },
202
+ handler: async (request, reply) => {
203
+ if (!ctx.core)
204
+ return reply.status(503).send({ error: "Service not configured" });
205
+ const identity = resolveIdentity(request);
206
+ try {
207
+ const entries = await ctx.core.projects.listFiles(
208
+ identity,
209
+ request.params.projectId,
210
+ );
211
+ return reply.send(entries);
212
+ } catch (err) {
213
+ if (err instanceof ProjectNotFoundError) {
214
+ return reply.status(404).send({ error: "Project not found" });
215
+ }
216
+ throw err;
217
+ }
218
+ },
219
+ });
220
+
221
+ typed.route({
222
+ method: "GET",
223
+ url: "/projects/:projectId/files/*",
224
+ schema: {
225
+ params: ProjectFileParamsSchema,
226
+ response: {
227
+ 200: FileContentSchema,
228
+ 404: ErrorSchema,
229
+ 409: ErrorSchema,
230
+ 415: ErrorSchema,
231
+ 503: ErrorSchema,
232
+ },
233
+ },
234
+ handler: async (request, reply) => {
235
+ if (!ctx.core)
236
+ return reply.status(503).send({ error: "Service not configured" });
237
+ const identity = resolveIdentity(request);
238
+ const { projectId } = request.params;
239
+ const filePath = request.params["*"];
240
+ try {
241
+ const content = await ctx.core.projects.readFile(
242
+ identity,
243
+ projectId,
244
+ filePath,
245
+ );
246
+ return reply.send({ path: filePath, content });
247
+ } catch (err) {
248
+ if (err instanceof ProjectFileConflictError)
249
+ return reply.status(409).send({ error: err.message });
250
+ if (err instanceof ProjectFileNotTextError)
251
+ return reply.status(415).send({ error: err.message });
252
+ if (err instanceof ProjectNotFoundError) {
253
+ return reply.status(404).send({ error: "Project not found" });
254
+ }
255
+ if (err instanceof ProjectFileNotFoundError) {
256
+ return reply.status(404).send({ error: "File not found" });
257
+ }
258
+ throw err;
259
+ }
260
+ },
261
+ });
262
+
263
+ typed.route({
264
+ method: "PUT",
265
+ url: "/projects/:projectId/files/*",
266
+ schema: {
267
+ params: ProjectFileParamsSchema,
268
+ body: WriteFileSchema,
269
+ response: {
270
+ 200: FileContentSchema,
271
+ 404: ErrorSchema,
272
+ 409: ErrorSchema,
273
+ 415: ErrorSchema,
274
+ 503: ErrorSchema,
275
+ },
276
+ },
277
+ handler: async (request, reply) => {
278
+ if (!ctx.core)
279
+ return reply.status(503).send({ error: "Service not configured" });
280
+ const identity = resolveIdentity(request);
281
+ const { projectId } = request.params;
282
+ const filePath = request.params["*"];
283
+ try {
284
+ const content = await ctx.core.projects.writeFile(
285
+ identity,
286
+ projectId,
287
+ filePath,
288
+ request.body,
289
+ );
290
+ return reply.send({ path: filePath, content });
291
+ } catch (err) {
292
+ if (err instanceof ProjectFileConflictError)
293
+ return reply.status(409).send({ error: err.message });
294
+ if (err instanceof ProjectFileNotTextError)
295
+ return reply.status(415).send({ error: err.message });
296
+ if (err instanceof ProjectNotFoundError) {
297
+ return reply.status(404).send({ error: "Project not found" });
298
+ }
299
+ throw err;
300
+ }
301
+ },
302
+ });
303
+
304
+ typed.route({
305
+ method: "GET",
306
+ url: "/projects/:projectId/commits",
307
+ schema: {
308
+ params: ProjectIdParamsSchema,
309
+ querystring: PaginationQuerySchema,
310
+ response: {
311
+ 200: ListSchema(CommitSchema),
312
+ 404: ErrorSchema,
313
+ 503: ErrorSchema,
314
+ },
315
+ },
316
+ handler: async (request, reply) => {
317
+ if (!ctx.core)
318
+ return reply.status(503).send({ error: "Service not configured" });
319
+ const identity = resolveIdentity(request);
320
+ const { projectId } = request.params;
321
+ try {
322
+ await ctx.core.projects.get(identity, projectId);
323
+ const commits = await ctx.core.deployment.listCommits(
324
+ identity.tenantId,
325
+ projectId,
326
+ identity.externalUserId,
327
+ { maxCount: request.query.limit },
328
+ );
329
+ return reply.send({ items: commits, total: commits.length });
330
+ } catch (err) {
331
+ if (err instanceof ProjectNotFoundError) {
332
+ return reply.status(404).send({ error: "Project not found" });
333
+ }
334
+ throw err;
335
+ }
336
+ },
337
+ });
338
+
339
+ typed.route({
340
+ method: "GET",
341
+ url: "/projects/:projectId/status",
342
+ schema: {
343
+ params: ProjectIdParamsSchema,
344
+ response: {
345
+ 200: RepoStatusSchema,
346
+ 404: ErrorSchema,
347
+ 503: ErrorSchema,
348
+ },
349
+ },
350
+ handler: async (request, reply) => {
351
+ if (!ctx.core)
352
+ return reply.status(503).send({ error: "Service not configured" });
353
+ const identity = resolveIdentity(request);
354
+ const { projectId } = request.params;
355
+ try {
356
+ await ctx.core.projects.get(identity, projectId);
357
+ const status = await ctx.core.deployment.getStatus(
358
+ identity.tenantId,
359
+ projectId,
360
+ identity.externalUserId,
361
+ );
362
+ return reply.send(status);
363
+ } catch (err) {
364
+ if (err instanceof ProjectNotFoundError) {
365
+ return reply.status(404).send({ error: "Project not found" });
366
+ }
367
+ throw err;
368
+ }
369
+ },
370
+ });
371
+
372
+ typed.route({
373
+ method: "GET",
374
+ url: "/projects/:projectId/workdir",
375
+ schema: {
376
+ params: ProjectIdParamsSchema,
377
+ response: {
378
+ 200: DiffEntrySchema.array(),
379
+ 404: ErrorSchema,
380
+ 503: ErrorSchema,
381
+ },
382
+ },
383
+ handler: async (request, reply) => {
384
+ if (!ctx.core)
385
+ return reply.status(503).send({ error: "Service not configured" });
386
+ const identity = resolveIdentity(request);
387
+ const { projectId } = request.params;
388
+ try {
389
+ await ctx.core.projects.get(identity, projectId);
390
+ const diff = await ctx.core.deployment.workdirDiff(
391
+ identity.tenantId,
392
+ projectId,
393
+ identity.externalUserId,
394
+ );
395
+ return reply.send(diff);
396
+ } catch (err) {
397
+ if (err instanceof ProjectNotFoundError) {
398
+ return reply.status(404).send({ error: "Project not found" });
399
+ }
400
+ throw err;
401
+ }
402
+ },
403
+ });
404
+
405
+ typed.route({
406
+ method: "GET",
407
+ url: "/projects/:projectId/diff",
408
+ schema: {
409
+ params: ProjectIdParamsSchema,
410
+ querystring: z.object({
411
+ base: z.string().min(1),
412
+ head: z.string().min(1),
413
+ }),
414
+ response: {
415
+ 200: DiffEntrySchema.array(),
416
+ 404: ErrorSchema,
417
+ 503: ErrorSchema,
418
+ },
419
+ },
420
+ handler: async (request, reply) => {
421
+ if (!ctx.core)
422
+ return reply.status(503).send({ error: "Service not configured" });
423
+ const identity = resolveIdentity(request);
424
+ const { projectId } = request.params;
425
+ try {
426
+ await ctx.core.projects.get(identity, projectId);
427
+ const diff = await ctx.core.deployment.diffRefs(
428
+ identity.tenantId,
429
+ projectId,
430
+ identity.externalUserId,
431
+ request.query.base,
432
+ request.query.head,
433
+ );
434
+ return reply.send(diff);
435
+ } catch (err) {
436
+ if (err instanceof ProjectNotFoundError) {
437
+ return reply.status(404).send({ error: "Project not found" });
438
+ }
439
+ throw err;
440
+ }
441
+ },
442
+ });
443
+
444
+ typed.route({
445
+ method: "POST",
446
+ url: "/projects/:projectId/deploy",
447
+ schema: {
448
+ params: ProjectIdParamsSchema,
449
+ body: DeployRequestSchema,
450
+ response: {
451
+ 200: DeployResponseSchema,
452
+ 404: ErrorSchema,
453
+ 503: ErrorSchema,
454
+ },
455
+ },
456
+ handler: async (request, reply) => {
457
+ if (!ctx.core)
458
+ return reply.status(503).send({ error: "Service not configured" });
459
+ const identity = resolveIdentity(request);
460
+ const { projectId } = request.params;
461
+ try {
462
+ const project = await ctx.core.projects.get(identity, projectId);
463
+ assertProjectPermission(identity, projectId, "program:publish");
464
+ // A repository that existed before Work publishes through its own
465
+ // default branch (ADR 0170): a server project attached to it takes
466
+ // changes as pull requests, never as direct deploys that would fork
467
+ // its main from the code host's.
468
+ if (
469
+ project.remoteUrl &&
470
+ project.remoteOwnership !== "owned" &&
471
+ !(await ctx.core.projectManager.localPath({
472
+ tenantId: identity.tenantId,
473
+ projectId,
474
+ }))
475
+ )
476
+ throw new DeploymentBlockedError(
477
+ `This project's program is published from ${project.remoteUrl}. Open a pull request there, or propose the change.`,
478
+ );
479
+ const result = await ctx.core.deployment.deploy(
480
+ identity.tenantId,
481
+ projectId,
482
+ identity.externalUserId,
483
+ {
484
+ message: request.body.message,
485
+ files: request.body.files,
486
+ base: request.body.base,
487
+ // Role files change only with `roles:write`, whoever edited them.
488
+ guardPublishedPaths: (paths) =>
489
+ assertMayManageRolePolicy(identity, projectId, paths),
490
+ },
491
+ );
492
+ return reply.send(result);
493
+ } catch (err) {
494
+ if (err instanceof ProjectNotFoundError) {
495
+ return reply.status(404).send({ error: "Project not found" });
496
+ }
497
+ throw err;
498
+ }
499
+ },
500
+ });
501
+
502
+ typed.route({
503
+ method: "POST",
504
+ url: "/projects/:projectId/pull",
505
+ schema: {
506
+ params: ProjectIdParamsSchema,
507
+ body: PullRequestSchema,
508
+ response: {
509
+ 200: PullResponseSchema,
510
+ 404: ErrorSchema,
511
+ 503: ErrorSchema,
512
+ },
513
+ },
514
+ handler: async (request, reply) => {
515
+ if (!ctx.core)
516
+ return reply.status(503).send({ error: "Service not configured" });
517
+ const identity = resolveIdentity(request);
518
+ const { projectId } = request.params;
519
+ try {
520
+ await ctx.core.projects.get(identity, projectId);
521
+ assertProjectPermission(identity, projectId, "program:write");
522
+ const result = await ctx.core.deployment.pullFromRemote(
523
+ identity.tenantId,
524
+ projectId,
525
+ identity.externalUserId,
526
+ { files: request.body.files },
527
+ );
528
+ return reply.send(result);
529
+ } catch (err) {
530
+ if (err instanceof ProjectNotFoundError) {
531
+ return reply.status(404).send({ error: "Project not found" });
532
+ }
533
+ throw err;
534
+ }
535
+ },
536
+ });
537
+
538
+ typed.route({
539
+ method: "POST",
540
+ url: "/projects/:projectId/discard",
541
+ schema: {
542
+ params: ProjectIdParamsSchema,
543
+ response: {
544
+ 200: DiscardResponseSchema,
545
+ 404: ErrorSchema,
546
+ 503: ErrorSchema,
547
+ },
548
+ },
549
+ handler: async (request, reply) => {
550
+ if (!ctx.core)
551
+ return reply.status(503).send({ error: "Service not configured" });
552
+ const identity = resolveIdentity(request);
553
+ const { projectId } = request.params;
554
+ try {
555
+ await ctx.core.projects.get(identity, projectId);
556
+ assertProjectPermission(identity, projectId, "program:write");
557
+ const result = await ctx.core.deployment.discardDraft(
558
+ identity.tenantId,
559
+ projectId,
560
+ identity.externalUserId,
561
+ );
562
+ return reply.send(result);
563
+ } catch (err) {
564
+ if (err instanceof ProjectNotFoundError) {
565
+ return reply.status(404).send({ error: "Project not found" });
566
+ }
567
+ throw err;
568
+ }
569
+ },
570
+ });
571
+
572
+ typed.route({
573
+ method: "POST",
574
+ url: "/projects/:projectId/resolve-conflicts",
575
+ schema: {
576
+ params: ProjectIdParamsSchema,
577
+ body: ResolveConflictsSchema,
578
+ response: {
579
+ 200: z.object({ commitSha: z.string() }),
580
+ 404: ErrorSchema,
581
+ 503: ErrorSchema,
582
+ },
583
+ },
584
+ handler: async (request, reply) => {
585
+ if (!ctx.core)
586
+ return reply.status(503).send({ error: "Service not configured" });
587
+ const identity = resolveIdentity(request);
588
+ const { projectId } = request.params;
589
+ try {
590
+ await ctx.core.projects.get(identity, projectId);
591
+ assertProjectPermission(identity, projectId, "program:write");
592
+ const result = await ctx.core.deployment.resolveConflicts(
593
+ identity.tenantId,
594
+ projectId,
595
+ identity.externalUserId,
596
+ {
597
+ resolutions: request.body.resolutions,
598
+ message: request.body.message,
599
+ },
600
+ );
601
+ return reply.send(result);
602
+ } catch (err) {
603
+ if (err instanceof ProjectNotFoundError) {
604
+ return reply.status(404).send({ error: "Project not found" });
605
+ }
606
+ throw err;
607
+ }
608
+ },
609
+ });
610
+
611
+ typed.route({
612
+ method: "POST",
613
+ url: "/projects/:projectId/ai-resolve-conflicts",
614
+ schema: {
615
+ params: ProjectIdParamsSchema,
616
+ body: z.object({
617
+ conflicts: z.array(
618
+ z.object({
619
+ path: z.string(),
620
+ base: z.string().nullable(),
621
+ ours: z.string().nullable(),
622
+ theirs: z.string().nullable(),
623
+ }),
624
+ ),
625
+ }),
626
+ response: {
627
+ 200: z.object({
628
+ resolutions: z.record(z.string(), z.string()),
629
+ notes: z.string().optional(),
630
+ }),
631
+ 503: ErrorSchema,
632
+ },
633
+ },
634
+ handler: async (request, reply) => {
635
+ assertProjectPermission(
636
+ resolveIdentity(request),
637
+ request.params.projectId,
638
+ "program:write",
639
+ );
640
+ // Stub: naive "prefer theirs" resolution until Codex-backed resolver lands.
641
+ // Falls back to ours if theirs is null (file deleted upstream we want to keep).
642
+ const resolutions: Record<string, string> = {};
643
+ for (const conflict of request.body.conflicts) {
644
+ const resolved =
645
+ conflict.theirs ?? conflict.ours ?? conflict.base ?? "";
646
+ resolutions[conflict.path] = resolved;
647
+ }
648
+ return reply.send({
649
+ resolutions,
650
+ notes: "Placeholder resolver; prefers remote version.",
651
+ });
652
+ },
653
+ });
654
+
655
+ typed.route({
656
+ method: "GET",
657
+ url: "/projects/:projectId/files-at-ref",
658
+ schema: {
659
+ params: ProjectIdParamsSchema,
660
+ querystring: RefQuerySchema.extend({ ref: z.string().min(1) }),
661
+ response: {
662
+ 200: z.record(z.string(), z.string()),
663
+ 404: ErrorSchema,
664
+ 503: ErrorSchema,
665
+ },
666
+ },
667
+ handler: async (request, reply) => {
668
+ if (!ctx.core)
669
+ return reply.status(503).send({ error: "Service not configured" });
670
+ const identity = resolveIdentity(request);
671
+ const { projectId } = request.params;
672
+ try {
673
+ await ctx.core.projects.get(identity, projectId);
674
+ const files = await ctx.core.deployment.filesAtRef(
675
+ identity.tenantId,
676
+ projectId,
677
+ identity.externalUserId,
678
+ request.query.ref,
679
+ );
680
+ return reply.send(files);
681
+ } catch (err) {
682
+ if (err instanceof ProjectNotFoundError) {
683
+ return reply.status(404).send({ error: "Project not found" });
684
+ }
685
+ throw err;
686
+ }
687
+ },
688
+ });
689
+ }
690
+
691
+ async function safeListWorkflows(
692
+ core: NonNullable<RouteContext["core"]>,
693
+ identity: Identity,
694
+ projectId: string,
695
+ ): Promise<{
696
+ workflows: WorkflowSummary[];
697
+ files: string[];
698
+ }> {
699
+ try {
700
+ const workflows = await core.workflows.list({ identity, projectId });
701
+ if (!hasProjectPermission(identity, projectId, "program:read"))
702
+ return { workflows, files: [] };
703
+ const allFiles = await core.projects.readAllFiles(identity, projectId);
704
+ return { workflows, files: Object.keys(allFiles) };
705
+ } catch {
706
+ return { workflows: [], files: [] };
707
+ }
708
+ }