feat(billing): per-service and platform-wide free call limits
release / release (release) Canceled after 0s
release / release (release) Canceled after 0s
- mcp_service.free_daily_limit: per service, per user per day (NULL/0 = unlimited) - sys_config free_global_daily_limit: applies to every free service - effective limit = smaller of the two, so a service without its own value still inherits the global one - admin: Pricing tab input, System Settings > Business > Free Call Limits - submission review pages and endpoints (scaffolding, not yet published end to end) - allow LAN IPs in http_utils, add docker-compose.yml
This commit is contained in:
1 parent
2fd6272bbd
commit
d18adf035b
24 files changed
+720
-24
No files matched your search
@@ -26,6 +26,7 @@ import {
|
||||
Captions,
|
||||
Code,
|
||||
CreditCard,
|
||||
Gauge,
|
||||
GhostIcon,
|
||||
GlobeIcon,
|
||||
LockIcon,
|
||||
@@ -181,6 +182,12 @@ const SystemSettingsModal: React.FC<SystemSettingsModalProps> = ({
|
||||
icon: <CreditCard size={16} />,
|
||||
description: "Configure payment channels",
|
||||
},
|
||||
{
|
||||
key: SettingModalTab.FREE_QUOTA,
|
||||
label: "Free Call Limits",
|
||||
icon: <Gauge size={16} />,
|
||||
description: "Global daily call cap for free services",
|
||||
},
|
||||
{
|
||||
key: SettingModalTab.XPACK_EXPLORE,
|
||||
label: "XPack Explore",
|
||||
@@ -256,6 +263,7 @@ const SystemSettingsModal: React.FC<SystemSettingsModalProps> = ({
|
||||
title: "Business",
|
||||
items: groupItems([
|
||||
SettingModalTab.PAYMENT,
|
||||
SettingModalTab.FREE_QUOTA,
|
||||
SettingModalTab.XPACK_EXPLORE,
|
||||
]),
|
||||
},
|
||||
|
||||
@@ -31,6 +31,7 @@ import { AdminConfig, EmailConfig } from "@/types/system";
|
||||
import { LoginSettingsContent } from "./LoginSettingsContent";
|
||||
import { EmailConfigForm } from "@/components/system-setting/EmailConfigForm";
|
||||
import { LanguageSetting } from "@/components/system-setting/LanguageSetting";
|
||||
import { FreeQuotaSettings } from "@/components/system-setting/FreeQuotaSettings";
|
||||
import { TagsBarSetting } from "../system-setting/TagsBarSetting";
|
||||
|
||||
interface SystemSettingsRightContentProps {
|
||||
@@ -221,6 +222,8 @@ const SystemSettingsRightContent: React.FC<SystemSettingsRightContentProps> = ({
|
||||
<PaymentChannelsContent labelPlacement="outside" />
|
||||
)}
|
||||
|
||||
{section.key === "free-quota" && <FreeQuotaSettings />}
|
||||
|
||||
{section.key === "xpack-explore" && (
|
||||
<ExplorePanel
|
||||
isEnabled={!!platformConfig.is_showcased}
|
||||
|
||||
@@ -5,6 +5,7 @@ export enum SettingModalTab {
|
||||
BRANDING = "branding",
|
||||
THEME = "theme",
|
||||
PAYMENT = "payment",
|
||||
FREE_QUOTA = "free-quota",
|
||||
CODE_INJECTION = "code-injection",
|
||||
XPACK_EXPLORE = "xpack-explore",
|
||||
NAVIGATION = "navigation",
|
||||
|
||||
@@ -30,6 +30,7 @@ const _DefaultFormData: MCPServiceFormData = {
|
||||
auth_token: "",
|
||||
charge_type: ChargeType.Free,
|
||||
price: "",
|
||||
free_daily_limit: "",
|
||||
enabled: 0,
|
||||
tags: [],
|
||||
apis: [],
|
||||
@@ -46,12 +47,18 @@ const isValidUrl = (url: string) => {
|
||||
}
|
||||
};
|
||||
|
||||
// Helper function to validate domain format
|
||||
// Helper function to validate domain format (allow domain or IP address including LAN IPs)
|
||||
const isValidDomain = (url: string) => {
|
||||
if (!url) return false;
|
||||
try {
|
||||
const parsed = new URL(url);
|
||||
const hostname = parsed.hostname;
|
||||
// Allow IPv4 addresses (including private/lan IPs like 192.168.x.x, 10.x.x.x, 172.x.x.x)
|
||||
const ipv4Regex = /^(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$/;
|
||||
if (ipv4Regex.test(hostname)) return true;
|
||||
// Allow IPv6 (simplified)
|
||||
if (hostname.includes(":")) return true;
|
||||
// Domain format
|
||||
const domainRegex = /^(?:(?:[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?\.)+[a-zA-Z]{2,})$/;
|
||||
return domainRegex.test(hostname);
|
||||
} catch {
|
||||
|
||||
@@ -0,0 +1,111 @@
|
||||
"use client";
|
||||
|
||||
import React, { useEffect, useState } from "react";
|
||||
import { Button, Input } from "@nextui-org/react";
|
||||
import { fetchAdminAPI } from "@/rpc/admin-api";
|
||||
import { useTranslation } from "@/shared/lib/useTranslation";
|
||||
import toast from "react-hot-toast";
|
||||
|
||||
interface FreeQuotaResponse {
|
||||
free_global_daily_limit: number | null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Platform-wide daily call cap for free services.
|
||||
*
|
||||
* Stored in sys_config under free_global_daily_limit. The billing service takes
|
||||
* the smaller of this value and any per-service limit, so a service with no
|
||||
* limit of its own still inherits this one.
|
||||
*/
|
||||
export const FreeQuotaSettings: React.FC = () => {
|
||||
const { t } = useTranslation();
|
||||
const [value, setValue] = useState<string>("");
|
||||
const [loading, setLoading] = useState<boolean>(true);
|
||||
const [saving, setSaving] = useState<boolean>(false);
|
||||
|
||||
useEffect(() => {
|
||||
let mounted = true;
|
||||
(async () => {
|
||||
const res = await fetchAdminAPI<FreeQuotaResponse>(
|
||||
"/api/sysconfig/free-quota",
|
||||
{ method: "GET" }
|
||||
);
|
||||
if (!mounted) return;
|
||||
if (res.success) {
|
||||
const n = res.data?.free_global_daily_limit;
|
||||
setValue(n && n > 0 ? String(n) : "");
|
||||
} else if (res.error_message) {
|
||||
toast.error(res.error_message);
|
||||
}
|
||||
setLoading(false);
|
||||
})();
|
||||
return () => {
|
||||
mounted = false;
|
||||
};
|
||||
}, []);
|
||||
|
||||
const handleSave = async () => {
|
||||
const raw = value.trim();
|
||||
let payload: number | null = null;
|
||||
if (raw !== "") {
|
||||
const n = Number(raw);
|
||||
if (!Number.isInteger(n) || n < 0) {
|
||||
toast.error(t("Enter a non-negative integer"));
|
||||
return;
|
||||
}
|
||||
payload = n;
|
||||
}
|
||||
|
||||
setSaving(true);
|
||||
const res = await fetchAdminAPI<FreeQuotaResponse>(
|
||||
"/api/sysconfig/free-quota",
|
||||
{ method: "PUT", body: { free_global_daily_limit: payload } as unknown as BodyInit }
|
||||
);
|
||||
setSaving(false);
|
||||
|
||||
if (res.success) {
|
||||
const n = res.data?.free_global_daily_limit;
|
||||
setValue(n && n > 0 ? String(n) : "");
|
||||
toast.success(t("Free call limit saved"));
|
||||
} else {
|
||||
toast.error(res.error_message || t("Failed to save free call limit"));
|
||||
}
|
||||
};
|
||||
|
||||
return (
|
||||
<div className="space-y-4">
|
||||
<Input
|
||||
type="number"
|
||||
min={0}
|
||||
step={1}
|
||||
labelPlacement="outside"
|
||||
label={t("Platform-wide daily call limit per user")}
|
||||
placeholder={t("Example 100")}
|
||||
value={value}
|
||||
isDisabled={loading || saving}
|
||||
onValueChange={setValue}
|
||||
description={t(
|
||||
"Applies to every free service, leave blank or 0 for unlimited"
|
||||
)}
|
||||
/>
|
||||
|
||||
<p className="text-xs text-default-400">
|
||||
{t("If a service also has its own limit, the smaller number wins")}
|
||||
</p>
|
||||
|
||||
<div className="flex gap-3 py-4">
|
||||
<Button
|
||||
color="primary"
|
||||
variant="solid"
|
||||
onPress={handleSave}
|
||||
isLoading={saving}
|
||||
size="sm"
|
||||
>
|
||||
{t("Save")}
|
||||
</Button>
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
};
|
||||
|
||||
export default FreeQuotaSettings;
|
||||
Reference in new issue
Block a user