Skip to content

A native macOS / windows / Linux database IDE built with Swift and AppKit. Connect to PostgreSQL, MySQL, SQLite, and Redis from a single app with a fast, keyboard-driven interface.

C++

fix(ai): real default model + GridexError factory + clean type-switch

Tobic9eb0f4265cd
diff --git a/macos/Core/Enums/ProviderType.swift b/macos/Core/Enums/ProviderType.swift
index 4332b5a..42fab21 100644
--- a/macos/Core/Enums/ProviderType.swift
+++ b/macos/Core/Enums/ProviderType.swift
@@ -104,7 +104,13 @@ enum ProviderType: String, Codable, Sendable, CaseIterable, Identifiable {
case .anthropic: return "claude-sonnet-4-6"
case .gemini: return "gemini-2.5-flash"
case .ollama: return "llama3"
- case .chatGPT: return "gpt-5.4"
+ // ChatGPT model slugs are owned by `/backend-api/codex/models`. The
+ // authoritative list comes from the live fetch after sign-in;
+ // `gpt-5-codex` is Codex CLI's own default and the safest known-real
+ // slug to use as a stop-gap until the live `/models` call resolves.
+ // (Earlier value `gpt-5.4` was invented and produced HTTP 400 on
+ // first request before the picker had a chance to update.)
+ case .chatGPT: return "gpt-5-codex"
case .openAI: return "gpt-4o"
case .azureOpenAI: return "gpt-4o"
case .groq: return "llama-3.3-70b-versatile"
diff --git a/macos/Presentation/Windows/Settings/ProviderEditSheet.swift b/macos/Presentation/Windows/Settings/ProviderEditSheet.swift
index 4f7910b..9a0ff24 100644
--- a/macos/Presentation/Windows/Settings/ProviderEditSheet.swift
+++ b/macos/Presentation/Windows/Settings/ProviderEditSheet.swift
@@ -353,6 +353,16 @@ struct ProviderEditSheet: View {
Task { await refreshChatGPTStatus() }
return
}
+ // Switching OUT of .chatGPT mid-flow: cancel any in-flight OAuth task so
+ // a tardy callback doesn't write tokens to the keychain after the form
+ // has already moved on, and reset the ChatGPT-side UI state so a later
+ // switch back to .chatGPT starts clean.
+ chatGPTSignInTask?.cancel()
+ chatGPTSignInTask = nil
+ isSigningIn = false
+ signInError = nil
+ chatGPTStatus = .signedOut
+
apiBase = newType.defaultBaseURL
model = "" // user must fetch or type — avoids presetting a model the endpoint may not support
availableModels = []
diff --git a/macos/Services/AI/ProviderFactory.swift b/macos/Services/AI/ProviderFactory.swift
index 23fd097..1c9f209 100644
--- a/macos/Services/AI/ProviderFactory.swift
+++ b/macos/Services/AI/ProviderFactory.swift
@@ -32,7 +32,11 @@ enum ProviderFactory {
return OllamaProvider(baseURLString: base)
case .chatGPT:
guard let svc = chatGPTOAuthService else {
- preconditionFailure("ChatGPT provider requires chatGPTOAuthService")
+ // Surface as a normal LLMService error rather than crashing —
+ // misconfiguration in a future caller (e.g. the legacy
+ // type-only `make(type:apiKey:baseURL:)`) becomes a clean
+ // GridexError on first use instead of a process crash.
+ return MisconfiguredChatGPTProvider()
}
return ChatGPTProvider(providerId: config.id, baseURL: base, oauthService: svc)
case .openAI, .azureOpenAI, .groq, .deepseek, .mistral, .xAI,
@@ -54,3 +58,37 @@ enum ProviderFactory {
return make(config: config, apiKey: apiKey)
}
}
+
+/// Stand-in returned when `make(...)` is asked for a `.chatGPT` provider but
+/// the caller didn't thread the OAuth service through. Every method throws
+/// `GridexError.aiProviderError`, so the misconfiguration surfaces as a normal
+/// in-band error on the first request rather than a process-wide crash.
+private struct MisconfiguredChatGPTProvider: LLMService {
+ let providerName = "ChatGPT (misconfigured)"
+
+ func stream(
+ messages: [LLMMessage],
+ systemPrompt: String,
+ model: String,
+ maxTokens: Int,
+ temperature: Double
+ ) -> AsyncThrowingStream<String, Error> {
+ AsyncThrowingStream { continuation in
+ continuation.finish(throwing: GridexError.aiProviderError(
+ "ChatGPT provider misconfigured — OAuth service was not supplied to ProviderFactory."
+ ))
+ }
+ }
+
+ func availableModels() async throws -> [LLMModel] {
+ throw GridexError.aiProviderError(
+ "ChatGPT provider misconfigured — OAuth service was not supplied to ProviderFactory."
+ )
+ }
+
+ func validateAPIKey() async throws -> Bool {
+ throw GridexError.aiProviderError(
+ "ChatGPT provider misconfigured — OAuth service was not supplied to ProviderFactory."
+ )
+ }
+}
diff --git a/macos/Tests/GridexTests/ProviderFactoryTests.swift b/macos/Tests/GridexTests/ProviderFactoryTests.swift
new file mode 100644
index 0000000..9a46750
--- /dev/null
+++ b/macos/Tests/GridexTests/ProviderFactoryTests.swift
@@ -0,0 +1,108 @@
+// ProviderFactoryTests.swift
+//
+// Pin the dispatch behaviour of ProviderFactory.make so a future contributor
+// can't silently break the .chatGPT case (e.g. by flipping a switch label,
+// or by reaching for the legacy `make(type:apiKey:baseURL:)` overload that
+// can't thread an OAuth service through).
+
+import XCTest
+@testable import Gridex
+
+final class ProviderFactoryTests: XCTestCase {
+
+ // MARK: - .chatGPT dispatch
+
+ func test_make_returnsChatGPTProvider_whenOAuthServiceProvided() {
+ let oauth = ChatGPTOAuthService(
+ keychainService: KeychainService(),
+ urlSession: .shared
+ )
+ let config = ProviderConfig(name: "ChatGPT", type: .chatGPT)
+
+ let service = ProviderFactory.make(
+ config: config,
+ apiKey: "",
+ chatGPTOAuthService: oauth
+ )
+
+ XCTAssertTrue(service is ChatGPTProvider,
+ "with OAuth service supplied, factory must return a real ChatGPTProvider")
+ }
+
+ func test_make_returnsMisconfiguredStub_whenOAuthServiceMissing() {
+ // Reachable from `make(type:apiKey:baseURL:)` (legacy / DependencyContainer
+ // path) which never threads chatGPTOAuthService through.
+ let config = ProviderConfig(name: "ChatGPT", type: .chatGPT)
+ let service = ProviderFactory.make(config: config, apiKey: "")
+
+ // Stub is private; verify by behaviour: every call must throw GridexError.
+ // (Pre-fix this code-path was preconditionFailure → process crash.)
+ let stream = service.stream(
+ messages: [],
+ systemPrompt: "",
+ model: "gpt-5-codex",
+ maxTokens: 100,
+ temperature: 1.0
+ )
+
+ let exp = expectation(description: "stream throws aiProviderError")
+ Task {
+ do {
+ for try await _ in stream {}
+ XCTFail("misconfigured stub must not yield values")
+ } catch let GridexError.aiProviderError(msg) {
+ XCTAssertTrue(msg.contains("misconfigured"), "got: \(msg)")
+ exp.fulfill()
+ } catch {
+ XCTFail("expected GridexError.aiProviderError, got \(error)")
+ }
+ }
+ wait(for: [exp], timeout: 1.0)
+ }
+
+ func test_make_misconfiguredStub_availableModelsThrows() async {
+ let config = ProviderConfig(name: "ChatGPT", type: .chatGPT)
+ let service = ProviderFactory.make(config: config, apiKey: "")
+
+ do {
+ _ = try await service.availableModels()
+ XCTFail("availableModels must throw when ChatGPT factory is misconfigured")
+ } catch let GridexError.aiProviderError(msg) {
+ XCTAssertTrue(msg.contains("misconfigured"))
+ } catch {
+ XCTFail("expected GridexError.aiProviderError, got \(error)")
+ }
+ }
+
+ // MARK: - Other providers route correctly
+
+ func test_make_anthropic_returnsAnthropicProvider() {
+ let config = ProviderConfig(name: "Anthropic", type: .anthropic)
+ let service = ProviderFactory.make(config: config, apiKey: "sk-test")
+ XCTAssertTrue(service is AnthropicProvider)
+ }
+
+ func test_make_openAI_returnsOpenAIProvider() {
+ let config = ProviderConfig(name: "OpenAI", type: .openAI)
+ let service = ProviderFactory.make(config: config, apiKey: "sk-test")
+ XCTAssertTrue(service is OpenAIProvider)
+ }
+
+ // MARK: - Default model for .chatGPT must be a real slug
+
+ // Regression for the `defaultModel = "gpt-5.4"` bug — that slug doesn't
+ // exist server-side, so a freshly-created ChatGPT provider fired a request
+ // before the live `/models` picker resolved would 400 immediately.
+ func test_defaultModel_chatGPT_isRealisticSlug() {
+ let model = ProviderType.chatGPT.defaultModel
+ XCTAssertFalse(model.isEmpty, "ChatGPT default model must be set")
+ // Avoid the literal regression: invented "gpt-X.Y" decimal slugs.
+ XCTAssertFalse(model.contains("gpt-5.4"), "must not use the invented 'gpt-5.4' slug")
+ // Codex CLI uses gpt-5-codex; either that or another known-real slug
+ // (gpt-5, gpt-5-codex variants) is acceptable.
+ XCTAssertTrue(
+ model.hasPrefix("gpt-5") || model == "gpt-4o",
+ "default must be a known-real OpenAI model slug, got: \(model)"
+ )
+ }
+}