Skip to content

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

improve ui and generate agent options

Hugo Richard74be21a31650
diff --git a/apps/chat/app/components/ChatLoading.vue b/apps/chat/app/components/ChatLoading.vue
index 27ad911..be448eb 100644
--- a/apps/chat/app/components/ChatLoading.vue
+++ b/apps/chat/app/components/ChatLoading.vue
@@ -1,11 +1,28 @@
<script setup lang="ts">
import { motion } from 'motion-v'
+import { useIntervalFn, useRafFn } from '@vueuse/core'
+
+interface CommandResult {
+ command: string
+ stdout: string
+ stderr: string
+ exitCode: number
+ success: boolean
+}
+
+interface ToolExecutionResult {
+ commands: CommandResult[]
+ success: boolean
+ durationMs: number
+ error?: string
+}
interface ToolCall {
toolCallId: string
toolName: string
args: Record<string, unknown>
- state: 'loading' | 'done'
+ state: 'loading' | 'done' | 'error'
+ result?: ToolExecutionResult
}
const props = defineProps<{
@@ -15,116 +32,89 @@ const props = defineProps<{
}>()
const messages = [
- 'Searching the documentation',
- 'Reading through the docs',
+ 'Exploring the sources',
+ 'Searching for content',
'Looking for relevant files',
- 'Analyzing the content',
+ 'Analyzing the results',
'Finding the best answer',
'Checking related resources',
'Almost there',
]
-const finishedMessage = 'Search done'
+const finishedMessage = 'Done'
+const chars = 'abcdefghijklmnopqrstuvwxyz'
const currentIndex = ref(0)
+const displayedText = ref('')
+
const targetText = computed(() => {
- if (!props.isLoading) {
- return finishedMessage
- }
+ if (!props.isLoading) return finishedMessage
return props.text || messages[currentIndex.value]
})
-const displayedText = ref(targetText.value)
-const chars = 'abcdefghijklmnopqrstuvwxyz'
+let frame = 0
+let totalFrames = 15
+let fromText = ''
+let toText = ''
-function scrambleText(from: string, to: string) {
- const maxLength = Math.max(from.length, to.length)
- let frame = 0
- const totalFrames = 15
-
- const animate = () => {
- frame++
- let result = ''
-
- for (let i = 0; i < maxLength; i++) {
- const progress = frame / totalFrames
- const charProgress = progress * maxLength
-
- if (i < charProgress - 2) {
- result += to[i] || ''
- } else if (i < charProgress) {
- result += chars[Math.floor(Math.random() * chars.length)]
- } else {
- result += from[i] || ''
- }
- }
+const { pause: pauseScramble, resume: resumeScramble } = useRafFn(() => {
+ frame++
+ const maxLength = Math.max(fromText.length, toText.length)
+ let result = ''
- displayedText.value = result
+ for (let i = 0; i < maxLength; i++) {
+ const progress = frame / totalFrames
+ const charProgress = progress * maxLength
- if (frame < totalFrames) {
- requestAnimationFrame(animate)
+ if (i < charProgress - 2) {
+ result += toText[i] || ''
+ } else if (i < charProgress) {
+ result += chars[Math.floor(Math.random() * chars.length)]
} else {
- displayedText.value = to
+ result += fromText[i] || ''
}
}
- requestAnimationFrame(animate)
-}
+ displayedText.value = result
+
+ if (frame >= totalFrames) {
+ displayedText.value = toText
+ pauseScramble()
+ }
+}, { immediate: false })
-let textInterval: ReturnType<typeof setInterval> | null = null
+function scrambleText(from: string, to: string) {
+ fromText = from
+ toText = to
+ frame = 0
+ resumeScramble()
+}
watch(targetText, (newText, oldText) => {
if (newText !== oldText && newText && oldText) {
scrambleText(oldText, newText)
}
-})
+}, { immediate: true })
-// Stop text rotation when loading finishes
-watch(() => props.isLoading, (isLoading) => {
- if (!isLoading && textInterval) {
- clearInterval(textInterval)
- textInterval = null
- }
+onMounted(() => {
+ displayedText.value = targetText.value || ''
})
-function getToolLabel(tool: ToolCall) {
- const { toolName, args, state } = tool
- const isDone = state === 'done'
-
- if (toolName === 'search_and_read') {
- return isDone
- ? `Searched "${args?.query || '...'}"`
- : `Searching "${args?.query || '...'}"`
- }
-
- if (toolName === 'read') {
- const paths = args?.paths as string[] | undefined
- const path = paths?.[0] || '...'
- return isDone
- ? `Read ${path}`
- : `Reading ${path}`
- }
-
- return toolName
-}
+const { pause, resume } = useIntervalFn(() => {
+ currentIndex.value = (currentIndex.value + 1) % messages.length
+}, 3500, { immediate: false })
-onMounted(() => {
- // Text rotation only when loading
- if (!props.text && props.isLoading) {
- textInterval = setInterval(() => {
- currentIndex.value = (currentIndex.value + 1) % messages.length
- }, 3500)
+watch(() => props.isLoading, (isLoading) => {
+ if (isLoading && !props.text) {
+ resume()
+ } else {
+ pause()
}
-})
-
-onUnmounted(() => {
- if (textInterval) clearInterval(textInterval)
-})
+}, { immediate: true })
</script>
<template>
<div class="flex flex-col gap-2 mb-4">
- <!-- Main loader with matrix and text -->
<div class="flex items-center text-sm text-muted overflow-hidden">
<motion.div
v-if="isLoading"
@@ -144,25 +134,16 @@ onUnmounted(() => {
</motion.span>
</div>
- <!-- Tool calls displayed below -->
<div
v-if="toolCalls?.length"
- class="flex flex-col gap-1.5"
+ class="flex flex-col gap-0.5"
:class="isLoading ? 'pl-[31px]' : 'pl-0'"
>
- <motion.div
+ <ToolCallItem
v-for="tool in toolCalls"
:key="tool.toolCallId"
- :initial="{ opacity: 0, x: -4 }"
- :animate="{ opacity: 1, x: 0 }"
- :transition="{ duration: 0.15 }"
- class="flex items-center gap-2"
- >
- <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>
+ :tool
+ />
</div>
</div>
</template>
diff --git a/apps/chat/app/components/ChatMatrix.vue b/apps/chat/app/components/ChatMatrix.vue
index 3d25609..443742d 100644
--- a/apps/chat/app/components/ChatMatrix.vue
+++ b/apps/chat/app/components/ChatMatrix.vue
@@ -1,4 +1,6 @@
<script setup lang="ts">
+import { useIntervalFn } from '@vueuse/core'
+
interface Props {
size?: number
dotSize?: number
@@ -14,34 +16,19 @@ const props = withDefaults(defineProps<Props>(), {
const totalDots = computed(() => props.size * props.size)
const activeDots = ref<Set<number>>(new Set())
-// Patterns for 4x4 grid (indices 0-15)
-// Grid layout:
-// 0 1 2 3
-// 4 5 6 7
-// 8 9 10 11
-// 12 13 14 15
const patterns = [
- // Spiral inward
[[0], [1], [2], [3], [7], [11], [15], [14], [13], [12], [8], [4], [5], [6], [10], [9]],
- // Wave horizontal
[[0, 4, 8, 12], [1, 5, 9, 13], [2, 6, 10, 14], [3, 7, 11, 15]],
- // Diamond pulse
[[5, 6, 9, 10], [1, 4, 7, 8, 11, 14], [0, 3, 12, 15], [1, 4, 7, 8, 11, 14], [5, 6, 9, 10]],
- // Loading bar
[[0, 1, 2, 3], [4, 5, 6, 7], [8, 9, 10, 11], [12, 13, 14, 15]],
- // Corners rotate
[[0], [3], [15], [12]],
- // Cross pulse
[[5, 6, 9, 10], [1, 2, 4, 7, 8, 11, 13, 14], [0, 3, 12, 15]],
- // Snake
[[0], [1], [2], [3], [7], [6], [5], [4], [8], [9], [10], [11], [15], [14], [13], [12]],
- // Diagonal wave
[[0], [1, 4], [2, 5, 8], [3, 6, 9, 12], [7, 10, 13], [11, 14], [15]],
]
let patternIndex = 0
let stepIndex = 0
-let interval: ReturnType<typeof setInterval> | null = null
function nextStep() {
const pattern = patterns[patternIndex]
@@ -69,14 +56,7 @@ const dotStyle = computed(() => ({
height: `${props.dotSize}px`,
}))
-onMounted(() => {
- interval = setInterval(nextStep, 120)
- nextStep()
-})
-
-onUnmounted(() => {
- if (interval) clearInterval(interval)
-})
+useIntervalFn(nextStep, 120, { immediateCallback: true })
</script>
<template>
diff --git a/apps/chat/app/components/ModelSelect.vue b/apps/chat/app/components/ModelSelect.vue
index 14fcee2..2d81acc 100644
--- a/apps/chat/app/components/ModelSelect.vue
+++ b/apps/chat/app/components/ModelSelect.vue
@@ -1,7 +1,6 @@
<script setup lang="ts">
const { model, models, formatModelName } = useModels()
-// Map providers to icons (some don't have simple-icons)
const providerIcons: Record<string, string> = {
openai: 'i-simple-icons-openai',
anthropic: 'i-simple-icons-anthropic',
diff --git a/apps/chat/app/components/SourceCard.vue b/apps/chat/app/components/SourceCard.vue
index 25f8d45..7b8057c 100644
--- a/apps/chat/app/components/SourceCard.vue
+++ b/apps/chat/app/components/SourceCard.vue
@@ -1,4 +1,6 @@
<script setup lang="ts">
+import { useTimeoutFn } from '@vueuse/core'
+
interface SourceData {
id: string
type: 'github' | 'youtube'
@@ -25,12 +27,14 @@ const emit = defineEmits<{
const isSyncing = ref(false)
+const { start: resetSyncState } = useTimeoutFn(() => {
+ isSyncing.value = false
+}, 2000, { immediate: false })
+
function handleSync() {
isSyncing.value = true
emit('sync')
- setTimeout(() => {
- isSyncing.value = false
- }, 2000)
+ resetSyncState()
}
const sourceUrl = computed(() => {
diff --git a/apps/chat/app/components/ToolCallItem.vue b/apps/chat/app/components/ToolCallItem.vue
new file mode 100644
index 0000000..7937e0b
--- /dev/null
+++ b/apps/chat/app/components/ToolCallItem.vue
@@ -0,0 +1,252 @@
+<script setup lang="ts">
+import { motion, AnimatePresence } from 'motion-v'
+import { useTimeoutFn } from '@vueuse/core'
+
+interface CommandResult {
+ command: string
+ stdout: string
+ stderr: string
+ exitCode: number
+ success: boolean
+}
+
+interface ToolExecutionResult {
+ commands: CommandResult[]
+ success: boolean
+ durationMs: number
+ error?: string
+}
+
+interface ToolCall {
+ toolCallId: string
+ toolName: string
+ args: Record<string, unknown>
+ state: 'loading' | 'done' | 'error'
+ result?: ToolExecutionResult
+}
+
+const props = defineProps<{
+ tool: ToolCall
+}>()
+
+const isExpanded = ref(false)
+const displayedLabel = ref('')
+
+const fullLabel = computed(() => {
+ const { toolName, args, state } = props.tool
+ const commands = args?.commands as string[] || (args?.command ? [args.command as string] : [])
+
+ if (toolName === 'search_and_read') {
+ const query = args?.query || '...'
+ return state === 'loading' ? `Searching "${query}"` : `Searched "${query}"`
+ }
+
+ if (toolName === 'read') {
+ const paths = args?.paths as string[] | undefined
+ const path = paths?.[0]?.split('/').pop() || '...'
+ return state === 'loading' ? `Reading ${path}` : `Read ${path}`
+ }
+
+ if (!commands.length) {
+ return state === 'loading' ? 'Running...' : 'Done'
+ }
+
+ const summaries = commands.slice(0, 2).map(summarizeCommand)
+ const suffix = commands.length > 2 ? ` +${commands.length - 2}` : ''
+ return `${summaries.join(', ')}${suffix}`
+})
+
+function summarizeCommand(cmd: string): string {
+ const trimmed = cmd.trim()
+ const firstPart = (trimmed.split('|')[0] ?? '').trim()
+
+ if (firstPart.startsWith('grep ') || firstPart.startsWith('rg ')) {
+ const match = trimmed.match(/(?:grep|rg)\s+(?:-[a-zA-Z]+\s+)*["']?([^"'\s|]+)["']?/)
+ return match ? `grep "${match[1]}"` : 'grep'
+ }
+ if (firstPart.startsWith('find ')) {
+ const nameMatch = trimmed.match(/-name\s+["']?([^"'\s]+)["']?/)
+ return nameMatch ? `find ${nameMatch[1]}` : 'find'
+ }
+ if (firstPart.startsWith('cat ') || firstPart.startsWith('head ') || firstPart.startsWith('tail ')) {
+ const pathMatch = firstPart.match(/(?:cat|head|tail)\s+(?:-[a-zA-Z0-9]+\s+)*(.+)/)
+ const fileName = pathMatch?.[1]?.trim().split('/').pop() || 'file'
+ return `read ${fileName}`
+ }
+ if (firstPart.startsWith('ls ')) {
+ const pathMatch = firstPart.match(/ls\s+(?:-[a-zA-Z]+\s+)*(.*)/)
+ const path = pathMatch?.[1]?.trim() || '.'
+ const dirName = path === '.' ? 'docs' : path.split('/').pop()
+ return `ls ${dirName}`
+ }
+
+ const [cmdName] = firstPart.split(/\s+/)
+ return cmdName || 'command'
+}
+
+let typewriterTimeout: ReturnType<typeof setTimeout> | null = null
+
+function typewrite(text: string, index = 0) {
+ if (index <= text.length) {
+ displayedLabel.value = text.slice(0, index)
+ typewriterTimeout = setTimeout(() => typewrite(text, index + 1), 20)
+ }
+}
+
+watch(fullLabel, (newLabel) => {
+ if (typewriterTimeout) clearTimeout(typewriterTimeout)
+ if (newLabel.length > displayedLabel.value.length) {
+ typewrite(newLabel, displayedLabel.value.length)
+ } else {
+ displayedLabel.value = newLabel
+ }
+}, { immediate: true })
+
+const canExpand = computed(() => {
+ const { args, result } = props.tool
+ return args?.commands || args?.command || result?.commands?.length
+})
+
+const { start: startExpand, stop: stopExpand } = useTimeoutFn(() => {
+ isExpanded.value = true
+}, 150, { immediate: false })
+
+const { start: startCollapse, stop: stopCollapse } = useTimeoutFn(() => {
+ isExpanded.value = false
+}, 2000, { immediate: false })
+
+watch(() => props.tool.state, (newState, oldState) => {
+ if (!canExpand.value) return
+
+ stopExpand()
+ stopCollapse()
+
+ if (newState === 'loading' && oldState !== 'loading') {
+ startExpand()
+ } else if (newState === 'done' && oldState === 'loading') {
+ startCollapse()
+ }
+}, { immediate: true })
+
+onUnmounted(() => {
+ if (typewriterTimeout) clearTimeout(typewriterTimeout)
+ stopExpand()
+ stopCollapse()
+})
+
+const commandsToShow = computed(() => {
+ const { args, result } = props.tool
+
+ if (result?.commands?.length) {
+ return result.commands
+ }
+
+ const cmds = args?.commands as string[] || (args?.command ? [args.command as string] : [])
+ return cmds.map(cmd => ({
+ command: cmd,
+ stdout: '',
+ stderr: '',
+ exitCode: 0,
+ success: true,
+ }))
+})
+
+function truncateOutput(text: string, maxLines = 8): string {
+ const lines = text.split('\n').filter(l => l.trim())
+ if (lines.length <= maxLines) return lines.join('\n')
+ return `${lines.slice(0, maxLines).join('\n')}\n··· ${lines.length - maxLines} more lines`
+}
+</script>
+
+<template>
+ <motion.div
+ :initial="{ opacity: 0, x: -6 }"
+ :animate="{ opacity: 1, x: 0 }"
+ :transition="{ duration: 0.35, ease: [0.25, 0.1, 0.25, 1] }"
+ >
+ <button
+ class="flex items-center gap-2 w-full text-left py-0.5 group rounded -mx-1 px-1"
+ :class="[
+ canExpand ? 'cursor-pointer hover:bg-muted/5' : 'cursor-default',
+ isExpanded && 'bg-muted/5',
+ ]"
+ @click="canExpand && (isExpanded = !isExpanded)"
+ >
+ <span class="relative flex size-1.5 shrink-0">
+ <span
+ v-if="tool.state === 'loading'"
+ class="animate-ping absolute inline-flex h-full w-full rounded-full bg-current opacity-75"
+ />
+ <span
+ class="relative inline-flex rounded-full size-1.5 bg-current"
+ :class="tool.state === 'loading' ? 'opacity-100' : 'opacity-40'"
+ />
+ </span>
+
+ <span class="text-xs text-dimmed font-mono flex-1 truncate">
+ {{ displayedLabel }}<span v-if="tool.state === 'loading'" class="animate-pulse">_</span>
+ </span>
+
+ <span
+ v-if="tool.result?.durationMs && tool.state === 'done'"
+ class="text-[10px] text-muted/40 tabular-nums"
+ >
+ {{ tool.result.durationMs }}ms
+ </span>
+
+ <motion.span
+ v-if="canExpand"
+ :animate="{ rotate: isExpanded ? 180 : 0 }"
+ :transition="{ duration: 0.35, ease: [0.25, 0.1, 0.25, 1] }"
+ >
+ <UIcon name="i-lucide-chevron-down" class="size-3 text-muted/40" />
+ </motion.span>
+ </button>
+
+ <AnimatePresence>
+ <motion.div
+ v-if="isExpanded && commandsToShow.length"
+ :initial="{ height: 0, opacity: 0 }"
+ :animate="{ height: 'auto', opacity: 1 }"
+ :exit="{ height: 0, opacity: 0 }"
+ :transition="{ duration: 0.4, ease: [0.25, 0.1, 0.25, 1] }"
+ class="overflow-hidden"
+ >
+ <div class="ml-3.5 pl-2 border-l border-muted/20 mt-1 mb-2 space-y-2">
+ <div
+ v-for="(cmd, idx) in commandsToShow"
+ :key="idx"
+ class="space-y-1"
+ >
+ <div class="flex items-start gap-1.5 text-[11px]">
+ <span class="text-muted/60 font-mono shrink-0">$</span>
+ <code class="text-muted/80 font-mono break-all">{{ cmd.command }}</code>
+ </div>
+
+ <div
+ v-if="cmd.stdout || cmd.stderr"
+ class="bg-muted/5 rounded px-2 py-1.5 font-mono text-[10px] text-dimmed/70 overflow-x-auto max-h-40 overflow-y-auto"
+ >
+ <pre v-if="cmd.stdout" class="whitespace-pre-wrap">{{ truncateOutput(cmd.stdout) }}</pre>
+ <pre v-if="cmd.stderr" class="whitespace-pre-wrap text-muted/60 mt-1">{{ truncateOutput(cmd.stderr, 3) }}</pre>
+ </div>
+
+ <div
+ v-else-if="tool.state === 'loading'"
+ class="text-[10px] text-muted/40 italic"
+ >
+ running...
+ </div>
+
+ <div
+ v-else-if="tool.state === 'done' && tool.result"
+ class="text-[10px] text-muted/30 italic"
+ >
+ (no output)
+ </div>
+ </div>
+ </div>
+ </motion.div>
+ </AnimatePresence>
+ </motion.div>
+</template>
diff --git a/apps/chat/app/pages/chat/[id].vue b/apps/chat/app/pages/chat/[id].vue
index f3d124f..9e03730 100644
--- a/apps/chat/app/pages/chat/[id].vue
+++ b/apps/chat/app/pages/chat/[id].vue
@@ -96,20 +96,27 @@ function copy(e: MouseEvent, message: UIMessage) {
}, 2000)
}
-const lastMessage = computed(() => chat.messages.at(-1))
-const showThinking = computed(() => {
- if (chat.status !== 'streaming') return false
- const msg = lastMessage.value
- if (msg?.role !== 'assistant') return false
- const parts = msg.parts as Array<{ type: string }> | undefined
- return !parts?.some(p => p.type === 'text')
-})
+interface CommandResult {
+ command: string
+ stdout: string
+ stderr: string
+ exitCode: number
+ success: boolean
+}
+
+interface ToolExecutionResult {
+ commands: CommandResult[]
+ success: boolean
+ durationMs: number
+ error?: string
+}
interface ToolCallInfo {
toolCallId: string
toolName: string
args: Record<string, unknown>
- state: 'loading' | 'done'
+ state: 'loading' | 'done' | 'error'
+ result?: ToolExecutionResult
}
function getMessageToolCalls(message: UIMessage): ToolCallInfo[] {
diff --git a/apps/chat/server/api/chats/[id].post.ts b/apps/chat/server/api/chats/[id].post.ts
index 132ed2b..8ada4ed 100644
--- a/apps/chat/server/api/chats/[id].post.ts
+++ b/apps/chat/server/api/chats/[id].post.ts
@@ -5,49 +5,52 @@ import { and, eq } from 'drizzle-orm'
import { createSavoir } from '@savoir/sdk'
import { log, useLogger } from 'evlog'
import { generateTitle } from '../../utils/chat/generate-title'
+import { routeQuestion, buildSystemPromptWithComplexity } from '../../utils/router/route-question'
-const SYSTEM_PROMPT = `You are an AI assistant specialized in the Nuxt/Nitro ecosystem.
+const SYSTEM_PROMPT = `You are an AI assistant that answers questions based on the available sources.
-## CRITICAL: Documentation First
+## CRITICAL: Sources First
-Your knowledge may be outdated. ONLY answer based on what you find in the documentation.
-- If you can't find information in the docs, say "I couldn't find this in the documentation"
-- NEVER make up information or guess - only state what you found in docs
-- NEVER mention package versions unless you verified them in the docs
+Your knowledge may be outdated. ONLY answer based on what you find in the sources.
+- If you can't find information, say "I couldn't find this in the available sources"
+- NEVER make up information or guess - only state what you found
+- Always cite the source file when quoting content
-## Tools
+## Search Strategy (IMPORTANT for speed)
-You have access to documentation search tools:
+1. **Explore first** - Start by discovering the available sources:
+ \`\`\`bash
+ ls /docs # See what sources are available
+ \`\`\`
-- **search_and_read**: Search AND read files in one step (PREFERRED - faster and more efficient)
-- **read**: Read specific files by path (use when you already know the exact file path)
+2. **Target specific directories** - Don't search everything at once:
+ \`\`\`bash
+ ls /docs/[source]/ # Explore the structure
+ grep -r "keyword" /docs/[source] --include="*.md" -l | head -10
+ \`\`\`
-## How to Search Effectively
-
-1. **Use ONE simple keyword**: "composables" ✓, "vue composables usage" ✗
-2. **Always prefer search_and_read** - it's faster and returns content directly
-3. **Search by concept**: "middleware", "plugins", "routing", "state", "data-fetching"
-4. **Search by package/module name**: "unstorage", "ofetch", "nitro", "h3", "unhead"
+3. **Use simple keywords**: one word is better than a phrase
## Workflow
1. User asks a question
-2. Call **search_and_read** with a relevant keyword
-3. Read the results and find the answer
-4. If not found, try different keywords or broader terms
-5. Answer based ONLY on what you found in the documentation
+2. Explore available sources with \`ls\`
+3. Identify which source(s) are relevant
+4. Search within those specific directories
+5. Read the relevant files
+6. Answer based ONLY on what you found
## Response Style
- Be concise and helpful
-- Include relevant code examples from the docs
+- Include relevant code examples when available
- Use markdown formatting
-- Cite the source file when quoting documentation
+- Cite the source file path
`
defineRouteMeta({
openAPI: {
- description: 'Chat with AI about Nuxt documentation.',
+ description: 'Chat with AI about the available sources.',
tags: ['ai'],
},
})
@@ -118,7 +121,10 @@ export default defineEventHandler(async (event) => {
apiUrl: getRequestURL(event).origin,
apiKey: savoirConfig.apiKey || undefined,
onToolCall: (info) => {
- log.info('chat', `[${requestId}] Tool call [${info.state}]: ${info.toolName} ${JSON.stringify(info.args)}`)
+ const resultSummary = info.result
+ ? ` [${info.result.success ? 'OK' : 'FAIL'}] (${info.result.durationMs}ms)`
+ : ''
+ log.info('chat', `[${requestId}] Tool call [${info.state}]: ${info.toolName}${resultSummary}`)
if (streamWriter) {
streamWriter.write({
@@ -129,13 +135,16 @@ export default defineEventHandler(async (event) => {
toolName: info.toolName,
args: info.args,
state: info.state,
+ result: info.result,
},
})
}
},
})
- log.info('chat', `[${requestId}] Starting agent with ${model}`)
+ const agentConfig = await routeQuestion(messages, requestId)
+
+ log.info('chat', `[${requestId}] Starting agent with ${model} (routed: ${agentConfig.complexity}, ${agentConfig.maxSteps} steps)`)
const stream = createUIMessageStream({
execute: async ({ writer }) => {
@@ -143,8 +152,9 @@ export default defineEventHandler(async (event) => {
const agent = new ToolLoopAgent({
model,
- instructions: SYSTEM_PROMPT,
+ instructions: buildSystemPromptWithComplexity(SYSTEM_PROMPT, agentConfig),
tools: savoir.tools,
+ stopWhen: stepCountIs(agentConfig.maxSteps),
onStepFinish: (stepResult) => {
const stepDurationMs = Date.now() - stepStartTime
stepDurations.push(stepDurationMs)
@@ -177,6 +187,9 @@ export default defineEventHandler(async (event) => {
toolCallCount,
stepDurations,
totalAgentMs: totalDurationMs,
+ routerComplexity: agentConfig.complexity,
+ routerMaxSteps: agentConfig.maxSteps,
+ routerReasoning: agentConfig.reasoning,
})
log.info('chat', `[${requestId}] Finished: ${result.finishReason} (total: ${totalDurationMs}ms)`)
},
diff --git a/apps/chat/server/utils/router/route-question.ts b/apps/chat/server/utils/router/route-question.ts
new file mode 100644
index 0000000..abb8543
--- /dev/null
+++ b/apps/chat/server/utils/router/route-question.ts
@@ -0,0 +1,96 @@
+import { createGateway } from '@ai-sdk/gateway'
+import { generateText, Output } from 'ai'
+import { log } from 'evlog'
+import type { UIMessage } from 'ai'
+import { type AgentConfig, agentConfigSchema, getDefaultConfig } from './schema'
+
+const ROUTER_MODEL = 'google/gemini-2.5-flash-lite'
+
+const ROUTER_SYSTEM_PROMPT = `You are a question classifier for an AI assistant.
+Analyze the user's question and determine the appropriate configuration for the agent.
+
+## Classification Guidelines
+
+**trivial** (maxSteps: 3, model: gemini-2.5-flash-lite)
+- Simple greetings: "Hello", "Thanks", "Hi there"
+- Acknowledgments without questions
+
+**simple** (maxSteps: 6, model: gemini-2.5-flash-lite)
+- Single concept lookups: "What is X?", "How to use Y?"
+- Direct questions with likely clear answers
+- Questions about a single topic
+
+**moderate** (maxSteps: 12, model: gemini-3-flash)
+- Comparisons: "Difference between X and Y?"
+- Integration questions requiring exploration
+- Questions requiring multiple file searches
+
+**complex** (maxSteps: 20, model: gemini-3-flash or claude-opus-4.5)
+- Debugging scenarios
+- Architecture questions
+- Deep analysis requiring extensive research
+- Cross-topic questions
+
+Use claude-opus-4.5 only for the most complex cases requiring deep reasoning.`
+
+function extractQuestionFromMessages(messages: UIMessage[]): string {
+ // Get the last user message
+ const lastUserMessage = [...messages].reverse().find(m => m.role === 'user')
+ if (!lastUserMessage) return ''
+
+ // Extract text from message parts
+ const textParts = lastUserMessage.parts
+ ?.filter((p): p is { type: 'text', text: string } => p.type === 'text')
+ .map(p => p.text)
+ .join('\n')
+
+ return textParts || ''
+}
+
+export async function routeQuestion(
+ messages: UIMessage[],
+ requestId: string,
+): Promise<AgentConfig> {
+ const config = useRuntimeConfig()
+ const gateway = createGateway({ apiKey: config.savoir?.apiKey })
+
+ const question = extractQuestionFromMessages(messages)
+ if (!question) {
+ log.info('chat', `[${requestId}] Router: no question found, using default config`)
+ return getDefaultConfig()
+ }
+
+ try {
+ const { output } = await generateText({
+ model: gateway(ROUTER_MODEL),
+ output: Output.object({ schema: agentConfigSchema }),
+ messages: [
+ { role: 'system', content: ROUTER_SYSTEM_PROMPT },
+ { role: 'user', content: `Question: ${question}` },
+ ],
+ })
+
+ if (!output) {
+ log.warn('chat', `[${requestId}] Router returned no output, using default config`)
+ return getDefaultConfig()
+ }
+
+ log.info('chat', `[${requestId}] Router decision: ${output.complexity} (${output.model}, ${output.maxSteps} steps) - ${output.reasoning}`)
+ return output
+ } catch (error) {
+ const errorMessage = error instanceof Error ? error.message : 'Unknown error'
+ log.error('chat', `[${requestId}] Router failed: ${errorMessage}, using default config`)
+ return getDefaultConfig()
+ }
+}
+
+export function buildSystemPromptWithComplexity(basePrompt: string, agentConfig: AgentConfig): string {
+ const complexityHints: Record<AgentConfig['complexity'], string> = {
+ trivial: 'This is a simple greeting or acknowledgment. Respond briefly without searching.',
+ simple: 'This is a straightforward question. Explore with `ls`, then do a targeted search.',
+ moderate: 'This requires some research. Explore the sources structure first, then search within relevant directories.',
+ complex: 'This is a complex question. Map out the available sources with `ls`, then systematically search relevant areas.',
+ }
+
+ return `${basePrompt}\n\n## Task Complexity: ${agentConfig.complexity}\n${complexityHints[agentConfig.complexity]}`
+}
diff --git a/apps/chat/server/utils/router/schema.ts b/apps/chat/server/utils/router/schema.ts
new file mode 100644
index 0000000..185f898
--- /dev/null
+++ b/apps/chat/server/utils/router/schema.ts
@@ -0,0 +1,29 @@
+import { z } from 'zod'
+
+export const agentConfigSchema = z.object({
+ complexity: z.enum(['trivial', 'simple', 'moderate', 'complex'])
+ .describe('trivial=greeting, simple=single lookup, moderate=multi-search, complex=deep analysis'),
+
+ maxSteps: z.number().min(1).max(30)
+ .describe('Agent iterations: 3 trivial, 6 simple, 12 moderate, 20 complex'),
+
+ model: z.enum([
+ 'google/gemini-3-flash',
+ 'google/gemini-2.5-flash-lite',
+ 'anthropic/claude-opus-4.5',
+ ]).describe('flash-lite for trivial/simple, flash for moderate, opus for complex'),
+
+ reasoning: z.string().max(200)
+ .describe('Brief explanation of the classification'),
+})
+
+export type AgentConfig = z.infer<typeof agentConfigSchema>
+
+export function getDefaultConfig(): AgentConfig {
+ return {
+ complexity: 'moderate',
+ maxSteps: 12,
+ model: 'google/gemini-3-flash',
+ reasoning: 'Default fallback configuration',
+ }
+}
diff --git a/apps/github-bot/server/utils/ai.ts b/apps/github-bot/server/utils/ai.ts
index f02c31f..6b8226c 100644
--- a/apps/github-bot/server/utils/ai.ts
+++ b/apps/github-bot/server/utils/ai.ts
@@ -1,7 +1,83 @@
-import { ToolLoopAgent } from 'ai'
+import { createGateway } from '@ai-sdk/gateway'
+import { generateText, Output, stepCountIs, ToolLoopAgent } from 'ai'
import { log } from 'evlog'
import { createSavoir } from '@savoir/sdk'
import type { IssueContext } from './types'
+import { type AgentConfig, agentConfigSchema, getDefaultConfig } from './router-schema'
+
+const ROUTER_MODEL = 'google/gemini-2.5-flash-lite'
+
+const ROUTER_SYSTEM_PROMPT = `You are a question classifier for a documentation assistant bot.
+Analyze the user's question and determine the appropriate configuration for the agent.
+
+## Classification Guidelines
+
+**trivial** (maxSteps: 3, model: gemini-2.5-flash-lite)
+- Simple greetings: "Hello", "Thanks", "Hi there"
+- Acknowledgments without questions
+
+**simple** (maxSteps: 6, model: gemini-2.5-flash-lite)
+- Single concept lookups: "What is useAsyncData?", "How to use useFetch?"
+- Direct API questions with clear answers
+
+**moderate** (maxSteps: 12, model: gemini-3-flash)
+- Comparisons: "Difference between useFetch and useAsyncData?"
+- Integration questions: "How to use Nuxt with TypeScript?"
+- Questions requiring multiple file searches
+
+**complex** (maxSteps: 20, model: gemini-3-flash or claude-opus-4.5)
+- Debugging scenarios: "My middleware isn't working with auth"
+- Architecture questions: "How to structure a multi-package monorepo?"
+- Deep analysis requiring extensive documentation review
+
+Use claude-opus-4.5 only for the most complex cases requiring deep reasoning.`
+
+function buildRouterInput(question: string, context?: IssueContext): string {
+ const parts: string[] = []
+
+ if (context) {
+ parts.push(`Repository: ${context.owner}/${context.repo}`)
+ parts.push(`Issue #${context.number}: ${context.title}`)
+ if (context.body) {
+ parts.push(`Issue body: ${context.body.slice(0, 500)}`)
+ }
+ if (context.labels.length) {
+ parts.push(`Labels: ${context.labels.join(', ')}`)
+ }
+ }
+
+ parts.push(`Question: ${question}`)
+
+ return parts.join('\n')
+}
+
+async function routeQuestion(question: string, context?: IssueContext): Promise<AgentConfig> {
+ const config = useRuntimeConfig()
+ const gateway = createGateway({ apiKey: config.savoir.apiKey })
+
+ try {
+ const { output } = await generateText({
+ model: gateway(ROUTER_MODEL),
+ output: Output.object({ schema: agentConfigSchema }),
+ messages: [
+ { role: 'system', content: ROUTER_SYSTEM_PROMPT },
+ { role: 'user', content: buildRouterInput(question, context) },
+ ],
+ })
+
+ if (!output) {
+ log.warn('github-bot', 'Router returned no output, using default config')
+ return getDefaultConfig()
+ }
+
+ log.info('github-bot', `Router decision: ${output.complexity} (${output.model}, ${output.maxSteps} steps) - ${output.reasoning}`)
+ return output
+ } catch (error) {
+ const errorMessage = error instanceof Error ? error.message : 'Unknown error'
+ log.error('github-bot', `Router failed: ${errorMessage}, using default config`)
+ return getDefaultConfig()
+ }
+}
export async function generateAIResponse(
question: string,
@@ -26,7 +102,9 @@ Once configured, I'll be able to search the documentation and help answer your q
let toolCallCount = 0
try {
- log.info('github-bot', `Starting agent for issue #${context?.number || 'unknown'}`)
+ const agentConfig = await routeQuestion(question, context)
+
+ log.info('github-bot', `Starting agent for issue #${context?.number || 'unknown'} with ${agentConfig.complexity} complexity`)
const savoir = createSavoir({
apiUrl: config.savoir.apiUrl,
@@ -39,9 +117,10 @@ Once configured, I'll be able to search the documentation and help answer your q
})
const agent = new ToolLoopAgent({
- model: 'google/gemini-3-flash',
- instructions: buildSystemPrompt(context),
+ model: agentConfig.model,
+ instructions: buildSystemPrompt(context, agentConfig),
tools: savoir.tools,
+ stopWhen: stepCountIs(agentConfig.maxSteps),
onStepFinish: ({ usage, toolCalls }) => {
stepCount++
totalInputTokens += usage.inputTokens || 0
@@ -91,7 +170,7 @@ Please try again later or open a discussion if this persists.`
}
}
-function buildSystemPrompt(context?: IssueContext): string {
+function buildSystemPrompt(context?: IssueContext, agentConfig?: AgentConfig): string {
const basePrompt = `You are a documentation assistant with bash access to a sandbox containing docs (markdown, JSON, YAML).
## Speed is Important
@@ -121,13 +200,23 @@ ls docs/ && grep -rl "keyword" docs/ --include="*.md" | head -10
- Include code examples
- Use markdown`
- if (context) {
- return `${basePrompt}
+ let prompt = basePrompt
-Issue #${context.number}: "${context.title}" in ${context.owner}/${context.repo}`
+ if (agentConfig) {
+ const complexityHints: Record<AgentConfig['complexity'], string> = {
+ trivial: 'This is a simple greeting or acknowledgment. Respond briefly without searching.',
+ simple: 'This is a straightforward question. One or two searches should suffice.',
+ moderate: 'This requires some research. Take time to find relevant documentation.',
+ complex: 'This is a complex question. Thoroughly search the documentation and provide a detailed answer.',
+ }
+ prompt += `\n\n## Task Complexity: ${agentConfig.complexity}\n${complexityHints[agentConfig.complexity]}`
+ }
+
+ if (context) {
+ prompt += `\n\nIssue #${context.number}: "${context.title}" in ${context.owner}/${context.repo}`
}
- return basePrompt
+ return prompt
}
function buildUserMessage(question: string, context?: IssueContext): string {
diff --git a/apps/github-bot/server/utils/router-schema.ts b/apps/github-bot/server/utils/router-schema.ts
new file mode 100644
index 0000000..185f898
--- /dev/null
+++ b/apps/github-bot/server/utils/router-schema.ts
@@ -0,0 +1,29 @@
+import { z } from 'zod'
+
+export const agentConfigSchema = z.object({
+ complexity: z.enum(['trivial', 'simple', 'moderate', 'complex'])
+ .describe('trivial=greeting, simple=single lookup, moderate=multi-search, complex=deep analysis'),
+
+ maxSteps: z.number().min(1).max(30)
+ .describe('Agent iterations: 3 trivial, 6 simple, 12 moderate, 20 complex'),
+
+ model: z.enum([
+ 'google/gemini-3-flash',
+ 'google/gemini-2.5-flash-lite',
+ 'anthropic/claude-opus-4.5',
+ ]).describe('flash-lite for trivial/simple, flash for moderate, opus for complex'),
+
+ reasoning: z.string().max(200)
+ .describe('Brief explanation of the classification'),
+})
+
+export type AgentConfig = z.infer<typeof agentConfigSchema>
+
+export function getDefaultConfig(): AgentConfig {
+ return {
+ complexity: 'moderate',
+ maxSteps: 12,
+ model: 'google/gemini-3-flash',
+ reasoning: 'Default fallback configuration',
+ }
+}
diff --git a/packages/sdk/src/index.ts b/packages/sdk/src/index.ts
index 67b5c57..6e6c909 100644
--- a/packages/sdk/src/index.ts
+++ b/packages/sdk/src/index.ts
@@ -2,7 +2,7 @@ import { SavoirClient } from './client'
import { createBashBatchTool, createBashTool } from './tools'
import type { SavoirConfig } from './types'
-export type { SavoirConfig, ShellResponse, ShellBatchResponse, ShellCommandResult, SyncOptions, SyncResponse, SnapshotResponse, GitHubSource, YouTubeSource, SourcesResponse, SyncSourceResponse, ToolCallInfo, ToolCallCallback, ToolCallState } from './types'
+export type { SavoirConfig, ShellResponse, ShellBatchResponse, ShellCommandResult, SyncOptions, SyncResponse, SnapshotResponse, GitHubSource, YouTubeSource, SourcesResponse, SyncSourceResponse, ToolCallInfo, ToolCallCallback, ToolCallState, ToolExecutionResult, CommandResult } from './types'
export { SavoirError, NetworkError } from './errors'
export { SavoirClient } from './client'
diff --git a/packages/sdk/src/tools/shell.ts b/packages/sdk/src/tools/shell.ts
index de7ab05..1671c1f 100644
--- a/packages/sdk/src/tools/shell.ts
+++ b/packages/sdk/src/tools/shell.ts
@@ -1,7 +1,7 @@
import { tool } from 'ai'
import { z } from 'zod'
import type { SavoirClient } from '../client'
-import type { ToolCallCallback } from '../types'
+import type { ToolCallCallback, ToolExecutionResult, CommandResult } from '../types'
/**
* Create the bash tool for AI SDK - runs a single command in the sandbox
@@ -22,19 +22,57 @@ Use standard Unix commands to explore and read files.`,
})
},
execute: async ({ command }, { toolCallId }) => {
- const result = await client.bash(command)
+ const startTime = Date.now()
- onToolCall?.({
- toolCallId,
- toolName: 'bash',
- args: { command },
- state: 'done',
- })
+ try {
+ const apiResult = await client.bash(command)
+ const durationMs = Date.now() - startTime
+
+ const commandResult: CommandResult = {
+ command,
+ stdout: apiResult.stdout,
+ stderr: apiResult.stderr,
+ exitCode: apiResult.exitCode,
+ success: apiResult.exitCode === 0,
+ }
+
+ const executionResult: ToolExecutionResult = {
+ commands: [commandResult],
+ success: commandResult.success,
+ durationMs,
+ }
+
+ onToolCall?.({
+ toolCallId,
+ toolName: 'bash',
+ args: { command },
+ state: 'done',
+ result: executionResult,
+ })
+
+ return {
+ stdout: apiResult.stdout,
+ stderr: apiResult.stderr,
+ exitCode: apiResult.exitCode,
+ }
+ } catch (error) {
+ const durationMs = Date.now() - startTime
+ const errorMessage = error instanceof Error ? error.message : 'Unknown error'
+
+ onToolCall?.({
+ toolCallId,
+ toolName: 'bash',
+ args: { command },
+ state: 'error',
+ result: {
+ commands: [{ command, stdout: '', stderr: errorMessage, exitCode: 1, success: false }],
+ success: false,
+ durationMs,
+ error: errorMessage,
+ },
+ })
- return {
- stdout: result.stdout,
- stderr: result.stderr,
- exitCode: result.exitCode,
+ throw error
}
},
})
@@ -61,22 +99,60 @@ Maximum 10 commands per batch.`,
})
},
execute: async ({ commands }, { toolCallId }) => {
- const result = await client.bashBatch(commands)
+ const startTime = Date.now()
- onToolCall?.({
- toolCallId,
- toolName: 'bash_batch',
- args: { commands },
- state: 'done',
- })
+ try {
+ const apiResult = await client.bashBatch(commands)
+ const durationMs = Date.now() - startTime
- return {
- results: result.results.map(r => ({
+ const commandResults: CommandResult[] = apiResult.results.map(r => ({
command: r.command,
stdout: r.stdout,
stderr: r.stderr,
exitCode: r.exitCode,
- })),
+ success: r.exitCode === 0,
+ }))
+
+ const executionResult: ToolExecutionResult = {
+ commands: commandResults,
+ success: commandResults.every(r => r.success),
+ durationMs,
+ }
+
+ onToolCall?.({
+ toolCallId,
+ toolName: 'bash_batch',
+ args: { commands },
+ state: 'done',
+ result: executionResult,
+ })
+
+ return {
+ results: apiResult.results.map(r => ({
+ command: r.command,
+ stdout: r.stdout,
+ stderr: r.stderr,
+ exitCode: r.exitCode,
+ })),
+ }
+ } catch (error) {
+ const durationMs = Date.now() - startTime
+ const errorMessage = error instanceof Error ? error.message : 'Unknown error'
+
+ onToolCall?.({
+ toolCallId,
+ toolName: 'bash_batch',
+ args: { commands },
+ state: 'error',
+ result: {
+ commands: commands.map(cmd => ({ command: cmd, stdout: '', stderr: '', exitCode: 1, success: false })),
+ success: false,
+ durationMs,
+ error: errorMessage,
+ },
+ })
+
+ throw error
}
},
})
diff --git a/packages/sdk/src/types.ts b/packages/sdk/src/types.ts
index 74e78ca..ae60d22 100644
--- a/packages/sdk/src/types.ts
+++ b/packages/sdk/src/types.ts
@@ -29,7 +29,32 @@ export interface SavoirConfig {
/**
* Tool call state
*/
-export type ToolCallState = 'loading' | 'done'
+export type ToolCallState = 'loading' | 'done' | 'error'
+
+/**
+ * Result of a single command execution
+ */
+export interface CommandResult {
+ command: string
+ stdout: string
+ stderr: string
+ exitCode: number
+ success: boolean
+}
+
+/**
+ * Tool execution result (available when state is 'done' or 'error')
+ */
+export interface ToolExecutionResult {
+ /** Results for each command (single item for bash, multiple for bash_batch) */
+ commands: CommandResult[]
+ /** Whether all commands succeeded */
+ success: boolean
+ /** Execution duration in milliseconds */
+ durationMs: number
+ /** Error message if state is 'error' */
+ error?: string
+}
/**
* Tool call information passed to onToolCall callback
@@ -39,6 +64,8 @@ export interface ToolCallInfo {
toolName: string
args: Record<string, unknown>
state: ToolCallState
+ /** Execution result (only available when state is 'done' or 'error') */
+ result?: ToolExecutionResult
}
/**