Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add dependency check and environment variable export #1

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions .devcontainer/devcontainer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"tasks": {
"test": "npm install && npm test",
"build": "npm install",
"launch": "echo \"No specific launch command found. This repository is intended to be used as a library in other projects.\""
}
}
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -369,6 +369,7 @@ You will need to make sure you have a `.env` file with `OPENAI_API_KEY=` set in
to run tests. From there, running the test suite is easy.

```shell
$ npm install
$ npm test
```

Expand Down
40 changes: 14 additions & 26 deletions examples/node_devenv.mjs
Original file line number Diff line number Diff line change
@@ -1,34 +1,9 @@
// javascript
//
// This script connects to the OpenAI Realtime API to create a voice-based assistant.
//
// It captures audio input from your microphone, sends it to the OpenAI API for processing,
// and plays back the assistant's audio response through your speakers.
//
// **How to Run on a Mac:**
//
// 1. **Install Dependencies:**
// - Ensure you have Node.js and npm installed.
// - Run `npm init & npm install` to install all required packages.
//
// 2. **Set Up Environment Variables:**
// - Create a `.env` file in the same directory as this script.
// - Add your OpenAI API key to the `.env` file:
// ```
// OPENAI_API_KEY=your_api_key_here
// ```
//
// 3. **Run the Script:**
// - Execute the script with the command `node node_devenv.mjs`.
//
// **Note:** Make sure your microphone and speakers are properly configured and accessible on your Mac.
//

import { RealtimeClient } from '@openai/realtime-api-beta';
import mic from 'mic';
import { Readable } from 'stream';
import Speaker from 'speaker';
import dotenv from 'dotenv';
import os from 'os';

dotenv.config();

Expand Down Expand Up @@ -164,3 +139,16 @@ function playAudio(audioData) {
}

// END MANAGE AUDIO INTERFACES

// Check if all dependencies are installed
import { exec } from 'child_process';

exec('npm ls', (error, stdout, stderr) => {
if (error) {
console.error('Error checking dependencies:', error);
console.error('Please run "npm install" to install missing dependencies.');
process.exit(1);
} else {
console.log('All dependencies are installed.');
}
});
28 changes: 28 additions & 0 deletions lib/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import { RealtimeEventHandler } from './event_handler.js';
import { RealtimeAPI } from './api.js';
import { RealtimeConversation } from './conversation.js';
import { RealtimeUtils } from './utils.js';
import { exec } from 'child_process';
import open from 'open';

/**
* Valid audio formats
Expand Down Expand Up @@ -722,4 +724,30 @@ export class RealtimeClient extends RealtimeEventHandler {
const { item } = event;
return { item };
}

/**
* Adds a tool for open commands that takes a file path or URL as a parameter and opens it using the appropriate application
*/
addOpenCommandTool() {
this.addTool(
{
name: 'open_command',
description: 'Opens a file path or URL using the appropriate application.',
parameters: {
type: 'object',
properties: {
path: {
type: 'string',
description: 'File path or URL to open',
},
},
required: ['path'],
},
},
async ({ path }) => {
await open(path);
return { success: true };
},
);
}
}
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
"main": "index.js",
"types": "./dist/index.d.ts",
"scripts": {
"test": "mocha ./test/index.js"
"test": "mocha ./test/index.js",
"verify-installation": "npm ls"
},
"author": "Keith Horwood <[email protected]>",
"license": "MIT",
Expand Down