-
Notifications
You must be signed in to change notification settings - Fork 1.3k
[codex] Add Sakana AI usage provider #1728
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 all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
3585d1b
Add Sakana AI usage provider
e9d4fd6
ci: rerun Sakana provider checks
3676c20
fix(sakana): avoid fake reset descriptions
0bad8fd
Merge remote-tracking branch 'upstream/main' into feat/sakana-provider
f23516e
fix(sakana): parse windows without reset text
84a1c06
fix(sakana): harden cli and quota parsing
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
51 changes: 51 additions & 0 deletions
51
Sources/CodexBar/Providers/Sakana/SakanaProviderImplementation.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,51 @@ | ||
| import AppKit | ||
| import CodexBarCore | ||
| import Foundation | ||
|
|
||
| struct SakanaProviderImplementation: ProviderImplementation { | ||
| let id: UsageProvider = .sakana | ||
|
|
||
| @MainActor | ||
| func presentation(context _: ProviderPresentationContext) -> ProviderPresentation { | ||
| ProviderPresentation { _ in "web" } | ||
| } | ||
|
|
||
| @MainActor | ||
| func observeSettings(_ settings: SettingsStore) { | ||
| _ = settings.sakanaCookieHeader | ||
| } | ||
|
|
||
| @MainActor | ||
| func isAvailable(context: ProviderAvailabilityContext) -> Bool { | ||
| SakanaSettingsReader.cookieHeader(environment: context.environment) != nil | ||
| } | ||
|
|
||
| @MainActor | ||
| func settingsFields(context: ProviderSettingsContext) -> [ProviderSettingsFieldDescriptor] { | ||
| let subtitle = "Stored in ~/.codexbar/config.json. Copy the Sakana AI console Cookie request header." | ||
|
|
||
| return [ | ||
| ProviderSettingsFieldDescriptor( | ||
| id: "sakana-cookie", | ||
| title: "Cookie header", | ||
| subtitle: subtitle, | ||
| kind: .secure, | ||
| placeholder: "Cookie: ...", | ||
| binding: context.stringBinding(\.sakanaCookieHeader), | ||
| actions: [ | ||
| ProviderSettingsActionDescriptor( | ||
| id: "sakana-open-dashboard", | ||
| title: "Open Sakana AI Console", | ||
| style: .link, | ||
| isVisible: nil, | ||
| perform: { | ||
| if let url = URL(string: "https://console.sakana.ai/billing") { | ||
| NSWorkspace.shared.open(url) | ||
| } | ||
| }), | ||
| ], | ||
| isVisible: nil, | ||
| onActivate: nil), | ||
| ] | ||
| } | ||
| } |
14 changes: 14 additions & 0 deletions
14
Sources/CodexBar/Providers/Sakana/SakanaSettingsStore.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,14 @@ | ||
| import CodexBarCore | ||
| import Foundation | ||
|
|
||
| extension SettingsStore { | ||
| var sakanaCookieHeader: String { | ||
| get { self.configSnapshot.providerConfig(for: .sakana)?.sanitizedCookieHeader ?? "" } | ||
| set { | ||
| self.updateProviderConfig(provider: .sakana) { entry in | ||
| entry.cookieHeader = self.normalizedConfigValue(newValue) | ||
| } | ||
| self.logSecretUpdate(provider: .sakana, field: "cookieHeader", value: newValue) | ||
| } | ||
| } | ||
| } |
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
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 = "800a06dead603ea7" | ||
| static let value = "e59cf52c58111c43" | ||
| } |
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
66 changes: 66 additions & 0 deletions
66
Sources/CodexBarCore/Providers/Sakana/SakanaProviderDescriptor.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,66 @@ | ||
| import Foundation | ||
|
|
||
| public enum SakanaProviderDescriptor { | ||
| public static let descriptor: ProviderDescriptor = Self.makeDescriptor() | ||
|
|
||
| static func makeDescriptor() -> ProviderDescriptor { | ||
| ProviderDescriptor( | ||
| id: .sakana, | ||
| metadata: ProviderMetadata( | ||
| id: .sakana, | ||
| displayName: "Sakana AI", | ||
| sessionLabel: "5-hour", | ||
| weeklyLabel: "Weekly", | ||
| opusLabel: nil, | ||
| supportsOpus: false, | ||
| supportsCredits: false, | ||
| creditsHint: "", | ||
| toggleTitle: "Show Sakana AI usage", | ||
| cliName: "sakana", | ||
| defaultEnabled: false, | ||
| isPrimaryProvider: false, | ||
| usesAccountFallback: false, | ||
| browserCookieOrder: nil, | ||
| dashboardURL: "https://console.sakana.ai/billing", | ||
| statusPageURL: nil), | ||
| branding: ProviderBranding( | ||
| iconStyle: .sakana, | ||
| iconResourceName: "ProviderIcon-sakana", | ||
| color: ProviderColor(red: 0.16, green: 0.46, blue: 0.86)), | ||
| tokenCost: ProviderTokenCostConfig( | ||
| supportsTokenCost: false, | ||
| noDataMessage: { "Sakana AI cost summary is not supported." }), | ||
| fetchPlan: ProviderFetchPlan( | ||
| sourceModes: [.auto, .web], | ||
| pipeline: ProviderFetchPipeline(resolveStrategies: { _ in | ||
| [SakanaWebFetchStrategy()] | ||
| })), | ||
| cli: ProviderCLIConfig( | ||
| name: "sakana", | ||
| aliases: ["sakana-ai"], | ||
| versionDetector: nil)) | ||
| } | ||
| } | ||
|
|
||
| struct SakanaWebFetchStrategy: ProviderFetchStrategy { | ||
| let id: String = "sakana.web" | ||
| let kind: ProviderFetchKind = .web | ||
|
|
||
| func isAvailable(_ context: ProviderFetchContext) async -> Bool { | ||
| SakanaSettingsReader.cookieHeader(environment: context.env) != nil | ||
| } | ||
|
|
||
| func fetch(_ context: ProviderFetchContext) async throws -> ProviderFetchResult { | ||
| guard let cookieHeader = SakanaSettingsReader.cookieHeader(environment: context.env) else { | ||
| throw SakanaUsageError.missingCookie | ||
| } | ||
| let usage = try await SakanaUsageFetcher.fetchUsage( | ||
| cookieHeader: cookieHeader, | ||
| timeout: context.webTimeout) | ||
| return self.makeResult(usage: usage.toUsageSnapshot(), sourceLabel: "web") | ||
| } | ||
|
|
||
| func shouldFallback(on _: Error, context _: ProviderFetchContext) -> Bool { | ||
| false | ||
| } | ||
| } | ||
22 changes: 22 additions & 0 deletions
22
Sources/CodexBarCore/Providers/Sakana/SakanaSettingsReader.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,22 @@ | ||
| import Foundation | ||
|
|
||
| public enum SakanaSettingsReader { | ||
| public static let cookieHeaderKey = "SAKANA_COOKIE" | ||
|
|
||
| public static func cookieHeader(environment: [String: String] = ProcessInfo.processInfo.environment) -> String? { | ||
| CookieHeaderNormalizer.normalize(self.cleaned(environment[self.cookieHeaderKey])) | ||
| } | ||
|
|
||
| static func cleaned(_ raw: String?) -> String? { | ||
| guard var value = raw?.trimmingCharacters(in: .whitespacesAndNewlines), !value.isEmpty else { | ||
| return nil | ||
| } | ||
| if (value.hasPrefix("\"") && value.hasSuffix("\"")) || | ||
| (value.hasPrefix("'") && value.hasSuffix("'")) | ||
| { | ||
| value = String(value.dropFirst().dropLast()) | ||
| } | ||
| value = value.trimmingCharacters(in: .whitespacesAndNewlines) | ||
| return value.isEmpty ? nil : value | ||
| } | ||
| } |
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.
Uh oh!
There was an error while loading. Please reload this page.