@catamorphic/fastify-plugin 0.1.0-alpha.37 → 0.1.0-alpha.39

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.
@@ -0,0 +1,287 @@
1
+ import {
2
+ DocumentConflictError,
3
+ DocumentNotFoundError,
4
+ DocumentPathError,
5
+ PrivateStateDeleteSchema,
6
+ PrivateStateReadSchema,
7
+ PrivateStateSchema,
8
+ PrivateStateWriteSchema,
9
+ PublishNotificationSchema,
10
+ } from "@catamorphic/core";
11
+ import type { FastifyInstance } from "fastify";
12
+ import type { ZodTypeProvider } from "fastify-type-provider-zod";
13
+ import { z } from "zod";
14
+ import type { RouteContext } from "../app.js";
15
+ import { resolveIdentity } from "../http-identity.js";
16
+ import {
17
+ AuthenticationRequiredSchema,
18
+ ErrorSchema,
19
+ JsonValueSchema,
20
+ RunSchema,
21
+ WorkflowEnablementPreviewSchema,
22
+ WorkflowEnablementSchema,
23
+ } from "../schemas.js";
24
+ import { replyForTriggerError } from "./run-errors.js";
25
+ import { SessionArtifactSchema } from "./session-artifacts.js";
26
+ import { handleEnablementError } from "./workflow-enablements.js";
27
+
28
+ const State = PrivateStateSchema.extend({ value: JsonValueSchema });
29
+ const StateWrite = PrivateStateWriteSchema.extend({ value: JsonValueSchema });
30
+ const Project = z.object({ projectId: z.string().uuid() });
31
+ const Artifact = Project.extend({ artifactId: z.string().uuid() });
32
+ const Revision = z.object({
33
+ revision: z.number().int().positive(),
34
+ environment: z.string().optional(),
35
+ });
36
+ const errors = {
37
+ 400: ErrorSchema,
38
+ 403: ErrorSchema,
39
+ 404: ErrorSchema,
40
+ 409: ErrorSchema.extend({ currentVersion: z.number().optional() }),
41
+ 422: ErrorSchema,
42
+ 428: AuthenticationRequiredSchema,
43
+ 429: ErrorSchema,
44
+ 503: ErrorSchema,
45
+ };
46
+
47
+ export function registerPrivateCustomizationRoutes(
48
+ app: FastifyInstance,
49
+ ctx: RouteContext,
50
+ ) {
51
+ app.register(async (privateApp) => {
52
+ privateApp.setErrorHandler((error, _request, reply) => {
53
+ if (error instanceof DocumentConflictError)
54
+ return reply
55
+ .status(409)
56
+ .send({ error: error.message, currentVersion: error.currentVersion });
57
+ if (error instanceof DocumentNotFoundError)
58
+ return reply.status(404).send({ error: error.message });
59
+ if (error instanceof DocumentPathError)
60
+ return reply.status(400).send({ error: error.message });
61
+ const triggerError = replyForTriggerError(error, reply);
62
+ if (triggerError) return triggerError;
63
+ return handleEnablementError(error, reply);
64
+ });
65
+ const typed = privateApp.withTypeProvider<ZodTypeProvider>();
66
+ typed.post(
67
+ "/projects/:projectId/private-state/read",
68
+ {
69
+ schema: {
70
+ params: Project,
71
+ body: PrivateStateReadSchema,
72
+ response: { 200: State, ...errors },
73
+ },
74
+ },
75
+ async (request, reply) => {
76
+ if (!ctx.core)
77
+ return reply
78
+ .status(503)
79
+ .send({ error: "Private state is unavailable" });
80
+ return ctx.core.privateState.read({
81
+ ...request.params,
82
+ ...request.body,
83
+ identity: resolveIdentity(request),
84
+ });
85
+ },
86
+ );
87
+ typed.put(
88
+ "/projects/:projectId/private-state",
89
+ {
90
+ schema: {
91
+ params: Project,
92
+ body: StateWrite,
93
+ response: { 200: State, ...errors },
94
+ },
95
+ },
96
+ async (request, reply) => {
97
+ if (!ctx.core)
98
+ return reply
99
+ .status(503)
100
+ .send({ error: "Private state is unavailable" });
101
+ return ctx.core.privateState.write({
102
+ ...request.params,
103
+ ...request.body,
104
+ identity: resolveIdentity(request),
105
+ });
106
+ },
107
+ );
108
+ typed.delete(
109
+ "/projects/:projectId/private-state",
110
+ {
111
+ schema: {
112
+ params: Project,
113
+ body: PrivateStateDeleteSchema,
114
+ response: { 200: z.object({ version: z.number().int() }), ...errors },
115
+ },
116
+ },
117
+ async (request, reply) => {
118
+ if (!ctx.core)
119
+ return reply
120
+ .status(503)
121
+ .send({ error: "Private state is unavailable" });
122
+ return ctx.core.privateState.delete({
123
+ ...request.params,
124
+ ...request.body,
125
+ identity: resolveIdentity(request),
126
+ });
127
+ },
128
+ );
129
+ typed.post(
130
+ "/projects/:projectId/notifications",
131
+ {
132
+ schema: {
133
+ params: Project,
134
+ body: PublishNotificationSchema,
135
+ response: {
136
+ 200: z.object({ published: z.literal(true) }),
137
+ ...errors,
138
+ },
139
+ },
140
+ },
141
+ async (request, reply) => {
142
+ if (!ctx.core)
143
+ return reply
144
+ .status(503)
145
+ .send({ error: "Notifications are unavailable" });
146
+ return ctx.core.notifications.notify({
147
+ ...request.params,
148
+ ...request.body,
149
+ identity: resolveIdentity(request),
150
+ });
151
+ },
152
+ );
153
+ typed.get(
154
+ "/projects/:projectId/private-workflows",
155
+ {
156
+ schema: {
157
+ params: Project,
158
+ response: { 200: z.array(SessionArtifactSchema), ...errors },
159
+ },
160
+ },
161
+ async (request, reply) => {
162
+ if (!ctx.core)
163
+ return reply
164
+ .status(503)
165
+ .send({ error: "Private workflows are unavailable" });
166
+ return ctx.core.sessionArtifacts.list({
167
+ ...request.params,
168
+ identity: resolveIdentity(request),
169
+ lifetime: "member",
170
+ });
171
+ },
172
+ );
173
+ typed.post(
174
+ "/projects/:projectId/private-workflows",
175
+ {
176
+ bodyLimit: 6 * 1024 * 1024,
177
+ schema: {
178
+ params: Project,
179
+ body: z.object({
180
+ name: z.string().min(1).max(100),
181
+ title: z.string().max(200).optional(),
182
+ source: z.string().max(5 * 1024 * 1024),
183
+ files: z.record(z.string(), z.string()).optional(),
184
+ }),
185
+ response: { 201: SessionArtifactSchema, ...errors },
186
+ },
187
+ },
188
+ async (request, reply) => {
189
+ if (!ctx.core)
190
+ return reply
191
+ .status(503)
192
+ .send({ error: "Private workflows are unavailable" });
193
+ return reply.status(201).send(
194
+ await ctx.core.sessionArtifacts.create({
195
+ ...request.params,
196
+ ...request.body,
197
+ identity: resolveIdentity(request),
198
+ lifetime: "member",
199
+ kind: "workflow",
200
+ }),
201
+ );
202
+ },
203
+ );
204
+ typed.post(
205
+ "/projects/:projectId/private-workflows/:artifactId/preview",
206
+ {
207
+ schema: {
208
+ params: Artifact,
209
+ body: Revision,
210
+ response: { 200: WorkflowEnablementPreviewSchema, ...errors },
211
+ },
212
+ },
213
+ async (request, reply) => {
214
+ if (!ctx.core)
215
+ return reply
216
+ .status(503)
217
+ .send({ error: "Private workflows are unavailable" });
218
+ return ctx.core.privateWorkflows.preview({
219
+ ...request.params,
220
+ ...request.body,
221
+ identity: resolveIdentity(request),
222
+ });
223
+ },
224
+ );
225
+ typed.post(
226
+ "/projects/:projectId/private-workflows/:artifactId/activate",
227
+ {
228
+ schema: {
229
+ params: Artifact,
230
+ body: Revision.extend({ consentDigest: z.string() }),
231
+ response: { 200: WorkflowEnablementSchema, ...errors },
232
+ },
233
+ },
234
+ async (request, reply) => {
235
+ if (!ctx.core)
236
+ return reply
237
+ .status(503)
238
+ .send({ error: "Private workflows are unavailable" });
239
+ return ctx.core.privateWorkflows.activate({
240
+ ...request.params,
241
+ ...request.body,
242
+ identity: resolveIdentity(request),
243
+ });
244
+ },
245
+ );
246
+ typed.post(
247
+ "/projects/:projectId/private-workflows/:artifactId/stop",
248
+ {
249
+ schema: {
250
+ params: Artifact,
251
+ response: { 200: z.object({ stopped: z.boolean() }), ...errors },
252
+ },
253
+ },
254
+ async (request, reply) => {
255
+ if (!ctx.core)
256
+ return reply
257
+ .status(503)
258
+ .send({ error: "Private workflows are unavailable" });
259
+ return ctx.core.privateWorkflows.stop({
260
+ ...request.params,
261
+ identity: resolveIdentity(request),
262
+ });
263
+ },
264
+ );
265
+ typed.post(
266
+ "/projects/:projectId/private-workflows/:artifactId/run",
267
+ {
268
+ schema: {
269
+ params: Artifact,
270
+ body: Revision.extend({ input: JsonValueSchema.optional() }),
271
+ response: { 200: RunSchema, ...errors },
272
+ },
273
+ },
274
+ async (request, reply) => {
275
+ if (!ctx.core)
276
+ return reply
277
+ .status(503)
278
+ .send({ error: "Private workflows are unavailable" });
279
+ return ctx.core.privateWorkflows.run({
280
+ ...request.params,
281
+ ...request.body,
282
+ identity: resolveIdentity(request),
283
+ });
284
+ },
285
+ );
286
+ });
287
+ }
@@ -16,6 +16,8 @@ export const SessionArtifactSchema = z.object({
16
16
  id: z.string().uuid(),
17
17
  projectId: z.string().uuid(),
18
18
  sessionId: z.string().uuid().nullable(),
19
+ lifetime: z.enum(["session", "member"]),
20
+ workflowEnablementId: z.string().uuid().nullable(),
19
21
  kind: z.enum(["app", "workflow"]),
20
22
  name: z.string(),
21
23
  title: z.string(),
@@ -286,7 +286,7 @@ export function registerWorkflowEnablementRoutes(
286
286
  });
287
287
  }
