Skip to content

Open source AI coding agent. Designed for large projects and real world tasks.

'credits log' command and supporting functionality

Dane Schneider9441671ac955
diff --git a/app/cli/api/methods.go b/app/cli/api/methods.go
index 532717af..327e375f 100644
--- a/app/cli/api/methods.go
+++ b/app/cli/api/methods.go
@@ -2026,3 +2026,32 @@ func (a *Api) DeleteModelPack(setId string) *shared.ApiError {
return nil
}
+
+func (a *Api) GetCreditsTransactions() ([]*shared.CreditsTransaction, *shared.ApiError) {
+ serverUrl := fmt.Sprintf("%s/billing/credits_transactions", getApiHost())
+
+ resp, err := authenticatedFastClient.Get(serverUrl)
+ if err != nil {
+ return nil, &shared.ApiError{Type: shared.ApiErrorTypeOther, Msg: fmt.Sprintf("error sending request: %v", err)}
+ }
+ defer resp.Body.Close()
+
+ if resp.StatusCode >= 400 {
+ errorBody, _ := io.ReadAll(resp.Body)
+
+ apiErr := handleApiError(resp, errorBody)
+ tokenRefreshed, apiErr := refreshTokenIfNeeded(apiErr)
+ if tokenRefreshed {
+ return a.GetCreditsTransactions()
+ }
+ return nil, apiErr
+ }
+
+ var transactions []*shared.CreditsTransaction
+ err = json.NewDecoder(resp.Body).Decode(&transactions)
+ if err != nil {
+ return nil, &shared.ApiError{Type: shared.ApiErrorTypeOther, Msg: fmt.Sprintf("error decoding response: %v", err)}
+ }
+
+ return transactions, nil
+}
diff --git a/app/cli/cmd/credits_log.go b/app/cli/cmd/credits_log.go
new file mode 100644
index 00000000..2e5c4a83
--- /dev/null
+++ b/app/cli/cmd/credits_log.go
@@ -0,0 +1,117 @@
+package cmd
+
+import (
+ "fmt"
+ "plandex/api"
+ "plandex/auth"
+ "plandex/term"
+ "strings"
+
+ "github.com/fatih/color"
+ "github.com/olekukonko/tablewriter"
+ "github.com/plandex/plandex/shared"
+ "github.com/shopspring/decimal"
+ "github.com/spf13/cobra"
+)
+
+var creditsLogCmd = &cobra.Command{
+ Use: "log",
+ Short: "Display the credits transactions log",
+ Run: creditsLog,
+}
+
+func init() {
+ // subcommand of 'credits'
+ creditsCmd.AddCommand(creditsLogCmd)
+}
+
+func creditsLog(cmd *cobra.Command, args []string) {
+ auth.MustResolveAuthWithOrg()
+
+ term.StartSpinner("")
+ transactions, apiErr := api.Client.GetCreditsTransactions()
+ term.StopSpinner()
+
+ if apiErr != nil {
+ term.OutputErrorAndExit("Error getting credits transactions: %v", apiErr)
+ return
+ }
+
+ tableString := &strings.Builder{}
+ table := tablewriter.NewWriter(tableString)
+ table.SetAutoWrapText(false)
+ table.SetHeader([]string{"Amount", "Balance", "Transaction"})
+
+ for _, transaction := range transactions {
+ var sign string
+ var c color.Attribute
+
+ desc := transaction.CreatedAt.Local().Format("2006-01-02 15:04:05.000 PDT") + "\n"
+
+ if transaction.TransactionType == "debit" {
+ sign = "-"
+ c = term.ColorHiRed
+
+ var t string
+ if *transaction.DebitType == shared.DebitTypeModelInput {
+ t = "input"
+ } else if *transaction.DebitType == shared.DebitTypeModelOutput {
+ t = "output"
+ }
+
+ if transaction.DebitPlanName != nil {
+ desc += fmt.Sprintf("Plan → %s\n", *transaction.DebitPlanName)
+ }
+
+ surchargePct := transaction.DebitSurcharge.Div(*transaction.DebitBaseAmount)
+
+ price := transaction.DebitModelPricePerToken.Mul(decimal.NewFromInt(1000000)).Mul(surchargePct.Add(decimal.NewFromInt(1))).StringFixed(4)
+
+ for i := 0; i < 2; i++ {
+ if strings.HasSuffix(price, "0") {
+ price = price[:len(price)-1]
+ }
+ }
+
+ desc += fmt.Sprintf("⚡️ %s\n🧠 %s/%s → %s\n💳 Price → $%s per 1M 🪙\n💸 Used → %d 🪙\n", *transaction.DebitPurpose, string(*transaction.DebitModelProvider), *transaction.DebitModelName, t, price, *transaction.DebitTokens)
+
+ } else {
+ sign = "+"
+ c = term.ColorHiGreen
+
+ switch *transaction.CreditType {
+ case shared.CreditTypeGrant:
+ desc += "Monthly subscription payment"
+ case shared.CreditTypeTrial:
+ desc += "Started trial"
+ case shared.CreditTypePurchase:
+ desc += "Purchased credits"
+ }
+ }
+
+ amountStr := transaction.Amount.StringFixed(6)
+ for i := 0; i < 4; i++ {
+ if strings.HasSuffix(amountStr, "0") {
+ amountStr = amountStr[:len(amountStr)-1]
+ }
+ }
+
+ balanceStr := transaction.EndBalance.StringFixed(4)
+ for i := 0; i < 2; i++ {
+ if strings.HasSuffix(balanceStr, "0") {
+ balanceStr = balanceStr[:len(balanceStr)-1]
+ }
+ }
+
+ table.Append([]string{
+ color.New(c).Sprint(sign + "$" + amountStr),
+ "$" + balanceStr,
+
+ desc,
+ })
+ }
+
+ table.Render()
+
+ term.PageOutput(tableString.String())
+}
diff --git a/app/cli/term/help.go b/app/cli/term/help.go
index d4698545..105f7f2e 100644
--- a/app/cli/term/help.go
+++ b/app/cli/term/help.go
@@ -62,6 +62,8 @@ var CmdDesc = map[string][2]string{
"invite": {"", "invite a user to join your org"},
"revoke": {"", "revoke an invite or remove a user from your org"},
"users": {"", "list users and pending invites in your org"},
+ "credits": {"", "show Plandex Cloud credits balance"},
+ "credits log": {"", "show Plandex Cloud credits transaction log"},
}
func PrintCmds(prefix string, cmds ...string) {
diff --git a/app/cli/types/api.go b/app/cli/types/api.go
index f69368ee..e2f41811 100644
--- a/app/cli/types/api.go
+++ b/app/cli/types/api.go
@@ -97,4 +97,5 @@ type ApiClient interface {
CreateModelPack(set *shared.ModelPack) *shared.ApiError
ListModelPacks() ([]*shared.ModelPack, *shared.ApiError)
DeleteModelPack(setId string) *shared.ApiError
+ GetCreditsTransactions() ([]*shared.CreditsTransaction, *shared.ApiError)
}
diff --git a/app/shared/data_models.go b/app/shared/data_models.go
index 36340b0d..80154fdd 100644
--- a/app/shared/data_models.go
+++ b/app/shared/data_models.go
@@ -366,3 +366,63 @@ type CloudBillingFields struct {
BillingCycleStartedAt time.Time `json:"billingCycleStartedAt"`
TrialPaid bool `json:"trialPaid"`
}
+
+type CreditsTransactionType string
+
+const (
+ CreditsTransactionTypeCredit CreditsTransactionType = "credit"
+ CreditsTransactionTypeDebit CreditsTransactionType = "debit"
+)
+
+type CreditType string
+
+const (
+ CreditTypeTrial CreditType = "trial"
+ CreditTypeGrant CreditType = "grant"
+ CreditTypePurchase CreditType = "purchase"
+)
+
+type DebitType string
+
+const (
+ DebitTypeModelInput DebitType = "model_input"
+ DebitTypeModelOutput DebitType = "model_output"
+)
+
+type CreditsTransaction struct {
+ Id string `json:"id"`
+ OrgId string `json:"orgId"`
+ OrgName string `json:"orgName"`
+ UserId string `json:"userId"`
+ UserEmail string `json:"userEmail"`
+ UserName string `json:"userName"`
+ TransactionType CreditsTransactionType `json:"transactionType"`
+ Amount decimal.Decimal `json:"amount"`
+ StartBalance decimal.Decimal `json:"startBalance"`
+ EndBalance decimal.Decimal `json:"endBalance"`
+ CreditType *CreditType `json:"creditType,omitempty"`
+ CreditIsAutoRebuy bool `json:"creditIsAutoRebuy"`
+ CreditAutoRebuyMinThreshold *decimal.Decimal `json:"creditAutoRebuyMinThreshold,omitempty"`
+ CreditAutoRebuyToBalance *decimal.Decimal `json:"creditAutoRebuyToBalance,omitempty"`
+ CreditSubscriptionId *string `json:"creditSubscriptionId,omitempty"`
+ CreditSubscriptionPlanId *string `json:"creditSubscriptionPlanId,omitempty"`
+ CreditStripeCustomerId *string `json:"creditStripeCustomerId,omitempty"`
+ CreditStripeSubscriptionId *string `json:"creditStripeSubscriptionId,omitempty"`
+ CreditStripePriceId *string `json:"creditStripePriceId,omitempty"`
+ CreditStripePaymentId *string `json:"creditStripePaymentId,omitempty"`
+ DebitType *DebitType `json:"debitType,omitempty"`
+ DebitTokens *int `json:"debitTokens,omitempty"`
+ DebitBaseAmount *decimal.Decimal `json:"debitBaseAmount,omitempty"`
+ DebitSurcharge *decimal.Decimal `json:"debitSurcharge,omitempty"`
+ DebitModelProvider *ModelProvider `json:"debitModelProvider,omitempty"`
+ DebitModelName *string `json:"debitModelName,omitempty"`
+ DebitModelPackName *string `json:"debitModelPackName,omitempty"`
+ DebitModelRole *ModelRole `json:"debitModelRole,omitempty"`
+ DebitModelPricePerToken *decimal.Decimal `json:"debitModelPricePerToken,omitempty"`
+ DebitApiKeyHash *string `json:"debitApiKeyHash,omitempty"`
+ DebitPurpose *string `json:"debitPurpose,omitempty"`
+ DebitPlanId *string `json:"debitPlanId,omitempty"`
+ DebitPlanName *string `json:"debitPlanName,omitempty"`
+ DebitId *string `json:"debitId,omitempty"`
+ CreatedAt time.Time `json:"createdAt"`
+}