-
Notifications
You must be signed in to change notification settings - Fork 26
feat: add gh models usage command for premium request billing
#97
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
Open
marcelsafin
wants to merge
2
commits into
github:main
Choose a base branch
from
marcelsafin:feat/usage-command
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+703
−2
Open
Changes from all commits
Commits
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,336 @@ | ||
| // Package usage provides a gh command to show GitHub Models and Copilot usage information. | ||
| package usage | ||
|
|
||
| import ( | ||
| "context" | ||
| "encoding/json" | ||
| "fmt" | ||
| "io" | ||
| "net/http" | ||
| "sort" | ||
| "time" | ||
|
|
||
| "github.com/MakeNowJust/heredoc" | ||
| "github.com/cli/go-gh/v2/pkg/tableprinter" | ||
| "github.com/github/gh-models/pkg/command" | ||
| "github.com/mgutz/ansi" | ||
| "github.com/spf13/cobra" | ||
| ) | ||
|
|
||
| const defaultGitHubAPIBase = "https://api.github.com" | ||
|
|
||
| var ( | ||
| headerColor = ansi.ColorFunc("white+du") | ||
| greenColor = ansi.ColorFunc("green") | ||
| yellowColor = ansi.ColorFunc("yellow") | ||
| ) | ||
|
|
||
| // usageOptions holds configuration for the usage command, allowing dependency injection for testing. | ||
| type usageOptions struct { | ||
| apiBase string | ||
| httpClient *http.Client | ||
| } | ||
|
|
||
| // premiumRequestUsageResponse represents the API response for premium request usage. | ||
| type premiumRequestUsageResponse struct { | ||
| TimePeriod timePeriod `json:"timePeriod"` | ||
| User string `json:"user"` | ||
| UsageItems []usageItem `json:"usageItems"` | ||
| } | ||
|
|
||
| type timePeriod struct { | ||
| Year int `json:"year"` | ||
| Month int `json:"month"` | ||
| Day int `json:"day,omitempty"` | ||
| } | ||
|
|
||
| type usageItem struct { | ||
| Product string `json:"product"` | ||
| SKU string `json:"sku"` | ||
| Model string `json:"model"` | ||
| UnitType string `json:"unitType"` | ||
| PricePerUnit float64 `json:"pricePerUnit"` | ||
| GrossQuantity float64 `json:"grossQuantity"` | ||
| GrossAmount float64 `json:"grossAmount"` | ||
| DiscountQuantity float64 `json:"discountQuantity"` | ||
| DiscountAmount float64 `json:"discountAmount"` | ||
| NetQuantity float64 `json:"netQuantity"` | ||
| NetAmount float64 `json:"netAmount"` | ||
| } | ||
|
|
||
| func defaultOptions() *usageOptions { | ||
| return &usageOptions{ | ||
| apiBase: defaultGitHubAPIBase, | ||
| httpClient: &http.Client{ | ||
| Timeout: 30 * time.Second, | ||
| }, | ||
| } | ||
| } | ||
|
|
||
| // NewUsageCommand returns a new command to show model usage information. | ||
| func NewUsageCommand(cfg *command.Config) *cobra.Command { | ||
| return newUsageCommand(cfg, nil) | ||
| } | ||
|
|
||
| // newUsageCommand is the internal constructor that accepts options for testing. | ||
| func newUsageCommand(cfg *command.Config, opts *usageOptions) *cobra.Command { | ||
| if opts == nil { | ||
| opts = defaultOptions() | ||
| } | ||
|
|
||
| var ( | ||
| flagMonth int | ||
| flagYear int | ||
| flagDay int | ||
| flagToday bool | ||
| ) | ||
|
|
||
| cmd := &cobra.Command{ | ||
| Use: "usage", | ||
| Short: "Show premium request usage and costs", | ||
| Long: heredoc.Docf(` | ||
| Display premium request usage statistics for GitHub Models and Copilot. | ||
|
|
||
| Shows a breakdown of requests by model, with gross and net costs. | ||
| By default, shows usage for the current billing period (month). | ||
|
|
||
| Use %[1]s--today%[1]s to see only today's usage, or %[1]s--month%[1]s and | ||
| %[1]s--year%[1]s to query a specific billing period. | ||
|
|
||
| Requires the %[1]suser%[1]s scope on your GitHub token. If you get a 404 error, | ||
| run: %[1]sgh auth refresh -h github.com -s user%[1]s | ||
| `, "`"), | ||
| Example: heredoc.Doc(` | ||
| # Show current month's usage | ||
| $ gh models usage | ||
|
|
||
| # Show today's usage | ||
| $ gh models usage --today | ||
|
|
||
| # Show usage for a specific month | ||
| $ gh models usage --year 2026 --month 2 | ||
|
|
||
| # Show usage for a specific day | ||
| $ gh models usage --year 2026 --month 3 --day 15 | ||
| `), | ||
| Args: cobra.NoArgs, | ||
| RunE: func(cmd *cobra.Command, args []string) error { | ||
| token := cfg.Token | ||
| if token == "" { | ||
| return fmt.Errorf("no GitHub token found. Please run 'gh auth login' to authenticate") | ||
| } | ||
|
|
||
| ctx := cmd.Context() | ||
|
|
||
| // Resolve time period | ||
| now := time.Now().UTC() | ||
| year := flagYear | ||
| month := flagMonth | ||
| day := flagDay | ||
|
|
||
| if flagToday { | ||
| year = now.Year() | ||
| month = int(now.Month()) | ||
| day = now.Day() | ||
| } | ||
|
marcelsafin marked this conversation as resolved.
|
||
|
|
||
| if year == 0 { | ||
| year = now.Year() | ||
| } | ||
| if month == 0 { | ||
| month = int(now.Month()) | ||
| } | ||
|
|
||
|
marcelsafin marked this conversation as resolved.
|
||
| // Validate date arguments to fail fast with a clear message | ||
| // instead of letting the API return a confusing error. | ||
| if year < 1 { | ||
| return fmt.Errorf("invalid value for --year: %d (must be >= 1)", year) | ||
| } | ||
| if month < 1 || month > 12 { | ||
| return fmt.Errorf("invalid value for --month: %d (must be between 1 and 12)", month) | ||
| } | ||
| // day == 0 means "not specified" and is allowed; validate only if non-zero. | ||
| if day < 0 || day > 31 { | ||
| return fmt.Errorf("invalid value for --day: %d (must be between 1 and 31)", day) | ||
| } | ||
|
|
||
| // Get username | ||
| username, err := getUsername(ctx, opts, token) | ||
| if err != nil { | ||
| return fmt.Errorf("failed to get username: %w", err) | ||
| } | ||
|
|
||
| // Build query params | ||
| query := fmt.Sprintf("?year=%d&month=%d", year, month) | ||
| if day > 0 { | ||
| query += fmt.Sprintf("&day=%d", day) | ||
| } | ||
|
|
||
| // Fetch usage | ||
| data, err := fetchPremiumRequestUsage(ctx, opts, token, username, query) | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| // Format period string | ||
| periodStr := fmt.Sprintf("%d-%02d", data.TimePeriod.Year, data.TimePeriod.Month) | ||
| if data.TimePeriod.Day > 0 { | ||
| periodStr += fmt.Sprintf("-%02d", data.TimePeriod.Day) | ||
| } | ||
|
|
||
| if len(data.UsageItems) == 0 { | ||
| cfg.WriteToOut(fmt.Sprintf("\nNo usage found for %s\n\n", periodStr)) | ||
| return nil | ||
| } | ||
|
|
||
| // Sort by gross amount descending | ||
| items := data.UsageItems | ||
| sort.Slice(items, func(i, j int) bool { | ||
| return items[i].GrossAmount > items[j].GrossAmount | ||
| }) | ||
|
|
||
| // Calculate totals across the rows that will actually be rendered | ||
| // (matching the GrossQuantity > 0 filter applied below) so the | ||
| // printed totals agree with the visible rows. | ||
| var totalReqs, totalGross, totalNet float64 | ||
| for _, item := range items { | ||
| if item.GrossQuantity == 0 { | ||
| continue | ||
| } | ||
| totalReqs += item.GrossQuantity | ||
| totalGross += item.GrossAmount | ||
| totalNet += item.NetAmount | ||
|
marcelsafin marked this conversation as resolved.
|
||
| } | ||
|
|
||
| // Print header | ||
| if cfg.IsTerminalOutput { | ||
| cfg.WriteToOut("\n") | ||
| if flagToday { | ||
| cfg.WriteToOut(fmt.Sprintf("Premium request usage for %s (%s, today)\n", username, periodStr)) | ||
| } else { | ||
| cfg.WriteToOut(fmt.Sprintf("Premium request usage for %s (%s)\n", username, periodStr)) | ||
| } | ||
| cfg.WriteToOut("\n") | ||
| } | ||
|
|
||
| // Print table | ||
| printer := cfg.NewTablePrinter() | ||
|
|
||
| printer.AddHeader([]string{"PRODUCT", "MODEL", "REQUESTS", "GROSS", "NET"}, tableprinter.WithColor(headerColor)) | ||
| printer.EndRow() | ||
|
|
||
| for _, item := range items { | ||
| if item.GrossQuantity == 0 { | ||
| continue | ||
| } | ||
| printer.AddField(item.Product) | ||
| printer.AddField(item.Model) | ||
| printer.AddField(fmt.Sprintf("%.1f", item.GrossQuantity)) | ||
| printer.AddField(fmt.Sprintf("$%.2f", item.GrossAmount)) | ||
| printer.AddField(fmt.Sprintf("$%.2f", item.NetAmount)) | ||
| printer.EndRow() | ||
| } | ||
|
|
||
| if err := printer.Render(); err != nil { | ||
| return err | ||
| } | ||
|
|
||
| // Print summary | ||
| if cfg.IsTerminalOutput { | ||
| cfg.WriteToOut("\n") | ||
| cfg.WriteToOut(fmt.Sprintf("Total: %.0f requests, $%.2f gross, $%.2f net\n", totalReqs, totalGross, totalNet)) | ||
|
|
||
| if totalGross > 0 && totalNet == 0 { | ||
| cfg.WriteToOut(greenColor("All usage included in your plan (100% discount)") + "\n") | ||
| } else if totalGross > 0 && totalNet > 0 { | ||
| pct := (totalNet / totalGross) * 100 | ||
| cfg.WriteToOut(yellowColor(fmt.Sprintf("Net cost: $%.2f (%.0f%% of gross)", totalNet, pct)) + "\n") | ||
| } else if totalNet > 0 { | ||
| // Defensive: avoid divide-by-zero / Inf / NaN if the API | ||
| // reports net cost without a gross amount. | ||
| cfg.WriteToOut(yellowColor(fmt.Sprintf("Net cost: $%.2f (gross amount unavailable; percentage not shown)", totalNet)) + "\n") | ||
| } | ||
| cfg.WriteToOut("\n") | ||
| } | ||
|
|
||
| return nil | ||
| }, | ||
| } | ||
|
|
||
| cmd.Flags().IntVar(&flagYear, "year", 0, "Filter by year (default: current year)") | ||
| cmd.Flags().IntVar(&flagMonth, "month", 0, "Filter by month (default: current month)") | ||
| cmd.Flags().IntVar(&flagDay, "day", 0, "Filter by specific day") | ||
| cmd.Flags().BoolVar(&flagToday, "today", false, "Show only today's usage") | ||
|
|
||
| // --today is shorthand for the current Y/M/D, so it conflicts with | ||
| // any explicit date flag; refuse the combination instead of silently | ||
| // overriding the user's input. | ||
| cmd.MarkFlagsMutuallyExclusive("today", "year") | ||
| cmd.MarkFlagsMutuallyExclusive("today", "month") | ||
| cmd.MarkFlagsMutuallyExclusive("today", "day") | ||
|
|
||
| return cmd | ||
| } | ||
|
|
||
| func getUsername(ctx context.Context, opts *usageOptions, token string) (string, error) { | ||
| req, err := http.NewRequestWithContext(ctx, http.MethodGet, opts.apiBase+"/user", nil) | ||
| if err != nil { | ||
| return "", err | ||
| } | ||
| req.Header.Set("Authorization", "Bearer "+token) | ||
| req.Header.Set("Accept", "application/vnd.github+json") | ||
| req.Header.Set("X-GitHub-Api-Version", "2022-11-28") | ||
|
|
||
| resp, err := opts.httpClient.Do(req) | ||
| if err != nil { | ||
| return "", err | ||
| } | ||
| defer resp.Body.Close() | ||
|
|
||
| if resp.StatusCode != http.StatusOK { | ||
| return "", fmt.Errorf("failed to get user info: HTTP %d", resp.StatusCode) | ||
| } | ||
|
|
||
| var user struct { | ||
| Login string `json:"login"` | ||
| } | ||
| if err := json.NewDecoder(resp.Body).Decode(&user); err != nil { | ||
| return "", err | ||
| } | ||
| return user.Login, nil | ||
| } | ||
|
|
||
| func fetchPremiumRequestUsage(ctx context.Context, opts *usageOptions, token, username, query string) (*premiumRequestUsageResponse, error) { | ||
| url := fmt.Sprintf("%s/users/%s/settings/billing/premium_request/usage%s", opts.apiBase, username, query) | ||
|
|
||
| req, err := http.NewRequestWithContext(ctx, http.MethodGet, url, nil) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
| req.Header.Set("Authorization", "Bearer "+token) | ||
| req.Header.Set("Accept", "application/vnd.github+json") | ||
| req.Header.Set("X-GitHub-Api-Version", "2022-11-28") | ||
|
|
||
| resp, err := opts.httpClient.Do(req) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
| defer resp.Body.Close() | ||
|
|
||
| if resp.StatusCode == http.StatusNotFound { | ||
| body, _ := io.ReadAll(resp.Body) | ||
| return nil, fmt.Errorf("usage data not available (HTTP 404). You may need the 'user' scope.\nRun: gh auth refresh -h github.com -s user\n\nResponse: %s", string(body)) | ||
| } | ||
|
marcelsafin marked this conversation as resolved.
|
||
|
|
||
| if resp.StatusCode != http.StatusOK { | ||
| body, _ := io.ReadAll(resp.Body) | ||
| return nil, fmt.Errorf("failed to fetch usage data: HTTP %d\n%s", resp.StatusCode, string(body)) | ||
| } | ||
|
|
||
| var data premiumRequestUsageResponse | ||
| if err := json.NewDecoder(resp.Body).Decode(&data); err != nil { | ||
| return nil, fmt.Errorf("failed to parse usage response: %w", err) | ||
| } | ||
|
|
||
| return &data, nil | ||
| } | ||
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.