Skip to content

armgit / alook

Public Imported from GitHub
0

Multi-Agent Collaboration for Team and Friends

perf: optimize chat loading with two-path architecture and unified conversationInit endpoint

Gus6eb9cb7d9dfa
diff --git a/src/shared/src/db/queries/message.ts b/src/shared/src/db/queries/message.ts
index 5c2adce0a..858db8cf8 100644
--- a/src/shared/src/db/queries/message.ts
+++ b/src/shared/src/db/queries/message.ts
@@ -29,6 +29,19 @@ export async function createMessage(
const DEFAULT_MESSAGE_LIMIT = 20;
+export async function getNewestMessageId(
+ db: Database,
+ conversationId: string
+): Promise<string | null> {
+ const rows = await db
+ .select({ id: message.id })
+ .from(message)
+ .where(and(eq(message.conversationId, conversationId), eq(message.status, "active")))
+ .orderBy(desc(message.createdAt), desc(message.id))
+ .limit(1);
+ return rows[0]?.id ?? null;
+}
+
export async function listMessages(
db: Database,
conversationId: string,
diff --git a/src/web/src/app/api/conversations/[id]/init/route.ts b/src/web/src/app/api/conversations/[id]/init/route.ts
new file mode 100644
index 000000000..aabfa91c9
--- /dev/null
+++ b/src/web/src/app/api/conversations/[id]/init/route.ts
@@ -0,0 +1,132 @@
+import { getCloudflareContext } from "@opennextjs/cloudflare";
+import { queries } from "@alook/shared";
+import { getDb } from "@/lib/db";
+import { withAuth } from "@/lib/middleware/auth";
+import { withWorkspaceMember } from "@/lib/middleware/workspace";
+import { writeJSON, writeError } from "@/lib/middleware/helpers";
+import {
+ conversationToResponse,
+ messageToResponse,
+ taskToResponse,
+ taskMessageToResponse,
+} from "@/lib/api/responses";
+import { TaskService } from "@/lib/services/task";
+
+const MESSAGE_LIMIT = 20;
+const ARTIFACT_LIMIT = 50;
+
+export const GET = withAuth(async (req, ctx) => {
+ const ws = await withWorkspaceMember(req, ctx);
+ if (ws instanceof Response) return ws;
+
+ const { env } = getCloudflareContext();
+ const db = getDb((env as Env).DB);
+
+ const id = ctx.params?.id;
+ if (!id) {
+ return writeError("conversation id is required", 400);
+ }
+
+ const newestMessageId = new URL(req.url).searchParams.get("newest_message_id");
+
+ const conversation = await queries.conversation.getConversation(db, id, ws.workspaceId);
+ if (!conversation) {
+ return writeError("conversation not found", 404);
+ }
+
+ let cacheValid = false;
+ if (newestMessageId) {
+ const serverNewest = await queries.message.getNewestMessageId(db, id);
+ cacheValid = serverNewest === newestMessageId;
+ }
+
+ const [messagesResult, artifacts, buffered, activeTask, flaggedMessageIds, hasMoreConversations] =
+ await Promise.all([
+ queries.message.listMessages(db, id, { limit: MESSAGE_LIMIT }),
+ queries.artifact.listArtifactsByConversation(db, id, ws.workspaceId, {
+ limit: ARTIFACT_LIMIT,
+ }).catch(() => [] as Awaited<ReturnType<typeof queries.artifact.listArtifactsByConversation>>),
+ queries.message.listBufferedMessages(db, id).catch(() => [] as Awaited<ReturnType<typeof queries.message.listBufferedMessages>>),
+ queries.task.getActiveTaskByConversation(db, id, ws.workspaceId).catch(() => null),
+ queries.messageFlag.listFlaggedMessageIds(db, ctx.userId, ws.workspaceId, id).catch(() => [] as string[]),
+ queries.conversation.hasPreviousConversations(
+ db,
+ ws.workspaceId,
+ ctx.userId,
+ conversation.agentId,
+ id,
+ conversation.channel || undefined,
+ ).catch(() => false),
+ ]);
+
+ const { messages, has_more: hasMoreMessages } = messagesResult;
+
+ // Orphaned-buffer recovery
+ let resolvedActiveTask = activeTask;
+ let resolvedBuffered = buffered;
+ if (resolvedBuffered.length > 0 && !resolvedActiveTask) {
+ try {
+ const taskService = new TaskService(db);
+ const dispatched = await taskService.dispatchNextBufferedMessage(id, ws.workspaceId);
+ if (dispatched) {
+ resolvedActiveTask = dispatched;
+ resolvedBuffered = await queries.message.listBufferedMessages(db, id);
+ }
+ } catch {
+ // non-critical
+ }
+ }
+
+ let taskMessages: unknown[] = [];
+ if (
+ resolvedActiveTask &&
+ !["completed", "failed", "cancelled", "superseded"].includes(resolvedActiveTask.status)
+ ) {
+ try {
+ const tmsgs = await queries.taskMessage.listTaskMessages(db, resolvedActiveTask.id);
+ taskMessages = tmsgs.map(taskMessageToResponse);
+ } catch {
+ // non-critical
+ }
+ }
+
+ const stepCounts: Record<string, number> = {};
+ if (messages.length > 0) {
+ const taskIds = [
+ ...new Set(
+ messages
+ .filter((m) => m.role === "assistant" && m.taskId)
+ .map((m) => m.taskId!)
+ ),
+ ];
+ if (taskIds.length > 0) {
+ try {
+ const rows = await queries.taskMessage.countTaskMessagesByTaskIds(
+ db,
+ taskIds,
+ ws.workspaceId
+ );
+ for (const row of rows) {
+ stepCounts[row.taskId] = row.count;
+ }
+ } catch {
+ // non-critical
+ }
+ }
+ }
+
+ return writeJSON({
+ conversation: conversationToResponse(conversation),
+ messages: cacheValid ? null : messages.map(messageToResponse),
+ has_more_messages: hasMoreMessages,
+ has_more_conversations: hasMoreConversations,
+ has_more_artifacts: artifacts.length >= ARTIFACT_LIMIT,
+ artifacts: artifacts.map(queries.artifact.artifactToResponse),
+ buffered_messages: resolvedBuffered.map(messageToResponse),
+ flagged_message_ids: flaggedMessageIds,
+ step_counts: stepCounts,
+ active_task: resolvedActiveTask ? taskToResponse(resolvedActiveTask) : null,
+ task_messages: taskMessages,
+ cache_valid: cacheValid,
+ });
+});
diff --git a/src/web/src/app/api/conversations/check-fresh/route.ts b/src/web/src/app/api/conversations/check-fresh/route.ts
new file mode 100644
index 000000000..dfa7108c8
--- /dev/null
+++ b/src/web/src/app/api/conversations/check-fresh/route.ts
@@ -0,0 +1,47 @@
+import { getCloudflareContext } from "@opennextjs/cloudflare";
+import { queries } from "@alook/shared";
+import { getDb } from "@/lib/db";
+import { withAuth } from "@/lib/middleware/auth";
+import { withWorkspaceMember } from "@/lib/middleware/workspace";
+import { writeJSON, writeError } from "@/lib/middleware/helpers";
+
+export const GET = withAuth(async (req, ctx) => {
+ const ws = await withWorkspaceMember(req, ctx);
+ if (ws instanceof Response) return ws;
+
+ const { env } = getCloudflareContext();
+ const db = getDb((env as Env).DB);
+
+ const url = new URL(req.url);
+ const conversationId = url.searchParams.get("conversation_id");
+ const agentId = url.searchParams.get("agent_id");
+ const channel = url.searchParams.get("channel") || undefined;
+
+ let convId: string;
+
+ if (conversationId) {
+ const conv = await queries.conversation.getConversation(db, conversationId, ws.workspaceId);
+ if (!conv) {
+ return writeError("conversation not found", 404);
+ }
+ convId = conv.id;
+ } else if (agentId) {
+ const conv = await queries.conversation.getOrCreateAgentConversation(
+ db,
+ ws.workspaceId,
+ ctx.userId,
+ agentId,
+ channel,
+ );
+ convId = conv.id;
+ } else {
+ return writeError("conversation_id or agent_id is required", 400);
+ }
+
+ const newestMessageId = await queries.message.getNewestMessageId(db, convId);
+
+ return writeJSON({
+ conversation_id: convId,
+ newest_message_id: newestMessageId,
+ });
+});
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 b6d4bd433..87d23e128 100644
--- a/src/web/src/components/agent-chat/agent-chat-view.tsx
+++ b/src/web/src/components/agent-chat/agent-chat-view.tsx
@@ -8,15 +8,15 @@ import { Button } from "@/components/ui/button";
import { TaskStream } from "@/components/task-stream";
import {
chatInit,
+ checkFreshness,
+ conversationInit,
createConversation,
- getConversation,
listMessages,
listMessagesAroundTask,
listPreviousConversations,
sendMessage,
getTask,
getTaskMessages,
- getTaskStepCounts,
listArtifacts,
listBufferedMessages,
createBufferedMessage,
@@ -32,7 +32,7 @@ import {
flagMessage as apiFlagMessage,
unflagMessage as apiUnflagMessage,
} from "@/lib/api";
-import { appendCachedMessage, getCachedMessages, getCachedMessagesBefore, mergeCachedMessages } from "@/lib/chat-cache";
+import { appendCachedMessage, getCachedMessages, getCachedMessagesBefore, getCacheMeta, mergeCachedMessages } from "@/lib/chat-cache";
import type { PreviousConversation, TraceTask } from "@/lib/api";
import type { Artifact, Conversation, Issue, IssueComment, Message, TaskApi as Task, TaskMessage, WsMessage } from "@alook/shared";
import { useAgentContext } from "@/contexts/agent-context";
@@ -333,7 +333,7 @@ export function AgentChatView({
const [cancelling, setCancelling] = useState(false);
const [activeTask, setActiveTask] = useState<Task | null>(null);
const [taskMessages, setTaskMessages] = useState<TaskMessage[]>([]);
- const [loading, setLoading] = useState(true);
+ const [messagesLoading, setMessagesLoading] = useState(true);
const [connectionLost, setConnectionLost] = useState(false);
const [hasMore, setHasMore] = useState(false);
const [loadingMore, setLoadingMore] = useState(false);
@@ -482,7 +482,7 @@ export function AgentChatView({
pollRef.current = null;
pollTaskIdRef.current = null;
let ignore = false;
- setLoading(true);
+ setMessagesLoading(true);
initialScrollDone.current = false;
setActiveTask(null);
setTaskMessages([]);
@@ -496,66 +496,91 @@ export function AgentChatView({
setInput(localStorage.getItem(`chat-draft:${agentId}:${targetConvId ?? 'default'}`) ?? "");
setMessages([]);
async function load() {
+ let hasCachedMessages = false;
try {
+ let convId: string | null = null;
+ let cacheMeta: Awaited<ReturnType<typeof getCacheMeta>> = null;
+
if (targetConvId) {
- const cached = await getCachedMessages(targetConvId, workspaceId);
- if (ignore) return;
- if (cached && cached.length > 0) {
- setMessages(cached);
- setLoading(false);
+ // Fast path: we already know the conv ID — render from cache immediately, no network needed
+ convId = targetConvId;
+ cacheMeta = await getCacheMeta(convId, workspaceId);
+ if (cacheMeta?.newestMessageId) {
+ const cached = await getCachedMessages(convId, workspaceId);
+ if (ignore) return;
+ if (cached && cached.length > 0) {
+ hasCachedMessages = true;
+ setMessages(cached);
+ setHasMore(cacheMeta.hasMore);
+ setMessagesLoading(false);
+ }
}
+ } else {
+ // Slow path: need server to resolve conv ID first
try {
- const [conv, msgsResult, arts, buffered] = await Promise.all([
- getConversation(targetConvId, workspaceId),
- listMessages(targetConvId, workspaceId),
- listArtifacts(targetConvId, workspaceId).catch(() => [] as Artifact[]),
- listBufferedMessages(targetConvId, workspaceId).catch(() => [] as Message[]),
- ]);
+ const fresh = await checkFreshness({ agentId, channel: activeChannel }, workspaceId);
if (ignore) return;
- setConversation(conv);
- setMessages((prev) => mergeMessages(prev, msgsResult.messages));
- setHasMore(msgsResult.has_more);
- setArtifacts(arts);
- setBufferedMessages(buffered);
- writeToCacheRef.current(msgsResult.messages, msgsResult.has_more).catch(() => {});
- const taskIds = [...new Set(msgsResult.messages.filter((m) => m.role === "assistant" && m.task_id).map((m) => m.task_id!))];
- if (taskIds.length > 0) {
- getTaskStepCounts(taskIds, workspaceId)
- .then((counts) => { if (!ignore) setStepCounts(counts); })
- .catch(() => {});
- }
- listFlaggedMessageIds(workspaceId, targetConvId)
- .then((r) => { if (!ignore) setFlaggedIds(new Set(r.message_ids)); })
- .catch(() => {});
- if (scrollToTaskId) {
- const task = await getTask(scrollToTaskId, workspaceId).catch(() => null);
+ convId = fresh.conversation_id;
+ cacheMeta = await getCacheMeta(convId, workspaceId);
+ const cacheValid = !!(cacheMeta?.newestMessageId && cacheMeta.newestMessageId === fresh.newest_message_id);
+ if (cacheValid) {
+ const cached = await getCachedMessages(convId, workspaceId);
if (ignore) return;
- if (task && !["completed", "failed", "cancelled", "superseded"].includes(task.status)) {
- setActiveTask(task);
- const tmsgs = await getTaskMessages(scrollToTaskId, workspaceId).catch(() => [] as TaskMessage[]);
- if (ignore) return;
- setTaskMessages(tmsgs);
- if (tmsgs.length > 0) {
- lastSeqRef.current = Math.max(...tmsgs.map((m) => m.seq));
- }
- startPollingRef.current?.(task.id, targetConvId, lastSeqRef.current);
+ if (cached && cached.length > 0) {
+ hasCachedMessages = true;
+ setMessages(cached);
+ setHasMore(cacheMeta!.hasMore);
+ setMessagesLoading(false);
}
}
} catch {
- const data = await chatInit(agentId, workspaceId, activeChannel);
- if (ignore) return;
- setConversation(data.conversation);
- setMessages((prev) => prev.length > 0 ? mergeMessages(prev, data.messages) : data.messages);
- setHasMore(data.has_more_messages);
- setArtifacts(data.artifacts);
- setBufferedMessages(data.buffered_messages);
- setHasMoreConversations(data.has_more_conversations);
+ // checkFreshness failed — fall back to chatInit below
+ }
+ }
+
+ // Phase B: full data fetch (background hydration or stale-cache refresh)
+ if (convId) {
+ const data = await conversationInit(convId, workspaceId, {
+ newestMessageId: cacheMeta?.newestMessageId ?? undefined,
+ });
+ if (ignore) return;
+ setConversation(data.conversation);
+ setHasMoreConversations(data.has_more_conversations);
+ if (!data.cache_valid && data.messages) {
+ setMessages((prev) => mergeMessages(prev, data.messages!));
writeToCacheRef.current(data.messages, data.has_more_messages).catch(() => {});
- listFlaggedMessageIds(workspaceId, data.conversation.id)
- .then((r) => { if (!ignore) setFlaggedIds(new Set(r.message_ids)); })
- .catch(() => {});
+ setHasMore(data.has_more_messages);
+ } else if (cacheMeta) {
+ setHasMore(cacheMeta.hasMore);
+ }
+ setStepCounts(data.step_counts);
+ setArtifacts(data.artifacts);
+ setBufferedMessages(data.buffered_messages);
+ setFlaggedIds(new Set(data.flagged_message_ids));
+ if (data.active_task) {
+ setActiveTask(data.active_task);
+ setTaskMessages(data.task_messages);
+ if (data.task_messages.length > 0) {
+ lastSeqRef.current = Math.max(...data.task_messages.map((m) => m.seq));
+ }
+ startPollingRef.current?.(data.active_task.id, convId, lastSeqRef.current);
+ }
+ if (scrollToTaskId) {
+ const task = await getTask(scrollToTaskId, workspaceId).catch(() => null);
+ if (ignore) return;
+ if (task && !["completed", "failed", "cancelled", "superseded"].includes(task.status)) {
+ setActiveTask(task);
+ const tmsgs = await getTaskMessages(scrollToTaskId, workspaceId).catch(() => [] as TaskMessage[]);
+ if (ignore) return;
+ setTaskMessages(tmsgs);
+ if (tmsgs.length > 0) {
+ lastSeqRef.current = Math.max(...tmsgs.map((m) => m.seq));
+ }
+ startPollingRef.current?.(task.id, convId, lastSeqRef.current);
+ }
}
} else {
+ // checkFreshness failed entirely — fall back to chatInit
const data = await chatInit(agentId, workspaceId, activeChannel);
if (ignore) return;
setConversation(data.conversation);
@@ -568,7 +593,6 @@ export function AgentChatView({
listFlaggedMessageIds(workspaceId, data.conversation.id)
.then((r) => { if (!ignore) setFlaggedIds(new Set(r.message_ids)); })
.catch(() => {});
-
if (data.active_task) {
setActiveTask(data.active_task);
if (data.task_messages.length > 0) {
@@ -581,9 +605,13 @@ export function AgentChatView({
}
}
} catch {
- toast.error("Failed to load conversation");
+ if (!hasCachedMessages) {
+ toast.error("Failed to load conversation");
+ } else {
+ toast.error("Couldn't refresh conversation");
+ }
} finally {
- if (!ignore) setLoading(false);
+ if (!ignore) setMessagesLoading(false);
}
}
load();
@@ -611,7 +639,7 @@ export function AgentChatView({
// Scroll to bottom on initial load (skip if scroll-to-task/message is active)
useEffect(() => {
- if (!loading && messages.length > 0 && !initialScrollDone.current) {
+ if (!messagesLoading && messages.length > 0 && !initialScrollDone.current) {
initialScrollDone.current = true;
if (scrollToTaskId || scrollToMessageId) {
isNearBottom.current = false;
@@ -631,11 +659,11 @@ export function AgentChatView({
}, 50);
}
}
- }, [loading, messages.length, scrollToTaskId, scrollToMessageId, propTargetConvId]);
+ }, [messagesLoading, messages.length, scrollToTaskId, scrollToMessageId, propTargetConvId]);
// Scroll to task when ?task= param is present
useEffect(() => {
- if (!scrollToTaskId || loading || !conversation) return;
+ if (!scrollToTaskId || messagesLoading || !conversation) return;
isNearBottom.current = false;
scrollTargetActiveRef.current = true;
let cancelled = false;
@@ -679,11 +707,11 @@ export function AgentChatView({
clearTimeout(timerId);
if (highlightTimerId) clearTimeout(highlightTimerId);
};
- }, [scrollToTaskId, loading, conversation, workspaceId]);
+ }, [scrollToTaskId, messagesLoading, conversation, workspaceId]);
// Scroll to message when ?msg= param is present (skip if task scroll is active)
useEffect(() => {
- if (!scrollToMessageId || scrollToTaskId || loading || !conversation) return;
+ if (!scrollToMessageId || scrollToTaskId || messagesLoading || !conversation) return;
isNearBottom.current = false;
scrollTargetActiveRef.current = true;
let cancelled = false;
@@ -712,7 +740,7 @@ export function AgentChatView({
clearTimeout(timerId);
if (highlightTimerId) clearTimeout(highlightTimerId);
};
- }, [scrollToMessageId, scrollToTaskId, loading, conversation]);
+ }, [scrollToMessageId, scrollToTaskId, messagesLoading, conversation]);
// Auto-scroll when task badge appears or new task steps arrive
const taskStatus = activeTask?.status;
@@ -930,13 +958,13 @@ export function AgentChatView({
const MIN_MESSAGES = 10;
useEffect(() => {
- if (loading || !conversation) return;
+ if (messagesLoading || !conversation) return;
if (messages.length >= MIN_MESSAGES || !canLoadMore) return;
if (loadingMore) return;
if (backfillAttemptsRef.current >= 3) return;
backfillAttemptsRef.current += 1;
loadOlderMessages(true);
- }, [loading, messages.length, canLoadMore, loadingMore, conversation, loadOlderMessages]);
+ }, [messagesLoading, messages.length, canLoadMore, loadingMore, conversation, loadOlderMessages]);
const handleScroll = useCallback(() => {
const el = scrollRef.current;
@@ -1601,7 +1629,7 @@ export function AgentChatView({
const isTaskActive = !!activeTask && !["completed", "failed", "cancelled", "superseded"].includes(activeTask.status);
- if (loading) {
+ if (messagesLoading) {
return (
<>
<div className="flex-1 overflow-y-auto px-3 md:px-5">
@@ -1640,7 +1668,7 @@ export function AgentChatView({
);
}
- if (!conversation) {
+ if (!messagesLoading && !conversation && messages.length === 0) {
return (
<div className="flex flex-1 items-center justify-center text-sm text-muted-foreground">
Failed to load conversation
@@ -1677,7 +1705,7 @@ export function AgentChatView({
}}
>
<div className="mx-auto max-w-2xl py-6 space-y-4 min-w-0">
- {canLoadMore && !loadingMore && (
+ {conversation && canLoadMore && !loadingMore && (
<div className="flex justify-center py-2">
<button
onClick={() => loadOlderMessages()}
diff --git a/src/web/src/lib/api.ts b/src/web/src/lib/api.ts
index 6f53c1b9d..4291830a0 100644
--- a/src/web/src/lib/api.ts
+++ b/src/web/src/lib/api.ts
@@ -275,6 +275,51 @@ export const chatInit = (agentId: string, workspaceId: string, channel?: string)
body: JSON.stringify({ ...(channel ? { channel } : {}) }),
});
+export interface ConversationInitResponse {
+ conversation: Conversation;
+ messages: Message[] | null;
+ has_more_messages: boolean;
+ has_more_conversations: boolean;
+ has_more_artifacts: boolean;
+ artifacts: Artifact[];
+ buffered_messages: Message[];
+ flagged_message_ids: string[];
+ step_counts: Record<string, number>;
+ active_task: TaskApi | null;
+ task_messages: TaskMessage[];
+ cache_valid: boolean;
+}
+
+export const conversationInit = (
+ conversationId: string,
+ workspaceId: string,
+ opts?: { newestMessageId?: string },
+) => {
+ const extra: Record<string, string> = {};
+ if (opts?.newestMessageId) extra.newest_message_id = opts.newestMessageId;
+ return apiFetch<ConversationInitResponse>(
+ `/api/conversations/${conversationId}/init${wsQuery(workspaceId, extra)}`,
+ );
+};
+
+export interface FreshnessCheckResponse {
+ conversation_id: string;
+ newest_message_id: string | null;
+}
+
+export const checkFreshness = (
+ opts: { conversationId?: string; agentId?: string; channel?: string },
+ workspaceId: string,
+) => {
+ const extra: Record<string, string> = {};
+ if (opts.conversationId) extra.conversation_id = opts.conversationId;
+ if (opts.agentId) extra.agent_id = opts.agentId;
+ if (opts.channel) extra.channel = opts.channel;
+ return apiFetch<FreshnessCheckResponse>(
+ `/api/conversations/check-fresh${wsQuery(workspaceId, extra)}`,
+ );
+};
+
export const deleteConversation = (id: string, workspaceId: string) =>
apiFetch<void>(`/api/conversations/${id}${wsQuery(workspaceId)}`, { method: "DELETE" });