Skip to content

armgit / alook

Public Imported from GitHub
0

Multi-Agent Collaboration for Team and Friends

fix: timeline message inside conversation

Gus6d44e72e03f1
diff --git a/src/web/src/components/agent-chat/agent-chat-view.test.ts b/src/web/src/components/agent-chat/agent-chat-view.test.ts
index 2bf3302c6..978f46aa2 100644
--- a/src/web/src/components/agent-chat/agent-chat-view.test.ts
+++ b/src/web/src/components/agent-chat/agent-chat-view.test.ts
@@ -301,6 +301,123 @@ describe("buildTimeline", () => {
});
});
+function msgInConv(id: string, created_at: string, conversation_id: string): Message {
+ return { id, conversation_id, role: "user", content: "", task_id: null, attachment_ids: null, created_at };
+}
+
+function artifactInConv(id: string, created_at: string, conversation_id: string): Artifact {
+ return { id, conversation_id, filename: "file.txt", content_type: "text/plain", size: 100, source: "agent", r2_key: `key-${id}`, created_at };
+}
+
+describe("buildTimeline — conversation grouping", () => {
+ it("groups messages from multiple conversations correctly", () => {
+ const msgs = [
+ msgInConv("a1", "2024-01-01T00:00:00Z", "convA"),
+ msgInConv("a2", "2024-01-01T01:00:00Z", "convA"),
+ msgInConv("b1", "2024-01-02T00:00:00Z", "convB"),
+ msgInConv("b2", "2024-01-02T01:00:00Z", "convB"),
+ ];
+ const naps = [nap("nap-convA", "2024-01-01T12:00:00Z")];
+
+ const result = buildTimeline(msgs, [], naps, "convB");
+ expect(result.map((i) => i.data.id)).toEqual(["a1", "a2", "nap-convA", "b1", "b2"]);
+ });
+
+ it("new message in old conversation stays in its section", () => {
+ const msgs = [
+ msgInConv("a1", "2024-01-01T00:00:00Z", "convA"),
+ msgInConv("a2", "2024-01-01T01:00:00Z", "convA"),
+ msgInConv("b1", "2024-01-02T00:00:00Z", "convB"),
+ msgInConv("b2", "2024-01-02T01:00:00Z", "convB"),
+ msgInConv("a3", "2024-01-03T00:00:00Z", "convA"), // newer than everything but belongs to old conv
+ ];
+ const naps = [nap("nap-convA", "2024-01-01T12:00:00Z")];
+
+ const result = buildTimeline(msgs, [], naps, "convB");
+ expect(result.map((i) => i.data.id)).toEqual(["a1", "a2", "a3", "nap-convA", "b1", "b2"]);
+ });
+
+ it("artifacts are grouped within their conversation section", () => {
+ const msgs = [
+ msgInConv("a1", "2024-01-01T00:00:00Z", "convA"),
+ msgInConv("a2", "2024-01-01T02:00:00Z", "convA"),
+ msgInConv("b1", "2024-01-02T00:00:00Z", "convB"),
+ ];
+ const arts = [artifactInConv("art1", "2024-01-01T01:00:00Z", "convA")];
+ const naps = [nap("nap-convA", "2024-01-01T12:00:00Z")];
+
+ const result = buildTimeline(msgs, arts, naps, "convB");
+ expect(result.map((i) => i.data.id)).toEqual(["a1", "art1", "a2", "nap-convA", "b1"]);
+ });
+
+ it("single conversation (no nap markers) behaves as before", () => {
+ const msgs = [
+ msgInConv("m2", "2024-01-02T00:00:00Z", "convA"),
+ msgInConv("m1", "2024-01-01T00:00:00Z", "convA"),
+ ];
+ const result = buildTimeline(msgs, [], [], "convA");
+ expect(result.map((i) => i.data.id)).toEqual(["m1", "m2"]);
+ });
+
+ it("currentConversationId is null → fallback to global sort", () => {
+ const msgs = [
+ msgInConv("a1", "2024-01-01T00:00:00Z", "convA"),
+ msgInConv("b1", "2024-01-02T00:00:00Z", "convB"),
+ ];
+ const naps = [nap("nap-convA", "2024-01-01T12:00:00Z")];
+
+ const result = buildTimeline(msgs, [], naps, null);
+ // falls back to global timestamp sort
+ expect(result.map((i) => i.data.id)).toEqual(["a1", "nap-convA", "b1"]);
+ });
+
+ it("empty messages array returns only nap markers", () => {
+ const naps = [nap("nap-convA", "2024-01-01T00:00:00Z")];
+ const result = buildTimeline([], [], naps, "convB");
+ expect(result).toHaveLength(1);
+ expect(result[0].kind).toBe("nap");
+ });
+
+ it("nap marker ID parsing handles expected format", () => {
+ const msgs = [
+ msgInConv("m1", "2024-01-01T00:00:00Z", "conv_abc123"),
+ msgInConv("m2", "2024-01-02T00:00:00Z", "conv_xyz"),
+ ];
+ const naps = [nap("nap-conv_abc123", "2024-01-01T12:00:00Z")];
+
+ const result = buildTimeline(msgs, [], naps, "conv_xyz");
+ expect(result.map((i) => i.data.id)).toEqual(["m1", "nap-conv_abc123", "m2"]);
+ });
+
+ it("multiple nap markers order sections correctly", () => {
+ const msgs = [
+ msgInConv("a1", "2024-01-01T00:00:00Z", "convA"),
+ msgInConv("b1", "2024-01-02T00:00:00Z", "convB"),
+ msgInConv("c1", "2024-01-03T00:00:00Z", "convC"),
+ ];
+ const naps = [
+ nap("nap-convA", "2024-01-01T12:00:00Z"),
+ nap("nap-convB", "2024-01-02T12:00:00Z"),
+ ];
+
+ const result = buildTimeline(msgs, [], naps, "convC");
+ expect(result.map((i) => i.data.id)).toEqual(["a1", "nap-convA", "b1", "nap-convB", "c1"]);
+ });
+
+ it("orphan messages (unknown conversation_id) are placed before first nap marker", () => {
+ const msgs = [
+ msgInConv("a1", "2024-01-01T00:00:00Z", "convA"),
+ msgInConv("x1", "2024-01-01T06:00:00Z", "convX"), // unknown
+ msgInConv("b1", "2024-01-02T00:00:00Z", "convB"),
+ ];
+ const naps = [nap("nap-convA", "2024-01-01T12:00:00Z")];
+
+ const result = buildTimeline(msgs, [], naps, "convB");
+ // orphan x1 is placed before the first nap marker
+ expect(result.map((i) => i.data.id)).toEqual(["a1", "x1", "nap-convA", "b1"]);
+ });
+});
+
describe("addBufferedIfNew", () => {
it("adds a new message when id is not present", () => {
const prev = [msg("m1", "2024-01-01T00:00:00Z")];
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 db02b10ba..f1c727db0 100644
--- a/src/web/src/components/agent-chat/agent-chat-view.tsx
+++ b/src/web/src/components/agent-chat/agent-chat-view.tsx
@@ -123,22 +123,81 @@ type TimelineItem =
| { kind: "artifact"; data: Artifact }
| { kind: "nap"; data: NapMarker };
-export function buildTimeline(messages: Message[], artifacts: Artifact[], napMarkers: NapMarker[]): TimelineItem[] {
- const items: TimelineItem[] = [
- ...messages.map((m): TimelineItem => ({ kind: "message", data: m })),
- ...artifacts.map((a): TimelineItem => ({ kind: "artifact", data: a })),
- ...napMarkers.map((n): TimelineItem => ({ kind: "nap", data: n })),
- ];
- return items.sort((a, b) => {
- const cmp = a.data.created_at.localeCompare(b.data.created_at);
- if (cmp !== 0) return cmp;
- if (a.kind === "nap" || b.kind === "nap") {
- if (a.kind === "nap" && b.kind !== "nap") return 1;
- if (a.kind !== "nap" && b.kind === "nap") return -1;
+export function buildTimeline(
+ messages: Message[],
+ artifacts: Artifact[],
+ napMarkers: NapMarker[],
+ currentConversationId?: string | null,
+): TimelineItem[] {
+ if (!currentConversationId || napMarkers.length === 0) {
+ const items: TimelineItem[] = [
+ ...messages.map((m): TimelineItem => ({ kind: "message", data: m })),
+ ...artifacts.map((a): TimelineItem => ({ kind: "artifact", data: a })),
+ ...napMarkers.map((n): TimelineItem => ({ kind: "nap", data: n })),
+ ];
+ return items.sort((a, b) => {
+ const cmp = a.data.created_at.localeCompare(b.data.created_at);
+ if (cmp !== 0) return cmp;
+ if (a.kind === "nap" || b.kind === "nap") {
+ if (a.kind === "nap" && b.kind !== "nap") return 1;
+ if (a.kind !== "nap" && b.kind === "nap") return -1;
+ }
+ if (a.kind !== b.kind) return a.kind === "message" ? -1 : 1;
+ return a.data.id.localeCompare(b.data.id);
+ });
+ }
+
+ const napConvIds = new Set(napMarkers.map((n) => n.id.replace(/^nap-/, "")));
+ const sortedNaps = [...napMarkers].sort((a, b) => a.created_at.localeCompare(b.created_at));
+
+ const groupItems = (convId: string): TimelineItem[] => {
+ const msgs: TimelineItem[] = messages
+ .filter((m) => m.conversation_id === convId)
+ .map((m) => ({ kind: "message" as const, data: m }));
+ const arts: TimelineItem[] = artifacts
+ .filter((a) => a.conversation_id === convId)
+ .map((a) => ({ kind: "artifact" as const, data: a }));
+ return [...msgs, ...arts].sort((a, b) => {
+ const cmp = a.data.created_at.localeCompare(b.data.created_at);
+ if (cmp !== 0) return cmp;
+ if (a.kind !== b.kind) return a.kind === "message" ? -1 : 1;
+ return a.data.id.localeCompare(b.data.id);
+ });
+ };
+
+ const result: TimelineItem[] = [];
+
+ for (const nap of sortedNaps) {
+ const convId = nap.id.replace(/^nap-/, "");
+ result.push(...groupItems(convId));
+ result.push({ kind: "nap", data: nap });
+ }
+
+ result.push(...groupItems(currentConversationId));
+
+ const knownConvIds = new Set([...napConvIds, currentConversationId]);
+ const orphanMsgs = messages.filter((m) => !knownConvIds.has(m.conversation_id));
+ const orphanArts = artifacts.filter((a) => !knownConvIds.has(a.conversation_id));
+ if (orphanMsgs.length > 0 || orphanArts.length > 0) {
+ const orphanItems: TimelineItem[] = [
+ ...orphanMsgs.map((m): TimelineItem => ({ kind: "message", data: m })),
+ ...orphanArts.map((a): TimelineItem => ({ kind: "artifact", data: a })),
+ ];
+ orphanItems.sort((a, b) => {
+ const cmp = a.data.created_at.localeCompare(b.data.created_at);
+ if (cmp !== 0) return cmp;
+ if (a.kind !== b.kind) return a.kind === "message" ? -1 : 1;
+ return a.data.id.localeCompare(b.data.id);
+ });
+ const napIdx = result.findIndex((item) => item.kind === "nap");
+ if (napIdx >= 0) {
+ result.splice(napIdx, 0, ...orphanItems);
+ } else {
+ result.push(...orphanItems);
}
- if (a.kind !== b.kind) return a.kind === "message" ? -1 : 1;
- return a.data.id.localeCompare(b.data.id);
- });
+ }
+
+ return result;
}
function useLatest<T>(value: T) {
@@ -282,7 +341,7 @@ export function AgentChatView() {
const agentArtifacts = useMemo(() => artifacts.filter((a) => a.source === "agent"), [artifacts]);
- const timeline = useMemo(() => buildTimeline(messages, agentArtifacts, napMarkers), [messages, agentArtifacts, napMarkers]);
+ const timeline = useMemo(() => buildTimeline(messages, agentArtifacts, napMarkers, conversation?.id), [messages, agentArtifacts, napMarkers, conversation?.id]);
const handleArtifactClick = useCallback((artifact: Artifact) => {
if (isPreviewable(artifact)) {