Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions Sources/OpenAI/OpenAI.swift
Original file line number Diff line number Diff line change
Expand Up @@ -429,6 +429,10 @@ extension APIPath {
.init(stringValue: "responses/\(responseId)/input_items")
}

static func cancelModelResponse(responseId: String) -> Responses {
.init(stringValue: "responses/\(responseId)/cancel")
}

let stringValue: String
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
//
// CancelModelResponseQuery.swift
// OpenAI
//
// Created by Tqtifnypmb on 2026/8/8.
//

import Foundation

/// Cancels a model response with the given ID. Only responses created with the background parameter set to true can be cancelled.
public struct CancelModelResponseQuery: Codable, Equatable, Sendable {
/// The ID of the response to cancel.
public let responseId: String

public init(responseId: String) {
self.responseId = responseId
}

private enum CodingKeys: String, CodingKey {
case responseId = "response_id"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@ public struct CreateModelResponseQuery: Codable, Equatable, Sendable {
/// Whether to run the model response in the background. [Learn more](https://platform.openai.com/docs/guides/background).
public let background: Bool?

/// Context management configuration for this request.
public let contextManagement: [Schemas.ContextManagementParam]?

/// Inserts a system (or developer) message as the first item in the model's context.
///
/// When using along with `previousResponseId`, the instructions from a previous response will not be carried over to the next response.
Expand Down Expand Up @@ -137,6 +140,7 @@ public struct CreateModelResponseQuery: Codable, Equatable, Sendable {
model: String? = nil,
include: [Schemas.IncludeEnum]? = nil,
background: Bool? = nil,
contextManagement: [Schemas.ContextManagementParam]? = nil,
instructions: String? = nil,
maxOutputTokens: Int? = nil,
metadata: Schemas.Metadata? = nil,
Expand All @@ -160,6 +164,7 @@ public struct CreateModelResponseQuery: Codable, Equatable, Sendable {
self.model = model
self.include = include
self.background = background
self.contextManagement = contextManagement
self.instructions = instructions
self.maxOutputTokens = maxOutputTokens
self.metadata = metadata
Expand All @@ -185,6 +190,7 @@ public struct CreateModelResponseQuery: Codable, Equatable, Sendable {
case model
case include
case background
case contextManagement = "context_management"
case instructions
case maxOutputTokens = "max_output_tokens"
case metadata
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@ public struct GetModelResponseQuery: Codable, Equatable, Sendable {
/// * `computer_call_output.output.image_url`: Include image urls from the computer call output.
public let include: [Components.Schemas.IncludeEnum]?

public init(responseId: String, include: [Components.Schemas.IncludeEnum]?) {
self.responseId = responseId
self.include = include
}

private enum CodingKeys: String, CodingKey {
case responseId = "response_id"
case include
Expand Down
2 changes: 2 additions & 0 deletions Sources/OpenAI/Public/Protocols/ResponsesEndpointAsync.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,6 @@ import Foundation
public protocol ResponsesEndpointAsync: Sendable {
func createResponse(query: CreateModelResponseQuery) async throws -> ResponseObject
func createResponseStreaming(query: CreateModelResponseQuery) -> AsyncThrowingStream<ResponseStreamEvent, Error>
func retrieveResponse(query: GetModelResponseQuery) async throws -> ResponseObject
func cancelResponse(query: CancelModelResponseQuery) async throws -> ResponseObject
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,7 @@ import Combine
public protocol ResponsesEndpointCombine: Sendable {
func createResponse(query: CreateModelResponseQuery) -> AnyPublisher<ResponseObject, Error>
func createResponseStreaming(query: CreateModelResponseQuery) -> AnyPublisher<Result<ResponseStreamEvent, Error>, Error>
func retrieveResponse(query: GetModelResponseQuery) -> AnyPublisher<ResponseObject, Error>
func cancelResponse(query: CancelModelResponseQuery) -> AnyPublisher<ResponseObject, Error>
}
#endif
11 changes: 11 additions & 0 deletions Sources/OpenAI/Public/Protocols/ResponsesEndpointProtocol.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,15 @@ public protocol ResponsesEndpointProtocol: ResponsesEndpointModern {
onResult: @escaping @Sendable (Result<ResponseStreamEvent, Error>) -> Void,
completion: (@Sendable (Error?) -> Void)?
) -> CancellableRequest

func retrieveResponse(
query: GetModelResponseQuery,
completion: @escaping @Sendable (Result<ResponseObject, Error>) -> Void
) -> CancellableRequest

func cancelResponse(
query: CancelModelResponseQuery,
completion: @escaping @Sendable (Result<ResponseObject, Error>) -> Void
) -> CancellableRequest

}
8 changes: 8 additions & 0 deletions Sources/OpenAI/ResponsesEndpoint+Async.swift
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,12 @@ extension ResponsesEndpoint: ResponsesEndpointAsync {
}
}
}

public func retrieveResponse(query: GetModelResponseQuery) async throws -> ResponseObject {
try await asyncClient.performRequest(request: makeRetrieveResponseRequest(query: query))
}

public func cancelResponse(query: CancelModelResponseQuery) async throws -> ResponseObject {
try await asyncClient.performRequest(request: makeCancelResponseRequest(query: query))
}
}
8 changes: 8 additions & 0 deletions Sources/OpenAI/ResponsesEndpoint+Combine.swift
Original file line number Diff line number Diff line change
Expand Up @@ -37,5 +37,13 @@ extension ResponsesEndpoint: ResponsesEndpointCombine {
})
.eraseToAnyPublisher()
}

public func retrieveResponse(query: GetModelResponseQuery) -> AnyPublisher<ResponseObject, any Error> {
combineClient.performRequest(request: makeRetrieveResponseRequest(query: query))
}

public func cancelResponse(query: CancelModelResponseQuery) -> AnyPublisher<ResponseObject, any Error> {
combineClient.performRequest(request: makeCancelResponseRequest(query: query))
}
}
#endif
30 changes: 30 additions & 0 deletions Sources/OpenAI/ResponsesEndpoint.swift
Original file line number Diff line number Diff line change
Expand Up @@ -69,3 +69,33 @@
.buildURL()
}
}

