diff --git a/src/apis/assistants.ts b/src/apis/assistants.ts index 7f1f5f1..698ee2f 100644 --- a/src/apis/assistants.ts +++ b/src/apis/assistants.ts @@ -2,7 +2,7 @@ import { ApiClientInterface } from "../_types/generalTypes"; import { ApiResource } from "../apiResource"; import { RequestOptions } from "../baseClient"; import { OPEN_AI_API_KEY, PORTKEY_BASE_URL } from "../constants"; -import { overrideConfig } from "../utils"; +import { finalResponse, overrideConfig } from "../utils"; import { createHeaders } from "./createHeaders"; import OpenAI from "openai"; @@ -75,8 +75,9 @@ export class Assistants extends ApiResource { defaultHeaders: {...this.client.customHeaders, ...this.client.portkeyHeaders}, }); - const result = await OAIclient.beta.assistants.create(body, opts); - return result; + const result = await OAIclient.beta.assistants.create(body, opts).withResponse(); + + return finalResponse(result); } async list( @@ -99,8 +100,9 @@ export class Assistants extends ApiResource { defaultHeaders: {...this.client.customHeaders, ...this.client.portkeyHeaders}, }); - const result = await OAIclient.beta.assistants.list(query, opts); - return result; + const result = await OAIclient.beta.assistants.list(query, opts).withResponse(); + + return finalResponse(result); } async retrieve( @@ -122,8 +124,9 @@ export class Assistants extends ApiResource { defaultHeaders: {...this.client.customHeaders, ...this.client.portkeyHeaders}, }); - const result = await OAIclient.beta.assistants.retrieve(assistantId, opts); - return result; + const result = await OAIclient.beta.assistants.retrieve(assistantId, opts).withResponse(); + + return finalResponse(result); } async update( @@ -147,8 +150,9 @@ export class Assistants extends ApiResource { defaultHeaders: {...this.client.customHeaders, ...this.client.portkeyHeaders}, }); - const result = await OAIclient.beta.assistants.update(assistantId, body, opts); - return result; + const result = await OAIclient.beta.assistants.update(assistantId, body, opts).withResponse(); + + return finalResponse(result); } async del( @@ -170,8 +174,9 @@ export class Assistants extends ApiResource { defaultHeaders: {...this.client.customHeaders, ...this.client.portkeyHeaders}, }); - const result = await OAIclient.beta.assistants.del(assistantId, opts); - return result; + const result = await OAIclient.beta.assistants.del(assistantId, opts).withResponse(); + + return finalResponse(result); } } @@ -200,8 +205,9 @@ export class Files extends ApiResource{ defaultHeaders: {...this.client.customHeaders, ...this.client.portkeyHeaders}, }); - const result = await OAIclient.beta.assistants.files.create(assistantId, body, opts); - return result; + const result = await OAIclient.beta.assistants.files.create(assistantId, body, opts).withResponse(); + + return finalResponse(result); } async list( @@ -225,8 +231,9 @@ export class Files extends ApiResource{ defaultHeaders: {...this.client.customHeaders, ...this.client.portkeyHeaders}, }); - const result = await OAIclient.beta.assistants.files.list(assistantId, query, opts); - return result; + const result = await OAIclient.beta.assistants.files.list(assistantId, query, opts).withResponse(); + + return finalResponse(result); } async retrieve( @@ -249,8 +256,9 @@ export class Files extends ApiResource{ defaultHeaders: {...this.client.customHeaders, ...this.client.portkeyHeaders}, }); - const result = await OAIclient.beta.assistants.files.retrieve(assistantId, fileId, opts); - return result; + const result = await OAIclient.beta.assistants.files.retrieve(assistantId, fileId, opts).withResponse(); + + return finalResponse(result); } async del( @@ -273,8 +281,9 @@ export class Files extends ApiResource{ defaultHeaders: {...this.client.customHeaders, ...this.client.portkeyHeaders}, }); - const result = await OAIclient.beta.assistants.files.del(assistantId, fileId, opts); - return result; + const result = await OAIclient.beta.assistants.files.del(assistantId, fileId, opts).withResponse(); + + return finalResponse(result); } } \ No newline at end of file diff --git a/src/apis/files.ts b/src/apis/files.ts index 5de4c55..71fb6eb 100644 --- a/src/apis/files.ts +++ b/src/apis/files.ts @@ -2,7 +2,7 @@ import { ApiClientInterface } from "../_types/generalTypes"; import { ApiResource } from "../apiResource"; import { RequestOptions } from "../baseClient"; import { OPEN_AI_API_KEY, PORTKEY_BASE_URL } from "../constants"; -import { overrideConfig } from "../utils"; +import { finalResponse, overrideConfig } from "../utils"; import { createHeaders } from "./createHeaders"; import OpenAI from "openai"; @@ -28,8 +28,9 @@ export class MainFiles extends ApiResource { defaultHeaders: {...this.client.customHeaders, ...this.client.portkeyHeaders}, }); - const result = await OAIclient.files.create(body, opts); - return result; + const result = await OAIclient.files.create(body, opts).withResponse(); + + return finalResponse(result); } async list( @@ -52,8 +53,9 @@ export class MainFiles extends ApiResource { defaultHeaders: {...this.client.customHeaders, ...this.client.portkeyHeaders}, }); - const result = await OAIclient.files.list(query, opts); - return result; + const result = await OAIclient.files.list(query, opts).withResponse(); + + return finalResponse(result); } async retrieve( @@ -75,8 +77,9 @@ export class MainFiles extends ApiResource { defaultHeaders: {...this.client.customHeaders, ...this.client.portkeyHeaders}, }); - const result = await OAIclient.files.retrieve(fileId, opts); - return result; + const result = await OAIclient.files.retrieve(fileId, opts).withResponse(); + + return finalResponse(result); } async del( @@ -98,8 +101,9 @@ export class MainFiles extends ApiResource { defaultHeaders: {...this.client.customHeaders, ...this.client.portkeyHeaders}, }); - const result = await OAIclient.files.del(fileId, opts); - return result; + const result = await OAIclient.files.del(fileId, opts).withResponse(); + + return finalResponse(result); } async retrieveContent( @@ -121,8 +125,9 @@ export class MainFiles extends ApiResource { defaultHeaders: {...this.client.customHeaders, ...this.client.portkeyHeaders}, }); - const result = await OAIclient.files.content(fileId, opts); - return result; + const result = await OAIclient.files.content(fileId, opts).withResponse(); + + return finalResponse(result); } } diff --git a/src/apis/images.ts b/src/apis/images.ts index 8714d36..0c2c56d 100644 --- a/src/apis/images.ts +++ b/src/apis/images.ts @@ -2,7 +2,7 @@ import { ApiClientInterface } from "../_types/generalTypes"; import { ApiResource } from "../apiResource"; import { RequestOptions } from "../baseClient"; import { OPEN_AI_API_KEY, PORTKEY_BASE_URL } from "../constants"; -import { overrideConfig } from "../utils"; +import { finalResponse, overrideConfig } from "../utils"; import { createHeaders } from "./createHeaders"; import OpenAI from "openai"; @@ -69,8 +69,9 @@ export class Images extends ApiResource { defaultHeaders: {...this.client.customHeaders, ...this.client.portkeyHeaders}, }); - const result = await OAIclient.images.generate(body, opts); - return result; + const result = await OAIclient.images.generate(body, opts).withResponse(); + + return finalResponse(result); } async edit( @@ -93,8 +94,9 @@ export class Images extends ApiResource { defaultHeaders: {...this.client.customHeaders, ...this.client.portkeyHeaders}, }); - const result = await OAIclient.images.edit(body, opts); - return result; + const result = await OAIclient.images.edit(body, opts).withResponse(); + + return finalResponse(result); } async createVariation( @@ -117,7 +119,8 @@ export class Images extends ApiResource { defaultHeaders: {...this.client.customHeaders, ...this.client.portkeyHeaders}, }); - const result = await OAIclient.images.createVariation(body, opts); - return result; + const result = await OAIclient.images.createVariation(body, opts).withResponse(); + + return finalResponse(result); } } diff --git a/src/apis/models.ts b/src/apis/models.ts index 1cc7531..95b3c2e 100644 --- a/src/apis/models.ts +++ b/src/apis/models.ts @@ -2,7 +2,7 @@ import { ApiClientInterface } from "../_types/generalTypes"; import { ApiResource } from "../apiResource"; import { RequestOptions } from "../baseClient"; import { OPEN_AI_API_KEY, PORTKEY_BASE_URL } from "../constants"; -import { overrideConfig } from "../utils"; +import { finalResponse, overrideConfig } from "../utils"; import { createHeaders } from "./createHeaders"; import OpenAI from "openai"; @@ -22,8 +22,9 @@ export class Models extends ApiResource { defaultHeaders: {...this.client.customHeaders, ...this.client.portkeyHeaders}, }); - const result = await OAIclient.models.list(opts); - return result; + const result = await OAIclient.models.list(opts).withResponse(); + + return finalResponse(result); } async retrieve( @@ -45,8 +46,9 @@ export class Models extends ApiResource { defaultHeaders: {...this.client.customHeaders, ...this.client.portkeyHeaders}, }); - const result = await OAIclient.models.retrieve(model, opts); - return result; + const result = await OAIclient.models.retrieve(model, opts).withResponse(); + + return finalResponse(result); } async del( @@ -68,7 +70,8 @@ export class Models extends ApiResource { defaultHeaders: {...this.client.customHeaders, ...this.client.portkeyHeaders}, }); - const result = await OAIclient.models.del(model, opts); - return result; + const result = await OAIclient.models.del(model, opts).withResponse(); + + return finalResponse(result); } } diff --git a/src/apis/threads.ts b/src/apis/threads.ts index 00f99f4..c6bf311 100644 --- a/src/apis/threads.ts +++ b/src/apis/threads.ts @@ -3,7 +3,7 @@ import { ApiClientInterface } from "../_types/generalTypes"; import { ApiResource } from "../apiResource"; import { RequestOptions } from "../baseClient"; import { OPEN_AI_API_KEY, PORTKEY_BASE_URL } from "../constants"; -import { overrideConfig } from "../utils"; +import { finalResponse, overrideConfig } from "../utils"; import { createHeaders } from "./createHeaders"; import OpenAI from "openai"; @@ -39,8 +39,9 @@ export class Threads extends ApiResource { defaultHeaders: {...this.client.customHeaders, ...this.client.portkeyHeaders}, }); - const result = await OAIclient.beta.threads.create(body, opts); - return result; + const result = await OAIclient.beta.threads.create(body, opts).withResponse(); + + return finalResponse(result); } async retrieve( @@ -62,8 +63,9 @@ export class Threads extends ApiResource { defaultHeaders: {...this.client.customHeaders, ...this.client.portkeyHeaders}, }); - const result = await OAIclient.beta.threads.retrieve(threadId, opts); - return result; + const result = await OAIclient.beta.threads.retrieve(threadId, opts).withResponse(); + + return finalResponse(result); } async update( @@ -87,8 +89,9 @@ export class Threads extends ApiResource { defaultHeaders: {...this.client.customHeaders, ...this.client.portkeyHeaders}, }); - const result = await OAIclient.beta.threads.update(threadId, body, opts); - return result; + const result = await OAIclient.beta.threads.update(threadId, body, opts).withResponse(); + + return finalResponse(result); } async del( @@ -110,8 +113,9 @@ export class Threads extends ApiResource { defaultHeaders: {...this.client.customHeaders, ...this.client.portkeyHeaders}, }); - const result = await OAIclient.beta.threads.del(threadId, opts); - return result; + const result = await OAIclient.beta.threads.del(threadId, opts).withResponse(); + + return finalResponse(result); } async createAndRun( @@ -134,8 +138,9 @@ export class Threads extends ApiResource { defaultHeaders: {...this.client.customHeaders, ...this.client.portkeyHeaders}, }); - const result = await OAIclient.beta.threads.createAndRun(body, opts); - return result; + const result = await OAIclient.beta.threads.createAndRun(body, opts).withResponse(); + + return finalResponse(result); } } @@ -171,8 +176,9 @@ export class Messages extends ApiResource{ defaultHeaders: {...this.client.customHeaders, ...this.client.portkeyHeaders}, }); - const result = await OAIclient.beta.threads.messages.create(threadId, body, opts); - return result; + const result = await OAIclient.beta.threads.messages.create(threadId, body, opts).withResponse(); + + return finalResponse(result); } async list( @@ -196,8 +202,9 @@ export class Messages extends ApiResource{ defaultHeaders: {...this.client.customHeaders, ...this.client.portkeyHeaders}, }); - const result = await OAIclient.beta.threads.messages.list(threadId, query, opts); - return result; + const result = await OAIclient.beta.threads.messages.list(threadId, query, opts).withResponse(); + + return finalResponse(result); } async retrieve( @@ -220,8 +227,9 @@ export class Messages extends ApiResource{ defaultHeaders: {...this.client.customHeaders, ...this.client.portkeyHeaders}, }); - const result = await OAIclient.beta.threads.messages.retrieve(threadId, messageId, opts); - return result; + const result = await OAIclient.beta.threads.messages.retrieve(threadId, messageId, opts).withResponse(); + + return finalResponse(result); } @@ -247,8 +255,9 @@ export class Messages extends ApiResource{ defaultHeaders: {...this.client.customHeaders, ...this.client.portkeyHeaders}, }); - const result = await OAIclient.beta.threads.messages.update(threadId, messageId, body, opts); - return result; + const result = await OAIclient.beta.threads.messages.update(threadId, messageId, body, opts).withResponse(); + + return finalResponse(result); } @@ -278,8 +287,9 @@ export class Files extends ApiResource{ defaultHeaders: {...this.client.customHeaders, ...this.client.portkeyHeaders}, }); - const result = await OAIclient.beta.threads.messages.files.list(threadId, messageId, query, opts); - return result; + const result = await OAIclient.beta.threads.messages.files.list(threadId, messageId, query, opts).withResponse(); + + return finalResponse(result); } async retrieve( @@ -303,8 +313,9 @@ export class Files extends ApiResource{ defaultHeaders: {...this.client.customHeaders, ...this.client.portkeyHeaders}, }); - const result = await OAIclient.beta.threads.messages.files.retrieve(threadId, messageId, fileId, opts); - return result; + const result = await OAIclient.beta.threads.messages.files.retrieve(threadId, messageId, fileId, opts).withResponse(); + + return finalResponse(result); } } @@ -340,8 +351,9 @@ export class Runs extends ApiResource{ defaultHeaders: {...this.client.customHeaders, ...this.client.portkeyHeaders}, }); - const result = await OAIclient.beta.threads.runs.create(threadId, body, opts); - return result; + const result = await OAIclient.beta.threads.runs.create(threadId, body, opts).withResponse(); + + return finalResponse(result); } async list( @@ -365,8 +377,9 @@ export class Runs extends ApiResource{ defaultHeaders: {...this.client.customHeaders, ...this.client.portkeyHeaders}, }); - const result = await OAIclient.beta.threads.runs.list(threadId, query, opts); - return result; + const result = await OAIclient.beta.threads.runs.list(threadId, query, opts).withResponse(); + + return finalResponse(result); } async retrieve( @@ -389,8 +402,9 @@ export class Runs extends ApiResource{ defaultHeaders: {...this.client.customHeaders, ...this.client.portkeyHeaders}, }); - const result = await OAIclient.beta.threads.runs.retrieve(threadId, runId, opts); - return result; + const result = await OAIclient.beta.threads.runs.retrieve(threadId, runId, opts).withResponse(); + + return finalResponse(result); } async update( @@ -415,8 +429,9 @@ export class Runs extends ApiResource{ defaultHeaders: {...this.client.customHeaders, ...this.client.portkeyHeaders}, }); - const result = await OAIclient.beta.threads.runs.update(threadId, runId, body, opts); - return result; + const result = await OAIclient.beta.threads.runs.update(threadId, runId, body, opts).withResponse(); + + return finalResponse(result); } async submitToolOutputs( @@ -441,8 +456,9 @@ export class Runs extends ApiResource{ defaultHeaders: {...this.client.customHeaders, ...this.client.portkeyHeaders}, }); - const result = await OAIclient.beta.threads.runs.submitToolOutputs(threadId, runId, body, opts); - return result; + const result = await OAIclient.beta.threads.runs.submitToolOutputs(threadId, runId, body, opts).withResponse(); + + return finalResponse(result); } async cancel( @@ -465,8 +481,9 @@ export class Runs extends ApiResource{ defaultHeaders: {...this.client.customHeaders, ...this.client.portkeyHeaders}, }); - const result = await OAIclient.beta.threads.runs.cancel(threadId, runId, opts); - return result; + const result = await OAIclient.beta.threads.runs.cancel(threadId, runId, opts).withResponse(); + + return finalResponse(result); } } @@ -495,8 +512,9 @@ export class Steps extends ApiResource{ defaultHeaders: {...this.client.customHeaders, ...this.client.portkeyHeaders}, }); - const result = await OAIclient.beta.threads.runs.steps.list(threadId, runId, query, opts); - return result; + const result = await OAIclient.beta.threads.runs.steps.list(threadId, runId, query, opts).withResponse(); + + return finalResponse(result); } async retrieve( @@ -520,17 +538,15 @@ export class Steps extends ApiResource{ defaultHeaders: {...this.client.customHeaders, ...this.client.portkeyHeaders}, }); - const result = await OAIclient.beta.threads.runs.steps.retrieve(threadId, runId, stepId, opts); - return result; + const result = await OAIclient.beta.threads.runs.steps.retrieve(threadId, runId, stepId, opts).withResponse(); + + return finalResponse(result); } } - - - export interface ThreadCreateParams { messages?: Array; metadata?: unknown | null; diff --git a/src/utils.ts b/src/utils.ts index 270f77c..640f228 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -1,4 +1,5 @@ import { PORTKEY_HEADER_PREFIX } from "./constants"; +import { createResponseHeaders } from "./streaming"; type PlatformProperties = { "x-portkey-runtime"?: string, @@ -81,4 +82,25 @@ export const parseBody = (data: Record | undefined | null): Rec parsedData[k] = v } return parsedData +} + +export function finalResponse(response:any) { + + const headers = portkeyHeaders(response.response.headers); + const json = { + ...response.data?.body || response.data, + getHeaders: () => headers + } + return json +} + +export function portkeyHeaders(headers:any) { + + const parsedHeaders = createResponseHeaders(headers); + const prefix = PORTKEY_HEADER_PREFIX + const filteredHeaders = Object.entries(parsedHeaders) + .filter(([key, _]) => key.startsWith(prefix)) + .map(([key, value]) => [key.replace(prefix, ''), value]) + + return Object.fromEntries(filteredHeaders) } \ No newline at end of file