Repository navigation
Expand file tree
/
Copy pathEventStream.js
More file actions
190 lines (190 loc) · 9.84 KB
/
Copy pathEventStream.js
File metadata and controls
190 lines (190 loc) · 9.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
"use strict";
var _EventStream_instances, _EventStream_connectedPromise, _EventStream_resolveConnectedPromise, _EventStream_rejectConnectedPromise, _EventStream_endPromise, _EventStream_resolveEndPromise, _EventStream_rejectEndPromise, _EventStream_listeners, _EventStream_ended, _EventStream_errored, _EventStream_aborted, _EventStream_catchingPromiseCreated, _EventStream_handleError;
Object.defineProperty(exports, "__esModule", { value: true });
exports.EventStream = void 0;
const tslib_1 = require("../internal/tslib.js");
const error_1 = require("../error.js");
class EventStream {
constructor() {
_EventStream_instances.add(this);
this.controller = new AbortController();
_EventStream_connectedPromise.set(this, void 0);
_EventStream_resolveConnectedPromise.set(this, () => { });
_EventStream_rejectConnectedPromise.set(this, () => { });
_EventStream_endPromise.set(this, void 0);
_EventStream_resolveEndPromise.set(this, () => { });
_EventStream_rejectEndPromise.set(this, () => { });
_EventStream_listeners.set(this, {});
_EventStream_ended.set(this, false);
_EventStream_errored.set(this, false);
_EventStream_aborted.set(this, false);
_EventStream_catchingPromiseCreated.set(this, false);
tslib_1.__classPrivateFieldSet(this, _EventStream_connectedPromise, new Promise((resolve, reject) => {
tslib_1.__classPrivateFieldSet(this, _EventStream_resolveConnectedPromise, resolve, "f");
tslib_1.__classPrivateFieldSet(this, _EventStream_rejectConnectedPromise, reject, "f");
}), "f");
tslib_1.__classPrivateFieldSet(this, _EventStream_endPromise, new Promise((resolve, reject) => {
tslib_1.__classPrivateFieldSet(this, _EventStream_resolveEndPromise, resolve, "f");
tslib_1.__classPrivateFieldSet(this, _EventStream_rejectEndPromise, reject, "f");
}), "f");
// Don't let these promises cause unhandled rejection errors.
// we will manually cause an unhandled rejection error later
// if the user hasn't registered any error listener or called
// any promise-returning method.
tslib_1.__classPrivateFieldGet(this, _EventStream_connectedPromise, "f").catch(() => { });
tslib_1.__classPrivateFieldGet(this, _EventStream_endPromise, "f").catch(() => { });
}
_run(executor) {
// Unfortunately if we call `executor()` immediately we get runtime errors about
// references to `this` before the `super()` constructor call returns.
setTimeout(() => {
executor().then(() => {
this._emitFinal();
this._emit('end');
}, tslib_1.__classPrivateFieldGet(this, _EventStream_instances, "m", _EventStream_handleError).bind(this));
}, 0);
}
_connected() {
if (this.ended)
return;
tslib_1.__classPrivateFieldGet(this, _EventStream_resolveConnectedPromise, "f").call(this);
this._emit('connect');
}
get ended() {
return tslib_1.__classPrivateFieldGet(this, _EventStream_ended, "f");
}
get errored() {
return tslib_1.__classPrivateFieldGet(this, _EventStream_errored, "f");
}
get aborted() {
return tslib_1.__classPrivateFieldGet(this, _EventStream_aborted, "f");
}
abort() {
this.controller.abort();
}
/**
* Adds the listener function to the end of the listeners array for the event.
* No checks are made to see if the listener has already been added. Multiple calls passing
* the same combination of event and listener will result in the listener being added, and
* called, multiple times.
* @returns this ChatCompletionStream, so that calls can be chained
*/
on(event, listener) {
const listeners = tslib_1.__classPrivateFieldGet(this, _EventStream_listeners, "f")[event] || (tslib_1.__classPrivateFieldGet(this, _EventStream_listeners, "f")[event] = []);
listeners.push({ listener });
return this;
}
/**
* Removes the specified listener from the listener array for the event.
* off() will remove, at most, one instance of a listener from the listener array. If any single
* listener has been added multiple times to the listener array for the specified event, then
* off() must be called multiple times to remove each instance.
* @returns this ChatCompletionStream, so that calls can be chained
*/
off(event, listener) {
const listeners = tslib_1.__classPrivateFieldGet(this, _EventStream_listeners, "f")[event];
if (!listeners)
return this;
const index = listeners.findIndex((l) => l.listener === listener);
if (index >= 0)
listeners.splice(index, 1);
return this;
}
/**
* Adds a one-time listener function for the event. The next time the event is triggered,
* this listener is removed and then invoked.
* @returns this ChatCompletionStream, so that calls can be chained
*/
once(event, listener) {
const listeners = tslib_1.__classPrivateFieldGet(this, _EventStream_listeners, "f")[event] || (tslib_1.__classPrivateFieldGet(this, _EventStream_listeners, "f")[event] = []);
listeners.push({ listener, once: true });
return this;
}
/**
* This is similar to `.once()`, but returns a Promise that resolves the next time
* the event is triggered, instead of calling a listener callback.
* @returns a Promise that resolves the next time given event is triggered,
* or rejects if an error is emitted. (If you request the 'error' event,
* returns a promise that resolves with the error).
*
* Example:
*
* const message = await stream.emitted('message') // rejects if the stream errors
*/
emitted(event) {
return new Promise((resolve, reject) => {
tslib_1.__classPrivateFieldSet(this, _EventStream_catchingPromiseCreated, true, "f");
if (event !== 'error')
this.once('error', reject);
this.once(event, resolve);
});
}
async done() {
tslib_1.__classPrivateFieldSet(this, _EventStream_catchingPromiseCreated, true, "f");
await tslib_1.__classPrivateFieldGet(this, _EventStream_endPromise, "f");
}
_emit(event, ...args) {
// make sure we don't emit any events after end
if (tslib_1.__classPrivateFieldGet(this, _EventStream_ended, "f")) {
return;
}
if (event === 'end') {
tslib_1.__classPrivateFieldSet(this, _EventStream_ended, true, "f");
tslib_1.__classPrivateFieldGet(this, _EventStream_resolveEndPromise, "f").call(this);
}
const listeners = tslib_1.__classPrivateFieldGet(this, _EventStream_listeners, "f")[event];
if (listeners) {
tslib_1.__classPrivateFieldGet(this, _EventStream_listeners, "f")[event] = listeners.filter((l) => !l.once);
listeners.forEach(({ listener }) => listener(...args));
}
if (event === 'abort') {
const error = args[0];
if (!tslib_1.__classPrivateFieldGet(this, _EventStream_catchingPromiseCreated, "f") && !listeners?.length) {
Promise.reject(error);
}
tslib_1.__classPrivateFieldGet(this, _EventStream_rejectConnectedPromise, "f").call(this, error);
tslib_1.__classPrivateFieldGet(this, _EventStream_rejectEndPromise, "f").call(this, error);
this._emit('end');
return;
}
if (event === 'error') {
// NOTE: _emit('error', error) should only be called from #handleError().
const error = args[0];
if (!tslib_1.__classPrivateFieldGet(this, _EventStream_catchingPromiseCreated, "f") && !listeners?.length) {
// Trigger an unhandled rejection if the user hasn't registered any error handlers.
// If you are seeing stack traces here, make sure to handle errors via either:
// - runner.on('error', () => ...)
// - await runner.done()
// - await runner.finalChatCompletion()
// - etc.
Promise.reject(error);
}
tslib_1.__classPrivateFieldGet(this, _EventStream_rejectConnectedPromise, "f").call(this, error);
tslib_1.__classPrivateFieldGet(this, _EventStream_rejectEndPromise, "f").call(this, error);
this._emit('end');
}
}
_emitFinal() { }
}
exports.EventStream = EventStream;
_EventStream_connectedPromise = new WeakMap(), _EventStream_resolveConnectedPromise = new WeakMap(), _EventStream_rejectConnectedPromise = new WeakMap(), _EventStream_endPromise = new WeakMap(), _EventStream_resolveEndPromise = new WeakMap(), _EventStream_rejectEndPromise = new WeakMap(), _EventStream_listeners = new WeakMap(), _EventStream_ended = new WeakMap(), _EventStream_errored = new WeakMap(), _EventStream_aborted = new WeakMap(), _EventStream_catchingPromiseCreated = new WeakMap(), _EventStream_instances = new WeakSet(), _EventStream_handleError = function _EventStream_handleError(error) {
tslib_1.__classPrivateFieldSet(this, _EventStream_errored, true, "f");
if (error instanceof Error && error.name === 'AbortError') {
error = new error_1.APIUserAbortError();
}
if (error instanceof error_1.APIUserAbortError) {
tslib_1.__classPrivateFieldSet(this, _EventStream_aborted, true, "f");
return this._emit('abort', error);
}
if (error instanceof error_1.OpenAIError) {
return this._emit('error', error);
}
if (error instanceof Error) {
const openAIError = new error_1.OpenAIError(error.message);
// @ts-ignore
openAIError.cause = error;
return this._emit('error', openAIError);
}
return this._emit('error', new error_1.OpenAIError(String(error)));
};
//# sourceMappingURL=EventStream.js.map