@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,276 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* REFACTORING BILLING IA - GUIDE D'IMPLÉMENTATION
|
|
3
|
+
* =================================================
|
|
4
|
+
*
|
|
5
|
+
* Ce fichier documente les changements nécessaires pour centraliser
|
|
6
|
+
* la logique de billing IA et passer à un modèle "Crédits IA" (USD).
|
|
7
|
+
*
|
|
8
|
+
* ✅ DÉJÀ FAIT :
|
|
9
|
+
* ==============
|
|
10
|
+
* 1. `packages/module-ai/src/server/billing.ts`
|
|
11
|
+
* - Ajout documentation règles économiques
|
|
12
|
+
* - Création fonction `computeAndDebitAIUsage()` centralisée
|
|
13
|
+
* - Configuration minimums par action (ACTION_MIN_SELL_USD)
|
|
14
|
+
* - Logging anonyme de chaque débit
|
|
15
|
+
*
|
|
16
|
+
* 2. `packages/module-ai/src/api/public/pricing-summary.ts`
|
|
17
|
+
* - Endpoint GET pour afficher estimations selon nouveau modèle
|
|
18
|
+
*
|
|
19
|
+
* ⚠️ À FAIRE :
|
|
20
|
+
* ============
|
|
21
|
+
*
|
|
22
|
+
* A) Modifier tous les endpoints pour utiliser `computeAndDebitAIUsage()`
|
|
23
|
+
* -------------------------------------------------------------------
|
|
24
|
+
*
|
|
25
|
+
* Fichiers à modifier :
|
|
26
|
+
* - `packages/module-ai/src/api/auth/generate-text.ts`
|
|
27
|
+
* - `packages/module-ai/src/api/auth/generate-image.ts`
|
|
28
|
+
* - `packages/module-recipes-pro/src/api/auth/recipes-bilingual.ts`
|
|
29
|
+
* - Tous les endpoints de suggestions UI (autocomplete, etc.)
|
|
30
|
+
*
|
|
31
|
+
* Pattern de remplacement :
|
|
32
|
+
*
|
|
33
|
+
* ```typescript
|
|
34
|
+
* // AVANT (ancien système avec recordConsumption)
|
|
35
|
+
* const consumeResult = await recordConsumption(user.id, usage, pricing, metadata);
|
|
36
|
+
*
|
|
37
|
+
* // APRÈS (nouveau système centralisé)
|
|
38
|
+
* import { computeAndDebitAIUsage, computeProviderCostUsd } from "@lastbrain/module-ai/server/billing";
|
|
39
|
+
* import { getWalletState } from "@lastbrain/module-ai/server/billing";
|
|
40
|
+
*
|
|
41
|
+
* // 1. Calculer le coût provider
|
|
42
|
+
* const providerCostUsd = computeProviderCostUsd(usage, pricing);
|
|
43
|
+
*
|
|
44
|
+
* // 2. Récupérer wallet
|
|
45
|
+
* const wallet = await getWalletState(user.id);
|
|
46
|
+
*
|
|
47
|
+
* // 3. Compute & debit avec actionType approprié
|
|
48
|
+
* const billingResult = computeAndDebitAIUsage({
|
|
49
|
+
* userId: user.id,
|
|
50
|
+
* actionType: "generate-recipe-text", // ou "generate-image", "generate-text", etc.
|
|
51
|
+
* providerCostUsd,
|
|
52
|
+
* wallet,
|
|
53
|
+
* metadata: {
|
|
54
|
+
* model: modelName,
|
|
55
|
+
* provider,
|
|
56
|
+
* endpoint: "generate-text",
|
|
57
|
+
* prompt: prompt.substring(0, 32),
|
|
58
|
+
* generatedText: generatedText.substring(0, 500),
|
|
59
|
+
* },
|
|
60
|
+
* });
|
|
61
|
+
*
|
|
62
|
+
* if (!billingResult.success) {
|
|
63
|
+
* return NextResponse.json(
|
|
64
|
+
* { error: "Crédits insuffisants" },
|
|
65
|
+
* { status: 402 } // Payment Required
|
|
66
|
+
* );
|
|
67
|
+
* }
|
|
68
|
+
*
|
|
69
|
+
* // 4. Enregistrer dans le ledger via RPC
|
|
70
|
+
* const { error: ledgerError } = await supabase.rpc("record_token_consumption", {
|
|
71
|
+
* p_user_id: user.id,
|
|
72
|
+
* p_debit_tokens: billingResult.tokensDebited || 0,
|
|
73
|
+
* p_provider_cost_usd: providerCostUsd,
|
|
74
|
+
* p_sell_usd: billingResult.sellCostUsd,
|
|
75
|
+
* p_margin_usd: billingResult.marginUsd,
|
|
76
|
+
* p_meta: metadata,
|
|
77
|
+
* });
|
|
78
|
+
* ```
|
|
79
|
+
*
|
|
80
|
+
*
|
|
81
|
+
* B) Refactorer PurchaseTokensPage.tsx
|
|
82
|
+
* ------------------------------------
|
|
83
|
+
*
|
|
84
|
+
* Modifications UI :
|
|
85
|
+
*
|
|
86
|
+
* 1. Remplacer tous "Tokens" par "Crédits IA"
|
|
87
|
+
*
|
|
88
|
+
* 2. Afficher pour chaque pack :
|
|
89
|
+
* ```tsx
|
|
90
|
+
* <div className="mb-6">
|
|
91
|
+
* <p className="text-4xl font-bold text-primary mb-1">
|
|
92
|
+
* {formatPrice(pack.price_cents, pack.currency)}
|
|
93
|
+
* </p>
|
|
94
|
+
* <p className="text-sm text-default-700">Crédit IA</p>
|
|
95
|
+
* </div>
|
|
96
|
+
*
|
|
97
|
+
* <div className="mb-4 p-3 bg-blue-50 rounded-lg">
|
|
98
|
+
* <p className="text-xs text-gray-600 mb-1">
|
|
99
|
+
* Budget fournisseur inclus
|
|
100
|
+
* </p>
|
|
101
|
+
* <p className="text-sm font-semibold text-blue-600">
|
|
102
|
+
* ${((pack.price_cents / 100) / 2.5).toFixed(2)}
|
|
103
|
+
* </p>
|
|
104
|
+
* <p className="text-xs text-gray-500 mt-1">
|
|
105
|
+
* (marge et infrastructure incluses)
|
|
106
|
+
* </p>
|
|
107
|
+
* </div>
|
|
108
|
+
*
|
|
109
|
+
* <div className="mb-6 text-xs text-gray-600">
|
|
110
|
+
* <p>· Recette complète : à partir de 0.20$</p>
|
|
111
|
+
* <p>· Image : à partir de 0.05$</p>
|
|
112
|
+
* <p className="mt-2 font-semibold text-green-600">
|
|
113
|
+
* ≈ {Math.floor((pack.price_cents / 100) / 0.20)} recettes max
|
|
114
|
+
* </p>
|
|
115
|
+
* </div>
|
|
116
|
+
* ```
|
|
117
|
+
*
|
|
118
|
+
* 3. Supprimer/simplifier la section "Models Pricing"
|
|
119
|
+
* - Ne plus afficher prix token/million tokens
|
|
120
|
+
* - Afficher coûts estimés par action en USD
|
|
121
|
+
* - Utiliser endpoint `/api/ai/public/pricing-summary`
|
|
122
|
+
*
|
|
123
|
+
*
|
|
124
|
+
* C) Refactorer UsageAndTokensPage.tsx
|
|
125
|
+
* ------------------------------------
|
|
126
|
+
*
|
|
127
|
+
* Modifications UI :
|
|
128
|
+
*
|
|
129
|
+
* 1. Remplacer TokenKpiGrid par une barre de progression simple :
|
|
130
|
+
* ```tsx
|
|
131
|
+
* <Card>
|
|
132
|
+
* <CardHeader>
|
|
133
|
+
* <h3>Crédits IA restants</h3>
|
|
134
|
+
* </CardHeader>
|
|
135
|
+
* <CardBody>
|
|
136
|
+
* <div className="mb-2">
|
|
137
|
+
* <div className="flex justify-between mb-1">
|
|
138
|
+
* <span className="text-2xl font-bold">
|
|
139
|
+
* {wallet ? `$${wallet.walletSellValueUsd.toFixed(2)}` : "—"}
|
|
140
|
+
* </span>
|
|
141
|
+
* <span className={`text-sm ${getColorClass(percentRemaining)}`}>
|
|
142
|
+
* {percentRemaining.toFixed(1)}%
|
|
143
|
+
* </span>
|
|
144
|
+
* </div>
|
|
145
|
+
* <div className="w-full bg-gray-200 rounded-full h-3">
|
|
146
|
+
* <div
|
|
147
|
+
* className={`h-3 rounded-full ${getProgressBarColor(percentRemaining)}`}
|
|
148
|
+
* style={{ width: `${percentRemaining}%` }}
|
|
149
|
+
* />
|
|
150
|
+
* </div>
|
|
151
|
+
* </div>
|
|
152
|
+
*
|
|
153
|
+
* {percentRemaining < 25 && (
|
|
154
|
+
* <Alert color="warning" className="mt-4">
|
|
155
|
+
* <AlertCircle size={20} />
|
|
156
|
+
* <p>Vos crédits sont faibles. Rechargez maintenant.</p>
|
|
157
|
+
* <PurchaseButton />
|
|
158
|
+
* </Alert>
|
|
159
|
+
* )}
|
|
160
|
+
* </CardBody>
|
|
161
|
+
* </Card>
|
|
162
|
+
* ```
|
|
163
|
+
*
|
|
164
|
+
* 2. Modifier UsageByModelBarChart pour afficher :
|
|
165
|
+
* - Modèle
|
|
166
|
+
* - Nombre d'appels
|
|
167
|
+
* - Total facturé (sellUsd)
|
|
168
|
+
* - Ne PAS afficher tokens
|
|
169
|
+
*
|
|
170
|
+
* 3. Seuils couleurs :
|
|
171
|
+
* ```typescript
|
|
172
|
+
* function getColorClass(percent: number): string {
|
|
173
|
+
* if (percent > 50) return "text-green-600";
|
|
174
|
+
* if (percent > 25) return "text-orange-500";
|
|
175
|
+
* return "text-red-600";
|
|
176
|
+
* }
|
|
177
|
+
*
|
|
178
|
+
* function getProgressBarColor(percent: number): string {
|
|
179
|
+
* if (percent > 50) return "bg-green-500";
|
|
180
|
+
* if (percent > 25) return "bg-orange-500";
|
|
181
|
+
* return "bg-red-500";
|
|
182
|
+
* }
|
|
183
|
+
* ```
|
|
184
|
+
*
|
|
185
|
+
*
|
|
186
|
+
* D) Endpoint wallet
|
|
187
|
+
* ------------------
|
|
188
|
+
*
|
|
189
|
+
* Créer/modifier `/api/ai/auth/wallet` pour retourner :
|
|
190
|
+
* ```typescript
|
|
191
|
+
* {
|
|
192
|
+
* walletSellValueUsd: number, // Crédit restant
|
|
193
|
+
* walletProviderBudgetUsd: number, // Budget provider restant
|
|
194
|
+
* percentRemaining: number, // % restant
|
|
195
|
+
* tokenBalance: number, // Pour compatibilité (ne pas afficher en UI)
|
|
196
|
+
* }
|
|
197
|
+
* ```
|
|
198
|
+
*
|
|
199
|
+
*
|
|
200
|
+
* E) Tests à ajouter
|
|
201
|
+
* ------------------
|
|
202
|
+
*
|
|
203
|
+
* Créer `packages/module-ai/src/server/__tests__/billing.test.ts` :
|
|
204
|
+
*
|
|
205
|
+
* ```typescript
|
|
206
|
+
* import { computeAndDebitAIUsage } from "../billing";
|
|
207
|
+
*
|
|
208
|
+
* describe("Billing centralisé", () => {
|
|
209
|
+
* test("Pack 5$ : recette doit débiter min 0.20$", () => {
|
|
210
|
+
* const result = computeAndDebitAIUsage({
|
|
211
|
+
* userId: "test-user",
|
|
212
|
+
* actionType: "generate-recipe-text",
|
|
213
|
+
* providerCostUsd: 0.001, // Très bas
|
|
214
|
+
* wallet: {
|
|
215
|
+
* tokenBalance: 100000,
|
|
216
|
+
* walletProviderBudgetUsd: 2.0,
|
|
217
|
+
* walletSellValueUsd: 5.0,
|
|
218
|
+
* },
|
|
219
|
+
* metadata: { model: "gpt-4o-mini" },
|
|
220
|
+
* });
|
|
221
|
+
*
|
|
222
|
+
* expect(result.success).toBe(true);
|
|
223
|
+
* expect(result.sellCostUsd).toBeGreaterThanOrEqual(0.20);
|
|
224
|
+
* });
|
|
225
|
+
*
|
|
226
|
+
* test("Budget provider insuffisant => refus", () => {
|
|
227
|
+
* const result = computeAndDebitAIUsage({
|
|
228
|
+
* userId: "test-user",
|
|
229
|
+
* actionType: "generate-recipe-text",
|
|
230
|
+
* providerCostUsd: 0.15,
|
|
231
|
+
* wallet: {
|
|
232
|
+
* tokenBalance: 100000,
|
|
233
|
+
* walletProviderBudgetUsd: 0.10, // Insuffisant
|
|
234
|
+
* walletSellValueUsd: 5.0,
|
|
235
|
+
* },
|
|
236
|
+
* metadata: { model: "gpt-4o-mini" },
|
|
237
|
+
* });
|
|
238
|
+
*
|
|
239
|
+
* expect(result.success).toBe(false);
|
|
240
|
+
* expect(result.error).toBe("insufficient_credits");
|
|
241
|
+
* });
|
|
242
|
+
* });
|
|
243
|
+
* ```
|
|
244
|
+
*
|
|
245
|
+
*
|
|
246
|
+
* F) Migration base de données
|
|
247
|
+
* ----------------------------
|
|
248
|
+
*
|
|
249
|
+
* Aucune migration nécessaire - le système est rétrocompatible.
|
|
250
|
+
* Les anciennes données continuent de fonctionner avec les nouveaux calculs.
|
|
251
|
+
*
|
|
252
|
+
*
|
|
253
|
+
* G) Documentation utilisateur
|
|
254
|
+
* ----------------------------
|
|
255
|
+
*
|
|
256
|
+
* Mettre à jour la doc pour expliquer :
|
|
257
|
+
* - Le système de Crédits IA (USD) remplace les tokens
|
|
258
|
+
* - Coûts minimum par action garantissent qualité service
|
|
259
|
+
* - Budget provider séparé évite les pertes
|
|
260
|
+
* - Marge de 60% permet maintenance infrastructure
|
|
261
|
+
*
|
|
262
|
+
*
|
|
263
|
+
* VALIDATION FINALE :
|
|
264
|
+
* ==================
|
|
265
|
+
*
|
|
266
|
+
* Avant mise en prod, vérifier :
|
|
267
|
+
*
|
|
268
|
+
* ✅ Tous les endpoints appellent computeAndDebitAIUsage()
|
|
269
|
+
* ✅ UI affiche "Crédits IA" au lieu de "tokens"
|
|
270
|
+
* ✅ Minimums appliqués : recette ≥ 0.20$, image ≥ 0.05$
|
|
271
|
+
* ✅ walletProviderBudgetUsd ne passe jamais sous 0
|
|
272
|
+
* ✅ Logs anonymes apparaissent à chaque débit
|
|
273
|
+
* ✅ Tests passent
|
|
274
|
+
* ✅ Pas de régression pour utilisateurs existants
|
|
275
|
+
*/
|
|
276
|
+
export {};
|
package/dist/index.d.ts
CHANGED
|
@@ -1,14 +1,28 @@
|
|
|
1
1
|
export { TextareaGenerative } from "./web/components/TextareaGenerative";
|
|
2
2
|
export { ImageGenerative } from "./web/components/ImageGenerative";
|
|
3
3
|
export { ButtonGenerative } from "./web/components/ButtonGenerative";
|
|
4
|
-
export {
|
|
4
|
+
export { UsageAndTokensPage } from "./web/auth/UsageAndTokensPage";
|
|
5
|
+
export { APIKeysPage } from "./web/auth/APIKeysPage";
|
|
6
|
+
export { AISettingsPage } from "./web/auth/AISettingsPage";
|
|
7
|
+
export { HistoryPage } from "./web/auth/HistoryPage";
|
|
8
|
+
export { PromptsPage } from "./web/auth/PromptsPage";
|
|
5
9
|
export { UserTokenPage } from "./web/admin/UserTokenPage";
|
|
6
10
|
export { AdminTokenPacksPage } from "./web/admin/AdminTokenPacksPage";
|
|
11
|
+
export { BillingAnalyticsPage } from "./web/admin/BillingAnalyticsPage";
|
|
12
|
+
export { default as GlobalAISettingsPage } from "./web/admin/GlobalAISettingsPage";
|
|
7
13
|
export { UserTokenTab } from "./components/admin/UserTokenTab";
|
|
14
|
+
export { AuthDashboardAi } from "./components/auth/AuthDashboardAi";
|
|
15
|
+
export { PurchaseTokensPage } from "./web/public/PurchaseTokensPage";
|
|
16
|
+
export { PublicPromptsPage } from "./web/public/PublicPromptsPage";
|
|
17
|
+
export { UserPromptsPage } from "./web/public/UserPromptsPage";
|
|
18
|
+
export { UserAvatar } from "./web/public/UserAvatar";
|
|
8
19
|
export { Doc } from "./components/Doc";
|
|
9
20
|
export { Doc as AiModuleDoc } from "./components/Doc";
|
|
10
21
|
export type { GenerativeResponse, TextareaGenerativeProps, } from "./web/components/TextareaGenerative";
|
|
11
22
|
export type { GenerativeImageResponse, ImageGenerativeProps, } from "./web/components/ImageGenerative";
|
|
12
23
|
export type { ButtonGenerativeResponse, ButtonGenerativeProps, } from "./web/components/ButtonGenerative";
|
|
24
|
+
export type { PlanType, UserTokenQuotaMonthly, UserTokenQuotaLedger, TokenQuotaStatus, TokenBalance, DebitTokensRequest, DebitTokensResponse, UpdateQuotaRequest, } from "./types/quota";
|
|
25
|
+
export { PLAN_DEFAULT_QUOTAS } from "./types/quota";
|
|
13
26
|
export { default as buildConfig } from "./ai.build.config";
|
|
27
|
+
export { sitemapManifest } from "./sitemap/manifest";
|
|
14
28
|
//# sourceMappingURL=index.d.ts.map
|
package/dist/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,kBAAkB,EAAE,MAAM,qCAAqC,CAAC;AACzE,OAAO,EAAE,eAAe,EAAE,MAAM,kCAAkC,CAAC;AACnE,OAAO,EAAE,gBAAgB,EAAE,MAAM,mCAAmC,CAAC;AAGrE,OAAO,EAAE,
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,kBAAkB,EAAE,MAAM,qCAAqC,CAAC;AACzE,OAAO,EAAE,eAAe,EAAE,MAAM,kCAAkC,CAAC;AACnE,OAAO,EAAE,gBAAgB,EAAE,MAAM,mCAAmC,CAAC;AAGrE,OAAO,EAAE,kBAAkB,EAAE,MAAM,+BAA+B,CAAC;AACnE,OAAO,EAAE,WAAW,EAAE,MAAM,wBAAwB,CAAC;AACrD,OAAO,EAAE,cAAc,EAAE,MAAM,2BAA2B,CAAC;AAC3D,OAAO,EAAE,WAAW,EAAE,MAAM,wBAAwB,CAAC;AACrD,OAAO,EAAE,WAAW,EAAE,MAAM,wBAAwB,CAAC;AAGrD,OAAO,EAAE,aAAa,EAAE,MAAM,2BAA2B,CAAC;AAC1D,OAAO,EAAE,mBAAmB,EAAE,MAAM,iCAAiC,CAAC;AACtE,OAAO,EAAE,oBAAoB,EAAE,MAAM,kCAAkC,CAAC;AACxE,OAAO,EAAE,OAAO,IAAI,oBAAoB,EAAE,MAAM,kCAAkC,CAAC;AAGnF,OAAO,EAAE,YAAY,EAAE,MAAM,iCAAiC,CAAC;AAG/D,OAAO,EAAE,eAAe,EAAE,MAAM,mCAAmC,CAAC;AAGpE,OAAO,EAAE,kBAAkB,EAAE,MAAM,iCAAiC,CAAC;AACrE,OAAO,EAAE,iBAAiB,EAAE,MAAM,gCAAgC,CAAC;AACnE,OAAO,EAAE,eAAe,EAAE,MAAM,8BAA8B,CAAC;AAC/D,OAAO,EAAE,UAAU,EAAE,MAAM,yBAAyB,CAAC;AAIrD,OAAO,EAAE,GAAG,EAAE,MAAM,kBAAkB,CAAC;AACvC,OAAO,EAAE,GAAG,IAAI,WAAW,EAAE,MAAM,kBAAkB,CAAC;AAGtD,YAAY,EACV,kBAAkB,EAClB,uBAAuB,GACxB,MAAM,qCAAqC,CAAC;AAE7C,YAAY,EACV,uBAAuB,EACvB,oBAAoB,GACrB,MAAM,kCAAkC,CAAC;AAE1C,YAAY,EACV,wBAAwB,EACxB,qBAAqB,GACtB,MAAM,mCAAmC,CAAC;AAG3C,YAAY,EACV,QAAQ,EACR,qBAAqB,EACrB,oBAAoB,EACpB,gBAAgB,EAChB,YAAY,EACZ,kBAAkB,EAClB,mBAAmB,EACnB,kBAAkB,GACnB,MAAM,eAAe,CAAC;AAEvB,OAAO,EAAE,mBAAmB,EAAE,MAAM,eAAe,CAAC;AAGpD,OAAO,EAAE,OAAO,IAAI,WAAW,EAAE,MAAM,mBAAmB,CAAC;AAG3D,OAAO,EAAE,eAAe,EAAE,MAAM,oBAAoB,CAAC"}
|
package/dist/index.js
CHANGED
|
@@ -3,14 +3,31 @@ export { TextareaGenerative } from "./web/components/TextareaGenerative";
|
|
|
3
3
|
export { ImageGenerative } from "./web/components/ImageGenerative";
|
|
4
4
|
export { ButtonGenerative } from "./web/components/ButtonGenerative";
|
|
5
5
|
// Pages Auth
|
|
6
|
-
export {
|
|
6
|
+
export { UsageAndTokensPage } from "./web/auth/UsageAndTokensPage";
|
|
7
|
+
export { APIKeysPage } from "./web/auth/APIKeysPage";
|
|
8
|
+
export { AISettingsPage } from "./web/auth/AISettingsPage";
|
|
9
|
+
export { HistoryPage } from "./web/auth/HistoryPage";
|
|
10
|
+
export { PromptsPage } from "./web/auth/PromptsPage";
|
|
7
11
|
// Pages Admin
|
|
8
12
|
export { UserTokenPage } from "./web/admin/UserTokenPage";
|
|
9
13
|
export { AdminTokenPacksPage } from "./web/admin/AdminTokenPacksPage";
|
|
14
|
+
export { BillingAnalyticsPage } from "./web/admin/BillingAnalyticsPage";
|
|
15
|
+
export { default as GlobalAISettingsPage } from "./web/admin/GlobalAISettingsPage";
|
|
10
16
|
// Admin Components - User Tabs
|
|
11
17
|
export { UserTokenTab } from "./components/admin/UserTokenTab";
|
|
18
|
+
// Auth Dashboard Components
|
|
19
|
+
export { AuthDashboardAi } from "./components/auth/AuthDashboardAi";
|
|
20
|
+
// Public Pages
|
|
21
|
+
export { PurchaseTokensPage } from "./web/public/PurchaseTokensPage";
|
|
22
|
+
export { PublicPromptsPage } from "./web/public/PublicPromptsPage";
|
|
23
|
+
export { UserPromptsPage } from "./web/public/UserPromptsPage";
|
|
24
|
+
export { UserAvatar } from "./web/public/UserAvatar";
|
|
25
|
+
// PromptDetailPageServer exporté depuis server.ts (Server Component)
|
|
12
26
|
// Documentation
|
|
13
27
|
export { Doc } from "./components/Doc";
|
|
14
28
|
export { Doc as AiModuleDoc } from "./components/Doc";
|
|
29
|
+
export { PLAN_DEFAULT_QUOTAS } from "./types/quota";
|
|
15
30
|
// Configuration de build
|
|
16
31
|
export { default as buildConfig } from "./ai.build.config";
|
|
32
|
+
// Sitemap manifest
|
|
33
|
+
export { sitemapManifest } from "./sitemap/manifest";
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Migration script: Give $1 USD wallet to all users with tokens
|
|
3
|
+
*
|
|
4
|
+
* Usage: DRY_RUN=true pnpm tsx packages/module-ai/src/scripts/migrate-tokens-to-wallet.ts
|
|
5
|
+
*
|
|
6
|
+
* This script:
|
|
7
|
+
* 1. Loads environment variables from .env.local at monorepo root
|
|
8
|
+
* 2. Finds all users with any token activity
|
|
9
|
+
* 3. Gives them $1 USD wallet credit (= $0.40 real budget with 60% margin)
|
|
10
|
+
* 4. Logs all migrations for audit
|
|
11
|
+
*/
|
|
12
|
+
export {};
|
|
13
|
+
//# sourceMappingURL=migrate-tokens-to-wallet.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"migrate-tokens-to-wallet.d.ts","sourceRoot":"","sources":["../../src/scripts/migrate-tokens-to-wallet.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;GAUG"}
|
|
@@ -0,0 +1,165 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Migration script: Give $1 USD wallet to all users with tokens
|
|
3
|
+
*
|
|
4
|
+
* Usage: DRY_RUN=true pnpm tsx packages/module-ai/src/scripts/migrate-tokens-to-wallet.ts
|
|
5
|
+
*
|
|
6
|
+
* This script:
|
|
7
|
+
* 1. Loads environment variables from .env.local at monorepo root
|
|
8
|
+
* 2. Finds all users with any token activity
|
|
9
|
+
* 3. Gives them $1 USD wallet credit (= $0.40 real budget with 60% margin)
|
|
10
|
+
* 4. Logs all migrations for audit
|
|
11
|
+
*/
|
|
12
|
+
import { config } from "dotenv";
|
|
13
|
+
import { resolve } from "path";
|
|
14
|
+
import { getSupabaseServiceClient } from "@lastbrain/core/server";
|
|
15
|
+
// Charger les variables d'environnement depuis la racine du monorepo
|
|
16
|
+
const envPath = resolve(process.cwd(), ".env.local");
|
|
17
|
+
config({ path: envPath });
|
|
18
|
+
console.log(`📁 Loading environment from: ${envPath}`);
|
|
19
|
+
console.log(`🔑 SUPABASE_URL: ${process.env.NEXT_PUBLIC_SUPABASE_URL ? "✅" : "❌"}`);
|
|
20
|
+
console.log(`🔑 SERVICE_ROLE_KEY: ${process.env.SUPABASE_SERVICE_ROLE_KEY ? "✅" : "❌"}`);
|
|
21
|
+
console.log("");
|
|
22
|
+
// Configuration de migration simple
|
|
23
|
+
const CONFIG = {
|
|
24
|
+
// Crédit offert à chaque utilisateur ayant des tokens
|
|
25
|
+
WALLET_CREDIT_USD: 1.0, // $1 USD = $0.40 budget réel
|
|
26
|
+
// Marge cible (60% comme dans le nouveau système)
|
|
27
|
+
MARGIN_TARGET: 0.6,
|
|
28
|
+
// Reset complet des anciens tokens (supprime l'historique)
|
|
29
|
+
RESET_OLD_TOKENS: true,
|
|
30
|
+
// Mode dry-run (ne fait pas de modifications réelles)
|
|
31
|
+
DRY_RUN: process.env.DRY_RUN === "true",
|
|
32
|
+
};
|
|
33
|
+
async function migrateTokensToWallet() {
|
|
34
|
+
console.log("🚀 Starting token-to-wallet migration...");
|
|
35
|
+
console.log(`📊 Configuration:`);
|
|
36
|
+
console.log(` - Wallet credit: $${CONFIG.WALLET_CREDIT_USD} USD per user`);
|
|
37
|
+
console.log(` - Real budget: $${CONFIG.WALLET_CREDIT_USD * (1 - CONFIG.MARGIN_TARGET)} USD (${CONFIG.MARGIN_TARGET * 100}% margin)`);
|
|
38
|
+
console.log(` - Reset old tokens: ${CONFIG.RESET_OLD_TOKENS ? "YES (will delete old ledger entries)" : "NO"}`);
|
|
39
|
+
console.log(` - Dry run: ${CONFIG.DRY_RUN ? "YES (no changes will be made)" : "NO (will modify database)"}`);
|
|
40
|
+
console.log("");
|
|
41
|
+
const supabase = await getSupabaseServiceClient();
|
|
42
|
+
try {
|
|
43
|
+
// 1. Récupérer tous les utilisateurs avec activité tokens
|
|
44
|
+
console.log("📋 Fetching users with token activity...");
|
|
45
|
+
const { data: usersWithTokens, error: fetchError } = await supabase
|
|
46
|
+
.from("user_token_ledger")
|
|
47
|
+
.select("owner_id")
|
|
48
|
+
.order("owner_id");
|
|
49
|
+
if (fetchError)
|
|
50
|
+
throw fetchError;
|
|
51
|
+
// Dédupliquer les owner_id
|
|
52
|
+
const uniqueUserIds = Array.from(new Set(usersWithTokens?.map((u) => u.owner_id) || []));
|
|
53
|
+
console.log(`✅ Found ${uniqueUserIds.length} users with token activity`);
|
|
54
|
+
await migrateUsers(uniqueUserIds, supabase);
|
|
55
|
+
}
|
|
56
|
+
catch (error) {
|
|
57
|
+
console.error("❌ Migration failed:", error);
|
|
58
|
+
process.exit(1);
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
async function migrateUsers(userIds, supabase) {
|
|
62
|
+
let migratedCount = 0;
|
|
63
|
+
let skippedCount = 0;
|
|
64
|
+
let errorCount = 0;
|
|
65
|
+
const sellValueUsd = CONFIG.WALLET_CREDIT_USD;
|
|
66
|
+
const providerBudgetUsd = sellValueUsd * (1 - CONFIG.MARGIN_TARGET);
|
|
67
|
+
console.log(`\n💰 Wallet credit per user:`);
|
|
68
|
+
console.log(` → Sell value: $${sellValueUsd.toFixed(2)}`);
|
|
69
|
+
console.log(` → Provider budget: $${providerBudgetUsd.toFixed(2)}`);
|
|
70
|
+
console.log("");
|
|
71
|
+
for (const userId of userIds) {
|
|
72
|
+
try {
|
|
73
|
+
// Vérifier si le wallet existe déjà
|
|
74
|
+
const { data: existingWallet } = await supabase
|
|
75
|
+
.from("user_token_wallet")
|
|
76
|
+
.select("wallet_sell_value_usd")
|
|
77
|
+
.eq("user_id", userId)
|
|
78
|
+
.single();
|
|
79
|
+
if (existingWallet && existingWallet.wallet_sell_value_usd > 0) {
|
|
80
|
+
console.log(`⏭️ Skip user ${userId}: wallet already exists with $${existingWallet.wallet_sell_value_usd.toFixed(2)}`);
|
|
81
|
+
skippedCount++;
|
|
82
|
+
continue;
|
|
83
|
+
}
|
|
84
|
+
console.log(`👤 User: ${userId}`);
|
|
85
|
+
if (CONFIG.DRY_RUN) {
|
|
86
|
+
console.log(` → Would reset old tokens and create wallet with $${sellValueUsd.toFixed(2)} USD`);
|
|
87
|
+
console.log(` ⚠️ DRY RUN - no changes made`);
|
|
88
|
+
migratedCount++;
|
|
89
|
+
continue;
|
|
90
|
+
}
|
|
91
|
+
// Reset des anciens tokens (supprime tout l'historique)
|
|
92
|
+
if (CONFIG.RESET_OLD_TOKENS) {
|
|
93
|
+
const { error: deleteError, count } = await supabase
|
|
94
|
+
.from("user_token_ledger")
|
|
95
|
+
.delete()
|
|
96
|
+
.eq("owner_id", userId);
|
|
97
|
+
if (deleteError) {
|
|
98
|
+
console.log(` ❌ Delete error:`, deleteError.message);
|
|
99
|
+
errorCount++;
|
|
100
|
+
continue;
|
|
101
|
+
}
|
|
102
|
+
console.log(` 🗑️ Deleted ${count || 0} old token entries`);
|
|
103
|
+
}
|
|
104
|
+
// Créer une entrée dans le ledger pour la migration
|
|
105
|
+
const { error: ledgerError } = await supabase
|
|
106
|
+
.from("user_token_ledger")
|
|
107
|
+
.insert({
|
|
108
|
+
owner_id: userId,
|
|
109
|
+
type: "gift",
|
|
110
|
+
amount: 0, // Plus utilisé
|
|
111
|
+
sell_value_added_usd: sellValueUsd,
|
|
112
|
+
provider_budget_added_usd: providerBudgetUsd,
|
|
113
|
+
meta: {
|
|
114
|
+
reason: "migration_tokens_to_wallet",
|
|
115
|
+
migration_date: new Date().toISOString(),
|
|
116
|
+
},
|
|
117
|
+
});
|
|
118
|
+
if (ledgerError) {
|
|
119
|
+
console.log(` ❌ Ledger error:`, ledgerError.message);
|
|
120
|
+
errorCount++;
|
|
121
|
+
continue;
|
|
122
|
+
}
|
|
123
|
+
// Mettre à jour le wallet
|
|
124
|
+
const { error: walletError } = await supabase
|
|
125
|
+
.from("user_token_wallet")
|
|
126
|
+
.upsert({
|
|
127
|
+
user_id: userId,
|
|
128
|
+
wallet_provider_budget_usd: providerBudgetUsd,
|
|
129
|
+
wallet_sell_value_usd: sellValueUsd,
|
|
130
|
+
updated_at: new Date().toISOString(),
|
|
131
|
+
});
|
|
132
|
+
if (walletError) {
|
|
133
|
+
console.log(` ❌ Wallet error:`, walletError.message);
|
|
134
|
+
errorCount++;
|
|
135
|
+
continue;
|
|
136
|
+
}
|
|
137
|
+
console.log(` ✅ Migrated successfully`);
|
|
138
|
+
migratedCount++;
|
|
139
|
+
}
|
|
140
|
+
catch (error) {
|
|
141
|
+
console.error(` ❌ Error migrating user ${userId}:`, error.message);
|
|
142
|
+
errorCount++;
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
console.log("\n" + "=".repeat(60));
|
|
146
|
+
console.log("📊 Migration Summary:");
|
|
147
|
+
console.log(` ✅ Migrated: ${migratedCount} users`);
|
|
148
|
+
console.log(` ⏭️ Skipped: ${skippedCount} users`);
|
|
149
|
+
console.log(` ❌ Errors: ${errorCount} users`);
|
|
150
|
+
console.log("=".repeat(60));
|
|
151
|
+
if (CONFIG.DRY_RUN) {
|
|
152
|
+
console.log("\n⚠️ DRY RUN MODE - No changes were made to the database");
|
|
153
|
+
console.log("💡 To apply changes, run: DRY_RUN=false pnpm tsx packages/module-ai/src/scripts/migrate-tokens-to-wallet.ts");
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
// Exécuter la migration
|
|
157
|
+
migrateTokensToWallet()
|
|
158
|
+
.then(() => {
|
|
159
|
+
console.log("\n✅ Migration completed");
|
|
160
|
+
process.exit(0);
|
|
161
|
+
})
|
|
162
|
+
.catch((error) => {
|
|
163
|
+
console.error("\n❌ Migration failed:", error);
|
|
164
|
+
process.exit(1);
|
|
165
|
+
});
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"billing.test.d.ts","sourceRoot":"","sources":["../../../src/server/__tests__/billing.test.ts"],"names":[],"mappings":"AAAA;;GAEG"}
|