ResponseStream.d.ts 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. import { ResponseTextConfig, type ParsedResponse, type ResponseCreateParamsBase, type ResponseStreamEvent } from "../../resources/responses/responses.js";
  2. import * as Core from "../../core.js";
  3. import OpenAI from "../../index.js";
  4. import { type BaseEvents, EventStream } from "../EventStream.js";
  5. import { type ResponseFunctionCallArgumentsDeltaEvent, type ResponseTextDeltaEvent } from "./EventTypes.js";
  6. import { ParseableToolsParams } from "../ResponsesParser.js";
  7. export type ResponseStreamParams = ResponseCreateAndStreamParams | ResponseStreamByIdParams;
  8. export type ResponseCreateAndStreamParams = Omit<ResponseCreateParamsBase, 'stream'> & {
  9. stream?: true;
  10. };
  11. export type ResponseStreamByIdParams = {
  12. /**
  13. * The ID of the response to stream.
  14. */
  15. response_id: string;
  16. /**
  17. * If provided, the stream will start after the event with the given sequence number.
  18. */
  19. starting_after?: number;
  20. /**
  21. * Configuration options for a text response from the model. Can be plain text or
  22. * structured JSON data. Learn more:
  23. *
  24. * - [Text inputs and outputs](https://platform.openai.com/docs/guides/text)
  25. * - [Structured Outputs](https://platform.openai.com/docs/guides/structured-outputs)
  26. */
  27. text?: ResponseTextConfig;
  28. /**
  29. * An array of tools the model may call while generating a response. When continuing a stream, provide
  30. * the same tools as the original request.
  31. */
  32. tools?: ParseableToolsParams;
  33. };
  34. type ResponseEvents = BaseEvents & Omit<{
  35. [K in ResponseStreamEvent['type']]: (event: Extract<ResponseStreamEvent, {
  36. type: K;
  37. }>) => void;
  38. }, 'response.output_text.delta' | 'response.function_call_arguments.delta'> & {
  39. event: (event: ResponseStreamEvent) => void;
  40. 'response.output_text.delta': (event: ResponseTextDeltaEvent) => void;
  41. 'response.function_call_arguments.delta': (event: ResponseFunctionCallArgumentsDeltaEvent) => void;
  42. };
  43. export type ResponseStreamingParams = Omit<ResponseCreateParamsBase, 'stream'> & {
  44. stream?: true;
  45. };
  46. export declare class ResponseStream<ParsedT = null> extends EventStream<ResponseEvents> implements AsyncIterable<ResponseStreamEvent> {
  47. #private;
  48. constructor(params: ResponseStreamingParams | null);
  49. static createResponse<ParsedT>(client: OpenAI, params: ResponseStreamParams, options?: Core.RequestOptions): ResponseStream<ParsedT>;
  50. protected _createOrRetrieveResponse(client: OpenAI, params: ResponseStreamParams, options?: Core.RequestOptions): Promise<ParsedResponse<ParsedT>>;
  51. [Symbol.asyncIterator](this: ResponseStream<ParsedT>): AsyncIterator<ResponseStreamEvent>;
  52. /**
  53. * @returns a promise that resolves with the final Response, or rejects
  54. * if an error occurred or the stream ended prematurely without producing a REsponse.
  55. */
  56. finalResponse(): Promise<ParsedResponse<ParsedT>>;
  57. }
  58. export {};
  59. //# sourceMappingURL=ResponseStream.d.ts.map