Skip to content
Source
Sign inSign up
TanStack/ai
Open History to see the last commit

README.md

325 lines8.6 KB

1Welcome to your new TanStack app!
2​
3# Getting Started
4​
5To run this application:
6​
7```bash
8pnpm install
9pnpm start
10```
11​
12# Building For Production
13​
14To build this application for production:
15​
16```bash
17pnpm build
18```
19​
20## Testing
21​
22This project uses [Vitest](https://vitest.dev/) for testing. You can run the tests with:
23​
24```bash
25pnpm test
26```
27​
28## Styling
29​
30This project uses [Tailwind CSS](https://tailwindcss.com/) for styling.
31​
32# TanStack Chat Application
33​
34An example chat application built with TanStack Start, TanStack Store, and **TanStack AI** (our open-source AI SDK).
35​
36## .env Updates
37​
38```env
39OPENAI_API_KEY=your_openai_api_key
40```
41​
42## ✨ Features
43​
44### AI Capabilities
45​
46- 🤖 Powered by **TanStack AI** with OpenAI GPT-4o
47- 📝 Rich markdown formatting with syntax highlighting
48- 🎯 Customizable system prompts for tailored AI behavior
49- 🔄 Real-time streaming responses with Server-Sent Events
50- 🔌 **Connection adapters** - flexible streaming architecture
51- 🛠️ **Automatic tool execution loop** - tools are executed automatically by the SDK
52- 🎸 Tool/function calling with guitar recommendations
53​
54### User Experience
55​
56- 🎨 Modern UI with Tailwind CSS and Lucide icons
57- 🔍 Conversation management and history
58- 🔐 Secure API key management
59- 📋 Markdown rendering with code highlighting
60​
61### Technical Features
62​
63- 📦 Centralized state management with TanStack Store
64- 🔌 Extensible architecture for multiple AI providers
65- 🛠️ TypeScript for type safety
66​
67## Architecture
68​
69### Tech Stack
70​
71- **Frontend Framework**: TanStack Start
72- **Routing**: TanStack Router
73- **State Management**: TanStack Store
74- **Styling**: Tailwind CSS
75- **AI Integration**: TanStack AI with OpenAI GPT-4o
76- **Chat Client**: `@tanstack/ai-solid` with connection adapters
77- **Streaming**: Server-Sent Events via `fetchServerSentEvents`
78- **Tool Execution**: Automatic loop with `ToolCallManager`
79​
80## Routing
81​
82This project uses [TanStack Router](https://tanstack.com/router). The initial setup is a file based router. Which means that the routes are managed as files in `src/routes`.
83​
84### Adding A Route
85​
86To add a new route to your application just add another a new file in the `./src/routes` directory.
87​
88TanStack will automatically generate the content of the route file for you.
89​
90Now that you have two routes you can use a `Link` component to navigate between them.
91​
92### Adding Links
93​
94To use SPA (Single Page Application) navigation you will need to import the `Link` component from `@tanstack/solid-router`.
95​
96```tsx
97import { Link } from '@tanstack/solid-router'
98```
99​
100Then anywhere in your JSX you can use it like so:
101​
102```tsx
103<Link to="/about">About</Link>
104```
105​
106This will create a link that will navigate to the `/about` route.
107​
108More information on the `Link` component can be found in the [Link documentation](https://tanstack.com/router/v1/docs/framework/solid/api/router/linkComponent).
109​
110### Using A Layout
111​
112In the File Based Routing setup the layout is located in `src/routes/__root.tsx`. Anything you add to the root route will appear in all the routes. The route content will appear in the JSX where you use the `<Outlet />` component.
113​
114Here is an example layout that includes a header:
115​
116```tsx
117import { Outlet, createRootRoute } from '@tanstack/solid-router'
118import { TanStackRouterDevtools } from '@tanstack/solid-router-devtools'
119​
120import { Link } from '@tanstack/solid-router'
121​
122export const Route = createRootRoute({
123 component: () => (
124 <>
125 <header>
126 <nav>
127 <Link to="/">Home</Link>
128 <Link to="/about">About</Link>
129 </nav>
130 </header>
131 <Outlet />
132 <TanStackRouterDevtools />
133 </>
134 ),
135})
136```
137​
138The `<TanStackRouterDevtools />` component is not required so you can remove it if you don't want it in your layout.
139​
140More information on layouts can be found in the [Layouts documentation](https://tanstack.com/router/latest/docs/framework/solid/guide/routing-concepts#layouts).
141​
142## Data Fetching
143​
144There are multiple ways to fetch data in your application. You can use TanStack Query to fetch data from a server. But you can also use the `loader` functionality built into TanStack Router to load the data for a route before it's rendered.
145​
146For example:
147​
148```tsx
149const peopleRoute = createRoute({
150 getParentRoute: () => rootRoute,
151 path: '/people',
152 loader: async () => {
153 const response = await fetch('https://swapi.dev/api/people')
154 return response.json() as Promise<{
155 results: {
156 name: string
157 }[]
158 }>
159 },
160 component: () => {
161 const data = peopleRoute.useLoaderData()
162 return (
163 <ul>
164 {data.results.map((person) => (
165 <li key={person.name}>{person.name}</li>
166 ))}
167 </ul>
168 )
169 },
170})
171```
172​
173Loaders simplify your data fetching logic dramatically. Check out more information in the [Loader documentation](https://tanstack.com/router/latest/docs/framework/solid/guide/data-loading#loader-parameters).
174​
175### Solid-Query
176​
177Solid-Query is an excellent addition or alternative to route loading and integrating it into you application is a breeze.
178​
179First add your dependencies:
180​
181```bash
182pnpm add @tanstack/solid-query @tanstack/solid-query-devtools
183```
184​
185Next we'll need to create a query client and provider. We recommend putting those in `main.tsx`.
186​
187```tsx
188import { QueryClient, QueryClientProvider } from '@tanstack/solid-query'
189​
190// ...
191​
192const queryClient = new QueryClient()
193​
194// ...
195```
196​
197You can also add TanStack Query Devtools to the root route (optional).
198​
199```tsx
200import { SolidQueryDevtools } from '@tanstack/solid-query-devtools'
201​
202const rootRoute = createRootRoute({
203 component: () => (
204 <>
205 <Outlet />
206 <SolidQueryDevtools buttonPosition="top-right" />
207 <TanStackRouterDevtools />
208 </>
209 ),
210})
211```
212​
213Now you can use `useQuery` to fetch your data.
214​
215```tsx
216import { useQuery } from '@tanstack/solid-query'
217​
218import './App.css'
219​
220function App() {
221 const { data } = useQuery({
222 queryKey: ['people'],
223 queryFn: () =>
224 fetch('https://swapi.dev/api/people')
225 .then((res) => res.json())
226 .then((data) => data.results as { name: string }[]),
227 initialData: [],
228 })
229​
230 return (
231 <div>
232 <ul>
233 {data.map((person) => (
234 <li key={person.name}>{person.name}</li>
235 ))}
236 </ul>
237 </div>
238 )
240​
241export default App
242```
243​
244You can find out everything you need to know on how to use Solid-Query in the [Solid-Query documentation](https://tanstack.com/query/latest/docs/framework/solid/overview).
245​
246## State Management
247​
248Another common requirement for Solid applications is state management. There are many options for state management in Solid. TanStack Store provides a great starting point for your project.
249​
250First you need to add TanStack Store as a dependency:
251​
252```bash
253pnpm add @tanstack/store
254```
255​
256Now let's create a simple counter in the `src/App.tsx` file as a demonstration.
257​
258```tsx
259import { useStore } from '@tanstack/solid-store'
260import { Store } from '@tanstack/store'
261import './App.css'
262​
263const countStore = new Store(0)
264​
265function App() {
266 const count = useStore(countStore)
267 return (
268 <div>
269 <button onClick={() => countStore.setState((n) => n + 1)}>
270 Increment - {count}
271 </button>
272 </div>
273 )
275​
276export default App
277```
278​
279One of the many nice features of TanStack Store is the ability to derive state from other state. That derived state will update when the base state updates.
280​
281Let's check this out by doubling the count using derived state.
282​
283```tsx
284import { useStore } from '@tanstack/solid-store'
285import { Store, Derived } from '@tanstack/store'
286import './App.css'
287​
288const countStore = new Store(0)
289​
290const doubledStore = new Derived({
291 fn: () => countStore.state * 2,
292 deps: [countStore],
293})
294doubledStore.mount()
295​
296function App() {
297 const count = useStore(countStore)
298 const doubledCount = useStore(doubledStore)
299​
300 return (
301 <div>
302 <button onClick={() => countStore.setState((n) => n + 1)}>
303 Increment - {count}
304 </button>
305 <div>Doubled - {doubledCount}</div>
306 </div>
307 )
309​
310export default App
311```
312​
313We use the `Derived` class to create a new store that is derived from another store. The `Derived` class has a `mount` method that will start the derived store updating.
314​
315Once we've created the derived store we can use it in the `App` component just like we would any other store using the `useStore` hook.
316​
317You can find out everything you need to know on how to use TanStack Store in the [TanStack Store documentation](https://tanstack.com/store/latest).
318​
319# Demo files
320​
321Files prefixed with `demo` can be safely deleted. They are there to provide a starting point for you to play around with the features you've installed.
322​
323# Learn More
324​
325You can learn more about all of the offerings from TanStack in the [TanStack documentation](https://tanstack.com).