ws.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. "use strict";
  2. var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
  3. if (k2 === undefined) k2 = k;
  4. var desc = Object.getOwnPropertyDescriptor(m, k);
  5. if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
  6. desc = { enumerable: true, get: function() { return m[k]; } };
  7. }
  8. Object.defineProperty(o, k2, desc);
  9. }) : (function(o, m, k, k2) {
  10. if (k2 === undefined) k2 = k;
  11. o[k2] = m[k];
  12. }));
  13. var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
  14. Object.defineProperty(o, "default", { enumerable: true, value: v });
  15. }) : function(o, v) {
  16. o["default"] = v;
  17. });
  18. var __importStar = (this && this.__importStar) || function (mod) {
  19. if (mod && mod.__esModule) return mod;
  20. var result = {};
  21. if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
  22. __setModuleDefault(result, mod);
  23. return result;
  24. };
  25. Object.defineProperty(exports, "__esModule", { value: true });
  26. exports.OpenAIRealtimeWS = void 0;
  27. const WS = __importStar(require("ws"));
  28. const index_1 = require("../../index.js");
  29. const internal_base_1 = require("./internal-base.js");
  30. class OpenAIRealtimeWS extends internal_base_1.OpenAIRealtimeEmitter {
  31. constructor(props, client) {
  32. super();
  33. client ?? (client = new index_1.OpenAI());
  34. this.url = (0, internal_base_1.buildRealtimeURL)(client, props.model);
  35. this.socket = new WS.WebSocket(this.url, {
  36. ...props.options,
  37. headers: {
  38. ...props.options?.headers,
  39. ...((0, internal_base_1.isAzure)(client) ? {} : { Authorization: `Bearer ${client.apiKey}` }),
  40. 'OpenAI-Beta': 'realtime=v1',
  41. },
  42. });
  43. this.socket.on('message', (wsEvent) => {
  44. const event = (() => {
  45. try {
  46. return JSON.parse(wsEvent.toString());
  47. }
  48. catch (err) {
  49. this._onError(null, 'could not parse websocket event', err);
  50. return null;
  51. }
  52. })();
  53. if (event) {
  54. this._emit('event', event);
  55. if (event.type === 'error') {
  56. this._onError(event);
  57. }
  58. else {
  59. // @ts-expect-error TS isn't smart enough to get the relationship right here
  60. this._emit(event.type, event);
  61. }
  62. }
  63. });
  64. this.socket.on('error', (err) => {
  65. this._onError(null, err.message, err);
  66. });
  67. }
  68. static async azure(client, options = {}) {
  69. const deploymentName = options.deploymentName ?? client.deploymentName;
  70. if (!deploymentName) {
  71. throw new Error('No deployment name provided');
  72. }
  73. return new OpenAIRealtimeWS({ model: deploymentName, options: { headers: await getAzureHeaders(client) } }, 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. async function getAzureHeaders(client) {
  94. if (client.apiKey !== '<Missing Key>') {
  95. return { 'api-key': client.apiKey };
  96. }
  97. else {
  98. const token = await client._getAzureADToken();
  99. if (token) {
  100. return { Authorization: `Bearer ${token}` };
  101. }
  102. else {
  103. throw new Error('AzureOpenAI is not instantiated correctly. No API key or token provided.');
  104. }
  105. }
  106. }
  107. //# sourceMappingURL=ws.js.map