@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,323 @@
1
+ import {
2
+ ProjectNotFoundError,
3
+ RunCapabilityError,
4
+ RunResumeConflictError,
5
+ RunSignalNotFoundError,
6
+ WorkflowNotFoundError,
7
+ } from "@catamorphic/core";
8
+ import type { FastifyInstance } 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 {
14
+ AuthenticationRequiredSchema,
15
+ CallRunSchema,
16
+ CancelRunByKeySchema,
17
+ ErrorSchema,
18
+ ListSchema,
19
+ RefQuerySchema,
20
+ RunCallOutcomeSchema,
21
+ RunSchema,
22
+ RunsQuerySchema,
23
+ SignalRunSchema,
24
+ TriggerRunSchema,
25
+ WorkflowDetailSchema,
26
+ WorkflowNameParamsSchema,
27
+ WorkflowSummarySchema,
28
+ } from "../schemas.js";
29
+ import { replyForTriggerError } from "./run-errors.js";
30
+ import { attachTriggerKindDisplays } from "./triggers.js";
31
+
32
+ export function registerWorkflowRoutes(
33
+ app: FastifyInstance,
34
+ ctx: RouteContext,
35
+ ) {
36
+ const typed = app.withTypeProvider<ZodTypeProvider>();
37
+
38
+ typed.route({
39
+ method: "GET",
40
+ url: "/projects/:projectId/workflows",
41
+ schema: {
42
+ params: WorkflowNameParamsSchema.pick({ projectId: true }),
43
+ querystring: RefQuerySchema,
44
+ response: {
45
+ 200: WorkflowSummarySchema.array(),
46
+ 404: ErrorSchema,
47
+ 503: ErrorSchema,
48
+ },
49
+ },
50
+ handler: async (request, reply) => {
51
+ if (!ctx.core)
52
+ return reply.status(503).send({ error: "Service not configured" });
53
+ const identity = resolveIdentity(request);
54
+ try {
55
+ const workflows = await ctx.core.workflows.list({
56
+ identity,
57
+ projectId: request.params.projectId,
58
+ ref: request.query.ref,
59
+ });
60
+ return reply.send(workflows);
61
+ } catch (err) {
62
+ if (err instanceof ProjectNotFoundError) {
63
+ return reply.status(404).send({ error: "Project not found" });
64
+ }
65
+ throw err;
66
+ }
67
+ },
68
+ });
69
+
70
+ typed.route({
71
+ method: "GET",
72
+ url: "/projects/:projectId/workflows/:name",
73
+ schema: {
74
+ params: WorkflowNameParamsSchema,
75
+ querystring: RefQuerySchema,
76
+ response: {
77
+ 200: WorkflowDetailSchema,
78
+ 404: ErrorSchema,
79
+ 503: ErrorSchema,
80
+ },
81
+ },
82
+ handler: async (request, reply) => {
83
+ if (!ctx.core)
84
+ return reply.status(503).send({ error: "Service not configured" });
85
+ const identity = resolveIdentity(request);
86
+ const { projectId, name } = request.params;
87
+ const { ref } = request.query;
88
+ try {
89
+ const graph = await ctx.core.workflows.get({
90
+ identity,
91
+ projectId,
92
+ workflowName: name,
93
+ ref,
94
+ });
95
+ return reply.send(attachTriggerKindDisplays(ctx.core, graph));
96
+ } catch (err) {
97
+ if (err instanceof ProjectNotFoundError) {
98
+ return reply.status(404).send({ error: "Project not found" });
99
+ }
100
+ if (err instanceof WorkflowNotFoundError) {
101
+ return reply.status(404).send({ error: "Workflow not found" });
102
+ }
103
+ throw err;
104
+ }
105
+ },
106
+ });
107
+
108
+ typed.route({
109
+ method: "POST",
110
+ url: "/projects/:projectId/workflows/:name/runs",
111
+ schema: {
112
+ params: WorkflowNameParamsSchema,
113
+ body: TriggerRunSchema,
114
+ response: {
115
+ 201: RunSchema,
116
+ 400: ErrorSchema,
117
+ 403: ErrorSchema,
118
+ 404: ErrorSchema,
119
+ 409: ErrorSchema,
120
+ 422: ErrorSchema,
121
+ 428: AuthenticationRequiredSchema,
122
+ 429: ErrorSchema,
123
+ 503: ErrorSchema,
124
+ },
125
+ },
126
+ handler: async (request, reply) => {
127
+ if (!ctx.core)
128
+ return reply.status(503).send({ error: "Service not configured" });
129
+ const identity = resolveIdentity(request);
130
+ try {
131
+ const run = await ctx.core.runs.triggerProduction({
132
+ identity,
133
+ projectId: request.params.projectId,
134
+ workflowName: request.params.name,
135
+ environment: request.body.environment,
136
+ input: request.body.input,
137
+ ...(request.body.correlationKey === undefined
138
+ ? {}
139
+ : { correlationKey: request.body.correlationKey }),
140
+ ...(request.body.onConflict === undefined
141
+ ? {}
142
+ : { onConflict: request.body.onConflict }),
143
+ });
144
+ return reply.status(201).send(run);
145
+ } catch (err) {
146
+ return replyForTriggerError(err, reply) ?? Promise.reject(err);
147
+ }
148
+ },
149
+ });
150
+
151
+ typed.route({
152
+ method: "POST",
153
+ url: "/projects/:projectId/workflows/:name/calls",
154
+ schema: {
155
+ params: WorkflowNameParamsSchema,
156
+ body: CallRunSchema,
157
+ response: {
158
+ 200: RunCallOutcomeSchema,
159
+ 400: ErrorSchema,
160
+ 403: ErrorSchema,
161
+ 404: ErrorSchema,
162
+ 409: ErrorSchema,
163
+ 422: ErrorSchema,
164
+ 428: AuthenticationRequiredSchema,
165
+ 429: ErrorSchema,
166
+ 503: ErrorSchema,
167
+ },
168
+ },
169
+ handler: async (request, reply) => {
170
+ if (!ctx.core)
171
+ return reply.status(503).send({ error: "Service not configured" });
172
+ try {
173
+ // Sync is a calling mode, not a workflow kind: the run is triggered
174
+ // exactly like an async one and driven inline until it settles or
175
+ // reaches a durable wait, at which point `suspended` hands the run
176
+ // id back for polling.
177
+ const outcome = await ctx.core.runs.call({
178
+ identity: resolveIdentity(request),
179
+ projectId: request.params.projectId,
180
+ workflowName: request.params.name,
181
+ environment: request.body.environment,
182
+ input: request.body.input,
183
+ ...(request.body.correlationKey === undefined
184
+ ? {}
185
+ : { correlationKey: request.body.correlationKey }),
186
+ ...(request.body.onConflict === undefined
187
+ ? {}
188
+ : { onConflict: request.body.onConflict }),
189
+ ...(request.body.budgetMs === undefined
190
+ ? {}
191
+ : { budgetMs: request.body.budgetMs }),
192
+ });
193
+ return reply.send(outcome);
194
+ } catch (err) {
195
+ return replyForTriggerError(err, reply) ?? Promise.reject(err);
196
+ }
197
+ },
198
+ });
199
+
200
+ typed.route({
201
+ method: "GET",
202
+ url: "/projects/:projectId/workflows/:name/runs",
203
+ schema: {
204
+ params: WorkflowNameParamsSchema,
205
+ querystring: RunsQuerySchema,
206
+ response: {
207
+ 200: ListSchema(RunSchema),
208
+ 404: ErrorSchema,
209
+ 503: ErrorSchema,
210
+ },
211
+ },
212
+ handler: async (request, reply) => {
213
+ if (!ctx.core)
214
+ return reply.status(503).send({ error: "Service not configured" });
215
+ const identity = resolveIdentity(request);
216
+ const { projectId, name } = request.params;
217
+ try {
218
+ const result = await ctx.core.runs.list({
219
+ identity,
220
+ projectId,
221
+ workflowName: name,
222
+ limit: request.query.limit,
223
+ offset: request.query.offset,
224
+ ...(request.query.correlationKey === undefined
225
+ ? {}
226
+ : { correlationKey: request.query.correlationKey }),
227
+ });
228
+ return reply.send(result);
229
+ } catch (err) {
230
+ if (err instanceof ProjectNotFoundError) {
231
+ return reply.status(404).send({ error: "Project not found" });
232
+ }
233
+ throw err;
234
+ }
235
+ },
236
+ });
237
+
238
+ typed.route({
239
+ method: "POST",
240
+ url: "/projects/:projectId/workflows/:name/signals",
241
+ schema: {
242
+ params: WorkflowNameParamsSchema,
243
+ body: SignalRunSchema,
244
+ response: {
245
+ 200: RunSchema,
246
+ 404: ErrorSchema,
247
+ 409: ErrorSchema,
248
+ 503: ErrorSchema,
249
+ },
250
+ },
251
+ handler: async (request, reply) => {
252
+ if (!ctx.core)
253
+ return reply.status(503).send({ error: "Service not configured" });
254
+ try {
255
+ return reply.send(
256
+ await ctx.core.runs.signalByKey({
257
+ identity: resolveIdentity(request),
258
+ projectId: request.params.projectId,
259
+ workflowName: request.params.name,
260
+ correlationKey: request.body.correlationKey,
261
+ signal: request.body.signal,
262
+ idempotencyKey: request.body.idempotencyKey,
263
+ value: request.body.value,
264
+ }),
265
+ );
266
+ } catch (err) {
267
+ if (err instanceof ProjectNotFoundError) {
268
+ return reply.status(404).send({ error: "Project not found" });
269
+ }
270
+ if (err instanceof RunSignalNotFoundError) {
271
+ return reply.status(404).send({ error: err.message });
272
+ }
273
+ // The pause can time out or be cancelled between resolving it and
274
+ // resuming it, which is a conflict the caller can retry against — not
275
+ // a server fault.
276
+ if (
277
+ err instanceof RunResumeConflictError ||
278
+ err instanceof RunCapabilityError
279
+ ) {
280
+ return reply.status(409).send({ error: err.message });
281
+ }
282
+ throw err;
283
+ }
284
+ },
285
+ });
286
+
287
+ typed.route({
288
+ method: "POST",
289
+ url: "/projects/:projectId/workflows/:name/cancellations",
290
+ schema: {
291
+ params: WorkflowNameParamsSchema,
292
+ body: CancelRunByKeySchema,
293
+ response: {
294
+ 200: RunSchema,
295
+ 204: z.null(),
296
+ 404: ErrorSchema,
297
+ 503: ErrorSchema,
298
+ },
299
+ },
300
+ handler: async (request, reply) => {
301
+ if (!ctx.core)
302
+ return reply.status(503).send({ error: "Service not configured" });
303
+ try {
304
+ const run = await ctx.core.runs.cancelByKey({
305
+ identity: resolveIdentity(request),
306
+ projectId: request.params.projectId,
307
+ workflowName: request.params.name,
308
+ correlationKey: request.body.correlationKey,
309
+ ...(request.body.reason === undefined
310
+ ? {}
311
+ : { reason: request.body.reason }),
312
+ });
313
+ // A repeated opt-out is a no-op, not an error.
314
+ return run ? reply.send(run) : reply.status(204).send(null);
315
+ } catch (err) {
316
+ if (err instanceof ProjectNotFoundError) {
317
+ return reply.status(404).send({ error: "Project not found" });
318
+ }
319
+ throw err;
320
+ }
321
+ },
322
+ });
323
+ }