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:
@@ -50,7 +50,7 @@ async function main() {
|
||||
},
|
||||
});
|
||||
registerMessageRoutes(app, sql, {
|
||||
onSend: (sessionId, _userId, assistantId) => {
|
||||
enqueueInference: (sessionId, assistantId) => {
|
||||
inference.enqueue(sessionId, assistantId);
|
||||
},
|
||||
publishUserMessage: (sessionId, userMessageId, content) => {
|
||||
@@ -69,6 +69,12 @@ async function main() {
|
||||
message_id: userMessageId,
|
||||
});
|
||||
},
|
||||
publishMessagesDeleted: (sessionId, messageIds) => {
|
||||
broker.publish(sessionId, {
|
||||
type: 'messages_deleted',
|
||||
message_ids: messageIds,
|
||||
});
|
||||
},
|
||||
});
|
||||
registerWebSocket(app, sql, broker);
|
||||
|
||||
|
||||
@@ -8,12 +8,13 @@ const SendBody = z.object({
|
||||
});
|
||||
|
||||
interface MessageHandlers {
|
||||
onSend: (sessionId: string, userMessageId: string, assistantMessageId: string) => void;
|
||||
enqueueInference: (sessionId: string, assistantMessageId: string) => void;
|
||||
publishUserMessage: (
|
||||
sessionId: string,
|
||||
userMessageId: string,
|
||||
content: string
|
||||
) => void;
|
||||
publishMessagesDeleted: (sessionId: string, messageIds: string[]) => void;
|
||||
}
|
||||
|
||||
export function registerMessageRoutes(
|
||||
@@ -30,7 +31,8 @@ export function registerMessageRoutes(
|
||||
return { error: 'session not found' };
|
||||
}
|
||||
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
|
||||
WHERE session_id = ${req.params.id}
|
||||
ORDER BY created_at ASC, id ASC
|
||||
@@ -74,10 +76,66 @@ export function registerMessageRoutes(
|
||||
result.user_message_id,
|
||||
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);
|
||||
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 };
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
@@ -22,7 +22,8 @@ export function registerWebSocket(
|
||||
}
|
||||
|
||||
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
|
||||
WHERE session_id = ${sessionId}
|
||||
ORDER BY created_at ASC, id ASC
|
||||
|
||||
@@ -32,6 +32,12 @@ CREATE TABLE IF NOT EXISTS messages (
|
||||
|
||||
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 (
|
||||
key TEXT PRIMARY KEY,
|
||||
value JSONB NOT NULL
|
||||
|
||||
157
apps/server/src/services/auto_name.ts
Normal file
157
apps/server/src/services/auto_name.ts
Normal 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');
|
||||
}
|
||||
@@ -4,6 +4,7 @@ import type { Config } from '../config.js';
|
||||
import type { Message, Project, Session, ToolCall } 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';
|
||||
|
||||
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.`;
|
||||
@@ -12,8 +13,17 @@ const DB_FLUSH_INTERVAL_MS = 500;
|
||||
const MAX_TOOL_LOOP_DEPTH = 5;
|
||||
|
||||
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_ids?: string[];
|
||||
tool_message_id?: string;
|
||||
tool_call_id?: string;
|
||||
role?: 'assistant' | 'tool' | 'user';
|
||||
@@ -22,6 +32,14 @@ export interface InferenceFrame {
|
||||
output?: unknown;
|
||||
truncated?: boolean;
|
||||
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;
|
||||
@@ -49,13 +67,21 @@ interface ChatCompletionDelta {
|
||||
}
|
||||
|
||||
interface ChatCompletionChunk {
|
||||
choices: Array<{
|
||||
choices?: Array<{
|
||||
delta: ChatCompletionDelta;
|
||||
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;
|
||||
config: Config;
|
||||
log: FastifyBaseLogger;
|
||||
@@ -130,7 +156,8 @@ async function loadContext(
|
||||
const project = projectRows[0]!;
|
||||
|
||||
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
|
||||
WHERE session_id = ${sessionId}
|
||||
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(
|
||||
ctx: InferenceContext,
|
||||
model: string,
|
||||
messages: OpenAiMessage[],
|
||||
includeTools: boolean,
|
||||
onDelta: (content: string) => void
|
||||
): Promise<{ finishReason: string | null; content: string; toolCalls: ToolCall[] }> {
|
||||
const body: Record<string, unknown> = { model, messages, stream: true };
|
||||
): Promise<StreamResult> {
|
||||
const body: Record<string, unknown> = {
|
||||
model,
|
||||
messages,
|
||||
stream: true,
|
||||
stream_options: { include_usage: true },
|
||||
};
|
||||
if (includeTools) {
|
||||
body['tools'] = toolJsonSchemas();
|
||||
body['tool_choice'] = 'auto';
|
||||
@@ -187,6 +228,9 @@ async function streamCompletion(
|
||||
|
||||
let content = '';
|
||||
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 }>();
|
||||
|
||||
for await (const line of sseLines(res.body)) {
|
||||
@@ -199,6 +243,19 @@ async function streamCompletion(
|
||||
} catch {
|
||||
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];
|
||||
if (!choice) continue;
|
||||
const delta = choice.delta ?? {};
|
||||
@@ -232,7 +289,7 @@ async function streamCompletion(
|
||||
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(
|
||||
@@ -279,7 +336,9 @@ async function runAssistantTurn(
|
||||
if (depth > MAX_TOOL_LOOP_DEPTH) {
|
||||
await ctx.sql`
|
||||
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}
|
||||
`;
|
||||
ctx.publish(sessionId, {
|
||||
@@ -299,6 +358,14 @@ async function runAssistantTurn(
|
||||
const projectRoot = await resolveProjectRoot(project.path);
|
||||
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, {
|
||||
type: 'message_started',
|
||||
message_id: assistantMessageId,
|
||||
@@ -328,12 +395,9 @@ async function runAssistantTurn(
|
||||
}, DB_FLUSH_INTERVAL_MS);
|
||||
};
|
||||
|
||||
let content = '';
|
||||
let finishReason: string | null = null;
|
||||
let toolCalls: ToolCall[] = [];
|
||||
|
||||
let result: StreamResult;
|
||||
try {
|
||||
const result = await streamCompletion(
|
||||
result = await streamCompletion(
|
||||
ctx,
|
||||
session.model,
|
||||
messages,
|
||||
@@ -349,9 +413,6 @@ async function runAssistantTurn(
|
||||
scheduleFlush();
|
||||
}
|
||||
);
|
||||
content = result.content;
|
||||
finishReason = result.finishReason;
|
||||
toolCalls = result.toolCalls;
|
||||
} catch (err) {
|
||||
if (pendingFlushTimer) {
|
||||
clearTimeout(pendingFlushTimer);
|
||||
@@ -360,7 +421,9 @@ async function runAssistantTurn(
|
||||
const errMsg = err instanceof Error ? err.message : String(err);
|
||||
await ctx.sql`
|
||||
UPDATE messages
|
||||
SET status = 'failed', content = ${accumulated}
|
||||
SET status = 'failed',
|
||||
content = ${accumulated},
|
||||
finished_at = clock_timestamp()
|
||||
WHERE id = ${assistantMessageId}
|
||||
`;
|
||||
ctx.publish(sessionId, {
|
||||
@@ -378,12 +441,22 @@ async function runAssistantTurn(
|
||||
}
|
||||
await flushPromise;
|
||||
|
||||
const { content, finishReason, toolCalls, promptTokens, completionTokens, nCtx } = result;
|
||||
|
||||
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
|
||||
SET content = ${content}, status = 'complete',
|
||||
tool_calls = ${ctx.sql.json(toolCalls as never)}
|
||||
SET content = ${content},
|
||||
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}
|
||||
RETURNING tokens_used, ctx_used, ctx_max, finished_at
|
||||
`;
|
||||
for (const tc of toolCalls) {
|
||||
ctx.publish(sessionId, {
|
||||
@@ -395,6 +468,12 @@ async function runAssistantTurn(
|
||||
ctx.publish(sessionId, {
|
||||
type: 'message_complete',
|
||||
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(
|
||||
@@ -405,12 +484,12 @@ async function runAssistantTurn(
|
||||
RETURNING id
|
||||
`;
|
||||
const toolMessageId = toolRow!.id;
|
||||
const result = await executeToolCall(projectRoot, tc);
|
||||
const tres = await executeToolCall(projectRoot, tc);
|
||||
const stored = {
|
||||
tool_call_id: tc.id,
|
||||
output: result.output,
|
||||
truncated: result.truncated,
|
||||
...(result.error ? { error: result.error } : {}),
|
||||
output: tres.output,
|
||||
truncated: tres.truncated,
|
||||
...(tres.error ? { error: tres.error } : {}),
|
||||
};
|
||||
await ctx.sql`
|
||||
UPDATE messages
|
||||
@@ -421,9 +500,9 @@ async function runAssistantTurn(
|
||||
type: 'tool_result',
|
||||
tool_message_id: toolMessageId,
|
||||
tool_call_id: tc.id,
|
||||
output: result.output,
|
||||
truncated: result.truncated,
|
||||
...(result.error ? { error: result.error } : {}),
|
||||
output: tres.output,
|
||||
truncated: tres.truncated,
|
||||
...(tres.error ? { error: tres.error } : {}),
|
||||
});
|
||||
})
|
||||
);
|
||||
@@ -437,16 +516,40 @@ async function runAssistantTurn(
|
||||
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
|
||||
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}
|
||||
RETURNING tokens_used, ctx_used, ctx_max, finished_at
|
||||
`;
|
||||
ctx.publish(sessionId, {
|
||||
type: 'message_complete',
|
||||
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(
|
||||
@@ -460,9 +563,18 @@ export async function runInference(
|
||||
export function createInferenceRunner(ctx: InferenceContext) {
|
||||
return {
|
||||
enqueue(sessionId: string, assistantMessageId: string) {
|
||||
void runInference(ctx, sessionId, assistantMessageId).catch((err) => {
|
||||
ctx.log.error({ err }, 'unhandled inference error');
|
||||
});
|
||||
void (async () => {
|
||||
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');
|
||||
}
|
||||
})();
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
@@ -46,6 +46,11 @@ export interface Message {
|
||||
tool_results: ToolResult | null;
|
||||
status: MessageStatus;
|
||||
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;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user