EventStream.d.ts 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. import { APIUserAbortError, OpenAIError } from "../error.js";
  2. export declare class EventStream<EventTypes extends BaseEvents> {
  3. #private;
  4. controller: AbortController;
  5. constructor();
  6. protected _run(this: EventStream<EventTypes>, executor: () => Promise<any>): void;
  7. protected _connected(this: EventStream<EventTypes>): void;
  8. get ended(): boolean;
  9. get errored(): boolean;
  10. get aborted(): boolean;
  11. abort(): void;
  12. /**
  13. * Adds the listener function to the end of the listeners array for the event.
  14. * No checks are made to see if the listener has already been added. Multiple calls passing
  15. * the same combination of event and listener will result in the listener being added, and
  16. * called, multiple times.
  17. * @returns this ChatCompletionStream, so that calls can be chained
  18. */
  19. on<Event extends keyof EventTypes>(event: Event, listener: EventListener<EventTypes, Event>): this;
  20. /**
  21. * Removes the specified listener from the listener array for the event.
  22. * off() will remove, at most, one instance of a listener from the listener array. If any single
  23. * listener has been added multiple times to the listener array for the specified event, then
  24. * off() must be called multiple times to remove each instance.
  25. * @returns this ChatCompletionStream, so that calls can be chained
  26. */
  27. off<Event extends keyof EventTypes>(event: Event, listener: EventListener<EventTypes, Event>): this;
  28. /**
  29. * Adds a one-time listener function for the event. The next time the event is triggered,
  30. * this listener is removed and then invoked.
  31. * @returns this ChatCompletionStream, so that calls can be chained
  32. */
  33. once<Event extends keyof EventTypes>(event: Event, listener: EventListener<EventTypes, Event>): this;
  34. /**
  35. * This is similar to `.once()`, but returns a Promise that resolves the next time
  36. * the event is triggered, instead of calling a listener callback.
  37. * @returns a Promise that resolves the next time given event is triggered,
  38. * or rejects if an error is emitted. (If you request the 'error' event,
  39. * returns a promise that resolves with the error).
  40. *
  41. * Example:
  42. *
  43. * const message = await stream.emitted('message') // rejects if the stream errors
  44. */
  45. emitted<Event extends keyof EventTypes>(event: Event): Promise<EventParameters<EventTypes, Event> extends [infer Param] ? Param : EventParameters<EventTypes, Event> extends [] ? void : EventParameters<EventTypes, Event>>;
  46. done(): Promise<void>;
  47. _emit<Event extends keyof BaseEvents>(event: Event, ...args: EventParameters<BaseEvents, Event>): void;
  48. _emit<Event extends keyof EventTypes>(event: Event, ...args: EventParameters<EventTypes, Event>): void;
  49. protected _emitFinal(): void;
  50. }
  51. type EventListener<Events, EventType extends keyof Events> = Events[EventType];
  52. export type EventParameters<Events, EventType extends keyof Events> = {
  53. [Event in EventType]: EventListener<Events, EventType> extends (...args: infer P) => any ? P : never;
  54. }[EventType];
  55. export interface BaseEvents {
  56. connect: () => void;
  57. error: (error: OpenAIError) => void;
  58. abort: (error: APIUserAbortError) => void;
  59. end: () => void;
  60. }
  61. export {};
  62. //# sourceMappingURL=EventStream.d.ts.map