fix: preserve investigation execution history by round

This commit is contained in:
2026-10-30 17:07:53 +08:00
parent 1ee21d1c7f
commit 185bf31446
16 changed files with 830 additions and 32 deletions
+66 -4
View File
@@ -21,6 +21,7 @@ export async function mockAgentApi(page: Page, options: MockAgentApiOptions = {}
icon: "bolt" as const
};
const triggeredSessions = new Map<string, { sessionId: string; triggerId: string }>();
const executionDebugSessions = new Map<string, { taskId: string; sourceSessionId: string }>();
await page.route("http://127.0.0.1:8787/api/v1/**", async (route) => {
if (route.request().method() === "OPTIONS") {
await route.fulfill({
@@ -70,6 +71,58 @@ export async function mockAgentApi(page: Page, options: MockAgentApiOptions = {}
return;
}
const executionHistoryMatch = pathname.match(/\/task\/([^/]+)\/execution-session$/);
if (executionHistoryMatch && route.request().method() === "GET") {
const url = new URL(route.request().url());
const sessionId = url.searchParams.get("session_id") ?? "task-execution-session";
await route.fulfill({
headers: corsHeaders,
json: {
id: sessionId,
session_id: sessionId,
title: "水质投诉动态调查 · 执行记录",
purpose: "task_execution",
task_id: executionHistoryMatch[1],
status: "active",
created_at: "2026-09-22T02:00:00Z",
updated_at: "2026-09-22T02:10:00Z",
messages: [
{ id: "execution-user", role: "user", parts: [{ type: "text", text: "执行后台调查任务" }] },
{ id: "execution-assistant", role: "assistant", parts: [{ type: "text", text: "已完成压力与流量证据核对" }] }
],
is_streaming: false,
run_status: "completed",
read_only: true
}
});
return;
}
const executionDebugMatch = pathname.match(/\/task\/([^/]+)\/execution-session\/debug$/);
if (executionDebugMatch && route.request().method() === "POST") {
const body = (route.request().postDataJSON() ?? {}) as { session_id?: string };
const sessionId = `execution-debug-${executionDebugMatch[1]}`;
const created = !executionDebugSessions.has(sessionId);
executionDebugSessions.set(sessionId, {
taskId: executionDebugMatch[1],
sourceSessionId: body.session_id ?? "task-execution-session"
});
await route.fulfill({
status: created ? 201 : 200,
headers: corsHeaders,
json: {
session_id: sessionId,
title: "水质投诉动态调查 · 调试",
purpose: "task_collaboration",
task_id: executionDebugMatch[1],
parent_session_id: body.session_id,
status: "active",
created
}
});
return;
}
if (pathname.endsWith("/session") && route.request().method() === "POST") {
const body = (route.request().postDataJSON() ?? {}) as {
trigger_context?: { trigger_id?: string };
@@ -98,25 +151,34 @@ export async function mockAgentApi(page: Page, options: MockAgentApiOptions = {}
const sessionMatch = pathname.match(/\/session\/([^/]+)$/);
if (sessionMatch && route.request().method() === "GET") {
const collaboration = sessionMatch[1].startsWith("collaboration-");
const executionDebug = executionDebugSessions.get(sessionMatch[1]);
const stored = options.sessions?.find((session) => (session.session_id ?? session.id) === sessionMatch[1]);
await route.fulfill({
headers: corsHeaders,
json: {
id: sessionMatch[1],
session_id: sessionMatch[1],
title: stored?.title ?? (collaboration ? "任务协作问答" : "水质投诉事件调查"),
purpose: stored?.purpose ?? (collaboration ? "task_collaboration" : "ordinary_chat"),
task_id: collaboration ? sessionMatch[1].slice("collaboration-".length) : undefined,
title: stored?.title ?? (executionDebug ? "水质投诉动态调查 · 调试" : collaboration ? "任务协作问答" : "水质投诉事件调查"),
purpose: stored?.purpose ?? (executionDebug || collaboration ? "task_collaboration" : "ordinary_chat"),
task_id: executionDebug?.taskId ?? (collaboration ? sessionMatch[1].slice("collaboration-".length) : undefined),
status: "active",
created_at: "2026-09-22T02:00:00Z",
updated_at: "2026-09-22T02:00:00Z",
messages: [],
messages: executionDebug ? [
{ id: "execution-user", role: "user", parts: [{ type: "text", text: "执行后台调查任务" }] },
{ id: "execution-assistant", role: "assistant", parts: [{ type: "text", text: "已完成压力与流量证据核对" }] }
] : [],
is_streaming: false
}
});
return;
}
if (/\/session\/[^/]+\/interactions$/.test(pathname)) {
await route.fulfill({ headers: corsHeaders, json: { items: [] } });
return;
}
if (pathname.endsWith("/responses")) {
await route.fulfill({
status: 200,