Skip to content

Commit

Permalink
add concurrency
Browse files Browse the repository at this point in the history
  • Loading branch information
Shonchik committed Aug 8, 2024
1 parent e4b07b3 commit 02a6d77
Show file tree
Hide file tree
Showing 8 changed files with 237 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
//
// AnyPublisher+Concurrency.swift
//
// Copyright (c) 2024 Exyte
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
//

import Combine
import Foundation

extension AnyPublisher {
func async() async throws -> Output {
try await withCheckedThrowingContinuation { continuation in
var cancellable: AnyCancellable?
var finishedWithoutValue = true
cancellable = first()
.sink(receiveCompletion: { result in
switch result {
case .finished:
if finishedWithoutValue {
continuation.resume(throwing: OpenAIError.emptyResponse)
}
case .failure(let error):
continuation.resume(throwing: error)
}
cancellable?.cancel()

},
receiveValue: { value in
finishedWithoutValue = false
continuation.resume(with: .success(value))

})
}
}
}
28 changes: 28 additions & 0 deletions Sources/ExyteOpenAI/OpenAI+Assistants.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
import Foundation
import Combine

// MARK: - Combine

public extension OpenAI {

func createAssistant(from payload: CreateAssistantPayload) -> AnyPublisher<Assistant, OpenAIError> {
Expand Down Expand Up @@ -68,3 +70,29 @@ public extension OpenAI {
}

}

// MARK: - Concurrency

public extension OpenAI {

func createAssistant(from payload: CreateAssistantPayload) async throws -> Assistant {
try await createAssistant(from: payload).async()
}

func modifyAssistant(id: String, payload: CreateAssistantPayload) async throws -> Assistant {
try await modifyAssistant(id: id, payload: payload).async()
}

func deleteAssistant(id: String) async throws -> DeletionStatus {
try await deleteAssistant(id: id).async()
}

func listAssistants(payload: ListPayload) async throws -> ObjectList<Assistant> {
try await listAssistants(payload: payload).async()
}

func retrieveAssistant(id: String) async throws -> Assistant {
try await retrieveAssistant(id: id).async()
}

}
12 changes: 12 additions & 0 deletions Sources/ExyteOpenAI/OpenAI+Chats.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
import Foundation
import Combine

// MARK: - Combine

public extension OpenAI {

func createChatCompletion(from payload: CreateChatCompletionPayload) -> AnyPublisher<ChatCompletion, OpenAIError> {
Expand All @@ -35,3 +37,13 @@ public extension OpenAI {
}

}

// MARK: - Concurrency

public extension OpenAI {

func createChatCompletion(from payload: CreateChatCompletionPayload) async throws -> ChatCompletion {
try await createChatCompletion(from: payload).async()
}

}
28 changes: 28 additions & 0 deletions Sources/ExyteOpenAI/OpenAI+Files.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
import Foundation
import Combine

// MARK: - Combine

public extension OpenAI {

func uploadFile(payload: FilePayload) -> AnyPublisher<File, OpenAIError> {
Expand Down Expand Up @@ -64,3 +66,29 @@ public extension OpenAI {
}

}

// MARK: - Concurrency

public extension OpenAI {

func uploadFile(payload: FilePayload) async throws -> File {
try await uploadFile(payload: payload).async()
}

func listFiles() async throws -> ObjectList<File> {
try await listFiles().async()
}

func retrieveFile(id: String) async throws -> File {
try await retrieveFile(id: id).async()
}

func deleteFile(id: String) async throws -> DeletionStatus {
try await deleteFile(id: id).async()
}

func retrieveFileContent(id: String, destinationURL: URL) async throws -> URL {
try await retrieveFileContent(id: id, destinationURL: destinationURL).async()
}

}
24 changes: 24 additions & 0 deletions Sources/ExyteOpenAI/OpenAI+Messages.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
import Foundation
import Combine

// MARK: - Combine

public extension OpenAI {

func createMessage(in threadId: String, payload: CreateMessagePayload) -> AnyPublisher<Message, OpenAIError> {
Expand Down Expand Up @@ -72,3 +74,25 @@ public extension OpenAI {
}

}

// MARK: - Concurrency

public extension OpenAI {

func createMessage(in threadId: String, payload: CreateMessagePayload) async throws -> Message {
try await createMessage(in: threadId, payload: payload).async()
}

func listMessages(from threadId: String, payload: ListPayload) async throws -> ObjectList<Message> {
try await listMessages(from: threadId, payload: payload).async()
}

func retrieveMessage(id: String, from threadId: String) async throws -> Message {
try await retrieveMessage(id: id, from: threadId).async()
}

func modifyMessage(id: String, from threadId: String, payload: ModifyPayload) async throws -> Message {
try await modifyMessage(id: id, from: threadId, payload: payload).async()
}

}
20 changes: 20 additions & 0 deletions Sources/ExyteOpenAI/OpenAI+Models.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
import Foundation
import Combine

// MARK: - Combine

public extension OpenAI {

func listModels() -> AnyPublisher<ObjectList<Model>, OpenAIError> {
Expand All @@ -49,3 +51,21 @@ public extension OpenAI {
}

}

// MARK: - Concurrency

public extension OpenAI {

func listModels() async throws -> ObjectList<Model> {
try await listModels().async()
}

func retrieveModel(with id: String) async throws -> Model {
try await retrieveModel(with: id).async()
}

func deleteModel(with id: String) async throws -> DeletionStatus {
try await deleteModel(with: id).async()
}

}
48 changes: 48 additions & 0 deletions Sources/ExyteOpenAI/OpenAI+Runs.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ import Foundation
import Combine
import EventSourceHttpBody

// MARK: - Combine

public extension OpenAI {

func createRun(in threadId: String, payload: CreateRunPayload) -> AnyPublisher<Run, OpenAIError> {
Expand Down Expand Up @@ -174,3 +176,49 @@ public extension OpenAI {
}

}

// MARK: - Concurrency

public extension OpenAI {

func createRun(in threadId: String, payload: CreateRunPayload) async throws -> Run {
try await createRun(in: threadId, payload: payload).async()
}

func createStreamRun(in threadId: String, payload: CreateStreamRunPayload) async throws -> StreamEvent {
try await createStreamRun(in: threadId, payload: payload).async()
}

func createThreadAndRun(from payload: CreateThreadAndRunPayload) async throws -> Run {
try await createThreadAndRun(from: payload).async()
}

func listRuns(from threadId: String, payload: ListPayload) async throws -> ObjectList<Run> {
try await listRuns(from: threadId, payload: payload).async()
}

func retrieveRun(id: String, from threadId: String) async throws -> Run {
try await retrieveRun(id: id, from: threadId).async()
}

func modifyRun(id: String, from threadId: String, payload: ModifyPayload) async throws -> Run {
try await modifyRun(id: id, from: threadId, payload: payload).async()
}

func cancelRun(id: String, from threadId: String) async throws -> Run {
try await cancelRun(id: id, from: threadId).async()
}

func submitToolOutputs(to runId: String, from threadId: String, payload: SubmitToolOutputsPayload) async throws -> Run {
try await submitToolOutputs(to: runId, from: threadId, payload: payload).async()
}

func listRunSteps(from runId: String, in threadId: String, payload: ListPayload) async throws -> ObjectList<RunStep> {
try await listRunSteps(from: runId, in: threadId, payload: payload).async()
}

func retrieveRunStep(id: String, from runId: String, in threadId: String) async throws -> RunStep {
try await retrieveRunStep(id: id, from: runId, in: threadId).async()
}

}
24 changes: 24 additions & 0 deletions Sources/ExyteOpenAI/OpenAI+Threads.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
import Foundation
import Combine

// MARK: - Combine

public extension OpenAI {

func createThread(from payload: CreateThreadPayload) -> AnyPublisher<Thread, OpenAIError> {
Expand Down Expand Up @@ -61,3 +63,25 @@ public extension OpenAI {
}

}

// MARK: - Concurrency

public extension OpenAI {

func createThread(from payload: CreateThreadPayload) async throws -> Thread {
try await createThread(from: payload).async()
}

func retrieveThread(id: String) async throws -> Thread {
try await retrieveThread(id: id).async()
}

func modifyThread(id: String, payload: ModifyPayload) async throws -> Thread {
try await modifyThread(id: id, payload: payload).async()
}

func deleteThread(id: String) async throws -> DeletionStatus {
try await deleteThread(id: id).async()
}

}

0 comments on commit 02a6d77

Please sign in to comment.