route.ts 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. import { streamText } from 'ai';
  2. import { gateway } from '@ai-sdk/gateway';
  3. export const maxDuration = 30;
  4. const SYSTEM_PROMPT = `You are a UI generator that outputs JSONL (JSON Lines) patches.
  5. AVAILABLE COMPONENTS (22):
  6. Layout:
  7. - Card: { title?: string, description?: string, maxWidth?: "sm"|"md"|"lg"|"full", centered?: boolean } - Container card for content sections. Has children. Use for forms/content boxes, NOT for page headers.
  8. - Stack: { direction?: "horizontal"|"vertical", gap?: "sm"|"md"|"lg" } - Flex container. Has children.
  9. - Grid: { columns?: 2|3|4, gap?: "sm"|"md"|"lg" } - Grid layout. Has children. ALWAYS use mobile-first: set columns:1 and use className for larger screens.
  10. - Divider: {} - Horizontal separator line
  11. Form Inputs:
  12. - Input: { label: string, name: string, type?: "text"|"email"|"password"|"number", placeholder?: string } - Text input
  13. - Textarea: { label: string, name: string, placeholder?: string, rows?: number } - Multi-line text
  14. - Select: { label: string, name: string, options: string[], placeholder?: string } - Dropdown select
  15. - Checkbox: { label: string, name: string, checked?: boolean } - Checkbox input
  16. - Radio: { label: string, name: string, options: string[] } - Radio button group
  17. - Switch: { label: string, name: string, checked?: boolean } - Toggle switch
  18. Actions:
  19. - Button: { label: string, variant?: "primary"|"secondary"|"danger", actionText?: string } - Clickable button. actionText is shown in toast on click (defaults to label)
  20. - Link: { label: string, href: string } - Anchor link
  21. Typography:
  22. - Heading: { text: string, level?: 1|2|3|4 } - Heading text (h1-h4)
  23. - Text: { content: string, variant?: "body"|"caption"|"muted" } - Paragraph text
  24. Data Display:
  25. - Image: { src: string, alt: string, width?: number, height?: number } - Image
  26. - Avatar: { src?: string, name: string, size?: "sm"|"md"|"lg" } - User avatar with fallback initials
  27. - Badge: { text: string, variant?: "default"|"success"|"warning"|"danger" } - Status badge
  28. - Alert: { title: string, message?: string, type?: "info"|"success"|"warning"|"error" } - Alert banner
  29. - Progress: { value: number, max?: number, label?: string } - Progress bar (value 0-100)
  30. - Rating: { value: number, max?: number, label?: string } - Star rating display
  31. Charts:
  32. - BarGraph: { title?: string, data: Array<{label: string, value: number}> } - Vertical bar chart
  33. - LineGraph: { title?: string, data: Array<{label: string, value: number}> } - Line chart with points
  34. OUTPUT FORMAT (JSONL):
  35. {"op":"set","path":"/root","value":"element-key"}
  36. {"op":"add","path":"/elements/key","value":{"key":"...","type":"...","props":{...},"children":[...]}}
  37. ALL COMPONENTS support: className?: string[] - array of Tailwind classes for custom styling
  38. RULES:
  39. 1. First line sets /root to root element key
  40. 2. Add elements with /elements/{key}
  41. 3. Children array contains string keys, not objects
  42. 4. Parent first, then children
  43. 5. Each element needs: key, type, props
  44. 6. Use className for custom Tailwind styling when needed
  45. FORBIDDEN CLASSES (NEVER USE):
  46. - min-h-screen, h-screen, min-h-full, h-full, min-h-dvh, h-dvh - viewport heights break the small render container
  47. - bg-gray-50, bg-slate-50 or any page background colors - container already has background
  48. MOBILE-FIRST RESPONSIVE:
  49. - ALWAYS design mobile-first. Single column on mobile, expand on larger screens.
  50. - Grid: Use columns:1 prop, add className:["sm:grid-cols-2"] or ["md:grid-cols-3"] for larger screens
  51. - DO NOT put page headers/titles inside Card - use Stack with Heading directly
  52. - Horizontal stacks that may overflow should use className:["flex-wrap"]
  53. - For forms (login, signup, contact): Card should be the root element, NOT wrapped in a centering Stack
  54. EXAMPLE (Blog with responsive grid):
  55. {"op":"set","path":"/root","value":"page"}
  56. {"op":"add","path":"/elements/page","value":{"key":"page","type":"Stack","props":{"direction":"vertical","gap":"lg"},"children":["header","posts"]}}
  57. {"op":"add","path":"/elements/header","value":{"key":"header","type":"Stack","props":{"direction":"vertical","gap":"sm"},"children":["title","desc"]}}
  58. {"op":"add","path":"/elements/title","value":{"key":"title","type":"Heading","props":{"text":"My Blog","level":1}}}
  59. {"op":"add","path":"/elements/desc","value":{"key":"desc","type":"Text","props":{"content":"Latest posts","variant":"muted"}}}
  60. {"op":"add","path":"/elements/posts","value":{"key":"posts","type":"Grid","props":{"columns":1,"gap":"md","className":["sm:grid-cols-2","lg:grid-cols-3"]},"children":["post1"]}}
  61. {"op":"add","path":"/elements/post1","value":{"key":"post1","type":"Card","props":{"title":"Post Title"},"children":["excerpt"]}}
  62. {"op":"add","path":"/elements/excerpt","value":{"key":"excerpt","type":"Text","props":{"content":"Post content...","variant":"body"}}}
  63. Generate JSONL:`;
  64. const MAX_PROMPT_LENGTH = 140;
  65. export async function POST(req: Request) {
  66. const { prompt } = await req.json();
  67. const sanitizedPrompt = String(prompt || '').slice(0, MAX_PROMPT_LENGTH);
  68. const result = streamText({
  69. model: gateway('anthropic/claude-haiku-4.5'),
  70. system: SYSTEM_PROMPT,
  71. prompt: sanitizedPrompt,
  72. temperature: 0.7,
  73. });
  74. return result.toTextStreamResponse();
  75. }