|
| 1 | +// |
| 2 | +// OpenAI+OpenAIAsync.swift |
| 3 | +// OpenAI |
| 4 | +// |
| 5 | +// Created by Oleksii Nezhyborets on 31.01.2025. |
| 6 | +// |
| 7 | + |
| 8 | +import Foundation |
| 9 | + |
| 10 | +@available(iOS 13.0, macOS 10.15, tvOS 13.0, watchOS 6.0, *) |
| 11 | +extension OpenAI: OpenAIAsync { |
| 12 | + public func images(query: ImagesQuery) async throws -> ImagesResult { |
| 13 | + try await performRequestAsync( |
| 14 | + request: makeImagesRequest(query: query) |
| 15 | + ) |
| 16 | + } |
| 17 | + |
| 18 | + public func imageEdits(query: ImageEditsQuery) async throws -> ImagesResult { |
| 19 | + try await performRequestAsync( |
| 20 | + request: makeImageEditsRequest(query: query) |
| 21 | + ) |
| 22 | + } |
| 23 | + |
| 24 | + public func imageVariations(query: ImageVariationsQuery) async throws -> ImagesResult { |
| 25 | + try await performRequestAsync( |
| 26 | + request: makeImageVariationsRequest(query: query) |
| 27 | + ) |
| 28 | + } |
| 29 | + |
| 30 | + public func embeddings(query: EmbeddingsQuery) async throws -> EmbeddingsResult { |
| 31 | + try await performRequestAsync( |
| 32 | + request: makeEmbeddingsRequest(query: query) |
| 33 | + ) |
| 34 | + } |
| 35 | + |
| 36 | + public func chats(query: ChatQuery) async throws -> ChatResult { |
| 37 | + try await performRequestAsync( |
| 38 | + request: makeChatsRequest(query: query) |
| 39 | + ) |
| 40 | + } |
| 41 | + |
| 42 | + public func chatsStream(query: ChatQuery) -> AsyncThrowingStream<ChatStreamResult, Error> { |
| 43 | + return AsyncThrowingStream { continuation in |
| 44 | + let cancellableRequest = chatsStream(query: query) { result in |
| 45 | + continuation.yield(with: result) |
| 46 | + } completion: { error in |
| 47 | + continuation.finish(throwing: error) |
| 48 | + } |
| 49 | + |
| 50 | + continuation.onTermination = { termination in |
| 51 | + switch termination { |
| 52 | + case .cancelled: |
| 53 | + cancellableRequest.cancelRequest() |
| 54 | + case .finished: |
| 55 | + break |
| 56 | + @unknown default: |
| 57 | + break |
| 58 | + } |
| 59 | + } |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + public func model(query: ModelQuery) async throws -> ModelResult { |
| 64 | + try await performRequestAsync( |
| 65 | + request: makeModelRequest(query: query) |
| 66 | + ) |
| 67 | + } |
| 68 | + |
| 69 | + public func models() async throws -> ModelsResult { |
| 70 | + try await performRequestAsync( |
| 71 | + request: makeModelsRequest() |
| 72 | + ) |
| 73 | + } |
| 74 | + |
| 75 | + public func moderations(query: ModerationsQuery) async throws -> ModerationsResult { |
| 76 | + try await performRequestAsync( |
| 77 | + request: makeModerationsRequest(query: query) |
| 78 | + ) |
| 79 | + } |
| 80 | + |
| 81 | + public func audioCreateSpeech(query: AudioSpeechQuery) async throws -> AudioSpeechResult { |
| 82 | + try await performRequestAsync( |
| 83 | + request: makeAudioCreateSpeechRequest(query: query) |
| 84 | + ) |
| 85 | + } |
| 86 | + |
| 87 | + public func audioTranscriptions(query: AudioTranscriptionQuery) async throws -> AudioTranscriptionResult { |
| 88 | + try await performRequestAsync( |
| 89 | + request: makeAudioTranscriptionsRequest(query: query) |
| 90 | + ) |
| 91 | + } |
| 92 | + |
| 93 | + public func audioTranslations(query: AudioTranslationQuery) async throws -> AudioTranslationResult { |
| 94 | + try await performRequestAsync( |
| 95 | + request: makeAudioTranslationsRequest(query: query) |
| 96 | + ) |
| 97 | + } |
| 98 | + |
| 99 | + public func assistants() async throws -> AssistantsResult { |
| 100 | + try await assistants(after: nil) |
| 101 | + } |
| 102 | + |
| 103 | + public func assistants(after: String?) async throws -> AssistantsResult { |
| 104 | + try await performRequestAsync( |
| 105 | + request: makeAssistantsRequest(after) |
| 106 | + ) |
| 107 | + } |
| 108 | + |
| 109 | + public func assistantCreate(query: AssistantsQuery) async throws -> AssistantResult { |
| 110 | + try await performRequestAsync( |
| 111 | + request: makeAssistantCreateRequest(query) |
| 112 | + ) |
| 113 | + } |
| 114 | + |
| 115 | + public func assistantModify(query: AssistantsQuery, assistantId: String) async throws -> AssistantResult { |
| 116 | + try await performRequestAsync( |
| 117 | + request: makeAssistantModifyRequest(assistantId, query) |
| 118 | + ) |
| 119 | + } |
| 120 | + |
| 121 | + public func threads(query: ThreadsQuery) async throws -> ThreadsResult { |
| 122 | + try await performRequestAsync( |
| 123 | + request: makeThreadsRequest(query) |
| 124 | + ) |
| 125 | + } |
| 126 | + |
| 127 | + public func threadRun(query: ThreadRunQuery) async throws -> RunResult { |
| 128 | + try await performRequestAsync( |
| 129 | + request: makeThreadRunRequest(query) |
| 130 | + ) |
| 131 | + } |
| 132 | + |
| 133 | + public func runs(threadId: String,query: RunsQuery) async throws -> RunResult { |
| 134 | + try await performRequestAsync( |
| 135 | + request: makeRunsRequest(threadId, query) |
| 136 | + ) |
| 137 | + } |
| 138 | + |
| 139 | + public func runRetrieve(threadId: String, runId: String) async throws -> RunResult { |
| 140 | + try await performRequestAsync( |
| 141 | + request: makeRunRetrieveRequest(threadId, runId) |
| 142 | + ) |
| 143 | + } |
| 144 | + |
| 145 | + public func runRetrieveSteps(threadId: String, runId: String) async throws -> RunRetrieveStepsResult { |
| 146 | + try await runRetrieveSteps(threadId: threadId, runId: runId, before: nil) |
| 147 | + } |
| 148 | + |
| 149 | + public func runRetrieveSteps(threadId: String, runId: String, before: String?) async throws -> RunRetrieveStepsResult { |
| 150 | + try await performRequestAsync( |
| 151 | + request: makeRunRetrieveStepsRequest(threadId, runId, before) |
| 152 | + ) |
| 153 | + } |
| 154 | + |
| 155 | + public func runSubmitToolOutputs(threadId: String, runId: String, query: RunToolOutputsQuery) async throws -> RunResult { |
| 156 | + try await performRequestAsync( |
| 157 | + request: makeRunSubmitToolOutputsRequest(threadId, runId, query) |
| 158 | + ) |
| 159 | + } |
| 160 | + |
| 161 | + public func threadsMessages(threadId: String) async throws -> ThreadsMessagesResult { |
| 162 | + try await performRequestAsync( |
| 163 | + request: makeThreadsMessagesRequest(threadId, before: nil) |
| 164 | + ) |
| 165 | + } |
| 166 | + |
| 167 | + public func threadsMessages(threadId: String, before: String?) async throws -> ThreadsMessagesResult { |
| 168 | + try await performRequestAsync( |
| 169 | + request: makeThreadsMessagesRequest(threadId, before: before) |
| 170 | + ) |
| 171 | + } |
| 172 | + |
| 173 | + public func threadsAddMessage(threadId: String, query: MessageQuery) async throws -> ThreadAddMessageResult { |
| 174 | + try await performRequestAsync( |
| 175 | + request: makeThreadsAddMessageRequest(threadId, query) |
| 176 | + ) |
| 177 | + } |
| 178 | + |
| 179 | + public func files(query: FilesQuery) async throws -> FilesResult { |
| 180 | + try await performRequestAsync( |
| 181 | + request: makeFilesRequest(query: query) |
| 182 | + ) |
| 183 | + } |
| 184 | + |
| 185 | + func performRequestAsync<ResultType: Codable>(request: any URLRequestBuildable) async throws -> ResultType { |
| 186 | + let urlRequest = try request.build(token: configuration.token, |
| 187 | + organizationIdentifier: configuration.organizationIdentifier, |
| 188 | + timeoutInterval: configuration.timeoutInterval) |
| 189 | + if #available(iOS 15.0, macOS 12.0, tvOS 15.0, watchOS 8.0, *) { |
| 190 | + let (data, _) = try await session.data(for: urlRequest, delegate: nil) |
| 191 | + let decoder = JSONDecoder() |
| 192 | + do { |
| 193 | + return try decoder.decode(ResultType.self, from: data) |
| 194 | + } catch { |
| 195 | + throw (try? decoder.decode(APIErrorResponse.self, from: data)) ?? error |
| 196 | + } |
| 197 | + } else { |
| 198 | + let dataTaskStore = URLSessionDataTaskStore() |
| 199 | + return try await withTaskCancellationHandler { |
| 200 | + return try await withCheckedThrowingContinuation { continuation in |
| 201 | + let dataTask = self.makeDataTask(forRequest: urlRequest) { (result: Result<ResultType, Error>) in |
| 202 | + continuation.resume(with: result) |
| 203 | + } |
| 204 | + |
| 205 | + dataTask.resume() |
| 206 | + |
| 207 | + Task { |
| 208 | + await dataTaskStore.setDataTask(dataTask) |
| 209 | + } |
| 210 | + } |
| 211 | + } onCancel: { |
| 212 | + Task { |
| 213 | + await dataTaskStore.getDataTask()?.cancel() |
| 214 | + } |
| 215 | + } |
| 216 | + } |
| 217 | + } |
| 218 | +} |
0 commit comments