2025-12-06 12:46:40 +09:00
|
|
|
import { LangfuseClient } from "@langfuse/client"
|
|
|
|
|
import { observe, updateActiveTrace } from "@langfuse/tracing"
|
|
|
|
|
import * as api from "@opentelemetry/api"
|
2025-12-05 21:15:02 +09:00
|
|
|
|
|
|
|
|
// Singleton LangfuseClient instance for direct API calls
|
2025-12-06 12:46:40 +09:00
|
|
|
let langfuseClient: LangfuseClient | null = null
|
2025-12-05 21:15:02 +09:00
|
|
|
|
|
|
|
|
export function getLangfuseClient(): LangfuseClient | null {
|
2025-12-06 12:46:40 +09:00
|
|
|
if (!process.env.LANGFUSE_PUBLIC_KEY || !process.env.LANGFUSE_SECRET_KEY) {
|
|
|
|
|
return null
|
|
|
|
|
}
|
2025-12-05 21:15:02 +09:00
|
|
|
|
2025-12-06 12:46:40 +09:00
|
|
|
if (!langfuseClient) {
|
|
|
|
|
langfuseClient = new LangfuseClient({
|
|
|
|
|
publicKey: process.env.LANGFUSE_PUBLIC_KEY,
|
|
|
|
|
secretKey: process.env.LANGFUSE_SECRET_KEY,
|
|
|
|
|
baseUrl: process.env.LANGFUSE_BASEURL,
|
|
|
|
|
})
|
|
|
|
|
}
|
2025-12-05 21:15:02 +09:00
|
|
|
|
2025-12-06 12:46:40 +09:00
|
|
|
return langfuseClient
|
2025-12-05 21:15:02 +09:00
|
|
|
}
|
|
|
|
|
|
2025-12-23 16:26:45 +09:00
|
|
|
// Check if Langfuse is configured (both keys required)
|
2025-12-05 21:15:02 +09:00
|
|
|
export function isLangfuseEnabled(): boolean {
|
2025-12-23 16:26:45 +09:00
|
|
|
return !!(
|
|
|
|
|
process.env.LANGFUSE_PUBLIC_KEY && process.env.LANGFUSE_SECRET_KEY
|
|
|
|
|
)
|
2025-12-05 21:15:02 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Update trace with input data at the start of request
|
|
|
|
|
export function setTraceInput(params: {
|
2025-12-06 12:46:40 +09:00
|
|
|
input: string
|
|
|
|
|
sessionId?: string
|
|
|
|
|
userId?: string
|
2025-12-05 21:15:02 +09:00
|
|
|
}) {
|
2025-12-06 12:46:40 +09:00
|
|
|
if (!isLangfuseEnabled()) return
|
2025-12-05 21:15:02 +09:00
|
|
|
|
2025-12-06 12:46:40 +09:00
|
|
|
updateActiveTrace({
|
|
|
|
|
name: "chat",
|
|
|
|
|
input: params.input,
|
|
|
|
|
sessionId: params.sessionId,
|
|
|
|
|
userId: params.userId,
|
|
|
|
|
})
|
2025-12-05 21:15:02 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Update trace with output and end the span
|
2025-12-23 16:26:45 +09:00
|
|
|
// Note: AI SDK 6 telemetry automatically reports token usage on its spans,
|
|
|
|
|
// so we only need to set the output text and close our wrapper span
|
|
|
|
|
export function setTraceOutput(output: string) {
|
2025-12-06 12:46:40 +09:00
|
|
|
if (!isLangfuseEnabled()) return
|
2025-12-05 21:15:02 +09:00
|
|
|
|
2025-12-06 12:46:40 +09:00
|
|
|
updateActiveTrace({ output })
|
2025-12-05 21:15:02 +09:00
|
|
|
|
2025-12-23 16:26:45 +09:00
|
|
|
// End the observe() wrapper span (AI SDK creates its own child spans with usage)
|
2025-12-06 12:46:40 +09:00
|
|
|
const activeSpan = api.trace.getActiveSpan()
|
|
|
|
|
if (activeSpan) {
|
|
|
|
|
activeSpan.end()
|
2025-12-05 21:15:02 +09:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Get telemetry config for streamText
|
|
|
|
|
export function getTelemetryConfig(params: {
|
2025-12-06 12:46:40 +09:00
|
|
|
sessionId?: string
|
|
|
|
|
userId?: string
|
2025-12-05 21:15:02 +09:00
|
|
|
}) {
|
2025-12-06 12:46:40 +09:00
|
|
|
if (!isLangfuseEnabled()) return undefined
|
2025-12-05 21:15:02 +09:00
|
|
|
|
2025-12-06 12:46:40 +09:00
|
|
|
return {
|
|
|
|
|
isEnabled: true,
|
2025-12-07 20:58:44 +09:00
|
|
|
recordInputs: true,
|
2025-12-06 12:46:40 +09:00
|
|
|
recordOutputs: true,
|
|
|
|
|
metadata: {
|
|
|
|
|
sessionId: params.sessionId,
|
|
|
|
|
userId: params.userId,
|
|
|
|
|
},
|
|
|
|
|
}
|
2025-12-05 21:15:02 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Wrap a handler with Langfuse observe
|
|
|
|
|
export function wrapWithObserve<T>(
|
2025-12-06 12:46:40 +09:00
|
|
|
handler: (req: Request) => Promise<T>,
|
2025-12-05 21:15:02 +09:00
|
|
|
): (req: Request) => Promise<T> {
|
2025-12-06 12:46:40 +09:00
|
|
|
if (!isLangfuseEnabled()) {
|
|
|
|
|
return handler
|
|
|
|
|
}
|
2025-12-05 21:15:02 +09:00
|
|
|
|
2025-12-06 12:46:40 +09:00
|
|
|
return observe(handler, { name: "chat", endOnExit: false })
|
2025-12-05 21:15:02 +09:00
|
|
|
}
|