File size: 4,412 Bytes
05c5ed5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import { WorkflowToolKey } from "lib/ai/workflow/workflow.interface";
import { groupBy } from "lib/utils";
import { ChevronDownIcon, WrenchIcon } from "lucide-react";
import { useTranslations } from "next-intl";
import { ReactNode, useMemo, useState } from "react";
import { Button } from "ui/button";
import {
  Command,
  CommandEmpty,
  CommandGroup,
  CommandInput,
  CommandItem,
  CommandList,
} from "ui/command";
import { MCPIcon } from "ui/mcp-icon";
import { Popover, PopoverContent, PopoverTrigger } from "ui/popover";

export function WorkflowToolSelect({
  tools,
  onChange,
  children,
  side,
  align,
  tool,
}: {
  side?: "top" | "bottom" | "left" | "right";
  align?: "start" | "end" | "center";
  tools: WorkflowToolKey[];
  onChange: (tool: WorkflowToolKey) => void;
  children?: ReactNode;
  tool?: WorkflowToolKey;
}) {
  const t = useTranslations();
  const [open, setOpen] = useState(false);
  const mcpToolsByServerId = useMemo(() => {
    const mcpTools = tools.filter((tool) => tool.type == "mcp-tool");
    return Object.entries(groupBy(mcpTools, "serverId")).map(
      ([serverId, tools]) => {
        return {
          serverId,
          serverName: tools[0].serverName,
          tools,
        };
      },
    );
  }, [tools]);
  const defaultTools = useMemo(() => {
    return tools.filter((tool) => tool.type == "app-tool");
  }, [tools]);

  const selectedToolLabel = useMemo(() => {
    if (!tool)
      return (
        <>
          <WrenchIcon className="size-3.5" />
          <span className="text-muted-foreground">
            {t("Common.selectTool")}
          </span>
        </>
      );
    if (tool.type == "mcp-tool") {
      return (
        <>
          <MCPIcon className="size-3.5" />
          <span className="font-bold">{tool.serverName}</span>
          <div className="bg-primary text-primary-foreground px-2 rounded-md truncate">
            {tool.id}
          </div>
        </>
      );
    }
    return (
      <>
        <WrenchIcon className="size-3.5" />
        <span className="font-semibold truncate">{tool.id}</span>
      </>
    );
  }, [tool]);

  return (
    <Popover open={open} onOpenChange={setOpen}>
      <PopoverTrigger asChild>
        {children || (
          <Button
            variant={"outline"}
            size={"lg"}
            className="border data-[state=open]:bg-input!"
          >
            {selectedToolLabel}
            <ChevronDownIcon className="size-3.5 ml-auto" />
          </Button>
        )}
      </PopoverTrigger>
      <PopoverContent className="p-0" side={side} align={align}>
        <Command>
          <CommandInput placeholder={t("Common.search")} />
          <CommandList>
            <CommandEmpty>{t("Common.noResults")}</CommandEmpty>
            {mcpToolsByServerId.map((mcpTools) => {
              return (
                <CommandGroup
                  key={mcpTools.serverId}
                  heading={mcpTools.serverName}
                >
                  {mcpTools.tools.map((tool) => {
                    return (
                      <CommandItem
                        key={tool.id}
                        onSelect={() => {
                          onChange(tool);
                          setOpen(false);
                        }}
                        className="cursor-pointer"
                      >
                        <WrenchIcon className="size-3.5" />
                        <span className="font-semibold truncate">
                          {tool.id}
                        </span>
                      </CommandItem>
                    );
                  })}
                </CommandGroup>
              );
            })}
            <CommandGroup heading={"App Default Tools"}>
              {defaultTools.map((tool) => {
                return (
                  <CommandItem
                    key={tool.id}
                    onSelect={() => {
                      onChange(tool);
                      setOpen(false);
                    }}
                    className="cursor-pointer"
                  >
                    <WrenchIcon className="size-3.5" />
                    <span className="font-semibold truncate">{tool.id}</span>
                  </CommandItem>
                );
              })}
            </CommandGroup>
          </CommandList>
        </Command>
      </PopoverContent>
    </Popover>
  );
}