Skip to content

armgit / alook

Public Imported from GitHub
0

Multi-Agent Collaboration for Team and Friends

fix: message jump locate

Gus4fd439aa0407
diff --git a/src/web/src/app/(app)/w/[slug]/flags/page.tsx b/src/web/src/app/(app)/w/[slug]/flags/page.tsx
index 483e90735..e1b48290d 100644
--- a/src/web/src/app/(app)/w/[slug]/flags/page.tsx
+++ b/src/web/src/app/(app)/w/[slug]/flags/page.tsx
@@ -200,7 +200,7 @@ export default function FlagsPage() {
onClick={(e) => {
if (e.metaKey || e.ctrlKey || e.shiftKey || e.button !== 0) return;
e.preventDefault();
- openAgentChat(item.agent_id, { conversationId: item.conversation_id });
+ openAgentChat(item.agent_id, { conversationId: item.conversation_id, messageId: item.message_id });
}}
onUnflag={() => handleUnflag(item)}
/>
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 a63eb070d..9620742b6 100644
--- a/src/web/src/components/agent-chat/agent-chat-view.tsx
+++ b/src/web/src/components/agent-chat/agent-chat-view.tsx
@@ -303,10 +303,12 @@ export function AgentChatView({
agentId: propAgentId,
targetConvId: propTargetConvId,
scrollToTaskId: propScrollToTaskId,
+ scrollToMessageId: propScrollToMessageId,
}: {
agentId?: string;
targetConvId?: string | null;
scrollToTaskId?: string | null;
+ scrollToMessageId?: string | null;
}) {
const params = useParams();
const searchParams = useSearchParams();
@@ -316,6 +318,7 @@ export function AgentChatView({
const { activeChannel, loading: channelLoading, setAgentId: setChannelAgentId } = useChannel();
const agentId = propAgentId ?? (params.id as string);
const scrollToTaskId = propScrollToTaskId !== undefined ? propScrollToTaskId : searchParams.get("task");
+ const scrollToMessageId = propScrollToMessageId !== undefined ? propScrollToMessageId : searchParams.get("msg");
const targetConvId = propTargetConvId !== undefined ? propTargetConvId : searchParams.get("conv");
const [conversation, setConversation] = useState<Conversation | null>(null);
@@ -415,6 +418,7 @@ export function AgentChatView({
const initialScrollDone = useRef(false);
const loadingMoreRef = useRef(false);
const isNearBottom = useRef(true);
+ const scrollTargetActiveRef = useRef(false);
const startPollingRef = useRef<((taskId: string, conversationId: string, initialSeq?: number) => void) | null>(null);
const oldestConversationCursorRef = useRef<PreviousConversation | null>(null);
const backfillAttemptsRef = useRef(0);
@@ -580,46 +584,115 @@ export function AgentChatView({
};
}, [conversation?.id, workspaceId]);
- // Scroll to bottom on initial load (skip if scroll-to-task is active)
+ // Scroll to bottom on initial load (skip if scroll-to-task/message is active)
useEffect(() => {
if (!loading && messages.length > 0 && !initialScrollDone.current) {
initialScrollDone.current = true;
- if (!scrollToTaskId) {
+ if (scrollToTaskId || scrollToMessageId) {
+ isNearBottom.current = false;
+ } else if (propTargetConvId) {
+ setTimeout(() => {
+ const assistantMsgs = scrollRef.current?.querySelectorAll('[data-quote-source]');
+ if (assistantMsgs && assistantMsgs.length > 0) {
+ const lastAssistant = assistantMsgs[assistantMsgs.length - 1];
+ lastAssistant.scrollIntoView({ behavior: "instant", block: "start" });
+ } else {
+ scrollRef.current?.scrollTo({ top: scrollRef.current.scrollHeight });
+ }
+ }, 50);
+ } else {
setTimeout(() => {
scrollRef.current?.scrollTo({ top: scrollRef.current.scrollHeight });
}, 50);
}
}
- }, [loading, messages.length, scrollToTaskId]);
+ }, [loading, messages.length, scrollToTaskId, scrollToMessageId, propTargetConvId]);
// Scroll to task when ?task= param is present
useEffect(() => {
if (!scrollToTaskId || loading || !conversation) return;
+ isNearBottom.current = false;
+ scrollTargetActiveRef.current = true;
+ let cancelled = false;
+ let highlightTimerId: ReturnType<typeof setTimeout> | undefined;
const tryScroll = () => {
+ if (cancelled) return false;
const el = document.querySelector(`[data-task-id="${CSS.escape(scrollToTaskId)}"]`);
if (!el) return false;
- el.scrollIntoView({ behavior: "smooth", block: "center" });
+ el.scrollIntoView({ behavior: "smooth", block: "start" });
el.classList.add("task-highlight");
- setTimeout(() => el.classList.remove("task-highlight"), 1500);
+ highlightTimerId = setTimeout(() => {
+ el.classList.remove("task-highlight");
+ if (!cancelled) scrollTargetActiveRef.current = false;
+ }, 1500);
return true;
};
- setTimeout(async () => {
+ const timerId = setTimeout(async () => {
+ if (cancelled) return;
if (tryScroll()) return;
try {
const around = await listMessagesAroundTask(conversation.id, workspaceId, scrollToTaskId);
+ if (cancelled) return;
if (around.length > 0) {
setMessages((prev) => mergeMessages(prev, around));
requestAnimationFrame(() => {
- setTimeout(() => tryScroll(), 100);
+ setTimeout(() => {
+ if (!tryScroll()) {
+ scrollTargetActiveRef.current = false;
+ }
+ }, 100);
});
+ } else {
+ scrollTargetActiveRef.current = false;
}
- } catch { }
+ } catch {
+ if (!cancelled) scrollTargetActiveRef.current = false;
+ }
}, 100);
+ return () => {
+ cancelled = true;
+ clearTimeout(timerId);
+ if (highlightTimerId) clearTimeout(highlightTimerId);
+ };
}, [scrollToTaskId, loading, conversation, workspaceId]);
+ // Scroll to message when ?msg= param is present (skip if task scroll is active)
+ useEffect(() => {
+ if (!scrollToMessageId || scrollToTaskId || loading || !conversation) return;
+ isNearBottom.current = false;
+ scrollTargetActiveRef.current = true;
+ let cancelled = false;
+ let highlightTimerId: ReturnType<typeof setTimeout> | undefined;
+ const tryScroll = () => {
+ if (cancelled) return false;
+ const el = document.querySelector(`[data-message-id="${CSS.escape(scrollToMessageId)}"]`);
+ if (!el) return false;
+ el.scrollIntoView({ behavior: "smooth", block: "start" });
+ el.classList.add("task-highlight");
+ highlightTimerId = setTimeout(() => {
+ el.classList.remove("task-highlight");
+ if (!cancelled) scrollTargetActiveRef.current = false;
+ }, 1500);
+ return true;
+ };
+ const timerId = setTimeout(() => {
+ if (cancelled) return;
+ if (!tryScroll()) {
+ scrollRef.current?.scrollTo({ top: scrollRef.current.scrollHeight });
+ scrollTargetActiveRef.current = false;
+ }
+ }, 100);
+ return () => {
+ cancelled = true;
+ clearTimeout(timerId);
+ if (highlightTimerId) clearTimeout(highlightTimerId);
+ };
+ }, [scrollToMessageId, scrollToTaskId, loading, conversation]);
+
// Auto-scroll when task badge appears or new task steps arrive
const taskStatus = activeTask?.status;
useEffect(() => {
+ if (scrollTargetActiveRef.current) return;
const isRunning = taskStatus === "running" || taskStatus === "queued";
if (isRunning && isNearBottom.current) {
scrollToBottom();
@@ -885,7 +958,7 @@ export function AgentChatView({
.catch(() => {});
}, 1000);
- const shouldScroll = isNearBottom.current;
+ const shouldScroll = !scrollTargetActiveRef.current && isNearBottom.current;
try {
const [latestResult, arts] = await Promise.all([
listMessages(conversationId, workspaceId),
diff --git a/src/web/src/components/agent-chat/message-list.tsx b/src/web/src/components/agent-chat/message-list.tsx
index e19b1baff..c530d9333 100644
--- a/src/web/src/components/agent-chat/message-list.tsx
+++ b/src/web/src/components/agent-chat/message-list.tsx
@@ -158,7 +158,7 @@ export const MessageItem = memo(function MessageItem({
{msg.role === "user" ? (() => {
const awaitingRun = isLastMessage && !!activeTask && activeTask.status !== "running" && !["completed", "failed", "cancelled", "superseded"].includes(activeTask.status);
return (
- <div className="flex justify-end" {...(msg.task_id ? { "data-task-id": msg.task_id } : {})}>
+ <div className="flex justify-end" data-message-id={msg.id} {...(msg.task_id ? { "data-task-id": msg.task_id } : {})}>
<div className={cn(
"max-w-[80%] rounded-lg px-4 py-2 bg-primary text-primary-foreground text-base relative",
)}>
@@ -182,7 +182,7 @@ export const MessageItem = memo(function MessageItem({
const eventIssueId = msg.metadata?.issueId as string | undefined;
const isClickable = !!eventEmailId || !!eventIssueId;
return (
- <div className="flex justify-start" {...(msg.task_id ? { "data-task-id": msg.task_id } : {})}>
+ <div className="flex justify-start" data-message-id={msg.id} {...(msg.task_id ? { "data-task-id": msg.task_id } : {})}>
<div
className={cn(
"w-full rounded-md border bg-muted/50 text-muted-foreground text-sm px-3 py-2 flex items-start gap-2",
@@ -199,7 +199,7 @@ export const MessageItem = memo(function MessageItem({
<div className={cn(
"group/msg flex flex-col justify-start",
isFlagged && "bg-muted/30 rounded-lg px-2 -mx-2"
- )} data-quote-source {...(msg.task_id ? { "data-task-id": msg.task_id } : {})}>
+ )} data-message-id={msg.id} data-quote-source {...(msg.task_id ? { "data-task-id": msg.task_id } : {})}>
<div className="markdown max-w-full min-w-0 px-1 py-1 text-base text-foreground">
<Streamdown controls={{ code: { copy: true, download: false }, table: { copy: true, download: false, fullscreen: true } }} linkSafety={{ enabled: false }} allowedTags={MENTION_ALLOWED_TAGS} literalTagContent={MENTION_LITERAL_TAGS} components={mentionComponents}>{highlightMentions(msg.content, agents)}</Streamdown>
</div>
diff --git a/src/web/src/components/canvas/agent-chat-sheet.tsx b/src/web/src/components/canvas/agent-chat-sheet.tsx
index 60701365b..2ba5919e8 100644
--- a/src/web/src/components/canvas/agent-chat-sheet.tsx
+++ b/src/web/src/components/canvas/agent-chat-sheet.tsx
@@ -24,13 +24,14 @@ interface AgentChatSheetProps {
agent: Agent | null;
targetConvId?: string | null;
scrollToTaskId?: string | null;
+ scrollToMessageId?: string | null;
}
const MIN_WIDTH = 320;
const MAX_WIDTH_RATIO = 0.8;
const DEFAULT_WIDTH = 480;
-export function AgentChatSheet({ open, onOpenChange, agent, targetConvId, scrollToTaskId }: AgentChatSheetProps) {
+export function AgentChatSheet({ open, onOpenChange, agent, targetConvId, scrollToTaskId, scrollToMessageId }: AgentChatSheetProps) {
const { runtimes, activeTaskCounts } = useAgentContext();
const { slug } = useWorkspace();
const router = useRouter();
@@ -74,6 +75,7 @@ export function AgentChatSheet({ open, onOpenChange, agent, targetConvId, scroll
{agent && (() => {
const params = new URLSearchParams();
if (scrollToTaskId) params.set("task", scrollToTaskId);
+ if (scrollToMessageId) params.set("msg", scrollToMessageId);
if (targetConvId) params.set("conv", targetConvId);
const qs = params.toString();
const fullPageUrl = `/w/${slug}/agents/${agent.id}${qs ? `?${qs}` : ""}`;
@@ -136,6 +138,7 @@ export function AgentChatSheet({ open, onOpenChange, agent, targetConvId, scroll
agentId={agent.id}
targetConvId={targetConvId}
scrollToTaskId={scrollToTaskId}
+ scrollToMessageId={scrollToMessageId}
/>
</div>
)}
diff --git a/src/web/src/components/flag-popover.tsx b/src/web/src/components/flag-popover.tsx
index cce73b9cc..f51af66e1 100644
--- a/src/web/src/components/flag-popover.tsx
+++ b/src/web/src/components/flag-popover.tsx
@@ -84,7 +84,7 @@ export function FlagPopover({
e.preventDefault();
setOpen(false);
onNavigate?.();
- openAgentChat(item.agent_id, { conversationId: item.conversation_id });
+ openAgentChat(item.agent_id, { conversationId: item.conversation_id, messageId: item.message_id });
}, [onNavigate, openAgentChat]);
return (
diff --git a/src/web/src/contexts/agent-chat-sheet-context.tsx b/src/web/src/contexts/agent-chat-sheet-context.tsx
index cdc009cff..3a6338ef3 100644
--- a/src/web/src/contexts/agent-chat-sheet-context.tsx
+++ b/src/web/src/contexts/agent-chat-sheet-context.tsx
@@ -16,7 +16,7 @@ import { AgentChatSheet } from "@/components/canvas/agent-chat-sheet";
interface AgentChatSheetContextValue {
openAgentChat: (
agentId: string,
- opts?: { conversationId?: string; taskId?: string },
+ opts?: { conversationId?: string; taskId?: string; messageId?: string },
) => void;
}
@@ -42,6 +42,7 @@ export function AgentChatSheetProvider({ children }: { children: ReactNode }) {
const [agentId, setAgentId] = useState<string | null>(null);
const [targetConvId, setTargetConvId] = useState<string | null>(null);
const [scrollToTaskId, setScrollToTaskId] = useState<string | null>(null);
+ const [scrollToMessageId, setScrollToMessageId] = useState<string | null>(null);
const agent = agentId ? agents.find((a) => a.id === agentId) ?? null : null;
@@ -49,7 +50,7 @@ export function AgentChatSheetProvider({ children }: { children: ReactNode }) {
agentsRef.current = agents;
const openAgentChat = useCallback(
- (id: string, opts?: { conversationId?: string; taskId?: string }) => {
+ (id: string, opts?: { conversationId?: string; taskId?: string; messageId?: string }) => {
const found = agentsRef.current.find((a) => a.id === id);
if (!found) {
router.push(`/w/${slug}/agents/${id}`);
@@ -58,6 +59,7 @@ export function AgentChatSheetProvider({ children }: { children: ReactNode }) {
setAgentId(id);
setTargetConvId(opts?.conversationId ?? null);
setScrollToTaskId(opts?.taskId ?? null);
+ setScrollToMessageId(opts?.messageId ?? null);
setOpen(true);
},
[router, slug],
@@ -68,6 +70,7 @@ export function AgentChatSheetProvider({ children }: { children: ReactNode }) {
if (!nextOpen) {
setTargetConvId(null);
setScrollToTaskId(null);
+ setScrollToMessageId(null);
}
}, []);
@@ -80,6 +83,7 @@ export function AgentChatSheetProvider({ children }: { children: ReactNode }) {
agent={agent}
targetConvId={targetConvId}
scrollToTaskId={scrollToTaskId}
+ scrollToMessageId={scrollToMessageId}
/>
</AgentChatSheetContext.Provider>
);