Skip to content

Commit f868486

Browse files
feat(cli): add streamdown in ai example of all react web templates (#554)
1 parent 383ea6f commit f868486

File tree

9 files changed

+51
-24
lines changed

9 files changed

+51
-24
lines changed

apps/cli/src/constants.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,7 @@ export const dependencyVersionMap = {
108108
"@ai-sdk/vue": "^2.0.9",
109109
"@ai-sdk/svelte": "^3.0.9",
110110
"@ai-sdk/react": "^2.0.9",
111+
streamdown: "^1.1.6",
111112

112113
"@orpc/server": "^1.8.4",
113114
"@orpc/client": "^1.8.4",

apps/cli/src/helpers/addons/examples-setup.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ export async function setupExamples(config: ProjectConfig) {
4343
} else if (hasSvelte) {
4444
dependencies.push("@ai-sdk/svelte");
4545
} else if (hasReactWeb) {
46-
dependencies.push("@ai-sdk/react");
46+
dependencies.push("@ai-sdk/react", "streamdown");
4747
}
4848
await addPackageDependency({
4949
dependencies,

apps/cli/src/helpers/core/template-manager.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -740,6 +740,19 @@ export async function setupExamplesTemplate(
740740
if (hasReactWeb) {
741741
const exampleWebSrc = path.join(exampleBaseDir, "web/react");
742742
if (await fs.pathExists(exampleWebSrc)) {
743+
if (example === "ai") {
744+
const exampleWebBaseSrc = path.join(exampleWebSrc, "base");
745+
if (await fs.pathExists(exampleWebBaseSrc)) {
746+
await processAndCopyFiles(
747+
"**/*",
748+
exampleWebBaseSrc,
749+
webAppDir,
750+
context,
751+
false,
752+
);
753+
}
754+
}
755+
743756
const reactFramework = context.frontend.find((f) =>
744757
[
745758
"next",
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
"use client";
2+
3+
import { type ComponentProps, memo } from "react";
4+
import { Streamdown } from "streamdown";
5+
import { cn } from "@/lib/utils";
6+
7+
type ResponseProps = ComponentProps<typeof Streamdown>;
8+
9+
export const Response = memo(
10+
({ className, ...props }: ResponseProps) => (
11+
<Streamdown
12+
className={cn(
13+
"size-full [&>*:first-child]:mt-0 [&>*:last-child]:mb-0",
14+
className,
15+
)}
16+
{...props}
17+
/>
18+
),
19+
(prevProps, nextProps) => prevProps.children === nextProps.children,
20+
);
21+
22+
Response.displayName = "Response";

apps/cli/templates/examples/ai/web/react/next/src/app/ai/page.tsx.hbs

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,11 @@
22

33
import { useChat } from "@ai-sdk/react";
44
import { DefaultChatTransport } from "ai";
5-
import { Input } from "@/components/ui/input";
6-
import { Button } from "@/components/ui/button";
75
import { Send } from "lucide-react";
8-
import { useRef, useEffect, useState } from "react";
6+
import { useEffect, useRef, useState } from "react";
7+
import { Response } from "@/components/response";
8+
import { Button } from "@/components/ui/button";
9+
import { Input } from "@/components/ui/input";
910

1011
export default function AIPage() {
1112
const [input, setInput] = useState("");
@@ -51,11 +52,7 @@ export default function AIPage() {
5152
</p>
5253
{message.parts?.map((part, index) => {
5354
if (part.type === "text") {
54-
return (
55-
<div key={index} className="whitespace-pre-wrap">
56-
{part.text}
57-
</div>
58-
);
55+
return <Response key={index}>{part.text}</Response>;
5956
}
6057
return null;
6158
})}

apps/cli/templates/examples/ai/web/react/react-router/src/routes/ai.tsx.hbs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { DefaultChatTransport } from "ai";
44
import { Input } from "@/components/ui/input";
55
import { Button } from "@/components/ui/button";
66
import { Send } from "lucide-react";
7+
import { Response } from "@/components/response";
78

89
const AI: React.FC = () => {
910
const [input, setInput] = useState("");
@@ -49,11 +50,7 @@ const AI: React.FC = () => {
4950
</p>
5051
{message.parts?.map((part, index) => {
5152
if (part.type === "text") {
52-
return (
53-
<div key={index} className="whitespace-pre-wrap">
54-
{part.text}
55-
</div>
56-
);
53+
return <Response key={index}>{part.text}</Response>;
5754
}
5855
return null;
5956
})}

apps/cli/templates/examples/ai/web/react/tanstack-router/src/routes/ai.tsx.hbs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { Input } from "@/components/ui/input";
55
import { Button } from "@/components/ui/button";
66
import { Send } from "lucide-react";
77
import { useRef, useEffect, useState } from "react";
8+
import { Response } from "@/components/response";
89

910
export const Route = createFileRoute("/ai")({
1011
component: RouteComponent,
@@ -54,11 +55,7 @@ function RouteComponent() {
5455
</p>
5556
{message.parts?.map((part, index) => {
5657
if (part.type === "text") {
57-
return (
58-
<div key={index} className="whitespace-pre-wrap">
59-
{part.text}
60-
</div>
61-
);
58+
return <Response key={index}>{part.text}</Response>;
6259
}
6360
return null;
6461
})}

apps/cli/templates/examples/ai/web/react/tanstack-start/src/routes/ai.tsx.hbs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { Input } from "@/components/ui/input";
55
import { Button } from "@/components/ui/button";
66
import { Send } from "lucide-react";
77
import { useRef, useEffect, useState } from "react";
8+
import { Response } from "@/components/response";
89

910
export const Route = createFileRoute("/ai")({
1011
component: RouteComponent,
@@ -54,11 +55,7 @@ function RouteComponent() {
5455
</p>
5556
{message.parts?.map((part, index) => {
5657
if (part.type === "text") {
57-
return (
58-
<div key={index} className="whitespace-pre-wrap">
59-
{part.text}
60-
</div>
61-
);
58+
return <Response key={index}>{part.text}</Response>;
6259
}
6360
return null;
6461
})}

apps/cli/templates/frontend/react/web-base/src/index.css renamed to apps/cli/templates/frontend/react/web-base/src/index.css.hbs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
@import "tailwindcss";
22
@import "tw-animate-css";
3+
{{#if (includes examples "ai")}}
4+
@source "../node_modules/streamdown/dist/index.js";
5+
{{/if}}
36

47
@custom-variant dark (&:where(.dark, .dark *));
58

0 commit comments

Comments
 (0)