feat(api): conversations + messages routes
Add conversations CRUD-lite (list, create, get, PATCH status, PATCH summary which flips status to summarized) and conversation-scoped messages (append, list ordered by created_at). Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
This commit is contained in:
65
lib/api/routes/conversations.js
Normal file
65
lib/api/routes/conversations.js
Normal file
@@ -0,0 +1,65 @@
|
||||
import { Router } from 'express';
|
||||
import { z } from 'zod';
|
||||
import * as repo from '../../db/repos/conversations.js';
|
||||
import { validate } from '../validate.js';
|
||||
import { parsePagination } from '../pagination.js';
|
||||
import { NotFoundError, asyncWrap } from '../errors.js';
|
||||
|
||||
const STATUSES = ['open', 'summarized', 'archived'];
|
||||
|
||||
const createSchema = z.object({
|
||||
title: z.string().nullable().optional(),
|
||||
agent_id: z.string().uuid().nullable().optional(),
|
||||
participants: z.array(z.string()).optional(),
|
||||
metadata: z.record(z.string(), z.any()).optional()
|
||||
});
|
||||
|
||||
const statusSchema = z.object({ status: z.enum(STATUSES) });
|
||||
const summarySchema = z.object({ summary: z.string().min(1) });
|
||||
|
||||
const idParams = z.object({ id: z.string().uuid() });
|
||||
|
||||
export const router = Router();
|
||||
|
||||
router.get('/',
|
||||
validate({ query: z.object({ limit: z.string().optional(), offset: z.string().optional() }) }),
|
||||
asyncWrap(async (req, res) => {
|
||||
const { limit, offset } = parsePagination(req);
|
||||
res.json(await repo.list({ limit, offset }));
|
||||
})
|
||||
);
|
||||
|
||||
router.post('/',
|
||||
validate({ body: createSchema }),
|
||||
asyncWrap(async (req, res) => {
|
||||
const row = await repo.create(req.body, req.actor);
|
||||
res.status(201).json(row);
|
||||
})
|
||||
);
|
||||
|
||||
router.get('/:id',
|
||||
validate({ params: idParams }),
|
||||
asyncWrap(async (req, res) => {
|
||||
const row = await repo.getById(req.params.id);
|
||||
if (!row) throw new NotFoundError('conversation not found');
|
||||
res.json(row);
|
||||
})
|
||||
);
|
||||
|
||||
router.patch('/:id/status',
|
||||
validate({ params: idParams, body: statusSchema }),
|
||||
asyncWrap(async (req, res) => {
|
||||
const existing = await repo.getById(req.params.id);
|
||||
if (!existing) throw new NotFoundError('conversation not found');
|
||||
res.json(await repo.setStatus(req.params.id, req.body.status, req.actor));
|
||||
})
|
||||
);
|
||||
|
||||
router.patch('/:id/summary',
|
||||
validate({ params: idParams, body: summarySchema }),
|
||||
asyncWrap(async (req, res) => {
|
||||
const existing = await repo.getById(req.params.id);
|
||||
if (!existing) throw new NotFoundError('conversation not found');
|
||||
res.json(await repo.setSummary(req.params.id, req.body.summary));
|
||||
})
|
||||
);
|
||||
41
lib/api/routes/messages.js
Normal file
41
lib/api/routes/messages.js
Normal file
@@ -0,0 +1,41 @@
|
||||
import { Router } from 'express';
|
||||
import { z } from 'zod';
|
||||
import * as repo from '../../db/repos/messages.js';
|
||||
import * as conversations from '../../db/repos/conversations.js';
|
||||
import { validate } from '../validate.js';
|
||||
import { NotFoundError, ValidationError, asyncWrap } from '../errors.js';
|
||||
|
||||
const appendSchema = z.object({
|
||||
role: z.string().min(1).max(64),
|
||||
body: z.string().min(1),
|
||||
agent_id: z.string().uuid().nullable().optional(),
|
||||
metadata: z.record(z.string(), z.any()).optional()
|
||||
});
|
||||
|
||||
const convParams = z.object({ conversation_id: z.string().uuid() });
|
||||
|
||||
export const conversationsScopedRouter = Router({ mergeParams: true });
|
||||
|
||||
conversationsScopedRouter.get('/',
|
||||
validate({ params: convParams,
|
||||
query: z.object({ limit: z.string().optional() }) }),
|
||||
asyncWrap(async (req, res) => {
|
||||
const existing = await conversations.getById(req.params.conversation_id);
|
||||
if (!existing) throw new NotFoundError('conversation not found');
|
||||
const limit = req.validatedQuery.limit ? Number(req.validatedQuery.limit) : 1000;
|
||||
if (!Number.isFinite(limit) || limit < 1 || limit > 5000) {
|
||||
throw new ValidationError('limit must be 1..5000');
|
||||
}
|
||||
res.json(await repo.listByConversation(req.params.conversation_id, { limit }));
|
||||
})
|
||||
);
|
||||
|
||||
conversationsScopedRouter.post('/',
|
||||
validate({ params: convParams, body: appendSchema }),
|
||||
asyncWrap(async (req, res) => {
|
||||
const existing = await conversations.getById(req.params.conversation_id);
|
||||
if (!existing) throw new NotFoundError('conversation not found');
|
||||
const row = await repo.append(req.params.conversation_id, req.body);
|
||||
res.status(201).json(row);
|
||||
})
|
||||
);
|
||||
Reference in New Issue
Block a user