mirror of
https://github.com/DayuanJiang/next-ai-draw-io.git
synced 2026-09-01 17:10:24 +08:00
* [Feature] Add VLM-based diagram validation Add automatic VLM (Vision Language Model) validation after display_diagram tool execution. The system captures a screenshot of the rendered diagram, sends it to a VLM for visual analysis, and uses feedback to improve diagram quality through the existing retry mechanism. Changes: - Add /api/validate-diagram endpoint for VLM validation - Add diagram-validator.ts for client-side validation orchestration - Add validation-prompts.ts for VLM system prompts - Add ValidationCard component to display validation status in chat - Add PNG capture functionality to diagram context - Integrate validation into tool handlers with retry support (max 3) - Add "Improve with Suggestions" button for manual regeneration - Add settings toggle to enable/disable VLM validation - Add getValidationModel() helper in ai-providers.ts * refactor(validation): use AI SDK structured outputs and address review feedback - Replace generateText + manual JSON parsing with generateObject and Zod schema for type-safe structured validation output - Use AbortSignal.timeout() instead of Promise.race for cleaner timeout handling - Add timeout validation with minimum 1000ms to handle malformed env values - Remove unused xml parameter from validateRenderedDiagram API - Remove parseValidationResponse function (now handled by schema) - Clear validationStates on session switch and new chat to prevent memory leak - Update 100ms render delay comment to clarify best-effort heuristic - Remove unused useEffect import from ValidationCard - Fix optional chaining lint warning in ValidationCard - Add unit tests for formatValidationFeedback function * refactor(validation): use AI SDK experimental_useObject hook instead of raw fetch - Change API endpoint from generateObject to streamObject for useObject compatibility - Create useValidateDiagram hook using AI SDK's experimental_useObject for reactive validation - Update useDiagramToolHandlers to accept validation function as parameter - Update chat-panel to use new useValidateDiagram hook - Remove validateRenderedDiagram function from lib/diagram-validator.ts (now in hook) - Export ValidationResultSchema from API route for client-side use * fix(validation): extract schema to shared file for client/server compatibility Move ValidationResultSchema to lib/validation-schema.ts to avoid importing server-side modules (ai-providers) into client-side code. This fixes the Turbopack build error caused by the hook importing from the API route. * fix(validation): use 'Valid' instead of 'Complete' for validation success Change ValidationCard success label from 'Complete' to 'Valid' to avoid conflicting with ToolCallCard's 'Complete' badge in E2E tests. This fixes the diagram-generation E2E test that expects a specific count of 'Complete' badges. * fix(validation): add aria-hidden to icons to prevent duplicate ID warning * fix: improve VLM validation with bug fixes and i18n - Fix race condition in pendingValidationRef (reject previous pending validation) - Fix response format consistency (use streaming for all responses) - Remove dead code (unused lastRequestRef and ValidationRequest interface) - Consolidate duplicate types (re-export from validation-schema.ts) - Add 'success_with_warnings' status for valid diagrams with warnings - Fix tool card auto-collapse (only collapse once, respect user toggle) - Set VLM validation default to disabled - Add i18n support for diagram validation settings (en/zh/ja) - Mark feature as experimental in settings UI * fix: resolve TypeScript errors in electron-standalone - Add forwardRef support to ChatInput component with ChatInputRef type - Copy electron.d.ts to electron-standalone/electron folder - Exclude electron-standalone from root tsconfig type checking * fix: return empty string for valid result with no issues in formatValidationFeedback * feat(i18n): add validation strings for ValidationCard component - Add validation section to en.json, zh.json, ja.json dictionaries - Update ValidationCard to use useDictionary hook - Replace all hardcoded English strings with i18n keys --------- Co-authored-by: dayuan.jiang <jdy.toh@gmail.com>
65 lines
1.9 KiB
TypeScript
65 lines
1.9 KiB
TypeScript
/**
|
|
* Types and utilities for VLM-based diagram validation.
|
|
* The actual validation is performed via useValidateDiagram hook using AI SDK's useObject.
|
|
*/
|
|
|
|
// Re-export types from the schema file (single source of truth)
|
|
export type { ValidationIssue, ValidationResult } from "./validation-schema"
|
|
|
|
import type { ValidationResult } from "./validation-schema"
|
|
|
|
/**
|
|
* Format validation feedback for display to the AI model.
|
|
* This creates a human-readable error message that guides the AI to fix issues.
|
|
*
|
|
* @param result - The validation result from VLM
|
|
* @returns Formatted string for tool error output
|
|
*/
|
|
export function formatValidationFeedback(result: ValidationResult): string {
|
|
// If validation passed with no issues, return empty string
|
|
if (result.valid && result.issues.length === 0) {
|
|
return ""
|
|
}
|
|
|
|
const lines: string[] = []
|
|
|
|
lines.push("DIAGRAM VISUAL VALIDATION FAILED")
|
|
lines.push("")
|
|
|
|
// Group issues by severity
|
|
const criticalIssues = result.issues.filter(
|
|
(i) => i.severity === "critical",
|
|
)
|
|
const warnings = result.issues.filter((i) => i.severity === "warning")
|
|
|
|
if (criticalIssues.length > 0) {
|
|
lines.push("Critical Issues (must fix):")
|
|
for (const issue of criticalIssues) {
|
|
lines.push(` - [${issue.type}] ${issue.description}`)
|
|
}
|
|
lines.push("")
|
|
}
|
|
|
|
if (warnings.length > 0) {
|
|
lines.push("Warnings:")
|
|
for (const issue of warnings) {
|
|
lines.push(` - [${issue.type}] ${issue.description}`)
|
|
}
|
|
lines.push("")
|
|
}
|
|
|
|
if (result.suggestions.length > 0) {
|
|
lines.push("Suggestions to fix:")
|
|
for (const suggestion of result.suggestions) {
|
|
lines.push(` - ${suggestion}`)
|
|
}
|
|
lines.push("")
|
|
}
|
|
|
|
lines.push(
|
|
"Please regenerate the diagram with corrected layout to fix these visual issues.",
|
|
)
|
|
|
|
return lines.join("\n")
|
|
}
|