From e3084ff50ec2456973d443ee3f8c93532e9a7680 Mon Sep 17 00:00:00 2001 From: JP Maia Date: Tue, 26 Aug 2025 23:11:48 -0300 Subject: [PATCH 1/4] Fix sessionID decoder from agent --- .../xcschemes/aiXplainKitTests.xcscheme | 60 +++++++++++++++++++ .../Manager/FileManager/FileManager.swift | 5 +- .../AgentExecuteResponse.swift | 2 +- .../ResponseDecoders/AgentOutput.swift | 5 +- 4 files changed, 66 insertions(+), 6 deletions(-) create mode 100644 .swiftpm/xcode/xcshareddata/xcschemes/aiXplainKitTests.xcscheme diff --git a/.swiftpm/xcode/xcshareddata/xcschemes/aiXplainKitTests.xcscheme b/.swiftpm/xcode/xcshareddata/xcschemes/aiXplainKitTests.xcscheme new file mode 100644 index 0000000..f7ac229 --- /dev/null +++ b/.swiftpm/xcode/xcshareddata/xcschemes/aiXplainKitTests.xcscheme @@ -0,0 +1,60 @@ + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/Sources/aiXplainKit/Manager/FileManager/FileManager.swift b/Sources/aiXplainKit/Manager/FileManager/FileManager.swift index da6c78e..435b96f 100644 --- a/Sources/aiXplainKit/Manager/FileManager/FileManager.swift +++ b/Sources/aiXplainKit/Manager/FileManager/FileManager.swift @@ -225,7 +225,7 @@ public final class FileUploadManager { /// - Parameter localURL: The local URL of the data to be uploaded if necessary. /// - Returns: The remote URL of the uploaded data. /// - Throws: Any error that may occur during the file upload process. - public func uploadDataIfNeedIt(from url: URL) async throws -> URL { + public func uploadDataIfNeedIt(from url: URL, temporary: Bool = true) async throws -> URL { var url = url switch url.absoluteString { case let link where link.starts(with: "s3://"): @@ -236,9 +236,8 @@ public final class FileUploadManager { break default: let fileManager = FileUploadManager() - url = try await fileManager.uploadFile(at: url) + url = try await fileManager.uploadFile(at: url, temporary: temporary) } return url } - } diff --git a/Sources/aiXplainKit/Networking/ResponseDecoders/AgentExecuteResponse.swift b/Sources/aiXplainKit/Networking/ResponseDecoders/AgentExecuteResponse.swift index 6adcbc7..cd19300 100644 --- a/Sources/aiXplainKit/Networking/ResponseDecoders/AgentExecuteResponse.swift +++ b/Sources/aiXplainKit/Networking/ResponseDecoders/AgentExecuteResponse.swift @@ -8,7 +8,7 @@ import Foundation struct AgentExecuteResponse:Decodable{ let requestId:String - let sessionId:String + let sessionId:String? let data:String var maybeUrl:URL?{ diff --git a/Sources/aiXplainKit/Networking/ResponseDecoders/AgentOutput.swift b/Sources/aiXplainKit/Networking/ResponseDecoders/AgentOutput.swift index 4e7f5b7..a6f19ce 100644 --- a/Sources/aiXplainKit/Networking/ResponseDecoders/AgentOutput.swift +++ b/Sources/aiXplainKit/Networking/ResponseDecoders/AgentOutput.swift @@ -53,7 +53,8 @@ extension AgentOutput { // MARK: - DataClass public struct DataClass: Codable { - public let input, output, sessionID: String + public let input, output: String + public let sessionID: String? public let intermediateSteps: [IntermediateStep] enum CodingKeys: String, CodingKey { @@ -84,7 +85,7 @@ extension DataClass { func with( input: String? = nil, output: String? = nil, - sessionID: String? = nil, + sessionID: String?? = nil, intermediateSteps: [IntermediateStep]? = nil ) -> DataClass { return DataClass( From 074087b7f8822174aec260288ef28ad748d59a62 Mon Sep 17 00:00:00 2001 From: JP Maia Date: Thu, 28 Aug 2025 10:33:20 -0300 Subject: [PATCH 2/4] Agent session id generation --- .../Modules/Agents/Agents+session.swift | 56 +++++++++++++++++++ .../Modules/Agents/Input/AgentSession.swift | 19 +++++++ Sources/aiXplainKit/Modules/Model/Model.swift | 6 +- Sources/aiXplainKit/Provider/Agent/.swift | 21 +++++++ 4 files changed, 101 insertions(+), 1 deletion(-) create mode 100644 Sources/aiXplainKit/Modules/Agents/Agents+session.swift create mode 100644 Sources/aiXplainKit/Modules/Agents/Input/AgentSession.swift create mode 100644 Sources/aiXplainKit/Provider/Agent/.swift diff --git a/Sources/aiXplainKit/Modules/Agents/Agents+session.swift b/Sources/aiXplainKit/Modules/Agents/Agents+session.swift new file mode 100644 index 0000000..8945f41 --- /dev/null +++ b/Sources/aiXplainKit/Modules/Agents/Agents+session.swift @@ -0,0 +1,56 @@ +// +// File.swift +// aiXplainKit +// +// Created by Joao Maia on 27/08/25. +// + +import Foundation + +extension Agent{ + //TODO: do this + public func createSession() async throws -> AgentSession { + var session = AgentSession() + let headers = try self.networking.buildHeader() + + let payload: [String: Any] = [ + "id": self.id, + "query": "/", + "sessionId": session.id, + "history": [], + "executionParams": [ + "maxTokens": 2048, + "maxIterations": 10, + "outputFormat": "TEXT", + "expectedOutput": NSNull() + ], + "allowHistoryAndSessionId": true + ] + + + guard let backendURL = APIKeyManager.shared.BACKEND_URL else { + throw AgentsError.missingBackendURL + } + + + guard let url = URL(string: backendURL.absoluteString + Networking.Endpoint.agentRun(agentIdentifier: self.id).path ) else { + throw AgentsError.invalidURL(url: backendURL.absoluteString) + } + + + let data = try JSONSerialization.data(withJSONObject: payload, options: []) + let response = try await networking.post(url: url, headers: headers, body: data) + + if let httpResponse = response.1 as? HTTPURLResponse, + httpResponse.statusCode != 201 { + throw NetworkingError.invalidStatusCode(statusCode: httpResponse.statusCode) + } + + return AgentSession() + } + + +} + + + diff --git a/Sources/aiXplainKit/Modules/Agents/Input/AgentSession.swift b/Sources/aiXplainKit/Modules/Agents/Input/AgentSession.swift new file mode 100644 index 0000000..c00110c --- /dev/null +++ b/Sources/aiXplainKit/Modules/Agents/Input/AgentSession.swift @@ -0,0 +1,19 @@ +// +// File.swift +// aiXplainKit +// +// Created by Joao Maia on 27/08/25. +// + +import Foundation + +public struct AgentSession:CustomStringConvertible { + var id: String = UUID().uuidString + var timestamp: Date = Date() + + public var description: String { + return id + } + + +} diff --git a/Sources/aiXplainKit/Modules/Model/Model.swift b/Sources/aiXplainKit/Modules/Model/Model.swift index 479af65..1300155 100644 --- a/Sources/aiXplainKit/Modules/Model/Model.swift +++ b/Sources/aiXplainKit/Modules/Model/Model.swift @@ -79,6 +79,8 @@ public class Model:Codable, CustomStringConvertible { /// The organization or individual who developed the model. public let developedBy: String + + public var supportsStreaming:Bool = false /// Parameters that can be passed to the model during execution public let parameters: [ModelParameter] @@ -135,6 +137,8 @@ public class Model:Codable, CustomStringConvertible { function = try? container.decodeIfPresent(Function.self, forKey: .function) + supportsStreaming = (try? container.decodeIfPresent(Bool.self, forKey: .supportsStreaming)) ?? false + privacy = nil license = nil logger = Logger(subsystem: "AiXplain", category: "Model(\(name)") @@ -190,7 +194,7 @@ public class Model:Codable, CustomStringConvertible { // Private enum for coding keys to improve readability and maintainability. private enum CodingKeys: String, CodingKey { - case id, name, description, supplier, version, license, privacy, pricing, hostedBy, developedBy, params, function + case id, name, description, supplier, version, license, privacy, pricing, hostedBy, developedBy, params, function, supportsStreaming } } diff --git a/Sources/aiXplainKit/Provider/Agent/.swift b/Sources/aiXplainKit/Provider/Agent/.swift new file mode 100644 index 0000000..6c1c3ca --- /dev/null +++ b/Sources/aiXplainKit/Provider/Agent/.swift @@ -0,0 +1,21 @@ +// +// File.swift +// aiXplainKit +// +// Created by Joao Maia on 27/08/25. +// + +import Foundation + +final public class AgentSessionProvider { + static public func create() -> AgentSession { + return AgentSession() + } +} + + + +struct AgentSelection { + let timestamp: Date = .now() + let sessionID: String = UUID().uuidString +} From 29c329048628e684c196ef65210440f35b9932ad Mon Sep 17 00:00:00 2001 From: JP Maia Date: Thu, 28 Aug 2025 11:41:52 -0300 Subject: [PATCH 3/4] Public var in id --- Sources/aiXplainKit/Modules/Agents/Input/AgentSession.swift | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Sources/aiXplainKit/Modules/Agents/Input/AgentSession.swift b/Sources/aiXplainKit/Modules/Agents/Input/AgentSession.swift index c00110c..31bc2a4 100644 --- a/Sources/aiXplainKit/Modules/Agents/Input/AgentSession.swift +++ b/Sources/aiXplainKit/Modules/Agents/Input/AgentSession.swift @@ -8,8 +8,8 @@ import Foundation public struct AgentSession:CustomStringConvertible { - var id: String = UUID().uuidString - var timestamp: Date = Date() + public var id: String = UUID().uuidString + public var timestamp: Date = Date() public var description: String { return id From 103be3d96865786c3c855414ca8675b6ff5170e6 Mon Sep 17 00:00:00 2001 From: JP Maia Date: Fri, 29 Aug 2025 10:35:05 -0300 Subject: [PATCH 4/4] Fix session id format --- .../Modules/Agents/Agents+session.swift | 34 +++++++++---------- .../Modules/Agents/Input/AgentSession.swift | 19 ----------- 2 files changed, 17 insertions(+), 36 deletions(-) delete mode 100644 Sources/aiXplainKit/Modules/Agents/Input/AgentSession.swift diff --git a/Sources/aiXplainKit/Modules/Agents/Agents+session.swift b/Sources/aiXplainKit/Modules/Agents/Agents+session.swift index 8945f41..2e1c5a7 100644 --- a/Sources/aiXplainKit/Modules/Agents/Agents+session.swift +++ b/Sources/aiXplainKit/Modules/Agents/Agents+session.swift @@ -9,23 +9,23 @@ import Foundation extension Agent{ //TODO: do this - public func createSession() async throws -> AgentSession { - var session = AgentSession() + public func createSession() async throws -> String { + var session = self.id + "_" + UUID().uuidString let headers = try self.networking.buildHeader() - + let payload: [String: Any] = [ - "id": self.id, - "query": "/", - "sessionId": session.id, - "history": [], - "executionParams": [ - "maxTokens": 2048, - "maxIterations": 10, - "outputFormat": "TEXT", - "expectedOutput": NSNull() - ], - "allowHistoryAndSessionId": true - ] + "id": self.id, + "query": "/", + "sessionId": session, + "history": [], + "executionParams": [ + "maxTokens": 2048, + "maxIterations": 10, + "outputFormat": "TEXT", + "expectedOutput": NSNull() + ], + "allowHistoryAndSessionId": true + ] guard let backendURL = APIKeyManager.shared.BACKEND_URL else { @@ -45,8 +45,8 @@ extension Agent{ httpResponse.statusCode != 201 { throw NetworkingError.invalidStatusCode(statusCode: httpResponse.statusCode) } - - return AgentSession() + + return session } diff --git a/Sources/aiXplainKit/Modules/Agents/Input/AgentSession.swift b/Sources/aiXplainKit/Modules/Agents/Input/AgentSession.swift deleted file mode 100644 index 31bc2a4..0000000 --- a/Sources/aiXplainKit/Modules/Agents/Input/AgentSession.swift +++ /dev/null @@ -1,19 +0,0 @@ -// -// File.swift -// aiXplainKit -// -// Created by Joao Maia on 27/08/25. -// - -import Foundation - -public struct AgentSession:CustomStringConvertible { - public var id: String = UUID().uuidString - public var timestamp: Date = Date() - - public var description: String { - return id - } - - -}