feat(*): 模型协议 - 升级声明式协议、扩充官方插件并完善生成任务恢复
This commit is contained in:
1 parent
3f4a9ea3d0
commit
4724f0767b
132 files changed
+4576
-1420
No files matched your search
@@ -759,6 +759,111 @@ func (r *Repository) SettleBillingOrder(id string, providerRequestID string) err
|
||||
return err
|
||||
}
|
||||
|
||||
// RestoreRefundedBillingOrder compensates a billing order that was refunded
|
||||
// before an operator confirmed the upstream task had actually succeeded.
|
||||
//
|
||||
// The original reservation has already been returned to available credits, so
|
||||
// this transition charges the final amount directly from available credits.
|
||||
// The conditional refunded -> settled update keeps concurrent/repeated manual
|
||||
// recovery requests idempotent.
|
||||
func (r *Repository) RestoreRefundedBillingOrder(id string, providerRequestID string) error {
|
||||
return r.db.Transaction(func(tx *gorm.DB) error {
|
||||
var order model.BillingOrder
|
||||
if err := tx.First(&order, "id = ?", id).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
if order.Status == model.BillingStatusSettled {
|
||||
return nil
|
||||
}
|
||||
if order.Status != model.BillingStatusRefunded {
|
||||
return ErrBillingStateConflict
|
||||
}
|
||||
|
||||
actual := order.AmountMicrocredits
|
||||
var usage *BillingUsage
|
||||
if order.BillingMode == "token" {
|
||||
if zeroPricedTokenOrder(order) {
|
||||
actual = 0
|
||||
} else {
|
||||
var err error
|
||||
usage, err = billingUsage(tx, id)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
actual, err = tokenUsageAmount(order, usage)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
}
|
||||
if actual < 0 {
|
||||
return errors.New("invalid restored billing amount")
|
||||
}
|
||||
|
||||
updated := tx.Model(&model.CreditAccount{}).
|
||||
Where("user_id = ?", order.UserID).
|
||||
Updates(map[string]any{
|
||||
"available_microcredits": gorm.Expr("available_microcredits - ?", actual),
|
||||
"version": gorm.Expr("version + 1"),
|
||||
"updated_at": time.Now(),
|
||||
})
|
||||
if updated.Error != nil {
|
||||
return updated.Error
|
||||
}
|
||||
if updated.RowsAffected != 1 {
|
||||
return errors.New("credit account does not exist")
|
||||
}
|
||||
|
||||
var account model.CreditAccount
|
||||
if err := tx.First(&account, "user_id = ?", order.UserID).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
now := time.Now()
|
||||
orderUpdates := map[string]any{
|
||||
"status": model.BillingStatusSettled,
|
||||
"actual_amount_microcredits": actual,
|
||||
"refunded_amount_microcredits": max(order.AmountMicrocredits-actual, int64(0)),
|
||||
"refunded_at": nil,
|
||||
"settled_at": &now,
|
||||
"error": "",
|
||||
"updated_at": now,
|
||||
}
|
||||
if providerRequestID != "" {
|
||||
orderUpdates["provider_request_id"] = providerRequestID
|
||||
}
|
||||
if usage != nil {
|
||||
orderUpdates["input_tokens"] = usage.InputTokens
|
||||
orderUpdates["output_tokens"] = usage.OutputTokens
|
||||
orderUpdates["cached_tokens"] = usage.CachedTokens
|
||||
orderUpdates["usage_available"] = true
|
||||
}
|
||||
orderUpdate := tx.Model(&model.BillingOrder{}).
|
||||
Where("id = ? AND status = ?", order.ID, model.BillingStatusRefunded).
|
||||
Updates(orderUpdates)
|
||||
if orderUpdate.Error != nil {
|
||||
return orderUpdate.Error
|
||||
}
|
||||
if orderUpdate.RowsAffected != 1 {
|
||||
return ErrBillingStateConflict
|
||||
}
|
||||
|
||||
return tx.Create(&model.CreditLedgerEntry{
|
||||
ID: newRepositoryID(),
|
||||
UserID: order.UserID,
|
||||
Type: model.CreditLedgerConsume,
|
||||
AmountMicrocredits: -actual,
|
||||
AvailableDeltaMicrocredits: -actual,
|
||||
AvailableAfterMicrocredits: account.AvailableMicrocredits,
|
||||
ReservedAfterMicrocredits: account.ReservedMicrocredits,
|
||||
BillingOrderID: order.ID,
|
||||
Model: order.Model,
|
||||
ChannelID: order.ChannelID,
|
||||
Scene: order.Scene,
|
||||
Note: "人工查询确认上游成功,退款订单重新扣费",
|
||||
}).Error
|
||||
})
|
||||
}
|
||||
|
||||
func zeroPricedTokenOrder(order model.BillingOrder) bool {
|
||||
return order.BillingMode == "token" &&
|
||||
order.InputTokenPriceMicrocredits == 0 &&
|
||||
|
||||
@@ -0,0 +1,109 @@
|
||||
package repository
|
||||
|
||||
import (
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"infinite-canvas/backend/internal/model"
|
||||
|
||||
"gorm.io/driver/sqlite"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
func newRefundedBillingRecoveryRepository(t *testing.T) (*Repository, *gorm.DB) {
|
||||
t.Helper()
|
||||
db, err := gorm.Open(sqlite.Open("file:"+newRepositoryID()+"?mode=memory&cache=shared"), &gorm.Config{})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := db.AutoMigrate(&model.CreditAccount{}, &model.BillingOrder{}, &model.ApiCallLog{}, &model.CreditLedgerEntry{}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
return &Repository{db: db}, db
|
||||
}
|
||||
|
||||
func TestRestoreRefundedBillingOrderChargesFixedOrderOnce(t *testing.T) {
|
||||
repo, db := newRefundedBillingRecoveryRepository(t)
|
||||
now := time.Now()
|
||||
if err := db.Create(&model.CreditAccount{UserID: "user-1", AvailableMicrocredits: 9_000_000}).Error; err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := db.Create(&model.BillingOrder{
|
||||
ID: "order-1", UserID: "user-1", IdempotencyKey: "task:task-1", TaskID: "task-1",
|
||||
Model: "Wan 3.0", Capability: "video", BillingMode: "fixed", AmountMicrocredits: 3_000_000,
|
||||
ReservedAmountMicrocredits: 3_000_000, RefundedAmountMicrocredits: 3_000_000,
|
||||
Status: model.BillingStatusRefunded, RefundedAt: &now,
|
||||
}).Error; err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
for range 2 {
|
||||
if err := repo.RestoreRefundedBillingOrder("order-1", "provider-task-1"); err != nil {
|
||||
t.Fatalf("RestoreRefundedBillingOrder() error = %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
var order model.BillingOrder
|
||||
if err := db.First(&order, "id = ?", "order-1").Error; err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if order.Status != model.BillingStatusSettled || order.ActualAmountMicrocredits != 3_000_000 || order.RefundedAmountMicrocredits != 0 || order.RefundedAt != nil || order.ProviderRequestID != "provider-task-1" {
|
||||
t.Fatalf("restored order = %#v", order)
|
||||
}
|
||||
var account model.CreditAccount
|
||||
if err := db.First(&account, "user_id = ?", "user-1").Error; err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if account.AvailableMicrocredits != 6_000_000 || account.ReservedMicrocredits != 0 {
|
||||
t.Fatalf("restored account = %#v", account)
|
||||
}
|
||||
var consumeCount int64
|
||||
if err := db.Model(&model.CreditLedgerEntry{}).Where("billing_order_id = ? AND type = ?", "order-1", model.CreditLedgerConsume).Count(&consumeCount).Error; err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if consumeCount != 1 {
|
||||
t.Fatalf("consume ledger count = %d, want 1", consumeCount)
|
||||
}
|
||||
}
|
||||
|
||||
func TestRestoreRefundedBillingOrderUsesObservedTokenAmount(t *testing.T) {
|
||||
repo, db := newRefundedBillingRecoveryRepository(t)
|
||||
now := time.Now()
|
||||
if err := db.Create(&model.CreditAccount{UserID: "user-1", AvailableMicrocredits: 100_000}).Error; err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := db.Create(&model.BillingOrder{
|
||||
ID: "order-1", UserID: "user-1", IdempotencyKey: "task:task-1", TaskID: "task-1",
|
||||
Capability: "video", BillingMode: "token", AmountMicrocredits: 1_916_640,
|
||||
ReservedAmountMicrocredits: 1_916_640, RefundedAmountMicrocredits: 1_916_640,
|
||||
OutputTokenPriceMicrocredits: 16_000_000, MultiplierBasisPoints: 10_000,
|
||||
Status: model.BillingStatusRefunded, RefundedAt: &now,
|
||||
}).Error; err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := db.Create(&model.ApiCallLog{
|
||||
ID: "poll-log-1", BillingOrderID: "order-1", RequestKind: "poll", Billable: false,
|
||||
Status: model.ApiCallStatusSucceeded, UsageAvailable: true, OutputTokens: 108900,
|
||||
}).Error; err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
if err := repo.RestoreRefundedBillingOrder("order-1", "provider-task-1"); err != nil {
|
||||
t.Fatalf("RestoreRefundedBillingOrder() error = %v", err)
|
||||
}
|
||||
|
||||
var order model.BillingOrder
|
||||
if err := db.First(&order, "id = ?", "order-1").Error; err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if order.ActualAmountMicrocredits != 1_742_400 || order.RefundedAmountMicrocredits != 174_240 || order.OutputTokens != 108900 || !order.UsageAvailable {
|
||||
t.Fatalf("restored token order = %#v", order)
|
||||
}
|
||||
var account model.CreditAccount
|
||||
if err := db.First(&account, "user_id = ?", "user-1").Error; err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if account.AvailableMicrocredits != -1_642_400 {
|
||||
t.Fatalf("available balance = %d, want -1642400", account.AvailableMicrocredits)
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user