Skip to content

Commit 003dfe7

Browse files
committed
Switch to single ai elements skill with many references
1 parent 108ae63 commit 003dfe7

File tree

133 files changed

+518
-352
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

133 files changed

+518
-352
lines changed

.github/workflows/generate-skills.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ on:
55
branches:
66
- main
77
paths:
8-
- 'apps/docs/content/docs/components/**/*.mdx'
8+
- 'apps/docs/content/docs/**/*.mdx'
99
- 'packages/examples/src/**/*.tsx'
1010
- 'packages/scripts/**'
1111

packages/scripts/src/generate-skills.ts

Lines changed: 102 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,11 @@ import { basename, join } from 'node:path';
44
import matter from 'gray-matter';
55

66
const ROOT_DIR = join(import.meta.dirname, '../../..');
7-
const DOCS_DIR = join(ROOT_DIR, 'apps/docs/content/docs/components');
7+
const DOCS_DIR = join(ROOT_DIR, 'apps/docs/content/docs');
8+
const COMPONENTS_DIR = join(DOCS_DIR, 'components');
89
const EXAMPLES_DIR = join(ROOT_DIR, 'packages/examples/src');
910
const SKILLS_DIR = join(ROOT_DIR, 'skills');
11+
const SKILL_DIR = join(SKILLS_DIR, 'ai-elements');
1012

