refactor(chat): remove frontend state saves
This commit is contained in:
@@ -27,13 +27,6 @@ export const createEmptyChatState = (): LoadedChatState => ({
|
||||
const sanitizeMessages = (messages: Message[] | undefined) =>
|
||||
Array.isArray(messages) ? cloneMessages(messages) : [];
|
||||
|
||||
const hasChatContent = (state: {
|
||||
messages: Message[];
|
||||
sessionId?: string;
|
||||
}) =>
|
||||
state.messages.length > 0 ||
|
||||
Boolean(state.sessionId);
|
||||
|
||||
const compareSessionsByAnchorTime = (
|
||||
left: Pick<ChatSessionSummary, "id" | "createdAt" | "updatedAt">,
|
||||
right: Pick<ChatSessionSummary, "id" | "createdAt" | "updatedAt">,
|
||||
@@ -113,64 +106,6 @@ const fetchBackendChatSession = async (sessionId: string): Promise<LoadedChatSta
|
||||
};
|
||||
};
|
||||
|
||||
const createBackendChatSession = async (payload?: {
|
||||
sessionId?: string;
|
||||
parentSessionId?: string;
|
||||
}) => {
|
||||
const response = await apiFetch(`${config.AGENT_URL}/api/v1/agent/chat/session`, {
|
||||
method: "POST",
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
},
|
||||
body: JSON.stringify({
|
||||
session_id: payload?.sessionId,
|
||||
parent_session_id: payload?.parentSessionId,
|
||||
}),
|
||||
projectHeaderMode: "include",
|
||||
userHeaderMode: "include",
|
||||
skipAuthRedirect: true,
|
||||
});
|
||||
if (!response.ok) {
|
||||
throw new Error(await response.text());
|
||||
}
|
||||
const body = (await response.json()) as {
|
||||
session_id?: string;
|
||||
};
|
||||
const sessionId = body.session_id?.trim();
|
||||
if (!sessionId) {
|
||||
throw new Error("backend did not return session_id");
|
||||
}
|
||||
return sessionId;
|
||||
};
|
||||
|
||||
const saveBackendChatState = async (
|
||||
sessionId: string,
|
||||
state: LoadedChatState,
|
||||
): Promise<string> => {
|
||||
const response = await apiFetch(
|
||||
`${config.AGENT_URL}/api/v1/agent/chat/session/${encodeURIComponent(sessionId)}`,
|
||||
{
|
||||
method: "PUT",
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
},
|
||||
body: JSON.stringify({
|
||||
title: normalizeTitle(state.title),
|
||||
is_title_manually_edited: state.isTitleManuallyEdited ?? false,
|
||||
messages: sanitizeMessages(state.messages),
|
||||
}),
|
||||
projectHeaderMode: "include",
|
||||
userHeaderMode: "include",
|
||||
skipAuthRedirect: true,
|
||||
},
|
||||
);
|
||||
if (!response.ok) {
|
||||
throw new Error(await response.text());
|
||||
}
|
||||
const payload = (await response.json()) as { id?: string; session_id?: string };
|
||||
return payload.id ?? payload.session_id ?? sessionId;
|
||||
};
|
||||
|
||||
const updateBackendChatSessionTitle = async (
|
||||
sessionId: string,
|
||||
title: string,
|
||||
@@ -212,27 +147,6 @@ const deleteBackendChatSession = async (sessionId: string) => {
|
||||
}
|
||||
};
|
||||
|
||||
export const saveActiveChatState = async (
|
||||
state: LoadedChatState,
|
||||
): Promise<string | undefined> => {
|
||||
if (typeof window === "undefined") return state.sessionId;
|
||||
|
||||
if (!hasChatContent(state)) {
|
||||
return undefined;
|
||||
}
|
||||
|
||||
let backendSessionId = state.sessionId;
|
||||
if (!backendSessionId) {
|
||||
backendSessionId = await createBackendChatSession();
|
||||
}
|
||||
|
||||
const savedSessionId = await saveBackendChatState(backendSessionId, {
|
||||
...state,
|
||||
sessionId: backendSessionId,
|
||||
});
|
||||
return savedSessionId;
|
||||
};
|
||||
|
||||
export const listChatSessions = async (): Promise<ChatSessionSummary[]> => {
|
||||
if (typeof window === "undefined") return [];
|
||||
return await fetchBackendChatSessions();
|
||||
|
||||
Reference in New Issue
Block a user