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:
@@ -8,7 +8,7 @@ const SendBody = z.object({
|
||||
});
|
||||
|
||||
interface MessageHandlers {
|
||||
enqueueInference: (sessionId: string, assistantMessageId: string) => void;
|
||||
enqueueInference: (sessionId: string, assistantMessageId: string, user: string) => void;
|
||||
publishUserMessage: (
|
||||
sessionId: string,
|
||||
userMessageId: string,
|
||||
@@ -76,7 +76,7 @@ export function registerMessageRoutes(
|
||||
result.user_message_id,
|
||||
parsed.data.content
|
||||
);
|
||||
handlers.enqueueInference(req.params.id, result.assistant_message_id);
|
||||
handlers.enqueueInference(req.params.id, result.assistant_message_id, req.user!);
|
||||
|
||||
reply.code(202);
|
||||
return result;
|
||||
@@ -132,7 +132,7 @@ export function registerMessageRoutes(
|
||||
});
|
||||
|
||||
handlers.publishMessagesDeleted(sessionId, deletedIds);
|
||||
handlers.enqueueInference(sessionId, newAssistantId);
|
||||
handlers.enqueueInference(sessionId, newAssistantId, req.user!);
|
||||
|
||||
reply.code(202);
|
||||
return { assistant_message_id: newAssistantId };
|
||||
|
||||
@@ -4,6 +4,7 @@ import { realpath, stat, readdir, access } from 'node:fs/promises';
|
||||
import { basename, resolve, sep } from 'node:path';
|
||||
import type { Sql } from '../db.js';
|
||||
import type { Config } from '../config.js';
|
||||
import type { Broker } from '../services/broker.js';
|
||||
import type { Project, AvailableProject } from '../types/api.js';
|
||||
|
||||
const AddProjectBody = z.object({
|
||||
@@ -42,7 +43,8 @@ async function resolveProjectPath(
|
||||
export function registerProjectRoutes(
|
||||
app: FastifyInstance,
|
||||
sql: Sql,
|
||||
config: Config
|
||||
config: Config,
|
||||
broker: Broker
|
||||
): void {
|
||||
app.get('/api/projects', async () => {
|
||||
const rows = await sql<Project[]>`
|
||||
@@ -71,6 +73,7 @@ export function registerProjectRoutes(
|
||||
VALUES (${name}, ${resolved.real})
|
||||
RETURNING id, name, path, added_at, last_session_id
|
||||
`;
|
||||
broker.publishUser(req.user!, { type: 'project_created', project: row as unknown as Project });
|
||||
reply.code(201);
|
||||
return row;
|
||||
} catch (err) {
|
||||
@@ -89,6 +92,7 @@ export function registerProjectRoutes(
|
||||
reply.code(404);
|
||||
return { error: 'not found' };
|
||||
}
|
||||
broker.publishUser(req.user!, { type: 'project_deleted', project_id: id });
|
||||
reply.code(204);
|
||||
return null;
|
||||
});
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
@@ -43,4 +43,23 @@ export function registerWebSocket(
|
||||
socket.on('error', () => unsubscribe());
|
||||
}
|
||||
);
|
||||
|
||||
app.get('/api/ws/user', { websocket: true }, async (socket, req) => {
|
||||
const user = req.user;
|
||||
if (!user) {
|
||||
socket.close(1008, 'unauthenticated');
|
||||
return;
|
||||
}
|
||||
// No snapshot — user channel is purely live updates.
|
||||
const unsubscribe = broker.subscribeUser(user, (frame) => {
|
||||
if (socket.readyState !== socket.OPEN) return;
|
||||
try {
|
||||
socket.send(JSON.stringify(frame));
|
||||
} catch (err) {
|
||||
app.log.warn({ err, user }, 'user ws send failed');
|
||||
}
|
||||
});
|
||||
socket.on('close', () => unsubscribe());
|
||||
socket.on('error', () => unsubscribe());
|
||||
});
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user