batch3 T3: broker user channel + /api/ws/user + project/session/inference emits

- broker.subscribeUser/publishUser via separate user topics map
- /api/ws/user WS route subscribes to the user channel
- projects/sessions POST/DELETE handlers emit lifecycle frames
- inference 3 terminal-state sites emit session_updated with RETURNING

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
2026-05-15 15:06:31 +00:00
parent d88b3348a2
commit 8fc525eab9
7 changed files with 132 additions and 50 deletions

View File

@@ -2,6 +2,7 @@ import type { FastifyInstance } from 'fastify';
import { z } from 'zod';
import type { Sql } from '../db.js';
import type { Config } from '../config.js';
import type { Broker } from '../services/broker.js';
import type { Session } from '../types/api.js';
import { getSetting } from './settings.js';
@@ -26,7 +27,8 @@ async function resolveDefaultModel(sql: Sql, config: Config): Promise<string> {
export function registerSessionRoutes(
app: FastifyInstance,
sql: Sql,
config: Config
config: Config,
broker: Broker
): void {
app.get<{ Params: { id: string } }>(
'/api/projects/:id/sessions',
@@ -86,6 +88,11 @@ export function registerSessionRoutes(
`;
return session!;
});
broker.publishUser(req.user!, {
type: 'session_created',
session: row,
project_id: row.project_id,
});
reply.code(201);
return row;
}
@@ -133,11 +140,16 @@ export function registerSessionRoutes(
app.delete<{ Params: { id: string } }>(
'/api/sessions/:id',
async (req, reply) => {
const result = await sql`DELETE FROM sessions WHERE id = ${req.params.id}`;
if (result.count === 0) {
const id = req.params.id;
const deleted = await sql<{ project_id: string }[]>`
DELETE FROM sessions WHERE id = ${id} RETURNING project_id
`;
if (deleted.length === 0) {
reply.code(404);
return { error: 'not found' };
}
const project_id = deleted[0]!.project_id;
broker.publishUser(req.user!, { type: 'session_deleted', session_id: id, project_id });
reply.code(204);
return null;
}