-
Notifications
You must be signed in to change notification settings - Fork 492
MiniMax: make region picker visible + retry China API host #344
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 2 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
cc7013b
fix: use region-specific API endpoint for MiniMax usage fetch
apoorvdarshan 0422dd1
MiniMax: retry China API host on invalid credentials
ratulsarna 609eb6b
MiniMax: preserve API fallback on China retry failure
ratulsarna 6e1a33a
Merge branch 'main' into codex/minimax-region-followup
ratulsarna cb43430
Update CHANGELOG for MiniMax region fallback
ratulsarna File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,144 @@ | ||
| import CodexBarCore | ||
| import Foundation | ||
| import Testing | ||
|
|
||
| @Suite(.serialized) | ||
| struct MiniMaxAPITokenFetchTests { | ||
| @Test | ||
| func retriesChinaHostWhenGlobalRejectsToken() async throws { | ||
| let registered = URLProtocol.registerClass(MiniMaxAPITokenStubURLProtocol.self) | ||
| defer { | ||
| if registered { | ||
| URLProtocol.unregisterClass(MiniMaxAPITokenStubURLProtocol.self) | ||
| } | ||
| MiniMaxAPITokenStubURLProtocol.handler = nil | ||
| MiniMaxAPITokenStubURLProtocol.requests = [] | ||
| } | ||
|
|
||
| MiniMaxAPITokenStubURLProtocol.handler = { request in | ||
| guard let url = request.url else { throw URLError(.badURL) } | ||
| let host = url.host ?? "" | ||
| if host == "api.minimax.io" { | ||
| return Self.makeResponse(url: url, body: "{}", statusCode: 401) | ||
| } | ||
| if host == "api.minimaxi.com" { | ||
| let start = 1_700_000_000_000 | ||
| let end = start + 5 * 60 * 60 * 1000 | ||
| let body = """ | ||
| { | ||
| "base_resp": { "status_code": 0 }, | ||
| "current_subscribe_title": "Max", | ||
| "model_remains": [ | ||
| { | ||
| "current_interval_total_count": 1000, | ||
| "current_interval_usage_count": 250, | ||
| "start_time": \(start), | ||
| "end_time": \(end), | ||
| "remains_time": 240000 | ||
| } | ||
| ] | ||
| } | ||
| """ | ||
| return Self.makeResponse(url: url, body: body) | ||
| } | ||
| return Self.makeResponse(url: url, body: "{}", statusCode: 404) | ||
| } | ||
|
|
||
| let now = Date(timeIntervalSince1970: 1_700_000_000) | ||
| let snapshot = try await MiniMaxUsageFetcher.fetchUsage(apiToken: "sk-cp-test", region: .global, now: now) | ||
|
|
||
| #expect(snapshot.planName == "Max") | ||
| #expect(MiniMaxAPITokenStubURLProtocol.requests.count == 2) | ||
| #expect(MiniMaxAPITokenStubURLProtocol.requests.first?.url?.host == "api.minimax.io") | ||
| #expect(MiniMaxAPITokenStubURLProtocol.requests.last?.url?.host == "api.minimaxi.com") | ||
| } | ||
|
|
||
| @Test | ||
| func doesNotRetryWhenRegionIsChinaMainland() async throws { | ||
| let registered = URLProtocol.registerClass(MiniMaxAPITokenStubURLProtocol.self) | ||
| defer { | ||
| if registered { | ||
| URLProtocol.unregisterClass(MiniMaxAPITokenStubURLProtocol.self) | ||
| } | ||
| MiniMaxAPITokenStubURLProtocol.handler = nil | ||
| MiniMaxAPITokenStubURLProtocol.requests = [] | ||
| } | ||
|
|
||
| MiniMaxAPITokenStubURLProtocol.handler = { request in | ||
| guard let url = request.url else { throw URLError(.badURL) } | ||
| let host = url.host ?? "" | ||
| if host == "api.minimaxi.com" { | ||
| let start = 1_700_000_000_000 | ||
| let end = start + 5 * 60 * 60 * 1000 | ||
| let body = """ | ||
| { | ||
| "base_resp": { "status_code": 0 }, | ||
| "current_subscribe_title": "Max", | ||
| "model_remains": [ | ||
| { | ||
| "current_interval_total_count": 1000, | ||
| "current_interval_usage_count": 250, | ||
| "start_time": \(start), | ||
| "end_time": \(end), | ||
| "remains_time": 240000 | ||
| } | ||
| ] | ||
| } | ||
| """ | ||
| return Self.makeResponse(url: url, body: body) | ||
| } | ||
| return Self.makeResponse(url: url, body: "{}", statusCode: 401) | ||
| } | ||
|
|
||
| let now = Date(timeIntervalSince1970: 1_700_000_000) | ||
| _ = try await MiniMaxUsageFetcher.fetchUsage(apiToken: "sk-cp-test", region: .chinaMainland, now: now) | ||
|
|
||
| #expect(MiniMaxAPITokenStubURLProtocol.requests.count == 1) | ||
| #expect(MiniMaxAPITokenStubURLProtocol.requests.first?.url?.host == "api.minimaxi.com") | ||
| } | ||
|
|
||
| private static func makeResponse( | ||
| url: URL, | ||
| body: String, | ||
| statusCode: Int = 200) -> (HTTPURLResponse, Data) | ||
| { | ||
| let response = HTTPURLResponse( | ||
| url: url, | ||
| statusCode: statusCode, | ||
| httpVersion: "HTTP/1.1", | ||
| headerFields: ["Content-Type": "application/json"])! | ||
| return (response, Data(body.utf8)) | ||
| } | ||
| } | ||
|
|
||
| final class MiniMaxAPITokenStubURLProtocol: URLProtocol { | ||
| nonisolated(unsafe) static var handler: ((URLRequest) throws -> (HTTPURLResponse, Data))? | ||
| nonisolated(unsafe) static var requests: [URLRequest] = [] | ||
|
|
||
| override static func canInit(with request: URLRequest) -> Bool { | ||
| guard let host = request.url?.host else { return false } | ||
| return host == "api.minimax.io" || host == "api.minimaxi.com" | ||
| } | ||
|
|
||
| override static func canonicalRequest(for request: URLRequest) -> URLRequest { | ||
| request | ||
| } | ||
|
|
||
| override func startLoading() { | ||
| Self.requests.append(self.request) | ||
| guard let handler = Self.handler else { | ||
| self.client?.urlProtocol(self, didFailWithError: URLError(.badServerResponse)) | ||
| return | ||
| } | ||
| do { | ||
| let (response, data) = try handler(self.request) | ||
| self.client?.urlProtocol(self, didReceive: response, cacheStoragePolicy: .notAllowed) | ||
| self.client?.urlProtocol(self, didLoad: data) | ||
| self.client?.urlProtocolDidFinishLoading(self) | ||
| } catch { | ||
| self.client?.urlProtocol(self, didFailWithError: error) | ||
| } | ||
| } | ||
|
|
||
| override func stopLoading() {} | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.