Skip to content

The open alternative to Salesforce, designed for AI.

Show unsent drafts in the AI chat list (#28503)

Félix Malfait6c1ab9ad61bd
diff --git a/packages/twenty-front/src/modules/ai/components/AiChatThreadSubtitle.tsx b/packages/twenty-front/src/modules/ai/components/AiChatThreadSubtitle.tsx
index f49ae2efd23..0cf37a945b6 100644
--- a/packages/twenty-front/src/modules/ai/components/AiChatThreadSubtitle.tsx
+++ b/packages/twenty-front/src/modules/ai/components/AiChatThreadSubtitle.tsx
@@ -1,16 +1,18 @@
import { styled } from '@linaria/react';
import { useLingui } from '@lingui/react/macro';
import { isDefined, isNonEmptyString } from 'twenty-shared/utils';
-import { IconHandClick } from 'twenty-ui/icon';
+import { IconHandClick, IconPencil } from 'twenty-ui/icon';
import { VisuallyHidden } from 'twenty-ui/primitives/accessibility';
import { Avatar } from 'twenty-ui/primitives/data-display';
import { themeCssVariables, useTheme } from 'twenty-ui/theme';
import { useIsAgentChatThreadShownAsUnread } from '@/ai/hooks/useIsAgentChatThreadShownAsUnread';
+import { agentChatDraftPreviewTextFamilySelector } from '@/ai/states/selectors/agentChatDraftPreviewTextFamilySelector';
import { type AgentChatThreadRecord } from '@/ai/types/AgentChatThreadRecord';
import { getAgentChatThreadPreviewText } from '@/ai/utils/getAgentChatThreadPreviewText';
import { currentWorkspaceMemberState } from '@/auth/states/currentWorkspaceMemberState';
import { currentWorkspaceMembersState } from '@/auth/states/currentWorkspaceMembersState';
+import { useAtomFamilySelectorValue } from '@/ui/utilities/state/jotai/hooks/useAtomFamilySelectorValue';
import { useAtomStateValue } from '@/ui/utilities/state/jotai/hooks/useAtomStateValue';
import { getWorkspaceMemberNameOrEmail } from '@/workspace-member/utils/getWorkspaceMemberNameOrEmail';
import { getAbsoluteImageUrl } from '~/utils/image/getAbsoluteImageUrl';
@@ -25,10 +27,10 @@ const StyledSubtitle = styled.div`
min-width: 0;
`;
-const StyledNeedsInput = styled.div<{ $isUnread: boolean }>`
+const StyledStatus = styled.div<{ $isHighlighted: boolean }>`
align-items: center;
- color: ${({ $isUnread }) =>
- $isUnread
+ color: ${({ $isHighlighted }) =>
+ $isHighlighted
? themeCssVariables.color.blue
: themeCssVariables.font.color.tertiary};
display: flex;
@@ -70,21 +72,35 @@ export const AiChatThreadSubtitle = ({ thread }: AiChatThreadSubtitleProps) => {
: undefined;
const isAwaitingAnswer =
!isDefined(thread.deletedAt) && isDefined(thread.pendingQuestionMessageId);
- const previewText = getAgentChatThreadPreviewText({
- thread,
- workspaceMembers: currentWorkspaceMembers,
- currentWorkspaceMemberId: currentWorkspaceMember?.id,
- });
+ const draftPreviewText = useAtomFamilySelectorValue(
+ agentChatDraftPreviewTextFamilySelector,
+ thread.id,
+ );
+ const hasDraft = isNonEmptyString(draftPreviewText);
+ const previewText = hasDraft
+ ? draftPreviewText
+ : getAgentChatThreadPreviewText({
+ thread,
+ workspaceMembers: currentWorkspaceMembers,
+ currentWorkspaceMemberId: currentWorkspaceMember?.id,
+ });
return (
<StyledSubtitle>
{isAwaitingAnswer && (
- <StyledNeedsInput $isUnread={isUnread}>
+ <StyledStatus $isHighlighted={isUnread}>
<IconHandClick size={theme.icon.size.sm} />
{t`Needs input`}
- </StyledNeedsInput>
+ </StyledStatus>
+ )}
+ {isAwaitingAnswer && hasDraft && <span aria-hidden>·</span>}
+ {hasDraft && (
+ <StyledStatus $isHighlighted={false}>
+ <IconPencil size={theme.icon.size.sm} />
+ {t`Draft`}
+ </StyledStatus>
)}
- {isAwaitingAnswer && isNonEmptyString(previewText) && (
+ {(isAwaitingAnswer || hasDraft) && isNonEmptyString(previewText) && (
<span aria-hidden>·</span>
)}
<StyledPreview>{previewText}</StyledPreview>
diff --git a/packages/twenty-front/src/modules/ai/components/__tests__/AiChatThreadListItem.test.tsx b/packages/twenty-front/src/modules/ai/components/__tests__/AiChatThreadListItem.test.tsx
index 138cf0341c0..d2cf02efb7b 100644
--- a/packages/twenty-front/src/modules/ai/components/__tests__/AiChatThreadListItem.test.tsx
+++ b/packages/twenty-front/src/modules/ai/components/__tests__/AiChatThreadListItem.test.tsx
@@ -6,7 +6,9 @@ import { type ComponentProps } from 'react';
import { Provider as JotaiProvider } from 'jotai';
import { SOURCE_LOCALE } from 'twenty-shared/translations';
+import { serializePlainTextAsAdvancedTextEditorDocument } from '@/advanced-text-editor/utils/serializePlainTextAsAdvancedTextEditorDocument';
import { AiChatThreadListItem } from '@/ai/components/AiChatThreadListItem';
+import { agentChatDraftsByThreadIdState } from '@/ai/states/agentChatDraftsByThreadIdState';
import { type AgentChatThreadRecord } from '@/ai/types/AgentChatThreadRecord';
import {
jotaiStore,
@@ -110,6 +112,35 @@ describe('AiChatThreadListItem', () => {
expect(screen.queryByText('Needs input')).not.toBeInTheDocument();
});
+ it('shows an unsent draft in place of the last message', () => {
+ const thread = buildThread({});
+
+ jotaiStore.set(agentChatDraftsByThreadIdState.atom, {
+ [thread.id]: serializePlainTextAsAdvancedTextEditorDocument(
+ 'Quote the **annual** plan',
+ ),
+ });
+
+ renderListItem(thread);
+
+ expect(screen.getByText('Draft')).toBeInTheDocument();
+ expect(screen.getByText('Quote the **annual** plan')).toBeInTheDocument();
+ expect(
+ screen.queryByText('Which plan should I quote?'),
+ ).not.toBeInTheDocument();
+ });
+
+ it('shows no draft once the composer is cleared', () => {
+ const thread = buildThread({});
+
+ jotaiStore.set(agentChatDraftsByThreadIdState.atom, { [thread.id]: '' });
+
+ renderListItem(thread);
+
+ expect(screen.queryByText('Draft')).not.toBeInTheDocument();
+ expect(screen.getByText('Which plan should I quote?')).toBeInTheDocument();
+ });
+
it('has no checkbox where chats cannot be selected', () => {
renderListItem(buildThread({}));
diff --git a/packages/twenty-front/src/modules/ai/states/selectors/agentChatDraftPreviewTextFamilySelector.ts b/packages/twenty-front/src/modules/ai/states/selectors/agentChatDraftPreviewTextFamilySelector.ts
new file mode 100644
index 00000000000..1c83a4b35b7
--- /dev/null
+++ b/packages/twenty-front/src/modules/ai/states/selectors/agentChatDraftPreviewTextFamilySelector.ts
@@ -0,0 +1,28 @@
+import { isNonEmptyString } from '@sniptt/guards';
+import { tipTapDocumentToMarkdown } from 'twenty-shared/utils';
+
+import { agentChatDraftsByThreadIdState } from '@/ai/states/agentChatDraftsByThreadIdState';
+import { getAgentChatPreviewPlainText } from '@/ai/utils/getAgentChatPreviewPlainText';
+import { createAtomFamilySelector } from '@/ui/utilities/state/jotai/utils/createAtomFamilySelector';
+
+export const agentChatDraftPreviewTextFamilySelector = createAtomFamilySelector<
+ string | null,
+ string
+>({
+ key: 'agentChatDraftPreviewTextFamilySelector',
+ get:
+ (threadId: string) =>
+ ({ get }) => {
+ const serializedDraft = get(agentChatDraftsByThreadIdState)[threadId];
+
+ if (!isNonEmptyString(serializedDraft)) {
+ return null;
+ }
+
+ const text = getAgentChatPreviewPlainText(
+ tipTapDocumentToMarkdown(serializedDraft),
+ );
+
+ return isNonEmptyString(text) ? text : null;
+ },
+});
diff --git a/packages/twenty-front/src/modules/ai/utils/getAgentChatPreviewPlainText.ts b/packages/twenty-front/src/modules/ai/utils/getAgentChatPreviewPlainText.ts
new file mode 100644
index 00000000000..5b7f5bb1084
--- /dev/null
+++ b/packages/twenty-front/src/modules/ai/utils/getAgentChatPreviewPlainText.ts
@@ -0,0 +1,13 @@
+import { isString } from '@sniptt/guards';
+
+import { getChatReferenceSegments } from '@/ai/utils/getChatReferenceSegments';
+import { getPlainTextFromMarkdown } from '~/utils/string/getPlainTextFromMarkdown';
+
+// The server cuts the text short, which can leave half a reference at the end
+export const getAgentChatPreviewPlainText = (markdown: string): string =>
+ getPlainTextFromMarkdown(
+ getChatReferenceSegments(markdown)
+ .map((segment) => (isString(segment) ? segment : segment.displayName))
+ .join('')
+ .replace(/\[\[[^\]]*$/, ''),
+ );
diff --git a/packages/twenty-front/src/modules/ai/utils/getAgentChatThreadPreviewText.ts b/packages/twenty-front/src/modules/ai/utils/getAgentChatThreadPreviewText.ts
index cc61b35e1db..cffbd29cb6d 100644
--- a/packages/twenty-front/src/modules/ai/utils/getAgentChatThreadPreviewText.ts
+++ b/packages/twenty-front/src/modules/ai/utils/getAgentChatThreadPreviewText.ts
@@ -1,12 +1,10 @@
import { t } from '@lingui/core/macro';
-import { isString } from '@sniptt/guards';
import { isDefined, isNonEmptyString } from 'twenty-shared/utils';
import { type AgentChatThreadRecord } from '@/ai/types/AgentChatThreadRecord';
+import { getAgentChatPreviewPlainText } from '@/ai/utils/getAgentChatPreviewPlainText';
import { getAgentChatSenderLabel } from '@/ai/utils/getAgentChatSenderLabel';
-import { getChatReferenceSegments } from '@/ai/utils/getChatReferenceSegments';
import { type PartialWorkspaceMember } from '@/settings/roles/types/RoleWithPartialMembers';
-import { getPlainTextFromMarkdown } from '~/utils/string/getPlainTextFromMarkdown';
// Members' messages carry who wrote them; agent replies read as plain text,
// as they do in the chat itself
@@ -22,13 +20,7 @@ export const getAgentChatThreadPreviewText = ({
workspaceMembers: PartialWorkspaceMember[];
currentWorkspaceMemberId: string | undefined;
}): string | null => {
- // The server cuts the text short, which can leave half a reference at the end
- const text = getPlainTextFromMarkdown(
- getChatReferenceSegments(thread.lastMessageText ?? '')
- .map((segment) => (isString(segment) ? segment : segment.displayName))
- .join('')
- .replace(/\[\[[^\]]*$/, ''),
- );
+ const text = getAgentChatPreviewPlainText(thread.lastMessageText ?? '');
if (!isNonEmptyString(text)) {
return null;