Skip to content

The open alternative to Salesforce, designed for AI.

Make Other an explicit option in the AI chat question card (#24193)

Raphaël Bosiecc8de2d7ba1
diff --git a/packages/twenty-front/src/modules/ai/components/AiChatQuestionCard.tsx b/packages/twenty-front/src/modules/ai/components/AiChatQuestionCard.tsx
index 6d1ab3e1d22..890ac4b71b1 100644
--- a/packages/twenty-front/src/modules/ai/components/AiChatQuestionCard.tsx
+++ b/packages/twenty-front/src/modules/ai/components/AiChatQuestionCard.tsx
@@ -29,6 +29,7 @@ import { ThemeContext, themeCssVariables } from 'twenty-ui/theme-constants';
import { AgentChatFileUploadButton } from '@/ai/components/internal/AgentChatFileUploadButton';
import { AiChatContextUsageButton } from '@/ai/components/internal/AiChatContextUsageButton';
+import { AiChatQuestionOtherOption } from '@/ai/components/internal/AiChatQuestionOtherOption';
import { TextWithChatReferences } from '@/ai/components/TextWithChatReferences';
import { useAgentChatModelId } from '@/ai/hooks/useAgentChatModelId';
import { useAiModelOptions } from '@/ai/hooks/useAiModelOptions';
@@ -164,29 +165,9 @@ const StyledComposerSection = styled.div`
box-sizing: border-box;
display: flex;
flex-direction: column;
- gap: ${themeCssVariables.spacing[2]};
- min-height: 80px;
padding: ${themeCssVariables.spacing[2]};
`;
-const StyledFreeTextArea = styled.textarea`
- background: transparent;
- border: none;
- color: ${themeCssVariables.font.color.primary};
- flex: 1 0 0;
- font-family: inherit;
- font-size: ${themeCssVariables.font.size.md};
- line-height: 1.4;
- min-height: 24px;
- outline: none;
- resize: none;
-
- &::placeholder {
- color: ${themeCssVariables.font.color.light};
- font-weight: ${themeCssVariables.font.weight.medium};
- }
-`;
-
const StyledActionsRow = styled.div`
align-items: center;
display: flex;
@@ -210,11 +191,13 @@ const areAllQuestionsAnswered = (
questions: AskQuestionItem[],
selectedByQuestion: Record<number, number[]>,
freeTextByQuestion: Record<number, string>,
+ otherSelectedByQuestion: Record<number, boolean>,
) =>
questions.every(
(_, index) =>
(selectedByQuestion[index]?.length ?? 0) > 0 ||
- (freeTextByQuestion[index] ?? '').trim().length > 0,
+ (otherSelectedByQuestion[index] &&
+ (freeTextByQuestion[index] ?? '').trim().length > 0),
);
type AiChatQuestionCardProps = {
@@ -235,6 +218,9 @@ export const AiChatQuestionCard = ({
const [freeTextByQuestion, setFreeTextByQuestion] = useState<
Record<number, string>
>({});
+ const [otherSelectedByQuestion, setOtherSelectedByQuestion] = useState<
+ Record<number, boolean>
+ >({});
const [isSubmitting, setIsSubmitting] = useState(false);
const { submitAnswer } = useSubmitQuestionAnswer();
@@ -257,14 +243,17 @@ export const AiChatQuestionCard = ({
const buildAnswers = (
selected: Record<number, number[]>,
+ otherSelected: Record<number, boolean>,
): AskQuestionAnswer[] =>
questions.map((_, index) => {
const trimmedFreeText = (freeTextByQuestion[index] ?? '').trim();
+ const shouldIncludeFreeText =
+ (otherSelected[index] ?? false) && trimmedFreeText.length > 0;
return {
questionIndex: index,
selectedOptionIndices: selected[index] ?? [],
- freeText: trimmedFreeText.length > 0 ? trimmedFreeText : undefined,
+ freeText: shouldIncludeFreeText ? trimmedFreeText : undefined,
};
});
@@ -296,8 +285,13 @@ export const AiChatQuestionCard = ({
...selectedByQuestion,
[currentIndex]: [optionIndex],
};
+ const nextOtherSelected = {
+ ...otherSelectedByQuestion,
+ [currentIndex]: false,
+ };
setSelectedByQuestion(nextSelected);
+ setOtherSelectedByQuestion(nextOtherSelected);
if (!isLastQuestion) {
setCurrentIndex(currentIndex + 1);
@@ -305,8 +299,56 @@ export const AiChatQuestionCard = ({
return;
}
- if (areAllQuestionsAnswered(questions, nextSelected, freeTextByQuestion)) {
- void submit(buildAnswers(nextSelected));
+ if (
+ areAllQuestionsAnswered(
+ questions,
+ nextSelected,
+ freeTextByQuestion,
+ nextOtherSelected,
+ )
+ ) {
+ void submit(buildAnswers(nextSelected, nextOtherSelected));
+ }
+ };
+
+ const selectOther = () => {
+ setOtherSelectedByQuestion((previous) => ({
+ ...previous,
+ [currentIndex]: true,
+ }));
+
+ if (currentQuestion.allowMultiSelect !== true) {
+ setSelectedByQuestion((previous) => ({
+ ...previous,
+ [currentIndex]: [],
+ }));
+ }
+ };
+
+ const handleToggleOther = () => {
+ if (
+ currentQuestion.allowMultiSelect === true &&
+ otherSelectedByQuestion[currentIndex]
+ ) {
+ setOtherSelectedByQuestion((previous) => ({
+ ...previous,
+ [currentIndex]: false,
+ }));
+
+ return;
+ }
+
+ selectOther();
+ };
+
+ const handleOtherTextChange = (value: string) => {
+ setFreeTextByQuestion((previous) => ({
+ ...previous,
+ [currentIndex]: value,
+ }));
+
+ if (value.trim().length > 0) {
+ selectOther();
}
};
@@ -316,8 +358,14 @@ export const AiChatQuestionCard = ({
questions,
selectedByQuestion,
freeTextByQuestion,
+ otherSelectedByQuestion,
),
- [questions, selectedByQuestion, freeTextByQuestion],
+ [
+ questions,
+ selectedByQuestion,
+ freeTextByQuestion,
+ otherSelectedByQuestion,
+ ],
);
const handleSend = () => {
@@ -325,7 +373,7 @@ export const AiChatQuestionCard = ({
return;
}
- void submit(buildAnswers(selectedByQuestion));
+ void submit(buildAnswers(selectedByQuestion, otherSelectedByQuestion));
};
const handleKeyDown = (event: KeyboardEvent<HTMLTextAreaElement>) => {
@@ -385,7 +433,8 @@ export const AiChatQuestionCard = ({
selectedByQuestion[currentIndex] ?? []
).includes(optionIndex);
const hasSelection =
- (selectedByQuestion[currentIndex] ?? []).length > 0;
+ (selectedByQuestion[currentIndex] ?? []).length > 0 ||
+ (otherSelectedByQuestion[currentIndex] ?? false);
const isHighlighted =
isSelected || (!hasSelection && option.isRecommended === true);
const tooltipId = `ask-question-option-${toolCallId}-${currentIndex}-${optionIndex}`;
@@ -443,24 +492,23 @@ export const AiChatQuestionCard = ({
</StyledOptionRow>
);
})}
+ <AiChatQuestionOtherOption
+ NumberIcon={
+ NUMBER_ICONS[currentQuestion.options.length] ??
+ NUMBER_ICONS[NUMBER_ICONS.length - 1]
+ }
+ isHighlighted={otherSelectedByQuestion[currentIndex] ?? false}
+ value={freeTextByQuestion[currentIndex] ?? ''}
+ onChange={handleOtherTextChange}
+ onSelect={handleToggleOther}
+ onTextareaKeyDown={handleKeyDown}
+ />
</StyledOptionsList>
</StyledQuestionSection>
<StyledDivider />
<StyledComposerSection>
- <StyledFreeTextArea
- value={freeTextByQuestion[currentIndex] ?? ''}
- placeholder={t`Type anything to do differently.`}
- onChange={(event) =>
- setFreeTextByQuestion((previous) => ({
- ...previous,
- [currentIndex]: event.target.value,
- }))
- }
- onKeyDown={handleKeyDown}
- autoFocus
- />
<StyledActionsRow>
<StyledLeftActions>
<AgentChatFileUploadButton />
diff --git a/packages/twenty-front/src/modules/ai/components/__stories__/AiChatQuestionCard.stories.tsx b/packages/twenty-front/src/modules/ai/components/__stories__/AiChatQuestionCard.stories.tsx
index 035968750d4..c0c70887bb6 100644
--- a/packages/twenty-front/src/modules/ai/components/__stories__/AiChatQuestionCard.stories.tsx
+++ b/packages/twenty-front/src/modules/ai/components/__stories__/AiChatQuestionCard.stories.tsx
@@ -1,6 +1,7 @@
import { type Meta, type StoryObj } from '@storybook/react-vite';
import { useStore } from 'jotai';
import { type ReactNode, useEffect } from 'react';
+import { expect, userEvent, within } from 'storybook/test';
import { ComponentDecorator } from 'twenty-ui/testing';
import { AiChatQuestionCard } from '@/ai/components/AiChatQuestionCard';
@@ -76,6 +77,23 @@ const longQuestion: AgentChatPendingQuestion = {
],
};
+const multiSelectQuestion: AgentChatPendingQuestion = {
+ messageId: 'assistant-1',
+ toolCallId: 'call-4',
+ questions: [
+ {
+ header: 'Channels',
+ question: 'Which channels should the outreach campaign use?',
+ allowMultiSelect: true,
+ options: [
+ { label: 'Email', isRecommended: true },
+ { label: 'LinkedIn' },
+ { label: 'Phone' },
+ ],
+ },
+ ],
+};
+
const StoreSeeder = ({ children }: { children: ReactNode }) => {
const store = useStore();
@@ -124,3 +142,30 @@ export const MultipleQuestions: Story = {
export const LongQuestion: Story = {
args: { pendingQuestion: longQuestion },
};
+
+export const MultiSelectQuestion: Story = {
+ args: { pendingQuestion: multiSelectQuestion },
+ play: async ({ canvasElement }) => {
+ const canvas = within(canvasElement);
+
+ const emailOption = await canvas.findByRole('button', { name: /Email/ });
+ await userEvent.click(emailOption);
+
+ const otherOption = await canvas.findByRole('button', { name: 'Other' });
+ expect(otherOption).toHaveAttribute('aria-pressed', 'false');
+
+ const otherTextArea = canvas.getByPlaceholderText(
+ 'Type your own answer here',
+ );
+ otherTextArea.focus();
+ await userEvent.type(otherTextArea, 'Carrier pigeon', { skipClick: true });
+ expect(otherOption).toHaveAttribute('aria-pressed', 'true');
+
+ await userEvent.click(otherOption);
+ expect(otherOption).toHaveAttribute('aria-pressed', 'false');
+ expect(otherTextArea).toHaveValue('Carrier pigeon');
+
+ await userEvent.click(otherOption);
+ expect(otherOption).toHaveAttribute('aria-pressed', 'true');
+ },
+};
diff --git a/packages/twenty-front/src/modules/ai/components/internal/AiChatQuestionOtherOption.tsx b/packages/twenty-front/src/modules/ai/components/internal/AiChatQuestionOtherOption.tsx
new file mode 100644
index 00000000000..f66597f18b2
--- /dev/null
+++ b/packages/twenty-front/src/modules/ai/components/internal/AiChatQuestionOtherOption.tsx
@@ -0,0 +1,123 @@
+import { styled } from '@linaria/react';
+import { useLingui } from '@lingui/react/macro';
+import { type KeyboardEvent, useContext, useRef } from 'react';
+import { type IconComponent } from 'twenty-ui/icon';
+import { ThemeContext, themeCssVariables } from 'twenty-ui/theme-constants';
+
+const StyledContainer = styled.div<{ isHighlighted: boolean }>`
+ background: ${({ isHighlighted }) =>
+ isHighlighted
+ ? themeCssVariables.background.transparent.light
+ : 'transparent'};
+ border-radius: ${themeCssVariables.border.radius.sm};
+ box-sizing: border-box;
+ cursor: pointer;
+ display: flex;
+ flex-direction: column;
+ padding: 0 ${themeCssVariables.spacing[1]} ${themeCssVariables.spacing[1]};
+
+ &:hover {
+ background: ${themeCssVariables.background.transparent.light};
+ }
+`;
+
+const StyledLabelRow = styled.div`
+ align-items: center;
+ display: flex;
+ gap: ${themeCssVariables.spacing[2]};
+ height: 32px;
+`;
+
+const StyledLabel = styled.span`
+ color: ${themeCssVariables.font.color.secondary};
+ font-size: ${themeCssVariables.font.size.md};
+ line-height: 1.4;
+`;
+
+const StyledTextArea = styled.textarea`
+ background: ${themeCssVariables.background.primary};
+ border: 1px solid ${themeCssVariables.border.color.medium};
+ border-radius: ${themeCssVariables.border.radius.sm};
+ box-sizing: border-box;
+ color: ${themeCssVariables.font.color.primary};
+ font-family: inherit;
+ font-size: ${themeCssVariables.font.size.md};
+ line-height: 1.4;
+ min-height: 24px;
+ outline: none;
+ padding: ${themeCssVariables.spacing[1]} ${themeCssVariables.spacing[2]};
+ resize: none;
+ width: 100%;
+
+ &:focus {
+ border-color: ${themeCssVariables.border.color.blue};
+ }
+
+ &::placeholder {
+ color: ${themeCssVariables.font.color.light};
+ font-weight: ${themeCssVariables.font.weight.medium};
+ }
+`;
+
+type AiChatQuestionOtherOptionProps = {
+ NumberIcon: IconComponent;
+ isHighlighted: boolean;
+ value: string;
+ onChange: (value: string) => void;
+ onSelect: () => void;
+ onTextareaKeyDown: (event: KeyboardEvent<HTMLTextAreaElement>) => void;
+};
+
+export const AiChatQuestionOtherOption = ({
+ NumberIcon,
+ isHighlighted,
+ value,
+ onChange,
+ onSelect,
+ onTextareaKeyDown,
+}: AiChatQuestionOtherOptionProps) => {
+ const { t } = useLingui();
+ const { theme } = useContext(ThemeContext);
+ const textareaRef = useRef<HTMLTextAreaElement>(null);
+
+ const selectAndFocus = () => {
+ onSelect();
+ textareaRef.current?.focus();
+ };
+
+ return (
+ <StyledContainer isHighlighted={isHighlighted} onClick={selectAndFocus}>
+ <StyledLabelRow
+ role="button"
+ tabIndex={0}
+ aria-pressed={isHighlighted}
+ onKeyDown={(event) => {
+ if (event.key === 'Enter' || event.key === ' ') {
+ event.preventDefault();
+ selectAndFocus();
+ }
+ }}
+ >
+ <NumberIcon
+ size={theme.icon.size.sm}
+ color={themeCssVariables.font.color.tertiary}
+ />
+ <StyledLabel>{t`Other`}</StyledLabel>
+ </StyledLabelRow>
+ <StyledTextArea
+ ref={textareaRef}
+ value={value}
+ placeholder={t`Type your own answer here`}
+ rows={1}
+ onClick={(event) => {
+ event.stopPropagation();
+ if (!isHighlighted) {
+ onSelect();
+ }
+ }}
+ onChange={(event) => onChange(event.target.value)}
+ onKeyDown={onTextareaKeyDown}
+ />
+ </StyledContainer>
+ );
+};