Compare commits

...
62 Commits
Author SHA1 Message Date
jinghui 721a2be32b feat(payment): integrate epay (易支付) gateway for CNY recharges
release / release (release) Canceled after 0s
- new EpayClient: V1 md5 signing, submit.php page pay, api.php order query
- create/verify epay payment links with CNY exchange-rate conversion
- signed async notify handler (/admin/payment/callback_epay) with amount
  check plus idempotent crediting, and a gateway query fallback
- epay channel config form, channel row and payment_method enum extended
- RechargeModal: send payment_method (was payment_channel, so every recharge
  fell through to Stripe and 500'd) and re-enable the channel picker
- payment config encryption key moved to a local .env, never committed
2026-11-13 15:17:05 +08:00
jinghui 1fbb6711f2 feat(submission): auto-publish approved submissions and expose review tab
- approving a submission now creates the McpService/McpToolApi records
- and records the published service id back on the submission
- review console folded into the admin console as a Submissions tab
- submit page rewritten around the shared submission API
2026-11-13 15:17:05 +08:00
jinghui d18adf035b feat(billing): per-service and platform-wide free call limits
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
2026-11-13 10:20:25 +08:00
Dot.L 2fd6272bbd Update release.yaml 2026-10-26 19:02:45 +08:00
Liujian 470d53cad6 feat(mcp): support MCP 0728 stateless JSON-RPC protocol and pin mcp SDK version
- Implement stateless HTTP JSON-RPC request handling (discover, tools/list, tools/call)
- Support extracting apikey from Authorization and X-Api-Key headers
- Pin mcp dependency to '>=1.12.0,<2.0.0' to prevent breaking changes in v2.x
- Update docker-compose image tag and volume config
2026-10-26 18:57:48 +08:00
Liujian f2b61bae7d Change the mysql-connector-python dependency to pymysql 2026-05-18 11:00:07 +08:00
Dot.L 588f2770b7 Merge pull request #167 from xpack-ai/feature/fix-billing-transaction
Feature/fix billing transaction
2026-04-01 17:23:01 +08:00
Liujian ad743d6ab7 Cancel the _update_wallet_cache method from being included in transactions 2026-03-30 18:17:27 +08:00
Liujian 6145d0b005 _update_wallet_cache is also included in transactions. 2026-03-30 18:01:08 +08:00
Dot.L d147fcb070 Merge pull request #166 from xpack-ai/feature/fix-billing-transaction
Feature/fix billing transaction
2026-03-30 16:40:17 +08:00
Liujian 6196b93e97 The billing processor adds transaction processing and retry mechanisms. 2026-03-30 16:00:58 +08:00
Liujian b51933d625 Fixed the issue where access remained even after the API key was deleted. 2026-03-30 15:59:49 +08:00
Liujian 40ff118798 Handling billing transactions 2026-03-27 18:36:28 +08:00
Dot.L d470677abe Merge pull request #155 from xpack-ai/revert-154-dev
Revert "Dev"
2026-03-11 15:59:28 +08:00
Dot.L 6eac3c6100 Revert "Dev" 2026-03-11 15:56:53 +08:00
Dot.L d22513730a Merge pull request #154 from xpack-ai/dev
Dev
2026-03-11 15:56:36 +08:00
Dot.L 613cece94b Merge pull request #153 from xpack-ai/open-base
user service add update_password
2026-03-11 15:25:00 +08:00
Liujian 5cd1c12323 user service add update_password 2026-03-11 15:13:28 +08:00
Dot.L dcf73b25bf Merge pull request #152 from xpack-ai/open-base
update temp_mcp_service_repository.py
2026-03-11 11:37:35 +08:00
Liujian 64a67e9c8d update temp_mcp_service_repository.py 2026-03-11 11:36:30 +08:00
Dot.L b6160af3c5 Merge pull request #150 from xpack-ai/open-base
mcp service add sort and category_id
2026-03-11 10:57:22 +08:00
Liujian 2ea41dfb75 mcp service add sort and category_id 2026-03-11 10:56:54 +08:00
Dot.L cbcc419f0b Merge pull request #149 from xpack-ai/open-base
update tenant_id type
2026-03-10 18:42:45 +08:00
Liujian 687949beb2 update tenant_id type 2026-03-10 18:41:49 +08:00
Dot.L fedaa5f5df Merge pull request #148 from xpack-ai/open-base
update sys_config index
2026-03-10 16:19:17 +08:00
Liujian c272c87e1f update sys_config index 2026-03-10 16:18:34 +08:00
Dot.L 8a052ff910 Merge pull request #147 from xpack-ai/open-base
The concept of a tenant is added as the foundation of the SaaS code.
2026-03-10 15:37:05 +08:00
Liujian d1c7a4b8c0 The concept of a tenant is added as the foundation of the SaaS code. 2026-03-10 15:33:57 +08:00
Dot.L 1a104338e8 Merge pull request #144 from xpack-ai/feature/1.4-liujian
Feature/1.4 liujian
2026-02-28 16:02:01 +08:00
Liujian 5dd47b2d01 Redis client methods supplement 2026-02-28 15:53:35 +08:00
Dot.L a28de60b1c Merge pull request #143 from xpack-ai/feature/1.4-liujian
Adjustment of some interface permissions
2026-02-26 10:29:38 +08:00
Liujian e4a807b2a0 Delete Print code 2026-02-26 10:28:18 +08:00
Liujian d0e7ed2771 Remove authentication checks from /api/common/config and /api/common/homepage 2026-02-26 10:26:00 +08:00
Dot.L 05c0088f1b Merge pull request #142 from xpack-ai/feature/1.4-liujian
Adjust the streamable-http initialization connection waiting logic
2026-02-25 15:23:08 +08:00
Liujian 7f66cfd21e Adjust the streamable-http initialization connection waiting logic 2026-02-25 15:22:10 +08:00
Dot.L 05b48f3656 Merge pull request #141 from xpack-ai/feature/1.4-liujian
Fix: Removed permission verification from the user information retrie…
2026-02-25 15:18:58 +08:00
Liujian 07fe83b7b5 Fix: Removed permission verification from the user information retrieval interface. 2026-02-25 15:18:19 +08:00
Dot.L 5fa1cd741e Merge pull request #140 from xpack-ai/feature/1.4-liujian
Adjust the streamable-http initialization connection waiting timeout
2026-02-25 11:24:50 +08:00
Liujian 3e1d986c1c Adjust the streamable-http initialization connection waiting timeout 2026-02-25 11:24:23 +08:00
Dot.L 2cf95fd1c1 Merge pull request #139 from xpack-ai/feature/1.4-liujian
Adjust the streamable-http initialization connection waiting logic
2026-02-25 11:13:53 +08:00
Liujian 09b6811391 Adjust the streamable-http initialization connection waiting logic 2026-02-25 11:13:18 +08:00
Dot.L 08d326dc1f Merge pull request #138 from xpack-ai/feature/1.4-liujian
fix: Handle None message in custom MCP event store during priming event
2026-02-24 16:28:17 +08:00
Dot.L fb23d62fb2 Merge pull request #137 from xpack-ai/feature/1.4-liujian
fix: Handle None message in custom MCP event store during priming event
2026-02-24 15:16:14 +08:00
Liujian 79400146ea fix: Handle None message in custom MCP event store during priming event 2026-02-24 15:15:09 +08:00
Dot.L 503b3bf078 Merge pull request #136 from xpack-ai/feature/1.4-liujian
API adds user role verification
2026-02-23 19:10:38 +08:00
Dot.L eb4ecff6ed Merge pull request #135 from xpack-ai/feature/1.4-liujian
API adds user role verification
2026-02-14 11:24:00 +08:00
Liujian 7f8947f070 Modify payment-related interface permissions 2026-02-14 11:23:50 +08:00
Liujian 6dca9b08cc API adds user role verification 2026-02-14 10:03:22 +08:00
lcx 06034cf46e Merge remote-tracking branch 'origin/feature/cx-1.4' 2026-02-06 15:53:17 +08:00
ningyv ffdf931cad Merge pull request #134 from xpack-ai/feature/cx-1.4
chore: upgrade Next.js version
2026-02-06 15:36:06 +08:00
lcx 24e59f18d0 chore: upgrade Next.js version 2026-02-06 15:35:16 +08:00
lcx 5a538863b1 Merge remote-tracking branch 'origin/feature/cx-1.4' into dev 2026-02-06 14:49:55 +08:00
lcx f468fe270f chore: upgrade Next.js version 2026-02-06 14:49:10 +08:00
lcx 4cc8b87de5 chore: upgrade Next.js version 2026-02-06 14:44:28 +08:00
Dot.L f6e0130035 Merge pull request #132 from xpack-ai/feature/1.4-liujian
MySQL reconnection mechanism optimization
2026-02-05 13:43:56 +08:00
Dot.L 6f882845c2 Merge pull request #129 from xpack-ai/feature/1.4-liujian
Optimize return prompt words
2026-01-29 18:26:34 +08:00
ningyv 5654f83644 Merge pull request #128 from xpack-ai/feature/cx-1.4
feat: 1.4 - List supports search
2026-01-29 18:13:24 +08:00
Dot.L 6d15420b08 Merge pull request #127 from xpack-ai/feature/1.4-liujian
Optimize return prompt words
2026-01-29 18:07:56 +08:00
Dot.L 5469eca450 Merge pull request #126 from xpack-ai/feature/1.4-liujian
Optimize return prompt words
2026-01-29 17:51:28 +08:00
ningyv 94fd46c8ca Merge pull request #125 from xpack-ai/feature/cx-1.4
feat: 1.4 - List supports search
2026-01-29 17:51:15 +08:00
Dot.L 57bd964d22 Merge pull request #124 from xpack-ai/feature/1.4-liujian
Optimize return prompt words
2026-01-29 16:31:00 +08:00
Dot.L aac3a187d1 Merge pull request #123 from xpack-ai/feature/1.4-liujian
Added logic to disable IP login after repeated login failures
2026-01-28 19:18:17 +08:00
77 changed files with 2579 additions and 18814 deletions

No files matched your search

+10 -1
View File
@@ -26,8 +26,17 @@ RABBITMQ_USER=rabbitmq
RABBITMQ_PASSWORD=rabbitmq_Gs123dA
RABBITMQ_VHOST=/
# Billing queue / retry settings
BILLING_QUEUE_NAME=billing.api.calls
# Max retry attempts (excluding initial consumption)
BILLING_RETRY_TIMES=3
# Base delay seconds for exponential backoff: base * 2^retry_count
BILLING_RETRY_BASE_DELAY_SECONDS=30
# Max delay seconds cap
BILLING_RETRY_MAX_DELAY_SECONDS=1800
# Payment config encryption key (Base64-encoded 32-byte random value for AES-GCM)
# Quick generation: python -c "import os,base64; print(base64.b64encode(os.urandom(32)).decode())"
PAYMENT_CONFIG_SECRET_KEY=uyyhfMJ+xpEE+IC5mPigpscekkR2ucz6Sj7sHYLhJQA=
# Optional: key ID used to support key rotation identification
PAYMENT_CONFIG_KEY_ID=default
PAYMENT_CONFIG_KEY_ID=default
+2 -2
View File
@@ -20,7 +20,7 @@ jobs:
node-version: '22.16'
- name: Pnpm install and build
run: |
npm install -g pnpm
npm install -g pnpm@9
./scripts/frontend_build.sh
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3
@@ -43,4 +43,4 @@ jobs:
QINIU_BUCKET: ${{ secrets.QINIU_BUCKET }}
QINIU_NAME: ${{ secrets.QINIU_NAME }}
run: cd scripts && ./docker_publish.sh ${{ secrets.DOCKER_USERNAME }} upload_qiniu
+3
View File
@@ -42,3 +42,6 @@ local_settings.py
/.secrets
/uploads/
/.vercel/
# Local deployment data
mysql-data/
+68
View File
@@ -0,0 +1,68 @@
version: '3'
services:
xpack-mysql:
image: mysql:8.0.37
privileged: true
restart: always
container_name: xpack-mysql
hostname: xpack-mysql
command:
- "--character-set-server=utf8mb4"
- "--collation-server=utf8mb4_unicode_ci"
ports:
- "33306:3306"
environment:
- MYSQL_ROOT_PASSWORD=mysql_ZTdhRB
- MYSQL_DATABASE=xpack
volumes:
- /vol3/1000/HD3/docker/xpack-mcp/mysql-data:/var/lib/mysql
networks:
- xpack
xpack-mcp-market:
image: xpackai/xpack-mcp-market:zhcn
container_name: xpack-mcp-market
privileged: true
restart: always
environment:
# AES-256-GCM key protecting payment channel config (epay merchant key, stripe secret).
# Lives here rather than in the image so the sample key shipped in .env.example
# can never decrypt this deployment's config.
- PAYMENT_CONFIG_SECRET_KEY=${PAYMENT_CONFIG_SECRET_KEY:?PAYMENT_CONFIG_SECRET_KEY not set - copy .env.example to .env}
- PAYMENT_CONFIG_KEY_ID=default
networks:
- xpack
ports:
- "8010:80"
depends_on:
- xpack-mysql
- xpack-redis
- xpack-rabbitmq
xpack-redis:
container_name: xpack-redis
image: redis:7.2.4
hostname: xpack-redis
privileged: true
restart: always
command:
- bash
- -c
- "redis-server --protected-mode yes --logfile redis.log --appendonly no --port 6379 --requirepass redis_6sJZDm"
networks:
- xpack
xpack-rabbitmq:
container_name: xpack-rabbitmq
image: rabbitmq:4.1.2-alpine
privileged: true
restart: always
environment:
- RABBITMQ_DEFAULT_USER=rabbitmq
- RABBITMQ_DEFAULT_PASS=rabbitmq_Gs123dA
networks:
- xpack
networks:
xpack:
driver: bridge
ipam:
driver: default
config:
- subnet: 172.101.0.0/24
+1 -1
View File
@@ -9,7 +9,7 @@
!.yarn/plugins
!.yarn/releases
!.yarn/versions
/pnpm-lock.yaml
# testing
/coverage
-57
View File
@@ -1,57 +0,0 @@
/** @type {import('next').NextConfig} */
const nextConfig = {
reactStrictMode: false,
// Enable SSR
output: "standalone",
// Optimize images
images: {
domains: [],
unoptimized: false, // Enable image optimization for SSR
},
// Webpack configuration
webpack: (config) => {
return config;
},
// Redirect configuration
async redirects() {
return [];
},
async rewrites() {
return [
// {
// source: "/api/:path*",
// destination: `${process.env.NEXT_PUBLIC_API_URL || "http://127.0.0.1:8001"}/api/:path*`,
// },
{
source: "/uploads/:path*",
destination: `${
process.env.NEXT_PUBLIC_API_URL || "http://127.0.0.1:8001"
}/uploads/:path*`,
},
];
},
// Headers for better performance
async headers() {
return [
{
source: "/(.*)",
headers: [
{
key: "X-Content-Type-Options",
value: "nosniff",
},
{
key: "X-XSS-Protection",
value: "1; mode=block",
},
],
},
];
},
};
module.exports = nextConfig;
+41
View File
@@ -1,10 +1,15 @@
import type { NextConfig } from "next";
const nextConfig: NextConfig = {
reactStrictMode: false,
output: "standalone",
experimental: {
optimizePackageImports: ["@nextui-org/react"],
},
transpilePackages: ["@nextui-org/react", "@nextui-org/theme"],
images: {
remotePatterns: [
{
@@ -12,6 +17,42 @@ const nextConfig: NextConfig = {
hostname: "**",
},
],
unoptimized: false,
},
// Redirect configuration
async redirects() {
return [];
},
async rewrites() {
return [
{
source: "/uploads/:path*",
destination: `${
process.env.NEXT_PUBLIC_API_URL || "http://127.0.0.1:8001"
}/uploads/:path*`,
},
];
},
// Headers for better performance
async headers() {
return [
{
source: "/(.*)",
headers: [
{
key: "X-Content-Type-Options",
value: "nosniff",
},
{
key: "X-XSS-Protection",
value: "1; mode=block",
},
],
},
];
},
};
+2 -2
View File
@@ -33,7 +33,7 @@
"immer": "^10.0.3",
"lottie-react": "^2.4.1",
"lucide-react": "^0.475.0",
"next": "^15.5.9",
"next": "^16.1.5",
"react": "^19.1.4",
"react-dom": "^19.1.4",
"react-hook-form": "^7.49.3",
@@ -55,7 +55,7 @@
"@types/three": "^0.177.0",
"autoprefixer": "^10.4.17",
"eslint": "^8.56.0",
"eslint-config-next": "15.5.9",
"eslint-config-next": "16.1.5",
"postcss": "^8.4.33",
"tailwindcss": "^3.4.17",
"typescript": "^5.3.3"
-18565
View File
File diff suppressed because it is too large. Load diff
+14 -3
View File
@@ -483,7 +483,6 @@
"Enter server ID": "Enter server ID",
"Modifying the ID will affect the MCP access URL. Please modify with caution.": "Modifying the ID will affect the MCP access URL. Please modify with caution.",
"You have modified the Server ID. This will affect the MCP access URL and may break existing integrations. Are you sure you want to continue?": "You have modified the Server ID. This will affect the MCP access URL and may break existing integrations. Are you sure you want to continue?",
"The header name for the API key (e.g., X-API-Key, Authorization)": "The header name for the API key (e.g., X-API-Key, Authorization)",
"Enter API key": "Enter API key",
"Enter bearer token": "Enter bearer token",
@@ -891,5 +890,17 @@
"Auth key name \"{{name}}\" already exists": "Auth key name \"{{name}}\" already exists",
"Start date must be before end date": "Start date must be before end date",
"Start date cannot be earlier than 30 days ago": "Start date cannot be earlier than 30 days ago",
"Resource Groups": "Resource Groups"
}
"Resource Groups": "Resource Groups",
"Daily call limit per user": "Daily call limit per user",
"Max calls per user per day (Beijing time). Leave blank or 0 for unlimited": "Max calls per user per day (Beijing time). Leave blank or 0 for unlimited",
"e.g. 50": "e.g. 50",
"Free Call Limits": "Free Call Limits",
"Global daily call cap for free services": "Global daily call cap for free services",
"Platform-wide daily call limit per user": "Platform-wide daily call limit per user",
"Applies to every free service, leave blank or 0 for unlimited": "Applies to every free service, leave blank or 0 for unlimited",
"If a service also has its own limit, the smaller number wins": "If a service also has its own limit, the smaller number wins",
"Example 100": "Example 100",
"Enter a non-negative integer": "Enter a non-negative integer",
"Free call limit saved": "Free call limit saved",
"Failed to save free call limit": "Failed to save free call limit"
}
+14 -3
View File
@@ -485,7 +485,6 @@
"Enter server ID": "输入服务器ID",
"Modifying the ID will affect the MCP access URL. Please modify with caution.": "修改ID会影响MCP的访问地址,需要谨慎修改。",
"You have modified the Server ID. This will affect the MCP access URL and may break existing integrations. Are you sure you want to continue?": "您已修改了服务器ID。这将影响MCP访问地址,可能会破坏现有的集成。您确定要继续吗?",
"The header name for the API key (e.g., X-API-Key, Authorization)": "API密钥的头部名称(例如:X-API-Key, Authorization)",
"Enter API key": "输入API密钥",
"Enter bearer token": "输入Bearer令牌",
@@ -957,5 +956,17 @@
"Resource group name \"{{name}}\" already exists": "资源组名称 \"{{name}}\" 已存在",
"Auth key name \"{{name}}\" already exists": "认证密钥 \"{{name}}\" 已存在",
"Start date must be before end date": "开始日期必须早于结束日期",
"Start date cannot be earlier than 30 days ago": "开始日期不能早于30天前"
}
"Start date cannot be earlier than 30 days ago": "开始日期不能早于30天前",
"Daily call limit per user": "每用户每日调用上限",
"Max calls per user per day (Beijing time). Leave blank or 0 for unlimited": "每个用户每天最多调用次数(北京时间)。留空或填 0 表示不限",
"e.g. 50": "如 50",
"Free Call Limits": "免费调用限制",
"Global daily call cap for free services": "免费服务的全局每日调用上限",
"Platform-wide daily call limit per user": "全站每用户每日调用上限",
"Applies to every free service, leave blank or 0 for unlimited": "对所有免费服务生效,留空或填 0 表示不限",
"If a service also has its own limit, the smaller number wins": "若单个服务也设置了上限,两者取更小值",
"Example 100": "如 100",
"Enter a non-negative integer": "请输入非负整数",
"Free call limit saved": "免费调用上限已保存",
"Failed to save free call limit": "保存免费调用上限失败"
}
+1 -1
View File
@@ -12,7 +12,7 @@ export async function revalidatePlatformConfig() {
revalidatePath("/", "layout");
// Revalidate host-specific cache tag
revalidateTag(`platform-config-${host}`);
revalidateTag(`platform-config-${host}`, 'max');
console.info(
`Platform config cache revalidated for host: platform-config-${host}`
@@ -0,0 +1,7 @@
"use client";
import React from "react";
import { SubmissionsManagement } from "@/components/console/SubmissionsManagement";
export default function AdminSubmissionsPage() {
return <SubmissionsManagement />;
}
+7 -6
View File
@@ -1,13 +1,14 @@
input::placeholder,
textarea::placeholder {
--nextui-foreground-500: 240 5.03% 64.9%;
}
/* Import theme styles */
@import "../styles/themes.css";
@tailwind base;
@tailwind components;
@tailwind utilities;
/* Import theme styles */
@import "../styles/themes.css";
input::placeholder,
textarea::placeholder {
--nextui-foreground-500: 240 5.03% 64.9%;
}
html,
body {
+2 -2
View File
@@ -27,12 +27,12 @@ export default async function RootLayout({
const platformConfig = await getCachedPlatformConfig();
return (
<html lang="en" suppressHydrationWarning data-color-mode="light">
<html lang="zh-CN" suppressHydrationWarning data-color-mode="light">
<head>
<script
dangerouslySetInnerHTML={{
__html: `
window.localStorage.setItem("i18nextLng", "${platformConfig?.platform?.language || "en"}");
window.localStorage.setItem("i18nextLng", "${platformConfig?.platform?.language || "zh-CN"}");
`,
}}
/>
+173
View File
@@ -0,0 +1,173 @@
"use client";
import React, { useState } from "react";
import { fetchAPI } from "@/shared/rpc/common-function";
type SubType = "mcp" | "skill" | "plugin";
export default function SubmitPage() {
const [type, setType] = useState<SubType>("mcp");
const [name, setName] = useState("");
const [desc, setDesc] = useState("");
const [file, setFile] = useState<File | null>(null);
const [baseUrl, setBaseUrl] = useState("");
const [serviceType, setServiceType] = useState("openapi");
const [tags, setTags] = useState("");
const [chargeType, setChargeType] = useState<"free" | "paid">("free");
const [price, setPrice] = useState("");
const [freeDailyLimit, setFreeDailyLimit] = useState("");
const [msg, setMsg] = useState<{ ok: boolean; text: string } | null>(null);
const [submitting, setSubmitting] = useState(false);
const handleSubmit = async (e: React.FormEvent) => {
e.preventDefault();
setMsg(null);
if (typeof window !== "undefined" && !localStorage.getItem("user_token")) {
setMsg({ ok: false, text: "请先登录后再提交(右上角登录)" });
return;
}
if (type === "mcp" && !baseUrl.trim()) {
setMsg({ ok: false, text: "MCP 服务必须填写服务地址 base_url" });
return;
}
setSubmitting(true);
try {
const fd = new FormData();
fd.append("type", type);
fd.append("name", name);
if (desc) fd.append("description", desc);
if (file) fd.append("file", file);
if (type === "mcp") {
fd.append("base_url", baseUrl.trim());
fd.append("service_type", serviceType);
if (tags.trim()) fd.append("tags", tags.trim());
fd.append("charge_type", chargeType);
if (chargeType === "paid" && price) fd.append("price", price);
if (chargeType === "free" && freeDailyLimit) fd.append("free_daily_limit", freeDailyLimit);
}
const data = await fetchAPI<{ success: boolean; error_message?: string; data?: { id: string; status: string } }>(
"/api/submission/submission",
{ method: "POST", body: fd }
);
if (data?.success) {
setMsg({ ok: true, text: "提交成功,等待管理员审核。审核通过后将自动上架到市场。" });
setName(""); setDesc(""); setFile(null); setBaseUrl(""); setTags("");
} else {
setMsg({ ok: false, text: data?.error_message || "提交失败,请稍后重试" });
}
} catch (err) {
setMsg({ ok: false, text: "提交失败:" + (err as Error).message });
} finally {
setSubmitting(false);
}
};
const inputCls = "border p-2 w-full rounded";
return (
<div className="max-w-xl mx-auto p-6">
<div className="flex items-center justify-between mb-4">
<h1 className="text-2xl font-bold">上传 MCP / Skill / Plugin</h1>
</div>
<form onSubmit={handleSubmit} className="space-y-4">
<select value={type} onChange={(e) => setType(e.target.value as SubType)} className={inputCls}>
<option value="mcp">MCP 服务</option>
<option value="skill">AI 技能</option>
<option value="plugin">AI 插件</option>
</select>
<input value={name} onChange={(e) => setName(e.target.value)} placeholder="名称" className={inputCls} required />
<textarea value={desc} onChange={(e) => setDesc(e.target.value)} placeholder="描述" className={inputCls} rows={3} />
{type === "mcp" && (
<div className="space-y-3 border rounded p-3 bg-gray-50">
<div>
<label className="block text-sm font-medium mb-1">服务地址 base_url *</label>
<input
value={baseUrl}
onChange={(e) => setBaseUrl(e.target.value)}
placeholder="http://192.168.1.10:8020"
className={inputCls}
required
/>
</div>
<div>
<label className="block text-sm font-medium mb-1">服务类型</label>
<select value={serviceType} onChange={(e) => setServiceType(e.target.value)} className={inputCls}>
<option value="openapi">openapi(HTTP 接口)</option>
</select>
</div>
<div>
<label className="block text-sm font-medium mb-1">标签(逗号分隔)</label>
<input value={tags} onChange={(e) => setTags(e.target.value)} placeholder="工具, 搜索, 免费" className={inputCls} />
</div>
<div>
<label className="block text-sm font-medium mb-1">计费方式</label>
<select
value={chargeType}
onChange={(e) => setChargeType(e.target.value as "free" | "paid")}
className={inputCls}
>
<option value="free">免费</option>
<option value="paid">按次付费</option>
</select>
</div>
{chargeType === "paid" ? (
<div>
<label className="block text-sm font-medium mb-1">单价(元/次)</label>
<input
type="number" step="0.01" min="0"
value={price}
onChange={(e) => setPrice(e.target.value)}
placeholder="0.10"
className={inputCls}
/>
</div>
) : (
<div>
<label className="block text-sm font-medium mb-1">每日免费次数(留空 = 不限)</label>
<input
type="number" min="1"
value={freeDailyLimit}
onChange={(e) => setFreeDailyLimit(e.target.value)}
placeholder="100"
className={inputCls}
/>
</div>
)}
<div>
<label className="block text-sm font-medium mb-1">
OpenAPI 规范文件(.json,用于自动生成工具列表)
</label>
<input type="file" onChange={(e) => setFile(e.target.files?.[0] || null)} className={inputCls} />
<p className="text-xs text-gray-500 mt-1">
上传后审核通过会自动生成工具;不上传则上架后需在服务管理里手动添加工具。
</p>
</div>
</div>
)}
{type !== "mcp" && (
<input type="file" onChange={(e) => setFile(e.target.files?.[0] || null)} className={inputCls} />
)}
<button
type="submit"
disabled={submitting}
className="bg-blue-600 text-white px-4 py-2 rounded disabled:opacity-50"
>
{submitting ? "提交中..." : "提交"}
</button>
</form>
{msg && (
<pre className={`mt-4 text-sm whitespace-pre-wrap ${msg.ok ? "text-green-700" : "text-red-600"}`}>
{msg.text}
</pre>
)}
</div>
);
}
+21 -4
View File
@@ -12,7 +12,7 @@ import UserManagement from "@/components/user-management/UserManagement";
import RevenueManagement from "@/components/revenue-management/RevenueManagement";
import ConsoleSidebar from "./Sidebar";
import { withComponentInjection } from "@/shared/hooks/useComponentInjection";
import { Boxes, DollarSign, Home, ServerIcon, Users } from "lucide-react";
import { Boxes, DollarSign, Home, Inbox, ServerIcon, Users } from "lucide-react";
import OnboardingWelcome, { REQUIRED_TASK_KEYS } from "./OnboardingWelcome";
import ConsoleStats from "./ConsoleStats";
import SystemSettingsModal from "./SystemSettingsModal";
@@ -24,6 +24,7 @@ import {
updateAdminOnboardingTasks,
} from "@/api/onboard.api";
import ResourceGroupManagement from "../resource-group/resourceGroup";
import { SubmissionsManagement } from "./SubmissionsManagement";
const ConsoleContent: React.FC = () => {
const { t } = useTranslation();
@@ -37,9 +38,13 @@ const ConsoleContent: React.FC = () => {
const tabFromUrl = searchParams.get("tab") as TabKey;
if (
tabFromUrl &&
["mcp-services", "user-management", "revenue-management", "resource-group"].includes(
tabFromUrl
)
[
"mcp-services",
"user-management",
"revenue-management",
"resource-group",
"submissions",
].includes(tabFromUrl)
) {
return tabFromUrl;
}
@@ -69,6 +74,12 @@ const ConsoleContent: React.FC = () => {
label: t("MCP"),
description: t("Manage MCP servers and API tools"),
},
{
key: TabKey.SUBMISSIONS,
icon: <Inbox size={18} />,
label: t("Submissions"),
description: t("Review user submissions and publish to marketplace"),
},
{
key: TabKey.USER_MANAGEMENT,
icon: <Users size={18} />,
@@ -213,6 +224,12 @@ const ConsoleContent: React.FC = () => {
switch (activeTab) {
case TabKey.MCP_SERVICES:
return <MCPServicesManagement />;
case TabKey.SUBMISSIONS:
return (
<div className="h-full overflow-auto">
<SubmissionsManagement />
</div>
);
case TabKey.USER_MANAGEMENT:
return (
<div className="h-full overflow-auto">
@@ -4,6 +4,7 @@ import React from "react";
import { StripeConfigForm } from "../payment/StripeConfigForm";
import { AlipayConfigForm } from "../payment/AlipayConfigForm";
import { WechatConfigForm } from "../payment/WechatConfigForm";
import { EpayConfigForm } from "../payment/EpayConfigForm";
import { usePaymentChannelManagement } from "@/hooks/usePaymentChannelManagement";
interface PaymentChannelsContentProps {
@@ -17,12 +18,15 @@ export const PaymentChannelsContent: React.FC<PaymentChannelsContentProps> = ({
stripeConfig,
alipayConfig,
wechatConfig,
epayConfig,
isWechatSaving,
isStripeSaving,
isAlipaySaving,
isEpaySaving,
saveStripeConfig,
saveAlipayConfig,
saveWechatConfig,
saveEpayConfig,
enableChannel,
disableChannel,
} = usePaymentChannelManagement();
@@ -38,6 +42,15 @@ export const PaymentChannelsContent: React.FC<PaymentChannelsContentProps> = ({
isLoading={isStripeSaving}
labelPlacement={labelPlacement}
/>
{/* epay (易支付) configuration */}
<EpayConfigForm
config={epayConfig}
onSave={saveEpayConfig}
onEnable={enableChannel}
onDisable={disableChannel}
isLoading={isEpaySaving}
labelPlacement={labelPlacement}
/>
{/* coming soon */}
{/* alipay configuration */}
{/* <AlipayConfigForm
@@ -0,0 +1,173 @@
"use client";
import React, { useEffect, useState } from "react";
import { fetchAdminAPI } from "@/rpc/admin-api";
interface Metadata {
base_url?: string;
service_type?: string;
tags?: string;
charge_type?: string;
price?: number;
free_daily_limit?: number | null;
published_service_id?: string;
}
interface Submission {
id: string;
user_id: string;
type: string;
name: string;
description?: string;
file_path?: string;
metadata_json?: Metadata | null;
status: string;
created_at?: string;
review_note?: string;
}
const statusColor: Record<string, string> = {
pending: "bg-yellow-100 text-yellow-800",
approved: "bg-green-100 text-green-800",
rejected: "bg-red-100 text-red-800",
};
export function SubmissionsManagement() {
const [subs, setSubs] = useState<Submission[]>([]);
const [filter, setFilter] = useState<string>("");
const [loading, setLoading] = useState(false);
const [error, setError] = useState("");
const [rejectNote, setRejectNote] = useState<Record<string, string>>({});
const load = async () => {
setLoading(true);
setError("");
try {
const qs = filter ? `?status=${filter}` : "";
// fetchAdminAPI<T> already returns the { success, error_message, data } envelope
const response = await fetchAdminAPI<Submission[]>(
`/api/submission/admin/submissions${qs}`
);
if (response?.success === false) {
setError(response.error_message || "加载失败");
setSubs([]);
} else if (response?.data) {
setSubs(response.data);
} else {
setSubs([]);
}
} catch (err) {
setError("加载失败:" + (err as Error).message);
setSubs([]);
} finally {
setLoading(false);
}
};
useEffect(() => { load(); }, [filter]);
const act = async (id: string, action: "approve" | "reject") => {
const body: RequestInit =
action === "approve"
? { method: "PUT" }
: {
method: "PUT",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ note: rejectNote[id] || "" }),
};
await fetchAdminAPI(`/api/submission/admin/submission/${id}/${action}`, body);
load();
};
return (
<div className="max-w-6xl mx-auto p-6">
<div className="flex items-center justify-between mb-4">
<h1 className="text-2xl font-bold">投稿审核</h1>
<select value={filter} onChange={(e) => setFilter(e.target.value)} className="border p-2">
<option value="">全部</option>
<option value="pending">待审核</option>
<option value="approved">已通过</option>
<option value="rejected">已拒绝</option>
</select>
</div>
{error && <div className="mb-4 p-3 bg-red-50 text-red-700 rounded">{error}</div>}
{loading && <div className="mb-4 text-gray-500">加载中...</div>}
{!loading && !error && subs.length === 0 && (
<div className="mb-4 text-gray-500">暂无提交记录</div>
)}
<div className="space-y-4">
{subs.map((s) => {
const meta = s.metadata_json || {};
return (
<div key={s.id} className="border rounded p-4 space-y-2">
<div className="flex items-center justify-between">
<div className="flex items-center gap-2">
<span className="font-semibold">{s.name}</span>
<span className="px-2 py-0.5 rounded text-xs">{s.type}</span>
<span className={`px-2 py-0.5 rounded text-xs ${statusColor[s.status] || "bg-gray-100"}`}>{s.status}</span>
</div>
<span className="text-sm text-gray-500">{s.created_at?.slice(0, 19).replace("T", " ")}</span>
</div>
{s.description && <p className="text-sm text-gray-700">{s.description}</p>}
{s.type === "mcp" && (meta.base_url || meta.tags || meta.charge_type) && (
<div className="text-sm text-gray-600 bg-gray-50 rounded p-2 space-y-1">
{meta.base_url && <div>服务地址:<code className="text-xs">{meta.base_url}</code></div>}
{meta.tags && <div>标签:{meta.tags}</div>}
<div>
计费:
{meta.charge_type === "paid"
? `按次付费 ¥${meta.price ?? 0}/次`
: `免费${meta.free_daily_limit ? `(每日 ${meta.free_daily_limit} 次)` : "(不限次)"}`}
</div>
{meta.published_service_id && (
<div className="text-green-700">
已上架市场,服务 ID:<code className="text-xs">{meta.published_service_id}</code>
</div>
)}
</div>
)}
{s.file_path && (
<div className="text-sm text-gray-500">
附件:<code className="text-xs">{s.file_path}</code>
</div>
)}
{s.review_note && (
<div className="text-sm text-gray-600">审核备注:{s.review_note}</div>
)}
{s.status === "pending" ? (
<div className="space-y-2">
<input
value={rejectNote[s.id] || ""}
onChange={(e) => setRejectNote({ ...rejectNote, [s.id]: e.target.value })}
placeholder="拒绝原因(可选)"
className="border p-2 w-full text-sm"
/>
<div className="flex gap-2">
<button
onClick={() => act(s.id, "approve")}
className="bg-green-600 text-white px-3 py-1 rounded text-sm"
>
通过(自动上架)
</button>
<button
onClick={() => act(s.id, "reject")}
className="bg-red-600 text-white px-3 py-1 rounded text-sm"
>
拒绝
</button>
</div>
</div>
) : null}
</div>
);
})}
</div>
</div>
);
}
@@ -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,234 @@
"use client";
import React, { useEffect, useState } from "react";
import { Input, Button, AccordionItem, Accordion, Switch } from "@nextui-org/react";
import { Eye, EyeOff } from "lucide-react";
import { EpayChannelConfig } from "@/types/payment";
import { useTranslation } from "@/shared/lib/useTranslation";
interface EpayConfigFormProps {
config: EpayChannelConfig;
onSave: (config: EpayChannelConfig) => void;
onEnable: (channelId: string) => Promise<boolean>;
onDisable: (channelId: string) => Promise<boolean>;
isLoading?: boolean;
labelPlacement?: "outside" | "inside";
}
export const EpayConfigForm: React.FC<EpayConfigFormProps> = ({
config,
onSave,
onEnable,
onDisable,
isLoading = false,
labelPlacement = "inside",
}) => {
const { t } = useTranslation();
const [formData, setFormData] = useState<EpayChannelConfig>(config);
const [showKey, setShowKey] = useState(false);
useEffect(() => {
setFormData(config);
}, [config]);
const handleInputChange = (field: keyof EpayChannelConfig, value: string | boolean) => {
setFormData((prev) => ({ ...prev, [field]: value }));
};
const handleEnableDisable = async (value: boolean) => {
const result = value ? await onEnable("epay") : await onDisable("epay");
if (result) {
setFormData((prev) => ({ ...prev, is_enabled: value }));
}
};
const handleSave = () => onSave(formData);
const isFormValid =
!!formData?.gateway?.trim() && !!formData?.pid?.trim() && !!formData?.key?.trim();
const panelContent = () => (
<>
<div className={`flex flex-col gap-4 ${formData.is_enabled ? "" : "hidden"}`}>
<Input
label={t("Gateway URL")}
placeholder="https://pay.example.com"
description={t("Epay gateway base address, no trailing slash")}
value={formData.gateway || ""}
onChange={(e) => handleInputChange("gateway", e.target.value)}
isRequired
autoComplete="off"
isDisabled={!formData.is_enabled}
labelPlacement="outside"
/>
<Input
label={t("Merchant ID (pid)")}
placeholder={t("Enter Merchant ID")}
description={t("Numeric merchant id issued by the epay platform")}
value={formData.pid || ""}
onChange={(e) => handleInputChange("pid", e.target.value)}
isRequired
autoComplete="off"
isDisabled={!formData.is_enabled}
labelPlacement="outside"
/>
<Input
label={t("Merchant Key (key)")}
placeholder={t("Enter Merchant Key")}
description={t("Used to sign requests and verify notifications, 32 characters")}
value={formData.key || ""}
onChange={(e) => handleInputChange("key", e.target.value)}
type={showKey ? "text" : "password"}
isRequired
autoComplete="off"
isDisabled={!formData.is_enabled}
labelPlacement="outside"
endContent={
<button
type="button"
onClick={() => setShowKey(!showKey)}
className="focus:outline-none"
>
{showKey ? (
<EyeOff className="w-4 h-4 text-gray-400" />
) : (
<Eye className="w-4 h-4 text-gray-400" />
)}
</button>
}
/>
<div className="flex flex-col gap-1">
<label className="text-sm font-medium">{t("Payment Method")}</label>
<select
className="w-full px-3 py-2 text-sm border border-gray-300 rounded-md bg-white focus:outline-none focus:ring-2 focus:ring-primary-500 disabled:opacity-50 disabled:cursor-not-allowed"
value={formData.pay_type || ""}
disabled={!formData.is_enabled}
onChange={(e) => handleInputChange("pay_type", e.target.value)}
>
<option value="">{t("Let customer choose at checkout")}</option>
<option value="alipay">{t("Alipay")}</option>
<option value="wxpay">{t("WeChat Pay")}</option>
<option value="qqpay">{t("QQ Wallet")}</option>
</select>
<span className="text-xs text-gray-500">
{t("Leave empty to show the gateway cashier with all methods")}
</span>
</div>
<Input
label={t("Exchange Rate")}
placeholder="1"
description={t(
"CNY charged per 1 balance unit. Default 1 means 1:1. e.g. 7 means a $10 recharge bills ¥70"
)}
value={String(formData.exchange_rate ?? 1)}
onChange={(e) => handleInputChange("exchange_rate", e.target.value)}
type="number"
min="0"
step="0.01"
autoComplete="off"
isDisabled={!formData.is_enabled}
labelPlacement="outside"
/>
<Input
label={t("Notify URL (optional)")}
placeholder={t("Auto derived from the site URL when empty")}
description={t(
"Async payment notification address that the gateway can reach from the internet"
)}
value={formData.notify_url || ""}
onChange={(e) => handleInputChange("notify_url", e.target.value)}
autoComplete="off"
isDisabled={!formData.is_enabled}
labelPlacement="outside"
/>
<Input
label={t("Return URL (optional)")}
placeholder={t("Auto derived when empty")}
description={t("Page opened after the customer finishes paying")}
value={formData.return_url || ""}
onChange={(e) => handleInputChange("return_url", e.target.value)}
autoComplete="off"
isDisabled={!formData.is_enabled}
labelPlacement="outside"
/>
</div>
{formData.is_enabled && (
<div className="flex gap-3 py-4">
<Button
color="primary"
variant="solid"
onPress={handleSave}
isLoading={isLoading}
isDisabled={!isFormValid}
size="sm"
>
{t("Save")}
</Button>
</div>
)}
</>
);
if (labelPlacement === "outside") {
return (
<div className="flex flex-col gap-4">
<div className="flex items-center justify-between p-4 bg-gray-50 rounded-lg">
<div className="flex flex-col gap-1">
<span className="text-sm font-medium">{t("Enable Epay Payment")}</span>
<span className="text-xs text-gray-500">
{t("Alipay / WeChat Pay through your own epay gateway")}
</span>
</div>
<Switch
isSelected={formData.is_enabled || false}
onValueChange={handleEnableDisable}
color="primary"
size="sm"
/>
</div>
{panelContent()}
</div>
);
}
return (
<Accordion
variant="splitted"
itemClasses={{ base: "shadow-none border-1" }}
defaultExpandedKeys={["epay-config"]}
className="px-0"
>
<AccordionItem
key="epay-config"
title={
<div className="flex flex-col justify-between">
<div className="flex items-center justify-between w-full">
<div>
<h3 className="text-lg font-semibold">{t("Epay Configuration")}</h3>
<p className="text-sm text-gray-500">
{t("Configure your epay (Epay) gateway")}
</p>
</div>
<Switch
isSelected={formData.is_enabled || false}
onValueChange={handleEnableDisable}
size="sm"
/>
</div>
</div>
}
>
{panelContent()}
</AccordionItem>
</Accordion>
);
};
export default EpayConfigForm;
@@ -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;
@@ -4,6 +4,7 @@ import {
StripeConfig,
AlipayConfig,
WechatConfig,
EpayChannelConfig,
PaymentChannelTestResult,
} from "@/types/payment";
import { paymentChannelService } from "@/services/paymentChannelService";
@@ -29,6 +30,14 @@ export const usePaymentChannelManagement = () => {
mch_id: "",
is_enabled: false,
});
const [epayConfig, setEpayConfig] = useState<EpayChannelConfig>({
gateway: "",
pid: "",
key: "",
pay_type: "",
exchange_rate: 1,
is_enabled: false,
});
const [loading, setLoading] = useState(false);
const [error, setError] = useState<string | null>(null);
const [testResult, setTestResult] = useState<PaymentChannelTestResult | null>(
@@ -37,6 +46,7 @@ export const usePaymentChannelManagement = () => {
const [isWechatSaving, setIsWechatSaving] = useState(false);
const [isStripeSaving, setIsStripeSaving] = useState(false);
const [isAlipaySaving, setIsAlipaySaving] = useState(false);
const [isEpaySaving, setIsEpaySaving] = useState(false);
// get payment channel list
const fetchChannels = useCallback(async () => {
@@ -69,6 +79,16 @@ export const usePaymentChannelManagement = () => {
} as AlipayConfig);
}
const epayChannel = response.data.find(
(channel) => channel.id === "epay"
);
if (epayChannel) {
setEpayConfig({
...epayChannel.config,
is_enabled: epayChannel.is_enabled,
} as EpayChannelConfig);
}
const wechatChannel = response.data.find(
(channel) => channel.id === "wechat"
);
@@ -184,6 +204,28 @@ export const usePaymentChannelManagement = () => {
}
}, []);
// save epay config
const saveEpayConfig = useCallback(async (config: EpayChannelConfig) => {
setIsEpaySaving(true);
setError(null);
try {
const result = await paymentChannelService.saveEpayConfig(config);
if (result) {
setEpayConfig(config);
return true;
}
return false;
} catch (err) {
const errorMessage =
err instanceof Error ? err.message : "Failed to save epay config";
setError(errorMessage);
return false;
} finally {
setIsEpaySaving(false);
}
}, []);
// save stripe config
const saveStripeConfig = useCallback(async (config: StripeConfig) => {
setIsStripeSaving(true);
@@ -316,6 +358,8 @@ export const usePaymentChannelManagement = () => {
stripeConfig,
alipayConfig,
wechatConfig,
epayConfig,
isEpaySaving,
isWechatSaving,
isStripeSaving,
isAlipaySaving,
@@ -326,6 +370,7 @@ export const usePaymentChannelManagement = () => {
saveStripeConfig,
saveAlipayConfig,
saveWechatConfig,
saveEpayConfig,
testStripeConnection,
testAlipayConnection,
testWechatConnection,
@@ -2,6 +2,7 @@ import {
StripeConfig,
AlipayConfig,
WechatConfig,
EpayChannelConfig,
PaymentChannelApiItem,
PaymentChannelListResponse,
PaymentChannelTestResult,
@@ -187,6 +188,11 @@ export class PaymentChannelService {
return this.savePaymentChannelConfig("wechat", config, "WeChat");
}
// Save Epay (易支付) configuration
async saveEpayConfig(config: EpayChannelConfig): Promise<boolean> {
return this.savePaymentChannelConfig("epay", config, "Epay");
}
// Test Alipay connection
async testAlipayConnection(
config: AlipayConfig
@@ -65,6 +65,27 @@ export const PricingTab: React.FC<PricingTabProps> = ({
</SelectItem>
</Select>
{formData.charge_type === ChargeType.Free && (
<Input
type="number"
min="0"
step="1"
value={
formData.free_daily_limit === null ||
formData.free_daily_limit === undefined
? ""
: String(formData.free_daily_limit)
}
onChange={(e) => onInputChange("free_daily_limit", e.target.value)}
description={t(
"Max calls per user per day (Beijing time). Leave blank or 0 for unlimited"
)}
placeholder={t("e.g. 50")}
label={t("Daily call limit per user")}
labelPlacement="outside"
/>
)}
{formData.charge_type !== ChargeType.Free && (
<>
{formData.charge_type === ChargeType.PerToken ? (
@@ -33,6 +33,17 @@ export const RechargeModal: React.FC<RechargeModalProps> = ({
paymentChannels[0]?.id || ""
);
// payment channels arrive asynchronously -- fall back to the first one so the
// confirm button is not stuck disabled on a modal opened before they load.
const effectiveChannel = selectedPaymentChannel || paymentChannels[0]?.id || "";
const currencySymbol = effectiveChannel === "epay" ? "¥" : "$";
const channelLabel = (id: string, name: string) => {
if (id === "epay") return t("Epay (Alipay / WeChat)");
if (id === "stripe") return name || t("Credit Card");
return name || id;
};
const [paymentId, setPaymentId] = useState("");
const [isConfirmOpen, setIsConfirmOpen] = useState(false);
@@ -48,7 +59,7 @@ export const RechargeModal: React.FC<RechargeModalProps> = ({
return;
}
if (!selectedPaymentChannel) {
if (!effectiveChannel) {
setError(t("Please select a payment method"));
return;
}
@@ -57,7 +68,9 @@ export const RechargeModal: React.FC<RechargeModalProps> = ({
method: "POST",
body: {
amount: numAmount,
payment_channel: selectedPaymentChannel,
currency: effectiveChannel === "epay" ? "cny" : "usd",
// channel id doubles as the payment method the backend switches on
payment_method: effectiveChannel,
success_url: window.location.origin + "/pay_success",
} as unknown as BodyInit,
});
@@ -128,7 +141,9 @@ export const RechargeModal: React.FC<RechargeModalProps> = ({
onChange={handleAmountChange}
startContent={
<div className="pointer-events-none flex items-center">
<span className="text-default-400 text-small">$</span>
<span className="text-default-400 text-small">
{currencySymbol}
</span>
</div>
}
type="text"
@@ -144,15 +159,13 @@ export const RechargeModal: React.FC<RechargeModalProps> = ({
}
}}
/>
{/**coming soon : select payment channel*/}
{/* {paymentChannels.length > 0 && (
{paymentChannels.length > 1 && (
<div className="space-y-2">
<label className="text-sm font-medium text-default-700">
{t("Payment Method")}
</label>
<RadioGroup
value={selectedPaymentChannel}
value={effectiveChannel}
onValueChange={setSelectedPaymentChannel}
isInvalid={!!error && error.includes("payment method")}
errorMessage={
@@ -165,12 +178,20 @@ export const RechargeModal: React.FC<RechargeModalProps> = ({
>
{paymentChannels.map((channel) => (
<Radio key={channel.id} value={channel.id}>
{channel.name}
{channelLabel(channel.id, channel.name)}
</Radio>
))}
</RadioGroup>
</div>
)} */}
)}
{effectiveChannel === "epay" && (
<p className="text-xs text-default-500">
{t(
"Billed in CNY via your epay gateway (Alipay / WeChat Pay), credited to your balance 1:1"
)}
</p>
)}
</ModalBody>
<ModalFooter>
<Button variant="light" onPress={handleClose}>
@@ -179,7 +200,7 @@ export const RechargeModal: React.FC<RechargeModalProps> = ({
<Button
color="primary"
onPress={handlePurchase}
isDisabled={!selectedPaymentChannel || !amount}
isDisabled={!effectiveChannel || !amount}
>
{t("Confirm")}
</Button>
+1
View File
@@ -4,6 +4,7 @@ export enum TabKey {
USER_MANAGEMENT = "user-management",
RESOURCE_GROUP = "resource-group",
REVENUE_MANAGEMENT = "revenue-management",
SUBMISSIONS = "submissions",
PAYMENT_CHANNELS = "payment-channels",
LOGIN_SETTINGS = "login-settings",
SYSTEM_SETTINGS = "system-settings",
+1
View File
@@ -68,6 +68,7 @@ export interface MCPServiceFormData {
price?: string;
input_token_price?: string; // Price for input tokens when charge_type is PerToken
output_token_price?: string; // Price for output tokens when charge_type is PerToken
free_daily_limit?: string | number | null; // free services only: max calls per user per day (Beijing time), blank/0 = unlimited
enabled: EnabledEnum; // API requires string type
apis: MCPServiceAPIItem[];
headers?: MCPAuthHeaderItem[]; // add headers to form data
+3 -3
View File
@@ -15,8 +15,8 @@ const getUrlLanguage = (): string | null => {
const getBrowserDefaultLanguage = (): string => {
if (typeof window === "undefined") {
// SSR时根据Accept-Language header判断,这里先返回英语
return "en";
// SSR时根据Accept-Language header判断,这里先返回中文
return "zh-CN";
}
const browserLang = navigator.language;
@@ -31,7 +31,7 @@ const getBrowserDefaultLanguage = (): string => {
const langPrefix = browserLang.split("-")[0];
const match = supportedLanguages.find((lang) => lang.startsWith(langPrefix));
return match || "en"; // 最后才fallback到英语
return match || "zh-CN"; // 最后才fallback到中文
};
export const getDefaultLanguage = (): string => {
+21 -2
View File
@@ -1,5 +1,5 @@
// payment channel type
export type PaymentChannelType = "stripe" | "alipay" | "wechat";
export type PaymentChannelType = "stripe" | "alipay" | "wechat" | "epay";
// stripe config - admin need to configure
export interface StripeConfig {
@@ -23,8 +23,27 @@ export interface WechatConfig {
is_enabled?: boolean;
}
// epay (易支付) config - admin need to configure
export interface EpayChannelConfig {
/** gateway base address, e.g. https://pay.example.com */
gateway: string;
/** merchant id */
pid: string;
/** merchant key used for md5 signing */
key: string;
/** "", alipay, wxpay or qqpay -- empty lets the customer choose */
pay_type?: string;
/** CNY charged per 1 balance unit (default 1 = 1:1) */
exchange_rate?: number | string;
/** async notify url, auto derived when empty */
notify_url?: string;
/** browser return url, auto derived when empty */
return_url?: string;
is_enabled?: boolean;
}
// union type for all payment configs
export type PaymentConfig = StripeConfig | AlipayConfig | WechatConfig;
export type PaymentConfig = StripeConfig | AlipayConfig | WechatConfig | EpayChannelConfig;
// API return payment channel item structure
export interface PaymentChannelApiItem {
+19 -5
View File
@@ -1,7 +1,11 @@
{
"compilerOptions": {
"target": "ES2017",
"lib": ["dom", "dom.iterable", "esnext"],
"lib": [
"dom",
"dom.iterable",
"esnext"
],
"allowJs": true,
"skipLibCheck": true,
"strict": true,
@@ -11,7 +15,7 @@
"moduleResolution": "bundler",
"resolveJsonModule": true,
"isolatedModules": true,
"jsx": "preserve",
"jsx": "react-jsx",
"incremental": true,
"plugins": [
{
@@ -19,9 +23,19 @@
}
],
"paths": {
"@/*": ["./src/*"]
"@/*": [
"./src/*"
]
}
},
"include": ["next-env.d.ts", "**/*.ts", "**/*.tsx", ".next/types/**/*.ts"],
"exclude": ["node_modules"]
"include": [
"next-env.d.ts",
"**/*.ts",
"**/*.tsx",
".next/types/**/*.ts",
".next/dev/types/**/*.ts"
],
"exclude": [
"node_modules"
]
}
+1
View File
@@ -14,6 +14,7 @@ python-multipart
google-auth>=2.0.0
google-auth-oauthlib>=1.0.0
fastapi_mcp
mcp>=1.12.0,<2.0.0
alipay-sdk-python>=3.0.0
wechatpayv3[async]
cryptography>=41.0.0
+10 -11
View File
@@ -3,7 +3,7 @@ import sys
import re
from pathlib import Path
from dotenv import load_dotenv
import mysql.connector
import pymysql
from typing import Optional, List, Tuple, Union
# 加载环境变量
@@ -29,12 +29,13 @@ def get_db_connection(db_config: Optional[DBConfig] = None):
if db_config is None:
db_config = DBConfig()
return mysql.connector.connect(
return pymysql.connect(
host=db_config.MYSQL_HOST,
port=db_config.MYSQL_PORT,
user=db_config.MYSQL_USER,
password=db_config.MYSQL_PASSWORD,
database=db_config.MYSQL_DB
database=db_config.MYSQL_DB,
charset='utf8mb4'
)
def get_current_version(db_config: Optional[DBConfig] = None) -> Optional[str]:
@@ -50,19 +51,17 @@ def get_current_version(db_config: Optional[DBConfig] = None) -> Optional[str]:
conn = get_db_connection(db_config)
cursor = conn.cursor()
# 查询version配置
cursor.execute("SELECT value FROM sys_config WHERE `key` = 'version'")
result = cursor.fetchone()
cursor.close()
conn.close()
if result and len(result) > 0:
# 将元组的第一个元素转换为字符串并返回
return str(result['value'] if isinstance(result, dict) else result[0])
if result:
return str(result[0])
return None
except mysql.connector.Error as err:
except pymysql.Error as err:
# 如果表不存在或其他数据库错误,返回None
print(f"查询版本信息失败: {err}")
return None
@@ -124,7 +123,7 @@ def execute_sql_file(file_path: str, db_config: Optional[DBConfig] = None) -> bo
try:
cursor.execute(sql)
print(f" -> 成功")
except mysql.connector.Error as e:
except pymysql.Error as e:
print(f" -> SET语句执行错误: {e}")
# SET语句失败不影响后续执行
continue
@@ -144,7 +143,7 @@ def execute_sql_file(file_path: str, db_config: Optional[DBConfig] = None) -> bo
try:
cursor.execute(sql)
print(f" -> 成功")
except mysql.connector.Error as e:
except pymysql.Error as e:
if "Duplicate column name" in str(e):
print(f" -> 忽略重复列错误: {e}")
continue
@@ -166,7 +165,7 @@ def execute_sql_file(file_path: str, db_config: Optional[DBConfig] = None) -> bo
except FileNotFoundError:
print(f"错误: SQL 文件不存在: {file_path}")
return False
except mysql.connector.Error as err:
except pymysql.Error as err:
print(f"数据库错误: {err}")
if sql:
print(f"出错的SQL语句: {sql}")
+1
View File
@@ -66,6 +66,7 @@ CREATE TABLE IF NOT EXISTS `user_apikey` (
`expire_at` TIMESTAMP NULL DEFAULT NULL COMMENT 'Expiration timestamp',
`created_at` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT 'Creation timestamp',
`updated_at` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT 'Last update timestamp',
`is_deleted` TINYINT NOT NULL DEFAULT 0 COMMENT 'Logical deletion: 0 (normal), 1 (deleted)',
PRIMARY KEY (`id`),
UNIQUE INDEX `uk_apikey` (`apikey`) COMMENT 'Unique API key index',
FOREIGN KEY (`user_id`) REFERENCES `user` (`id`) ON DELETE CASCADE
+5
View File
@@ -0,0 +1,5 @@
ALTER TABLE `user_apikey` ADD COLUMN `is_deleted` TINYINT NOT NULL DEFAULT 0 COMMENT 'Logical deletion: 0 (normal), 1 (deleted)' AFTER `updated_at`;
INSERT INTO `sys_config` (`id`,`key`, `value`,`description`,`created_at`,`updated_at`)
VALUES ('xpack-version','version', '1.4.0', 'Add is_deleted column to user_apikey', CURRENT_TIMESTAMP, CURRENT_TIMESTAMP)
ON DUPLICATE KEY UPDATE `value` = VALUES(`value`), `description` = VALUES(`description`), `updated_at` = CURRENT_TIMESTAMP;
+3 -3
View File
@@ -1,11 +1,11 @@
{
"version": "v1.2",
"releaseDate": "2025-12-22",
"version": "v1.4",
"releaseDate": "2026-02-23",
"description": "This is the latest release of the application.",
"downloads": {
"program": {
"xpack_mcp_market": {
"version": "v1.2.1",
"version": "v1.4.5",
"docker": "xpackai/xpack-mcp-market",
"tar": "xpack-mcp-market.tar.gz",
"url": "https://public.eolinker.com/xpack/images/xpack-mcp-market.${VERSION}.${ARCH}.tar.gz"
@@ -44,4 +44,7 @@ KEY_SOCIAL_ACCOUNT_X_URL = "social_account_x_url"
KEY_CNAME_A_IP = "cname_a_ip"
KEY_DEFAULT_RESOURCE_GROUP = "default_resource_group"
KEY_DEFAULT_RESOURCE_GROUP = "default_resource_group"
# Platform-wide daily cap on free MCP services (see api_service billing_service)
KEY_FREE_GLOBAL_DAILY_LIMIT = "free_global_daily_limit"
@@ -21,9 +21,14 @@ class BillingMessageConsumer:
def __init__(self):
self.queue_name = os.getenv("BILLING_QUEUE_NAME") or "billing.api.calls"
self.retry_queue_name = f"{self.queue_name}.retry"
self.connection = None
self.channel = None
self.consuming = False
# retry times from env, default 3
self.max_retries = int(os.getenv("BILLING_RETRY_TIMES", "3"))
self.retry_base_delay_seconds = int(os.getenv("BILLING_RETRY_BASE_DELAY_SECONDS", "30"))
self.retry_max_delay_seconds = int(os.getenv("BILLING_RETRY_MAX_DELAY_SECONDS", "1800"))
self._setup_connection()
def _setup_connection(self):
@@ -48,6 +53,14 @@ class BillingMessageConsumer:
# Declare queue
self.channel.queue_declare(queue=self.queue_name, durable=True)
self.channel.queue_declare(
queue=self.retry_queue_name,
durable=True,
arguments={
"x-dead-letter-exchange": "",
"x-dead-letter-routing-key": self.queue_name,
},
)
# Set QoS, only process one message at a time
self.channel.basic_qos(prefetch_count=1)
@@ -124,6 +137,29 @@ class BillingMessageConsumer:
body: Message body
"""
message_data = None
settled = False
raw_body = body.decode("utf-8", errors="replace") if isinstance(body, (bytes, bytearray)) else str(body)
def _safe_ack() -> None:
nonlocal settled
if settled:
return
settled = True
try:
channel.basic_ack(delivery_tag=method.delivery_tag)
except Exception:
logger.error("Failed to ack message", exc_info=True)
def _safe_nack(requeue: bool) -> None:
nonlocal settled
if settled:
return
settled = True
try:
channel.basic_nack(delivery_tag=method.delivery_tag, requeue=requeue)
except Exception:
logger.error("Failed to nack message", exc_info=True)
try:
# Parse message
message_data = json.loads(body)
@@ -137,27 +173,68 @@ class BillingMessageConsumer:
if success:
# Ack if processed successfully
channel.basic_ack(delivery_tag=method.delivery_tag)
_safe_ack()
logger.info(f"Message processed and acknowledged: {message_data.get('user_id')}")
else:
# If failed, ack and drop (avoid infinite retry)
channel.basic_ack(delivery_tag=method.delivery_tag)
logger.error(f"Message processing failed, message dropped: {message_data.get('user_id')}")
# Retry with limited attempts
retry_count = self._get_retry_count(properties)
if retry_count < self.max_retries:
try:
delay_ms = self._get_retry_delay_ms(retry_count)
self._publish_delayed_retry(channel, message_data, retry_count + 1, delay_ms)
_safe_ack()
logger.warning(
f"Message processing failed, scheduled retry {retry_count + 1}/{self.max_retries} after {delay_ms}ms: {message_data.get('user_id')}, tool={message_data.get('tool_name')}, "
)
except Exception as pub_err:
logger.error(f"Failed to requeue message for retry: {pub_err}", exc_info=True)
# Requeue original message
_safe_nack(requeue=True)
else:
# Exceeded retries; ack and drop
_safe_ack()
self._log_retry_exceeded(
reason="handler_returned_false",
retry_count=retry_count,
properties=properties,
method=method,
message_data=message_data,
raw_body=raw_body,
)
finally:
db.close()
try:
db.close()
except Exception:
logger.error("Failed to close db session", exc_info=True)
except json.JSONDecodeError as e:
logger.error(f"Message format error: {str(e)}, body: {body}")
# Format error, ack directly (do not requeue)
channel.basic_ack(delivery_tag=method.delivery_tag)
_safe_ack()
except Exception as e:
logger.error(f"Exception occurred while processing message: {str(e)}", exc_info=True)
# On exception, ack and drop (avoid infinite retry)
try:
channel.basic_ack(delivery_tag=method.delivery_tag)
logger.error(f"Exception handling failed, message dropped")
retry_count = self._get_retry_count(properties)
if message_data is not None and retry_count < self.max_retries:
delay_ms = self._get_retry_delay_ms(retry_count)
self._publish_delayed_retry(channel, message_data, retry_count + 1, delay_ms, error=str(e))
_safe_ack()
logger.warning(
f"Exception during processing, scheduled retry {retry_count + 1}/{self.max_retries} after {delay_ms}ms: {message_data.get('user_id')}"
)
else:
_safe_ack()
self._log_retry_exceeded(
reason="exception",
retry_count=retry_count,
properties=properties,
method=method,
message_data=message_data,
error=str(e),
raw_body=raw_body,
)
except:
# If even ack fails, log error but do not raise
logger.error("Unable to acknowledge message", exc_info=True)
@@ -173,9 +250,62 @@ class BillingMessageConsumer:
int: Retry count
"""
if properties and properties.headers:
return properties.headers.get("x-retry-count", 0)
raw = properties.headers.get("x-retry-count", 0)
try:
return int(raw)
except Exception:
return 0
return 0
def _get_retry_delay_ms(self, current_retry_count: int) -> int:
delay_seconds = self.retry_base_delay_seconds * (2 ** current_retry_count)
if delay_seconds > self.retry_max_delay_seconds:
delay_seconds = self.retry_max_delay_seconds
return int(delay_seconds * 1000)
def _publish_delayed_retry(self, channel, message_data: dict, next_retry_count: int, delay_ms: int, error: str | None = None) -> None:
headers: dict[str, str | int] = {"x-retry-count": next_retry_count}
if error:
headers["x-last-error"] = error
props = pika.BasicProperties(
delivery_mode=2,
headers=headers,
expiration=str(delay_ms),
)
channel.basic_publish(exchange="", routing_key=self.retry_queue_name, body=json.dumps(message_data), properties=props)
def _log_retry_exceeded(self, reason: str, retry_count: int, properties, method, message_data: dict | None, error: str | None = None, raw_body: str | None = None) -> None:
headers = {}
if properties and properties.headers:
try:
headers = dict(properties.headers)
except Exception:
headers = {}
event = {
"reason": reason,
"retry_count": retry_count,
"max_retries": self.max_retries,
"queue": self.queue_name,
"routing_key": getattr(method, "routing_key", None),
"delivery_tag": getattr(method, "delivery_tag", None),
"headers": headers,
"user_id": (message_data or {}).get("user_id"),
"service_id": (message_data or {}).get("service_id"),
"api_id": (message_data or {}).get("api_id"),
"tool_name": (message_data or {}).get("tool_name"),
"call_start_time": (message_data or {}).get("call_start_time"),
"call_end_time": (message_data or {}).get("call_end_time"),
}
if error:
event["error"] = error
if raw_body is not None:
event["raw_body"] = raw_body
try:
payload = json.dumps(event, ensure_ascii=False, default=str)
except Exception:
payload = repr(event)
logger.error(f"ALERT billing message retries exceeded: {payload}")
def start_billing_consumer():
"""Start billing message consumer"""
@@ -1,4 +1,4 @@
from fastapi import APIRouter, Depends
from fastapi import APIRouter, Depends, Request
from sqlalchemy.orm import Session
from sqlalchemy import func
from datetime import datetime, date
@@ -7,6 +7,10 @@ from yarl import Query
from services.common.database import get_db
from services.common.utils.response_utils import ResponseUtils
from services.admin_service.services.stats_service import StatsService
from services.admin_service.utils.user_utils import UserUtils
from services.common import error_msg
router = APIRouter()
@@ -15,10 +19,13 @@ def get_stats_service(db: Session = Depends(get_db)) -> StatsService:
@router.get("/stats/analytics")
async def stats(
request: Request,
start: int= 0,
end: int=0,
stats_service: StatsService = Depends(get_stats_service)
):
if not UserUtils.is_admin(request):
return ResponseUtils.error(error_msg=error_msg.NO_PERMISSION)
if end == 0:
end = int(datetime.now().timestamp())
if start == 0:
@@ -1,11 +1,13 @@
# Read config from sys_config table, key is configured in services/admin_service/constants/sys_config_key.py
import logging
from fastapi import APIRouter, Depends
from fastapi import APIRouter, Depends, Request
import json
from sqlalchemy.orm import Session
from services.common.database import get_db
from services.common.utils.response_utils import ResponseUtils
from services.admin_service.utils.user_utils import UserUtils
from services.common import error_msg
from services.admin_service.services.sys_config_service import SysConfigService
from services.admin_service.constants.sys_config_key import *
from services.admin_service.services.payment_channel_service import PaymentChannelService
@@ -16,6 +18,7 @@ router = APIRouter()
@router.get("/config", summary="Get configuration (no login required)", tags=["common"])
def get_config(
request: Request,
db: Session = Depends(get_db),
):
"""Get platform configuration settings without authentication."""
@@ -81,8 +84,10 @@ def get_config(
return ResponseUtils.error(message="Failed to get configuration")
@router.get("/homepage", summary="Get homepage configuration (no login required)", tags=["common"])
def get_homepage_config(db: Session = Depends(get_db)):
"""Get homepage configuration settings without authentication."""
def get_homepage_config(
request: Request,
db: Session = Depends(get_db),
):
try:
# Create service instance
sys_config_service = SysConfigService(db)
@@ -90,7 +90,9 @@ def update_mcp_service_info(request: Request, body: dict = Body(...), mcp_manage
@router.delete("/service", summary="Delete MCP service")
def delete_mcp_service(body: dict = Body(...), mcp_manager_service: McpManagerService = Depends(get_mcp_manager)):
def delete_mcp_service(request: Request, body: dict = Body(...), mcp_manager_service: McpManagerService = Depends(get_mcp_manager)):
if not UserUtils.is_admin(request):
return ResponseUtils.error(error_msg=error_msg.NO_PERMISSION)
"""Delete MCP service by ID."""
id = body.get("id")
if not id:
@@ -102,11 +104,14 @@ def delete_mcp_service(body: dict = Body(...), mcp_manager_service: McpManagerSe
@router.post("/openapi_parse", summary="openapi import", response_model=dict)
async def openapi_parse(
request: Request,
url: Optional[HttpUrl] = Form(None, description="OpenAPI document URL (optional)"),
file: Optional[UploadFile] = File(None, description="OpenAPI document file (JSON/YAML, optional)"),
mcp_manager_service: McpManagerService = Depends(get_mcp_manager),
):
"""Parse and import OpenAPI document from URL or file upload."""
if not UserUtils.is_admin(request):
return ResponseUtils.error(error_msg=error_msg.NO_PERMISSION)
try:
if url:
url_str = str(url)
+9 -3
View File
@@ -1,13 +1,13 @@
from os import stat
from typing import Optional, List
from fastapi import APIRouter, Depends, Query
from services.common.models.user_wallet_history import TransactionType
from fastapi import APIRouter, Depends, Query, Request
from sqlalchemy import table
from sqlalchemy.orm import Session
from services.common.database import get_db
from services.common.utils.response_utils import ResponseUtils
from services.admin_service.services.user_service import UserService
from services.admin_service.services.user_wallet_history_service import UserWalletHistoryService
from services.admin_service.utils.user_utils import UserUtils
from services.common import error_msg
router = APIRouter()
@@ -22,6 +22,7 @@ def get_user_service(db: Session = Depends(get_db)) -> UserService:
@router.get("/list", summary="Get user order list")
def get_user_order_list(
request: Request,
page: int = Query(1, description="Current page number"),
page_size: int = Query(15, description="Number of items per page"),
keyword: str = Query("", description="Search keyword for user email or name"),
@@ -30,6 +31,8 @@ def get_user_order_list(
user_service: UserService = Depends(get_user_service),
user_wallet_history_service: UserWalletHistoryService = Depends(get_user_wallet_history_service),
):
if not UserUtils.is_admin(request):
return ResponseUtils.error(error_msg=error_msg.NO_PERMISSION)
"""Get paginated list of user order history."""
offset = (page - 1) * page_size
if filter_payment_type:
@@ -65,6 +68,7 @@ def get_user_order_list(
@router.get("/list/by_user", summary="Get user order list by user id")
def get_user_order_list_by_user_id(
request: Request,
user_id: str = Query(..., description="User ID"),
page: int = Query(1, description="Current page number"),
page_size: int = Query(15, description="Number of items per page"),
@@ -73,6 +77,8 @@ def get_user_order_list_by_user_id(
user_service: UserService = Depends(get_user_service),
user_wallet_history_service: UserWalletHistoryService = Depends(get_user_wallet_history_service),
):
if not UserUtils.is_admin(request):
return ResponseUtils.error(error_msg=error_msg.NO_PERMISSION)
"""Get paginated list of user order history by user id."""
user = user_service.get_by_id(user_id)
if not user:
@@ -1,12 +1,16 @@
from fastapi import APIRouter, Depends, Request
from fastapi.responses import PlainTextResponse
from sqlalchemy.orm import Session
from pydantic import BaseModel
from urllib.parse import parse_qsl
from services.common.database import get_db
from services.common.utils.response_utils import ResponseUtils
from services.admin_service.services.payment_service import PaymentService
from services.admin_service.services.user_wallet_history_service import UserWalletHistoryService
from services.admin_service.tasks.order_monitor_task import OrderMonitorTask
from services.admin_service.utils.user_utils import UserUtils
from services.common import error_msg
router = APIRouter()
@@ -18,6 +22,15 @@ def get_user_wallet_history(db: Session = Depends(get_db)) -> UserWalletHistoryS
return UserWalletHistoryService(db)
def _request_base_url(request: Request) -> str:
"""Public base URL of this site (honours the reverse proxy headers)."""
forwarded_proto = (request.headers.get("X-Forwarded-Proto") or "").split(",")[0].strip()
scheme = forwarded_proto or request.url.scheme or "http"
host = (request.headers.get("X-Forwarded-Host") or "").split(",")[0].strip()
host = host or request.headers.get("Host") or request.url.netloc
return f"{scheme}://{host}"
class CreatePaymentLinkRequest(BaseModel):
amount: float
currency: str = "usd"
@@ -32,6 +45,8 @@ def create_payment_link(
payment: PaymentService = Depends(get_payment),
):
"""Create Stripe payment link for the user."""
if not UserUtils.is_normal_user(request):
return ResponseUtils.error(error_msg=error_msg.NO_PERMISSION)
user = request.scope.get("user")
if not user:
return ResponseUtils.error(message="User not found", code=404)
@@ -49,6 +64,20 @@ def create_payment_link(
return ResponseUtils.error(message="Failed to create Wechat payment link", code=500)
task = OrderMonitorTask.get_instance()
task.add_order_to_monitor(payment_id=payment_info.get("payment_id"))
case "epay":
payment_info = payment.create_epay_payment_link(
user_id=user.id,
amount=body.amount,
base_url=_request_base_url(request),
success_url=body.success_url,
currency=body.currency,
)
if not payment_info:
return ResponseUtils.error(
message="Failed to create Epay payment link (check channel config)", code=500
)
# epay is settled by the async notify; the order monitor has no
# epay client, so it is intentionally not registered here.
case _:
if not body.success_url:
return ResponseUtils.error(message="Success URL is required for Stripe payments", code=400)
@@ -73,6 +102,32 @@ async def callback_stripe(
else:
return ResponseUtils.error(message="Stripe callback failed", code=500)
@router.get("/callback_epay")
@router.post("/callback_epay")
async def callback_epay(
request: Request,
payment: PaymentService = Depends(get_payment),
):
"""Handle an epay (易支付) async notify.
The gateway sends a GET query string (some forks POST form data) and
expects the literal body `success` to stop retrying.
"""
if request.method == "POST":
body = (await request.body()).decode("utf-8", "replace")
params = dict(parse_qsl(body, keep_blank_values=True))
else:
params = dict(request.query_params)
try:
ok = payment.epay_payment_callback(params)
except Exception as e: # never let an unexpected error hide the ack
from services.common.logging_config import get_logger
get_logger(__name__).error(f"Epay callback failed: {e}")
ok = False
return PlainTextResponse("success" if ok else "error", status_code=200 if ok else 500)
# @router.post("/callback_alipay", response_model=dict)
# async def callback_alipay(request: Request, payment: PaymentService = Depends(get_payment)):
# """Handle Alipay payment webhook callback."""
@@ -86,10 +141,24 @@ async def callback_stripe(
@router.get("/order_status", response_model=dict)
async def order_status(
request: Request,
payment_id: str,
payment: PaymentService = Depends(get_payment),
get_user_wallet_history: UserWalletHistoryService = Depends(get_user_wallet_history),
):
"""Check payment order completion status."""
if not UserUtils.is_normal_user(request):
return ResponseUtils.error(error_msg=error_msg.NO_PERMISSION)
if get_user_wallet_history.check_order_complete(payment_id):
return ResponseUtils.success({"status": 1})
# epay fallback: if the async notify never reached us, ask the gateway now.
try:
if payment.sync_epay_order(payment_id):
return ResponseUtils.success({"status": 1})
except Exception:
pass
if get_user_wallet_history.check_order_complete(payment_id):
return ResponseUtils.success({"status": 1})
return ResponseUtils.success({"status": 0})
@@ -1,4 +1,4 @@
from fastapi import APIRouter, Depends, Query, Body
from fastapi import APIRouter, Depends, Query, Body, Request
from sqlalchemy.orm import Session
from services.common.database import get_db
@@ -6,7 +6,8 @@ from services.common.utils.response_utils import ResponseUtils
from services.admin_service.services.payment_channel_service import PaymentChannelService
from services.common.utils.secure_config import decrypt_config, encrypt_config
from services.admin_service.tasks.order_monitor_task import OrderMonitorTask
import json
from services.admin_service.utils.user_utils import UserUtils
from services.common import error_msg
router = APIRouter()
@@ -17,10 +18,13 @@ def get_payment_channel(db: Session = Depends(get_db)) -> PaymentChannelService:
@router.get("/list")
async def payment_channel_list(
request: Request,
page: int = Query(1, description="Current page number"),
page_size: int = Query(15, description="Number of items per page"),
payment_channel_service: PaymentChannelService = Depends(get_payment_channel),
):
if not UserUtils.is_admin(request):
return ResponseUtils.error(error_msg=error_msg.NO_PERMISSION)
"""Get paginated list of payment channels."""
total, list = payment_channel_service.list()
if total == 0:
@@ -45,9 +49,12 @@ async def payment_channel_list(
@router.put("/enable")
async def payment_channel_enable(
request: Request,
payment_channel_service: PaymentChannelService = Depends(get_payment_channel),
body: dict = Body(..., description="Payment channel configuration"),
):
if not UserUtils.is_admin(request):
return ResponseUtils.error(error_msg=error_msg.NO_PERMISSION)
"""Enable a payment channel by ID."""
id = body.get("id")
if not id:
@@ -103,9 +110,12 @@ async def payment_channel_disable(
@router.put("/info")
async def payment_channel_config(
request: Request,
payment_channel_service: PaymentChannelService = Depends(get_payment_channel),
body: dict = Body(..., description="Payment channel configuration"),
):
if not UserUtils.is_admin(request):
return ResponseUtils.error(error_msg=error_msg.NO_PERMISSION)
"""Update payment channel configuration."""
id = body.get("id")
if not id:
@@ -0,0 +1,308 @@
"""User submission controller for MCP/Skill/Plugin marketplace."""
import json
import logging
import os
import uuid
from datetime import datetime
from typing import Optional
from fastapi import APIRouter, Depends, Request, UploadFile, File, Form, HTTPException
from sqlalchemy.orm import Session
from services.common.database import get_db
from services.common.utils.response_utils import ResponseUtils
from services.admin_service.utils.user_utils import UserUtils
from services.common import error_msg
from services.common.models.submission import Submission
from services.common.models.mcp_service import McpService, ChargeType
from services.common.models.mcp_tool_api import McpToolApi, HttpMethod
from services.admin_service.services.openapi_helper import convert_openapi_for_ai
logger = logging.getLogger(__name__)
router = APIRouter()
UPLOAD_DIR = "/tmp/submission_uploads"
os.makedirs(UPLOAD_DIR, exist_ok=True)
def _slugify(text: str) -> str:
"""Convert text to a slug-safe English string (letters, digits, -, _)."""
import re
s = re.sub(r"[^A-Za-z0-9]+", "-", (text or "").strip()).strip("-").lower()
return s or "service"
def _unique_slug(db: Session, text: str) -> str:
base = _slugify(text)
slug = base
counter = 1
while db.query(McpService).filter(McpService.slug_name == slug).first():
slug = f"{base}-{counter}"
counter += 1
return slug
def _publish_mcp_service(db: Session, sub: Submission) -> str:
"""
Create a market listing (mcp_service + mcp_tool_api rows) from an approved
MCP submission. Returns the new service id.
Tool definitions come from the uploaded file when it parses as an OpenAPI
spec; otherwise the service is published without tools (admin can add them
later from the service edit page).
"""
meta = sub.metadata_json or {}
base_url = (meta.get("base_url") or "").strip()
if not base_url:
raise ValueError("MCP submission is missing base_url")
charge_type_raw = (meta.get("charge_type") or "free").strip().lower()
try:
price = float(meta.get("price") or 0)
except (TypeError, ValueError):
price = 0.0
try:
free_daily_limit = int(meta.get("free_daily_limit")) if meta.get("free_daily_limit") else None
except (TypeError, ValueError):
free_daily_limit = None
if free_daily_limit is not None and free_daily_limit <= 0:
free_daily_limit = None
service_type = (meta.get("service_type") or "openapi").strip() or "openapi"
tags = meta.get("tags") or ""
if isinstance(tags, list):
tags = ",".join(str(t).strip() for t in tags if str(t).strip())
# Best-effort: parse the uploaded file as an OpenAPI spec to build tool rows.
openapi_data = None
if sub.file_path and os.path.exists(sub.file_path):
try:
with open(sub.file_path, "r", encoding="utf-8", errors="ignore") as f:
content = f.read()
openapi_data = convert_openapi_for_ai(content)
if not getattr(openapi_data, "apis", None):
openapi_data = None
except Exception as e: # noqa: BLE001 - file may not be an OpenAPI doc
logger.info("Submission %s file is not an OpenAPI spec: %s", sub.id, e)
openapi_data = None
service = McpService()
service.id = str(uuid.uuid4())
service.name = sub.name
service.slug_name = _unique_slug(db, sub.name)
desc = (sub.description or "").strip() or sub.name
service.short_description = desc[:255]
service.long_description = desc
service.base_url = base_url
service.headers = "[]"
service.charge_type = ChargeType.PER_CALL if (charge_type_raw in ("paid", "per_call") and price > 0) else ChargeType.FREE
service.price = price if service.charge_type == ChargeType.PER_CALL else 0.0
service.free_daily_limit = free_daily_limit if service.charge_type == ChargeType.FREE else None
service.tags = tags
service.service_type = service_type
service.enabled = 1 # auto-publish: immediately visible in the market
db.add(service)
db.flush()
tool_count = 0
if openapi_data is not None:
for api in openapi_data.apis:
try:
http_method = HttpMethod(api.method)
except ValueError:
# HEAD/OPTIONS/TRACE are not part of the supported tool enum
continue
tool = McpToolApi()
tool.id = str(uuid.uuid4())
tool.service_id = service.id
tool.name = api.summary or f"{api.method} {api.path}"
tool.description = api.description or api.summary or tool.name
tool.path = api.path
tool.method = http_method
tool.header_parameters = json.dumps(api.header_parameters) if api.header_parameters else ""
tool.query_parameters = json.dumps(api.query_parameters) if api.query_parameters else ""
tool.path_parameters = json.dumps(api.path_parameters) if api.path_parameters else ""
tool.request_body_schema = json.dumps(api.request_body_schema) if api.request_body_schema else ""
tool.response_schema = json.dumps(api.response_schema) if api.response_schema else ""
tool.response_examples = json.dumps(api.response_examples) if api.response_examples else ""
tool.response_headers = json.dumps(api.response_headers) if api.response_headers else ""
tool.operation_examples = json.dumps(api.operation_examples) if api.operation_examples else ""
tool.enabled = 1
tool.is_deleted = 0
db.add(tool)
tool_count += 1
db.flush()
logger.info(
"Auto-published submission %s -> service %s (%d tools, base_url=%s)",
sub.id, service.id, tool_count, base_url,
)
return service.id
@router.post("/submission", summary="User submit MCP/Skill/Plugin")
async def create_submission(
request: Request,
type: str = Form(...),
name: str = Form(...),
description: str = Form(None),
file: UploadFile = File(None),
base_url: str = Form(None),
service_type: str = Form(None),
tags: str = Form(None),
charge_type: str = Form(None),
price: float = Form(None),
free_daily_limit: int = Form(None),
db: Session = Depends(get_db),
):
try:
user_id = UserUtils.get_request_user_id(request)
except Exception:
user_id = None
if not user_id:
return ResponseUtils.error(error_msg=error_msg.NO_PERMISSION)
if type not in ("mcp", "skill", "plugin"):
return ResponseUtils.error(error_msg=error_msg.INVALID_PARAMETER)
# MCP submissions must declare where the service lives so that approving
# the submission can auto-publish a working market listing.
base_url = (base_url or "").strip()
if type == "mcp" and not base_url:
return ResponseUtils.error(message="base_url is required for MCP submissions")
file_path = None
if file:
safe_name = f"{uuid.uuid4()}_{file.filename}"
file_path = os.path.join(UPLOAD_DIR, safe_name)
with open(file_path, "wb") as f:
f.write(await file.read())
metadata = {}
if type == "mcp":
metadata = {
"base_url": base_url,
"service_type": (service_type or "openapi").strip() or "openapi",
"tags": (tags or "").strip(),
"charge_type": (charge_type or "free").strip() or "free",
"price": price,
"free_daily_limit": free_daily_limit,
}
sub = Submission(
user_id=user_id,
type=type,
name=name,
description=description,
file_path=file_path,
metadata_json=metadata or None,
status="pending",
)
db.add(sub)
db.commit()
db.refresh(sub)
return ResponseUtils.success(data={"id": sub.id, "status": sub.status})
@router.get("/submissions", summary="List user submissions")
def list_submissions(request: Request, db: Session = Depends(get_db)):
try:
user_id = UserUtils.get_request_user_id(request)
except Exception:
user_id = None
if not user_id:
return ResponseUtils.error(error_msg=error_msg.NO_PERMISSION)
subs = db.query(Submission).filter(Submission.user_id == user_id).order_by(Submission.created_at.desc()).all()
data = [
{
"id": s.id,
"type": s.type,
"name": s.name,
"description": s.description,
"status": s.status,
"metadata_json": s.metadata_json,
"created_at": s.created_at.isoformat() if s.created_at else None,
"review_note": s.review_note,
}
for s in subs
]
return ResponseUtils.success(data=data)
# Admin endpoints
@router.get("/admin/submissions", summary="Admin list submissions")
def admin_list_submissions(request: Request, status: str = None, db: Session = Depends(get_db)):
if not UserUtils.is_admin(request):
return ResponseUtils.error(error_msg=error_msg.NO_PERMISSION)
q = db.query(Submission)
if status:
q = q.filter(Submission.status == status)
subs = q.order_by(Submission.created_at.desc()).all()
data = [
{
"id": s.id,
"user_id": s.user_id,
"type": s.type,
"name": s.name,
"description": s.description,
"file_path": s.file_path,
"metadata_json": s.metadata_json,
"status": s.status,
"created_at": s.created_at.isoformat() if s.created_at else None,
"review_note": s.review_note,
"reviewed_at": s.reviewed_at.isoformat() if s.reviewed_at else None,
}
for s in subs
]
return ResponseUtils.success(data=data)
@router.put("/admin/submission/{sub_id}/approve", summary="Approve submission (auto-publish to market)")
def approve_submission(sub_id: str, request: Request, db: Session = Depends(get_db)):
if not UserUtils.is_admin(request):
return ResponseUtils.error(error_msg=error_msg.NO_PERMISSION)
sub = db.query(Submission).filter(Submission.id == sub_id).first()
if not sub:
return ResponseUtils.error(error_msg=error_msg.NOT_FOUND)
if sub.status != "pending":
return ResponseUtils.error(message="Submission has already been processed")
service_id = None
if sub.type == "mcp":
try:
service_id = _publish_mcp_service(db, sub)
except Exception as e: # noqa: BLE001 - report the exact reason to the admin
db.rollback()
logger.error("Auto-publish failed for submission %s: %s", sub_id, e)
return ResponseUtils.error(error_msg=f"Auto-publish failed: {e}")
sub.status = "approved"
sub.reviewer_id = UserUtils.get_request_user_id(request)
sub.reviewed_at = datetime.utcnow()
if service_id:
meta = sub.metadata_json or {}
meta["published_service_id"] = service_id
sub.metadata_json = meta
db.commit()
return ResponseUtils.success(data={"service_id": service_id})
@router.put("/admin/submission/{sub_id}/reject", summary="Reject submission")
def reject_submission(sub_id: str, request: Request, body: dict = None, db: Session = Depends(get_db)):
if not UserUtils.is_admin(request):
return ResponseUtils.error(error_msg=error_msg.NO_PERMISSION)
sub = db.query(Submission).filter(Submission.id == sub_id).first()
if not sub:
return ResponseUtils.error(error_msg=error_msg.NOT_FOUND)
sub.status = "rejected"
sub.reviewer_id = UserUtils.get_request_user_id(request)
sub.reviewed_at = datetime.utcnow()
if body:
sub.review_note = body.get("note")
db.commit()
return ResponseUtils.success()
@@ -3,7 +3,7 @@
Provides endpoints to read and update platform configurations, homepage content,
and to test email settings. All responses and comments are standardized in English.
"""
from fastapi import APIRouter, Depends, Body
from fastapi import APIRouter, Depends, Body, Request
import json
from sqlalchemy.orm import Session
from services.common.database import get_db
@@ -11,6 +11,8 @@ from services.common.utils.response_utils import ResponseUtils
from services.common.utils.email_utils import EmailUtils
from services.admin_service.services.sys_config_service import SysConfigService
from services.admin_service.services.user_service import UserService
from services.admin_service.utils.user_utils import UserUtils
from services.common import error_msg
from services.admin_service.services.platform_report_service import PlatformReportService
from services.admin_service.constants import sys_config_key
from services.common.logging_config import get_logger
@@ -32,6 +34,58 @@ def get_sysconfig_service(db: Session = Depends(get_db)) -> SysConfigService:
def get_user_service(db: Session = Depends(get_db)) -> UserService:
return UserService(db)
@router.get("/free-quota")
def get_free_quota(
sysconfig_service: SysConfigService = Depends(get_sysconfig_service),
):
"""Platform-wide daily cap applied to every free service (None = unlimited)."""
raw = sysconfig_service.get_value_by_key(sys_config_key.KEY_FREE_GLOBAL_DAILY_LIMIT)
limit = None
if raw not in (None, ""):
try:
limit = int(raw)
except (TypeError, ValueError):
limit = None
if limit is not None and limit <= 0:
limit = None
return ResponseUtils.success(data={"free_global_daily_limit": limit})
@router.put("/free-quota")
def set_free_quota(
request: Request,
body: dict = Body(...),
sysconfig_service: SysConfigService = Depends(get_sysconfig_service),
):
"""Update the platform-wide daily cap. Empty value clears it (unlimited)."""
if not UserUtils.is_admin(request):
return ResponseUtils.error(error_msg=error_msg.NO_PERMISSION)
raw = body.get("free_global_daily_limit")
if raw in (None, ""):
sysconfig_service.set_value_by_key(
sys_config_key.KEY_FREE_GLOBAL_DAILY_LIMIT, "", "Global free daily limit"
)
return ResponseUtils.success(data={"free_global_daily_limit": None})
try:
limit = int(raw)
except (TypeError, ValueError):
return ResponseUtils.error(
message="free_global_daily_limit must be a non-negative integer", code=400
)
if limit < 0:
return ResponseUtils.error(
message="free_global_daily_limit must be a non-negative integer", code=400
)
sysconfig_service.set_value_by_key(
sys_config_key.KEY_FREE_GLOBAL_DAILY_LIMIT, str(limit), "Global free daily limit"
)
return ResponseUtils.success(data={"free_global_daily_limit": limit})
@router.get("/homepage")
def get_homepage(
sysconfig_service: SysConfigService = Depends(get_sysconfig_service),
@@ -91,9 +145,12 @@ def update_homepage(
@router.get("/info")
def get_sysconfig(
request: Request,
sysconfig_service: SysConfigService = Depends(get_sysconfig_service),
user_service: UserService = Depends(get_user_service),
):
if not UserUtils.is_admin(request):
return ResponseUtils.error(error_msg=error_msg.NO_PERMISSION)
"""Get all system configuration settings."""
admin_user = user_service.get_admin_user()
# Batch read configurations (small table)
@@ -173,10 +230,13 @@ def get_sysconfig(
@router.put("/info")
def set_sysconfig(
request: Request,
body: dict = Body(...),
user_service: UserService = Depends(get_user_service),
sysconfig_service: SysConfigService = Depends(get_sysconfig_service),
):
if not UserUtils.is_admin(request):
return ResponseUtils.error(error_msg=error_msg.NO_PERMISSION)
"""Update system configuration settings."""
try:
platform_name = ""
@@ -334,7 +394,7 @@ def set_sysconfig(
# Log error but continue with main operation - reporting failure should not affect configuration update
logger.warning(f"Failed to initiate platform reporting: {e}")
return get_sysconfig(sysconfig_service, user_service)
return get_sysconfig(request, sysconfig_service, user_service)
except Exception as e:
return ResponseUtils.error(f"Failed to update system configuration: {str(e)}")
@@ -3,7 +3,10 @@ import hashlib
import os
import shutil
from datetime import datetime
from fastapi import Request
from services.common.utils.response_utils import ResponseUtils
from services.admin_service.utils.user_utils import UserUtils
from services.common import error_msg
router = APIRouter()
@@ -14,6 +17,7 @@ and store files under date-based directories.
"""
@router.post("/image")
async def upload(
request: Request,
img: UploadFile = File(None),
sha256: str = Form(None),
):
@@ -24,6 +28,8 @@ async def upload(
- Saves the file under `uploads/images/<YYYYMMDD>/`
- Returns the relative `file_path`
"""
if not UserUtils.is_admin(request):
return ResponseUtils.error(error_msg=error_msg.NO_PERMISSION)
# 1. Get uploaded file name
if not img:
return ResponseUtils.error(message="No file provided", code=400)
@@ -11,6 +11,9 @@ from services.admin_service.services.user_task_service import UserTaskService
from services.admin_service.services.auth_service import AuthService
from services.admin_service.services.resource_group_service import ResourceGroupService
from services.admin_service.services.user_wallet_history_service import UserWalletHistoryService
from services.common import error_msg
from typing import Optional,List
@@ -6,6 +6,9 @@ from services.common.response.apikey_response import ApikeyResponse
from services.admin_service.services.user_apikey_service import UserApiKeyService
from services.admin_service.utils.user_utils import UserUtils
from services.common.models.user_apikey import UserApiKey
from services.common import error_msg
router = APIRouter()
@@ -29,6 +32,8 @@ def convert_to_apikey_response(user_apikey: UserApiKey) -> ApikeyResponse:
@router.post("/info", summary="add apikey")
def add_apikey(request: Request, apikey_service: UserApiKeyService = Depends(get_apikey_service), body: dict = Body(...)):
"""Create a new API key for the user."""
if not UserUtils.is_normal_user(request):
return ResponseUtils.error(error_msg=error_msg.NO_PERMISSION)
user_id = UserUtils.get_request_user_id(request)
name = body.get("name")
if name is None:
@@ -46,6 +51,8 @@ def add_apikey(request: Request, apikey_service: UserApiKeyService = Depends(get
@router.get("/list", summary="get user apikey list")
def get_user_apikey_list(request: Request, apikey_service: UserApiKeyService = Depends(get_apikey_service)):
"""Get list of API keys for the current user."""
if not UserUtils.is_normal_user(request):
return ResponseUtils.error(error_msg=error_msg.NO_PERMISSION)
user_id = UserUtils.get_request_user_id(request)
apikey_list = apikey_service.get_by_user_id(user_id)
data = [convert_to_apikey_response(item) for item in apikey_list]
@@ -55,6 +62,8 @@ def get_user_apikey_list(request: Request, apikey_service: UserApiKeyService = D
@router.delete("/info", summary="delete user apikey")
def delete_apikey(request: Request, apikey_service: UserApiKeyService = Depends(get_apikey_service), body: dict = Body(...)):
"""Delete an API key by ID for the current user."""
if not UserUtils.is_normal_user(request):
return ResponseUtils.error(error_msg=error_msg.NO_PERMISSION)
user_id = UserUtils.get_request_user_id(request)
id = body.get("apikey_id")
if id is None:
@@ -68,6 +77,8 @@ def delete_apikey(request: Request, apikey_service: UserApiKeyService = Depends(
@router.put("/info", summary="modify user apikey")
def modify_apikey(request: Request, apikey_service: UserApiKeyService = Depends(get_apikey_service), body: dict = Body(...)):
"""Update API key information for the current user."""
if not UserUtils.is_normal_user(request):
return ResponseUtils.error(error_msg=error_msg.NO_PERMISSION)
user_id = UserUtils.get_request_user_id(request)
id = body.get("apikey_id")
name = body.get("name")
@@ -63,11 +63,13 @@ async def update_user_resource_group(
@router.delete("/account", summary="Delete user account")
async def delete_user(
request: Request,
id: str,
user_service: UserService = Depends(get_user_service),
):
"""Delete user by ID."""
if not UserUtils.is_admin(request):
return ResponseUtils.error(error_msg=error_msg.NO_PERMISSION)
# Validate user ID parameter
ValidationUtils.require_non_empty_string(id, "id")
@@ -88,6 +90,7 @@ async def delete_user(
@router.get("/account/list", summary="Get user list with pagination")
async def get_user_list(
request: Request,
page: int = Query(1, description="Page number (starts from 1)"),
page_size: int = Query(15, description="Number of items per page"),
keyword: str = Query(None, description="Search keyword for email or name"),
@@ -95,7 +98,8 @@ async def get_user_list(
user_wallet_service: UserWalletService = Depends(get_user_wallet_service),
):
"""Get paginated list of users with wallet information."""
if not UserUtils.is_admin(request):
return ResponseUtils.error(error_msg=error_msg.NO_PERMISSION)
# Validate pagination parameters
validated_page, validated_page_size = ValidationUtils.validate_pagination(page, page_size)
@@ -126,6 +130,8 @@ async def recharge_balance(
request: Request,
payment_service: PaymentService = Depends(get_payment_service),
):
if not UserUtils.is_admin(request):
return ResponseUtils.error(error_msg=error_msg.NO_PERMISSION)
body = await request.body()
body_str = body.decode('utf-8')
timestamp=request.headers.get("x-xpack-timestamp")
@@ -2,12 +2,14 @@
User statistics controller
"""
from fastapi import APIRouter, Depends, Query
from fastapi import APIRouter, Depends, Query, Request
from sqlalchemy.orm import Session
from services.common.database import get_db
from services.common.utils.response_utils import ResponseUtils
from services.admin_service.services.user_stats_service import UserStatsService
from services.admin_service.utils.user_utils import UserUtils
from services.common.logging_config import get_logger
from services.common import error_msg
logger = get_logger(__name__)
router = APIRouter()
@@ -15,10 +17,13 @@ router = APIRouter()
@router.get("/key_call_tool")
async def get_key_call_tool(
request: Request,
apikey_id: str = Query(..., description="apikey ID (not the apikey itself)"),
last_day: int = Query(30, description="Days to query, default 30"),
db: Session = Depends(get_db),
):
if not UserUtils.is_normal_user(request):
return ResponseUtils.error(error_msg=error_msg.NO_PERMISSION)
"""
Get tool call statistics by apikey
"""
@@ -51,12 +56,12 @@ async def get_key_call_tool(
except ValueError as e:
# Handle business logic errors (invalid apikey_id)
error_msg = str(e)
logger.error(f"Business logic error: {error_msg}")
return ResponseUtils.error(error_msg, code=400)
err_msg = str(e)
logger.error(f"Business logic error: {err_msg}")
return ResponseUtils.error(err_msg, code=400)
except Exception as e:
# Handle unexpected errors
error_msg = f"Failed to retrieve call tool statistics: {str(e)}"
logger.error(f"Unexpected error in get_key_call_tool_count: {error_msg}", exc_info=True)
err_msg = f"Failed to retrieve call tool statistics: {str(e)}"
logger.error(f"Unexpected error in get_key_call_tool_count: {err_msg}", exc_info=True)
return ResponseUtils.error("Internal server error occurred while retrieving statistics", code=500)
+2
View File
@@ -26,6 +26,7 @@ from services.admin_service.controllers import email_test
from services.admin_service.controllers import upload
from services.admin_service.controllers import user_task
from services.admin_service.controllers import resource_group
from services.admin_service.controllers import submission
@@ -170,6 +171,7 @@ app.include_router(email_test.router, prefix="/api/email_test")
app.include_router(upload.router, prefix="/api/upload")
app.include_router(user_task.router, prefix="/api/onboarding")
app.include_router(resource_group.router, prefix="/api/resource_group")
app.include_router(submission.router, prefix="/api/submission")
@@ -11,17 +11,20 @@ class McpCallLogRepository:
def __init__(self, db: Session):
self.db = db
def create(self, call_log: McpCallLog) -> McpCallLog:
def create(self, call_log: McpCallLog, commit: bool = True) -> McpCallLog:
self.db.add(call_log)
self.db.commit()
self.db.refresh(call_log)
if commit:
self.db.commit()
self.db.refresh(call_log)
else:
self.db.flush()
return call_log
def get_by_id(self, log_id: str) -> Optional[McpCallLog]:
return self.db.query(McpCallLog).filter(McpCallLog.id == log_id).first()
def update_status(
self, log_id: str, status: ProcessStatus, error_msg: Optional[str] = None, wallet_history_id: Optional[str] = None
self, log_id: str, status: ProcessStatus, error_msg: Optional[str] = None, wallet_history_id: Optional[str] = None, commit: bool = True
) -> bool:
log_record = self.get_by_id(log_id)
if log_record:
@@ -30,7 +33,10 @@ class McpCallLogRepository:
log_record.error_msg = error_msg
if wallet_history_id:
log_record.wallet_history_id = wallet_history_id
self.db.commit()
if commit:
self.db.commit()
else:
self.db.flush()
return True
return False
@@ -1,7 +1,7 @@
from datetime import date, datetime, timedelta
from typing import Optional, List, Tuple, cast
from sqlalchemy.orm import Session
from sqlalchemy import func, literal_column
from sqlalchemy import Boolean, func, literal_column
from sqlalchemy.exc import IntegrityError
from sqlalchemy.dialects.mysql import insert as mysql_insert
from sqlalchemy.sql.schema import Table
@@ -91,14 +91,12 @@ class StatsMcpServiceDateRepository:
total = query.scalar()
return int(total or 0)
def increment(self, service_id: str, stats_date: datetime, inc: int = 1) -> None:
def increment(self, service_id: str, stats_date: datetime, inc: int = 1, commit: bool = True):
"""Atomically increment call_count for (service_id, stats_date) under high concurrency.
Implementation uses MySQL's ON DUPLICATE KEY UPDATE to avoid race conditions.
If the row does not exist, it will be inserted with call_count=inc; otherwise, call_count is increased.
"""
# Logger for debug purposes
print(f"Incrementing call count for service {service_id} on {stats_date} by {inc}")
# Cast mapped table for typing compatibility with mysql_insert
table: Table = cast(Table, StatsMcpServiceDate.__table__)
@@ -114,7 +112,10 @@ class StatsMcpServiceDateRepository:
updated_at=func.current_timestamp(),
)
self.db.execute(ondup)
self.db.commit()
if commit:
self.db.commit()
else:
self.db.flush()
def increment_fallback(self, service_id: str, stats_date: datetime, inc: int = 1) -> None:
"""Fallback increment strategy without dialect-specific upsert.
@@ -15,8 +15,8 @@ class UserRepository:
def get_by_email(self, email: str) -> Optional[User]:
return self.db.query(User).filter(User.email == email).first()
def get_by_account(self, name: str) -> Optional[User]:
return self.db.query(User).filter(User.name == name).first()
def get_by_account(self, name: str, role_id: int = 1) -> Optional[User]:
return self.db.query(User).filter(User.name == name, User.role_id == role_id).first()
def update_password(self,user_id:str, password:str) -> Optional[User]:
user = self.get_by_id(user_id)
@@ -181,6 +181,23 @@ class UserWalletHistoryRepository:
self.db.refresh(history)
return history
def set_callback_data(self, record_id: str, data: str) -> bool:
"""Persist raw gateway metadata (e.g. charged CNY amount) on a record."""
try:
obj = self.db.query(UserWalletHistory).filter(
UserWalletHistory.id == record_id
).first()
if not obj:
self.logging.error(f"UserWalletHistory not found: id={record_id}")
return False
obj.callback_data = data
self.db.commit()
return True
except Exception as e:
self.logging.error(f"Error setting callback_data: id={record_id}, error={str(e)}")
self.db.rollback()
return False
def deposit_complete(self, id: str, transaction_id: str) -> bool:
"""
Mark deposit order as completed and safely update balance_after.
@@ -296,7 +313,7 @@ class UserWalletHistoryRepository:
)
return total, history
def add_consume_record(self, wallet_history: UserWalletHistory) -> Optional[UserWalletHistory]:
def add_consume_record(self, wallet_history: UserWalletHistory, commit: bool = True) -> Optional[UserWalletHistory]:
"""
Add consume record to database.
@@ -310,12 +327,18 @@ class UserWalletHistoryRepository:
# Add the wallet history record to database
# The created_at and updated_at fields should be set by the caller
self.db.add(wallet_history)
self.db.commit()
self.db.refresh(wallet_history)
if commit:
self.db.commit()
self.db.refresh(wallet_history)
else:
self.db.flush()
return wallet_history
except Exception as e:
self.logging.error(f"Failed to add consume record: {str(e)}")
self.db.rollback()
try:
self.db.rollback()
except Exception:
pass
return None
def order_list(self, payment_method: str, status: int, start: Optional[datetime] = None, end: Optional[datetime] = None) -> List[UserWalletHistory]:
@@ -435,4 +458,4 @@ class UserWalletHistoryRepository:
result.append({"stats_day": current_day, "count": amounts_by_day.get(current_day, 0.0)})
current_day += timedelta(days=1)
return result
return result
@@ -32,13 +32,16 @@ class UserWalletRepository:
"""Get user wallet with row-level lock for update"""
return self.db.query(UserWallet).filter(UserWallet.user_id == user_id).with_for_update().first()
def update_balance(self, user_id: str, new_balance: float) -> bool:
def update_balance(self, user_id: str, new_balance: float, commit: bool = True) -> bool:
"""Update user balance"""
wallet = self.get_by_user_id(user_id)
if wallet:
wallet.balance = new_balance
wallet.updated_at = datetime.now(timezone.utc)
self.db.commit()
if commit:
self.db.commit()
else:
self.db.flush()
return True
return False
@@ -3,6 +3,7 @@ Billing message handler service
"""
import json
from re import finditer
import uuid
import logging
from datetime import datetime, timezone
@@ -44,41 +45,60 @@ class BillingMessageHandler:
Returns:
bool: Whether processing was successful
"""
call_log_id: Optional[str] = None
tx = self.db.begin()
try:
# Parse message
billing_message = self._parse_message(message_data)
if not billing_message:
tx.rollback()
return False
# Create API call log
call_log_id = self._create_call_log(billing_message)
if not call_log_id:
tx.rollback()
return False
# Update stats_mcp_service_date record
# Bucket stats by hour (UTC), aligned to full hours
start_dt = billing_message.call_start_time
start_utc = start_dt.replace(tzinfo=timezone.utc) if start_dt.tzinfo is None else start_dt.astimezone(timezone.utc)
# Store as naive UTC datetime to match MySQL DATETIME behavior
stats_date = start_utc.replace(minute=0, second=0, microsecond=0, tzinfo=None)
self.stats_repo.increment(billing_message.service_id, stats_date, 1)
stats_date = start_utc.replace(hour=0, minute=0, second=0, microsecond=0, tzinfo=None)
self.stats_repo.increment(billing_message.service_id, stats_date, 1, commit=False)
# Process billing logic
if billing_message.call_success and billing_message.unit_price > 0:
success = self._process_billing(billing_message, call_log_id)
success = self._process_billing(billing_message, call_log_id, commit=False)
if not success:
# Update record status to failed
self.call_log_repo.update_status(call_log_id, ProcessStatus.FAILED, "Billing processing failed")
self.call_log_repo.update_status(call_log_id, ProcessStatus.FAILED, "Billing processing failed", commit=False)
tx.rollback()
return False
# Update record status to processed
self.call_log_repo.update_status(call_log_id, ProcessStatus.PROCESSED)
logger.info(f"Billing message processed successfully - User ID: {billing_message.user_id}, Tool: {billing_message.tool_name}")
return True
if not self.call_log_repo.update_status(call_log_id, ProcessStatus.PROCESSED, commit=False):
tx.rollback()
return False
tx.commit()
logger.info(f"Billing message processed successfully - User ID: {billing_message.user_id}, Tool: {billing_message.tool_name}")
# Update wallet cache after successful commit
try:
wallet = self.wallet_repo.get_by_user_id(billing_message.user_id)
if wallet:
self._update_wallet_cache(billing_message.user_id, Decimal(str(wallet.balance)))
except Exception:
pass
return True
except Exception as e:
logger.error(f"Failed to process billing message: {str(e)}", exc_info=True)
try:
tx.rollback()
except Exception:
pass
if call_log_id:
try:
self.call_log_repo.update_status(call_log_id, ProcessStatus.FAILED, "Billing processing exception")
except Exception:
logger.exception("Failed to update call log status after exception")
return False
finally:
if tx.is_active:
tx.rollback()
def _parse_message(self, message_data: dict) -> Optional[BillingMessage]:
"""
@@ -146,14 +166,14 @@ class BillingMessageHandler:
updated_at=datetime.now(timezone.utc),
)
created_log = self.call_log_repo.create(call_log)
created_log = self.call_log_repo.create(call_log, commit=False)
return created_log.id
except Exception as e:
logger.error(f"Failed to create API call log record: {str(e)}")
return None
def _process_billing(self, billing_message: BillingMessage, call_log_id: str) -> bool:
def _process_billing(self, billing_message: BillingMessage, call_log_id: str, commit: bool = True) -> bool:
"""
Process actual billing logic
@@ -183,7 +203,7 @@ class BillingMessageHandler:
# Execute deduction
new_balance = current_balance - amount
success = self.wallet_repo.update_balance(user_id, float(new_balance))
success = self.wallet_repo.update_balance(user_id, float(new_balance), commit=commit)
if not success:
logger.error(f"Failed to update user wallet balance - User ID: {user_id}")
return False
@@ -206,22 +226,15 @@ class BillingMessageHandler:
updated_at=now,
)
created_history = self.wallet_history_repo.add_consume_record(wallet_history)
created_history = self.wallet_history_repo.add_consume_record(wallet_history, commit=commit)
if not created_history:
logger.error(f"Failed to create wallet history record - User ID: {user_id}")
# Rollback balance update
self.wallet_repo.update_balance(user_id, float(current_balance))
return False
logger.info(f"Wallet history record created successfully - User ID: {user_id}, History ID: {history_id}, Created at: {now}")
# Update API call record's actual deduction amount and associated history record ID
self.call_log_repo.update_status(call_log_id, ProcessStatus.PROCESSED, None, history_id)
# Update wallet balance cache in Redis
self._update_wallet_cache(user_id, new_balance)
self.call_log_repo.update_status(call_log_id, ProcessStatus.PROCESSED, None, history_id, commit=commit)
logger.info(
f"Billing processing completed successfully - User ID: {user_id}, Amount deducted: {amount}, Balance: {current_balance} -> {new_balance}"
@@ -228,6 +228,21 @@ class McpManagerService:
if body["output_token_price"] != existing_service.output_token_price:
self._delete_service_price_cache(service_id)
existing_service.output_token_price = body["output_token_price"]
if "free_daily_limit" in body:
raw_limit = body["free_daily_limit"]
if raw_limit in (None, ""):
new_limit = None
else:
try:
new_limit = int(raw_limit)
except (TypeError, ValueError):
raise ValueError("free_daily_limit must be a non-negative integer")
if new_limit < 0:
raise ValueError("free_daily_limit must be a non-negative integer")
if new_limit != existing_service.free_daily_limit:
# api_service reads this out of the 1h price cache
self._delete_service_price_cache(service_id)
existing_service.free_daily_limit = new_limit
if "tags" in body and body["tags"] is not None:
# If provided as array, convert to string for storage
if isinstance(body["tags"], list):
@@ -316,6 +331,7 @@ class McpManagerService:
"input_token_price": str(float(service.input_token_price)) if service.input_token_price and service.charge_type == ChargeType.PER_TOKEN else "0.00",
"output_token_price": str(float(service.output_token_price)) if service.output_token_price and service.charge_type == ChargeType.PER_TOKEN else "0.00",
"enabled": service.enabled,
"free_daily_limit": service.free_daily_limit,
"tags": parse_tags_to_array(service.tags),
"apis": [{"id": api.id, "name": api.name, "description": api.description,"url":api.path} for api in apis],
}
@@ -1,3 +1,4 @@
import json
import stripe
import logging
import asyncio
@@ -11,10 +12,12 @@ from services.admin_service.repositories.user_wallet_history_repository import U
from services.admin_service.repositories.sys_config_repository import SysConfigRepository
from services.admin_service.services.payment_channel_service import PaymentChannelService
from services.admin_service.utils.alipay_client import AlipayClient
from services.admin_service.utils.epay_client import EpayClient, verify_sign
from services.admin_service.utils.wxpay_client import WxPayClient,WxPayConfig
from services.admin_service.constants.sys_config_key import (
KEY_PLATFORM_NAME
)
from services.common.models.user_wallet_history import PaymentMethod
from services.common.redis import redis_client
logger = logging.getLogger(__name__)
@@ -242,6 +245,202 @@ class PaymentService:
logger.error(f"Failed to create WeChat payment link: {str(e)}")
return None
def create_epay_payment_link(
self,
user_id: str,
amount: float,
base_url: str,
success_url: str = "",
currency: str = "cny",
) -> Optional[dict]:
"""Create an epay (易支付) page-pay link for the user.
The gateway settles in CNY while wallet balances use the platform unit,
so `exchange_rate` (CNY per 1 balance unit, default 1) converts the amount.
"""
config = self.payment_channel_service.get_config("epay")
if config is None or not config.get("enable"):
logger.error("Epay payment channel is not enabled or configured")
return None
gateway = str(config.get("gateway") or "").strip()
pid = str(config.get("pid") or "").strip()
key = str(config.get("key") or "").strip()
missing = [n for n, v in (("gateway", gateway), ("pid", pid), ("key", key)) if not v]
if missing:
logger.error(f"Epay payment configuration incomplete. Missing: {', '.join(missing)}")
return None
if amount is None or float(amount) <= 0:
raise ValueError("Invalid amount.")
user = self.user_repository.get_by_id(user_id)
if not user:
logger.error(f"User with ID {user_id} not found.")
raise ValueError("User not found.")
# Pending deposit record; its id is used as epay's out_trade_no.
history = self.user_wallet_history_repository.add_deposit(
user_id=user_id, amount=float(amount), payment_method="epay"
)
if history is None:
logger.error("Failed to create user wallet history for deposit.")
raise RuntimeError("Failed to create payment.")
try:
rate = float(config.get("exchange_rate") or 1)
except (TypeError, ValueError):
rate = 1.0
if rate <= 0:
rate = 1.0
money_cny = round(float(amount) * rate, 2)
if money_cny < 0.01:
money_cny = 0.01
base = (base_url or "").rstrip("/")
notify_url = str(config.get("notify_url") or "").strip() or f"{base}/api/payment/callback_epay"
return_url = str(config.get("return_url") or "").strip() or success_url or (base + "/")
platform_name = self.sys_config_repository.get_by_key(KEY_PLATFORM_NAME)
if platform_name is None:
platform_name = "XPack"
client = EpayClient(gateway=gateway, pid=pid, key=key)
pay_url = client.build_submit_url(
out_trade_no=history.id,
name=f"[Deposit] {platform_name}",
money=money_cny,
notify_url=notify_url,
return_url=return_url,
pay_type=str(config.get("pay_type") or "").strip(),
)
if not pay_url:
logger.error(f"Failed to build epay payment URL for order {history.id}")
return None
self.user_wallet_history_repository.set_callback_data(
history.id,
json.dumps({"money_cny": money_cny, "exchange_rate": rate}, ensure_ascii=False),
)
logger.info(
f"Created epay payment link for order {history.id}: {money_cny} CNY "
f"({amount} x {rate})"
)
return {"payment_link": pay_url, "payment_id": history.id}
def epay_payment_callback(self, params: dict) -> bool:
"""Handle an epay (易支付) async notify.
Verifies the MD5 signature, the trade status and the notified amount,
then credits the wallet exactly once (idempotent).
"""
config = self.payment_channel_service.get_config("epay")
if config is None or not config.get("enable"):
logger.error("Epay payment channel is disabled, dropping notify")
return False
key = str(config.get("key") or "")
out_trade_no = str(params.get("out_trade_no") or "")
if not verify_sign(params, key):
logger.error(f"Epay notify signature verification failed for order {out_trade_no}")
return False
trade_status = str(params.get("trade_status") or "")
if trade_status != "TRADE_SUCCESS":
logger.info(f"Epay notify order {out_trade_no} status '{trade_status}' ignored")
return True # acknowledged: nothing to credit, gateway should stop retrying
if not out_trade_no:
logger.error("Epay notify without out_trade_no")
return False
history = self.user_wallet_history_repository.get_by_id(out_trade_no)
if history is None:
logger.error(f"Epay notify for unknown order {out_trade_no}")
return False
method = getattr(history.payment_method, "value", history.payment_method)
if method != PaymentMethod.EPAY.value:
logger.error(f"Epay notify for order {out_trade_no} paid via '{method}'")
return False
try:
notified_money = round(float(params.get("money")), 2)
except (TypeError, ValueError):
logger.error(f"Epay notify for order {out_trade_no} has invalid money")
return False
charged_money = None
try:
meta = json.loads(history.callback_data) if history.callback_data else {}
charged_money = round(float(meta.get("money_cny")), 2)
except (TypeError, ValueError, json.JSONDecodeError):
charged_money = None
if charged_money is not None and abs(charged_money - notified_money) > 0.01:
logger.error(
f"Epay notify amount mismatch for order {out_trade_no}: "
f"notified={notified_money} charged={charged_money}"
)
return False
if history.status == 1:
logger.info(f"Epay notify order {out_trade_no} already completed")
return True
# `transaction_id` is unique: use the gateway trade_no as the channel id.
channel_tx = str(params.get("trade_no") or "") or f"epay-{out_trade_no}"
return self._add_wallet_balance(payment_id=out_trade_no, payment_channel_id=channel_tx)
def sync_epay_order(self, payment_id: str) -> bool:
"""Fallback settlement: query the gateway for a pending epay order.
Used when the async notify could not reach us (e.g. the site has no
public URL). Credits the wallet when the gateway reports payment.
Safe to call repeatedly -- already settled orders are a no-op.
"""
if not payment_id:
return False
history = self.user_wallet_history_repository.get_by_id(payment_id)
if history is None:
return False
method = getattr(history.payment_method, "value", history.payment_method)
if method != PaymentMethod.EPAY.value or history.status == 1:
return False
config = self.payment_channel_service.get_config("epay")
if config is None or not config.get("enable"):
return False
client = EpayClient(
gateway=str(config.get("gateway") or "").strip(),
pid=str(config.get("pid") or "").strip(),
key=str(config.get("key") or "").strip(),
)
data = client.query_order(payment_id)
if not data:
return False
if int(data.get("status", 0) or 0) != 1:
return False
try:
notified_money = round(float(data.get("money")), 2)
except (TypeError, ValueError):
notified_money = None
if notified_money is not None:
try:
meta = json.loads(history.callback_data) if history.callback_data else {}
charged_money = round(float(meta.get("money_cny")), 2)
except (TypeError, ValueError, json.JSONDecodeError):
charged_money = None
if charged_money is not None and abs(charged_money - notified_money) > 0.01:
logger.error(
f"Epay query amount mismatch for order {payment_id}: "
f"queried={notified_money} charged={charged_money}"
)
return False
channel_tx = str(data.get("trade_no") or "") or f"epay-{payment_id}"
logger.info(f"Settling epay order {payment_id} via gateway query (trade_no={channel_tx})")
return self._add_wallet_balance(payment_id=payment_id, payment_channel_id=channel_tx)
def payment_callback(self, payment_id: str, payment_channel_id: str, status: int = 1) -> bool:
"""
Handle Alipay payment webhook callback.
+135
View File
@@ -0,0 +1,135 @@
"""
Epay (易支付) V1 protocol client.
Protocol reference: https://pay.88531.cn gateway (classic epay / V1):
* Submit page : GET {gateway}/submit.php (browser side, no IP whitelist)
* API order : POST {gateway}/mapi.php (server side, IP whitelist)
* Query order : GET {gateway}/api.php?act=order (uses plaintext `key`, no sign)
* Async notify: GET {notify_url} -> reply exactly `success`
Signature (MD5):
1. drop `sign`, `sign_type` and params whose value is empty
2. sort params by ASCII ascending
3. join as `k1=v1&k2=v2` -- values are NOT url-encoded
4. append the merchant key directly (no separator), md5 -> 32 char LOWERCASE hex
"""
import hashlib
import json
import logging
import urllib.error
import urllib.request
from typing import Optional
from urllib.parse import quote, urlencode
logger = logging.getLogger(__name__)
SIGN_TYPE = "MD5"
def build_sign(params: dict, key: str) -> str:
"""Build the epay V1 MD5 signature for `params`."""
parts = []
for field in sorted(params.keys()):
if field in ("sign", "sign_type"):
continue
value = params[field]
if value is None or value == "":
continue
if isinstance(value, (list, dict, tuple, set)):
continue
parts.append(f"{field}={value}")
raw = "&".join(parts) + str(key)
return hashlib.md5(raw.encode("utf-8")).hexdigest().lower()
def verify_sign(params: dict, key: str) -> bool:
"""Verify an inbound epay notify. Comparison is case insensitive."""
given = str(params.get("sign", "") or "").strip()
if not given:
return False
return build_sign(params, key).lower() == given.lower()
class EpayClient:
"""Builds epay payment requests."""
def __init__(self, gateway: str, pid: str, key: str):
self.gateway = (gateway or "").rstrip("/")
self.pid = str(pid or "")
self.key = str(key or "")
@property
def configured(self) -> bool:
return bool(self.gateway and self.pid and self.key)
def build_submit_url(
self,
out_trade_no: str,
name: str,
money: float,
notify_url: str,
return_url: str = "",
pay_type: str = "",
param: str = "",
) -> Optional[str]:
"""Build the browser payment URL ({gateway}/submit.php).
Returns None when the client is not configured or the URL cannot be built.
"""
if not self.configured:
logger.error("Epay client is not configured (gateway/pid/key missing)")
return None
if not out_trade_no or not notify_url:
logger.error("Epay payment requires out_trade_no and notify_url")
return None
params = {
"pid": self.pid,
"out_trade_no": out_trade_no,
"notify_url": notify_url,
"name": str(name or "Deposit")[:64],
"money": f"{float(money):.2f}",
"sign_type": SIGN_TYPE,
}
if return_url:
params["return_url"] = return_url
if pay_type:
params["type"] = str(pay_type)
if param:
params["param"] = str(param)
params["sign"] = build_sign(params, self.key)
# urlencode for the actual HTTP request only -- the signature above was
# computed on the raw (decoded) values, exactly as the gateway expects.
query = urlencode(list(params.items()), quote_via=quote, safe="")
return f"{self.gateway}/submit.php?{query}"
def query_order(self, out_trade_no: str, timeout: float = 8.0) -> Optional[dict]:
"""Query one order through {gateway}/api.php?act=order.
Returns the parsed JSON body, or None when the gateway is unreachable
or rejects the call (e.g. our IP is not whitelisted).
"""
if not self.configured or not out_trade_no:
return None
params = {
"act": "order",
"pid": self.pid,
"key": self.key,
"out_trade_no": out_trade_no,
}
url = f"{self.gateway}/api.php?{urlencode(list(params.items()), quote_via=quote, safe='')}"
try:
with urllib.request.urlopen(url, timeout=timeout) as resp:
payload = resp.read().decode("utf-8", "replace")
except (urllib.error.URLError, OSError, ValueError) as exc:
logger.error(f"Epay order query failed for {out_trade_no}: {exc}")
return None
try:
data = json.loads(payload)
except json.JSONDecodeError:
logger.error(f"Epay order query returned non-JSON for {out_trade_no}: {payload[:200]}")
return None
if not isinstance(data, dict):
return None
return data
@@ -25,3 +25,9 @@ class UserUtils:
"""Check if current user is admin"""
user = UserUtils.get_request_user(request)
return user and user.role_id == 1 if user else False
@staticmethod
def is_normal_user(request) -> bool:
"""Check if current user is normal user"""
user = UserUtils.get_request_user(request)
return user and user.role_id == 2 if user else False
+170 -35
View File
@@ -5,6 +5,7 @@ Supports both service_id (UUID) and slug_name service identifiers
from typing import Optional
from datetime import datetime, timezone
import json
from sqlalchemy import false
from starlette.requests import Request
from starlette.responses import Response
@@ -165,6 +166,10 @@ class McpController:
logger.info(f"Received SSE connection request - Service ID: {service_id}, Client: {client_ip}, UA: {user_agent[:50]}...")
# Validate Accept & Content-Type headers according to MCP Streamable HTTP rules
accept = (req.headers.get('accept') or '').lower()
content_type = (req.headers.get('content-type') or '').lower()
# Extract user ID (for billing) - must provide valid apikey
user_info = self._extract_user_info(req)
if not user_info:
@@ -288,6 +293,9 @@ class McpController:
logger.info(f"Received StreamableHTTP request - Service ID: {service_id}, Client: {client_ip}, UA: {user_agent[:50]}...")
accept = (req.headers.get('accept') or '').lower()
content_type = (req.headers.get('content-type') or '').lower()
# Security: validate Origin to prevent DNS rebinding (MCP spec requirement)
origin = req.headers.get('origin')
if origin and not self._is_origin_allowed(origin, req):
@@ -304,9 +312,42 @@ class McpController:
await send({'type': 'http.response.body', 'body': response_body})
return
# Validate Accept & Content-Type headers according to MCP Streamable HTTP rules
# Extract user ID (for billing) - must provide valid apikey
user_info = self._extract_user_info(req)
if not user_info:
response_body = b"Missing or invalid apikey parameter"
await send({
'type': 'http.response.start',
'status': 401,
'headers': [
[b'content-type', b'text/plain'],
[b'content-length', str(len(response_body)).encode()],
],
})
await send({'type': 'http.response.body', 'body': response_body})
return
user_id, apikey_id = user_info
if not self._check_invoke_permission(user_id, service_id):
response_body = b"Invoke permission denied"
await send({
'type': 'http.response.start',
'status': 403,
'headers': [
[b'content-type', b'text/plain'],
[b'content-length', str(len(response_body)).encode()],
],
})
await send({'type': 'http.response.body', 'body': response_body})
return
# MCP 0728 (2026-07-28) Stateless Handler for POST JSON-RPC requests
if req.method == 'POST' and ('application/json' in content_type or req.headers.get("mcp-method") or req.headers.get("mcp-protocol-version")):
stateless_handled = await self._handle_stateless_mcp_request(req, send, service_id, user_id, apikey_id)
if stateless_handled:
return
# Validate Accept & Content-Type headers according to legacy MCP Streamable HTTP rules
accept = (req.headers.get('accept') or '').lower()
content_type = (req.headers.get('content-type') or '').lower()
if req.method == 'GET':
if 'text/event-stream' not in accept:
logger.warning("Missing Accept: text/event-stream for GET SSE")
@@ -350,34 +391,6 @@ class McpController:
})
await send({'type': 'http.response.body', 'body': response_body})
return
# Extract user ID (for billing) - must provide valid apikey
user_info = self._extract_user_info(req)
if not user_info:
response_body = b"Missing or invalid apikey parameter"
await send({
'type': 'http.response.start',
'status': 401,
'headers': [
[b'content-type', b'text/plain'],
[b'content-length', str(len(response_body)).encode()],
],
})
await send({'type': 'http.response.body', 'body': response_body})
return
user_id, apikey_id = user_info
if not self._check_invoke_permission(user_id, service_id):
response_body = b"Invoke permission denied"
await send({
'type': 'http.response.start',
'status': 403,
'headers': [
[b'content-type', b'text/plain'],
[b'content-length', str(len(response_body)).encode()],
],
})
await send({'type': 'http.response.body', 'body': response_body})
return
raw_client_id = req.headers.get('x-client-instance-id') or req.headers.get('x-client-id')
client_instance_id = (re.sub(r'[^a-zA-Z0-9._-]', '', raw_client_id)[:32]) if raw_client_id else None
ua = user_agent or ""
@@ -679,14 +692,22 @@ class McpController:
def _extract_user_info(self, request: Request) -> Optional[tuple[str, str]]:
"""Extract user ID and apikey ID from request by validating apikey parameter."""
# Get apikey from URL query parameters
"""Extract user ID and apikey ID from request by validating apikey parameter or headers."""
# Get apikey from URL query parameters or Headers
apikey = request.query_params.get("apikey")
if not apikey:
apikey = request.query_params.get("authkey")
if not apikey:
logger.warning("Apikey not found in URL parameters")
return None
if not apikey:
auth_header = request.headers.get("authorization", "")
if auth_header.startswith("Bearer "):
apikey = auth_header[7:].strip()
elif auth_header:
apikey = auth_header.strip()
if not apikey:
apikey = request.headers.get("x-api-key")
if not apikey:
logger.warning("Apikey not found in URL parameters or Headers")
return None
logger.debug(f"Validating apikey: {apikey[:10]}...") # Only log first 10 characters for debugging
@@ -775,3 +796,117 @@ class McpController:
pass
finally:
logger.info("Session GC task stopped")
async def _handle_stateless_mcp_request(
self,
req: Request,
send,
service_id: str,
user_id: str,
apikey_id: str
) -> bool:
"""
Handle MCP 0728 (2026-07-28) stateless HTTP JSON-RPC request.
Returns True if request was handled directly, False if it should fallback to legacy session transport.
"""
mcp_method_hdr = req.headers.get("mcp-method")
protocol_hdr = req.headers.get("mcp-protocol-version")
try:
body_bytes = await req.body()
if not body_bytes:
return False
payload = json.loads(body_bytes.decode("utf-8"))
except Exception:
return False
if not isinstance(payload, dict):
return False
method = mcp_method_hdr or payload.get("method")
req_id = payload.get("id")
if not method:
return False
logger.info(f"Handling MCP 0728 stateless request - Service ID: {service_id}, Method: {method}, ReqID: {req_id}")
result = None
error = None
try:
if method in ("server/discover", "discover"):
result = await self.server_factory.get_server_discover(service_id)
elif method in ("tools/list", "list_tools"):
tools = await self.server_factory._handle_list_tools(service_id)
result = {
"tools": [t.model_dump(by_alias=True, exclude_none=True) for t in tools]
}
elif method in ("tools/call", "call_tool"):
params = payload.get("params") or {}
tool_name = req.headers.get("mcp-name") or params.get("name")
arguments = params.get("arguments") or {}
if not tool_name:
error = {"code": -32602, "message": "Missing tool name in params or header"}
else:
contents, extra = await self.server_factory.execute_tool(
service_id, tool_name, arguments, user_id, apikey_id
)
content_list = []
for c in contents:
if hasattr(c, "model_dump"):
content_list.append(c.model_dump(by_alias=True, exclude_none=True))
elif isinstance(c, dict):
content_list.append(c)
else:
content_list.append({"type": "text", "text": str(c)})
result = {
"content": content_list,
"isError": False
}
elif method == "initialize":
result = {
"protocolVersion": "2026-07-28",
"capabilities": {
"tools": {
"listChanged": False,
"ttlMs": 3600000,
"cacheScope": "service"
}
},
"serverInfo": {
"name": f"mcp-service-{service_id}",
"version": "1.0.0"
}
}
elif method in ("notifications/initialized", "ping"):
result = {}
elif method in ("resources/list", "prompts/list"):
result = {method.split("/")[0]: []}
else:
error = {"code": -32601, "message": f"Method not found: {method}"}
except Exception as e:
logger.error(f"Error handling stateless MCP method {method}: {str(e)}", exc_info=True)
error = {"code": -32603, "message": f"Internal error: {str(e)}"}
response_payload = {
"jsonrpc": "2.0",
"id": req_id
}
if error is not None:
response_payload["error"] = error
else:
response_payload["result"] = result
resp_bytes = json.dumps(response_payload).encode("utf-8")
await send({
'type': 'http.response.start',
'status': 200,
'headers': [
[b'content-type', b'application/json; charset=utf-8'],
[b'content-length', str(len(resp_bytes)).encode()],
[b'mcp-protocol-version', b'2026-07-28'],
],
})
await send({'type': 'http.response.body', 'body': resp_bytes})
return True
+1 -1
View File
@@ -199,7 +199,7 @@ def mcp_connections_stats():
mcp_routes = [
Route("/{service_id}", endpoint=mcp.handle_sse_connection_asgi, methods=["GET"]), # 保留原有 SSE GET
Mount("/messages/", app=mcp.get_sse_mount_handler()), # 保留原有 SSE POST 挂载
Route("/{service_id}/streamable-http", endpoint=mcp.handle_streamable_http_asgi, methods=["GET", "POST", "DELETE"]), # 新增 StreamableHTTP 路由
Route("/{service_id}/streamable-http", endpoint=mcp.handle_streamable_http_asgi, methods=["GET", "POST", "DELETE"]), # StreamableHTTP 路由
]
mcp_app = Starlette(routes=mcp_routes)
@@ -37,7 +37,7 @@ class UserApiKeyRepository:
return cached_model
# Query from database if not in cache
user_apikey = self.db.query(UserApiKey).filter(UserApiKey.apikey == apikey).first()
user_apikey = self.db.query(UserApiKey).filter(UserApiKey.apikey == apikey, UserApiKey.is_deleted == 0).first()
if user_apikey:
# Cache the result using new SQLAlchemy-specific method
CacheUtils.set_sqlalchemy_cache(cache_key, user_apikey, 300)
@@ -12,13 +12,16 @@ from typing import Tuple, Optional
from contextlib import asynccontextmanager
from services.common.redis import redis_client
from services.common.redis_keys import RedisKeys
from services.common.rabbitmq import rabbitmq_client
from services.common.models.billing import BillingMessage, PreDeductResult, ApiCallLogInfo
from services.common.models.mcp_service import ChargeType
from services.common.models.sys_config import SysConfig
from services.common.models.user_wallet import UserWallet
from services.common.database import get_db
from services.api_service.repositories.mcp_service_repository import McpServiceRepository
from services.api_service.repositories.user_wallet_repository import UserWalletRepository
from services.admin_service.constants import sys_config_key
from services.common.logging_config import get_logger
logger = get_logger(__name__)
@@ -51,10 +54,45 @@ class BillingService:
"""
try:
# Get service price information
price, input_token_price, output_token_price, charge_type = await self._get_service_price(service_id)
price, input_token_price, output_token_price, charge_type, free_daily_limit = await self._get_service_price(service_id)
service_price = price
match charge_type:
case ChargeType.FREE:
# Free services are not billed, but may be limited to N calls
# per user per day (Beijing time) to stop one user draining them.
# The platform-wide cap (sys_config) applies to every free service
# and wins whenever it is the smaller of the two, so a service
# with no per-service value still inherits the global one.
limits = [
(value, scope)
for value, scope in (
(free_daily_limit, "service"),
(self._get_global_free_limit(), "platform"),
)
if value and value > 0
]
if limits:
limit, scope = min(limits, key=lambda item: item[0])
allowed, used = self._check_free_quota(user_id, service_id, limit)
if not allowed:
scope_note = " (platform-wide limit)" if scope == "platform" else ""
logger.warning(
f"Free daily limit reached - User ID: {user_id}, Service ID: {service_id}, "
f"Limit: {limit}{scope_note}, Used: {used}"
)
return PreDeductResult(
success=False,
message=(
f"Free daily limit reached: {limit} calls per day{scope_note}. "
f"Used {used} today, please try again after midnight (Beijing time) "
f"or upgrade to a paid plan"
),
service_price=Decimal("0"),
user_balance=Decimal("0"),
input_token_price=input_token_price,
output_token_price=output_token_price,
charge_type=charge_type.value
)
logger.info(f"Free service, skip billing check - User ID: {user_id}, Service ID: {service_id}")
return PreDeductResult(
success=True,
@@ -168,7 +206,76 @@ class BillingService:
logger.error(f"Failed to send billing message: {str(e)}", exc_info=True)
# Consider saving failed messages to local queue for retry
async def _get_service_price(self, service_id: str) -> Tuple[Decimal, Decimal, Decimal, ChargeType]:
def _check_free_quota(self, user_id: str, service_id: str, limit: int) -> tuple[bool, int]:
"""Count today's calls for a free service and say whether this one is still allowed.
One Redis key per service/user/Beijing-day, so the counter costs a single
INCR and expires by itself after midnight (no cleanup job).
Returns (allowed, used_today). A rejected call is counted too, otherwise a
client could loop past the limit by simply retrying.
"""
from datetime import timedelta
tz_cst = timezone(timedelta(hours=8))
now_cst = datetime.now(tz_cst)
day = now_cst.strftime("%Y%m%d")
key = f"xpack:free:calls:{service_id}:{user_id}:{day}"
try:
used = int(self.redis.incr(key))
if used == 1:
# seconds until the next Beijing midnight, plus a day of slack
next_midnight = (now_cst + timedelta(days=1)).replace(
hour=0, minute=0, second=0, microsecond=0
)
ttl = int((next_midnight - now_cst).total_seconds()) + 86400
self.redis.expire(key, ttl)
except Exception as e:
# Redis being down must not lock paying/free users out of their tools
logger.warning(f"Free quota counter unavailable, allowing call - User ID: {user_id}: {e}")
return True, 0
return used <= limit, used
def _get_global_free_limit(self) -> Optional[int]:
"""Platform-wide daily cap on free services, stored in sys_config.
Reads the same cache key the admin service invalidates on save, so a new
value takes effect on the very next call. Unset / empty / unparsable all
mean "no platform cap" and never fail the request.
"""
cache_key = RedisKeys.sys_config_key(sys_config_key.KEY_FREE_GLOBAL_DAILY_LIMIT)
raw = None
try:
cached = self.redis.get(cache_key)
if cached is not None:
raw = None if cached == "__NULL__" else str(cached)
else:
db = next(get_db())
try:
row = (
db.query(SysConfig)
.filter(SysConfig.key == sys_config_key.KEY_FREE_GLOBAL_DAILY_LIMIT)
.first()
)
raw = row.value if row is not None else None
finally:
db.close()
# Cache the miss too, so an unset key costs no query per call
self.redis.set(cache_key, "__NULL__" if raw in (None, "") else raw, ex=3600)
except Exception as e:
logger.warning(f"Global free limit unavailable, treating as unset: {e}")
return None
if raw in (None, "", "__NULL__"):
return None
try:
value = int(raw)
except (TypeError, ValueError):
return None
return value if value > 0 else None
async def _get_service_price(self, service_id: str) -> Tuple[Decimal, Decimal, Decimal, ChargeType, Optional[int]]:
"""
Get service price and charge type
@@ -184,7 +291,15 @@ class BillingService:
if cached_data:
try:
data = json.loads(cached_data)
return Decimal(data["price"]), Decimal(data["input_token_price"]), Decimal(data["output_token_price"]), ChargeType(data["charge_type"])
# free_daily_limit was added later; older cache entries simply lack it
free_daily_limit = data.get("free_daily_limit")
return (
Decimal(data["price"]),
Decimal(data["input_token_price"]),
Decimal(data["output_token_price"]),
ChargeType(data["charge_type"]),
int(free_daily_limit) if free_daily_limit not in (None, "") else None,
)
except (json.JSONDecodeError, KeyError, ValueError):
logger.warning(f"Service price cache data anomaly, re-fetch from database - Service ID: {service_id}")
@@ -209,17 +324,20 @@ class BillingService:
input_token_price = Decimal(str(service.input_token_price))
output_token_price = Decimal(str(service.output_token_price))
charge_type = service.charge_type
free_daily_limit = getattr(service, "free_daily_limit", None)
free_daily_limit = int(free_daily_limit) if free_daily_limit else None
# Cache to Redis
cache_data = {
"price": str(price),
"input_token_price": str(input_token_price),
"output_token_price":str(output_token_price),
"charge_type": charge_type.value
"charge_type": charge_type.value,
"free_daily_limit": free_daily_limit
}
self.redis.set(cache_key, json.dumps(cache_data), ex=self.SERVICE_CACHE_EXPIRE)
return price,input_token_price,output_token_price, charge_type
return price,input_token_price,output_token_price, charge_type, free_daily_limit
finally:
db.close()
@@ -70,6 +70,43 @@ class McpServerFactory:
logger.info("MCP server instance created successfully")
return app
async def execute_tool(
self,
service_id: str,
name: str,
arguments: dict,
user_id: str,
apikey_id: Optional[str] = None
) -> tuple[List[types.Content], dict]:
"""
Execute tool directly with billing for stateless 0728 protocol calls.
"""
return await self._handle_call_tool_with_billing(service_id, name, arguments, user_id, apikey_id)
async def get_server_discover(self, service_id: str) -> dict:
"""
Return server discovery metadata complying with MCP 0728 (2026-07-28) spec.
Includes ttlMs and cacheScope for tools list caching.
"""
tools = await self._handle_list_tools(service_id)
return {
"protocolVersion": "2026-07-28",
"capabilities": {
"tools": {
"listChanged": False,
"ttlMs": 3600000, # Cache 1 hour
"cacheScope": "service"
},
"resources": {},
"prompts": {}
},
"serverInfo": {
"name": f"mcp-service-{service_id}",
"version": "1.0.0"
},
"tools": [tool.model_dump(by_alias=True, exclude_none=True) for tool in tools]
}
async def _handle_list_tools(self, service_id: str) -> List[types.Tool]:
"""
Handle tools list query
@@ -45,7 +45,9 @@ class RedisEventStore(EventStore):
async def store_event(self, stream_id: StreamId, message: JSONRPCMessage) -> EventId:
key = self._key(stream_id)
payload = json.dumps(message.model_dump(by_alias=True, exclude_none=True))
payload = json.dumps(
message.model_dump(by_alias=True, exclude_none=True) if message is not None else {}
)
# Append to Redis list via thread wrapper to avoid blocking
seq: int = await asyncio.to_thread(self._rpush, key, payload)
# rpush returns new length; sequence index is length-1
+5
View File
@@ -33,6 +33,11 @@ class McpService(Base):
Numeric(10, 2), nullable=True, comment="Output token price (2 decimal places, for per_token charge type)"
)
enabled: Mapped[int] = mapped_column(Integer, nullable=True, comment="Service status: 0=disabled, 1=enabled")
free_daily_limit: Mapped[int] = mapped_column(
Integer,
nullable=True,
comment="Free service: max calls per user per day (Beijing time). NULL or 0 = unlimited",
)
tags: Mapped[str] = mapped_column(String, nullable=True, comment="Tags")
service_type: Mapped[str] = mapped_column(String(255), nullable=True, comment="Service type",default="openapi")
created_at: Mapped[datetime] = mapped_column(
+22
View File
@@ -0,0 +1,22 @@
"""User submission model for MCP/Skill/Plugin marketplace."""
from sqlalchemy import Column, String, Text, DateTime, JSON, Enum
from sqlalchemy.sql import func
from services.common.models.base import Base
import uuid
class Submission(Base):
__tablename__ = "submission"
id = Column(String(36), primary_key=True, default=lambda: str(uuid.uuid4()))
user_id = Column(String(36), nullable=False, index=True)
type = Column(Enum("mcp", "skill", "plugin", name="submission_type"), nullable=False)
name = Column(String(255), nullable=False)
description = Column(Text, nullable=True)
file_path = Column(String(512), nullable=True)
metadata_json = Column(JSON, nullable=True)
status = Column(Enum("pending", "approved", "rejected", name="submission_status"), default="pending", nullable=False)
reviewer_id = Column(String(36), nullable=True)
review_note = Column(Text, nullable=True)
created_at = Column(DateTime, server_default=func.now())
reviewed_at = Column(DateTime, nullable=True)
@@ -10,6 +10,7 @@ class PaymentMethod(PyEnum):
STRIPE = "stripe"
ALIPAY = "alipay"
WECHAT = "wechat"
EPAY = "epay"
class TransactionType(PyEnum):
+8 -3
View File
@@ -30,7 +30,7 @@ class RabbitMQClient:
logger.error(f"Failed to establish RabbitMQ connection: {str(e)}")
raise
def publish(self, queue: str, message: str, persistent: bool = True):
def publish(self, queue: str, message: str, persistent: bool = True, headers: dict | None = None):
"""Publish message to queue"""
try:
# Ensure connection is available
@@ -46,7 +46,9 @@ class RabbitMQClient:
# Publish message
properties = None
if persistent:
properties = pika.BasicProperties(delivery_mode=2)
properties = pika.BasicProperties(delivery_mode=2, headers=headers or None)
elif headers:
properties = pika.BasicProperties(headers=headers)
self.channel.basic_publish(exchange="", routing_key=queue, body=message, properties=properties)
logger.info(f"Message successfully published to queue: {queue}, message length: {len(message)} bytes")
@@ -61,7 +63,10 @@ class RabbitMQClient:
# Retry sending
self.channel.queue_declare(queue=queue, durable=True)
properties = pika.BasicProperties(delivery_mode=2) if persistent else None
if persistent:
properties = pika.BasicProperties(delivery_mode=2, headers=headers or None)
else:
properties = pika.BasicProperties(headers=headers or None)
self.channel.basic_publish(exchange="", routing_key=queue, body=message, properties=properties)
logger.info(f"Message retry successful to queue {queue}")
except Exception as retry_error:
-1
View File
@@ -33,7 +33,6 @@ def verify_token(token: str, db: Session) -> Optional[User]:
set_model_cache(token_cache_key, user_access_token, TOKEN_CACHE_EXPIRE_TIME)
logging.info(f"query token info")
# Check if token is expired
expire_at = user_access_token.expire_at
try:
+6 -6
View File
@@ -66,9 +66,9 @@ class HttpUtils:
if parsed.scheme not in ("http", "https"):
raise ValidationException("URL scheme must be http or https")
host = (parsed.hostname or "").lower()
private_prefixes = ("127.", "10.", "192.168.")
if host in ("localhost", "127.0.0.1") or host.startswith(private_prefixes) or host.startswith("172."):
raise ValidationException("Access to local/private addresses is not allowed")
# Allow LAN/private IPs (user request), only block loopback
if host in ("localhost", "127.0.0.1", "::1") or host.startswith("127."):
raise ValidationException("Access to loopback addresses is not allowed")
async with aiohttp.ClientSession(
timeout=aiohttp.ClientTimeout(total=timeout),
@@ -193,9 +193,9 @@ class HttpUtils:
if parsed.scheme not in ("http", "https"):
raise ValidationException("URL scheme must be http or https")
host = (parsed.hostname or "").lower()
private_prefixes = ("127.", "10.", "192.168.")
if host in ("localhost", "127.0.0.1") or host.startswith(private_prefixes) or host.startswith("172."):
raise ValidationException("Access to local/private addresses is not allowed")
# Allow LAN/private IPs (user request), only block loopback
if host in ("localhost", "127.0.0.1", "::1") or host.startswith("127."):
raise ValidationException("Access to loopback addresses is not allowed")
async with aiohttp.ClientSession(
timeout=aiohttp.ClientTimeout(total=timeout),