First Tool
Defining the Tool
Create your first tool. This will be called getProducts
.
Note: We are using a generator function here (function*
), which allows you to pause its execution and return a value, then resume from where it left off on the next call. This is useful for handling data streams, as you can fetch and return data from an asynchronous source like an API, then resume the function to fetch the next chunk when needed. By yielding values one at a time, generator functions enable efficient processing of streaming data without blocking the main thread.
Run the dev server and head over to http://localhost:3000 and ask the bot for Apple Products.
You should see a list of Apple Products generated. Awesome, but that's just text! Let's create a component to display them.
Move Schemas
To make it easier to share types throughout our application, let's move our product schema to a new file. Create a file at lib/schemas/products.ts
and paste the following code:
Note: we are also adding a few more properties to generate a description
, price
, and type
.
Generate a Products Component
We can use v0 to generate a component to display your "products". Let's use this carousel.
Install it with the following command and name it ProductCarousel
.
Update the component to map through the products and display the product details.
Go back to your Server Action and update the products schema to the one defined in the previous step. Return the newly generated ProductCarousel
component, passing in the generated products as a prop.
Remember to import productsSchema
!
Head back to your browser and ask for Apple Products again.