@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.
- package/LICENSE.txt +202 -0
- package/NOTICE.txt +32 -0
- package/README.md +143 -2
- package/dist/app-presentation-tools.d.ts +4 -0
- package/dist/app-presentation-tools.d.ts.map +1 -0
- package/dist/app.d.ts +27 -0
- package/dist/app.d.ts.map +1 -0
- package/dist/chunk-baagvvm3.js +164 -0
- package/dist/chunk-baagvvm3.js.map +10 -0
- package/dist/http-identity.d.ts +52 -0
- package/dist/http-identity.d.ts.map +1 -0
- package/dist/hung-up.d.ts +7 -0
- package/dist/hung-up.d.ts.map +1 -0
- package/dist/index.d.ts +11 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +10884 -0
- package/dist/index.js.map +56 -0
- package/dist/mcp-shared.d.ts +117 -0
- package/dist/mcp-shared.d.ts.map +1 -0
- package/dist/mcp.d.ts +6 -0
- package/dist/mcp.d.ts.map +1 -0
- package/dist/mcp.js +13 -0
- package/dist/mcp.js.map +9 -0
- package/dist/plugin.d.ts +113 -0
- package/dist/plugin.d.ts.map +1 -0
- package/dist/project-capabilities.d.ts +25 -0
- package/dist/project-capabilities.d.ts.map +1 -0
- package/dist/project-mcp-program.d.ts +15 -0
- package/dist/project-mcp-program.d.ts.map +1 -0
- package/dist/project-mcp-surface.d.ts +16 -0
- package/dist/project-mcp-surface.d.ts.map +1 -0
- package/dist/request-logging.d.ts +18 -0
- package/dist/request-logging.d.ts.map +1 -0
- package/dist/routes/agent-capabilities.d.ts +4 -0
- package/dist/routes/agent-capabilities.d.ts.map +1 -0
- package/dist/routes/agent.d.ts +4 -0
- package/dist/routes/agent.d.ts.map +1 -0
- package/dist/routes/apps-mcp.d.ts +4 -0
- package/dist/routes/apps-mcp.d.ts.map +1 -0
- package/dist/routes/apps.d.ts +12 -0
- package/dist/routes/apps.d.ts.map +1 -0
- package/dist/routes/client-runners.d.ts +11 -0
- package/dist/routes/client-runners.d.ts.map +1 -0
- package/dist/routes/code-hosts.d.ts +11 -0
- package/dist/routes/code-hosts.d.ts.map +1 -0
- package/dist/routes/connection-mcp.d.ts +10 -0
- package/dist/routes/connection-mcp.d.ts.map +1 -0
- package/dist/routes/connections.d.ts +4 -0
- package/dist/routes/connections.d.ts.map +1 -0
- package/dist/routes/documents.d.ts +11 -0
- package/dist/routes/documents.d.ts.map +1 -0
- package/dist/routes/environments.d.ts +4 -0
- package/dist/routes/environments.d.ts.map +1 -0
- package/dist/routes/git-gateway.d.ts +21 -0
- package/dist/routes/git-gateway.d.ts.map +1 -0
- package/dist/routes/http-gateway.d.ts +13 -0
- package/dist/routes/http-gateway.d.ts.map +1 -0
- package/dist/routes/me.d.ts +12 -0
- package/dist/routes/me.d.ts.map +1 -0
- package/dist/routes/memberships.d.ts +12 -0
- package/dist/routes/memberships.d.ts.map +1 -0
- package/dist/routes/model-gateway.d.ts +12 -0
- package/dist/routes/model-gateway.d.ts.map +1 -0
- package/dist/routes/notifications.d.ts +4 -0
- package/dist/routes/notifications.d.ts.map +1 -0
- package/dist/routes/parse.d.ts +5 -0
- package/dist/routes/parse.d.ts.map +1 -0
- package/dist/routes/personal-environment.d.ts +13 -0
- package/dist/routes/personal-environment.d.ts.map +1 -0
- package/dist/routes/plugins.d.ts +4 -0
- package/dist/routes/plugins.d.ts.map +1 -0
- package/dist/routes/project-mcp.d.ts +25 -0
- package/dist/routes/project-mcp.d.ts.map +1 -0
- package/dist/routes/projects.d.ts +4 -0
- package/dist/routes/projects.d.ts.map +1 -0
- package/dist/routes/publications.d.ts +15 -0
- package/dist/routes/publications.d.ts.map +1 -0
- package/dist/routes/run-errors.d.ts +8 -0
- package/dist/routes/run-errors.d.ts.map +1 -0
- package/dist/routes/runs.d.ts +4 -0
- package/dist/routes/runs.d.ts.map +1 -0
- package/dist/routes/session-artifacts.d.ts +27 -0
- package/dist/routes/session-artifacts.d.ts.map +1 -0
- package/dist/routes/session-mailboxes.d.ts +4 -0
- package/dist/routes/session-mailboxes.d.ts.map +1 -0
- package/dist/routes/session-previews.d.ts +40 -0
- package/dist/routes/session-previews.d.ts.map +1 -0
- package/dist/routes/session-terminals.d.ts +15 -0
- package/dist/routes/session-terminals.d.ts.map +1 -0
- package/dist/routes/triggers.d.ts +20 -0
- package/dist/routes/triggers.d.ts.map +1 -0
- package/dist/routes/watchers.d.ts +4 -0
- package/dist/routes/watchers.d.ts.map +1 -0
- package/dist/routes/webhooks.d.ts +11 -0
- package/dist/routes/webhooks.d.ts.map +1 -0
- package/dist/routes/workflow-enablements.d.ts +4 -0
- package/dist/routes/workflow-enablements.d.ts.map +1 -0
- package/dist/routes/workflows.d.ts +4 -0
- package/dist/routes/workflows.d.ts.map +1 -0
- package/dist/schemas.d.ts +17776 -0
- package/dist/schemas.d.ts.map +1 -0
- package/dist/session-artifact-tools.d.ts +5 -0
- package/dist/session-artifact-tools.d.ts.map +1 -0
- package/dist/session-event-stream.d.ts +45 -0
- package/dist/session-event-stream.d.ts.map +1 -0
- package/dist/spa.d.ts +24 -0
- package/dist/spa.d.ts.map +1 -0
- package/dist/telemetry.d.ts +4 -0
- package/dist/telemetry.d.ts.map +1 -0
- package/package.json +73 -4
- package/src/app-presentation-tools.ts +46 -0
- package/src/app.ts +115 -0
- package/src/http-identity.ts +145 -0
- package/src/hung-up.ts +13 -0
- package/src/index.ts +25 -0
- package/src/mcp-shared.ts +331 -0
- package/src/mcp.ts +10 -0
- package/src/plugin.ts +332 -0
- package/src/project-capabilities.ts +153 -0
- package/src/project-mcp-program.ts +607 -0
- package/src/project-mcp-surface.ts +881 -0
- package/src/request-logging.ts +47 -0
- package/src/routes/agent-capabilities.ts +109 -0
- package/src/routes/agent.ts +1566 -0
- package/src/routes/apps-mcp.ts +303 -0
- package/src/routes/apps.ts +692 -0
- package/src/routes/client-runners.ts +251 -0
- package/src/routes/code-hosts.ts +247 -0
- package/src/routes/connection-mcp.ts +135 -0
- package/src/routes/connections.ts +494 -0
- package/src/routes/documents.ts +538 -0
- package/src/routes/environments.ts +72 -0
- package/src/routes/git-gateway.ts +106 -0
- package/src/routes/http-gateway.ts +141 -0
- package/src/routes/me.ts +122 -0
- package/src/routes/memberships.ts +173 -0
- package/src/routes/model-gateway.ts +134 -0
- package/src/routes/notifications.ts +101 -0
- package/src/routes/parse.ts +32 -0
- package/src/routes/personal-environment.ts +146 -0
- package/src/routes/plugins.ts +341 -0
- package/src/routes/project-mcp.ts +394 -0
- package/src/routes/projects.ts +708 -0
- package/src/routes/publications.ts +222 -0
- package/src/routes/run-errors.ts +86 -0
- package/src/routes/runs.ts +253 -0
- package/src/routes/session-artifacts.ts +293 -0
- package/src/routes/session-mailboxes.ts +80 -0
- package/src/routes/session-previews.ts +224 -0
- package/src/routes/session-terminals.ts +209 -0
- package/src/routes/triggers.ts +241 -0
- package/src/routes/watchers.ts +94 -0
- package/src/routes/webhooks.ts +171 -0
- package/src/routes/workflow-enablements.ts +331 -0
- package/src/routes/workflows.ts +323 -0
- package/src/schemas.ts +2735 -0
- package/src/session-artifact-tools.ts +170 -0
- package/src/session-event-stream.ts +122 -0
- package/src/spa.ts +135 -0
- package/src/telemetry.ts +93 -0
|
@@ -0,0 +1,209 @@
|
|
|
1
|
+
import {
|
|
2
|
+
AgentSessionNotFoundError,
|
|
3
|
+
EnvironmentBindingUnavailableError,
|
|
4
|
+
EnvironmentCapacityError,
|
|
5
|
+
EnvironmentNotFoundError,
|
|
6
|
+
SessionTerminalNotFoundError,
|
|
7
|
+
SessionTerminalSecretsError,
|
|
8
|
+
SessionWorkspaceUnavailableError,
|
|
9
|
+
} from "@catamorphic/core";
|
|
10
|
+
import type { FastifyInstance, FastifyReply } from "fastify";
|
|
11
|
+
import type { ZodTypeProvider } from "fastify-type-provider-zod";
|
|
12
|
+
import type { RouteContext } from "../app.js";
|
|
13
|
+
import { resolveIdentity } from "../http-identity.js";
|
|
14
|
+
import {
|
|
15
|
+
AgentSessionIdParamsSchema,
|
|
16
|
+
ErrorSchema,
|
|
17
|
+
OkSchema,
|
|
18
|
+
OpenSessionTerminalSchema,
|
|
19
|
+
ResizeSessionTerminalSchema,
|
|
20
|
+
SessionTerminalInputSchema,
|
|
21
|
+
SessionTerminalOutputQuerySchema,
|
|
22
|
+
SessionTerminalOutputSchema,
|
|
23
|
+
SessionTerminalParamsSchema,
|
|
24
|
+
SessionTerminalSchema,
|
|
25
|
+
SessionWorkspaceErrorSchema,
|
|
26
|
+
} from "../schemas.js";
|
|
27
|
+
|
|
28
|
+
const NOT_CONFIGURED = { error: "Agent sessions not configured" };
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* The answer for what stops a person from working in a chat's workspace,
|
|
32
|
+
* or undefined for any other error. Shared with the preview routes.
|
|
33
|
+
*/
|
|
34
|
+
export function workspaceRefusal(
|
|
35
|
+
reply: FastifyReply,
|
|
36
|
+
error: unknown,
|
|
37
|
+
): FastifyReply | undefined {
|
|
38
|
+
if (
|
|
39
|
+
error instanceof AgentSessionNotFoundError ||
|
|
40
|
+
error instanceof SessionTerminalNotFoundError
|
|
41
|
+
)
|
|
42
|
+
return reply.status(404).send({ error: error.message });
|
|
43
|
+
if (error instanceof SessionTerminalSecretsError)
|
|
44
|
+
return reply.status(403).send({ error: error.message });
|
|
45
|
+
if (error instanceof SessionWorkspaceUnavailableError)
|
|
46
|
+
return reply.status(409).send({ error: error.message, code: error.reason });
|
|
47
|
+
if (error instanceof EnvironmentCapacityError)
|
|
48
|
+
return reply
|
|
49
|
+
.status(409)
|
|
50
|
+
.send({ error: error.message, code: "environment_full" });
|
|
51
|
+
if (
|
|
52
|
+
error instanceof EnvironmentBindingUnavailableError ||
|
|
53
|
+
error instanceof EnvironmentNotFoundError
|
|
54
|
+
)
|
|
55
|
+
return reply
|
|
56
|
+
.status(409)
|
|
57
|
+
.send({ error: error.message, code: "environment_unavailable" });
|
|
58
|
+
return undefined;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/**
|
|
62
|
+
* Terminals in a chat's workspace (ADR 0209): open one (starting the
|
|
63
|
+
* workspace when it was given back), read its output by cursor with a
|
|
64
|
+
* wait, type into it, resize it, close it. Only the person who opened a
|
|
65
|
+
* terminal reaches it.
|
|
66
|
+
*/
|
|
67
|
+
export function registerSessionTerminalRoutes(
|
|
68
|
+
app: FastifyInstance,
|
|
69
|
+
ctx: RouteContext,
|
|
70
|
+
) {
|
|
71
|
+
const typed = app.withTypeProvider<ZodTypeProvider>();
|
|
72
|
+
const conflicts = {
|
|
73
|
+
403: ErrorSchema,
|
|
74
|
+
404: ErrorSchema,
|
|
75
|
+
409: SessionWorkspaceErrorSchema,
|
|
76
|
+
503: ErrorSchema,
|
|
77
|
+
};
|
|
78
|
+
|
|
79
|
+
typed.route({
|
|
80
|
+
method: "POST",
|
|
81
|
+
url: "/projects/:projectId/agent/sessions/:sessionId/terminals",
|
|
82
|
+
schema: {
|
|
83
|
+
params: AgentSessionIdParamsSchema,
|
|
84
|
+
body: OpenSessionTerminalSchema,
|
|
85
|
+
response: { 201: SessionTerminalSchema, ...conflicts },
|
|
86
|
+
},
|
|
87
|
+
handler: async (request, reply) => {
|
|
88
|
+
const terminals = ctx.core?.sessionTerminals;
|
|
89
|
+
if (!terminals) return reply.status(503).send(NOT_CONFIGURED);
|
|
90
|
+
try {
|
|
91
|
+
const terminal = await terminals.open({
|
|
92
|
+
identity: resolveIdentity(request),
|
|
93
|
+
...request.params,
|
|
94
|
+
...request.body,
|
|
95
|
+
});
|
|
96
|
+
return reply.status(201).send(terminal);
|
|
97
|
+
} catch (error) {
|
|
98
|
+
const refused = workspaceRefusal(reply, error);
|
|
99
|
+
if (refused) return refused;
|
|
100
|
+
throw error;
|
|
101
|
+
}
|
|
102
|
+
},
|
|
103
|
+
});
|
|
104
|
+
|
|
105
|
+
typed.route({
|
|
106
|
+
method: "GET",
|
|
107
|
+
url: "/projects/:projectId/agent/sessions/:sessionId/terminals/:terminalId/output",
|
|
108
|
+
schema: {
|
|
109
|
+
params: SessionTerminalParamsSchema,
|
|
110
|
+
querystring: SessionTerminalOutputQuerySchema,
|
|
111
|
+
response: { 200: SessionTerminalOutputSchema, ...conflicts },
|
|
112
|
+
},
|
|
113
|
+
handler: async (request, reply) => {
|
|
114
|
+
const terminals = ctx.core?.sessionTerminals;
|
|
115
|
+
if (!terminals) return reply.status(503).send(NOT_CONFIGURED);
|
|
116
|
+
try {
|
|
117
|
+
return reply.send(
|
|
118
|
+
await terminals.read({
|
|
119
|
+
identity: resolveIdentity(request),
|
|
120
|
+
...request.params,
|
|
121
|
+
...request.query,
|
|
122
|
+
}),
|
|
123
|
+
);
|
|
124
|
+
} catch (error) {
|
|
125
|
+
const refused = workspaceRefusal(reply, error);
|
|
126
|
+
if (refused) return refused;
|
|
127
|
+
throw error;
|
|
128
|
+
}
|
|
129
|
+
},
|
|
130
|
+
});
|
|
131
|
+
|
|
132
|
+
typed.route({
|
|
133
|
+
method: "POST",
|
|
134
|
+
url: "/projects/:projectId/agent/sessions/:sessionId/terminals/:terminalId/input",
|
|
135
|
+
schema: {
|
|
136
|
+
params: SessionTerminalParamsSchema,
|
|
137
|
+
body: SessionTerminalInputSchema,
|
|
138
|
+
response: { 200: OkSchema, 413: ErrorSchema, ...conflicts },
|
|
139
|
+
},
|
|
140
|
+
handler: async (request, reply) => {
|
|
141
|
+
const terminals = ctx.core?.sessionTerminals;
|
|
142
|
+
if (!terminals) return reply.status(503).send(NOT_CONFIGURED);
|
|
143
|
+
try {
|
|
144
|
+
await terminals.write({
|
|
145
|
+
identity: resolveIdentity(request),
|
|
146
|
+
...request.params,
|
|
147
|
+
data: request.body.data,
|
|
148
|
+
});
|
|
149
|
+
return reply.send({ ok: true });
|
|
150
|
+
} catch (error) {
|
|
151
|
+
if (error instanceof RangeError)
|
|
152
|
+
return reply.status(413).send({ error: error.message });
|
|
153
|
+
const refused = workspaceRefusal(reply, error);
|
|
154
|
+
if (refused) return refused;
|
|
155
|
+
throw error;
|
|
156
|
+
}
|
|
157
|
+
},
|
|
158
|
+
});
|
|
159
|
+
|
|
160
|
+
typed.route({
|
|
161
|
+
method: "POST",
|
|
162
|
+
url: "/projects/:projectId/agent/sessions/:sessionId/terminals/:terminalId/resize",
|
|
163
|
+
schema: {
|
|
164
|
+
params: SessionTerminalParamsSchema,
|
|
165
|
+
body: ResizeSessionTerminalSchema,
|
|
166
|
+
response: { 200: OkSchema, ...conflicts },
|
|
167
|
+
},
|
|
168
|
+
handler: async (request, reply) => {
|
|
169
|
+
const terminals = ctx.core?.sessionTerminals;
|
|
170
|
+
if (!terminals) return reply.status(503).send(NOT_CONFIGURED);
|
|
171
|
+
try {
|
|
172
|
+
await terminals.resize({
|
|
173
|
+
identity: resolveIdentity(request),
|
|
174
|
+
...request.params,
|
|
175
|
+
...request.body,
|
|
176
|
+
});
|
|
177
|
+
return reply.send({ ok: true });
|
|
178
|
+
} catch (error) {
|
|
179
|
+
const refused = workspaceRefusal(reply, error);
|
|
180
|
+
if (refused) return refused;
|
|
181
|
+
throw error;
|
|
182
|
+
}
|
|
183
|
+
},
|
|
184
|
+
});
|
|
185
|
+
|
|
186
|
+
typed.route({
|
|
187
|
+
method: "DELETE",
|
|
188
|
+
url: "/projects/:projectId/agent/sessions/:sessionId/terminals/:terminalId",
|
|
189
|
+
schema: {
|
|
190
|
+
params: SessionTerminalParamsSchema,
|
|
191
|
+
response: { 200: OkSchema, ...conflicts },
|
|
192
|
+
},
|
|
193
|
+
handler: async (request, reply) => {
|
|
194
|
+
const terminals = ctx.core?.sessionTerminals;
|
|
195
|
+
if (!terminals) return reply.status(503).send(NOT_CONFIGURED);
|
|
196
|
+
try {
|
|
197
|
+
await terminals.close({
|
|
198
|
+
identity: resolveIdentity(request),
|
|
199
|
+
...request.params,
|
|
200
|
+
});
|
|
201
|
+
return reply.send({ ok: true });
|
|
202
|
+
} catch (error) {
|
|
203
|
+
const refused = workspaceRefusal(reply, error);
|
|
204
|
+
if (refused) return refused;
|
|
205
|
+
throw error;
|
|
206
|
+
}
|
|
207
|
+
},
|
|
208
|
+
});
|
|
209
|
+
}
|
|
@@ -0,0 +1,241 @@
|
|
|
1
|
+
import {
|
|
2
|
+
AuthenticationRequiredError,
|
|
3
|
+
EnvironmentAccessDeniedError,
|
|
4
|
+
EnvironmentBindingUnavailableError,
|
|
5
|
+
EnvironmentCapacityError,
|
|
6
|
+
EnvironmentIncompatibleError,
|
|
7
|
+
EnvironmentNotFoundError,
|
|
8
|
+
NoCompatibleEnvironmentError,
|
|
9
|
+
ProductionDeploymentNotFoundError,
|
|
10
|
+
ProjectNotFoundError,
|
|
11
|
+
RunEnrollmentConflictError,
|
|
12
|
+
RunInputInvalidError,
|
|
13
|
+
TenantActiveRunLimitError,
|
|
14
|
+
TriggerBindingsInvalidError,
|
|
15
|
+
TriggerKindNotRegisteredError,
|
|
16
|
+
TriggerModeNotAllowedError,
|
|
17
|
+
TriggerPayloadInvalidError,
|
|
18
|
+
} from "@catamorphic/core";
|
|
19
|
+
import type { FastifyInstance } from "fastify";
|
|
20
|
+
import type { ZodTypeProvider } from "fastify-type-provider-zod";
|
|
21
|
+
import { z } from "zod";
|
|
22
|
+
import type { RouteContext } from "../app.js";
|
|
23
|
+
import { resolveIdentity } from "../http-identity.js";
|
|
24
|
+
import {
|
|
25
|
+
AuthenticationRequiredSchema,
|
|
26
|
+
ErrorSchema,
|
|
27
|
+
FireTriggerSchema,
|
|
28
|
+
SyncTriggerTypesResultSchema,
|
|
29
|
+
TriggerBindingInfoSchema,
|
|
30
|
+
TriggerFireResultSchema,
|
|
31
|
+
TriggerKindInfoSchema,
|
|
32
|
+
} from "../schemas.js";
|
|
33
|
+
|
|
34
|
+
const ProjectParamsSchema = z.object({ projectId: z.string() });
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* Resolves each graph trigger binding's display metadata from the host's
|
|
38
|
+
* registered kinds. Serving-layer concern: the parser only knows kind names.
|
|
39
|
+
*/
|
|
40
|
+
export function attachTriggerKindDisplays<
|
|
41
|
+
Graph extends {
|
|
42
|
+
nodes: Array<{
|
|
43
|
+
triggerBindings?: Array<{
|
|
44
|
+
kind: string;
|
|
45
|
+
display?: { label?: string; icon?: string; color?: string };
|
|
46
|
+
}>;
|
|
47
|
+
}>;
|
|
48
|
+
},
|
|
49
|
+
>(core: RouteContext["core"], graph: Graph): Graph {
|
|
50
|
+
if (!core) return graph;
|
|
51
|
+
for (const node of graph.nodes) {
|
|
52
|
+
for (const binding of node.triggerBindings ?? []) {
|
|
53
|
+
const display = core.triggers.kindInfo(binding.kind)?.display;
|
|
54
|
+
if (display) binding.display = display;
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
return graph;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
export function registerTriggerRoutes(app: FastifyInstance, ctx: RouteContext) {
|
|
61
|
+
const typed = app.withTypeProvider<ZodTypeProvider>();
|
|
62
|
+
|
|
63
|
+
typed.route({
|
|
64
|
+
method: "GET",
|
|
65
|
+
url: "/trigger-kinds",
|
|
66
|
+
schema: {
|
|
67
|
+
response: { 200: TriggerKindInfoSchema.array(), 503: ErrorSchema },
|
|
68
|
+
},
|
|
69
|
+
handler: async (_request, reply) => {
|
|
70
|
+
if (!ctx.core)
|
|
71
|
+
return reply.status(503).send({ error: "Service not configured" });
|
|
72
|
+
return reply.send(
|
|
73
|
+
ctx.core.triggers.listKinds() as unknown as z.infer<
|
|
74
|
+
typeof TriggerKindInfoSchema
|
|
75
|
+
>[],
|
|
76
|
+
);
|
|
77
|
+
},
|
|
78
|
+
});
|
|
79
|
+
|
|
80
|
+
typed.route({
|
|
81
|
+
method: "GET",
|
|
82
|
+
url: "/projects/:projectId/triggers",
|
|
83
|
+
schema: {
|
|
84
|
+
params: ProjectParamsSchema,
|
|
85
|
+
querystring: z.object({ kind: z.string().optional() }),
|
|
86
|
+
response: {
|
|
87
|
+
200: TriggerBindingInfoSchema.array(),
|
|
88
|
+
404: ErrorSchema,
|
|
89
|
+
422: ErrorSchema,
|
|
90
|
+
428: AuthenticationRequiredSchema,
|
|
91
|
+
503: ErrorSchema,
|
|
92
|
+
},
|
|
93
|
+
},
|
|
94
|
+
handler: async (request, reply) => {
|
|
95
|
+
if (!ctx.core)
|
|
96
|
+
return reply.status(503).send({ error: "Service not configured" });
|
|
97
|
+
const identity = resolveIdentity(request);
|
|
98
|
+
try {
|
|
99
|
+
const bindings = await ctx.core.triggers.list({
|
|
100
|
+
identity,
|
|
101
|
+
projectId: request.params.projectId,
|
|
102
|
+
kind: request.query.kind,
|
|
103
|
+
});
|
|
104
|
+
return reply.send(
|
|
105
|
+
bindings as unknown as z.infer<typeof TriggerBindingInfoSchema>[],
|
|
106
|
+
);
|
|
107
|
+
} catch (err) {
|
|
108
|
+
return handleTriggerError(err, reply) as never;
|
|
109
|
+
}
|
|
110
|
+
},
|
|
111
|
+
});
|
|
112
|
+
|
|
113
|
+
typed.route({
|
|
114
|
+
method: "POST",
|
|
115
|
+
url: "/projects/:projectId/triggers/:kind/fire",
|
|
116
|
+
schema: {
|
|
117
|
+
params: ProjectParamsSchema.extend({ kind: z.string() }),
|
|
118
|
+
body: FireTriggerSchema,
|
|
119
|
+
response: {
|
|
120
|
+
200: TriggerFireResultSchema,
|
|
121
|
+
404: ErrorSchema,
|
|
122
|
+
409: ErrorSchema,
|
|
123
|
+
422: ErrorSchema,
|
|
124
|
+
429: ErrorSchema,
|
|
125
|
+
503: ErrorSchema,
|
|
126
|
+
},
|
|
127
|
+
},
|
|
128
|
+
handler: async (request, reply) => {
|
|
129
|
+
if (!ctx.core)
|
|
130
|
+
return reply.status(503).send({ error: "Service not configured" });
|
|
131
|
+
const identity = resolveIdentity(request);
|
|
132
|
+
try {
|
|
133
|
+
const result = await ctx.core.triggers.fire({
|
|
134
|
+
identity,
|
|
135
|
+
projectId: request.params.projectId,
|
|
136
|
+
kind: request.params.kind,
|
|
137
|
+
payload: request.body.payload ?? null,
|
|
138
|
+
environment: request.body.environment,
|
|
139
|
+
mode: request.body.mode,
|
|
140
|
+
workflows: request.body.workflows,
|
|
141
|
+
correlationKey: request.body.correlationKey,
|
|
142
|
+
onConflict: request.body.onConflict,
|
|
143
|
+
budgetMs: request.body.budgetMs,
|
|
144
|
+
});
|
|
145
|
+
// The core's readonly Json and zod's JSONType are structurally
|
|
146
|
+
// identical but not mutually assignable.
|
|
147
|
+
return reply.send(
|
|
148
|
+
result as unknown as z.infer<typeof TriggerFireResultSchema>,
|
|
149
|
+
);
|
|
150
|
+
} catch (err) {
|
|
151
|
+
if (err instanceof RunEnrollmentConflictError) {
|
|
152
|
+
return reply.status(409).send({ error: err.message });
|
|
153
|
+
}
|
|
154
|
+
if (err instanceof TenantActiveRunLimitError) {
|
|
155
|
+
return reply.status(429).send({ error: err.message });
|
|
156
|
+
}
|
|
157
|
+
return handleTriggerError(err, reply) as never;
|
|
158
|
+
}
|
|
159
|
+
},
|
|
160
|
+
});
|
|
161
|
+
|
|
162
|
+
typed.route({
|
|
163
|
+
method: "POST",
|
|
164
|
+
url: "/projects/:projectId/triggers/sync-types",
|
|
165
|
+
schema: {
|
|
166
|
+
params: ProjectParamsSchema,
|
|
167
|
+
response: {
|
|
168
|
+
200: SyncTriggerTypesResultSchema,
|
|
169
|
+
404: ErrorSchema,
|
|
170
|
+
503: ErrorSchema,
|
|
171
|
+
},
|
|
172
|
+
},
|
|
173
|
+
handler: async (request, reply) => {
|
|
174
|
+
if (!ctx.core)
|
|
175
|
+
return reply.status(503).send({ error: "Service not configured" });
|
|
176
|
+
const identity = resolveIdentity(request);
|
|
177
|
+
try {
|
|
178
|
+
const result = await ctx.core.triggers.syncTypes({
|
|
179
|
+
identity,
|
|
180
|
+
projectId: request.params.projectId,
|
|
181
|
+
});
|
|
182
|
+
return reply.send(result);
|
|
183
|
+
} catch (err) {
|
|
184
|
+
if (err instanceof ProjectNotFoundError) {
|
|
185
|
+
return reply.status(404).send({ error: "Project not found" });
|
|
186
|
+
}
|
|
187
|
+
throw err;
|
|
188
|
+
}
|
|
189
|
+
},
|
|
190
|
+
});
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
function handleTriggerError(
|
|
194
|
+
err: unknown,
|
|
195
|
+
reply: {
|
|
196
|
+
status(code: number): { send(body: unknown): unknown };
|
|
197
|
+
},
|
|
198
|
+
): unknown {
|
|
199
|
+
if (err instanceof ProjectNotFoundError) {
|
|
200
|
+
return reply.status(404).send({ error: "Project not found" });
|
|
201
|
+
}
|
|
202
|
+
if (err instanceof ProductionDeploymentNotFoundError) {
|
|
203
|
+
return reply.status(404).send({ error: err.message });
|
|
204
|
+
}
|
|
205
|
+
if (err instanceof AuthenticationRequiredError) {
|
|
206
|
+
return reply.status(428).send({
|
|
207
|
+
error: err.message,
|
|
208
|
+
code: "authentication_required",
|
|
209
|
+
environment: err.environment,
|
|
210
|
+
requirements: [...err.requirements],
|
|
211
|
+
});
|
|
212
|
+
}
|
|
213
|
+
if (err instanceof EnvironmentAccessDeniedError) {
|
|
214
|
+
return reply.status(403).send({ error: err.message });
|
|
215
|
+
}
|
|
216
|
+
if (err instanceof EnvironmentNotFoundError) {
|
|
217
|
+
return reply.status(404).send({ error: err.message });
|
|
218
|
+
}
|
|
219
|
+
if (
|
|
220
|
+
err instanceof EnvironmentCapacityError ||
|
|
221
|
+
err instanceof EnvironmentBindingUnavailableError
|
|
222
|
+
) {
|
|
223
|
+
return reply.status(409).send({ error: err.message });
|
|
224
|
+
}
|
|
225
|
+
if (
|
|
226
|
+
err instanceof EnvironmentIncompatibleError ||
|
|
227
|
+
err instanceof NoCompatibleEnvironmentError
|
|
228
|
+
) {
|
|
229
|
+
return reply.status(422).send({ error: err.message });
|
|
230
|
+
}
|
|
231
|
+
if (
|
|
232
|
+
err instanceof RunInputInvalidError ||
|
|
233
|
+
err instanceof TriggerKindNotRegisteredError ||
|
|
234
|
+
err instanceof TriggerModeNotAllowedError ||
|
|
235
|
+
err instanceof TriggerPayloadInvalidError ||
|
|
236
|
+
err instanceof TriggerBindingsInvalidError
|
|
237
|
+
) {
|
|
238
|
+
return reply.status(422).send({ error: err.message });
|
|
239
|
+
}
|
|
240
|
+
throw err;
|
|
241
|
+
}
|
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
import {
|
|
2
|
+
AgentSessionNotFoundError,
|
|
3
|
+
ProjectNotFoundError,
|
|
4
|
+
} from "@catamorphic/core";
|
|
5
|
+
import type { FastifyInstance } from "fastify";
|
|
6
|
+
import type { ZodTypeProvider } from "fastify-type-provider-zod";
|
|
7
|
+
import type { RouteContext } from "../app.js";
|
|
8
|
+
import { resolveIdentity } from "../http-identity.js";
|
|
9
|
+
import {
|
|
10
|
+
AgentSessionIdParamsSchema,
|
|
11
|
+
ErrorSchema,
|
|
12
|
+
ListSchema,
|
|
13
|
+
OkSchema,
|
|
14
|
+
WatcherIdParamsSchema,
|
|
15
|
+
WatcherSchema,
|
|
16
|
+
} from "../schemas.js";
|
|
17
|
+
|
|
18
|
+
export function registerWatcherRoutes(app: FastifyInstance, ctx: RouteContext) {
|
|
19
|
+
const typed = app.withTypeProvider<ZodTypeProvider>();
|
|
20
|
+
|
|
21
|
+
typed.route({
|
|
22
|
+
method: "GET",
|
|
23
|
+
url: "/projects/:projectId/agent/sessions/:sessionId/watchers",
|
|
24
|
+
schema: {
|
|
25
|
+
params: AgentSessionIdParamsSchema,
|
|
26
|
+
response: {
|
|
27
|
+
200: ListSchema(WatcherSchema),
|
|
28
|
+
404: ErrorSchema,
|
|
29
|
+
503: ErrorSchema,
|
|
30
|
+
},
|
|
31
|
+
},
|
|
32
|
+
handler: async (request, reply) => {
|
|
33
|
+
const watchers = ctx.core?.watchers;
|
|
34
|
+
if (!watchers) {
|
|
35
|
+
return reply.status(503).send({ error: "Watchers are not configured" });
|
|
36
|
+
}
|
|
37
|
+
try {
|
|
38
|
+
const items = await watchers.list({
|
|
39
|
+
identity: resolveIdentity(request),
|
|
40
|
+
projectId: request.params.projectId,
|
|
41
|
+
sessionId: request.params.sessionId,
|
|
42
|
+
});
|
|
43
|
+
return reply.send({ items, total: items.length });
|
|
44
|
+
} catch (error) {
|
|
45
|
+
if (
|
|
46
|
+
error instanceof ProjectNotFoundError ||
|
|
47
|
+
error instanceof AgentSessionNotFoundError
|
|
48
|
+
) {
|
|
49
|
+
return reply.status(404).send({ error: "Session not found" });
|
|
50
|
+
}
|
|
51
|
+
throw error;
|
|
52
|
+
}
|
|
53
|
+
},
|
|
54
|
+
});
|
|
55
|
+
|
|
56
|
+
typed.route({
|
|
57
|
+
method: "DELETE",
|
|
58
|
+
url: "/projects/:projectId/agent/sessions/:sessionId/watchers/:watcherId",
|
|
59
|
+
schema: {
|
|
60
|
+
params: WatcherIdParamsSchema,
|
|
61
|
+
response: {
|
|
62
|
+
200: OkSchema,
|
|
63
|
+
404: ErrorSchema,
|
|
64
|
+
409: ErrorSchema,
|
|
65
|
+
503: ErrorSchema,
|
|
66
|
+
},
|
|
67
|
+
},
|
|
68
|
+
handler: async (request, reply) => {
|
|
69
|
+
const watchers = ctx.core?.watchers;
|
|
70
|
+
if (!watchers) {
|
|
71
|
+
return reply.status(503).send({ error: "Watchers are not configured" });
|
|
72
|
+
}
|
|
73
|
+
try {
|
|
74
|
+
const stopped = await watchers.stop({
|
|
75
|
+
identity: resolveIdentity(request),
|
|
76
|
+
projectId: request.params.projectId,
|
|
77
|
+
sessionId: request.params.sessionId,
|
|
78
|
+
watcherId: request.params.watcherId,
|
|
79
|
+
});
|
|
80
|
+
return stopped
|
|
81
|
+
? reply.send({ ok: true })
|
|
82
|
+
: reply.status(409).send({ error: "Watcher is already terminal" });
|
|
83
|
+
} catch (error) {
|
|
84
|
+
if (
|
|
85
|
+
error instanceof ProjectNotFoundError ||
|
|
86
|
+
error instanceof AgentSessionNotFoundError
|
|
87
|
+
) {
|
|
88
|
+
return reply.status(404).send({ error: "Session not found" });
|
|
89
|
+
}
|
|
90
|
+
throw error;
|
|
91
|
+
}
|
|
92
|
+
},
|
|
93
|
+
});
|
|
94
|
+
}
|