Skip to content

armgit / macro

Public Imported from GitHub
0

Macro is a unified workspace for teams: email, chat, docs, tasks, agents, calls, and CRM — @-linked together with shared AI memory.

feat(fe): Chat with agent button (#2794)

Evan Hutnik0dc08e6ccd20
diff --git a/js/app/packages/app/component/ChatWithAgentButton.tsx b/js/app/packages/app/component/ChatWithAgentButton.tsx
new file mode 100644
index 0000000000..2ba403f98c
--- /dev/null
+++ b/js/app/packages/app/component/ChatWithAgentButton.tsx
@@ -0,0 +1,131 @@
+import { globalSplitManager } from '@app/signal/splitLayout';
+import { toast } from '@core/component/Toast/Toast';
+import { Tooltip } from '@core/component/Tooltip';
+import type { Attachment } from '@core/component/AI/types';
+import { asFileType } from '@core/component/AI/util';
+import { storeChatStateImmediate } from '@core/component/AI/util/storage';
+import { createChat } from '@core/util/create';
+import { AnimatedStarIcon } from '@macro-icons/wide/animating/star';
+import { ChannelType } from '@service-cognition/generated/schemas/channelType';
+import { createSignal } from 'solid-js';
+import { match } from 'ts-pattern';
+
+export { AnimatedStarIcon as ChatWithAgentIcon };
+
+const CHANNEL_TYPE_VALUES = new Set<string>(Object.values(ChannelType));
+
+export function toChatChannelType(
+ t: string | undefined | null
+): ChannelType | undefined {
+ if (t && CHANNEL_TYPE_VALUES.has(t)) return t as ChannelType;
+ return undefined;
+}
+
+export type ChatWithAgentEntity =
+ | { type: 'email'; id: string; name: string }
+ | {
+ type: 'document';
+ id: string;
+ name: string;
+ fileType: string | null | undefined;
+ }
+ | { type: 'project'; id: string; name: string }
+ | { type: 'channel'; id: string; name: string; channelType: ChannelType };
+
+function buildAttachment(entity: ChatWithAgentEntity): Attachment | undefined {
+ return match(entity)
+ .with({ type: 'email' }, (e) => ({
+ id: `${e.id}-email-attachment`,
+ attachmentId: e.id,
+ attachmentType: 'email' as const,
+ metadata: {
+ type: 'email' as const,
+ email_subject: e.name || 'No Subject',
+ },
+ }))
+ .with({ type: 'document' }, (e) => {
+ const fileType = asFileType(e.fileType);
+ if (!fileType) return undefined;
+ return {
+ id: `${e.id}-document-attachment`,
+ attachmentId: e.id,
+ attachmentType: 'document' as const,
+ metadata: {
+ type: 'document' as const,
+ document_type: fileType,
+ document_name: e.name,
+ },
+ };
+ })
+ .with({ type: 'project' }, (e) => ({
+ id: `${e.id}-project-attachment`,
+ attachmentId: e.id,
+ attachmentType: 'project' as const,
+ metadata: { type: 'project' as const, project_name: e.name },
+ }))
+ .with({ type: 'channel' }, (e) => ({
+ id: `${e.id}-channel-attachment`,
+ attachmentId: e.id,
+ attachmentType: 'channel' as const,
+ metadata: {
+ type: 'channel' as const,
+ channel_type: e.channelType,
+ channel_name: e.name,
+ },
+ }))
+ .exhaustive();
+}
+
+async function createAndOpenChat(seed: {
+ input?: string;
+ attachments?: Attachment[];
+}) {
+ const result = await createChat();
+ if ('error' in result || !result.chatId) {
+ console.warn('createAndOpenChat: createChat failed', result);
+ toast.failure('Unable to start chat');
+ return;
+ }
+
+ storeChatStateImmediate(result.chatId, seed);
+ globalSplitManager()?.openWithSplit(
+ { type: 'chat', id: result.chatId },
+ { activate: true, preferNewSplit: true }
+ );
+}
+
+export async function openChatWithAgent(entity: ChatWithAgentEntity) {
+ const attachment = buildAttachment(entity);
+ if (!attachment) {
+ console.warn('openChatWithAgent: unable to build attachment', entity);
+ toast.failure("Can't attach this item to a chat");
+ return;
+ }
+ await createAndOpenChat({ attachments: [attachment] });
+}
+
+export async function openChatWithInput(initialInput: string) {
+ await createAndOpenChat({ input: initialInput });
+}
+
+export function ChatWithAgentButton(props: { entity: ChatWithAgentEntity }) {
+ const [hovering, setHovering] = createSignal(false);
+
+ return (
+ <Tooltip tooltip="Chat with Agent">
+ <div class="border-1 border-edge-muted flex ml-1 items-stretch rounded-xs">
+ <button
+ class="h-7 px-2 flex items-center gap-1 text-xs hover:bg-hover hover-transition-bg"
+ onMouseEnter={() => setHovering(true)}
+ onMouseLeave={() => setHovering(false)}
+ onClick={() => openChatWithAgent(props.entity)}
+ >
+ <div class="size-4">
+ <AnimatedStarIcon triggerAnimation={hovering()} />
+ </div>
+ <span class="text-ink">Chat</span>
+ </button>
+ </div>
+ </Tooltip>
+ );
+}
diff --git a/js/app/packages/block-channel/component/NewChannelBlockAdapter.tsx b/js/app/packages/block-channel/component/NewChannelBlockAdapter.tsx
index 2bb0381fb0..b098db5a9a 100644
--- a/js/app/packages/block-channel/component/NewChannelBlockAdapter.tsx
+++ b/js/app/packages/block-channel/component/NewChannelBlockAdapter.tsx
@@ -41,6 +41,10 @@ import {
useCall,
} from '@channel/Call';
import { ENABLE_CALLS } from '@core/constant/featureFlags';
+import {
+ ChatWithAgentButton,
+ toChatChannelType,
+} from '@app/component/ChatWithAgentButton';
import { SplitHeaderRight } from '@app/component/split-layout/components/SplitHeader';
import { useMaybePreviewPanel } from '@app/component/PreviewPanel';
import { globalSplitManager } from '@app/signal/splitLayout';
@@ -71,6 +75,7 @@ function NewTop(props: { channelId: string }) {
const { activeTab, setActiveTab } = useChannelTab();
const channelName = useChannelName(props.channelId);
const channelType = useChannelType(props.channelId);
+ const chatChannelType = () => toChatChannelType(channelType());
const participantsQuery = useChannelParticipantsQuery(() => props.channelId);
const call = useCall(() => props.channelId);
const participants = () =>
@@ -103,9 +108,25 @@ function NewTop(props: { channelId: string }) {
activeTab={activeTab()}
onTabChange={setActiveTab}
/>
- <Show when={ENABLE_CALLS()}>
+ <Show when={chatChannelType() || ENABLE_CALLS()}>
<SplitHeaderRight>
- <ChannelCallButton channelId={props.channelId} />
+ <div class="flex items-center gap-1.5">
+ <Show when={chatChannelType()}>
+ {(type) => (
+ <ChatWithAgentButton
+ entity={{
+ type: 'channel',
+ id: props.channelId,
+ name: channelName() ?? 'Channel',
+ channelType: type(),
+ }}
+ />
+ )}
+ </Show>
+ <Show when={ENABLE_CALLS()}>
+ <ChannelCallButton channelId={props.channelId} />
+ </Show>
+ </div>
</SplitHeaderRight>
</Show>
<ChannelTopBarLiveIndicators />
diff --git a/js/app/packages/block-email/component/TopBar.tsx b/js/app/packages/block-email/component/TopBar.tsx
index ce89789dcd..6bb615bd95 100644
--- a/js/app/packages/block-email/component/TopBar.tsx
+++ b/js/app/packages/block-email/component/TopBar.tsx
@@ -17,6 +17,11 @@ import {
useShareDialogContext,
} from '@core/component/TopBar/ShareButton';
import ArrowCounterClockwise from '@phosphor-icons/core/regular/arrow-counter-clockwise.svg?component-solid';
+import {
+ ChatWithAgentButton,
+ ChatWithAgentIcon,
+ openChatWithAgent,
+} from '@app/component/ChatWithAgentButton';
import { toast } from '@core/component/Toast/Toast';
import { ENABLE_EMAIL_SHARING } from '@core/constant/featureFlags';
import { TOKENS } from '@core/hotkey/tokens';
@@ -170,7 +175,7 @@ export function TopBar(props: {
onMouseLeave={() => setHovering(false)}
onClick={openTaskCompose}
>
- <div class="size-4 text-task">
+ <div class="size-4">
<AnimatedTaskIcon triggerAnimation={hovering()} />
</div>
<span class="text-ink">Task</span>
@@ -179,6 +184,24 @@ export function TopBar(props: {
);
},
},
+ {
+ label: 'Chat',
+ icon: ChatWithAgentIcon,
+ action: () => {
+ const threadId = emailCtx.thread()?.db_id;
+ if (!threadId) return;
+ openChatWithAgent({ type: 'email', id: threadId, name: props.title });
+ },
+ condition: () => !!emailCtx.thread()?.db_id,
+ buttonComponent: () => {
+ const id = emailCtx.thread()?.db_id;
+ return id ? (
+ <ChatWithAgentButton
+ entity={{ type: 'email', id, name: props.title }}
+ />
+ ) : null;
+ },
+ },
{
label: 'Share',
icon: IconShared,
diff --git a/js/app/packages/block-md/component/TopBar.tsx b/js/app/packages/block-md/component/TopBar.tsx
index d9b5cfad8e..562653362c 100644
--- a/js/app/packages/block-md/component/TopBar.tsx
+++ b/js/app/packages/block-md/component/TopBar.tsx
@@ -1,4 +1,9 @@
import { useGlobalNotificationSource } from '@app/component/GlobalAppState';
+import {
+ ChatWithAgentButton,
+ ChatWithAgentIcon,
+ openChatWithAgent,
+} from '@app/component/ChatWithAgentButton';
import {
type BlockTool,
ToolButton,
@@ -230,11 +235,32 @@ export function TopBar() {
condition: () => isTask && !isMobile(),
buttonComponent: () => <DispatchAgentButton />,
},
+ {
+ label: 'Chat',
+ icon: ChatWithAgentIcon,
+ action: () =>
+ openChatWithAgent({
+ type: 'document',
+ id: blockId,
+ name: name(),
+ fileType: 'md',
+ }),
+ divideAbove: true,
+ buttonComponent: () => (
+ <ChatWithAgentButton
+ entity={{
+ type: 'document',
+ id: blockId,
+ name: name(),
+ fileType: 'md',
+ }}
+ />
+ ),
+ },
{
label: 'Share',
icon: IconShared,
action: () => shareCtx.open(),
- divideAbove: true,
buttonComponent: () => <ShareTrigger />,
},
{
diff --git a/js/app/packages/block-pdf/component/TopBar.tsx b/js/app/packages/block-pdf/component/TopBar.tsx
index 204eff1e96..5c48a244de 100644
--- a/js/app/packages/block-pdf/component/TopBar.tsx
+++ b/js/app/packages/block-pdf/component/TopBar.tsx
@@ -1,4 +1,9 @@
import { useAnalytics } from '@app/component/analytics-context';
+import {
+ ChatWithAgentButton,
+ ChatWithAgentIcon,
+ openChatWithAgent,
+} from '@app/component/ChatWithAgentButton';
import { useDrawerControl } from '@app/component/split-layout/components/SplitDrawerContext';
import type { BlockTool } from '@app/component/ResponsiveBlockToolbar';
import type { FileOperation } from '@app/component/split-layout/components/SplitFileMenu';
@@ -224,11 +229,32 @@ export function TopBar() {
/>
),
},
+ {
+ label: 'Chat',
+ icon: ChatWithAgentIcon,
+ action: () =>
+ openChatWithAgent({
+ type: 'document',
+ id: documentId,
+ name: fileName(),
+ fileType,
+ }),
+ divideAbove: true,
+ buttonComponent: () => (
+ <ChatWithAgentButton
+ entity={{
+ type: 'document',
+ id: documentId,
+ name: fileName(),
+ fileType,
+ }}
+ />
+ ),
+ },
{
label: 'Share',
icon: IconShared,
action: () => shareCtx.open(),
- divideAbove: true,
buttonComponent: () => <ShareTrigger copyLink={copyLink} />,
},
];
diff --git a/js/app/packages/block-project/component/TopBar.tsx b/js/app/packages/block-project/component/TopBar.tsx
index 940faaa469..8531f571e6 100644
--- a/js/app/packages/block-project/component/TopBar.tsx
+++ b/js/app/packages/block-project/component/TopBar.tsx
@@ -1,4 +1,9 @@
import { useDrawerControl } from '@app/component/split-layout/components/SplitDrawerContext';
+import {
+ ChatWithAgentButton,
+ ChatWithAgentIcon,
+ openChatWithAgent,
+} from '@app/component/ChatWithAgentButton';
import {
type BlockTool,
ResponsivePermissionsBadge,
@@ -110,11 +115,20 @@ export function TopBar() {
condition: () => !isSpecialProject,
buttonComponent: () => <ProjectPropertiesButton buttonSize="sm" />,
},
+ {
+ label: 'Chat',
+ icon: ChatWithAgentIcon,
+ action: () => openChatWithAgent({ type: 'project', id, name: name() }),
+ condition: () => !isSpecialProject,
+ divideAbove: true,
+ buttonComponent: () => (
+ <ChatWithAgentButton entity={{ type: 'project', id, name: name() }} />
+ ),
+ },
{
label: 'Share',
icon: IconShared,
action: () => shareCtx.open(),
- divideAbove: true,
condition: () => ENABLE_PROJECT_SHARING && !isSpecialProject,
buttonComponent: () => <ShareTrigger copyLink={handleCopyLink} />,
},
diff --git a/js/app/packages/channel/Channel/Channel.tsx b/js/app/packages/channel/Channel/Channel.tsx
index 51b19347b2..a251bbfb32 100644
--- a/js/app/packages/channel/Channel/Channel.tsx
+++ b/js/app/packages/channel/Channel/Channel.tsx
@@ -46,6 +46,7 @@ import { hasSendableInputContent } from '../Input/utils/sendable-content';
import { ChannelInputContainer } from '../Input/ChannelInputContainer';
import { createChannelMessageActions } from './create-channel-message-actions';
import { useSplitLayout } from '@app/component/split-layout/layout';
+import { openChatWithInput } from '@app/component/ChatWithAgentButton';
import { useChannelName, useChannelActivity } from '@core/context/channels';
import { buildMentionMarkdownString, markdownToPlainText } from '@lexical-core';
import { createActivityTracker } from '@channel/activity-tracker';
@@ -213,6 +214,21 @@ export function Channel(props: ChannelProps) {
const channelName = useChannelName(props.channelId);
const { popoverSplit } = useSplitLayout();
+ const buildChannelMessageMention = (message: {
+ id: string;
+ thread_id?: string | null;
+ }) =>
+ buildMentionMarkdownString({
+ type: 'document',
+ documentId: props.channelId,
+ documentName: channelName() ?? '',
+ blockName: 'channel',
+ blockParams: {
+ channel_message_id: message.id,
+ ...(message.thread_id && { channel_thread_id: message.thread_id }),
+ },
+ });
+
const getMessageActions = createChannelMessageActions({
channelId: () => props.channelId,
userId,
@@ -236,21 +252,13 @@ export function Channel(props: ChannelProps) {
id: 'task-compose',
params: {
initialTitle: title,
- initialContent: buildMentionMarkdownString({
- type: 'document',
- documentId: props.channelId,
- documentName: channelName() ?? '',
- blockName: 'channel',
- blockParams: {
- channel_message_id: ctx.message.id,
- ...(ctx.message.thread_id && {
- channel_thread_id: ctx.message.thread_id,
- }),
- },
- }),
+ initialContent: buildChannelMessageMention(ctx.message),
},
});
},
+ onChat: (ctx) => {
+ openChatWithInput(`${buildChannelMessageMention(ctx.message)}\n\n`);
+ },
});
const selection = createMessageSelection({
diff --git a/js/app/packages/channel/Channel/create-channel-message-actions.ts b/js/app/packages/channel/Channel/create-channel-message-actions.ts
index 527c95eecd..1bd4682646 100644
--- a/js/app/packages/channel/Channel/create-channel-message-actions.ts
+++ b/js/app/packages/channel/Channel/create-channel-message-actions.ts
@@ -57,6 +57,7 @@ export type CreateChannelMessageActionsOptions = {
onReply?: MessageActionHandler;
onEdit?: MessageActionHandler;
onCreateTask?: MessageActionHandler;
+ onChat?: MessageActionHandler;
effects?: Partial<ChannelMessageActionEffects>;
};
@@ -184,6 +185,7 @@ export function createChannelMessageActions(
}
: undefined,
onCreateTask: options.onCreateTask,
+ onChat: !isDeleted ? options.onChat : undefined,
};
};
}
diff --git a/js/app/packages/channel/Message/ActionMenu.tsx b/js/app/packages/channel/Message/ActionMenu.tsx
index c1febc4851..03954f60f2 100644
--- a/js/app/packages/channel/Message/ActionMenu.tsx
+++ b/js/app/packages/channel/Message/ActionMenu.tsx
@@ -3,6 +3,7 @@ import LinkIcon from '@macro-icons/square/link.svg';
import EditIcon from '@macro-icons/square/edit.svg';
import AddEmojiIcon from '@macro-icons/square/add-emoji.svg';
import TrashIcon from '@macro-icons/square/trash.svg';
+import StarIcon from '@macro-icons/wide/star.svg';
import TaskIcon from '@macro-icons/wide/task.svg';
import { cn } from '@ui/utils/classname';
import { createSignal, For, Show, type Component, type JSX } from 'solid-js';
@@ -14,7 +15,13 @@ import type { MessageActionEvent, MessageActionHandler } from './types';
const QUICK_REACTION_EMOJIS = ['❤️', '👍', '😂'] as const;
-type ActionId = 'reply' | 'copy-link' | 'create-task' | 'edit' | 'delete';
+type ActionId =
+ | 'reply'
+ | 'copy-link'
+ | 'create-task'
+ | 'chat'
+ | 'edit'
+ | 'delete';
type ActionItem = {
id: ActionId;
@@ -67,13 +74,21 @@ export function ActionMenu(props: ActionMenuProps) {
const hasReactAction = () => actions?.onReact !== undefined;
- const actionItems: ActionItem[] = [
+ const composeActions: ActionItem[] = [
{
id: 'create-task',
label: 'Task',
icon: TaskIcon,
onClick: actions?.onCreateTask,
},
+ {
+ id: 'chat',
+ label: 'Chat with Agent',
+ icon: StarIcon,
+ onClick: actions?.onChat,
+ },
+ ];
+ const otherActions: ActionItem[] = [
{
id: 'reply',
label: 'Reply',
@@ -105,7 +120,9 @@ export function ActionMenu(props: ActionMenuProps) {
},
];
- const visibleActions = actionItems.filter((item) => item.onClick);
+ const visibleCompose = composeActions.filter((item) => item.onClick);
+ const visibleOther = otherActions.filter((item) => item.onClick);
+ const visibleActions = [...visibleCompose, ...visibleOther];
return (
<Show when={hasReactAction() || visibleActions.length > 0}>
@@ -154,7 +171,20 @@ export function ActionMenu(props: ActionMenuProps) {
</Show>
</Show>
- <For each={visibleActions}>
+ <For each={visibleCompose}>
+ {(action) => (
+ <ActionButton
+ action={action}
+ onClick={(event) => {
+ void action.onClick?.({ message: message(), event });
+ }}
+ />
+ )}
+ </For>
+ <Show when={visibleCompose.length > 0 && visibleOther.length > 0}>
+ <div class="w-px self-stretch bg-edge-muted mx-1" />
+ </Show>
+ <For each={visibleOther}>
{(action) => (
<ActionButton
action={action}
diff --git a/js/app/packages/channel/Message/types.ts b/js/app/packages/channel/Message/types.ts
index 6e50df5897..098cf5bde3 100644
--- a/js/app/packages/channel/Message/types.ts
+++ b/js/app/packages/channel/Message/types.ts
@@ -34,4 +34,5 @@ export type MessageActions = {
onEdit?: MessageActionHandler;
onDelete?: MessageActionHandler;
onCreateTask?: MessageActionHandler;
+ onChat?: MessageActionHandler;
};
diff --git a/js/app/packages/core/component/AI/util/storage.ts b/js/app/packages/core/component/AI/util/storage.ts
index f513f967c8..e37b6e7101 100644
--- a/js/app/packages/core/component/AI/util/storage.ts
+++ b/js/app/packages/core/component/AI/util/storage.ts
@@ -61,6 +61,13 @@ function useStoreChatState() {
export const storeChatState = useStoreChatState();
+export function storeChatStateImmediate(
+ id: string,
+ state: Partial<StoredStuff>
+) {
+ setPersistentChatState(id, { ...state, used_at: Date.now() });
+}
+
export function getChatInputStoredState(id: string): Partial<StoredStuff> {
const storedStuff = untrack(() => persistentChatState[id]);
if (!storedStuff) return {};