@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.
- 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,1566 @@
|
|
|
1
|
+
import {
|
|
2
|
+
type JsonObject,
|
|
3
|
+
type JsonValue,
|
|
4
|
+
SessionMetadataError,
|
|
5
|
+
} from "@catamorphic/agent-protocol";
|
|
6
|
+
import {
|
|
7
|
+
AccessDeniedError,
|
|
8
|
+
AgentDelegationDeniedError,
|
|
9
|
+
AgentDelegationSettingsError,
|
|
10
|
+
AgentNotConfiguredError,
|
|
11
|
+
AgentSessionArchiveConfirmationRequiredError,
|
|
12
|
+
AgentSessionAuthorityRequiredError,
|
|
13
|
+
AgentSessionClosedError,
|
|
14
|
+
AgentSessionHandoffPendingError,
|
|
15
|
+
AgentSessionNotFoundError,
|
|
16
|
+
AgentSessionRewindingError,
|
|
17
|
+
AgentTurnInProgressError,
|
|
18
|
+
AuthenticationRequiredError,
|
|
19
|
+
EnvironmentAccessDeniedError,
|
|
20
|
+
EnvironmentBindingUnavailableError,
|
|
21
|
+
EnvironmentCapacityError,
|
|
22
|
+
EnvironmentIncompatibleError,
|
|
23
|
+
EnvironmentNotFoundError,
|
|
24
|
+
ForeignSessionEventError,
|
|
25
|
+
keyedChatOwnerId,
|
|
26
|
+
NoCompatibleEnvironmentError,
|
|
27
|
+
ProjectNotFoundError,
|
|
28
|
+
parseChatKey,
|
|
29
|
+
SessionMirrorBehindError,
|
|
30
|
+
SessionMirrorDivergedError,
|
|
31
|
+
UnsupportedAgentTopologyError,
|
|
32
|
+
} from "@catamorphic/core";
|
|
33
|
+
import type { FastifyInstance } from "fastify";
|
|
34
|
+
import type { ZodTypeProvider } from "fastify-type-provider-zod";
|
|
35
|
+
import { z } from "zod";
|
|
36
|
+
import type { RouteContext } from "../app.js";
|
|
37
|
+
import { resolveIdentity } from "../http-identity.js";
|
|
38
|
+
import { hungUp } from "../hung-up.js";
|
|
39
|
+
import {
|
|
40
|
+
AgentCatalogSchema,
|
|
41
|
+
AgentSessionArchiveConfirmationSchema,
|
|
42
|
+
AgentSessionArchiveImpactSchema,
|
|
43
|
+
AgentSessionArchiveResultSchema,
|
|
44
|
+
AgentSessionDetailSchema,
|
|
45
|
+
AgentSessionIdParamsSchema,
|
|
46
|
+
AgentSessionMetadataSchema,
|
|
47
|
+
AgentSessionPeerSchema,
|
|
48
|
+
AgentSessionSchema,
|
|
49
|
+
AgentSessionsQuerySchema,
|
|
50
|
+
AgentSubsessionIdParamsSchema,
|
|
51
|
+
AgentSubsessionSchema,
|
|
52
|
+
ArchiveAgentSessionSchema,
|
|
53
|
+
AuthenticationRequiredSchema,
|
|
54
|
+
ClosedKeyedChatSchema,
|
|
55
|
+
CommandReceiptSchema,
|
|
56
|
+
CreateAgentSessionSchema,
|
|
57
|
+
CreateAgentSubsessionSchema,
|
|
58
|
+
EnvironmentAccessErrorSchema,
|
|
59
|
+
EnvironmentErrorSchema,
|
|
60
|
+
ErrorSchema,
|
|
61
|
+
ForkAgentSessionSchema,
|
|
62
|
+
KeyedChatParamsSchema,
|
|
63
|
+
KeyedChatQuerySchema,
|
|
64
|
+
ListSchema,
|
|
65
|
+
MirrorAgentSessionResultSchema,
|
|
66
|
+
MirrorAgentSessionSchema,
|
|
67
|
+
MirrorConflictSchema,
|
|
68
|
+
MirrorExportSchema,
|
|
69
|
+
OkSchema,
|
|
70
|
+
ProjectAgentEntrySchema,
|
|
71
|
+
ProjectIdParamsSchema,
|
|
72
|
+
RenameAgentSessionSchema,
|
|
73
|
+
ResumeAgentSessionSchema,
|
|
74
|
+
SessionCommandSchema,
|
|
75
|
+
SessionConflictSchema,
|
|
76
|
+
SessionEventsQuerySchema,
|
|
77
|
+
SessionItemsPageSchema,
|
|
78
|
+
SessionItemsQuerySchema,
|
|
79
|
+
SessionMirrorQuerySchema,
|
|
80
|
+
SessionStreamMessageSchema,
|
|
81
|
+
SkillSchema,
|
|
82
|
+
UpdateAgentSessionActivitySchema,
|
|
83
|
+
UpdateAgentSessionMetadataSchema,
|
|
84
|
+
UpdateAgentSessionSchema,
|
|
85
|
+
WaitForAgentSubsessionsSchema,
|
|
86
|
+
} from "../schemas.js";
|
|
87
|
+
import { SessionEventStream } from "../session-event-stream.js";
|
|
88
|
+
|
|
89
|
+
export function registerAgentRoutes(app: FastifyInstance, ctx: RouteContext) {
|
|
90
|
+
const typed = app.withTypeProvider<ZodTypeProvider>();
|
|
91
|
+
|
|
92
|
+
typed.route({
|
|
93
|
+
method: "POST",
|
|
94
|
+
url: "/projects/:projectId/agent/sessions",
|
|
95
|
+
schema: {
|
|
96
|
+
params: ProjectIdParamsSchema,
|
|
97
|
+
body: CreateAgentSessionSchema,
|
|
98
|
+
response: {
|
|
99
|
+
201: AgentSessionSchema,
|
|
100
|
+
400: ErrorSchema,
|
|
101
|
+
404: ErrorSchema,
|
|
102
|
+
403: EnvironmentAccessErrorSchema,
|
|
103
|
+
409: EnvironmentErrorSchema,
|
|
104
|
+
422: EnvironmentErrorSchema,
|
|
105
|
+
428: AuthenticationRequiredSchema,
|
|
106
|
+
503: ErrorSchema,
|
|
107
|
+
},
|
|
108
|
+
},
|
|
109
|
+
handler: async (request, reply) => {
|
|
110
|
+
const agentSessions = ctx.core?.agentSessions;
|
|
111
|
+
if (!agentSessions)
|
|
112
|
+
return reply.status(503).send({ error: "Coding agent not configured" });
|
|
113
|
+
const identity = resolveIdentity(request);
|
|
114
|
+
try {
|
|
115
|
+
const session = await agentSessions.create(
|
|
116
|
+
identity,
|
|
117
|
+
request.params.projectId,
|
|
118
|
+
{
|
|
119
|
+
systemPrompt: request.body.systemPrompt,
|
|
120
|
+
agentId: request.body.agentId,
|
|
121
|
+
model: request.body.model,
|
|
122
|
+
effort: request.body.effort,
|
|
123
|
+
environment: request.body.environment,
|
|
124
|
+
source: request.body.source,
|
|
125
|
+
parentSessionId: request.body.parentSessionId,
|
|
126
|
+
title: request.body.title,
|
|
127
|
+
...(request.body.workspace
|
|
128
|
+
? { workspace: request.body.workspace }
|
|
129
|
+
: {}),
|
|
130
|
+
},
|
|
131
|
+
);
|
|
132
|
+
return reply.status(201).send(session);
|
|
133
|
+
} catch (err) {
|
|
134
|
+
if (err instanceof ProjectNotFoundError) {
|
|
135
|
+
return reply.status(404).send({ error: "Project not found" });
|
|
136
|
+
}
|
|
137
|
+
if (err instanceof AgentNotConfiguredError) {
|
|
138
|
+
return reply.status(400).send({ error: err.message });
|
|
139
|
+
}
|
|
140
|
+
if (err instanceof AgentSessionClosedError) {
|
|
141
|
+
return reply
|
|
142
|
+
.status(409)
|
|
143
|
+
.send({ error: err.message, code: "parent_session_closed" });
|
|
144
|
+
}
|
|
145
|
+
if (err instanceof AuthenticationRequiredError) {
|
|
146
|
+
return reply.status(428).send({
|
|
147
|
+
error: err.message,
|
|
148
|
+
code: "authentication_required",
|
|
149
|
+
environment: err.environment,
|
|
150
|
+
requirements: [...err.requirements],
|
|
151
|
+
});
|
|
152
|
+
}
|
|
153
|
+
if (err instanceof EnvironmentAccessDeniedError) {
|
|
154
|
+
return reply.status(403).send({
|
|
155
|
+
error: err.message,
|
|
156
|
+
code: "environment_access_denied",
|
|
157
|
+
});
|
|
158
|
+
}
|
|
159
|
+
if (
|
|
160
|
+
err instanceof EnvironmentCapacityError ||
|
|
161
|
+
err instanceof EnvironmentBindingUnavailableError ||
|
|
162
|
+
err instanceof EnvironmentNotFoundError
|
|
163
|
+
) {
|
|
164
|
+
return reply.status(409).send({
|
|
165
|
+
error: err.message,
|
|
166
|
+
code:
|
|
167
|
+
err instanceof EnvironmentCapacityError
|
|
168
|
+
? "environment_full"
|
|
169
|
+
: "environment_unavailable",
|
|
170
|
+
});
|
|
171
|
+
}
|
|
172
|
+
if (err instanceof NoCompatibleEnvironmentError)
|
|
173
|
+
return reply.status(422).send({
|
|
174
|
+
error: err.message,
|
|
175
|
+
code: "environment_unavailable",
|
|
176
|
+
reasons: Object.values(err.reasons).flatMap((items) => [...items]),
|
|
177
|
+
});
|
|
178
|
+
if (err instanceof EnvironmentIncompatibleError) {
|
|
179
|
+
return reply.status(422).send({
|
|
180
|
+
error: err.message,
|
|
181
|
+
code: "environment_incompatible",
|
|
182
|
+
reasons: [...err.reasons],
|
|
183
|
+
});
|
|
184
|
+
}
|
|
185
|
+
if (err instanceof UnsupportedAgentTopologyError) {
|
|
186
|
+
return reply.status(422).send({
|
|
187
|
+
error: err.message,
|
|
188
|
+
code: "agent_topology_unsupported",
|
|
189
|
+
});
|
|
190
|
+
}
|
|
191
|
+
throw err;
|
|
192
|
+
}
|
|
193
|
+
},
|
|
194
|
+
});
|
|
195
|
+
|
|
196
|
+
typed.route({
|
|
197
|
+
method: "PATCH",
|
|
198
|
+
url: "/projects/:projectId/agent/sessions/:sessionId",
|
|
199
|
+
schema: {
|
|
200
|
+
params: AgentSessionIdParamsSchema,
|
|
201
|
+
body: UpdateAgentSessionSchema,
|
|
202
|
+
response: {
|
|
203
|
+
200: AgentSessionSchema,
|
|
204
|
+
400: ErrorSchema,
|
|
205
|
+
403: EnvironmentAccessErrorSchema,
|
|
206
|
+
404: ErrorSchema,
|
|
207
|
+
409: z.union([SessionConflictSchema, ErrorSchema]),
|
|
208
|
+
422: EnvironmentErrorSchema,
|
|
209
|
+
428: AuthenticationRequiredSchema,
|
|
210
|
+
503: ErrorSchema,
|
|
211
|
+
},
|
|
212
|
+
},
|
|
213
|
+
handler: async (request, reply) => {
|
|
214
|
+
const agentSessions = ctx.core?.agentSessions;
|
|
215
|
+
if (!agentSessions)
|
|
216
|
+
return reply.status(503).send({ error: "Coding agent not configured" });
|
|
217
|
+
const identity = resolveIdentity(request);
|
|
218
|
+
try {
|
|
219
|
+
const session = await agentSessions.update(
|
|
220
|
+
identity,
|
|
221
|
+
request.params.projectId,
|
|
222
|
+
request.params.sessionId,
|
|
223
|
+
request.body,
|
|
224
|
+
);
|
|
225
|
+
return reply.send(session);
|
|
226
|
+
} catch (err) {
|
|
227
|
+
if (
|
|
228
|
+
err instanceof ProjectNotFoundError ||
|
|
229
|
+
err instanceof AgentSessionNotFoundError
|
|
230
|
+
) {
|
|
231
|
+
return reply.status(404).send({ error: "Session not found" });
|
|
232
|
+
}
|
|
233
|
+
if (err instanceof AgentNotConfiguredError) {
|
|
234
|
+
return reply.status(400).send({ error: err.message });
|
|
235
|
+
}
|
|
236
|
+
if (err instanceof AuthenticationRequiredError) {
|
|
237
|
+
return reply.status(428).send({
|
|
238
|
+
error: err.message,
|
|
239
|
+
code: "authentication_required",
|
|
240
|
+
environment: err.environment,
|
|
241
|
+
requirements: [...err.requirements],
|
|
242
|
+
});
|
|
243
|
+
}
|
|
244
|
+
const conflict = sessionConflict(err);
|
|
245
|
+
if (conflict) return reply.status(409).send(conflict);
|
|
246
|
+
if (err instanceof AgentTurnInProgressError) {
|
|
247
|
+
return reply.status(409).send({
|
|
248
|
+
error: "A turn is in progress; try again when it settles",
|
|
249
|
+
});
|
|
250
|
+
}
|
|
251
|
+
if (err instanceof EnvironmentAccessDeniedError) {
|
|
252
|
+
return reply.status(403).send({
|
|
253
|
+
error: err.message,
|
|
254
|
+
code: "environment_access_denied",
|
|
255
|
+
});
|
|
256
|
+
}
|
|
257
|
+
if (
|
|
258
|
+
err instanceof EnvironmentCapacityError ||
|
|
259
|
+
err instanceof EnvironmentBindingUnavailableError ||
|
|
260
|
+
err instanceof EnvironmentNotFoundError
|
|
261
|
+
) {
|
|
262
|
+
return reply.status(409).send({
|
|
263
|
+
error: err.message,
|
|
264
|
+
});
|
|
265
|
+
}
|
|
266
|
+
if (err instanceof NoCompatibleEnvironmentError)
|
|
267
|
+
return reply.status(422).send({
|
|
268
|
+
error: err.message,
|
|
269
|
+
code: "environment_unavailable",
|
|
270
|
+
reasons: Object.values(err.reasons).flatMap((items) => [...items]),
|
|
271
|
+
});
|
|
272
|
+
if (err instanceof EnvironmentIncompatibleError) {
|
|
273
|
+
return reply.status(422).send({
|
|
274
|
+
error: err.message,
|
|
275
|
+
code: "environment_incompatible",
|
|
276
|
+
reasons: [...err.reasons],
|
|
277
|
+
});
|
|
278
|
+
}
|
|
279
|
+
throw err;
|
|
280
|
+
}
|
|
281
|
+
},
|
|
282
|
+
});
|
|
283
|
+
|
|
284
|
+
typed.route({
|
|
285
|
+
method: "GET",
|
|
286
|
+
url: "/projects/:projectId/agent/sessions",
|
|
287
|
+
schema: {
|
|
288
|
+
params: ProjectIdParamsSchema,
|
|
289
|
+
querystring: AgentSessionsQuerySchema,
|
|
290
|
+
response: { 200: ListSchema(AgentSessionSchema), 404: ErrorSchema },
|
|
291
|
+
},
|
|
292
|
+
handler: async (request, reply) => {
|
|
293
|
+
const agentSessions = ctx.core?.agentSessions;
|
|
294
|
+
if (!agentSessions) return reply.send({ items: [], total: 0 });
|
|
295
|
+
const identity = resolveIdentity(request);
|
|
296
|
+
try {
|
|
297
|
+
const result = await agentSessions.list(
|
|
298
|
+
identity,
|
|
299
|
+
request.params.projectId,
|
|
300
|
+
request.query,
|
|
301
|
+
);
|
|
302
|
+
return reply.send(result);
|
|
303
|
+
} catch (err) {
|
|
304
|
+
if (err instanceof ProjectNotFoundError) {
|
|
305
|
+
return reply.status(404).send({ error: "Project not found" });
|
|
306
|
+
}
|
|
307
|
+
throw err;
|
|
308
|
+
}
|
|
309
|
+
},
|
|
310
|
+
});
|
|
311
|
+
|
|
312
|
+
typed.route({
|
|
313
|
+
method: "GET",
|
|
314
|
+
url: "/projects/:projectId/agent/sessions/:sessionId",
|
|
315
|
+
schema: {
|
|
316
|
+
params: AgentSessionIdParamsSchema,
|
|
317
|
+
response: {
|
|
318
|
+
200: AgentSessionDetailSchema,
|
|
319
|
+
404: ErrorSchema,
|
|
320
|
+
503: ErrorSchema,
|
|
321
|
+
},
|
|
322
|
+
},
|
|
323
|
+
handler: async (request, reply) => {
|
|
324
|
+
const agentSessions = ctx.core?.agentSessions;
|
|
325
|
+
if (!agentSessions)
|
|
326
|
+
return reply.status(503).send({ error: "Coding agent not configured" });
|
|
327
|
+
const identity = resolveIdentity(request);
|
|
328
|
+
try {
|
|
329
|
+
const detail = await agentSessions.get(
|
|
330
|
+
identity,
|
|
331
|
+
request.params.projectId,
|
|
332
|
+
request.params.sessionId,
|
|
333
|
+
);
|
|
334
|
+
return reply.send(detail);
|
|
335
|
+
} catch (err) {
|
|
336
|
+
if (
|
|
337
|
+
err instanceof ProjectNotFoundError ||
|
|
338
|
+
err instanceof AgentSessionNotFoundError
|
|
339
|
+
) {
|
|
340
|
+
return reply.status(404).send({ error: "Session not found" });
|
|
341
|
+
}
|
|
342
|
+
throw err;
|
|
343
|
+
}
|
|
344
|
+
},
|
|
345
|
+
});
|
|
346
|
+
|
|
347
|
+
// Older items of a session, paging back from a snapshot's `olderBefore`.
|
|
348
|
+
typed.route({
|
|
349
|
+
method: "GET",
|
|
350
|
+
url: "/projects/:projectId/agent/sessions/:sessionId/items",
|
|
351
|
+
schema: {
|
|
352
|
+
params: AgentSessionIdParamsSchema,
|
|
353
|
+
querystring: SessionItemsQuerySchema,
|
|
354
|
+
response: {
|
|
355
|
+
200: SessionItemsPageSchema,
|
|
356
|
+
404: ErrorSchema,
|
|
357
|
+
503: ErrorSchema,
|
|
358
|
+
},
|
|
359
|
+
},
|
|
360
|
+
handler: async (request, reply) => {
|
|
361
|
+
const agentSessions = ctx.core?.agentSessions;
|
|
362
|
+
if (!agentSessions)
|
|
363
|
+
return reply.status(503).send({ error: "Coding agent not configured" });
|
|
364
|
+
try {
|
|
365
|
+
return reply.send(
|
|
366
|
+
await agentSessions.items(
|
|
367
|
+
resolveIdentity(request),
|
|
368
|
+
request.params.projectId,
|
|
369
|
+
request.params.sessionId,
|
|
370
|
+
{
|
|
371
|
+
before: request.query.before,
|
|
372
|
+
...(request.query.limit ? { limit: request.query.limit } : {}),
|
|
373
|
+
},
|
|
374
|
+
),
|
|
375
|
+
);
|
|
376
|
+
} catch (error) {
|
|
377
|
+
if (
|
|
378
|
+
error instanceof ProjectNotFoundError ||
|
|
379
|
+
error instanceof AgentSessionNotFoundError
|
|
380
|
+
)
|
|
381
|
+
return reply.status(404).send({ error: "Session not found" });
|
|
382
|
+
throw error;
|
|
383
|
+
}
|
|
384
|
+
},
|
|
385
|
+
});
|
|
386
|
+
|
|
387
|
+
// The session's events after a cursor, as server-sent events (ADR 0197):
|
|
388
|
+
// the gap (or `reset` with a fresh snapshot when it is too large), then
|
|
389
|
+
// live events, with a heartbeat every 15 seconds. A reader that falls too
|
|
390
|
+
// far behind is closed and resumes from its cursor, which each event's
|
|
391
|
+
// `id:` carries, so a browser EventSource resumes by itself.
|
|
392
|
+
typed.route({
|
|
393
|
+
method: "GET",
|
|
394
|
+
url: "/projects/:projectId/agent/sessions/:sessionId/events",
|
|
395
|
+
schema: {
|
|
396
|
+
params: AgentSessionIdParamsSchema,
|
|
397
|
+
querystring: SessionEventsQuerySchema,
|
|
398
|
+
headers: z.looseObject({
|
|
399
|
+
"last-event-id": z
|
|
400
|
+
.string()
|
|
401
|
+
.regex(/^\d+$/)
|
|
402
|
+
.optional()
|
|
403
|
+
.describe(
|
|
404
|
+
"The last sequence the client applied; a reconnecting EventSource sends it, and it wins over `after`",
|
|
405
|
+
),
|
|
406
|
+
}),
|
|
407
|
+
response: {
|
|
408
|
+
200: {
|
|
409
|
+
description:
|
|
410
|
+
"A stream of `data: <SessionStreamMessage JSON>` events; each `id:` is the stream's sequence",
|
|
411
|
+
content: {
|
|
412
|
+
"text/event-stream": { schema: SessionStreamMessageSchema },
|
|
413
|
+
},
|
|
414
|
+
},
|
|
415
|
+
404: ErrorSchema,
|
|
416
|
+
503: ErrorSchema,
|
|
417
|
+
},
|
|
418
|
+
},
|
|
419
|
+
handler: async (request, reply) => {
|
|
420
|
+
const agentSessions = ctx.core?.agentSessions;
|
|
421
|
+
if (!agentSessions)
|
|
422
|
+
return reply.status(503).send({ error: "Coding agent not configured" });
|
|
423
|
+
const lastEventId = request.headers["last-event-id"];
|
|
424
|
+
const after =
|
|
425
|
+
lastEventId !== undefined
|
|
426
|
+
? Number(lastEventId)
|
|
427
|
+
: (request.query.after ?? 0);
|
|
428
|
+
const stream = new SessionEventStream({
|
|
429
|
+
raw: reply.raw,
|
|
430
|
+
sequence: after,
|
|
431
|
+
});
|
|
432
|
+
try {
|
|
433
|
+
stream.attach(
|
|
434
|
+
await agentSessions.subscribe(
|
|
435
|
+
resolveIdentity(request),
|
|
436
|
+
request.params.projectId,
|
|
437
|
+
request.params.sessionId,
|
|
438
|
+
{
|
|
439
|
+
after,
|
|
440
|
+
send: (message) => stream.send(message),
|
|
441
|
+
onClose: () => stream.end(),
|
|
442
|
+
},
|
|
443
|
+
),
|
|
444
|
+
);
|
|
445
|
+
} catch (error) {
|
|
446
|
+
stream.end();
|
|
447
|
+
if (
|
|
448
|
+
error instanceof ProjectNotFoundError ||
|
|
449
|
+
error instanceof AgentSessionNotFoundError
|
|
450
|
+
)
|
|
451
|
+
return reply.status(404).send({ error: "Session not found" });
|
|
452
|
+
throw error;
|
|
453
|
+
}
|
|
454
|
+
reply.hijack();
|
|
455
|
+
stream.open(reply.getHeaders());
|
|
456
|
+
return reply;
|
|
457
|
+
},
|
|
458
|
+
});
|
|
459
|
+
|
|
460
|
+
// A person's command to a session (ADR 0197): send, interrupt, retry,
|
|
461
|
+
// queue edits, send now, answer a request, roll back. Each carries a
|
|
462
|
+
// client `commandId`; sending it again returns the first receipt. A
|
|
463
|
+
// refusal is a durable answer too, so both receipts are 200.
|
|
464
|
+
typed.route({
|
|
465
|
+
method: "POST",
|
|
466
|
+
url: "/projects/:projectId/agent/sessions/:sessionId/commands",
|
|
467
|
+
// Base64 media rides in a send's attachments; the rest of the API keeps
|
|
468
|
+
// Fastify's default cap.
|
|
469
|
+
bodyLimit: 96 * 1024 * 1024,
|
|
470
|
+
schema: {
|
|
471
|
+
params: AgentSessionIdParamsSchema,
|
|
472
|
+
body: SessionCommandSchema,
|
|
473
|
+
response: {
|
|
474
|
+
200: CommandReceiptSchema,
|
|
475
|
+
404: ErrorSchema,
|
|
476
|
+
409: SessionConflictSchema,
|
|
477
|
+
422: EnvironmentErrorSchema,
|
|
478
|
+
503: ErrorSchema,
|
|
479
|
+
},
|
|
480
|
+
},
|
|
481
|
+
handler: async (request, reply) => {
|
|
482
|
+
const agentSessions = ctx.core?.agentSessions;
|
|
483
|
+
if (!agentSessions)
|
|
484
|
+
return reply.status(503).send({ error: "Coding agent not configured" });
|
|
485
|
+
try {
|
|
486
|
+
return reply.send(
|
|
487
|
+
await agentSessions.command(
|
|
488
|
+
resolveIdentity(request),
|
|
489
|
+
request.params.projectId,
|
|
490
|
+
request.params.sessionId,
|
|
491
|
+
request.body,
|
|
492
|
+
),
|
|
493
|
+
);
|
|
494
|
+
} catch (err) {
|
|
495
|
+
if (
|
|
496
|
+
err instanceof ProjectNotFoundError ||
|
|
497
|
+
err instanceof AgentSessionNotFoundError
|
|
498
|
+
)
|
|
499
|
+
return reply.status(404).send({ error: "Session not found" });
|
|
500
|
+
const conflict = sessionConflict(err);
|
|
501
|
+
if (conflict) return reply.status(409).send(conflict);
|
|
502
|
+
// Another rollback is rewinding the files: the same command, sent
|
|
503
|
+
// again shortly, answers with its receipt.
|
|
504
|
+
if (err instanceof AgentSessionRewindingError)
|
|
505
|
+
return reply.status(503).send({ error: err.message });
|
|
506
|
+
if (err instanceof UnsupportedAgentTopologyError)
|
|
507
|
+
return reply.status(422).send({
|
|
508
|
+
error: err.message,
|
|
509
|
+
code: "agent_topology_unsupported",
|
|
510
|
+
});
|
|
511
|
+
throw err;
|
|
512
|
+
}
|
|
513
|
+
},
|
|
514
|
+
});
|
|
515
|
+
|
|
516
|
+
// Session mirroring (ADR 0197): another backend (a linked desktop)
|
|
517
|
+
// pushes the session's log after this copy's sequence, so members see it
|
|
518
|
+
// here and can continue it when the source is gone. A copy that does not
|
|
519
|
+
// exist yet starts from the push's `base` snapshot.
|
|
520
|
+
// A mirrored chat is continued here: this host takes its authority, if
|
|
521
|
+
// the source still holds the revision the caller saw (ADR 0077).
|
|
522
|
+
typed.route({
|
|
523
|
+
method: "POST",
|
|
524
|
+
url: "/projects/:projectId/agent/sessions/:sessionId/resume",
|
|
525
|
+
schema: {
|
|
526
|
+
params: AgentSessionIdParamsSchema,
|
|
527
|
+
body: ResumeAgentSessionSchema,
|
|
528
|
+
response: {
|
|
529
|
+
200: AgentSessionSchema,
|
|
530
|
+
404: ErrorSchema,
|
|
531
|
+
409: ErrorSchema,
|
|
532
|
+
503: ErrorSchema,
|
|
533
|
+
},
|
|
534
|
+
},
|
|
535
|
+
handler: async (request, reply) => {
|
|
536
|
+
const agentSessions = ctx.core?.agentSessions;
|
|
537
|
+
if (!agentSessions) {
|
|
538
|
+
return reply.status(503).send({ error: "Coding agent not configured" });
|
|
539
|
+
}
|
|
540
|
+
try {
|
|
541
|
+
return reply.send(
|
|
542
|
+
await agentSessions.resume(
|
|
543
|
+
resolveIdentity(request),
|
|
544
|
+
request.params.projectId,
|
|
545
|
+
request.params.sessionId,
|
|
546
|
+
request.body,
|
|
547
|
+
),
|
|
548
|
+
);
|
|
549
|
+
} catch (error) {
|
|
550
|
+
if (
|
|
551
|
+
error instanceof ProjectNotFoundError ||
|
|
552
|
+
error instanceof AgentSessionNotFoundError
|
|
553
|
+
) {
|
|
554
|
+
return reply.status(404).send({ error: "Session not found" });
|
|
555
|
+
}
|
|
556
|
+
if (
|
|
557
|
+
error instanceof AgentSessionClosedError ||
|
|
558
|
+
error instanceof SessionMirrorDivergedError
|
|
559
|
+
) {
|
|
560
|
+
return reply.status(409).send({ error: error.message });
|
|
561
|
+
}
|
|
562
|
+
throw error;
|
|
563
|
+
}
|
|
564
|
+
},
|
|
565
|
+
});
|
|
566
|
+
|
|
567
|
+
typed.route({
|
|
568
|
+
method: "PUT",
|
|
569
|
+
url: "/projects/:projectId/agent/sessions/:sessionId/mirror",
|
|
570
|
+
// A new copy carries a whole session (tool inputs and results
|
|
571
|
+
// included): the order of the media one message may carry.
|
|
572
|
+
bodyLimit: 96 * 1024 * 1024,
|
|
573
|
+
schema: {
|
|
574
|
+
params: AgentSessionIdParamsSchema,
|
|
575
|
+
body: MirrorAgentSessionSchema,
|
|
576
|
+
response: {
|
|
577
|
+
200: MirrorAgentSessionResultSchema,
|
|
578
|
+
400: ErrorSchema,
|
|
579
|
+
403: EnvironmentAccessErrorSchema,
|
|
580
|
+
404: ErrorSchema,
|
|
581
|
+
409: z.union([MirrorConflictSchema, EnvironmentErrorSchema]),
|
|
582
|
+
422: EnvironmentErrorSchema,
|
|
583
|
+
428: AuthenticationRequiredSchema,
|
|
584
|
+
503: ErrorSchema,
|
|
585
|
+
},
|
|
586
|
+
},
|
|
587
|
+
handler: async (request, reply) => {
|
|
588
|
+
const agentSessions = ctx.core?.agentSessions;
|
|
589
|
+
if (!agentSessions)
|
|
590
|
+
return reply.status(503).send({ error: "Coding agent not configured" });
|
|
591
|
+
try {
|
|
592
|
+
const { sequence, agentNotice, ...session } =
|
|
593
|
+
await agentSessions.mirror(
|
|
594
|
+
resolveIdentity(request),
|
|
595
|
+
request.params.projectId,
|
|
596
|
+
request.params.sessionId,
|
|
597
|
+
request.body,
|
|
598
|
+
);
|
|
599
|
+
return reply.send({
|
|
600
|
+
session,
|
|
601
|
+
sequence,
|
|
602
|
+
...(agentNotice ? { agentNotice } : {}),
|
|
603
|
+
});
|
|
604
|
+
} catch (err) {
|
|
605
|
+
if (err instanceof ProjectNotFoundError)
|
|
606
|
+
return reply.status(404).send({ error: "Project not found" });
|
|
607
|
+
if (
|
|
608
|
+
err instanceof ForeignSessionEventError ||
|
|
609
|
+
err instanceof SessionMetadataError
|
|
610
|
+
)
|
|
611
|
+
return reply.status(400).send({ error: err.message });
|
|
612
|
+
if (err instanceof SessionMirrorDivergedError)
|
|
613
|
+
return reply
|
|
614
|
+
.status(409)
|
|
615
|
+
.send({ error: err.message, code: "diverged" as const });
|
|
616
|
+
if (err instanceof SessionMirrorBehindError)
|
|
617
|
+
return reply.status(409).send({
|
|
618
|
+
error: err.message,
|
|
619
|
+
code: "behind" as const,
|
|
620
|
+
sequence: err.sequence,
|
|
621
|
+
});
|
|
622
|
+
if (err instanceof AgentTurnInProgressError)
|
|
623
|
+
return reply.status(409).send({
|
|
624
|
+
error: "A turn is in progress here; push again when it settles",
|
|
625
|
+
code: "turn_in_progress" as const,
|
|
626
|
+
});
|
|
627
|
+
if (err instanceof AuthenticationRequiredError)
|
|
628
|
+
return reply.status(428).send({
|
|
629
|
+
error: err.message,
|
|
630
|
+
code: "authentication_required",
|
|
631
|
+
environment: err.environment,
|
|
632
|
+
requirements: [...err.requirements],
|
|
633
|
+
});
|
|
634
|
+
if (err instanceof EnvironmentAccessDeniedError)
|
|
635
|
+
return reply.status(403).send({
|
|
636
|
+
error: err.message,
|
|
637
|
+
code: "environment_access_denied",
|
|
638
|
+
});
|
|
639
|
+
if (
|
|
640
|
+
err instanceof EnvironmentCapacityError ||
|
|
641
|
+
err instanceof EnvironmentBindingUnavailableError ||
|
|
642
|
+
err instanceof EnvironmentNotFoundError
|
|
643
|
+
)
|
|
644
|
+
return reply.status(409).send({
|
|
645
|
+
error: err.message,
|
|
646
|
+
code:
|
|
647
|
+
err instanceof EnvironmentCapacityError
|
|
648
|
+
? "environment_full"
|
|
649
|
+
: "environment_unavailable",
|
|
650
|
+
});
|
|
651
|
+
if (err instanceof NoCompatibleEnvironmentError)
|
|
652
|
+
return reply.status(422).send({
|
|
653
|
+
error: err.message,
|
|
654
|
+
code: "environment_unavailable",
|
|
655
|
+
reasons: Object.values(err.reasons).flatMap((items) => [...items]),
|
|
656
|
+
});
|
|
657
|
+
if (err instanceof EnvironmentIncompatibleError)
|
|
658
|
+
return reply.status(422).send({
|
|
659
|
+
error: err.message,
|
|
660
|
+
code: "environment_incompatible",
|
|
661
|
+
reasons: [...err.reasons],
|
|
662
|
+
});
|
|
663
|
+
throw err;
|
|
664
|
+
}
|
|
665
|
+
},
|
|
666
|
+
});
|
|
667
|
+
|
|
668
|
+
// What a mirror of this session pushes (ADR 0197): its log after the
|
|
669
|
+
// copy's sequence, or the whole session (`base`) for a copy that does not
|
|
670
|
+
// exist yet or fell too far behind.
|
|
671
|
+
typed.route({
|
|
672
|
+
method: "GET",
|
|
673
|
+
url: "/projects/:projectId/agent/sessions/:sessionId/mirror",
|
|
674
|
+
schema: {
|
|
675
|
+
params: AgentSessionIdParamsSchema,
|
|
676
|
+
querystring: SessionMirrorQuerySchema,
|
|
677
|
+
response: {
|
|
678
|
+
200: MirrorExportSchema,
|
|
679
|
+
404: ErrorSchema,
|
|
680
|
+
503: ErrorSchema,
|
|
681
|
+
},
|
|
682
|
+
},
|
|
683
|
+
handler: async (request, reply) => {
|
|
684
|
+
const agentSessions = ctx.core?.agentSessions;
|
|
685
|
+
if (!agentSessions)
|
|
686
|
+
return reply.status(503).send({ error: "Coding agent not configured" });
|
|
687
|
+
try {
|
|
688
|
+
const { projectEvents, ...exported } = await agentSessions.mirrorExport(
|
|
689
|
+
{
|
|
690
|
+
identity: resolveIdentity(request),
|
|
691
|
+
projectId: request.params.projectId,
|
|
692
|
+
sessionId: request.params.sessionId,
|
|
693
|
+
after: request.query.after ?? null,
|
|
694
|
+
},
|
|
695
|
+
);
|
|
696
|
+
return reply.send({
|
|
697
|
+
...exported,
|
|
698
|
+
...(projectEvents
|
|
699
|
+
? {
|
|
700
|
+
projectEvents: projectEvents.map((event) => ({
|
|
701
|
+
...event,
|
|
702
|
+
payload: wireObject(event.payload),
|
|
703
|
+
})),
|
|
704
|
+
}
|
|
705
|
+
: {}),
|
|
706
|
+
});
|
|
707
|
+
} catch (error) {
|
|
708
|
+
if (
|
|
709
|
+
error instanceof ProjectNotFoundError ||
|
|
710
|
+
error instanceof AgentSessionNotFoundError
|
|
711
|
+
)
|
|
712
|
+
return reply.status(404).send({ error: "Session not found" });
|
|
713
|
+
throw error;
|
|
714
|
+
}
|
|
715
|
+
},
|
|
716
|
+
});
|
|
717
|
+
|
|
718
|
+
typed.route({
|
|
719
|
+
method: "POST",
|
|
720
|
+
url: "/projects/:projectId/agent/sessions/:sessionId/attention/acknowledge",
|
|
721
|
+
schema: {
|
|
722
|
+
params: AgentSessionIdParamsSchema,
|
|
723
|
+
body: z
|
|
724
|
+
.object({ observedRevision: z.number().int().nonnegative().optional() })
|
|
725
|
+
.nullish(),
|
|
726
|
+
response: {
|
|
727
|
+
200: AgentSessionSchema,
|
|
728
|
+
404: ErrorSchema,
|
|
729
|
+
503: ErrorSchema,
|
|
730
|
+
},
|
|
731
|
+
},
|
|
732
|
+
handler: async (request, reply) => {
|
|
733
|
+
const agentSessions = ctx.core?.agentSessions;
|
|
734
|
+
if (!agentSessions) {
|
|
735
|
+
return reply.status(503).send({ error: "Coding agent not configured" });
|
|
736
|
+
}
|
|
737
|
+
const identity = resolveIdentity(request);
|
|
738
|
+
try {
|
|
739
|
+
return reply.send(
|
|
740
|
+
await agentSessions.acknowledgeAttention(
|
|
741
|
+
identity,
|
|
742
|
+
request.params.projectId,
|
|
743
|
+
request.params.sessionId,
|
|
744
|
+
request.body ?? {},
|
|
745
|
+
),
|
|
746
|
+
);
|
|
747
|
+
} catch (err) {
|
|
748
|
+
if (
|
|
749
|
+
err instanceof ProjectNotFoundError ||
|
|
750
|
+
err instanceof AgentSessionNotFoundError
|
|
751
|
+
) {
|
|
752
|
+
return reply.status(404).send({ error: "Session not found" });
|
|
753
|
+
}
|
|
754
|
+
throw err;
|
|
755
|
+
}
|
|
756
|
+
},
|
|
757
|
+
});
|
|
758
|
+
|
|
759
|
+
typed.route({
|
|
760
|
+
method: "GET",
|
|
761
|
+
url: "/projects/:projectId/agent/sessions/:sessionId/peers",
|
|
762
|
+
schema: {
|
|
763
|
+
params: AgentSessionIdParamsSchema,
|
|
764
|
+
response: {
|
|
765
|
+
200: AgentSessionPeerSchema.array(),
|
|
766
|
+
404: ErrorSchema,
|
|
767
|
+
503: ErrorSchema,
|
|
768
|
+
},
|
|
769
|
+
},
|
|
770
|
+
handler: async (request, reply) => {
|
|
771
|
+
const agentSessions = ctx.core?.agentSessions;
|
|
772
|
+
if (!agentSessions) {
|
|
773
|
+
return reply.status(503).send({ error: "Coding agent not configured" });
|
|
774
|
+
}
|
|
775
|
+
try {
|
|
776
|
+
return reply.send(
|
|
777
|
+
await agentSessions.listPeers(
|
|
778
|
+
resolveIdentity(request),
|
|
779
|
+
request.params.projectId,
|
|
780
|
+
request.params.sessionId,
|
|
781
|
+
),
|
|
782
|
+
);
|
|
783
|
+
} catch (error) {
|
|
784
|
+
if (
|
|
785
|
+
error instanceof ProjectNotFoundError ||
|
|
786
|
+
error instanceof AgentSessionNotFoundError
|
|
787
|
+
) {
|
|
788
|
+
return reply.status(404).send({ error: "Session not found" });
|
|
789
|
+
}
|
|
790
|
+
throw error;
|
|
791
|
+
}
|
|
792
|
+
},
|
|
793
|
+
});
|
|
794
|
+
|
|
795
|
+
typed.route({
|
|
796
|
+
method: "POST",
|
|
797
|
+
url: "/projects/:projectId/agent/sessions/:sessionId/subsessions",
|
|
798
|
+
schema: {
|
|
799
|
+
params: AgentSessionIdParamsSchema,
|
|
800
|
+
body: CreateAgentSubsessionSchema,
|
|
801
|
+
response: {
|
|
802
|
+
201: AgentSubsessionSchema,
|
|
803
|
+
400: ErrorSchema,
|
|
804
|
+
403: ErrorSchema,
|
|
805
|
+
404: ErrorSchema,
|
|
806
|
+
409: ErrorSchema,
|
|
807
|
+
503: ErrorSchema,
|
|
808
|
+
},
|
|
809
|
+
},
|
|
810
|
+
handler: async (request, reply) => {
|
|
811
|
+
const agentSessions = ctx.core?.agentSessions;
|
|
812
|
+
if (!agentSessions) {
|
|
813
|
+
return reply.status(503).send({ error: "Coding agent not configured" });
|
|
814
|
+
}
|
|
815
|
+
try {
|
|
816
|
+
const child = await agentSessions.createSubsession(
|
|
817
|
+
resolveIdentity(request),
|
|
818
|
+
request.params.projectId,
|
|
819
|
+
request.params.sessionId,
|
|
820
|
+
request.body,
|
|
821
|
+
);
|
|
822
|
+
return reply.status(201).send(child);
|
|
823
|
+
} catch (error) {
|
|
824
|
+
if (
|
|
825
|
+
error instanceof ProjectNotFoundError ||
|
|
826
|
+
error instanceof AgentSessionNotFoundError
|
|
827
|
+
) {
|
|
828
|
+
return reply.status(404).send({ error: "Session not found" });
|
|
829
|
+
}
|
|
830
|
+
if (error instanceof AgentDelegationDeniedError) {
|
|
831
|
+
return reply.status(403).send({ error: error.message });
|
|
832
|
+
}
|
|
833
|
+
if (
|
|
834
|
+
error instanceof AgentNotConfiguredError ||
|
|
835
|
+
error instanceof AgentDelegationSettingsError
|
|
836
|
+
) {
|
|
837
|
+
return reply.status(400).send({ error: error.message });
|
|
838
|
+
}
|
|
839
|
+
if (error instanceof AgentSessionClosedError) {
|
|
840
|
+
return reply.status(409).send({ error: error.message });
|
|
841
|
+
}
|
|
842
|
+
throw error;
|
|
843
|
+
}
|
|
844
|
+
},
|
|
845
|
+
});
|
|
846
|
+
|
|
847
|
+
typed.route({
|
|
848
|
+
method: "GET",
|
|
849
|
+
url: "/projects/:projectId/agent/sessions/:sessionId/subsessions",
|
|
850
|
+
schema: {
|
|
851
|
+
params: AgentSessionIdParamsSchema,
|
|
852
|
+
response: {
|
|
853
|
+
200: AgentSubsessionSchema.array(),
|
|
854
|
+
404: ErrorSchema,
|
|
855
|
+
503: ErrorSchema,
|
|
856
|
+
},
|
|
857
|
+
},
|
|
858
|
+
handler: async (request, reply) => {
|
|
859
|
+
const agentSessions = ctx.core?.agentSessions;
|
|
860
|
+
if (!agentSessions) {
|
|
861
|
+
return reply.status(503).send({ error: "Coding agent not configured" });
|
|
862
|
+
}
|
|
863
|
+
try {
|
|
864
|
+
return reply.send(
|
|
865
|
+
await agentSessions.listSubsessions(
|
|
866
|
+
resolveIdentity(request),
|
|
867
|
+
request.params.projectId,
|
|
868
|
+
request.params.sessionId,
|
|
869
|
+
),
|
|
870
|
+
);
|
|
871
|
+
} catch (error) {
|
|
872
|
+
if (
|
|
873
|
+
error instanceof ProjectNotFoundError ||
|
|
874
|
+
error instanceof AgentSessionNotFoundError
|
|
875
|
+
) {
|
|
876
|
+
return reply.status(404).send({ error: "Session not found" });
|
|
877
|
+
}
|
|
878
|
+
throw error;
|
|
879
|
+
}
|
|
880
|
+
},
|
|
881
|
+
});
|
|
882
|
+
|
|
883
|
+
typed.route({
|
|
884
|
+
method: "POST",
|
|
885
|
+
url: "/projects/:projectId/agent/sessions/:sessionId/subsessions/wait",
|
|
886
|
+
schema: {
|
|
887
|
+
params: AgentSessionIdParamsSchema,
|
|
888
|
+
body: WaitForAgentSubsessionsSchema,
|
|
889
|
+
response: {
|
|
890
|
+
200: AgentSubsessionSchema.array(),
|
|
891
|
+
404: ErrorSchema,
|
|
892
|
+
503: ErrorSchema,
|
|
893
|
+
},
|
|
894
|
+
},
|
|
895
|
+
handler: async (request, reply) => {
|
|
896
|
+
const agentSessions = ctx.core?.agentSessions;
|
|
897
|
+
if (!agentSessions) {
|
|
898
|
+
return reply.status(503).send({ error: "Coding agent not configured" });
|
|
899
|
+
}
|
|
900
|
+
try {
|
|
901
|
+
return reply.send(
|
|
902
|
+
await agentSessions.waitForSubsessions(
|
|
903
|
+
resolveIdentity(request),
|
|
904
|
+
request.params.projectId,
|
|
905
|
+
request.params.sessionId,
|
|
906
|
+
// A caller that went away has stopped waiting.
|
|
907
|
+
{ ...request.body, stopWaiting: hungUp(reply) },
|
|
908
|
+
),
|
|
909
|
+
);
|
|
910
|
+
} catch (error) {
|
|
911
|
+
if (
|
|
912
|
+
error instanceof ProjectNotFoundError ||
|
|
913
|
+
error instanceof AgentSessionNotFoundError
|
|
914
|
+
) {
|
|
915
|
+
return reply.status(404).send({ error: "Session not found" });
|
|
916
|
+
}
|
|
917
|
+
throw error;
|
|
918
|
+
}
|
|
919
|
+
},
|
|
920
|
+
});
|
|
921
|
+
|
|
922
|
+
typed.route({
|
|
923
|
+
method: "POST",
|
|
924
|
+
url: "/projects/:projectId/agent/sessions/:sessionId/subsessions/:childSessionId/interrupt",
|
|
925
|
+
schema: {
|
|
926
|
+
params: AgentSubsessionIdParamsSchema,
|
|
927
|
+
response: {
|
|
928
|
+
200: OkSchema,
|
|
929
|
+
403: ErrorSchema,
|
|
930
|
+
404: ErrorSchema,
|
|
931
|
+
503: ErrorSchema,
|
|
932
|
+
},
|
|
933
|
+
},
|
|
934
|
+
handler: async (request, reply) => {
|
|
935
|
+
const agentSessions = ctx.core?.agentSessions;
|
|
936
|
+
if (!agentSessions) {
|
|
937
|
+
return reply.status(503).send({ error: "Coding agent not configured" });
|
|
938
|
+
}
|
|
939
|
+
try {
|
|
940
|
+
await agentSessions.interruptSubsession(
|
|
941
|
+
resolveIdentity(request),
|
|
942
|
+
request.params.projectId,
|
|
943
|
+
request.params.sessionId,
|
|
944
|
+
request.params.childSessionId,
|
|
945
|
+
);
|
|
946
|
+
return reply.send({ ok: true });
|
|
947
|
+
} catch (error) {
|
|
948
|
+
if (
|
|
949
|
+
error instanceof ProjectNotFoundError ||
|
|
950
|
+
error instanceof AgentSessionNotFoundError
|
|
951
|
+
) {
|
|
952
|
+
return reply.status(404).send({ error: "Session not found" });
|
|
953
|
+
}
|
|
954
|
+
if (error instanceof AgentDelegationDeniedError) {
|
|
955
|
+
return reply.status(403).send({ error: error.message });
|
|
956
|
+
}
|
|
957
|
+
throw error;
|
|
958
|
+
}
|
|
959
|
+
},
|
|
960
|
+
});
|
|
961
|
+
|
|
962
|
+
typed.route({
|
|
963
|
+
method: "POST",
|
|
964
|
+
url: "/projects/:projectId/agent/sessions/:sessionId/attention",
|
|
965
|
+
schema: {
|
|
966
|
+
params: AgentSessionIdParamsSchema,
|
|
967
|
+
response: {
|
|
968
|
+
200: AgentSessionSchema,
|
|
969
|
+
404: ErrorSchema,
|
|
970
|
+
503: ErrorSchema,
|
|
971
|
+
},
|
|
972
|
+
},
|
|
973
|
+
handler: async (request, reply) => {
|
|
974
|
+
const agentSessions = ctx.core?.agentSessions;
|
|
975
|
+
if (!agentSessions) {
|
|
976
|
+
return reply.status(503).send({ error: "Coding agent not configured" });
|
|
977
|
+
}
|
|
978
|
+
try {
|
|
979
|
+
return reply.send(
|
|
980
|
+
await agentSessions.requestAttention(
|
|
981
|
+
resolveIdentity(request),
|
|
982
|
+
request.params.projectId,
|
|
983
|
+
request.params.sessionId,
|
|
984
|
+
),
|
|
985
|
+
);
|
|
986
|
+
} catch (error) {
|
|
987
|
+
if (
|
|
988
|
+
error instanceof ProjectNotFoundError ||
|
|
989
|
+
error instanceof AgentSessionNotFoundError
|
|
990
|
+
) {
|
|
991
|
+
return reply.status(404).send({ error: "Session not found" });
|
|
992
|
+
}
|
|
993
|
+
throw error;
|
|
994
|
+
}
|
|
995
|
+
},
|
|
996
|
+
});
|
|
997
|
+
|
|
998
|
+
typed.route({
|
|
999
|
+
method: "PATCH",
|
|
1000
|
+
url: "/projects/:projectId/agent/sessions/:sessionId/activity",
|
|
1001
|
+
schema: {
|
|
1002
|
+
params: AgentSessionIdParamsSchema,
|
|
1003
|
+
body: UpdateAgentSessionActivitySchema,
|
|
1004
|
+
response: {
|
|
1005
|
+
200: OkSchema,
|
|
1006
|
+
404: ErrorSchema,
|
|
1007
|
+
409: SessionConflictSchema,
|
|
1008
|
+
503: ErrorSchema,
|
|
1009
|
+
},
|
|
1010
|
+
},
|
|
1011
|
+
handler: async (request, reply) => {
|
|
1012
|
+
const agentSessions = ctx.core?.agentSessions;
|
|
1013
|
+
if (!agentSessions) {
|
|
1014
|
+
return reply.status(503).send({ error: "Coding agent not configured" });
|
|
1015
|
+
}
|
|
1016
|
+
try {
|
|
1017
|
+
await agentSessions.setActivity(
|
|
1018
|
+
resolveIdentity(request),
|
|
1019
|
+
request.params.projectId,
|
|
1020
|
+
request.params.sessionId,
|
|
1021
|
+
request.body.activity,
|
|
1022
|
+
);
|
|
1023
|
+
return reply.send({ ok: true });
|
|
1024
|
+
} catch (error) {
|
|
1025
|
+
if (
|
|
1026
|
+
error instanceof ProjectNotFoundError ||
|
|
1027
|
+
error instanceof AgentSessionNotFoundError
|
|
1028
|
+
) {
|
|
1029
|
+
return reply.status(404).send({ error: "Session not found" });
|
|
1030
|
+
}
|
|
1031
|
+
// Only the host holding the session changes it.
|
|
1032
|
+
const conflict = sessionConflict(error);
|
|
1033
|
+
if (conflict) return reply.status(409).send(conflict);
|
|
1034
|
+
throw error;
|
|
1035
|
+
}
|
|
1036
|
+
},
|
|
1037
|
+
});
|
|
1038
|
+
|
|
1039
|
+
// A person names their chat; a harness's title no longer replaces it
|
|
1040
|
+
// (ADR 0247).
|
|
1041
|
+
typed.route({
|
|
1042
|
+
method: "PATCH",
|
|
1043
|
+
url: "/projects/:projectId/agent/sessions/:sessionId/title",
|
|
1044
|
+
schema: {
|
|
1045
|
+
params: AgentSessionIdParamsSchema,
|
|
1046
|
+
body: RenameAgentSessionSchema,
|
|
1047
|
+
response: {
|
|
1048
|
+
200: OkSchema,
|
|
1049
|
+
404: ErrorSchema,
|
|
1050
|
+
409: SessionConflictSchema,
|
|
1051
|
+
503: ErrorSchema,
|
|
1052
|
+
},
|
|
1053
|
+
},
|
|
1054
|
+
handler: async (request, reply) => {
|
|
1055
|
+
const agentSessions = ctx.core?.agentSessions;
|
|
1056
|
+
if (!agentSessions) {
|
|
1057
|
+
return reply.status(503).send({ error: "Coding agent not configured" });
|
|
1058
|
+
}
|
|
1059
|
+
try {
|
|
1060
|
+
await agentSessions.rename(
|
|
1061
|
+
resolveIdentity(request),
|
|
1062
|
+
request.params.projectId,
|
|
1063
|
+
request.params.sessionId,
|
|
1064
|
+
request.body.title,
|
|
1065
|
+
);
|
|
1066
|
+
return reply.send({ ok: true });
|
|
1067
|
+
} catch (error) {
|
|
1068
|
+
if (
|
|
1069
|
+
error instanceof ProjectNotFoundError ||
|
|
1070
|
+
error instanceof AgentSessionNotFoundError
|
|
1071
|
+
) {
|
|
1072
|
+
return reply.status(404).send({ error: "Session not found" });
|
|
1073
|
+
}
|
|
1074
|
+
// Only the host holding the session changes it.
|
|
1075
|
+
const conflict = sessionConflict(error);
|
|
1076
|
+
if (conflict) return reply.status(409).send(conflict);
|
|
1077
|
+
throw error;
|
|
1078
|
+
}
|
|
1079
|
+
},
|
|
1080
|
+
});
|
|
1081
|
+
|
|
1082
|
+
// Named values on a session (ADR 0226): each key takes its value, null
|
|
1083
|
+
// removes it. Agents set them through their tools; any client may.
|
|
1084
|
+
typed.route({
|
|
1085
|
+
method: "PATCH",
|
|
1086
|
+
url: "/projects/:projectId/agent/sessions/:sessionId/metadata",
|
|
1087
|
+
schema: {
|
|
1088
|
+
params: AgentSessionIdParamsSchema,
|
|
1089
|
+
body: UpdateAgentSessionMetadataSchema,
|
|
1090
|
+
response: {
|
|
1091
|
+
200: AgentSessionMetadataSchema,
|
|
1092
|
+
400: ErrorSchema,
|
|
1093
|
+
403: ErrorSchema,
|
|
1094
|
+
404: ErrorSchema,
|
|
1095
|
+
409: SessionConflictSchema,
|
|
1096
|
+
503: ErrorSchema,
|
|
1097
|
+
},
|
|
1098
|
+
},
|
|
1099
|
+
handler: async (request, reply) => {
|
|
1100
|
+
const agentSessions = ctx.core?.agentSessions;
|
|
1101
|
+
if (!agentSessions) {
|
|
1102
|
+
return reply.status(503).send({ error: "Coding agent not configured" });
|
|
1103
|
+
}
|
|
1104
|
+
try {
|
|
1105
|
+
const metadata = await agentSessions.setMetadata({
|
|
1106
|
+
identity: resolveIdentity(request),
|
|
1107
|
+
projectId: request.params.projectId,
|
|
1108
|
+
sessionId: request.params.sessionId,
|
|
1109
|
+
values: request.body.metadata,
|
|
1110
|
+
});
|
|
1111
|
+
return reply.send({ metadata });
|
|
1112
|
+
} catch (error) {
|
|
1113
|
+
if (error instanceof SessionMetadataError) {
|
|
1114
|
+
return reply.status(400).send({ error: error.message });
|
|
1115
|
+
}
|
|
1116
|
+
if (
|
|
1117
|
+
error instanceof ProjectNotFoundError ||
|
|
1118
|
+
error instanceof AgentSessionNotFoundError
|
|
1119
|
+
) {
|
|
1120
|
+
return reply.status(404).send({ error: "Session not found" });
|
|
1121
|
+
}
|
|
1122
|
+
// Only the host holding the session writes it (a mirror would fork).
|
|
1123
|
+
const conflict = sessionConflict(error);
|
|
1124
|
+
if (conflict) return reply.status(409).send(conflict);
|
|
1125
|
+
throw error;
|
|
1126
|
+
}
|
|
1127
|
+
},
|
|
1128
|
+
});
|
|
1129
|
+
|
|
1130
|
+
// Fork the conversation: a new session on the same agent carrying the
|
|
1131
|
+
// transcript through the item `messageId` names (or every settled turn).
|
|
1132
|
+
typed.route({
|
|
1133
|
+
method: "POST",
|
|
1134
|
+
url: "/projects/:projectId/agent/sessions/:sessionId/fork",
|
|
1135
|
+
schema: {
|
|
1136
|
+
params: AgentSessionIdParamsSchema,
|
|
1137
|
+
body: ForkAgentSessionSchema,
|
|
1138
|
+
response: {
|
|
1139
|
+
201: AgentSessionSchema,
|
|
1140
|
+
404: ErrorSchema,
|
|
1141
|
+
503: ErrorSchema,
|
|
1142
|
+
},
|
|
1143
|
+
},
|
|
1144
|
+
handler: async (request, reply) => {
|
|
1145
|
+
const agentSessions = ctx.core?.agentSessions;
|
|
1146
|
+
if (!agentSessions)
|
|
1147
|
+
return reply.status(503).send({ error: "Coding agent not configured" });
|
|
1148
|
+
const identity = resolveIdentity(request);
|
|
1149
|
+
try {
|
|
1150
|
+
const session = await agentSessions.fork(
|
|
1151
|
+
identity,
|
|
1152
|
+
request.params.projectId,
|
|
1153
|
+
request.params.sessionId,
|
|
1154
|
+
{ messageId: request.body?.messageId },
|
|
1155
|
+
);
|
|
1156
|
+
return reply.status(201).send(session);
|
|
1157
|
+
} catch (err) {
|
|
1158
|
+
if (
|
|
1159
|
+
err instanceof ProjectNotFoundError ||
|
|
1160
|
+
err instanceof AgentSessionNotFoundError
|
|
1161
|
+
) {
|
|
1162
|
+
return reply.status(404).send({ error: "Session not found" });
|
|
1163
|
+
}
|
|
1164
|
+
throw err;
|
|
1165
|
+
}
|
|
1166
|
+
},
|
|
1167
|
+
});
|
|
1168
|
+
|
|
1169
|
+
typed.route({
|
|
1170
|
+
method: "DELETE",
|
|
1171
|
+
url: "/projects/:projectId/agent/sessions/:sessionId",
|
|
1172
|
+
schema: {
|
|
1173
|
+
params: AgentSessionIdParamsSchema,
|
|
1174
|
+
response: { 200: AgentSessionSchema, 404: ErrorSchema, 503: ErrorSchema },
|
|
1175
|
+
},
|
|
1176
|
+
handler: async (request, reply) => {
|
|
1177
|
+
const agentSessions = ctx.core?.agentSessions;
|
|
1178
|
+
if (!agentSessions)
|
|
1179
|
+
return reply.status(503).send({ error: "Coding agent not configured" });
|
|
1180
|
+
const identity = resolveIdentity(request);
|
|
1181
|
+
try {
|
|
1182
|
+
const session = await agentSessions.close(
|
|
1183
|
+
identity,
|
|
1184
|
+
request.params.projectId,
|
|
1185
|
+
request.params.sessionId,
|
|
1186
|
+
);
|
|
1187
|
+
return reply.send(session);
|
|
1188
|
+
} catch (err) {
|
|
1189
|
+
if (
|
|
1190
|
+
err instanceof ProjectNotFoundError ||
|
|
1191
|
+
err instanceof AgentSessionNotFoundError
|
|
1192
|
+
) {
|
|
1193
|
+
return reply.status(404).send({ error: "Session not found" });
|
|
1194
|
+
}
|
|
1195
|
+
throw err;
|
|
1196
|
+
}
|
|
1197
|
+
},
|
|
1198
|
+
});
|
|
1199
|
+
|
|
1200
|
+
// A chat by the project's key (ADR 0173). Finding never starts a chat.
|
|
1201
|
+
typed.get(
|
|
1202
|
+
"/projects/:projectId/agent/chats/:key",
|
|
1203
|
+
{
|
|
1204
|
+
schema: {
|
|
1205
|
+
params: KeyedChatParamsSchema,
|
|
1206
|
+
querystring: KeyedChatQuerySchema,
|
|
1207
|
+
response: {
|
|
1208
|
+
200: AgentSessionSchema,
|
|
1209
|
+
404: ErrorSchema,
|
|
1210
|
+
503: ErrorSchema,
|
|
1211
|
+
},
|
|
1212
|
+
},
|
|
1213
|
+
},
|
|
1214
|
+
async (request, reply) => {
|
|
1215
|
+
const agentSessions = ctx.core?.agentSessions;
|
|
1216
|
+
if (!agentSessions)
|
|
1217
|
+
return reply.status(503).send({ error: "Coding agent not configured" });
|
|
1218
|
+
const identity = resolveIdentity(request);
|
|
1219
|
+
const { projectId } = request.params;
|
|
1220
|
+
const sessionId = await agentSessions.keyedChatId({
|
|
1221
|
+
projectId,
|
|
1222
|
+
key: parseChatKey(request.params.key),
|
|
1223
|
+
ownerId: keyedChatOwnerId({
|
|
1224
|
+
caller: identity,
|
|
1225
|
+
audience: keyedAudience(request.query),
|
|
1226
|
+
}),
|
|
1227
|
+
});
|
|
1228
|
+
if (!sessionId) return reply.status(404).send({ error: "No open chat" });
|
|
1229
|
+
try {
|
|
1230
|
+
return reply.send(
|
|
1231
|
+
await agentSessions.get(identity, projectId, sessionId),
|
|
1232
|
+
);
|
|
1233
|
+
} catch (err) {
|
|
1234
|
+
// A chat the caller may not see answers like no chat at all.
|
|
1235
|
+
if (
|
|
1236
|
+
err instanceof ProjectNotFoundError ||
|
|
1237
|
+
err instanceof AgentSessionNotFoundError ||
|
|
1238
|
+
err instanceof AccessDeniedError
|
|
1239
|
+
)
|
|
1240
|
+
return reply.status(404).send({ error: "No open chat" });
|
|
1241
|
+
throw err;
|
|
1242
|
+
}
|
|
1243
|
+
},
|
|
1244
|
+
);
|
|
1245
|
+
|
|
1246
|
+
// Close a keyed chat: its work, workspace and key are released; the
|
|
1247
|
+
// transcript stays readable. Closing a key with no open chat is a no-op.
|
|
1248
|
+
typed.route({
|
|
1249
|
+
method: "DELETE",
|
|
1250
|
+
url: "/projects/:projectId/agent/chats/:key",
|
|
1251
|
+
schema: {
|
|
1252
|
+
params: KeyedChatParamsSchema,
|
|
1253
|
+
querystring: KeyedChatQuerySchema,
|
|
1254
|
+
response: {
|
|
1255
|
+
200: ClosedKeyedChatSchema,
|
|
1256
|
+
404: ErrorSchema,
|
|
1257
|
+
503: ErrorSchema,
|
|
1258
|
+
},
|
|
1259
|
+
},
|
|
1260
|
+
handler: async (request, reply) => {
|
|
1261
|
+
const agentSessions = ctx.core?.agentSessions;
|
|
1262
|
+
if (!agentSessions)
|
|
1263
|
+
return reply.status(503).send({ error: "Coding agent not configured" });
|
|
1264
|
+
const identity = resolveIdentity(request);
|
|
1265
|
+
const { projectId } = request.params;
|
|
1266
|
+
const sessionId = await agentSessions.keyedChatId({
|
|
1267
|
+
projectId,
|
|
1268
|
+
key: parseChatKey(request.params.key),
|
|
1269
|
+
ownerId: keyedChatOwnerId({
|
|
1270
|
+
caller: identity,
|
|
1271
|
+
audience: keyedAudience(request.query),
|
|
1272
|
+
}),
|
|
1273
|
+
});
|
|
1274
|
+
if (!sessionId) return reply.send({ sessionId: null, closed: false });
|
|
1275
|
+
try {
|
|
1276
|
+
await agentSessions.close(identity, projectId, sessionId);
|
|
1277
|
+
return reply.send({ sessionId, closed: true });
|
|
1278
|
+
} catch (err) {
|
|
1279
|
+
if (err instanceof ProjectNotFoundError)
|
|
1280
|
+
return reply.status(404).send({ error: "No open chat" });
|
|
1281
|
+
// A chat the caller may not reach answers like no open chat.
|
|
1282
|
+
if (
|
|
1283
|
+
err instanceof AgentSessionNotFoundError ||
|
|
1284
|
+
err instanceof AccessDeniedError
|
|
1285
|
+
)
|
|
1286
|
+
return reply.send({ sessionId: null, closed: false });
|
|
1287
|
+
throw err;
|
|
1288
|
+
}
|
|
1289
|
+
},
|
|
1290
|
+
});
|
|
1291
|
+
|
|
1292
|
+
typed.get(
|
|
1293
|
+
"/projects/:projectId/agent/sessions/:sessionId/archive-impact",
|
|
1294
|
+
{
|
|
1295
|
+
schema: {
|
|
1296
|
+
params: AgentSessionIdParamsSchema,
|
|
1297
|
+
response: {
|
|
1298
|
+
200: AgentSessionArchiveImpactSchema,
|
|
1299
|
+
404: ErrorSchema,
|
|
1300
|
+
503: ErrorSchema,
|
|
1301
|
+
},
|
|
1302
|
+
},
|
|
1303
|
+
},
|
|
1304
|
+
async (request, reply) => {
|
|
1305
|
+
const sessions = ctx.core?.agentSessions;
|
|
1306
|
+
if (!sessions)
|
|
1307
|
+
return reply.status(503).send({ error: "Coding agent not configured" });
|
|
1308
|
+
try {
|
|
1309
|
+
return reply.send(
|
|
1310
|
+
await sessions.archiveImpact(
|
|
1311
|
+
resolveIdentity(request),
|
|
1312
|
+
request.params.projectId,
|
|
1313
|
+
request.params.sessionId,
|
|
1314
|
+
),
|
|
1315
|
+
);
|
|
1316
|
+
} catch (error) {
|
|
1317
|
+
if (
|
|
1318
|
+
error instanceof ProjectNotFoundError ||
|
|
1319
|
+
error instanceof AgentSessionNotFoundError
|
|
1320
|
+
)
|
|
1321
|
+
return reply.status(404).send({ error: "Session not found" });
|
|
1322
|
+
throw error;
|
|
1323
|
+
}
|
|
1324
|
+
},
|
|
1325
|
+
);
|
|
1326
|
+
|
|
1327
|
+
typed.route({
|
|
1328
|
+
method: "POST",
|
|
1329
|
+
url: "/projects/:projectId/agent/sessions/:sessionId/archive",
|
|
1330
|
+
schema: {
|
|
1331
|
+
params: AgentSessionIdParamsSchema,
|
|
1332
|
+
body: ArchiveAgentSessionSchema,
|
|
1333
|
+
response: {
|
|
1334
|
+
200: AgentSessionArchiveResultSchema,
|
|
1335
|
+
404: ErrorSchema,
|
|
1336
|
+
409: AgentSessionArchiveConfirmationSchema,
|
|
1337
|
+
503: ErrorSchema,
|
|
1338
|
+
},
|
|
1339
|
+
},
|
|
1340
|
+
handler: async (request, reply) => {
|
|
1341
|
+
const agentSessions = ctx.core?.agentSessions;
|
|
1342
|
+
if (!agentSessions) {
|
|
1343
|
+
return reply.status(503).send({ error: "Coding agent not configured" });
|
|
1344
|
+
}
|
|
1345
|
+
try {
|
|
1346
|
+
return reply.send(
|
|
1347
|
+
await agentSessions.archive(
|
|
1348
|
+
resolveIdentity(request),
|
|
1349
|
+
request.params.projectId,
|
|
1350
|
+
request.params.sessionId,
|
|
1351
|
+
request.body,
|
|
1352
|
+
),
|
|
1353
|
+
);
|
|
1354
|
+
} catch (error) {
|
|
1355
|
+
if (
|
|
1356
|
+
error instanceof ProjectNotFoundError ||
|
|
1357
|
+
error instanceof AgentSessionNotFoundError
|
|
1358
|
+
) {
|
|
1359
|
+
return reply.status(404).send({ error: "Session not found" });
|
|
1360
|
+
}
|
|
1361
|
+
if (error instanceof AgentSessionArchiveConfirmationRequiredError) {
|
|
1362
|
+
return reply.status(409).send({
|
|
1363
|
+
error: error.message,
|
|
1364
|
+
code: "archive_confirmation_required",
|
|
1365
|
+
impact: error.impact,
|
|
1366
|
+
});
|
|
1367
|
+
}
|
|
1368
|
+
throw error;
|
|
1369
|
+
}
|
|
1370
|
+
},
|
|
1371
|
+
});
|
|
1372
|
+
|
|
1373
|
+
typed.route({
|
|
1374
|
+
method: "POST",
|
|
1375
|
+
url: "/projects/:projectId/agent/sessions/:sessionId/unarchive",
|
|
1376
|
+
schema: {
|
|
1377
|
+
params: AgentSessionIdParamsSchema,
|
|
1378
|
+
response: {
|
|
1379
|
+
200: AgentSessionSchema.array(),
|
|
1380
|
+
404: ErrorSchema,
|
|
1381
|
+
403: ErrorSchema,
|
|
1382
|
+
409: ErrorSchema,
|
|
1383
|
+
422: ErrorSchema,
|
|
1384
|
+
428: AuthenticationRequiredSchema,
|
|
1385
|
+
503: ErrorSchema,
|
|
1386
|
+
},
|
|
1387
|
+
},
|
|
1388
|
+
handler: async (request, reply) => {
|
|
1389
|
+
const agentSessions = ctx.core?.agentSessions;
|
|
1390
|
+
if (!agentSessions) {
|
|
1391
|
+
return reply.status(503).send({ error: "Coding agent not configured" });
|
|
1392
|
+
}
|
|
1393
|
+
try {
|
|
1394
|
+
return reply.send(
|
|
1395
|
+
await agentSessions.unarchive(
|
|
1396
|
+
resolveIdentity(request),
|
|
1397
|
+
request.params.projectId,
|
|
1398
|
+
request.params.sessionId,
|
|
1399
|
+
),
|
|
1400
|
+
);
|
|
1401
|
+
} catch (error) {
|
|
1402
|
+
if (
|
|
1403
|
+
error instanceof EnvironmentCapacityError ||
|
|
1404
|
+
error instanceof EnvironmentBindingUnavailableError
|
|
1405
|
+
)
|
|
1406
|
+
return reply.status(409).send({ error: error.message });
|
|
1407
|
+
if (error instanceof EnvironmentAccessDeniedError)
|
|
1408
|
+
return reply.status(403).send({ error: error.message });
|
|
1409
|
+
if (
|
|
1410
|
+
error instanceof EnvironmentIncompatibleError ||
|
|
1411
|
+
error instanceof NoCompatibleEnvironmentError
|
|
1412
|
+
)
|
|
1413
|
+
return reply.status(422).send({ error: error.message });
|
|
1414
|
+
if (error instanceof AuthenticationRequiredError)
|
|
1415
|
+
return reply.status(428).send({
|
|
1416
|
+
error: error.message,
|
|
1417
|
+
code: "authentication_required",
|
|
1418
|
+
environment: error.environment,
|
|
1419
|
+
requirements: [...error.requirements],
|
|
1420
|
+
});
|
|
1421
|
+
if (
|
|
1422
|
+
error instanceof ProjectNotFoundError ||
|
|
1423
|
+
error instanceof AgentSessionNotFoundError
|
|
1424
|
+
) {
|
|
1425
|
+
return reply.status(404).send({ error: "Session not found" });
|
|
1426
|
+
}
|
|
1427
|
+
throw error;
|
|
1428
|
+
}
|
|
1429
|
+
},
|
|
1430
|
+
});
|
|
1431
|
+
|
|
1432
|
+
typed.route({
|
|
1433
|
+
method: "GET",
|
|
1434
|
+
url: "/projects/:projectId/skills",
|
|
1435
|
+
schema: {
|
|
1436
|
+
params: ProjectIdParamsSchema,
|
|
1437
|
+
response: {
|
|
1438
|
+
200: SkillSchema.array(),
|
|
1439
|
+
404: ErrorSchema,
|
|
1440
|
+
503: ErrorSchema,
|
|
1441
|
+
},
|
|
1442
|
+
},
|
|
1443
|
+
handler: async (request, reply) => {
|
|
1444
|
+
if (!ctx.core)
|
|
1445
|
+
return reply.status(503).send({ error: "Service not configured" });
|
|
1446
|
+
const identity = resolveIdentity(request);
|
|
1447
|
+
try {
|
|
1448
|
+
const skills = await ctx.core.skills.list(
|
|
1449
|
+
identity,
|
|
1450
|
+
request.params.projectId,
|
|
1451
|
+
);
|
|
1452
|
+
return reply.send(skills);
|
|
1453
|
+
} catch (err) {
|
|
1454
|
+
if (err instanceof ProjectNotFoundError) {
|
|
1455
|
+
return reply.status(404).send({ error: "Project not found" });
|
|
1456
|
+
}
|
|
1457
|
+
throw err;
|
|
1458
|
+
}
|
|
1459
|
+
},
|
|
1460
|
+
});
|
|
1461
|
+
|
|
1462
|
+
typed.route({
|
|
1463
|
+
method: "GET",
|
|
1464
|
+
url: "/projects/:projectId/agent-catalog",
|
|
1465
|
+
schema: {
|
|
1466
|
+
params: ProjectIdParamsSchema,
|
|
1467
|
+
response: { 200: AgentCatalogSchema, 404: ErrorSchema, 503: ErrorSchema },
|
|
1468
|
+
},
|
|
1469
|
+
handler: async (request, reply) => {
|
|
1470
|
+
if (!ctx.core?.agentSessions)
|
|
1471
|
+
return reply.status(503).send({ error: "Agents are not configured" });
|
|
1472
|
+
return reply.send(
|
|
1473
|
+
AgentCatalogSchema.parse(
|
|
1474
|
+
await ctx.core.agentSessions.catalog({
|
|
1475
|
+
identity: resolveIdentity(request),
|
|
1476
|
+
projectId: request.params.projectId,
|
|
1477
|
+
}),
|
|
1478
|
+
),
|
|
1479
|
+
);
|
|
1480
|
+
},
|
|
1481
|
+
});
|
|
1482
|
+
|
|
1483
|
+
// Committed project agent definitions (`.work/agents/*.json`, ADR 0050) —
|
|
1484
|
+
// parsed and validated; unusable files are reported per entry.
|
|
1485
|
+
typed.route({
|
|
1486
|
+
method: "GET",
|
|
1487
|
+
url: "/projects/:projectId/agents",
|
|
1488
|
+
schema: {
|
|
1489
|
+
params: ProjectIdParamsSchema,
|
|
1490
|
+
response: {
|
|
1491
|
+
200: ProjectAgentEntrySchema.array(),
|
|
1492
|
+
404: ErrorSchema,
|
|
1493
|
+
503: ErrorSchema,
|
|
1494
|
+
},
|
|
1495
|
+
},
|
|
1496
|
+
handler: async (request, reply) => {
|
|
1497
|
+
if (!ctx.core)
|
|
1498
|
+
return reply.status(503).send({ error: "Service not configured" });
|
|
1499
|
+
const identity = resolveIdentity(request);
|
|
1500
|
+
try {
|
|
1501
|
+
const agents = await ctx.core.agentDefinitions.list(
|
|
1502
|
+
identity,
|
|
1503
|
+
request.params.projectId,
|
|
1504
|
+
);
|
|
1505
|
+
return reply.send(agents);
|
|
1506
|
+
} catch (err) {
|
|
1507
|
+
if (err instanceof ProjectNotFoundError) {
|
|
1508
|
+
return reply.status(404).send({ error: "Project not found" });
|
|
1509
|
+
}
|
|
1510
|
+
throw err;
|
|
1511
|
+
}
|
|
1512
|
+
},
|
|
1513
|
+
});
|
|
1514
|
+
}
|
|
1515
|
+
|
|
1516
|
+
/**
|
|
1517
|
+
* A stored JSON object as wire JSON: members holding `undefined` are
|
|
1518
|
+
* dropped, as serializing would drop them.
|
|
1519
|
+
*/
|
|
1520
|
+
/** A session's refusal of a change, as a 409 body; null when it is not one. */
|
|
1521
|
+
function sessionConflict(
|
|
1522
|
+
err: unknown,
|
|
1523
|
+
): z.infer<typeof SessionConflictSchema> | null {
|
|
1524
|
+
if (err instanceof AgentSessionClosedError)
|
|
1525
|
+
return { error: "Session is closed", code: "session_closed" };
|
|
1526
|
+
if (err instanceof AgentSessionAuthorityRequiredError)
|
|
1527
|
+
return {
|
|
1528
|
+
error: err.message,
|
|
1529
|
+
code: "authority_required",
|
|
1530
|
+
authorityRevision: err.authorityRevision,
|
|
1531
|
+
};
|
|
1532
|
+
if (err instanceof AgentSessionHandoffPendingError)
|
|
1533
|
+
return { error: err.message, code: "handoff_pending" };
|
|
1534
|
+
return null;
|
|
1535
|
+
}
|
|
1536
|
+
|
|
1537
|
+
function wireObject(value: object): JsonObject {
|
|
1538
|
+
return Object.fromEntries(
|
|
1539
|
+
Object.entries(value)
|
|
1540
|
+
.filter((entry) => entry[1] !== undefined)
|
|
1541
|
+
.map(([key, member]) => [key, wireJson(member)]),
|
|
1542
|
+
);
|
|
1543
|
+
}
|
|
1544
|
+
|
|
1545
|
+
function wireJson(value: unknown): JsonValue {
|
|
1546
|
+
if (
|
|
1547
|
+
value === null ||
|
|
1548
|
+
typeof value === "string" ||
|
|
1549
|
+
typeof value === "number" ||
|
|
1550
|
+
typeof value === "boolean"
|
|
1551
|
+
)
|
|
1552
|
+
return value;
|
|
1553
|
+
if (Array.isArray(value))
|
|
1554
|
+
return value.map((member) =>
|
|
1555
|
+
member === undefined ? null : wireJson(member),
|
|
1556
|
+
);
|
|
1557
|
+
return typeof value === "object" ? wireObject(value) : null;
|
|
1558
|
+
}
|
|
1559
|
+
|
|
1560
|
+
function keyedAudience(query: {
|
|
1561
|
+
audience?: "project";
|
|
1562
|
+
member?: string;
|
|
1563
|
+
}): "project" | { member: string } | undefined {
|
|
1564
|
+
if (query.audience) return query.audience;
|
|
1565
|
+
return query.member ? { member: query.member } : undefined;
|
|
1566
|
+
}
|