288
288
 
289
- function handleEnablementError(error: unknown, reply: FastifyReply) {
289
+ export function handleEnablementError(error: unknown, reply: FastifyReply) {
290
290
  if (error instanceof AuthenticationRequiredError) {
291
291
  return reply.status(428).send({
292
292
  error: error.message,
package/src/schemas.ts CHANGED
@@ -1069,7 +1069,7 @@ export const SandboxSchema = z.object({
1069
1069
  provider: z.string(),
1070
1070
  providerId: z.string(),
1071
1071
  sandboxType: z.enum(["execution", "dev"]),
1072
- commitSha: z.string().length(40).nullable(),
1072
+ commitSha: z.string().nullable(),
1073
1073
  userId: z.string().uuid().nullable(),
1074
1074
  status: z.string(),
1075
1075
  snapshotName: z.string().nullable(),
@@ -1117,6 +1117,8 @@ export const AgentSessionsQuerySchema = PaginationQuerySchema.extend({
1117
1117
  .enum(["true", "false"])
1118
1118
  .transform((value) => value === "true")
1119
1119
  .optional(),
1120
+ /** Words each found in the chat's title or one of its messages. */
1121
+ query: z.string().max(500).optional(),
1120
1122
  }).refine((value) => !(value.rootsOnly && value.parentSessionId), {
1121
1123
  message: "Choose rootsOnly or parentSessionId, not both.",
1122
1124
  });
@@ -1226,6 +1228,8 @@ export const AgentSessionSchema = z.object({
1226
1228
  attentionMessage: z
1227
1229
  .object({ id: z.string().uuid(), content: z.string() })
1228
1230
  .optional(),
1231
+ /** A searched list's excerpt of the message the words were found in. */
1232
+ match: z.string().optional(),
1229
1233
  /** The project's key for this chat (ADR 0173); closing frees it. */
1230
1234
  key: z.string().nullable(),
1231
1235
  /** Workflows that delivered to the chat by its key. */
@@ -1435,7 +1439,13 @@ const TurnErrorSchema = describes<TurnError>()(
1435
1439
  z.object({
1436
1440
  message: z.string(),
1437
1441
  kind: z
1438
- .enum(["auth", "rate_limit", "unavailable", "model_incompat"])
1442
+ .enum([
1443
+ "auth",
1444
+ "rate_limit",
1445
+ "unavailable",
1446
+ "model_incompat",
1447
+ "agent_missing",
1448
+ ])
1439
1449
  .optional(),
1440
1450
  retrySafe: z.boolean().optional(),
1441
1451
  }),