DeepTutor: Lifelong Personalized Tutoring. https://deeptutor.info/.
Python ai-agentsai-tutorclawbotcli-tooldeepresearchinteractive-learninglearningmulti-agent-systemsrag
fix(history): search full conversation transcripts
XZH356c60c3ec98
diff --git a/web/components/space/ChatHistorySection.tsx b/web/components/space/ChatHistorySection.tsxindex 8219212d8..e8e41b05e 100644--- a/web/components/space/ChatHistorySection.tsx+++ b/web/components/space/ChatHistorySection.tsx@@ -2,7 +2,7 @@import { navigateTask } from "@/lib/workspace-scope";import { sessionWorkspaceId } from "@/lib/session-api";-import { useCallback, useEffect, useMemo, useState } from "react";+import { useCallback, useEffect, useMemo, useRef, useState } from "react";import { useRouter } from "next/navigation";import {History,@@ -30,9 +30,11 @@ import { notifySessionsChanged } from "@/lib/session-events";import {deleteSession,listAllSessions,+ searchAllSessions,updateSessionTitle,updateSessionOrganization,type SessionOrganizationPatch,+ type SessionSearchResult,type SessionSummary,} from "@/lib/session-api";@@ -67,6 +69,10 @@ export default function ChatHistorySection({const [loading, setLoading] = useState(true);const [restoringId, setRestoringId] = useState<string | null>(null);const [query, setQuery] = useState("");+ const [searchResults, setSearchResults] = useState<SessionSearchResult[]>([]);+ const [searchLoading, setSearchLoading] = useState(false);+ const [searchVersion, setSearchVersion] = useState(0);+ const searchGenerationRef = useRef(0);const [courseFilter] = useState("all");const [kindFilter, setKindFilter] = useState("all");const [archiveFilter, setArchiveFilter] = useState("active");@@ -95,13 +101,58 @@ export default function ChatHistorySection({}}, []);+ const reload = useCallback(+ async (force = false, quiet = false) => {+ await load(force, quiet);+ setSearchVersion((version) => version + 1);+ },+ [load],+ );+useEffect(() => {void load(true);}, [load]);+ useEffect(() => {+ const generation = ++searchGenerationRef.current;+ const term = query.trim();+ if (!term) {+ setSearchResults([]);+ setSearchLoading(false);+ return;+ }++ setSearchResults([]);+ setSearchLoading(true);+ let controller: AbortController | null = null;+ const timer = window.setTimeout(() => {+ controller = new AbortController();+ searchAllSessions(term, controller.signal, { allWorkspaces: true })+ .then((results) => {+ if (generation !== searchGenerationRef.current) return;+ setSearchResults(results);+ })+ .catch((error: unknown) => {+ if (error instanceof DOMException && error.name === "AbortError")+ return;+ if (generation !== searchGenerationRef.current) return;+ setSearchResults([]);+ })+ .finally(() => {+ if (generation === searchGenerationRef.current)+ setSearchLoading(false);+ });+ }, 300);++ return () => {+ window.clearTimeout(timer);+ controller?.abort();+ };+ }, [query, searchVersion]);+const filteredSessions = useMemo(() => {- const needle = query.trim().toLowerCase();- return sessions.filter((session) => {+ const source = query.trim() ? searchResults : sessions;+ return source.filter((session) => {const prefs = session.preferences ?? {};if (archiveFilter === "active" && prefs.archived) return false;if (archiveFilter === "archived" && !prefs.archived) return false;@@ -119,40 +170,54 @@ export default function ChatHistorySection({prefs.session_kind !== "selection_tutor")return false;- if (!needle) return true;- return [session.title, session.last_message]- .filter(Boolean)- .some((value) => value.toLowerCase().includes(needle));+ return true;});- }, [archiveFilter, courseFilter, kindFilter, query, sessions]);+ }, [archiveFilter, courseFilter, kindFilter, query, searchResults, sessions]);const handleSelect = useCallback((sessionId: string) => {setActiveSessionId(sessionId);- const session = sessions.find((item) => item.session_id === sessionId);+ const session = filteredSessions.find(+ (item) => item.session_id === sessionId,+ );navigateTask(session ? sessionRoute(session) : `${basePath}/${sessionId}`, router.push);},- [basePath, router, sessions, setActiveSessionId],+ [basePath, filteredSessions, router, setActiveSessionId],);const handleRename = useCallback(async (sessionId: string, title: string) => {- await updateSessionTitle(sessionId, title, sessionWorkspaceId(sessions.find(item => item.session_id === sessionId)));- await load(true);+ await updateSessionTitle(+ sessionId,+ title,+ sessionWorkspaceId(+ filteredSessions.find((item) => item.session_id === sessionId),+ ),+ );+ await reload(true);},- [load, sessions],+ [filteredSessions, reload],);const handleDelete = useCallback(async (sessionId: string) => {if (!window.confirm(t("Permanently delete this chat and its tutor threads? This cannot be undone."))) return;- await deleteSession(sessionId, sessionWorkspaceId(sessions.find(item => item.session_id === sessionId)));+ await deleteSession(+ sessionId,+ sessionWorkspaceId(+ filteredSessions.find((item) => item.session_id === sessionId),+ ),+ );if (activeSessionId === sessionId) setActiveSessionId(null);setSessions((prev) =>prev.filter((session) => session.session_id !== sessionId),);+ setSearchResults((prev) =>+ prev.filter((session) => session.session_id !== sessionId),+ );+ setSearchVersion((version) => version + 1);},- [activeSessionId, setActiveSessionId, t, sessions],+ [activeSessionId, filteredSessions, setActiveSessionId, t],);// The archived view is built from the same filtered set as the list, so the@@ -174,24 +239,30 @@ export default function ChatHistorySection({await updateSessionOrganization(sessionId, { archived: false });// Restoring cascades to the tutor threads under the conversation, so// the server's own list is what says which rows are left.- await load(true, true);+ await reload(true, true);notifySessionsChanged();} finally {setRestoringId(null);}},- [load, sessions],+ [reload],);const handleOrganize = useCallback(async (sessionId: string, patch: SessionOrganizationPatch) => {- await updateSessionOrganization(sessionId, patch, sessionWorkspaceId(sessions.find(item => item.session_id === sessionId)));- await load(true);+ await updateSessionOrganization(+ sessionId,+ patch,+ sessionWorkspaceId(+ filteredSessions.find((item) => item.session_id === sessionId),+ ),+ );+ await reload(true);// Archiving or restoring here changes what the sidebar beside this page// is allowed to show, and that list was fetched when the shell mounted.notifySessionsChanged();},- [load, sessions],+ [filteredSessions, reload],);const HeaderIcon = icon ?? History;@@ -216,11 +287,11 @@ export default function ChatHistorySection({action={<buttontype="button"- onClick={() => void load(true)}- disabled={loading}+ onClick={() => void reload(true)}+ disabled={loading || searchLoading}className="inline-flex items-center gap-1.5 rounded-lg border border-[var(--border)]/50 px-3 py-1.5 text-[12px] font-medium text-[var(--muted-foreground)] transition-colors hover:border-[var(--border)] hover:text-[var(--foreground)] disabled:opacity-40">- {loading ? (+ {loading || searchLoading ? (<Loader2 className="h-3 w-3 animate-spin" />) : (<RefreshCw className="h-3 w-3" />@@ -237,6 +308,7 @@ export default function ChatHistorySection({<inputvalue={query}onChange={(event) => setQuery(event.target.value)}+ maxLength={200}placeholder={t("Search chat history...")}className="min-w-0 flex-1 bg-transparent text-[13px] text-[var(--foreground)] outline-none placeholder:text-[var(--muted-foreground)]/55"/>@@ -275,7 +347,7 @@ export default function ChatHistorySection({</div><div className="px-3 py-3">- {loading ? (+ {loading || searchLoading ? (<div className="space-y-2 p-2">{[0, 1, 2, 3].map((item) => (<divdiff --git a/web/lib/session-api.ts b/web/lib/session-api.tsindex 3aa032fd8..30a155d2c 100644--- a/web/lib/session-api.ts+++ b/web/lib/session-api.ts@@ -251,12 +251,14 @@ export async function searchSessions(limit = 50,offset = 0,signal?: AbortSignal,+ options?: { allWorkspaces?: boolean },): Promise<SessionSearchPage> {const qs = new URLSearchParams({q: query,limit: String(limit),offset: String(offset),});+ if (options?.allWorkspaces) qs.set("all_workspaces", "true");const response = await apiFetch(apiUrl(`/api/sessions/search?${qs}`), {cache: "no-store",signal,@@ -264,6 +266,35 @@ export async function searchSessions(return expectJson<SessionSearchPage>(response);}+/** Fetch every full-text search hit in bounded pages for the history console. */+export async function searchAllSessions(+ query: string,+ signal?: AbortSignal,+ options?: { allWorkspaces?: boolean },+): Promise<SessionSearchResult[]> {+ const pageSize = 100;+ const sessions: SessionSearchResult[] = [];+ const seen = new Set<string>();+ for (let offset = 0; ; offset += pageSize) {+ const page = await searchSessions(+ query,+ pageSize,+ offset,+ signal,+ options,+ );+ for (const session of page.sessions) {+ const key = `${sessionWorkspaceId(session)}:${session.session_id}`;+ if (seen.has(key)) continue;+ seen.add(key);+ sessions.push(session);+ }+ if (sessions.length >= page.total || page.sessions.length < pageSize) {+ return sessions;+ }+ }+}+export async function getSession(sessionId: string,signal?: AbortSignal,diff --git a/web/tests/chat-history-search.spec.tsx b/web/tests/chat-history-search.spec.tsxnew file mode 100644index 000000000..169da7bc6--- /dev/null+++ b/web/tests/chat-history-search.spec.tsx@@ -0,0 +1,130 @@+import { act, fireEvent, render, screen } from "@testing-library/react";+import { afterEach, expect, it, vi } from "vitest";+import ChatHistorySection from "@/components/space/ChatHistorySection";++const fixture = vi.hoisted(() => ({+ calls: [] as Array<{ query: string; signal?: AbortSignal }>,+}));++vi.mock("react-i18next", () => ({+ useTranslation: () => ({ t: (key: string) => key }),+}));+vi.mock("next/navigation", () => ({+ useRouter: () => ({ push: vi.fn() }),+}));+vi.mock("@/context/AppShellContext", () => ({+ useAppShell: () => ({+ activeSessionId: null,+ setActiveSessionId: vi.fn(),+ }),+}));+vi.mock("@/lib/workspace-scope", () => ({+ navigateTask: vi.fn(),+}));+vi.mock("@/lib/learning-api", () => ({+ fetchMasteryTopicIndex: vi.fn(async () => []),+}));+vi.mock("@/lib/reading-workspace-api", () => ({+ fetchReadingCollectionIndex: vi.fn(async () => []),+}));+vi.mock("@/lib/session-archive", () => ({+ collectArchivedConversations: vi.fn(() => ({ groups: [] })),+}));+vi.mock("@/lib/session-events", () => ({+ notifySessionsChanged: vi.fn(),+}));+vi.mock("@/components/space/SpaceSectionHeader", () => ({+ default: ({ title }: { title: string }) => <h1>{title}</h1>,+}));+vi.mock("@/components/space/ArchivedConversations", () => ({+ default: () => null,+}));+vi.mock("@/components/courses/OrganizedSessionList", () => ({+ default: ({+ sessions,+ }: {+ sessions: Array<{+ session_id: string;+ title: string;+ match_excerpt?: string;+ }>;+ }) => (+ <div>+ {sessions.map((session) => (+ <div key={session.session_id}>+ <span>{session.title}</span>+ {session.match_excerpt ? (+ <span>{session.match_excerpt}</span>+ ) : null}+ </div>+ ))}+ </div>+ ),+}));+vi.mock("@/lib/session-api", () => ({+ sessionWorkspaceId: () => "",+ listAllSessions: vi.fn(async () => [+ {+ id: "recent",+ session_id: "recent",+ title: "Recent chat",+ created_at: 1,+ updated_at: 1,+ message_count: 2,+ last_message: "unrelated ending",+ },+ ]),+ searchAllSessions: vi.fn(+ async (query: string, signal?: AbortSignal) => {+ fixture.calls.push({ query, signal });+ return [+ {+ id: "older",+ session_id: "older",+ title: "Older conversation",+ created_at: 1,+ updated_at: 2,+ message_count: 4,+ last_message: "unrelated ending",+ match_excerpt: "Earlier transcript contains the term",+ match_role: "user",+ match_message_id: 7,+ match_created_at: 1,+ },+ ];+ },+ ),+ updateSessionTitle: vi.fn(),+ updateSessionOrganization: vi.fn(),+ deleteSession: vi.fn(),+}));++afterEach(() => {+ vi.useRealTimers();+ fixture.calls.length = 0;+});++it("searches the full account history, then restores the unfiltered index", async () => {+ render(<ChatHistorySection />);++ await act(async () => {+ await Promise.resolve();+ });+ expect(screen.getByText("Recent chat")).toBeInTheDocument();++ vi.useFakeTimers();+ const input = screen.getByPlaceholderText("Search chat history...");+ fireEvent.change(input, { target: { value: "Bayes" } });+ await act(async () => {+ vi.advanceTimersByTime(300);+ });++ expect(fixture.calls.map((call) => call.query)).toEqual(["Bayes"]);+ expect(screen.getByText("Earlier transcript contains the term")).toBeInTheDocument();+ expect(screen.getByText("Older conversation")).toBeInTheDocument();+ expect(screen.queryByText("Recent chat")).toBeNull();++ fireEvent.change(input, { target: { value: "" } });+ expect(screen.getByText("Recent chat")).toBeInTheDocument();+ expect(screen.queryByText("Older conversation")).toBeNull();+});diff --git a/web/tests/session-search-api.test.ts b/web/tests/session-search-api.test.tsindex 08b6a8a7e..d5f22420a 100644--- a/web/tests/session-search-api.test.ts+++ b/web/tests/session-search-api.test.ts@@ -1,7 +1,7 @@import test from "node:test";import assert from "node:assert/strict";-import { searchSessions } from "../lib/session-api";+import { searchAllSessions, searchSessions } from "../lib/session-api";test("session search sends a literal bounded query and pagination", async () => {const original = globalThis.fetch;@@ -38,3 +38,52 @@ test("session search sends a literal bounded query and pagination", async () =>(globalThis as { fetch: typeof fetch }).fetch = original;}});++test("history search fetches every account-scoped result page", async () => {+ const original = globalThis.fetch;+ const urls: string[] = [];+ (globalThis as { fetch: typeof fetch }).fetch = async (input) => {+ const url = new URL(String(input), "http://deeptutor.local");+ urls.push(url.pathname + url.search);+ const offset = Number(url.searchParams.get("offset"));+ const count = offset === 0 ? 100 : 50;+ return new Response(+ JSON.stringify({+ sessions: Array.from({ length: count }, (_, index) => ({+ id: `session-${offset + index}`,+ session_id: `session-${offset + index}`,+ title: "Chat",+ created_at: 1,+ updated_at: 1,+ message_count: 1,+ last_message: "match",+ match_excerpt: "match",+ })),+ total: 150,+ limit: 100,+ offset,+ }),+ { status: 200, headers: { "Content-Type": "application/json" } },+ );+ };++ try {+ const sessions = await searchAllSessions("Bayes", undefined, {+ allWorkspaces: true,+ });+ assert.equal(sessions.length, 150);+ assert.equal(urls.length, 2);+ assert.equal(+ new URL(urls[0], "http://deeptutor.local").searchParams.get(+ "all_workspaces",+ ),+ "true",+ );+ assert.equal(+ new URL(urls[1], "http://deeptutor.local").searchParams.get("offset"),+ "100",+ );+ } finally {+ (globalThis as { fetch: typeof fetch }).fetch = original;+ }+});