v1.1 batch 1: markdown, message actions, tok/s+ctx, AI naming

Four features land together on this branch:

1. Markdown rendering — assistant messages go through react-markdown +
   remark-gfm. Fenced code blocks render via existing CodeBlock (with copy
   button); inline `code` is styled inline. User messages stay plain text.
   No raw HTML (no rehype-raw).

2. Per-message Copy + Regenerate. New endpoint
   POST /api/sessions/:id/messages/:message_id/regenerate validates the
   target (404/400/409), atomically deletes the target plus any later
   messages in the session, inserts a fresh streaming assistant row, and
   enqueues a normal inference run. The DELETE bound uses a SQL subquery
   (`created_at >= (SELECT created_at FROM messages WHERE id = $1)`)
   instead of a JS round-trip so postgres TIMESTAMPTZ µs precision is
   preserved — otherwise sub-ms clock_timestamp() differences between the
   user row and the assistant row collapsed to the same JS Date, pulling
   the triggering user message into the >= bound. New `messages_deleted`
   WS frame so already-connected clients prune the stale tail without
   needing a full snapshot resend.

3. tok/s + ctx counter. Five new nullable message columns: tokens_used,
   ctx_used, ctx_max, started_at, finished_at. started_at is set right
   before the OpenAI call in services/inference.ts (not in the route, not
   in the frame handler); finished_at + tokens_used + ctx_used + ctx_max
   are committed in the same UPDATE that flips status to 'complete'. The
   inference request now opts into stream_options.include_usage so the
   final chunk carries usage; defensive parsing also picks up timings.n_ctx
   when llama.cpp emits it (currently absent for our llama-swap models, so
   ctx_max stays NULL and the UI just shows `<used> ctx`). message_complete
   frame extended with tokens_used / ctx_used / ctx_max / started_at /
   finished_at / model. Frontend StatsLine in MessageBubble computes tok/s
   client-side from the timestamps and renders muted mono text below the
   body of completed assistant messages.

4. AI chat naming after the first turn. Backend services/auto_name.ts
   runs via setImmediate after the top-level inference resolves; it
   checks that there is exactly one completed assistant message and that
   the session has not been user-renamed (`name IS NULL OR name = '' OR
   name = 'New session'`), then fires a single non-streaming chat
   completion with the spec prompt. Qwen3 chat templates emit chain-of-
   thought into reasoning_content and burn the entire max_tokens budget
   without producing visible output, so the request includes
   `chat_template_kwargs: { enable_thinking: false }` and max_tokens=30.
   Title is trimmed, quote-stripped, "Title:" prefix dropped, and
   truncated to 60 chars before a guarded UPDATE on sessions.name. New
   `session_renamed` WS frame propagates to the open session view
   directly and to the project's session list via a tiny module-scope
   event bus (apps/web/src/hooks/sessionEvents.ts) — kept dumb: one event
   type, two methods, no library.

Cleanups: dropped the now-unused splitCodeBlocks export from CodeBlock.tsx
(react-markdown supersedes it), and added a long-form NOTE in auto_name.ts
documenting the enable_thinking + max_tokens pattern for any future Qwen-
family non-streaming utility calls (planned: fork-message, agent-routing,
web-search summarization).

Schema bootstrap remains idempotent (ADD COLUMN IF NOT EXISTS). Auth,
broker, clock_timestamp() conventions, and zod validation all unchanged.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
2026-05-14 22:52:40 +00:00
parent a7f218e182
commit 2464d23bb6
18 changed files with 1559 additions and 94 deletions

View File

@@ -50,7 +50,7 @@ async function main() {
}, },
}); });
registerMessageRoutes(app, sql, { registerMessageRoutes(app, sql, {
onSend: (sessionId, _userId, assistantId) => { enqueueInference: (sessionId, assistantId) => {
inference.enqueue(sessionId, assistantId); inference.enqueue(sessionId, assistantId);
}, },
publishUserMessage: (sessionId, userMessageId, content) => { publishUserMessage: (sessionId, userMessageId, content) => {
@@ -69,6 +69,12 @@ async function main() {
message_id: userMessageId, message_id: userMessageId,
}); });
}, },
publishMessagesDeleted: (sessionId, messageIds) => {
broker.publish(sessionId, {
type: 'messages_deleted',
message_ids: messageIds,
});
},
}); });
registerWebSocket(app, sql, broker); registerWebSocket(app, sql, broker);

View File

@@ -8,12 +8,13 @@ const SendBody = z.object({
}); });
interface MessageHandlers { interface MessageHandlers {
onSend: (sessionId: string, userMessageId: string, assistantMessageId: string) => void; enqueueInference: (sessionId: string, assistantMessageId: string) => void;
publishUserMessage: ( publishUserMessage: (
sessionId: string, sessionId: string,
userMessageId: string, userMessageId: string,
content: string content: string
) => void; ) => void;
publishMessagesDeleted: (sessionId: string, messageIds: string[]) => void;
} }
export function registerMessageRoutes( export function registerMessageRoutes(
@@ -30,7 +31,8 @@ export function registerMessageRoutes(
return { error: 'session not found' }; return { error: 'session not found' };
} }
const rows = await sql<Message[]>` const rows = await sql<Message[]>`
SELECT id, session_id, role, content, tool_calls, tool_results, status, last_seq, created_at SELECT id, session_id, role, content, tool_calls, tool_results, status, last_seq,
tokens_used, ctx_used, ctx_max, started_at, finished_at, created_at
FROM messages FROM messages
WHERE session_id = ${req.params.id} WHERE session_id = ${req.params.id}
ORDER BY created_at ASC, id ASC ORDER BY created_at ASC, id ASC
@@ -74,10 +76,66 @@ export function registerMessageRoutes(
result.user_message_id, result.user_message_id,
parsed.data.content parsed.data.content
); );
handlers.onSend(req.params.id, result.user_message_id, result.assistant_message_id); handlers.enqueueInference(req.params.id, result.assistant_message_id);
reply.code(202); reply.code(202);
return result; return result;
} }
); );
app.post<{ Params: { id: string; message_id: string } }>(
'/api/sessions/:id/messages/:message_id/regenerate',
async (req, reply) => {
const { id: sessionId, message_id: targetId } = req.params;
const target = await sql<{ id: string; role: string; status: string }[]>`
SELECT id, role, status
FROM messages
WHERE session_id = ${sessionId} AND id = ${targetId}
`;
if (target.length === 0) {
reply.code(404);
return { error: 'message not found' };
}
const targetRow = target[0]!;
if (targetRow.role !== 'assistant') {
reply.code(400);
return { error: 'only assistant messages can be regenerated' };
}
if (targetRow.status === 'streaming') {
reply.code(409);
return { error: 'message is still streaming' };
}
const { newAssistantId, deletedIds } = await sql.begin(async (tx) => {
// Subquery keeps created_at in postgres at TIMESTAMPTZ µs precision.
// Round-tripping through JS Date loses sub-ms precision and can pull
// earlier rows (e.g. the triggering user message) into the >= bound.
const deletedRows = await tx<{ id: string }[]>`
DELETE FROM messages
WHERE session_id = ${sessionId}
AND created_at >= (
SELECT created_at FROM messages WHERE id = ${targetId}
)
RETURNING id
`;
const [row] = await tx<{ id: string }[]>`
INSERT INTO messages (session_id, role, content, status, created_at)
VALUES (${sessionId}, 'assistant', '', 'streaming', clock_timestamp())
RETURNING id
`;
await tx`UPDATE sessions SET updated_at = NOW() WHERE id = ${sessionId}`;
return {
newAssistantId: row!.id,
deletedIds: deletedRows.map((r) => r.id),
};
});
handlers.publishMessagesDeleted(sessionId, deletedIds);
handlers.enqueueInference(sessionId, newAssistantId);
reply.code(202);
return { assistant_message_id: newAssistantId };
}
);
} }

