Open source file-system and knowledge based agent template. Build AI agents that stay up to date with your knowledge base
improve agent creation
Hugo Richard5c32e5561157
diff --git a/apps/chat/server/api/chats/[id].post.ts b/apps/chat/server/api/chats/[id].post.tsindex 3d9c299..bada84d 100644--- a/apps/chat/server/api/chats/[id].post.ts+++ b/apps/chat/server/api/chats/[id].post.ts@@ -1,4 +1,4 @@-import { convertToModelMessages, createUIMessageStream, createUIMessageStreamResponse, stepCountIs, ToolLoopAgent, type UIMessage } from 'ai'+import { convertToModelMessages, createUIMessageStream, createUIMessageStreamResponse, type UIMessage } from 'ai'import { z } from 'zod'import { db, schema } from '@nuxthub/db'import { kv } from '@nuxthub/kv'@@ -7,11 +7,11 @@ import { createSavoir } from '@savoir/sdk'import { log, useLogger } from 'evlog'import { generateTitle } from '../../utils/chat/generate-title'import { routeQuestion } from '../../utils/router/route-question'-import { getAgentConfig } from '../../utils/agent-config'import { KV_KEYS } from '../../utils/sandbox/types'import { adminTools } from '../../utils/chat/admin-tools'import { ADMIN_SYSTEM_PROMPT, buildChatSystemPrompt } from '../../utils/prompts/chat'import { applyComplexity } from '../../utils/prompts/shared'+import { createAgent, type RoutingResult } from '../../utils/create-agent'defineRouteMeta({openAPI: {@@ -121,24 +121,86 @@ export default defineEventHandler(async (event) => {},})- const [routerConfig, agentConfigData] = await Promise.all([- routeQuestion(messages, requestId),- getAgentConfig(),- ])-- const effectiveMaxSteps = isAdminChat- ? 15 // Admin tools are faster, fewer steps needed- : Math.round(routerConfig.maxSteps * agentConfigData.maxStepsMultiplier)-- const effectiveModel = agentConfigData.defaultModel || model-- const dynamicSystemPrompt = isAdminChat- ? ADMIN_SYSTEM_PROMPT- : buildChatSystemPrompt(agentConfigData)+ let routingResult: RoutingResult | undefined+ let effectiveModel = model++ const agent = createAgent({+ tools: savoir.tools,+ route: () => routeQuestion(messages, requestId),+ buildPrompt: (routerConfig, agentConfig) => applyComplexity(buildChatSystemPrompt(agentConfig), routerConfig),+ resolveModel: (_, agentConfig) => agentConfig.defaultModel || model,+ admin: isAdminChat+ ? { tools: adminTools, systemPrompt: ADMIN_SYSTEM_PROMPT }+ : undefined,+ onRouted: (routed) => {+ routingResult = routed+ const { effectiveModel: routedModel, effectiveMaxSteps, routerConfig, agentConfig } = routed+ effectiveModel = routedModel+ log.info('chat', `[${requestId}] Starting agent [${chat.mode}] with ${routedModel} (routed: ${routerConfig.complexity}, ${effectiveMaxSteps} steps, multiplier: ${agentConfig.maxStepsMultiplier}x)`)+ },+ onStepFinish: (stepResult) => {+ const stepDurationMs = Date.now() - stepStartTime+ stepDurations.push(stepDurationMs)+ stepCount++++ if (stepResult.usage) {+ totalInputTokens += stepResult.usage.inputTokens ?? 0+ totalOutputTokens += stepResult.usage.outputTokens ?? 0+ }- const effectiveTools = isAdminChat ? adminTools : savoir.tools+ if (stepResult.toolCalls && stepResult.toolCalls.length > 0) {+ toolCallCount += stepResult.toolCalls.length+ const tools = stepResult.toolCalls.map((c: { toolName: string }) => c.toolName).join(', ')+ log.info('chat', `[${requestId}] Step ${stepCount}: ${tools} (${stepDurationMs}ms)`)++ // Emit tool call events for admin tools (SDK tools handle this via onToolCall)+ if (isAdminChat && streamWriter) {+ for (const tc of stepResult.toolCalls) {+ streamWriter.write({+ type: 'data-tool-call',+ id: tc.toolCallId,+ data: {+ toolCallId: tc.toolCallId,+ toolName: tc.toolName,+ args: tc.args,+ state: 'done',+ result: {+ success: true,+ durationMs: stepDurationMs,+ commands: [],+ },+ },+ })+ }+ }+ } else {+ log.info('chat', `[${requestId}] Step ${stepCount}: response (${stepDurationMs}ms)`)+ }- log.info('chat', `[${requestId}] Starting agent [${chat.mode}] with ${effectiveModel} (routed: ${routerConfig.complexity}, ${effectiveMaxSteps} steps, multiplier: ${agentConfigData.maxStepsMultiplier}x)`)+ 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,+ ...(routingResult && {+ routerComplexity: routingResult.routerConfig.complexity,+ routerMaxSteps: routingResult.routerConfig.maxSteps,+ effectiveMaxSteps: routingResult.effectiveMaxSteps,+ stepsMultiplier: routingResult.agentConfig.maxStepsMultiplier,+ routerReasoning: routingResult.routerConfig.reasoning,+ }),+ })+ log.info('chat', `[${requestId}] Finished: ${result.finishReason} (total: ${totalDurationMs}ms)`)+ },+ })const requestStartTime = Date.now()@@ -156,79 +218,6 @@ export default defineEventHandler(async (event) => {execute: async ({ writer }) => {streamWriter = writer- const agent = new ToolLoopAgent({- model: effectiveModel,- instructions: isAdminChat ? dynamicSystemPrompt : applyComplexity(dynamicSystemPrompt, routerConfig),- tools: effectiveTools,- stopWhen: stepCountIs(effectiveMaxSteps),- prepareStep: isAdminChat ? undefined : ({ stepNumber }) => {- // Remove tools on the last step to force text output- if (stepNumber >= effectiveMaxSteps - 1) {- return { activeTools: [] }- }- },- 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)`)-- // Emit tool call events for admin tools (SDK tools handle this via onToolCall)- if (isAdminChat && streamWriter) {- for (const tc of stepResult.toolCalls) {- streamWriter.write({- type: 'data-tool-call',- id: tc.toolCallId,- data: {- toolCallId: tc.toolCallId,- toolName: tc.toolName,- args: tc.args,- state: 'done',- result: {- success: true,- durationMs: stepDurationMs,- commands: [],- },- },- })- }- }- } else {- log.info('chat', `[${requestId}] Step ${stepCount}: response (${stepDurationMs}ms)`)- }-- 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,- routerComplexity: routerConfig.complexity,- routerMaxSteps: routerConfig.maxSteps,- effectiveMaxSteps,- stepsMultiplier: agentConfigData.maxStepsMultiplier,- routerReasoning: routerConfig.reasoning,- })- log.info('chat', `[${requestId}] Finished: ${result.finishReason} (total: ${totalDurationMs}ms)`)- },- })-if (!chat.title && messages[0]) {generateTitle({firstMessage: messages[0],diff --git a/apps/chat/server/utils/bot/ai.ts b/apps/chat/server/utils/bot/ai.tsindex a6eeff7..a88ce1c 100644--- a/apps/chat/server/utils/bot/ai.ts+++ b/apps/chat/server/utils/bot/ai.ts@@ -1,13 +1,12 @@-import { generateText, Output, stepCountIs, ToolLoopAgent } from 'ai'+import { generateText, Output } from 'ai'import { log } from 'evlog'-import { type AgentConfig, agentConfigSchema, getDefaultConfig } from '../router/schema'+import { type AgentConfig, agentConfigSchema, getDefaultConfig, ROUTER_MODEL } from '../router/schema'import { ROUTER_SYSTEM_PROMPT } from '../prompts/router'import { buildBotSystemPrompt, buildBotUserMessage } from '../prompts/bot'+import { createAgent } from '../create-agent'import { createInternalSavoir } from './savoir'import type { ThreadContext } from './types'-const ROUTER_MODEL = 'google/gemini-2.5-flash-lite'-function buildRouterInput(question: string, context?: ThreadContext): string {const parts: string[] = []@@ -73,32 +72,11 @@ export async function generateAIResponse(},})- const [routerConfig, savoirConfig] = await Promise.all([- routeQuestion(question, context),- savoir.getAgentConfig().catch((error) => {- log.warn('bot', `Failed to fetch agent config: ${error instanceof Error ? error.message : 'Unknown error'}`)- return null- }),- ])-- const effectiveMaxSteps = savoirConfig- ? Math.round(routerConfig.maxSteps * savoirConfig.maxStepsMultiplier)- : routerConfig.maxSteps- const effectiveModel = savoirConfig?.defaultModel || routerConfig.model-- log.info('bot', `Starting agent for #${context?.number || 'unknown'} with ${routerConfig.complexity} complexity (${effectiveMaxSteps} steps, multiplier: ${savoirConfig?.maxStepsMultiplier || 1}x)`)-- const agent = new ToolLoopAgent({- model: effectiveModel,- instructions: buildBotSystemPrompt(context, routerConfig, savoirConfig),+ const agent = createAgent({tools: savoir.tools,- stopWhen: stepCountIs(effectiveMaxSteps),- prepareStep: ({ stepNumber }) => {- // Remove tools on the last step to force text output- if (stepNumber >= effectiveMaxSteps - 1) {- return { activeTools: [] }- }- },+ route: () => routeQuestion(question, context),+ buildPrompt: (routerConfig, agentConfig) => buildBotSystemPrompt(context, routerConfig, agentConfig),+ resolveModel: (routerConfig, agentConfig) => agentConfig.defaultModel || routerConfig.model,})const result = await agent.generate({@@ -147,4 +125,3 @@ ${errorMessage}Please try again later or open a discussion if this persists.`}}-diff --git a/apps/chat/server/utils/create-agent.ts b/apps/chat/server/utils/create-agent.tsnew file mode 100644index 0000000..df574dc--- /dev/null+++ b/apps/chat/server/utils/create-agent.ts@@ -0,0 +1,90 @@+import { stepCountIs, ToolLoopAgent } from 'ai'+import type { AgentConfig } from './router/schema'+import { DEFAULT_MODEL } from './router/schema'+import type { AgentConfigData } from './agent-config'+import { getAgentConfig } from './agent-config'++export interface RoutingResult {+ routerConfig: AgentConfig+ agentConfig: AgentConfigData+ effectiveModel: string+ effectiveMaxSteps: number+}++interface CreateAgentOptions {+ tools: Record<string, any>+ route: () => Promise<AgentConfig>+ buildPrompt: (routerConfig: AgentConfig, agentConfig: AgentConfigData) => string+ resolveModel?: (routerConfig: AgentConfig, agentConfig: AgentConfigData) => string+ admin?: {+ tools: Record<string, any>+ systemPrompt: string+ maxSteps?: number+ }+ onRouted?: (result: RoutingResult) => void+ onStepFinish?: (stepResult: any) => void+ onFinish?: (result: any) => void+}++export function createAgent({+ tools,+ route,+ buildPrompt,+ resolveModel,+ admin,+ onRouted,+ onStepFinish,+ onFinish,+}: CreateAgentOptions) {+ let isAdminMode = false+ let maxSteps = 15++ return new ToolLoopAgent({+ model: DEFAULT_MODEL,+ prepareCall: async ({ ...settings }) => {+ if (admin) {+ isAdminMode = true+ maxSteps = admin.maxSteps ?? 15+ return {+ ...settings,+ instructions: admin.systemPrompt,+ tools: admin.tools,+ stopWhen: stepCountIs(maxSteps),+ }+ }++ isAdminMode = false++ const [routerConfig, agentConfig] = await Promise.all([+ route(),+ getAgentConfig(),+ ])++ const effectiveMaxSteps = Math.round(routerConfig.maxSteps * agentConfig.maxStepsMultiplier)+ const effectiveModel = resolveModel?.(routerConfig, agentConfig)+ ?? agentConfig.defaultModel+ ?? DEFAULT_MODEL++ maxSteps = effectiveMaxSteps++ onRouted?.({ routerConfig, agentConfig, effectiveModel, effectiveMaxSteps })++ return {+ ...settings,+ model: effectiveModel,+ instructions: buildPrompt(routerConfig, agentConfig),+ tools,+ stopWhen: stepCountIs(effectiveMaxSteps),+ }+ },+ prepareStep: ({ stepNumber }) => {+ if (isAdminMode) return+ // Remove tools on the last step to force text output+ if (stepNumber >= maxSteps - 1) {+ return { activeTools: [] }+ }+ },+ onStepFinish,+ onFinish,+ })+}diff --git a/apps/chat/server/utils/router/route-question.ts b/apps/chat/server/utils/router/route-question.tsindex 8235297..2480237 100644--- a/apps/chat/server/utils/router/route-question.ts+++ b/apps/chat/server/utils/router/route-question.ts@@ -3,9 +3,7 @@ import { generateText, Output } from 'ai'import { log } from 'evlog'import type { UIMessage } from 'ai'import { ROUTER_SYSTEM_PROMPT } from '../prompts/router'-import { type AgentConfig, agentConfigSchema, getDefaultConfig } from './schema'--const ROUTER_MODEL = 'google/gemini-2.5-flash-lite'+import { type AgentConfig, agentConfigSchema, getDefaultConfig, ROUTER_MODEL } from './schema'function extractQuestionFromMessages(messages: UIMessage[]): string {const lastUserMessage = [...messages].reverse().find(m => m.role === 'user')diff --git a/apps/chat/server/utils/router/schema.ts b/apps/chat/server/utils/router/schema.tsindex ed3f0ac..4c32175 100644--- a/apps/chat/server/utils/router/schema.ts+++ b/apps/chat/server/utils/router/schema.ts@@ -1,5 +1,8 @@import { z } from 'zod'+export const ROUTER_MODEL = 'google/gemini-2.5-flash-lite'+export const DEFAULT_MODEL = 'google/gemini-3-flash'+export const agentConfigSchema = z.object({complexity: z.enum(['trivial', 'simple', 'moderate', 'complex']).describe('trivial=greeting, simple=single lookup, moderate=multi-search, complex=deep analysis'),