Skip to content

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

updates for cloud credits handling

Dane Schneider7cf781973185
diff --git a/app/cli/cmd/credits_log.go b/app/cli/cmd/credits_log.go
index a126f6d6..7c3e2c61 100644
--- a/app/cli/cmd/credits_log.go
+++ b/app/cli/cmd/credits_log.go
@@ -64,26 +64,21 @@ func creditsLog(cmd *cobra.Command, args []string) {
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)
+ inputPrice := transaction.DebitModelInputPricePerToken.Mul(decimal.NewFromInt(1000000)).Mul(surchargePct.Add(decimal.NewFromInt(1))).StringFixed(4)
+ outputPrice := transaction.DebitModelOutputPricePerToken.Mul(decimal.NewFromInt(1000000)).Mul(surchargePct.Add(decimal.NewFromInt(1))).StringFixed(4)
for i := 0; i < 2; i++ {
- price = strings.TrimSuffix(price, "0")
+ inputPrice = strings.TrimSuffix(inputPrice, "0")
+ outputPrice = strings.TrimSuffix(outputPrice, "0")
}
- 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)
+ desc += fmt.Sprintf("⚡️ %s\n🧠 %s/%s\n💳 Price → $%s input / $%s output per 1M\n🪙 Used → %d input / %d output\n", *transaction.DebitPurpose, string(*transaction.DebitModelProvider), *transaction.DebitModelName, inputPrice, outputPrice, *transaction.DebitInputTokens, *transaction.DebitOutputTokens)
} else {
sign = "+"
diff --git a/app/server/hooks/hooks.go b/app/server/hooks/hooks.go
index bbcbe531..b32c7c85 100644
--- a/app/server/hooks/hooks.go
+++ b/app/server/hooks/hooks.go
@@ -31,14 +31,22 @@ type WillSendModelRequestParams struct {
}
type DidSendModelRequestParams struct {
- InputTokens int
- OutputTokens int
- ModelName string
- ModelProvider shared.ModelProvider
- ModelRole shared.ModelRole
- ModelPackName string
- Purpose string
- GenerationId string
+ InputTokens int
+ OutputTokens int
+ ModelName string
+ ModelProvider shared.ModelProvider
+ ModelRole shared.ModelRole
+ ModelPackName string
+ Purpose string
+ GenerationId string
+ PlanId string
+ ModelStreamId string
+ ConvoMessageId string
+ BuildId string
+ StoppedEarly bool
+ UserCancelled bool
+ HadError bool
+ NoReportedUsage bool
}
type CreateOrgHookRequestParams struct {
diff --git a/app/server/model/name.go b/app/server/model/name.go
index 9c1fd1ee..dfd12bee 100644
--- a/app/server/model/name.go
+++ b/app/server/model/name.go
@@ -111,6 +111,7 @@ func GenPlanName(
ModelRole: shared.ModelRolePlanSummary,
Purpose: "Generated plan name",
GenerationId: resp.ID,
+ PlanId: plan.Id,
},
})
@@ -241,6 +242,7 @@ func GenPipedDataName(
ModelRole: shared.ModelRolePlanSummary,
Purpose: "Generated name for data piped into context",
GenerationId: resp.ID,
+ PlanId: plan.Id,
},
})
@@ -371,6 +373,7 @@ func GenNoteName(
ModelRole: shared.ModelRolePlanSummary,
Purpose: "Generated name for note added to context",
GenerationId: resp.ID,
+ PlanId: plan.Id,
},
})
diff --git a/app/server/model/plan/build_structured_edits.go b/app/server/model/plan/build_structured_edits.go
index 12dcefa8..f5763747 100644
--- a/app/server/model/plan/build_structured_edits.go
+++ b/app/server/model/plan/build_structured_edits.go
@@ -183,14 +183,18 @@ func (fileState *activeBuildStreamFileState) buildStructuredEdits() {
Auth: auth,
Plan: fileState.plan,
DidSendModelRequestParams: &hooks.DidSendModelRequestParams{
- InputTokens: resp.Usage.PromptTokens,
- OutputTokens: resp.Usage.CompletionTokens,
- ModelName: config.BaseModelConfig.ModelName,
- ModelProvider: config.BaseModelConfig.Provider,
- ModelPackName: fileState.settings.ModelPack.Name,
- ModelRole: shared.ModelRoleBuilder,
- Purpose: "File edit (structured edits)",
- GenerationId: resp.ID,
+ InputTokens: resp.Usage.PromptTokens,
+ OutputTokens: resp.Usage.CompletionTokens,
+ ModelName: config.BaseModelConfig.ModelName,
+ ModelProvider: config.BaseModelConfig.Provider,
+ ModelPackName: fileState.settings.ModelPack.Name,
+ ModelRole: shared.ModelRoleBuilder,
+ Purpose: "File edit (structured edits)",
+ GenerationId: resp.ID,
+ PlanId: fileState.plan.Id,
+ ModelStreamId: fileState.tellState.activePlan.ModelStreamId,
+ ConvoMessageId: fileState.convoMessageId,
+ BuildId: fileState.build.Id,
},
})
diff --git a/app/server/model/plan/build_whole_file.go b/app/server/model/plan/build_whole_file.go
index e37694a3..9d5de69e 100644
--- a/app/server/model/plan/build_whole_file.go
+++ b/app/server/model/plan/build_whole_file.go
@@ -84,14 +84,18 @@ func (fileState *activeBuildStreamFileState) buildWholeFileFallback(proposedCont
Auth: auth,
Plan: fileState.plan,
DidSendModelRequestParams: &hooks.DidSendModelRequestParams{
- InputTokens: resp.Usage.PromptTokens,
- OutputTokens: resp.Usage.CompletionTokens,
- ModelName: config.BaseModelConfig.ModelName,
- ModelProvider: config.BaseModelConfig.Provider,
- ModelPackName: fileState.settings.ModelPack.Name,
- ModelRole: shared.ModelRoleBuilder,
- Purpose: "File edit (whole file)",
- GenerationId: resp.ID,
+ InputTokens: resp.Usage.PromptTokens,
+ OutputTokens: resp.Usage.CompletionTokens,
+ ModelName: config.BaseModelConfig.ModelName,
+ ModelProvider: config.BaseModelConfig.Provider,
+ ModelPackName: fileState.settings.ModelPack.Name,
+ ModelRole: shared.ModelRoleBuilder,
+ Purpose: "File edit (whole file)",
+ GenerationId: resp.ID,
+ PlanId: fileState.plan.Id,
+ ModelStreamId: fileState.tellState.activePlan.ModelStreamId,
+ ConvoMessageId: fileState.convoMessageId,
+ BuildId: fileState.build.Id,
},
})
diff --git a/app/server/model/plan/commit_msg.go b/app/server/model/plan/commit_msg.go
index bff747ed..3e238043 100644
--- a/app/server/model/plan/commit_msg.go
+++ b/app/server/model/plan/commit_msg.go
@@ -119,14 +119,17 @@ func (state *activeTellStreamState) genPlanDescription() (*db.ConvoMessageDescri
Auth: auth,
Plan: plan,
DidSendModelRequestParams: &hooks.DidSendModelRequestParams{
- InputTokens: inputTokens,
- OutputTokens: outputTokens,
- ModelName: config.BaseModelConfig.ModelName,
- ModelProvider: config.BaseModelConfig.Provider,
- ModelPackName: settings.ModelPack.Name,
- ModelRole: shared.ModelRoleCommitMsg,
- Purpose: "Generated commit message for suggested changes",
- GenerationId: descResp.ID,
+ InputTokens: inputTokens,
+ OutputTokens: outputTokens,
+ ModelName: config.BaseModelConfig.ModelName,
+ ModelProvider: config.BaseModelConfig.Provider,
+ ModelPackName: settings.ModelPack.Name,
+ ModelRole: shared.ModelRoleCommitMsg,
+ Purpose: "Generated commit message for suggested changes",
+ GenerationId: descResp.ID,
+ PlanId: planId,
+ ModelStreamId: activePlan.ModelStreamId,
+ ConvoMessageId: state.replyId,
},
})
diff --git a/app/server/model/plan/exec_status.go b/app/server/model/plan/exec_status.go
index 34c29d52..d3b004a5 100644
--- a/app/server/model/plan/exec_status.go
+++ b/app/server/model/plan/exec_status.go
@@ -129,14 +129,17 @@ func (state *activeTellStreamState) execStatusShouldContinue(message string, ctx
Auth: auth,
Plan: plan,
DidSendModelRequestParams: &hooks.DidSendModelRequestParams{
- InputTokens: inputTokens,
- OutputTokens: outputTokens,
- ModelName: config.BaseModelConfig.ModelName,
- ModelProvider: config.BaseModelConfig.Provider,
- ModelPackName: settings.ModelPack.Name,
- ModelRole: shared.ModelRolePlanSummary,
- Purpose: "Evaluate if plan should auto-continue",
- GenerationId: resp.ID,
+ InputTokens: inputTokens,
+ OutputTokens: outputTokens,
+ ModelName: config.BaseModelConfig.ModelName,
+ ModelProvider: config.BaseModelConfig.Provider,
+ ModelPackName: settings.ModelPack.Name,
+ ModelRole: shared.ModelRolePlanSummary,
+ Purpose: "Evaluate if plan should auto-continue",
+ GenerationId: resp.ID,
+ PlanId: plan.Id,
+ ModelStreamId: state.activePlan.ModelStreamId,
+ ConvoMessageId: state.replyId,
},
})
diff --git a/app/server/model/plan/tell_stream_usage.go b/app/server/model/plan/tell_stream_usage.go
index 7e00d21f..9dcac97f 100644
--- a/app/server/model/plan/tell_stream_usage.go
+++ b/app/server/model/plan/tell_stream_usage.go
@@ -22,14 +22,17 @@ func (state *activeTellStreamState) handleUsageChunk(usage *openai.Usage) {
Auth: auth,
Plan: plan,
DidSendModelRequestParams: &hooks.DidSendModelRequestParams{
- InputTokens: usage.PromptTokens,
- OutputTokens: usage.CompletionTokens,
- ModelName: state.settings.ModelPack.Planner.BaseModelConfig.ModelName,
- ModelProvider: state.settings.ModelPack.Planner.BaseModelConfig.Provider,
- ModelPackName: state.settings.ModelPack.Name,
- ModelRole: shared.ModelRolePlanner,
- Purpose: "Generated plan reply",
- GenerationId: generationId,
+ InputTokens: usage.PromptTokens,
+ OutputTokens: usage.CompletionTokens,
+ ModelName: state.settings.ModelPack.Planner.BaseModelConfig.ModelName,
+ ModelProvider: state.settings.ModelPack.Planner.BaseModelConfig.Provider,
+ ModelPackName: state.settings.ModelPack.Name,
+ ModelRole: shared.ModelRolePlanner,
+ Purpose: "Generated plan reply",
+ GenerationId: generationId,
+ PlanId: plan.Id,
+ ModelStreamId: state.activePlan.ModelStreamId,
+ ConvoMessageId: state.replyId,
},
})
@@ -60,14 +63,21 @@ func (state *activeTellStreamState) execHookOnStop(sendStreamErr bool) {
Auth: auth,
Plan: plan,
DidSendModelRequestParams: &hooks.DidSendModelRequestParams{
- InputTokens: state.totalRequestTokens,
- OutputTokens: active.NumTokens,
- ModelName: state.settings.ModelPack.Planner.BaseModelConfig.ModelName,
- ModelProvider: state.settings.ModelPack.Planner.BaseModelConfig.Provider,
- ModelPackName: state.settings.ModelPack.Name,
- ModelRole: shared.ModelRolePlanner,
- Purpose: "Generated plan reply (stopped)",
- GenerationId: generationId,
+ InputTokens: state.totalRequestTokens,
+ OutputTokens: active.NumTokens,
+ ModelName: state.settings.ModelPack.Planner.BaseModelConfig.ModelName,
+ ModelProvider: state.settings.ModelPack.Planner.BaseModelConfig.Provider,
+ ModelPackName: state.settings.ModelPack.Name,
+ ModelRole: shared.ModelRolePlanner,
+ Purpose: "Generated plan reply (stopped)",
+ GenerationId: generationId,
+ PlanId: plan.Id,
+ ModelStreamId: state.activePlan.ModelStreamId,
+ ConvoMessageId: state.replyId,
+ StoppedEarly: true,
+ UserCancelled: !sendStreamErr,
+ HadError: sendStreamErr,
+ NoReportedUsage: true,
},
})
diff --git a/app/server/model/plan/tell_summary.go b/app/server/model/plan/tell_summary.go
index 8a0813c0..6f0cff4c 100644
--- a/app/server/model/plan/tell_summary.go
+++ b/app/server/model/plan/tell_summary.go
@@ -325,6 +325,7 @@ func summarizeConvo(clients map[string]*openai.Client, config shared.ModelRoleCo
Auth: params.auth,
Plan: plan,
ModelPackName: params.modelPackName,
+ ActivePlan: active,
}, ctx)
if err != nil {
diff --git a/app/server/model/summarize.go b/app/server/model/summarize.go
index 8f50dc18..93197065 100644
--- a/app/server/model/summarize.go
+++ b/app/server/model/summarize.go
@@ -102,14 +102,18 @@ func PlanSummary(clients map[string]*openai.Client, config shared.ModelRoleConfi
Auth: params.Auth,
Plan: params.Plan,
DidSendModelRequestParams: &hooks.DidSendModelRequestParams{
- InputTokens: inputTokens,
- OutputTokens: outputTokens,
- ModelName: config.BaseModelConfig.ModelName,
- ModelProvider: config.BaseModelConfig.Provider,
- ModelPackName: params.ModelPackName,
- ModelRole: shared.ModelRolePlanSummary,
- Purpose: "Generated plan summary",
- GenerationId: resp.ID,
+ InputTokens: inputTokens,
+ OutputTokens: outputTokens,
+ ModelName: config.BaseModelConfig.ModelName,
+ ModelProvider: config.BaseModelConfig.Provider,
+ ModelPackName: params.ModelPackName,
+ ModelRole: shared.ModelRolePlanSummary,
+ Purpose: "Generated plan summary",
+ GenerationId: resp.ID,
+ PlanId: params.Plan.Id,
+ ModelStreamId: params.ActivePlan.ModelStreamId,
+ ConvoMessageId: params.LatestConvoMessageId,
+ BuildId: params.Plan.Id,
},
})
diff --git a/app/shared/data_models.go b/app/shared/data_models.go
index 9c8b45f1..d8268b35 100644
--- a/app/shared/data_models.go
+++ b/app/shared/data_models.go
@@ -458,48 +458,40 @@ const (
CreditTypeSwitch CreditType = "switch"
)
-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"`
- CreditStripeInvoiceId *string `json:"creditStripeInvoiceId,omitempty"`
- CreditStripePaymentMethod *string `json:"creditStripePaymentMethod,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"`
+ 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"`
+
+ DebitInputTokens *int `json:"debitInputTokens,omitempty"`
+ DebitOutputTokens *int `json:"debitOutputTokens,omitempty"`
+ DebitModelInputPricePerToken *decimal.Decimal `json:"debitModelInputPricePerToken,omitempty"`
+ DebitModelOutputPricePerToken *decimal.Decimal `json:"debitModelOutputPricePerToken,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"`
+
+ 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"`
}