mirror of
https://github.com/DayuanJiang/next-ai-draw-io.git
synced 2026-10-09 03:07:46 +08:00
- useChat throttles streamed message updates (experimental_throttle, 150 ms), replacing the two hand-written 150 ms timers of the display_diagram and edit_diagram previews (94 lines less). The preview now only runs while the input streams; once it is complete the tool handler takes over, so a queued preview can no longer redraw an edit the handler rejected and rolled back. Measured on a streamed 60-cell diagram: 41 redraws at least 97 ms apart, before 37 with gaps down to 48 ms - The diagram check endpoint uses streamText with Output.object instead of the deprecated streamObject, and returns its fixed result as a plain text response; new route test - Import createGateway/gateway from ai and drop the direct @ai-sdk/gateway dependency - The per-request message structure logs only print with DEBUG_LLM_PAYLOAD=true - Remove an empty onFinish callback
141 lines
4.5 KiB
TypeScript
141 lines
4.5 KiB
TypeScript
/**
|
|
* API endpoint for VLM-based diagram validation.
|
|
* Accepts a PNG image and streams validation results using useObject-compatible format.
|
|
*/
|
|
|
|
import { Output, streamText } from "ai"
|
|
import { checkAccessCode } from "@/lib/access-code"
|
|
import { getValidationModel } from "@/lib/ai-providers"
|
|
import { VALIDATION_SYSTEM_PROMPT } from "@/lib/validation-prompts"
|
|
import {
|
|
type ValidationResult,
|
|
ValidationResultSchema,
|
|
} from "@/lib/validation-schema"
|
|
|
|
export const maxDuration = 30
|
|
|
|
// Data URL length cap (~3.75 MB of PNG), well above a normal diagram capture
|
|
const MAX_IMAGE_DATA_LENGTH = 5 * 1024 * 1024
|
|
|
|
interface ValidateDiagramRequest {
|
|
imageData: string // Base64 PNG data URL
|
|
sessionId?: string
|
|
}
|
|
|
|
// Default valid result for disabled/error cases
|
|
const DEFAULT_VALID_RESULT: ValidationResult = {
|
|
valid: true,
|
|
issues: [],
|
|
suggestions: [],
|
|
}
|
|
|
|
/** A fixed result in the text format useObject reads */
|
|
function createStreamingResponse(result: ValidationResult): Response {
|
|
return new Response(JSON.stringify(result), {
|
|
headers: { "Content-Type": "text/plain; charset=utf-8" },
|
|
})
|
|
}
|
|
|
|
export async function POST(req: Request): Promise<Response> {
|
|
// Uses the server's model credentials, so require the access code
|
|
const accessError = checkAccessCode(req)
|
|
if (accessError) return accessError
|
|
|
|
try {
|
|
// Check if VLM validation is enabled (default: true)
|
|
const enableValidation = process.env.ENABLE_VLM_VALIDATION !== "false"
|
|
if (!enableValidation) {
|
|
return createStreamingResponse(DEFAULT_VALID_RESULT)
|
|
}
|
|
|
|
const body: ValidateDiagramRequest = await req.json()
|
|
const { imageData, sessionId } = body
|
|
|
|
if (!imageData) {
|
|
return Response.json(
|
|
{ error: "Missing imageData" },
|
|
{ status: 400 },
|
|
)
|
|
}
|
|
|
|
// Validate image data format
|
|
if (
|
|
!imageData.startsWith("data:image/png;base64,") &&
|
|
!imageData.startsWith("data:image/")
|
|
) {
|
|
return Response.json(
|
|
{ error: "Invalid image data format" },
|
|
{ status: 400 },
|
|
)
|
|
}
|
|
|
|
if (imageData.length > MAX_IMAGE_DATA_LENGTH) {
|
|
return Response.json(
|
|
{ error: "Image data too large" },
|
|
{ status: 413 },
|
|
)
|
|
}
|
|
|
|
// Get the validation model
|
|
let model
|
|
try {
|
|
model = getValidationModel()
|
|
} catch (error) {
|
|
console.warn(
|
|
"[validate-diagram] Validation model not available:",
|
|
error,
|
|
)
|
|
// Return valid if no vision model is configured
|
|
return createStreamingResponse(DEFAULT_VALID_RESULT)
|
|
}
|
|
|
|
// Parse timeout with validation (minimum 1000ms, default 10000ms)
|
|
const timeout =
|
|
Math.max(
|
|
1000,
|
|
parseInt(process.env.VALIDATION_TIMEOUT || "10000", 10),
|
|
) || 10000
|
|
|
|
// Stream the VLM response for useObject consumption
|
|
const result = streamText({
|
|
model,
|
|
output: Output.object({ schema: ValidationResultSchema }),
|
|
system: VALIDATION_SYSTEM_PROMPT,
|
|
messages: [
|
|
{
|
|
role: "user",
|
|
content: [
|
|
{
|
|
type: "image",
|
|
image: imageData,
|
|
},
|
|
{
|
|
type: "text",
|
|
text: "Please analyze this diagram for visual quality issues.",
|
|
},
|
|
],
|
|
},
|
|
],
|
|
maxOutputTokens: 1024,
|
|
abortSignal: AbortSignal.timeout(timeout),
|
|
onFinish: ({ output }) => {
|
|
if (sessionId && output) {
|
|
console.log(
|
|
`[validate-diagram] Session ${sessionId}: valid=${output.valid}, issues=${output.issues?.length ?? 0}`,
|
|
)
|
|
}
|
|
},
|
|
})
|
|
|
|
return result.toTextStreamResponse()
|
|
} catch (error) {
|
|
// Log with session context if available
|
|
const errorMessage =
|
|
error instanceof Error ? error.message : String(error)
|
|
console.error("[validate-diagram] Error:", errorMessage)
|
|
|
|
// On error, return valid to not block the user
|
|
return createStreamingResponse(DEFAULT_VALID_RESULT)
|
|
}
|
|
}
|