Skip to content

Commit 6c33844

Browse files
committed
Accept only sendable model type in request handler
1 parent 040e485 commit 6c33844

File tree

3 files changed

+13
-13
lines changed

3 files changed

+13
-13
lines changed

Sources/OpenAI/Models/ModelsResponse.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55
// Created by Firdavs Khaydarov on 15/04/2023.
66
//
77

8-
public struct ModelsResponse: Decodable {
9-
public struct Model: Decodable, Identifiable {
8+
public struct ModelsResponse: Decodable, Sendable {
9+
public struct Model: Decodable, Identifiable, Sendable {
1010
public let id: String
1111
public let object: String
1212
public let created: Int

Sources/OpenAI/Request/Handler/BaseRequestHandler.swift

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import FoundationNetworking
1414
public struct BaseRequestHandler: RequestHandler, Sendable {
1515
let urlSession: URLSession
1616
let eventSource: EventSource
17-
17+
1818
private let decoder: JSONDecoder = .openAIDecoder
1919

2020
private let logger = PlatformLogger(subsystem: "me.recouse.OpenAI", category: "BaseRequestHandler")
@@ -23,8 +23,8 @@ public struct BaseRequestHandler: RequestHandler, Sendable {
2323
self.urlSession = urlSession
2424
self.eventSource = eventSource
2525
}
26-
27-
public func perform<T>(for model: T.Type, with request: Request) async throws -> T where T: Decodable {
26+
27+
public func perform<T>(for model: T.Type, with request: Request) async throws -> T where T: Decodable, T: Sendable {
2828
let urlRequest = urlRequest(from: request)
2929
let (data, response) = try await urlSession.data(for: urlRequest)
3030

@@ -40,13 +40,13 @@ public struct BaseRequestHandler: RequestHandler, Sendable {
4040
let parsed = try decoder.decode(T.self, from: data)
4141
return parsed
4242
}
43-
43+
4444
public func stream<T>(
4545
for model: T.Type,
4646
with request: Request
47-
) -> AsyncThrowingStream<T, Error> where T: Decodable {
47+
) -> AsyncThrowingStream<T, Error> where T: Decodable, T: Sendable {
4848
let urlRequest = urlRequest(from: request)
49-
49+
5050
return AsyncThrowingStream { continuation in
5151
Task { @Sendable in
5252
let dataTask = eventSource.dataTask(for: urlRequest)
@@ -91,7 +91,7 @@ public struct BaseRequestHandler: RequestHandler, Sendable {
9191
}
9292
}
9393
}
94-
94+
9595
private func urlRequest(from request: Request) -> URLRequest {
9696
var url = URL(string: "https://\(request.host)")!
9797
if #available(macOS 13.0, iOS 16.0, tvOS 16.0, watchOS 9.0, *) {
@@ -109,7 +109,7 @@ public struct BaseRequestHandler: RequestHandler, Sendable {
109109

110110
return urlRequest
111111
}
112-
112+
113113
private func parseError(from data: Data, with httpStatusCode: Int) -> OpenAIError {
114114
do {
115115
let error = try decoder.decode(APIError.self, from: data)

Sources/OpenAI/Request/Handler/RequestHandler.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
//
77

88
public protocol RequestHandler {
9-
func perform<T>(for model: T.Type, with request: Request) async throws -> T where T: Decodable
10-
11-
func stream<T>(for model: T.Type, with request: Request) -> AsyncThrowingStream<T, Error> where T: Decodable
9+
func perform<T>(for model: T.Type, with request: Request) async throws -> T where T: Decodable, T: Sendable
10+
11+
func stream<T>(for model: T.Type, with request: Request) -> AsyncThrowingStream<T, Error> where T: Decodable, T: Sendable
1212
}

0 commit comments

Comments
 (0)