File size: 22,011 Bytes
87337b1 |
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 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 |
import * as React from "react"
import { buttonVariants } from "@/components/ui/button"
import {
Select,
SelectContent,
SelectGroup,
SelectItem,
SelectLabel,
SelectTrigger,
SelectValue,
} from "@/components/ui/select"
import {
Sheet,
SheetContent,
SheetDescription,
SheetHeader,
SheetTitle,
SheetTrigger,
} from "@/components/ui/sheet"
import {
Form,
FormControl,
FormDescription,
FormField,
FormItem,
FormLabel,
FormMessage,
} from "@/components/ui/form"
import { Button } from "@/components/ui/button"
import { cn } from "@/lib/utils"
import { useAppSelector, useGraphs, } from "@/common/hooks"
import { AddonDef, Graph, Destination, GraphEditor, ProtocolLabel as GraphConnProtocol, ProtocolLabel } from "@/common/graph"
import { toast } from "sonner"
import { BoxesIcon, ChevronRightIcon, LoaderCircleIcon, SettingsIcon, Trash2Icon, WrenchIcon } from "lucide-react"
import { useForm } from "react-hook-form"
import { zodResolver } from "@hookform/resolvers/zod"
import { z } from "zod"
import { DropdownMenu, DropdownMenuContent, DropdownMenuItem, DropdownMenuPortal, DropdownMenuSub, DropdownMenuSubContent, DropdownMenuSubTrigger, DropdownMenuTrigger } from "../ui/dropdown"
import { isLLM } from "@/common"
import { compatibleTools, ModuleRegistry, ModuleTypeLabels, moduleRegistry, toolModuleRegistry } from "@/common/moduleConfig"
export function RemoteModuleCfgSheet() {
const addonModules = useAppSelector((state) => state.global.addonModules);
const { getGraphNodeAddonByName, selectedGraph, update: updateGraph, installedAndRegisteredModulesMap, installedAndRegisteredToolModules } = useGraphs();
const metadata = React.useMemo(() => {
const dynamicMetadata: Record<string, { type: string; options: { value: string; label: string }[] }> = {};
if (selectedGraph) {
Object.keys(installedAndRegisteredModulesMap).forEach((key) => {
const moduleTypeKey = key as ModuleRegistry.NonToolModuleType;
// Check if the current graph has a node whose name contains the ModuleType
const hasMatchingNode = selectedGraph.nodes.some((node) =>
node.name.includes(moduleTypeKey)
);
if (hasMatchingNode) {
dynamicMetadata[moduleTypeKey] = {
type: "string",
options: installedAndRegisteredModulesMap[moduleTypeKey].map((module) => ({
value: module.name,
label: module.label,
})),
};
}
});
}
return dynamicMetadata;
}, [installedAndRegisteredModulesMap, selectedGraph]);
const initialData = React.useMemo(() => {
const dynamicInitialData: Record<string, string | null | undefined> = {};
if (selectedGraph) {
Object.keys(installedAndRegisteredModulesMap).forEach((key) => {
const moduleTypeKey = key as ModuleRegistry.ModuleType;
// Check if the current graph has a node whose name contains the ModuleType
const hasMatchingNode = selectedGraph.nodes.some((node) =>
node.name.includes(moduleTypeKey)
);
if (hasMatchingNode) {
dynamicInitialData[moduleTypeKey] = getGraphNodeAddonByName(moduleTypeKey)?.addon;
}
});
}
return dynamicInitialData;
}, [installedAndRegisteredModulesMap, selectedGraph, getGraphNodeAddonByName]);
return (
<Sheet>
<SheetTrigger
className={cn(
buttonVariants({ variant: "outline", size: "icon" }),
"bg-transparent"
)}
>
<BoxesIcon />
</SheetTrigger>
<SheetContent className="w-[400px] overflow-y-auto sm:w-[540px]">
<SheetHeader>
<SheetTitle>Module Picker</SheetTitle>
<SheetDescription>
You can adjust STT/TTS/LLM/LLMv2v extension modules here, the values will be
written into property.json file when you save.
</SheetDescription>
</SheetHeader>
<div className="my-4">
<GraphModuleCfgForm
initialData={initialData}
metadata={metadata}
onUpdate={async (data, tools) => {
try {
// Clone the selectedGraph to avoid mutating the original graph
const selectedGraphCopy: Graph = JSON.parse(JSON.stringify(selectedGraph));
const nodes = selectedGraphCopy.nodes;
let needUpdate = false;
let enableRTCVideoSubscribe = false;
// Retrieve the agora_rtc node
const agoraRtcNode = GraphEditor.findNode(selectedGraphCopy, "agora_rtc");
if (!agoraRtcNode) {
toast.error("agora_rtc node not found in the graph");
return;
}
// Update graph nodes with selected modules
Object.entries(data).forEach(([key, value]) => {
const node = nodes.find((n) => n.name === key);
if (node && value && node.addon !== value) {
node.addon = value;
node.property = addonModules.find((module) => module.name === value)?.defaultProperty;
needUpdate = true;
}
});
const reasoningNodesWithVisualSupport = GraphEditor.findNodeByPredicate(selectedGraphCopy, (node) => {
const module = moduleRegistry[node.addon]
if (!module) {
return false
}
if (module.type === ModuleRegistry.ModuleType.LLM) {
const llmModule = module as ModuleRegistry.LLMModule
return isLLM(node.name) && llmModule.options.inputModalities.includes(ModuleRegistry.Modalities.Video)
}
if (module.type === ModuleRegistry.ModuleType.V2V) {
const v2vModule = module as ModuleRegistry.V2VModule
return isLLM(node.name) && v2vModule.options.inputModalities.includes(ModuleRegistry.Modalities.Video)
}
return false
})
if (reasoningNodesWithVisualSupport) {
GraphEditor.addOrUpdateConnection(
selectedGraphCopy,
`${agoraRtcNode.name}`,
`${reasoningNodesWithVisualSupport.name}`,
ProtocolLabel.VIDEO_FRAME,
"video_frame"
);
enableRTCVideoSubscribe = true;
}
// Identify removed tools and process them
const currentToolsInGraph = nodes
.filter((node) => installedAndRegisteredToolModules.map((module) => module.name).includes(node.addon))
.map((node) => node.addon);
const removedTools = currentToolsInGraph.filter((tool) => !tools.includes(tool));
removedTools.forEach((tool) => {
GraphEditor.removeNodeAndConnections(selectedGraphCopy, tool);
needUpdate = true;
});
// Process tool modules
if (tools.length > 0) {
if (!enableRTCVideoSubscribe) {
enableRTCVideoSubscribe = tools.some((tool) => tool.includes("vision"))
}
tools.forEach((tool) => {
if (!currentToolsInGraph.includes(tool)) {
const toolModule = addonModules.find((module) => module.name === tool);
if (!toolModule) {
toast.error(`Module ${tool} not found`);
return;
}
const toolNode = GraphEditor.addNode(selectedGraphCopy, tool, tool, "default", toolModule.defaultProperty)
// Create or update connections
const llmNode = GraphEditor.findNodeByPredicate(selectedGraphCopy, (node) => isLLM(node.name));
if (llmNode) {
GraphEditor.linkTool(selectedGraphCopy, llmNode, toolNode, toolModuleRegistry[tool]);
}
}
});
needUpdate = true;
}
GraphEditor.enableRTCVideoSubscribe(selectedGraphCopy, enableRTCVideoSubscribe);
// Perform the update if changes are detected
if (needUpdate) {
await updateGraph(selectedGraphCopy.id, selectedGraphCopy);
toast.success("Modules updated", {
description: `Graph: ${selectedGraphCopy.id}`,
});
}
} catch (e: any) {
toast.error(`Failed to update modules: ${e}`);
}
}}
/>
</div>
</SheetContent>
</Sheet>
);
}
const GraphModuleCfgForm = ({
initialData,
metadata,
onUpdate,
}: {
initialData: Record<string, string | null | undefined>;
metadata: Record<string, { type: string; options: { value: string, label: string }[] }>;
onUpdate: (data: Record<string, string | null>, tools: string[]) => void;
}) => {
const formSchema = z.record(z.string(), z.string().nullable());
const form = useForm<z.infer<typeof formSchema>>({
resolver: zodResolver(formSchema),
defaultValues: initialData,
});
const { selectedGraph, installedAndRegisteredToolModules } = useGraphs();
const { watch } = form;
// Watch for changes in "llm" and "v2v" fields
const llmValue = watch("llm");
const v2vValue = watch("v2v");
const toolModules = React.useMemo(() => {
// Step 1: Get installed and registered tool modules
const allToolModules = installedAndRegisteredToolModules || [];
// Step 2: Determine the active module based on form values
const activeModule = llmValue || v2vValue;
// Step 3: Get compatible tools for the active module
if (activeModule) {
const compatibleToolNames = compatibleTools[activeModule] || [];
return allToolModules.filter((module) => compatibleToolNames.includes(module.name));
}
// If no LLM or V2V module is selected, return all tool modules
return [];
}, [installedAndRegisteredToolModules, selectedGraph, llmValue, v2vValue]);
const onSubmit = (data: z.infer<typeof formSchema>) => {
onUpdate(data, selectedTools);
};
const [selectedTools, setSelectedTools] = React.useState<string[]>([]);
// Synchronize selectedTools with selectedGraph and toolModules
React.useEffect(() => {
const toolNames = toolModules.map((module) => module.name);
const graphToolAddons =
selectedGraph?.nodes
.filter((node) => toolNames.includes(node.addon))
.map((node) => node.addon) || [];
setSelectedTools(graphToolAddons);
}, [toolModules, selectedGraph]);
// Desired field order
const fieldOrder: ModuleRegistry.NonToolModuleType[] = [
ModuleRegistry.ModuleType.STT,
ModuleRegistry.ModuleType.LLM,
ModuleRegistry.ModuleType.V2V,
ModuleRegistry.ModuleType.TTS,
];
return (
<Form {...form}>
<form onSubmit={form.handleSubmit(onSubmit)} className="space-y-6">
{fieldOrder.map(
(key) =>
metadata[key] && ( // Check if the field exists in metadata
<div key={key} className="space-y-2">
<FormField
control={form.control}
name={key}
render={({ field }) => (
<FormItem>
<FormLabel>
<div className="flex items-center justify-between ">
<div className="py-3">{ModuleTypeLabels[key]}</div>
{isLLM(key) && (
<DropdownMenu>
<DropdownMenuTrigger className={cn(
buttonVariants({ variant: "outline", size: "icon" }),
"bg-transparent",
)}><WrenchIcon />
</DropdownMenuTrigger>
<DropdownMenuContent>
<DropdownMenuSub>
<DropdownMenuSubTrigger icon={<ChevronRightIcon size={15} />} className="flex justify-between">
Add Tools
</DropdownMenuSubTrigger>
<DropdownMenuPortal>
<DropdownMenuSubContent
className="DropdownMenuSubContent"
sideOffset={2}
alignOffset={-5}
>
{toolModules.length > 0 ? toolModules.map((module) => (
<DropdownMenuItem
key={module.name}
disabled={selectedTools.includes(module.name)} // Disable if the tool is already selected
onClick={() => {
if (!selectedTools.includes(module.name)) {
setSelectedTools((prev) => [
...prev,
module.name,
]);
}
}}>
{module.name}
</DropdownMenuItem>
)) : (
<DropdownMenuItem disabled>No compatible tools</DropdownMenuItem>
)}
</DropdownMenuSubContent>
</DropdownMenuPortal>
</DropdownMenuSub>
</DropdownMenuContent>
</DropdownMenu>
)}
</div>
</FormLabel>
<FormControl>
<Select
value={field.value ?? ""}
onValueChange={field.onChange}
>
<SelectTrigger>
<SelectValue placeholder={`Select a ${key.toUpperCase()} option`} />
</SelectTrigger>
<SelectContent>
{metadata[key].options.map((option) => (
<SelectItem key={option.value} value={option.value}>
{option.label}
</SelectItem>
))}
</SelectContent>
</Select>
</FormControl>
</FormItem>
)}
/>
{isLLM(key) && selectedTools.length > 0 && (
<div className="mt-2">
{selectedTools.map((tool) => (
<div
key={tool}
className="flex items-center justify-between py-2 px-3 rounded-md"
>
<span className="text-sm">{tool}</span>
<div
className={cn(
buttonVariants({ variant: "outline", size: "icon" }),
"bg-transparent",
"ml-2",
"cursor-pointer"
)}
onClick={() => {
setSelectedTools((prev) => prev.filter((t) => t !== tool))
}} // Delete action
>
<Trash2Icon />
</div>
</div>
))}
</div>
)}
</div>
)
)}
<Button type="submit" disabled={form.formState.isSubmitting}>
{form.formState.isSubmitting ? (
<>
<LoaderCircleIcon className="h-4 w-4 animate-spin" />
<span>Saving...</span>
</>
) : (
"Save changes"
)}
</Button>
</form>
</Form>
);
};
|