Skip to content

armgit / alook

Public Imported from GitHub
0

Multi-Agent Collaboration for Team and Friends

feat: add indexes for chat init

Gusb78bdbaf67c1
diff --git a/src/shared/src/db/queries/artifact.ts b/src/shared/src/db/queries/artifact.ts
index 1dea3b649..191d21da1 100644
--- a/src/shared/src/db/queries/artifact.ts
+++ b/src/shared/src/db/queries/artifact.ts
@@ -25,7 +25,7 @@ export async function listArtifactsByConversation(
db: Database,
conversationId: string,
workspaceId: string,
- opts?: { source?: string },
+ opts?: { source?: string; limit?: number },
) {
const conditions = [
eq(artifact.conversationId, conversationId),
@@ -34,11 +34,15 @@ export async function listArtifactsByConversation(
if (opts?.source) {
conditions.push(eq(artifact.source, opts.source));
}
- return db
+ let query = db
.select()
.from(artifact)
.where(and(...conditions))
.orderBy(desc(artifact.createdAt));
+ if (opts?.limit != null) {
+ query = query.limit(opts.limit) as typeof query;
+ }
+ return query;
}
export async function getArtifact(db: Database, id: string, workspaceId: string) {
diff --git a/src/shared/src/db/queries/conversation.ts b/src/shared/src/db/queries/conversation.ts
index f1f2bfada..fb5f84d87 100644
--- a/src/shared/src/db/queries/conversation.ts
+++ b/src/shared/src/db/queries/conversation.ts
@@ -1,4 +1,4 @@
-import { eq, and, desc, count, ne, lt } from "drizzle-orm";
+import { eq, and, desc, count, ne, lt, sql } from "drizzle-orm";
import { conversation, message } from "../schema";
import type { Database } from "../index";
import { TASK_TYPES, type TaskType } from "../../constants";
@@ -183,3 +183,29 @@ export async function listPreviousConversations(
.orderBy(desc(conversation.createdAt))
.limit(limit);
}
+
+export async function hasPreviousConversations(
+ db: Database,
+ workspaceId: string,
+ userId: string,
+ agentId: string,
+ excludeId: string,
+ channel?: string,
+): Promise<boolean> {
+ const conditions = [
+ eq(conversation.workspaceId, workspaceId),
+ eq(conversation.userId, userId),
+ eq(conversation.agentId, agentId),
+ eq(conversation.type, TASK_TYPES.USER_DM_MESSAGE),
+ ne(conversation.id, excludeId),
+ ];
+ if (channel) {
+ conditions.push(eq(conversation.channel, channel));
+ }
+ const rows = await db
+ .select({ exists: sql<number>`1` })
+ .from(conversation)
+ .where(and(...conditions))
+ .limit(1);
+ return rows.length > 0;
+}
diff --git a/src/shared/src/db/schema.ts b/src/shared/src/db/schema.ts
index d0d6e83c6..4f5b887fe 100644
--- a/src/shared/src/db/schema.ts
+++ b/src/shared/src/db/schema.ts
@@ -300,6 +300,8 @@ export const conversation = sqliteTable(
createdAt: text("created_at").notNull().$defaultFn(() => new Date().toISOString()),
},
(t) => [
+ index("idx_conversation_agent_lookup")
+ .on(t.workspaceId, t.agentId, t.userId, t.type, t.channel, t.createdAt),
foreignKey({
columns: [t.agentId, t.workspaceId],
foreignColumns: [agent.id, agent.workspaceId],
@@ -363,6 +365,8 @@ export const agentTaskQueue = sqliteTable(
.where(sql`status IN ('queued', 'dispatched', 'running')`),
index("idx_task_queue_agent_history")
.on(t.agentId, t.workspaceId, t.createdAt),
+ index("idx_task_queue_conversation_status")
+ .on(t.conversationId, t.status),
foreignKey({
columns: [t.agentId, t.workspaceId],
foreignColumns: [agent.id, agent.workspaceId],
diff --git a/src/web/migrations/0020_chat_init_indexes.sql b/src/web/migrations/0020_chat_init_indexes.sql
new file mode 100644
index 000000000..17584979f
--- /dev/null
+++ b/src/web/migrations/0020_chat_init_indexes.sql
@@ -0,0 +1,7 @@
+-- Composite index for conversation agent lookups (getOrCreateAgentConversation, listPreviousConversations)
+CREATE INDEX IF NOT EXISTS idx_conversation_agent_lookup
+ ON conversation (workspace_id, agent_id, user_id, type, channel, created_at);
+
+-- Index for active task lookup by conversation (getActiveTaskByConversation)
+CREATE INDEX IF NOT EXISTS idx_task_queue_conversation_status
+ ON agent_task_queue (conversation_id, status);
diff --git a/src/web/src/app/api/agents/[id]/chat-init/route.test.ts b/src/web/src/app/api/agents/[id]/chat-init/route.test.ts
index ec5a41e87..aaadb3888 100644
--- a/src/web/src/app/api/agents/[id]/chat-init/route.test.ts
+++ b/src/web/src/app/api/agents/[id]/chat-init/route.test.ts
@@ -6,7 +6,7 @@ vi.mock("@opennextjs/cloudflare", () => ({
const mockGetAgent = vi.fn();
const mockGetOrCreateAgentConversation = vi.fn();
-const mockListPreviousConversations = vi.fn();
+const mockHasPreviousConversations = vi.fn();
const mockListMessages = vi.fn();
const mockListArtifactsByConversation = vi.fn();
const mockListBufferedMessages = vi.fn();
@@ -34,8 +34,8 @@ vi.mock("@alook/shared", () => ({
conversation: {
getOrCreateAgentConversation: (...args: unknown[]) =>
mockGetOrCreateAgentConversation(...args),
- listPreviousConversations: (...args: unknown[]) =>
- mockListPreviousConversations(...args),
+ hasPreviousConversations: (...args: unknown[]) =>
+ mockHasPreviousConversations(...args),
},
message: {
listMessages: (...args: unknown[]) => mockListMessages(...args),
@@ -122,7 +122,7 @@ const CONV = {
function setupDefaults() {
mockGetAgent.mockResolvedValue({ id: "a1", name: "Agent" });
mockGetOrCreateAgentConversation.mockResolvedValue(CONV);
- mockListPreviousConversations.mockResolvedValue([]);
+ mockHasPreviousConversations.mockResolvedValue(false);
mockListMessages.mockResolvedValue([]);
mockListArtifactsByConversation.mockResolvedValue([]);
mockListBufferedMessages.mockResolvedValue([]);
@@ -152,7 +152,7 @@ describe("POST /api/agents/[id]/chat-init", () => {
mockGetAgent.mockResolvedValue({ id: "a1", name: "Agent" });
mockGetOrCreateAgentConversation.mockResolvedValue(CONV);
- mockListPreviousConversations.mockResolvedValue([]);
+ mockHasPreviousConversations.mockResolvedValue(false);
mockListMessages.mockResolvedValue([msg]);
mockListArtifactsByConversation.mockResolvedValue([artifact]);
mockListBufferedMessages.mockResolvedValue([]);
@@ -171,8 +171,8 @@ describe("POST /api/agents/[id]/chat-init", () => {
expect(body.active_task).toBeNull();
expect(body.task_messages).toEqual([]);
expect(body.has_more_messages).toBe(false);
- expect(body.previous_conversations).toEqual([]);
expect(body.has_more_conversations).toBe(false);
+ expect(body.has_more_artifacts).toBe(false);
});
it("returns 404 for non-existent agent", async () => {
@@ -268,6 +268,38 @@ describe("POST /api/agents/[id]/chat-init", () => {
expect(body.messages).toHaveLength(20);
});
+ it("sets has_more_artifacts true when artifacts reach limit", async () => {
+ const arts = Array.from({ length: 50 }, (_, i) => ({
+ id: `art${i}`,
+ conversationId: "c1",
+ agentId: "a1",
+ filename: `file${i}.png`,
+ contentType: "image/png",
+ size: 100,
+ source: "agent",
+ createdAt: "2024-01-01T00:00:00.000Z",
+ }));
+
+ setupDefaults();
+ mockListArtifactsByConversation.mockResolvedValue(arts);
+
+ const res = await POST(makeReq(), makeCtx());
+ const body = await res.json();
+
+ expect(body.has_more_artifacts).toBe(true);
+ expect(body.artifacts).toHaveLength(50);
+ });
+
+ it("sets has_more_conversations true when previous conversations exist", async () => {
+ setupDefaults();
+ mockHasPreviousConversations.mockResolvedValue(true);
+
+ const res = await POST(makeReq(), makeCtx());
+ const body = await res.json();
+
+ expect(body.has_more_conversations).toBe(true);
+ });
+
it("returns empty state for new conversation", async () => {
setupDefaults();
diff --git a/src/web/src/app/api/agents/[id]/chat-init/route.ts b/src/web/src/app/api/agents/[id]/chat-init/route.ts
index d054f41f8..8440bab49 100644
--- a/src/web/src/app/api/agents/[id]/chat-init/route.ts
+++ b/src/web/src/app/api/agents/[id]/chat-init/route.ts
@@ -12,7 +12,7 @@ import {
} from "@/lib/api/responses";
const MESSAGE_LIMIT = 20;
-const PREV_CONV_LIMIT = 10;
+const ARTIFACT_LIMIT = 50;
export const POST = withAuth(async (req, ctx) => {
const ws = await withWorkspaceMember(req, ctx);
@@ -49,15 +49,16 @@ export const POST = withAuth(async (req, ctx) => {
const convId = conversation.id;
- const [messagesResult, artifactsResult, bufferedResult, activeTaskResult, prevConvsResult] =
+ const [messagesResult, artifactsResult, bufferedResult, activeTaskResult, hasMoreConvsResult] =
await Promise.allSettled([
queries.message.listMessages(db, convId, { limit: MESSAGE_LIMIT }),
- queries.artifact.listArtifactsByConversation(db, convId, ws.workspaceId),
+ queries.artifact.listArtifactsByConversation(db, convId, ws.workspaceId, {
+ limit: ARTIFACT_LIMIT,
+ }),
queries.message.listBufferedMessages(db, convId),
queries.task.getActiveTaskByConversation(db, convId, ws.workspaceId),
- queries.conversation.listPreviousConversations(
+ queries.conversation.hasPreviousConversations(
db, ws.workspaceId, ctx.userId, id, convId, channel,
- { limit: PREV_CONV_LIMIT },
),
]);
@@ -71,8 +72,8 @@ export const POST = withAuth(async (req, ctx) => {
activeTaskResult.status === "fulfilled"
? activeTaskResult.value
: null;
- const prevConvs =
- prevConvsResult.status === "fulfilled" ? prevConvsResult.value : [];
+ const hasMoreConvs =
+ hasMoreConvsResult.status === "fulfilled" ? hasMoreConvsResult.value : false;
let taskMessages: unknown[] = [];
if (
@@ -98,10 +99,7 @@ export const POST = withAuth(async (req, ctx) => {
active_task: activeTask ? taskToResponse(activeTask) : null,
task_messages: taskMessages,
has_more_messages: messages.length >= MESSAGE_LIMIT,
- previous_conversations: prevConvs.map((c) => ({
- id: c.id,
- created_at: c.createdAt,
- })),
- has_more_conversations: prevConvs.length >= PREV_CONV_LIMIT,
+ has_more_conversations: hasMoreConvs,
+ has_more_artifacts: artifacts.length >= ARTIFACT_LIMIT,
});
});
diff --git a/src/web/src/components/agent-chat/agent-chat-view.tsx b/src/web/src/components/agent-chat/agent-chat-view.tsx
index 87a0cb24b..157f0f12b 100644
--- a/src/web/src/components/agent-chat/agent-chat-view.tsx
+++ b/src/web/src/components/agent-chat/agent-chat-view.tsx
@@ -1,6 +1,7 @@
"use client";
import React, { useEffect, useState, useRef, useCallback, useMemo } from "react";
+import { flushSync } from "react-dom";
import { useParams, useSearchParams } from "next/navigation";
import { useWorkspace } from "@/contexts/workspace-context";
import { Button } from "@/components/ui/button";
@@ -347,7 +348,6 @@ export function AgentChatView() {
setHasMore(data.has_more_messages);
setArtifacts(data.artifacts);
setBufferedMessages(data.buffered_messages);
- setPreviousConversations(data.previous_conversations);
setHasMoreConversations(data.has_more_conversations);
}
} else {
@@ -357,7 +357,6 @@ export function AgentChatView() {
setHasMore(data.has_more_messages);
setArtifacts(data.artifacts);
setBufferedMessages(data.buffered_messages);
- setPreviousConversations(data.previous_conversations);
setHasMoreConversations(data.has_more_conversations);
if (data.active_task) {
@@ -492,19 +491,23 @@ export function AgentChatView() {
before: oldest!.created_at,
beforeId: oldest!.id,
});
- if (older.length === 0) {
- setHasMore(false);
- } else {
- setHasMore(older.length >= MESSAGE_LIMIT);
- setMessages((prev) => {
- const existingIds = new Set(prev.map((m) => m.id));
- const unique = older.filter((m) => !existingIds.has(m.id));
- return [...unique, ...prev];
- });
- }
+ flushSync(() => {
+ if (older.length === 0) {
+ setHasMore(false);
+ } else {
+ setHasMore(older.length >= MESSAGE_LIMIT);
+ setMessages((prev) => {
+ const existingIds = new Set(prev.map((m) => m.id));
+ const unique = older.filter((m) => !existingIds.has(m.id));
+ return [...unique, ...prev];
+ });
+ }
+ });
} else if (canLoadPrevConv) {
let consumed = 0;
let loadedOlder: Message[] = [];
+ let napTs = "";
+ let napId = "";
while (consumed < prevConvsList.length) {
const prevConv = prevConvsList[consumed]!;
@@ -519,16 +522,9 @@ export function AgentChatView() {
continue;
}
- const napTs = oldestConvCreatedAtRef.current ?? conversation.created_at;
- const napId = `nap-${prevConv.id}`;
- setNapMarkers((prev) =>
- prev.some((m) => m.id === napId)
- ? prev
- : [...prev, { agentName: currentAgentName, created_at: napTs, id: napId }],
- );
-
+ napTs = oldestConvCreatedAtRef.current ?? conversation.created_at;
+ napId = `nap-${prevConv.id}`;
loadedOlder = older;
- setHasMore(older.length >= MESSAGE_LIMIT);
oldestConvIdRef.current = prevConv.id;
oldestConvCreatedAtRef.current = prevConv.created_at;
break;
@@ -538,23 +534,29 @@ export function AgentChatView() {
setPreviousConversations((prev) => prev.slice(consumed));
}
- if (loadedOlder.length > 0) {
- setMessages((prev) => {
- const existingIds = new Set(prev.map((m) => m.id));
- const unique = loadedOlder.filter((m) => !existingIds.has(m.id));
- return [...unique, ...prev];
- });
- } else {
- setHasMore(false);
- }
+ flushSync(() => {
+ if (loadedOlder.length > 0) {
+ setNapMarkers((prev) =>
+ prev.some((m) => m.id === napId)
+ ? prev
+ : [...prev, { agentName: currentAgentName, created_at: napTs, id: napId }],
+ );
+ setHasMore(loadedOlder.length >= MESSAGE_LIMIT);
+ setMessages((prev) => {
+ const existingIds = new Set(prev.map((m) => m.id));
+ const unique = loadedOlder.filter((m) => !existingIds.has(m.id));
+ return [...unique, ...prev];
+ });
+ } else {
+ setHasMore(false);
+ }
+ });
}
- requestAnimationFrame(() => {
- if (el) {
- const newScrollHeight = el.scrollHeight;
- el.scrollTop = newScrollHeight - prevScrollHeight;
- }
- });
+ if (el) {
+ const newScrollHeight = el.scrollHeight;
+ el.scrollTop = newScrollHeight - prevScrollHeight;
+ }
} catch {
toast.error("Failed to load older messages");
} finally {
diff --git a/src/web/src/lib/api.ts b/src/web/src/lib/api.ts
index 4899eb88b..5528565b1 100644
--- a/src/web/src/lib/api.ts
+++ b/src/web/src/lib/api.ts
@@ -213,8 +213,8 @@ export interface ChatInitResponse {
active_task: TaskApi | null;
task_messages: TaskMessage[];
has_more_messages: boolean;
- previous_conversations: PreviousConversation[];
has_more_conversations: boolean;
+ has_more_artifacts: boolean;
}
export const listPreviousConversations = (