Files
DeepTutor/web/components/notebook/useNotebookLibrary.ts
Bingxi Zhao (Frank) 7c6bcfaef7 release: v1.5.14
Immersive Reading, self-configuration, and a two-way IMA library.

- Immersive Reading: a new capability that opens a document beside the
  thread. New reading engine (PDF pages / slides / page-sized sections),
  five reading tools, a deterministic locate pre-pass that grounds the
  turn without a second LLM call, /api/v1/reading, and a reader pane with
  selection, highlights, notes, and export to an annotated PDF or Markdown.
- Setup capability: inspect the install, apply a setting, install a
  parsing engine, or request a credential from a chat turn. New settings
  spec table; a candidate value is probed before it is committed, and API
  keys never enter the model's context.
- Tencent IMA: pipeline split into transport/envelope/models/sources/
  notes/media/inventory, an additive capability with five tools, and
  connected libraries that can finally be enumerated (bounded listings
  are reported as a lower bound).
- Retrieval: supports_rag_retrieval() replaces is_connected_kb() for the
  "can I sweep this?" question, so linked / lightrag_server / ima sources
  stop being silently dropped from every book.
- Notebooks: their own console at /notebook, per-notebook locks and atomic
  writes, copy / move / export routes, damaged files surfaced instead of
  dropped, and renaming a record no longer clears its knowledge-base link.
- Settings persistence: one lock and one atomic replace shared by every
  writer of interface.json; the PUT endpoints stop writing the
  defaults-merged view back to disk.

Release notes: assets/releases/ver1-5-14.md
2026-08-19 17:34:52 +08:00

285 lines
7.6 KiB
TypeScript

