Files
supabase/apps/studio/lib/ai/generate-assistant-response.sdk.test.ts
Matt Rossman ddabeac30c feat(studio): enable EU Assistant tracing (#53332)
Enables Braintrust tracing and feedback for Assistant chats on EU-region
projects. This was the final holdout that makes it so that tracing is
applied consistently across all projects.

Adds `projectRegion` metadata to root traces so the project’s region is
recorded with each chat (consistent with the existing
[isHighComplianceProject
metadata](https://github.com/supabase/supabase/blob/917dcb388939b8c54ea13c1e31b3c7285fb2ca65/apps/studio/lib/ai/generate-assistant-response.ts#L188)).

Verified in staging with a [successful Assistant trace recording
`projectRegion:
eu-central-1`](https://www.braintrust.dev/app/supabase.io/p/Assistant/logs?r=1b4a05d7a6c1414e62f532094aa39dc1&sid=4fdc76b08a4c5a43).

<img width="2656" height="1560" alt="CleanShot 2026-11-12 at 1 12 30
PM@2x"
src="https://github.com/user-attachments/assets/4e81033a-c754-46db-845d-586d1d71e035"
/>

Closes AI-1197
2026-11-13 11:02:46 -05:00

50 lines
1.8 KiB
TypeScript

import { convertArrayToReadableStream, MockLanguageModelV4 } from 'ai/test'
import { expect, it } from 'vitest'
it('exports project region on the root trace', async () => {
const braintrust = await import('braintrust')
const { setInitialTestState, simulateLoginForTests, useTestBackgroundLogger } =
braintrust._exportsForTestingOnly
setInitialTestState()
await simulateLoginForTests()
const uploads = useTestBackgroundLogger()
braintrust.initLogger({
projectId: '00000000-0000-4000-8000-000000000000',
projectName: 'test',
})
const { generateAssistantResponse } = await import('./generate-assistant-response')
const result = await generateAssistantResponse({
model: new MockLanguageModelV4({
doStream: async () => ({
stream: convertArrayToReadableStream([
{ type: 'text-start', id: 'text' },
{ type: 'text-delta', id: 'text', delta: 'Hello!' },
{ type: 'text-end', id: 'text' },
{
type: 'finish',
finishReason: { unified: 'stop', raw: undefined },
usage: {
inputTokens: { total: 1, noCache: 1, cacheRead: 0, cacheWrite: 0 },
outputTokens: { total: 1, text: 1, reasoning: 0 },
},
},
]),
}),
}),
messages: [{ id: 'message', role: 'user', parts: [{ type: 'text', text: 'Hi!' }] }],
tools: {},
projectRef: 'test-project',
projectRegion: 'eu-west-1',
allowTracing: true,
})
await result.consumeStream()
await braintrust.flush()
expect(await uploads.drain()).toContainEqual(
expect.objectContaining({
span_attributes: expect.objectContaining({ name: 'generateAssistantResponse' }),
metadata: expect.objectContaining({ projectRef: 'test-project', projectRegion: 'eu-west-1' }),
})
)
})