xonecas.io
53129
Pa

PAT / commit 373aef0

feat: track tool calls during gen in UI

xonecas2025-12-17 373aef00048681c0ab7cf94a82d5f0a2202d66cb parent 4deb00a

8 files changed

app/api/chat/route.ts+3−2
@@ -40,15 +40,16 @@ export async function POST(request: Request) {
4040 });
4141
4242 // 4. Get assistant response
43- const { content: answer, generationTimeMs } = await fetchOllamaResponse(ollamaMessages);
43+ const { content: answer, generationTimeMs, toolCalls } = await fetchOllamaResponse(ollamaMessages);
4444
45- // 5. Store assistant message with generation time
45+ // 5. Store assistant message with generation time and tool calls
4646 await db.insert(messages).values({
4747 threadId: parseInt(threadId),
4848 role: "assistant",
4949 content: answer,
5050 createdAt: new Date(),
5151 generationTimeMs,
52+ toolCalls: toolCalls ? JSON.stringify(toolCalls) : null,
5253 });
5354
5455 // 6. Update thread's updatedAt timestamp
app/page.tsx+17−0
@@ -154,6 +154,7 @@ export default function Home() {
154154 content: message,
155155 createdAt: new Date(),
156156 generationTimeMs: null,
157+ toolCalls: null,
157158 };
158159 setMessages((prev) => [...prev, userMsg]);
159160
@@ -173,6 +174,7 @@ export default function Home() {
173174 content: data.answer || "",
174175 createdAt: new Date(),
175176 generationTimeMs: null,
177+ toolCalls: null,
176178 };
177179 setMessages((prev) => [...prev, botMsg]);
178180
@@ -204,6 +206,18 @@ export default function Home() {
204206 return `${(ms / 1000).toFixed(1)}s`;
205207 };
206208
209+ const formatToolCalls = (toolCallsJson: string | null): string | null => {
210+ if (!toolCallsJson) return null;
211+ try {
212+ const toolCalls = JSON.parse(toolCallsJson);
213+ if (!Array.isArray(toolCalls) || toolCalls.length === 0) return null;
214+ const toolNames = toolCalls.map((tc: any) => tc.function?.name || "unknown").join(", ");
215+ return toolNames;
216+ } catch {
217+ return null;
218+ }
219+ };
220+
207221 return (
208222 <div className="flex h-screen bg-neutral-100 text-neutral-800 dark:bg-neutral-950 dark:text-neutral-200">
209223 <div className="flex min-w-0 flex-1 flex-col">
@@ -248,6 +262,9 @@ export default function Home() {
248262 • generated in {formatGenerationTime(msg.generationTimeMs)}
249263 </span>
250264 )}
265+ {msg.role === "assistant" && formatToolCalls(msg.toolCalls) && (
266+ <span className="ml-2">• tools: {formatToolCalls(msg.toolCalls)}</span>
267+ )}
251268 </div>
252269 </div>
253270 </div>
drizzle/0002_cynical_thunderbolts.sql+1−0
@@ -0,0 +1 @@
1+ALTER TABLE `messages` ADD `tool_calls` text;
drizzle/meta/0002_snapshot.json+129−0
@@ -0,0 +1,129 @@
1+{
2+ "version": "6",
3+ "dialect": "sqlite",
4+ "id": "58b9a40a-76cd-4efa-8945-4bce2b8a1e2e",
5+ "prevId": "8ba81961-1ce9-4e1d-b4b2-5559d2f2de11",
6+ "tables": {
7+ "messages": {
8+ "name": "messages",
9+ "columns": {
10+ "id": {
11+ "name": "id",
12+ "type": "integer",
13+ "primaryKey": true,
14+ "notNull": true,
15+ "autoincrement": true
16+ },
17+ "thread_id": {
18+ "name": "thread_id",
19+ "type": "integer",
20+ "primaryKey": false,
21+ "notNull": true,
22+ "autoincrement": false
23+ },
24+ "role": {
25+ "name": "role",
26+ "type": "text",
27+ "primaryKey": false,
28+ "notNull": true,
29+ "autoincrement": false
30+ },
31+ "content": {
32+ "name": "content",
33+ "type": "text",
34+ "primaryKey": false,
35+ "notNull": true,
36+ "autoincrement": false
37+ },
38+ "created_at": {
39+ "name": "created_at",
40+ "type": "integer",
41+ "primaryKey": false,
42+ "notNull": true,
43+ "autoincrement": false
44+ },
45+ "generation_time_ms": {
46+ "name": "generation_time_ms",
47+ "type": "integer",
48+ "primaryKey": false,
49+ "notNull": false,
50+ "autoincrement": false
51+ },
52+ "tool_calls": {
53+ "name": "tool_calls",
54+ "type": "text",
55+ "primaryKey": false,
56+ "notNull": false,
57+ "autoincrement": false
58+ }
59+ },
60+ "indexes": {},
61+ "foreignKeys": {
62+ "messages_thread_id_threads_id_fk": {
63+ "name": "messages_thread_id_threads_id_fk",
64+ "tableFrom": "messages",
65+ "tableTo": "threads",
66+ "columnsFrom": [
67+ "thread_id"
68+ ],
69+ "columnsTo": [
70+ "id"
71+ ],
72+ "onDelete": "cascade",
73+ "onUpdate": "no action"
74+ }
75+ },
76+ "compositePrimaryKeys": {},
77+ "uniqueConstraints": {},
78+ "checkConstraints": {}
79+ },
80+ "threads": {
81+ "name": "threads",
82+ "columns": {
83+ "id": {
84+ "name": "id",
85+ "type": "integer",
86+ "primaryKey": true,
87+ "notNull": true,
88+ "autoincrement": true
89+ },
90+ "title": {
91+ "name": "title",
92+ "type": "text",
93+ "primaryKey": false,
94+ "notNull": false,
95+ "autoincrement": false
96+ },
97+ "created_at": {
98+ "name": "created_at",
99+ "type": "integer",
100+ "primaryKey": false,
101+ "notNull": true,
102+ "autoincrement": false
103+ },
104+ "updated_at": {
105+ "name": "updated_at",
106+ "type": "integer",
107+ "primaryKey": false,
108+ "notNull": true,
109+ "autoincrement": false
110+ }
111+ },
112+ "indexes": {},
113+ "foreignKeys": {},
114+ "compositePrimaryKeys": {},
115+ "uniqueConstraints": {},
116+ "checkConstraints": {}
117+ }
118+ },
119+ "views": {},
120+ "enums": {},
121+ "_meta": {
122+ "schemas": {},
123+ "tables": {},
124+ "columns": {}
125+ },
126+ "internal": {
127+ "indexes": {}
128+ }
129+}
drizzle/meta/_journal.json+7−0
@@ -15,6 +15,13 @@
1515 "when": 1765910805241,
1616 "tag": "0001_worthless_wolf_cub",
1717 "breakpoints": true
18+ },
19+ {
20+ "idx": 2,
21+ "version": "6",
22+ "when": 1765931091454,
23+ "tag": "0002_cynical_thunderbolts",
24+ "breakpoints": true
1825 }
1926 ]
2027 }
src/lib/db/schema.ts+1−0
@@ -24,6 +24,7 @@ export const messages = sqliteTable("messages", {
2424 .notNull()
2525 .$defaultFn(() => new Date()),
2626 generationTimeMs: integer("generation_time_ms"), // Time taken to generate response (for assistant messages)
27+ toolCalls: text("tool_calls"), // JSON string of tool calls made to generate this message (for assistant messages)
2728 });
2829
2930 // Define relations
src/lib/ollama/index.ts+15−2
@@ -18,6 +18,7 @@ export interface OllamaMessageInput {
1818 export interface OllamaResponse {
1919 content: string;
2020 generationTimeMs: number;
21+ toolCalls?: any[]; // Array of tool calls made during the conversation
2122 }
2223
2324 /**
@@ -100,6 +101,7 @@ export async function fetchOllamaResponse(
100101 }));
101102 const maxIterations = 10; // Prevent infinite loops
102103 let iterations = 0;
104+ const allToolCalls: any[] = []; // Collect all tool calls across iterations
103105
104106 debug(`[Ollama] Starting chat with model: ${model}, messages: ${messages.length}`);
105107
@@ -133,6 +135,9 @@ export async function fetchOllamaResponse(
133135 }))
134136 );
135137
138+ // Collect tool calls for the final response
139+ allToolCalls.push(...result.message.tool_calls);
140+
136141 // Add the assistant's message with tool calls (use result.message.tool_calls directly)
137142 currentMessages.push({
138143 role: "assistant",
@@ -172,7 +177,11 @@ export async function fetchOllamaResponse(
172177 `[Ollama] Final response (${generationTimeMs}ms total, ${iterations} iteration${iterations !== 1 ? "s" : ""}), content length: ${content.length} chars`
173178 );
174179
175- return { content, generationTimeMs };
180+ return {
181+ content,
182+ generationTimeMs,
183+ toolCalls: allToolCalls.length > 0 ? allToolCalls : undefined,
184+ };
176185 }
177186
178187 // If we hit max iterations, force a final response without tools
@@ -209,6 +218,10 @@ export async function fetchOllamaResponse(
209218 `[Ollama] Final response after max iterations (${generationTimeMs}ms total, ${iterations} iterations), content length: ${content.length} chars`
210219 );
211220
212- return { content, generationTimeMs };
221+ return {
222+ content,
223+ generationTimeMs,
224+ toolCalls: allToolCalls.length > 0 ? allToolCalls : undefined,
225+ };
213226 }
214227
src/types.ts+1−0
@@ -12,4 +12,5 @@ export type Message = {
1212 content: string;
1313 createdAt: Date;
1414 generationTimeMs: number | null;
15+ toolCalls: string | null; // JSON string of tool calls
1516 };