Skip to content

The open alternative to Salesforce, designed for AI.

Pull the pure parts out of useAgentChatSubscription (#27561)

Félix Malfait2be26299f84d
diff --git a/packages/twenty-front/src/modules/ai/hooks/useAgentChatSubscription.ts b/packages/twenty-front/src/modules/ai/hooks/useAgentChatSubscription.ts
index 509a94b7dfb..a4ed0c42543 100644
--- a/packages/twenty-front/src/modules/ai/hooks/useAgentChatSubscription.ts
+++ b/packages/twenty-front/src/modules/ai/hooks/useAgentChatSubscription.ts
@@ -14,10 +14,8 @@ import { useStore } from 'jotai';
import {
type AgentChatSubscriptionEvent,
type ExtendedUIMessage,
- type ExtendedUIMessagePart,
} from 'twenty-shared/ai';
import { isDefined, isNonEmptyString } from 'twenty-shared/utils';
-import { v4 } from 'uuid';
import { AGENT_CHAT_REFETCH_MESSAGES_EVENT_NAME } from '@/ai/constants/AgentChatRefetchMessagesEventName';
import { useApplyAgentChatThreadUpdate } from '@/ai/hooks/useApplyAgentChatThreadUpdate';
@@ -32,9 +30,13 @@ import { agentChatStreamLastEventTimestampState } from '@/ai/states/agentChatStr
import { agentChatStreamResubscribeNonceState } from '@/ai/states/agentChatStreamResubscribeNonceState';
import { agentChatUsageFamilyState } from '@/ai/states/agentChatUsageFamilyState';
import { agentChatThreadRecordFamilySelector } from '@/ai/states/selectors/agentChatThreadRecordFamilySelector';
+import { accumulateAgentChatUsage } from '@/ai/utils/accumulateAgentChatUsage';
import { AiChatErrorCode } from '@/ai/utils/aiChatErrorCode';
import { createAiChatCodedError } from '@/ai/utils/createAiChatCodedError';
+import { createMidStreamAdapter } from '@/ai/utils/createMidStreamAdapter';
import { createStreamChunkSequencer } from '@/ai/utils/createStreamChunkSequencer';
+import { getAgentChatMessageThreadTitle } from '@/ai/utils/getAgentChatMessageThreadTitle';
+import { upsertAgentChatMessage } from '@/ai/utils/upsertAgentChatMessage';
import { currentWorkspaceState } from '@/auth/states/currentWorkspaceState';
import { dispatchBrowserEvent } from '@/browser-event/utils/dispatchBrowserEvent';
import { sseClientState } from '@/sse-db-event/states/sseClientState';
@@ -48,71 +50,6 @@ import {
const THROTTLE_MS = 100;
const PERMISSIONS_REFRESH_INTERVAL_MS = 30_000;
-// readUIMessageStream needs start chunks that a mid-stream reconnect missed, so inject synthetic ones.
-const createMidStreamAdapter = () => {
- let hasSeenStart = false;
- const knownTextPartIds = new Set<string>();
- const knownReasoningPartIds = new Set<string>();
- const knownToolCallIds = new Set<string>();
-
- return new TransformStream<UIMessageChunk, UIMessageChunk>({
- transform(chunk, controller) {
- if (!hasSeenStart) {
- hasSeenStart = true;
- if (chunk.type !== 'start') {
- controller.enqueue({ type: 'start', messageId: v4() });
- controller.enqueue({ type: 'start-step' });
- }
- }
-
- if (chunk.type === 'text-start') {
- knownTextPartIds.add(chunk.id);
- } else if (
- (chunk.type === 'text-delta' || chunk.type === 'text-end') &&
- !knownTextPartIds.has(chunk.id)
- ) {
- controller.enqueue({ type: 'text-start', id: chunk.id });
- knownTextPartIds.add(chunk.id);
- }
-
- if (chunk.type === 'reasoning-start') {
- knownReasoningPartIds.add(chunk.id);
- } else if (
- (chunk.type === 'reasoning-delta' || chunk.type === 'reasoning-end') &&
- !knownReasoningPartIds.has(chunk.id)
- ) {
- controller.enqueue({ type: 'reasoning-start', id: chunk.id });
- knownReasoningPartIds.add(chunk.id);
- }
-
- if (chunk.type === 'tool-input-start') {
- knownToolCallIds.add(chunk.toolCallId);
- } else if (
- chunk.type === 'tool-input-delta' &&
- !knownToolCallIds.has(chunk.toolCallId)
- ) {
- controller.enqueue({
- type: 'tool-input-start',
- toolCallId: chunk.toolCallId,
- toolName: 'unknown',
- });
- knownToolCallIds.add(chunk.toolCallId);
- }
-
- controller.enqueue(chunk);
- },
- });
-};
-
-type ThreadTitleDataPart = Extract<
- ExtendedUIMessagePart,
- { type: 'data-thread-title' }
->;
-
-const isThreadTitleDataPart = (
- part: ExtendedUIMessagePart,
-): part is ThreadTitleDataPart => part.type === 'data-thread-title';
-
export const useAgentChatSubscription = (threadId: string | null) => {
const store = useStore();
// Only a resubscribe trigger: an access denial ends the subscription until the member's permissions change
@@ -184,20 +121,9 @@ export const useAgentChatSubscription = (threadId: string | null) => {
return;
}
- const currentMessages = store.get(messagesAtom);
-
- const streamingMsgIndex = currentMessages.findIndex(
- (message) => message.id === messageToFlush.id,
+ store.set(messagesAtom, (currentMessages) =>
+ upsertAgentChatMessage(currentMessages, messageToFlush),
);
-
- if (streamingMsgIndex >= 0) {
- const updatedMessages = [...currentMessages];
-
- updatedMessages[streamingMsgIndex] = messageToFlush;
- store.set(messagesAtom, updatedMessages);
- } else {
- store.set(messagesAtom, [...currentMessages, messageToFlush]);
- }
};
const scheduleAtomUpdate = (message: ExtendedUIMessage) => {
@@ -224,8 +150,7 @@ export const useAgentChatSubscription = (threadId: string | null) => {
}
const extendedMessage = message as ExtendedUIMessage;
- const title = extendedMessage.parts.find(isThreadTitleDataPart)?.data
- .title;
+ const title = getAgentChatMessageThreadTitle(extendedMessage);
const threadRecord = store.get(
agentChatThreadRecordFamilySelector.selectorFamily(threadId),
@@ -249,23 +174,9 @@ export const useAgentChatSubscription = (threadId: string | null) => {
) {
lastUsageCountedMessageId = extendedMessage.id;
- store.set(usageAtom, (prev) => ({
- lastMessage: {
- inputTokens: usage.inputTokens,
- outputTokens: usage.outputTokens,
- cachedInputTokens: usage.cachedInputTokens,
- inputCredits: usage.inputCredits,
- outputCredits: usage.outputCredits,
- },
- cachedInputTokens:
- (prev?.cachedInputTokens ?? 0) + usage.cachedInputTokens,
- conversationSize: usage.conversationSize,
- contextWindowTokens: model.contextWindowTokens,
- inputTokens: (prev?.inputTokens ?? 0) + usage.inputTokens,
- outputTokens: (prev?.outputTokens ?? 0) + usage.outputTokens,
- inputCredits: (prev?.inputCredits ?? 0) + usage.inputCredits,
- outputCredits: (prev?.outputCredits ?? 0) + usage.outputCredits,
- }));
+ store.set(usageAtom, (previousUsage) =>
+ accumulateAgentChatUsage(previousUsage, usage, model),
+ );
}
scheduleAtomUpdate(extendedMessage);
diff --git a/packages/twenty-front/src/modules/ai/utils/__tests__/accumulateAgentChatUsage.test.ts b/packages/twenty-front/src/modules/ai/utils/__tests__/accumulateAgentChatUsage.test.ts
new file mode 100644
index 00000000000..b462614159e
--- /dev/null
+++ b/packages/twenty-front/src/modules/ai/utils/__tests__/accumulateAgentChatUsage.test.ts
@@ -0,0 +1,71 @@
+import { accumulateAgentChatUsage } from '@/ai/utils/accumulateAgentChatUsage';
+
+const messageUsage = {
+ inputTokens: 100,
+ outputTokens: 20,
+ cachedInputTokens: 50,
+ inputCredits: 1,
+ outputCredits: 2,
+ conversationSize: 120,
+};
+
+describe('accumulateAgentChatUsage', () => {
+ it('starts totals from the first message usage', () => {
+ expect(
+ accumulateAgentChatUsage(null, messageUsage, {
+ contextWindowTokens: 200_000,
+ }),
+ ).toEqual({
+ lastMessage: {
+ inputTokens: 100,
+ outputTokens: 20,
+ cachedInputTokens: 50,
+ inputCredits: 1,
+ outputCredits: 2,
+ },
+ cachedInputTokens: 50,
+ conversationSize: 120,
+ contextWindowTokens: 200_000,
+ inputTokens: 100,
+ outputTokens: 20,
+ inputCredits: 1,
+ outputCredits: 2,
+ });
+ });
+
+ it('sums totals and takes the latest message, conversation size and context window', () => {
+ const previousUsage = accumulateAgentChatUsage(null, messageUsage, {
+ contextWindowTokens: 200_000,
+ });
+
+ expect(
+ accumulateAgentChatUsage(
+ previousUsage,
+ {
+ inputTokens: 300,
+ outputTokens: 40,
+ cachedInputTokens: 10,
+ inputCredits: 3,
+ outputCredits: 4,
+ conversationSize: 460,
+ },
+ { contextWindowTokens: 1_000_000 },
+ ),
+ ).toEqual({
+ lastMessage: {
+ inputTokens: 300,
+ outputTokens: 40,
+ cachedInputTokens: 10,
+ inputCredits: 3,
+ outputCredits: 4,
+ },
+ cachedInputTokens: 60,
+ conversationSize: 460,
+ contextWindowTokens: 1_000_000,
+ inputTokens: 400,
+ outputTokens: 60,
+ inputCredits: 4,
+ outputCredits: 6,
+ });
+ });
+});
diff --git a/packages/twenty-front/src/modules/ai/utils/__tests__/createMidStreamAdapter.test.ts b/packages/twenty-front/src/modules/ai/utils/__tests__/createMidStreamAdapter.test.ts
new file mode 100644
index 00000000000..21245ad49e1
--- /dev/null
+++ b/packages/twenty-front/src/modules/ai/utils/__tests__/createMidStreamAdapter.test.ts
@@ -0,0 +1,120 @@
+import { type UIMessageChunk } from 'ai';
+
+import { createMidStreamAdapter } from '@/ai/utils/createMidStreamAdapter';
+
+const adapt = async (chunks: UIMessageChunk[]) => {
+ const adaptedChunks: UIMessageChunk[] = [];
+ const reader = new ReadableStream<UIMessageChunk>({
+ start(controller) {
+ chunks.forEach((chunk) => controller.enqueue(chunk));
+ controller.close();
+ },
+ })
+ .pipeThrough(createMidStreamAdapter())
+ .getReader();
+
+ for (;;) {
+ const { done, value } = await reader.read();
+
+ if (done) {
+ return adaptedChunks;
+ }
+
+ adaptedChunks.push(value);
+ }
+};
+
+describe('createMidStreamAdapter', () => {
+ it('passes a stream that starts from the beginning through unchanged', async () => {
+ const chunks: UIMessageChunk[] = [
+ { type: 'start', messageId: 'message-1' },
+ { type: 'text-start', id: 'text-1' },
+ { type: 'text-delta', id: 'text-1', delta: 'Hello' },
+ { type: 'text-end', id: 'text-1' },
+ { type: 'reasoning-start', id: 'reasoning-1' },
+ { type: 'reasoning-delta', id: 'reasoning-1', delta: 'Thinking' },
+ {
+ type: 'tool-input-start',
+ toolCallId: 'call-1',
+ toolName: 'search',
+ },
+ { type: 'tool-input-delta', toolCallId: 'call-1', inputTextDelta: '{' },
+ ];
+
+ expect(await adapt(chunks)).toEqual(chunks);
+ });
+
+ it('injects start and start-step when joining mid-stream', async () => {
+ const adaptedChunks = await adapt([
+ { type: 'text-start', id: 'text-1' },
+ { type: 'text-delta', id: 'text-1', delta: 'Hello' },
+ ]);
+
+ expect(adaptedChunks).toEqual([
+ { type: 'start', messageId: expect.any(String) },
+ { type: 'start-step' },
+ { type: 'text-start', id: 'text-1' },
+ { type: 'text-delta', id: 'text-1', delta: 'Hello' },
+ ]);
+ });
+
+ it('injects text-start once for a text part whose start was missed', async () => {
+ const adaptedChunks = await adapt([
+ { type: 'start', messageId: 'message-1' },
+ { type: 'text-delta', id: 'text-1', delta: 'Hel' },
+ { type: 'text-delta', id: 'text-1', delta: 'lo' },
+ { type: 'text-end', id: 'text-1' },
+ ]);
+
+ expect(adaptedChunks).toEqual([
+ { type: 'start', messageId: 'message-1' },
+ { type: 'text-start', id: 'text-1' },
+ { type: 'text-delta', id: 'text-1', delta: 'Hel' },
+ { type: 'text-delta', id: 'text-1', delta: 'lo' },
+ { type: 'text-end', id: 'text-1' },
+ ]);
+ });
+
+ it('injects text-start before a lone text-end', async () => {
+ const adaptedChunks = await adapt([
+ { type: 'start', messageId: 'message-1' },
+ { type: 'text-end', id: 'text-1' },
+ ]);
+
+ expect(adaptedChunks).toEqual([
+ { type: 'start', messageId: 'message-1' },
+ { type: 'text-start', id: 'text-1' },
+ { type: 'text-end', id: 'text-1' },
+ ]);
+ });
+
+ it('injects reasoning-start for a reasoning part whose start was missed', async () => {
+ const adaptedChunks = await adapt([
+ { type: 'start', messageId: 'message-1' },
+ { type: 'reasoning-delta', id: 'reasoning-1', delta: 'Thinking' },
+ { type: 'reasoning-end', id: 'reasoning-1' },
+ ]);
+
+ expect(adaptedChunks).toEqual([
+ { type: 'start', messageId: 'message-1' },
+ { type: 'reasoning-start', id: 'reasoning-1' },
+ { type: 'reasoning-delta', id: 'reasoning-1', delta: 'Thinking' },
+ { type: 'reasoning-end', id: 'reasoning-1' },
+ ]);
+ });
+
+ it('injects tool-input-start for a tool call whose start was missed', async () => {
+ const adaptedChunks = await adapt([
+ { type: 'start', messageId: 'message-1' },
+ { type: 'tool-input-delta', toolCallId: 'call-1', inputTextDelta: '{' },
+ { type: 'tool-input-delta', toolCallId: 'call-1', inputTextDelta: '}' },
+ ]);
+
+ expect(adaptedChunks).toEqual([
+ { type: 'start', messageId: 'message-1' },
+ { type: 'tool-input-start', toolCallId: 'call-1', toolName: 'unknown' },
+ { type: 'tool-input-delta', toolCallId: 'call-1', inputTextDelta: '{' },
+ { type: 'tool-input-delta', toolCallId: 'call-1', inputTextDelta: '}' },
+ ]);
+ });
+});
diff --git a/packages/twenty-front/src/modules/ai/utils/__tests__/getAgentChatMessageThreadTitle.test.ts b/packages/twenty-front/src/modules/ai/utils/__tests__/getAgentChatMessageThreadTitle.test.ts
new file mode 100644
index 00000000000..eec1450354e
--- /dev/null
+++ b/packages/twenty-front/src/modules/ai/utils/__tests__/getAgentChatMessageThreadTitle.test.ts
@@ -0,0 +1,28 @@
+import { type ExtendedUIMessage } from 'twenty-shared/ai';
+
+import { getAgentChatMessageThreadTitle } from '@/ai/utils/getAgentChatMessageThreadTitle';
+
+describe('getAgentChatMessageThreadTitle', () => {
+ it('returns the title carried by a thread title data part', () => {
+ const message: ExtendedUIMessage = {
+ id: 'message',
+ role: 'assistant',
+ parts: [
+ { type: 'text', text: 'Hello' },
+ { type: 'data-thread-title', data: { title: 'Quarterly review' } },
+ ],
+ };
+
+ expect(getAgentChatMessageThreadTitle(message)).toBe('Quarterly review');
+ });
+
+ it('returns undefined when the message has no thread title part', () => {
+ const message: ExtendedUIMessage = {
+ id: 'message',
+ role: 'assistant',
+ parts: [{ type: 'text', text: 'Hello' }],
+ };
+
+ expect(getAgentChatMessageThreadTitle(message)).toBeUndefined();
+ });
+});
diff --git a/packages/twenty-front/src/modules/ai/utils/__tests__/upsertAgentChatMessage.test.ts b/packages/twenty-front/src/modules/ai/utils/__tests__/upsertAgentChatMessage.test.ts
new file mode 100644
index 00000000000..6c92215d0c8
--- /dev/null
+++ b/packages/twenty-front/src/modules/ai/utils/__tests__/upsertAgentChatMessage.test.ts
@@ -0,0 +1,33 @@
+import { type ExtendedUIMessage } from 'twenty-shared/ai';
+
+import { upsertAgentChatMessage } from '@/ai/utils/upsertAgentChatMessage';
+
+const buildMessage = (id: string, text: string): ExtendedUIMessage => ({
+ id,
+ role: 'assistant',
+ parts: [{ type: 'text', text }],
+});
+
+describe('upsertAgentChatMessage', () => {
+ it('appends a message that is not in the list yet', () => {
+ const first = buildMessage('first', 'Hello');
+ const second = buildMessage('second', 'World');
+
+ expect(upsertAgentChatMessage([first], second)).toEqual([first, second]);
+ });
+
+ it('replaces the message with the same id in place without mutating the list', () => {
+ const messages = [
+ buildMessage('first', 'Hello'),
+ buildMessage('streaming', 'Wor'),
+ buildMessage('last', '!'),
+ ];
+ const updatedStreamingMessage = buildMessage('streaming', 'World');
+
+ const result = upsertAgentChatMessage(messages, updatedStreamingMessage);
+
+ expect(result).toEqual([messages[0], updatedStreamingMessage, messages[2]]);
+ expect(result).not.toBe(messages);
+ expect(messages[1].parts).toEqual([{ type: 'text', text: 'Wor' }]);
+ });
+});
diff --git a/packages/twenty-front/src/modules/ai/utils/accumulateAgentChatUsage.ts b/packages/twenty-front/src/modules/ai/utils/accumulateAgentChatUsage.ts
new file mode 100644
index 00000000000..d363582a3fb
--- /dev/null
+++ b/packages/twenty-front/src/modules/ai/utils/accumulateAgentChatUsage.ts
@@ -0,0 +1,29 @@
+import {
+ type AiChatModelMetadata,
+ type AiChatUsageMetadata,
+} from 'twenty-shared/ai';
+
+import { type AgentChatUsageState } from '@/ai/states/agentChatUsageFamilyState';
+
+export const accumulateAgentChatUsage = (
+ previousUsage: AgentChatUsageState | null,
+ messageUsage: AiChatUsageMetadata,
+ model: AiChatModelMetadata,
+): AgentChatUsageState => ({
+ lastMessage: {
+ inputTokens: messageUsage.inputTokens,
+ outputTokens: messageUsage.outputTokens,
+ cachedInputTokens: messageUsage.cachedInputTokens,
+ inputCredits: messageUsage.inputCredits,
+ outputCredits: messageUsage.outputCredits,
+ },
+ cachedInputTokens:
+ (previousUsage?.cachedInputTokens ?? 0) + messageUsage.cachedInputTokens,
+ conversationSize: messageUsage.conversationSize,
+ contextWindowTokens: model.contextWindowTokens,
+ inputTokens: (previousUsage?.inputTokens ?? 0) + messageUsage.inputTokens,
+ outputTokens: (previousUsage?.outputTokens ?? 0) + messageUsage.outputTokens,
+ inputCredits: (previousUsage?.inputCredits ?? 0) + messageUsage.inputCredits,
+ outputCredits:
+ (previousUsage?.outputCredits ?? 0) + messageUsage.outputCredits,
+});
diff --git a/packages/twenty-front/src/modules/ai/utils/createMidStreamAdapter.ts b/packages/twenty-front/src/modules/ai/utils/createMidStreamAdapter.ts
new file mode 100644
index 00000000000..2f883d96b84
--- /dev/null
+++ b/packages/twenty-front/src/modules/ai/utils/createMidStreamAdapter.ts
@@ -0,0 +1,58 @@
+import { type UIMessageChunk } from 'ai';
+import { v4 } from 'uuid';
+
+// readUIMessageStream needs start chunks that a mid-stream reconnect missed, so inject synthetic ones.
+export const createMidStreamAdapter = () => {
+ let hasSeenStart = false;
+ const knownTextPartIds = new Set<string>();
+ const knownReasoningPartIds = new Set<string>();
+ const knownToolCallIds = new Set<string>();
+
+ return new TransformStream<UIMessageChunk, UIMessageChunk>({
+ transform(chunk, controller) {
+ if (!hasSeenStart) {
+ hasSeenStart = true;
+ if (chunk.type !== 'start') {
+ controller.enqueue({ type: 'start', messageId: v4() });
+ controller.enqueue({ type: 'start-step' });
+ }
+ }
+
+ if (chunk.type === 'text-start') {
+ knownTextPartIds.add(chunk.id);
+ } else if (
+ (chunk.type === 'text-delta' || chunk.type === 'text-end') &&
+ !knownTextPartIds.has(chunk.id)
+ ) {
+ controller.enqueue({ type: 'text-start', id: chunk.id });
+ knownTextPartIds.add(chunk.id);
+ }
+
+ if (chunk.type === 'reasoning-start') {
+ knownReasoningPartIds.add(chunk.id);
+ } else if (
+ (chunk.type === 'reasoning-delta' || chunk.type === 'reasoning-end') &&
+ !knownReasoningPartIds.has(chunk.id)
+ ) {
+ controller.enqueue({ type: 'reasoning-start', id: chunk.id });
+ knownReasoningPartIds.add(chunk.id);
+ }
+
+ if (chunk.type === 'tool-input-start') {
+ knownToolCallIds.add(chunk.toolCallId);
+ } else if (
+ chunk.type === 'tool-input-delta' &&
+ !knownToolCallIds.has(chunk.toolCallId)
+ ) {
+ controller.enqueue({
+ type: 'tool-input-start',
+ toolCallId: chunk.toolCallId,
+ toolName: 'unknown',
+ });
+ knownToolCallIds.add(chunk.toolCallId);
+ }
+
+ controller.enqueue(chunk);
+ },
+ });
+};
diff --git a/packages/twenty-front/src/modules/ai/utils/getAgentChatMessageThreadTitle.ts b/packages/twenty-front/src/modules/ai/utils/getAgentChatMessageThreadTitle.ts
new file mode 100644
index 00000000000..2c2fba50230
--- /dev/null
+++ b/packages/twenty-front/src/modules/ai/utils/getAgentChatMessageThreadTitle.ts
@@ -0,0 +1,16 @@
+import {
+ type ExtendedUIMessage,
+ type ExtendedUIMessagePart,
+} from 'twenty-shared/ai';
+
+type ThreadTitleDataPart = Extract<
+ ExtendedUIMessagePart,
+ { type: 'data-thread-title' }
+>;
+
+const isThreadTitleDataPart = (
+ part: ExtendedUIMessagePart,
+): part is ThreadTitleDataPart => part.type === 'data-thread-title';
+
+export const getAgentChatMessageThreadTitle = (message: ExtendedUIMessage) =>
+ message.parts.find(isThreadTitleDataPart)?.data.title;
diff --git a/packages/twenty-front/src/modules/ai/utils/upsertAgentChatMessage.ts b/packages/twenty-front/src/modules/ai/utils/upsertAgentChatMessage.ts
new file mode 100644
index 00000000000..59af35db556
--- /dev/null
+++ b/packages/twenty-front/src/modules/ai/utils/upsertAgentChatMessage.ts
@@ -0,0 +1,20 @@
+import { type ExtendedUIMessage } from 'twenty-shared/ai';
+
+export const upsertAgentChatMessage = (
+ messages: ExtendedUIMessage[],
+ message: ExtendedUIMessage,
+): ExtendedUIMessage[] => {
+ const messageIndex = messages.findIndex(
+ (existingMessage) => existingMessage.id === message.id,
+ );
+
+ if (messageIndex < 0) {
+ return [...messages, message];
+ }
+
+ const updatedMessages = [...messages];
+
+ updatedMessages[messageIndex] = message;
+
+ return updatedMessages;
+};