feat(web): add orchestrator command system in chat interface (#521)
All checks were successful
ci/woodpecker/push/web Pipeline was successful
All checks were successful
ci/woodpecker/push/web Pipeline was successful
Co-authored-by: Jason Woltje <jason@diversecanvas.com> Co-committed-by: Jason Woltje <jason@diversecanvas.com>
This commit was merged in pull request #521.
This commit is contained in:
@@ -2,6 +2,7 @@
|
||||
|
||||
import type { KeyboardEvent, RefObject } from "react";
|
||||
import { useCallback, useState, useEffect, useRef } from "react";
|
||||
import { ORCHESTRATOR_COMMANDS } from "@/hooks/useOrchestratorCommands";
|
||||
|
||||
export const AVAILABLE_MODELS = [
|
||||
{ id: "llama3.2", label: "Llama 3.2" },
|
||||
@@ -94,6 +95,11 @@ export function ChatInput({
|
||||
const [isModelDropdownOpen, setIsModelDropdownOpen] = useState(false);
|
||||
const [isParamsOpen, setIsParamsOpen] = useState(false);
|
||||
|
||||
// Command autocomplete state
|
||||
const [commandSuggestions, setCommandSuggestions] = useState<typeof ORCHESTRATOR_COMMANDS>([]);
|
||||
const [highlightedCommandIndex, setHighlightedCommandIndex] = useState(0);
|
||||
const commandDropdownRef = useRef<HTMLDivElement>(null);
|
||||
|
||||
const modelDropdownRef = useRef<HTMLDivElement>(null);
|
||||
const paramsDropdownRef = useRef<HTMLDivElement>(null);
|
||||
|
||||
@@ -147,6 +153,35 @@ export function ChatInput({
|
||||
});
|
||||
}, []);
|
||||
|
||||
// Update command autocomplete suggestions when message changes
|
||||
useEffect(() => {
|
||||
const trimmed = message.trimStart();
|
||||
if (!trimmed.startsWith("/")) {
|
||||
setCommandSuggestions([]);
|
||||
setHighlightedCommandIndex(0);
|
||||
return;
|
||||
}
|
||||
|
||||
// If the input contains a space, a command has been completed — no suggestions
|
||||
if (trimmed.includes(" ")) {
|
||||
setCommandSuggestions([]);
|
||||
setHighlightedCommandIndex(0);
|
||||
return;
|
||||
}
|
||||
|
||||
const typedCommand = trimmed.toLowerCase();
|
||||
|
||||
// Build flat list including aliases
|
||||
const matches = ORCHESTRATOR_COMMANDS.filter((cmd) => {
|
||||
if (cmd.name.startsWith(typedCommand)) return true;
|
||||
if (cmd.aliases?.some((a) => a.startsWith(typedCommand))) return true;
|
||||
return false;
|
||||
});
|
||||
|
||||
setCommandSuggestions(matches);
|
||||
setHighlightedCommandIndex(0);
|
||||
}, [message]);
|
||||
|
||||
// Close dropdowns on outside click
|
||||
useEffect(() => {
|
||||
const handleClickOutside = (e: MouseEvent): void => {
|
||||
@@ -156,6 +191,9 @@ export function ChatInput({
|
||||
if (paramsDropdownRef.current && !paramsDropdownRef.current.contains(e.target as Node)) {
|
||||
setIsParamsOpen(false);
|
||||
}
|
||||
if (commandDropdownRef.current && !commandDropdownRef.current.contains(e.target as Node)) {
|
||||
setCommandSuggestions([]);
|
||||
}
|
||||
};
|
||||
document.addEventListener("mousedown", handleClickOutside);
|
||||
return (): void => {
|
||||
@@ -174,8 +212,48 @@ export function ChatInput({
|
||||
onStopStreaming?.();
|
||||
}, [onStopStreaming]);
|
||||
|
||||
const acceptCommand = useCallback((cmdName: string): void => {
|
||||
setMessage(cmdName + " ");
|
||||
setCommandSuggestions([]);
|
||||
setHighlightedCommandIndex(0);
|
||||
}, []);
|
||||
|
||||
const handleKeyDown = useCallback(
|
||||
(e: KeyboardEvent<HTMLTextAreaElement>) => {
|
||||
// Command autocomplete navigation
|
||||
if (commandSuggestions.length > 0) {
|
||||
if (e.key === "ArrowDown") {
|
||||
e.preventDefault();
|
||||
setHighlightedCommandIndex((prev) =>
|
||||
prev < commandSuggestions.length - 1 ? prev + 1 : 0
|
||||
);
|
||||
return;
|
||||
}
|
||||
if (e.key === "ArrowUp") {
|
||||
e.preventDefault();
|
||||
setHighlightedCommandIndex((prev) =>
|
||||
prev > 0 ? prev - 1 : commandSuggestions.length - 1
|
||||
);
|
||||
return;
|
||||
}
|
||||
if (
|
||||
e.key === "Tab" ||
|
||||
(e.key === "Enter" && !e.shiftKey && commandSuggestions.length > 0)
|
||||
) {
|
||||
e.preventDefault();
|
||||
const selected = commandSuggestions[highlightedCommandIndex];
|
||||
if (selected) {
|
||||
acceptCommand(selected.name);
|
||||
}
|
||||
return;
|
||||
}
|
||||
if (e.key === "Escape") {
|
||||
e.preventDefault();
|
||||
setCommandSuggestions([]);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
if (e.key === "Enter" && !e.shiftKey) {
|
||||
e.preventDefault();
|
||||
handleSubmit();
|
||||
@@ -185,7 +263,7 @@ export function ChatInput({
|
||||
handleSubmit();
|
||||
}
|
||||
},
|
||||
[handleSubmit]
|
||||
[handleSubmit, commandSuggestions, highlightedCommandIndex, acceptCommand]
|
||||
);
|
||||
|
||||
const handleModelSelect = useCallback(
|
||||
@@ -462,6 +540,55 @@ export function ChatInput({
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{/* Command Autocomplete Dropdown */}
|
||||
{commandSuggestions.length > 0 && (
|
||||
<div
|
||||
ref={commandDropdownRef}
|
||||
className="rounded-lg border shadow-lg"
|
||||
style={{
|
||||
backgroundColor: "rgb(var(--surface-0))",
|
||||
borderColor: "rgb(var(--border-default))",
|
||||
}}
|
||||
role="listbox"
|
||||
aria-label="Command suggestions"
|
||||
data-testid="command-autocomplete"
|
||||
>
|
||||
{commandSuggestions.map((cmd, idx) => (
|
||||
<button
|
||||
key={cmd.name}
|
||||
role="option"
|
||||
aria-selected={idx === highlightedCommandIndex}
|
||||
onClick={() => {
|
||||
acceptCommand(cmd.name);
|
||||
}}
|
||||
className="w-full flex items-center gap-3 px-3 py-2 text-left text-sm transition-colors first:rounded-t-lg last:rounded-b-lg"
|
||||
style={{
|
||||
backgroundColor:
|
||||
idx === highlightedCommandIndex
|
||||
? "rgb(var(--accent-primary) / 0.1)"
|
||||
: "transparent",
|
||||
color: "rgb(var(--text-primary))",
|
||||
}}
|
||||
>
|
||||
<span
|
||||
className="font-mono text-xs font-semibold"
|
||||
style={{ color: "rgb(var(--accent-primary))" }}
|
||||
>
|
||||
{cmd.name}
|
||||
</span>
|
||||
{cmd.aliases && cmd.aliases.length > 0 && (
|
||||
<span className="text-xs" style={{ color: "rgb(var(--text-muted))" }}>
|
||||
({cmd.aliases.join(", ")})
|
||||
</span>
|
||||
)}
|
||||
<span className="text-xs ml-auto" style={{ color: "rgb(var(--text-secondary))" }}>
|
||||
{cmd.description}
|
||||
</span>
|
||||
</button>
|
||||
))}
|
||||
</div>
|
||||
)}
|
||||
|
||||
{/* Input Container */}
|
||||
<div
|
||||
className="relative rounded-lg border transition-all duration-150"
|
||||
|
||||
Reference in New Issue
Block a user