Open source file-system and knowledge based agent template. Build AI agents that stay up to date with your knowledge base
import { tool } from 'ai'
import { z } from 'zod'
import { db, schema } from '@nuxthub/db'
import { desc, eq, and, sql } from 'drizzle-orm'
import { preview, cmd } from './_preview'
export const queryChatsTool = tool({
description: `Query recent chats to understand what users are asking about.
Use this to find popular topics, identify common questions, or debug user issues.`,
inputSchema: z.object({
limit: z.number().min(1).max(50).default(20).describe('Number of recent chats to return'),
userId: z.string().optional().describe('Filter chats by a specific user ID'),
}),
execute: async function* ({ limit, userId }) {
const label = 'Query chats'
yield { status: 'loading' as const, commands: [cmd(label, '')] }
const start = Date.now()
const conditions = userId
? and(eq(schema.chats.userId, userId))
: undefined
const chats = await db.query.chats.findMany({
where: () => conditions ?? sql`1=1`,
orderBy: () => desc(schema.chats.createdAt),
limit,
with: {
messages: {
limit: 1,
orderBy: () => schema.messages.createdAt,
},
},
})
const chatsData = chats.map(c => ({
id: c.id,
title: c.title,
mode: c.mode,
userId: c.userId,
createdAt: c.createdAt,
messageCount: c.messages?.length ?? 0,
}))
yield {
status: 'done' as const,
commands: [cmd(label, preview(chatsData))],
durationMs: Date.now() - start,
chats: chats.map(c => ({
id: c.id,
title: c.title,
mode: c.mode,
userId: c.userId,
createdAt: c.createdAt,
messageCount: c.messages?.length ?? 0,
firstMessage: c.messages?.[0]?.parts
? JSON.stringify(c.messages[0].parts).slice(0, 200)
: null,
})),
}
},
})