Files
DeepTutor/web/lib/reading-turn-state.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

89 lines
3.0 KiB
TypeScript

/**
* The reader's live state, read once when a chat turn is sent.
*
* Deliberately a module-level cell rather than React state on a shared context.
* The viewport changes on every scroll tick; if the chat subscribed to it, every
* pixel of scrolling would re-render the whole message list. Nothing renders
* from these values — they are only read at send time — so a plain cell is both
* cheaper and more honest about that.
*
* Written by the reader pane; read by the chat's turn builder.
*/
/** The capability value the composer sends for immersive reading. */
export const READING_CAPABILITY = "immersive_reading";
export interface ReadingTurnState {
materialId: string | null;
locator: number;
selection: string;
}
const state: ReadingTurnState = {
materialId: null,
locator: 0,
selection: "",
};
export function setReadingMaterial(materialId: string | null): void {
state.materialId = materialId;
if (!materialId) {
// Closing a document must not leave its viewport behind: the next turn
// would tell the model the user is looking at a page of a closed file.
state.locator = 0;
state.selection = "";
}
}
export function setReadingViewport(next: {
locator?: number;
selection?: string;
}): void {
if (typeof next.locator === "number" && Number.isFinite(next.locator)) {
state.locator = next.locator > 0 ? Math.floor(next.locator) : 0;
}
if (typeof next.selection === "string") {
state.selection = next.selection;
}
}
export function getReadingTurnState(): ReadingTurnState {
return { ...state };
}
/**
* Turn fields to merge into a `start_turn` payload.
*
* Empty unless the turn is *actually* an immersive-reading turn — the caller
* passes the capability it is about to send, and anything else gets nothing.
*
* Both halves of that condition are load-bearing. The open document lives in a
* provider mounted in the workspace layout so it survives the remount that
* sending the first message causes, which also means it survives switching modes
* and starting a new session. Keying only on "is a document open" therefore
* attached the reader to *every* later turn: a fresh chat session, in Chat mode,
* would open with "I see you're reading …" and cite pages from a document the
* user had moved on from.
*/
export function readingTurnFields(capability: string | null | undefined): {
reading_material_id?: string;
reading_viewport?: { locator?: number; selection?: string };
} {
if (capability !== READING_CAPABILITY) return {};
if (!state.materialId) return {};
const viewport: { locator?: number; selection?: string } = {};
if (state.locator > 0) viewport.locator = state.locator;
if (state.selection) viewport.selection = state.selection;
return {
reading_material_id: state.materialId,
...(Object.keys(viewport).length ? { reading_viewport: viewport } : {}),
};
}
/** Test seam: reset the cell between cases. */
export function resetReadingTurnState(): void {
state.materialId = null;
state.locator = 0;
state.selection = "";
}