Skip to content

Commit 691fee0

Browse files
authored
Fix chat list by project (#128)
Add proper filtering per project
1 parent deeb03b commit 691fee0

File tree

3 files changed

+29
-5
lines changed

3 files changed

+29
-5
lines changed

apps/dbagent/src/app/api/chat/route.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { generateTitleFromUserMessage } from '~/app/(main)/projects/[project]/ch
55
import { generateUUID } from '~/components/chat/utils';
66
import { getChatSystemPrompt, getModelInstance } from '~/lib/ai/agent';
77
import { getTools } from '~/lib/ai/tools';
8-
import { deleteChatById, getChatById, getChats, saveMessages, updateChat } from '~/lib/db/chats';
8+
import { deleteChatById, getChatById, getChatsByProject, saveMessages, updateChat } from '~/lib/db/chats';
99
import { getConnection } from '~/lib/db/connections';
1010
import { getUserSessionDBAccess } from '~/lib/db/db';
1111
import { getProjectById } from '~/lib/db/projects';
@@ -16,11 +16,18 @@ export const maxDuration = 60;
1616

1717
export async function GET(request: NextRequest) {
1818
const { searchParams } = new URL(request.url);
19+
20+
const project = searchParams.get('project');
21+
if (!project) {
22+
return new Response('Project is required', { status: 400 });
23+
}
24+
1925
const limit = parseInt(searchParams.get('limit') || '10', 10);
26+
const offset = parseInt(searchParams.get('offset') || '0', 10);
2027

2128
const dbAccess = await getUserSessionDBAccess();
2229

23-
const chats = await getChats(dbAccess, { limit });
30+
const chats = await getChatsByProject(dbAccess, { project, limit, offset });
2431

2532
return Response.json({ chats });
2633
}

apps/dbagent/src/components/ui/side-nav.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ export function SideNav({ className, project, onboardingComplete }: SideNavProps
7272
const queryClient = useQueryClient();
7373
const { data: { chats = [] } = {} } = useQuery<{ chats: Chat[] }>({
7474
queryKey: ['chats'],
75-
queryFn: () => fetcher('/api/chat')
75+
queryFn: () => fetcher(`/api/chat?project=${project.id}`)
7676
});
7777

7878
const isActive = (path: string) => {

apps/dbagent/src/lib/db/chats.ts

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,26 @@ export async function deleteChatById(dbAccess: DBAccess, { id }: { id: string })
4141
});
4242
}
4343

44-
export async function getChats(dbAccess: DBAccess, { limit = 100, offset = 0 }: { limit?: number; offset?: number }) {
44+
export async function getChatsByProject(
45+
dbAccess: DBAccess,
46+
{
47+
project,
48+
limit = 100,
49+
offset = 0
50+
}: {
51+
project: string;
52+
limit?: number;
53+
offset?: number;
54+
}
55+
) {
4556
return dbAccess.query(async ({ db }) => {
46-
return await db.select().from(chats).orderBy(desc(chats.createdAt)).limit(limit).offset(offset);
57+
return await db
58+
.select()
59+
.from(chats)
60+
.where(eq(chats.projectId, project))
61+
.orderBy(desc(chats.createdAt))
62+
.limit(limit)
63+
.offset(offset);
4764
});
4865
}
4966

0 commit comments

Comments
 (0)