chore: import upstream snapshot with attribution
CI / Shell Format Check (push) Has been cancelled
CI / Check Ruby (3.4) (push) Has been cancelled
CI / CI Config (push) Has been cancelled
CI / Test on Node ${{ matrix.node }} and ${{ matrix.os }}${{ matrix.shard && format(' (shard {0}/3)', matrix.shard) || '' }} (push) Has been cancelled
CI / Build on Node ${{ matrix.node }} (push) Has been cancelled
CI / Style Check (push) Has been cancelled
CI / Generate Assets (push) Has been cancelled
CI / Check Python (3.14) (push) Has been cancelled
CI / Check Python (3.9) (push) Has been cancelled
CI / Build Docs (push) Has been cancelled
CI / Code Scan Action (push) Has been cancelled
CI / Site tests (push) Has been cancelled
CI / webui tests (push) Has been cancelled
CI / Run Integration Tests (push) Has been cancelled
CI / Run Smoke Tests (push) Has been cancelled
CI / Go Tests (push) Has been cancelled
CI / Share Test (push) Has been cancelled
CI / Redteam (Production API) (push) Has been cancelled
CI / Redteam (Staging API) (push) Has been cancelled
CI / GitHub Actions Lint (push) Has been cancelled
CI / Check Ruby (3.0) (push) Has been cancelled
release-please / release-please (push) Has been cancelled
release-please / build (push) Has been cancelled
release-please / publish-npm (push) Has been cancelled
release-please / publish-npm-backfill (push) Has been cancelled
release-please / docker (push) Has been cancelled
release-please / publish-code-scan-action (push) Has been cancelled
release-please / attest-code-scan-action (push) Has been cancelled
Deploy local.promptfoo.app / Deploy to Cloudflare Pages (push) Has been cancelled
Test and Publish Multi-arch Docker Image / test (push) Has been cancelled
Test and Publish Multi-arch Docker Image / build-docker-and-push-digests (map[digest-suffix:linux-amd64 platform:linux/amd64 runner:ubuntu-latest]) (push) Has been cancelled
Test and Publish Multi-arch Docker Image / build-docker-and-push-digests (map[digest-suffix:linux-arm64 platform:linux/arm64 runner:ubuntu-24.04-arm]) (push) Has been cancelled
Test and Publish Multi-arch Docker Image / merge-docker-digests (push) Has been cancelled
Test and Publish Multi-arch Docker Image / Attest Multi-arch Image (push) Has been cancelled
Validate Renovate Config / Validate Renovate Configuration (push) Has been cancelled
CI / Shell Format Check (push) Has been cancelled
CI / Check Ruby (3.4) (push) Has been cancelled
CI / CI Config (push) Has been cancelled
CI / Test on Node ${{ matrix.node }} and ${{ matrix.os }}${{ matrix.shard && format(' (shard {0}/3)', matrix.shard) || '' }} (push) Has been cancelled
CI / Build on Node ${{ matrix.node }} (push) Has been cancelled
CI / Style Check (push) Has been cancelled
CI / Generate Assets (push) Has been cancelled
CI / Check Python (3.14) (push) Has been cancelled
CI / Check Python (3.9) (push) Has been cancelled
CI / Build Docs (push) Has been cancelled
CI / Code Scan Action (push) Has been cancelled
CI / Site tests (push) Has been cancelled
CI / webui tests (push) Has been cancelled
CI / Run Integration Tests (push) Has been cancelled
CI / Run Smoke Tests (push) Has been cancelled
CI / Go Tests (push) Has been cancelled
CI / Share Test (push) Has been cancelled
CI / Redteam (Production API) (push) Has been cancelled
CI / Redteam (Staging API) (push) Has been cancelled
CI / GitHub Actions Lint (push) Has been cancelled
CI / Check Ruby (3.0) (push) Has been cancelled
release-please / release-please (push) Has been cancelled
release-please / build (push) Has been cancelled
release-please / publish-npm (push) Has been cancelled
release-please / publish-npm-backfill (push) Has been cancelled
release-please / docker (push) Has been cancelled
release-please / publish-code-scan-action (push) Has been cancelled
release-please / attest-code-scan-action (push) Has been cancelled
Deploy local.promptfoo.app / Deploy to Cloudflare Pages (push) Has been cancelled
Test and Publish Multi-arch Docker Image / test (push) Has been cancelled
Test and Publish Multi-arch Docker Image / build-docker-and-push-digests (map[digest-suffix:linux-amd64 platform:linux/amd64 runner:ubuntu-latest]) (push) Has been cancelled
Test and Publish Multi-arch Docker Image / build-docker-and-push-digests (map[digest-suffix:linux-arm64 platform:linux/arm64 runner:ubuntu-24.04-arm]) (push) Has been cancelled
Test and Publish Multi-arch Docker Image / merge-docker-digests (push) Has been cancelled
Test and Publish Multi-arch Docker Image / Attest Multi-arch Image (push) Has been cancelled
Validate Renovate Config / Validate Renovate Configuration (push) Has been cancelled
This commit is contained in:
@@ -0,0 +1,101 @@
|
||||
# config-websockets/streaming/server (OpenAI WebSocket Server)
|
||||
|
||||
Simple Node.js server using Express and native WebSockets that exposes two real-time endpoints to interact with the OpenAI Responses API.
|
||||
|
||||
## Requirements
|
||||
|
||||
- Node.js >= 18.17
|
||||
- An OpenAI API key
|
||||
|
||||
## Setup
|
||||
|
||||
1. Install dependencies:
|
||||
|
||||
```bash
|
||||
npm install
|
||||
```
|
||||
|
||||
2. Configure environment:
|
||||
|
||||
- set `OPENAI_API_KEY` in your environment or use .env file
|
||||
|
||||
```bash
|
||||
cp env.example .env
|
||||
# edit .env and set OPENAI_API_KEY
|
||||
```
|
||||
|
||||
3. Start the server (defaults to port 3300):
|
||||
|
||||
```bash
|
||||
npm start
|
||||
```
|
||||
|
||||
You can also run in dev mode with automatic restarts:
|
||||
|
||||
```bash
|
||||
npm run dev
|
||||
```
|
||||
|
||||
## HTTP
|
||||
|
||||
- Health check: `GET /health` → `{ "status": "ok" }`
|
||||
|
||||
## Real-time (WebSocket)
|
||||
|
||||
Two WebSocket upgrade paths are provided:
|
||||
|
||||
- `/ws` — non-streaming. Emits a single `response` when the OpenAI request completes.
|
||||
- `/ws-stream` — streaming. Emits incremental `delta` and `message` events, then `done`.
|
||||
|
||||
Both endpoints accept the same request payload (model is configured via env):
|
||||
|
||||
```json
|
||||
{ "input": "Hello there!" }
|
||||
```
|
||||
|
||||
Model is read from `CHATBOT_MODEL` env var and defaults to `gpt-4.1-mini`.
|
||||
|
||||
### Client examples
|
||||
|
||||
Non-streaming (`/ws`):
|
||||
|
||||
```js
|
||||
const ws = new WebSocket('ws://localhost:3300/ws');
|
||||
|
||||
ws.onopen = () => {
|
||||
ws.send(JSON.stringify({ input: 'Hello there!' }));
|
||||
};
|
||||
|
||||
ws.onmessage = (event) => {
|
||||
const msg = JSON.parse(event.data);
|
||||
// msg.type: 'ready' | 'response' | 'done' | 'error'
|
||||
console.log(msg);
|
||||
};
|
||||
|
||||
ws.onerror = (err) => console.error('ws error', err);
|
||||
```
|
||||
|
||||
Streaming (`/ws-stream`):
|
||||
|
||||
```js
|
||||
const ws = new WebSocket('ws://localhost:3300/ws-stream');
|
||||
|
||||
ws.onopen = () => {
|
||||
ws.send(JSON.stringify({ input: 'Stream this please' }));
|
||||
};
|
||||
|
||||
ws.onmessage = (event) => {
|
||||
const msg = JSON.parse(event.data);
|
||||
// msg.type: 'ready' | 'delta' | 'message' | 'done' | 'error'
|
||||
if (msg.type === 'delta') process.stdout.write(msg.message || '');
|
||||
else console.log(msg);
|
||||
};
|
||||
|
||||
ws.onerror = (err) => console.error('ws error', err);
|
||||
```
|
||||
|
||||
## Notes
|
||||
|
||||
- Configure port via `PORT` in `.env`.
|
||||
- Configure model via `CHATBOT_MODEL` in `.env` (default: `gpt-4.1-mini`).
|
||||
- Health route is `GET /health`. WebSocket upgrade paths are `/ws` and `/ws-stream`.
|
||||
@@ -0,0 +1,26 @@
|
||||
{
|
||||
"name": "openai-ws-server",
|
||||
"version": "0.1.0",
|
||||
"license": "MIT",
|
||||
"private": true,
|
||||
"type": "module",
|
||||
"description": "Express server with WebSocket endpoint that calls OpenAI Responses API",
|
||||
"engines": {
|
||||
"node": ">=18.20.8"
|
||||
},
|
||||
"scripts": {
|
||||
"start": "node server.js",
|
||||
"dev": "nodemon --watch server.js server.js",
|
||||
"lint": "echo \"No linter configured\""
|
||||
},
|
||||
"dependencies": {
|
||||
"cors": "^2.8.6",
|
||||
"dotenv": "^17.2.3",
|
||||
"express": "^5.2.1",
|
||||
"openai": "^6.37.0",
|
||||
"ws": "^8.19.0"
|
||||
},
|
||||
"devDependencies": {
|
||||
"nodemon": "^3.1.11"
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,196 @@
|
||||
import http from 'http';
|
||||
|
||||
import cors from 'cors';
|
||||
import dotenv from 'dotenv';
|
||||
import express from 'express';
|
||||
import OpenAI from 'openai';
|
||||
import WebSocket, { WebSocketServer } from 'ws';
|
||||
|
||||
dotenv.config();
|
||||
|
||||
function validateEnvironmentVariables() {
|
||||
const missing = [];
|
||||
if (!process.env.OPENAI_API_KEY) {
|
||||
missing.push('OPENAI_API_KEY');
|
||||
}
|
||||
if (missing.length > 0) {
|
||||
// eslint-disable-next-line no-console
|
||||
console.error(`Missing required environment variables: ${missing.join(', ')}`);
|
||||
process.exit(1);
|
||||
}
|
||||
}
|
||||
|
||||
validateEnvironmentVariables();
|
||||
|
||||
const PORT = Number(process.env.PORT || 3300);
|
||||
const app = express();
|
||||
const server = http.createServer(app);
|
||||
const wss = new WebSocketServer({ noServer: true });
|
||||
const wssStream = new WebSocketServer({ noServer: true });
|
||||
const openai = new OpenAI({ apiKey: process.env.OPENAI_API_KEY });
|
||||
const DEFAULT_MODEL = 'gpt-4.1-mini';
|
||||
const CHATBOT_MODEL =
|
||||
process.env.CHATBOT_MODEL && process.env.CHATBOT_MODEL.trim().length > 0
|
||||
? process.env.CHATBOT_MODEL
|
||||
: DEFAULT_MODEL;
|
||||
|
||||
app.use(cors());
|
||||
app.use(express.json());
|
||||
|
||||
app.get('/health', (_req, res) => {
|
||||
res.json({ status: 'ok' });
|
||||
});
|
||||
|
||||
// WebSocket: /ws (non-streaming)
|
||||
wss.on('connection', (ws, request) => {
|
||||
const clientAddress = request.socket.remoteAddress;
|
||||
// eslint-disable-next-line no-console
|
||||
console.log(`WS client connected to /ws${clientAddress ? `: ${clientAddress}` : ''}`);
|
||||
|
||||
ws.on('message', async (raw) => {
|
||||
let payload;
|
||||
try {
|
||||
payload = JSON.parse(raw.toString());
|
||||
} catch {
|
||||
ws.send(JSON.stringify({ type: 'error', error: 'Invalid JSON payload' }));
|
||||
return;
|
||||
}
|
||||
|
||||
const { input } = payload || {};
|
||||
if (typeof input !== 'string' || input.trim().length === 0) {
|
||||
ws.send(
|
||||
JSON.stringify({
|
||||
type: 'error',
|
||||
error: 'Missing "input" (non-empty string) in payload',
|
||||
}),
|
||||
);
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
const response = await openai.responses.create({
|
||||
model: CHATBOT_MODEL,
|
||||
input,
|
||||
});
|
||||
const text = response.output[0].content[0].text;
|
||||
ws.send(JSON.stringify({ type: 'message', message: text, raw: response }));
|
||||
} catch (err) {
|
||||
console.error(err);
|
||||
const message = err && err.message ? err.message : 'OpenAI request failed';
|
||||
ws.send(JSON.stringify({ type: 'error', error: message }));
|
||||
}
|
||||
});
|
||||
|
||||
ws.on('close', () => {
|
||||
// eslint-disable-next-line no-console
|
||||
console.log('WS client disconnected from /ws');
|
||||
});
|
||||
});
|
||||
|
||||
// WebSocket: /ws-stream (streaming)
|
||||
wssStream.on('connection', (ws, request) => {
|
||||
const clientAddress = request.socket.remoteAddress;
|
||||
// eslint-disable-next-line no-console
|
||||
console.log(`WS client connected to /ws-stream${clientAddress ? `: ${clientAddress}` : ''}`);
|
||||
|
||||
if (ws.readyState === WebSocket.OPEN) {
|
||||
ws.send(JSON.stringify({ type: 'ready', message: 'Connected to /ws-stream' }));
|
||||
}
|
||||
|
||||
ws.on('message', async (raw) => {
|
||||
let payload;
|
||||
try {
|
||||
payload = JSON.parse(raw.toString());
|
||||
} catch {
|
||||
console.error('Invalid JSON payload', raw);
|
||||
ws.send(JSON.stringify({ type: 'error', message: 'Invalid JSON payload' }));
|
||||
return;
|
||||
}
|
||||
|
||||
const { input } = payload || {};
|
||||
if (typeof input !== 'string' || input.trim().length === 0) {
|
||||
ws.send(
|
||||
JSON.stringify({
|
||||
type: 'error',
|
||||
error: 'Missing "input" (non-empty string) in payload',
|
||||
}),
|
||||
);
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
const stream = await openai.responses.stream(
|
||||
{
|
||||
model: CHATBOT_MODEL,
|
||||
input,
|
||||
},
|
||||
{ stream: true },
|
||||
);
|
||||
|
||||
stream.on('response.output_text.delta', (delta) => {
|
||||
if (ws.readyState === WebSocket.OPEN) {
|
||||
ws.send(JSON.stringify({ type: 'delta', message: delta.delta }));
|
||||
}
|
||||
});
|
||||
|
||||
stream.on('response.output_text.done', ({ text }) => {
|
||||
if (ws.readyState === WebSocket.OPEN) {
|
||||
ws.send(JSON.stringify({ type: 'message', message: text }));
|
||||
}
|
||||
});
|
||||
|
||||
stream.on('error', (err) => {
|
||||
if (ws.readyState === WebSocket.OPEN) {
|
||||
ws.send(
|
||||
JSON.stringify({
|
||||
type: 'error',
|
||||
message: err && err.message ? err.message : 'Unknown streaming error',
|
||||
}),
|
||||
);
|
||||
}
|
||||
});
|
||||
|
||||
await stream.done();
|
||||
// Debug: mark stream completion
|
||||
if (ws.readyState === WebSocket.OPEN) {
|
||||
ws.send(JSON.stringify({ type: 'done' }));
|
||||
}
|
||||
} catch (err) {
|
||||
const message = err && err.message ? err.message : 'OpenAI request failed';
|
||||
if (ws.readyState === WebSocket.OPEN) {
|
||||
ws.send(JSON.stringify({ type: 'error', message: message }));
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
ws.on('close', () => {
|
||||
// eslint-disable-next-line no-console
|
||||
console.log('WS client disconnected from /ws-stream');
|
||||
});
|
||||
});
|
||||
|
||||
server.on('upgrade', (request, socket, head) => {
|
||||
const url = request.url || '';
|
||||
if (url === '/ws' || url.startsWith('/ws?')) {
|
||||
wss.handleUpgrade(request, socket, head, (ws) => {
|
||||
wss.emit('connection', ws, request);
|
||||
});
|
||||
} else if (url === '/ws-stream' || url.startsWith('/ws-stream?')) {
|
||||
wssStream.handleUpgrade(request, socket, head, (ws) => {
|
||||
wssStream.emit('connection', ws, request);
|
||||
});
|
||||
} else {
|
||||
socket.destroy();
|
||||
}
|
||||
});
|
||||
|
||||
server.listen(PORT, () => {
|
||||
// eslint-disable-next-line no-console
|
||||
console.log(`Server listening on http://localhost:${PORT}`);
|
||||
});
|
||||
|
||||
process.on('SIGINT', () => {
|
||||
// eslint-disable-next-line no-console
|
||||
console.log('\nGracefully shutting down...');
|
||||
server.close(() => process.exit(0));
|
||||
});
|
||||
Reference in New Issue
Block a user