page.mdx 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. export const metadata = { title: "Catalog" }
  2. # Catalog
  3. The catalog defines what AI can generate. It's your guardrail.
  4. ## What is a Catalog?
  5. A catalog is a schema that defines:
  6. - **Components** — UI elements AI can create (with props and optional slots)
  7. - **Actions** — Operations AI can trigger
  8. - **Functions** — Custom validation or transformation functions
  9. ## Creating a Catalog
  10. ```typescript
  11. import { defineCatalog } from '@json-render/core';
  12. import { schema } from '@json-render/react';
  13. import { z } from 'zod';
  14. const catalog = defineCatalog(schema, {
  15. components: {
  16. // Define each component with its props schema
  17. Card: {
  18. props: z.object({
  19. title: z.string(),
  20. description: z.string().nullable(),
  21. padding: z.enum(['sm', 'md', 'lg']).nullable(),
  22. }),
  23. slots: ["default"], // Can contain other components
  24. description: "Container card for grouping content",
  25. },
  26. Metric: {
  27. props: z.object({
  28. label: z.string(),
  29. valuePath: z.string(), // JSON Pointer to data
  30. format: z.enum(['currency', 'percent', 'number']),
  31. }),
  32. description: "Display a single metric value",
  33. },
  34. },
  35. actions: {
  36. submit_form: {
  37. params: z.object({
  38. formId: z.string(),
  39. }),
  40. description: 'Submit a form',
  41. },
  42. export_data: {
  43. params: z.object({
  44. format: z.enum(['csv', 'pdf', 'json']),
  45. }),
  46. description: 'Export data in various formats',
  47. },
  48. },
  49. });
  50. ```
  51. ## Component Definition
  52. Each component in the catalog has:
  53. ```typescript
  54. {
  55. props: z.object({...}), // Zod schema for props (use .nullable() for optional)
  56. slots?: string[], // Named slots for children (e.g., ["default"])
  57. description?: string, // Help AI understand when to use it
  58. }
  59. ```
  60. Use `slots: ["default"]` for components that can contain children. The slot name corresponds to where child elements are rendered.
  61. ## Generating AI Prompts
  62. Use the `catalog.prompt()` method to generate a system prompt for AI:
  63. ```typescript
  64. // Generate a system prompt from your catalog
  65. const systemPrompt = catalog.prompt();
  66. // Or with custom rules for the AI
  67. const customPrompt = catalog.prompt({
  68. customRules: [
  69. "Always use Card as the root element for forms",
  70. "Group related inputs in a Stack with direction=vertical",
  71. ],
  72. });
  73. // Pass this to your AI model as the system prompt
  74. ```
  75. ## Next
  76. Learn how to [register components](/docs/registry) in your registry.