@critical-path/server 0.19.0 → 0.20.0

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/dist/router.js CHANGED
@@ -120,612 +120,648 @@ export class CriticalPathRouter {
120
120
  }
121
121
  : { ...ANONYMOUS_ACTOR, tenantId: context?.tenantId });
122
122
  try {
123
- // OpenAPI contract (behind the same auth as every other route)
124
- if (segments[0] === 'openapi.json' && segments.length === 1 && method === 'GET') {
125
- return this.jsonResponse(buildOpenApiDocument({ serverUrl: this.openApiServerUrl(url, subpath) }));
126
- }
127
- // Webhooks API
128
- if (segments[0] === 'webhooks') {
129
- const webhookId = segments[1];
130
- if (!webhookId) {
131
- if (method === 'GET') {
132
- return this.jsonResponse({ webhooks: await engine.getWebhooks() });
133
- }
134
- if (method === 'POST') {
135
- const body = await this.readBody(request, schemas.CreateWebhookSchema);
136
- const { webhook, secret } = await engine.createWebhook(body);
137
- return this.jsonResponse({ webhook, secret }, 201);
138
- }
123
+ // Plugin init (and seeding) must finish before any request is served.
124
+ await this.engine.ready;
125
+ const pluginContext = { engine, context: context ?? undefined, url };
126
+ const handle = async () => {
127
+ const matched = this.matchPluginRoute(method, segments);
128
+ if (matched)
129
+ return matched.route.handler(request, { ...pluginContext, params: matched.params });
130
+ return this.builtInRoutes(request, { url, pathname, method, subpath, segments, engine, context });
131
+ };
132
+ // Plugin middleware wraps routing, in registration order (first registered runs outermost).
133
+ const chain = this.engine.plugins
134
+ .getMiddleware()
135
+ .reduceRight((next, middleware) => async () => middleware(request, pluginContext, next), handle);
136
+ return await chain();
137
+ }
138
+ catch (err) {
139
+ return this.errorResponse(err, request);
140
+ }
141
+ }
142
+ matchPluginRoute(method, segments) {
143
+ for (const route of this.engine.plugins.getRoutes()) {
144
+ if (route.method !== method && !(route.method === 'PATCH' && method === 'PUT'))
145
+ continue;
146
+ const pattern = route.path.split('/').filter(Boolean);
147
+ if (pattern.length !== segments.length)
148
+ continue;
149
+ const params = {};
150
+ const matches = pattern.every((part, i) => {
151
+ if (part.startsWith(':')) {
152
+ params[part.slice(1)] = decodeURIComponent(segments[i]);
153
+ return true;
154
+ }
155
+ return part === segments[i];
156
+ });
157
+ if (matches)
158
+ return { route, params };
159
+ }
160
+ return null;
161
+ }
162
+ async builtInRoutes(request, { url, pathname, method, subpath, segments, engine, context }) {
163
+ // OpenAPI contract (behind the same auth as every other route)
164
+ if (segments[0] === 'openapi.json' && segments.length === 1 && method === 'GET') {
165
+ return this.jsonResponse(buildOpenApiDocument({ serverUrl: this.openApiServerUrl(url, subpath) }));
166
+ }
167
+ // Webhooks API
168
+ if (segments[0] === 'webhooks') {
169
+ const webhookId = segments[1];
170
+ if (!webhookId) {
171
+ if (method === 'GET') {
172
+ return this.jsonResponse({ webhooks: await engine.getWebhooks() });
139
173
  }
140
- else {
141
- if (method === 'GET') {
142
- const webhook = await engine.getWebhook(webhookId);
143
- if (!webhook)
144
- return this.jsonResponse({ error: 'Webhook not found' }, 404);
145
- return this.jsonResponse({ webhook });
146
- }
147
- if (method === 'PATCH' || method === 'PUT') {
148
- const body = await this.readBody(request, schemas.UpdateWebhookSchema);
149
- const webhook = await engine.updateWebhook(webhookId, body);
150
- if (!webhook)
151
- return this.jsonResponse({ error: 'Webhook not found' }, 404);
152
- return this.jsonResponse({ webhook });
153
- }
154
- if (method === 'DELETE') {
155
- const deleted = await engine.deleteWebhook(webhookId);
156
- if (!deleted)
157
- return this.jsonResponse({ error: 'Not found' }, 404);
158
- return this.jsonResponse({ success: true });
159
- }
174
+ if (method === 'POST') {
175
+ const body = await this.readBody(request, schemas.CreateWebhookSchema);
176
+ const { webhook, secret } = await engine.createWebhook(body);
177
+ return this.jsonResponse({ webhook, secret }, 201);
160
178
  }
161
179
  }
162
- // Workflows API
163
- if (segments[0] === 'workflows') {
164
- const workflowId = segments[1];
165
- if (!workflowId) {
166
- if (method === 'GET') {
167
- const workflows = await engine.getWorkflows();
168
- return this.jsonResponse({ workflows });
169
- }
170
- if (method === 'POST') {
171
- const body = await this.readBody(request, schemas.CreateWorkflowSchema);
172
- const workflow = await engine.createWorkflow(body);
173
- return this.jsonResponse({ workflow }, 201);
174
- }
180
+ else {
181
+ if (method === 'GET') {
182
+ const webhook = await engine.getWebhook(webhookId);
183
+ if (!webhook)
184
+ return this.jsonResponse({ error: 'Webhook not found' }, 404);
185
+ return this.jsonResponse({ webhook });
186
+ }
187
+ if (method === 'PATCH' || method === 'PUT') {
188
+ const body = await this.readBody(request, schemas.UpdateWebhookSchema);
189
+ const webhook = await engine.updateWebhook(webhookId, body);
190
+ if (!webhook)
191
+ return this.jsonResponse({ error: 'Webhook not found' }, 404);
192
+ return this.jsonResponse({ webhook });
193
+ }
194
+ if (method === 'DELETE') {
195
+ const deleted = await engine.deleteWebhook(webhookId);
196
+ if (!deleted)
197
+ return this.jsonResponse({ error: 'Not found' }, 404);
198
+ return this.jsonResponse({ success: true });
175
199
  }
176
- else {
177
- if (method === 'GET') {
178
- const workflow = await engine.getWorkflow(workflowId);
179
- if (!workflow)
180
- return this.jsonResponse({ error: 'Workflow not found' }, 404);
181
- return this.jsonResponse({ workflow });
182
- }
183
- if (method === 'PATCH' || method === 'PUT') {
184
- const body = await this.readBody(request, schemas.UpdateWorkflowSchema);
185
- const updated = await engine.updateWorkflow(workflowId, body);
186
- if (!updated)
187
- return this.jsonResponse({ error: 'Workflow not found' }, 404);
188
- return this.jsonResponse({ workflow: updated });
189
- }
190
- if (method === 'DELETE') {
191
- const deleted = await engine.deleteWorkflow(workflowId);
192
- if (!deleted)
193
- return this.jsonResponse({ error: 'Not found' }, 404);
194
- return this.jsonResponse({ success: true });
195
- }
200
+ }
201
+ }
202
+ // Workflows API
203
+ if (segments[0] === 'workflows') {
204
+ const workflowId = segments[1];
205
+ if (!workflowId) {
206
+ if (method === 'GET') {
207
+ const workflows = await engine.getWorkflows();
208
+ return this.jsonResponse({ workflows });
209
+ }
210
+ if (method === 'POST') {
211
+ const body = await this.readBody(request, schemas.CreateWorkflowSchema);
212
+ const workflow = await engine.createWorkflow(body);
213
+ return this.jsonResponse({ workflow }, 201);
196
214
  }
197
215
  }
198
- // Projects API
199
- if (segments[0] === 'projects') {
200
- const projectId = segments[1];
201
- const subResource = segments[2];
202
- if (!projectId) {
203
- if (method === 'GET') {
204
- const projects = await engine.getProjects();
205
- return this.jsonResponse({ projects });
206
- }
207
- if (method === 'POST') {
208
- const body = await this.readBody(request, schemas.CreateProjectSchema);
209
- const project = await engine.createProject(body);
210
- return this.jsonResponse({ project }, 201);
211
- }
216
+ else {
217
+ if (method === 'GET') {
218
+ const workflow = await engine.getWorkflow(workflowId);
219
+ if (!workflow)
220
+ return this.jsonResponse({ error: 'Workflow not found' }, 404);
221
+ return this.jsonResponse({ workflow });
222
+ }
223
+ if (method === 'PATCH' || method === 'PUT') {
224
+ const body = await this.readBody(request, schemas.UpdateWorkflowSchema);
225
+ const updated = await engine.updateWorkflow(workflowId, body);
226
+ if (!updated)
227
+ return this.jsonResponse({ error: 'Workflow not found' }, 404);
228
+ return this.jsonResponse({ workflow: updated });
229
+ }
230
+ if (method === 'DELETE') {
231
+ const deleted = await engine.deleteWorkflow(workflowId);
232
+ if (!deleted)
233
+ return this.jsonResponse({ error: 'Not found' }, 404);
234
+ return this.jsonResponse({ success: true });
212
235
  }
213
- else if (subResource === 'critical-path') {
214
- if (method === 'GET') {
215
- const analysis = await engine.calculateCriticalPath(projectId);
216
- return this.jsonResponse({ analysis });
217
- }
236
+ }
237
+ }
238
+ // Projects API
239
+ if (segments[0] === 'projects') {
240
+ const projectId = segments[1];
241
+ const subResource = segments[2];
242
+ if (!projectId) {
243
+ if (method === 'GET') {
244
+ const projects = await engine.getProjects();
245
+ return this.jsonResponse({ projects });
218
246
  }
219
- else if (subResource === 'ladder' || subResource === 'timeline-ladder') {
220
- if (method === 'GET') {
221
- const level = (url.searchParams.get('level') || 'all');
222
- const containerId = url.searchParams.get('containerId') || undefined;
223
- const iterationId = url.searchParams.get('iterationId') || undefined;
224
- const ladder = await engine.getTimelineLadder(projectId, { level, containerId, iterationId });
225
- return this.jsonResponse({ ladder });
226
- }
247
+ if (method === 'POST') {
248
+ const body = await this.readBody(request, schemas.CreateProjectSchema);
249
+ const project = await engine.createProject(body);
250
+ return this.jsonResponse({ project }, 201);
227
251
  }
228
- else if (subResource === 'workload' || subResource === 'workload-distribution') {
229
- if (method === 'GET') {
230
- const startDate = url.searchParams.get('startDate') || undefined;
231
- const endDate = url.searchParams.get('endDate') || undefined;
232
- const interval = (url.searchParams.get('interval') || undefined);
233
- const groupBy = (url.searchParams.get('groupBy') || undefined);
234
- const metric = (url.searchParams.get('metric') || undefined);
235
- const defaultWeeklyCapacityHours = url.searchParams.get('defaultWeeklyCapacityHours')
236
- ? parseFloat(url.searchParams.get('defaultWeeklyCapacityHours'))
237
- : undefined;
238
- const workload = await engine.getWorkloadDistribution(projectId, {
239
- startDate,
240
- endDate,
241
- interval,
242
- groupBy,
243
- metric,
244
- defaultWeeklyCapacityHours
245
- });
246
- return this.jsonResponse({ workload });
247
- }
252
+ }
253
+ else if (subResource === 'critical-path') {
254
+ if (method === 'GET') {
255
+ const analysis = await engine.calculateCriticalPath(projectId);
256
+ return this.jsonResponse({ analysis });
248
257
  }
249
- else {
250
- if (method === 'GET') {
251
- const project = await engine.getProject(projectId);
252
- if (!project)
253
- return this.jsonResponse({ error: 'Project not found' }, 404);
254
- return this.jsonResponse({ project });
255
- }
256
- if (method === 'PATCH' || method === 'PUT') {
257
- const updates = await this.readBody(request, schemas.UpdateProjectSchema);
258
- const project = await engine.updateProject(projectId, updates);
259
- if (!project)
260
- return this.jsonResponse({ error: 'Project not found' }, 404);
261
- return this.jsonResponse({ project });
262
- }
263
- if (method === 'DELETE') {
264
- const deleted = await engine.deleteProject(projectId);
265
- if (!deleted)
266
- return this.jsonResponse({ error: 'Not found' }, 404);
267
- return this.jsonResponse({ success: true });
268
- }
258
+ }
259
+ else if (subResource === 'ladder' || subResource === 'timeline-ladder') {
260
+ if (method === 'GET') {
261
+ const level = (url.searchParams.get('level') || 'all');
262
+ const containerId = url.searchParams.get('containerId') || undefined;
263
+ const iterationId = url.searchParams.get('iterationId') || undefined;
264
+ const ladder = await engine.getTimelineLadder(projectId, { level, containerId, iterationId });
265
+ return this.jsonResponse({ ladder });
269
266
  }
270
267
  }
271
- // Tasks API & Task Sub-resources (Dependencies, Lifecycle State, Transitions)
272
- if (segments[0] === 'tasks') {
273
- const taskId = segments[1];
274
- const subResource = segments[2];
275
- if (!taskId) {
276
- if (method === 'GET') {
277
- const projectId = url.searchParams.get('projectId') || undefined;
278
- const tasks = await engine.getTasks(projectId);
279
- return this.jsonResponse({ tasks });
280
- }
281
- if (method === 'POST') {
282
- const body = await this.readBody(request, schemas.CreateTaskSchema);
283
- const task = await engine.createTask(body);
284
- return this.jsonResponse({ task }, 201);
285
- }
268
+ else if (subResource === 'workload' || subResource === 'workload-distribution') {
269
+ if (method === 'GET') {
270
+ const startDate = url.searchParams.get('startDate') || undefined;
271
+ const endDate = url.searchParams.get('endDate') || undefined;
272
+ const interval = (url.searchParams.get('interval') || undefined);
273
+ const groupBy = (url.searchParams.get('groupBy') || undefined);
274
+ const metric = (url.searchParams.get('metric') || undefined);
275
+ const defaultWeeklyCapacityHours = url.searchParams.get('defaultWeeklyCapacityHours')
276
+ ? parseFloat(url.searchParams.get('defaultWeeklyCapacityHours'))
277
+ : undefined;
278
+ const workload = await engine.getWorkloadDistribution(projectId, {
279
+ startDate,
280
+ endDate,
281
+ interval,
282
+ groupBy,
283
+ metric,
284
+ defaultWeeklyCapacityHours
285
+ });
286
+ return this.jsonResponse({ workload });
286
287
  }
287
- else if (subResource === 'comments') {
288
- if (method === 'GET') {
289
- const comments = await engine.getComments(taskId);
290
- return this.jsonResponse({ comments });
291
- }
292
- if (method === 'POST') {
293
- const body = await this.readBody(request, schemas.CreateCommentSchema, { taskId });
294
- const comment = await engine.addComment(body);
295
- return this.jsonResponse({ comment }, 201);
296
- }
288
+ }
289
+ else {
290
+ if (method === 'GET') {
291
+ const project = await engine.getProject(projectId);
292
+ if (!project)
293
+ return this.jsonResponse({ error: 'Project not found' }, 404);
294
+ return this.jsonResponse({ project });
295
+ }
296
+ if (method === 'PATCH' || method === 'PUT') {
297
+ const updates = await this.readBody(request, schemas.UpdateProjectSchema);
298
+ const project = await engine.updateProject(projectId, updates);
299
+ if (!project)
300
+ return this.jsonResponse({ error: 'Project not found' }, 404);
301
+ return this.jsonResponse({ project });
302
+ }
303
+ if (method === 'DELETE') {
304
+ const deleted = await engine.deleteProject(projectId);
305
+ if (!deleted)
306
+ return this.jsonResponse({ error: 'Not found' }, 404);
307
+ return this.jsonResponse({ success: true });
297
308
  }
298
- else if (subResource === 'attachments') {
299
- if (method === 'GET') {
300
- const attachments = await engine.getAttachments({ taskId });
301
- return this.jsonResponse({ attachments });
302
- }
303
- if (method === 'POST') {
304
- const body = await this.readBody(request, schemas.CreateAttachmentSchema, { taskId });
305
- const attachment = await engine.createAttachment(body);
306
- return this.jsonResponse({ attachment }, 201);
307
- }
309
+ }
310
+ }
311
+ // Tasks API & Task Sub-resources (Dependencies, Lifecycle State, Transitions)
312
+ if (segments[0] === 'tasks') {
313
+ const taskId = segments[1];
314
+ const subResource = segments[2];
315
+ if (!taskId) {
316
+ if (method === 'GET') {
317
+ const projectId = url.searchParams.get('projectId') || undefined;
318
+ const tasks = await engine.getTasks(projectId);
319
+ return this.jsonResponse({ tasks });
308
320
  }
309
- else if (subResource === 'dependencies') {
310
- if (method === 'GET') {
311
- const graph = await engine.getTaskDependencyGraph(taskId);
312
- return this.jsonResponse({ graph });
313
- }
314
- if (method === 'POST') {
315
- const body = await this.readBody(request, schemas.CreateDependencySchema);
316
- const dep = await engine.addDependency({
317
- taskId,
318
- dependsOnTaskId: body.dependsOnTaskId,
319
- type: body.type || 'blocking'
320
- });
321
- return this.jsonResponse({ dependency: dep }, 201);
322
- }
321
+ if (method === 'POST') {
322
+ const body = await this.readBody(request, schemas.CreateTaskSchema);
323
+ const task = await engine.createTask(body);
324
+ return this.jsonResponse({ task }, 201);
323
325
  }
324
- else if (subResource === 'state' || subResource === 'lifecycle') {
325
- if (method === 'GET') {
326
- const state = await engine.getTaskLifecycleState(taskId);
327
- if (!state)
328
- return this.jsonResponse({ error: 'Task not found' }, 404);
329
- return this.jsonResponse({ state });
330
- }
326
+ }
327
+ else if (subResource === 'comments') {
328
+ if (method === 'GET') {
329
+ const comments = await engine.getComments(taskId);
330
+ return this.jsonResponse({ comments });
331
331
  }
332
- else if (subResource === 'transitions' || subResource === 'allowed-transitions') {
333
- if (method === 'GET') {
334
- const allowedNextStatuses = await engine.getAllowedTaskTransitions(taskId);
335
- const allowedPreviousStatuses = await engine.getAllowedPreviousTaskTransitions(taskId);
336
- return this.jsonResponse({ allowedNextStatuses, allowedPreviousStatuses });
337
- }
332
+ if (method === 'POST') {
333
+ const body = await this.readBody(request, schemas.CreateCommentSchema, { taskId });
334
+ const comment = await engine.addComment(body);
335
+ return this.jsonResponse({ comment }, 201);
338
336
  }
339
- else if (subResource === 'ladder') {
340
- if (method === 'GET') {
341
- const taskLadder = await engine.getTaskLadder(taskId);
342
- if (!taskLadder)
343
- return this.jsonResponse({ error: 'Task not found' }, 404);
344
- return this.jsonResponse({ taskLadder });
345
- }
337
+ }
338
+ else if (subResource === 'attachments') {
339
+ if (method === 'GET') {
340
+ const attachments = await engine.getAttachments({ taskId });
341
+ return this.jsonResponse({ attachments });
346
342
  }
347
- else if (subResource === 'metrics') {
348
- if (method === 'GET') {
349
- const metrics = await engine.getTaskMetrics(taskId);
350
- if (!metrics)
351
- return this.jsonResponse({ error: 'Task not found' }, 404);
352
- return this.jsonResponse({ metrics });
353
- }
343
+ if (method === 'POST') {
344
+ const body = await this.readBody(request, schemas.CreateAttachmentSchema, { taskId });
345
+ const attachment = await engine.createAttachment(body);
346
+ return this.jsonResponse({ attachment }, 201);
354
347
  }
355
- else if (subResource === 'progress-history') {
356
- if (method === 'GET') {
357
- const progressHistory = await engine.getTaskProgressHistory(taskId);
358
- if (!progressHistory)
359
- return this.jsonResponse({ error: 'Task not found' }, 404);
360
- return this.jsonResponse({ progressHistory });
361
- }
348
+ }
349
+ else if (subResource === 'dependencies') {
350
+ if (method === 'GET') {
351
+ const graph = await engine.getTaskDependencyGraph(taskId);
352
+ return this.jsonResponse({ graph });
362
353
  }
363
- else {
364
- if (method === 'GET') {
365
- const task = await engine.getTask(taskId);
366
- if (!task)
367
- return this.jsonResponse({ error: 'Task not found' }, 404);
368
- return this.jsonResponse({ task });
369
- }
370
- if (method === 'PATCH' || method === 'PUT') {
371
- const body = await this.readBody(request, schemas.UpdateTaskSchema);
372
- const updated = await engine.updateTask(taskId, body);
373
- if (!updated)
374
- return this.jsonResponse({ error: 'Task not found' }, 404);
375
- return this.jsonResponse({ task: updated });
376
- }
377
- if (method === 'DELETE') {
378
- const deleted = await engine.deleteTask(taskId);
379
- if (!deleted)
380
- return this.jsonResponse({ error: 'Not found' }, 404);
381
- return this.jsonResponse({ success: true });
382
- }
354
+ if (method === 'POST') {
355
+ const body = await this.readBody(request, schemas.CreateDependencySchema);
356
+ const dep = await engine.addDependency({
357
+ taskId,
358
+ dependsOnTaskId: body.dependsOnTaskId,
359
+ type: body.type || 'blocking'
360
+ });
361
+ return this.jsonResponse({ dependency: dep }, 201);
383
362
  }
384
363
  }
385
- // Teams API
386
- if (segments[0] === 'teams') {
387
- const teamId = segments[1];
388
- if (!teamId) {
389
- if (method === 'GET') {
390
- const teams = await engine.getTeams();
391
- return this.jsonResponse({ teams });
392
- }
393
- if (method === 'POST') {
394
- const body = await this.readBody(request, schemas.CreateTeamSchema);
395
- const team = await engine.createTeam(body);
396
- return this.jsonResponse({ team }, 201);
397
- }
364
+ else if (subResource === 'state' || subResource === 'lifecycle') {
365
+ if (method === 'GET') {
366
+ const state = await engine.getTaskLifecycleState(taskId);
367
+ if (!state)
368
+ return this.jsonResponse({ error: 'Task not found' }, 404);
369
+ return this.jsonResponse({ state });
398
370
  }
399
- else {
400
- if (method === 'GET') {
401
- const team = await engine.getTeam(teamId);
402
- if (!team)
403
- return this.jsonResponse({ error: 'Team not found' }, 404);
404
- return this.jsonResponse({ team });
405
- }
406
- if (method === 'PATCH' || method === 'PUT') {
407
- const body = await this.readBody(request, schemas.UpdateTeamSchema);
408
- const updated = await engine.updateTeam(teamId, body);
409
- if (!updated)
410
- return this.jsonResponse({ error: 'Team not found' }, 404);
411
- return this.jsonResponse({ team: updated });
412
- }
413
- if (method === 'DELETE') {
414
- const deleted = await engine.deleteTeam(teamId);
415
- if (!deleted)
416
- return this.jsonResponse({ error: 'Not found' }, 404);
417
- return this.jsonResponse({ success: true });
418
- }
371
+ }
372
+ else if (subResource === 'transitions' || subResource === 'allowed-transitions') {
373
+ if (method === 'GET') {
374
+ const allowedNextStatuses = await engine.getAllowedTaskTransitions(taskId);
375
+ const allowedPreviousStatuses = await engine.getAllowedPreviousTaskTransitions(taskId);
376
+ return this.jsonResponse({ allowedNextStatuses, allowedPreviousStatuses });
419
377
  }
420
378
  }
421
- // Task Containers API
422
- if (segments[0] === 'containers') {
423
- const containerId = segments[1];
424
- if (!containerId) {
425
- if (method === 'GET') {
426
- const projectId = url.searchParams.get('projectId');
427
- if (!projectId)
428
- return this.jsonResponse({ error: 'projectId parameter required' }, 400);
429
- const containers = await engine.getContainers(projectId);
430
- return this.jsonResponse({ containers });
431
- }
432
- if (method === 'POST') {
433
- const body = await this.readBody(request, schemas.CreateContainerSchema);
434
- const container = await engine.createContainer(body);
435
- return this.jsonResponse({ container }, 201);
436
- }
379
+ else if (subResource === 'ladder') {
380
+ if (method === 'GET') {
381
+ const taskLadder = await engine.getTaskLadder(taskId);
382
+ if (!taskLadder)
383
+ return this.jsonResponse({ error: 'Task not found' }, 404);
384
+ return this.jsonResponse({ taskLadder });
437
385
  }
438
- else {
439
- if (method === 'GET') {
440
- const container = await engine.getContainer(containerId);
441
- if (!container)
442
- return this.jsonResponse({ error: 'Container not found' }, 404);
443
- return this.jsonResponse({ container });
444
- }
445
- if (method === 'PATCH' || method === 'PUT') {
446
- const body = await this.readBody(request, schemas.UpdateContainerSchema);
447
- const updated = await engine.updateContainer(containerId, body);
448
- if (!updated)
449
- return this.jsonResponse({ error: 'Container not found' }, 404);
450
- return this.jsonResponse({ container: updated });
451
- }
452
- if (method === 'DELETE') {
453
- const deleted = await engine.deleteContainer(containerId);
454
- if (!deleted)
455
- return this.jsonResponse({ error: 'Not found' }, 404);
456
- return this.jsonResponse({ success: true });
457
- }
386
+ }
387
+ else if (subResource === 'metrics') {
388
+ if (method === 'GET') {
389
+ const metrics = await engine.getTaskMetrics(taskId);
390
+ if (!metrics)
391
+ return this.jsonResponse({ error: 'Task not found' }, 404);
392
+ return this.jsonResponse({ metrics });
458
393
  }
459
394
  }
460
- // Deliverables API
461
- if (segments[0] === 'deliverables') {
462
- const deliverableId = segments[1];
463
- if (!deliverableId) {
464
- if (method === 'GET') {
465
- const projectId = url.searchParams.get('projectId');
466
- if (!projectId)
467
- return this.jsonResponse({ error: 'projectId parameter required' }, 400);
468
- const deliverables = await engine.getDeliverables(projectId);
469
- return this.jsonResponse({ deliverables });
470
- }
471
- if (method === 'POST') {
472
- const body = await this.readBody(request, schemas.CreateDeliverableSchema);
473
- const deliverable = await engine.createDeliverable(body);
474
- return this.jsonResponse({ deliverable }, 201);
475
- }
395
+ else if (subResource === 'progress-history') {
396
+ if (method === 'GET') {
397
+ const progressHistory = await engine.getTaskProgressHistory(taskId);
398
+ if (!progressHistory)
399
+ return this.jsonResponse({ error: 'Task not found' }, 404);
400
+ return this.jsonResponse({ progressHistory });
476
401
  }
477
- else if (segments[2] === 'summary') {
478
- if (method === 'GET') {
479
- const summary = await engine.getDeliverableSummary(deliverableId);
480
- if (!summary)
481
- return this.jsonResponse({ error: 'Deliverable not found' }, 404);
482
- return this.jsonResponse({ summary });
483
- }
402
+ }
403
+ else {
404
+ if (method === 'GET') {
405
+ const task = await engine.getTask(taskId);
406
+ if (!task)
407
+ return this.jsonResponse({ error: 'Task not found' }, 404);
408
+ return this.jsonResponse({ task });
409
+ }
410
+ if (method === 'PATCH' || method === 'PUT') {
411
+ const body = await this.readBody(request, schemas.UpdateTaskSchema);
412
+ const updated = await engine.updateTask(taskId, body);
413
+ if (!updated)
414
+ return this.jsonResponse({ error: 'Task not found' }, 404);
415
+ return this.jsonResponse({ task: updated });
416
+ }
417
+ if (method === 'DELETE') {
418
+ const deleted = await engine.deleteTask(taskId);
419
+ if (!deleted)
420
+ return this.jsonResponse({ error: 'Not found' }, 404);
421
+ return this.jsonResponse({ success: true });
484
422
  }
485
- else {
486
- if (method === 'GET') {
487
- const deliverable = await engine.getDeliverable(deliverableId);
488
- if (!deliverable)
489
- return this.jsonResponse({ error: 'Deliverable not found' }, 404);
490
- return this.jsonResponse({ deliverable });
491
- }
492
- if (method === 'PATCH' || method === 'PUT') {
493
- const body = await this.readBody(request, schemas.UpdateDeliverableSchema);
494
- const updated = await engine.updateDeliverable(deliverableId, body);
495
- if (!updated)
496
- return this.jsonResponse({ error: 'Deliverable not found' }, 404);
497
- return this.jsonResponse({ deliverable: updated });
498
- }
499
- if (method === 'DELETE') {
500
- const deleted = await engine.deleteDeliverable(deliverableId);
501
- if (!deleted)
502
- return this.jsonResponse({ error: 'Not found' }, 404);
503
- return this.jsonResponse({ success: true });
504
- }
423
+ }
424
+ }
425
+ // Teams API
426
+ if (segments[0] === 'teams') {
427
+ const teamId = segments[1];
428
+ if (!teamId) {
429
+ if (method === 'GET') {
430
+ const teams = await engine.getTeams();
431
+ return this.jsonResponse({ teams });
432
+ }
433
+ if (method === 'POST') {
434
+ const body = await this.readBody(request, schemas.CreateTeamSchema);
435
+ const team = await engine.createTeam(body);
436
+ return this.jsonResponse({ team }, 201);
505
437
  }
506
438
  }
507
- // Iterations API
508
- if (segments[0] === 'iterations') {
509
- const iterationId = segments[1];
510
- if (!iterationId) {
511
- if (method === 'GET') {
512
- const projectId = url.searchParams.get('projectId');
513
- if (!projectId)
514
- return this.jsonResponse({ error: 'projectId parameter required' }, 400);
515
- const iterations = await engine.getIterations(projectId);
516
- return this.jsonResponse({ iterations });
517
- }
518
- if (method === 'POST') {
519
- const body = await this.readBody(request, schemas.CreateIterationSchema);
520
- const iteration = await engine.createIteration(body);
521
- return this.jsonResponse({ iteration }, 201);
522
- }
439
+ else {
440
+ if (method === 'GET') {
441
+ const team = await engine.getTeam(teamId);
442
+ if (!team)
443
+ return this.jsonResponse({ error: 'Team not found' }, 404);
444
+ return this.jsonResponse({ team });
445
+ }
446
+ if (method === 'PATCH' || method === 'PUT') {
447
+ const body = await this.readBody(request, schemas.UpdateTeamSchema);
448
+ const updated = await engine.updateTeam(teamId, body);
449
+ if (!updated)
450
+ return this.jsonResponse({ error: 'Team not found' }, 404);
451
+ return this.jsonResponse({ team: updated });
452
+ }
453
+ if (method === 'DELETE') {
454
+ const deleted = await engine.deleteTeam(teamId);
455
+ if (!deleted)
456
+ return this.jsonResponse({ error: 'Not found' }, 404);
457
+ return this.jsonResponse({ success: true });
523
458
  }
524
- else {
525
- if (method === 'GET') {
526
- const iteration = await engine.getIteration(iterationId);
527
- if (!iteration)
528
- return this.jsonResponse({ error: 'Iteration not found' }, 404);
529
- return this.jsonResponse({ iteration });
530
- }
531
- if (method === 'PATCH' || method === 'PUT') {
532
- const body = await this.readBody(request, schemas.UpdateIterationSchema);
533
- const updated = await engine.updateIteration(iterationId, body);
534
- if (!updated)
535
- return this.jsonResponse({ error: 'Iteration not found' }, 404);
536
- return this.jsonResponse({ iteration: updated });
537
- }
538
- if (method === 'DELETE') {
539
- const deleted = await engine.deleteIteration(iterationId);
540
- if (!deleted)
541
- return this.jsonResponse({ error: 'Not found' }, 404);
542
- return this.jsonResponse({ success: true });
543
- }
459
+ }
460
+ }
461
+ // Task Containers API
462
+ if (segments[0] === 'containers') {
463
+ const containerId = segments[1];
464
+ if (!containerId) {
465
+ if (method === 'GET') {
466
+ const projectId = url.searchParams.get('projectId');
467
+ if (!projectId)
468
+ return this.jsonResponse({ error: 'projectId parameter required' }, 400);
469
+ const containers = await engine.getContainers(projectId);
470
+ return this.jsonResponse({ containers });
471
+ }
472
+ if (method === 'POST') {
473
+ const body = await this.readBody(request, schemas.CreateContainerSchema);
474
+ const container = await engine.createContainer(body);
475
+ return this.jsonResponse({ container }, 201);
544
476
  }
545
477
  }
546
- // Activities API
547
- if (segments[0] === 'activities') {
478
+ else {
548
479
  if (method === 'GET') {
549
- const projectId = url.searchParams.get('projectId') || undefined;
550
- const taskId = url.searchParams.get('taskId') || undefined;
551
- const activities = await engine.getActivities({ projectId, taskId });
552
- return this.jsonResponse({ activities });
553
- }
554
- }
555
- // Comments API
556
- if (segments[0] === 'comments') {
557
- const commentId = segments[1];
558
- const subResource = segments[2];
559
- if (commentId && subResource === 'reactions') {
560
- if (method === 'POST') {
561
- const body = await this.readBody(request, schemas.CommentReactionSchema);
562
- const comment = await engine.addCommentReaction(commentId, body);
563
- if (!comment)
564
- return this.jsonResponse({ error: 'Comment not found' }, 404);
565
- return this.jsonResponse({ comment }, 200);
566
- }
567
- if (method === 'DELETE') {
568
- const emoji = url.searchParams.get('emoji');
569
- if (!emoji) {
570
- return this.jsonResponse({ error: 'emoji query parameter is required' }, 400);
571
- }
572
- const comment = await engine.removeCommentReaction(commentId, { emoji });
573
- if (!comment)
574
- return this.jsonResponse({ error: 'Comment not found' }, 404);
575
- return this.jsonResponse({ comment }, 200);
576
- }
480
+ const container = await engine.getContainer(containerId);
481
+ if (!container)
482
+ return this.jsonResponse({ error: 'Container not found' }, 404);
483
+ return this.jsonResponse({ container });
484
+ }
485
+ if (method === 'PATCH' || method === 'PUT') {
486
+ const body = await this.readBody(request, schemas.UpdateContainerSchema);
487
+ const updated = await engine.updateContainer(containerId, body);
488
+ if (!updated)
489
+ return this.jsonResponse({ error: 'Container not found' }, 404);
490
+ return this.jsonResponse({ container: updated });
491
+ }
492
+ if (method === 'DELETE') {
493
+ const deleted = await engine.deleteContainer(containerId);
494
+ if (!deleted)
495
+ return this.jsonResponse({ error: 'Not found' }, 404);
496
+ return this.jsonResponse({ success: true });
577
497
  }
578
- else if (!commentId) {
579
- if (method === 'GET') {
580
- const taskId = url.searchParams.get('taskId');
581
- if (!taskId)
582
- return this.jsonResponse({ error: 'taskId parameter required' }, 400);
583
- const comments = await engine.getComments(taskId);
584
- return this.jsonResponse({ comments });
585
- }
586
- if (method === 'POST') {
587
- const body = await this.readBody(request, schemas.CreateCommentSchema);
588
- const comment = await engine.addComment(body);
589
- return this.jsonResponse({ comment }, 201);
590
- }
498
+ }
499
+ }
500
+ // Deliverables API
501
+ if (segments[0] === 'deliverables') {
502
+ const deliverableId = segments[1];
503
+ if (!deliverableId) {
504
+ if (method === 'GET') {
505
+ const projectId = url.searchParams.get('projectId');
506
+ if (!projectId)
507
+ return this.jsonResponse({ error: 'projectId parameter required' }, 400);
508
+ const deliverables = await engine.getDeliverables(projectId);
509
+ return this.jsonResponse({ deliverables });
591
510
  }
592
- else {
593
- if (method === 'GET') {
594
- const comment = await engine.getComment(commentId);
595
- if (!comment)
596
- return this.jsonResponse({ error: 'Comment not found' }, 404);
597
- return this.jsonResponse({ comment });
598
- }
599
- if (method === 'PATCH' || method === 'PUT') {
600
- const body = await this.readBody(request, schemas.UpdateCommentSchema);
601
- const updated = await engine.updateComment(commentId, body);
602
- if (!updated)
603
- return this.jsonResponse({ error: 'Comment not found' }, 404);
604
- return this.jsonResponse({ comment: updated });
605
- }
606
- if (method === 'DELETE') {
607
- const deleted = await engine.deleteComment(commentId);
608
- if (!deleted)
609
- return this.jsonResponse({ error: 'Not found' }, 404);
610
- return this.jsonResponse({ success: true });
611
- }
511
+ if (method === 'POST') {
512
+ const body = await this.readBody(request, schemas.CreateDeliverableSchema);
513
+ const deliverable = await engine.createDeliverable(body);
514
+ return this.jsonResponse({ deliverable }, 201);
612
515
  }
613
516
  }
614
- // Attachments API
615
- if (segments[0] === 'attachments') {
616
- const attachmentId = segments[1];
617
- if (attachmentId === 'presign') {
618
- if (method === 'POST') {
619
- const body = await this.readBody(request, schemas.PresignAttachmentSchema);
620
- const presigned = await engine.getPresignedAttachmentUploadUrl(body);
621
- return this.jsonResponse({ presigned });
622
- }
517
+ else if (segments[2] === 'summary') {
518
+ if (method === 'GET') {
519
+ const summary = await engine.getDeliverableSummary(deliverableId);
520
+ if (!summary)
521
+ return this.jsonResponse({ error: 'Deliverable not found' }, 404);
522
+ return this.jsonResponse({ summary });
623
523
  }
624
- else if (attachmentId === 'upload') {
625
- if (method === 'POST') {
626
- const body = await this.readBody(request, schemas.UploadAttachmentSchema);
627
- const attachment = await engine.uploadAttachmentFile(body);
628
- return this.jsonResponse({ attachment }, 201);
629
- }
524
+ }
525
+ else {
526
+ if (method === 'GET') {
527
+ const deliverable = await engine.getDeliverable(deliverableId);
528
+ if (!deliverable)
529
+ return this.jsonResponse({ error: 'Deliverable not found' }, 404);
530
+ return this.jsonResponse({ deliverable });
531
+ }
532
+ if (method === 'PATCH' || method === 'PUT') {
533
+ const body = await this.readBody(request, schemas.UpdateDeliverableSchema);
534
+ const updated = await engine.updateDeliverable(deliverableId, body);
535
+ if (!updated)
536
+ return this.jsonResponse({ error: 'Deliverable not found' }, 404);
537
+ return this.jsonResponse({ deliverable: updated });
538
+ }
539
+ if (method === 'DELETE') {
540
+ const deleted = await engine.deleteDeliverable(deliverableId);
541
+ if (!deleted)
542
+ return this.jsonResponse({ error: 'Not found' }, 404);
543
+ return this.jsonResponse({ success: true });
630
544
  }
631
- else if (!attachmentId) {
632
- if (method === 'GET') {
633
- const taskId = url.searchParams.get('taskId') || undefined;
634
- const projectId = url.searchParams.get('projectId') || undefined;
635
- const commentId = url.searchParams.get('commentId') || undefined;
636
- const attachments = await engine.getAttachments({ taskId, projectId, commentId });
637
- return this.jsonResponse({ attachments });
638
- }
639
- if (method === 'POST') {
640
- const body = await this.readBody(request, schemas.CreateAttachmentSchema);
641
- const attachment = await engine.createAttachment(body);
642
- return this.jsonResponse({ attachment }, 201);
643
- }
545
+ }
546
+ }
547
+ // Iterations API
548
+ if (segments[0] === 'iterations') {
549
+ const iterationId = segments[1];
550
+ if (!iterationId) {
551
+ if (method === 'GET') {
552
+ const projectId = url.searchParams.get('projectId');
553
+ if (!projectId)
554
+ return this.jsonResponse({ error: 'projectId parameter required' }, 400);
555
+ const iterations = await engine.getIterations(projectId);
556
+ return this.jsonResponse({ iterations });
644
557
  }
645
- else {
646
- if (method === 'GET') {
647
- const attachment = await engine.getAttachment(attachmentId);
648
- if (!attachment)
649
- return this.jsonResponse({ error: 'Attachment not found' }, 404);
650
- return this.jsonResponse({ attachment });
651
- }
652
- if (method === 'DELETE') {
653
- const deleted = await engine.deleteAttachment(attachmentId);
654
- if (!deleted)
655
- return this.jsonResponse({ error: 'Not found' }, 404);
656
- return this.jsonResponse({ success: true });
657
- }
558
+ if (method === 'POST') {
559
+ const body = await this.readBody(request, schemas.CreateIterationSchema);
560
+ const iteration = await engine.createIteration(body);
561
+ return this.jsonResponse({ iteration }, 201);
658
562
  }
659
563
  }
660
- // Time Tracking API
661
- if (segments[0] === 'time-entries') {
564
+ else {
565
+ if (method === 'GET') {
566
+ const iteration = await engine.getIteration(iterationId);
567
+ if (!iteration)
568
+ return this.jsonResponse({ error: 'Iteration not found' }, 404);
569
+ return this.jsonResponse({ iteration });
570
+ }
571
+ if (method === 'PATCH' || method === 'PUT') {
572
+ const body = await this.readBody(request, schemas.UpdateIterationSchema);
573
+ const updated = await engine.updateIteration(iterationId, body);
574
+ if (!updated)
575
+ return this.jsonResponse({ error: 'Iteration not found' }, 404);
576
+ return this.jsonResponse({ iteration: updated });
577
+ }
578
+ if (method === 'DELETE') {
579
+ const deleted = await engine.deleteIteration(iterationId);
580
+ if (!deleted)
581
+ return this.jsonResponse({ error: 'Not found' }, 404);
582
+ return this.jsonResponse({ success: true });
583
+ }
584
+ }
585
+ }
586
+ // Activities API
587
+ if (segments[0] === 'activities') {
588
+ if (method === 'GET') {
589
+ const projectId = url.searchParams.get('projectId') || undefined;
590
+ const taskId = url.searchParams.get('taskId') || undefined;
591
+ const activities = await engine.getActivities({ projectId, taskId });
592
+ return this.jsonResponse({ activities });
593
+ }
594
+ }
595
+ // Comments API
596
+ if (segments[0] === 'comments') {
597
+ const commentId = segments[1];
598
+ const subResource = segments[2];
599
+ if (commentId && subResource === 'reactions') {
600
+ if (method === 'POST') {
601
+ const body = await this.readBody(request, schemas.CommentReactionSchema);
602
+ const comment = await engine.addCommentReaction(commentId, body);
603
+ if (!comment)
604
+ return this.jsonResponse({ error: 'Comment not found' }, 404);
605
+ return this.jsonResponse({ comment }, 200);
606
+ }
607
+ if (method === 'DELETE') {
608
+ const emoji = url.searchParams.get('emoji');
609
+ if (!emoji) {
610
+ return this.jsonResponse({ error: 'emoji query parameter is required' }, 400);
611
+ }
612
+ const comment = await engine.removeCommentReaction(commentId, { emoji });
613
+ if (!comment)
614
+ return this.jsonResponse({ error: 'Comment not found' }, 404);
615
+ return this.jsonResponse({ comment }, 200);
616
+ }
617
+ }
618
+ else if (!commentId) {
662
619
  if (method === 'GET') {
663
620
  const taskId = url.searchParams.get('taskId');
664
621
  if (!taskId)
665
622
  return this.jsonResponse({ error: 'taskId parameter required' }, 400);
666
- const entries = await engine.getTimeEntries(taskId);
667
- return this.jsonResponse({ timeEntries: entries });
623
+ const comments = await engine.getComments(taskId);
624
+ return this.jsonResponse({ comments });
668
625
  }
669
626
  if (method === 'POST') {
670
- const body = await this.readBody(request, schemas.LogTimeSchema);
671
- const entry = await engine.logTime(body);
672
- return this.jsonResponse({ timeEntry: entry }, 201);
627
+ const body = await this.readBody(request, schemas.CreateCommentSchema);
628
+ const comment = await engine.addComment(body);
629
+ return this.jsonResponse({ comment }, 201);
673
630
  }
674
631
  }
675
- // Workload & Capacity API
676
- if (segments[0] === 'workload') {
632
+ else {
677
633
  if (method === 'GET') {
678
- const projectId = url.searchParams.get('projectId') || undefined;
679
- const startDate = url.searchParams.get('startDate') || undefined;
680
- const endDate = url.searchParams.get('endDate') || undefined;
681
- const interval = (url.searchParams.get('interval') || undefined);
682
- const groupBy = (url.searchParams.get('groupBy') || undefined);
683
- const metric = (url.searchParams.get('metric') || undefined);
684
- const defaultWeeklyCapacityHours = url.searchParams.get('defaultWeeklyCapacityHours')
685
- ? parseFloat(url.searchParams.get('defaultWeeklyCapacityHours'))
686
- : undefined;
687
- const workload = await engine.getWorkloadDistribution(projectId, {
688
- startDate,
689
- endDate,
690
- interval,
691
- groupBy,
692
- metric,
693
- defaultWeeklyCapacityHours
694
- });
695
- return this.jsonResponse({ workload });
634
+ const comment = await engine.getComment(commentId);
635
+ if (!comment)
636
+ return this.jsonResponse({ error: 'Comment not found' }, 404);
637
+ return this.jsonResponse({ comment });
638
+ }
639
+ if (method === 'PATCH' || method === 'PUT') {
640
+ const body = await this.readBody(request, schemas.UpdateCommentSchema);
641
+ const updated = await engine.updateComment(commentId, body);
642
+ if (!updated)
643
+ return this.jsonResponse({ error: 'Comment not found' }, 404);
644
+ return this.jsonResponse({ comment: updated });
645
+ }
646
+ if (method === 'DELETE') {
647
+ const deleted = await engine.deleteComment(commentId);
648
+ if (!deleted)
649
+ return this.jsonResponse({ error: 'Not found' }, 404);
650
+ return this.jsonResponse({ success: true });
696
651
  }
697
652
  }
698
- // Agent Status / Telemetry API
699
- if (segments[0] === 'status') {
653
+ }
654
+ // Attachments API
655
+ if (segments[0] === 'attachments') {
656
+ const attachmentId = segments[1];
657
+ if (attachmentId === 'presign') {
700
658
  if (method === 'POST') {
701
- const body = await this.readJson(request);
702
- const status = typeof body.status === 'string' ? body.status : 'active';
703
- if (engine.events) {
704
- engine.events.publish({
705
- type: 'agent.status_updated',
706
- aggregateId: body.taskId || body.projectId || 'system',
707
- payload: {
708
- status,
709
- taskId: body.taskId,
710
- projectId: body.projectId,
711
- details: body.details,
712
- isEngaged: body.isEngaged ?? true,
713
- timestamp: Date.now()
714
- }
715
- });
716
- }
717
- return this.jsonResponse({
718
- success: true,
719
- status,
720
- timestamp: Date.now()
721
- });
659
+ const body = await this.readBody(request, schemas.PresignAttachmentSchema);
660
+ const presigned = await engine.getPresignedAttachmentUploadUrl(body);
661
+ return this.jsonResponse({ presigned });
662
+ }
663
+ }
664
+ else if (attachmentId === 'upload') {
665
+ if (method === 'POST') {
666
+ const body = await this.readBody(request, schemas.UploadAttachmentSchema);
667
+ const attachment = await engine.uploadAttachmentFile(body);
668
+ return this.jsonResponse({ attachment }, 201);
669
+ }
670
+ }
671
+ else if (!attachmentId) {
672
+ if (method === 'GET') {
673
+ const taskId = url.searchParams.get('taskId') || undefined;
674
+ const projectId = url.searchParams.get('projectId') || undefined;
675
+ const commentId = url.searchParams.get('commentId') || undefined;
676
+ const attachments = await engine.getAttachments({ taskId, projectId, commentId });
677
+ return this.jsonResponse({ attachments });
678
+ }
679
+ if (method === 'POST') {
680
+ const body = await this.readBody(request, schemas.CreateAttachmentSchema);
681
+ const attachment = await engine.createAttachment(body);
682
+ return this.jsonResponse({ attachment }, 201);
683
+ }
684
+ }
685
+ else {
686
+ if (method === 'GET') {
687
+ const attachment = await engine.getAttachment(attachmentId);
688
+ if (!attachment)
689
+ return this.jsonResponse({ error: 'Attachment not found' }, 404);
690
+ return this.jsonResponse({ attachment });
691
+ }
692
+ if (method === 'DELETE') {
693
+ const deleted = await engine.deleteAttachment(attachmentId);
694
+ if (!deleted)
695
+ return this.jsonResponse({ error: 'Not found' }, 404);
696
+ return this.jsonResponse({ success: true });
722
697
  }
723
698
  }
724
- return this.jsonResponse({ error: `Route not found: ${method} ${pathname}` }, 404);
725
699
  }
726
- catch (err) {
727
- return this.errorResponse(err, request);
700
+ // Time Tracking API
701
+ if (segments[0] === 'time-entries') {
702
+ if (method === 'GET') {
703
+ const taskId = url.searchParams.get('taskId');
704
+ if (!taskId)
705
+ return this.jsonResponse({ error: 'taskId parameter required' }, 400);
706
+ const entries = await engine.getTimeEntries(taskId);
707
+ return this.jsonResponse({ timeEntries: entries });
708
+ }
709
+ if (method === 'POST') {
710
+ const body = await this.readBody(request, schemas.LogTimeSchema);
711
+ const entry = await engine.logTime(body);
712
+ return this.jsonResponse({ timeEntry: entry }, 201);
713
+ }
714
+ }
715
+ // Workload & Capacity API
716
+ if (segments[0] === 'workload') {
717
+ if (method === 'GET') {
718
+ const projectId = url.searchParams.get('projectId') || undefined;
719
+ const startDate = url.searchParams.get('startDate') || undefined;
720
+ const endDate = url.searchParams.get('endDate') || undefined;
721
+ const interval = (url.searchParams.get('interval') || undefined);
722
+ const groupBy = (url.searchParams.get('groupBy') || undefined);
723
+ const metric = (url.searchParams.get('metric') || undefined);
724
+ const defaultWeeklyCapacityHours = url.searchParams.get('defaultWeeklyCapacityHours')
725
+ ? parseFloat(url.searchParams.get('defaultWeeklyCapacityHours'))
726
+ : undefined;
727
+ const workload = await engine.getWorkloadDistribution(projectId, {
728
+ startDate,
729
+ endDate,
730
+ interval,
731
+ groupBy,
732
+ metric,
733
+ defaultWeeklyCapacityHours
734
+ });
735
+ return this.jsonResponse({ workload });
736
+ }
737
+ }
738
+ // Agent Status / Telemetry API
739
+ if (segments[0] === 'status') {
740
+ if (method === 'POST') {
741
+ const body = await this.readJson(request);
742
+ const status = typeof body.status === 'string' ? body.status : 'active';
743
+ if (engine.events) {
744
+ engine.events.publish({
745
+ type: 'agent.status_updated',
746
+ aggregateId: body.taskId || body.projectId || 'system',
747
+ payload: {
748
+ status,
749
+ taskId: body.taskId,
750
+ projectId: body.projectId,
751
+ details: body.details,
752
+ isEngaged: body.isEngaged ?? true,
753
+ timestamp: Date.now()
754
+ }
755
+ });
756
+ }
757
+ return this.jsonResponse({
758
+ success: true,
759
+ status,
760
+ timestamp: Date.now()
761
+ });
762
+ }
728
763
  }
764
+ return this.jsonResponse({ error: `Route not found: ${method} ${pathname}` }, 404);
729
765
  }
730
766
  /**
731
767
  * Reads and validates a JSON body. `overrides` (e.g. a `taskId` from the URL) are applied before