All files / src/components/chat/ChatDock ChatDockPanel.tsx

94.11% Statements 16/17
95.45% Branches 21/22
80% Functions 4/5
94.11% Lines 16/17

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149                                                    1x       1x                       1x         59x 59x   11x   59x   11x   59x   59x   59x   59x       59x   59x                       3x                                                                                                 1x                                      
import AgentChatView from "@/components/chat/AgentChat/AgentChatView";
import ChatTargetPicker from "@/components/chat/ChatDock/ChatTargetPicker";
import {
  type ChatTarget,
  defaultTarget,
} from "@/components/chat/ChatDock/target";
import { ChatViewStatus } from "@/components/chat/ChatView/ChatViewFrame";
import ModelChatView from "@/components/chat/ModelChat/ModelChatView";
import type { Agent } from "@/objects/Agent";
import { type Model, selectableModels } from "@/objects/Model";
import { APP_ROUTES } from "@/routes/const";
import { QUERY_KEYS } from "@/services/query_keys";
import { useApiHandler } from "@/services/useApiHandler";
import { useQuery } from "@tanstack/react-query";
import { ChevronDown, Maximize2, MessageCircle, Minimize2 } from "lucide-react";
import React, { useState } from "react";
import { Link } from "react-router-dom";
 
export interface ChatDockPanelProps {
  fullscreen: boolean;
  onFullscreenChange: (fullscreen: boolean) => void;
  /** Minimise: the dock hides the panel and keeps it as it is. */
  onClose: () => void;
}
 
const HEADER_BUTTON =
  "inline-flex items-center justify-center size-8 p-0 rounded-md border-0 bg-transparent text-gray-500 dark:text-gray-400 hover:text-gray-800 dark:hover:text-gray-200 hover:bg-gray-100 dark:hover:bg-gray-800 transition-colors";
 
/** Full screen is for a wide screen: a phone's panel already fills it. */
const FULLSCREEN_BUTTON =
  "hidden sm:inline-flex items-center justify-center size-8 p-0 rounded-md border-0 bg-transparent text-gray-500 dark:text-gray-400 hover:text-gray-800 dark:hover:text-gray-200 hover:bg-gray-100 dark:hover:bg-gray-800 transition-colors";
 
/**
 * The chat dock's panel (`ChatDock`, which loads it on first open): who to
 * chat with, and that chat — the same `AgentChatView` / `ModelChatView` the
 * chat pages draw, in their panel frame.
 *
 * It opens on CHAINR Bot (`defaultTarget`) until someone picks another;
 * the choice lasts as long as the dock does, not past a reload. Picking
 * another closes the chat on screen — a turn it was streaming runs on and
 * is stored, as when its page is left — and keys a fresh one.
 */
const ChatDockPanel: React.FC<ChatDockPanelProps> = ({
  fullscreen,
  onFullscreenChange,
  onClose,
}) => {
  const { getApiHandler } = useApiHandler();
  const agentsQuery = useQuery<Agent[] | null>({
    queryKey: [QUERY_KEYS.GET.AGENTS],
    queryFn: () => getApiHandler().agent_router.list(),
  });
  const modelsQuery = useQuery<Model[] | null>({
    queryKey: [QUERY_KEYS.GET.MODELS],
    queryFn: () => getApiHandler().model_router.list(),
  });
  const [chosen, setChosen] = useState<ChatTarget | null>(null);
 
  const agents = agentsQuery.data ?? [];
  const target =
    chosen ??
    defaultTarget(agents, modelsQuery.data, !agentsQuery.isPending);
  const models = selectableModels(
    modelsQuery.data ?? [],
    target?.kind === "model" ? target.id : undefined,
  );
  const loading = agentsQuery.isPending || modelsQuery.isPending;
 
  return (
    <>
      <header className="flex shrink-0 items-center gap-2 border-b border-gray-200 dark:border-gray-700 bg-white dark:bg-gray-900 px-2 py-1.5">
        <ChatTargetPicker
          agents={agents}
          models={models}
          value={target}
          onChange={setChosen}
        />
        <div className="ml-auto flex shrink-0 items-center gap-0.5">
          <button
            type="button"
            onClick={() => onFullscreenChange(!fullscreen)}
            aria-label={fullscreen ? "Exit full screen" : "Full screen"}
            title={fullscreen ? "Exit full screen (Esc)" : "Full screen"}
            className={FULLSCREEN_BUTTON}
          >
            {fullscreen ? (
              <Minimize2 className="size-4" aria-hidden="true" />
            ) : (
              <Maximize2 className="size-4" aria-hidden="true" />
            )}
          </button>
          <button
            type="button"
            onClick={onClose}
            aria-label="Minimise chat"
            title="Minimise chat (Esc)"
            className={HEADER_BUTTON}
          >
            <ChevronDown className="size-4" aria-hidden="true" />
          </button>
        </div>
      </header>
 
      <div className="flex flex-1 min-h-0 flex-col p-3">
        {target?.kind === "agent" ? (
          <AgentChatView
            key={`agent:${target.id}`}
            agentId={target.id}
            frame="panel"
            autoFocus
          />
        ) : target?.kind === "model" ? (
          <ModelChatView
            key={`model:${target.id}`}
            modelId={target.id}
            frame="panel"
            autoFocus
          />
        ) : loading ? (
          <ChatViewStatus frame="panel" state="loading" noun="agent" />
        ) : (
          <NothingToChatWith />
        )}
      </div>
    </>
  );
};
 
/** A workspace with no agent and no model a chat can use. */
const NothingToChatWith: React.FC = () => (
  <div className="flex flex-1 flex-col items-center justify-center gap-2 px-6 text-center">
    <MessageCircle className="size-6 text-gray-300 dark:text-gray-600" aria-hidden="true" />
    <p className="text-sm font-medium text-gray-700 dark:text-gray-300">
      Nothing to chat with here yet
    </p>
    <p className="text-xs text-gray-500 dark:text-gray-400">
      This workspace has no agent, and no model a chat can use.
    </p>
    <Link
      to={APP_ROUTES.USER.INFERENCE.AGENTS.new}
      className="text-xs font-medium text-indigo-600 dark:text-indigo-400 hover:underline"
    >
      Create an agent
    </Link>
  </div>
);
 
export default ChatDockPanel;