diff --git a/src/apis/threads.ts b/src/apis/threads.ts index bedc7c5..768551d 100644 --- a/src/apis/threads.ts +++ b/src/apis/threads.ts @@ -1,7 +1,6 @@ import { ApiClientInterface } from '../_types/generalTypes'; import { ApiResource } from '../apiResource'; import { RequestOptions } from '../baseClient'; - import { finalResponse, initOpenAIClient, overrideConfig } from '../utils'; import { createHeaders } from './createHeaders'; @@ -113,6 +112,8 @@ export class Threads extends ApiResource { opts?: RequestOptions ): Promise { const body: ThreadCreateAndRunParams = _body; + const { stream } = body; + if (params) { const config = overrideConfig(this.client.config, params.config); this.client.customHeaders = { @@ -123,11 +124,19 @@ export class Threads extends ApiResource { const OAIclient = initOpenAIClient(this.client); - const result = await OAIclient.beta.threads - .createAndRun(body, opts) - .withResponse(); - - return finalResponse(result); + if (stream === true) { + const streamResponse = await OAIclient.beta.threads.createAndRunStream( + body as any, + opts + ); + return streamResponse; + } else { + const result = await OAIclient.beta.threads + .createAndRun(body, opts) + .withResponse(); + + return finalResponse(result); + } } async createAndRunPoll( @@ -286,6 +295,7 @@ export class Runs extends ApiResource { opts?: RequestOptions ): Promise { const body: RunCreateParams = _body; + const { stream } = body; if (params) { const config = overrideConfig(this.client.config, params.config); this.client.customHeaders = { @@ -296,11 +306,20 @@ export class Runs extends ApiResource { const OAIclient = initOpenAIClient(this.client); - const result = await OAIclient.beta.threads.runs - .create(threadId, body, opts) - .withResponse(); - - return finalResponse(result); + if (stream === true) { + const streamResponse = await OAIclient.beta.threads.runs.stream( + threadId, + body as any, + opts + ); + return streamResponse; + } else { + const result = await OAIclient.beta.threads.runs + .create(threadId, body, opts) + .withResponse(); + + return finalResponse(result); + } } async list( @@ -685,6 +704,7 @@ export interface RunCreateParams { metadata?: unknown | null; model?: string | null; tools?: Array | null; + stream?: boolean | null; } export interface RunCreateParamsNonStreaming extends RunCreateParams { @@ -698,6 +718,7 @@ export interface ThreadCreateAndRunParams { model?: string | null; thread?: any; tools?: Array | null; + stream?: boolean | null; } export interface ThreadCreateAndRunParamsNonStreaming diff --git a/src/baseClient.ts b/src/baseClient.ts index f90f64d..dd67916 100644 --- a/src/baseClient.ts +++ b/src/baseClient.ts @@ -57,7 +57,7 @@ async function defaultParseResponse(props: APIResponseProps): Promise { if (contentType?.includes('application/json')) { const headers = defaultParseHeaders(props); const json = { - ...(await response.json() as any), + ...((await response.json()) as any), getHeaders: () => headers, }; diff --git a/src/utils.ts b/src/utils.ts index 033bc26..20f8c4c 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -163,7 +163,8 @@ export function toQueryParams( | VirtualKeysListParams | ApiKeysListParams | CongfigsListParams - | LogsExportListParams|any + | LogsExportListParams + | any ): string { if (!params) { return '';