-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Add LongCat usage provider #2217
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
Closed
Closed
Changes from 8 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
56f1892
feat: add LongCat usage provider
LeoLin990405 1cd4332
fix(longcat): surface invalid-session and drop unused today-token path
LeoLin990405 ff72ead
fix(longcat): route env cookie through settings reader
LeoLin990405 e0cd2b7
fix(longcat): honor cookie source Off/Manual in web strategy
LeoLin990405 01ea71d
fix(longcat): gate browser cookie import
43052e2
fix(longcat): tighten quota and cookie defaults
e945811
test(longcat): cover HTTP status handling; pin fuel-pack fields
008ed4c
Isolate LongCat HTTP transport
joeVenner ad26479
Fix LongCat lint checks
joeVenner 7a9941a
Fix LongCat cookie CLI handling
joeVenner 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
100 changes: 100 additions & 0 deletions
100
Sources/CodexBar/Providers/LongCat/LongCatProviderImplementation.swift
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,100 @@ | ||
| import AppKit | ||
| import CodexBarCore | ||
| import Foundation | ||
| import SwiftUI | ||
|
|
||
| struct LongCatProviderImplementation: ProviderImplementation { | ||
| let id: UsageProvider = .longcat | ||
|
|
||
| @MainActor | ||
| func presentation(context _: ProviderPresentationContext) -> ProviderPresentation { | ||
| ProviderPresentation { context in | ||
| context.store.sourceLabel(for: context.provider) | ||
| } | ||
| } | ||
|
|
||
| @MainActor | ||
| func observeSettings(_ settings: SettingsStore) { | ||
| _ = settings.longcatUsageDataSource | ||
| _ = settings.longcatCookieSource | ||
| _ = settings.longcatManualCookieHeader | ||
| } | ||
|
|
||
| @MainActor | ||
| func settingsSnapshot(context: ProviderSettingsSnapshotContext) -> ProviderSettingsSnapshotContribution? { | ||
| .longcat(context.settings.longcatSettingsSnapshot(tokenOverride: context.tokenOverride)) | ||
| } | ||
|
|
||
| @MainActor | ||
| func defaultSourceLabel(context: ProviderSourceLabelContext) -> String? { | ||
| context.settings.longcatUsageDataSource.rawValue | ||
| } | ||
|
|
||
| @MainActor | ||
| func sourceMode(context: ProviderSourceModeContext) -> ProviderSourceMode { | ||
| switch context.settings.longcatUsageDataSource { | ||
| case .web: .web | ||
| case .auto, .api, .cli, .oauth: .auto | ||
| } | ||
| } | ||
|
|
||
| @MainActor | ||
| func settingsPickers(context: ProviderSettingsContext) -> [ProviderSettingsPickerDescriptor] { | ||
| let cookieBinding = Binding( | ||
| get: { context.settings.longcatCookieSource.rawValue }, | ||
| set: { raw in | ||
| context.settings.longcatCookieSource = ProviderCookieSource(rawValue: raw) ?? .auto | ||
| }) | ||
| let options = ProviderCookieSourceUI.options( | ||
| allowsOff: true, | ||
| keychainDisabled: context.settings.debugDisableKeychainAccess) | ||
|
|
||
| let subtitle: () -> String? = { | ||
| ProviderCookieSourceUI.subtitle( | ||
| source: context.settings.longcatCookieSource, | ||
| keychainDisabled: context.settings.debugDisableKeychainAccess, | ||
| auto: "Automatic imports longcat.chat cookies from your browser.", | ||
| manual: "Paste a Cookie header copied from longcat.chat.", | ||
| off: "LongCat cookies are disabled.") | ||
| } | ||
|
|
||
| return [ | ||
| ProviderSettingsPickerDescriptor( | ||
| id: "longcat-cookie-source", | ||
| title: "Cookie source", | ||
| subtitle: "Automatic imports longcat.chat cookies from your browser.", | ||
| dynamicSubtitle: subtitle, | ||
| binding: cookieBinding, | ||
| options: options, | ||
| isVisible: nil, | ||
| onChange: nil), | ||
| ] | ||
| } | ||
|
|
||
| @MainActor | ||
| func settingsFields(context: ProviderSettingsContext) -> [ProviderSettingsFieldDescriptor] { | ||
| [ | ||
| ProviderSettingsFieldDescriptor( | ||
| id: "longcat-cookie", | ||
| title: "", | ||
| subtitle: "", | ||
| kind: .secure, | ||
| placeholder: "Cookie: \u{2026}", | ||
| binding: context.stringBinding(\.longcatManualCookieHeader), | ||
| actions: [ | ||
| ProviderSettingsActionDescriptor( | ||
| id: "longcat-open-console", | ||
| title: "Open Console", | ||
| style: .link, | ||
| isVisible: nil, | ||
| perform: { | ||
| if let url = URL(string: "https://longcat.chat/platform/") { | ||
| NSWorkspace.shared.open(url) | ||
| } | ||
| }), | ||
| ], | ||
| isVisible: { context.settings.longcatCookieSource == .manual }, | ||
| onActivate: { context.settings.ensureLongCatCookieLoaded() }), | ||
| ] | ||
| } | ||
| } | ||
54 changes: 54 additions & 0 deletions
54
Sources/CodexBar/Providers/LongCat/LongCatSettingsStore.swift
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,54 @@ | ||
| import CodexBarCore | ||
| import Foundation | ||
|
|
||
| extension SettingsStore { | ||
| var longcatUsageDataSource: ProviderSourceMode { | ||
| get { self.configSnapshot.providerConfig(for: .longcat)?.source ?? .auto } | ||
| set { | ||
| let source: ProviderSourceMode? = switch newValue { | ||
| case .auto: .auto | ||
| case .web: .web | ||
| case .api, .cli, .oauth: .auto | ||
| } | ||
| self.updateProviderConfig(provider: .longcat) { entry in | ||
| entry.source = source | ||
| } | ||
| self.logProviderModeChange(provider: .longcat, field: "usageSource", value: newValue.rawValue) | ||
| } | ||
| } | ||
|
|
||
| var longcatManualCookieHeader: String { | ||
| get { self.configSnapshot.providerConfig(for: .longcat)?.sanitizedCookieHeader ?? "" } | ||
| set { | ||
| self.updateProviderConfig(provider: .longcat) { entry in | ||
| entry.cookieHeader = self.normalizedConfigValue(newValue) | ||
| } | ||
| self.logSecretUpdate(provider: .longcat, field: "cookieHeader", value: newValue) | ||
| } | ||
| } | ||
|
|
||
| var longcatCookieSource: ProviderCookieSource { | ||
| get { self.resolvedCookieSource(provider: .longcat, fallback: .auto) } | ||
| set { | ||
| self.updateProviderConfig(provider: .longcat) { entry in | ||
| entry.cookieSource = newValue | ||
| } | ||
| self.logProviderModeChange(provider: .longcat, field: "cookieSource", value: newValue.rawValue) | ||
| } | ||
| } | ||
|
|
||
| func ensureLongCatCookieLoaded() {} | ||
| } | ||
|
|
||
| extension SettingsStore { | ||
| func longcatSettingsSnapshot(tokenOverride: TokenAccountOverride?) | ||
| -> ProviderSettingsSnapshot.LongCatProviderSettings | ||
| { | ||
| self.ensureLongCatCookieLoaded() | ||
| return self.resolvedCookieSettings( | ||
| provider: .longcat, | ||
| configuredSource: self.longcatCookieSource, | ||
| configuredHeader: self.longcatManualCookieHeader, | ||
| tokenOverride: tokenOverride) | ||
| } | ||
| } |
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
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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 |
|---|---|---|
| @@ -1,5 +1,5 @@ | ||
| // Generated by Scripts/regenerate-codex-parser-hash.sh. Do not edit by hand. | ||
|
|
||
| enum CodexParserHash { | ||
| static let value = "9b26fd821cf090dc" | ||
| static let value = "910b475f0fded3e5" | ||
| } |
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
27 changes: 27 additions & 0 deletions
27
Sources/CodexBarCore/Providers/LongCat/LongCatAPIError.swift
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,27 @@ | ||
| import Foundation | ||
|
|
||
| public enum LongCatAPIError: LocalizedError, Sendable, Equatable { | ||
| case missingCookies | ||
| case invalidSession | ||
| case invalidRequest(String) | ||
| case networkError(String) | ||
| case apiError(String) | ||
| case parseFailed(String) | ||
|
|
||
| public var errorDescription: String? { | ||
| switch self { | ||
| case .missingCookies: | ||
| "LongCat session cookies are missing. Sign in at longcat.chat, or paste a cookie header." | ||
| case .invalidSession: | ||
| "LongCat session is invalid or expired. Please sign in again at longcat.chat." | ||
| case let .invalidRequest(message): | ||
| "Invalid request: \(message)" | ||
| case let .networkError(message): | ||
| "LongCat network error: \(message)" | ||
| case let .apiError(message): | ||
| "LongCat API error: \(message)" | ||
| case let .parseFailed(message): | ||
| "Failed to parse LongCat usage data: \(message)" | ||
| } | ||
| } | ||
| } |
77 changes: 77 additions & 0 deletions
77
Sources/CodexBarCore/Providers/LongCat/LongCatCookieHeader.swift
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,77 @@ | ||
| import Foundation | ||
|
|
||
| public struct LongCatCookieOverride: Sendable { | ||
| /// Full `Cookie:` header value (e.g. `name=value; name2=value2`). | ||
| public let cookieHeader: String | ||
|
|
||
| public init(cookieHeader: String) { | ||
| self.cookieHeader = cookieHeader | ||
| } | ||
| } | ||
|
|
||
| public enum LongCatCookieHeader { | ||
| private static let log = CodexBarLog.logger(LogCategories.longcatCookie) | ||
| private static let headerPatterns: [String] = [ | ||
| #"(?i)-H\s*'Cookie:\s*([^']+)'"#, | ||
| #"(?i)-H\s*"Cookie:\s*([^"]+)""#, | ||
| #"(?i)\bcookie:\s*'([^']+)'"#, | ||
| #"(?i)\bcookie:\s*"([^"]+)""#, | ||
| #"(?i)\bcookie:\s*([^\r\n]+)"#, | ||
| ] | ||
|
|
||
| public static func resolveCookieOverride(context: ProviderFetchContext) -> LongCatCookieOverride? { | ||
| // Off disables LongCat web auth entirely — including a lingering env cookie. | ||
| if context.settings?.longcat?.cookieSource == .off { | ||
| return nil | ||
| } | ||
|
|
||
| if let settings = context.settings?.longcat, settings.cookieSource == .manual { | ||
| if let manual = settings.manualCookieHeader, !manual.isEmpty { | ||
| return self.override(from: manual) | ||
| } | ||
| } | ||
|
|
||
| // Route env cookies through the settings reader so the lower-case | ||
| // `longcat_manual_cookie` alias and quote-trimming apply on the env path too. | ||
| if let envValue = LongCatSettingsReader.cookieHeader(environment: context.env), | ||
| let envHeader = self.override(from: envValue) | ||
| { | ||
| return envHeader | ||
| } | ||
|
|
||
| return nil | ||
| } | ||
|
|
||
| public static func override(from raw: String?) -> LongCatCookieOverride? { | ||
| guard let raw = raw?.trimmingCharacters(in: .whitespacesAndNewlines), !raw.isEmpty else { | ||
| return nil | ||
| } | ||
|
|
||
| if let header = self.extractHeader(from: raw) { | ||
| return LongCatCookieOverride(cookieHeader: header) | ||
| } | ||
|
|
||
| // A bare `name=value; ...` string is itself a usable cookie header. | ||
| if raw.contains("=") { | ||
| return LongCatCookieOverride(cookieHeader: raw) | ||
| } | ||
|
|
||
| return nil | ||
| } | ||
|
|
||
| private static func extractHeader(from raw: String) -> String? { | ||
| for pattern in self.headerPatterns { | ||
| guard let regex = try? NSRegularExpression(pattern: pattern, options: []) else { continue } | ||
| let range = NSRange(raw.startIndex..<raw.endIndex, in: raw) | ||
| guard let match = regex.firstMatch(in: raw, options: [], range: range), | ||
| match.numberOfRanges >= 2, | ||
| let captureRange = Range(match.range(at: 1), in: raw) | ||
| else { | ||
| continue | ||
| } | ||
| let captured = String(raw[captureRange]).trimmingCharacters(in: .whitespacesAndNewlines) | ||
| if !captured.isEmpty { return captured } | ||
| } | ||
| return nil | ||
| } | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This app-side snapshot wiring does not cover the CLI path:
TokenAccountCLIContext.makeCookieBackedSnapshot/makeSnapshothas no LongCat case, and the environment resolver does not project a configcookieHeaderintoLONGCAT_MANUAL_COOKIE. In the scenario where a user runscodexbar usage --provider longcatwith LongCat credentials saved in the config, the fetch context is built withsettings == nil, soLongCatCookieHeaderonly sees process env and reports missing cookies even though the config contains them.Useful? React with 👍 / 👎.