@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,222 @@
1
+ import {
2
+ DocumentNotFoundError,
3
+ DocumentPathError,
4
+ ProjectNotFoundError,
5
+ type Publication,
6
+ PublicationNotFoundError,
7
+ PublicationSlugError,
8
+ PublicationSlugTakenError,
9
+ } from "@catamorphic/core";
10
+ import type { FastifyInstance, FastifyReply } from "fastify";
11
+ import type { ZodTypeProvider } from "fastify-type-provider-zod";
12
+ import { z } from "zod";
13
+ import type { RouteContext } from "../app.js";
14
+ import { optionalIdentity, resolveIdentity } from "../http-identity.js";
15
+ import {
16
+ ErrorSchema,
17
+ ProjectIdParamsSchema,
18
+ PublicationParamsSchema,
19
+ PublicationSchema,
20
+ PublishSchema,
21
+ } from "../schemas.js";
22
+
23
+ /**
24
+ * Publications (ADR 0055): publish / list / revoke for the project's
25
+ * members, and two serving routes —
26
+ * GET /projects/:id/publications/:slug members (host auth), or public
27
+ * GET /public/:id/:slug public only, no auth (route
28
+ * config `public: true` skips the
29
+ * identity requirement)
30
+ * both stream the document as the audience's identity: an anonymous
31
+ * document-scoped one for public, the member narrowed to the document
32
+ * otherwise. Nothing else on the plugin is reachable anonymously.
33
+ */
34
+ export function registerPublicationRoutes(
35
+ app: FastifyInstance,
36
+ ctx: RouteContext,
37
+ ) {
38
+ const typed = app.withTypeProvider<ZodTypeProvider>();
39
+ const core = () => {
40
+ if (!ctx.core) {
41
+ throw Object.assign(new Error("Service not configured"), {
42
+ statusCode: 503,
43
+ });
44
+ }
45
+ return ctx.core;
46
+ };
47
+ const withUrl = (
48
+ publication: Publication,
49
+ ): Publication & { url: string } => ({
50
+ ...publication,
51
+ url:
52
+ publication.audience === "public"
53
+ ? `/public/${publication.projectId}/${publication.slug}`
54
+ : `/projects/${publication.projectId}/publications/${publication.slug}`,
55
+ });
56
+ const handleErrors = (err: unknown, reply: FastifyReply) => {
57
+ if (err instanceof ProjectNotFoundError) {
58
+ return reply.status(404).send({ error: "Project not found" });
59
+ }
60
+ if (err instanceof PublicationNotFoundError) {
61
+ return reply.status(404).send({ error: err.message });
62
+ }
63
+ if (
64
+ err instanceof DocumentPathError ||
65
+ err instanceof PublicationSlugError
66
+ ) {
67
+ return reply.status(400).send({ error: err.message });
68
+ }
69
+ if (err instanceof PublicationSlugTakenError) {
70
+ return reply.status(409).send({ error: err.message });
71
+ }
72
+ throw err;
73
+ };
74
+
75
+ typed.route({
76
+ method: "POST",
77
+ url: "/projects/:projectId/publications",
78
+ schema: {
79
+ params: ProjectIdParamsSchema,
80
+ body: PublishSchema,
81
+ response: {
82
+ 201: PublicationSchema,
83
+ 400: ErrorSchema,
84
+ 403: ErrorSchema,
85
+ 404: ErrorSchema,
86
+ 409: ErrorSchema,
87
+ },
88
+ },
89
+ handler: async (request, reply) => {
90
+ const allowed = ctx.features.publications;
91
+ if (
92
+ allowed === false ||
93
+ (allowed === "members" && request.body.audience === "public")
94
+ ) {
95
+ return reply.status(403).send({
96
+ error:
97
+ allowed === false
98
+ ? "Publishing is turned off on this server"
99
+ : "Public links are turned off on this server; publish to members instead",
100
+ });
101
+ }
102
+ try {
103
+ const publication = await core().publications.publish({
104
+ identity: resolveIdentity(request),
105
+ projectId: request.params.projectId,
106
+ path: request.body.path,
107
+ audience: request.body.audience,
108
+ ...(request.body.slug ? { slug: request.body.slug } : {}),
109
+ });
110
+ return reply.status(201).send(withUrl(publication));
111
+ } catch (err) {
112
+ return handleErrors(err, reply);
113
+ }
114
+ },
115
+ });
116
+
117
+ typed.route({
118
+ method: "GET",
119
+ url: "/projects/:projectId/publications",
120
+ schema: {
121
+ params: ProjectIdParamsSchema,
122
+ response: { 200: PublicationSchema.array(), 404: ErrorSchema },
123
+ },
124
+ handler: async (request, reply) => {
125
+ try {
126
+ const list = await core().publications.list({
127
+ identity: resolveIdentity(request),
128
+ projectId: request.params.projectId,
129
+ });
130
+ return reply.send(list.map(withUrl));
131
+ } catch (err) {
132
+ return handleErrors(err, reply);
133
+ }
134
+ },
135
+ });
136
+
137
+ typed.route({
138
+ method: "DELETE",
139
+ url: "/projects/:projectId/publications/:slug",
140
+ schema: {
141
+ params: PublicationParamsSchema,
142
+ response: { 204: z.null(), 404: ErrorSchema },
143
+ },
144
+ handler: async (request, reply) => {
145
+ try {
146
+ await core().publications.revoke({
147
+ identity: resolveIdentity(request),
148
+ projectId: request.params.projectId,
149
+ slug: request.params.slug,
150
+ });
151
+ return reply.status(204).send(null);
152
+ } catch (err) {
153
+ return handleErrors(err, reply);
154
+ }
155
+ },
156
+ });
157
+
158
+ const serve = async (
159
+ reply: FastifyReply,
160
+ args: {
161
+ projectId: string;
162
+ slug: string;
163
+ caller: Parameters<typeof optionalIdentity>[0];
164
+ },
165
+ ) => {
166
+ const resolved = await core().publications.resolve({
167
+ projectId: args.projectId,
168
+ slug: args.slug,
169
+ caller: optionalIdentity(args.caller),
170
+ });
171
+ // One uniform answer for unknown, revoked, or not-for-you: no oracle.
172
+ if (!resolved) return reply.status(404).send({ error: "Not found" });
173
+ try {
174
+ const doc = await core().documents.readBytes({
175
+ identity: resolved.identity,
176
+ projectId: args.projectId,
177
+ path: resolved.path,
178
+ });
179
+ reply.header("content-type", doc.contentType);
180
+ reply.header("cache-control", "no-store");
181
+ if (doc.version !== undefined) {
182
+ reply.header("x-catamorphic-document-version", String(doc.version));
183
+ }
184
+ return reply.send(Buffer.from(doc.bytes));
185
+ } catch (err) {
186
+ if (
187
+ err instanceof DocumentNotFoundError ||
188
+ err instanceof DocumentPathError
189
+ ) {
190
+ return reply.status(404).send({ error: "Not found" });
191
+ }
192
+ throw err;
193
+ }
194
+ };
195
+
196
+ // Members (or anyone signed in, for public publications).
197
+ typed.route({
198
+ method: "GET",
199
+ url: "/projects/:projectId/publications/:slug",
200
+ schema: { params: PublicationParamsSchema },
201
+ handler: (request, reply) =>
202
+ serve(reply, {
203
+ projectId: request.params.projectId,
204
+ slug: request.params.slug,
205
+ caller: request,
206
+ }),
207
+ });
208
+
209
+ // Public: no identity required (the hook honours `config.public`).
210
+ typed.route({
211
+ method: "GET",
212
+ url: "/public/:projectId/:slug",
213
+ config: { public: true },
214
+ schema: { params: PublicationParamsSchema },
215
+ handler: (request, reply) =>
216
+ serve(reply, {
217
+ projectId: request.params.projectId,
218
+ slug: request.params.slug,
219
+ caller: request,
220
+ }),
221
+ });
222
+ }
@@ -0,0 +1,86 @@
1
+ import {
2
+ AuthenticationRequiredError,
3
+ EnvironmentAccessDeniedError,
4
+ EnvironmentBindingUnavailableError,
5
+ EnvironmentCapacityError,
6
+ EnvironmentIncompatibleError,
7
+ EnvironmentNotFoundError,
8
+ NoCompatibleEnvironmentError,
9
+ PluginSecretsMissingError,
10
+ ProductionDeploymentNotFoundError,
11
+ ProjectNotFoundError,
12
+ RunCapabilityError,
13
+ RunEnrollmentConflictError,
14
+ RunInputInvalidError,
15
+ SandboxProviderNotConfiguredError,
16
+ TenantActiveRunLimitError,
17
+ WorkflowNotFoundError,
18
+ } from "@catamorphic/core";
19
+ import type { FastifyReply } from "fastify";
20
+
21
+ /**
22
+ * Maps the errors a run trigger (async start or sync call) can raise onto
23
+ * HTTP statuses. Returns the reply when handled, `null` when the error is
24
+ * not a trigger error and should propagate.
25
+ */
26
+ export function replyForTriggerError(
27
+ err: unknown,
28
+ reply: FastifyReply,
29
+ ): FastifyReply | null {
30
+ if (err instanceof ProjectNotFoundError) {
31
+ return reply.status(404).send({ error: "Project not found" });
32
+ }
33
+ if (err instanceof WorkflowNotFoundError) {
34
+ return reply.status(404).send({ error: "Workflow not found" });
35
+ }
36
+ if (err instanceof ProductionDeploymentNotFoundError) {
37
+ return reply.status(409).send({ error: err.message });
38
+ }
39
+ if (err instanceof RunEnrollmentConflictError) {
40
+ return reply.status(409).send({ error: err.message });
41
+ }
42
+ if (err instanceof AuthenticationRequiredError) {
43
+ return reply.status(428).send({
44
+ error: err.message,
45
+ code: "authentication_required",
46
+ environment: err.environment,
47
+ requirements: [...err.requirements],
48
+ });
49
+ }
50
+ if (err instanceof EnvironmentAccessDeniedError) {
51
+ return reply.status(403).send({ error: err.message });
52
+ }
53
+ if (err instanceof EnvironmentNotFoundError) {
54
+ return reply.status(404).send({ error: err.message });
55
+ }
56
+ if (
57
+ err instanceof EnvironmentCapacityError ||
58
+ err instanceof EnvironmentBindingUnavailableError
59
+ ) {
60
+ return reply.status(409).send({ error: err.message });
61
+ }
62
+ if (
63
+ err instanceof EnvironmentIncompatibleError ||
64
+ err instanceof NoCompatibleEnvironmentError
65
+ ) {
66
+ return reply.status(422).send({ error: err.message });
67
+ }
68
+ if (err instanceof TenantActiveRunLimitError) {
69
+ return reply.status(429).send({ error: err.message });
70
+ }
71
+ if (err instanceof RunCapabilityError) {
72
+ return reply.status(409).send({ error: err.message });
73
+ }
74
+ if (err instanceof PluginSecretsMissingError) {
75
+ return reply.status(400).send({ error: err.message });
76
+ }
77
+ if (err instanceof RunInputInvalidError) {
78
+ return reply.status(400).send({ error: err.message });
79
+ }
80
+ if (err instanceof SandboxProviderNotConfiguredError) {
81
+ // The host chooses and constructs the execution backend; the library
82
+ // names none of them.
83
+ return reply.status(503).send({ error: err.message });
84
+ }
85
+ return null;
86
+ }
@@ -0,0 +1,253 @@
1
+ import {
2
+ RunCapabilityError,
3
+ RunNotFoundError,
4
+ RunPauseNotFoundError,
5
+ RunResumeConflictError,
6
+ } from "@catamorphic/core";
7
+ import type { FastifyInstance, FastifyReply } from "fastify";
8
+ import type { ZodTypeProvider } from "fastify-type-provider-zod";
9
+ import { z } from "zod";
10
+ import type { RouteContext } from "../app.js";
11
+ import { resolveIdentity } from "../http-identity.js";
12
+ import {
13
+ BatchItemSchema,
14
+ BatchItemStepSchema,
15
+ CancelRunSchema,
16
+ ErrorSchema,
17
+ ListSchema,
18
+ ResumeRunPauseSchema,
19
+ RunDetailSchema,
20
+ RunIdParamsSchema,
21
+ RunItemParamsSchema,
22
+ RunItemsQuerySchema,
23
+ RunPauseParamsSchema,
24
+ RunSchema,
25
+ RunStepAttemptParamsSchema,
26
+ } from "../schemas.js";
27
+
28
+ export function registerRunRoutes(app: FastifyInstance, ctx: RouteContext) {
29
+ const typed = app.withTypeProvider<ZodTypeProvider>();
30
+
31
+ typed.route({
32
+ method: "GET",
33
+ url: "/runs/:runId",
34
+ schema: {
35
+ params: RunIdParamsSchema,
36
+ response: { 200: RunDetailSchema, 404: ErrorSchema, 503: ErrorSchema },
37
+ },
38
+ handler: async (request, reply) => {
39
+ if (!ctx.core)
40
+ return reply.status(503).send({ error: "Service not configured" });
41
+ try {
42
+ return reply.send(
43
+ await ctx.core.runs.get({
44
+ identity: resolveIdentity(request),
45
+ runId: request.params.runId,
46
+ }),
47
+ );
48
+ } catch (error) {
49
+ if (error instanceof RunNotFoundError) {
50
+ return reply.status(404).send({ error: error.message });
51
+ }
52
+ throw error;
53
+ }
54
+ },
55
+ });
56
+
57
+ typed.route({
58
+ method: "POST",
59
+ url: "/runs/:runId/cancel",
60
+ schema: {
61
+ params: RunIdParamsSchema,
62
+ body: CancelRunSchema,
63
+ response: {
64
+ 200: RunSchema,
65
+ 404: ErrorSchema,
66
+ 409: ErrorSchema,
67
+ 503: ErrorSchema,
68
+ },
69
+ },
70
+ handler: async (request, reply) => {
71
+ if (!ctx.core)
72
+ return reply.status(503).send({ error: "Service not configured" });
73
+ try {
74
+ return reply.send(
75
+ await ctx.core.runs.cancel({
76
+ identity: resolveIdentity(request),
77
+ runId: request.params.runId,
78
+ reason: request.body.reason,
79
+ }),
80
+ );
81
+ } catch (error) {
82
+ return handleRunError({ error, reply });
83
+ }
84
+ },
85
+ });
86
+
87
+ typed.route({
88
+ method: "POST",
89
+ url: "/runs/:runId/pause",
90
+ schema: {
91
+ params: RunIdParamsSchema,
92
+ response: {
93
+ 200: RunSchema,
94
+ 404: ErrorSchema,
95
+ 409: ErrorSchema,
96
+ 503: ErrorSchema,
97
+ },
98
+ },
99
+ handler: async (request, reply) => {
100
+ if (!ctx.core)
101
+ return reply.status(503).send({ error: "Service not configured" });
102
+ try {
103
+ return reply.send(
104
+ await ctx.core.runs.pause({
105
+ identity: resolveIdentity(request),
106
+ runId: request.params.runId,
107
+ }),
108
+ );
109
+ } catch (error) {
110
+ return handleRunError({ error, reply });
111
+ }
112
+ },
113
+ });
114
+
115
+ typed.route({
116
+ method: "POST",
117
+ url: "/runs/:runId/resume",
118
+ schema: {
119
+ params: RunIdParamsSchema,
120
+ response: {
121
+ 200: RunSchema,
122
+ 404: ErrorSchema,
123
+ 409: ErrorSchema,
124
+ 503: ErrorSchema,
125
+ },
126
+ },
127
+ handler: async (request, reply) => {
128
+ if (!ctx.core)
129
+ return reply.status(503).send({ error: "Service not configured" });
130
+ try {
131
+ return reply.send(
132
+ await ctx.core.runs.resume({
133
+ identity: resolveIdentity(request),
134
+ runId: request.params.runId,
135
+ }),
136
+ );
137
+ } catch (error) {
138
+ return handleRunError({ error, reply });
139
+ }
140
+ },
141
+ });
142
+
143
+ typed.route({
144
+ method: "POST",
145
+ url: "/runs/:runId/pauses/:pauseId/resume",
146
+ schema: {
147
+ params: RunPauseParamsSchema,
148
+ body: ResumeRunPauseSchema,
149
+ response: {
150
+ 200: RunSchema,
151
+ 404: ErrorSchema,
152
+ 409: ErrorSchema,
153
+ 503: ErrorSchema,
154
+ },
155
+ },
156
+ handler: async (request, reply) => {
157
+ if (!ctx.core)
158
+ return reply.status(503).send({ error: "Service not configured" });
159
+ try {
160
+ return reply.send(
161
+ await ctx.core.runs.resumePause({
162
+ identity: resolveIdentity(request),
163
+ runId: request.params.runId,
164
+ pauseId: request.params.pauseId,
165
+ idempotencyKey: request.body.idempotencyKey,
166
+ value: request.body.value,
167
+ }),
168
+ );
169
+ } catch (error) {
170
+ return handleRunError({ error, reply });
171
+ }
172
+ },
173
+ });
174
+
175
+ typed.route({
176
+ method: "GET",
177
+ url: "/runs/:runId/steps/:workflowStepAttemptId/items",
178
+ schema: {
179
+ params: RunStepAttemptParamsSchema,
180
+ querystring: RunItemsQuerySchema,
181
+ response: {
182
+ 200: ListSchema(BatchItemSchema),
183
+ 404: ErrorSchema,
184
+ 409: ErrorSchema,
185
+ 503: ErrorSchema,
186
+ },
187
+ },
188
+ handler: async (request, reply) => {
189
+ if (!ctx.core)
190
+ return reply.status(503).send({ error: "Service not configured" });
191
+ try {
192
+ return reply.send(
193
+ await ctx.core.runs.listItems({
194
+ identity: resolveIdentity(request),
195
+ runId: request.params.runId,
196
+ workflowStepAttemptId: request.params.workflowStepAttemptId,
197
+ status: request.query.status,
198
+ limit: request.query.limit,
199
+ offset: request.query.offset,
200
+ }),
201
+ );
202
+ } catch (error) {
203
+ return handleRunError({ error, reply });
204
+ }
205
+ },
206
+ });
207
+
208
+ typed.route({
209
+ method: "GET",
210
+ url: "/runs/:runId/steps/:workflowStepAttemptId/items/:itemId/steps",
211
+ schema: {
212
+ params: RunItemParamsSchema,
213
+ response: {
214
+ 200: z.array(BatchItemStepSchema),
215
+ 404: ErrorSchema,
216
+ 409: ErrorSchema,
217
+ 503: ErrorSchema,
218
+ },
219
+ },
220
+ handler: async (request, reply) => {
221
+ if (!ctx.core)
222
+ return reply.status(503).send({ error: "Service not configured" });
223
+ try {
224
+ return reply.send(
225
+ await ctx.core.runs.listItemSteps({
226
+ identity: resolveIdentity(request),
227
+ runId: request.params.runId,
228
+ workflowStepAttemptId: request.params.workflowStepAttemptId,
229
+ itemId: request.params.itemId,
230
+ }),
231
+ );
232
+ } catch (error) {
233
+ return handleRunError({ error, reply });
234
+ }
235
+ },
236
+ });
237
+ }
238
+
239
+ function handleRunError(args: { error: unknown; reply: FastifyReply }) {
240
+ if (
241
+ args.error instanceof RunNotFoundError ||
242
+ args.error instanceof RunPauseNotFoundError
243
+ ) {
244
+ return args.reply.status(404).send({ error: args.error.message });
245
+ }
246
+ if (
247
+ args.error instanceof RunCapabilityError ||
248
+ args.error instanceof RunResumeConflictError
249
+ ) {
250
+ return args.reply.status(409).send({ error: args.error.message });
251
+ }
252
+ throw args.error;
253
+ }