mirror of
https://github.com/Significant-Gravitas/AutoGPT.git
synced 2026-04-08 03:00:28 -04:00
chore: make sessions nicer
This commit is contained in:
@@ -1,6 +1,25 @@
|
||||
import type { ToolResult } from "@/types/chat";
|
||||
import type { ChatMessageData } from "../ChatMessage/useChatMessage";
|
||||
|
||||
export function removePageContext(content: string): string {
|
||||
// Remove "Page URL: ..." pattern (case insensitive, handles various formats)
|
||||
let cleaned = content.replace(/Page URL:\s*[^\n\r]*/gi, "");
|
||||
|
||||
// Find "User Message:" marker to preserve the actual user message
|
||||
const userMessageMatch = cleaned.match(/User Message:\s*([\s\S]*)$/i);
|
||||
if (userMessageMatch) {
|
||||
// If we found "User Message:", extract everything after it
|
||||
cleaned = userMessageMatch[1];
|
||||
} else {
|
||||
// If no "User Message:" marker, remove "Page Content:" and everything after it
|
||||
cleaned = cleaned.replace(/Page Content:[\s\S]*$/gi, "");
|
||||
}
|
||||
|
||||
// Clean up extra whitespace and newlines
|
||||
cleaned = cleaned.replace(/\n\s*\n\s*\n+/g, "\n\n").trim();
|
||||
return cleaned;
|
||||
}
|
||||
|
||||
export function createUserMessage(content: string): ChatMessageData {
|
||||
return {
|
||||
type: "message",
|
||||
|
||||
@@ -10,6 +10,7 @@ import {
|
||||
isToolCallArray,
|
||||
isValidMessage,
|
||||
parseToolResponse,
|
||||
removePageContext,
|
||||
} from "./helpers";
|
||||
|
||||
interface UseChatContainerArgs {
|
||||
@@ -45,9 +46,19 @@ export function useChatContainer({
|
||||
);
|
||||
})
|
||||
.map((msg: Record<string, unknown>) => {
|
||||
const content = String(msg.content || "");
|
||||
let content = String(msg.content || "");
|
||||
const role = String(msg.role || "assistant").toLowerCase();
|
||||
const toolCalls = msg.tool_calls;
|
||||
|
||||
// Remove page context from user messages when loading existing sessions
|
||||
if (role === "user") {
|
||||
content = removePageContext(content);
|
||||
// Skip user messages that become empty after removing page context
|
||||
if (!content.trim()) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
if (
|
||||
role === "assistant" &&
|
||||
toolCalls &&
|
||||
@@ -71,6 +82,10 @@ export function useChatContainer({
|
||||
}
|
||||
return toolResponse;
|
||||
}
|
||||
// Skip assistant messages with empty content (they're handled by tool calls)
|
||||
if (role === "assistant" && !content.trim()) {
|
||||
return null;
|
||||
}
|
||||
return {
|
||||
type: "message",
|
||||
role: role as "user" | "assistant" | "system",
|
||||
|
||||
@@ -30,7 +30,14 @@ export function SessionsDrawer({
|
||||
},
|
||||
);
|
||||
|
||||
const sessions = data?.status === 200 ? data.data.sessions : [];
|
||||
const sessions =
|
||||
data?.status === 200
|
||||
? data.data.sessions.filter((session) => {
|
||||
// Filter out sessions without messages (sessions that were never updated)
|
||||
// If updated_at equals created_at, the session was created but never had messages
|
||||
return session.updated_at !== session.created_at;
|
||||
})
|
||||
: [];
|
||||
|
||||
function handleSelectSession(sessionId: string) {
|
||||
onSelectSession(sessionId);
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
import {
|
||||
getGetV2GetSessionQueryKey,
|
||||
getGetV2GetSessionQueryOptions,
|
||||
postV2CreateSession,
|
||||
useGetV2GetSession,
|
||||
usePatchV2SessionAssignUser,
|
||||
@@ -156,8 +157,14 @@ export function useChatSession({
|
||||
setError(null);
|
||||
setSessionId(id);
|
||||
storage.set(Key.CHAT_SESSION_ID, id);
|
||||
const result = await refetch();
|
||||
if (!result.data || result.isError) {
|
||||
const queryOptions = getGetV2GetSessionQueryOptions(id, {
|
||||
query: {
|
||||
staleTime: Infinity,
|
||||
retry: 1,
|
||||
},
|
||||
});
|
||||
const result = await queryClient.fetchQuery(queryOptions);
|
||||
if (!result || ("status" in result && result.status !== 200)) {
|
||||
console.warn("Session not found on server, clearing local state");
|
||||
storage.clean(Key.CHAT_SESSION_ID);
|
||||
setSessionId(null);
|
||||
@@ -170,7 +177,7 @@ export function useChatSession({
|
||||
throw error;
|
||||
}
|
||||
},
|
||||
[refetch],
|
||||
[queryClient],
|
||||
);
|
||||
|
||||
const refreshSession = useCallback(
|
||||
|
||||
Reference in New Issue
Block a user