View File

@@ -22,7 +22,8 @@ export function registerWebSocket(
} }
const messages = await sql<Message[]>` const messages = await sql<Message[]>`
SELECT id, session_id, role, content, tool_calls, tool_results, status, last_seq, created_at SELECT id, session_id, role, content, tool_calls, tool_results, status, last_seq,
tokens_used, ctx_used, ctx_max, started_at, finished_at, created_at
FROM messages FROM messages
WHERE session_id = ${sessionId} WHERE session_id = ${sessionId}
ORDER BY created_at ASC, id ASC ORDER BY created_at ASC, id ASC

View File

@@ -32,6 +32,12 @@ CREATE TABLE IF NOT EXISTS messages (
CREATE INDEX IF NOT EXISTS idx_messages_session ON messages(session_id, created_at); CREATE INDEX IF NOT EXISTS idx_messages_session ON messages(session_id, created_at);
ALTER TABLE messages ADD COLUMN IF NOT EXISTS tokens_used INTEGER;
ALTER TABLE messages ADD COLUMN IF NOT EXISTS ctx_used INTEGER;
ALTER TABLE messages ADD COLUMN IF NOT EXISTS ctx_max INTEGER;
ALTER TABLE messages ADD COLUMN IF NOT EXISTS started_at TIMESTAMPTZ;
ALTER TABLE messages ADD COLUMN IF NOT EXISTS finished_at TIMESTAMPTZ;
CREATE TABLE IF NOT EXISTS settings ( CREATE TABLE IF NOT EXISTS settings (
key TEXT PRIMARY KEY, key TEXT PRIMARY KEY,
value JSONB NOT NULL value JSONB NOT NULL

View File

@@ -0,0 +1,157 @@
import type { InferenceContext } from './inference.js';
const NAMING_SYSTEM_PROMPT =
'You name chat sessions. Reply directly with no thinking, reasoning, or explanation. Output ONLY the title, 4 words max, no quotes, no punctuation, no prefix like "Title:".';
const MAX_TITLE_CHARS = 60;
// QWEN3 NON-STREAMING UTILITY-CALL PATTERN
// ----------------------------------------
// Qwen3-family chat templates default to chain-of-thought reasoning: the
// model emits a long <think>…</think> block into `reasoning_content` and
// only finalizes a real reply in `content`. For short utility calls
// (naming, classification, routing, summarization) with a tight token
// budget, the model burns the entire budget on reasoning and returns:
// - content: ""
// - reasoning_content: "Thinking Process: 1. ..." (mid-thought, truncated)
// - finish_reason: "length"
// Fix: pass `chat_template_kwargs: { enable_thinking: false }` to skip the
// thinking block, and keep `max_tokens` low (~30 is plenty for a 4-word
// title). The kwarg is a no-op for non-Qwen chat templates, so it's safe
// to apply unconditionally for any short non-streaming model call.
// Apply this same pattern to: fork-message (planned), agent-routing
// (planned), web-search summarization (planned).
function cleanTitle(raw: string): string {
let name = raw.trim();
// Strip surrounding straight or smart quotes (one layer).
const quotes = ['"', "'", '`', '', '', '“', '”'];
while (name.length >= 2 && quotes.includes(name[0]!) && quotes.includes(name[name.length - 1]!)) {
name = name.slice(1, -1).trim();
}
// Drop a leading "Title:" prefix if the model added one despite instructions.
name = name.replace(/^title\s*:\s*/i, '').trim();
if (name.length > MAX_TITLE_CHARS) {
name = name.slice(0, MAX_TITLE_CHARS).trim();
}
return name;
}
interface NamingResponse {
choices?: Array<{
message?: {
content?: string;
reasoning_content?: string;
};
}>;
}
// Some Qwen-family models emit "thinking" tokens into reasoning_content and
// only finalize a real reply in content. Pull a sensible candidate string.
function pickTitleSource(data: NamingResponse): string {
const choice = data.choices?.[0]?.message;
if (!choice) return '';
if (choice.content && choice.content.trim().length > 0) return choice.content;
// Fallback: try to extract a last-line title from reasoning, if present.
const reasoning = choice.reasoning_content ?? '';
if (reasoning.length === 0) return '';
const lines = reasoning
.split('\n')
.map((l) => l.trim())
.filter((l) => l.length > 0);
return lines[lines.length - 1] ?? '';
}
export async function maybeAutoNameSession(
ctx: InferenceContext,
sessionId: string
): Promise<void> {
const counts = await ctx.sql<{ n: number }[]>`
SELECT COUNT(*)::int AS n
FROM messages
WHERE session_id = ${sessionId}
AND role = 'assistant'
AND status = 'complete'
`;
if (counts[0]?.n !== 1) return;
const sessionRows = await ctx.sql<
{ id: string; name: string; model: string }[]
>`
SELECT id, name, model FROM sessions WHERE id = ${sessionId}
`;
const session = sessionRows[0];
if (!session) return;
const existingName = session.name ?? '';
if (existingName !== '' && existingName !== 'New session') return;
const userMsg = await ctx.sql<{ content: string }[]>`
SELECT content FROM messages
WHERE session_id = ${sessionId} AND role = 'user'
ORDER BY created_at ASC
LIMIT 1
`;
const assistantMsg = await ctx.sql<{ content: string }[]>`
SELECT content FROM messages
WHERE session_id = ${sessionId}
AND role = 'assistant'
AND status = 'complete'
ORDER BY created_at ASC
LIMIT 1
`;
if (!userMsg[0] || !assistantMsg[0]) return;
const userText = userMsg[0].content.slice(0, 2000);
const assistantText = assistantMsg[0].content.slice(0, 2000);
const body = {
model: session.model,
messages: [
{ role: 'system', content: NAMING_SYSTEM_PROMPT },
{
role: 'user',
content: `First user message: ${userText}\nFirst assistant reply: ${assistantText}`,
},
],
max_tokens: 30,
temperature: 0.3,
stream: false,
// Qwen-family models default to chain-of-thought; this template kwarg
// tells llama.cpp's chat template renderer to skip the thinking block.
// Harmless for non-Qwen models.
chat_template_kwargs: { enable_thinking: false },
};
const res = await fetch(`${ctx.config.LLAMA_SWAP_URL}/v1/chat/completions`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(body),
});
if (!res.ok) {
const text = await res.text().catch(() => '');
throw new Error(`naming request failed: ${res.status} ${text.slice(0, 200)}`);
}
const data = (await res.json()) as NamingResponse;
const raw = pickTitleSource(data);
const name = cleanTitle(raw);
if (!name) {
ctx.log.warn({ sessionId, raw }, 'auto-name: empty title from model');
return;
}
const updated = await ctx.sql<{ id: string; name: string }[]>`
UPDATE sessions
SET name = ${name}, updated_at = NOW()
WHERE id = ${sessionId}
AND (name IS NULL OR name = '' OR name = 'New session')
RETURNING id, name
`;
if (updated.length === 0) return;
ctx.publish(sessionId, {
type: 'session_renamed',
session_id: sessionId,
name,
});
ctx.log.info({ sessionId, name }, 'session auto-named');
}

View File

@@ -4,6 +4,7 @@ import type { Config } from '../config.js';
import type { Message, Project, Session, ToolCall } from '../types/api.js'; import type { Message, Project, Session, ToolCall } from '../types/api.js';
import { ALL_TOOLS, TOOLS_BY_NAME, toolJsonSchemas } from './tools.js'; import { ALL_TOOLS, TOOLS_BY_NAME, toolJsonSchemas } from './tools.js';
import { PathScopeError, resolveProjectRoot } from './path_guard.js'; import { PathScopeError, resolveProjectRoot } from './path_guard.js';
import { maybeAutoNameSession } from './auto_name.js';
const BASE_SYSTEM_PROMPT = (projectPath: string) => const BASE_SYSTEM_PROMPT = (projectPath: string) =>
`You are BooCode Chat, a code investigation assistant. The user is working on a project located at ${projectPath}. Use the file-read tools (view_file, list_dir, grep, find_files) to investigate code when needed. Be concise. Cite file paths and line numbers when discussing code. Do not hallucinate file contents — read the file first. Tool results may be truncated; if so, narrow your query rather than guessing.`; `You are BooCode Chat, a code investigation assistant. The user is working on a project located at ${projectPath}. Use the file-read tools (view_file, list_dir, grep, find_files) to investigate code when needed. Be concise. Cite file paths and line numbers when discussing code. Do not hallucinate file contents — read the file first. Tool results may be truncated; if so, narrow your query rather than guessing.`;
@@ -12,8 +13,17 @@ const DB_FLUSH_INTERVAL_MS = 500;
const MAX_TOOL_LOOP_DEPTH = 5; const MAX_TOOL_LOOP_DEPTH = 5;
export interface InferenceFrame { export interface InferenceFrame {
type: 'message_started' | 'delta' | 'tool_call' | 'tool_result' | 'message_complete' | 'error'; type:
| 'message_started'
| 'delta'
| 'tool_call'
| 'tool_result'
| 'message_complete'
| 'messages_deleted'
| 'session_renamed'
| 'error';
message_id?: string; message_id?: string;
message_ids?: string[];
tool_message_id?: string; tool_message_id?: string;
tool_call_id?: string; tool_call_id?: string;
role?: 'assistant' | 'tool' | 'user'; role?: 'assistant' | 'tool' | 'user';
@@ -22,6 +32,14 @@ export interface InferenceFrame {
output?: unknown; output?: unknown;
truncated?: boolean; truncated?: boolean;
error?: string; error?: string;
tokens_used?: number | null;
ctx_used?: number | null;
ctx_max?: number | null;
started_at?: string | null;
finished_at?: string | null;
model?: string;
session_id?: string;
name?: string;
} }
export type FramePublisher = (sessionId: string, frame: InferenceFrame) => void; export type FramePublisher = (sessionId: string, frame: InferenceFrame) => void;
@@ -49,13 +67,21 @@ interface ChatCompletionDelta {
} }
interface ChatCompletionChunk { interface ChatCompletionChunk {
choices: Array<{ choices?: Array<{
delta: ChatCompletionDelta; delta: ChatCompletionDelta;
finish_reason: string | null; finish_reason: string | null;
}>; }>;
usage?: {
prompt_tokens?: number;
completion_tokens?: number;
total_tokens?: number;
};
timings?: {
n_ctx?: number;
};
} }
interface InferenceContext { export interface InferenceContext {
sql: Sql; sql: Sql;
config: Config; config: Config;
log: FastifyBaseLogger; log: FastifyBaseLogger;
@@ -130,7 +156,8 @@ async function loadContext(
const project = projectRows[0]!; const project = projectRows[0]!;
const history = await sql<Message[]>` const history = await sql<Message[]>`
SELECT id, session_id, role, content, tool_calls, tool_results, status, last_seq, created_at SELECT id, session_id, role, content, tool_calls, tool_results, status, last_seq,
tokens_used, ctx_used, ctx_max, started_at, finished_at, created_at
FROM messages FROM messages
WHERE session_id = ${sessionId} WHERE session_id = ${sessionId}
ORDER BY created_at ASC, id ASC ORDER BY created_at ASC, id ASC
@@ -162,14 +189,28 @@ async function* sseLines(stream: ReadableStream<Uint8Array>): AsyncGenerator<str
} }
} }
interface StreamResult {
finishReason: string | null;
content: string;
toolCalls: ToolCall[];
promptTokens: number | null;
completionTokens: number | null;
nCtx: number | null;
}
async function streamCompletion( async function streamCompletion(
ctx: InferenceContext, ctx: InferenceContext,
model: string, model: string,
messages: OpenAiMessage[], messages: OpenAiMessage[],
includeTools: boolean, includeTools: boolean,
onDelta: (content: string) => void onDelta: (content: string) => void
): Promise<{ finishReason: string | null; content: string; toolCalls: ToolCall[] }> { ): Promise<StreamResult> {
const body: Record<string, unknown> = { model, messages, stream: true }; const body: Record<string, unknown> = {
model,
messages,
stream: true,
stream_options: { include_usage: true },
};
if (includeTools) { if (includeTools) {
body['tools'] = toolJsonSchemas(); body['tools'] = toolJsonSchemas();
body['tool_choice'] = 'auto'; body['tool_choice'] = 'auto';
@@ -187,6 +228,9 @@ async function streamCompletion(
let content = ''; let content = '';
let finishReason: string | null = null; let finishReason: string | null = null;
let promptTokens: number | null = null;
let completionTokens: number | null = null;
let nCtx: number | null = null;
const toolCallsBuffer = new Map<number, { id: string; name: string; argsText: string }>(); const toolCallsBuffer = new Map<number, { id: string; name: string; argsText: string }>();
for await (const line of sseLines(res.body)) { for await (const line of sseLines(res.body)) {
@@ -199,6 +243,19 @@ async function streamCompletion(
} catch { } catch {
continue; continue;
} }
if (parsed.usage) {
if (typeof parsed.usage.prompt_tokens === 'number') {
promptTokens = parsed.usage.prompt_tokens;
}
if (typeof parsed.usage.completion_tokens === 'number') {
completionTokens = parsed.usage.completion_tokens;
}
}
if (parsed.timings && typeof parsed.timings.n_ctx === 'number') {
nCtx = parsed.timings.n_ctx;
}
const choice = parsed.choices?.[0]; const choice = parsed.choices?.[0];
if (!choice) continue; if (!choice) continue;
const delta = choice.delta ?? {}; const delta = choice.delta ?? {};
@@ -232,7 +289,7 @@ async function streamCompletion(
toolCalls.push({ id: t.id || `call_${toolCalls.length}`, name: t.name, args }); toolCalls.push({ id: t.id || `call_${toolCalls.length}`, name: t.name, args });
} }
return { finishReason, content, toolCalls }; return { finishReason, content, toolCalls, promptTokens, completionTokens, nCtx };
} }
async function executeToolCall( async function executeToolCall(
@@ -279,7 +336,9 @@ async function runAssistantTurn(
if (depth > MAX_TOOL_LOOP_DEPTH) { if (depth > MAX_TOOL_LOOP_DEPTH) {
await ctx.sql` await ctx.sql`
UPDATE messages UPDATE messages
SET status = 'failed', content = ${'tool loop depth exceeded'} SET status = 'failed',
content = ${'tool loop depth exceeded'},
finished_at = clock_timestamp()
WHERE id = ${assistantMessageId} WHERE id = ${assistantMessageId}
`; `;
ctx.publish(sessionId, { ctx.publish(sessionId, {
@@ -299,6 +358,14 @@ async function runAssistantTurn(
const projectRoot = await resolveProjectRoot(project.path); const projectRoot = await resolveProjectRoot(project.path);
const messages = buildMessagesPayload(session, project, history); const messages = buildMessagesPayload(session, project, history);
const startedRow = await ctx.sql<{ started_at: string }[]>`
UPDATE messages
SET started_at = clock_timestamp()
WHERE id = ${assistantMessageId}
RETURNING started_at
`;
const startedAt = startedRow[0]?.started_at ?? null;
ctx.publish(sessionId, { ctx.publish(sessionId, {
type: 'message_started', type: 'message_started',
message_id: assistantMessageId, message_id: assistantMessageId,
@@ -328,12 +395,9 @@ async function runAssistantTurn(
}, DB_FLUSH_INTERVAL_MS); }, DB_FLUSH_INTERVAL_MS);
}; };
let content = ''; let result: StreamResult;
let finishReason: string | null = null;
let toolCalls: ToolCall[] = [];
try { try {
const result = await streamCompletion( result = await streamCompletion(
ctx, ctx,
session.model, session.model,
messages, messages,
@@ -349,9 +413,6 @@ async function runAssistantTurn(
scheduleFlush(); scheduleFlush();
} }
); );
content = result.content;
finishReason = result.finishReason;
toolCalls = result.toolCalls;
} catch (err) { } catch (err) {
if (pendingFlushTimer) { if (pendingFlushTimer) {
clearTimeout(pendingFlushTimer); clearTimeout(pendingFlushTimer);
@@ -360,7 +421,9 @@ async function runAssistantTurn(
const errMsg = err instanceof Error ? err.message : String(err); const errMsg = err instanceof Error ? err.message : String(err);
await ctx.sql` await ctx.sql`
UPDATE messages UPDATE messages
SET status = 'failed', content = ${accumulated} SET status = 'failed',
content = ${accumulated},
finished_at = clock_timestamp()
WHERE id = ${assistantMessageId} WHERE id = ${assistantMessageId}
`; `;
ctx.publish(sessionId, { ctx.publish(sessionId, {
@@ -378,12 +441,22 @@ async function runAssistantTurn(
} }
await flushPromise; await flushPromise;
const { content, finishReason, toolCalls, promptTokens, completionTokens, nCtx } = result;
if (toolCalls.length > 0) { if (toolCalls.length > 0) {
await ctx.sql` const [updated] = await ctx.sql<
{ tokens_used: number | null; ctx_used: number | null; ctx_max: number | null; finished_at: string | null }[]
>`
UPDATE messages UPDATE messages
SET content = ${content}, status = 'complete', SET content = ${content},
tool_calls = ${ctx.sql.json(toolCalls as never)} status = 'complete',
tool_calls = ${ctx.sql.json(toolCalls as never)},
tokens_used = ${completionTokens},
ctx_used = ${promptTokens},
ctx_max = ${nCtx},
finished_at = clock_timestamp()
WHERE id = ${assistantMessageId} WHERE id = ${assistantMessageId}
RETURNING tokens_used, ctx_used, ctx_max, finished_at
`; `;
for (const tc of toolCalls) { for (const tc of toolCalls) {
ctx.publish(sessionId, { ctx.publish(sessionId, {
@@ -395,6 +468,12 @@ async function runAssistantTurn(
ctx.publish(sessionId, { ctx.publish(sessionId, {
type: 'message_complete', type: 'message_complete',
message_id: assistantMessageId, message_id: assistantMessageId,
tokens_used: updated?.tokens_used ?? null,
ctx_used: updated?.ctx_used ?? null,
ctx_max: updated?.ctx_max ?? null,
started_at: startedAt,
finished_at: updated?.finished_at ?? null,
model: session.model,
}); });
await Promise.all( await Promise.all(
@@ -405,12 +484,12 @@ async function runAssistantTurn(
RETURNING id RETURNING id
`; `;
const toolMessageId = toolRow!.id; const toolMessageId = toolRow!.id;
const result = await executeToolCall(projectRoot, tc); const tres = await executeToolCall(projectRoot, tc);
const stored = { const stored = {
tool_call_id: tc.id, tool_call_id: tc.id,
output: result.output, output: tres.output,
truncated: result.truncated, truncated: tres.truncated,
...(result.error ? { error: result.error } : {}), ...(tres.error ? { error: tres.error } : {}),
}; };
await ctx.sql` await ctx.sql`
UPDATE messages UPDATE messages
@@ -421,9 +500,9 @@ async function runAssistantTurn(
type: 'tool_result', type: 'tool_result',
tool_message_id: toolMessageId, tool_message_id: toolMessageId,
tool_call_id: tc.id, tool_call_id: tc.id,
output: result.output, output: tres.output,
truncated: result.truncated, truncated: tres.truncated,
...(result.error ? { error: result.error } : {}), ...(tres.error ? { error: tres.error } : {}),
}); });
}) })
); );
@@ -437,16 +516,40 @@ async function runAssistantTurn(
return; return;
} }
await ctx.sql` const [updated] = await ctx.sql<
{ tokens_used: number | null; ctx_used: number | null; ctx_max: number | null; finished_at: string | null }[]
>`
UPDATE messages UPDATE messages
SET content = ${content}, status = 'complete' SET content = ${content},
status = 'complete',
tokens_used = ${completionTokens},
ctx_used = ${promptTokens},
ctx_max = ${nCtx},
finished_at = clock_timestamp()
WHERE id = ${assistantMessageId} WHERE id = ${assistantMessageId}
RETURNING tokens_used, ctx_used, ctx_max, finished_at
`; `;
ctx.publish(sessionId, { ctx.publish(sessionId, {
type: 'message_complete', type: 'message_complete',
message_id: assistantMessageId, message_id: assistantMessageId,
tokens_used: updated?.tokens_used ?? null,
ctx_used: updated?.ctx_used ?? null,
ctx_max: updated?.ctx_max ?? null,
started_at: startedAt,
finished_at: updated?.finished_at ?? null,
model: session.model,
}); });
ctx.log.info({ sessionId, assistantMessageId, finishReason, chars: content.length }, 'inference complete'); ctx.log.info(
{
sessionId,
assistantMessageId,
finishReason,
chars: content.length,
tokens_used: updated?.tokens_used,
ctx_used: updated?.ctx_used,
},
'inference complete'
);
} }
export async function runInference( export async function runInference(
@@ -460,9 +563,18 @@ export async function runInference(
export function createInferenceRunner(ctx: InferenceContext) { export function createInferenceRunner(ctx: InferenceContext) {
return { return {
enqueue(sessionId: string, assistantMessageId: string) { enqueue(sessionId: string, assistantMessageId: string) {
void runInference(ctx, sessionId, assistantMessageId).catch((err) => { void (async () => {
ctx.log.error({ err }, 'unhandled inference error'); try {
}); await runInference(ctx, sessionId, assistantMessageId);
setImmediate(() => {
void maybeAutoNameSession(ctx, sessionId).catch((err) => {
ctx.log.warn({ err, sessionId }, 'auto-name failed');
});
});
} catch (err) {
ctx.log.error({ err }, 'unhandled inference error');
}
})();
}, },
}; };
} }

View File

@@ -46,6 +46,11 @@ export interface Message {
tool_results: ToolResult | null; tool_results: ToolResult | null;
status: MessageStatus; status: MessageStatus;
last_seq: number; last_seq: number;
tokens_used: number | null;
ctx_used: number | null;
ctx_max: number | null;
started_at: string | null;
finished_at: string | null;
created_at: string; created_at: string;
} }

View File

@@ -19,7 +19,9 @@
"radix-ui": "^1.4.3", "radix-ui": "^1.4.3",
"react": "^18.3.1", "react": "^18.3.1",
"react-dom": "^18.3.1", "react-dom": "^18.3.1",
"react-markdown": "^10.1.0",
"react-router-dom": "^6.26.0", "react-router-dom": "^6.26.0",
"remark-gfm": "^4.0.1",
"shadcn": "^4.7.0", "shadcn": "^4.7.0",
"sonner": "^2.0.7", "sonner": "^2.0.7",
"tailwind-merge": "^3.6.0", "tailwind-merge": "^3.6.0",

View File

@@ -83,6 +83,11 @@ export const api = {
body: JSON.stringify({ content }), body: JSON.stringify({ content }),
} }
), ),
regenerate: (sessionId: string, messageId: string) =>
request<{ assistant_message_id: string }>(
`/api/sessions/${sessionId}/messages/${messageId}/regenerate`,
{ method: 'POST' }
),
}, },
models: () => request<ModelInfo[]>('/api/models'), models: () => request<ModelInfo[]>('/api/models'),

View File

@@ -46,6 +46,11 @@ export interface Message {
tool_results: ToolResult | null; tool_results: ToolResult | null;
status: MessageStatus; status: MessageStatus;
last_seq: number; last_seq: number;
tokens_used: number | null;
ctx_used: number | null;
ctx_max: number | null;
started_at: string | null;
finished_at: string | null;
created_at: string; created_at: string;
} }
@@ -67,5 +72,15 @@ export type WsFrame =
truncated: boolean; truncated: boolean;
error?: string; error?: string;
} }
| { type: 'message_complete'; message_id: string } | {
type: 'message_complete';
message_id: string;
tokens_used?: number | null;
ctx_used?: number | null;
ctx_max?: number | null;
started_at?: string | null;
finished_at?: string | null;
}
| { type: 'messages_deleted'; message_ids: string[] }
| { type: 'session_renamed'; session_id: string; name: string }
| { type: 'error'; message_id?: string; error: string }; | { type: 'error'; message_id?: string; error: string };

View File

@@ -42,36 +42,3 @@ export function CodeBlock({ code, lang }: Props) {
</div> </div>
); );
} }
interface SegmentText {
kind: 'text';
value: string;
}
interface SegmentCode {
kind: 'code';
lang?: string;
value: string;
}
export type Segment = SegmentText | SegmentCode;
export function splitCodeBlocks(input: string): Segment[] {
const segments: Segment[] = [];
const fence = /```([a-zA-Z0-9_-]*)\n([\s\S]*?)```/g;
let lastIndex = 0;
let match: RegExpExecArray | null;
while ((match = fence.exec(input)) !== null) {
if (match.index > lastIndex) {
segments.push({ kind: 'text', value: input.slice(lastIndex, match.index) });
}
segments.push({
kind: 'code',
lang: match[1] || undefined,
value: (match[2] ?? '').replace(/\n$/, ''),
});
lastIndex = match.index + match[0].length;
}
if (lastIndex < input.length) {
segments.push({ kind: 'text', value: input.slice(lastIndex) });
}
return segments;
}

View File

@@ -1,49 +1,209 @@
import { useState } from 'react';
import Markdown from 'react-markdown';
import remarkGfm from 'remark-gfm';
import { Copy, RefreshCw, Check } from 'lucide-react';
import { toast } from 'sonner';
import type { Message } from '@/api/types'; import type { Message } from '@/api/types';
import { api } from '@/api/client';
import { ToolCallCard } from './ToolCallCard'; import { ToolCallCard } from './ToolCallCard';
import { CodeBlock, splitCodeBlocks } from './CodeBlock'; import { CodeBlock } from './CodeBlock';
interface Props { interface Props {
message: Message; message: Message;
sessionId: string;
} }
export function MessageBubble({ message }: Props) { function MarkdownBody({ content }: { content: string }) {
return (
<Markdown
remarkPlugins={[remarkGfm]}
components={{
pre: ({ children }) => <>{children}</>,
code: (props) => {
const { children, className, ...rest } = props as {
children?: unknown;
className?: string;
};
const text = String(children ?? '').replace(/\n$/, '');
const langMatch = /language-([\w-]+)/.exec(className ?? '');
const isBlock = !!langMatch || text.includes('\n');
if (isBlock) {
return <CodeBlock code={text} lang={langMatch?.[1]} />;
}
return (
<code
{...rest}
className="rounded bg-muted px-1 py-0.5 font-mono text-[0.85em]"
>
{children as React.ReactNode}
</code>
);
},
a: ({ children, href }) => (
<a
href={href}
target="_blank"
rel="noreferrer"
className="underline decoration-muted-foreground/40 underline-offset-2 hover:decoration-foreground"
>
{children}
</a>
),
ul: ({ children }) => (
<ul className="list-disc pl-5 space-y-1">{children}</ul>
),
ol: ({ children }) => (
<ol className="list-decimal pl-5 space-y-1">{children}</ol>
),
p: ({ children }) => <p className="leading-relaxed">{children}</p>,
h1: ({ children }) => <h1 className="text-base font-semibold mt-2">{children}</h1>,
h2: ({ children }) => <h2 className="text-sm font-semibold mt-2">{children}</h2>,
h3: ({ children }) => <h3 className="text-sm font-semibold mt-1">{children}</h3>,
blockquote: ({ children }) => (
<blockquote className="border-l-2 border-border pl-3 text-muted-foreground">
{children}
</blockquote>
),
table: ({ children }) => (
<div className="overflow-x-auto">
<table className="border-collapse text-xs">{children}</table>
</div>
),
th: ({ children }) => (
<th className="border border-border px-2 py-1 text-left font-medium">{children}</th>
),
td: ({ children }) => (
<td className="border border-border px-2 py-1">{children}</td>
),
}}
>
{content}
</Markdown>
);
}
function StatsLine({ message }: { message: Message }) {
const tokens = message.tokens_used;
if (typeof tokens !== 'number' || tokens <= 0) return null;
const started = message.started_at ? Date.parse(message.started_at) : NaN;
const finished = message.finished_at ? Date.parse(message.finished_at) : NaN;
let tps: number | null = null;
if (!Number.isNaN(started) && !Number.isNaN(finished) && finished > started) {
const seconds = (finished - started) / 1000;
if (seconds > 0) tps = Math.round((tokens / seconds) * 10) / 10;
}
const ctxUsed = message.ctx_used;
const ctxMax = message.ctx_max;
const ctxPart =
typeof ctxUsed === 'number'
? typeof ctxMax === 'number' && ctxMax > 0
? `${ctxUsed} / ${ctxMax} ctx`
: `${ctxUsed} ctx`
: null;
const parts: string[] = [`${tokens} tokens`];
if (tps !== null) parts.push(`${tps.toFixed(1)} tok/s`);
if (ctxPart) parts.push(ctxPart);
return (
<div className="text-[10px] font-mono text-muted-foreground">
{parts.join(' · ')}
</div>
);
}
function ActionRow({
message,
sessionId,
}: {
message: Message;
sessionId: string;
}) {
const [justCopied, setJustCopied] = useState(false);
const [regenerating, setRegenerating] = useState(false);
async function copy() {
try {
await navigator.clipboard.writeText(message.content);
setJustCopied(true);
setTimeout(() => setJustCopied(false), 1200);
} catch (err) {
toast.error(err instanceof Error ? err.message : 'copy failed');
}
}
async function regenerate() {
if (regenerating || message.status === 'streaming') return;
setRegenerating(true);
try {
await api.messages.regenerate(sessionId, message.id);
} catch (err) {
toast.error(err instanceof Error ? err.message : 'regenerate failed');
} finally {
setRegenerating(false);
}
}
const isAssistant = message.role === 'assistant';
const canRegen = isAssistant && message.status !== 'streaming';
return (
<div className="flex gap-1 opacity-0 group-hover:opacity-100 transition-opacity">
<button
type="button"
onClick={() => void copy()}
className="inline-flex items-center justify-center size-6 rounded text-muted-foreground hover:bg-muted hover:text-foreground"
aria-label="Copy message"
title="Copy"
>
{justCopied ? <Check className="size-3" /> : <Copy className="size-3" />}
</button>
{isAssistant && (
<button
type="button"
onClick={() => void regenerate()}
disabled={!canRegen || regenerating}
className="inline-flex items-center justify-center size-6 rounded text-muted-foreground hover:bg-muted hover:text-foreground disabled:opacity-40 disabled:cursor-not-allowed"
aria-label="Regenerate message"
title="Regenerate"
>
<RefreshCw className={`size-3 ${regenerating ? 'animate-spin' : ''}`} />
</button>
)}
</div>
);
}
export function MessageBubble({ message, sessionId }: Props) {
if (message.role === 'tool') { if (message.role === 'tool') {
return <ToolCallCard message={message} />; return <ToolCallCard message={message} />;
} }
if (message.role === 'user') { if (message.role === 'user') {
return ( return (
<div className="flex justify-end"> <div className="group flex flex-col items-end gap-1">
<div className="max-w-[80%] rounded-lg bg-primary text-primary-foreground px-3 py-2 text-sm whitespace-pre-wrap"> <div className="max-w-[80%] rounded-lg bg-primary text-primary-foreground px-3 py-2 text-sm whitespace-pre-wrap">
{message.content} {message.content}
</div> </div>
<ActionRow message={message} sessionId={sessionId} />
</div> </div>
); );
} }
const isStreaming = message.status === 'streaming'; const isStreaming = message.status === 'streaming';
const failed = message.status === 'failed'; const failed = message.status === 'failed';
const hasContent = message.content.length > 0;
const hasToolCalls = (message.tool_calls?.length ?? 0) > 0;
return ( return (
<div className="flex flex-col gap-2"> <div className="group flex flex-col gap-2">
{message.tool_calls?.map((tc) => ( {message.tool_calls?.map((tc) => (
<ToolCallCard key={tc.id} toolCall={tc} /> <ToolCallCard key={tc.id} toolCall={tc} />
))} ))}
{(message.content.length > 0 || (!message.tool_calls?.length && isStreaming)) && ( {(hasContent || (!hasToolCalls && isStreaming)) && (
<div className="max-w-[90%] text-sm leading-relaxed space-y-2"> <div className="max-w-[90%] text-sm leading-relaxed space-y-2">
{splitCodeBlocks(message.content).map((seg, i) => {hasContent ? <MarkdownBody content={message.content} /> : null}
seg.kind === 'code' ? ( {isStreaming && (
<CodeBlock key={i} code={seg.value} lang={seg.lang} />
) : (
<div key={i} className="whitespace-pre-wrap">
{seg.value}
{isStreaming && i === splitCodeBlocks(message.content).length - 1 && (
<span className="inline-block w-1.5 h-3.5 align-baseline bg-muted-foreground/60 animate-pulse ml-0.5" />
)}
</div>
)
)}
{message.content.length === 0 && isStreaming && (
<span className="inline-block w-1.5 h-3.5 align-baseline bg-muted-foreground/60 animate-pulse" /> <span className="inline-block w-1.5 h-3.5 align-baseline bg-muted-foreground/60 animate-pulse" />
)} )}
</div> </div>
@@ -51,6 +211,10 @@ export function MessageBubble({ message }: Props) {
{failed && ( {failed && (
<div className="text-xs text-destructive">message failed</div> <div className="text-xs text-destructive">message failed</div>
)} )}
{!isStreaming && <StatsLine message={message} />}
{!isStreaming && (hasContent || hasToolCalls) && (
<ActionRow message={message} sessionId={sessionId} />
)}
</div> </div>
); );
} }

View File

@@ -4,9 +4,10 @@ import { MessageBubble } from './MessageBubble';
interface Props { interface Props {
messages: Message[]; messages: Message[];
sessionId: string;
} }
export function MessageList({ messages }: Props) { export function MessageList({ messages, sessionId }: Props) {
const endRef = useRef<HTMLDivElement>(null); const endRef = useRef<HTMLDivElement>(null);
useEffect(() => { useEffect(() => {
@@ -24,7 +25,7 @@ export function MessageList({ messages }: Props) {
return ( return (
<div className="flex-1 overflow-y-auto px-6 py-4 space-y-4"> <div className="flex-1 overflow-y-auto px-6 py-4 space-y-4">
{messages.map((m) => ( {messages.map((m) => (
<MessageBubble key={m.id} message={m} /> <MessageBubble key={m.id} message={m} sessionId={sessionId} />
))} ))}
<div ref={endRef} /> <div ref={endRef} />
</div> </div>

View File

@@ -0,0 +1,32 @@
// Tiny in-app event bus for session metadata changes that need to propagate
// across hooks (e.g. AI rename arriving via WS in the session view needs to
// also refresh the sidebar's session list). One event type for now.
export interface SessionRenamedEvent {
type: 'session_renamed';
session_id: string;
name: string;
}
type SessionEvent = SessionRenamedEvent;
type Listener = (event: SessionEvent) => void;
const listeners = new Set<Listener>();
export const sessionEvents = {
emit(event: SessionEvent) {
for (const listener of listeners) {
try {
listener(event);
} catch {
// swallow — one bad listener shouldn't break others
}
}
},
subscribe(listener: Listener): () => void {
listeners.add(listener);
return () => {
listeners.delete(listener);
};
},
};

View File

@@ -1,5 +1,6 @@
import { useEffect, useRef, useState } from 'react'; import { useEffect, useRef, useState } from 'react';
import type { Message, WsFrame } from '@/api/types'; import type { Message, WsFrame } from '@/api/types';
import { sessionEvents } from './sessionEvents';
interface State { interface State {
messages: Message[]; messages: Message[];
@@ -24,6 +25,11 @@ function applyFrame(state: State, frame: WsFrame): State {
tool_results: null, tool_results: null,
status: 'streaming', status: 'streaming',
last_seq: 0, last_seq: 0,
tokens_used: null,
ctx_used: null,
ctx_max: null,
started_at: null,
finished_at: null,
created_at: new Date().toISOString(), created_at: new Date().toISOString(),
}; };
return { ...state, messages: [...state.messages, newMsg] }; return { ...state, messages: [...state.messages, newMsg] };
@@ -76,16 +82,47 @@ function applyFrame(state: State, frame: WsFrame): State {
}, },
status: 'complete', status: 'complete',
last_seq: 0, last_seq: 0,
tokens_used: null,
ctx_used: null,
ctx_max: null,
started_at: null,
finished_at: null,
created_at: new Date().toISOString(), created_at: new Date().toISOString(),
}; };
return { ...state, messages: [...state.messages, newMsg] }; return { ...state, messages: [...state.messages, newMsg] };
} }
case 'message_complete': { case 'message_complete': {
const next = state.messages.map((m) => const next = state.messages.map((m) =>
m.id === frame.message_id ? { ...m, status: 'complete' as const } : m m.id === frame.message_id
? {
...m,
status: 'complete' as const,
...(frame.tokens_used !== undefined ? { tokens_used: frame.tokens_used } : {}),
...(frame.ctx_used !== undefined ? { ctx_used: frame.ctx_used } : {}),
...(frame.ctx_max !== undefined ? { ctx_max: frame.ctx_max } : {}),
...(frame.started_at !== undefined ? { started_at: frame.started_at } : {}),
...(frame.finished_at !== undefined ? { finished_at: frame.finished_at } : {}),
}
: m
); );
return { ...state, messages: next }; return { ...state, messages: next };
} }
case 'messages_deleted': {
const removeSet = new Set(frame.message_ids);
return {
...state,
messages: state.messages.filter((m) => !removeSet.has(m.id)),
};
}
case 'session_renamed': {
// Side-effect, not state — dispatch via event bus to other hooks.
sessionEvents.emit({
type: 'session_renamed',
session_id: frame.session_id,
name: frame.name,
});
return state;
}
case 'error': { case 'error': {
const next = frame.message_id const next = frame.message_id
? state.messages.map((m) => ? state.messages.map((m) =>

View File

@@ -1,6 +1,7 @@
import { useCallback, useEffect, useState } from 'react'; import { useCallback, useEffect, useState } from 'react';
import { api } from '@/api/client'; import { api } from '@/api/client';
import type { Session } from '@/api/types'; import type { Session } from '@/api/types';
import { sessionEvents } from './sessionEvents';
export function useSessions(projectId: string | undefined) { export function useSessions(projectId: string | undefined) {
const [sessions, setSessions] = useState<Session[] | null>(null); const [sessions, setSessions] = useState<Session[] | null>(null);
@@ -24,6 +25,23 @@ export function useSessions(projectId: string | undefined) {
void refresh(); void refresh();
}, [refresh]); }, [refresh]);
useEffect(() => {
return sessionEvents.subscribe((event) => {
if (event.type !== 'session_renamed') return;
setSessions((prev) => {
if (!prev) return prev;
let changed = false;
const next = prev.map((s) => {
if (s.id !== event.session_id) return s;
if (s.name === event.name) return s;
changed = true;
return { ...s, name: event.name };
});
return changed ? next : prev;
});
});
}, []);
const create = useCallback( const create = useCallback(
async (body: { name?: string; model?: string; system_prompt?: string }) => { async (body: { name?: string; model?: string; system_prompt?: string }) => {
if (!projectId) throw new Error('no project'); if (!projectId) throw new Error('no project');

View File

@@ -5,6 +5,7 @@ import { toast } from 'sonner';
import { api } from '@/api/client'; import { api } from '@/api/client';
import type { Session as SessionType } from '@/api/types'; import type { Session as SessionType } from '@/api/types';
import { useSessionStream } from '@/hooks/useSessionStream'; import { useSessionStream } from '@/hooks/useSessionStream';
import { sessionEvents } from '@/hooks/sessionEvents';
import { MessageList } from '@/components/MessageList'; import { MessageList } from '@/components/MessageList';
import { ChatInput } from '@/components/ChatInput'; import { ChatInput } from '@/components/ChatInput';
import { ModelPicker } from '@/components/ModelPicker'; import { ModelPicker } from '@/components/ModelPicker';
@@ -39,6 +40,16 @@ export function Session() {
.catch(() => {}); .catch(() => {});
}, [id]); }, [id]);
useEffect(() => {
if (!id) return;
return sessionEvents.subscribe((event) => {
if (event.type !== 'session_renamed') return;
if (event.session_id !== id) return;
setSession((prev) => (prev ? { ...prev, name: event.name } : prev));
setName((prev) => (editingName ? prev : event.name));
});
}, [id, editingName]);
async function saveName() { async function saveName() {
if (!id || !session) return; if (!id || !session) return;
const trimmed = name.trim(); const trimmed = name.trim();
@@ -111,7 +122,7 @@ export function Session() {
)} )}
</header> </header>
<MessageList messages={stream.messages} /> {id && <MessageList messages={stream.messages} sessionId={id} />}
<ChatInput disabled={streaming} onSend={handleSend} /> <ChatInput disabled={streaming} onSend={handleSend} />
</div> </div>

868
pnpm-lock.yaml generated

File diff suppressed because it is too large Load Diff