mirror of
https://github.com/supabase/supabase.git
synced 2026-12-04 07:29:16 +00:00
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
50 lines
1.9 KiB
TypeScript
50 lines
1.9 KiB
TypeScript
import { currentSpan, initLogger, type SpanExportData } from 'braintrust'
|
|
import { configureInstrumentation } from 'braintrust/instrumentation'
|
|
|
|
import { NO_DATA_ERROR } from './tools/execute-sql-output'
|
|
|
|
const BRAINTRUST_API_KEY = process.env.BRAINTRUST_API_KEY
|
|
const BRAINTRUST_PROJECT_ID = process.env.BRAINTRUST_PROJECT_ID
|
|
|
|
export const IS_TRACING_ENABLED =
|
|
BRAINTRUST_API_KEY !== undefined && BRAINTRUST_PROJECT_ID !== undefined
|
|
|
|
if (IS_TRACING_ENABLED) {
|
|
initLogger({
|
|
apiKey: BRAINTRUST_API_KEY,
|
|
projectId: BRAINTRUST_PROJECT_ID,
|
|
})
|
|
// Span customizers can redact errors before upload.
|
|
// https://www.braintrust.dev/docs/admin/data-management/protect-sensitive-data#redact-before-upload
|
|
configureInstrumentation({ spanCustomizers: [{ onSpanExport: redactHiddenSpanError }] })
|
|
}
|
|
|
|
// Markers survive if their span never exports an end record.
|
|
const hiddenErrorSpanIds = new Set<string>()
|
|
|
|
/**
|
|
* Keeps a shared parent marked across parallel failures. See braintrust-logger.sdk.test.ts.
|
|
* [tool error, tool error] → one marker → parent end clears it
|
|
*/
|
|
export function hideSpanErrorFromTrace(spanId = currentSpan().spanId) {
|
|
if (spanId) hiddenErrorSpanIds.add(spanId)
|
|
}
|
|
|
|
// The parent marker also hides unrelated child errors.
|
|
export function redactHiddenSpanError(data: SpanExportData): SpanExportData {
|
|
const parentIds = Array.isArray(data.span_parents) ? data.span_parents : []
|
|
const hiddenId = [data.span_id, ...parentIds].find(
|
|
(id): id is string => typeof id === 'string' && hiddenErrorSpanIds.has(id)
|
|
)
|
|
if (hiddenId === undefined) return data
|
|
|
|
const isEnd = isRecord(data.metrics) && data.metrics.end !== undefined
|
|
if (data.span_id === hiddenId && isEnd) hiddenErrorSpanIds.delete(hiddenId)
|
|
|
|
return data.error === undefined ? data : { ...data, error: NO_DATA_ERROR }
|
|
}
|
|
|
|
function isRecord(value: unknown): value is Record<string, unknown> {
|
|
return typeof value === 'object' && value !== null
|
|
}
|