Merge pull request #48 from xpack-ai/feature/synchronize_saas

Feature/synchronize saas
This commit is contained in:
Dot.L authored and GitHub committed 2025-10-17 18:38:51 +08:00
commit 186ba8ca81
36 files changed
+628 -394

No files matched your search

+44 -7
View File
@@ -26,12 +26,18 @@ importers:
'@types/crypto-js':
specifier: ^4.2.2
version: 4.2.2
'@uiw/react-markdown-preview':
specifier: ^5.1.5
version: 5.1.5(@types/react@18.3.23)(react-dom@18.3.1(react@18.3.1))(react@18.3.1)
'@uiw/react-md-editor':
specifier: ^4.0.7
version: 4.0.7(@types/react@18.3.23)(react-dom@18.3.1(react@18.3.1))(react@18.3.1)
animejs:
specifier: ^4.0.2
version: 4.0.2
async_hooks:
specifier: ^1.0.0
version: 1.0.0
crypto-js:
specifier: ^4.2.0
version: 4.2.0
@@ -80,9 +86,6 @@ importers:
react-icons:
specifier: ^5.3.0
version: 5.5.0(react@18.3.1)
react-markdown:
specifier: ^10.1.0
version: 10.1.0(@types/react@18.3.23)(react@18.3.1)
recharts:
specifier: ^2.15.1
version: 2.15.1(react-dom@18.3.1(react@18.3.1))(react@18.3.1)
@@ -1219,72 +1222,85 @@ packages:
resolution: {integrity: sha512-IVfGJa7gjChDET1dK9SekxFFdflarnUB8PwW8aGwEoF3oAsSDuNUTYS+SKDOyOJxQyDC1aPFMuRYLoDInyV9Ew==}
cpu: [arm64]
os: [linux]
libc: [glibc]
'@img/sharp-libvips-linux-arm@1.1.0':
resolution: {integrity: sha512-s8BAd0lwUIvYCJyRdFqvsj+BJIpDBSxs6ivrOPm/R7piTs5UIwY5OjXrP2bqXC9/moGsyRa37eYWYCOGVXxVrA==}
cpu: [arm]
os: [linux]
libc: [glibc]
'@img/sharp-libvips-linux-ppc64@1.1.0':
resolution: {integrity: sha512-tiXxFZFbhnkWE2LA8oQj7KYR+bWBkiV2nilRldT7bqoEZ4HiDOcePr9wVDAZPi/Id5fT1oY9iGnDq20cwUz8lQ==}
cpu: [ppc64]
os: [linux]
libc: [glibc]
'@img/sharp-libvips-linux-s390x@1.1.0':
resolution: {integrity: sha512-xukSwvhguw7COyzvmjydRb3x/09+21HykyapcZchiCUkTThEQEOMtBj9UhkaBRLuBrgLFzQ2wbxdeCCJW/jgJA==}
cpu: [s390x]
os: [linux]
libc: [glibc]
'@img/sharp-libvips-linux-x64@1.1.0':
resolution: {integrity: sha512-yRj2+reB8iMg9W5sULM3S74jVS7zqSzHG3Ol/twnAAkAhnGQnpjj6e4ayUz7V+FpKypwgs82xbRdYtchTTUB+Q==}
cpu: [x64]
os: [linux]
libc: [glibc]
'@img/sharp-libvips-linuxmusl-arm64@1.1.0':
resolution: {integrity: sha512-jYZdG+whg0MDK+q2COKbYidaqW/WTz0cc1E+tMAusiDygrM4ypmSCjOJPmFTvHHJ8j/6cAGyeDWZOsK06tP33w==}
cpu: [arm64]
os: [linux]
libc: [musl]
'@img/sharp-libvips-linuxmusl-x64@1.1.0':
resolution: {integrity: sha512-wK7SBdwrAiycjXdkPnGCPLjYb9lD4l6Ze2gSdAGVZrEL05AOUJESWU2lhlC+Ffn5/G+VKuSm6zzbQSzFX/P65A==}
cpu: [x64]
os: [linux]
libc: [musl]
'@img/sharp-linux-arm64@0.34.2':
resolution: {integrity: sha512-D8n8wgWmPDakc83LORcfJepdOSN6MvWNzzz2ux0MnIbOqdieRZwVYY32zxVx+IFUT8er5KPcyU3XXsn+GzG/0Q==}
engines: {node: ^18.17.0 || ^20.3.0 || >=21.0.0}
cpu: [arm64]
os: [linux]
libc: [glibc]
'@img/sharp-linux-arm@0.34.2':
resolution: {integrity: sha512-0DZzkvuEOqQUP9mo2kjjKNok5AmnOr1jB2XYjkaoNRwpAYMDzRmAqUIa1nRi58S2WswqSfPOWLNOr0FDT3H5RQ==}
engines: {node: ^18.17.0 || ^20.3.0 || >=21.0.0}
cpu: [arm]
os: [linux]
libc: [glibc]
'@img/sharp-linux-s390x@0.34.2':
resolution: {integrity: sha512-EGZ1xwhBI7dNISwxjChqBGELCWMGDvmxZXKjQRuqMrakhO8QoMgqCrdjnAqJq/CScxfRn+Bb7suXBElKQpPDiw==}
engines: {node: ^18.17.0 || ^20.3.0 || >=21.0.0}
cpu: [s390x]
os: [linux]
libc: [glibc]
'@img/sharp-linux-x64@0.34.2':
resolution: {integrity: sha512-sD7J+h5nFLMMmOXYH4DD9UtSNBD05tWSSdWAcEyzqW8Cn5UxXvsHAxmxSesYUsTOBmUnjtxghKDl15EvfqLFbQ==}
engines: {node: ^18.17.0 || ^20.3.0 || >=21.0.0}
cpu: [x64]
os: [linux]
libc: [glibc]
'@img/sharp-linuxmusl-arm64@0.34.2':
resolution: {integrity: sha512-NEE2vQ6wcxYav1/A22OOxoSOGiKnNmDzCYFOZ949xFmrWZOVII1Bp3NqVVpvj+3UeHMFyN5eP/V5hzViQ5CZNA==}
engines: {node: ^18.17.0 || ^20.3.0 || >=21.0.0}
cpu: [arm64]
os: [linux]
libc: [musl]
'@img/sharp-linuxmusl-x64@0.34.2':
resolution: {integrity: sha512-DOYMrDm5E6/8bm/yQLCWyuDJwUnlevR8xtF8bs+gjZ7cyUNYXiSf/E8Kp0Ss5xasIaXSHzb888V1BE4i1hFhAA==}
engines: {node: ^18.17.0 || ^20.3.0 || >=21.0.0}
cpu: [x64]
os: [linux]
libc: [musl]
'@img/sharp-wasm32@0.34.2':
resolution: {integrity: sha512-/VI4mdlJ9zkaq53MbIG6rZY+QRN3MLbR6usYlgITEzi4Rpx5S6LFKsycOQjkOGmqTNmkIdLjEvooFKwww6OpdQ==}
@@ -1391,24 +1407,28 @@ packages:
engines: {node: '>= 10'}
cpu: [arm64]
os: [linux]
libc: [glibc]
'@next/swc-linux-arm64-musl@15.3.4':
resolution: {integrity: sha512-wFyZ7X470YJQtpKot4xCY3gpdn8lE9nTlldG07/kJYexCUpX1piX+MBfZdvulo+t1yADFVEuzFfVHfklfEx8kw==}
engines: {node: '>= 10'}
cpu: [arm64]
os: [linux]
libc: [musl]
'@next/swc-linux-x64-gnu@15.3.4':
resolution: {integrity: sha512-gEbH9rv9o7I12qPyvZNVTyP/PWKqOp8clvnoYZQiX800KkqsaJZuOXkWgMa7ANCCh/oEN2ZQheh3yH8/kWPSEg==}
engines: {node: '>= 10'}
cpu: [x64]
os: [linux]
libc: [glibc]
'@next/swc-linux-x64-musl@15.3.4':
resolution: {integrity: sha512-Cf8sr0ufuC/nu/yQ76AnarbSAXcwG/wj+1xFPNbyNo8ltA6kw5d5YqO8kQuwVIxk13SBdtgXrNyom3ZosHAy4A==}
engines: {node: '>= 10'}
cpu: [x64]
os: [linux]
libc: [musl]
'@next/swc-win32-arm64-msvc@15.3.4':
resolution: {integrity: sha512-ay5+qADDN3rwRbRpEhTOreOn1OyJIXS60tg9WMYTWCy3fB6rGoyjLVxc4dR9PYjEdR2iDYsaF5h03NA+XuYPQQ==}
@@ -2712,24 +2732,28 @@ packages:
engines: {node: '>=10'}
cpu: [arm64]
os: [linux]
libc: [glibc]
'@swc/core-linux-arm64-musl@1.10.18':
resolution: {integrity: sha512-4rv+E4VLdgQw6zjbTAauCAEExxChvxMpBUMCiZweTNPKbJJ2dY6BX2WGJ1ea8+RcgqR/Xysj3AFbOz1LBz6dGA==}
engines: {node: '>=10'}
cpu: [arm64]
os: [linux]
libc: [musl]
'@swc/core-linux-x64-gnu@1.10.18':
resolution: {integrity: sha512-vTNmyRBVP+sZca+vtwygYPGTNudTU6Gl6XhaZZ7cEUTBr8xvSTgEmYXoK/2uzyXpaTUI4Bmtp1x81cGN0mMoLQ==}
engines: {node: '>=10'}
cpu: [x64]
os: [linux]
libc: [glibc]
'@swc/core-linux-x64-musl@1.10.18':
resolution: {integrity: sha512-1TZPReKhFCeX776XaT6wegknfg+g3zODve+r4oslFHI+g7cInfWlxoGNDS3niPKyuafgCdOjme2g3OF+zzxfsQ==}
engines: {node: '>=10'}
cpu: [x64]
os: [linux]
libc: [musl]
'@swc/core-win32-arm64-msvc@1.10.18':
resolution: {integrity: sha512-o/2CsaWSN3bkzVQ6DA+BiFKSVEYvhWGA1h+wnL2zWmIDs2Knag54sOEXZkCaf8YQyZesGeXJtPEy9hh/vjJgkA==}
@@ -3083,8 +3107,8 @@ packages:
'@uiw/copy-to-clipboard@1.0.17':
resolution: {integrity: sha512-O2GUHV90Iw2VrSLVLK0OmNIMdZ5fgEg4NhvtwINsX+eZ/Wf6DWD0TdsK9xwV7dNRnK/UI2mQtl0a2/kRgm1m1A==}
'@uiw/react-markdown-preview@5.1.4':
resolution: {integrity: sha512-6k13WVNHCEaamz3vh54OQ1tseIXneKlir1+E/VFQBPq8PRod+gwLfYtiitDBWu+ZFttoiKPLZ7flgHrVM+JNOg==}
'@uiw/react-markdown-preview@5.1.5':
resolution: {integrity: sha512-DNOqx1a6gJR7Btt57zpGEKTfHRlb7rWbtctMRO2f82wWcuoJsxPBrM+JWebDdOD0LfD8oe2CQvW2ICQJKHQhZg==}
peerDependencies:
react: '>=16.8.0'
react-dom: '>=16.8.0'
@@ -3137,41 +3161,49 @@ packages:
resolution: {integrity: sha512-UYA0MA8ajkEDCFRQdng/FVx3F6szBvk3EPnkTTQuuO9lV1kPGuTB+V9TmbDxy5ikaEgyWKxa4CI3ySjklZ9lFA==}
cpu: [arm64]
os: [linux]
libc: [glibc]
'@unrs/resolver-binding-linux-arm64-musl@1.9.2':
resolution: {integrity: sha512-P/CO3ODU9YJIHFqAkHbquKtFst0COxdphc8TKGL5yCX75GOiVpGqd1d15ahpqu8xXVsqP4MGFP2C3LRZnnL5MA==}
cpu: [arm64]
os: [linux]
libc: [musl]
'@unrs/resolver-binding-linux-ppc64-gnu@1.9.2':
resolution: {integrity: sha512-uKStFlOELBxBum2s1hODPtgJhY4NxYJE9pAeyBgNEzHgTqTiVBPjfTlPFJkfxyTjQEuxZbbJlJnMCrRgD7ubzw==}
cpu: [ppc64]
os: [linux]
libc: [glibc]
'@unrs/resolver-binding-linux-riscv64-gnu@1.9.2':
resolution: {integrity: sha512-LkbNnZlhINfY9gK30AHs26IIVEZ9PEl9qOScYdmY2o81imJYI4IMnJiW0vJVtXaDHvBvxeAgEy5CflwJFIl3tQ==}
cpu: [riscv64]
os: [linux]
libc: [glibc]
'@unrs/resolver-binding-linux-riscv64-musl@1.9.2':
resolution: {integrity: sha512-vI+e6FzLyZHSLFNomPi+nT+qUWN4YSj8pFtQZSFTtmgFoxqB6NyjxSjAxEC1m93qn6hUXhIsh8WMp+fGgxCoRg==}
cpu: [riscv64]
os: [linux]
libc: [musl]
'@unrs/resolver-binding-linux-s390x-gnu@1.9.2':
resolution: {integrity: sha512-sSO4AlAYhSM2RAzBsRpahcJB1msc6uYLAtP6pesPbZtptF8OU/CbCPhSRW6cnYOGuVmEmWVW5xVboAqCnWTeHQ==}
cpu: [s390x]
os: [linux]
libc: [glibc]
'@unrs/resolver-binding-linux-x64-gnu@1.9.2':
resolution: {integrity: sha512-jkSkwch0uPFva20Mdu8orbQjv2A3G88NExTN2oPTI1AJ+7mZfYW3cDCTyoH6OnctBKbBVeJCEqh0U02lTkqD5w==}
cpu: [x64]
os: [linux]
libc: [glibc]
'@unrs/resolver-binding-linux-x64-musl@1.9.2':
resolution: {integrity: sha512-Uk64NoiTpQbkpl+bXsbeyOPRpUoMdcUqa+hDC1KhMW7aN1lfW8PBlBH4mJ3n3Y47dYE8qi0XTxy1mBACruYBaw==}
cpu: [x64]
os: [linux]
libc: [musl]
'@unrs/resolver-binding-wasm32-wasi@1.9.2':
resolution: {integrity: sha512-EpBGwkcjDicjR/ybC0g8wO5adPNdVuMrNalVgYcWi+gYtC1XYNuxe3rufcO7dA76OHGeVabcO6cSkPJKVcbCXQ==}
@@ -3342,6 +3374,9 @@ packages:
resolution: {integrity: sha512-hsU18Ae8CDTR6Kgu9DYf0EbCr/a5iGL0rytQDobUcdpYOKokk8LEjVphnXkDkgpi0wYVsqrXuP0bZxJaTqdgoA==}
engines: {node: '>= 0.4'}
async_hooks@1.0.0:
resolution: {integrity: sha512-t4BSJgx48V3e7U6Ll3/WOUNmxIRPzmPdxVfgbyzcnRItEnn4iKp4F//b0sV3L9hzbdr5qxWdNWzOF7t+rjYSfA==}
attr-accept@2.2.5:
resolution: {integrity: sha512-0bDNnY/u6pPwHDMoF0FieU354oBi0a8rD9FcsLwzcGWbc8KS8KPIi7y+s13OlVY+gMWc/9xEMUgNE6Qm8ZllYQ==}
engines: {node: '>=4'}
@@ -11173,7 +11208,7 @@ snapshots:
'@uiw/copy-to-clipboard@1.0.17': {}
'@uiw/react-markdown-preview@5.1.4(@types/react@18.3.23)(react-dom@18.3.1(react@18.3.1))(react@18.3.1)':
'@uiw/react-markdown-preview@5.1.5(@types/react@18.3.23)(react-dom@18.3.1(react@18.3.1))(react@18.3.1)':
dependencies:
'@babel/runtime': 7.26.9
'@uiw/copy-to-clipboard': 1.0.17
@@ -11197,7 +11232,7 @@ snapshots:
'@uiw/react-md-editor@4.0.7(@types/react@18.3.23)(react-dom@18.3.1(react@18.3.1))(react@18.3.1)':
dependencies:
'@babel/runtime': 7.26.9
'@uiw/react-markdown-preview': 5.1.4(@types/react@18.3.23)(react-dom@18.3.1(react@18.3.1))(react@18.3.1)
'@uiw/react-markdown-preview': 5.1.5(@types/react@18.3.23)(react-dom@18.3.1(react@18.3.1))(react@18.3.1)
react: 18.3.1
react-dom: 18.3.1(react@18.3.1)
rehype: 13.0.2
@@ -11445,6 +11480,8 @@ snapshots:
async-function@1.0.0: {}
async_hooks@1.0.0: {}
attr-accept@2.2.5: {}
autoprefixer@10.4.20(postcss@8.5.3):
+3 -3
View File
@@ -4,9 +4,9 @@
FROM python:3.12.11-alpine3.22
RUN apk add nodejs=~22 npm uv nginx
ENV TZ=Asia/Shanghai
RUN ln -sf /usr/share/zoneinfo/${TZ} /etc/localtime \
&& echo "Asia/Shanghai" > /etc/timezone
# ENV TZ=Asia/Shanghai
# RUN ln -sf /usr/share/zoneinfo/${TZ} /etc/localtime \
# && echo "Asia/Shanghai" > /etc/timezone
ARG APP XPack-MCP-Market
ARG VERSION latest
+2 -2
View File
@@ -72,8 +72,8 @@ clean_build_cache() {
frontend_build() {
echo_info "Begin frontend build..."
cd "${FRONTEND_DIR}" || exit 1
echo_debug "Running command: pnpm install --frozen-lockfile"
pnpm install --frozen-lockfile || exit 1
echo_debug "Running command: pnpm install"
pnpm install || exit 1
echo_debug "Running command: pnpm build"
pnpm build || exit 1
+4 -8
View File
@@ -39,8 +39,7 @@ MYSQL_DATA_MOUNT="/var/lib/xpack/mysql"
REDIS_PASSWORD="redis_6sJZDm"
REDIS_MAP_PORT=6379
XPACK_MCP_MARKET_WEB_MAP_PORT=3000
XPACK_MCP_MARKET_MCP_MAP_PORT=8002
XPACK_MCP_MARKET_MAP_PORT=3000
RABBITMQ_USER="rabbitmq"
RABBITMQ_PASSWORD="rabbitmq_Gs123dA"
@@ -881,14 +880,13 @@ upgrade_xpack_mcp_market() {
run_xpack_mcp_market() {
imageName=$(download_package_docker "program.xpack_mcp_market")
dockerCmd="docker run -dt --name ${XPACK_MCP_MARKET_CONTAINER_NAME} --restart=always --privileged=true \
--network=${NETWORK_NAME} -p ${XPACK_MCP_MARKET_WEB_MAP_PORT}:3000 -p ${XPACK_MCP_MARKET_MCP_MAP_PORT}:8002 \
--network=${NETWORK_NAME} -p ${XPACK_MCP_MARKET_MAP_PORT}:80 \
${imageName}"
echo -e `${dockerCmd}`
write_env_var ${ENV_XPACK_MCP_MARKET_WEB_PORT} ${XPACK_MCP_MARKET_WEB_MAP_PORT}
write_env_var ${ENV_XPACK_MCP_MARKET_MCP_PORT} ${XPACK_MCP_MARKET_MCP_MAP_PORT}
write_env_var ${ENV_XPACK_MCP_MARKET_WEB_PORT} ${XPACK_MCP_MARKET_MAP_PORT}
wait_for "XPACK MCP MARKET" "curl -s -o /dev/null http://127.0.0.1:${XPACK_MCP_MARKET_WEB_MAP_PORT}"
wait_for "XPACK MCP MARKET" "curl -s -o /dev/null http://127.0.0.1:${XPACK_MCP_MARKET_MAP_PORT}"
}
install_xpack_mcp_market() {
@@ -951,14 +949,12 @@ print_xpack_mcp_market_info() {
webPort=$(read_env_var ${ENV_XPACK_MCP_MARKET_WEB_PORT})
mcpPort=$(read_env_var ${ENV_XPACK_MCP_MARKET_MCP_PORT})
echo_pass "XPack MCP Market has run successfully. Version is ${XPACK_VERSION}."
echo_info "The Web UI information is as follows:"
echo_info "Homepage: http://$(read_env_var ${ENV_EXTERNAL_IP}):${webPort}"
echo_info "Admin dashboard: http://$(read_env_var ${ENV_EXTERNAL_IP}):${webPort}/admin"
echo_info "Admin Username: admin"
echo_info "Admin Password: 123456789"
echo_info "MCP endpoint: http://$(read_env_var ${ENV_EXTERNAL_IP}):${mcpPort}"
}
valid_port() {
+8 -8
View File
@@ -263,15 +263,15 @@ CREATE TABLE IF NOT EXISTS `mcp_call_log` (
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci COMMENT='Stores API call logs';
CREATE TABLE IF NOT EXISTS `sys_config_large` (
`id` char(36) NOT NULL COMMENT '主键,UUID',
`key` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NOT NULL COMMENT '配置键名',
`value` text CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NOT NULL COMMENT '配置值',
`description` text CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NOT NULL COMMENT '配置项描述',
`created_at` timestamp NULL DEFAULT NULL COMMENT '记录创建时间',
`updated_at` timestamp NULL DEFAULT NULL COMMENT '记录最后更新时间',
`id` char(36) NOT NULL COMMENT 'Primary key, UUID',
`key` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NOT NULL COMMENT 'Configuration key name',
`value` text CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NOT NULL COMMENT 'Configuration value',
`description` text CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NOT NULL COMMENT 'Configuration item description',
`created_at` timestamp NULL DEFAULT NULL COMMENT 'Record creation time',
`updated_at` timestamp NULL DEFAULT NULL COMMENT 'Record last update time',
PRIMARY KEY (`id`) USING BTREE,
UNIQUE KEY `uk_key` (`key`) USING BTREE COMMENT '配置键名唯一索引'
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci ROW_FORMAT=DYNAMIC COMMENT='系统参数配置表,用于存储系统级配置信息';
UNIQUE KEY `uk_key` (`key`) USING BTREE COMMENT 'Unique index for configuration key name'
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE utf8mb4_0900_ai_ci ROW_FORMAT=DYNAMIC COMMENT='System configuration table for storing system-level configuration information';
INSERT INTO `user` (`id`, `name`, `email`, `password`, `is_active`, `is_deleted`, `register_type`, `role_id`, `created_at`, `updated_at`)
VALUES ('admin', 'admin', 'admin@xpack.com', '25f9e794323b453885f5181f1b624d0b', 1, 0, 'inner', 1, CURRENT_TIMESTAMP, CURRENT_TIMESTAMP)
+24 -14
View File
@@ -1,6 +1,6 @@
SET FOREIGN_KEY_CHECKS=0;
ALTER TABLE `mcp_call_log` MODIFY COLUMN `process_status` enum('pending','processed','failed') CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT 'pending' COMMENT '处理状态,\'pending\',\'processed\',\'failed\'' AFTER `call_end_time`;
ALTER TABLE `mcp_call_log` MODIFY COLUMN `process_status` enum('pending','processed','failed') CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT 'pending' COMMENT 'Processing status: pending, processed, failed' AFTER `call_end_time`;
ALTER TABLE `mcp_service` ADD COLUMN `headers` text CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL COMMENT 'headers config,like: [{\"name\":\"name\",\"value\":\"value\",\"description\":\"description\"}]' AFTER `auth_token`;
@@ -39,42 +39,52 @@ END;
CREATE TABLE `onboarding_task` (
`id` bigint NOT NULL AUTO_INCREMENT,
`task_id` char(100) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NOT NULL COMMENT '任务ID',
`user_id` char(36) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NOT NULL COMMENT '用户ID',
`task_status` tinyint NOT NULL COMMENT '任务状态,0:未完成,1:已完成',
`task_id` char(100) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NOT NULL COMMENT 'Task ID',
`user_id` char(36) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NOT NULL COMMENT 'User ID',
`task_status` tinyint NOT NULL COMMENT 'Task status: 0 = incomplete, 1 = completed',
`created_at` timestamp NULL DEFAULT NULL,
`updated_at` timestamp NULL DEFAULT NULL,
PRIMARY KEY (`id`) USING BTREE,
UNIQUE INDEX `uk`(`task_id` ASC, `user_id` ASC) USING BTREE
) ENGINE = InnoDB CHARACTER SET = utf8mb4 COLLATE = utf8mb4_0900_ai_ci COMMENT = '新手引导任务表' ROW_FORMAT = DYNAMIC;
) ENGINE = InnoDB CHARACTER SET = utf8mb4 COLLATE = utf8mb4_0900_ai_ci COMMENT = 'Onboarding task table' ROW_FORMAT = DYNAMIC;
CREATE TABLE `stats_mcp_service_date` (
`stats_date` datetime NOT NULL,
`service_id` char(100) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NOT NULL,
`call_count` int NOT NULL DEFAULT '0' COMMENT '调用次数',
`call_count` int NOT NULL DEFAULT '0' COMMENT 'Call count',
`created_at` timestamp NULL DEFAULT NULL,
`updated_at` timestamp NULL DEFAULT NULL,
UNIQUE KEY `uk_stats` (`stats_date`,`service_id`) USING BTREE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci ROW_FORMAT=DYNAMIC;
ALTER TABLE `temp_mcp_service` ADD COLUMN `headers` text CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL COMMENT '请求头配置,[{\"name\":\"name\",\"value\":\"value\",\"description\":\"description\"}]' AFTER `auth_token`;
ALTER TABLE `temp_mcp_service` ADD COLUMN `headers` text CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL COMMENT 'Request headers configuration,[{\"name\":\"name\",\"value\":\"value\",\"description\":\"description\"}]' AFTER `auth_token`;
ALTER TABLE `temp_mcp_service` MODIFY COLUMN `id` char(36) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NOT NULL COMMENT '主键,服务ID' FIRST;
ALTER TABLE `temp_mcp_service` MODIFY COLUMN `id` char(36) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NOT NULL COMMENT 'Primary key, service ID' FIRST;
ALTER TABLE `temp_mcp_service` MODIFY COLUMN `auth_method` enum('free','apikey','token') CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL COMMENT '废弃,鉴权方式:free(免费)、apikey(API 密钥)、token(令牌)' AFTER `base_url`;
ALTER TABLE `temp_mcp_service` MODIFY COLUMN `auth_method` enum('free','apikey','token') CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL COMMENT 'Deprecated, authentication method: free, apikey, token' AFTER `base_url`;
ALTER TABLE `temp_mcp_service` MODIFY COLUMN `auth_header` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL COMMENT '废弃,鉴权请求头名称' AFTER `auth_method`;
ALTER TABLE `temp_mcp_service` MODIFY COLUMN `auth_header` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL COMMENT 'Deprecated, authentication header name' AFTER `auth_method`;
ALTER TABLE `temp_mcp_service` MODIFY COLUMN `auth_token` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL COMMENT '废弃,鉴权令牌值' AFTER `auth_header`;
ALTER TABLE `temp_mcp_service` MODIFY COLUMN `auth_token` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL COMMENT 'Deprecated, authentication token value' AFTER `auth_header`;
ALTER TABLE `temp_mcp_service` MODIFY COLUMN `input_token_price` decimal(10, 2) NULL DEFAULT NULL COMMENT '输入token价钱' AFTER `price`;
ALTER TABLE `temp_mcp_service` MODIFY COLUMN `input_token_price` decimal(10, 2) NULL DEFAULT NULL COMMENT 'Input token price' AFTER `price`;
ALTER TABLE `temp_mcp_service` MODIFY COLUMN `output_token_price` decimal(10, 2) NULL DEFAULT NULL COMMENT '输出token价钱' AFTER `input_token_price`;
ALTER TABLE `temp_mcp_service` MODIFY COLUMN `output_token_price` decimal(10, 2) NULL DEFAULT NULL COMMENT 'Output token price' AFTER `input_token_price`;
ALTER TABLE `user` MODIFY COLUMN `role_id` int NOT NULL COMMENT '角色 ID:0:未确定、1(租户)、2(租户下的普通用户)' AFTER `register_type`;
ALTER TABLE `user` MODIFY COLUMN `role_id` int NOT NULL COMMENT 'Role ID: 0 = unspecified, 1 = tenant, 2 = tenant user' AFTER `register_type`;
DELETE FROM sys_config WHERE `key` = "mcp_server_prefix";
CREATE TABLE `drop_mcp_service` (
`id` char(36) NOT NULL COMMENT 'Primary key, service UUID',
`name` varchar(255) NOT NULL COMMENT 'Service name',
`slug_name` varchar(255) NOT NULL COMMENT 'Unique service identifier (slug format)',
`short_description` text NOT NULL COMMENT 'Brief service description',
`long_description` longtext COMMENT 'Detailed service description (Markdown)',
`updated_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT 'Last update timestamp',
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci COMMENT='Stores MCP alerady dropped service information and configurations';
INSERT INTO `sys_config` (`id`,`key`, `value`,`description`,`created_at`,`updated_at`)
VALUES ('xpack-version','version', '1.1.0', 'User wallet history max count', CURRENT_TIMESTAMP, CURRENT_TIMESTAMP)
+3 -3
View File
@@ -1,11 +1,11 @@
{
"version": "v1.0",
"releaseDate": "2025-07-30",
"version": "v1.1",
"releaseDate": "2025-10-17",
"description": "This is the latest release of the application.",
"downloads": {
"program": {
"xpack_mcp_market": {
"version": "v1.0.1",
"version": "v1.1.0",
"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"
@@ -30,34 +30,3 @@ async def stats(
)
except Exception as e:
return ResponseUtils.error(f"Failed to get stats analytics: {str(e)}", 500)
# @router.get("/platform")
# async def get_platform_overview(db: Session = Depends(get_db)):
# """Get platform overview statistics and metrics."""
# try:
# # Get total users (non-admin and not deleted)
# total_user = db.query(User).filter(User.role_id != 1, User.is_deleted == 0).count() # Non-admin (role_id 1 is admin), not deleted
# # Get total balance
# total_balance_result = db.query(func.sum(UserWallet.balance)).scalar()
# total_balance = int(total_balance_result) if total_balance_result else 0
# # Get total number of services
# total_service = db.query(McpService).count()
# # Get today's invocation count
# today = date.today()
# today_invoke_count = db.query(McpCallLog).filter(func.date(McpCallLog.created_at) == today).count()
# # Build response data
# response_data = {
# "total_user": total_user,
# "total_balance": total_balance,
# "total_service": total_service,
# "invoke_count": {"today": today_invoke_count},
# }
# return ResponseUtils.success(response_data)
# except Exception as e:
# return ResponseUtils.error(f"Failed to get platform overview: {str(e)}", 500)
@@ -33,15 +33,15 @@ def get_config(
try:
faq = json.loads(faq)
except json.JSONDecodeError:
return ResponseUtils.error("FAQ配置格式错误")
return ResponseUtils.error("Invalid FAQ configuration format")
try:
embeded_html = json.loads(embeded_html)
except json.JSONDecodeError:
return ResponseUtils.error("Embeded HTML配置格式错误")
return ResponseUtils.error("Invalid embedded HTML configuration format")
try:
top_navigation = json.loads(top_navigation)
except json.JSONDecodeError:
return ResponseUtils.error("Top Navigation配置格式错误")
return ResponseUtils.error("Invalid top navigation configuration format")
# Build response data
config_data = {
"login": {
@@ -93,15 +93,15 @@ def get_homepage_config(db: Session = Depends(get_db)):
try:
faq = json.loads(faq)
except json.JSONDecodeError:
return ResponseUtils.error("FAQ配置格式错误")
return ResponseUtils.error("Invalid FAQ configuration format")
try:
embeded_html = json.loads(embeded_html)
except json.JSONDecodeError:
return ResponseUtils.error("Embeded HTML配置格式错误")
return ResponseUtils.error("Invalid embedded HTML configuration format")
try:
top_navigation = json.loads(top_navigation)
except json.JSONDecodeError:
return ResponseUtils.error("Top Navigation配置格式错误")
return ResponseUtils.error("Invalid top navigation configuration format")
config_data = {
"faq": faq,
"embeded_html": embeded_html,
@@ -1,3 +1,8 @@
"""Admin MCP service management controller.
Includes endpoints to enable/disable services, update/delete services,
import OpenAPI definitions, and query service info and lists.
"""
import uuid
import logging
from fastapi import APIRouter, Depends, Request, Body, UploadFile, File, HTTPException, Form
@@ -50,7 +55,7 @@ def update_mcp_service_info(request: Request, body: dict = Body(...), mcp_manage
if not UserUtils.is_admin(request):
return ResponseUtils.error(error_msg=error_msg.NO_PERMISSION)
# 验证必须传递 ID
# Validate ID is required
if not body.get("id"):
return ResponseUtils.error(error_msg=error_msg.MISSING_PARAMETER)
@@ -94,7 +99,7 @@ async def openapi_parse(
else:
return ResponseUtils.error(error_msg=error_msg.MISSING_URL_OR_FILE)
# 创建MCP服务
# Create MCP service
service_id = mcp_manager_service.create_service_from_openapi(openapi_for_ai)
result = {"service_id": service_id}
@@ -106,10 +111,10 @@ async def openapi_parse(
return ResponseUtils.error(error_msg=error_msg.INTERNAL_ERROR)
@router.post("/openapi_parse_update", summary="openapi解析(更新)", response_model=dict)
@router.post("/openapi_parse_update", summary="OpenAPI parse (update)", response_model=dict)
async def openapi_parse_update(
request: Request,
id: str = Form(..., description="服务ID"),
id: str = Form(..., description="Service ID"),
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),
@@ -119,11 +124,11 @@ async def openapi_parse_update(
return ResponseUtils.error(error_msg=error_msg.NO_PERMISSION)
try:
# 验证必须提供URL或文件
# Validate URL or file must be provided
if not url and not file:
return ResponseUtils.error(error_msg=error_msg.MISSING_URL_OR_FILE)
# 解析OpenAPI文档
# Parse OpenAPI document
if url:
url_str = str(url)
is_valid = await openapi_manager.validate_openapi_url(url_str)
@@ -135,7 +140,7 @@ async def openapi_parse_update(
else:
return ResponseUtils.error(error_msg=error_msg.MISSING_URL_OR_FILE)
# 更新服务并保存到临时表
# Update service and save into temporary table
result = mcp_manager_service.update_service_from_openapi(id, openapi_for_ai)
return ResponseUtils.success(data=result)
@@ -148,7 +153,7 @@ async def openapi_parse_update(
return ResponseUtils.error(error_msg=error_msg.INTERNAL_ERROR)
@router.get("/service/info", summary="获取MCP服务信息")
@router.get("/service/info", summary="Get MCP service information")
def get_mcp_service_info(request: Request, id: str, mcp_manager_service: McpManagerService = Depends(get_mcp_manager)):
"""Get detailed information of a specific MCP service including API list."""
if not UserUtils.is_admin(request):
@@ -168,7 +173,7 @@ def get_mcp_service_info(request: Request, id: str, mcp_manager_service: McpMana
return ResponseUtils.error(error_msg=error_msg.INTERNAL_ERROR)
@router.get("/service/list", summary="获取mcp服务列表")
@router.get("/service/list", summary="Get MCP service list")
def get_mcp_service_list(
request: Request, page: int = 1, page_size: int = 10, mcp_manager_service: McpManagerService = Depends(get_mcp_manager)
):
@@ -177,11 +182,11 @@ def get_mcp_service_list(
return ResponseUtils.error(error_msg=error_msg.NO_PERMISSION)
try:
# 获取分页数据
# Fetch paginated data
try:
services, total = mcp_manager_service.get_all_paginated(page=page, page_size=page_size)
except AttributeError:
# 如果方法不存在,使用非分页方式
# Fallback to non-paginated method when missing
all_services = mcp_manager_service.get_all()
total = len(all_services)
start = (page - 1) * page_size
@@ -1,3 +1,8 @@
"""Admin system configuration controller.
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
import json
import socket
@@ -31,32 +36,33 @@ def get_user_service(db: Session = Depends(get_db)) -> UserService:
def get_homepage(
sysconfig_service: SysConfigService = Depends(get_sysconfig_service),
):
"""Get homepage configuration including FAQ, top navigation, and embedded HTML."""
faq = sysconfig_service.get_value_by_key(sys_config_key.KEY_FAQ) or "[]"
# 判断是否为空值,若非空,则json反序列化
# Check empty value; if not empty, deserialize JSON
try:
# 数组类型
# Array type
if faq.startswith("[") and faq.endswith("]"):
faq = json.loads(faq)
else:
return ResponseUtils.error("FAQ配置格式错误")
return ResponseUtils.error("Invalid FAQ configuration format")
except json.JSONDecodeError:
return ResponseUtils.error("FAQ配置格式错误")
return ResponseUtils.error("Invalid FAQ configuration format")
top_navigation = sysconfig_service.get_value_by_key(sys_config_key.KEY_TOP_NAVIGATION) or "[]"
try:
# 数组类型
# Array type
if top_navigation.startswith("[") and top_navigation.endswith("]"):
top_navigation = json.loads(top_navigation)
else:
return ResponseUtils.error("Top Navigation配置格式错误")
return ResponseUtils.error("Invalid top navigation configuration format")
except json.JSONDecodeError:
return ResponseUtils.error("Top Navigation配置格式错误")
return ResponseUtils.error("Invalid top navigation configuration format")
embeded_html = sysconfig_service.get_value_by_key(sys_config_key.KEY_EMBEDED_HTML,True) or "{}"
try:
embeded_html = json.loads(embeded_html)
except json.JSONDecodeError:
return ResponseUtils.error("Embeded HTML配置格式错误")
return ResponseUtils.error("Invalid embedded HTML configuration format")
return ResponseUtils.success(
data={
@@ -90,7 +96,7 @@ def get_sysconfig(
):
"""Get all system configuration settings."""
admin_user = user_service.get_admin_user()
# 批量读取配置(小表)
# Batch read configurations (small table)
large_keys = [
sys_config_key.KEY_ABOUT_PAGE,
@@ -103,12 +109,12 @@ def get_sysconfig(
cname_a_ip = small_values.get(sys_config_key.KEY_CNAME_A_IP, "")
if not cname_a_ip:
# 获取外网IP,若无法连接外网,则获取局域网IP
# Get public IP; if unavailable, fall back to local IP
public_ip = get_public_ip()
cname_a_ip = public_ip if public_ip else get_local_ip()
sysconfig_service.set_value_by_key(sys_config_key.KEY_CNAME_A_IP, cname_a_ip,"CNAME A IP")
# 管理员用户名
# Admin username
admin_username = ""
if admin_user and admin_user.name:
admin_username = admin_user.name
@@ -190,7 +196,7 @@ def set_sysconfig(
email_smtp_password = ""
email_smtp_sender = ""
# 使用 get 方法安全地获取嵌套值
# Safely get nested values using `.get`
platform = body.get("platform", {})
platform_name = platform.get("name")
platform_logo = platform.get("logo")
@@ -222,12 +228,12 @@ def set_sysconfig(
google_config = login.get("google", {})
login_google_client = google_config.get("client_id")
login_google_secret = google_config.get("client_secret")
login_google_enable = google_config.get("is_enabled") # 设置默认值为 False
login_google_enable = google_config.get("is_enabled") # default is False
login_email_config = login.get("email", {})
login_email_mode = login_email_config.get("mode")
login_email_is_enable = login_email_config.get("is_enabled") # 设置默认值为 False
login_email_is_enable = login_email_config.get("is_enabled") # default is False
# 获取邮件配置 - 现在从外层获取
# Get email configuration from outer payload
email_config = body.get("email", {})
email_smtp_host = email_config.get("smtp_host")
email_smtp_port = email_config.get("smtp_port")
@@ -237,7 +243,7 @@ def set_sysconfig(
# 批量更新配置
# Batch update configuration
configs = [
(sys_config_key.KEY_PLATFORM_NAME, platform_name, "Platform name"),
(sys_config_key.KEY_PLATFORM_LOGO, platform_logo, "Platform logo",),
@@ -262,21 +268,21 @@ def set_sysconfig(
(sys_config_key.KEY_SOCIAL_ACCOUNT_X_URL, social_account_x_url, "X URL"),
(sys_config_key.KEY_LOGIN_GOOGLE_CLIENT, login_google_client, "Google login client ID"),
(sys_config_key.KEY_LOGIN_GOOGLE_SECRET, login_google_secret, "Google login client secret"),
(sys_config_key.KEY_LOGIN_GOOGLE_ENABLE, login_google_enable, "谷歌登录是否启用"),
(sys_config_key.KEY_EMAIL_SMTP_HOST, email_smtp_host, "邮件SMTP主机"),
(sys_config_key.KEY_EMAIL_SMTP_PORT, email_smtp_port, "邮件SMTP端口"),
(sys_config_key.KEY_EMAIL_SMTP_USER, email_smtp_user, "邮件SMTP用户名"),
(sys_config_key.KEY_EMAIL_SMTP_PASSWORD, email_smtp_password, "邮件SMTP密码"),
(sys_config_key.KEY_EMAIL_SMTP_SENDER, email_smtp_sender, "邮件发送者地址"),
(sys_config_key.KEY_LOGIN_EMAIL_ENABLE, login_email_is_enable, "邮件是否启用"),
(sys_config_key.KEY_LOGIN_EMAIL_MODE, login_email_mode, "邮件模式"),
(sys_config_key.KEY_LOGIN_GOOGLE_ENABLE, login_google_enable, "Google login enabled"),
(sys_config_key.KEY_EMAIL_SMTP_HOST, email_smtp_host, "Email SMTP host"),
(sys_config_key.KEY_EMAIL_SMTP_PORT, email_smtp_port, "Email SMTP port"),
(sys_config_key.KEY_EMAIL_SMTP_USER, email_smtp_user, "Email SMTP user"),
(sys_config_key.KEY_EMAIL_SMTP_PASSWORD, email_smtp_password, "Email SMTP password"),
(sys_config_key.KEY_EMAIL_SMTP_SENDER, email_smtp_sender, "Email sender address"),
(sys_config_key.KEY_LOGIN_EMAIL_ENABLE, login_email_is_enable, "Email login enabled"),
(sys_config_key.KEY_LOGIN_EMAIL_MODE, login_email_mode, "Email login mode"),
]
# 更新管理员用户名和密码
# Update admin username and password
user_service.update_admin(name=admin_username, password=admin_password)
for key, value, desc in configs:
if value is not None: # 只更新有值的配置
if value is not None: # Only update when value is not None
is_large = False
if key == sys_config_key.KEY_ABOUT_PAGE:
is_large = True
@@ -321,7 +327,7 @@ def set_sysconfig(
return get_sysconfig(sysconfig_service, user_service)
except Exception as e:
return ResponseUtils.error(f"更新系统配置失败:{str(e)}")
return ResponseUtils.error(f"Failed to update system configuration: {str(e)}")
@router.post("/test_email")
@@ -333,25 +339,25 @@ def test_email_config(
try:
test_email = body.get("email")
if not test_email:
return ResponseUtils.error("请提供测试邮箱地址")
return ResponseUtils.error("Please provide a test email address")
# 测试连接配置
# Test connection configuration
success, message = EmailUtils.test_email_config(db)
if not success:
return ResponseUtils.error(message)
# 发送测试邮件
# Send test email
test_success = EmailUtils.send_email(
db, "XPack 邮件配置测试", "这是一封测试邮件,用于验证邮件配置是否正确。如果您收到这封邮件,说明配置成功!", test_email, False
db, "XPack Email Configuration Test", "This is a test email to verify the email configuration. If you receive this email, the configuration is working!", test_email, False
)
if test_success:
return ResponseUtils.success(data={"message": "测试邮件发送成功,请检查收件箱"})
return ResponseUtils.success(data={"message": "Test email sent successfully, please check your inbox"})
else:
return ResponseUtils.error("测试邮件发送失败")
return ResponseUtils.error("Failed to send test email")
except Exception as e:
return ResponseUtils.error(f"测试邮件配置失败:{str(e)}")
return ResponseUtils.error(f"Failed to test email configuration: {str(e)}")
+22 -10
View File
@@ -7,39 +7,51 @@ from services.common.utils.response_utils import ResponseUtils
router = APIRouter()
"""Admin image upload controller.
Provides an endpoint to upload images, verify optional SHA256 checksum,
and store files under date-based directories.
"""
@router.post("/image")
async def upload(
img: UploadFile = File(None),
sha256: str = Form(None),
):
# 1. 获取上传文件的名称
"""Upload an image file.
- Validates presence of file and filename
- Optionally verifies uploaded content against provided SHA256
- Saves the file under `uploads/images/<YYYYMMDD>/`
- Returns the relative `file_path`
"""
# 1. Get uploaded file name
if not img:
return ResponseUtils.error(message="未提供文件", code=400)
return ResponseUtils.error(message="No file provided", code=400)
filename = img.filename
if not filename:
return ResponseUtils.error(message="未提供文件名称", code=400)
return ResponseUtils.error(message="No filename provided", code=400)
# 2. 计算上传文件的sha256值
# 2. Calculate SHA256 of uploaded content
contents = await img.read()
file_hash = hashlib.sha256(contents).hexdigest()
# 重置文件指针,以便后续保存文件
# Reset file pointer for subsequent save
await img.seek(0)
# 检查sha256是否一致
# Verify provided SHA256 matches computed hash
if sha256 and file_hash != sha256:
return ResponseUtils.error(message="文件SHA256校验失败", code=400)
return ResponseUtils.error(message="File SHA256 checksum mismatch", code=400)
# 3. 根据当前日期创建目录
# 3. Create directory based on current date
today = datetime.now()
date_dir = today.strftime("%Y%m%d")
# 创建上传目录
# Create upload directory
upload_dir = os.path.join("uploads","images")
upload_dir = os.path.join(upload_dir, date_dir)
os.makedirs(upload_dir, exist_ok=True)
# 保存文件
# Save file
file_path = os.path.join(upload_dir, filename)
with open(file_path, "wb") as f:
+3 -3
View File
@@ -40,7 +40,7 @@ from services.admin_service.task import *
setup_logging("admin_service")
logger = get_logger(__name__)
# 全局消费者实例
# Global consumer instance
consumer_instance = None
consumer_thread = None
@@ -172,12 +172,12 @@ app.include_router(user_task.router, prefix="/api/onboarding")
# Logging is already configured by setup_logging("admin_service")
# 创建uploads目录(如果不存在)
# Create uploads directory if it does not exist
uploads_dir = "uploads"
if not os.path.exists(uploads_dir):
os.makedirs(uploads_dir)
# 挂载静态文件目录
# Mount static file directory
app.mount("/uploads", StaticFiles(directory=uploads_dir), name="uploads")
@app.get("/")
@@ -0,0 +1,33 @@
from datetime import datetime, timezone
from sqlalchemy.orm import Session
from services.common.models.mcp_service import DropMCPService
from typing import Dict, List
"""Repository for MCP services: CRUD, pagination, and public listing."""
class DropMcpServiceRepository:
"""Data access layer for McpService model."""
def __init__(self, db: Session):
self.db = db
def create(self, mcp_service: DropMCPService) -> DropMCPService:
"""Create a service; set timestamps if missing and persist."""
# Set timestamps explicitly if not already set
current_time = datetime.now(timezone.utc)
if not hasattr(mcp_service, "updated_at") or mcp_service.updated_at is None:
mcp_service.updated_at = current_time
self.db.add(mcp_service)
self.db.commit()
self.db.refresh(mcp_service)
return mcp_service
def all(self) -> Dict[str, DropMCPService]:
"""Retrieve all services."""
services = self.db.query(DropMCPService).all()
return {service.id: service for service in services}
def all_by_service_ids(self, service_ids: List[str]) -> Dict[str, DropMCPService]:
"""Retrieve all services by service IDs."""
services = self.db.query(DropMCPService).filter(DropMCPService.id.in_(service_ids)).all()
return {service.id: service for service in services}
@@ -4,11 +4,14 @@ from services.common.models.mcp_service import McpService
from typing import Optional, Tuple, List
"""Repository for MCP services: CRUD, pagination, and public listing."""
class McpServiceRepository:
"""Data access layer for McpService model."""
def __init__(self, db: Session):
self.db = db
def update_enabled(self, id: str, enabled: int) -> McpService:
"""Enable or disable a service by ID; raises if not found."""
service = self.db.query(McpService).filter(McpService.id == id).first()
if not service:
raise ValueError("Service not found")
@@ -18,6 +21,7 @@ class McpServiceRepository:
return service
def delete(self, id: str) -> Optional[McpService]:
"""Delete a service by ID; returns deleted entity or None."""
service = self.db.query(McpService).filter(McpService.id == id).first()
if not service:
return None
@@ -26,6 +30,7 @@ class McpServiceRepository:
return service
def update(self, mcp_service: McpService) -> McpService:
"""Update mutable fields for an existing service; returns refreshed entity."""
existing_service = self.db.query(McpService).filter(McpService.id == mcp_service.id).first()
if not existing_service:
raise ValueError("Service not found")
@@ -47,12 +52,15 @@ class McpServiceRepository:
return existing_service
def get_by_id(self, id: str) -> Optional[McpService]:
"""Get a service by primary ID."""
return self.db.query(McpService).filter(McpService.id == id).first()
def get_by_slug_name(self, slug_name: str) -> Optional[McpService]:
"""Get a service by unique slug name."""
return self.db.query(McpService).filter(McpService.slug_name == slug_name).first()
def get_all(self) -> List[McpService]:
"""List all services ordered by creation time descending."""
return self.db.query(McpService).order_by(McpService.created_at.desc()).all()
def get_all_paginated(self, page: int = 1, page_size: int = 10) -> Tuple[List[McpService], int]:
@@ -66,6 +74,7 @@ class McpServiceRepository:
return services, total
def create(self, mcp_service: McpService) -> McpService:
"""Create a service; set timestamps if missing and persist."""
# Set timestamps explicitly if not already set
current_time = datetime.now(timezone.utc)
if not hasattr(mcp_service, "created_at") or mcp_service.created_at is None:
@@ -1,22 +1,28 @@
"""Repository for payment channels: list, get, update status and config."""
from datetime import datetime, timezone
from sqlalchemy.orm import Session
from services.common.models.payment_channel import PaymentChannel
from typing import Optional, Tuple, List
class PaymentChannelRepository:
"""Data access layer for PaymentChannel model."""
def __init__(self, db: Session):
self.db = db
def payment_channel_list(self) -> Tuple[int, List[PaymentChannel]]:
"""Return total count and all payment channels."""
return self.db.query(PaymentChannel).count(),self.db.query(PaymentChannel).all()
def payment_channel_get(self, id: str) -> Optional[PaymentChannel]:
"""Fetch a payment channel by primary ID."""
return self.db.query(PaymentChannel).filter(PaymentChannel.id == id).first()
def payment_channel_available_list(self) -> List[PaymentChannel]:
"""List enabled payment channels."""
return self.db.query(PaymentChannel).filter(PaymentChannel.status == 1).all()
def update_status(self, id: str, status: int) -> Optional[PaymentChannel]:
"""Update channel status and refresh updated_at; returns updated entity or None."""
payment_channel = self.payment_channel_get(id)
if payment_channel:
payment_channel.status = status
@@ -26,6 +32,7 @@ class PaymentChannelRepository:
return None
def update_config(self, id: str, config: str) -> Optional[PaymentChannel]:
"""Update channel config and refresh updated_at; returns updated entity or None."""
payment_channel = self.payment_channel_get(id)
if payment_channel:
payment_channel.config = config
@@ -5,23 +5,29 @@ from sqlalchemy.orm import Session
from services.common.models.sys_config_large import SysConfigLarge
from typing import Dict, Optional,List
"""Repository for large system configuration: KV CRUD helpers."""
class SysConfigLargeRepository:
"""Data access layer for SysConfigLarge model."""
def __init__(self, db: Session):
self.db = db
def get_all(self, keys: List[str] = []) -> Dict[str, str]:
"""Return mapping of key->value, optionally filtered by provided keys."""
if keys:
return {item.key: item.value for item in self.db.query(SysConfigLarge).filter(SysConfigLarge.key.in_(keys)).all()}
return {item.key: item.value for item in self.db.query(SysConfigLarge).all()}
def get_by_key(self, key: str) -> Optional[SysConfigLarge]:
"""Get SysConfigLarge entity by unique key."""
return self.db.query(SysConfigLarge).filter(SysConfigLarge.key == key).first()
def get_value_by_key(self, key: str) -> Optional[str]:
"""Get value string for given key, or None if missing."""
sys_config = self.get_by_key(key)
if sys_config:
return sys_config.value
return None
def set_value_by_key(self, key: str, value: str,description:str) -> Optional[SysConfigLarge]:
"""Set or create value and description for a key; returns entity."""
sys_config = self.get_by_key(key)
if sys_config:
sys_config.value = value
@@ -41,6 +47,7 @@ class SysConfigLargeRepository:
self.db.commit()
return sys_config
def delete_by_key(self, key: str) -> bool:
"""Delete config by key; returns True if deleted, else False."""
sys_config = self.get_by_key(key)
if sys_config:
self.db.delete(sys_config)
@@ -5,21 +5,27 @@ from sqlalchemy.orm import Session
from services.common.models.sys_config import SysConfig
from typing import Optional,Dict,List
"""Repository for small system configuration: KV CRUD helpers."""
class SysConfigRepository:
"""Data access layer for SysConfig model."""
def __init__(self, db: Session):
self.db = db
def get_all(self, keys: List[str] = []) -> Dict[str, str]:
"""Return mapping of key->value, optionally filtered by provided keys."""
if keys:
return {item.key: item.value for item in self.db.query(SysConfig).filter(SysConfig.key.in_(keys)).all()}
return {item.key: item.value for item in self.db.query(SysConfig).all()}
def get_by_key(self, key: str) -> Optional[SysConfig]:
"""Get SysConfig entity by unique key."""
return self.db.query(SysConfig).filter(SysConfig.key == key).first()
def get_value_by_key(self, key: str) -> Optional[str]:
"""Get value string for given key, or None if missing."""
sys_config = self.get_by_key(key)
if sys_config:
return sys_config.value
return None
def set_value_by_key(self, key: str, value: str,description:str) -> Optional[SysConfig]:
"""Set or create value and description for a key; returns entity."""
sys_config = self.get_by_key(key)
if sys_config:
sys_config.value = value
@@ -39,6 +45,7 @@ class SysConfigRepository:
self.db.commit()
return sys_config
def delete_by_key(self, key: str) -> bool:
"""Delete config by key; returns True if deleted, else False."""
sys_config = self.get_by_key(key)
if sys_config:
self.db.delete(sys_config)
@@ -4,11 +4,14 @@ from sqlalchemy.orm import Session
from services.common.models.user_access_token import UserAccessToken
"""Repository for user access tokens: create and revoke by token."""
class UserAccessTokenRepository:
"""Data access layer for UserAccessToken model."""
def __init__(self, db: Session):
self.db = db
def create(self, user_id: str) -> UserAccessToken:
"""Create a new access token for the user, expiring in 30 days."""
user_access_token = UserAccessToken(
id=str(uuid.uuid4()), user_id=user_id, token=str(uuid.uuid4()), expire_at=datetime.now(timezone.utc) + timedelta(days=30)
)
@@ -19,6 +22,7 @@ class UserAccessTokenRepository:
return user_access_token
def delete_by_token(self, token: str) -> None:
"""Revoke an access token by its token string."""
user_access_token = self.db.query(UserAccessToken).filter(UserAccessToken.token == token).first()
if user_access_token:
self.db.delete(user_access_token)
@@ -1,3 +1,4 @@
"""Repository for managing user API keys: create, update, delete, list."""
import uuid
import secrets
from datetime import datetime, timezone
@@ -7,10 +8,12 @@ from typing import Optional, List
class UserApiKeyRepository:
"""Repository for generating and managing user API keys."""
def __init__(self, db: Session):
self.db = db
def create(self, user_id: str, name: str, description: str) -> UserApiKey:
"""Create a new API key for the user with generated token."""
now = datetime.now(timezone.utc)
apikey = secrets.token_urlsafe(32)
user_apikey = UserApiKey(
@@ -29,6 +32,7 @@ class UserApiKeyRepository:
return user_apikey
def update(self, id: str, name: Optional[str] = None, description: Optional[str] = None, expire_at: Optional[datetime] = None) -> UserApiKey:
"""Update metadata for an existing API key (name, description, expiration)."""
user_apikey = self.db.query(UserApiKey).filter(UserApiKey.id == id).first()
if not user_apikey:
raise ValueError("API key not found or does not belong to the user")
@@ -46,6 +50,7 @@ class UserApiKeyRepository:
return user_apikey
def delete(self, id: str) -> None:
"""Delete API key by primary ID."""
user_apikey = self.db.query(UserApiKey).filter(UserApiKey.id == id).first()
if not user_apikey:
raise ValueError("API key not found or does not belong to the user")
@@ -54,7 +59,9 @@ class UserApiKeyRepository:
self.db.commit()
def get_by_user_id(self, user_id: str) -> List[UserApiKey]:
"""List API keys belonging to the specified user."""
return self.db.query(UserApiKey).filter(UserApiKey.user_id == user_id).all()
def get_by_id(self, id: str) -> Optional[UserApiKey]:
"""Get API key by primary ID."""
return self.db.query(UserApiKey).filter(UserApiKey.id == id).first()
@@ -1,16 +1,20 @@
"""User task repository: list and update user task status."""
from sqlalchemy.orm import Session
from datetime import datetime, timezone
from typing import List
from services.common.models.user_task import UserTask
class UserTaskRepository:
"""Repository for managing user tasks (list, skip, update)."""
def __init__(self, db: Session):
self.db = db
def user_tasks_by_id(self, user_id: str) -> List[UserTask]:
"""Return all tasks belonging to the given user."""
return self.db.query(UserTask).filter(UserTask.user_id == user_id).all()
def skip_task(self, user_id: str, task_id: str):
"""Mark task as skipped (status=1); create record if missing."""
user_task = self.db.query(UserTask).filter(UserTask.user_id == user_id, UserTask.task_id == task_id).first()
if user_task:
user_task.task_status = 1
@@ -20,6 +24,7 @@ class UserTaskRepository:
self.db.add(user_task)
self.db.commit()
def update_task_status(self, user_id: str, task_id: str, task_status: int):
"""Update task status for user and task; create record if missing."""
user_task = self.db.query(UserTask).filter(UserTask.user_id == user_id, UserTask.task_id == task_id).first()
status_int = 1 if int(task_status) == 1 else 0
if user_task:
@@ -56,7 +56,7 @@ class BillingMessageHandler:
return False
# Update stats_mcp_service_date record
# 统计按小时分桶(UTC 时区),对齐到整点
# 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
@@ -241,7 +241,7 @@ class BillingMessageHandler:
new_balance: New balance
"""
try:
cache_key = f"wallet:balance:{user_id}"
cache_key = f"xpack:wallet:balance:{user_id}"
self.redis.set(cache_key, str(new_balance), ex=300) # 5 minutes expiration
except Exception as e:
logger.warning(f"Failed to update wallet cache - User ID: {user_id}: {str(e)}")
@@ -9,11 +9,13 @@ from services.admin_service.repositories.mcp_service_repository import McpServic
from services.admin_service.repositories.mcp_tool_api_repository import McpToolApiRepository
from services.admin_service.repositories.temp_mcp_service_repository import TempMcpServiceRepository
from services.admin_service.repositories.temp_mcp_tool_api_repository import TempMcpToolApiRepository
from services.common.models.mcp_service import McpService, AuthMethod, ChargeType
from services.admin_service.repositories.drop_mcp_service_repository import DropMcpServiceRepository
from services.common.models.mcp_service import McpService, ChargeType,DropMCPService
from services.common.models.mcp_tool_api import McpToolApi, HttpMethod
from services.common.models.temp_mcp_service import TempMcpService, AuthMethod as TempAuthMethod, ChargeType as TempChargeType
from services.common.models.temp_mcp_service import TempMcpService, ChargeType as TempChargeType
from services.common.models.temp_mcp_tool_api import TempMcpToolApi, HttpMethod as TempHttpMethod
from services.admin_service.services.openapi_helper import OpenApiForAI
from services.common.redis import redis_client
logger = logging.getLogger(__name__)
@@ -76,11 +78,40 @@ class McpManagerService:
self.mcp_tool_api_repository = McpToolApiRepository(db)
self.temp_mcp_service_repository = TempMcpServiceRepository(db)
self.temp_mcp_tool_api_repository = TempMcpToolApiRepository(db)
self.drop_mcp_service_repository = DropMcpServiceRepository(db)
self.redis = redis_client
def _delete_service_price_cache(self, service_id: str) -> None:
"""
Update wallet balance cache in Redis
Args:
user_id: User ID
new_balance: New balance
"""
try:
cache_key = f"xpack:service:price:{service_id}"
self.redis.delete(cache_key) # 5 minutes expiration
except Exception as e:
logger.warning(f"Failed to delete service price cache - Service ID: {service_id}: {str(e)}")
def update_enabled(self, id: str, enabled: int) -> McpService:
return self.mcp_service_repository.update_enabled(id, enabled)
def delete(self, id: str) -> Optional[McpService]:
service = self.mcp_service_repository.get_by_id(id)
if not service:
raise ValueError("Service not found")
self._delete_service_price_cache(id)
service = self.drop_mcp_service_repository.create(DropMCPService(
id=id,
name=service.name,
slug_name=service.slug_name,
short_description=service.short_description,
long_description=service.long_description,
))
if not service:
raise ValueError("Failed to create drop service")
return self.mcp_service_repository.delete(id)
def update(self, body: dict) -> bool:
@@ -173,25 +204,33 @@ class McpManagerService:
if "base_url" in body and body["base_url"] is not None:
existing_service.base_url = body["base_url"]
if "charge_type" in body and body["charge_type"] is not None:
charge_type_value = body["charge_type"]
try:
old_charge_type = existing_service.charge_type
if isinstance(charge_type_value, str):
existing_service.charge_type = ChargeType(charge_type_value.lower())
else:
existing_service.charge_type = charge_type_value
if old_charge_type != existing_service.charge_type:
self._delete_service_price_cache(service_id)
except ValueError:
# If the provided value is not a valid ChargeType, default to FREE
existing_service.charge_type = ChargeType.FREE
match existing_service.charge_type:
case ChargeType.PER_CALL:
if "price" in body and body["price"] is not None:
if body["price"] != existing_service.price:
self._delete_service_price_cache(service_id)
existing_service.price = body["price"]
case ChargeType.PER_TOKEN:
if "input_token_price" in body and body["input_token_price"] is not None:
if body["input_token_price"] != existing_service.input_token_price:
self._delete_service_price_cache(service_id)
existing_service.input_token_price = body["input_token_price"]
if "output_token_price" in body and body["output_token_price"] is not None:
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 "tags" in body and body["tags"] is not None:
# If provided as array, convert to string for storage
@@ -128,5 +128,5 @@ class OpenApiManager:
return await HttpUtils.validate_url_accessibility(url, timeout=10)
# 创建全局实例
# Create global instance
openapi_manager = OpenApiManager()
@@ -3,6 +3,7 @@ import logging
import asyncio
from uuid import uuid4
from typing import Optional
from decimal import Decimal
from sqlalchemy.orm import Session
from services.admin_service.repositories.user_repository import UserRepository
from services.admin_service.repositories.user_wallet_repository import UserWalletRepository
@@ -14,6 +15,7 @@ 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.redis import redis_client
logger = logging.getLogger(__name__)
@@ -25,6 +27,7 @@ class PaymentService:
self.user_wallet_history_repository = UserWalletHistoryRepository(db)
self.payment_channel_service = PaymentChannelService(db)
self.sys_config_repository = SysConfigRepository(db)
self.redis = redis_client
def _get_stripe_config(self) -> Optional[dict]:
"""Get Stripe configuration"""
@@ -163,7 +166,7 @@ class PaymentService:
if not user:
logger.error(f"User with ID {user_id} not found.")
raise ValueError("User not found.")
# 生成32位uuid
# Generate 32-character UUID
transaction_id = str(uuid4()).replace("-", "")
# create a new user wallet history entry for the deposit
user_wallet_history = self.user_wallet_history_repository.add_deposit(user_id=user_id, transaction_id=transaction_id, amount=amount, payment_method="wechat")
@@ -171,7 +174,7 @@ class PaymentService:
logger.error("Failed to create user wallet history for deposit.")
raise RuntimeError("Failed to create payment.")
# 获取配置参数
# Get configuration parameters
app_id = config.get(WxPayConfig.APP_ID)
apiv3_key = config.get(WxPayConfig.APIV3_KEY)
mch_id = config.get(WxPayConfig.MCH_ID)
@@ -179,7 +182,7 @@ class PaymentService:
cert_serial_no = config.get(WxPayConfig.CERT_SERIAL_NO)
notify_url = config.get(WxPayConfig.NOTIFY_URL)
# 详细的配置验证
# Detailed configuration validation
missing_configs = []
if not app_id: missing_configs.append("app_id")
if not apiv3_key: missing_configs.append("apiv3_key")
@@ -193,7 +196,7 @@ class PaymentService:
logger.error(f"Current config keys: {list(config.keys()) if config else 'None'}")
return None
# 验证配置值的格式
# Validate configuration value formats
if not str(mch_id).isdigit():
logger.error(f"Invalid mch_id format: {mch_id} (should be numeric)")
return None
@@ -257,6 +260,7 @@ class PaymentService:
return False
if status == 1:
# Add wallet balance and complete the deposit
return self._add_wallet_balance(payment_id, payment_channel_id)
self.user_wallet_history_repository.set_status(payment_id, payment_channel_id, status)
return True
@@ -311,6 +315,7 @@ class PaymentService:
)
if success:
self._delete_wallet_cache(user_id)
# Create wallet history record after successful balance update
return self.user_wallet_history_repository.deposit_complete(payment_id, payment_channel_id)
else:
@@ -327,6 +332,20 @@ class PaymentService:
logger.error(f"Failed to update balance after {max_retries} attempts for user_id {user_id}")
return False
def _delete_wallet_cache(self, user_id: str) -> None:
"""
Update wallet balance cache in Redis
Args:
user_id: User ID
new_balance: New balance
"""
try:
cache_key = f"xpack:wallet:balance:{user_id}"
self.redis.delete(cache_key) # 5 minutes expiration
except Exception as e:
logger.warning(f"Failed to delete wallet cache - User ID: {user_id}: {str(e)}")
def platform_payment(self, user_id: str, amount: float, transaction_id: str, typ: str = "incr", payment_method: str = "platform", max_retries: int = 3) -> bool:
"""
Process platform payment for user wallet with high-concurrency transaction safety.
@@ -389,7 +408,7 @@ class PaymentService:
self.user_wallet_history_repository.set_balance(
user_id=user_id, amount=amount, payment_method=payment_method, transaction_id=transaction_id,status=1
)
self._delete_wallet_cache(user_id)
logger.info(f"Successfully updated balance for user_id {user_id}: {current_wallet.balance} -> {new_balance}")
return True
@@ -9,6 +9,8 @@ from services.admin_service.repositories.user_repository import UserRepository
from services.admin_service.repositories.user_wallet_history_repository import UserWalletHistoryRepository
from services.admin_service.repositories.stats_mcp_service_date_repository import StatsMcpServiceDateRepository
from services.admin_service.repositories.mcp_service_repository import McpServiceRepository
from services.admin_service.repositories.drop_mcp_service_repository import DropMcpServiceRepository
class StatsService:
def __init__(self, db: Session = SessionLocal()):
@@ -16,6 +18,7 @@ class StatsService:
self.user_wallet_repository = UserWalletHistoryRepository(db)
self.stats_mcp_service_date_repository = StatsMcpServiceDateRepository(db)
self.mcp_service_repository = McpServiceRepository(db)
self.drop_mcp_service_repository = DropMcpServiceRepository(db)
def get_registered_user_stats(self) -> dict:
"""
@@ -24,7 +27,7 @@ class StatsService:
Returns:
dict: Registered stats
"""
# 当天0点
# Start of day (00:00)
today_start = datetime.now().replace(hour=0, minute=0, second=0, microsecond=0)
pass30 = today_start - timedelta(days=30)
@@ -40,7 +43,7 @@ class StatsService:
Returns:
dict: Deposit stats
"""
# 当天0点
# Start of day (00:00)
today_start = datetime.now().replace(hour=0, minute=0, second=0, microsecond=0)
pass30 = today_start - timedelta(days=30)
@@ -56,7 +59,7 @@ class StatsService:
Returns:
dict: Call stats
"""
# 当天0点
# Start of day (00:00)
today_start = datetime.now().replace(hour=0, minute=0, second=0, microsecond=0)
pass30 = today_start - timedelta(days=30)
@@ -75,21 +78,40 @@ class StatsService:
"""
now = datetime.now()
pass30 = now - timedelta(days=30)
# 获取统计数据(仅包含有调用的服务)
# Get stats (include only services with calls)
stats = self.stats_mcp_service_date_repository.stats_call_count_group_by_service(pass30)
stats_map = {item["service_id"]: int(item.get("count", 0)) for item in stats}
# 遍历所有服务,填充缺失调用数为 0,并按调用数降序
# Iterate all services, fill missing call count as 0, sort by calls desc
services = self.mcp_service_repository.get_all()
result = [
{
result = []
for service in services:
result.append({
"id": service.id,
"name": service.name,
"short_description": service.short_description,
"call_count": stats_map.get(service.id, 0),
}
for service in services
]
# 先按调用数降序,再按名称升序
"is_deleted": False,
})
# delete service.id from stats_map
del stats_map[service.id]
drop_service_ids = []
for key in stats_map:
call_count = stats_map.get(key, 0)
if call_count != 0:
drop_service_ids.append(key)
# Add deleted services
deleted_services = self.drop_mcp_service_repository.all_by_service_ids(drop_service_ids)
for id,item in deleted_services.items():
result.append({
"id": id,
"name": item.name,
"short_description": item.short_description,
"call_count": stats_map.get(id, 0),
"is_deleted": True,
})
# Sort by calls desc, then by name asc
result.sort(key=lambda x: (-x["call_count"], x["name"]))
return result
@@ -80,7 +80,7 @@ class UserStatsService:
if len(columns) == 0:
result.append({"stats_day": date_str, "call_tool_count": 0, "amount": "0.0"})
else:
#columns[1]不用科学记数法,保留六位小数
# columns[1] use fixed-point with six decimals, not scientific notation
result.append({"stats_day": date_str, "call_tool_count": columns[0], "amount": columns[1]})
# result.append({"stats_day": date_str, "call_tool_count": call_count, "amount": amount})
+166 -167
View File
@@ -19,7 +19,7 @@ logger = get_logger(__name__)
@dataclass
class OrderInfo:
"""订单信息数据类"""
"""Order information data class"""
created_time: datetime
user_id: Optional[str] = None
amount: Optional[float] = None
@@ -28,7 +28,7 @@ class OrderInfo:
@dataclass
class PaymentChannelConfig:
"""支付渠道配置"""
"""Payment channel configuration"""
channel_id: str
enabled: bool
config: Dict[str, Any]
@@ -36,7 +36,7 @@ class PaymentChannelConfig:
@dataclass
class ClientStatus:
"""客户端状态"""
"""Client status"""
enabled: bool
last_check_time: datetime
error_count: int = 0
@@ -44,57 +44,57 @@ class ClientStatus:
class PaymentClient(ABC):
"""支付客户端抽象基类"""
"""Payment client abstract base class"""
@abstractmethod
def query_order_status(self, order_id: str) -> Optional[Dict[str, Any]]:
"""查询订单状态"""
"""Query order status"""
pass
@abstractmethod
def parse_order_status(self, response: Dict[str, Any]) -> tuple[str, bool]:
"""
解析订单状态响应
Parse order status response
Returns:
tuple[status, should_remove]: (状态描述, 是否应该从监控队列移除)
tuple[status, should_remove]: (status description, whether to remove from monitor queue)
"""
pass
class AlipayPaymentClient(PaymentClient):
"""支付宝支付客户端"""
"""Alipay payment client"""
def __init__(self, app_id: str, app_private_key: str, alipay_public_key: str):
self.client = AlipayClient(app_id, app_private_key, alipay_public_key)
def query_order_status(self, order_id: str) -> Optional[Dict[str, Any]]:
"""查询支付宝订单状态"""
"""Query Alipay order status"""
try:
response = self.client.query_order_status(order_id)
return response
except Exception as e:
logger.error(f"查询支付宝订单 {order_id} 状态失败: {e}")
logger.error(f"Failed to query Alipay order {order_id} status: {e}")
return None
def parse_order_status(self, response: Dict[str, Any]) -> tuple[str, bool]:
"""解析支付宝订单状态"""
"""Parse Alipay order status"""
try:
# 解析响应JSON
# Parse response JSON
if isinstance(response, str):
response_data = json.loads(response)
else:
response_data = response
# 获取响应内容
# Get response content
alipay_response = response_data.get('alipay_trade_query_response', {})
if alipay_response.get('code') != '10000':
logger.warning(f"支付宝订单查询失败: {alipay_response.get('msg', '未知错误')}")
logger.warning(f"Alipay order query failed: {alipay_response.get('msg', 'Unknown error')}")
return "QUERY_FAILED", False
trade_status = alipay_response.get('trade_status', '')
# 根据交易状态处理
# Handle according to trade status
if trade_status == 'WAIT_BUYER_PAY':
return "WAITING", False
elif trade_status in ['TRADE_SUCCESS', 'TRADE_FINISHED']:
@@ -102,24 +102,24 @@ class AlipayPaymentClient(PaymentClient):
elif trade_status in ['TRADE_CLOSED', 'TRADE_CANCELED']:
return "FAILED", True
else:
logger.warning(f"支付宝订单未知状态: {trade_status}")
logger.warning(f"Unknown Alipay trade status: {trade_status}")
return f"UNKNOWN_{trade_status}", False
except Exception as e:
logger.error(f"解析支付宝订单响应失败: {e}")
logger.error(f"Failed to parse Alipay order response: {e}")
return "PARSE_ERROR", False
class WxPayPaymentClient(PaymentClient):
"""微信支付客户端"""
"""WeChat Pay client"""
def __init__(self, app_id: str, apiv3_key: str, mch_id: str, private_key: str, cert_serial_no: str,notify_url: str):
self.client = WxPayClient(app_id, apiv3_key, mch_id, private_key, cert_serial_no,notify_url)
def query_order_status(self, order_id: str) -> Optional[Dict[str, Any]]:
"""查询微信支付订单状态"""
"""Query WeChat Pay order status"""
try:
# 微信支付客户端是异步的,需要在事件循环中运行
# WeChat Pay client is async; run in event loop
loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop)
try:
@@ -128,15 +128,15 @@ class WxPayPaymentClient(PaymentClient):
finally:
loop.close()
except Exception as e:
logger.error(f"查询微信支付订单 {order_id} 状态失败: {e}")
logger.error(f"Failed to query WeChat Pay order {order_id} status: {e}")
return None
def parse_order_status(self, response: Dict[str, Any]) -> tuple[str, bool]:
"""解析微信支付订单状态"""
"""Parse WeChat Pay order status"""
try:
trade_state = response.get('trade_state', '')
# 根据交易状态处理
# Handle according to trade state
if trade_state == 'NOTPAY':
return "WAITING", False
elif trade_state == 'SUCCESS':
@@ -146,16 +146,16 @@ class WxPayPaymentClient(PaymentClient):
elif trade_state == 'USERPAYING':
return "PAYING", False
else:
logger.warning(f"微信支付订单未知状态: {trade_state}")
logger.warning(f"Unknown WeChat trade state: {trade_state}")
return f"UNKNOWN_{trade_state}", False
except Exception as e:
logger.error(f"解析微信支付订单响应失败: {e}")
logger.error(f"Failed to parse WeChat order response: {e}")
return "PARSE_ERROR", False
class OrderMonitorTask:
"""通用支付订单状态监控服务 - 单例模式"""
"""Generic payment order status monitoring service - singleton pattern"""
_instance = None
_lock = threading.Lock()
@@ -169,38 +169,37 @@ class OrderMonitorTask:
return cls._instance
def __init__(self):
# 确保只初始化一次
# Ensure initialization only happens once
if hasattr(self, '_initialized') and self._initialized:
return
self.payment_clients: Dict[str, PaymentClient] = {}
self.client_status: Dict[str, ClientStatus] = {} # 客户端状态管理
self.client_status: Dict[str, ClientStatus] = {} # Client status management
self.order_queue: Queue[OrderInfo] = Queue(maxsize=200)
self.is_running = False
self.monitor_thread: Optional[threading.Thread] = None
self.config_check_thread: Optional[threading.Thread] = None # 配置检查线程
self.check_interval = 10 # 检查间隔10秒
self.config_check_interval = 60 # 配置检查间隔60秒
self.timeout_minutes = 6 # 超时时间6分钟
self.config_check_thread: Optional[threading.Thread] = None # Configuration check thread
self.check_interval = 10 # Check interval: 10s
self.config_check_interval = 60 # Config check interval: 60s
self.timeout_minutes = 6 # Timeout: 6 minutes
self._initialized = True
logger.info("OrderMonitorTask 通用订单监控服务单例实例已创建")
logger.info("OrderMonitorTask singleton instance created")
@classmethod
def get_instance(cls) -> 'OrderMonitorTask':
"""获取单例实例"""
"""Get singleton instance"""
if cls._instance is None:
cls._instance = cls()
return cls._instance
def _load_payment_channels(self) -> List[PaymentChannelConfig]:
"""加载支付渠道配置"""
"""Load payment channel configuration"""
db = SessionLocal()
try:
payment_channel_service = PaymentChannelService(db)
channels = []
# 加载支付宝配置
# Load Alipay config
alipay_config = payment_channel_service.get_config("alipay")
if alipay_config:
channels.append(PaymentChannelConfig(
@@ -209,7 +208,7 @@ class OrderMonitorTask:
config=alipay_config
))
# 加载微信支付配置
# Load WeChat Pay config
wechat_config = payment_channel_service.get_config("wechat")
if wechat_config:
channels.append(PaymentChannelConfig(
@@ -223,24 +222,24 @@ class OrderMonitorTask:
db.close()
def _initialize_payment_clients(self, channels: List[PaymentChannelConfig]):
"""初始化支付客户端"""
# 保存当前启用的客户端状态
"""Initialize payment clients"""
# Save currently enabled client status
current_enabled = {k: v.enabled for k, v in self.client_status.items()}
for channel in channels:
try:
# 检查客户端是否应该启用
# Check whether client should be enabled
should_enable = channel.enabled and current_enabled.get(channel.channel_id, True)
if not should_enable:
# 如果客户端被禁用,从payment_clients中移除但保留状态
# If the client is disabled, remove from payment_clients but keep status
if channel.channel_id in self.payment_clients:
del self.payment_clients[channel.channel_id]
self.client_status[channel.channel_id] = ClientStatus(
enabled=False,
last_check_time=datetime.now()
)
logger.info(f"{channel.channel_id} 支付客户端已禁用")
logger.info(f"Payment client {channel.channel_id} is disabled")
continue
if channel.channel_id == "alipay":
@@ -254,7 +253,7 @@ class OrderMonitorTask:
enabled=True,
last_check_time=datetime.now()
)
logger.info("支付宝支付客户端初始化成功")
logger.info("Alipay payment client initialized successfully")
elif channel.channel_id == "wechat":
client = WxPayPaymentClient(
@@ -270,11 +269,11 @@ class OrderMonitorTask:
enabled=True,
last_check_time=datetime.now()
)
logger.info("微信支付客户端初始化成功")
logger.info("WeChat Pay client initialized successfully")
except Exception as e:
logger.error(f"初始化 {channel.channel_id} 支付客户端失败: {e}")
# 记录错误状态
logger.error(f"Failed to initialize {channel.channel_id} payment client: {e}")
# Record error status
if channel.channel_id in self.client_status:
self.client_status[channel.channel_id].error_count += 1
self.client_status[channel.channel_id].last_error = str(e)
@@ -288,21 +287,21 @@ class OrderMonitorTask:
def add_order_to_monitor(self, order_info: Optional[OrderInfo] = None, payment_id: Optional[str] = None) -> bool:
"""
添加订单到监控队列
Add order to monitor queue
Args:
order_info: 订单信息(可选)
payment_id: 支付订单ID(可选)
order_info: Order information (optional)
payment_id: Payment order ID (optional)
Returns:
bool: 是否添加成功
bool: Whether added successfully
"""
if payment_id is not None:
# 创建新的数据库会话来查询订单
# Create new DB session to query order
db = SessionLocal()
try:
user_wallet_history_service = UserWalletHistoryService(db)
order = user_wallet_history_service.get_order_by_id(payment_id)
if order is None:
logger.error(f"订单 {payment_id} 不存在,无法添加到监控队列")
logger.error(f"Order {payment_id} does not exist; cannot add to monitor queue")
return False
order_info = OrderInfo(
created_time=order.created_at,
@@ -315,78 +314,78 @@ class OrderMonitorTask:
db.close()
if order_info is None:
logger.error("订单信息为空,无法添加到监控队列")
logger.error("Order info is empty; cannot add to monitor queue")
return False
# 检查支付渠道是否支持且启用
# Check whether payment channel is supported and enabled
if order_info.payment_channel_id is None:
logger.warning("支付渠道ID为空,无法添加到监控队列")
logger.warning("Payment channel ID is empty; cannot add to monitor queue")
return False
if (order_info.payment_channel_id not in self.client_status or
not self.client_status[order_info.payment_channel_id].enabled):
logger.warning(f"支付渠道 {order_info.payment_channel_id} 未启用,无法添加到监控队列")
logger.warning(f"Payment channel {order_info.payment_channel_id} not enabled; cannot add to monitor queue")
return False
try:
self.order_queue.put_nowait(order_info)
logger.info(f"订单 {order_info.payment_id} ({order_info.payment_channel_id}) 已添加到监控队列")
logger.info(f"Order {order_info.payment_id} ({order_info.payment_channel_id}) added to monitor queue")
return True
except Exception as e:
logger.error(f"添加订单到监控队列失败: {e}")
logger.error(f"Failed to add order to monitor queue: {e}")
return False
def start_monitor(self) -> bool:
"""
启动订单状态监控
Start order status monitoring
Returns:
bool: 是否启动成功
bool: Whether started successfully
"""
if self.is_running:
logger.warning("订单监控服务已在运行中")
logger.warning("Order monitoring service is already running")
return False
try:
# 加载支付渠道配置
# Load payment channel configuration
channels = self._load_payment_channels()
if not channels:
logger.error("未找到可用的支付渠道配置")
logger.error("No available payment channel configuration found")
return False
# 初始化支付客户端
# Initialize payment clients
self._initialize_payment_clients(channels)
# 加载待监控的订单
# Load pending orders
self._load_pending_orders()
# 启动监控线程
# Start monitoring thread
self.is_running = True
self.monitor_thread = threading.Thread(target=self._monitor_loop, daemon=True)
self.monitor_thread.start()
# 启动配置检查线程
# Start configuration check thread
self.config_check_thread = threading.Thread(target=self._config_check_loop, daemon=True)
self.config_check_thread.start()
enabled_clients = [k for k, v in self.client_status.items() if v.enabled]
logger.info(f"通用订单监控服务已启动,启用的支付渠道: {enabled_clients}")
logger.info(f"Order monitoring service started; enabled payment channels: {enabled_clients}")
return True
except Exception as e:
logger.error(f"启动订单监控服务失败: {e}")
logger.error(f"Failed to start order monitoring service: {e}")
self.is_running = False
return False
def _load_pending_orders(self):
"""加载待监控的订单"""
"""Load pending orders to monitor"""
db = SessionLocal()
try:
user_wallet_history_service = UserWalletHistoryService(db)
# 获取5分钟前的时间
# Get timestamp for 5 minutes ago
start_time = datetime.now() - timedelta(minutes=5)
# 加载各个支付渠道的待处理订单
# Load pending orders for each payment channel
for channel_id in self.payment_clients.keys():
order_list = user_wallet_history_service.order_list(channel_id, 0, start_time)
for order in order_list:
@@ -399,100 +398,100 @@ class OrderMonitorTask:
)
self.add_order_to_monitor(order_info=order_info)
logger.info(f"已加载待监控订单,当前队列大小: {self.order_queue.qsize()}")
logger.info(f"Loaded pending orders; current queue size: {self.order_queue.qsize()}")
finally:
db.close()
def stop_monitor(self) -> bool:
"""
停止订单状态监控
Stop order status monitoring
Returns:
bool: 是否停止成功
bool: Whether stopped successfully
"""
if not self.is_running:
logger.warning("订单监控服务未在运行")
logger.warning("Order monitoring service is not running")
return False
try:
self.is_running = False
# 停止监控线程
# Stop monitoring thread
if self.monitor_thread and self.monitor_thread.is_alive():
self.monitor_thread.join(timeout=10)
# 停止配置检查线程
# Stop configuration check thread
if self.config_check_thread and self.config_check_thread.is_alive():
self.config_check_thread.join(timeout=5)
logger.info("通用订单监控服务已停止")
logger.info("Order monitoring service stopped")
return True
except Exception as e:
logger.error(f"停止订单监控服务失败: {e}")
logger.error(f"Failed to stop order monitoring service: {e}")
return False
def _monitor_loop(self):
"""监控循环主逻辑"""
logger.info("订单监控循环开始")
"""Main monitoring loop logic"""
logger.info("Order monitoring loop started")
while self.is_running:
try:
# 获取当前队列中的所有订单
# Get all orders in the current queue
current_orders = self._get_all_orders_from_queue()
if not current_orders:
time.sleep(self.check_interval)
continue
logger.info(f"开始检查 {len(current_orders)} 个订单状态")
logger.info(f"Starting to check status of {len(current_orders)} orders")
# 按支付渠道分组检查订单
# Group orders by payment channel for checking
orders_by_channel = {}
for order in current_orders:
channel_id = order.payment_channel_id
if channel_id is None:
logger.warning(f"订单 {order.payment_id} 的支付渠道ID为空,跳过监控")
logger.warning(f"Order {order.payment_id} payment channel ID is empty; skipping monitoring")
continue
if channel_id not in orders_by_channel:
orders_by_channel[channel_id] = []
orders_by_channel[channel_id].append(order)
# 检查每个渠道的订单
# Check orders for each channel
remaining_orders = []
for channel_id, orders in orders_by_channel.items():
# 检查客户端是否启用
# Check whether client is enabled
if channel_id not in self.client_status or not self.client_status[channel_id].enabled:
logger.warning(f"支付渠道 {channel_id} 未启用,跳过 {len(orders)} 个订单的检查")
# 将订单重新放回队列等待客户端启用
logger.warning(f"Payment channel {channel_id} not enabled; skipping checks for {len(orders)} orders")
# Put orders back into queue while waiting for client to be enabled
remaining_orders.extend(orders)
continue
logger.info(f"检查 {channel_id} 渠道的 {len(orders)} 个订单")
logger.info(f"Checking {len(orders)} orders for channel {channel_id}")
for order in orders:
try:
should_keep = self._check_single_order(order)
if should_keep:
remaining_orders.append(order)
except Exception as e:
logger.error(f"检查订单 {order.payment_id} 状态失败: {e}")
# 出错的订单重新放回队列
logger.error(f"Check status for order {order.payment_id}: {e}")
# Put failed-checked orders back into the queue
remaining_orders.append(order)
# 将剩余订单重新放回队列
# Put remaining orders back into the queue
for order in remaining_orders:
self.order_queue.put_nowait(order)
logger.info(f"本轮检查完成,剩余 {len(remaining_orders)} 个订单继续监控")
logger.info(f"Round complete; {len(remaining_orders)} orders remain for monitoring")
except Exception as e:
logger.error(f"监控循环出现异常: {e}")
logger.error(f"Monitoring loop encountered an exception: {e}")
# 等待下次检查
# Wait for next check
time.sleep(self.check_interval)
logger.info("订单监控循环结束")
logger.info("Order monitoring loop ended")
def _get_all_orders_from_queue(self) -> List[OrderInfo]:
"""从队列中获取所有订单"""
"""Get all orders from queue"""
orders = []
while True:
try:
@@ -504,42 +503,42 @@ class OrderMonitorTask:
def _check_single_order(self, order: OrderInfo) -> bool:
"""
检查单个订单状态
Check single order status
Args:
order: 订单信息
order: Order information
Returns:
bool: 是否需要继续监控(True=继续,False=移除)
bool: Whether to continue monitoring (True=continue, False=remove)
"""
# 检查是否超时
# Check timeout
if self._is_order_timeout(order):
logger.info(f"订单 {order.payment_id} 已超时,从监控队列移除")
logger.info(f"Order {order.payment_id} timed out; removing from monitor queue")
self._handle_payment_failed(order)
return False
# 获取对应的支付客户端
# Get the corresponding payment client
if order.payment_channel_id is None:
logger.error("支付渠道ID为空,无法查询状态")
logger.error("Payment channel ID is empty; cannot query status")
return False
client = self.payment_clients.get(order.payment_channel_id)
if client is None:
logger.error(f"未找到 {order.payment_channel_id} 支付客户端")
logger.error(f"Payment client for {order.payment_channel_id} not found")
return False
if order.payment_id is None:
logger.error("订单ID为空,无法查询状态")
logger.error("Order ID is empty; cannot query status")
return False
try:
# 查询订单状态
# Query order status
response = client.query_order_status(order.payment_id)
if response is None:
logger.warning(f"订单 {order.payment_id} 查询响应为空,继续监控")
logger.warning(f"Order {order.payment_id} query response is empty; continue monitoring")
return True
# 解析订单状态
# Parse order status
status, should_remove = client.parse_order_status(response)
logger.info(f"订单 {order.payment_id} ({order.payment_channel_id}) 状态: {status}")
logger.info(f"Order {order.payment_id} ({order.payment_channel_id}) status: {status}")
if should_remove:
if status == "SUCCESS":
@@ -548,26 +547,26 @@ class OrderMonitorTask:
self._handle_payment_failed(order)
return False
else:
# 订单仍在等待支付,继续监控
# Order is still awaiting payment; continue monitoring
return True
except Exception as e:
logger.error(f"查询订单 {order.payment_id} 状态失败: {e}")
# 查询失败,继续监控
logger.error(f"Failed to query status for order {order.payment_id}: {e}")
# Query failed; continue monitoring
return True
def _is_order_timeout(self, order: OrderInfo) -> bool:
"""检查订单是否超时"""
"""Check if order timed out"""
timeout_time = order.created_time + timedelta(minutes=self.timeout_minutes)
return datetime.utcnow() > timeout_time
def _handle_payment_success(self, order: OrderInfo):
"""处理支付成功"""
"""Handle payment success"""
try:
logger.info(f"订单 {order.payment_id} ({order.payment_channel_id}) 支付成功")
logger.info(f"Order {order.payment_id} ({order.payment_channel_id}) payment succeeded")
if order.payment_id is not None and order.payment_channel_id is not None:
# 创建新的数据库会话来处理支付回调
# Create new DB session to handle payment callback
db = SessionLocal()
try:
payment_service = PaymentService(db)
@@ -576,23 +575,23 @@ class OrderMonitorTask:
payment_channel_id=order.payment_channel_id,
)
if result:
logger.info(f"订单 {order.payment_id} 支付成功处理完成")
logger.info(f"Order {order.payment_id} payment success handling completed")
else:
logger.error(f"订单 {order.payment_id} 支付成功处理失败")
logger.error(f"Order {order.payment_id} payment success handling failed")
finally:
db.close()
else:
logger.error("订单ID或支付渠道ID为空,无法处理支付成功")
logger.error("Order ID or payment channel ID is empty; cannot handle payment success")
except Exception as e:
logger.error(f"处理订单 {order.payment_id} 支付成功时发生错误: {e}")
logger.error(f"Error while handling payment success for order {order.payment_id}: {e}")
def _handle_payment_failed(self, order: OrderInfo):
"""处理支付失败"""
"""Handle payment failure"""
try:
logger.info(f"订单 {order.payment_id} ({order.payment_channel_id}) 支付失败")
logger.info(f"Order {order.payment_id} ({order.payment_channel_id}) payment failed")
if order.payment_id is not None and order.payment_channel_id is not None:
# 创建新的数据库会话来处理支付回调
# Create new DB session to handle payment callback
db = SessionLocal()
try:
payment_service = PaymentService(db)
@@ -602,18 +601,18 @@ class OrderMonitorTask:
status=2
)
if result:
logger.info(f"订单 {order.payment_id} 支付失败处理完成")
logger.info(f"Order {order.payment_id} payment failure handling completed")
else:
logger.error(f"订单 {order.payment_id} 支付失败处理失败")
logger.error(f"Order {order.payment_id} payment failure handling failed")
finally:
db.close()
else:
logger.error("订单ID或支付渠道ID为空,无法处理支付失败")
logger.error("Order ID or payment channel ID is empty; cannot handle payment failure")
except Exception as e:
logger.error(f"处理订单 {order.payment_id} 支付失败时发生错误: {e}")
logger.error(f"Error while handling payment failure for order {order.payment_id}: {e}")
def get_monitor_status(self) -> Dict[str, Any]:
"""获取监控服务状态"""
"""Get monitoring service status"""
client_status_info = {}
for channel_id, status in self.client_status.items():
client_status_info[channel_id] = {
@@ -636,22 +635,22 @@ class OrderMonitorTask:
def enable_client(self, channel_id: str) -> bool:
"""
启用指定的支付客户端
Enable specified payment client
Args:
channel_id: 支付渠道ID
channel_id: Payment channel ID
Returns:
bool: 是否启用成功
bool: Whether enabled successfully
"""
if channel_id not in self.client_status:
logger.error(f"未知的支付渠道: {channel_id}")
logger.error(f"Unknown payment channel: {channel_id}")
return False
if self.client_status[channel_id].enabled:
logger.warning(f"支付客户端 {channel_id} 已经启用")
logger.warning(f"Payment client {channel_id} is already enabled")
return True
try:
# 重新加载配置并初始化客户端
# Reload configuration and initialize client
channels = self._load_payment_channels()
target_channel = None
for channel in channels:
@@ -660,82 +659,82 @@ class OrderMonitorTask:
break
if target_channel is None:
logger.error(f"未找到 {channel_id} 的配置")
logger.error(f"Configuration for {channel_id} not found")
return False
# 启用客户端状态
# Enable client status
self.client_status[channel_id].enabled = True
# 重新初始化该客户端
# Re-initialize the client
self._initialize_payment_clients([target_channel])
logger.info(f"支付客户端 {channel_id} 已启用")
logger.info(f"Payment client {channel_id} enabled")
return True
except Exception as e:
logger.error(f"启用支付客户端 {channel_id} 失败: {e}")
logger.error(f"Failed to enable payment client {channel_id}: {e}")
self.client_status[channel_id].enabled = False
return False
def disable_client(self, channel_id: str) -> bool:
"""
禁用指定的支付客户端
Disable specified payment client
Args:
channel_id: 支付渠道ID
channel_id: Payment channel ID
Returns:
bool: 是否禁用成功
bool: Whether disabled successfully
"""
if channel_id not in self.client_status:
logger.error(f"未知的支付渠道: {channel_id}")
logger.error(f"Unknown payment channel: {channel_id}")
return False
if not self.client_status[channel_id].enabled:
logger.warning(f"支付客户端 {channel_id} 已经禁用")
logger.warning(f"Payment client {channel_id} is already disabled")
return True
try:
# 禁用客户端状态
# Disable client status
self.client_status[channel_id].enabled = False
# 从payment_clients中移除
# Remove from payment_clients
if channel_id in self.payment_clients:
del self.payment_clients[channel_id]
logger.info(f"支付客户端 {channel_id} 已禁用")
logger.info(f"Payment client {channel_id} disabled")
return True
except Exception as e:
logger.error(f"禁用支付客户端 {channel_id} 失败: {e}")
logger.error(f"Failed to disable payment client {channel_id}: {e}")
return False
def _config_check_loop(self):
"""配置检查循环 - 定时检查配置变化并重新加载客户端"""
logger.info("配置检查循环开始")
"""Configuration check loop - periodically checks for config changes and reloads clients"""
logger.info("Configuration check loop started")
while self.is_running:
try:
# 等待检查间隔
# Wait for check interval
time.sleep(self.config_check_interval)
if not self.is_running:
break
logger.debug("开始检查支付渠道配置变化")
logger.debug("Starting to check payment channel configuration changes")
# 重新加载配置
# Reload configuration
channels = self._load_payment_channels()
if not channels:
logger.warning("未找到任何支付渠道配置")
logger.warning("No payment channel configuration found")
continue
# 检查配置是否有变化
# Check whether configuration changed
config_changed = False
for channel in channels:
if channel.channel_id not in self.client_status:
config_changed = True
break
# 检查启用状态是否变化
# Check whether enabled state changed
current_enabled = self.client_status[channel.channel_id].enabled
if channel.enabled != current_enabled:
config_changed = True
@@ -743,22 +742,22 @@ class OrderMonitorTask:
if config_changed:
logger.info(self.client_status)
logger.info("检测到支付渠道配置变化,重新初始化客户端")
logger.info("Detected payment channel configuration changes; reinitializing clients")
self._initialize_payment_clients(channels)
# 更新检查时间
# Update check time
for channel_id in self.client_status:
self.client_status[channel_id].last_check_time = datetime.now()
else:
logger.debug("支付渠道配置无变化")
logger.debug("No changes in payment channel configuration")
except Exception as e:
logger.error(f"配置检查循环出现异常: {e}")
logger.error(f"Configuration check loop encountered an exception: {e}")
logger.info("配置检查循环结束")
logger.info("Configuration check loop ended")
def get_client_list(self) -> List[Dict[str, Any]]:
"""获取所有客户端列表及状态"""
"""Get list of all clients and their status"""
clients = []
for channel_id, status in self.client_status.items():
clients.append({
@@ -2,10 +2,7 @@ import os
import ssl
import urllib3
# 禁用SSL证书验证警告
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)
# 禁用SSL证书验证
# Disable SSL certificate verification warnings
try:
_create_unverified_https_context = ssl._create_unverified_context
except AttributeError:
+13 -13
View File
@@ -20,7 +20,7 @@ class WxPayClient:
def __init__(self, app_id: str, apiv3_key: str, mch_id: str, private_key: str, cert_serial_no: str, notify_url: str):
"""Initialize WeChat Pay client with required parameters."""
# 验证输入参数
# Validate input parameters
if not all([app_id, apiv3_key, mch_id, private_key, cert_serial_no, notify_url]):
missing_params = []
if not app_id: missing_params.append("app_id")
@@ -34,7 +34,7 @@ class WxPayClient:
logger.error(error_msg)
raise ValueError(error_msg)
# 存储配置参数
# Store configuration parameters
self.app_id = app_id
self.apiv3_key = apiv3_key
self.mch_id = mch_id
@@ -42,21 +42,21 @@ class WxPayClient:
self.cert_serial_no = cert_serial_no
self.notify_url = notify_url
# 修正证书目录路径计算 - 从 services/admin_service/utils/ 向上4级到项目根目录
# Fix certificate directory path: go up 4 levels from services/admin_service/utils to project root
current_dir = os.path.dirname(__file__) # utils/
admin_service_dir = os.path.dirname(current_dir) # admin_service/
services_dir = os.path.dirname(admin_service_dir) # services/
project_root = os.path.dirname(services_dir) # 项目根目录
project_root = os.path.dirname(services_dir) # Project root directory
self.cert_dir = os.path.join(project_root, 'wx_cer')
# 检查证书目录是否存在
# Check if certificate directory exists
if not os.path.exists(self.cert_dir):
logger.error(f"Certificate directory not found: {self.cert_dir}")
logger.error(f"Please ensure wx_cer directory exists in project root")
raise FileNotFoundError(f"Certificate directory not found: {self.cert_dir}")
else:
logger.info(f"Certificate directory found: {self.cert_dir}")
# 列出证书目录中的文件
# List files in certificate directory
cert_files = os.listdir(self.cert_dir)
logger.info(f"Certificate files: {cert_files}")
@@ -68,13 +68,13 @@ class WxPayClient:
logger.info(f"Creating WeChat Pay order: {order_id}, amount: {amount}, description: {description}")
logger.info(f"Using certificate directory: {self.cert_dir}")
# 验证金额格式 (微信支付要求金额为分,整数)
# Validate amount format (WeChat requires amount in cents, integer)
amount_fen = int(amount * 100)
if amount_fen <= 0:
logger.error(f"Invalid amount: {amount} (must be positive)")
return None
# 使用异步上下文管理器
# Use asynchronous context manager
async with AsyncWeChatPay(
wechatpay_type=WeChatPayType.NATIVE,
mchid=self.mch_id,
@@ -83,17 +83,17 @@ class WxPayClient:
apiv3_key=self.apiv3_key,
appid=self.app_id,
notify_url=self.notify_url,
cert_dir=self.cert_dir, # 使用正确的证书目录路径
cert_dir=self.cert_dir, # Use correct certificate directory path
partner_mode=False
) as wxpay:
# 调用微信支付API
# Call WeChat Pay API
code, message = await wxpay.pay(
out_trade_no=order_id,
amount={
"total": amount_fen,
"currency": "CNY"
}, # 微信支付金额单位为分
}, # WeChat Pay amount unit is cents
# amount= amount_fen,
description=description,
)
@@ -126,7 +126,7 @@ class WxPayClient:
try:
logger.info(f"Querying WeChat Pay order: {order_id}")
# 使用异步上下文管理器
# Use asynchronous context manager
async with AsyncWeChatPay(
wechatpay_type=WeChatPayType.NATIVE,
mchid=self.mch_id,
@@ -135,7 +135,7 @@ class WxPayClient:
apiv3_key=self.apiv3_key,
appid=self.app_id,
notify_url=self.notify_url,
cert_dir=self.cert_dir, # 使用正确的证书目录路径
cert_dir=self.cert_dir, # Use correct certificate directory path
partner_mode=False
) as wxpay:
@@ -1,3 +1,4 @@
"""Repository for MCP services in API service: cached reads by ID/slug."""
from sqlalchemy.orm import Session
from services.common.models.mcp_service import McpService
from services.common.utils.cache_utils import CacheUtils
@@ -8,21 +9,21 @@ logger = logging.getLogger(__name__)
class McpServiceRepository:
"""MCP service repository layer for API service"""
"""Repository for MCP services in API service: cached reads by ID/slug."""
def __init__(self, db: Session):
self.db = db
def get_by_id(self, service_id: str) -> Optional[McpService]:
def get_by_id(self, service_id: str, force_update: bool = False) -> Optional[McpService]:
"""
Get single MCP service by service ID
"""
cache_key = f"mcp_service:id:{service_id}"
# Try to get from cache using SQLAlchemy-specific method
cached_model = CacheUtils.get_sqlalchemy_cache(cache_key, McpService)
if cached_model:
return cached_model
cache_key = f"xpack:mcp_service:id:{service_id}"
if not force_update:
# Try to get from cache using SQLAlchemy-specific method
cached_model = CacheUtils.get_sqlalchemy_cache(cache_key, McpService)
if cached_model:
return cached_model
# Query from database if not in cache
service = self.db.query(McpService).filter(McpService.id == service_id, McpService.enabled == 1).first()
@@ -37,7 +38,7 @@ class McpServiceRepository:
"""
Get single MCP service by slug name
"""
cache_key = f"mcp_service:slug:{slug_name}"
cache_key = f"xpack:mcp_service:slug:{slug_name}"
# Try to get from cache using SQLAlchemy-specific method
cached_model = CacheUtils.get_sqlalchemy_cache(cache_key, McpService)
@@ -1,3 +1,4 @@
"""Repository for MCP tool APIs in API service: list and get operations."""
from sqlalchemy.orm import Session
from services.common.models.mcp_tool_api import McpToolApi
from typing import Optional, List
@@ -66,7 +66,7 @@ class BillingService:
charge_type=charge_type.value
)
case ChargeType.PER_TOKEN:
# 计算预估费用(按每百万Token计费)
# Calculate estimated cost (charged per million tokens)
estimated_input_cost = (Decimal(100) / Decimal("1000000")) * input_token_price
estimated_output_cost = (Decimal(500) / Decimal("1000000")) * output_token_price
service_price = estimated_input_cost + estimated_output_cost
@@ -179,7 +179,7 @@ class BillingService:
Tuple[Decimal, ChargeType]: Price and charge type
"""
# Try to get from Redis cache
cache_key = f"service:price:{service_id}"
cache_key = f"xpack:service:price:{service_id}"
cached_data = self.redis.get(cache_key)
if cached_data:
try:
@@ -192,7 +192,7 @@ class BillingService:
db = next(get_db())
try:
service_repo = McpServiceRepository(db)
service = service_repo.get_by_id(service_id)
service = service_repo.get_by_id(service_id, force_update=True)
if not service:
raise ValueError(f"Service not found: {service_id}")
@@ -235,7 +235,7 @@ class BillingService:
Decimal: User balance
"""
# Try to get from Redis cache
cache_key = f"wallet:balance:{user_id}"
cache_key = f"xpack:wallet:balance:{user_id}"
cached_balance = self.redis.get(cache_key)
if cached_balance:
@@ -273,7 +273,7 @@ class BillingService:
user_id: User ID
new_balance: New balance
"""
cache_key = f"wallet:balance:{user_id}"
cache_key = f"xpack:wallet:balance:{user_id}"
self.redis.set(cache_key, str(new_balance), ex=self.WALLET_CACHE_EXPIRE)
@asynccontextmanager
@@ -112,7 +112,6 @@ class McpServerFactory:
List[types.Content]: Execution result
"""
call_start_time = datetime.now(timezone.utc)
call_log_id = str(uuid.uuid4())
logger.info(f"Received tool call request with billing - User ID: {user_id}, Service ID: {service_id}, Tool name: {name}")
logger.debug(f"Tool arguments: {arguments}")
@@ -124,10 +123,29 @@ class McpServerFactory:
error_msg = f"Billing check failed: {pre_deduct_result.message}"
# Send failed billing message
# Lookup tool API id for logging
api_id_for_log = None
db_lookup = next(get_db())
try:
mcp_service = self._create_mcp_service(db_lookup)
tool_cfg = mcp_service.get_tool_by_name(service_id, name)
if tool_cfg:
api_id_for_log = tool_cfg.id
else:
logger.error(f"Unknown tool for billing log: {name}")
except Exception as lookup_err:
logger.error(f"Failed to lookup tool API id for billing log: {lookup_err}")
finally:
db_lookup.close()
# If tool API id not found, skip sending billing message to avoid FK errors
if not api_id_for_log:
return [types.TextContent(type="text", text=error_msg)]
call_log = ApiCallLogInfo(
user_id=user_id,
service_id=service_id,
api_id=call_log_id,
api_id=api_id_for_log,
tool_name=name,
input_params=json.dumps(arguments),
unit_price=pre_deduct_result.service_price,
@@ -163,18 +181,18 @@ class McpServerFactory:
# Calculate input token amount (prices are per million tokens)
input_token_amount = (Decimal(str(estimated_input_tokens)) / Decimal("1000000")) * pre_deduct_result.input_token_price
amount = input_token_amount
# Create service instance and find tool configuration before try/except
mcp_service = self._create_mcp_service(db)
tool_config = mcp_service.get_tool_by_name(service_id, name)
if not tool_config:
error_msg = f"Unknown tool: {name}"
logger.error(error_msg)
# Close DB and return error without sending billing message to avoid FK violation
db.close()
return [types.TextContent(type="text", text=error_msg)]
try:
# Create service instance
mcp_service = self._create_mcp_service(db)
# Find tool configuration
tool_config = mcp_service.get_tool_by_name(service_id, name)
if not tool_config:
error_msg = f"Unknown tool: {name}"
logger.error(error_msg)
raise ValueError(error_msg)
logger.info(f"Found tool configuration: {tool_config.name}")
# Get service authentication info
@@ -210,22 +228,24 @@ class McpServerFactory:
# 3. Send billing message
call_end_time = datetime.now(timezone.utc)
call_log = ApiCallLogInfo(
user_id=user_id,
service_id=service_id,
api_id=call_log_id,
tool_name=name,
input_params=json.dumps(arguments),
unit_price=amount.quantize(Decimal('0.000001')),
input_token=Decimal(str(input_token)).quantize(Decimal('0')),
output_token=Decimal(str(output_token)).quantize(Decimal('0')),
charge_type=pre_deduct_result.charge_type,
call_start_time=call_start_time,
call_end_time=call_end_time,
apikey_id=apikey_id,
)
# Send billing message only when tool_config was found (api_id exists)
if tool_config:
call_log = ApiCallLogInfo(
user_id=user_id,
service_id=service_id,
api_id=tool_config.id,
tool_name=name,
input_params=json.dumps(arguments),
unit_price=amount.quantize(Decimal('0.000001')),
input_token=Decimal(str(input_token)).quantize(Decimal('0')),
output_token=Decimal(str(output_token)).quantize(Decimal('0')),
charge_type=pre_deduct_result.charge_type,
call_start_time=call_start_time,
call_end_time=call_end_time,
apikey_id=apikey_id,
)
await self.billing_service.send_billing_message(call_log, call_success, call_end_time)
await self.billing_service.send_billing_message(call_log, call_success, call_end_time)
logger.info(f"Billing message sent - User ID: {user_id}, Tool: {name}, Success: {call_success}")
# Ensure return type is correct
+6 -5
View File
@@ -2,6 +2,8 @@ import json
import ast
import mcp.types as types
from typing import List, Optional
from sqlalchemy import false
from services.common.models.mcp_tool_api import McpToolApi
from services.common.models.mcp_service import McpService as McpServiceModel
from services.api_service.repositories.mcp_tool_api_repository import McpToolApiRepository
@@ -246,7 +248,7 @@ class McpService:
return tool_api
return None
def get_service_by_id(self, service_id: str) -> Optional[McpServiceModel]:
def get_service_by_id(self, service_id: str, force_update: bool = False) -> Optional[McpServiceModel]:
"""
Get service information by service ID
@@ -256,9 +258,9 @@ class McpService:
Returns:
Optional[McpServiceModel]: Service information, returns None if not found
"""
return self.service_repository.get_by_id(service_id)
return self.service_repository.get_by_id(service_id, force_update)
def get_service_call_params(self, service_id: str) -> dict:
service = self.service_repository.get_by_id(service_id)
service = self.service_repository.get_by_id(service_id, force_update=False)
if not service:
return {}
headers = {}
@@ -269,5 +271,4 @@ class McpService:
return {
"base_url": service.base_url or "",
"headers": headers
}
}
+21
View File
@@ -67,3 +67,24 @@ class McpService(Base):
server_onupdate=func.current_timestamp(),
comment="Last update timestamp",
)
class DropMCPService(Base):
__tablename__ = "drop_mcp_service"
id: Mapped[str] = mapped_column(
String(36),
primary_key=True,
autoincrement=False,
comment="Primary key, service UUID",
)
name: Mapped[str] = mapped_column(String(255), nullable=False, comment="Service name")
slug_name: Mapped[str] = mapped_column(String(255), unique=True, nullable=False, comment="Unique service identifier (slug format)")
short_description: Mapped[str] = mapped_column(String, nullable=False, comment="Brief service description")
long_description: Mapped[str] = mapped_column(String, nullable=True, comment="Detailed service description (Markdown)")
updated_at: Mapped[datetime] = mapped_column(
DateTime,
nullable=True,
server_default=func.current_timestamp(),
server_onupdate=func.current_timestamp(),
comment="Last update timestamp",
)