Skip to content

Open source file-system and knowledge based agent template. Build AI agents that stay up to date with your knowledge base

display tool-call

Hugo Richardb838a7039229
diff --git a/apps/chat/app/components/ChatLoading.vue b/apps/chat/app/components/ChatLoading.vue
index 8d03051..27ad911 100644
--- a/apps/chat/app/components/ChatLoading.vue
+++ b/apps/chat/app/components/ChatLoading.vue
@@ -5,7 +5,7 @@ interface ToolCall {
toolCallId: string
toolName: string
args: Record<string, unknown>
- state?: string
+ state: 'loading' | 'done'
}
const props = defineProps<{
@@ -24,7 +24,7 @@ const messages = [
'Almost there',
]
-const finishedMessage = 'Search complete'
+const finishedMessage = 'Search done'
const currentIndex = ref(0)
const targetText = computed(() => {
@@ -52,11 +52,9 @@ function scrambleText(from: string, to: string) {
if (i < charProgress - 2) {
result += to[i] || ''
- }
- else if (i < charProgress) {
+ } else if (i < charProgress) {
result += chars[Math.floor(Math.random() * chars.length)]
- }
- else {
+ } else {
result += from[i] || ''
}
}
@@ -65,8 +63,7 @@ function scrambleText(from: string, to: string) {
if (frame < totalFrames) {
requestAnimationFrame(animate)
- }
- else {
+ } else {
displayedText.value = to
}
}
@@ -90,13 +87,22 @@ watch(() => props.isLoading, (isLoading) => {
}
})
-function getToolLabel(toolName: string, args: Record<string, unknown>) {
+function getToolLabel(tool: ToolCall) {
+ const { toolName, args, state } = tool
+ const isDone = state === 'done'
+
if (toolName === 'search_and_read') {
- return `Searching "${args?.query || '...'}"`
+ return isDone
+ ? `Searched "${args?.query || '...'}"`
+ : `Searching "${args?.query || '...'}"`
}
if (toolName === 'read') {
- return `Reading ${args?.path || '...'}`
+ const paths = args?.paths as string[] | undefined
+ const path = paths?.[0] || '...'
+ return isDone
+ ? `Read ${path}`
+ : `Reading ${path}`
}
return toolName
@@ -117,17 +123,17 @@ onUnmounted(() => {
</script>
<template>
- <div class="flex flex-col gap-2">
+ <div class="flex flex-col gap-2 mb-4">
<!-- Main loader with matrix and text -->
- <div class="flex items-center text-xs text-muted overflow-hidden">
+ <div class="flex items-center text-sm text-muted overflow-hidden">
<motion.div
v-if="isLoading"
:initial="{ opacity: 1, width: 'auto' }"
:exit="{ opacity: 0, width: 0 }"
:transition="{ duration: 0.2 }"
- class="shrink-0 mr-2"
+ class="shrink-0 mr-2.5"
>
- <ChatMatrix />
+ <ChatMatrix :size="4" :dot-size="3" :gap="3" />
</motion.div>
<motion.span
:animate="{ x: 0 }"
@@ -141,20 +147,20 @@ onUnmounted(() => {
<!-- Tool calls displayed below -->
<div
v-if="toolCalls?.length"
- class="flex flex-col gap-1"
- :class="isLoading ? 'pl-[22px]' : 'pl-0'"
+ class="flex flex-col gap-1.5"
+ :class="isLoading ? 'pl-[31px]' : 'pl-0'"
>
<motion.div
v-for="tool in toolCalls"
- :key="`${tool.toolCallId}-${JSON.stringify(tool.args)}`"
+ :key="tool.toolCallId"
:initial="{ opacity: 0, x: -4 }"
:animate="{ opacity: 1, x: 0 }"
:transition="{ duration: 0.15 }"
- class="flex items-center gap-1.5"
+ class="flex items-center gap-2"
>
- <span class="size-1 rounded-full bg-current opacity-40" />
- <span class="text-[11px] text-dimmed truncate max-w-[200px]">
- {{ getToolLabel(tool.toolName, tool.args) }}
+ <span class="size-1.5 rounded-full bg-current opacity-40" />
+ <span class="text-xs text-dimmed truncate max-w-[300px]">
+ {{ getToolLabel(tool) }}
</span>
</motion.div>
</div>
diff --git a/apps/chat/app/pages/chat/[id].vue b/apps/chat/app/pages/chat/[id].vue
index 4c39f53..f3d124f 100644
--- a/apps/chat/app/pages/chat/[id].vue
+++ b/apps/chat/app/pages/chat/[id].vue
@@ -109,19 +109,16 @@ interface ToolCallInfo {
toolCallId: string
toolName: string
args: Record<string, unknown>
- state: string
+ state: 'loading' | 'done'
}
function getMessageToolCalls(message: UIMessage): ToolCallInfo[] {
if (!message?.parts) return []
- return (message.parts as Array<{ type: string, toolCallId?: string, toolName?: string, input?: Record<string, unknown>, state?: string }>)
- .filter(p => p.type === 'dynamic-tool')
- .map(p => ({
- toolCallId: p.toolCallId || '',
- toolName: p.toolName || '',
- args: p.input || {},
- state: p.state || ''
- }))
+ // Extract individual tool call data parts (reconciled by ID)
+ return (message.parts as Array<{ type: string, data?: ToolCallInfo }>)
+ .filter(p => p.type === 'data-tool-call')
+ .map(p => p.data!)
+ .filter(Boolean)
}
onMounted(() => {
@@ -163,7 +160,7 @@ onMounted(() => {
:tool-calls="getMessageToolCalls(message)"
:is-loading="false"
/>
- <template v-for="(part, index) in message.parts.filter(p => p.type !== 'data-sources' && p.type !== 'dynamic-tool')" :key="`${message.id}-${part.type}-${index}${'state' in part ? `-${part.state}` : ''}`">
+ <template v-for="(part, index) in message.parts.filter(p => p.type !== 'data-sources' && p.type !== 'data-tool-call')" :key="`${message.id}-${part.type}-${index}${'state' in part ? `-${part.state}` : ''}`">
<Reasoning
v-if="part.type === 'reasoning'"
:text="part.text"
diff --git a/apps/chat/server/api/chats/[id].post.ts b/apps/chat/server/api/chats/[id].post.ts
index 8489188..44f1e2f 100644
--- a/apps/chat/server/api/chats/[id].post.ts
+++ b/apps/chat/server/api/chats/[id].post.ts
@@ -104,11 +104,6 @@ export default defineEventHandler(async (event) => {
const { savoir: savoirConfig } = useRuntimeConfig()
- const savoir = createSavoir({
- apiUrl: getRequestURL(event).origin,
- apiKey: savoirConfig.apiKey || undefined,
- })
-
let stepCount = 0
let toolCallCount = 0
let totalInputTokens = 0
@@ -116,51 +111,77 @@ export default defineEventHandler(async (event) => {
let stepStartTime = Date.now()
const stepDurations: number[] = []
- log.info('chat', `[${requestId}] Starting agent with ${model}`)
-
- const agent = new ToolLoopAgent({
- model,
- instructions: SYSTEM_PROMPT,
- tools: savoir.tools,
- onStepFinish: (stepResult) => {
- const stepDurationMs = Date.now() - stepStartTime
- stepDurations.push(stepDurationMs)
- stepCount++
-
- if (stepResult.usage) {
- totalInputTokens += stepResult.usage.inputTokens ?? 0
- totalOutputTokens += stepResult.usage.outputTokens ?? 0
- }
+ // Writer reference to be used by onToolCall callback
+ let streamWriter: any = null
- if (stepResult.toolCalls && stepResult.toolCalls.length > 0) {
- toolCallCount += stepResult.toolCalls.length
- const tools = stepResult.toolCalls.map(c => c.toolName).join(', ')
- log.info('chat', `[${requestId}] Step ${stepCount}: ${tools} (${stepDurationMs}ms)`)
- } else {
- log.info('chat', `[${requestId}] Step ${stepCount}: response (${stepDurationMs}ms)`)
+ const savoir = createSavoir({
+ apiUrl: getRequestURL(event).origin,
+ apiKey: savoirConfig.apiKey || undefined,
+ onToolCall: (info) => {
+ log.info('chat', `[${requestId}] Tool call [${info.state}]: ${info.toolName} ${JSON.stringify(info.args)}`)
+
+ if (streamWriter) {
+ streamWriter.write({
+ type: 'data-tool-call',
+ id: info.toolCallId,
+ data: {
+ toolCallId: info.toolCallId,
+ toolName: info.toolName,
+ args: info.args,
+ state: info.state,
+ },
+ })
}
-
- // Reset timer for next step
- stepStartTime = Date.now()
- },
- onFinish: (result) => {
- const totalDurationMs = stepDurations.reduce((a, b) => a + b, 0)
- requestLog.set({
- finishReason: result.finishReason,
- totalInputTokens,
- totalOutputTokens,
- totalTokens: totalInputTokens + totalOutputTokens,
- stepCount,
- toolCallCount,
- stepDurations,
- totalAgentMs: totalDurationMs,
- })
- log.info('chat', `[${requestId}] Finished: ${result.finishReason} (total: ${totalDurationMs}ms)`)
},
})
+ log.info('chat', `[${requestId}] Starting agent with ${model}`)
+
const stream = createUIMessageStream({
execute: async ({ writer }) => {
+ streamWriter = writer
+
+ const agent = new ToolLoopAgent({
+ model,
+ instructions: SYSTEM_PROMPT,
+ tools: savoir.tools,
+ onStepFinish: (stepResult) => {
+ const stepDurationMs = Date.now() - stepStartTime
+ stepDurations.push(stepDurationMs)
+ stepCount++
+
+ if (stepResult.usage) {
+ totalInputTokens += stepResult.usage.inputTokens ?? 0
+ totalOutputTokens += stepResult.usage.outputTokens ?? 0
+ }
+
+ if (stepResult.toolCalls && stepResult.toolCalls.length > 0) {
+ toolCallCount += stepResult.toolCalls.length
+ const tools = stepResult.toolCalls.map(c => c.toolName).join(', ')
+ log.info('chat', `[${requestId}] Step ${stepCount}: ${tools} (${stepDurationMs}ms)`)
+ } else {
+ log.info('chat', `[${requestId}] Step ${stepCount}: response (${stepDurationMs}ms)`)
+ }
+
+ // Reset timer for next step
+ stepStartTime = Date.now()
+ },
+ onFinish: (result) => {
+ const totalDurationMs = stepDurations.reduce((a, b) => a + b, 0)
+ requestLog.set({
+ finishReason: result.finishReason,
+ totalInputTokens,
+ totalOutputTokens,
+ totalTokens: totalInputTokens + totalOutputTokens,
+ stepCount,
+ toolCallCount,
+ stepDurations,
+ totalAgentMs: totalDurationMs,
+ })
+ log.info('chat', `[${requestId}] Finished: ${result.finishReason} (total: ${totalDurationMs}ms)`)
+ },
+ })
+
if (!chat.title && messages[0]) {
generateTitle({
firstMessage: messages[0],
diff --git a/packages/sdk/src/index.ts b/packages/sdk/src/index.ts
index 5802b48..1bd6f6f 100644
--- a/packages/sdk/src/index.ts
+++ b/packages/sdk/src/index.ts
@@ -1,8 +1,8 @@
import { SavoirClient } from './client'
import { createReadTool, createSearchAndReadTool } from './tools'
-import type { SavoirConfig } from './types'
+import type { SavoirConfig, ToolCallCallback } from './types'
-export type { SavoirConfig, SearchResult, FileContent, SearchAndReadResponse, ReadResponse, SyncOptions, SyncResponse, SnapshotResponse, GitHubSource, YouTubeSource, SourcesResponse, SyncSourceResponse } from './types'
+export type { SavoirConfig, SearchResult, FileContent, SearchAndReadResponse, ReadResponse, SyncOptions, SyncResponse, SnapshotResponse, GitHubSource, YouTubeSource, SourcesResponse, SyncSourceResponse, ToolCallInfo, ToolCallCallback, ToolCallState } from './types'
export { SavoirError, NetworkError } from './errors'
export { SavoirClient } from './client'
@@ -46,6 +46,9 @@ export interface Savoir {
* apiUrl: process.env.SAVOIR_API_URL!, // Required
* apiKey: process.env.SAVOIR_API_KEY, // Optional if API doesn't require auth
* sessionId: 'optional-session-id', // For sandbox reuse
+ * onToolCall: (info) => { // Optional tool call callback
+ * console.log(`Tool ${info.toolName} called with:`, info.args)
+ * },
* })
*
* const { text } = await generateText({
@@ -57,12 +60,13 @@ export interface Savoir {
*/
export function createSavoir(config: SavoirConfig): Savoir {
const client = new SavoirClient(config)
+ const onToolCall: ToolCallCallback | undefined = config.onToolCall
return {
client,
tools: {
- search_and_read: createSearchAndReadTool(client),
- read: createReadTool(client),
+ search_and_read: createSearchAndReadTool(client, onToolCall),
+ read: createReadTool(client, onToolCall),
},
getSessionId: () => client.getSessionId(),
setSessionId: (sessionId: string) => client.setSessionId(sessionId),
diff --git a/packages/sdk/src/tools/read.ts b/packages/sdk/src/tools/read.ts
index feffaeb..73671d0 100644
--- a/packages/sdk/src/tools/read.ts
+++ b/packages/sdk/src/tools/read.ts
@@ -1,11 +1,12 @@
import { tool } from 'ai'
import { z } from 'zod'
import type { SavoirClient } from '../client'
+import type { ToolCallCallback } from '../types'
/**
* Create the read tool for AI SDK
*/
-export function createReadTool(client: SavoirClient) {
+export function createReadTool(client: SavoirClient, onToolCall?: ToolCallCallback) {
return tool({
description: `Read specific documentation files by their paths.
Use this tool when you know the exact file paths you want to read.
@@ -13,9 +14,24 @@ Returns the full content of each requested file.`,
inputSchema: z.object({
paths: z.array(z.string()).min(1).max(50).describe('Array of file paths to read'),
}),
- execute: async ({ paths }) => {
+ onInputAvailable: ({ toolCallId, input }) => {
+ onToolCall?.({
+ toolCallId,
+ toolName: 'read',
+ args: input,
+ state: 'loading',
+ })
+ },
+ execute: async ({ paths }, { toolCallId }) => {
const result = await client.read(paths)
+ onToolCall?.({
+ toolCallId,
+ toolName: 'read',
+ args: { paths },
+ state: 'done',
+ })
+
return {
fileCount: result.files.length,
files: result.files.map(f => ({
diff --git a/packages/sdk/src/tools/search-and-read.ts b/packages/sdk/src/tools/search-and-read.ts
index 01f458d..9165da5 100644
--- a/packages/sdk/src/tools/search-and-read.ts
+++ b/packages/sdk/src/tools/search-and-read.ts
@@ -1,11 +1,12 @@
import { tool } from 'ai'
import { z } from 'zod'
import type { SavoirClient } from '../client'
+import type { ToolCallCallback } from '../types'
/**
* Create the searchAndRead tool for AI SDK
*/
-export function createSearchAndReadTool(client: SavoirClient) {
+export function createSearchAndReadTool(client: SavoirClient, onToolCall?: ToolCallCallback) {
return tool({
description: `Search the documentation for content matching a query and return the full file contents.
Use this tool to find relevant documentation about a topic.
@@ -14,9 +15,24 @@ Returns both the matching lines with context and the complete file contents for
query: z.string().describe('The search query (supports regex patterns)'),
limit: z.number().int().min(1).max(100).default(20).describe('Maximum number of results to return'),
}),
- execute: async ({ query, limit }) => {
+ onInputAvailable: ({ toolCallId, input }) => {
+ onToolCall?.({
+ toolCallId,
+ toolName: 'search_and_read',
+ args: input,
+ state: 'loading',
+ })
+ },
+ execute: async ({ query, limit }, { toolCallId }) => {
const result = await client.searchAndRead(query, limit)
+ onToolCall?.({
+ toolCallId,
+ toolName: 'search_and_read',
+ args: { query, limit },
+ state: 'done',
+ })
+
return {
matchCount: result.matches.length,
fileCount: result.files.length,
diff --git a/packages/sdk/src/types.ts b/packages/sdk/src/types.ts
index c1e2bba..4cc314a 100644
--- a/packages/sdk/src/types.ts
+++ b/packages/sdk/src/types.ts
@@ -18,8 +18,34 @@ export interface SavoirConfig {
* When provided, the same sandbox will be reused for multiple requests
*/
sessionId?: string
+
+ /**
+ * Optional callback when a tool call is made
+ * Called with tool call info when input is available
+ */
+ onToolCall?: ToolCallCallback
}
+/**
+ * Tool call state
+ */
+export type ToolCallState = 'loading' | 'done'
+
+/**
+ * Tool call information passed to onToolCall callback
+ */
+export interface ToolCallInfo {
+ toolCallId: string
+ toolName: string
+ args: Record<string, unknown>
+ state: ToolCallState
+}
+
+/**
+ * Callback type for tool call notifications
+ */
+export type ToolCallCallback = (info: ToolCallInfo) => void
+
/**
* Search result from the Savoir API
*/