2025-07-19 03:30:44 +08:00
|
|
|
|
/*
|
|
|
|
|
|
Copyright (C) 2025 QuantumNous
|
|
|
|
|
|
|
|
|
|
|
|
This program is free software: you can redistribute it and/or modify
|
|
|
|
|
|
it under the terms of the GNU Affero General Public License as
|
|
|
|
|
|
published by the Free Software Foundation, either version 3 of the
|
|
|
|
|
|
License, or (at your option) any later version.
|
|
|
|
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
|
GNU Affero General Public License for more details.
|
|
|
|
|
|
|
|
|
|
|
|
You should have received a copy of the GNU Affero General Public License
|
|
|
|
|
|
along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
|
|
|
|
For commercial licensing, please contact support@quantumnous.com
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
2025-07-18 22:04:54 +08:00
|
|
|
|
import React from 'react';
|
|
|
|
|
|
import {
|
|
|
|
|
|
Avatar,
|
|
|
|
|
|
Space,
|
|
|
|
|
|
Tag,
|
|
|
|
|
|
Tooltip,
|
|
|
|
|
|
Popover,
|
2025-08-30 21:15:10 +08:00
|
|
|
|
Typography,
|
2025-07-18 22:04:54 +08:00
|
|
|
|
} from '@douyinfe/semi-ui';
|
|
|
|
|
|
import {
|
|
|
|
|
|
renderGroup,
|
|
|
|
|
|
renderQuota,
|
|
|
|
|
|
stringToColor,
|
|
|
|
|
|
getLogOther,
|
|
|
|
|
|
renderModelTag,
|
|
|
|
|
|
renderModelPriceSimple,
|
|
|
|
|
|
} from '../../../helpers';
|
2026-02-08 23:45:46 +08:00
|
|
|
|
import { IconHelpCircle } from '@douyinfe/semi-icons';
|
|
|
|
|
|
import { Route, Sparkles } from 'lucide-react';
|
2025-07-18 22:04:54 +08:00
|
|
|
|
|
|
|
|
|
|
const colors = [
|
|
|
|
|
|
'amber',
|
|
|
|
|
|
'blue',
|
|
|
|
|
|
'cyan',
|
|
|
|
|
|
'green',
|
|
|
|
|
|
'grey',
|
|
|
|
|
|
'indigo',
|
|
|
|
|
|
'light-blue',
|
|
|
|
|
|
'lime',
|
|
|
|
|
|
'orange',
|
|
|
|
|
|
'pink',
|
|
|
|
|
|
'purple',
|
|
|
|
|
|
'red',
|
|
|
|
|
|
'teal',
|
|
|
|
|
|
'violet',
|
|
|
|
|
|
'yellow',
|
|
|
|
|
|
];
|
|
|
|
|
|
|
2026-01-26 20:20:30 +08:00
|
|
|
|
function formatRatio(ratio) {
|
|
|
|
|
|
if (ratio === undefined || ratio === null) {
|
|
|
|
|
|
return '-';
|
|
|
|
|
|
}
|
|
|
|
|
|
if (typeof ratio === 'number') {
|
|
|
|
|
|
return ratio.toFixed(4);
|
|
|
|
|
|
}
|
|
|
|
|
|
return String(ratio);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-02-02 14:37:31 +08:00
|
|
|
|
function buildChannelAffinityTooltip(affinity, t) {
|
|
|
|
|
|
if (!affinity) {
|
|
|
|
|
|
return null;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
const keySource = affinity.key_source || '-';
|
|
|
|
|
|
const keyPath = affinity.key_path || affinity.key_key || '-';
|
|
|
|
|
|
const keyHint = affinity.key_hint || '';
|
|
|
|
|
|
const keyFp = affinity.key_fp ? `#${affinity.key_fp}` : '';
|
|
|
|
|
|
const keyText = `${keySource}:${keyPath}${keyFp}`;
|
|
|
|
|
|
|
|
|
|
|
|
const lines = [
|
|
|
|
|
|
t('渠道亲和性'),
|
|
|
|
|
|
`${t('规则')}:${affinity.rule_name || '-'}`,
|
|
|
|
|
|
`${t('分组')}:${affinity.selected_group || '-'}`,
|
|
|
|
|
|
`${t('Key')}:${keyText}`,
|
|
|
|
|
|
...(keyHint ? [`${t('Key 摘要')}:${keyHint}`] : []),
|
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
|
<div style={{ lineHeight: 1.6, display: 'flex', flexDirection: 'column' }}>
|
|
|
|
|
|
{lines.map((line, i) => (
|
|
|
|
|
|
<div key={i}>{line}</div>
|
|
|
|
|
|
))}
|
|
|
|
|
|
</div>
|
|
|
|
|
|
);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-07-18 22:04:54 +08:00
|
|
|
|
// Render functions
|
|
|
|
|
|
function renderType(type, t) {
|
|
|
|
|
|
switch (type) {
|
|
|
|
|
|
case 1:
|
|
|
|
|
|
return (
|
|
|
|
|
|
<Tag color='cyan' shape='circle'>
|
|
|
|
|
|
{t('充值')}
|
|
|
|
|
|
</Tag>
|
|
|
|
|
|
);
|
|
|
|
|
|
case 2:
|
|
|
|
|
|
return (
|
|
|
|
|
|
<Tag color='lime' shape='circle'>
|
|
|
|
|
|
{t('消费')}
|
|
|
|
|
|
</Tag>
|
|
|
|
|
|
);
|
|
|
|
|
|
case 3:
|
|
|
|
|
|
return (
|
|
|
|
|
|
<Tag color='orange' shape='circle'>
|
|
|
|
|
|
{t('管理')}
|
|
|
|
|
|
</Tag>
|
|
|
|
|
|
);
|
|
|
|
|
|
case 4:
|
|
|
|
|
|
return (
|
|
|
|
|
|
<Tag color='purple' shape='circle'>
|
|
|
|
|
|
{t('系统')}
|
|
|
|
|
|
</Tag>
|
|
|
|
|
|
);
|
|
|
|
|
|
case 5:
|
|
|
|
|
|
return (
|
|
|
|
|
|
<Tag color='red' shape='circle'>
|
|
|
|
|
|
{t('错误')}
|
|
|
|
|
|
</Tag>
|
|
|
|
|
|
);
|
2026-02-21 22:48:30 +08:00
|
|
|
|
case 6:
|
|
|
|
|
|
return (
|
|
|
|
|
|
<Tag color='teal' shape='circle'>
|
|
|
|
|
|
{t('退款')}
|
|
|
|
|
|
</Tag>
|
|
|
|
|
|
);
|
2025-07-18 22:04:54 +08:00
|
|
|
|
default:
|
|
|
|
|
|
return (
|
|
|
|
|
|
<Tag color='grey' shape='circle'>
|
|
|
|
|
|
{t('未知')}
|
|
|
|
|
|
</Tag>
|
|
|
|
|
|
);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function renderIsStream(bool, t) {
|
|
|
|
|
|
if (bool) {
|
|
|
|
|
|
return (
|
|
|
|
|
|
<Tag color='blue' shape='circle'>
|
|
|
|
|
|
{t('流')}
|
|
|
|
|
|
</Tag>
|
|
|
|
|
|
);
|
|
|
|
|
|
} else {
|
|
|
|
|
|
return (
|
|
|
|
|
|
<Tag color='purple' shape='circle'>
|
|
|
|
|
|
{t('非流')}
|
|
|
|
|
|
</Tag>
|
|
|
|
|
|
);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function renderUseTime(type, t) {
|
|
|
|
|
|
const time = parseInt(type);
|
|
|
|
|
|
if (time < 101) {
|
|
|
|
|
|
return (
|
|
|
|
|
|
<Tag color='green' shape='circle'>
|
|
|
|
|
|
{' '}
|
|
|
|
|
|
{time} s{' '}
|
|
|
|
|
|
</Tag>
|
|
|
|
|
|
);
|
|
|
|
|
|
} else if (time < 300) {
|
|
|
|
|
|
return (
|
|
|
|
|
|
<Tag color='orange' shape='circle'>
|
|
|
|
|
|
{' '}
|
|
|
|
|
|
{time} s{' '}
|
|
|
|
|
|
</Tag>
|
|
|
|
|
|
);
|
|
|
|
|
|
} else {
|
|
|
|
|
|
return (
|
|
|
|
|
|
<Tag color='red' shape='circle'>
|
|
|
|
|
|
{' '}
|
|
|
|
|
|
{time} s{' '}
|
|
|
|
|
|
</Tag>
|
|
|
|
|
|
);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function renderFirstUseTime(type, t) {
|
|
|
|
|
|
let time = parseFloat(type) / 1000.0;
|
|
|
|
|
|
time = time.toFixed(1);
|
|
|
|
|
|
if (time < 3) {
|
|
|
|
|
|
return (
|
|
|
|
|
|
<Tag color='green' shape='circle'>
|
|
|
|
|
|
{' '}
|
|
|
|
|
|
{time} s{' '}
|
|
|
|
|
|
</Tag>
|
|
|
|
|
|
);
|
|
|
|
|
|
} else if (time < 10) {
|
|
|
|
|
|
return (
|
|
|
|
|
|
<Tag color='orange' shape='circle'>
|
|
|
|
|
|
{' '}
|
|
|
|
|
|
{time} s{' '}
|
|
|
|
|
|
</Tag>
|
|
|
|
|
|
);
|
|
|
|
|
|
} else {
|
|
|
|
|
|
return (
|
|
|
|
|
|
<Tag color='red' shape='circle'>
|
|
|
|
|
|
{' '}
|
|
|
|
|
|
{time} s{' '}
|
|
|
|
|
|
</Tag>
|
|
|
|
|
|
);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
✨ feat: add subscription billing system (#2808)
* ci: create docker automation
* ✨ feat: add subscription billing system with admin management and user purchase flow
Implement a new subscription-based billing model alongside existing metered/per-request billing:
Backend:
- Add subscription plan models (SubscriptionPlan, SubscriptionPlanItem, UserSubscription, etc.)
- Implement CRUD APIs for subscription plan management (admin only)
- Add user subscription queries with support for multiple active/expired subscriptions
- Integrate payment gateways (Stripe, Creem, Epay) for subscription purchases
- Implement pre-consume and post-consume billing logic for subscription quota tracking
- Add billing preference settings (subscription_first, wallet_first, etc.)
- Enhance usage logs with subscription deduction details
Frontend - Admin:
- Add subscription management page with table view and drawer-based edit form
- Match UI/UX style with existing admin pages (redemption codes, users)
- Support enabling/disabling plans, configuring payment IDs, and model quotas
- Add user subscription binding modal in user management
Frontend - Wallet:
- Add subscription plans card with current subscription status display
- Show all subscriptions (active and expired) with remaining days/usage percentage
- Display purchasable plans with pricing cards following SaaS best practices
- Extract purchase modal to separate component matching payment confirm modal style
- Add skeleton loading states with active animation
- Implement billing preference selector in card header
- Handle payment gateway availability based on admin configuration
Frontend - Usage Logs:
- Display subscription deduction details in log entries
- Show step-by-step breakdown of subscription usage (pre-consumed, delta, final, remaining)
- Add subscription deduction tag for subscription-covered requests
* ✨ feat(admin): add user subscription management and refine UI/pagination
Add admin APIs to list/create/invalidate/delete user subscriptions
Add model helpers to fetch all user subscriptions (incl. expired) and support cancel/hard-delete
Wire new admin routes for user subscription operations
Replace “Bind subscription plan” entry with a dedicated User Subscriptions SideSheet in Users table
Use CardTable with responsive layout and working client-side pagination inside the SideSheet
Improve subscription purchase modal empty-gateway state with a Banner notice
* ✨ feat(admin): streamline subscription plan benefits editor with bulk actions
Restore the avatar/icon header for the “Model Benefits” section
Replace scattered controls with a compact toolbar-style workflow
Support multi-select add with a default quota for new items
Add row selection with bulk apply-to-selected / apply-to-all quota updates
Enable delete-selected to manage benefits faster and reduce mistakes
* ✨ fix(subscription): finalize payments, log billing, and clean up dead code
Complete subscription orders by creating a matching top-up record and writing billing logs
Add Epay return handler to verify and finalize browser callbacks
Require Stripe/Creem webhook configuration before starting subscription payments
Show subscription purchases in topup history with clearer labels/methods
Remove unused subscription helper, legacy Creem webhook struct, and unused topup fields
Simplify subscription self API payload to active/all lists only
* 🎨 style: format all code with gofmt and lint:fix
Apply consistent code formatting across the entire codebase using
gofmt and lint:fix tools. This ensures adherence to Go community
standards and improves code readability and maintainability.
Changes include:
- Run gofmt on all .go files to standardize formatting
- Apply lint:fix to automatically resolve linting issues
- Fix code style inconsistencies and formatting violations
No functional changes were made in this commit.
* ✨ feat(subscription): add quota reset periods and admin configuration
- Add reset period fields on subscription plans and user items
- Apply automatic quota resets during pre-consume based on plan schedule
- Expose reset-period configuration in the admin plan editor
- Display reset cadence in subscription cards and purchase modal
- Validate custom reset seconds on plan create/update
* ✨ feat(subscription): harden subscription billing with resets, idempotency, and production-grade stability
Add plan-level quota reset periods and display/reset cadence in admin/UI
Enforce natural reset alignment with background reset task and cleanup job
Make subscription pre-consume/refund idempotent with request-scoped records and retries
Use database time for consistent resets across multi-instance deployments
Harden payment callbacks with locking and idempotent order completion
Record subscription purchases in topup history and billing logs
Optimize subscription queries and add critical composite indexes
* ✨ feat(subscription): cache plan lookups and stabilize pre-consume
Introduce hybrid caches for subscription plans, items, and plan info with explicit
invalidation on admin updates. Streamline pre-consume transactions to reduce
redundant queries while preserving idempotency and reset logic.
* 🐛 fix(subscription): avoid pre-consume lookup noise
Use a RowsAffected check for the idempotency lookup so missing records
no longer surface as "record not found" errors while preserving behavior.
* 🔧 ci: Change workflow trigger to sub branch
Update the Docker image workflow to run on pushes to the sub branch instead of main.
* 💸 chore: Align subscription pricing display with global currency settings
Unify subscription price rendering to use the site-wide currency symbol/rate on the wallet and admin views.
Make subscription plan currency read-only in the editor and force USD on create/update to avoid drift.
Use global currency display type when creating Creem checkout payloads.
* 🔧 chore: Unify subscription plan status toggle with PATCH endpoint
Replace separate enable/disable flows with a single PATCH API that updates the enabled flag.
Update frontend hooks and table actions to call the unified endpoint and keep UI behavior consistent.
Introduce a minimal admin controller handler and route for the status update.
* ✨ feat: Add subscription limits and UI tags consistency
Add per-plan purchase limits with backend enforcement and UI disable states.
Expose limit configuration in admin plan editor and show limits in plan tables/cards.
Refine subscription UI tags with unified badge style and streamlined “My Subscriptions” layout.
* 🎨 style: tag color to white
* 🚀 refactor: Simplify subscription quota to total amount model
Remove per-model subscription items and switch to a single total quota per plan and user subscription. Update billing, reset, and logging flows to operate on total quota, and refactor admin/user UI to configure and display total quota consistently.
* 🚀 chore: Remove duplicate subscription usage percentage display
Keep the usage percentage shown only in the total quota line to avoid redundant “已用 0%” text while preserving remaining days in the summary.
* ✨ feat: Add subscription upgrade group with auto downgrade
* ✨ feat: Update subscription purchase modal display
Show total quota as currency with tooltip for raw quota, hide reset cycle when never, and display upgrade group when configured to match card display rules.
* ✨ feat: Extract quota conversion helpers to shared utils
Move quota display/conversion helpers into web/src/helpers/quota.js and update the subscription plan editor to import and use the shared utilities instead of inline functions.
* ✨ chore: Add upgrade group guidance in subscription editor
Add explanatory helper text under the upgrade group field to clarify automatic group upgrades, rollback conditions, and the expected delay before downgrading takes effect.
* 🔧 chore: remove unused Creem settings state
Drop the unused originInputs state and redundant updates to keep the Creem
settings form state minimal and easier to maintain.
* 🚀 chore: Remove useless action
* ✨ Add full i18n coverage for subscription-related UI across locales
* ✨ feat: harden subscription billing and improve UI consistency
Improve subscription payment safety and data integrity by handling user/URL lookup failures, fixing Stripe subscription mode, persisting quota reset fields, and correcting subscription delta accounting and DB timestamp casting. Refine the UI with stricter custom duration validation, accurate currency rounding, conditional Epay labeling, rollback on preference update failure, and shared subscription formatting helpers plus clearer component naming.
* 🔧 fix: make epay webhook and return flow subscription-aware
Ensure Epay webhook acknowledges success only after order completion, returning fail on processing errors to allow retries. Redirect subscription payment returns to the subscription page instead of top-up for correct user flow.
* 🚦 fix: guard epay return success on order completion
Redirect subscription return flow to failure when order completion fails, preventing false success states after payment verification.
* 🔧 fix: normalize epay error handling and webhook retries
Standardize SubscriptionRequestEpay error responses via ApiErrorMsg for a consistent schema.
Return "fail" on non-success trade statuses in the epay webhook to preserve retry behavior.
* 🧾 fix: persist epay orders before purchase
Create the subscription order before initiating epay payment and expire it if the provider call fails, preventing orphaned transactions and improving reconciliation.
* 🔧 fix: harden epay callbacks and billing fallbacks
Use POST and form parsing for epay notify/return routes, persist epay orders before provider calls with expiry on failure, and ensure notify handlers retry correctly.
Restrict subscription-first fallback to insufficient-subscription errors and log refund failures after retries to avoid silent quota drift.
* 🔧 fix: harden billing flow and sidebar settings
Add missing strings import for subscription fallback checks, log failed subscription refunds after retries, and extend sidebar module settings with a subscription management toggle plus translations.
* 🛡️ fix: fail fast on epay form parse errors
Handle ParseForm errors in epay notify/return handlers by returning fail or redirecting to failure, avoiding unsafe fallback to query parameters.
* ✨ fix: refine Japanese subscription status labels
Adjust Japanese UI wording for active-count labels to read more naturally and consistently.
* ✅ fix: standardize epay success response schema
Return subscription epay pay success responses via ApiSuccess to include the consistent success field and align with error schema.
2026-02-03 17:40:43 +08:00
|
|
|
|
function renderBillingTag(record, t) {
|
|
|
|
|
|
const other = getLogOther(record.other);
|
|
|
|
|
|
if (other?.billing_source === 'subscription') {
|
|
|
|
|
|
return (
|
|
|
|
|
|
<Tag color='green' shape='circle'>
|
|
|
|
|
|
{t('订阅抵扣')}
|
|
|
|
|
|
</Tag>
|
|
|
|
|
|
);
|
|
|
|
|
|
}
|
|
|
|
|
|
return null;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-07-18 22:04:54 +08:00
|
|
|
|
function renderModelName(record, copyText, t) {
|
|
|
|
|
|
let other = getLogOther(record.other);
|
|
|
|
|
|
let modelMapped =
|
|
|
|
|
|
other?.is_model_mapped &&
|
|
|
|
|
|
other?.upstream_model_name &&
|
|
|
|
|
|
other?.upstream_model_name !== '';
|
|
|
|
|
|
if (!modelMapped) {
|
|
|
|
|
|
return renderModelTag(record.model_name, {
|
|
|
|
|
|
onClick: (event) => {
|
2025-08-30 21:15:10 +08:00
|
|
|
|
copyText(event, record.model_name).then((r) => {});
|
2025-07-18 22:04:54 +08:00
|
|
|
|
},
|
|
|
|
|
|
});
|
|
|
|
|
|
} else {
|
|
|
|
|
|
return (
|
|
|
|
|
|
<>
|
|
|
|
|
|
<Space vertical align={'start'}>
|
|
|
|
|
|
<Popover
|
|
|
|
|
|
content={
|
|
|
|
|
|
<div style={{ padding: 10 }}>
|
|
|
|
|
|
<Space vertical align={'start'}>
|
|
|
|
|
|
<div className='flex items-center'>
|
|
|
|
|
|
<Typography.Text strong style={{ marginRight: 8 }}>
|
|
|
|
|
|
{t('请求并计费模型')}:
|
|
|
|
|
|
</Typography.Text>
|
|
|
|
|
|
{renderModelTag(record.model_name, {
|
|
|
|
|
|
onClick: (event) => {
|
2025-08-30 21:15:10 +08:00
|
|
|
|
copyText(event, record.model_name).then((r) => {});
|
2025-07-18 22:04:54 +08:00
|
|
|
|
},
|
|
|
|
|
|
})}
|
|
|
|
|
|
</div>
|
|
|
|
|
|
<div className='flex items-center'>
|
|
|
|
|
|
<Typography.Text strong style={{ marginRight: 8 }}>
|
|
|
|
|
|
{t('实际模型')}:
|
|
|
|
|
|
</Typography.Text>
|
|
|
|
|
|
{renderModelTag(other.upstream_model_name, {
|
|
|
|
|
|
onClick: (event) => {
|
|
|
|
|
|
copyText(event, other.upstream_model_name).then(
|
2025-08-30 21:15:10 +08:00
|
|
|
|
(r) => {},
|
2025-07-18 22:04:54 +08:00
|
|
|
|
);
|
|
|
|
|
|
},
|
|
|
|
|
|
})}
|
|
|
|
|
|
</div>
|
|
|
|
|
|
</Space>
|
|
|
|
|
|
</div>
|
|
|
|
|
|
}
|
|
|
|
|
|
>
|
|
|
|
|
|
{renderModelTag(record.model_name, {
|
|
|
|
|
|
onClick: (event) => {
|
2025-08-30 21:15:10 +08:00
|
|
|
|
copyText(event, record.model_name).then((r) => {});
|
2025-07-18 22:04:54 +08:00
|
|
|
|
},
|
|
|
|
|
|
suffixIcon: (
|
|
|
|
|
|
<Route
|
|
|
|
|
|
style={{ width: '0.9em', height: '0.9em', opacity: 0.75 }}
|
|
|
|
|
|
/>
|
|
|
|
|
|
),
|
|
|
|
|
|
})}
|
|
|
|
|
|
</Popover>
|
|
|
|
|
|
</Space>
|
|
|
|
|
|
</>
|
|
|
|
|
|
);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-02-11 21:50:39 +08:00
|
|
|
|
function toTokenNumber(value) {
|
|
|
|
|
|
const parsed = Number(value);
|
|
|
|
|
|
if (!Number.isFinite(parsed) || parsed <= 0) {
|
|
|
|
|
|
return 0;
|
|
|
|
|
|
}
|
|
|
|
|
|
return parsed;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function formatTokenCount(value) {
|
|
|
|
|
|
return toTokenNumber(value).toLocaleString();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function getPromptCacheSummary(other) {
|
|
|
|
|
|
if (!other || typeof other !== 'object') {
|
|
|
|
|
|
return null;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
const cacheReadTokens = toTokenNumber(other.cache_tokens);
|
|
|
|
|
|
const cacheCreationTokens = toTokenNumber(other.cache_creation_tokens);
|
|
|
|
|
|
const cacheCreationTokens5m = toTokenNumber(other.cache_creation_tokens_5m);
|
|
|
|
|
|
const cacheCreationTokens1h = toTokenNumber(other.cache_creation_tokens_1h);
|
|
|
|
|
|
|
|
|
|
|
|
const hasSplitCacheCreation =
|
|
|
|
|
|
cacheCreationTokens5m > 0 || cacheCreationTokens1h > 0;
|
|
|
|
|
|
const cacheWriteTokens = hasSplitCacheCreation
|
|
|
|
|
|
? cacheCreationTokens5m + cacheCreationTokens1h
|
|
|
|
|
|
: cacheCreationTokens;
|
|
|
|
|
|
|
|
|
|
|
|
if (cacheReadTokens <= 0 && cacheWriteTokens <= 0) {
|
|
|
|
|
|
return null;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
|
cacheReadTokens,
|
|
|
|
|
|
cacheWriteTokens,
|
|
|
|
|
|
};
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-03-14 19:05:23 +08:00
|
|
|
|
function normalizeDetailText(detail) {
|
|
|
|
|
|
return String(detail || '')
|
|
|
|
|
|
.replace(/\n\r/g, '\n')
|
|
|
|
|
|
.replace(/\r\n/g, '\n');
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function getUsageLogGroupSummary(groupRatio, userGroupRatio, t) {
|
|
|
|
|
|
const parsedUserGroupRatio = Number(userGroupRatio);
|
|
|
|
|
|
const useUserGroupRatio =
|
|
|
|
|
|
Number.isFinite(parsedUserGroupRatio) && parsedUserGroupRatio !== -1;
|
|
|
|
|
|
const ratio = useUserGroupRatio ? userGroupRatio : groupRatio;
|
|
|
|
|
|
if (ratio === undefined || ratio === null || ratio === '') {
|
|
|
|
|
|
return '';
|
|
|
|
|
|
}
|
|
|
|
|
|
return `${useUserGroupRatio ? t('专属倍率') : t('分组')} ${formatRatio(ratio)}x`;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function renderCompactDetailSummary(summarySegments) {
|
|
|
|
|
|
const segments = Array.isArray(summarySegments)
|
|
|
|
|
|
? summarySegments.filter((segment) => segment?.text)
|
|
|
|
|
|
: [];
|
|
|
|
|
|
if (!segments.length) {
|
|
|
|
|
|
return null;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
|
<div
|
|
|
|
|
|
style={{
|
|
|
|
|
|
maxWidth: 180,
|
|
|
|
|
|
lineHeight: 1.35,
|
|
|
|
|
|
}}
|
|
|
|
|
|
>
|
|
|
|
|
|
{segments.map((segment, index) => (
|
|
|
|
|
|
<Typography.Text
|
|
|
|
|
|
key={`${segment.text}-${index}`}
|
|
|
|
|
|
type={segment.tone === 'secondary' ? 'tertiary' : undefined}
|
|
|
|
|
|
size={segment.tone === 'secondary' ? 'small' : undefined}
|
|
|
|
|
|
style={{
|
|
|
|
|
|
display: 'block',
|
|
|
|
|
|
maxWidth: '100%',
|
|
|
|
|
|
fontSize: 12,
|
|
|
|
|
|
marginTop: index === 0 ? 0 : 2,
|
|
|
|
|
|
whiteSpace: 'nowrap',
|
|
|
|
|
|
overflow: 'hidden',
|
|
|
|
|
|
textOverflow: 'ellipsis',
|
|
|
|
|
|
}}
|
|
|
|
|
|
>
|
|
|
|
|
|
{segment.text}
|
|
|
|
|
|
</Typography.Text>
|
|
|
|
|
|
))}
|
|
|
|
|
|
</div>
|
|
|
|
|
|
);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function getUsageLogDetailSummary(record, text, billingDisplayMode, t) {
|
|
|
|
|
|
const other = getLogOther(record.other);
|
|
|
|
|
|
|
|
|
|
|
|
if (record.type === 6) {
|
|
|
|
|
|
return {
|
|
|
|
|
|
segments: [{ text: t('异步任务退款'), tone: 'primary' }],
|
|
|
|
|
|
};
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (other == null || record.type !== 2) {
|
|
|
|
|
|
return null;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (
|
|
|
|
|
|
other?.violation_fee === true ||
|
|
|
|
|
|
Boolean(other?.violation_fee_code) ||
|
|
|
|
|
|
Boolean(other?.violation_fee_marker)
|
|
|
|
|
|
) {
|
|
|
|
|
|
const feeQuota = other?.fee_quota ?? record?.quota;
|
|
|
|
|
|
const groupText = getUsageLogGroupSummary(
|
|
|
|
|
|
other?.group_ratio,
|
|
|
|
|
|
other?.user_group_ratio,
|
|
|
|
|
|
t,
|
|
|
|
|
|
);
|
|
|
|
|
|
return {
|
|
|
|
|
|
segments: [
|
|
|
|
|
|
groupText ? { text: groupText, tone: 'primary' } : null,
|
|
|
|
|
|
{ text: t('违规扣费'), tone: 'primary' },
|
|
|
|
|
|
{
|
|
|
|
|
|
text: `${t('扣费')}:${renderQuota(feeQuota, 6)}`,
|
|
|
|
|
|
tone: 'secondary',
|
|
|
|
|
|
},
|
|
|
|
|
|
text ? { text: `${t('详情')}:${text}`, tone: 'secondary' } : null,
|
|
|
|
|
|
].filter(Boolean),
|
|
|
|
|
|
};
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
|
segments: other?.claude
|
|
|
|
|
|
? renderModelPriceSimple(
|
|
|
|
|
|
other.model_ratio,
|
|
|
|
|
|
other.model_price,
|
|
|
|
|
|
other.group_ratio,
|
|
|
|
|
|
other?.user_group_ratio,
|
|
|
|
|
|
other.cache_tokens || 0,
|
|
|
|
|
|
other.cache_ratio || 1.0,
|
|
|
|
|
|
other.cache_creation_tokens || 0,
|
|
|
|
|
|
other.cache_creation_ratio || 1.0,
|
|
|
|
|
|
other.cache_creation_tokens_5m || 0,
|
|
|
|
|
|
other.cache_creation_ratio_5m || other.cache_creation_ratio || 1.0,
|
|
|
|
|
|
other.cache_creation_tokens_1h || 0,
|
|
|
|
|
|
other.cache_creation_ratio_1h || other.cache_creation_ratio || 1.0,
|
|
|
|
|
|
false,
|
|
|
|
|
|
1.0,
|
|
|
|
|
|
other?.is_system_prompt_overwritten,
|
|
|
|
|
|
'claude',
|
|
|
|
|
|
billingDisplayMode,
|
|
|
|
|
|
'segments',
|
|
|
|
|
|
)
|
|
|
|
|
|
: renderModelPriceSimple(
|
|
|
|
|
|
other.model_ratio,
|
|
|
|
|
|
other.model_price,
|
|
|
|
|
|
other.group_ratio,
|
|
|
|
|
|
other?.user_group_ratio,
|
|
|
|
|
|
other.cache_tokens || 0,
|
|
|
|
|
|
other.cache_ratio || 1.0,
|
|
|
|
|
|
0,
|
|
|
|
|
|
1.0,
|
|
|
|
|
|
0,
|
|
|
|
|
|
1.0,
|
|
|
|
|
|
0,
|
|
|
|
|
|
1.0,
|
|
|
|
|
|
false,
|
|
|
|
|
|
1.0,
|
|
|
|
|
|
other?.is_system_prompt_overwritten,
|
|
|
|
|
|
'openai',
|
|
|
|
|
|
billingDisplayMode,
|
|
|
|
|
|
'segments',
|
|
|
|
|
|
),
|
|
|
|
|
|
};
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-07-18 22:04:54 +08:00
|
|
|
|
export const getLogsColumns = ({
|
|
|
|
|
|
t,
|
|
|
|
|
|
COLUMN_KEYS,
|
|
|
|
|
|
copyText,
|
|
|
|
|
|
showUserInfoFunc,
|
2026-02-02 14:37:31 +08:00
|
|
|
|
openChannelAffinityUsageCacheModal,
|
2025-07-18 22:04:54 +08:00
|
|
|
|
isAdminUser,
|
2026-03-06 23:35:17 +08:00
|
|
|
|
billingDisplayMode = 'price',
|
2025-07-18 22:04:54 +08:00
|
|
|
|
}) => {
|
|
|
|
|
|
return [
|
|
|
|
|
|
{
|
|
|
|
|
|
key: COLUMN_KEYS.TIME,
|
|
|
|
|
|
title: t('时间'),
|
|
|
|
|
|
dataIndex: 'timestamp2string',
|
|
|
|
|
|
},
|
|
|
|
|
|
{
|
|
|
|
|
|
key: COLUMN_KEYS.CHANNEL,
|
|
|
|
|
|
title: t('渠道'),
|
|
|
|
|
|
dataIndex: 'channel',
|
|
|
|
|
|
render: (text, record, index) => {
|
|
|
|
|
|
let isMultiKey = false;
|
|
|
|
|
|
let multiKeyIndex = -1;
|
2026-02-08 23:45:46 +08:00
|
|
|
|
let content = t('渠道') + `:${record.channel}`;
|
|
|
|
|
|
let affinity = null;
|
|
|
|
|
|
let showMarker = false;
|
2025-07-18 22:04:54 +08:00
|
|
|
|
let other = getLogOther(record.other);
|
|
|
|
|
|
if (other?.admin_info) {
|
|
|
|
|
|
let adminInfo = other.admin_info;
|
|
|
|
|
|
if (adminInfo?.is_multi_key) {
|
|
|
|
|
|
isMultiKey = true;
|
|
|
|
|
|
multiKeyIndex = adminInfo.multi_key_index;
|
|
|
|
|
|
}
|
2026-02-08 23:45:46 +08:00
|
|
|
|
if (
|
|
|
|
|
|
Array.isArray(adminInfo.use_channel) &&
|
|
|
|
|
|
adminInfo.use_channel.length > 0
|
|
|
|
|
|
) {
|
|
|
|
|
|
content = t('渠道') + `:${adminInfo.use_channel.join('->')}`;
|
|
|
|
|
|
}
|
|
|
|
|
|
if (adminInfo.channel_affinity) {
|
|
|
|
|
|
affinity = adminInfo.channel_affinity;
|
|
|
|
|
|
showMarker = true;
|
|
|
|
|
|
}
|
2025-07-18 22:04:54 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-08-30 21:15:10 +08:00
|
|
|
|
return isAdminUser &&
|
2026-03-14 19:05:23 +08:00
|
|
|
|
(record.type === 0 ||
|
|
|
|
|
|
record.type === 2 ||
|
|
|
|
|
|
record.type === 5 ||
|
|
|
|
|
|
record.type === 6) ? (
|
2025-07-18 22:04:54 +08:00
|
|
|
|
<Space>
|
2026-02-08 23:45:46 +08:00
|
|
|
|
<span style={{ position: 'relative', display: 'inline-block' }}>
|
|
|
|
|
|
<Tooltip content={record.channel_name || t('未知渠道')}>
|
|
|
|
|
|
<span>
|
|
|
|
|
|
<Tag
|
|
|
|
|
|
color={colors[parseInt(text) % colors.length]}
|
|
|
|
|
|
shape='circle'
|
|
|
|
|
|
>
|
|
|
|
|
|
{text}
|
|
|
|
|
|
</Tag>
|
|
|
|
|
|
</span>
|
|
|
|
|
|
</Tooltip>
|
|
|
|
|
|
{showMarker && (
|
|
|
|
|
|
<Tooltip
|
|
|
|
|
|
content={
|
|
|
|
|
|
<div style={{ lineHeight: 1.6 }}>
|
|
|
|
|
|
<div>{content}</div>
|
|
|
|
|
|
{affinity ? (
|
|
|
|
|
|
<div style={{ marginTop: 6 }}>
|
|
|
|
|
|
{buildChannelAffinityTooltip(affinity, t)}
|
|
|
|
|
|
</div>
|
|
|
|
|
|
) : null}
|
|
|
|
|
|
</div>
|
|
|
|
|
|
}
|
2025-07-19 15:05:31 +08:00
|
|
|
|
>
|
2026-02-08 23:45:46 +08:00
|
|
|
|
<span
|
|
|
|
|
|
style={{
|
|
|
|
|
|
position: 'absolute',
|
|
|
|
|
|
right: -4,
|
|
|
|
|
|
top: -4,
|
|
|
|
|
|
lineHeight: 1,
|
|
|
|
|
|
fontWeight: 600,
|
|
|
|
|
|
color: '#f59e0b',
|
|
|
|
|
|
cursor: 'pointer',
|
|
|
|
|
|
userSelect: 'none',
|
|
|
|
|
|
}}
|
|
|
|
|
|
onClick={(e) => {
|
|
|
|
|
|
e.stopPropagation();
|
|
|
|
|
|
openChannelAffinityUsageCacheModal?.(affinity);
|
|
|
|
|
|
}}
|
|
|
|
|
|
>
|
|
|
|
|
|
<Sparkles
|
|
|
|
|
|
size={14}
|
|
|
|
|
|
strokeWidth={2}
|
|
|
|
|
|
color='currentColor'
|
|
|
|
|
|
fill='currentColor'
|
|
|
|
|
|
/>
|
|
|
|
|
|
</span>
|
|
|
|
|
|
</Tooltip>
|
|
|
|
|
|
)}
|
|
|
|
|
|
</span>
|
2025-07-18 22:04:54 +08:00
|
|
|
|
{isMultiKey && (
|
|
|
|
|
|
<Tag color='white' shape='circle'>
|
|
|
|
|
|
{multiKeyIndex}
|
|
|
|
|
|
</Tag>
|
|
|
|
|
|
)}
|
|
|
|
|
|
</Space>
|
|
|
|
|
|
) : null;
|
|
|
|
|
|
},
|
|
|
|
|
|
},
|
|
|
|
|
|
{
|
|
|
|
|
|
key: COLUMN_KEYS.USERNAME,
|
|
|
|
|
|
title: t('用户'),
|
|
|
|
|
|
dataIndex: 'username',
|
|
|
|
|
|
render: (text, record, index) => {
|
|
|
|
|
|
return isAdminUser ? (
|
|
|
|
|
|
<div>
|
|
|
|
|
|
<Avatar
|
|
|
|
|
|
size='extra-small'
|
|
|
|
|
|
color={stringToColor(text)}
|
|
|
|
|
|
style={{ marginRight: 4 }}
|
|
|
|
|
|
onClick={(event) => {
|
|
|
|
|
|
event.stopPropagation();
|
|
|
|
|
|
showUserInfoFunc(record.user_id);
|
|
|
|
|
|
}}
|
|
|
|
|
|
>
|
|
|
|
|
|
{typeof text === 'string' && text.slice(0, 1)}
|
|
|
|
|
|
</Avatar>
|
|
|
|
|
|
{text}
|
|
|
|
|
|
</div>
|
|
|
|
|
|
) : (
|
|
|
|
|
|
<></>
|
|
|
|
|
|
);
|
|
|
|
|
|
},
|
|
|
|
|
|
},
|
|
|
|
|
|
{
|
|
|
|
|
|
key: COLUMN_KEYS.TOKEN,
|
|
|
|
|
|
title: t('令牌'),
|
|
|
|
|
|
dataIndex: 'token_name',
|
|
|
|
|
|
render: (text, record, index) => {
|
2026-03-14 19:05:23 +08:00
|
|
|
|
return record.type === 0 ||
|
|
|
|
|
|
record.type === 2 ||
|
|
|
|
|
|
record.type === 5 ||
|
|
|
|
|
|
record.type === 6 ? (
|
2025-07-18 22:04:54 +08:00
|
|
|
|
<div>
|
|
|
|
|
|
<Tag
|
|
|
|
|
|
color='grey'
|
|
|
|
|
|
shape='circle'
|
|
|
|
|
|
onClick={(event) => {
|
|
|
|
|
|
copyText(event, text);
|
|
|
|
|
|
}}
|
|
|
|
|
|
>
|
|
|
|
|
|
{' '}
|
|
|
|
|
|
{t(text)}{' '}
|
|
|
|
|
|
</Tag>
|
|
|
|
|
|
</div>
|
|
|
|
|
|
) : (
|
|
|
|
|
|
<></>
|
|
|
|
|
|
);
|
|
|
|
|
|
},
|
|
|
|
|
|
},
|
|
|
|
|
|
{
|
|
|
|
|
|
key: COLUMN_KEYS.GROUP,
|
|
|
|
|
|
title: t('分组'),
|
|
|
|
|
|
dataIndex: 'group',
|
|
|
|
|
|
render: (text, record, index) => {
|
2026-03-14 19:05:23 +08:00
|
|
|
|
if (
|
|
|
|
|
|
record.type === 0 ||
|
|
|
|
|
|
record.type === 2 ||
|
|
|
|
|
|
record.type === 5 ||
|
|
|
|
|
|
record.type === 6
|
|
|
|
|
|
) {
|
2025-07-18 22:04:54 +08:00
|
|
|
|
if (record.group) {
|
|
|
|
|
|
return <>{renderGroup(record.group)}</>;
|
|
|
|
|
|
} else {
|
|
|
|
|
|
let other = null;
|
|
|
|
|
|
try {
|
|
|
|
|
|
other = JSON.parse(record.other);
|
|
|
|
|
|
} catch (e) {
|
|
|
|
|
|
console.error(
|
|
|
|
|
|
`Failed to parse record.other: "${record.other}".`,
|
|
|
|
|
|
e,
|
|
|
|
|
|
);
|
|
|
|
|
|
}
|
|
|
|
|
|
if (other === null) {
|
|
|
|
|
|
return <></>;
|
|
|
|
|
|
}
|
|
|
|
|
|
if (other.group !== undefined) {
|
|
|
|
|
|
return <>{renderGroup(other.group)}</>;
|
|
|
|
|
|
} else {
|
|
|
|
|
|
return <></>;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
} else {
|
|
|
|
|
|
return <></>;
|
|
|
|
|
|
}
|
|
|
|
|
|
},
|
|
|
|
|
|
},
|
|
|
|
|
|
{
|
|
|
|
|
|
key: COLUMN_KEYS.TYPE,
|
|
|
|
|
|
title: t('类型'),
|
|
|
|
|
|
dataIndex: 'type',
|
|
|
|
|
|
render: (text, record, index) => {
|
|
|
|
|
|
return <>{renderType(text, t)}</>;
|
|
|
|
|
|
},
|
|
|
|
|
|
},
|
|
|
|
|
|
{
|
|
|
|
|
|
key: COLUMN_KEYS.MODEL,
|
|
|
|
|
|
title: t('模型'),
|
|
|
|
|
|
dataIndex: 'model_name',
|
|
|
|
|
|
render: (text, record, index) => {
|
2026-03-14 19:05:23 +08:00
|
|
|
|
return record.type === 0 ||
|
|
|
|
|
|
record.type === 2 ||
|
|
|
|
|
|
record.type === 5 ||
|
|
|
|
|
|
record.type === 6 ? (
|
2025-07-18 22:04:54 +08:00
|
|
|
|
<>{renderModelName(record, copyText, t)}</>
|
|
|
|
|
|
) : (
|
|
|
|
|
|
<></>
|
|
|
|
|
|
);
|
|
|
|
|
|
},
|
|
|
|
|
|
},
|
|
|
|
|
|
{
|
|
|
|
|
|
key: COLUMN_KEYS.USE_TIME,
|
|
|
|
|
|
title: t('用时/首字'),
|
|
|
|
|
|
dataIndex: 'use_time',
|
|
|
|
|
|
render: (text, record, index) => {
|
|
|
|
|
|
if (!(record.type === 2 || record.type === 5)) {
|
|
|
|
|
|
return <></>;
|
|
|
|
|
|
}
|
|
|
|
|
|
if (record.is_stream) {
|
|
|
|
|
|
let other = getLogOther(record.other);
|
|
|
|
|
|
return (
|
|
|
|
|
|
<>
|
|
|
|
|
|
<Space>
|
|
|
|
|
|
{renderUseTime(text, t)}
|
|
|
|
|
|
{renderFirstUseTime(other?.frt, t)}
|
|
|
|
|
|
{renderIsStream(record.is_stream, t)}
|
|
|
|
|
|
</Space>
|
|
|
|
|
|
</>
|
|
|
|
|
|
);
|
|
|
|
|
|
} else {
|
|
|
|
|
|
return (
|
|
|
|
|
|
<>
|
|
|
|
|
|
<Space>
|
|
|
|
|
|
{renderUseTime(text, t)}
|
|
|
|
|
|
{renderIsStream(record.is_stream, t)}
|
|
|
|
|
|
</Space>
|
|
|
|
|
|
</>
|
|
|
|
|
|
);
|
|
|
|
|
|
}
|
|
|
|
|
|
},
|
|
|
|
|
|
},
|
|
|
|
|
|
{
|
|
|
|
|
|
key: COLUMN_KEYS.PROMPT,
|
2026-02-11 21:50:39 +08:00
|
|
|
|
title: (
|
|
|
|
|
|
<div className='flex items-center gap-1'>
|
|
|
|
|
|
{t('输入')}
|
|
|
|
|
|
<Tooltip
|
|
|
|
|
|
content={t(
|
|
|
|
|
|
'根据 Anthropic 协定,/v1/messages 的输入 tokens 仅统计非缓存输入,不包含缓存读取与缓存写入 tokens。',
|
|
|
|
|
|
)}
|
|
|
|
|
|
>
|
|
|
|
|
|
<IconHelpCircle className='text-gray-400 cursor-help' />
|
|
|
|
|
|
</Tooltip>
|
|
|
|
|
|
</div>
|
|
|
|
|
|
),
|
2025-07-18 22:04:54 +08:00
|
|
|
|
dataIndex: 'prompt_tokens',
|
|
|
|
|
|
render: (text, record, index) => {
|
2026-02-11 21:50:39 +08:00
|
|
|
|
const other = getLogOther(record.other);
|
|
|
|
|
|
const cacheSummary = getPromptCacheSummary(other);
|
|
|
|
|
|
const hasCacheRead = (cacheSummary?.cacheReadTokens || 0) > 0;
|
|
|
|
|
|
const hasCacheWrite = (cacheSummary?.cacheWriteTokens || 0) > 0;
|
|
|
|
|
|
let cacheText = '';
|
|
|
|
|
|
if (hasCacheRead && hasCacheWrite) {
|
|
|
|
|
|
cacheText = `${t('缓存读')} ${formatTokenCount(cacheSummary.cacheReadTokens)} · ${t('写')} ${formatTokenCount(cacheSummary.cacheWriteTokens)}`;
|
|
|
|
|
|
} else if (hasCacheRead) {
|
|
|
|
|
|
cacheText = `${t('缓存读')} ${formatTokenCount(cacheSummary.cacheReadTokens)}`;
|
|
|
|
|
|
} else if (hasCacheWrite) {
|
|
|
|
|
|
cacheText = `${t('缓存写')} ${formatTokenCount(cacheSummary.cacheWriteTokens)}`;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-03-14 19:05:23 +08:00
|
|
|
|
return record.type === 0 ||
|
|
|
|
|
|
record.type === 2 ||
|
|
|
|
|
|
record.type === 5 ||
|
|
|
|
|
|
record.type === 6 ? (
|
2026-02-11 21:50:39 +08:00
|
|
|
|
<div
|
|
|
|
|
|
style={{
|
|
|
|
|
|
display: 'inline-flex',
|
|
|
|
|
|
flexDirection: 'column',
|
|
|
|
|
|
alignItems: 'flex-start',
|
|
|
|
|
|
lineHeight: 1.2,
|
|
|
|
|
|
}}
|
|
|
|
|
|
>
|
|
|
|
|
|
<span>{text}</span>
|
|
|
|
|
|
{cacheText ? (
|
|
|
|
|
|
<span
|
|
|
|
|
|
style={{
|
|
|
|
|
|
marginTop: 2,
|
|
|
|
|
|
fontSize: 11,
|
|
|
|
|
|
color: 'var(--semi-color-text-2)',
|
|
|
|
|
|
whiteSpace: 'nowrap',
|
|
|
|
|
|
}}
|
|
|
|
|
|
>
|
|
|
|
|
|
{cacheText}
|
|
|
|
|
|
</span>
|
|
|
|
|
|
) : null}
|
|
|
|
|
|
</div>
|
2025-07-18 22:04:54 +08:00
|
|
|
|
) : (
|
|
|
|
|
|
<></>
|
|
|
|
|
|
);
|
|
|
|
|
|
},
|
|
|
|
|
|
},
|
|
|
|
|
|
{
|
|
|
|
|
|
key: COLUMN_KEYS.COMPLETION,
|
2025-10-03 21:49:24 +08:00
|
|
|
|
title: t('输出'),
|
2025-07-18 22:04:54 +08:00
|
|
|
|
dataIndex: 'completion_tokens',
|
|
|
|
|
|
render: (text, record, index) => {
|
|
|
|
|
|
return parseInt(text) > 0 &&
|
2026-03-14 19:05:23 +08:00
|
|
|
|
(record.type === 0 ||
|
|
|
|
|
|
record.type === 2 ||
|
|
|
|
|
|
record.type === 5 ||
|
|
|
|
|
|
record.type === 6) ? (
|
2025-07-18 22:04:54 +08:00
|
|
|
|
<>{<span> {text} </span>}</>
|
|
|
|
|
|
) : (
|
|
|
|
|
|
<></>
|
|
|
|
|
|
);
|
|
|
|
|
|
},
|
|
|
|
|
|
},
|
|
|
|
|
|
{
|
|
|
|
|
|
key: COLUMN_KEYS.COST,
|
|
|
|
|
|
title: t('花费'),
|
|
|
|
|
|
dataIndex: 'quota',
|
|
|
|
|
|
render: (text, record, index) => {
|
2026-03-14 19:05:23 +08:00
|
|
|
|
if (
|
|
|
|
|
|
!(
|
|
|
|
|
|
record.type === 0 ||
|
|
|
|
|
|
record.type === 2 ||
|
|
|
|
|
|
record.type === 5 ||
|
|
|
|
|
|
record.type === 6
|
|
|
|
|
|
)
|
|
|
|
|
|
) {
|
✨ feat: add subscription billing system (#2808)
* ci: create docker automation
* ✨ feat: add subscription billing system with admin management and user purchase flow
Implement a new subscription-based billing model alongside existing metered/per-request billing:
Backend:
- Add subscription plan models (SubscriptionPlan, SubscriptionPlanItem, UserSubscription, etc.)
- Implement CRUD APIs for subscription plan management (admin only)
- Add user subscription queries with support for multiple active/expired subscriptions
- Integrate payment gateways (Stripe, Creem, Epay) for subscription purchases
- Implement pre-consume and post-consume billing logic for subscription quota tracking
- Add billing preference settings (subscription_first, wallet_first, etc.)
- Enhance usage logs with subscription deduction details
Frontend - Admin:
- Add subscription management page with table view and drawer-based edit form
- Match UI/UX style with existing admin pages (redemption codes, users)
- Support enabling/disabling plans, configuring payment IDs, and model quotas
- Add user subscription binding modal in user management
Frontend - Wallet:
- Add subscription plans card with current subscription status display
- Show all subscriptions (active and expired) with remaining days/usage percentage
- Display purchasable plans with pricing cards following SaaS best practices
- Extract purchase modal to separate component matching payment confirm modal style
- Add skeleton loading states with active animation
- Implement billing preference selector in card header
- Handle payment gateway availability based on admin configuration
Frontend - Usage Logs:
- Display subscription deduction details in log entries
- Show step-by-step breakdown of subscription usage (pre-consumed, delta, final, remaining)
- Add subscription deduction tag for subscription-covered requests
* ✨ feat(admin): add user subscription management and refine UI/pagination
Add admin APIs to list/create/invalidate/delete user subscriptions
Add model helpers to fetch all user subscriptions (incl. expired) and support cancel/hard-delete
Wire new admin routes for user subscription operations
Replace “Bind subscription plan” entry with a dedicated User Subscriptions SideSheet in Users table
Use CardTable with responsive layout and working client-side pagination inside the SideSheet
Improve subscription purchase modal empty-gateway state with a Banner notice
* ✨ feat(admin): streamline subscription plan benefits editor with bulk actions
Restore the avatar/icon header for the “Model Benefits” section
Replace scattered controls with a compact toolbar-style workflow
Support multi-select add with a default quota for new items
Add row selection with bulk apply-to-selected / apply-to-all quota updates
Enable delete-selected to manage benefits faster and reduce mistakes
* ✨ fix(subscription): finalize payments, log billing, and clean up dead code
Complete subscription orders by creating a matching top-up record and writing billing logs
Add Epay return handler to verify and finalize browser callbacks
Require Stripe/Creem webhook configuration before starting subscription payments
Show subscription purchases in topup history with clearer labels/methods
Remove unused subscription helper, legacy Creem webhook struct, and unused topup fields
Simplify subscription self API payload to active/all lists only
* 🎨 style: format all code with gofmt and lint:fix
Apply consistent code formatting across the entire codebase using
gofmt and lint:fix tools. This ensures adherence to Go community
standards and improves code readability and maintainability.
Changes include:
- Run gofmt on all .go files to standardize formatting
- Apply lint:fix to automatically resolve linting issues
- Fix code style inconsistencies and formatting violations
No functional changes were made in this commit.
* ✨ feat(subscription): add quota reset periods and admin configuration
- Add reset period fields on subscription plans and user items
- Apply automatic quota resets during pre-consume based on plan schedule
- Expose reset-period configuration in the admin plan editor
- Display reset cadence in subscription cards and purchase modal
- Validate custom reset seconds on plan create/update
* ✨ feat(subscription): harden subscription billing with resets, idempotency, and production-grade stability
Add plan-level quota reset periods and display/reset cadence in admin/UI
Enforce natural reset alignment with background reset task and cleanup job
Make subscription pre-consume/refund idempotent with request-scoped records and retries
Use database time for consistent resets across multi-instance deployments
Harden payment callbacks with locking and idempotent order completion
Record subscription purchases in topup history and billing logs
Optimize subscription queries and add critical composite indexes
* ✨ feat(subscription): cache plan lookups and stabilize pre-consume
Introduce hybrid caches for subscription plans, items, and plan info with explicit
invalidation on admin updates. Streamline pre-consume transactions to reduce
redundant queries while preserving idempotency and reset logic.
* 🐛 fix(subscription): avoid pre-consume lookup noise
Use a RowsAffected check for the idempotency lookup so missing records
no longer surface as "record not found" errors while preserving behavior.
* 🔧 ci: Change workflow trigger to sub branch
Update the Docker image workflow to run on pushes to the sub branch instead of main.
* 💸 chore: Align subscription pricing display with global currency settings
Unify subscription price rendering to use the site-wide currency symbol/rate on the wallet and admin views.
Make subscription plan currency read-only in the editor and force USD on create/update to avoid drift.
Use global currency display type when creating Creem checkout payloads.
* 🔧 chore: Unify subscription plan status toggle with PATCH endpoint
Replace separate enable/disable flows with a single PATCH API that updates the enabled flag.
Update frontend hooks and table actions to call the unified endpoint and keep UI behavior consistent.
Introduce a minimal admin controller handler and route for the status update.
* ✨ feat: Add subscription limits and UI tags consistency
Add per-plan purchase limits with backend enforcement and UI disable states.
Expose limit configuration in admin plan editor and show limits in plan tables/cards.
Refine subscription UI tags with unified badge style and streamlined “My Subscriptions” layout.
* 🎨 style: tag color to white
* 🚀 refactor: Simplify subscription quota to total amount model
Remove per-model subscription items and switch to a single total quota per plan and user subscription. Update billing, reset, and logging flows to operate on total quota, and refactor admin/user UI to configure and display total quota consistently.
* 🚀 chore: Remove duplicate subscription usage percentage display
Keep the usage percentage shown only in the total quota line to avoid redundant “已用 0%” text while preserving remaining days in the summary.
* ✨ feat: Add subscription upgrade group with auto downgrade
* ✨ feat: Update subscription purchase modal display
Show total quota as currency with tooltip for raw quota, hide reset cycle when never, and display upgrade group when configured to match card display rules.
* ✨ feat: Extract quota conversion helpers to shared utils
Move quota display/conversion helpers into web/src/helpers/quota.js and update the subscription plan editor to import and use the shared utilities instead of inline functions.
* ✨ chore: Add upgrade group guidance in subscription editor
Add explanatory helper text under the upgrade group field to clarify automatic group upgrades, rollback conditions, and the expected delay before downgrading takes effect.
* 🔧 chore: remove unused Creem settings state
Drop the unused originInputs state and redundant updates to keep the Creem
settings form state minimal and easier to maintain.
* 🚀 chore: Remove useless action
* ✨ Add full i18n coverage for subscription-related UI across locales
* ✨ feat: harden subscription billing and improve UI consistency
Improve subscription payment safety and data integrity by handling user/URL lookup failures, fixing Stripe subscription mode, persisting quota reset fields, and correcting subscription delta accounting and DB timestamp casting. Refine the UI with stricter custom duration validation, accurate currency rounding, conditional Epay labeling, rollback on preference update failure, and shared subscription formatting helpers plus clearer component naming.
* 🔧 fix: make epay webhook and return flow subscription-aware
Ensure Epay webhook acknowledges success only after order completion, returning fail on processing errors to allow retries. Redirect subscription payment returns to the subscription page instead of top-up for correct user flow.
* 🚦 fix: guard epay return success on order completion
Redirect subscription return flow to failure when order completion fails, preventing false success states after payment verification.
* 🔧 fix: normalize epay error handling and webhook retries
Standardize SubscriptionRequestEpay error responses via ApiErrorMsg for a consistent schema.
Return "fail" on non-success trade statuses in the epay webhook to preserve retry behavior.
* 🧾 fix: persist epay orders before purchase
Create the subscription order before initiating epay payment and expire it if the provider call fails, preventing orphaned transactions and improving reconciliation.
* 🔧 fix: harden epay callbacks and billing fallbacks
Use POST and form parsing for epay notify/return routes, persist epay orders before provider calls with expiry on failure, and ensure notify handlers retry correctly.
Restrict subscription-first fallback to insufficient-subscription errors and log refund failures after retries to avoid silent quota drift.
* 🔧 fix: harden billing flow and sidebar settings
Add missing strings import for subscription fallback checks, log failed subscription refunds after retries, and extend sidebar module settings with a subscription management toggle plus translations.
* 🛡️ fix: fail fast on epay form parse errors
Handle ParseForm errors in epay notify/return handlers by returning fail or redirecting to failure, avoiding unsafe fallback to query parameters.
* ✨ fix: refine Japanese subscription status labels
Adjust Japanese UI wording for active-count labels to read more naturally and consistently.
* ✅ fix: standardize epay success response schema
Return subscription epay pay success responses via ApiSuccess to include the consistent success field and align with error schema.
2026-02-03 17:40:43 +08:00
|
|
|
|
return <></>;
|
|
|
|
|
|
}
|
|
|
|
|
|
const other = getLogOther(record.other);
|
|
|
|
|
|
const isSubscription = other?.billing_source === 'subscription';
|
|
|
|
|
|
if (isSubscription) {
|
|
|
|
|
|
// Subscription billed: show only tag (no $0), but keep tooltip for equivalent cost.
|
|
|
|
|
|
return (
|
|
|
|
|
|
<Tooltip content={`${t('由订阅抵扣')}:${renderQuota(text, 6)}`}>
|
|
|
|
|
|
<span>{renderBillingTag(record, t)}</span>
|
|
|
|
|
|
</Tooltip>
|
|
|
|
|
|
);
|
|
|
|
|
|
}
|
|
|
|
|
|
return <>{renderQuota(text, 6)}</>;
|
2025-07-18 22:04:54 +08:00
|
|
|
|
},
|
|
|
|
|
|
},
|
|
|
|
|
|
{
|
|
|
|
|
|
key: COLUMN_KEYS.IP,
|
|
|
|
|
|
title: (
|
2025-08-30 21:15:10 +08:00
|
|
|
|
<div className='flex items-center gap-1'>
|
2025-07-18 22:04:54 +08:00
|
|
|
|
{t('IP')}
|
2025-08-30 21:15:10 +08:00
|
|
|
|
<Tooltip
|
|
|
|
|
|
content={t(
|
|
|
|
|
|
'只有当用户设置开启IP记录时,才会进行请求和错误类型日志的IP记录',
|
|
|
|
|
|
)}
|
|
|
|
|
|
>
|
|
|
|
|
|
<IconHelpCircle className='text-gray-400 cursor-help' />
|
2025-07-18 22:04:54 +08:00
|
|
|
|
</Tooltip>
|
|
|
|
|
|
</div>
|
|
|
|
|
|
),
|
|
|
|
|
|
dataIndex: 'ip',
|
|
|
|
|
|
render: (text, record, index) => {
|
|
|
|
|
|
return (record.type === 2 || record.type === 5) && text ? (
|
|
|
|
|
|
<Tooltip content={text}>
|
2025-07-19 15:05:31 +08:00
|
|
|
|
<span>
|
|
|
|
|
|
<Tag
|
|
|
|
|
|
color='orange'
|
|
|
|
|
|
shape='circle'
|
|
|
|
|
|
onClick={(event) => {
|
|
|
|
|
|
copyText(event, text);
|
|
|
|
|
|
}}
|
|
|
|
|
|
>
|
|
|
|
|
|
{text}
|
|
|
|
|
|
</Tag>
|
|
|
|
|
|
</span>
|
2025-07-18 22:04:54 +08:00
|
|
|
|
</Tooltip>
|
|
|
|
|
|
) : (
|
|
|
|
|
|
<></>
|
|
|
|
|
|
);
|
|
|
|
|
|
},
|
|
|
|
|
|
},
|
|
|
|
|
|
{
|
|
|
|
|
|
key: COLUMN_KEYS.RETRY,
|
|
|
|
|
|
title: t('重试'),
|
|
|
|
|
|
dataIndex: 'retry',
|
|
|
|
|
|
render: (text, record, index) => {
|
|
|
|
|
|
if (!(record.type === 2 || record.type === 5)) {
|
|
|
|
|
|
return <></>;
|
|
|
|
|
|
}
|
|
|
|
|
|
let content = t('渠道') + `:${record.channel}`;
|
|
|
|
|
|
if (record.other !== '') {
|
|
|
|
|
|
let other = JSON.parse(record.other);
|
|
|
|
|
|
if (other === null) {
|
|
|
|
|
|
return <></>;
|
|
|
|
|
|
}
|
|
|
|
|
|
if (other.admin_info !== undefined) {
|
|
|
|
|
|
if (
|
2026-03-14 19:05:23 +08:00
|
|
|
|
other.admin_info.use_channel !== null &&
|
|
|
|
|
|
other.admin_info.use_channel !== undefined &&
|
|
|
|
|
|
other.admin_info.use_channel !== ''
|
2025-07-18 22:04:54 +08:00
|
|
|
|
) {
|
|
|
|
|
|
let useChannel = other.admin_info.use_channel;
|
|
|
|
|
|
let useChannelStr = useChannel.join('->');
|
|
|
|
|
|
content = t('渠道') + `:${useChannelStr}`;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2026-02-08 23:45:46 +08:00
|
|
|
|
return isAdminUser ? <div>{content}</div> : <></>;
|
2025-07-18 22:04:54 +08:00
|
|
|
|
},
|
|
|
|
|
|
},
|
|
|
|
|
|
{
|
|
|
|
|
|
key: COLUMN_KEYS.DETAILS,
|
|
|
|
|
|
title: t('详情'),
|
|
|
|
|
|
dataIndex: 'content',
|
|
|
|
|
|
fixed: 'right',
|
2026-03-14 19:05:23 +08:00
|
|
|
|
width: 200,
|
2025-07-18 22:04:54 +08:00
|
|
|
|
render: (text, record, index) => {
|
2026-03-14 19:05:23 +08:00
|
|
|
|
const detailSummary = getUsageLogDetailSummary(
|
|
|
|
|
|
record,
|
|
|
|
|
|
text,
|
|
|
|
|
|
billingDisplayMode,
|
|
|
|
|
|
t,
|
|
|
|
|
|
);
|
2026-01-26 20:20:30 +08:00
|
|
|
|
|
2026-03-14 19:05:23 +08:00
|
|
|
|
if (!detailSummary) {
|
2026-01-26 20:20:30 +08:00
|
|
|
|
return (
|
|
|
|
|
|
<Typography.Paragraph
|
|
|
|
|
|
ellipsis={{
|
|
|
|
|
|
rows: 2,
|
2026-03-16 12:44:30 +08:00
|
|
|
|
showTooltip: {
|
|
|
|
|
|
type: 'popover',
|
|
|
|
|
|
opts: { style: { width: 240 } },
|
|
|
|
|
|
},
|
2026-01-26 20:20:30 +08:00
|
|
|
|
}}
|
2026-03-14 19:05:23 +08:00
|
|
|
|
style={{ maxWidth: 200, marginBottom: 0 }}
|
2026-01-26 20:20:30 +08:00
|
|
|
|
>
|
2026-03-14 19:05:23 +08:00
|
|
|
|
{text}
|
2026-01-26 20:20:30 +08:00
|
|
|
|
</Typography.Paragraph>
|
|
|
|
|
|
);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-03-14 19:05:23 +08:00
|
|
|
|
return renderCompactDetailSummary(detailSummary.segments);
|
2025-07-18 22:04:54 +08:00
|
|
|
|
},
|
|
|
|
|
|
},
|
|
|
|
|
|
];
|
2025-08-30 21:15:10 +08:00
|
|
|
|
};
|