Nguyen DN 654f5e1c66
fix(frontend): render full content for multi-part AI messages (#3649)
* fix(frontend): render full content for multi-part AI messages

    Gemini streams the first content block as a {type:text} object carrying
    the thinking signature, then emits continuation deltas as bare strings.
    The content extractors only kept {type:text} objects and dropped the
    string parts, truncating each AI message to its first block.

    Handle string elements in extractContentFromMessage, extractTextFromMessage,
    and textOfMessage so the full message renders.

    Fixes #1000

* test(frontend): cover multi-part AI message content extraction

Add a regression test for the #1000 truncation: an AI message whose
content is [{type:text, signature}, "bare string"] (Gemini's shape after
LangChain merge_content). Fails on main, passes with the extractor fix.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>

* test(frontend): cover textOfMessage multi-part content extraction

Add tests for the bare-string continuation case and the null-when-empty
contract, and document why textOfMessage joins flat ("") while the body
extractors join with "\n". Addresses review feedback on #3649.

---------

Co-authored-by: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-06-19 21:46:59 +08:00

121 lines
3.1 KiB
TypeScript

import type { Message } from "@langchain/langgraph-sdk";
import { expect, test } from "vitest";
import {
channelSourceOfThread,
pathOfThread,
textOfMessage,
} from "@/core/threads/utils";
test("uses standard chat route when thread has no agent context", () => {
expect(pathOfThread("thread-123")).toBe("/workspace/chats/thread-123");
expect(
pathOfThread({
thread_id: "thread-123",
}),
).toBe("/workspace/chats/thread-123");
});
test("uses agent chat route when thread context has agent_name", () => {
expect(
pathOfThread({
thread_id: "thread-123",
context: { agent_name: "researcher" },
}),
).toBe("/workspace/agents/researcher/chats/thread-123");
});
test("uses provided context when pathOfThread is called with a thread id", () => {
expect(pathOfThread("thread-123", { agent_name: "ops agent" })).toBe(
"/workspace/agents/ops%20agent/chats/thread-123",
);
});
test("uses agent chat route when thread metadata has agent_name", () => {
expect(
pathOfThread({
thread_id: "thread-456",
metadata: { agent_name: "coder" },
}),
).toBe("/workspace/agents/coder/chats/thread-456");
});
test("prefers context.agent_name over metadata.agent_name", () => {
expect(
pathOfThread({
thread_id: "thread-789",
context: { agent_name: "from-context" },
metadata: { agent_name: "from-metadata" },
}),
).toBe("/workspace/agents/from-context/chats/thread-789");
});
test("reads IM channel source metadata", () => {
expect(
channelSourceOfThread({
metadata: {
channel_source: {
type: "im_channel",
provider: "feishu",
chat_id: "oc_123",
},
},
}),
).toEqual({
type: "im_channel",
provider: "feishu",
label: "Feishu",
});
});
test("ignores threads without valid IM channel source metadata", () => {
expect(channelSourceOfThread({ metadata: {} })).toBeNull();
expect(
channelSourceOfThread({
metadata: { channel_source: { provider: "" } },
}),
).toBeNull();
expect(
channelSourceOfThread({
metadata: {
channel_source: {
type: "other",
provider: "feishu",
},
},
}),
).toBeNull();
});
test("textOfMessage concatenates object and bare-string content parts", () => {
// Gemini's finalized shape: first signed {type:text} block + bare-string
// continuation. textOfMessage joins flat ("") for single-line consumers.
const message = {
id: "ai-1",
type: "ai",
content: [
{
type: "text",
text: "First block.",
extras: { signature: "abc123" },
index: 0,
},
" Continuation as a bare string.",
],
} as unknown as Message;
expect(textOfMessage(message)).toBe(
"First block. Continuation as a bare string.",
);
});
test("textOfMessage returns null when array content has no text", () => {
const message = {
id: "ai-1",
type: "ai",
content: [{ type: "image_url", image_url: "https://example.com/x.png" }],
} as unknown as Message;
expect(textOfMessage(message)).toBeNull();
});