Compare commits
3 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 57369772c7 | |||
| 7764e25398 | |||
| e60e1f6453 |
@@ -165,9 +165,6 @@ export const AgentHistoryPanel = ({
|
||||
<Typography variant="subtitle2" fontWeight={800} color="text.primary">
|
||||
历史会话
|
||||
</Typography>
|
||||
<Typography variant="caption" color="text.secondary">
|
||||
本地保存于浏览器
|
||||
</Typography>
|
||||
</Box>
|
||||
<Tooltip title="新建对话">
|
||||
<motion.div whileHover={{ scale: 1.08 }} whileTap={{ scale: 0.92 }} style={{ display: "flex" }}>
|
||||
|
||||
@@ -30,8 +30,8 @@ describe("AgentProgressTimeline", () => {
|
||||
id: "tool",
|
||||
phase: "tool",
|
||||
status: "running",
|
||||
title: "正在调用 dynamic_http_call",
|
||||
detail: "GET /api/v1/network/bottlenecks",
|
||||
title: "正在调用 tjwater_cli",
|
||||
detail: "analysis bottlenecks",
|
||||
startedAt: now - 1200,
|
||||
elapsedMs: 1200,
|
||||
elapsedSnapshotAt: now,
|
||||
@@ -43,7 +43,7 @@ describe("AgentProgressTimeline", () => {
|
||||
expect(screen.getByText(/Agent 过程:/)).toBeInTheDocument();
|
||||
expect(screen.getByText(/耗时 5.0s/)).toBeInTheDocument();
|
||||
expect(screen.getByText("查询后端数据")).toBeInTheDocument();
|
||||
expect(screen.getByText("GET /api/v1/network/bottlenecks")).toBeInTheDocument();
|
||||
expect(screen.getByText("analysis bottlenecks")).toBeInTheDocument();
|
||||
expect(screen.getByText("1.2s")).toBeInTheDocument();
|
||||
});
|
||||
|
||||
@@ -86,7 +86,7 @@ describe("AgentProgressTimeline", () => {
|
||||
id: "tool",
|
||||
phase: "tool",
|
||||
status: "completed",
|
||||
title: "正在调用 dynamic_http_call",
|
||||
title: "正在调用 tjwater_cli",
|
||||
startedAt: Date.now() - 4000,
|
||||
endedAt: Date.now(),
|
||||
},
|
||||
|
||||
@@ -76,7 +76,7 @@ const phaseIcon = (phase: string, status: ChatProgress["status"]) => {
|
||||
|
||||
const formatToolTitle = (item: ChatProgress) => {
|
||||
const text = `${item.title} ${item.detail ?? ""}`;
|
||||
if (text.includes("dynamic_http_call")) return "查询后端数据";
|
||||
if (text.includes("tjwater_cli")) return "查询后端数据";
|
||||
if (text.includes("show_chart")) return "生成图表";
|
||||
if (text.includes("locate_features")) return "地图定位";
|
||||
if (text.includes("view_history")) return "打开历史曲线";
|
||||
|
||||
@@ -60,7 +60,7 @@ export const GlobalChatbox: React.FC<Props> = ({ open, onClose }) => {
|
||||
const {
|
||||
messages,
|
||||
chatSessions,
|
||||
activeStorageSessionId,
|
||||
activeSessionId,
|
||||
branchGroups,
|
||||
branchTransition,
|
||||
isHydrating,
|
||||
@@ -129,33 +129,33 @@ export const GlobalChatbox: React.FC<Props> = ({ open, onClose }) => {
|
||||
}, []);
|
||||
|
||||
const handleSelectSession = useCallback(
|
||||
(storageSessionId: string) => {
|
||||
(sessionId: string) => {
|
||||
composerRef.current?.clear();
|
||||
void switchSession(storageSessionId);
|
||||
void switchSession(sessionId);
|
||||
},
|
||||
[switchSession],
|
||||
);
|
||||
|
||||
const handleDeleteSession = useCallback(
|
||||
(storageSessionId: string) => {
|
||||
void removeSession(storageSessionId);
|
||||
(sessionId: string) => {
|
||||
void removeSession(sessionId);
|
||||
},
|
||||
[removeSession],
|
||||
);
|
||||
|
||||
const handleRenameSession = useCallback(
|
||||
(storageSessionId: string, title: string) => {
|
||||
void renameSession(storageSessionId, title);
|
||||
(sessionId: string, title: string) => {
|
||||
void renameSession(sessionId, title);
|
||||
},
|
||||
[renameSession],
|
||||
);
|
||||
|
||||
const handleRenameActiveSession = useCallback(
|
||||
(title: string) => {
|
||||
if (!activeStorageSessionId) return;
|
||||
void renameSession(activeStorageSessionId, title);
|
||||
if (!activeSessionId) return;
|
||||
void renameSession(activeSessionId, title);
|
||||
},
|
||||
[activeStorageSessionId, renameSession],
|
||||
[activeSessionId, renameSession],
|
||||
);
|
||||
|
||||
const handleMouseDown = useCallback((event: React.MouseEvent) => {
|
||||
@@ -255,7 +255,7 @@ export const GlobalChatbox: React.FC<Props> = ({ open, onClose }) => {
|
||||
|
||||
<AgentHeader
|
||||
sessionTitle={sessionTitle}
|
||||
canRenameSessionTitle={Boolean(activeStorageSessionId)}
|
||||
canRenameSessionTitle={Boolean(activeSessionId)}
|
||||
isHydrating={isHydrating}
|
||||
isStreaming={isStreaming}
|
||||
isHistoryOpen={isHistoryOpen}
|
||||
@@ -294,7 +294,7 @@ export const GlobalChatbox: React.FC<Props> = ({ open, onClose }) => {
|
||||
>
|
||||
<AgentHistoryPanel
|
||||
sessions={chatSessions}
|
||||
activeSessionId={activeStorageSessionId}
|
||||
activeSessionId={activeSessionId}
|
||||
isHydrating={isHydrating}
|
||||
onNewSession={() => {
|
||||
handleNewConversation();
|
||||
|
||||
@@ -66,41 +66,21 @@ export type Props = {
|
||||
|
||||
export type SpeechState = "idle" | "playing" | "paused";
|
||||
|
||||
export type LegacyPersistedChatState = {
|
||||
messages: Message[];
|
||||
sessionId?: string;
|
||||
branchGroups?: BranchGroup[];
|
||||
};
|
||||
|
||||
export type ChatSessionRecord = {
|
||||
id: string;
|
||||
title: string;
|
||||
isTitleManuallyEdited?: boolean;
|
||||
createdAt: number;
|
||||
updatedAt: number;
|
||||
sessionId?: string;
|
||||
messages: Message[];
|
||||
branchGroups: BranchGroup[];
|
||||
};
|
||||
|
||||
export type ChatSessionSummary = {
|
||||
id: string;
|
||||
title: string;
|
||||
createdAt: number;
|
||||
updatedAt: number;
|
||||
};
|
||||
|
||||
export type ChatStorageMeta = {
|
||||
key: "chat-meta";
|
||||
activeSessionId?: string;
|
||||
migratedFromLocalStorage?: boolean;
|
||||
isStreaming?: boolean;
|
||||
runStatus?: string;
|
||||
};
|
||||
|
||||
export type LoadedChatState = {
|
||||
storageSessionId?: string;
|
||||
sessionId?: string;
|
||||
title?: string;
|
||||
isTitleManuallyEdited?: boolean;
|
||||
messages: Message[];
|
||||
sessionId?: string;
|
||||
branchGroups: BranchGroup[];
|
||||
isStreaming?: boolean;
|
||||
runStatus?: string;
|
||||
};
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import {
|
||||
loadActiveChatState,
|
||||
createEmptyChatState,
|
||||
saveActiveChatState,
|
||||
} from "./chatStorage";
|
||||
|
||||
@@ -11,17 +11,13 @@ jest.mock("@/lib/apiFetch", () => ({
|
||||
|
||||
describe("chatStorage backend-only persistence", () => {
|
||||
beforeEach(() => {
|
||||
window.localStorage.clear();
|
||||
apiFetch.mockReset();
|
||||
});
|
||||
|
||||
it("starts from an empty conversation instead of restoring a stored active id", async () => {
|
||||
window.localStorage.setItem("tjwater_agent_active_session_id_v2", "chat-active-1");
|
||||
|
||||
const loaded = await loadActiveChatState();
|
||||
it("creates an empty initial conversation state without backend calls", () => {
|
||||
const loaded = createEmptyChatState();
|
||||
|
||||
expect(loaded).toMatchObject({
|
||||
storageSessionId: undefined,
|
||||
title: undefined,
|
||||
messages: [],
|
||||
sessionId: undefined,
|
||||
@@ -30,24 +26,6 @@ describe("chatStorage backend-only persistence", () => {
|
||||
expect(apiFetch).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it("starts from an empty conversation when a project has a stored active id", async () => {
|
||||
window.localStorage.setItem(
|
||||
"tjwater_agent_active_session_id_v2:project-a",
|
||||
"chat-project-a",
|
||||
);
|
||||
window.localStorage.setItem(
|
||||
"tjwater_agent_active_session_id_v2:project-b",
|
||||
"chat-project-b",
|
||||
);
|
||||
|
||||
const loaded = await loadActiveChatState("project-b");
|
||||
|
||||
expect(loaded.storageSessionId).toBeUndefined();
|
||||
expect(loaded.title).toBeUndefined();
|
||||
expect(loaded.messages).toEqual([]);
|
||||
expect(apiFetch).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it("creates a backend conversation when saving the first non-empty state", async () => {
|
||||
apiFetch.mockImplementation(async (url: string, init?: RequestInit) => {
|
||||
if (url.endsWith("/api/v1/agent/chat/session")) {
|
||||
@@ -75,7 +53,6 @@ describe("chatStorage backend-only persistence", () => {
|
||||
|
||||
const savedSessionId = await saveActiveChatState(
|
||||
{
|
||||
storageSessionId: undefined,
|
||||
title: "新对话",
|
||||
isTitleManuallyEdited: false,
|
||||
messages: [
|
||||
@@ -89,13 +66,8 @@ describe("chatStorage backend-only persistence", () => {
|
||||
sessionId: undefined,
|
||||
branchGroups: [],
|
||||
},
|
||||
"project-a",
|
||||
);
|
||||
|
||||
expect(savedSessionId).toBe("chat-new-1");
|
||||
expect(
|
||||
window.localStorage.getItem("tjwater_agent_active_session_id_v2:project-a"),
|
||||
).toBeNull();
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
@@ -9,15 +9,16 @@ import type {
|
||||
} from "./GlobalChatbox.types";
|
||||
import { cloneBranchGroups, cloneMessages } from "./GlobalChatbox.utils";
|
||||
|
||||
type RemoteSessionPayload = {
|
||||
type BackendSessionPayload = {
|
||||
id?: string;
|
||||
title?: string;
|
||||
created_at?: string | number;
|
||||
updated_at?: string | number;
|
||||
is_streaming?: boolean;
|
||||
run_status?: string;
|
||||
};
|
||||
|
||||
const emptyLoadedChatState = (): LoadedChatState => ({
|
||||
storageSessionId: undefined,
|
||||
export const createEmptyChatState = (): LoadedChatState => ({
|
||||
title: undefined,
|
||||
isTitleManuallyEdited: false,
|
||||
messages: [],
|
||||
@@ -58,7 +59,7 @@ const toMillis = (value: string | number | undefined) =>
|
||||
|
||||
const normalizeTitle = (value?: string) => value?.trim() || "新对话";
|
||||
|
||||
const fetchRemoteChatSessions = async (): Promise<ChatSessionSummary[]> => {
|
||||
const fetchBackendChatSessions = async (): Promise<ChatSessionSummary[]> => {
|
||||
const response = await apiFetch(`${config.AGENT_URL}/api/v1/agent/chat/sessions`, {
|
||||
method: "GET",
|
||||
projectHeaderMode: "include",
|
||||
@@ -69,7 +70,7 @@ const fetchRemoteChatSessions = async (): Promise<ChatSessionSummary[]> => {
|
||||
throw new Error(await response.text());
|
||||
}
|
||||
const payload = (await response.json()) as {
|
||||
sessions?: RemoteSessionPayload[];
|
||||
sessions?: BackendSessionPayload[];
|
||||
};
|
||||
return (payload.sessions ?? [])
|
||||
.map((session) => ({
|
||||
@@ -77,12 +78,14 @@ const fetchRemoteChatSessions = async (): Promise<ChatSessionSummary[]> => {
|
||||
title: normalizeTitle(session.title),
|
||||
createdAt: toMillis(session.created_at),
|
||||
updatedAt: toMillis(session.updated_at),
|
||||
isStreaming: session.is_streaming,
|
||||
runStatus: session.run_status,
|
||||
}))
|
||||
.filter((session) => Boolean(session.id))
|
||||
.sort(compareSessionsByAnchorTime);
|
||||
};
|
||||
|
||||
const fetchRemoteChatSession = async (sessionId: string): Promise<LoadedChatState> => {
|
||||
const fetchBackendChatSession = async (sessionId: string): Promise<LoadedChatState> => {
|
||||
const response = await apiFetch(
|
||||
`${config.AGENT_URL}/api/v1/agent/chat/session/${encodeURIComponent(sessionId)}`,
|
||||
{
|
||||
@@ -94,7 +97,7 @@ const fetchRemoteChatSession = async (sessionId: string): Promise<LoadedChatStat
|
||||
);
|
||||
if (!response.ok) {
|
||||
if (response.status === 404) {
|
||||
return emptyLoadedChatState();
|
||||
return createEmptyChatState();
|
||||
}
|
||||
throw new Error(await response.text());
|
||||
}
|
||||
@@ -105,18 +108,21 @@ const fetchRemoteChatSession = async (sessionId: string): Promise<LoadedChatStat
|
||||
session_id?: string;
|
||||
messages?: Message[];
|
||||
branch_groups?: BranchGroup[];
|
||||
is_streaming?: boolean;
|
||||
run_status?: string;
|
||||
};
|
||||
return {
|
||||
storageSessionId: payload.id,
|
||||
title: normalizeTitle(payload.title),
|
||||
isTitleManuallyEdited: payload.is_title_manually_edited ?? false,
|
||||
messages: sanitizeMessages(payload.messages),
|
||||
sessionId: payload.session_id,
|
||||
sessionId: payload.session_id ?? payload.id,
|
||||
branchGroups: sanitizeBranchGroups(payload.branch_groups),
|
||||
isStreaming: payload.is_streaming ?? false,
|
||||
runStatus: payload.run_status,
|
||||
};
|
||||
};
|
||||
|
||||
const createRemoteChatSession = async (payload?: {
|
||||
const createBackendChatSession = async (payload?: {
|
||||
sessionId?: string;
|
||||
parentSessionId?: string;
|
||||
}) => {
|
||||
@@ -146,7 +152,7 @@ const createRemoteChatSession = async (payload?: {
|
||||
return sessionId;
|
||||
};
|
||||
|
||||
const saveRemoteChatState = async (
|
||||
const saveBackendChatState = async (
|
||||
sessionId: string,
|
||||
state: LoadedChatState,
|
||||
): Promise<string> => {
|
||||
@@ -175,7 +181,7 @@ const saveRemoteChatState = async (
|
||||
return payload.id ?? payload.session_id ?? sessionId;
|
||||
};
|
||||
|
||||
const updateRemoteChatSessionTitle = async (
|
||||
const updateBackendChatSessionTitle = async (
|
||||
sessionId: string,
|
||||
title: string,
|
||||
isTitleManuallyEdited?: boolean,
|
||||
@@ -201,7 +207,7 @@ const updateRemoteChatSessionTitle = async (
|
||||
}
|
||||
};
|
||||
|
||||
const deleteRemoteChatSession = async (sessionId: string) => {
|
||||
const deleteBackendChatSession = async (sessionId: string) => {
|
||||
const response = await apiFetch(
|
||||
`${config.AGENT_URL}/api/v1/agent/chat/session/${encodeURIComponent(sessionId)}`,
|
||||
{
|
||||
@@ -216,42 +222,34 @@ const deleteRemoteChatSession = async (sessionId: string) => {
|
||||
}
|
||||
};
|
||||
|
||||
export const loadActiveChatState = async (
|
||||
_projectId?: string | null,
|
||||
): Promise<LoadedChatState> => {
|
||||
return emptyLoadedChatState();
|
||||
};
|
||||
|
||||
export const saveActiveChatState = async (
|
||||
state: LoadedChatState,
|
||||
_projectId?: string | null,
|
||||
): Promise<string | undefined> => {
|
||||
if (typeof window === "undefined") return state.storageSessionId;
|
||||
if (typeof window === "undefined") return state.sessionId;
|
||||
|
||||
if (!hasChatContent(state)) {
|
||||
return undefined;
|
||||
}
|
||||
|
||||
let remoteSessionId = state.sessionId ?? state.storageSessionId;
|
||||
if (!remoteSessionId) {
|
||||
remoteSessionId = await createRemoteChatSession();
|
||||
let backendSessionId = state.sessionId;
|
||||
if (!backendSessionId) {
|
||||
backendSessionId = await createBackendChatSession();
|
||||
}
|
||||
|
||||
const savedSessionId = await saveRemoteChatState(remoteSessionId, {
|
||||
const savedSessionId = await saveBackendChatState(backendSessionId, {
|
||||
...state,
|
||||
storageSessionId: remoteSessionId,
|
||||
sessionId: remoteSessionId,
|
||||
sessionId: backendSessionId,
|
||||
});
|
||||
return savedSessionId;
|
||||
};
|
||||
|
||||
export const listChatSessions = async (): Promise<ChatSessionSummary[]> => {
|
||||
if (typeof window === "undefined") return [];
|
||||
return await fetchRemoteChatSessions();
|
||||
return await fetchBackendChatSessions();
|
||||
};
|
||||
|
||||
export const updateChatSessionTitle = async (
|
||||
storageSessionId: string,
|
||||
sessionId: string,
|
||||
title: string,
|
||||
options?: {
|
||||
isTitleManuallyEdited?: boolean;
|
||||
@@ -261,8 +259,8 @@ export const updateChatSessionTitle = async (
|
||||
|
||||
const normalizedTitle = title.trim();
|
||||
if (!normalizedTitle) return;
|
||||
await updateRemoteChatSessionTitle(
|
||||
storageSessionId,
|
||||
await updateBackendChatSessionTitle(
|
||||
sessionId,
|
||||
normalizedTitle,
|
||||
options?.isTitleManuallyEdited,
|
||||
);
|
||||
@@ -270,20 +268,18 @@ export const updateChatSessionTitle = async (
|
||||
|
||||
export const loadChatSessionById = async (
|
||||
sessionId: string,
|
||||
_projectId?: string | null,
|
||||
): Promise<LoadedChatState> => {
|
||||
if (typeof window === "undefined") return emptyLoadedChatState();
|
||||
if (typeof window === "undefined") return createEmptyChatState();
|
||||
|
||||
return await fetchRemoteChatSession(sessionId);
|
||||
return await fetchBackendChatSession(sessionId);
|
||||
};
|
||||
|
||||
export const deleteChatSession = async (
|
||||
sessionId: string,
|
||||
_projectId?: string | null,
|
||||
): Promise<string | undefined> => {
|
||||
if (typeof window === "undefined") return undefined;
|
||||
|
||||
await deleteRemoteChatSession(sessionId);
|
||||
await deleteBackendChatSession(sessionId);
|
||||
const nextActiveSession = (await listChatSessions())[0];
|
||||
return nextActiveSession?.id;
|
||||
};
|
||||
|
||||
@@ -3,53 +3,53 @@
|
||||
import { act, renderHook, waitFor } from "@testing-library/react";
|
||||
|
||||
import { useAgentChatSession } from "./useAgentChatSession";
|
||||
import { streamAgentChat } from "@/lib/chatStream";
|
||||
import { abortAgentChat, resumeAgentChatStream, streamAgentChat } from "@/lib/chatStream";
|
||||
import type { StreamEvent } from "@/lib/chatStream";
|
||||
|
||||
jest.mock("@/lib/chatStream", () => ({
|
||||
abortAgentChat: jest.fn(async () => undefined),
|
||||
forkAgentChat: jest.fn(async () => "forked-session"),
|
||||
resumeAgentChatStream: jest.fn(async () => undefined),
|
||||
streamAgentChat: jest.fn(async () => undefined),
|
||||
}));
|
||||
|
||||
const loadActiveChatState = jest.fn();
|
||||
const listChatSessions = jest.fn();
|
||||
const saveActiveChatState = jest.fn();
|
||||
const updateChatSessionTitle = jest.fn();
|
||||
|
||||
jest.mock("../chatStorage", () => ({
|
||||
deleteChatSession: jest.fn(async () => undefined),
|
||||
listChatSessions: (...args: unknown[]) => listChatSessions(...args),
|
||||
loadActiveChatState: (...args: unknown[]) => loadActiveChatState(...args),
|
||||
loadChatSessionById: jest.fn(async () => ({
|
||||
storageSessionId: "session-loaded",
|
||||
title: "已存在会话",
|
||||
createEmptyChatState: jest.fn(() => ({
|
||||
title: undefined,
|
||||
isTitleManuallyEdited: false,
|
||||
messages: [],
|
||||
sessionId: undefined,
|
||||
branchGroups: [],
|
||||
})),
|
||||
deleteChatSession: jest.fn(async () => undefined),
|
||||
listChatSessions: (...args: unknown[]) => listChatSessions(...args),
|
||||
loadChatSessionById: jest.fn(async () => ({
|
||||
title: "已存在会话",
|
||||
isTitleManuallyEdited: false,
|
||||
messages: [],
|
||||
sessionId: "session-loaded",
|
||||
branchGroups: [],
|
||||
})),
|
||||
saveActiveChatState: (...args: unknown[]) => saveActiveChatState(...args),
|
||||
updateChatSessionTitle: (...args: unknown[]) => updateChatSessionTitle(...args),
|
||||
}));
|
||||
|
||||
describe("useAgentChatSession", () => {
|
||||
beforeEach(() => {
|
||||
loadActiveChatState.mockReset();
|
||||
listChatSessions.mockReset();
|
||||
saveActiveChatState.mockReset();
|
||||
updateChatSessionTitle.mockReset();
|
||||
jest.mocked(abortAgentChat).mockReset();
|
||||
jest.mocked(resumeAgentChatStream).mockReset();
|
||||
jest.mocked(streamAgentChat).mockReset();
|
||||
saveActiveChatState.mockImplementation(async (state) => state.storageSessionId);
|
||||
|
||||
loadActiveChatState.mockResolvedValue({
|
||||
storageSessionId: undefined,
|
||||
title: undefined,
|
||||
isTitleManuallyEdited: false,
|
||||
messages: [],
|
||||
sessionId: undefined,
|
||||
branchGroups: [],
|
||||
});
|
||||
jest.mocked(abortAgentChat).mockImplementation(async () => undefined);
|
||||
jest.mocked(resumeAgentChatStream).mockImplementation(async () => undefined);
|
||||
jest.mocked(streamAgentChat).mockImplementation(async () => undefined);
|
||||
saveActiveChatState.mockImplementation(async (state) => state.sessionId);
|
||||
});
|
||||
|
||||
it("does not add a new empty session to history until there is actual chat content", async () => {
|
||||
@@ -70,7 +70,7 @@ describe("useAgentChatSession", () => {
|
||||
|
||||
await waitFor(() => expect(result.current.sessionTitle).toBe("新对话"));
|
||||
expect(result.current.chatSessions).toEqual([]);
|
||||
expect(result.current.activeStorageSessionId).toBeUndefined();
|
||||
expect(result.current.activeSessionId).toBeUndefined();
|
||||
expect(result.current.messages).toEqual([]);
|
||||
expect(result.current.isStreaming).toBe(false);
|
||||
expect(listChatSessions).toHaveBeenCalledTimes(1);
|
||||
@@ -109,7 +109,7 @@ describe("useAgentChatSession", () => {
|
||||
]);
|
||||
});
|
||||
|
||||
it("waits for the stream session id before persisting a new streaming conversation", async () => {
|
||||
it("persists a new conversation only after the stream is done", async () => {
|
||||
listChatSessions.mockResolvedValue([]);
|
||||
let emitStreamEvent: ((event: StreamEvent) => void) | undefined;
|
||||
jest.mocked(streamAgentChat).mockImplementationOnce(async ({ onEvent }) => {
|
||||
@@ -153,25 +153,142 @@ describe("useAgentChatSession", () => {
|
||||
jest.advanceTimersByTime(200);
|
||||
});
|
||||
|
||||
expect(saveActiveChatState).toHaveBeenCalledTimes(1);
|
||||
expect(saveActiveChatState).not.toHaveBeenCalled();
|
||||
|
||||
act(() => {
|
||||
emitStreamEvent?.({
|
||||
type: "done",
|
||||
sessionId: "chat-stream-1",
|
||||
});
|
||||
});
|
||||
|
||||
await act(async () => {
|
||||
jest.advanceTimersByTime(200);
|
||||
});
|
||||
|
||||
await waitFor(() => expect(saveActiveChatState).toHaveBeenCalledTimes(1));
|
||||
expect(saveActiveChatState.mock.calls[0][0]).toMatchObject({
|
||||
sessionId: "chat-stream-1",
|
||||
messages: [
|
||||
expect.objectContaining({ role: "user", content: "第一条消息" }),
|
||||
expect.objectContaining({ role: "assistant", content: "收到" }),
|
||||
],
|
||||
});
|
||||
} finally {
|
||||
jest.useRealTimers();
|
||||
}
|
||||
});
|
||||
|
||||
it("hydrates a backend streaming session and resumes its stream", async () => {
|
||||
listChatSessions.mockResolvedValue([
|
||||
{
|
||||
id: "session-streaming",
|
||||
title: "运行中",
|
||||
createdAt: 1,
|
||||
updatedAt: 2,
|
||||
isStreaming: true,
|
||||
runStatus: "running",
|
||||
},
|
||||
]);
|
||||
|
||||
const { result } = renderHook(() =>
|
||||
useAgentChatSession({
|
||||
projectId: "project-1",
|
||||
onToolCall: jest.fn(),
|
||||
}),
|
||||
);
|
||||
|
||||
await waitFor(() => expect(result.current.isHydrating).toBe(false));
|
||||
|
||||
expect(result.current.isStreaming).toBe(true);
|
||||
expect(result.current.activeSessionId).toBe("session-loaded");
|
||||
expect(resumeAgentChatStream).toHaveBeenCalledWith(
|
||||
expect.objectContaining({
|
||||
sessionId: "session-loaded",
|
||||
}),
|
||||
);
|
||||
});
|
||||
|
||||
it("updates resumed messages from state, token, and done events", async () => {
|
||||
listChatSessions.mockResolvedValue([
|
||||
{
|
||||
id: "session-streaming",
|
||||
title: "运行中",
|
||||
createdAt: 1,
|
||||
updatedAt: 2,
|
||||
isStreaming: true,
|
||||
},
|
||||
]);
|
||||
jest.mocked(resumeAgentChatStream).mockImplementationOnce(async ({ onEvent }) => {
|
||||
onEvent({
|
||||
type: "state",
|
||||
sessionId: "session-loaded",
|
||||
messages: [
|
||||
{ id: "u1", role: "user", content: "继续分析" },
|
||||
{ id: "a1", role: "assistant", content: "已有" },
|
||||
],
|
||||
isStreaming: true,
|
||||
runStatus: "running",
|
||||
});
|
||||
onEvent({
|
||||
type: "token",
|
||||
sessionId: "session-loaded",
|
||||
content: "输出",
|
||||
});
|
||||
onEvent({
|
||||
type: "done",
|
||||
sessionId: "session-loaded",
|
||||
});
|
||||
});
|
||||
|
||||
const { result } = renderHook(() =>
|
||||
useAgentChatSession({
|
||||
projectId: "project-1",
|
||||
onToolCall: jest.fn(),
|
||||
}),
|
||||
);
|
||||
|
||||
await waitFor(() => expect(result.current.isHydrating).toBe(false));
|
||||
await waitFor(() => expect(result.current.isStreaming).toBe(false));
|
||||
|
||||
expect(result.current.messages).toEqual([
|
||||
expect.objectContaining({ id: "u1", role: "user", content: "继续分析" }),
|
||||
expect.objectContaining({ id: "a1", role: "assistant", content: "已有输出" }),
|
||||
]);
|
||||
});
|
||||
|
||||
it("aborts a resumed streaming session through the backend abort endpoint", async () => {
|
||||
listChatSessions.mockResolvedValue([
|
||||
{
|
||||
id: "session-streaming",
|
||||
title: "运行中",
|
||||
createdAt: 1,
|
||||
updatedAt: 2,
|
||||
isStreaming: true,
|
||||
},
|
||||
]);
|
||||
jest.mocked(resumeAgentChatStream).mockImplementationOnce(async () => {
|
||||
await new Promise<void>(() => undefined);
|
||||
});
|
||||
|
||||
const { result } = renderHook(() =>
|
||||
useAgentChatSession({
|
||||
projectId: "project-1",
|
||||
onToolCall: jest.fn(),
|
||||
}),
|
||||
);
|
||||
|
||||
await waitFor(() => expect(result.current.isStreaming).toBe(true));
|
||||
|
||||
act(() => {
|
||||
result.current.abort();
|
||||
});
|
||||
|
||||
expect(abortAgentChat).toHaveBeenCalledWith("session-loaded");
|
||||
});
|
||||
|
||||
it("ignores generated session titles after the title was edited manually", async () => {
|
||||
listChatSessions.mockResolvedValue([]);
|
||||
loadActiveChatState.mockResolvedValue({
|
||||
storageSessionId: "session-1",
|
||||
title: "手动标题",
|
||||
isTitleManuallyEdited: true,
|
||||
messages: [],
|
||||
sessionId: "session-1",
|
||||
branchGroups: [],
|
||||
});
|
||||
jest.mocked(streamAgentChat).mockImplementationOnce(async ({ onEvent }) => {
|
||||
onEvent({
|
||||
type: "session_title",
|
||||
@@ -193,13 +310,23 @@ describe("useAgentChatSession", () => {
|
||||
|
||||
await waitFor(() => expect(result.current.isHydrating).toBe(false));
|
||||
|
||||
await act(async () => {
|
||||
await result.current.switchSession("session-loaded");
|
||||
});
|
||||
|
||||
await act(async () => {
|
||||
await result.current.renameSession("session-loaded", "手动标题");
|
||||
});
|
||||
|
||||
await waitFor(() => expect(updateChatSessionTitle).toHaveBeenCalled());
|
||||
|
||||
await act(async () => {
|
||||
await result.current.sendPrompt("帮我分析一下");
|
||||
});
|
||||
|
||||
expect(result.current.sessionTitle).toBe("手动标题");
|
||||
expect(updateChatSessionTitle).not.toHaveBeenCalledWith(
|
||||
"session-1",
|
||||
"session-loaded",
|
||||
"自动标题",
|
||||
expect.anything(),
|
||||
);
|
||||
|
||||
@@ -2,7 +2,12 @@
|
||||
|
||||
import { useCallback, useEffect, useRef, useState } from "react";
|
||||
|
||||
import { abortAgentChat, forkAgentChat, streamAgentChat } from "@/lib/chatStream";
|
||||
import {
|
||||
abortAgentChat,
|
||||
forkAgentChat,
|
||||
resumeAgentChatStream,
|
||||
streamAgentChat,
|
||||
} from "@/lib/chatStream";
|
||||
import type { AgentModel, StreamEvent } from "@/lib/chatStream";
|
||||
import type {
|
||||
AgentArtifact,
|
||||
@@ -19,9 +24,9 @@ import {
|
||||
createId,
|
||||
} from "../GlobalChatbox.utils";
|
||||
import {
|
||||
createEmptyChatState,
|
||||
deleteChatSession,
|
||||
listChatSessions,
|
||||
loadActiveChatState,
|
||||
loadChatSessionById,
|
||||
saveActiveChatState,
|
||||
updateChatSessionTitle,
|
||||
@@ -50,7 +55,6 @@ type PromptRunOptions = {
|
||||
|
||||
const createPersistedStateKey = (state: LoadedChatState) =>
|
||||
JSON.stringify({
|
||||
storageSessionId: state.storageSessionId ?? null,
|
||||
title: state.title ?? null,
|
||||
isTitleManuallyEdited: state.isTitleManuallyEdited ?? false,
|
||||
sessionId: state.sessionId ?? null,
|
||||
@@ -151,7 +155,6 @@ export const useAgentChatSession = ({
|
||||
onBeforeSend,
|
||||
getModel,
|
||||
}: UseAgentChatSessionOptions) => {
|
||||
const storageSessionIdRef = useRef<string | undefined>(undefined);
|
||||
const hydrationCompletedRef = useRef(false);
|
||||
const hydrationNonceRef = useRef(0);
|
||||
|
||||
@@ -166,16 +169,17 @@ export const useAgentChatSession = ({
|
||||
const [isHydrating, setIsHydrating] = useState(true);
|
||||
const abortRef = useRef<AbortController | null>(null);
|
||||
const sessionIdRef = useRef<string | undefined>(undefined);
|
||||
const messagesRef = useRef<Message[]>([]);
|
||||
const resumeStreamingSessionRef = useRef<((sessionId: string) => void) | null>(null);
|
||||
const isSessionTitleManuallyEditedRef = useRef(false);
|
||||
const cancelPromiseRef = useRef<Promise<void> | null>(null);
|
||||
const titleUpdateNonceRef = useRef(0);
|
||||
const lastPersistedStateKeyRef = useRef(
|
||||
createPersistedStateKey({
|
||||
storageSessionId: undefined,
|
||||
sessionId: undefined,
|
||||
title: undefined,
|
||||
isTitleManuallyEdited: false,
|
||||
messages: [],
|
||||
sessionId: undefined,
|
||||
branchGroups: [],
|
||||
}),
|
||||
);
|
||||
@@ -184,6 +188,10 @@ export const useAgentChatSession = ({
|
||||
sessionIdRef.current = sessionId;
|
||||
}, [sessionId]);
|
||||
|
||||
useEffect(() => {
|
||||
messagesRef.current = messages;
|
||||
}, [messages]);
|
||||
|
||||
useEffect(() => {
|
||||
isSessionTitleManuallyEditedRef.current = isSessionTitleManuallyEdited;
|
||||
}, [isSessionTitleManuallyEdited]);
|
||||
@@ -196,10 +204,8 @@ export const useAgentChatSession = ({
|
||||
hydrationCompletedRef.current = false;
|
||||
|
||||
if (!projectId) {
|
||||
storageSessionIdRef.current = undefined;
|
||||
sessionIdRef.current = undefined;
|
||||
lastPersistedStateKeyRef.current = createPersistedStateKey({
|
||||
storageSessionId: undefined,
|
||||
title: undefined,
|
||||
isTitleManuallyEdited: false,
|
||||
messages: [],
|
||||
@@ -221,13 +227,13 @@ export const useAgentChatSession = ({
|
||||
}
|
||||
|
||||
try {
|
||||
const [loadedState, sessions] = await Promise.all([
|
||||
loadActiveChatState(projectId),
|
||||
listChatSessions(),
|
||||
]);
|
||||
const sessions = await listChatSessions();
|
||||
const streamingSession = sessions.find((session) => session.isStreaming);
|
||||
const loadedState = streamingSession
|
||||
? await loadChatSessionById(streamingSession.id)
|
||||
: createEmptyChatState();
|
||||
if (cancelled) return;
|
||||
|
||||
storageSessionIdRef.current = loadedState.storageSessionId;
|
||||
sessionIdRef.current = loadedState.sessionId;
|
||||
lastPersistedStateKeyRef.current = createPersistedStateKey(loadedState);
|
||||
hydrationCompletedRef.current = true;
|
||||
@@ -240,6 +246,12 @@ export const useAgentChatSession = ({
|
||||
setSessionId(loadedState.sessionId);
|
||||
setBranchGroups(loadedState.branchGroups);
|
||||
setChatSessions(sessions);
|
||||
if (
|
||||
loadedState.sessionId &&
|
||||
(loadedState.isStreaming || streamingSession?.isStreaming)
|
||||
) {
|
||||
resumeStreamingSessionRef.current?.(loadedState.sessionId);
|
||||
}
|
||||
} catch (error) {
|
||||
console.error("[GlobalChatbox] Failed to hydrate chat state:", error);
|
||||
} finally {
|
||||
@@ -261,35 +273,30 @@ export const useAgentChatSession = ({
|
||||
|
||||
const currentHydrationNonce = hydrationNonceRef.current;
|
||||
const persistTimer = window.setTimeout(() => {
|
||||
if (isStreaming) {
|
||||
return;
|
||||
}
|
||||
|
||||
const state: LoadedChatState = {
|
||||
storageSessionId: storageSessionIdRef.current,
|
||||
title: sessionTitle,
|
||||
isTitleManuallyEdited: isSessionTitleManuallyEdited,
|
||||
messages,
|
||||
sessionId,
|
||||
branchGroups,
|
||||
};
|
||||
if (
|
||||
isStreaming &&
|
||||
!state.storageSessionId &&
|
||||
!state.sessionId &&
|
||||
state.messages.length > 0
|
||||
) {
|
||||
return;
|
||||
}
|
||||
|
||||
const currentStateKey = createPersistedStateKey(state);
|
||||
if (currentStateKey === lastPersistedStateKeyRef.current) {
|
||||
return;
|
||||
}
|
||||
|
||||
void saveActiveChatState(state, projectId)
|
||||
.then((storageSessionId) => {
|
||||
void saveActiveChatState(state)
|
||||
.then((sessionId) => {
|
||||
if (hydrationNonceRef.current !== currentHydrationNonce) return;
|
||||
storageSessionIdRef.current = storageSessionId;
|
||||
sessionIdRef.current = sessionId;
|
||||
lastPersistedStateKeyRef.current = createPersistedStateKey({
|
||||
...state,
|
||||
storageSessionId,
|
||||
sessionId,
|
||||
});
|
||||
return listChatSessions();
|
||||
})
|
||||
@@ -359,6 +366,150 @@ export const useAgentChatSession = ({
|
||||
);
|
||||
}, []);
|
||||
|
||||
const getLastAssistantMessageId = useCallback((fallback?: string) => {
|
||||
const assistant = [...messagesRef.current]
|
||||
.reverse()
|
||||
.find((message) => message.role === "assistant");
|
||||
return assistant?.id ?? fallback;
|
||||
}, []);
|
||||
|
||||
const applyStreamEvent = useCallback(
|
||||
(
|
||||
event: StreamEvent,
|
||||
options?: {
|
||||
assistantMessageId?: string;
|
||||
},
|
||||
) => {
|
||||
if ("sessionId" in event && event.sessionId && event.sessionId !== sessionIdRef.current) {
|
||||
sessionIdRef.current = event.sessionId;
|
||||
setSessionId(event.sessionId);
|
||||
}
|
||||
|
||||
if (event.type === "state") {
|
||||
const nextMessages = cloneMessages(event.messages as Message[]);
|
||||
messagesRef.current = nextMessages;
|
||||
setMessages(nextMessages);
|
||||
setIsStreaming(event.isStreaming);
|
||||
return;
|
||||
}
|
||||
|
||||
const assistantMessageId = getLastAssistantMessageId(options?.assistantMessageId);
|
||||
if (!assistantMessageId) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (event.type === "token") {
|
||||
setMessages((prev) =>
|
||||
prev.map((message) =>
|
||||
message.id === assistantMessageId
|
||||
? {
|
||||
...message,
|
||||
content: message.content + event.content,
|
||||
isError: false,
|
||||
}
|
||||
: message,
|
||||
),
|
||||
);
|
||||
} else if (event.type === "progress") {
|
||||
setMessages((prev) =>
|
||||
prev.map((message) =>
|
||||
message.id === assistantMessageId
|
||||
? { ...message, progress: upsertProgress(message.progress, event) }
|
||||
: message,
|
||||
),
|
||||
);
|
||||
} else if (event.type === "tool_call") {
|
||||
onToolCall(event, {
|
||||
assistantMessageId,
|
||||
appendArtifact,
|
||||
});
|
||||
} else if (event.type === "session_title") {
|
||||
const nextTitle = event.title.trim();
|
||||
if (nextTitle && !isSessionTitleManuallyEditedRef.current) {
|
||||
setSessionTitle(nextTitle);
|
||||
const currentSessionId = sessionIdRef.current;
|
||||
if (currentSessionId) {
|
||||
const currentNonce = ++titleUpdateNonceRef.current;
|
||||
void updateChatSessionTitle(currentSessionId, nextTitle, {
|
||||
isTitleManuallyEdited: false,
|
||||
})
|
||||
.then(() => listChatSessions())
|
||||
.then((sessions) => {
|
||||
if (titleUpdateNonceRef.current !== currentNonce) return;
|
||||
setChatSessions(sessions);
|
||||
})
|
||||
.catch((error) => {
|
||||
console.error("[GlobalChatbox] Failed to persist session title:", error);
|
||||
});
|
||||
}
|
||||
}
|
||||
} else if (event.type === "done") {
|
||||
setMessages((prev) =>
|
||||
prev.map((message) => {
|
||||
if (message.id !== assistantMessageId) return message;
|
||||
const completedProgress = completeRunningProgress(message.progress);
|
||||
if (
|
||||
message.content.trim().length === 0 &&
|
||||
!(message.artifacts?.length)
|
||||
) {
|
||||
return {
|
||||
...message,
|
||||
content:
|
||||
"Agent 已完成处理,但没有生成文本回答。请查看过程记录,或换个更具体的问题重试。",
|
||||
progress: completedProgress,
|
||||
};
|
||||
}
|
||||
return { ...message, progress: completedProgress };
|
||||
}),
|
||||
);
|
||||
setIsStreaming(false);
|
||||
} else if (event.type === "error") {
|
||||
setMessages((prev) =>
|
||||
prev.map((message) =>
|
||||
message.id === assistantMessageId
|
||||
? {
|
||||
...message,
|
||||
content: message.content || `⚠️ **错误:** ${event.message}`,
|
||||
isError: true,
|
||||
progress: completeRunningProgress(message.progress),
|
||||
}
|
||||
: message,
|
||||
),
|
||||
);
|
||||
setIsStreaming(false);
|
||||
}
|
||||
},
|
||||
[appendArtifact, getLastAssistantMessageId, onToolCall],
|
||||
);
|
||||
|
||||
const resumeStreamingSession = useCallback(
|
||||
(nextSessionId: string) => {
|
||||
const controller = new AbortController();
|
||||
abortRef.current?.abort();
|
||||
abortRef.current = controller;
|
||||
setIsStreaming(true);
|
||||
|
||||
void resumeAgentChatStream({
|
||||
sessionId: nextSessionId,
|
||||
signal: controller.signal,
|
||||
onEvent: (event) => applyStreamEvent(event),
|
||||
})
|
||||
.catch((error) => {
|
||||
if (!controller.signal.aborted) {
|
||||
console.error("[GlobalChatbox] Failed to resume chat stream:", error);
|
||||
setIsStreaming(false);
|
||||
}
|
||||
})
|
||||
.finally(() => {
|
||||
if (abortRef.current === controller) {
|
||||
abortRef.current = null;
|
||||
}
|
||||
});
|
||||
},
|
||||
[applyStreamEvent],
|
||||
);
|
||||
resumeStreamingSessionRef.current = resumeStreamingSession;
|
||||
|
||||
const runPrompt = useCallback(
|
||||
async ({
|
||||
prompt: rawPrompt,
|
||||
@@ -380,8 +531,10 @@ export const useAgentChatSession = ({
|
||||
preparedMessages ??
|
||||
[...messages, nextUserMessage, nextAssistantMessage];
|
||||
|
||||
const clonedNextMessages = cloneMessages(nextMessages);
|
||||
setIsStreaming(true);
|
||||
setMessages(cloneMessages(nextMessages));
|
||||
messagesRef.current = clonedNextMessages;
|
||||
setMessages(clonedNextMessages);
|
||||
if (sessionIdOverride !== undefined) {
|
||||
sessionIdRef.current = sessionIdOverride;
|
||||
setSessionId(sessionIdOverride);
|
||||
@@ -396,93 +549,10 @@ export const useAgentChatSession = ({
|
||||
sessionId: sessionIdOverride ?? sessionIdRef.current,
|
||||
model: getModel?.(),
|
||||
signal: controller.signal,
|
||||
onEvent: (event) => {
|
||||
if ("sessionId" in event && event.sessionId && event.sessionId !== sessionIdRef.current) {
|
||||
sessionIdRef.current = event.sessionId;
|
||||
setSessionId(event.sessionId);
|
||||
}
|
||||
|
||||
if (event.type === "token") {
|
||||
setMessages((prev) =>
|
||||
prev.map((message) =>
|
||||
message.id === nextAssistantMessage.id
|
||||
? {
|
||||
...message,
|
||||
content: message.content + event.content,
|
||||
isError: false,
|
||||
}
|
||||
: message,
|
||||
),
|
||||
);
|
||||
} else if (event.type === "progress") {
|
||||
setMessages((prev) =>
|
||||
prev.map((message) =>
|
||||
message.id === nextAssistantMessage.id
|
||||
? { ...message, progress: upsertProgress(message.progress, event) }
|
||||
: message,
|
||||
),
|
||||
);
|
||||
} else if (event.type === "tool_call") {
|
||||
onToolCall(event, {
|
||||
onEvent: (event) =>
|
||||
applyStreamEvent(event, {
|
||||
assistantMessageId: nextAssistantMessage.id,
|
||||
appendArtifact,
|
||||
});
|
||||
} else if (event.type === "session_title") {
|
||||
const nextTitle = event.title.trim();
|
||||
if (nextTitle && !isSessionTitleManuallyEditedRef.current) {
|
||||
setSessionTitle(nextTitle);
|
||||
const currentStorageSessionId = storageSessionIdRef.current;
|
||||
if (currentStorageSessionId) {
|
||||
const currentNonce = ++titleUpdateNonceRef.current;
|
||||
void updateChatSessionTitle(currentStorageSessionId, nextTitle, {
|
||||
isTitleManuallyEdited: false,
|
||||
})
|
||||
.then(() => listChatSessions())
|
||||
.then((sessions) => {
|
||||
if (titleUpdateNonceRef.current !== currentNonce) return;
|
||||
setChatSessions(sessions);
|
||||
})
|
||||
.catch((error) => {
|
||||
console.error("[GlobalChatbox] Failed to persist session title:", error);
|
||||
});
|
||||
}
|
||||
}
|
||||
} else if (event.type === "done") {
|
||||
setMessages((prev) =>
|
||||
prev.map((message) => {
|
||||
if (message.id !== nextAssistantMessage.id) return message;
|
||||
const completedProgress = completeRunningProgress(message.progress);
|
||||
if (
|
||||
message.content.trim().length === 0 &&
|
||||
!(message.artifacts?.length)
|
||||
) {
|
||||
return {
|
||||
...message,
|
||||
content:
|
||||
"Agent 已完成处理,但没有生成文本回答。请查看过程记录,或换个更具体的问题重试。",
|
||||
progress: completedProgress,
|
||||
};
|
||||
}
|
||||
return { ...message, progress: completedProgress };
|
||||
}),
|
||||
);
|
||||
setIsStreaming(false);
|
||||
} else if (event.type === "error") {
|
||||
setMessages((prev) =>
|
||||
prev.map((message) =>
|
||||
message.id === nextAssistantMessage.id
|
||||
? {
|
||||
...message,
|
||||
content: message.content || `⚠️ **错误:** ${event.message}`,
|
||||
isError: true,
|
||||
progress: completeRunningProgress(message.progress),
|
||||
}
|
||||
: message,
|
||||
),
|
||||
);
|
||||
setIsStreaming(false);
|
||||
}
|
||||
},
|
||||
});
|
||||
} catch (error) {
|
||||
if (controller.signal.aborted) {
|
||||
@@ -528,7 +598,7 @@ export const useAgentChatSession = ({
|
||||
setIsStreaming(false);
|
||||
}
|
||||
},
|
||||
[appendArtifact, getModel, isHydrating, isStreaming, messages, onBeforeSend, onToolCall],
|
||||
[applyStreamEvent, getModel, isHydrating, isStreaming, messages, onBeforeSend],
|
||||
);
|
||||
|
||||
const abort = useCallback(() => {
|
||||
@@ -555,10 +625,8 @@ export const useAgentChatSession = ({
|
||||
setBranchTransition(null);
|
||||
hydrationNonceRef.current += 1;
|
||||
titleUpdateNonceRef.current += 1;
|
||||
storageSessionIdRef.current = undefined;
|
||||
sessionIdRef.current = undefined;
|
||||
lastPersistedStateKeyRef.current = createPersistedStateKey({
|
||||
storageSessionId: undefined,
|
||||
title: "新对话",
|
||||
isTitleManuallyEdited: false,
|
||||
messages: [],
|
||||
@@ -574,21 +642,20 @@ export const useAgentChatSession = ({
|
||||
}, [isHydrating, isStreaming]);
|
||||
|
||||
const switchSession = useCallback(
|
||||
async (nextStorageSessionId: string) => {
|
||||
if (isHydrating || isStreaming || storageSessionIdRef.current === nextStorageSessionId) {
|
||||
async (nextSessionId: string) => {
|
||||
if (isHydrating || isStreaming || sessionIdRef.current === nextSessionId) {
|
||||
return;
|
||||
}
|
||||
|
||||
setIsHydrating(true);
|
||||
try {
|
||||
const [nextState, sessions] = await Promise.all([
|
||||
loadChatSessionById(nextStorageSessionId, projectId),
|
||||
loadChatSessionById(nextSessionId),
|
||||
listChatSessions(),
|
||||
]);
|
||||
|
||||
hydrationNonceRef.current += 1;
|
||||
titleUpdateNonceRef.current += 1;
|
||||
storageSessionIdRef.current = nextState.storageSessionId;
|
||||
sessionIdRef.current = nextState.sessionId;
|
||||
lastPersistedStateKeyRef.current = createPersistedStateKey(nextState);
|
||||
setBranchTransition(null);
|
||||
@@ -598,38 +665,40 @@ export const useAgentChatSession = ({
|
||||
setSessionId(nextState.sessionId);
|
||||
setBranchGroups(nextState.branchGroups);
|
||||
setChatSessions(sessions);
|
||||
if (nextState.sessionId && nextState.isStreaming) {
|
||||
resumeStreamingSession(nextState.sessionId);
|
||||
} else {
|
||||
setIsStreaming(false);
|
||||
}
|
||||
} catch (error) {
|
||||
console.error("[GlobalChatbox] Failed to switch chat session:", error);
|
||||
} finally {
|
||||
setIsHydrating(false);
|
||||
}
|
||||
},
|
||||
[isHydrating, isStreaming, projectId],
|
||||
[isHydrating, isStreaming, resumeStreamingSession],
|
||||
);
|
||||
|
||||
const removeSession = useCallback(
|
||||
async (targetStorageSessionId: string) => {
|
||||
async (targetSessionId: string) => {
|
||||
if (isHydrating || isStreaming) return;
|
||||
|
||||
try {
|
||||
const nextActiveSessionId = await deleteChatSession(
|
||||
targetStorageSessionId,
|
||||
projectId,
|
||||
targetSessionId,
|
||||
);
|
||||
const sessions = await listChatSessions();
|
||||
setChatSessions(sessions);
|
||||
|
||||
if (storageSessionIdRef.current !== targetStorageSessionId) {
|
||||
if (sessionIdRef.current !== targetSessionId) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (!nextActiveSessionId) {
|
||||
hydrationNonceRef.current += 1;
|
||||
titleUpdateNonceRef.current += 1;
|
||||
storageSessionIdRef.current = undefined;
|
||||
sessionIdRef.current = undefined;
|
||||
lastPersistedStateKeyRef.current = createPersistedStateKey({
|
||||
storageSessionId: undefined,
|
||||
title: undefined,
|
||||
isTitleManuallyEdited: false,
|
||||
messages: [],
|
||||
@@ -647,12 +716,11 @@ export const useAgentChatSession = ({
|
||||
|
||||
setIsHydrating(true);
|
||||
const [nextState, sessionsAfterDelete] = await Promise.all([
|
||||
loadChatSessionById(nextActiveSessionId, projectId),
|
||||
loadChatSessionById(nextActiveSessionId),
|
||||
listChatSessions(),
|
||||
]);
|
||||
hydrationNonceRef.current += 1;
|
||||
titleUpdateNonceRef.current += 1;
|
||||
storageSessionIdRef.current = nextState.storageSessionId;
|
||||
sessionIdRef.current = nextState.sessionId;
|
||||
lastPersistedStateKeyRef.current = createPersistedStateKey(nextState);
|
||||
setBranchTransition(null);
|
||||
@@ -668,7 +736,7 @@ export const useAgentChatSession = ({
|
||||
setIsHydrating(false);
|
||||
}
|
||||
},
|
||||
[isHydrating, isStreaming, projectId],
|
||||
[isHydrating, isStreaming],
|
||||
);
|
||||
|
||||
const sendPrompt = useCallback(
|
||||
@@ -679,26 +747,25 @@ export const useAgentChatSession = ({
|
||||
);
|
||||
|
||||
const renameSession = useCallback(
|
||||
async (targetStorageSessionId: string, nextTitle: string) => {
|
||||
async (targetSessionId: string, nextTitle: string) => {
|
||||
const normalizedTitle = nextTitle.trim();
|
||||
if (!normalizedTitle || isHydrating) return;
|
||||
|
||||
try {
|
||||
await updateChatSessionTitle(targetStorageSessionId, normalizedTitle, {
|
||||
await updateChatSessionTitle(targetSessionId, normalizedTitle, {
|
||||
isTitleManuallyEdited: true,
|
||||
});
|
||||
const sessions = await listChatSessions();
|
||||
setChatSessions(sessions);
|
||||
|
||||
if (storageSessionIdRef.current === targetStorageSessionId) {
|
||||
if (sessionIdRef.current === targetSessionId) {
|
||||
setSessionTitle(normalizedTitle);
|
||||
setIsSessionTitleManuallyEdited(true);
|
||||
lastPersistedStateKeyRef.current = createPersistedStateKey({
|
||||
storageSessionId: targetStorageSessionId,
|
||||
sessionId: targetSessionId,
|
||||
title: normalizedTitle,
|
||||
isTitleManuallyEdited: true,
|
||||
messages,
|
||||
sessionId: sessionIdRef.current,
|
||||
branchGroups,
|
||||
});
|
||||
}
|
||||
@@ -864,7 +931,7 @@ export const useAgentChatSession = ({
|
||||
return {
|
||||
messages,
|
||||
chatSessions,
|
||||
activeStorageSessionId: storageSessionIdRef.current,
|
||||
activeSessionId: sessionIdRef.current,
|
||||
branchGroups,
|
||||
branchTransition,
|
||||
isHydrating,
|
||||
|
||||
@@ -1,4 +1,9 @@
|
||||
import { abortAgentChat, forkAgentChat, streamAgentChat } from "./chatStream";
|
||||
import {
|
||||
abortAgentChat,
|
||||
forkAgentChat,
|
||||
resumeAgentChatStream,
|
||||
streamAgentChat,
|
||||
} from "./chatStream";
|
||||
import { ReadableStream } from "stream/web";
|
||||
import { TextEncoder, TextDecoder } from "util";
|
||||
|
||||
@@ -76,6 +81,51 @@ describe("streamAgentChat", () => {
|
||||
]);
|
||||
});
|
||||
|
||||
it("parses state events from a resumed stream", async () => {
|
||||
apiFetch.mockResolvedValue({
|
||||
ok: true,
|
||||
body: makeStream([
|
||||
'event: state\ndata: {"session_id":"s1","messages":[{"id":"a1","role":"assistant","content":"已输出"}],"is_streaming":true,"run_status":"running"}\n\n',
|
||||
'event: token\ndata: {"session_id":"s1","content":"继续"}\n\n',
|
||||
'event: done\ndata: {"session_id":"s1"}\n\n',
|
||||
]),
|
||||
});
|
||||
|
||||
const events: Array<{
|
||||
type: string;
|
||||
sessionId?: string;
|
||||
messages?: unknown[];
|
||||
isStreaming?: boolean;
|
||||
runStatus?: string;
|
||||
content?: string;
|
||||
}> = [];
|
||||
|
||||
await resumeAgentChatStream({
|
||||
sessionId: "s1",
|
||||
onEvent: (event) => events.push(event),
|
||||
});
|
||||
|
||||
expect(apiFetch).toHaveBeenCalledWith(
|
||||
expect.stringContaining("/api/v1/agent/chat/session/s1/stream"),
|
||||
expect.objectContaining({
|
||||
method: "GET",
|
||||
projectHeaderMode: "include",
|
||||
skipAuthRedirect: true,
|
||||
}),
|
||||
);
|
||||
expect(events).toEqual([
|
||||
{
|
||||
type: "state",
|
||||
sessionId: "s1",
|
||||
messages: [{ id: "a1", role: "assistant", content: "已输出" }],
|
||||
isStreaming: true,
|
||||
runStatus: "running",
|
||||
},
|
||||
{ type: "token", sessionId: "s1", content: "继续" },
|
||||
{ type: "done", sessionId: "s1" },
|
||||
]);
|
||||
});
|
||||
|
||||
it("parses progress events", async () => {
|
||||
apiFetch.mockResolvedValue({
|
||||
ok: true,
|
||||
@@ -103,11 +153,11 @@ describe("streamAgentChat", () => {
|
||||
});
|
||||
});
|
||||
|
||||
it("parses legacy tool_call arguments when params is empty", async () => {
|
||||
it("parses tool_call arguments when params is empty", async () => {
|
||||
apiFetch.mockResolvedValue({
|
||||
ok: true,
|
||||
body: makeStream([
|
||||
'event: tool_call\ndata: {"conversationId":"agent-1e75dd01-29e","tool":"locate_features","params":{},"arguments":"{\\"ids\\":[\\"142902\\"],\\"feature_type\\":\\"junction\\"}"}\n\n',
|
||||
'event: tool_call\ndata: {"session_id":"agent-1e75dd01-29e","tool":"locate_features","params":{},"arguments":"{\\"ids\\":[\\"142902\\"],\\"feature_type\\":\\"junction\\"}"}\n\n',
|
||||
'event: done\ndata: {"session_id":"agent-1e75dd01-29e"}\n\n',
|
||||
]),
|
||||
});
|
||||
|
||||
+169
-83
@@ -6,6 +6,13 @@ export type AgentModel =
|
||||
| "deepseek/deepseek-v4-pro";
|
||||
|
||||
export type StreamEvent =
|
||||
| {
|
||||
type: "state";
|
||||
sessionId: string;
|
||||
messages: unknown[];
|
||||
isStreaming: boolean;
|
||||
runStatus?: string;
|
||||
}
|
||||
| { type: "token"; sessionId: string; content: string }
|
||||
| { type: "done"; sessionId: string; totalDurationMs?: number }
|
||||
| { type: "session_title"; sessionId: string; title: string }
|
||||
@@ -44,6 +51,12 @@ type StreamOptions = {
|
||||
onEvent: (event: StreamEvent) => void;
|
||||
};
|
||||
|
||||
type ResumeStreamOptions = {
|
||||
sessionId: string;
|
||||
signal?: AbortSignal;
|
||||
onEvent: (event: StreamEvent) => void;
|
||||
};
|
||||
|
||||
const parseEventBlock = (block: string): { event?: string; data?: string } => {
|
||||
const lines = block.split("\n");
|
||||
let event: string | undefined;
|
||||
@@ -87,6 +100,126 @@ const resolveToolParams = (
|
||||
return isObjectRecord(params) ? params : {};
|
||||
};
|
||||
|
||||
const emitParsedStreamEvent = (
|
||||
event: string,
|
||||
data: string,
|
||||
onEvent: (event: StreamEvent) => void,
|
||||
) => {
|
||||
try {
|
||||
const parsed = JSON.parse(data) as {
|
||||
session_id?: string;
|
||||
content?: string;
|
||||
message?: string;
|
||||
detail?: string;
|
||||
tool?: string;
|
||||
params?: Record<string, unknown>;
|
||||
arguments?: unknown;
|
||||
id?: string;
|
||||
phase?: string;
|
||||
status?: "running" | "completed" | "error";
|
||||
title?: string;
|
||||
messages?: unknown[];
|
||||
is_streaming?: boolean;
|
||||
run_status?: string;
|
||||
started_at?: number;
|
||||
ended_at?: number;
|
||||
elapsed_ms?: number;
|
||||
duration_ms?: number;
|
||||
total_duration_ms?: number;
|
||||
};
|
||||
if (event === "state") {
|
||||
onEvent({
|
||||
type: "state",
|
||||
sessionId: parsed.session_id ?? "",
|
||||
messages: Array.isArray(parsed.messages) ? parsed.messages : [],
|
||||
isStreaming: parsed.is_streaming ?? false,
|
||||
runStatus: parsed.run_status,
|
||||
});
|
||||
} else if (event === "token") {
|
||||
onEvent({
|
||||
type: "token",
|
||||
sessionId: parsed.session_id ?? "",
|
||||
content: parsed.content ?? "",
|
||||
});
|
||||
} else if (event === "progress") {
|
||||
onEvent({
|
||||
type: "progress",
|
||||
sessionId: parsed.session_id ?? "",
|
||||
id: parsed.id ?? `${parsed.phase ?? "progress"}-${Date.now()}`,
|
||||
phase: parsed.phase ?? "progress",
|
||||
status: parsed.status ?? "running",
|
||||
title: parsed.title ?? "正在处理",
|
||||
detail: parsed.detail,
|
||||
startedAt: parsed.started_at,
|
||||
endedAt: parsed.ended_at,
|
||||
elapsedMs: parsed.elapsed_ms,
|
||||
durationMs: parsed.duration_ms,
|
||||
});
|
||||
} else if (event === "done") {
|
||||
onEvent({
|
||||
type: "done",
|
||||
sessionId: parsed.session_id ?? "",
|
||||
totalDurationMs: parsed.total_duration_ms,
|
||||
});
|
||||
} else if (event === "session_title") {
|
||||
onEvent({
|
||||
type: "session_title",
|
||||
sessionId: parsed.session_id ?? "",
|
||||
title: typeof parsed.title === "string" ? parsed.title : "",
|
||||
});
|
||||
} else if (event === "error") {
|
||||
onEvent({
|
||||
type: "error",
|
||||
sessionId: parsed.session_id,
|
||||
message: parsed.message ?? "unknown error",
|
||||
detail: parsed.detail,
|
||||
totalDurationMs: parsed.total_duration_ms,
|
||||
});
|
||||
} else if (event === "tool_call") {
|
||||
onEvent({
|
||||
type: "tool_call",
|
||||
sessionId: parsed.session_id ?? "",
|
||||
tool: parsed.tool ?? "",
|
||||
params: resolveToolParams(parsed.params, parsed.arguments),
|
||||
});
|
||||
}
|
||||
} catch {
|
||||
onEvent({
|
||||
type: "error",
|
||||
message: "invalid SSE data payload",
|
||||
detail: data,
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
const readStreamEvents = async (
|
||||
response: Response,
|
||||
onEvent: (event: StreamEvent) => void,
|
||||
) => {
|
||||
if (!response.body) {
|
||||
return;
|
||||
}
|
||||
|
||||
const reader = response.body.getReader();
|
||||
const decoder = new TextDecoder("utf-8");
|
||||
let buffer = "";
|
||||
|
||||
while (true) {
|
||||
const { done, value } = await reader.read();
|
||||
if (done) break;
|
||||
|
||||
buffer += decoder.decode(value, { stream: true });
|
||||
const blocks = buffer.split("\n\n");
|
||||
buffer = blocks.pop() ?? "";
|
||||
|
||||
for (const block of blocks) {
|
||||
const { event, data } = parseEventBlock(block);
|
||||
if (!event || !data) continue;
|
||||
emitParsedStreamEvent(event, data, onEvent);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
export const streamAgentChat = async ({
|
||||
message,
|
||||
sessionId,
|
||||
@@ -144,99 +277,52 @@ export const streamAgentChat = async ({
|
||||
return;
|
||||
}
|
||||
|
||||
const reader = response.body.getReader();
|
||||
const decoder = new TextDecoder("utf-8");
|
||||
let buffer = "";
|
||||
|
||||
while (true) {
|
||||
const { done, value } = await reader.read();
|
||||
if (done) break;
|
||||
|
||||
buffer += decoder.decode(value, { stream: true });
|
||||
const blocks = buffer.split("\n\n");
|
||||
buffer = blocks.pop() ?? "";
|
||||
|
||||
for (const block of blocks) {
|
||||
const { event, data } = parseEventBlock(block);
|
||||
if (!event || !data) continue;
|
||||
await readStreamEvents(response, onEvent);
|
||||
};
|
||||
|
||||
export const resumeAgentChatStream = async ({
|
||||
sessionId,
|
||||
signal,
|
||||
onEvent,
|
||||
}: ResumeStreamOptions) => {
|
||||
let response: Response;
|
||||
try {
|
||||
const parsed = JSON.parse(data) as {
|
||||
session_id?: string;
|
||||
conversationId?: string;
|
||||
content?: string;
|
||||
message?: string;
|
||||
detail?: string;
|
||||
tool?: string;
|
||||
params?: Record<string, unknown>;
|
||||
arguments?: unknown;
|
||||
id?: string;
|
||||
phase?: string;
|
||||
status?: "running" | "completed" | "error";
|
||||
title?: string;
|
||||
started_at?: number;
|
||||
ended_at?: number;
|
||||
elapsed_ms?: number;
|
||||
duration_ms?: number;
|
||||
total_duration_ms?: number;
|
||||
};
|
||||
if (event === "token") {
|
||||
onEvent({
|
||||
type: "token",
|
||||
sessionId: parsed.session_id ?? "",
|
||||
content: parsed.content ?? "",
|
||||
});
|
||||
} else if (event === "progress") {
|
||||
onEvent({
|
||||
type: "progress",
|
||||
sessionId: parsed.session_id ?? "",
|
||||
id: parsed.id ?? `${parsed.phase ?? "progress"}-${Date.now()}`,
|
||||
phase: parsed.phase ?? "progress",
|
||||
status: parsed.status ?? "running",
|
||||
title: parsed.title ?? "正在处理",
|
||||
detail: parsed.detail,
|
||||
startedAt: parsed.started_at,
|
||||
endedAt: parsed.ended_at,
|
||||
elapsedMs: parsed.elapsed_ms,
|
||||
durationMs: parsed.duration_ms,
|
||||
});
|
||||
} else if (event === "done") {
|
||||
onEvent({
|
||||
type: "done",
|
||||
sessionId: parsed.session_id ?? "",
|
||||
totalDurationMs: parsed.total_duration_ms,
|
||||
});
|
||||
} else if (event === "session_title") {
|
||||
onEvent({
|
||||
type: "session_title",
|
||||
sessionId: parsed.session_id ?? "",
|
||||
title: typeof parsed.title === "string" ? parsed.title : "",
|
||||
});
|
||||
} else if (event === "error") {
|
||||
response = await apiFetch(
|
||||
`${config.AGENT_URL}/api/v1/agent/chat/session/${encodeURIComponent(sessionId)}/stream`,
|
||||
{
|
||||
method: "GET",
|
||||
signal,
|
||||
headers: {
|
||||
Accept: "text/event-stream",
|
||||
},
|
||||
projectHeaderMode: "include",
|
||||
userHeaderMode: "include",
|
||||
skipAuthRedirect: true,
|
||||
},
|
||||
);
|
||||
} catch (error) {
|
||||
const detail = error instanceof Error ? error.message : String(error);
|
||||
onEvent({
|
||||
type: "error",
|
||||
sessionId: parsed.session_id,
|
||||
message: parsed.message ?? "unknown error",
|
||||
detail: parsed.detail,
|
||||
totalDurationMs: parsed.total_duration_ms,
|
||||
});
|
||||
} else if (event === "tool_call") {
|
||||
onEvent({
|
||||
type: "tool_call",
|
||||
sessionId: parsed.session_id ?? parsed.conversationId ?? "",
|
||||
tool: parsed.tool ?? "",
|
||||
params: resolveToolParams(parsed.params, parsed.arguments),
|
||||
sessionId,
|
||||
message: "network request failed",
|
||||
detail,
|
||||
});
|
||||
return;
|
||||
}
|
||||
} catch {
|
||||
|
||||
if (!response.ok || !response.body) {
|
||||
const detail = await response.text();
|
||||
onEvent({
|
||||
type: "error",
|
||||
message: "invalid SSE data payload",
|
||||
detail: data,
|
||||
sessionId,
|
||||
message: "stream request failed",
|
||||
detail,
|
||||
});
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
await readStreamEvents(response, onEvent);
|
||||
};
|
||||
|
||||
export const abortAgentChat = async (sessionId?: string) => {
|
||||
|
||||
Reference in New Issue
Block a user