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.

fix(web): derive agent senders for fetched discussion threads (#7069)

teo0afdaa75754a
diff --git a/apps/web/src/lib/queries/messages/document-messages.ts b/apps/web/src/lib/queries/messages/document-messages.ts
index 1e07cf0a02..9789fadeff 100644
--- a/apps/web/src/lib/queries/messages/document-messages.ts
+++ b/apps/web/src/lib/queries/messages/document-messages.ts
@@ -15,6 +15,7 @@ import {
usePatchThreadMutation,
useSendMessageMutation,
} from './mutations';
+import { fetchMessageThread } from './thread-replies';
import { useMessageTimelineQuery } from './timeline';
/** Positioning annotations needs every root, but never fetches every root's replies. */
@@ -61,7 +62,7 @@ export async function fetchDocumentThreads(
cursor: cursor ?? undefined,
});
for (const root of page.items) {
- threads.push(await entityMessagesClient.thread(parent, root.id));
+ threads.push(await fetchMessageThread(parent, root.id));
}
cursor = page.next_cursor;
} while (cursor);
diff --git a/apps/web/src/lib/queries/messages/message-sender.ts b/apps/web/src/lib/queries/messages/message-sender.ts
index 22689f4547..62990da2b0 100644
--- a/apps/web/src/lib/queries/messages/message-sender.ts
+++ b/apps/web/src/lib/queries/messages/message-sender.ts
@@ -3,6 +3,7 @@ import type { Bot } from '@service-storage/generated/schemas/bot';
import type {
Message as EntityMessage,
MessageListItem,
+ MessageThread,
MessageTimelinePage,
} from '@service-storage/messages';
import { firstPartyBotName } from '../bots/first-party-bot-name';
@@ -112,3 +113,18 @@ export function normalizeMessageTimelinePageSenders(
items: page.items.map(normalizeChannelMessageSender),
};
}
+
+/**
+ * A thread fetched on its own carries the same `bot_profile` / `triggered_by`
+ * as a timeline row, and its root and replies render through the same
+ * components, so they need the same derived `sender`.
+ */
+export function normalizeMessageThreadSenders(
+ thread: MessageThread
+): MessageThread {
+ return {
+ ...thread,
+ root: normalizeMessageSender(thread.root),
+ replies: thread.replies.map(normalizeMessageSender),
+ };
+}
diff --git a/apps/web/src/lib/queries/messages/sync.ts b/apps/web/src/lib/queries/messages/sync.ts
index e117a21a46..06e8e7df85 100644
--- a/apps/web/src/lib/queries/messages/sync.ts
+++ b/apps/web/src/lib/queries/messages/sync.ts
@@ -5,7 +5,6 @@ import type {
MessageParent,
MessageThread,
} from '@service-storage/messages';
-import { entityMessagesClient } from '@service-storage/messages';
import { queryClient } from '../client';
import { consumeNonce } from '../nonce';
import { MessageNonceKeys, messageKeys } from './keys';
@@ -20,7 +19,7 @@ import {
softInvalidateTargetCaches,
topLevelMessageHasReplies,
} from './reconcile';
-import { getThreadRepliesQueryKey } from './thread-replies';
+import { fetchMessageThread, getThreadRepliesQueryKey } from './thread-replies';
import {
getMessageTimelineQueryKey,
getMessageTimelineQueryKeyPrefix,
@@ -189,10 +188,15 @@ function refetchTimelineAwaitingFirstPage(parent: MessageParent): boolean {
return true;
}
-/** A live document root arrives without its anchor; its thread state carries it. */
+/**
+ * A live document root arrives without its anchor; its thread state carries
+ * it. The fetched thread replaces what the timeline seeded for this root, so
+ * it must carry the same derived senders or an agent's comment loses its name
+ * until the next full load.
+ */
async function loadDocumentRootState(parent: MessageParent, rootId: string) {
try {
- const thread = await entityMessagesClient.thread(parent, rootId);
+ const thread = await fetchMessageThread(parent, rootId);
queryClient.setQueryData<MessageThread>(
getThreadRepliesQueryKey(parent, rootId),
thread
diff --git a/apps/web/src/lib/queries/messages/tests/message-sender.test.ts b/apps/web/src/lib/queries/messages/tests/message-sender.test.ts
index c7680e9c53..0e336387a2 100644
--- a/apps/web/src/lib/queries/messages/tests/message-sender.test.ts
+++ b/apps/web/src/lib/queries/messages/tests/message-sender.test.ts
@@ -13,6 +13,7 @@ import {
firstPartyBotName,
getBotDisplayName,
normalizeChannelMessageSender,
+ normalizeMessageThreadSenders,
normalizeThreadReplySender,
senderFromStorageId,
} from '../../messages/message-sender';
@@ -136,4 +137,53 @@ describe('message sender normalization', () => {
'Bot'
);
});
+
+ it('derives agent senders for a fetched thread from its bot profiles', () => {
+ const {
+ state,
+ thread: _thread,
+ ...root
+ } = legacyMessage('bot|00000000-0000-0000-0000-000000000001');
+ const bot = {
+ ...root,
+ bot_profile: { name: 'Bingus', avatar_url: 'https://example.com/b.png' },
+ triggered_by: 'macro|[email protected]',
+ };
+ const reply = {
+ ...root,
+ id: 'reply-1',
+ thread_id: root.id,
+ sender_id: 'bot|00000000-0000-0000-0000-000000000003',
+ bot_profile: { name: 'Deploy Bot', avatar_url: null },
+ };
+
+ const thread = normalizeMessageThreadSenders({
+ state,
+ root: bot,
+ replies: [
+ reply,
+ {
+ ...root,
+ id: 'reply-2',
+ sender_id: 'macro|[email protected]',
+ bot_profile: null,
+ },
+ ],
+ });
+
+ expect(thread.root.sender).toEqual({
+ type: 'bot',
+ id: '00000000-0000-0000-0000-000000000001',
+ name: 'Bingus',
+ avatar_url: 'https://example.com/b.png',
+ triggered_by: 'macro|[email protected]',
+ });
+ expect(getBotDisplayName(thread.root.sender_id, thread.root.sender)).toBe(
+ 'Bingus'
+ );
+ expect(thread.replies.map((message) => message.sender)).toEqual([
+ expect.objectContaining({ type: 'bot', name: 'Deploy Bot' }),
+ expect.objectContaining({ type: 'user', id: 'macro|[email protected]' }),
+ ]);
+ });
});
diff --git a/apps/web/src/lib/queries/messages/tests/sync.test.ts b/apps/web/src/lib/queries/messages/tests/sync.test.ts
index 04eb254458..cff575aaf9 100644
--- a/apps/web/src/lib/queries/messages/tests/sync.test.ts
+++ b/apps/web/src/lib/queries/messages/tests/sync.test.ts
@@ -347,19 +347,27 @@ describe.each(['channel', 'document'] as const)(
root_id: 'newer-root',
anchor: { type: 'markdown', mark_id: 'mark' } as const,
};
+ // An agent's comment: the thread endpoint returns the same
+ // bot_profile the timeline does, never a pre-derived sender.
+ const agentRoot: Message = {
+ ...message(parent, 'newer-root'),
+ sender_id: 'bot|00000000-0000-0000-0000-000000000001',
+ bot_profile: { name: 'Bingus', avatar_url: null },
+ triggered_by: 'macro|[email protected]',
+ };
mocks.thread.mockResolvedValue({
- root: message(parent, 'newer-root'),
+ root: agentRoot,
state: rootState,
replies: [],
});
const invalidate = vi.spyOn(testQueryClient, 'invalidateQueries');
handleMessageEvent({
parent,
- actor: 'macro|[email protected]',
+ actor: agentRoot.sender_id,
nonce: null,
change: {
type: 'posted',
- message: message(parent, 'newer-root'),
+ message: agentRoot,
mentions: [],
notification_policy: 'Default',
},
@@ -367,16 +375,24 @@ describe.each(['channel', 'document'] as const)(
const newestRoot = () =>
testQueryClient.getQueryData<MessageTimelineData>(timelineKey())!
.pages[0].items[0];
+ const agentSender = expect.objectContaining({
+ type: 'bot',
+ name: 'Bingus',
+ triggered_by: 'macro|[email protected]',
+ });
+ expect(newestRoot().sender).toEqual(agentSender);
if (type === 'document') {
expect(mocks.thread).toHaveBeenCalledWith(parent, 'newer-root');
await vi.waitFor(() =>
expect(newestRoot().state.anchor).toEqual(rootState.anchor)
);
- expect(
- testQueryClient.getQueryData<MessageThread>(
- getThreadRepliesQueryKey(parent, 'newer-root')
- )?.state
- ).toEqual(rootState);
+ const thread = testQueryClient.getQueryData<MessageThread>(
+ getThreadRepliesQueryKey(parent, 'newer-root')
+ );
+ expect(thread?.state).toEqual(rootState);
+ // The margin renders this cached root, so the agent must keep its
+ // name here too rather than falling back to a generic "Bot".
+ expect(thread?.root.sender).toEqual(agentSender);
} else {
expect(mocks.thread).not.toHaveBeenCalled();
expect(newestRoot().id).toBe('newer-root');
@@ -557,6 +573,41 @@ describe.each(['channel', 'document'] as const)(
expect(threadReplies()).toEqual([]);
unsubscribe();
});
+ it('derives senders for a thread fetched on its own', async () => {
+ mocks.thread.mockReset();
+ const agent = 'bot|00000000-0000-0000-0000-000000000001';
+ mocks.thread.mockResolvedValue({
+ state,
+ root: {
+ ...message(parent, 'root'),
+ sender_id: agent,
+ bot_profile: { name: 'Bingus', avatar_url: null },
+ },
+ replies: [
+ {
+ ...message(parent, 'reply', 'root'),
+ sender_id: agent,
+ bot_profile: { name: 'Bingus', avatar_url: null },
+ triggered_by: 'macro|[email protected]',
+ },
+ message(parent, 'human-reply', 'root'),
+ ],
+ });
+ const thread = await testQueryClient.fetchQuery(
+ threadRepliesQueryOptions(parent, 'root')
+ );
+ expect(thread.root.sender).toEqual(
+ expect.objectContaining({ type: 'bot', name: 'Bingus' })
+ );
+ expect(thread.replies.map((reply) => reply.sender)).toEqual([
+ expect.objectContaining({
+ type: 'bot',
+ name: 'Bingus',
+ triggered_by: 'macro|[email protected]',
+ }),
+ { type: 'user', id: 'macro|[email protected]', triggered_by: undefined },
+ ]);
+ });
it('skips the sender nonce and scopes ephemeral typing to the parent and root', () => {
seed();
registerNonce(MessageNonceKeys.MESSAGE, 'own-send');
diff --git a/apps/web/src/lib/queries/messages/thread-replies.ts b/apps/web/src/lib/queries/messages/thread-replies.ts
index d07eda8ce3..ed5e8f9789 100644
--- a/apps/web/src/lib/queries/messages/thread-replies.ts
+++ b/apps/web/src/lib/queries/messages/thread-replies.ts
@@ -10,6 +10,7 @@ import { useQuery } from '@tanstack/solid-query';
import type { Accessor } from 'solid-js';
import { queryClient } from '../client';
import { messageKeys } from './keys';
+import { normalizeMessageThreadSenders } from './message-sender';
import { useMessageSubscription } from './subscription';
import { findTopLevelMessageInMessageTimeline } from './timeline';
@@ -22,13 +23,23 @@ type ThreadRepliesQueryKey = ReturnType<
typeof messageKeys.threadReplies
>['queryKey'];
+/** Every thread read shares this so a thread never enters the cache without derived senders. */
+export async function fetchMessageThread(
+ parent: MessageParent,
+ rootId: string
+): Promise<MessageThread> {
+ return normalizeMessageThreadSenders(
+ await entityMessagesClient.thread(parent, rootId)
+ );
+}
+
export function threadRepliesQueryOptions(
parent: MessageParent,
messageId: string
) {
return {
queryKey: messageKeys.threadReplies(parent, messageId).queryKey,
- queryFn: () => entityMessagesClient.thread(parent, messageId),
+ queryFn: () => fetchMessageThread(parent, messageId),
staleTime: Infinity,
};
}
diff --git a/apps/web/src/lib/service-clients/service-storage/messages.ts b/apps/web/src/lib/service-clients/service-storage/messages.ts
index b23f5904db..7a49578b9b 100644
--- a/apps/web/src/lib/service-clients/service-storage/messages.ts
+++ b/apps/web/src/lib/service-clients/service-storage/messages.ts
@@ -3,15 +3,19 @@ import { dssFetch } from './client';
import type { Message as StoredMessage } from './generated/schemas/message';
import type { MessageCursor } from './generated/schemas/messageCursor';
import type { MessageParent } from './generated/schemas/messageParent';
-import type { MessageThread } from './generated/schemas/messageThread';
+import type { MessageThread as StoredMessageThread } from './generated/schemas/messageThread';
import type { PostMessage } from './generated/schemas/postMessage';
import type { ThreadPatch } from './generated/schemas/threadPatch';
import type { ThreadState } from './generated/schemas/threadState';
-export type { MessageParent, MessageThread, PostMessage, ThreadPatch };
+export type { MessageParent, PostMessage, ThreadPatch };
export type Message = StoredMessage & {
sender?: import('./generated/schemas/apiMessageSender').ApiMessageSender;
};
+export type MessageThread = Omit<StoredMessageThread, 'root' | 'replies'> & {
+ root: Message;
+ replies: Message[];
+};
export type { MessagePatch } from './generated/schemas/messagePatch';
export type { MessageCursor };