mirror of
https://github.com/DayuanJiang/next-ai-draw-io.git
synced 2026-01-02 14:22:28 +08:00
refactor: add input validation and singleton pattern for Langfuse API routes
- Add Zod schema validation for log-feedback and log-save endpoints - Create singleton LangfuseClient to avoid per-request instantiation - Simplify log-save to only flag trace (no XML content sent) - Use generic error messages to prevent info leakage
This commit is contained in:
@@ -1,26 +1,34 @@
|
|||||||
import { LangfuseClient } from '@langfuse/client';
|
import { getLangfuseClient } from '@/lib/langfuse';
|
||||||
import { randomUUID } from 'crypto';
|
import { randomUUID } from 'crypto';
|
||||||
|
import { z } from 'zod';
|
||||||
|
|
||||||
|
const feedbackSchema = z.object({
|
||||||
|
messageId: z.string().min(1).max(200),
|
||||||
|
feedback: z.enum(['good', 'bad']),
|
||||||
|
sessionId: z.string().min(1).max(200).optional(),
|
||||||
|
});
|
||||||
|
|
||||||
export async function POST(req: Request) {
|
export async function POST(req: Request) {
|
||||||
// Check if Langfuse is configured
|
const langfuse = getLangfuseClient();
|
||||||
if (!process.env.LANGFUSE_PUBLIC_KEY || !process.env.LANGFUSE_SECRET_KEY) {
|
if (!langfuse) {
|
||||||
return Response.json({ success: true, logged: false });
|
return Response.json({ success: true, logged: false });
|
||||||
}
|
}
|
||||||
|
|
||||||
const { messageId, feedback, sessionId } = await req.json();
|
// Validate input
|
||||||
|
let data;
|
||||||
|
try {
|
||||||
|
data = feedbackSchema.parse(await req.json());
|
||||||
|
} catch {
|
||||||
|
return Response.json({ success: false, error: 'Invalid input' }, { status: 400 });
|
||||||
|
}
|
||||||
|
|
||||||
|
const { messageId, feedback, sessionId } = data;
|
||||||
|
|
||||||
// Get user IP for tracking
|
// Get user IP for tracking
|
||||||
const forwardedFor = req.headers.get('x-forwarded-for');
|
const forwardedFor = req.headers.get('x-forwarded-for');
|
||||||
const userId = forwardedFor?.split(',')[0]?.trim() || 'anonymous';
|
const userId = forwardedFor?.split(',')[0]?.trim() || 'anonymous';
|
||||||
|
|
||||||
try {
|
try {
|
||||||
// Create Langfuse client
|
|
||||||
const langfuse = new LangfuseClient({
|
|
||||||
publicKey: process.env.LANGFUSE_PUBLIC_KEY,
|
|
||||||
secretKey: process.env.LANGFUSE_SECRET_KEY,
|
|
||||||
baseUrl: process.env.LANGFUSE_BASEURL,
|
|
||||||
});
|
|
||||||
|
|
||||||
// Find the most recent chat trace for this session to attach the score to
|
// Find the most recent chat trace for this session to attach the score to
|
||||||
const tracesResponse = await langfuse.api.trace.list({
|
const tracesResponse = await langfuse.api.trace.list({
|
||||||
sessionId,
|
sessionId,
|
||||||
@@ -90,6 +98,6 @@ export async function POST(req: Request) {
|
|||||||
return Response.json({ success: true, logged: true });
|
return Response.json({ success: true, logged: true });
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error('Langfuse feedback error:', error);
|
console.error('Langfuse feedback error:', error);
|
||||||
return Response.json({ success: false, error: String(error) }, { status: 500 });
|
return Response.json({ success: false, error: 'Failed to log feedback' }, { status: 500 });
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,29 +1,33 @@
|
|||||||
import { LangfuseClient } from '@langfuse/client';
|
import { getLangfuseClient } from '@/lib/langfuse';
|
||||||
import { randomUUID } from 'crypto';
|
import { randomUUID } from 'crypto';
|
||||||
|
import { z } from 'zod';
|
||||||
|
|
||||||
|
const saveSchema = z.object({
|
||||||
|
filename: z.string().min(1).max(255),
|
||||||
|
format: z.enum(['drawio', 'png', 'svg']),
|
||||||
|
sessionId: z.string().min(1).max(200).optional(),
|
||||||
|
});
|
||||||
|
|
||||||
export async function POST(req: Request) {
|
export async function POST(req: Request) {
|
||||||
// Check if Langfuse is configured
|
const langfuse = getLangfuseClient();
|
||||||
if (!process.env.LANGFUSE_PUBLIC_KEY || !process.env.LANGFUSE_SECRET_KEY) {
|
if (!langfuse) {
|
||||||
return Response.json({ success: true, logged: false });
|
return Response.json({ success: true, logged: false });
|
||||||
}
|
}
|
||||||
|
|
||||||
const { xml, filename, format, sessionId } = await req.json();
|
// Validate input
|
||||||
|
let data;
|
||||||
|
try {
|
||||||
|
data = saveSchema.parse(await req.json());
|
||||||
|
} catch {
|
||||||
|
return Response.json({ success: false, error: 'Invalid input' }, { status: 400 });
|
||||||
|
}
|
||||||
|
|
||||||
// Get user IP for tracking
|
const { filename, format, sessionId } = data;
|
||||||
const forwardedFor = req.headers.get('x-forwarded-for');
|
|
||||||
const userId = forwardedFor?.split(',')[0]?.trim() || 'anonymous';
|
|
||||||
|
|
||||||
try {
|
try {
|
||||||
// Create Langfuse client
|
|
||||||
const langfuse = new LangfuseClient({
|
|
||||||
publicKey: process.env.LANGFUSE_PUBLIC_KEY,
|
|
||||||
secretKey: process.env.LANGFUSE_SECRET_KEY,
|
|
||||||
baseUrl: process.env.LANGFUSE_BASEURL,
|
|
||||||
});
|
|
||||||
|
|
||||||
const timestamp = new Date().toISOString();
|
const timestamp = new Date().toISOString();
|
||||||
|
|
||||||
// Find the most recent chat trace for this session to attach the save event to
|
// Find the most recent chat trace for this session to attach the save flag
|
||||||
const tracesResponse = await langfuse.api.trace.list({
|
const tracesResponse = await langfuse.api.trace.list({
|
||||||
sessionId,
|
sessionId,
|
||||||
limit: 1,
|
limit: 1,
|
||||||
@@ -33,54 +37,29 @@ export async function POST(req: Request) {
|
|||||||
const latestTrace = traces[0];
|
const latestTrace = traces[0];
|
||||||
|
|
||||||
if (latestTrace) {
|
if (latestTrace) {
|
||||||
// Create a span on the existing chat trace for the save event
|
// Add a score to the existing trace to flag that user saved
|
||||||
await langfuse.api.ingestion.batch({
|
await langfuse.api.ingestion.batch({
|
||||||
batch: [
|
batch: [
|
||||||
{
|
{
|
||||||
type: 'span-create',
|
type: 'score-create',
|
||||||
id: randomUUID(),
|
id: randomUUID(),
|
||||||
timestamp,
|
timestamp,
|
||||||
body: {
|
body: {
|
||||||
id: randomUUID(),
|
id: randomUUID(),
|
||||||
traceId: latestTrace.id,
|
traceId: latestTrace.id,
|
||||||
name: 'diagram-save',
|
name: 'diagram-saved',
|
||||||
input: { filename, format },
|
value: 1,
|
||||||
output: { xmlPreview: xml?.substring(0, 500), contentLength: xml?.length || 0 },
|
comment: `User saved diagram as ${filename}.${format}`,
|
||||||
metadata: { source: 'save-button' },
|
|
||||||
startTime: timestamp,
|
|
||||||
endTime: timestamp,
|
|
||||||
},
|
|
||||||
},
|
|
||||||
],
|
|
||||||
});
|
|
||||||
} else {
|
|
||||||
// No trace found - create a standalone trace
|
|
||||||
const traceId = randomUUID();
|
|
||||||
|
|
||||||
await langfuse.api.ingestion.batch({
|
|
||||||
batch: [
|
|
||||||
{
|
|
||||||
type: 'trace-create',
|
|
||||||
id: randomUUID(),
|
|
||||||
timestamp,
|
|
||||||
body: {
|
|
||||||
id: traceId,
|
|
||||||
name: 'diagram-save',
|
|
||||||
sessionId,
|
|
||||||
userId,
|
|
||||||
input: { filename, format },
|
|
||||||
output: { xmlPreview: xml?.substring(0, 500), contentLength: xml?.length || 0 },
|
|
||||||
metadata: { source: 'save-button', note: 'standalone - no chat trace found' },
|
|
||||||
timestamp,
|
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
],
|
],
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
// If no trace found, skip logging (user hasn't chatted yet)
|
||||||
|
|
||||||
return Response.json({ success: true, logged: true });
|
return Response.json({ success: true, logged: !!latestTrace });
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error('Langfuse save error:', error);
|
console.error('Langfuse save error:', error);
|
||||||
return Response.json({ success: false, error: String(error) }, { status: 500 });
|
return Response.json({ success: false, error: 'Failed to log save' }, { status: 500 });
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -133,23 +133,21 @@ export function DiagramProvider({ children }: { children: React.ReactNode }) {
|
|||||||
fileContent = xmlContent;
|
fileContent = xmlContent;
|
||||||
mimeType = "application/xml";
|
mimeType = "application/xml";
|
||||||
extension = ".drawio";
|
extension = ".drawio";
|
||||||
|
|
||||||
// Log XML to Langfuse
|
|
||||||
logSaveToLangfuse(xmlContent, filename, format, sessionId);
|
|
||||||
} else if (format === "png") {
|
} else if (format === "png") {
|
||||||
// PNG data comes as base64 data URL
|
// PNG data comes as base64 data URL
|
||||||
fileContent = exportData;
|
fileContent = exportData;
|
||||||
mimeType = "image/png";
|
mimeType = "image/png";
|
||||||
extension = ".png";
|
extension = ".png";
|
||||||
logSaveToLangfuse(exportData, filename, format, sessionId);
|
|
||||||
} else {
|
} else {
|
||||||
// SVG format
|
// SVG format
|
||||||
fileContent = exportData;
|
fileContent = exportData;
|
||||||
mimeType = "image/svg+xml";
|
mimeType = "image/svg+xml";
|
||||||
extension = ".svg";
|
extension = ".svg";
|
||||||
logSaveToLangfuse(exportData, filename, format, sessionId);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Log save event to Langfuse (flags the trace)
|
||||||
|
logSaveToLangfuse(filename, format, sessionId);
|
||||||
|
|
||||||
// Handle download
|
// Handle download
|
||||||
let url: string;
|
let url: string;
|
||||||
if (typeof fileContent === "string" && fileContent.startsWith("data:")) {
|
if (typeof fileContent === "string" && fileContent.startsWith("data:")) {
|
||||||
@@ -179,13 +177,13 @@ export function DiagramProvider({ children }: { children: React.ReactNode }) {
|
|||||||
drawioRef.current.exportDiagram({ format: drawioFormat });
|
drawioRef.current.exportDiagram({ format: drawioFormat });
|
||||||
};
|
};
|
||||||
|
|
||||||
// Log save event to Langfuse
|
// Log save event to Langfuse (just flags the trace, doesn't send content)
|
||||||
const logSaveToLangfuse = async (content: string, filename: string, format: string, sessionId?: string) => {
|
const logSaveToLangfuse = async (filename: string, format: string, sessionId?: string) => {
|
||||||
try {
|
try {
|
||||||
await fetch("/api/log-save", {
|
await fetch("/api/log-save", {
|
||||||
method: "POST",
|
method: "POST",
|
||||||
headers: { "Content-Type": "application/json" },
|
headers: { "Content-Type": "application/json" },
|
||||||
body: JSON.stringify({ xml: content, filename, format, sessionId }),
|
body: JSON.stringify({ filename, format, sessionId }),
|
||||||
});
|
});
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.warn("Failed to log save to Langfuse:", error);
|
console.warn("Failed to log save to Langfuse:", error);
|
||||||
|
|||||||
@@ -1,6 +1,26 @@
|
|||||||
import { observe, updateActiveTrace } from '@langfuse/tracing';
|
import { observe, updateActiveTrace } from '@langfuse/tracing';
|
||||||
|
import { LangfuseClient } from '@langfuse/client';
|
||||||
import * as api from '@opentelemetry/api';
|
import * as api from '@opentelemetry/api';
|
||||||
|
|
||||||
|
// Singleton LangfuseClient instance for direct API calls
|
||||||
|
let langfuseClient: LangfuseClient | null = null;
|
||||||
|
|
||||||
|
export function getLangfuseClient(): LangfuseClient | null {
|
||||||
|
if (!process.env.LANGFUSE_PUBLIC_KEY || !process.env.LANGFUSE_SECRET_KEY) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!langfuseClient) {
|
||||||
|
langfuseClient = new LangfuseClient({
|
||||||
|
publicKey: process.env.LANGFUSE_PUBLIC_KEY,
|
||||||
|
secretKey: process.env.LANGFUSE_SECRET_KEY,
|
||||||
|
baseUrl: process.env.LANGFUSE_BASEURL,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
return langfuseClient;
|
||||||
|
}
|
||||||
|
|
||||||
// Check if Langfuse is configured
|
// Check if Langfuse is configured
|
||||||
export function isLangfuseEnabled(): boolean {
|
export function isLangfuseEnabled(): boolean {
|
||||||
return !!process.env.LANGFUSE_PUBLIC_KEY;
|
return !!process.env.LANGFUSE_PUBLIC_KEY;
|
||||||
|
|||||||
Reference in New Issue
Block a user