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

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,171 @@
1
+ import {
2
+ WEBHOOK_DEFAULT_MAX_BYTES,
3
+ WebhookMethodNotAllowedError,
4
+ WebhookNotFoundError,
5
+ WebhookRejectedError,
6
+ WebhookTooLargeError,
7
+ } from "@catamorphic/core";
8
+ import type { FastifyInstance, FastifyRequest } from "fastify";
9
+ import type { ZodTypeProvider } from "fastify-type-provider-zod";
10
+ import { z } from "zod";
11
+ import type { RouteContext } from "../app.js";
12
+ import { resolveIdentity } from "../http-identity.js";
13
+ import { ErrorSchema, ProjectIdParamsSchema } from "../schemas.js";
14
+
15
+ const WebhookSchema = z.object({
16
+ name: z.string(),
17
+ url: z.string(),
18
+ workflows: z.array(z.string()),
19
+ listening: z.boolean(),
20
+ verified: z.boolean(),
21
+ });
22
+
23
+ /**
24
+ * Project webhooks (ADRs 0156, 0171). The intake is public: the token in
25
+ * its path is the sender's credential, checked with the declared
26
+ * verification before anything is stored. A declared handshake is answered
27
+ * 200 with its echoed value; any other request is stored and answered 202.
28
+ * `webhooks:read` lists the URLs; `webhooks:write` rotates their tokens.
29
+ */
30
+ export function registerWebhookRoutes(
31
+ app: FastifyInstance,
32
+ ctx: RouteContext,
33
+ ): void {
34
+ const typed = app.withTypeProvider<ZodTypeProvider>();
35
+ const apiBase = (request: FastifyRequest) =>
36
+ ctx.publicApiBase ??
37
+ `${request.protocol}://${request.host}${app.prefix}`.replace(/\/$/, "");
38
+
39
+ // Senders sign the exact bytes: keep every body raw on this route only.
40
+ // The parser admits up to the host's maximum; each endpoint's own limit
41
+ // is checked once the endpoint is known.
42
+ const hostMaxBytes =
43
+ ctx.core?.webhooks?.maxBodyBytes ?? WEBHOOK_DEFAULT_MAX_BYTES;
44
+ app.register(async (intake) => {
45
+ intake.removeAllContentTypeParsers();
46
+ intake.addContentTypeParser(
47
+ "*",
48
+ { parseAs: "buffer", bodyLimit: hostMaxBytes },
49
+ (_request, body, done) => done(null, body),
50
+ );
51
+ intake.withTypeProvider<ZodTypeProvider>().route({
52
+ // GET only ever answers a declared handshake (ADR 0171).
53
+ method: ["POST", "GET"],
54
+ url: "/hooks/:projectId/:name/:token",
55
+ config: { public: true },
56
+ bodyLimit: hostMaxBytes,
57
+ schema: {
58
+ params: z.object({
59
+ projectId: z.string().uuid(),
60
+ name: z.string().min(1).max(63),
61
+ token: z.string().min(1).max(200),
62
+ }),
63
+ response: {
64
+ 200: z.string(),
65
+ 202: z.object({ id: z.string(), duplicate: z.boolean() }),
66
+ 401: ErrorSchema,
67
+ 404: ErrorSchema,
68
+ 405: ErrorSchema,
69
+ 413: ErrorSchema,
70
+ 503: ErrorSchema,
71
+ },
72
+ },
73
+ handler: async (request, reply) => {
74
+ const webhooks = ctx.core?.webhooks;
75
+ if (!webhooks) return reply.status(503).send({ error: "Unavailable" });
76
+ try {
77
+ const received = await webhooks.receive({
78
+ ...request.params,
79
+ method: request.method,
80
+ headers: request.headers,
81
+ query: queryValues(request.query),
82
+ body: Buffer.isBuffer(request.body)
83
+ ? request.body
84
+ : Buffer.alloc(0),
85
+ });
86
+ if (received.type === "handshake")
87
+ return reply
88
+ .status(200)
89
+ .type("text/plain; charset=utf-8")
90
+ .send(received.answer);
91
+ return reply
92
+ .status(202)
93
+ .send({ id: received.eventId, duplicate: received.duplicate });
94
+ } catch (error) {
95
+ if (error instanceof WebhookNotFoundError)
96
+ return reply.status(404).send({ error: error.message });
97
+ if (error instanceof WebhookRejectedError)
98
+ return reply.status(401).send({ error: error.message });
99
+ if (error instanceof WebhookMethodNotAllowedError)
100
+ return reply
101
+ .status(405)
102
+ .header("allow", "POST")
103
+ .send({ error: error.message });
104
+ if (error instanceof WebhookTooLargeError)
105
+ return reply.status(413).send({ error: error.message });
106
+ throw error;
107
+ }
108
+ },
109
+ });
110
+ });
111
+
112
+ typed.route({
113
+ method: "GET",
114
+ url: "/projects/:projectId/webhooks",
115
+ schema: {
116
+ params: ProjectIdParamsSchema,
117
+ response: { 200: z.array(WebhookSchema), 503: ErrorSchema },
118
+ },
119
+ handler: async (request, reply) => {
120
+ const webhooks = ctx.core?.webhooks;
121
+ if (!webhooks) return reply.status(503).send({ error: "Unavailable" });
122
+ const endpoints = await webhooks.list({
123
+ identity: resolveIdentity(request),
124
+ projectId: request.params.projectId,
125
+ });
126
+ const base = apiBase(request);
127
+ return reply.send(
128
+ endpoints.map(({ path, ...endpoint }) => ({
129
+ ...endpoint,
130
+ url: `${base}${path}`,
131
+ })),
132
+ );
133
+ },
134
+ });
135
+
136
+ typed.route({
137
+ method: "POST",
138
+ url: "/projects/:projectId/webhooks/:name/rotate",
139
+ schema: {
140
+ params: ProjectIdParamsSchema.extend({ name: z.string().min(1) }),
141
+ response: { 200: WebhookSchema, 404: ErrorSchema, 503: ErrorSchema },
142
+ },
143
+ handler: async (request, reply) => {
144
+ const webhooks = ctx.core?.webhooks;
145
+ if (!webhooks) return reply.status(503).send({ error: "Unavailable" });
146
+ try {
147
+ const { path, ...endpoint } = await webhooks.rotate({
148
+ identity: resolveIdentity(request),
149
+ projectId: request.params.projectId,
150
+ name: request.params.name,
151
+ });
152
+ return reply.send({ ...endpoint, url: `${apiBase(request)}${path}` });
153
+ } catch (error) {
154
+ if (error instanceof WebhookNotFoundError)
155
+ return reply.status(404).send({ error: error.message });
156
+ throw error;
157
+ }
158
+ },
159
+ });
160
+ }
161
+
162
+ /** A sender's query parameters, first value of each. */
163
+ function queryValues(query: unknown): Record<string, string> {
164
+ if (typeof query !== "object" || query === null) return {};
165
+ return Object.fromEntries(
166
+ Object.entries(query).flatMap(([key, value]: [string, unknown]) => {
167
+ const first = Array.isArray(value) ? value[0] : value;
168
+ return typeof first === "string" ? [[key, first]] : [];
169
+ }),
170
+ );
171
+ }
@@ -0,0 +1,331 @@
1
+ import {
2
+ AccessDeniedError,
3
+ AuthenticationRequiredError,
4
+ ConnectionPermissionDeniedError,
5
+ ConnectionUnavailableError,
6
+ EnvironmentAccessDeniedError,
7
+ EnvironmentBindingUnavailableError,
8
+ EnvironmentIncompatibleError,
9
+ EnvironmentNotFoundError,
10
+ EnvironmentPolicyInvalidError,
11
+ ProductionDeploymentNotFoundError,
12
+ WorkflowEnablementConflictError,
13
+ WorkflowEnablementConsentRequiredError,
14
+ WorkflowEnablementNotFoundError,
15
+ WorkflowEnablementSuspendedError,
16
+ WorkflowNotFoundError,
17
+ } from "@catamorphic/core";
18
+ import type { FastifyInstance, FastifyReply } from "fastify";
19
+ import type { ZodTypeProvider } from "fastify-type-provider-zod";
20
+ import { z } from "zod";
21
+ import type { RouteContext } from "../app.js";
22
+ import { resolveIdentity } from "../http-identity.js";
23
+ import {
24
+ AuthenticationRequiredSchema,
25
+ ErrorSchema,
26
+ ProjectIdParamsSchema,
27
+ WorkflowEnablementOwnerSchema,
28
+ WorkflowEnablementPreviewSchema,
29
+ WorkflowEnablementSchema,
30
+ } from "../schemas.js";
31
+
32
+ const EnablementParamsSchema = ProjectIdParamsSchema.extend({
33
+ enablementId: z.string().uuid(),
34
+ });
35
+ /**
36
+ * Enabling uses the project's published version; a workflow that is only
37
+ * saved (or changed since publishing) is not there yet.
38
+ */
39
+ const NotPublishedErrorSchema = z.object({
40
+ error: z.string(),
41
+ reason: z.literal("not_published"),
42
+ });
43
+ const PreviewConflictSchema = z.union([NotPublishedErrorSchema, ErrorSchema]);
44
+
45
+ const SelectionSchema = z.record(z.string(), z.string().uuid()).optional();
46
+ const PreviewBodySchema = z.object({
47
+ workflowName: z.string().min(1),
48
+ environment: z.string().min(1).optional(),
49
+ owner: WorkflowEnablementOwnerSchema.optional(),
50
+ connectionSelections: SelectionSchema,
51
+ });
52
+
53
+ export function registerWorkflowEnablementRoutes(
54
+ app: FastifyInstance,
55
+ ctx: RouteContext,
56
+ ): void {
57
+ const typed = app.withTypeProvider<ZodTypeProvider>();
58
+ const service = () => ctx.core?.workflowEnablements;
59
+
60
+ typed.route({
61
+ method: "GET",
62
+ url: "/projects/:projectId/workflow-enablements",
63
+ schema: {
64
+ params: ProjectIdParamsSchema,
65
+ querystring: z.object({ workflowName: z.string().optional() }),
66
+ response: {
67
+ 200: z.object({
68
+ items: z.array(WorkflowEnablementSchema),
69
+ /** Whether the caller may enable and manage project automations. */
70
+ canManageProjectAutomations: z.boolean(),
71
+ }),
72
+ 503: ErrorSchema,
73
+ },
74
+ },
75
+ handler: async (request, reply) => {
76
+ const workflowEnablements = service();
77
+ if (!workflowEnablements) {
78
+ return reply
79
+ .status(503)
80
+ .send({ error: "Workflow enablements unavailable" });
81
+ }
82
+ const identity = resolveIdentity(request);
83
+ const { projectId } = request.params;
84
+ return reply.send({
85
+ items: await workflowEnablements.list({
86
+ identity,
87
+ projectId,
88
+ workflowName: request.query.workflowName,
89
+ }),
90
+ canManageProjectAutomations:
91
+ workflowEnablements.mayManageProjectAutomations({
92
+ identity,
93
+ projectId,
94
+ }),
95
+ });
96
+ },
97
+ });
98
+
99
+ typed.route({
100
+ method: "POST",
101
+ url: "/projects/:projectId/workflow-enablement-preview",
102
+ schema: {
103
+ params: ProjectIdParamsSchema,
104
+ body: PreviewBodySchema,
105
+ response: {
106
+ 200: WorkflowEnablementPreviewSchema,
107
+ 403: ErrorSchema,
108
+ 409: PreviewConflictSchema,
109
+ 428: AuthenticationRequiredSchema,
110
+ 503: ErrorSchema,
111
+ },
112
+ },
113
+ handler: async (request, reply) => {
114
+ const workflowEnablements = service();
115
+ if (!workflowEnablements) {
116
+ return reply
117
+ .status(503)
118
+ .send({ error: "Workflow enablements unavailable" });
119
+ }
120
+ try {
121
+ return reply.send(
122
+ await workflowEnablements.preview({
123
+ identity: resolveIdentity(request),
124
+ projectId: request.params.projectId,
125
+ ...request.body,
126
+ }),
127
+ );
128
+ } catch (error) {
129
+ return handleEnablementError(error, reply);
130
+ }
131
+ },
132
+ });
133
+
134
+ typed.route({
135
+ method: "POST",
136
+ url: "/projects/:projectId/workflow-enablements",
137
+ schema: {
138
+ params: ProjectIdParamsSchema,
139
+ body: PreviewBodySchema.extend({ consentDigest: z.string().length(64) }),
140
+ response: {
141
+ 201: WorkflowEnablementSchema,
142
+ 403: ErrorSchema,
143
+ 409: PreviewConflictSchema,
144
+ 428: AuthenticationRequiredSchema,
145
+ 503: ErrorSchema,
146
+ },
147
+ },
148
+ handler: async (request, reply) => {
149
+ const workflowEnablements = service();
150
+ if (!workflowEnablements) {
151
+ return reply
152
+ .status(503)
153
+ .send({ error: "Workflow enablements unavailable" });
154
+ }
155
+ try {
156
+ return reply.status(201).send(
157
+ await workflowEnablements.create({
158
+ identity: resolveIdentity(request),
159
+ projectId: request.params.projectId,
160
+ ...request.body,
161
+ }),
162
+ );
163
+ } catch (error) {
164
+ return handleEnablementError(error, reply);
165
+ }
166
+ },
167
+ });
168
+
169
+ typed.route({
170
+ method: "GET",
171
+ url: "/projects/:projectId/workflow-enablements/:enablementId",
172
+ schema: {
173
+ params: EnablementParamsSchema,
174
+ response: {
175
+ 200: WorkflowEnablementSchema,
176
+ 403: ErrorSchema,
177
+ 404: ErrorSchema,
178
+ },
179
+ },
180
+ handler: async (request, reply) => {
181
+ const workflowEnablements = service();
182
+ if (!workflowEnablements) {
183
+ return reply
184
+ .status(404)
185
+ .send({ error: "Workflow enablement not found" });
186
+ }
187
+ try {
188
+ const result = await workflowEnablements.get({
189
+ identity: resolveIdentity(request),
190
+ enablementId: request.params.enablementId,
191
+ });
192
+ return result.projectId === request.params.projectId
193
+ ? reply.send(result)
194
+ : reply.status(404).send({ error: "Workflow enablement not found" });
195
+ } catch (error) {
196
+ return handleEnablementError(error, reply);
197
+ }
198
+ },
199
+ });
200
+
201
+ for (const action of ["disable", "reenable"] as const) {
202
+ typed.route({
203
+ method: "POST",
204
+ url: `/projects/:projectId/workflow-enablements/:enablementId/${action}`,
205
+ schema: {
206
+ params: EnablementParamsSchema,
207
+ response: {
208
+ 200: WorkflowEnablementSchema,
209
+ 403: ErrorSchema,
210
+ 404: ErrorSchema,
211
+ 409: PreviewConflictSchema,
212
+ },
213
+ },
214
+ handler: async (request, reply) => {
215
+ const workflowEnablements = service();
216
+ if (!workflowEnablements) {
217
+ return reply
218
+ .status(404)
219
+ .send({ error: "Workflow enablement not found" });
220
+ }
221
+ try {
222
+ const identity = resolveIdentity(request);
223
+ const current = await workflowEnablements.get({
224
+ identity,
225
+ enablementId: request.params.enablementId,
226
+ });
227
+ if (current.projectId !== request.params.projectId) {
228
+ return reply
229
+ .status(404)
230
+ .send({ error: "Workflow enablement not found" });
231
+ }
232
+ const result = await workflowEnablements[action]({
233
+ identity,
234
+ enablementId: request.params.enablementId,
235
+ });
236
+ return reply.send(result);
237
+ } catch (error) {
238
+ return handleEnablementError(error, reply);
239
+ }
240
+ },
241
+ });
242
+ }
243
+
244
+ typed.route({
245
+ method: "POST",
246
+ url: "/projects/:projectId/workflow-enablements/:enablementId/update-deployment",
247
+ schema: {
248
+ params: EnablementParamsSchema,
249
+ body: z.object({ consentDigest: z.string().length(64) }),
250
+ response: {
251
+ 200: WorkflowEnablementSchema,
252
+ 403: ErrorSchema,
253
+ 404: ErrorSchema,
254
+ 409: PreviewConflictSchema,
255
+ 428: AuthenticationRequiredSchema,
256
+ },
257
+ },
258
+ handler: async (request, reply) => {
259
+ const workflowEnablements = service();
260
+ if (!workflowEnablements) {
261
+ return reply
262
+ .status(404)
263
+ .send({ error: "Workflow enablement not found" });
264
+ }
265
+ try {
266
+ const identity = resolveIdentity(request);
267
+ const current = await workflowEnablements.get({
268
+ identity,
269
+ enablementId: request.params.enablementId,
270
+ });
271
+ if (current.projectId !== request.params.projectId) {
272
+ return reply
273
+ .status(404)
274
+ .send({ error: "Workflow enablement not found" });
275
+ }
276
+ const result = await workflowEnablements.updateDeployment({
277
+ identity,
278
+ enablementId: request.params.enablementId,
279
+ consentDigest: request.body.consentDigest,
280
+ });
281
+ return reply.send(result);
282
+ } catch (error) {
283
+ return handleEnablementError(error, reply);
284
+ }
285
+ },
286
+ });
287
+ }
288
+
289
+ function handleEnablementError(error: unknown, reply: FastifyReply) {
290
+ if (error instanceof AuthenticationRequiredError) {
291
+ return reply.status(428).send({
292
+ error: error.message,
293
+ code: "authentication_required",
294
+ environment: error.environment,
295
+ requirements: error.requirements,
296
+ });
297
+ }
298
+ if (error instanceof WorkflowEnablementNotFoundError) {
299
+ return reply.status(404).send({ error: error.message });
300
+ }
301
+ if (
302
+ error instanceof WorkflowNotFoundError ||
303
+ error instanceof ProductionDeploymentNotFoundError
304
+ ) {
305
+ return reply.status(409).send({
306
+ error:
307
+ "This workflow isn't in the project's published version yet. Publish the project's changes, then turn it on.",
308
+ reason: "not_published",
309
+ });
310
+ }
311
+ if (
312
+ error instanceof AccessDeniedError ||
313
+ error instanceof ConnectionPermissionDeniedError ||
314
+ error instanceof EnvironmentAccessDeniedError
315
+ ) {
316
+ return reply.status(403).send({ error: error.message });
317
+ }
318
+ if (
319
+ error instanceof WorkflowEnablementConsentRequiredError ||
320
+ error instanceof WorkflowEnablementConflictError ||
321
+ error instanceof ConnectionUnavailableError ||
322
+ error instanceof EnvironmentNotFoundError ||
323
+ error instanceof EnvironmentPolicyInvalidError ||
324
+ error instanceof EnvironmentIncompatibleError ||
325
+ error instanceof EnvironmentBindingUnavailableError ||
326
+ error instanceof WorkflowEnablementSuspendedError
327
+ ) {
328
+ return reply.status(409).send({ error: error.message });
329
+ }
330
+ throw error;
331
+ }