Skip to content
Merged
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
1 change: 1 addition & 0 deletions TablePro/Core/AI/AIProviderFactory.swift
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ enum AIProviderFactory {
endpoint: config.endpoint,
apiKey: apiKey,
providerType: config.type,
model: config.model,
maxOutputTokens: config.maxOutputTokens
)
}
Expand Down
21 changes: 17 additions & 4 deletions TablePro/Core/AI/OpenAICompatibleProvider.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,27 @@ final class OpenAICompatibleProvider: AIProvider {
private let endpoint: String
private let apiKey: String?
private let providerType: AIProviderType
private let model: String
private let maxOutputTokens: Int?
private let session: URLSession
private var testConnectionModel: String {
model.isEmpty ? "test" : model
}

init(endpoint: String, apiKey: String?, providerType: AIProviderType, maxOutputTokens: Int? = nil) {
init(
endpoint: String,
apiKey: String?,
providerType: AIProviderType,
model: String = "",
maxOutputTokens: Int? = nil,
session: URLSession = URLSession(configuration: .ephemeral)
) {
self.endpoint = endpoint.hasSuffix("/") ? String(endpoint.dropLast()) : endpoint
self.apiKey = apiKey?.trimmingCharacters(in: .whitespacesAndNewlines)
self.providerType = providerType
self.model = model.trimmingCharacters(in: .whitespacesAndNewlines)
self.maxOutputTokens = maxOutputTokens
self.session = URLSession(configuration: .ephemeral)
self.session = session
}

// MARK: - AIProvider
Expand Down Expand Up @@ -183,14 +195,14 @@ final class OpenAICompatibleProvider: AIProvider {
}

let body: [String: Any] = [
"model": "test",
"model": testConnectionModel,
"messages": [["role": "user", "content": "Hi"]],
"max_tokens": 1,
"stream": false,
]
request.httpBody = try JSONSerialization.data(withJSONObject: body)

let (_, response) = try await session.data(for: request)
let (data, response) = try await session.data(for: request)

guard let httpResponse = response as? HTTPURLResponse else {
return false
Expand All @@ -199,6 +211,7 @@ final class OpenAICompatibleProvider: AIProvider {
// Check response is JSON (confirms we reached an API, not a random web page)
let contentType = httpResponse.value(forHTTPHeaderField: "Content-Type") ?? ""
let isJSON = contentType.contains("application/json")
|| (try? JSONSerialization.jsonObject(with: data)) != nil

if httpResponse.statusCode == 401 {
throw AIProviderError.authenticationFailed("")
Expand Down
1 change: 1 addition & 0 deletions TablePro/Core/AI/Registry/AIProviderRegistration.swift
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ enum AIProviderRegistration {
endpoint: config.endpoint,
apiKey: apiKey,
providerType: config.type,
model: config.model,
maxOutputTokens: config.maxOutputTokens
)
}
Expand Down
Loading