1113
const discoverMdxFiles = async (dir: string): Promise<string[]> => {
1214
const results: string[] = [];
@@ -15,7 +17,7 @@ const discoverMdxFiles = async (dir: string): Promise<string[]> => {
1517
for (const entry of entries) {
1618
const fullPath = join(dir, entry.name);
1719
if (entry.isDirectory()) {
18-
results.push(...await discoverMdxFiles(fullPath));
20+
results.push(...(await discoverMdxFiles(fullPath)));
1921
} else if (entry.name.endsWith('.mdx')) {
2022
results.push(fullPath);
2123
}
@@ -24,8 +26,18 @@ const discoverMdxFiles = async (dir: string): Promise<string[]> => {
2426
return results;
2527
};
2628

27-
const removePreviews = (content: string): string => {
28-
return content.replace(/<Preview\s+path=["'][^"']+["']\s*\/>/g, '');
29+
const replacePreviews = (content: string): string => {
30+
return content.replace(
31+
/<Preview\s+path=["']([^"']+)["']\s*\/>/g,
32+
(_, path) => `See \`scripts/${path}.tsx\` for this example.`
33+
);
34+
};
35+
36+
const removeCustomComponents = (content: string): string => {
37+
return content
38+
.replace(/<ElementsInstaller\s*\/>/g, '')
39+
.replace(/<ElementsDemo\s*\/>/g, '')
40+
.replace(/<Callout>\s*[\s\S]*?<\/Callout>/g, '');
2941
};
3042

3143
const replaceInstaller = (content: string): string => {
@@ -35,7 +47,9 @@ const replaceInstaller = (content: string): string => {
3547
);
3648
};
3749

38-
const parseTypeTableProps = (typeContent: string): Array<{
50+
const parseTypeTableProps = (
51+
typeContent: string
52+
): Array<{
3953
name: string;
4054
type: string;
4155
description: string;
@@ -103,31 +117,41 @@ const replaceTypeTables = (content: string): string => {
103117
});
104118
};
105119

106-
const transformMdx = (fileContent: string, title: string, description: string): string => {
120+
const removeCallouts = (content: string): string => {
121+
return content.replace(/<Callout[^>]*>[\s\S]*?<\/Callout>/g, '');
122+
};
123+
124+
const transformComponentMdx = (fileContent: string): string => {
107125
const { content } = matter(fileContent);
108126

109-
let processedContent = removePreviews(content);
127+
let processedContent = replacePreviews(content);
110128
processedContent = replaceInstaller(processedContent);
111129
processedContent = replaceTypeTables(processedContent);
130+
processedContent = removeCallouts(processedContent);
112131

113-
const frontmatter = [
114-
'---',
115-
`name: Using the ${title} component from AI Elements`,
116-
`description: ${description}`,
117-
'---',
118-
].join('\n');
132+
return processedContent.trim();
133+
};
119134

120-
return `${frontmatter}\n${processedContent}`;
135+
const transformOverviewMdx = (fileContent: string): string => {
136+
const { content } = matter(fileContent);
137+
138+
let processedContent = removeCustomComponents(content);
139+
processedContent = processedContent.trim();
140+
141+
return processedContent;
121142
};
122143

123-
const findMatchingExamples = async (componentName: string): Promise<string[]> => {
144+
const findMatchingExamples = async (
145+
componentName: string
146+
): Promise<string[]> => {
124147
const files = await readdir(EXAMPLES_DIR);
125148

126149
return files.filter((file) => {
127150
const fileBasename = file.replace('.tsx', '');
128151
return (
129152
file.endsWith('.tsx') &&
130-
(fileBasename === componentName || fileBasename.startsWith(`${componentName}-`))
153+
(fileBasename === componentName ||
154+
fileBasename.startsWith(`${componentName}-`))
131155
);
132156
});
133157
};
@@ -139,19 +163,64 @@ const cleanSkillsDir = (): void => {
139163
mkdirSync(SKILLS_DIR, { recursive: true });
140164
};
141165

142-
const processComponent = async (mdxPath: string): Promise<void> => {
166+
const generateOverviewSkill = async (): Promise<void> => {
167+
const indexContent = await readFile(join(DOCS_DIR, 'index.mdx'), 'utf-8');
168+
const usageContent = await readFile(join(DOCS_DIR, 'usage.mdx'), 'utf-8');
169+
const troubleshootingContent = await readFile(
170+
join(DOCS_DIR, 'troubleshooting.mdx'),
171+
'utf-8'
172+
);
173+
174+
const indexData = matter(indexContent);
175+
const usageData = matter(usageContent);
176+
const troubleshootingData = matter(troubleshootingContent);
177+
178+
const skillContent = `---
179+
name: ai-elements
180+
description: Create new AI chat interface components for the ai-elements library following established composable patterns, shadcn/ui integration, and Vercel AI SDK conventions. Use when creating new components in packages/elements/src or when the user asks to add a new component to ai-elements.
181+
---
182+
183+
# AI Elements
184+
185+
${transformOverviewMdx(indexContent)}
186+
187+
## Usage
188+
189+
${transformOverviewMdx(usageContent)}
190+
191+
## Troubleshooting
192+
193+
${transformOverviewMdx(troubleshootingContent)}
194+
195+
## Available Components
196+
197+
See the \`references/\` folder for detailed documentation on each component.
198+
`;
199+
200+
mkdirSync(SKILL_DIR, { recursive: true });
201+
await writeFile(join(SKILL_DIR, 'SKILL.md'), skillContent);
202+
console.log('Generated: SKILL.md (overview)');
203+
};
204+
205+
const processComponent = async (mdxPath: string): Promise<number> => {
143206
const componentName = basename(mdxPath, '.mdx');
144-
const skillDir = join(SKILLS_DIR, `use-${componentName}-component`);
145-
const scriptsDir = join(skillDir, 'scripts');
207+
const referencesDir = join(SKILL_DIR, 'references');
208+
const scriptsDir = join(SKILL_DIR, 'scripts');
146209

147210
const fileContent = await readFile(mdxPath, 'utf-8');
148211
const { data } = matter(fileContent);
149212

150-
const skillContent = transformMdx(fileContent, data.title, data.description);
151-
const examples = await findMatchingExamples(componentName);
213+
const referenceContent = `# ${data.title}
214+
215+
${data.description}
216+
217+
${transformComponentMdx(fileContent)}
218+
`;
152219

153-
mkdirSync(skillDir, { recursive: true });
154-
await writeFile(join(skillDir, 'SKILL.md'), skillContent);
220+
mkdirSync(referencesDir, { recursive: true });
221+
await writeFile(join(referencesDir, `${componentName}.md`), referenceContent);
222+
223+
const examples = await findMatchingExamples(componentName);
155224

156225
if (examples.length > 0) {
157226
mkdirSync(scriptsDir, { recursive: true });
@@ -164,22 +233,26 @@ const processComponent = async (mdxPath: string): Promise<void> => {
164233
}
165234
}
166235

167-
console.log(`Generated: use-${componentName}-component (${examples.length} examples)`);
236+
console.log(`Generated: references/${componentName}.md (${examples.length} examples)`);
237+
return examples.length;
168238
};
169239

170240
const main = async (): Promise<void> => {
171-
console.log('Generating skills from docs and examples...\n');
241+
console.log('Generating ai-elements skill from docs and examples...\n');
172242

173243
cleanSkillsDir();
174244

175-
const mdxFiles = await discoverMdxFiles(DOCS_DIR);
176-
console.log(`Found ${mdxFiles.length} MDX files\n`);
245+
await generateOverviewSkill();
246+
247+
const mdxFiles = await discoverMdxFiles(COMPONENTS_DIR);
248+
console.log(`\nFound ${mdxFiles.length} component MDX files\n`);
177249

250+
let totalExamples = 0;
178251
for (const mdxPath of mdxFiles) {
179-
await processComponent(mdxPath);
252+
totalExamples += await processComponent(mdxPath);
180253
}
181254

182-
console.log('\nDone!');
255+
console.log(`\nDone! Generated ${mdxFiles.length} references with ${totalExamples} examples.`);
183256
};
184257

185258
main().catch(console.error);

skills/ai-elements/SKILL.md

Lines changed: 166 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,166 @@
1+
---
2+
name: ai-elements
3+
description: Create new AI chat interface components for the ai-elements library following established composable patterns, shadcn/ui integration, and Vercel AI SDK conventions. Use when creating new components in packages/elements/src or when the user asks to add a new component to ai-elements.
4+
---
5+
6+
# AI Elements
7+
8+
[AI Elements](https://www.npmjs.com/package/ai-elements) is a component library and custom registry built on top of [shadcn/ui](https://ui.shadcn.com/) to help you build AI-native applications faster. It provides pre-built components like conversations, messages and more.
9+
10+
Installing AI Elements is straightforward and can be done in a couple of ways. You can use the dedicated CLI command for the fastest setup, or integrate via the standard shadcn/ui CLI if you've already adopted shadcn's workflow.
11+
12+
13+
14+
## Quick Start
15+
16+
Here are some basic examples of what you can achieve using components from AI Elements.
17+
18+
19+
20+
## Prerequisites
21+
22+
Before installing AI Elements, make sure your environment meets the following requirements:
23+
24+
- [Node.js](https://nodejs.org/en/download/), version 18 or later
25+
- A [Next.js](https://nextjs.org/) project with the [AI SDK](https://ai-sdk.dev/) installed.
26+
- [shadcn/ui](https://ui.shadcn.com/) installed in your project. If you don't have it installed, running any install command will automatically install it for you.
27+
- We also highly recommend using the [AI Gateway](https://vercel.com/docs/ai-gateway) and adding `AI_GATEWAY_API_KEY` to your `env.local` so you don't have to use an API key from every provider. AI Gateway also gives $5 in usage per month so you can experiment with models. You can obtain an API key [here](https://vercel.com/d?to=%2F%5Bteam%5D%2F%7E%2Fai%2Fapi-keys&title=Get%20your%20AI%20Gateway%20key).
28+
29+
30+
31+
## Installing Components
32+
33+
You can install AI Elements components using either the AI Elements CLI or the shadcn/ui CLI. Both achieve the same result: adding the selected component’s code and any needed dependencies to your project.
34+
35+
The CLI will download the component’s code and integrate it into your project’s directory (usually under your components folder). By default, AI Elements components are added to the `@/components/ai-elements/` directory (or whatever folder you’ve configured in your shadcn components settings).
36+
37+
After running the command, you should see a confirmation in your terminal that the files were added. You can then proceed to use the component in your code.
38+
39+
## Usage
40+
41+
Once an AI Elements component is installed, you can import it and use it in your application like any other React component. The components are added as part of your codebase (not hidden in a library), so the usage feels very natural.
42+
43+
## Example
44+
45+
After installing AI Elements components, you can use them in your application like any other React component. For example:
46+
47+
```tsx title="conversation.tsx"
48+
'use client';
49+
50+
import {
51+
Message,
52+
MessageContent,
53+
MessageResponse,
54+
} from '@/components/ai-elements/message';
55+
import { useChat } from '@ai-sdk/react';
56+
57+
const Example = () => {
58+
const { messages } = useChat();
59+
60+
return (
61+
<>
62+
{messages.map(({ role, parts }, index) => (
63+
<Message from={role} key={index}>
64+
<MessageContent>
65+
{parts.map((part, i) => {
66+
switch (part.type) {
67+
case 'text':
68+
return <MessageResponse key={`${role}-${i}`}>{part.text}</MessageResponse>;
69+
}
70+
})}
71+
</MessageContent>
72+
</Message>
73+
))}
74+
</>
75+
);
76+
};
77+
78+
export default Example;
79+
```
80+
81+
In the example above, we import the `Message` component from our AI Elements directory and include it in our JSX. Then, we compose the component with the `MessageContent` and `MessageResponse` subcomponents. You can style or configure the component just as you would if you wrote it yourself – since the code lives in your project, you can even open the component file to see how it works or make custom modifications.
82+
83+
## Extensibility
84+
85+
All AI Elements components take as many primitive attributes as possible. For example, the `Message` component extends `HTMLAttributes<HTMLDivElement>`, so you can pass any props that a `div` supports. This makes it easy to extend the component with your own styles or functionality.
86+
87+
## Customization
88+
89+
90+
91+
After installation, no additional setup is needed. The component’s styles (Tailwind CSS classes) and scripts are already integrated. You can start interacting with the component in your app immediately.
92+
93+
For example, if you'd like to remove the rounding on `Message`, you can go to `components/ai-elements/message.tsx` and remove `rounded-lg` as follows:
94+
95+
```tsx title="components/ai-elements/message.tsx" highlight="8"
96+
export const MessageContent = ({
97+
children,
98+
className,
99+
...props
100+
}: MessageContentProps) => (
101+
<div
102+
className={cn(
103+
'flex flex-col gap-2 text-sm text-foreground',
104+
'group-[.is-user]:bg-primary group-[.is-user]:text-primary-foreground group-[.is-user]:px-4 group-[.is-user]:py-3',
105+
className,
106+
)}
107+
{...props}
108+
>
109+
<div className="is-user:dark">{children}</div>
110+
</div>
111+
);
112+
```
113+
114+
## Troubleshooting
115+
116+
## Why are my components not styled?
117+
118+
Make sure your project is configured correctly for shadcn/ui in Tailwind 4 - this means having a `globals.css` file that imports Tailwind and includes the shadcn/ui base styles.
119+
120+
## I ran the AI Elements CLI but nothing was added to my project
121+
122+
Double-check that:
123+
124+
- Your current working directory is the root of your project (where `package.json` lives).
125+
- Your components.json file (if using shadcn-style config) is set up correctly.
126+
- You’re using the latest version of the AI Elements CLI:
127+
128+
```bash title="Terminal"
129+
npx ai-elements@latest
130+
```
131+
132+
If all else fails, feel free to open an [issue on GitHub](https://github.com/vercel/ai-elements/issues).
133+
134+
## Theme switching doesn’t work — my app stays in light mode
135+
136+
Ensure your app is using the same data-theme system that shadcn/ui and AI Elements expect. The default implementation toggles a data-theme attribute on the `<html>` element. Make sure your tailwind.config.js is using class or data- selectors accordingly:
137+
138+
## The component imports fail with “module not found”
139+
140+
Check the file exists. If it does, make sure your `tsconfig.json` has a proper paths alias for `@/` i.e.
141+
142+
```json title="tsconfig.json"
143+
{
144+
"compilerOptions": {
145+
"baseUrl": ".",
146+
"paths": {
147+
"@/*": ["./*"]
148+
}
149+
}
150+
}
151+
```
152+
153+
## My AI coding assistant can't access AI Elements components
154+
155+
1. Verify your config file syntax is valid JSON.
156+
2. Check that the file path is correct for your AI tool.
157+
3. Restart your coding assistant after making changes.
158+
4. Ensure you have a stable internet connection.
159+
160+
## Still stuck?
161+
162+
If none of these answers help, open an [issue on GitHub](https://github.com/vercel/ai-elements/issues) and someone will be happy to assist.
163+
164+
## Available Components
165+
166+
See the `references/` folder for detailed documentation on each component.

0 commit comments

Comments
 (0)