@lastbrain/module-ai 2.0.26 → 2.0.30
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +52 -1
- package/dist/ai.build.config.d.ts.map +1 -1
- package/dist/ai.build.config.js +508 -9
- package/dist/api/admin/ai-provider-models/[id].d.ts +18 -0
- package/dist/api/admin/ai-provider-models/[id].d.ts.map +1 -0
- package/dist/api/admin/ai-provider-models/[id].js +58 -0
- package/dist/api/admin/ai-provider-models.d.ts +20 -0
- package/dist/api/admin/ai-provider-models.d.ts.map +1 -0
- package/dist/api/admin/ai-provider-models.js +26 -0
- package/dist/api/admin/ai-providers/[key].d.ts +18 -0
- package/dist/api/admin/ai-providers/[key].d.ts.map +1 -0
- package/dist/api/admin/ai-providers/[key].js +55 -0
- package/dist/api/admin/ai-providers.d.ts +20 -0
- package/dist/api/admin/ai-providers.d.ts.map +1 -0
- package/dist/api/admin/ai-providers.js +26 -0
- package/dist/api/admin/billing-analytics.d.ts +43 -0
- package/dist/api/admin/billing-analytics.d.ts.map +1 -0
- package/dist/api/admin/billing-analytics.js +144 -0
- package/dist/api/admin/global-ai-settings.d.ts +14 -0
- package/dist/api/admin/global-ai-settings.d.ts.map +1 -0
- package/dist/api/admin/global-ai-settings.js +63 -0
- package/dist/api/admin/token-packs/[id].d.ts.map +1 -1
- package/dist/api/admin/token-packs/[id].js +3 -2
- package/dist/api/admin/token-packs.d.ts.map +1 -1
- package/dist/api/admin/token-packs.js +3 -2
- package/dist/api/admin/user-monthly-details.d.ts +49 -0
- package/dist/api/admin/user-monthly-details.d.ts.map +1 -0
- package/dist/api/admin/user-monthly-details.js +140 -0
- package/dist/api/admin/user-quota.d.ts +21 -0
- package/dist/api/admin/user-quota.d.ts.map +1 -0
- package/dist/api/admin/user-quota.js +59 -0
- package/dist/api/admin/user-token/[id].d.ts.map +1 -1
- package/dist/api/admin/user-token/[id].js +2 -1
- package/dist/api/admin/user-token.d.ts +5 -2
- package/dist/api/admin/user-token.d.ts.map +1 -1
- package/dist/api/admin/user-token.js +91 -17
- package/dist/api/admin/user-usage-by-model.d.ts +22 -0
- package/dist/api/admin/user-usage-by-model.d.ts.map +1 -0
- package/dist/api/admin/user-usage-by-model.js +78 -0
- package/dist/api/admin/user-wallet-analytics.d.ts +15 -0
- package/dist/api/admin/user-wallet-analytics.d.ts.map +1 -0
- package/dist/api/admin/user-wallet-analytics.js +67 -0
- package/dist/api/admin/wallet-repair/route.d.ts +30 -0
- package/dist/api/admin/wallet-repair/route.d.ts.map +1 -0
- package/dist/api/admin/wallet-repair/route.js +63 -0
- package/dist/api/auth/ai-model-settings.d.ts +21 -0
- package/dist/api/auth/ai-model-settings.d.ts.map +1 -0
- package/dist/api/auth/ai-model-settings.js +86 -0
- package/dist/api/auth/ai-settings.d.ts +17 -0
- package/dist/api/auth/ai-settings.d.ts.map +1 -0
- package/dist/api/auth/ai-settings.js +87 -0
- package/dist/api/auth/api-keys/[id].d.ts +17 -0
- package/dist/api/auth/api-keys/[id].d.ts.map +1 -0
- package/dist/api/auth/api-keys/[id].js +66 -0
- package/dist/api/auth/api-keys.d.ts +19 -0
- package/dist/api/auth/api-keys.d.ts.map +1 -0
- package/dist/api/auth/api-keys.js +94 -0
- package/dist/api/auth/create-checkout.d.ts +1 -1
- package/dist/api/auth/create-checkout.d.ts.map +1 -1
- package/dist/api/auth/create-checkout.js +8 -6
- package/dist/api/auth/generate-image.d.ts +2 -2
- package/dist/api/auth/generate-image.d.ts.map +1 -1
- package/dist/api/auth/generate-image.js +404 -104
- package/dist/api/auth/generate-text.d.ts +3 -2
- package/dist/api/auth/generate-text.d.ts.map +1 -1
- package/dist/api/auth/generate-text.js +130 -58
- package/dist/api/auth/process-ocr.d.ts +9 -0
- package/dist/api/auth/process-ocr.d.ts.map +1 -0
- package/dist/api/auth/process-ocr.js +43 -0
- package/dist/api/auth/prompts/stats.d.ts +14 -0
- package/dist/api/auth/prompts/stats.d.ts.map +1 -0
- package/dist/api/auth/prompts/stats.js +46 -0
- package/dist/api/auth/prompts.d.ts +26 -0
- package/dist/api/auth/prompts.d.ts.map +1 -0
- package/dist/api/auth/prompts.js +175 -0
- package/dist/api/auth/token-balance.d.ts +26 -0
- package/dist/api/auth/token-balance.d.ts.map +1 -0
- package/dist/api/auth/token-balance.js +47 -0
- package/dist/api/auth/token-checkout.d.ts.map +1 -1
- package/dist/api/auth/token-checkout.js +22 -4
- package/dist/api/auth/token-packs.d.ts.map +1 -1
- package/dist/api/auth/token-packs.js +2 -1
- package/dist/api/auth/usage-by-model.d.ts +25 -0
- package/dist/api/auth/usage-by-model.d.ts.map +1 -0
- package/dist/api/auth/usage-by-model.js +95 -0
- package/dist/api/auth/usage.d.ts +26 -0
- package/dist/api/auth/usage.d.ts.map +1 -0
- package/dist/api/auth/usage.js +127 -0
- package/dist/api/auth/user-tokens.d.ts.map +1 -1
- package/dist/api/auth/user-tokens.js +36 -2
- package/dist/api/auth/wallet/route.d.ts +17 -0
- package/dist/api/auth/wallet/route.d.ts.map +1 -0
- package/dist/api/auth/wallet/route.js +68 -0
- package/dist/api/auth/wallet.d.ts +16 -0
- package/dist/api/auth/wallet.d.ts.map +1 -0
- package/dist/api/auth/wallet.js +71 -0
- package/dist/api/public/gateway-models.d.ts +25 -0
- package/dist/api/public/gateway-models.d.ts.map +1 -0
- package/dist/api/public/gateway-models.js +49 -0
- package/dist/api/public/pricing-summary.d.ts +46 -0
- package/dist/api/public/pricing-summary.d.ts.map +1 -0
- package/dist/api/public/pricing-summary.js +70 -0
- package/dist/api/public/prompts/[id]/stats.d.ts +15 -0
- package/dist/api/public/prompts/[id]/stats.d.ts.map +1 -0
- package/dist/api/public/prompts/[id]/stats.js +37 -0
- package/dist/api/public/prompts/[id]/view.d.ts +15 -0
- package/dist/api/public/prompts/[id]/view.d.ts.map +1 -0
- package/dist/api/public/prompts/[id]/view.js +41 -0
- package/dist/api/public/prompts/slug/[slug].d.ts +15 -0
- package/dist/api/public/prompts/slug/[slug].d.ts.map +1 -0
- package/dist/api/public/prompts/slug/[slug].js +40 -0
- package/dist/api/public/prompts/user/[username].d.ts +17 -0
- package/dist/api/public/prompts/user/[username].d.ts.map +1 -0
- package/dist/api/public/prompts/user/[username].js +51 -0
- package/dist/api/public/prompts.d.ts +15 -0
- package/dist/api/public/prompts.d.ts.map +1 -0
- package/dist/api/public/prompts.js +45 -0
- package/dist/api/public/token-packs.d.ts +11 -0
- package/dist/api/public/token-packs.d.ts.map +1 -0
- package/dist/api/public/token-packs.js +25 -0
- package/dist/api/public/token-pricing.d.ts +44 -0
- package/dist/api/public/token-pricing.d.ts.map +1 -0
- package/dist/api/public/token-pricing.js +168 -0
- package/dist/api/public/v1/_lib/artifacts.d.ts +52 -0
- package/dist/api/public/v1/_lib/artifacts.d.ts.map +1 -0
- package/dist/api/public/v1/_lib/artifacts.js +217 -0
- package/dist/api/public/v1/_lib/auth.d.ts +43 -0
- package/dist/api/public/v1/_lib/auth.d.ts.map +1 -0
- package/dist/api/public/v1/_lib/auth.js +108 -0
- package/dist/api/public/v1/_lib/errors.d.ts +17 -0
- package/dist/api/public/v1/_lib/errors.d.ts.map +1 -0
- package/dist/api/public/v1/_lib/errors.js +16 -0
- package/dist/api/public/v1/_lib/log.d.ts +29 -0
- package/dist/api/public/v1/_lib/log.d.ts.map +1 -0
- package/dist/api/public/v1/_lib/log.js +68 -0
- package/dist/api/public/v1/_lib/quota.d.ts +24 -0
- package/dist/api/public/v1/_lib/quota.d.ts.map +1 -0
- package/dist/api/public/v1/_lib/quota.js +118 -0
- package/dist/api/public/v1/_lib/router.d.ts +54 -0
- package/dist/api/public/v1/_lib/router.d.ts.map +1 -0
- package/dist/api/public/v1/_lib/router.js +119 -0
- package/dist/api/public/v1/connect.d.ts +20 -0
- package/dist/api/public/v1/connect.d.ts.map +1 -0
- package/dist/api/public/v1/connect.js +119 -0
- package/dist/api/public/v1/doc.d.ts +239 -0
- package/dist/api/public/v1/doc.d.ts.map +1 -0
- package/dist/api/public/v1/doc.js +253 -0
- package/dist/api/public/v1/history/[id].d.ts +92 -0
- package/dist/api/public/v1/history/[id].d.ts.map +1 -0
- package/dist/api/public/v1/history/[id].js +176 -0
- package/dist/api/public/v1/history.d.ts +30 -0
- package/dist/api/public/v1/history.d.ts.map +1 -0
- package/dist/api/public/v1/history.js +142 -0
- package/dist/api/public/v1/image-ai.d.ts +24 -0
- package/dist/api/public/v1/image-ai.d.ts.map +1 -0
- package/dist/api/public/v1/image-ai.js +233 -0
- package/dist/api/public/v1/prompts.d.ts +19 -0
- package/dist/api/public/v1/prompts.d.ts.map +1 -0
- package/dist/api/public/v1/prompts.js +107 -0
- package/dist/api/public/v1/provider.d.ts +16 -0
- package/dist/api/public/v1/provider.d.ts.map +1 -0
- package/dist/api/public/v1/provider.js +130 -0
- package/dist/api/public/v1/purchase.d.ts +11 -0
- package/dist/api/public/v1/purchase.d.ts.map +1 -0
- package/dist/api/public/v1/purchase.js +18 -0
- package/dist/api/public/v1/status.d.ts +35 -0
- package/dist/api/public/v1/status.d.ts.map +1 -0
- package/dist/api/public/v1/status.js +163 -0
- package/dist/api/public/v1/text-ai.d.ts +26 -0
- package/dist/api/public/v1/text-ai.d.ts.map +1 -0
- package/dist/api/public/v1/text-ai.js +239 -0
- package/dist/api/public/webhook.d.ts.map +1 -1
- package/dist/api/public/webhook.js +50 -39
- package/dist/api/track-usage.d.ts +12 -0
- package/dist/api/track-usage.d.ts.map +1 -0
- package/dist/api/track-usage.js +37 -0
- package/dist/components/Doc.d.ts.map +1 -1
- package/dist/components/Doc.js +1 -1
- package/dist/components/DocUsageCustom.js +6 -6
- package/dist/components/admin/UserTokenTab.d.ts.map +1 -1
- package/dist/components/admin/UserTokenTab.js +170 -23
- package/dist/components/auth/AuthDashboardAi.d.ts +2 -0
- package/dist/components/auth/AuthDashboardAi.d.ts.map +1 -0
- package/dist/components/auth/AuthDashboardAi.js +53 -0
- package/dist/docs/REFACTORING_BILLING_GUIDE.d.ts +277 -0
- package/dist/docs/REFACTORING_BILLING_GUIDE.d.ts.map +1 -0
- package/dist/docs/REFACTORING_BILLING_GUIDE.js +276 -0
- package/dist/index.d.ts +15 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +18 -1
- package/dist/scripts/migrate-tokens-to-wallet.d.ts +13 -0
- package/dist/scripts/migrate-tokens-to-wallet.d.ts.map +1 -0
- package/dist/scripts/migrate-tokens-to-wallet.js +165 -0
- package/dist/server/__tests__/billing.test.d.ts +5 -0
- package/dist/server/__tests__/billing.test.d.ts.map +1 -0
- package/dist/server/__tests__/billing.test.js +223 -0
- package/dist/server/ai-client.d.ts +59 -0
- package/dist/server/ai-client.d.ts.map +1 -0
- package/dist/server/ai-client.js +111 -0
- package/dist/server/ai-generation-service.d.ts +66 -0
- package/dist/server/ai-generation-service.d.ts.map +1 -0
- package/dist/server/ai-generation-service.js +274 -0
- package/dist/server/billing.d.ts +200 -0
- package/dist/server/billing.d.ts.map +1 -0
- package/dist/server/billing.js +488 -0
- package/dist/server/gateway-service.d.ts +13 -0
- package/dist/server/gateway-service.d.ts.map +1 -0
- package/dist/server/gateway-service.js +161 -0
- package/dist/server/global-settings.d.ts +16 -0
- package/dist/server/global-settings.d.ts.map +1 -0
- package/dist/server/global-settings.js +42 -0
- package/dist/server/model-filter.d.ts +25 -0
- package/dist/server/model-filter.d.ts.map +1 -0
- package/dist/server/model-filter.js +240 -0
- package/dist/server/ocr.d.ts +39 -0
- package/dist/server/ocr.d.ts.map +1 -0
- package/dist/server/ocr.js +280 -0
- package/dist/server/openai-client.d.ts +19 -0
- package/dist/server/openai-client.d.ts.map +1 -0
- package/dist/server/openai-client.js +26 -0
- package/dist/server/pricing-config.d.ts +18 -0
- package/dist/server/pricing-config.d.ts.map +1 -0
- package/dist/server/pricing-config.js +94 -0
- package/dist/server/pricing-validator.d.ts +41 -0
- package/dist/server/pricing-validator.d.ts.map +1 -0
- package/dist/server/pricing-validator.js +113 -0
- package/dist/server/pricing.d.ts +121 -0
- package/dist/server/pricing.d.ts.map +1 -0
- package/dist/server/pricing.js +225 -0
- package/dist/server/quota.d.ts +66 -0
- package/dist/server/quota.d.ts.map +1 -0
- package/dist/server/quota.js +538 -0
- package/dist/server/wallet-repair.d.ts +32 -0
- package/dist/server/wallet-repair.d.ts.map +1 -0
- package/dist/server/wallet-repair.js +189 -0
- package/dist/server.d.ts +13 -1
- package/dist/server.d.ts.map +1 -1
- package/dist/server.js +87 -16
- package/dist/sitemap/handlers/prompts.d.ts +6 -0
- package/dist/sitemap/handlers/prompts.d.ts.map +1 -0
- package/dist/sitemap/handlers/prompts.js +72 -0
- package/dist/sitemap/handlers/users.d.ts +6 -0
- package/dist/sitemap/handlers/users.d.ts.map +1 -0
- package/dist/sitemap/handlers/users.js +80 -0
- package/dist/sitemap/manifest.d.ts +8 -0
- package/dist/sitemap/manifest.d.ts.map +1 -0
- package/dist/sitemap/manifest.js +27 -0
- package/dist/types/gateway.d.ts +40 -0
- package/dist/types/gateway.d.ts.map +1 -0
- package/dist/types/gateway.js +4 -0
- package/dist/types/quota.d.ts +74 -0
- package/dist/types/quota.d.ts.map +1 -0
- package/dist/types/quota.js +11 -0
- package/dist/utils/date.d.ts +7 -0
- package/dist/utils/date.d.ts.map +1 -0
- package/dist/utils/date.js +17 -0
- package/dist/web/admin/AdminTokenPacksPage.js +1 -1
- package/dist/web/admin/BillingAnalyticsPage.d.ts +2 -0
- package/dist/web/admin/BillingAnalyticsPage.d.ts.map +1 -0
- package/dist/web/admin/BillingAnalyticsPage.js +141 -0
- package/dist/web/admin/GlobalAISettingsPage.d.ts +2 -0
- package/dist/web/admin/GlobalAISettingsPage.d.ts.map +1 -0
- package/dist/web/admin/GlobalAISettingsPage.js +93 -0
- package/dist/web/admin/UserTokenPage.d.ts.map +1 -1
- package/dist/web/admin/UserTokenPage.js +20 -7
- package/dist/web/auth/AISettingsPage.d.ts +2 -0
- package/dist/web/auth/AISettingsPage.d.ts.map +1 -0
- package/dist/web/auth/AISettingsPage.js +258 -0
- package/dist/web/auth/APIKeysPage.d.ts +2 -0
- package/dist/web/auth/APIKeysPage.d.ts.map +1 -0
- package/dist/web/auth/APIKeysPage.js +154 -0
- package/dist/web/auth/HistoryPage.d.ts +2 -0
- package/dist/web/auth/HistoryPage.d.ts.map +1 -0
- package/dist/web/auth/HistoryPage.js +279 -0
- package/dist/web/auth/PromptsPage.d.ts +5 -0
- package/dist/web/auth/PromptsPage.d.ts.map +1 -0
- package/dist/web/auth/PromptsPage.js +137 -0
- package/dist/web/auth/TokenPage.d.ts.map +1 -1
- package/dist/web/auth/TokenPage.js +88 -31
- package/dist/web/auth/UsageAndTokensPage.d.ts +2 -0
- package/dist/web/auth/UsageAndTokensPage.d.ts.map +1 -0
- package/dist/web/auth/UsageAndTokensPage.js +157 -0
- package/dist/web/auth/UsagePage.d.ts +2 -0
- package/dist/web/auth/UsagePage.d.ts.map +1 -0
- package/dist/web/auth/UsagePage.js +62 -0
- package/dist/web/auth/components/ApiKeyFilterSelect.d.ts +13 -0
- package/dist/web/auth/components/ApiKeyFilterSelect.d.ts.map +1 -0
- package/dist/web/auth/components/ApiKeyFilterSelect.js +16 -0
- package/dist/web/auth/components/ModelUsageTable.d.ts +19 -0
- package/dist/web/auth/components/ModelUsageTable.d.ts.map +1 -0
- package/dist/web/auth/components/ModelUsageTable.js +37 -0
- package/dist/web/auth/components/PurchaseButton.d.ts +7 -0
- package/dist/web/auth/components/PurchaseButton.d.ts.map +1 -0
- package/dist/web/auth/components/PurchaseButton.js +13 -0
- package/dist/web/auth/components/TokenHistoryCard.d.ts +20 -0
- package/dist/web/auth/components/TokenHistoryCard.d.ts.map +1 -0
- package/dist/web/auth/components/TokenHistoryCard.js +76 -0
- package/dist/web/auth/components/TokenKpiGrid.d.ts +24 -0
- package/dist/web/auth/components/TokenKpiGrid.d.ts.map +1 -0
- package/dist/web/auth/components/TokenKpiGrid.js +38 -0
- package/dist/web/auth/components/UsageByDayChart.d.ts +11 -0
- package/dist/web/auth/components/UsageByDayChart.d.ts.map +1 -0
- package/dist/web/auth/components/UsageByDayChart.js +32 -0
- package/dist/web/auth/components/UsageByModelBarChart.d.ts +12 -0
- package/dist/web/auth/components/UsageByModelBarChart.d.ts.map +1 -0
- package/dist/web/auth/components/UsageByModelBarChart.js +32 -0
- package/dist/web/auth/components/WalletStatusCard.d.ts +9 -0
- package/dist/web/auth/components/WalletStatusCard.d.ts.map +1 -0
- package/dist/web/auth/components/WalletStatusCard.js +50 -0
- package/dist/web/components/ImageGenerative.d.ts +3 -1
- package/dist/web/components/ImageGenerative.d.ts.map +1 -1
- package/dist/web/components/ImageGenerative.js +139 -52
- package/dist/web/components/TextareaGenerative.d.ts +3 -1
- package/dist/web/components/TextareaGenerative.d.ts.map +1 -1
- package/dist/web/components/TextareaGenerative.js +10 -5
- package/dist/web/public/PromptDetailPage.d.ts +25 -0
- package/dist/web/public/PromptDetailPage.d.ts.map +1 -0
- package/dist/web/public/PromptDetailPage.js +71 -0
- package/dist/web/public/PromptDetailPageServer.d.ts +15 -0
- package/dist/web/public/PromptDetailPageServer.d.ts.map +1 -0
- package/dist/web/public/PromptDetailPageServer.js +68 -0
- package/dist/web/public/PublicPromptsPage.d.ts +5 -0
- package/dist/web/public/PublicPromptsPage.d.ts.map +1 -0
- package/dist/web/public/PublicPromptsPage.js +110 -0
- package/dist/web/public/PurchaseTokensPage.d.ts +2 -0
- package/dist/web/public/PurchaseTokensPage.d.ts.map +1 -0
- package/dist/web/public/PurchaseTokensPage.js +98 -0
- package/dist/web/public/UserAvatar.d.ts +13 -0
- package/dist/web/public/UserAvatar.d.ts.map +1 -0
- package/dist/web/public/UserAvatar.js +13 -0
- package/dist/web/public/UserDetailPageServer.d.ts +15 -0
- package/dist/web/public/UserDetailPageServer.d.ts.map +1 -0
- package/dist/web/public/UserDetailPageServer.js +31 -0
- package/dist/web/public/UserPromptsPage.d.ts +9 -0
- package/dist/web/public/UserPromptsPage.d.ts.map +1 -0
- package/dist/web/public/UserPromptsPage.js +112 -0
- package/dist/web/public/UserPromptsPageServer.d.ts +15 -0
- package/dist/web/public/UserPromptsPageServer.d.ts.map +1 -0
- package/dist/web/public/UserPromptsPageServer.js +31 -0
- package/package.json +18 -9
- package/supabase/migrations/20251125000000_ai_tokens.sql +7 -0
- package/supabase/migrations/20260123100002_user_token_quota_monthly copy.sql +173 -0
- package/supabase/migrations/20260128100003_update_and_add_table.sql +368 -0
- package/supabase/migrations/20260128120000_seed_providers_models.sql +78 -0
- package/supabase/migrations/20260128131405_add_api_key_id_to_ledgers.sql +41 -0
- package/supabase/migrations/20260128140000_ai_artifacts_storage.sql +99 -0
- package/supabase/migrations/20260128140002_ai_user_settings.sql +57 -0
- package/supabase/migrations/20260128150000_drop_ai_user_settings.sql +21 -0
- package/supabase/migrations/20260128160000_wallet_billing_system.sql +192 -0
- package/supabase/migrations/20260128160001_wallet_rpc_functions.sql +165 -0
- package/supabase/migrations/20260128170000_add_pack_coef_to_token_packs.sql +30 -0
- package/supabase/migrations/20260129120000_wallet_view_rpc.sql +41 -0
- package/supabase/migrations/20260129220003_update_pack_margins.sql +31 -0
- package/supabase/migrations/20260129330004_ai_user_prompts.sql +151 -0
- package/supabase/migrations/20260129330005_ai_prompts_ip_tracking.sql +92 -0
- package/supabase/migrations/20260129330006_ai_prompts_slug.sql +64 -0
- package/supabase/migrations/20260129330007_ai_prompts_view_slug.sql +26 -0
- package/supabase/migrations/20260129440000_ai_prompts_view_username.sql +33 -0
- package/supabase/migrations/20260129450000_ai_prompts_add_lang.sql +40 -0
- package/supabase/migrations/20260131000000_extract_model_prompt_in_ledger.sql +92 -0
- package/supabase/migrations/20260131140000_fix_duplicate_purchases.sql +64 -0
- package/supabase/migrations/20260201120000_module-ai_default_models.sql +63 -0
- package/supabase/migrations/20260201130000_module-ai_remove_provider_tables.sql +17 -0
- package/supabase/migrations-down/20251217120000_user_token_quota_monthly.sql +34 -0
- package/supabase/migrations-down/20260128131405_add_api_key_id_to_ledgers.sql +25 -0
|
@@ -0,0 +1,274 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Centralized AI Generation Service
|
|
3
|
+
*
|
|
4
|
+
* SINGLE SOURCE OF TRUTH for all AI generation with billing
|
|
5
|
+
* Used by both internal auth endpoints AND public API v1
|
|
6
|
+
*
|
|
7
|
+
* This service handles:
|
|
8
|
+
* 1. Pricing validation
|
|
9
|
+
* 2. Wallet pre-check (> 0)
|
|
10
|
+
* 3. AI API call
|
|
11
|
+
* 4. Real cost computation with minimums
|
|
12
|
+
* 5. Wallet debit
|
|
13
|
+
* 6. Ledger recording
|
|
14
|
+
*/
|
|
15
|
+
import { logger } from "@lastbrain/core";
|
|
16
|
+
import { getSupabaseServiceClient } from "@lastbrain/core/server";
|
|
17
|
+
import { textAI, imageAI } from "./ai-client";
|
|
18
|
+
import { isModelEnabled } from "./model-filter";
|
|
19
|
+
import { validateTextPricing, validateImagePricing } from "./pricing-validator";
|
|
20
|
+
import { computeAndDebitAIUsage, computeProviderCostUsd, getWalletState, } from "./billing";
|
|
21
|
+
// ============================================================================
|
|
22
|
+
// TEXT GENERATION
|
|
23
|
+
// ============================================================================
|
|
24
|
+
/**
|
|
25
|
+
* Generate text with automatic billing
|
|
26
|
+
*
|
|
27
|
+
* @param userId User ID
|
|
28
|
+
* @param params Generation parameters
|
|
29
|
+
* @returns Generation result with billing info
|
|
30
|
+
*/
|
|
31
|
+
export async function generateTextWithBilling(userId, params) {
|
|
32
|
+
try {
|
|
33
|
+
const { prompt, model = "gpt-4o-mini", systemPrompt, maxTokens = 3000, temperature = 0.7, actionType = "generate-text", context, promptUser, metadata = {}, } = params;
|
|
34
|
+
// 1. Build full model string
|
|
35
|
+
const fullModel = model.includes("/") ? model : `openai/${model}`;
|
|
36
|
+
const [provider, modelName] = fullModel.split("/", 2);
|
|
37
|
+
logger.info(`[AiGenService] Generating text with ${fullModel} for user ${userId}`);
|
|
38
|
+
// 2. Check if model is enabled
|
|
39
|
+
const filterResult = await isModelEnabled(userId, fullModel);
|
|
40
|
+
if (!filterResult.allowed) {
|
|
41
|
+
logger.warn(`[AiGenService] Model ${fullModel} not allowed for user ${userId}`);
|
|
42
|
+
return {
|
|
43
|
+
success: false,
|
|
44
|
+
error: filterResult.error || "Modèle non autorisé",
|
|
45
|
+
errorCode: "MODEL_NOT_ALLOWED",
|
|
46
|
+
};
|
|
47
|
+
}
|
|
48
|
+
// 3. Validate pricing
|
|
49
|
+
const pricingValidation = validateTextPricing(filterResult.model?.pricing, fullModel);
|
|
50
|
+
if (!pricingValidation.valid) {
|
|
51
|
+
logger.error(`[AiGenService] Invalid pricing for ${fullModel}`);
|
|
52
|
+
return {
|
|
53
|
+
success: false,
|
|
54
|
+
error: pricingValidation.error || "Pricing non disponible",
|
|
55
|
+
errorCode: "NO_PRICING",
|
|
56
|
+
};
|
|
57
|
+
}
|
|
58
|
+
const pricing = pricingValidation.pricing;
|
|
59
|
+
// 4. Check wallet (simple check: must have credits > 0)
|
|
60
|
+
const wallet = await getWalletState(userId);
|
|
61
|
+
if (!wallet.walletProviderBudgetUsd ||
|
|
62
|
+
wallet.walletProviderBudgetUsd <= 0) {
|
|
63
|
+
logger.warn(`[AiGenService] No credits for user ${userId}: $${wallet.walletProviderBudgetUsd}`);
|
|
64
|
+
return {
|
|
65
|
+
success: false,
|
|
66
|
+
error: "Crédits insuffisants pour cette opération",
|
|
67
|
+
errorCode: "NO_CREDITS",
|
|
68
|
+
};
|
|
69
|
+
}
|
|
70
|
+
logger.info(`[AiGenService] Pre-check passed: wallet=$${wallet.walletProviderBudgetUsd.toFixed(6)}`);
|
|
71
|
+
// 5. Call AI API
|
|
72
|
+
const result = await textAI({
|
|
73
|
+
prompt,
|
|
74
|
+
model: fullModel,
|
|
75
|
+
system: systemPrompt ||
|
|
76
|
+
"Tu es un assistant IA qui aide à générer du texte de qualité.",
|
|
77
|
+
maxTokens,
|
|
78
|
+
temperature,
|
|
79
|
+
});
|
|
80
|
+
const inputTokens = result.usage?.promptTokens || 0;
|
|
81
|
+
const outputTokens = result.usage?.completionTokens || 0;
|
|
82
|
+
// 6. Compute real cost
|
|
83
|
+
const usage = { inputTokens, outputTokens };
|
|
84
|
+
const providerCostUsd = computeProviderCostUsd(usage, pricing);
|
|
85
|
+
// 7. Debit wallet with centralized billing logic
|
|
86
|
+
const billingResult = computeAndDebitAIUsage({
|
|
87
|
+
userId,
|
|
88
|
+
actionType,
|
|
89
|
+
providerCostUsd,
|
|
90
|
+
wallet,
|
|
91
|
+
metadata: {
|
|
92
|
+
model: modelName,
|
|
93
|
+
provider,
|
|
94
|
+
endpoint: "text",
|
|
95
|
+
inputTokens,
|
|
96
|
+
outputTokens,
|
|
97
|
+
prompt: prompt.substring(0, 100),
|
|
98
|
+
...metadata,
|
|
99
|
+
},
|
|
100
|
+
});
|
|
101
|
+
if (!billingResult.success) {
|
|
102
|
+
logger.error(`[AiGenService] Billing failed: ${billingResult.error}`);
|
|
103
|
+
return {
|
|
104
|
+
success: false,
|
|
105
|
+
error: billingResult.error || "Erreur de facturation",
|
|
106
|
+
errorCode: "NO_CREDITS",
|
|
107
|
+
};
|
|
108
|
+
}
|
|
109
|
+
// 8. Record in ledger
|
|
110
|
+
const supabase = await getSupabaseServiceClient();
|
|
111
|
+
const { error: ledgerError } = await supabase.rpc("record_token_consumption", {
|
|
112
|
+
p_user_id: userId,
|
|
113
|
+
p_debit_tokens: 0, // Calculated by wallet system
|
|
114
|
+
p_provider_cost_usd: billingResult.providerCostUsd || 0,
|
|
115
|
+
p_sell_usd: billingResult.sellCostUsd || 0,
|
|
116
|
+
p_margin_usd: billingResult.marginUsd || 0,
|
|
117
|
+
p_meta: {
|
|
118
|
+
model: modelName,
|
|
119
|
+
provider,
|
|
120
|
+
endpoint: "text",
|
|
121
|
+
inputTokens,
|
|
122
|
+
outputTokens,
|
|
123
|
+
...metadata,
|
|
124
|
+
},
|
|
125
|
+
});
|
|
126
|
+
if (ledgerError) {
|
|
127
|
+
logger.error("[AiGenService] Failed to record in ledger:", ledgerError);
|
|
128
|
+
// Continue anyway - billing already validated
|
|
129
|
+
}
|
|
130
|
+
logger.info(`[AiGenService] ✅ Text generation completed: cost=$${billingResult.providerCostUsd?.toFixed(6)}, sell=$${billingResult.sellCostUsd?.toFixed(6)}`);
|
|
131
|
+
return {
|
|
132
|
+
success: true,
|
|
133
|
+
text: result.text || "",
|
|
134
|
+
inputTokens,
|
|
135
|
+
outputTokens,
|
|
136
|
+
model: modelName,
|
|
137
|
+
provider,
|
|
138
|
+
providerCostUsd: billingResult.providerCostUsd,
|
|
139
|
+
sellCostUsd: billingResult.sellCostUsd,
|
|
140
|
+
marginPercent: billingResult.marginPercent,
|
|
141
|
+
};
|
|
142
|
+
}
|
|
143
|
+
catch (error) {
|
|
144
|
+
logger.error("[AiGenService] Error generating text:", error);
|
|
145
|
+
return {
|
|
146
|
+
success: false,
|
|
147
|
+
error: error.message || "Erreur lors de la génération",
|
|
148
|
+
errorCode: "PROVIDER_ERROR",
|
|
149
|
+
};
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
// ============================================================================
|
|
153
|
+
// IMAGE GENERATION
|
|
154
|
+
// ============================================================================
|
|
155
|
+
/**
|
|
156
|
+
* Generate image with automatic billing
|
|
157
|
+
*
|
|
158
|
+
* @param userId User ID
|
|
159
|
+
* @param params Generation parameters
|
|
160
|
+
* @returns Generation result with billing info
|
|
161
|
+
*/
|
|
162
|
+
export async function generateImageWithBilling(userId, params) {
|
|
163
|
+
try {
|
|
164
|
+
const { prompt, model = "dall-e-3", size = "1024x1024", quality = "standard", actionType = "generate-image", metadata = {}, } = params;
|
|
165
|
+
// 1. Build full model string
|
|
166
|
+
const fullModel = model.includes("/") ? model : `openai/${model}`;
|
|
167
|
+
const [provider, modelName] = fullModel.split("/", 2);
|
|
168
|
+
logger.info(`[AiGenService] Generating image with ${fullModel} for user ${userId}`);
|
|
169
|
+
// 2. Check if model is enabled
|
|
170
|
+
const filterResult = await isModelEnabled(userId, fullModel);
|
|
171
|
+
if (!filterResult.allowed) {
|
|
172
|
+
logger.warn(`[AiGenService] Model ${fullModel} not allowed for user ${userId}`);
|
|
173
|
+
return {
|
|
174
|
+
success: false,
|
|
175
|
+
error: filterResult.error || "Modèle non autorisé",
|
|
176
|
+
errorCode: "MODEL_NOT_ALLOWED",
|
|
177
|
+
};
|
|
178
|
+
}
|
|
179
|
+
// 3. Validate pricing
|
|
180
|
+
const pricingValidation = validateImagePricing(filterResult.model?.pricing, fullModel);
|
|
181
|
+
if (!pricingValidation.valid) {
|
|
182
|
+
logger.error(`[AiGenService] Invalid pricing for ${fullModel}`);
|
|
183
|
+
return {
|
|
184
|
+
success: false,
|
|
185
|
+
error: pricingValidation.error || "Pricing non disponible",
|
|
186
|
+
errorCode: "NO_PRICING",
|
|
187
|
+
};
|
|
188
|
+
}
|
|
189
|
+
const pricing = pricingValidation.pricing;
|
|
190
|
+
// 4. Check wallet
|
|
191
|
+
const wallet = await getWalletState(userId);
|
|
192
|
+
if (!wallet.walletProviderBudgetUsd ||
|
|
193
|
+
wallet.walletProviderBudgetUsd <= 0) {
|
|
194
|
+
logger.warn(`[AiGenService] No credits for user ${userId}: $${wallet.walletProviderBudgetUsd}`);
|
|
195
|
+
return {
|
|
196
|
+
success: false,
|
|
197
|
+
error: "Crédits insuffisants pour cette opération",
|
|
198
|
+
errorCode: "NO_CREDITS",
|
|
199
|
+
};
|
|
200
|
+
}
|
|
201
|
+
logger.info(`[AiGenService] Pre-check passed: wallet=$${wallet.walletProviderBudgetUsd.toFixed(6)}`);
|
|
202
|
+
// 5. Call AI API
|
|
203
|
+
const result = await imageAI({
|
|
204
|
+
prompt,
|
|
205
|
+
model: fullModel,
|
|
206
|
+
size,
|
|
207
|
+
});
|
|
208
|
+
// 6. Compute real cost
|
|
209
|
+
const usage = { images: 1 };
|
|
210
|
+
const providerCostUsd = computeProviderCostUsd(usage, pricing);
|
|
211
|
+
// 7. Debit wallet
|
|
212
|
+
const billingResult = computeAndDebitAIUsage({
|
|
213
|
+
userId,
|
|
214
|
+
actionType,
|
|
215
|
+
providerCostUsd,
|
|
216
|
+
wallet,
|
|
217
|
+
metadata: {
|
|
218
|
+
model: modelName,
|
|
219
|
+
provider,
|
|
220
|
+
endpoint: "image",
|
|
221
|
+
prompt: prompt.substring(0, 100),
|
|
222
|
+
size,
|
|
223
|
+
quality,
|
|
224
|
+
...metadata,
|
|
225
|
+
},
|
|
226
|
+
});
|
|
227
|
+
if (!billingResult.success) {
|
|
228
|
+
logger.error(`[AiGenService] Billing failed: ${billingResult.error}`);
|
|
229
|
+
return {
|
|
230
|
+
success: false,
|
|
231
|
+
error: billingResult.error || "Erreur de facturation",
|
|
232
|
+
errorCode: "NO_CREDITS",
|
|
233
|
+
};
|
|
234
|
+
}
|
|
235
|
+
// 8. Record in ledger
|
|
236
|
+
const supabase = await getSupabaseServiceClient();
|
|
237
|
+
const { error: ledgerError } = await supabase.rpc("record_token_consumption", {
|
|
238
|
+
p_user_id: userId,
|
|
239
|
+
p_debit_tokens: 0, // Calculated by wallet system
|
|
240
|
+
p_provider_cost_usd: billingResult.providerCostUsd || 0,
|
|
241
|
+
p_sell_usd: billingResult.sellCostUsd || 0,
|
|
242
|
+
p_margin_usd: billingResult.marginUsd || 0,
|
|
243
|
+
p_meta: {
|
|
244
|
+
model: modelName,
|
|
245
|
+
provider,
|
|
246
|
+
endpoint: "image",
|
|
247
|
+
size,
|
|
248
|
+
quality,
|
|
249
|
+
...metadata,
|
|
250
|
+
},
|
|
251
|
+
});
|
|
252
|
+
if (ledgerError) {
|
|
253
|
+
logger.error("[AiGenService] Failed to record in ledger:", ledgerError);
|
|
254
|
+
}
|
|
255
|
+
logger.info(`[AiGenService] ✅ Image generation completed: cost=$${billingResult.providerCostUsd?.toFixed(6)}, sell=$${billingResult.sellCostUsd?.toFixed(6)}`);
|
|
256
|
+
return {
|
|
257
|
+
success: true,
|
|
258
|
+
imageUrl: result.imageUrl || "",
|
|
259
|
+
model: modelName,
|
|
260
|
+
provider,
|
|
261
|
+
providerCostUsd: billingResult.providerCostUsd,
|
|
262
|
+
sellCostUsd: billingResult.sellCostUsd,
|
|
263
|
+
marginPercent: billingResult.marginPercent,
|
|
264
|
+
};
|
|
265
|
+
}
|
|
266
|
+
catch (error) {
|
|
267
|
+
logger.error("[AiGenService] Error generating image:", error);
|
|
268
|
+
return {
|
|
269
|
+
success: false,
|
|
270
|
+
error: error.message || "Erreur lors de la génération",
|
|
271
|
+
errorCode: "PROVIDER_ERROR",
|
|
272
|
+
};
|
|
273
|
+
}
|
|
274
|
+
}
|
|
@@ -0,0 +1,200 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Core billing functions for wallet-based AI token system
|
|
3
|
+
*
|
|
4
|
+
* RÈGLE ÉCONOMIQUE FONDAMENTALE :
|
|
5
|
+
* ================================
|
|
6
|
+
* Un pack vendu X$ donne un budget fournisseur limité (ex: 5$ vendu => 2$ budget provider)
|
|
7
|
+
* Le reste (3$) = marge + infrastructure.
|
|
8
|
+
*
|
|
9
|
+
* L'utilisateur NE PEUT PAS générer des milliers de recettes pour 5$.
|
|
10
|
+
* Le système ne doit JAMAIS permettre d'être à perte.
|
|
11
|
+
*
|
|
12
|
+
* Pour garantir cela, CHAQUE action a un coût minimum de vente (minSellUsd):
|
|
13
|
+
* - generate-recipe-text: min 0.20$ (action à forte valeur)
|
|
14
|
+
* - generate-image: min calculé selon le modèle
|
|
15
|
+
* - autocomplete UI (input/textarea): min 0$ (micro-calls)
|
|
16
|
+
*
|
|
17
|
+
* Le calcul de marge cible est 60% sur le prix de vente:
|
|
18
|
+
* sellCostUsd = providerCostUsd / (1 - 0.60) = providerCostUsd / 0.40
|
|
19
|
+
*
|
|
20
|
+
* MAIS on respecte TOUJOURS la contrainte pack:
|
|
21
|
+
* - on ne peut pas dépasser le budget provider restant
|
|
22
|
+
* - on applique le minimum par action si configuré
|
|
23
|
+
*/
|
|
24
|
+
export interface ProviderPricing {
|
|
25
|
+
/** Input tokens cost in $/token (from Gateway) */
|
|
26
|
+
inputUsdPerToken?: number;
|
|
27
|
+
/** Output tokens cost in $/token (from Gateway) */
|
|
28
|
+
outputUsdPerToken?: number;
|
|
29
|
+
/** Cache read tokens cost in $/token */
|
|
30
|
+
cacheReadUsdPerToken?: number;
|
|
31
|
+
/** Web search cost in $/unit */
|
|
32
|
+
webSearchUsdPerUnit?: number;
|
|
33
|
+
/** Image cost in $/image */
|
|
34
|
+
imageUsdPerImage?: number;
|
|
35
|
+
}
|
|
36
|
+
export interface Usage {
|
|
37
|
+
/** Input tokens used */
|
|
38
|
+
inputTokens?: number;
|
|
39
|
+
/** Output tokens used */
|
|
40
|
+
outputTokens?: number;
|
|
41
|
+
/** Cache read tokens */
|
|
42
|
+
cacheReadTokens?: number;
|
|
43
|
+
/** Web search units */
|
|
44
|
+
webSearchUnits?: number;
|
|
45
|
+
/** Number of images generated */
|
|
46
|
+
images?: number;
|
|
47
|
+
}
|
|
48
|
+
export interface WalletState {
|
|
49
|
+
/** Current token balance */
|
|
50
|
+
tokenBalance: number;
|
|
51
|
+
/** Remaining provider budget (USD) */
|
|
52
|
+
walletProviderBudgetUsd: number;
|
|
53
|
+
/** Remaining sell value (USD) */
|
|
54
|
+
walletSellValueUsd: number;
|
|
55
|
+
}
|
|
56
|
+
export interface DebitResult {
|
|
57
|
+
/** Tokens to debit from user */
|
|
58
|
+
debitTokens: number;
|
|
59
|
+
/** Sell value for this transaction (USD) */
|
|
60
|
+
sellUsd: number;
|
|
61
|
+
/** Margin earned (USD) */
|
|
62
|
+
marginUsd: number;
|
|
63
|
+
/** USD provider cost per token (computed rate) */
|
|
64
|
+
usdProviderPerToken: number;
|
|
65
|
+
/** USD sell value per token (computed rate) */
|
|
66
|
+
usdSellPerToken: number;
|
|
67
|
+
}
|
|
68
|
+
export interface PackPurchase {
|
|
69
|
+
/** Number of tokens in pack */
|
|
70
|
+
packTokens: number;
|
|
71
|
+
/** Pack price in USD */
|
|
72
|
+
packPriceUsd: number;
|
|
73
|
+
/** Margin coefficient (e.g., 1.35 = 35% margin) */
|
|
74
|
+
packCoef: number;
|
|
75
|
+
}
|
|
76
|
+
export interface WalletUpdate {
|
|
77
|
+
/** Provider budget to add (USD) */
|
|
78
|
+
providerBudgetAddedUsd: number;
|
|
79
|
+
/** Sell value to add (USD) */
|
|
80
|
+
sellValueAddedUsd: number;
|
|
81
|
+
}
|
|
82
|
+
/**
|
|
83
|
+
* Compute provider cost in USD from usage and pricing
|
|
84
|
+
* @param usage Actual tokens/images used
|
|
85
|
+
* @param pricing Provider pricing ($/token, $/image)
|
|
86
|
+
* @returns Total provider cost in USD
|
|
87
|
+
*/
|
|
88
|
+
export declare function computeProviderCostUsd(usage: Usage, pricing: ProviderPricing): number;
|
|
89
|
+
/**
|
|
90
|
+
* Compute token debit from wallet (with automatic margin smoothing)
|
|
91
|
+
* @param wallet Current wallet state
|
|
92
|
+
* @param providerCostUsd Provider cost for this transaction
|
|
93
|
+
* @returns Debit details (tokens, sell value, margin)
|
|
94
|
+
*/
|
|
95
|
+
export declare function computeDebitFromWallet(wallet: WalletState, providerCostUsd: number): DebitResult;
|
|
96
|
+
/**
|
|
97
|
+
* Compute wallet update for pack purchase
|
|
98
|
+
* @param pack Pack details (tokens, price, coef)
|
|
99
|
+
* @returns Wallet deltas to add
|
|
100
|
+
*/
|
|
101
|
+
export declare function computeWalletUpdateForPurchase(pack: PackPurchase): WalletUpdate;
|
|
102
|
+
/**
|
|
103
|
+
* Estimate debit tokens for display (using pack coef)
|
|
104
|
+
* Useful for public pricing page before user has wallet
|
|
105
|
+
* @param providerCostUsd Provider cost
|
|
106
|
+
* @param packCoef Pack margin coefficient
|
|
107
|
+
* @param packTokens Pack size
|
|
108
|
+
* @param packPriceUsd Pack price
|
|
109
|
+
* @returns Estimated tokens
|
|
110
|
+
*/
|
|
111
|
+
export declare function estimateDebitTokens(providerCostUsd: number, packCoef: number, packTokens: number, packPriceUsd: number): number;
|
|
112
|
+
/**
|
|
113
|
+
* Validate that pricing has required fields for usage type
|
|
114
|
+
*/
|
|
115
|
+
export declare function validatePricing(pricing: ProviderPricing, usageType: "text" | "image"): boolean;
|
|
116
|
+
/**
|
|
117
|
+
* Valid action types for billing
|
|
118
|
+
*/
|
|
119
|
+
export type ActionType = "generate-recipe-text" | "generate-image" | "generate-text" | "autocomplete" | "suggestion-input" | "suggestion-textarea";
|
|
120
|
+
export interface ComputeAndDebitParams {
|
|
121
|
+
/** User ID */
|
|
122
|
+
userId: string;
|
|
123
|
+
/** Action type (e.g., "generate-recipe-text", "generate-image") */
|
|
124
|
+
actionType: ActionType;
|
|
125
|
+
/** Provider cost already calculated (USD) */
|
|
126
|
+
providerCostUsd: number;
|
|
127
|
+
/** Current wallet state */
|
|
128
|
+
wallet: WalletState;
|
|
129
|
+
/** Metadata for logging */
|
|
130
|
+
metadata: {
|
|
131
|
+
model?: string;
|
|
132
|
+
provider?: string;
|
|
133
|
+
endpoint?: string;
|
|
134
|
+
[key: string]: any;
|
|
135
|
+
};
|
|
136
|
+
/** Optional override for minimum sell cost */
|
|
137
|
+
minSellUsdOverride?: number;
|
|
138
|
+
}
|
|
139
|
+
export interface ComputeAndDebitResult {
|
|
140
|
+
success: boolean;
|
|
141
|
+
error?: string;
|
|
142
|
+
/** Sell cost charged to user (USD) */
|
|
143
|
+
sellCostUsd?: number;
|
|
144
|
+
/** Provider cost (USD) */
|
|
145
|
+
providerCostUsd?: number;
|
|
146
|
+
/** Margin earned (USD) */
|
|
147
|
+
marginUsd?: number;
|
|
148
|
+
/** Margin percentage */
|
|
149
|
+
marginPercent?: number;
|
|
150
|
+
/** New wallet state after debit */
|
|
151
|
+
newWallet?: {
|
|
152
|
+
walletProviderBudgetUsd: number;
|
|
153
|
+
walletSellValueUsd: number;
|
|
154
|
+
percentRemaining: number;
|
|
155
|
+
};
|
|
156
|
+
/** Tokens debited (for backward compatibility) */
|
|
157
|
+
tokensDebited?: number;
|
|
158
|
+
}
|
|
159
|
+
/**
|
|
160
|
+
* Get current wallet state for a user
|
|
161
|
+
* CENTRALIZED - This is the single source of truth for wallet state
|
|
162
|
+
*/
|
|
163
|
+
export declare function getWalletState(userId: string): Promise<WalletState>;
|
|
164
|
+
/**
|
|
165
|
+
* FONCTION CENTRALE DE BILLING
|
|
166
|
+
* ============================
|
|
167
|
+
* Calcule et débite l'utilisation IA selon les règles économiques.
|
|
168
|
+
*
|
|
169
|
+
* Cette fonction est la SEULE source de vérité pour tous les calculs de billing.
|
|
170
|
+
* Elle est appelée par TOUS les endpoints (generate-text, generate-image, recipes, etc.)
|
|
171
|
+
*
|
|
172
|
+
* RÈGLES DE CALCUL :
|
|
173
|
+
* 1. Marge cible = 60% du prix de vente
|
|
174
|
+
* => sellCostUsd = providerCostUsd / (1 - 0.60) = providerCostUsd / 0.40 = providerCostUsd * 2.5
|
|
175
|
+
*
|
|
176
|
+
* 2. Minimum par action (si configuré)
|
|
177
|
+
* => sellCostUsd = max(sellCostUsd calculé, minSellUsd)
|
|
178
|
+
*
|
|
179
|
+
* 3. Contrainte budget provider
|
|
180
|
+
* => on ne peut jamais débiter plus que le budget provider restant
|
|
181
|
+
*
|
|
182
|
+
* 4. Arrondi safe (ceil) pour éviter sous-facturation
|
|
183
|
+
*
|
|
184
|
+
* LOGGING : log ANONYME de chaque débit pour contrôle
|
|
185
|
+
*/
|
|
186
|
+
export declare function computeAndDebitAIUsage(params: ComputeAndDebitParams): ComputeAndDebitResult;
|
|
187
|
+
/**
|
|
188
|
+
* Add wallet credits to a user (admin function)
|
|
189
|
+
* Adds both provider budget and sell value proportionally
|
|
190
|
+
* @param userId User ID
|
|
191
|
+
* @param sellValueUsd Amount in USD to add to sell value
|
|
192
|
+
* @param reason Admin reason for adding credits
|
|
193
|
+
* @returns Success status and new wallet state
|
|
194
|
+
*/
|
|
195
|
+
export declare function addWalletCredits(userId: string, sellValueUsd: number, reason: string): Promise<{
|
|
196
|
+
success: boolean;
|
|
197
|
+
error?: string;
|
|
198
|
+
newBalance?: number;
|
|
199
|
+
}>;
|
|
200
|
+
//# sourceMappingURL=billing.d.ts.map
|
|
@@ -0,0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