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

100% Statements 21/21
96.15% Branches 25/26
100% Functions 11/11
100% Lines 17/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 150 151 152 153 154                                                                      1x 1x         1x 59x                               1x           59x 96x   92x   59x 59x   59x                                         3x         2x 2x                 48x                           48x                           48x                                  
import { ModelOption } from "@/components/chat/AgentChat/AgentChatToolbar";
import { AppearanceSwatch } from "@/components/appearance/AppearancePicker";
import { AGENT_ICON_REGISTRY } from "@/components/appearance/icons";
import MakerLogo from "@/components/img/MakerLogo/MakerLogo";
import {
  type ChatTarget,
  parseTargetKey,
  targetKey,
} from "@/components/chat/ChatDock/target";
import {
  DropdownMenu,
  DropdownMenuContent,
  DropdownMenuLabel,
  DropdownMenuRadioGroup,
  DropdownMenuRadioItem,
  DropdownMenuSeparator,
  DropdownMenuTrigger,
} from "@/components/ui/dropdown-menu";
import type { Agent } from "@/objects/Agent";
import { isSystemManaged } from "@/objects/Doc";
import { type Model, modelDisplayName } from "@/objects/Model";
import { modelMaker, modelMakerKey } from "@/objects/modelOrigin";
import { ChevronDown } from "lucide-react";
import React from "react";
 
export interface ChatTargetPickerProps {
  /** The workspace's agents, CHAINR Bot among them. */
  agents: Agent[];
  /** The models a chat may use (`selectableModels`). */
  models: Model[];
  value: ChatTarget | null;
  onChange: (target: ChatTarget) => void;
}
 
/** A model's glyph: its maker's mark, framed as an agent's swatch is. */
const ModelGlyph: React.FC<{ model: Model }> = ({ model }) => (
  <span className="inline-flex shrink-0 items-center justify-center rounded-lg border p-1.5 bg-white dark:bg-gray-800 border-gray-200 dark:border-gray-700">
    <MakerLogo maker={modelMakerKey(model)} className="size-4" decorative />
  </span>
);
 
const AgentGlyph: React.FC<{ agent: Agent }> = ({ agent }) => (
  <AppearanceSwatch
    registry={AGENT_ICON_REGISTRY}
    icon={agent.icon}
    accent={agent.accent}
    size="sm"
  />
);
 
/**
 * Who the chat dock is talking to: the built-in CHAINR Bot, one of the
 * workspace's agents, or a model directly — one menu, in that order.
 *
 * The menu portals to <body>, so it carries z-[70] to open over the dock in
 * full screen (z-[60]), as the agent chat's own menus do. Closing it leaves
 * focus where the new chat puts it (its message box), not on the trigger.
 */
const ChatTargetPicker: React.FC<ChatTargetPickerProps> = ({
  agents,
  models,
  value,
  onChange,
}) => {
  const builtIn = agents.filter(isSystemManaged);
  const own = agents.filter((a) => !isSystemManaged(a));
  const agent =
    value?.kind === "agent" ? agents.find((a) => a.id === value.id) : undefined;
  const model =
    value?.kind === "model" ? models.find((m) => m.id === value.id) : undefined;
  const name = agent?.name ?? (model ? modelDisplayName(model) : "Choose a chat");
 
  return (
    <DropdownMenu>
      <DropdownMenuTrigger asChild>
        <button
          type="button"
          aria-label={`Chat with: ${name}`}
          title="Choose who to chat with"
          className="min-w-0 inline-flex items-center gap-2 h-9 pl-1 pr-2 rounded-lg border-0 bg-transparent text-sm font-semibold text-gray-900 dark:text-gray-100 hover:bg-gray-100 dark:hover:bg-gray-800 data-[state=open]:bg-gray-100 dark:data-[state=open]:bg-gray-800 transition-colors"
        >
          {agent ? (
            <AgentGlyph agent={agent} />
          ) : model ? (
            <ModelGlyph model={model} />
          ) : null}
          <span className="min-w-0 truncate">{name}</span>
          <ChevronDown className="size-4 shrink-0 text-gray-400" />
        </button>
      </DropdownMenuTrigger>
      <DropdownMenuContent
        align="start"
        className="z-[70] w-72 max-w-[calc(100vw-2rem)] max-h-80"
        onCloseAutoFocus={(e) => e.preventDefault()}
      >
        <DropdownMenuRadioGroup
          value={value ? targetKey(value) : ""}
          onValueChange={(key) => {
            const next = parseTargetKey(key);
            Eif (next) onChange(next);
          }}
        >
          {builtIn.length > 0 && (
            <>
              <DropdownMenuLabel className="text-xs text-muted-foreground">
                Built-in
              </DropdownMenuLabel>
              {builtIn.map((a) => (
                <DropdownMenuRadioItem key={a.id} value={targetKey({ kind: "agent", id: a.id })}>
                  <AgentGlyph agent={a} />
                  <span className="min-w-0 truncate">{a.name}</span>
                </DropdownMenuRadioItem>
              ))}
            </>
          )}
          {own.length > 0 && (
            <>
              {builtIn.length > 0 && <DropdownMenuSeparator />}
              <DropdownMenuLabel className="text-xs text-muted-foreground">
                Agents
              </DropdownMenuLabel>
              {own.map((a) => (
                <DropdownMenuRadioItem key={a.id} value={targetKey({ kind: "agent", id: a.id })}>
                  <AgentGlyph agent={a} />
                  <span className="min-w-0 truncate">{a.name}</span>
                </DropdownMenuRadioItem>
              ))}
            </>
          )}
          {models.length > 0 && (
            <>
              {agents.length > 0 && <DropdownMenuSeparator />}
              <DropdownMenuLabel className="text-xs text-muted-foreground">
                Models
              </DropdownMenuLabel>
              {models.map((m) => (
                <DropdownMenuRadioItem key={m.id} value={targetKey({ kind: "model", id: m.id })}>
                  <ModelOption
                    name={modelDisplayName(m)}
                    detail={modelMaker(m)}
                    maker={modelMakerKey(m)}
                  />
                </DropdownMenuRadioItem>
              ))}
            </>
          )}
        </DropdownMenuRadioGroup>
      </DropdownMenuContent>
    </DropdownMenu>
  );
};
 
export default ChatTargetPicker;