From 7c795eafe0342e735b09bfde3d96c3852e050b43 Mon Sep 17 00:00:00 2001
From: Philippe <hello@philippec.me>
Date: Fri, 4 Oct 2024 14:36:21 -0400
Subject: [PATCH] improve URL handling for custom queries passed via URL

---
 lib/api.js | 9 ++++++++-
 1 file changed, 8 insertions(+), 1 deletion(-)

diff --git a/lib/api.js b/lib/api.js
index e522263..2032039 100644
--- a/lib/api.js
+++ b/lib/api.js
@@ -73,7 +73,14 @@ export class RealtimeAPI extends RealtimeEventHandler {
         );
       }
       const WebSocket = globalThis.WebSocket;
-      const ws = new WebSocket(`${this.url}${model ? `?model=${model}` : ''}`, [
+      // if the url ends with a query param, add it to the queryParams
+      const urlObj = new URL(this.url);
+      const queryParams = new URLSearchParams(urlObj.search ?? {});
+      queryParams.append('model', model);
+      const queryString = queryParams.toString();
+      const finalUrl = `${this.url}${queryString ? `?${queryString}` : ''}`;
+
+      const ws = new WebSocket(finalUrl, [
         'realtime',
         `openai-insecure-api-key.${this.apiKey}`,
         'openai-beta.realtime-v1',