// MARK: - Retrieve Response

extension ResponsesEndpoint {

public func retrieveResponse(query: GetModelResponseQuery, completion: @escaping @Sendable (Result<ResponseObject, any Error>) -> Void) -> any CancellableRequest {
client.performRequest(request: makeRetrieveResponseRequest(query: query),
completion: completion)
}

func makeRetrieveResponseRequest(query: GetModelResponseQuery) -> JSONRequest<ResponseObject> {
let path = String.Responses.getModelResponse(responseId: query.responseId).stringValue
return .init(url: buildURL(path: path), method: "GET")
}
}

// MARK: - Cancel Response

extension ResponsesEndpoint {

public func cancelResponse(query: CancelModelResponseQuery, completion: @escaping (Result<ResponseObject, any Error>) -> Void) -> any CancellableRequest {
client.performRequest(request: makeCancelResponseRequest(query: query),
completion: completion)

Check warning on line 94 in Sources/OpenAI/ResponsesEndpoint.swift

View workflow job for this annotation

GitHub Actions / macOS · test

passing non-Sendable parameter 'completion' to function expecting a '@sendable' closure

Check warning on line 94 in Sources/OpenAI/ResponsesEndpoint.swift

View workflow job for this annotation

GitHub Actions / watchOS · build

passing non-Sendable parameter 'completion' to function expecting a '@sendable' closure

Check warning on line 94 in Sources/OpenAI/ResponsesEndpoint.swift

View workflow job for this annotation

GitHub Actions / watchOS · build

passing non-Sendable parameter 'completion' to function expecting a '@sendable' closure

Check warning on line 94 in Sources/OpenAI/ResponsesEndpoint.swift

View workflow job for this annotation

GitHub Actions / watchOS · build

passing non-Sendable parameter 'completion' to function expecting a '@sendable' closure

Check warning on line 94 in Sources/OpenAI/ResponsesEndpoint.swift

View workflow job for this annotation

GitHub Actions / tvOS · build

passing non-Sendable parameter 'completion' to function expecting a '@sendable' closure

Check warning on line 94 in Sources/OpenAI/ResponsesEndpoint.swift

View workflow job for this annotation

GitHub Actions / visionOS · build

passing non-Sendable parameter 'completion' to function expecting a '@sendable' closure

Check warning on line 94 in Sources/OpenAI/ResponsesEndpoint.swift

View workflow job for this annotation

GitHub Actions / iOS Simulator · test

passing non-Sendable parameter 'completion' to function expecting a '@sendable' closure
}

func makeCancelResponseRequest(query: CancelModelResponseQuery) -> JSONRequest<ResponseObject> {
let path = String.Responses.cancelModelResponse(responseId: query.responseId).stringValue
return .init(body: query, url: buildURL(path: path))
}
}
Loading