Skip to content

Commit

Permalink
add new runs methods
Browse files Browse the repository at this point in the history
add runs tests
  • Loading branch information
DeveloperZelentsov committed Nov 20, 2023
1 parent f3f6ef8 commit 13e4d1b
Show file tree
Hide file tree
Showing 15 changed files with 419 additions and 28 deletions.
6 changes: 3 additions & 3 deletions Sources/AISwiftAssist/APIs/AssistantsAPI.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public protocol IAssistantsAPI: AnyObject {
/// Returns a list of assistants.
/// - Parameter parameters: Parameters for the list of assistants.
/// - Returns: A list of assistant objects.
func get(with parameters: ASAListAssistantsParameters?) async throws -> ASAListAssistantsResponse
func get(with parameters: ASAListAssistantsParameters?) async throws -> ASAAssistantsListResponse

/// Create an assistant with a model and instructions.
/// - Parameter createAssistant: The create assistant model.
Expand Down Expand Up @@ -58,9 +58,9 @@ public final class AssistantsAPI: HTTPClient, IAssistantsAPI {
self.urlSession = urlSession
}

public func get(with parameters: ASAListAssistantsParameters? = nil) async throws -> ASAListAssistantsResponse {
public func get(with parameters: ASAListAssistantsParameters? = nil) async throws -> ASAAssistantsListResponse {
let endpoint = AssistantEndpoint.getAssistants(parameters)
return try await sendRequest(session: urlSession, endpoint: endpoint, responseModel: ASAListAssistantsResponse.self)
return try await sendRequest(session: urlSession, endpoint: endpoint, responseModel: ASAAssistantsListResponse.self)
}

public func create(by createAssistant: ASACreateAssistantRequest) async throws -> ASAAssistant {
Expand Down
6 changes: 3 additions & 3 deletions Sources/AISwiftAssist/APIs/ModelsAPI.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public protocol IModelsAPI: AnyObject {

/// Lists the currently available models, and provides basic information about each one such as the owner and availability.
/// - Returns: A list of model objects.
func get() async throws -> ASAListModelsResponse
func get() async throws -> ASAModelsListResponse

/// Retrieves a model instance, providing basic information about the model such as the owner and permissioning.
/// - Parameter modelId: The ID of the model to use for this request
Expand Down Expand Up @@ -45,9 +45,9 @@ public final class ModelsAPI: HTTPClient, IModelsAPI {
self.urlSession = urlSession
}

public func get() async throws -> ASAListModelsResponse {
public func get() async throws -> ASAModelsListResponse {
let endpoint = ModelsEndpoint.getModels
return try await sendRequest(session: urlSession, endpoint: endpoint, responseModel: ASAListModelsResponse.self)
return try await sendRequest(session: urlSession, endpoint: endpoint, responseModel: ASAModelsListResponse.self)
}

public func retrieve(by modelId: String) async throws -> ASAModel {
Expand Down
37 changes: 37 additions & 0 deletions Sources/AISwiftAssist/APIs/RunsAPI.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,28 @@ public protocol IRunsAPI: AnyObject {
/// - createRun: Object with parameters for creating a run.
/// - Returns: A run object.
func create(by threadId: String, createRun: ASACreateRunRequest) async throws -> ASARun

/// Returns a list of runs belonging to a thread.
/// - Parameters:
/// - threadId: The ID of the thread the run belongs to.
/// - parameters: Parameters for the list of runs.
/// - Returns: A list of run objects.
func listRuns(by threadId: String, parameters: ASAListRunsParameters?) async throws -> ASARunsListResponse

/// Modifies a run.
/// - Parameters:
/// - threadId: The ID of the thread that was run.
/// - runId: The ID of the run to modify.
/// - modifyRun: A request structure for modifying a run.
/// - Returns: The modified run object matching the specified ID.
func modify(by threadId: String, runId: String, modifyRun: ASAModifyRunRequest) async throws -> ASARun

/// Retrieves a run.
/// - Parameters:
/// - threadId: The ID of the thread that was run.
/// - runId: The ID of the run to retrieve.
/// - Returns: The run object matching the specified ID.
func retrieve(by threadId: String, runId: String) async throws -> ASARun
}

public final class RunsAPI: HTTPClient, IRunsAPI {
Expand Down Expand Up @@ -43,4 +65,19 @@ public final class RunsAPI: HTTPClient, IRunsAPI {
return try await sendRequest(session: urlSession, endpoint: endpoint, responseModel: ASARun.self)
}

public func listRuns(by threadId: String, parameters: ASAListRunsParameters?) async throws -> ASARunsListResponse {
let endpoint = RunsEndpoint.listRuns(threadId, parameters)
return try await sendRequest(session: urlSession, endpoint: endpoint, responseModel: ASARunsListResponse.self)
}

public func modify(by threadId: String, runId: String, modifyRun: ASAModifyRunRequest) async throws -> ASARun {
let endpoint = RunsEndpoint.modifyRun(threadId, runId, modifyRun)
return try await sendRequest(session: urlSession, endpoint: endpoint, responseModel: ASARun.self)
}

public func retrieve(by threadId: String, runId: String) async throws -> ASARun {
let endpoint = RunsEndpoint.retrieveRun(threadId, runId)
return try await sendRequest(session: urlSession, endpoint: endpoint, responseModel: ASARun.self)
}

}
16 changes: 9 additions & 7 deletions Sources/AISwiftAssist/Base/HTTPClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -58,18 +58,20 @@ extension HTTPClient {
if responseModel is Data.Type {
return responseModel as! T
}
if let decodeData = data.decode(model: responseModel) {
do {
let decodeData = try data.decode(model: responseModel)
return decodeData
} else {
throw HTTPRequestError.decode
} catch {
throw HTTPRequestError.decode(error.localizedDescription)
}
case 400:
if let decodeData = data.decode(model: ValidatorErrorResponse.self) {
do {
let decodeData = try data.decode(model: ValidatorErrorResponse.self)
throw HTTPRequestError.validator(error: decodeData)
} catch {
throw HTTPRequestError.unexpectedStatusCode(code: responseCode,
localized: responseCode.localStatusCode)
}
throw HTTPRequestError.unexpectedStatusCode(code: responseCode,
localized: responseCode.localStatusCode)

case 401, 403: throw HTTPRequestError.unauthorizate
default: throw HTTPRequestError.unexpectedStatusCode(code: responseCode,
localized: responseCode.localStatusCode)
Expand Down
6 changes: 3 additions & 3 deletions Sources/AISwiftAssist/Base/HTTPRequestError.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public struct ValidatorErrorResponse: Codable {
/// Types of HTTP Request Errors
public enum HTTPRequestError: Error {
/// Model decoding error
case decode
case decode(String)
/// URL validation error
case invalidURL
/// Error receiving response from server
Expand Down Expand Up @@ -47,8 +47,8 @@ public enum HTTPRequestError: Error {
extension HTTPRequestError: LocalizedError {
public var errorDescription: String? {
switch self {
case .decode:
return "Decoding error"
case .decode(let message):
return "Decoding error\n\(message)"
case .invalidURL:
return "Invalid URL"
case .noResponse:
Expand Down
4 changes: 2 additions & 2 deletions Sources/AISwiftAssist/Extensions/Data+decode.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import Foundation

extension Data {
func decode<T: Decodable>(model: T.Type) -> T? {
return try? JSONDecoder().decode(model, from: self)
func decode<T: Decodable>(model: T.Type) throws -> T {
return try JSONDecoder().decode(model, from: self)
}
}
13 changes: 10 additions & 3 deletions Sources/AISwiftAssist/Models/Main/ASARun.swift
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public struct ASARun: Codable {
public let lastError: LastError?

/// The Unix timestamp (in seconds) for when the run will expire.
public let expiresAt: Int
public let expiresAt: Int?

/// The Unix timestamp (in seconds) for when the run was started. Null if not started.
public let startedAt: Int?
Expand All @@ -52,10 +52,11 @@ public struct ASARun: Codable {
public let model: String

/// The instructions that the assistant used for this run.
public let instructions: String
public let instructions: String?

/// The list of tools that the assistant used for this run.
public let tools: [String]
/// Tools can be of types code_interpreter, retrieval, or function.
public let tools: [Tool]

/// The list of File IDs the assistant used for this run.
public let fileIds: [String]
Expand Down Expand Up @@ -112,6 +113,12 @@ public struct ASARun: Codable {
public let message: String
}

/// Represents a tool enabled on the assistant.
public struct Tool: Codable {
/// The type of the tool (e.g., code_interpreter, retrieval, function).
public let type: String
}

enum CodingKeys: String, CodingKey {
case id, object
case createdAt = "created_at"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import Foundation

/// A response structure for listing assistants.
public struct ASAListAssistantsResponse: Codable {
public struct ASAAssistantsListResponse: Codable {
/// The object type, which is always 'list'.
public let object: String

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

import Foundation

public struct ASAListModelsResponse: Codable {
public struct ASAModelsListResponse: Codable {

/// The object type, which is always "list".
public let object: String
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import Foundation

/// Represents a response containing a list of runs.
public struct ASAListRunsResponse: Codable {
public struct ASARunsListResponse: Codable {
/// The object type, which is always 'list'.
public let object: String

Expand Down
2 changes: 1 addition & 1 deletion Tests/AISwiftAssistTests/APIs/AssistantsAPITests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ final class AssistantsAPITests: XCTestCase {
return (response, mockData)
}

let response: ASAListAssistantsResponse = try await assistantsAPI.get(with: nil)
let response: ASAAssistantsListResponse = try await assistantsAPI.get(with: nil)

XCTAssertNotNil(response)
XCTAssertEqual(response.object, "list")
Expand Down
4 changes: 2 additions & 2 deletions Tests/AISwiftAssistTests/APIs/MessagesAPITests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,8 @@ final class MessagesAPITests: XCTestCase {
return (response, mockData)
}

let listResponse: ASAMessagesListResponse = try await messagesAPI.getMessages(by: "thread_abc123", parameters: nil)
let listResponse: ASAMessagesListResponse = try await messagesAPI.getMessages(by: "thread_abc123",
parameters: nil)

XCTAssertEqual(listResponse.object, "list")
XCTAssertEqual(listResponse.data.count, 2)
Expand All @@ -122,5 +123,4 @@ final class MessagesAPITests: XCTestCase {
}
}


}
2 changes: 1 addition & 1 deletion Tests/AISwiftAssistTests/APIs/ModelsAPITests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ final class ModelsAPITests: XCTestCase {
return (response, mockData)
}

let listResponse: ASAListModelsResponse = try await modelsAPI.get()
let listResponse: ASAModelsListResponse = try await modelsAPI.get()

XCTAssertEqual(listResponse.data[0].id, "model-id-0")
XCTAssertEqual(listResponse.data[0].object, "model")
Expand Down
Loading

0 comments on commit 13e4d1b

Please sign in to comment.