Developing A Task Management ChatGPT Plugin with Typescript
Tags: typescript, chatgpt
Published: 2023-08-10 13:53:08

Task management is an essential aspect of productivity and organization in both personal and professional settings. 

With the rise of virtual assistants and chatbots, integrating task management capabilities into these platforms has become increasingly popular. 

In this tutorial, we will build a ChatGPT plugin using Next.js, leveraging its new functionality and routes. The goal of our plugin is to provide task management capabilities, allowing users to list, create, and delete tasks. 

Additionally, we will enable querying information about tasks, such as the number of remaining tasks. By the end of this tutorial, you will have a solid understanding of how to develop your own custom plugins.

To keep things simple, we will store our task data in memory using a store-like structure. However, in a production application, you would typically use a database for persistent storage. Now, let's dive into the development process.


To start with, we will scaffold our application by running : `npx create-next-app chat-gpt-plugin`
Following the prompt and select the appropriate instruction guide like this. choose

Would you like to use TypeScript with this project? ...Yes
Would you like to use ESLint with this project? » Yes
Would you like to use Tailwind CSS with this project? » No
Would you like to use `src/` directory with this project? » Yes
Use App Router (recommended)? » Yes
Would you like to customize the default import alias? » Yes
 What import alias would you like configured? ... @/* - press enter

Following these steps would create our nextjs application. 

Now, to start our application, navigate into the newly created `chat-gpt-plugin` folder and run :
`npm run dev`

Your application should now be running on http://localhost:3000/. Go to your browser and visit this address.

If everything works fine, you should see this Nextjs page in your browser:

![Frontpage](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/923owkfluurg631wj3h1.PNG)

Now, let's open the ChatGPT documentation on the OpenAI website and explore the relevant section for our plugin. This documentation will serve as our reference throughout the development process.

Once you have reviewed the documentation, let's return to our application. We need to set up some configurations, starting with the headers to enable Cross-Origin Resource Sharing (CORS) for the ChatGPT API at [Open API Doc](https://platform.openai.com/docs/plugins/getting-started)

 
Inside our `nextconfig` file, in your project folder, add the following code:
 {% gist https://gist.github.com/jamesoyanna/8cabb0869d6c96ae4936e017298d3e3f file=next.config.js%}
Here in this code, we provided a configuration for our application.

The headers function here is an asynchronous function that we set up as custom headers for specific routes. 

We set the Access-Control-Allow-Origin header to allow requests from https://chat.openai.com for all routes matching the /:path* pattern. 

This is necessary in order to register our plugin.

Now, inside the public folder create a folder called `.well-know`. Create a file inside the .well-know directory called `ai-plugin.json`

The `ai-plugin.json` is a manifest that provides information about your plugin, such as its name, version, and main configuration endpoint, which can be used by the ChatGPT system to interact with your plugin effectively.

Add the following code to the `ai-plugin.json` file:
{% gist https://gist.github.com/jamesoyanna/5f22db4efe46c15a1bcbcc86d7e40546 file=ai-plugin.json%}

Here in this code we simply provide a JSON object that represents the schema of our Chatgpt plugin.

This JSON object follows the schema defined for chatgpt plugins and provides essential information about the plugin, including its name, description, authentication requirements, API details, logo, contact email, and legal information.

Now let's create the main configuration for our app.

In the  public directory, create a file. Name it `openapi.yml` and add the following code to it :
{% gist https://gist.github.com/jamesoyanna/c25e42e806ad481e0cf2690307b3201e file=openapi.yml%}

Here in this code,  we added a specification for defining and documenting the APIs
`openapi: 3.0.1:` Here we specify that the API is using the OpenAPI version 3.0.1.

We then provided information about the API, including its title, description, and version

`servers:` We define the server where the API is hosted. In this case, the server is specified as http://localhost:3000

`paths:` This is the heart of the API specification. It defines the various paths (URL endpoints) that the API exposes, along with the HTTP methods (such as GET, POST, etc.) that can be used on those paths.

`/api/tasks:` This is a path for getting the list of tasks. It uses the HTTP GET method. 
The responses section specifies the possible responses from the API, including the HTTP status code and a description.
`
/api/create-task:` This path is for adding a new task. It uses the HTTP POST method. The request body section specifies that the request must include a JSON body with a task property. 

`/api/delete-task:` This path is for deleting a task. It also uses the HTTP POST method.

`
schemas: `This subsection defines the data structures used in the API.

`deleteTaskRequest:` This schema defines the structure of the request body required for deleting a task. It contains a single property task of type string.

`addTaskRequest:` This schema defines the structure of the request body required for adding a new task. It also contains a single property task of type string.

`getTasksResponse:` This schema defines the structure of the response body when getting the list of tasks. It includes a property `tasks` which is an array of strings.