Skip to content

Commit

Permalink
feat(api): updates (#501)
Browse files Browse the repository at this point in the history
  • Loading branch information
stainless-bot committed Nov 15, 2023
1 parent 09127e8 commit 944d58e
Show file tree
Hide file tree
Showing 28 changed files with 80 additions and 114 deletions.
21 changes: 0 additions & 21 deletions src/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -564,27 +564,6 @@ export abstract class APIClient {
}
}

export class APIResource {
protected client: APIClient;
constructor(client: APIClient) {
this.client = client;

this.get = client.get.bind(client);
this.post = client.post.bind(client);
this.patch = client.patch.bind(client);
this.put = client.put.bind(client);
this.delete = client.delete.bind(client);
this.getAPIList = client.getAPIList.bind(client);
}

protected get: APIClient['get'];
protected post: APIClient['post'];
protected patch: APIClient['patch'];
protected put: APIClient['put'];
protected delete: APIClient['delete'];
protected getAPIList: APIClient['getAPIList'];
}

export type PageInfo = { url: URL } | { params: Record<string, unknown> | null };

export abstract class AbstractPage<Item> implements AsyncIterable<Item> {
Expand Down
19 changes: 3 additions & 16 deletions src/resource.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,9 @@
import type { OpenAI } from './index';

export class APIResource {
protected client: OpenAI;
constructor(client: OpenAI) {
this.client = client;
protected _client: OpenAI;

this.get = client.get.bind(client);
this.post = client.post.bind(client);
this.patch = client.patch.bind(client);
this.put = client.put.bind(client);
this.delete = client.delete.bind(client);
this.getAPIList = client.getAPIList.bind(client);
constructor(client: OpenAI) {
this._client = client;
}

protected get: OpenAI['get'];
protected post: OpenAI['post'];
protected patch: OpenAI['patch'];
protected put: OpenAI['put'];
protected delete: OpenAI['delete'];
protected getAPIList: OpenAI['getAPIList'];
}
6 changes: 3 additions & 3 deletions src/resources/audio/audio.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ import * as TranscriptionsAPI from 'openai/resources/audio/transcriptions';
import * as TranslationsAPI from 'openai/resources/audio/translations';

export class Audio extends APIResource {
transcriptions: TranscriptionsAPI.Transcriptions = new TranscriptionsAPI.Transcriptions(this.client);
translations: TranslationsAPI.Translations = new TranslationsAPI.Translations(this.client);
speech: SpeechAPI.Speech = new SpeechAPI.Speech(this.client);
transcriptions: TranscriptionsAPI.Transcriptions = new TranscriptionsAPI.Transcriptions(this._client);
translations: TranslationsAPI.Translations = new TranslationsAPI.Translations(this._client);
speech: SpeechAPI.Speech = new SpeechAPI.Speech(this._client);
}

export namespace Audio {
Expand Down
2 changes: 1 addition & 1 deletion src/resources/audio/speech.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export class Speech extends APIResource {
* Generates audio from the input text.
*/
create(body: SpeechCreateParams, options?: Core.RequestOptions): Core.APIPromise<Response> {
return this.post('/audio/speech', { body, ...options, __binaryResponse: true });
return this._client.post('/audio/speech', { body, ...options, __binaryResponse: true });
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/resources/audio/transcriptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export class Transcriptions extends APIResource {
* Transcribes audio into the input language.
*/
create(body: TranscriptionCreateParams, options?: Core.RequestOptions): Core.APIPromise<Transcription> {
return this.post('/audio/transcriptions', multipartFormRequestOptions({ body, ...options }));
return this._client.post('/audio/transcriptions', multipartFormRequestOptions({ body, ...options }));
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/resources/audio/translations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export class Translations extends APIResource {
* Translates audio into English.
*/
create(body: TranslationCreateParams, options?: Core.RequestOptions): Core.APIPromise<Translation> {
return this.post('/audio/translations', multipartFormRequestOptions({ body, ...options }));
return this._client.post('/audio/translations', multipartFormRequestOptions({ body, ...options }));
}
}

Expand Down
12 changes: 6 additions & 6 deletions src/resources/beta/assistants/assistants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@ import * as FilesAPI from 'openai/resources/beta/assistants/files';
import { CursorPage, type CursorPageParams } from 'openai/pagination';

export class Assistants extends APIResource {
files: FilesAPI.Files = new FilesAPI.Files(this.client);
files: FilesAPI.Files = new FilesAPI.Files(this._client);

/**
* Create an assistant with a model and instructions.
*/
create(body: AssistantCreateParams, options?: Core.RequestOptions): Core.APIPromise<Assistant> {
return this.post('/assistants', {
return this._client.post('/assistants', {
body,
...options,
headers: { 'OpenAI-Beta': 'assistants=v1', ...options?.headers },
Expand All @@ -26,7 +26,7 @@ export class Assistants extends APIResource {
* Retrieves an assistant.
*/
retrieve(assistantId: string, options?: Core.RequestOptions): Core.APIPromise<Assistant> {
return this.get(`/assistants/${assistantId}`, {
return this._client.get(`/assistants/${assistantId}`, {
...options,
headers: { 'OpenAI-Beta': 'assistants=v1', ...options?.headers },
});
Expand All @@ -40,7 +40,7 @@ export class Assistants extends APIResource {
body: AssistantUpdateParams,
options?: Core.RequestOptions,
): Core.APIPromise<Assistant> {
return this.post(`/assistants/${assistantId}`, {
return this._client.post(`/assistants/${assistantId}`, {
body,
...options,
headers: { 'OpenAI-Beta': 'assistants=v1', ...options?.headers },
Expand All @@ -62,7 +62,7 @@ export class Assistants extends APIResource {
if (isRequestOptions(query)) {
return this.list({}, query);
}
return this.getAPIList('/assistants', AssistantsPage, {
return this._client.getAPIList('/assistants', AssistantsPage, {
query,
...options,
headers: { 'OpenAI-Beta': 'assistants=v1', ...options?.headers },
Expand All @@ -73,7 +73,7 @@ export class Assistants extends APIResource {
* Delete an assistant.
*/
del(assistantId: string, options?: Core.RequestOptions): Core.APIPromise<AssistantDeleted> {
return this.delete(`/assistants/${assistantId}`, {
return this._client.delete(`/assistants/${assistantId}`, {
...options,
headers: { 'OpenAI-Beta': 'assistants=v1', ...options?.headers },
});
Expand Down
8 changes: 4 additions & 4 deletions src/resources/beta/assistants/files.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export class Files extends APIResource {
body: FileCreateParams,
options?: Core.RequestOptions,
): Core.APIPromise<AssistantFile> {
return this.post(`/assistants/${assistantId}/files`, {
return this._client.post(`/assistants/${assistantId}/files`, {
body,
...options,
headers: { 'OpenAI-Beta': 'assistants=v1', ...options?.headers },
Expand All @@ -32,7 +32,7 @@ export class Files extends APIResource {
fileId: string,
options?: Core.RequestOptions,
): Core.APIPromise<AssistantFile> {
return this.get(`/assistants/${assistantId}/files/${fileId}`, {
return this._client.get(`/assistants/${assistantId}/files/${fileId}`, {
...options,
headers: { 'OpenAI-Beta': 'assistants=v1', ...options?.headers },
});
Expand All @@ -58,7 +58,7 @@ export class Files extends APIResource {
if (isRequestOptions(query)) {
return this.list(assistantId, {}, query);
}
return this.getAPIList(`/assistants/${assistantId}/files`, AssistantFilesPage, {
return this._client.getAPIList(`/assistants/${assistantId}/files`, AssistantFilesPage, {
query,
...options,
headers: { 'OpenAI-Beta': 'assistants=v1', ...options?.headers },
Expand All @@ -73,7 +73,7 @@ export class Files extends APIResource {
fileId: string,
options?: Core.RequestOptions,
): Core.APIPromise<FileDeleteResponse> {
return this.delete(`/assistants/${assistantId}/files/${fileId}`, {
return this._client.delete(`/assistants/${assistantId}/files/${fileId}`, {
...options,
headers: { 'OpenAI-Beta': 'assistants=v1', ...options?.headers },
});
Expand Down
6 changes: 3 additions & 3 deletions src/resources/beta/beta.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ import * as ChatAPI from 'openai/resources/beta/chat/chat';
import * as ThreadsAPI from 'openai/resources/beta/threads/threads';

export class Beta extends APIResource {
chat: ChatAPI.Chat = new ChatAPI.Chat(this.client);
assistants: AssistantsAPI.Assistants = new AssistantsAPI.Assistants(this.client);
threads: ThreadsAPI.Threads = new ThreadsAPI.Threads(this.client);
chat: ChatAPI.Chat = new ChatAPI.Chat(this._client);
assistants: AssistantsAPI.Assistants = new AssistantsAPI.Assistants(this._client);
threads: ThreadsAPI.Threads = new ThreadsAPI.Threads(this._client);
}

export namespace Beta {
Expand Down
2 changes: 1 addition & 1 deletion src/resources/beta/chat/chat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { APIResource } from 'openai/resource';
import * as CompletionsAPI from 'openai/resources/beta/chat/completions';

export class Chat extends APIResource {
completions: CompletionsAPI.Completions = new CompletionsAPI.Completions(this.client);
completions: CompletionsAPI.Completions = new CompletionsAPI.Completions(this._client);
}

export namespace Chat {
Expand Down
10 changes: 5 additions & 5 deletions src/resources/beta/chat/completions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,13 +53,13 @@ export class Completions extends APIResource {
): ChatCompletionRunner | ChatCompletionStreamingRunner {
if (body.stream) {
return ChatCompletionStreamingRunner.runFunctions(
this.client.chat.completions,
this._client.chat.completions,
body as ChatCompletionStreamingFunctionRunnerParams<FunctionsArgs>,
options,
);
}
return ChatCompletionRunner.runFunctions(
this.client.chat.completions,
this._client.chat.completions,
body as ChatCompletionFunctionRunnerParams<FunctionsArgs>,
options,
);
Expand Down Expand Up @@ -90,13 +90,13 @@ export class Completions extends APIResource {
): ChatCompletionRunner | ChatCompletionStreamingRunner {
if (body.stream) {
return ChatCompletionStreamingRunner.runTools(
this.client.chat.completions,
this._client.chat.completions,
body as ChatCompletionStreamingToolRunnerParams<FunctionsArgs>,
options,
);
}
return ChatCompletionRunner.runTools(
this.client.chat.completions,
this._client.chat.completions,
body as ChatCompletionToolRunnerParams<FunctionsArgs>,
options,
);
Expand All @@ -106,6 +106,6 @@ export class Completions extends APIResource {
* Creates a chat completion stream
*/
stream(body: ChatCompletionStreamParams, options?: Core.RequestOptions): ChatCompletionStream {
return ChatCompletionStream.createChatCompletion(this.client.chat.completions, body, options);
return ChatCompletionStream.createChatCompletion(this._client.chat.completions, body, options);
}
}
4 changes: 2 additions & 2 deletions src/resources/beta/threads/messages/files.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export class Files extends APIResource {
fileId: string,
options?: Core.RequestOptions,
): Core.APIPromise<MessageFile> {
return this.get(`/threads/${threadId}/messages/${messageId}/files/${fileId}`, {
return this._client.get(`/threads/${threadId}/messages/${messageId}/files/${fileId}`, {
...options,
headers: { 'OpenAI-Beta': 'assistants=v1', ...options?.headers },
});
Expand Down Expand Up @@ -45,7 +45,7 @@ export class Files extends APIResource {
if (isRequestOptions(query)) {
return this.list(threadId, messageId, {}, query);
}
return this.getAPIList(`/threads/${threadId}/messages/${messageId}/files`, MessageFilesPage, {
return this._client.getAPIList(`/threads/${threadId}/messages/${messageId}/files`, MessageFilesPage, {
query,
...options,
headers: { 'OpenAI-Beta': 'assistants=v1', ...options?.headers },
Expand Down
10 changes: 5 additions & 5 deletions src/resources/beta/threads/messages/messages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import * as FilesAPI from 'openai/resources/beta/threads/messages/files';
import { CursorPage, type CursorPageParams } from 'openai/pagination';

export class Messages extends APIResource {
files: FilesAPI.Files = new FilesAPI.Files(this.client);
files: FilesAPI.Files = new FilesAPI.Files(this._client);

/**
* Create a message.
Expand All @@ -18,7 +18,7 @@ export class Messages extends APIResource {
body: MessageCreateParams,
options?: Core.RequestOptions,
): Core.APIPromise<ThreadMessage> {
return this.post(`/threads/${threadId}/messages`, {
return this._client.post(`/threads/${threadId}/messages`, {
body,
...options,
headers: { 'OpenAI-Beta': 'assistants=v1', ...options?.headers },
Expand All @@ -33,7 +33,7 @@ export class Messages extends APIResource {
messageId: string,
options?: Core.RequestOptions,
): Core.APIPromise<ThreadMessage> {
return this.get(`/threads/${threadId}/messages/${messageId}`, {
return this._client.get(`/threads/${threadId}/messages/${messageId}`, {
...options,
headers: { 'OpenAI-Beta': 'assistants=v1', ...options?.headers },
});
Expand All @@ -48,7 +48,7 @@ export class Messages extends APIResource {
body: MessageUpdateParams,
options?: Core.RequestOptions,
): Core.APIPromise<ThreadMessage> {
return this.post(`/threads/${threadId}/messages/${messageId}`, {
return this._client.post(`/threads/${threadId}/messages/${messageId}`, {
body,
...options,
headers: { 'OpenAI-Beta': 'assistants=v1', ...options?.headers },
Expand All @@ -72,7 +72,7 @@ export class Messages extends APIResource {
if (isRequestOptions(query)) {
return this.list(threadId, {}, query);
}
return this.getAPIList(`/threads/${threadId}/messages`, ThreadMessagesPage, {
return this._client.getAPIList(`/threads/${threadId}/messages`, ThreadMessagesPage, {
query,
...options,
headers: { 'OpenAI-Beta': 'assistants=v1', ...options?.headers },
Expand Down
14 changes: 7 additions & 7 deletions src/resources/beta/threads/runs/runs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@ import * as StepsAPI from 'openai/resources/beta/threads/runs/steps';
import { CursorPage, type CursorPageParams } from 'openai/pagination';

export class Runs extends APIResource {
steps: StepsAPI.Steps = new StepsAPI.Steps(this.client);
steps: StepsAPI.Steps = new StepsAPI.Steps(this._client);

/**
* Create a run.
*/
create(threadId: string, body: RunCreateParams, options?: Core.RequestOptions): Core.APIPromise<Run> {
return this.post(`/threads/${threadId}/runs`, {
return this._client.post(`/threads/${threadId}/runs`, {
body,
...options,
headers: { 'OpenAI-Beta': 'assistants=v1', ...options?.headers },
Expand All @@ -26,7 +26,7 @@ export class Runs extends APIResource {
* Retrieves a run.
*/
retrieve(threadId: string, runId: string, options?: Core.RequestOptions): Core.APIPromise<Run> {
return this.get(`/threads/${threadId}/runs/${runId}`, {
return this._client.get(`/threads/${threadId}/runs/${runId}`, {
...options,
headers: { 'OpenAI-Beta': 'assistants=v1', ...options?.headers },
});
Expand All @@ -41,7 +41,7 @@ export class Runs extends APIResource {
body: RunUpdateParams,
options?: Core.RequestOptions,
): Core.APIPromise<Run> {
return this.post(`/threads/${threadId}/runs/${runId}`, {
return this._client.post(`/threads/${threadId}/runs/${runId}`, {
body,
...options,
headers: { 'OpenAI-Beta': 'assistants=v1', ...options?.headers },
Expand All @@ -65,7 +65,7 @@ export class Runs extends APIResource {
if (isRequestOptions(query)) {
return this.list(threadId, {}, query);
}
return this.getAPIList(`/threads/${threadId}/runs`, RunsPage, {
return this._client.getAPIList(`/threads/${threadId}/runs`, RunsPage, {
query,
...options,
headers: { 'OpenAI-Beta': 'assistants=v1', ...options?.headers },
Expand All @@ -76,7 +76,7 @@ export class Runs extends APIResource {
* Cancels a run that is `in_progress`.
*/
cancel(threadId: string, runId: string, options?: Core.RequestOptions): Core.APIPromise<Run> {
return this.post(`/threads/${threadId}/runs/${runId}/cancel`, {
return this._client.post(`/threads/${threadId}/runs/${runId}/cancel`, {
...options,
headers: { 'OpenAI-Beta': 'assistants=v1', ...options?.headers },
});
Expand All @@ -94,7 +94,7 @@ export class Runs extends APIResource {
body: RunSubmitToolOutputsParams,
options?: Core.RequestOptions,
): Core.APIPromise<Run> {
return this.post(`/threads/${threadId}/runs/${runId}/submit_tool_outputs`, {
return this._client.post(`/threads/${threadId}/runs/${runId}/submit_tool_outputs`, {
body,
...options,
headers: { 'OpenAI-Beta': 'assistants=v1', ...options?.headers },
Expand Down
4 changes: 2 additions & 2 deletions src/resources/beta/threads/runs/steps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export class Steps extends APIResource {
stepId: string,
options?: Core.RequestOptions,
): Core.APIPromise<RunStep> {
return this.get(`/threads/${threadId}/runs/${runId}/steps/${stepId}`, {
return this._client.get(`/threads/${threadId}/runs/${runId}/steps/${stepId}`, {
...options,
headers: { 'OpenAI-Beta': 'assistants=v1', ...options?.headers },
});
Expand Down Expand Up @@ -45,7 +45,7 @@ export class Steps extends APIResource {
if (isRequestOptions(query)) {
return this.list(threadId, runId, {}, query);
}
return this.getAPIList(`/threads/${threadId}/runs/${runId}/steps`, RunStepsPage, {
return this._client.getAPIList(`/threads/${threadId}/runs/${runId}/steps`, RunStepsPage, {
query,
...options,
headers: { 'OpenAI-Beta': 'assistants=v1', ...options?.headers },
Expand Down

0 comments on commit 944d58e

Please sign in to comment.