Skip to content

DeepTutor: Lifelong Personalized Tutoring. https://deeptutor.info/.

6.4 KB
Raw Blame
"use client";

/**
 * The reading companion's composer — the same one the main chat page and
 * mastery study use, wired through the unified chat context.
 *
 * A reading conversation is a chat session like any other, so it gets the
 * whole composer: attachments, the knowledge-base picker (with real scope
 * activation, not a reading-local facsimile), the model selector, dictation.
 * The one thing specific to this surface is grounding: when the learner has
 * a passage selected, the pending viewport/quote has to reach the backend
 * before the message does, exactly like the retired bespoke textarea did.
 */

import { useCallback, useRef } from "react";
import { COMMAND_CONFIRMATION_FAILED } from "@/features/chat/transport/command-delivery";
import { useTranslation } from "react-i18next";

import { QuotedPassage } from "@/components/chat/home/QuotedPassage";
import StandaloneComposer, {
  type StandaloneComposerSubmission,
} from "@/components/chat/home/StandaloneComposer";
import { useChatStateAdapter } from "@/features/chat/ChatStateAdapter";
import { useChatWorkspaces } from "@/hooks/useChatWorkspaces";
import { useComposerResources } from "@/hooks/useComposerResources";
import { useWorkspaceChatActions } from "@/hooks/useWorkspaceChatActions";
import {
  hasPendingAskUser,
  REPLY_SENT_AS_NEW_MESSAGE,
} from "@/lib/ask-user-state";
import { notify } from "@/lib/notifications";
import { setReadingViewport } from "@/lib/reading-turn-state";

export function ReadingComposer({
  placeholder,
  placeholderCompletion,
  selection,
  onSent,
  onRemoveSelection,
  linkedSessionIds,
  prefillInputRef,
}: {
  placeholder: string;
  /** Offered question the composer lets the learner take with Tab. */
  placeholderCompletion?: string;
  selection: { quote: string; locator: number } | null;
  /** Clears the pending selection once the message is on its way. */
  onSent: () => void;
  /** The learner dropped the quoted passage before sending. */
  onRemoveSelection: () => void;
  /** Reading-specific "reference these other reading conversations" links. */
  linkedSessionIds: string[];
  /** Lets the reader pane drop a quoted selection's focus into the box. */
  prefillInputRef?: React.MutableRefObject<((text: string) => void) | null>;
}) {
  const {
    state,
    sendMessage,
    submitUserReply,
    cancelStreamingTurn,
    setKBs,
    setLLMSelection,
    setPersonaSelection,
    setResourceSelection,
  } = useChatStateAdapter();
  const { workspaces } = useChatWorkspaces();
  const resourceCatalog = useComposerResources(state.workspaceId, workspaces);
  const { capabilities, activeCapabilityValue, selectCapability } =
    useWorkspaceChatActions();
  const { t } = useTranslation();
  const fallbackInputRef = useRef<((text: string) => void) | null>(null);
  const replyInputRef = prefillInputRef ?? fallbackInputRef;

  const awaitingUserReply = hasPendingAskUser(
    state.messages[state.messages.length - 1]?.events,
  );

  const handleSubmit = useCallback(
    (submission: StandaloneComposerSubmission) => {
      const sendAsNewMessage = () => {
        if (selection) {
          setReadingViewport({
            locator: selection.locator,
            selection: selection.quote,
          });
        }
        // The composer's own "@ reference an earlier session" picker and this
        // surface's persistent linked-conversations list share one wire slot;
        // union them rather than letting either silently win.
        const historyReferences = Array.from(
          new Set([...linkedSessionIds, ...submission.historyReferences]),
        );
        sendMessage(
          submission.content,
          submission.attachments,
          // How many times the companion may consult the selected agent this
          // turn. Absent when no agent is picked, which is the ordinary case.
          submission.subagentBudget
            ? {
                ...(submission.config ?? {}),
                subagent_consult_budget: submission.subagentBudget,
              }
            : submission.config,
          submission.notebookReferences,
          historyReferences,
          { bookReferences: submission.bookReferences },
          submission.questionNotebookReferences,
          submission.persona ?? undefined,
          submission.memoryReferences,
        );
        onSent();
        window.setTimeout(() => setReadingViewport({ selection: "" }), 0);
      };

      // A turn paused on a question: what the reader typed is their answer,
      // not a new message. See ChatWorkspace's handleSend for the same routing
      // — including the fall-through on a refusal, which is what keeps a dead
      // question from swallowing the text they just wrote.
      if (awaitingUserReply && submission.content.trim()) {
        void submitUserReply({ text: submission.content }).then((sent) => {
          if (sent) return;
          notify(t(REPLY_SENT_AS_NEW_MESSAGE));
          sendAsNewMessage();
        }).catch(() => {
          notify(t(COMMAND_CONFIRMATION_FAILED), { tone: "error" });
          replyInputRef.current?.(submission.content);
        });
        return;
      }
      sendAsNewMessage();
    },
    [
      awaitingUserReply,
      linkedSessionIds,
      onSent,
      selection,
      sendMessage,
      submitUserReply,
      replyInputRef,
      t,
    ],
  );

  return (
    <StandaloneComposer
      capabilities={capabilities}
      activeCapValue={activeCapabilityValue}
      onSelectCapability={selectCapability}
      showCapabilityChip
      hasMessages={state.messages.length > 0}
      isStreaming={state.isStreaming}
      awaitingUserReply={awaitingUserReply}
      selectedKnowledgeBases={state.knowledgeBases}
      onKnowledgeBasesChange={setKBs}
      llmSelection={state.llmSelection}
      onLLMSelectionChange={setLLMSelection}
      personaSelection={state.personaSelection}
      onPersonaSelectionChange={setPersonaSelection}
      resourceCatalog={resourceCatalog}
      resourceSelection={state.resourceSelection}
      onResourceSelectionChange={setResourceSelection}
      onSubmit={handleSubmit}
      onCancelStreaming={cancelStreamingTurn}
      inputPlaceholder={placeholder}
      inputPlaceholderCompletion={placeholderCompletion}
      inputHeader={
        selection ? (
          <QuotedPassage
            quote={selection.quote}
            onRemove={onRemoveSelection}
          />
        ) : null
      }
      prefillInputRef={replyInputRef}
    />
  );
}