This project demonstrates how to extend the capabilities of an OpenAI Assistant to interact with the system clipboard, enabling copy and paste operations. The implementation is done in TypeScript and integrates with the OpenAI API.
You can find my companion blog post here.
- Node.js installed on your machine
- An OpenAI API key
- Basic knowledge of TypeScript and Node.js
-
Clone the Repository: Clone this repository to your local machine.
-
Install Dependencies: Run
npm install
in the project directory to install the required dependencies, including the OpenAI SDK. -
Set Up Environment Variables: Create a
.env
file in the root directory and add your OpenAI API key:OPENAI_API_KEY=your_api_key_here
clipboard.ts
: Contains theClipboard
class for clipboard operations.main.ts
: The main entry point of the application, where the OpenAI Assistant is created and managed.openai
and related imports: OpenAI SDK modules for interacting with the API.
To run the application, execute node main.ts
in the terminal. The script will create an instance of the OpenAI Assistant, capable of performing clipboard operations based on user instructions.
const client = new OpenAI({ apiKey: process.env.OPENAI_API_KEY as string })
Initializes the OpenAI client using the provided API key.
The clipboard_operations
function within main.ts
is responsible for handling the copy and paste commands. It uses the Clipboard
class to interact with the system clipboard.
Located in clipboard.ts
, this class uses Node.js's child_process
module to execute platform-specific shell commands for clipboard operations.
In main.ts
, an instance of the OpenAI GPT-4 assistant is created with specific instructions for clipboard interaction. The assistant processes user prompts and executes the appropriate clipboard operation.