ws.js 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.OpenAIRealtimeWS = void 0;
  4. const tslib_1 = require("../internal/tslib.js");
  5. const WS = tslib_1.__importStar(require("ws"));
  6. const index_1 = require("../index.js");
  7. const internal_base_1 = require("./internal-base.js");
  8. class OpenAIRealtimeWS extends internal_base_1.OpenAIRealtimeEmitter {
  9. constructor(props, client) {
  10. super();
  11. client ?? (client = new index_1.OpenAI());
  12. const hasProvider = typeof client?._options?.apiKey === 'function';
  13. if (hasProvider && !props.__resolvedApiKey) {
  14. throw new Error([
  15. 'Cannot open Realtime WebSocket with a function-based apiKey.',
  16. 'Use the .create() method so that the key is resolved before connecting:',
  17. 'await OpenAIRealtimeWS.create(client, { model })',
  18. ].join('\n'));
  19. }
  20. this.url = (0, internal_base_1.buildRealtimeURL)(client, props.model);
  21. this.socket = new WS.WebSocket(this.url, {
  22. ...props.options,
  23. headers: {
  24. ...props.options?.headers,
  25. ...((0, internal_base_1.isAzure)(client) && !props.__resolvedApiKey ? {} : { Authorization: `Bearer ${client.apiKey}` }),
  26. },
  27. });
  28. this.socket.on('message', (wsEvent) => {
  29. const event = (() => {
  30. try {
  31. return JSON.parse(wsEvent.toString());
  32. }
  33. catch (err) {
  34. this._onError(null, 'could not parse websocket event', err);
  35. return null;
  36. }
  37. })();
  38. if (event) {
  39. this._emit('event', event);
  40. if (event.type === 'error') {
  41. this._onError(event);
  42. }
  43. else {
  44. // @ts-expect-error TS isn't smart enough to get the relationship right here
  45. this._emit(event.type, event);
  46. }
  47. }
  48. });
  49. this.socket.on('error', (err) => {
  50. this._onError(null, err.message, err);
  51. });
  52. }
  53. static async create(client, props) {
  54. return new OpenAIRealtimeWS({ ...props, __resolvedApiKey: await client._callApiKey() }, client);
  55. }
  56. static async azure(client, props = {}) {
  57. const isApiKeyProvider = await client._callApiKey();
  58. const deploymentName = props.deploymentName ?? client.deploymentName;
  59. if (!deploymentName) {
  60. throw new Error('No deployment name provided');
  61. }
  62. return new OpenAIRealtimeWS({
  63. model: deploymentName,
  64. options: {
  65. ...props.options,
  66. headers: {
  67. ...props.options?.headers,
  68. ...(isApiKeyProvider ? {} : { 'api-key': client.apiKey }),
  69. },
  70. },
  71. __resolvedApiKey: isApiKeyProvider,
  72. }, client);
  73. }
  74. send(event) {
  75. try {
  76. this.socket.send(JSON.stringify(event));
  77. }
  78. catch (err) {
  79. this._onError(null, 'could not send data', err);
  80. }
  81. }
  82. close(props) {
  83. try {
  84. this.socket.close(props?.code ?? 1000, props?.reason ?? 'OK');
  85. }
  86. catch (err) {
  87. this._onError(null, 'could not close the connection', err);
  88. }
  89. }
  90. }
  91. exports.OpenAIRealtimeWS = OpenAIRealtimeWS;
  92. //# sourceMappingURL=ws.js.map