ws.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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. 'OpenAI-Beta': 'realtime=v1',
  27. },
  28. });
  29. this.socket.on('message', (wsEvent) => {
  30. const event = (() => {
  31. try {
  32. return JSON.parse(wsEvent.toString());
  33. }
  34. catch (err) {
  35. this._onError(null, 'could not parse websocket event', err);
  36. return null;
  37. }
  38. })();
  39. if (event) {
  40. this._emit('event', event);
  41. if (event.type === 'error') {
  42. this._onError(event);
  43. }
  44. else {
  45. // @ts-expect-error TS isn't smart enough to get the relationship right here
  46. this._emit(event.type, event);
  47. }
  48. }
  49. });
  50. this.socket.on('error', (err) => {
  51. this._onError(null, err.message, err);
  52. });
  53. }
  54. static async create(client, props) {
  55. return new OpenAIRealtimeWS({ ...props, __resolvedApiKey: await client._callApiKey() }, client);
  56. }
  57. static async azure(client, props = {}) {
  58. const isApiKeyProvider = await client._callApiKey();
  59. const deploymentName = props.deploymentName ?? client.deploymentName;
  60. if (!deploymentName) {
  61. throw new Error('No deployment name provided');
  62. }
  63. return new OpenAIRealtimeWS({
  64. model: deploymentName,
  65. options: {
  66. ...props.options,
  67. headers: {
  68. ...props.options?.headers,
  69. ...(isApiKeyProvider ? {} : { 'api-key': client.apiKey }),
  70. },
  71. },
  72. __resolvedApiKey: isApiKeyProvider,
  73. }, client);
  74. }
  75. send(event) {
  76. try {
  77. this.socket.send(JSON.stringify(event));
  78. }
  79. catch (err) {
  80. this._onError(null, 'could not send data', err);
  81. }
  82. }
  83. close(props) {
  84. try {
  85. this.socket.close(props?.code ?? 1000, props?.reason ?? 'OK');
  86. }
  87. catch (err) {
  88. this._onError(null, 'could not close the connection', err);
  89. }
  90. }
  91. }
  92. exports.OpenAIRealtimeWS = OpenAIRealtimeWS;
  93. //# sourceMappingURL=ws.js.map