mirror of
https://github.com/bytedance/deer-flow.git
synced 2026-05-25 09:26:00 +00:00
Stabilize write artifact previews (#3172)
This commit is contained in:
@@ -0,0 +1,174 @@
|
||||
import { expect, test } from "@playwright/test";
|
||||
|
||||
import { mockLangGraphAPI } from "./utils/mock-api";
|
||||
|
||||
const ARTIFACT_PATH = "/artifact-fixtures/report.html";
|
||||
const MARKDOWN_ARTIFACT_PATH = "/artifact-fixtures/report.md";
|
||||
const JSON_ARTIFACT_PATH = "/artifact-fixtures/report.json";
|
||||
const IN_PROGRESS_THREAD_ID = "00000000-0000-0000-0000-000000003119";
|
||||
const COMPLETE_THREAD_ID = "00000000-0000-0000-0000-000000003120";
|
||||
const MARKDOWN_THREAD_ID = "00000000-0000-0000-0000-000000003121";
|
||||
const JSON_THREAD_ID = "00000000-0000-0000-0000-000000003122";
|
||||
|
||||
function writeFileMessages({
|
||||
path = ARTIFACT_PATH,
|
||||
content = "<!doctype html><html><body><h1>Report draft</h1><p>测试内容</p></body></html>",
|
||||
toolResult,
|
||||
}: {
|
||||
path?: string;
|
||||
content?: string;
|
||||
toolResult?: string;
|
||||
} = {}) {
|
||||
const messages: unknown[] = [
|
||||
{
|
||||
type: "human",
|
||||
id: "msg-human-artifact",
|
||||
content: [{ type: "text", text: "Create a report artifact" }],
|
||||
},
|
||||
{
|
||||
type: "ai",
|
||||
id: "msg-ai-write-artifact",
|
||||
content: "",
|
||||
tool_calls: [
|
||||
{
|
||||
id: "write-file-artifact",
|
||||
name: "write_file",
|
||||
args: {
|
||||
description: "Writing report artifact",
|
||||
path,
|
||||
content,
|
||||
},
|
||||
},
|
||||
],
|
||||
},
|
||||
];
|
||||
|
||||
if (toolResult !== undefined) {
|
||||
messages.push({
|
||||
type: "tool",
|
||||
id: "msg-tool-write-artifact",
|
||||
name: "write_file",
|
||||
tool_call_id: "write-file-artifact",
|
||||
content: toolResult,
|
||||
});
|
||||
}
|
||||
|
||||
return messages;
|
||||
}
|
||||
|
||||
test.describe("Artifact preview stability", () => {
|
||||
test("renders preview iframe for an in-progress write artifact", async ({
|
||||
page,
|
||||
}) => {
|
||||
mockLangGraphAPI(page, {
|
||||
threads: [
|
||||
{
|
||||
thread_id: IN_PROGRESS_THREAD_ID,
|
||||
title: "Artifact preview in progress",
|
||||
messages: writeFileMessages(),
|
||||
},
|
||||
],
|
||||
});
|
||||
|
||||
await page.goto(`/workspace/chats/${IN_PROGRESS_THREAD_ID}`);
|
||||
|
||||
await expect(page.getByText(ARTIFACT_PATH)).toBeVisible({
|
||||
timeout: 15_000,
|
||||
});
|
||||
await page.getByText(ARTIFACT_PATH).click();
|
||||
|
||||
const artifactsPanel = page.locator("#artifacts");
|
||||
await expect(artifactsPanel.getByText("report.html")).toBeVisible();
|
||||
await expect(
|
||||
artifactsPanel.locator('iframe[title="Artifact preview"]'),
|
||||
).toBeVisible();
|
||||
});
|
||||
|
||||
test("renders preview iframe after the write artifact succeeds", async ({
|
||||
page,
|
||||
}) => {
|
||||
mockLangGraphAPI(page, {
|
||||
threads: [
|
||||
{
|
||||
thread_id: COMPLETE_THREAD_ID,
|
||||
title: "Artifact preview complete",
|
||||
messages: writeFileMessages({ toolResult: "OK" }),
|
||||
},
|
||||
],
|
||||
});
|
||||
|
||||
await page.goto(`/workspace/chats/${COMPLETE_THREAD_ID}`);
|
||||
|
||||
await expect(page.getByText(ARTIFACT_PATH)).toBeVisible({
|
||||
timeout: 15_000,
|
||||
});
|
||||
await page.getByText(ARTIFACT_PATH).click();
|
||||
|
||||
const artifactsPanel = page.locator("#artifacts");
|
||||
await expect(artifactsPanel.getByText("report.html")).toBeVisible();
|
||||
await expect(
|
||||
artifactsPanel.locator('iframe[title="Artifact preview"]'),
|
||||
).toBeVisible();
|
||||
});
|
||||
|
||||
test("renders markdown preview for an in-progress write artifact", async ({
|
||||
page,
|
||||
}) => {
|
||||
mockLangGraphAPI(page, {
|
||||
threads: [
|
||||
{
|
||||
thread_id: MARKDOWN_THREAD_ID,
|
||||
title: "Markdown artifact preview in progress",
|
||||
messages: writeFileMessages({
|
||||
path: MARKDOWN_ARTIFACT_PATH,
|
||||
content: "# Markdown draft\n\n- 测试内容 1\n- English term",
|
||||
}),
|
||||
},
|
||||
],
|
||||
});
|
||||
|
||||
await page.goto(`/workspace/chats/${MARKDOWN_THREAD_ID}`);
|
||||
|
||||
await expect(page.getByText(MARKDOWN_ARTIFACT_PATH)).toBeVisible({
|
||||
timeout: 15_000,
|
||||
});
|
||||
await page.getByText(MARKDOWN_ARTIFACT_PATH).click();
|
||||
|
||||
const artifactsPanel = page.locator("#artifacts");
|
||||
await expect(artifactsPanel.getByText("report.md")).toBeVisible();
|
||||
await expect(artifactsPanel.getByText("Markdown draft")).toBeVisible();
|
||||
await expect(artifactsPanel.getByText("测试内容 1")).toBeVisible();
|
||||
});
|
||||
|
||||
test("renders code view for an in-progress non-preview write artifact", async ({
|
||||
page,
|
||||
}) => {
|
||||
mockLangGraphAPI(page, {
|
||||
threads: [
|
||||
{
|
||||
thread_id: JSON_THREAD_ID,
|
||||
title: "JSON artifact code view in progress",
|
||||
messages: writeFileMessages({
|
||||
path: JSON_ARTIFACT_PATH,
|
||||
content:
|
||||
'{\n "status": "draft",\n "中文字段": "测试内容",\n "count": 3\n}',
|
||||
}),
|
||||
},
|
||||
],
|
||||
});
|
||||
|
||||
await page.goto(`/workspace/chats/${JSON_THREAD_ID}`);
|
||||
|
||||
await expect(page.getByText(JSON_ARTIFACT_PATH)).toBeVisible({
|
||||
timeout: 15_000,
|
||||
});
|
||||
await page.getByText(JSON_ARTIFACT_PATH).click();
|
||||
|
||||
const artifactsPanel = page.locator("#artifacts");
|
||||
await expect(artifactsPanel.getByText("report.json")).toBeVisible();
|
||||
await expect(artifactsPanel.getByText('"status": "draft"')).toBeVisible();
|
||||
await expect(
|
||||
artifactsPanel.getByText('"中文字段": "测试内容"'),
|
||||
).toBeVisible();
|
||||
});
|
||||
});
|
||||
@@ -25,6 +25,8 @@ export type MockThread = {
|
||||
title?: string;
|
||||
updated_at?: string;
|
||||
agent_name?: string;
|
||||
messages?: unknown[];
|
||||
artifacts?: string[];
|
||||
};
|
||||
|
||||
export type MockAgent = {
|
||||
@@ -113,7 +115,7 @@ export function mockLangGraphAPI(page: Page, options?: MockAPIOptions) {
|
||||
{
|
||||
values: {
|
||||
title: matchingThread.title ?? "Untitled",
|
||||
messages: [
|
||||
messages: matchingThread.messages ?? [
|
||||
{
|
||||
type: "human",
|
||||
id: `msg-human-${matchingThread.thread_id}`,
|
||||
@@ -125,6 +127,7 @@ export function mockLangGraphAPI(page: Page, options?: MockAPIOptions) {
|
||||
content: `Response in thread ${matchingThread.title ?? matchingThread.thread_id}`,
|
||||
},
|
||||
],
|
||||
artifacts: matchingThread.artifacts ?? [],
|
||||
},
|
||||
next: [],
|
||||
metadata: {},
|
||||
@@ -155,7 +158,7 @@ export function mockLangGraphAPI(page: Page, options?: MockAPIOptions) {
|
||||
values: {
|
||||
title: matchingThread?.title ?? "Untitled",
|
||||
messages: matchingThread
|
||||
? [
|
||||
? (matchingThread.messages ?? [
|
||||
{
|
||||
type: "human",
|
||||
id: `msg-human-${matchingThread.thread_id}`,
|
||||
@@ -166,8 +169,9 @@ export function mockLangGraphAPI(page: Page, options?: MockAPIOptions) {
|
||||
id: `msg-ai-${matchingThread.thread_id}`,
|
||||
content: `Response in thread ${matchingThread.title ?? matchingThread.thread_id}`,
|
||||
},
|
||||
]
|
||||
])
|
||||
: [],
|
||||
artifacts: matchingThread?.artifacts ?? [],
|
||||
},
|
||||
next: [],
|
||||
metadata: {},
|
||||
@@ -183,15 +187,59 @@ export function mockLangGraphAPI(page: Page, options?: MockAPIOptions) {
|
||||
// followed by `?` or end-of-string. This must NOT match `/runs/stream`.
|
||||
void page.route(/\/api\/langgraph\/threads\/[^/]+\/runs(\?|$)/, (route) => {
|
||||
if (route.request().method() === "GET") {
|
||||
const url = route.request().url();
|
||||
const matchingThread = threads.find((t) => url.includes(t.thread_id));
|
||||
return route.fulfill({
|
||||
status: 200,
|
||||
contentType: "application/json",
|
||||
body: "[]",
|
||||
body: JSON.stringify(
|
||||
matchingThread
|
||||
? [
|
||||
{
|
||||
run_id: `run-${matchingThread.thread_id}`,
|
||||
thread_id: matchingThread.thread_id,
|
||||
assistant_id: "lead_agent",
|
||||
status: "success",
|
||||
metadata: {},
|
||||
kwargs: {},
|
||||
created_at: "2025-01-01T00:00:00Z",
|
||||
updated_at:
|
||||
matchingThread.updated_at ?? "2025-01-01T00:00:00Z",
|
||||
},
|
||||
]
|
||||
: [],
|
||||
),
|
||||
});
|
||||
}
|
||||
return route.fallback();
|
||||
});
|
||||
|
||||
void page.route(
|
||||
/\/api\/threads\/([^/]+)\/runs\/([^/]+)\/messages/,
|
||||
(route) => {
|
||||
if (route.request().method() === "GET") {
|
||||
const url = route.request().url();
|
||||
const matchingThread = threads.find((t) =>
|
||||
url.includes(`/api/threads/${t.thread_id}/runs/`),
|
||||
);
|
||||
return route.fulfill({
|
||||
status: 200,
|
||||
contentType: "application/json",
|
||||
body: JSON.stringify({
|
||||
data: (matchingThread?.messages ?? []).map((message, index) => ({
|
||||
run_id: `run-${matchingThread?.thread_id ?? "unknown"}`,
|
||||
content: message,
|
||||
metadata: { caller: "lead_agent" },
|
||||
created_at: `2025-01-01T00:00:${String(index).padStart(2, "0")}Z`,
|
||||
})),
|
||||
hasMore: false,
|
||||
}),
|
||||
});
|
||||
}
|
||||
return route.fallback();
|
||||
},
|
||||
);
|
||||
|
||||
// Run stream — returns a minimal SSE response with an AI message
|
||||
void page.route("**/api/langgraph/runs/stream", handleRunStream);
|
||||
void page.route("**/api/langgraph/threads/*/runs/stream", handleRunStream);
|
||||
|
||||
Reference in New Issue
Block a user