Files
DeepTutor/web/components/common/AssistantResponse.tsx
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

117 lines
4.1 KiB
TypeScript

"use client";
import { Fragment, memo, useMemo } from "react";
import MarkdownRenderer from "@/components/common/MarkdownRenderer";
import ModelThinkingCard from "@/components/common/ModelThinkingCard";
import { useReading } from "@/context/ReadingContext";
import {
hasVisibleMarkdownContent,
repairMalformedStrongEmphasis,
stripArtifactAnnotations,
} from "@/lib/markdown-display";
import { linkifyLocatorCitations } from "@/lib/reading-citations";
import { parseModelThinkingSegments } from "@/lib/think-segments";
import { useSmoothStreamText } from "@/hooks/useSmoothStreamText";
interface AssistantResponseProps {
content: string;
className?: string;
/**
* When true, the renderer drives the visible text through a rAF
* typewriter (``useSmoothStreamText``) so the markdown grows at a
* steady, frame-aligned pace even when the upstream LLM emits
* uneven chunks. Pass ``false`` for completed turns and any non-
* streaming surface — the hook short-circuits to a pass-through
* in that case.
*/
isStreaming?: boolean;
}
function AssistantResponseImpl({
content,
className = "text-[16px] leading-[1.75]",
isStreaming = false,
}: AssistantResponseProps) {
const displayContent = useSmoothStreamText(content, isStreaming);
// Immersive reading only: turn `[p.12]` citations into anchors the reader
// pane intercepts. Outside that mode `material` is null (there is no
// provider on most surfaces, and none when no document is open), so this is a
// no-op and every other chat surface renders byte-identically to before.
const { material } = useReading();
const citedContent = useMemo(
() =>
material
? linkifyLocatorCitations(displayContent, {
maxLocator: material.unit_count,
})
: displayContent,
[displayContent, material],
);
const segments = useMemo(
() => parseModelThinkingSegments(stripArtifactAnnotations(citedContent)),
[citedContent],
);
// Decide whether the message has anything worth rendering. We consider both
// ordinary markdown segments and model-thinking blocks: a turn that only
// ever produced a <think> scratchpad should still render the collapsed card
// instead of dropping the assistant bubble entirely.
const hasRenderableSegment = useMemo(() => {
return segments.some((segment) => {
if (segment.kind === "think") return segment.content.trim().length > 0;
return hasVisibleMarkdownContent(segment.content);
});
}, [segments]);
if (!hasRenderableSegment) return null;
// role="article" lets screen-reader users locate each assistant turn as a
// structured landmark. aria-live="polite" + aria-atomic="false" announces
// streamed-in content as the user pauses, without re-reading the whole
// bubble each token. Together this is the minimal pattern that turns a
// silent stream into an audible one.
return (
<div
role="article"
aria-live="polite"
aria-atomic="false"
className={className}
>
{segments.map((segment, index) => {
if (segment.kind === "think") {
return (
<ModelThinkingCard
key={`think-${index}`}
content={segment.content}
closed={segment.closed}
/>
);
}
const repairedContent = repairMalformedStrongEmphasis(segment.content);
if (!hasVisibleMarkdownContent(repairedContent)) {
return <Fragment key={`text-${index}`} />;
}
return (
<MarkdownRenderer
key={`text-${index}`}
content={repairedContent}
variant="prose"
className="text-[var(--foreground)]"
/>
);
})}
</div>
);
}
// Memoize so completed messages don't re-parse markdown when an
// unrelated streaming sibling updates the parent — the streaming
// message gets a fresh ``msg.content`` per delta and re-renders
// naturally, but every other bubble keeps its previous render output.
const AssistantResponse = memo(AssistantResponseImpl);
AssistantResponse.displayName = "AssistantResponse";
export default AssistantResponse;