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"
|
2026-10-12 12:35:55 +09:00
|
|
|
import { randomUUID } from "crypto"
|
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
|
|
|
|
2026-10-12 12:35:55 +09:00
|
|
|
type IngestionBatch = Parameters<
|
|
|
|
|
LangfuseClient["api"]["ingestion"]["batch"]
|
|
|
|
|
>[0]["batch"]
|
|
|
|
|
|
|
|
|
|
// Attach a score to the session's most recent chat trace. When the session
|
|
|
|
|
// has no trace yet, a fallbackTrace creates a standalone trace to carry the
|
|
|
|
|
// score; without one the score is skipped. Returns whether a score was written.
|
|
|
|
|
export async function scoreLatestTrace(
|
|
|
|
|
langfuse: LangfuseClient,
|
|
|
|
|
opts: {
|
|
|
|
|
sessionId: string
|
|
|
|
|
name: string
|
|
|
|
|
value: number
|
|
|
|
|
comment: string
|
|
|
|
|
fallbackTrace?: {
|
|
|
|
|
name: string
|
|
|
|
|
userId: string
|
|
|
|
|
input: unknown
|
|
|
|
|
metadata: Record<string, unknown>
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
): Promise<boolean> {
|
|
|
|
|
const { sessionId, name, value, comment, fallbackTrace } = opts
|
|
|
|
|
const traces = await langfuse.api.trace.list({ sessionId, limit: 1 })
|
|
|
|
|
let traceId = traces.data?.[0]?.id
|
|
|
|
|
const timestamp = new Date().toISOString()
|
|
|
|
|
const batch: IngestionBatch = []
|
|
|
|
|
|
|
|
|
|
if (!traceId) {
|
|
|
|
|
if (!fallbackTrace) return false
|
|
|
|
|
traceId = randomUUID()
|
|
|
|
|
batch.push({
|
|
|
|
|
type: "trace-create",
|
|
|
|
|
id: randomUUID(),
|
|
|
|
|
timestamp,
|
|
|
|
|
body: { id: traceId, sessionId, ...fallbackTrace, timestamp },
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
batch.push({
|
|
|
|
|
type: "score-create",
|
|
|
|
|
id: randomUUID(),
|
|
|
|
|
timestamp,
|
|
|
|
|
body: { id: randomUUID(), traceId, name, value, comment },
|
|
|
|
|
})
|
|
|
|
|
await langfuse.api.ingestion.batch({ batch })
|
|
|
|
|
return true
|
|
|
|
|
}
|
|
|
|
|
|
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)
|
2026-10-12 12:35:55 +09:00
|
|
|
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 })
|
2026-10-05 22:39:32 +09:00
|
|
|
endTrace()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// End the observe() wrapper span (AI SDK creates its own child spans with usage).
|
|
|
|
|
// It uses endOnExit: false, so every request path has to end it, or the trace
|
|
|
|
|
// is never exported: stream finish, stream error/abort, and early returns.
|
|
|
|
|
export function endTrace() {
|
|
|
|
|
if (!isLangfuseEnabled()) return
|
2025-12-05 21:15:02 +09:00
|
|
|
|
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
|
|
|
}
|