"use client";
import { useCallback, useEffect, useMemo, useRef, useState } from "react";
import {
createNotebook,
deleteNotebook,
deleteNotebookRecord,
getNotebook,
listNotebooks,
relocateNotebookRecord,
updateNotebook,
updateNotebookRecord,
type NotebookRecordItem,
type NotebookSummary,
} from "@/lib/notebook-api";
/**
* Every notebook read/write the console needs, in one place.
*
* The UI components below stay presentational: they render what this hook
* exposes and call back into it. Mutations update local state optimistically
* where the server response is predictable (rename, delete) and re-fetch
* where it is not (create, move), so the list never shows a stale count.
*/
export interface NotebookLibrary {
notebooks: NotebookSummary[];
selectedId: string | null;
selected: NotebookDetailState | null;
loading: boolean;
detailLoading: boolean;
error: string | null;
detailError: string | null;
select: (notebookId: string) => void;
reload: () => Promise<void>;
create: (name: string, description: string) => Promise<string | null>;
rename: (
notebookId: string,
changes: { name?: string; description?: string; color?: string },
) => Promise<void>;
remove: (notebookId: string) => Promise<void>;
editRecord: (
recordId: string,
changes: { title?: string; summary?: string; output?: string },
) => Promise<void>;
removeRecord: (recordId: string) => Promise<void>;
relocateRecord: (
recordId: string,
targetNotebookId: string,
mode: "move" | "copy",
) => Promise<void>;
}
export interface NotebookDetailState extends NotebookSummary {
records: NotebookRecordItem[];
}
function messageOf(error: unknown): string {
if (error instanceof Error) return error.message;
return String(error);
}
export function useNotebookLibrary(initialId?: string | null): NotebookLibrary {
const [notebooks, setNotebooks] = useState<NotebookSummary[]>([]);
const [selectedId, setSelectedId] = useState<string | null>(
initialId ?? null,
);
const [selected, setSelected] = useState<NotebookDetailState | null>(null);
const [loading, setLoading] = useState(true);
const [detailLoading, setDetailLoading] = useState(false);
const [error, setError] = useState<string | null>(null);
const [detailError, setDetailError] = useState<string | null>(null);
// Detail fetches are racy — clicking through the list fires several. Only
// the newest one is allowed to write state, otherwise a slow earlier
// response can overwrite the notebook the user is actually looking at.
const detailRequestRef = useRef(0);
const loadDetail = useCallback(async (notebookId: string) => {
const requestId = ++detailRequestRef.current;
setDetailLoading(true);
setDetailError(null);
try {
const data = await getNotebook(notebookId);
if (detailRequestRef.current !== requestId) return;
setSelected({ ...data, records: data.records ?? [] });
} catch (err) {
if (detailRequestRef.current !== requestId) return;
setSelected(null);
setDetailError(messageOf(err));
} finally {
if (detailRequestRef.current === requestId) setDetailLoading(false);
}
}, []);
const reload = useCallback(async () => {
setError(null);
try {
const next = await listNotebooks();
setNotebooks(next);
setSelectedId((current) => {
if (current && next.some((n) => n.id === current)) return current;
return next.length ? next[0].id : null;
});
} catch (err) {
setError(messageOf(err));
} finally {
setLoading(false);
}
}, []);
useEffect(() => {
void reload();
}, [reload]);
useEffect(() => {
if (!selectedId) {
setSelected(null);
return;
}
void loadDetail(selectedId);
}, [selectedId, loadDetail]);
const select = useCallback((notebookId: string) => {
setSelectedId(notebookId);
}, []);
const create = useCallback(
async (name: string, description: string) => {
const trimmed = name.trim();
if (!trimmed) return null;
const created = await createNotebook({
name: trimmed,
description: description.trim(),
});
await reload();
setSelectedId(created.id);
return created.id;
},
[reload],
);
const rename = useCallback(
async (
notebookId: string,
changes: { name?: string; description?: string; color?: string },
) => {
const updated = await updateNotebook(notebookId, changes);
setNotebooks((prev) =>
prev.map((n) => (n.id === notebookId ? { ...n, ...updated } : n)),
);
setSelected((prev) =>
prev && prev.id === notebookId ? { ...prev, ...updated } : prev,
);
},
[],
);
const remove = useCallback(
async (notebookId: string) => {
await deleteNotebook(notebookId);
setNotebooks((prev) => prev.filter((n) => n.id !== notebookId));
setSelectedId((current) => {
if (current !== notebookId) return current;
const rest = notebooks.filter((n) => n.id !== notebookId);
return rest.length ? rest[0].id : null;
});
},
[notebooks],
);
// Keep the sidebar's record count honest after any record-level change
// without paying for a full list refetch.
const adjustCount = useCallback((notebookId: string, delta: number) => {
setNotebooks((prev) =>
prev.map((n) =>
n.id === notebookId
? { ...n, record_count: Math.max(0, (n.record_count ?? 0) + delta) }
: n,
),
);
}, []);
const editRecord = useCallback(
async (
recordId: string,
changes: { title?: string; summary?: string; output?: string },
) => {
if (!selectedId) return;
const updated = await updateNotebookRecord(selectedId, recordId, changes);
setSelected((prev) =>
prev
? {
...prev,
records: prev.records.map((r) =>
r.id === recordId ? { ...r, ...updated } : r,
),
}
: prev,
);
},
[selectedId],
);
const removeRecord = useCallback(
async (recordId: string) => {
if (!selectedId) return;
await deleteNotebookRecord(selectedId, recordId);
setSelected((prev) =>
prev
? { ...prev, records: prev.records.filter((r) => r.id !== recordId) }
: prev,
);
adjustCount(selectedId, -1);
},
[selectedId, adjustCount],
);
const relocateRecord = useCallback(
async (
recordId: string,
targetNotebookId: string,
mode: "move" | "copy",
) => {
if (!selectedId) return;
await relocateNotebookRecord(
selectedId,
recordId,
targetNotebookId,
mode,
);
if (mode === "move") {
setSelected((prev) =>
prev
? {
...prev,
records: prev.records.filter((r) => r.id !== recordId),
}
: prev,
);
adjustCount(selectedId, -1);
}
adjustCount(targetNotebookId, 1);
},
[selectedId, adjustCount],
);
return useMemo(
() => ({
notebooks,
selectedId,
selected,
loading,
detailLoading,
error,
detailError,
select,
reload,
create,
rename,
remove,
editRecord,
removeRecord,
relocateRecord,
}),
[
notebooks,
selectedId,
selected,
loading,
detailLoading,
error,
detailError,
select,
reload,
create,
rename,
remove,
editRecord,
removeRecord,
relocateRecord,
],
);
}