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:
@@ -4,35 +4,53 @@ export type Listener = (frame: Frame) => void;
|
||||
export interface Broker {
|
||||
publish(sessionId: string, frame: Frame): void;
|
||||
subscribe(sessionId: string, listener: Listener): () => void;
|
||||
publishUser(user: string, frame: Frame): void;
|
||||
subscribeUser(user: string, listener: Listener): () => void;
|
||||
}
|
||||
|
||||
export function createBroker(): Broker {
|
||||
const topics = new Map<string, Set<Listener>>();
|
||||
const userTopics = new Map<string, Set<Listener>>();
|
||||
|
||||
function publishTo(map: Map<string, Set<Listener>>, key: string, frame: Frame): void {
|
||||
const set = map.get(key);
|
||||
if (!set) return;
|
||||
for (const listener of set) {
|
||||
try {
|
||||
listener(frame);
|
||||
} catch {
|
||||
// ignore listener errors so one bad subscriber doesn't break the rest
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function subscribeTo(map: Map<string, Set<Listener>>, key: string, listener: Listener): () => void {
|
||||
let set = map.get(key);
|
||||
if (!set) {
|
||||
set = new Set();
|
||||
map.set(key, set);
|
||||
}
|
||||
set.add(listener);
|
||||
return () => {
|
||||
const s = map.get(key);
|
||||
if (!s) return;
|
||||
s.delete(listener);
|
||||
if (s.size === 0) map.delete(key);
|
||||
};
|
||||
}
|
||||
|
||||
return {
|
||||
publish(sessionId, frame) {
|
||||
const set = topics.get(sessionId);
|
||||
if (!set) return;
|
||||
for (const listener of set) {
|
||||
try {
|
||||
listener(frame);
|
||||
} catch {
|
||||
// ignore listener errors so one bad subscriber doesn't break the rest
|
||||
}
|
||||
}
|
||||
publishTo(topics, sessionId, frame);
|
||||
},
|
||||
subscribe(sessionId, listener) {
|
||||
let set = topics.get(sessionId);
|
||||
if (!set) {
|
||||
set = new Set();
|
||||
topics.set(sessionId, set);
|
||||
}
|
||||
set.add(listener);
|
||||
return () => {
|
||||
const s = topics.get(sessionId);
|
||||
if (!s) return;
|
||||
s.delete(listener);
|
||||
if (s.size === 0) topics.delete(sessionId);
|
||||
};
|
||||
return subscribeTo(topics, sessionId, listener);
|
||||
},
|
||||
publishUser(user, frame) {
|
||||
publishTo(userTopics, user, frame);
|
||||
},
|
||||
subscribeUser(user, listener) {
|
||||
return subscribeTo(userTopics, user, listener);
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
import type { FastifyBaseLogger } from 'fastify';
|
||||
import type { Sql } from '../db.js';
|
||||
import type { Config } from '../config.js';
|
||||
import type { Message, Project, Session, ToolCall } from '../types/api.js';
|
||||
import type { Message, Project, Session, ToolCall, UserStreamFrame } from '../types/api.js';
|
||||
import { ALL_TOOLS, TOOLS_BY_NAME, toolJsonSchemas } from './tools.js';
|
||||
import { PathScopeError, resolveProjectRoot } from './path_guard.js';
|
||||
import { maybeAutoNameSession } from './auto_name.js';
|
||||
@@ -86,6 +86,7 @@ export interface InferenceContext {
|
||||
config: Config;
|
||||
log: FastifyBaseLogger;
|
||||
publish: FramePublisher;
|
||||
publishUser: (frame: UserStreamFrame) => void;
|
||||
}
|
||||
|
||||
export function buildMessagesPayload(
|
||||
@@ -426,7 +427,12 @@ async function runAssistantTurn(
|
||||
finished_at = clock_timestamp()
|
||||
WHERE id = ${assistantMessageId}
|
||||
`;
|
||||
await ctx.sql`UPDATE sessions SET updated_at = clock_timestamp() WHERE id = ${sessionId}`;
|
||||
const [failSessRow] = await ctx.sql<{ project_id: string; name: string; updated_at: string }[]>`
|
||||
UPDATE sessions SET updated_at = clock_timestamp()
|
||||
WHERE id = ${sessionId}
|
||||
RETURNING project_id, name, updated_at
|
||||
`;
|
||||
ctx.publishUser({ type: 'session_updated', session_id: sessionId, project_id: failSessRow!.project_id, name: failSessRow!.name, updated_at: failSessRow!.updated_at });
|
||||
ctx.publish(sessionId, {
|
||||
type: 'error',
|
||||
message_id: assistantMessageId,
|
||||
@@ -459,7 +465,12 @@ async function runAssistantTurn(
|
||||
WHERE id = ${assistantMessageId}
|
||||
RETURNING tokens_used, ctx_used, ctx_max, finished_at
|
||||
`;
|
||||
await ctx.sql`UPDATE sessions SET updated_at = clock_timestamp() WHERE id = ${sessionId}`;
|
||||
const [toolSessRow] = await ctx.sql<{ project_id: string; name: string; updated_at: string }[]>`
|
||||
UPDATE sessions SET updated_at = clock_timestamp()
|
||||
WHERE id = ${sessionId}
|
||||
RETURNING project_id, name, updated_at
|
||||
`;
|
||||
ctx.publishUser({ type: 'session_updated', session_id: sessionId, project_id: toolSessRow!.project_id, name: toolSessRow!.name, updated_at: toolSessRow!.updated_at });
|
||||
for (const tc of toolCalls) {
|
||||
ctx.publish(sessionId, {
|
||||
type: 'tool_call',
|
||||
@@ -531,7 +542,12 @@ async function runAssistantTurn(
|
||||
WHERE id = ${assistantMessageId}
|
||||
RETURNING tokens_used, ctx_used, ctx_max, finished_at
|
||||
`;
|
||||
await ctx.sql`UPDATE sessions SET updated_at = clock_timestamp() WHERE id = ${sessionId}`;
|
||||
const [completeSessRow] = await ctx.sql<{ project_id: string; name: string; updated_at: string }[]>`
|
||||
UPDATE sessions SET updated_at = clock_timestamp()
|
||||
WHERE id = ${sessionId}
|
||||
RETURNING project_id, name, updated_at
|
||||
`;
|
||||
ctx.publishUser({ type: 'session_updated', session_id: sessionId, project_id: completeSessRow!.project_id, name: completeSessRow!.name, updated_at: completeSessRow!.updated_at });
|
||||
ctx.publish(sessionId, {
|
||||
type: 'message_complete',
|
||||
message_id: assistantMessageId,
|
||||
@@ -563,19 +579,26 @@ export async function runInference(
|
||||
return runAssistantTurn(ctx, sessionId, assistantMessageId, 0);
|
||||
}
|
||||
|
||||
export function createInferenceRunner(ctx: InferenceContext) {
|
||||
export function createInferenceRunner(
|
||||
ctx: Omit<InferenceContext, 'publishUser'>,
|
||||
publishUserFn: (user: string, frame: UserStreamFrame) => void
|
||||
) {
|
||||
return {
|
||||
enqueue(sessionId: string, assistantMessageId: string) {
|
||||
enqueue(sessionId: string, assistantMessageId: string, user: string) {
|
||||
const callCtx: InferenceContext = {
|
||||
...ctx,
|
||||
publishUser: (frame) => publishUserFn(user, frame),
|
||||
};
|
||||
void (async () => {
|
||||
try {
|
||||
await runInference(ctx, sessionId, assistantMessageId);
|
||||
await runInference(callCtx, sessionId, assistantMessageId);
|
||||
setImmediate(() => {
|
||||
void maybeAutoNameSession(ctx, sessionId).catch((err) => {
|
||||
ctx.log.warn({ err, sessionId }, 'auto-name failed');
|
||||
void maybeAutoNameSession(callCtx, sessionId).catch((err) => {
|
||||
callCtx.log.warn({ err, sessionId }, 'auto-name failed');
|
||||
});
|
||||
});
|
||||
} catch (err) {
|
||||
ctx.log.error({ err }, 'unhandled inference error');
|
||||
callCtx.log.error({ err }, 'unhandled inference error');
|
||||
}
|
||||
})();
|
||||
},
|
||||
|
||||
Reference in New Issue
Block a user