### Install Melony Package
Source: https://github.com/ddaras/melony/blob/main/packages/melony-core/README.md
This command installs the Melony npm package, which is the first step to integrating Melony into your project. It ensures all necessary files and dependencies are downloaded and available for use.
```bash
npm install melony
```
--------------------------------
### Melony Component Syntax Example (HTML)
Source: https://github.com/ddaras/melony/blob/main/packages/melony-core/README.md
This HTML example showcases a Melony Card component used for a user profile. It includes layout components like Row and Col, content components like Image and Text, and an interactive Button with an associated action, adhering to Melony's HTML-like syntax.
```html
```
--------------------------------
### Melony Component Syntax Example (HTML-like)
Source: https://github.com/ddaras/melony/blob/main/README.md
Illustrates Melony's HTML-like syntax for building user interfaces. This example shows a 'card' component containing 'row', 'image', 'col', 'text', and 'button' components, demonstrating layout and interactive elements. This syntax is used for both built-in and custom widgets.
```html
```
--------------------------------
### Interactive Documentation with MelonyMarkdown
Source: https://github.com/ddaras/melony/blob/main/SEPARATION-SUMMARY.md
Renders API documentation with embedded interactive examples using `MelonyMarkdown`. This example shows how markdown content can include custom HTML-like tags for elements such as 'card', 'text', and 'button', enabling rich, interactive documentation pages.
```tsx
{`
# API Documentation
Here's how to use our API:
`}
```
--------------------------------
### Quick Start: Define and Export Weather Widget
Source: https://github.com/ddaras/melony/blob/main/README.md
This TypeScript snippet defines a simplified 'weather-card' widget and exports it. This is part of the quick start guide, demonstrating the initial step of defining custom widgets.
```typescript
// widgets/weather-widget.ts
import { defineWidget } from "melony/builder";
import { z } from "zod";
export const weatherWidget = defineWidget({
type: "weather-card",
name: "Weather Card",
description: "Shows weather info for a city",
schema: z.object({
type: z.literal("weather-card"),
city: z.string(),
temperature: z.number(),
condition: z.string(),
}),
template: `
`.trim()
});
```
--------------------------------
### Configure AI with Melony UI Guide (TypeScript/JavaScript)
Source: https://github.com/ddaras/melony/blob/main/packages/melony-core/README.md
This code snippet demonstrates how to configure an AI model, specifically using OpenAI with the ai-sdk, to understand and generate Melony UI components. It involves importing the MELONY_UI_GUIDE system prompt and passing it to the streamText function.
```tsx
import { MELONY_UI_GUIDE } from "melony/server";
import { streamText } from "ai";
import { openai } from "@ai-sdk/openai";
export async function POST(req: Request) {
const { messages } = await req.json();
const result = streamText({
model: openai("gpt-4"),
system: MELONY_UI_GUIDE, // Teaches AI to use Melony components
messages,
});
return result.toDataStreamResponse();
}
```
--------------------------------
### Generate AI Training Prompts for Custom Widgets with Melony
Source: https://context7.com/ddaras/melony/llms.txt
The `generateWidgetSystemPrompt` function creates a detailed system prompt for LLMs, outlining available custom widgets, their schemas, examples, and usage patterns. This facilitates effective AI-driven widget utilization.
```typescript
import { generateWidgetSystemPrompt } from "melony/builder";
import { weatherWidget } from "./widgets/weather";
const customWidgets = [weatherWidget];
const systemPrompt = generateWidgetSystemPrompt(customWidgets);
// Returns formatted prompt:
// ## Custom Widgets
//
// The following custom widgets are available for use:
//
// ### weather-card Widget
// **Description:** Displays current weather information for a city
// **Props:**
// | Prop | Type | Required | Default | Description |
// |------|------|----------|---------|-------------|
// | `city` | `string` | ✓ | | City name |
// | `temperature` | `number` | ✓ | | Temperature in Fahrenheit |
// ...
```
--------------------------------
### generateWidgetSystemPrompt - Generate AI Training Prompts
Source: https://context7.com/ddaras/melony/llms.txt
This function generates a system prompt that can be used to train Large Language Models (LLMs). The prompt includes detailed information about available custom widgets, their schemas, and usage examples.
```APIDOC
## generateWidgetSystemPrompt - Generate AI Training Prompts
### Description
Generates a comprehensive system prompt that teaches LLMs about available custom widgets, including schemas, examples, and usage patterns.
### Method
`generateWidgetSystemPrompt(widgets: CompiledWidget[])`
### Parameters
- **widgets** (Array) - Required - An array of compiled widget definitions.
### Request Example
```typescript
import { generateWidgetSystemPrompt } from "melony/builder";
import { weatherWidget } from "./widgets/weather";
const customWidgets = [weatherWidget];
const systemPrompt = generateWidgetSystemPrompt(customWidgets);
```
### Response
Returns a formatted string representing the system prompt for AI training.
```
--------------------------------
### AI-Generated Widget Usage Example
Source: https://github.com/ddaras/melony/blob/main/README.md
Illustrates how an AI might generate a Melony widget in HTML-like syntax. This showcases the output of the AI composition process, where defined widgets are instantiated with specific properties.
```html
```
--------------------------------
### Example Melony Component Structure (HTML)
Source: https://github.com/ddaras/melony/blob/main/packages/melony-core/README.md
This HTML snippet illustrates a Melony Card component containing various elements like text, badges, and buttons. It demonstrates how to structure UI elements using Melony's HTML-like tags and how to define actions using the onClickAction prop.
```html
```
--------------------------------
### MelonyWidget Usage Example: ComponentDef Input (JSX)
Source: https://github.com/ddaras/melony/blob/main/SEPARATION-SUMMARY.md
Illustrates using the MelonyWidget component with a structured ComponentDef object. This provides a more programmatic way to define widget hierarchies and their properties.
```jsx
{{
component: "Card",
props: { title: "Hello" },
children: [{ component: "Text", props: { value: "World" } }]
}}
```
--------------------------------
### defineWidget - Create Type-Safe Custom Widgets
Source: https://context7.com/ddaras/melony/llms.txt
The `defineWidget` API allows developers to create type-safe widget definitions. These definitions include a Zod schema for validation, a template for rendering, and examples for AI prompt generation.
```APIDOC
## defineWidget - Create Type-Safe Custom Widgets
### Description
Creates a compiled widget definition with Zod schema validation, template rendering, and AI prompt generation. Returns a CompiledWidget that can be registered with MelonyProvider.
### Method
`defineWidget()`
### Parameters
This function accepts a single configuration object with the following properties:
- **type** (string) - Required - A unique identifier for the widget.
- **name** (string) - Required - A human-readable name for the widget.
- **description** (string) - Required - A description of the widget's purpose.
- **schema** (ZodObject) - Required - A Zod schema defining the widget's props and their types.
- **template** (string) - Required - An HTML-like template string for rendering the widget. Uses Handlebars syntax for dynamic values (e.g., `{{cityName}}`).
- **examples** (Array