fix: Track first builder message telemetry correctly (no-changelog) (#31140)

This commit is contained in:
Albert Alises 2026-05-26 16:31:15 +02:00 committed by GitHub
parent 1b1b7a13e8
commit 9969faebf7
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 22 additions and 7 deletions

View File

@ -790,6 +790,24 @@ describe('createThreadRuntime - SSE and hydration', () => {
expect(mockPostMessage).toHaveBeenCalledTimes(1);
});
test('sendMessage tracks whether this is the first user message in the thread', async () => {
mockPostMessage.mockResolvedValue({ runId: 'run-1' });
await activeRuntime(registry).sendMessage('first');
await activeRuntime(registry).sendMessage('second');
expect(mockTelemetryTrack).toHaveBeenNthCalledWith(1, 'User sent builder message', {
thread_id: activeThreadId,
instance_id: 'instance-1',
is_first_message: true,
});
expect(mockTelemetryTrack).toHaveBeenNthCalledWith(2, 'User sent builder message', {
thread_id: activeThreadId,
instance_id: 'instance-1',
is_first_message: false,
});
});
test('sendMessage forwards pushRef to postMessage', async () => {
mockPostMessage.mockResolvedValue({ runId: 'run-1' });

View File

@ -755,15 +755,11 @@ export function createThreadRuntime(threadId: string, hooks: ThreadRuntimeHooks)
}
}
function trackUserMessageSent(optimistic: InstanceAiMessage): void {
// The first user message in the array is the first the thread has ever
// seen. `find` short-circuits at the first match, so this is O(1) once
// the user has sent more than one message.
const firstUser = messages.value.find((m) => m.role === 'user');
function trackUserMessageSent(isFirstMessage: boolean): void {
telemetry.track('User sent builder message', {
thread_id: threadId,
instance_id: rootStore.instanceId,
is_first_message: firstUser === optimistic,
is_first_message: isFirstMessage,
});
}
@ -815,8 +811,9 @@ export function createThreadRuntime(threadId: string, hooks: ThreadRuntimeHooks)
pendingMessageCount.value += 1;
try {
ensureSSEConnected();
const isFirstMessage = !messages.value.some((m) => m.role === 'user');
const optimistic = pushOptimisticUserMessage(message, attachments);
trackUserMessageSent(optimistic);
trackUserMessageSent(isFirstMessage);
if (!(await dispatchUserMessage(message, attachments, pushRef))) {
removeOptimisticMessage(optimistic);