feat(server): meter AI point usage across tenant AI features
Adds an ai_point_usage_logs ledger and a reserve/refund/complete pipeline that charges AI points for article selection optimize, template analyze /title/outline, and KOL prompt generate/optimize. Pending reservations are reconciled when the kol-assist worker and template-assist tasks finish or fail, so points refund automatically on errors. Workspace now exposes the AI quota status and a paginated usage ledger. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -48,7 +48,7 @@ func NewArticleHandler(a *bootstrap.App) *ArticleHandler {
|
||||
a.DB,
|
||||
a.LLM,
|
||||
a.Config.LLM.MaxOutputTokens,
|
||||
),
|
||||
).WithCache(a.Cache),
|
||||
promptGenerateSvc: app.NewPromptRuleGenerationService(
|
||||
a.DB,
|
||||
a.LLM,
|
||||
@@ -344,7 +344,7 @@ func (h *ArticleHandler) OptimizeSelectionStream(c *gin.Context) {
|
||||
var streamed strings.Builder
|
||||
var streamWriteErr error
|
||||
|
||||
result, err := h.selectionOptimize.OptimizeSelection(streamCtx, req, func(delta string) {
|
||||
result, err := h.selectionOptimize.OptimizeSelection(streamCtx, id, req, func(delta string) {
|
||||
if delta == "" || streamWriteErr != nil {
|
||||
return
|
||||
}
|
||||
|
||||
@@ -42,7 +42,7 @@ func NewKolManageHandler(a *bootstrap.App, assets *AssetHandler) *KolManageHandl
|
||||
profileSvc: profileSvc,
|
||||
pkgSvc: app.NewKolPackageService(profileSvc, a.KolPackages, a.KolPrompts).WithCache(a.Cache),
|
||||
promptSvc: app.NewKolPromptService(a.DB, profileSvc, a.KolPackages, a.KolPrompts, a.KolSubscriptions, a.KolPromptAsset).WithCache(a.Cache),
|
||||
assistSvc: app.NewKolAssistService(profileSvc, a.KolAssists, a.LLM, a.RabbitMQ, a.Config.LLM, a.Logger),
|
||||
assistSvc: app.NewKolAssistService(a.DB, profileSvc, a.KolAssists, a.LLM, a.RabbitMQ, a.Config.LLM, a.Logger).WithCache(a.Cache),
|
||||
assets: assets,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -80,6 +80,7 @@ func RegisterRoutes(app *bootstrap.App) {
|
||||
workspace.GET("/overview", wsHandler.Overview)
|
||||
workspace.GET("/recent-articles", wsHandler.RecentArticles)
|
||||
workspace.GET("/quota-summary", wsHandler.QuotaSummary)
|
||||
workspace.GET("/ai-point-usage", wsHandler.AIPointUsageLogs)
|
||||
workspace.GET("/template-cards", wsHandler.TemplateCards)
|
||||
workspace.GET("/kol-cards", wsHandler.KolCards)
|
||||
|
||||
|
||||
@@ -22,6 +22,7 @@ func NewWorkspaceHandler(a *bootstrap.App) *WorkspaceHandler {
|
||||
a.Cache,
|
||||
),
|
||||
repository.NewQuotaRepository(a.DB),
|
||||
repository.NewAIPointUsageRepository(a.DB),
|
||||
).WithCache(a.Cache),
|
||||
}
|
||||
}
|
||||
@@ -53,6 +54,30 @@ func (h *WorkspaceHandler) QuotaSummary(c *gin.Context) {
|
||||
response.Success(c, data)
|
||||
}
|
||||
|
||||
func (h *WorkspaceHandler) AIPointUsageLogs(c *gin.Context) {
|
||||
page := parseIntQuery(c.Query("page"), 1)
|
||||
pageSize := parseIntQuery(c.Query("page_size"), 20)
|
||||
if c.Query("page") == "" && c.Query("page_size") == "" && c.Query("limit") != "" {
|
||||
limit := parseIntQuery(c.Query("limit"), 20)
|
||||
offset := parseIntQuery(c.Query("offset"), 0)
|
||||
if limit > 0 {
|
||||
pageSize = limit
|
||||
page = offset/limit + 1
|
||||
}
|
||||
}
|
||||
|
||||
data, err := h.svc.AIPointUsageLogs(
|
||||
c.Request.Context(),
|
||||
page,
|
||||
pageSize,
|
||||
)
|
||||
if err != nil {
|
||||
response.Error(c, err)
|
||||
return
|
||||
}
|
||||
response.Success(c, data)
|
||||
}
|
||||
|
||||
func (h *WorkspaceHandler) TemplateCards(c *gin.Context) {
|
||||
data, err := h.svc.TemplateCards(c.Request.Context())
|
||||
if err != nil {
|
||||
|
||||
Reference in New Issue
Block a user