Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: commands can now be buttons #1969

Merged
merged 3 commits into from
Mar 7, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions backend/chainlit/types.py
Original file line number Diff line number Diff line change
@@ -257,6 +257,8 @@ class CommandDict(TypedDict):
description: str
# The lucide icon name
icon: str
# Display the command as a button in the composer
button: Optional[bool]


class FeedbackDict(TypedDict):
2 changes: 1 addition & 1 deletion cypress/e2e/command/main.py
Original file line number Diff line number Diff line change
@@ -2,7 +2,7 @@

commands = [
{"id": "Picture", "icon": "image", "description": "Use DALL-E"},
{"id": "Search", "icon": "globe", "description": "Find on the web"},
{"id": "Search", "icon": "globe", "description": "Find on the web", "button": True},
{
"id": "Canvas",
"icon": "pen-line",
67 changes: 67 additions & 0 deletions frontend/src/components/chat/MessageComposer/CommandButtons.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import { cn } from '@/lib/utils';
import { useRecoilValue } from 'recoil';

import { ICommand, commandsState } from '@chainlit/react-client';

import Icon from '@/components/Icon';
import { Button } from '@/components/ui/button';
import {
Tooltip,
TooltipContent,
TooltipProvider,
TooltipTrigger
} from '@/components/ui/tooltip';

interface Props {
disabled?: boolean;
selectedCommandId?: string;
onCommandSelect: (command?: ICommand) => void;
}

export const CommandButtons = ({
disabled = false,
selectedCommandId,
onCommandSelect
}: Props) => {
const commands = useRecoilValue(commandsState);
const commandButtons = commands.filter((c) => !!c.button);

if (!commandButtons.length) return null;

return (
<div className="flex gap-2 ml-1 flex-wrap">
<TooltipProvider>
{commandButtons.map((command) => (
<Tooltip>
<TooltipTrigger asChild>
<Button
key={command.id}
id={`command-${command.id}`}
variant="ghost"
disabled={disabled}
className={cn(
'p-2 h-9 text-[13px] font-medium rounded-full',
selectedCommandId === command.id &&
'border-transparent text-[#08f] hover:text-[#08f] bg-[#DAEEFF] hover:bg-[#BDDCF4] dark:bg-[#2A4A6D] dark:text-[#48AAFF] dark:hover:bg-[#1A416A]'
)}
onClick={() =>
selectedCommandId === command.id
? onCommandSelect(undefined)
: onCommandSelect(command)
}
>
<Icon name={command.icon} className="!h-5 !w-5" />
{command.id}
</Button>
</TooltipTrigger>
<TooltipContent>
<p>{command.description}</p>
</TooltipContent>
</Tooltip>
))}
</TooltipProvider>
</div>
);
};

export default CommandButtons;
Original file line number Diff line number Diff line change
@@ -28,7 +28,10 @@ interface Props {
onCommandSelect: (command: ICommand) => void;
}

export const CommandButton = ({ disabled = false, onCommandSelect }: Props) => {
export const CommandPopoverButton = ({
disabled = false,
onCommandSelect
}: Props) => {
const commands = useRecoilValue(commandsState);

if (!commands.length) return null;
@@ -89,4 +92,4 @@ export const CommandButton = ({ disabled = false, onCommandSelect }: Props) => {
);
};

export default CommandButton;
export default CommandPopoverButton;
2 changes: 1 addition & 1 deletion frontend/src/components/chat/MessageComposer/Input.tsx
Original file line number Diff line number Diff line change
@@ -159,7 +159,7 @@ const Input = forwardRef<InputMethods, Props>(
// Find existing command span
const existingCommandSpan = content.querySelector('.command-span');

if (selectedCommand) {
if (selectedCommand && !selectedCommand.button) {
// Create new command block
const newCommandBlock = document.createElement('div');
newCommandBlock.className =
8 changes: 7 additions & 1 deletion frontend/src/components/chat/MessageComposer/index.tsx
Original file line number Diff line number Diff line change
@@ -19,7 +19,8 @@ import { chatSettingsOpenState } from '@/state/project';
import { IAttachment, attachmentsState } from 'state/chat';

import { Attachments } from './Attachments';
import CommandButton from './CommandButton';
import CommandButtons from './CommandButtons';
import CommandButton from './CommandPopoverButton';
import Input, { InputMethods } from './Input';
import SubmitButton from './SubmitButton';
import UploadButton from './UploadButton';
@@ -181,6 +182,11 @@ export default function MessageComposer({
</Button>
)}
<VoiceButton disabled={disabled} />
<CommandButtons
disabled={disabled}
selectedCommandId={selectedCommand?.id}
onCommandSelect={setSelectedCommand}
/>
</div>
<div className="flex items-center gap-1">
<SubmitButton
1 change: 1 addition & 0 deletions frontend/tsconfig.tsbuildinfo

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions libs/react-client/src/types/command.ts
Original file line number Diff line number Diff line change
@@ -2,4 +2,5 @@ export interface ICommand {
id: string;
icon: string;
description: string;
button?: boolean;
}