feat: 支持后台配置webhook

This commit is contained in:
shaw
2025-08-23 20:20:32 +08:00
parent 74bcb99142
commit b426a759a8
14 changed files with 2319 additions and 377 deletions

View File

@@ -14,6 +14,7 @@ const oauthHelper = require('../utils/oauthHelper')
const CostCalculator = require('../utils/costCalculator')
const pricingService = require('../services/pricingService')
const claudeCodeHeadersService = require('../services/claudeCodeHeadersService')
const webhookNotifier = require('../utils/webhookNotifier')
const axios = require('axios')
const crypto = require('crypto')
const fs = require('fs')
@@ -1736,6 +1737,19 @@ router.put(
const newSchedulable = !account.schedulable
await claudeAccountService.updateAccount(accountId, { schedulable: newSchedulable })
// 如果账号被禁用发送webhook通知
if (!newSchedulable) {
await webhookNotifier.sendAccountAnomalyNotification({
accountId: account.id,
accountName: account.name || account.claudeAiOauth?.email || 'Claude Account',
platform: 'claude-oauth',
status: 'disabled',
errorCode: 'CLAUDE_OAUTH_MANUALLY_DISABLED',
reason: '账号已被管理员手动禁用调度',
timestamp: new Date().toISOString()
})
}
logger.success(
`🔄 Admin toggled Claude account schedulable status: ${accountId} -> ${newSchedulable ? 'schedulable' : 'not schedulable'}`
)
@@ -2006,6 +2020,19 @@ router.put(
const newSchedulable = !account.schedulable
await claudeConsoleAccountService.updateAccount(accountId, { schedulable: newSchedulable })
// 如果账号被禁用发送webhook通知
if (!newSchedulable) {
await webhookNotifier.sendAccountAnomalyNotification({
accountId: account.id,
accountName: account.name || 'Claude Console Account',
platform: 'claude-console',
status: 'disabled',
errorCode: 'CLAUDE_CONSOLE_MANUALLY_DISABLED',
reason: '账号已被管理员手动禁用调度',
timestamp: new Date().toISOString()
})
}
logger.success(
`🔄 Admin toggled Claude Console account schedulable status: ${accountId} -> ${newSchedulable ? 'schedulable' : 'not schedulable'}`
)
@@ -2280,6 +2307,19 @@ router.put(
.json({ error: 'Failed to toggle schedulable status', message: updateResult.error })
}
// 如果账号被禁用发送webhook通知
if (!newSchedulable) {
await webhookNotifier.sendAccountAnomalyNotification({
accountId: accountResult.data.id,
accountName: accountResult.data.name || 'Bedrock Account',
platform: 'bedrock',
status: 'disabled',
errorCode: 'BEDROCK_MANUALLY_DISABLED',
reason: '账号已被管理员手动禁用调度',
timestamp: new Date().toISOString()
})
}
logger.success(
`🔄 Admin toggled Bedrock account schedulable status: ${accountId} -> ${newSchedulable ? 'schedulable' : 'not schedulable'}`
)
@@ -2651,6 +2691,19 @@ router.put(
const updatedAccount = await geminiAccountService.getAccount(accountId)
const actualSchedulable = updatedAccount ? updatedAccount.schedulable : newSchedulable
// 如果账号被禁用发送webhook通知
if (!actualSchedulable) {
await webhookNotifier.sendAccountAnomalyNotification({
accountId: account.id,
accountName: account.accountName || 'Gemini Account',
platform: 'gemini',
status: 'disabled',
errorCode: 'GEMINI_MANUALLY_DISABLED',
reason: '账号已被管理员手动禁用调度',
timestamp: new Date().toISOString()
})
}
logger.success(
`🔄 Admin toggled Gemini account schedulable status: ${accountId} -> ${actualSchedulable ? 'schedulable' : 'not schedulable'}`
)
@@ -5212,6 +5265,23 @@ router.put(
const result = await openaiAccountService.toggleSchedulable(accountId)
// 如果账号被禁用发送webhook通知
if (!result.schedulable) {
// 获取账号信息
const account = await redis.getOpenAiAccount(accountId)
if (account) {
await webhookNotifier.sendAccountAnomalyNotification({
accountId: account.id,
accountName: account.name || 'OpenAI Account',
platform: 'openai',
status: 'disabled',
errorCode: 'OPENAI_MANUALLY_DISABLED',
reason: '账号已被管理员手动禁用调度',
timestamp: new Date().toISOString()
})
}
}
return res.json({
success: result.success,
schedulable: result.schedulable,
@@ -5441,6 +5511,23 @@ router.put(
const result = await azureOpenaiAccountService.toggleSchedulable(accountId)
// 如果账号被禁用发送webhook通知
if (!result.schedulable) {
// 获取账号信息
const account = await azureOpenaiAccountService.getAccount(accountId)
if (account) {
await webhookNotifier.sendAccountAnomalyNotification({
accountId: account.id,
accountName: account.name || 'Azure OpenAI Account',
platform: 'azure-openai',
status: 'disabled',
errorCode: 'AZURE_OPENAI_MANUALLY_DISABLED',
reason: '账号已被管理员手动禁用调度',
timestamp: new Date().toISOString()
})
}
}
return res.json({
success: true,
schedulable: result.schedulable,

View File

@@ -671,4 +671,103 @@ router.get('/v1/organizations/:org_id/usage', authenticateApiKey, async (req, re
}
})
// 🔢 Token计数端点 - count_tokens beta API
router.post('/v1/messages/count_tokens', authenticateApiKey, async (req, res) => {
try {
// 检查权限
if (
req.apiKey.permissions &&
req.apiKey.permissions !== 'all' &&
req.apiKey.permissions !== 'claude'
) {
return res.status(403).json({
error: {
type: 'permission_error',
message: 'This API key does not have permission to access Claude'
}
})
}
logger.info(`🔢 Processing token count request for key: ${req.apiKey.name}`)
// 生成会话哈希用于sticky会话
const sessionHash = sessionHelper.generateSessionHash(req.body)
// 选择可用的Claude账户
const requestedModel = req.body.model
const { accountId, accountType } = await unifiedClaudeScheduler.selectAccountForApiKey(
req.apiKey,
sessionHash,
requestedModel
)
let response
if (accountType === 'claude-official') {
// 使用官方Claude账号转发count_tokens请求
response = await claudeRelayService.relayRequest(
req.body,
req.apiKey,
req,
res,
req.headers,
{
skipUsageRecord: true, // 跳过usage记录这只是计数请求
customPath: '/v1/messages/count_tokens' // 指定count_tokens路径
}
)
} else if (accountType === 'claude-console') {
// 使用Console Claude账号转发count_tokens请求
response = await claudeConsoleRelayService.relayRequest(
req.body,
req.apiKey,
req,
res,
req.headers,
accountId,
{
skipUsageRecord: true, // 跳过usage记录这只是计数请求
customPath: '/v1/messages/count_tokens' // 指定count_tokens路径
}
)
} else {
// Bedrock不支持count_tokens
return res.status(501).json({
error: {
type: 'not_supported',
message: 'Token counting is not supported for Bedrock accounts'
}
})
}
// 直接返回响应不记录token使用量
res.status(response.statusCode)
// 设置响应头
const skipHeaders = ['content-encoding', 'transfer-encoding', 'content-length']
Object.keys(response.headers).forEach((key) => {
if (!skipHeaders.includes(key.toLowerCase())) {
res.setHeader(key, response.headers[key])
}
})
// 尝试解析并返回JSON响应
try {
const jsonData = JSON.parse(response.body)
res.json(jsonData)
} catch (parseError) {
res.send(response.body)
}
logger.info(`✅ Token count request completed for key: ${req.apiKey.name}`)
} catch (error) {
logger.error('❌ Token count error:', error)
res.status(500).json({
error: {
type: 'server_error',
message: 'Failed to count tokens'
}
})
}
})
module.exports = router

View File

@@ -1,18 +1,125 @@
const express = require('express')
const router = express.Router()
const logger = require('../utils/logger')
const webhookNotifier = require('../utils/webhookNotifier')
const webhookService = require('../services/webhookService')
const webhookConfigService = require('../services/webhookConfigService')
const { authenticateAdmin } = require('../middleware/auth')
// 获取webhook配置
router.get('/config', authenticateAdmin, async (req, res) => {
try {
const config = await webhookConfigService.getConfig()
res.json({
success: true,
config
})
} catch (error) {
logger.error('获取webhook配置失败:', error)
res.status(500).json({
error: 'Internal server error',
message: '获取webhook配置失败'
})
}
})
// 保存webhook配置
router.post('/config', authenticateAdmin, async (req, res) => {
try {
const config = await webhookConfigService.saveConfig(req.body)
res.json({
success: true,
message: 'Webhook配置已保存',
config
})
} catch (error) {
logger.error('保存webhook配置失败:', error)
res.status(500).json({
error: 'Internal server error',
message: error.message || '保存webhook配置失败'
})
}
})
// 添加webhook平台
router.post('/platforms', authenticateAdmin, async (req, res) => {
try {
const platform = await webhookConfigService.addPlatform(req.body)
res.json({
success: true,
message: 'Webhook平台已添加',
platform
})
} catch (error) {
logger.error('添加webhook平台失败:', error)
res.status(500).json({
error: 'Internal server error',
message: error.message || '添加webhook平台失败'
})
}
})
// 更新webhook平台
router.put('/platforms/:id', authenticateAdmin, async (req, res) => {
try {
const platform = await webhookConfigService.updatePlatform(req.params.id, req.body)
res.json({
success: true,
message: 'Webhook平台已更新',
platform
})
} catch (error) {
logger.error('更新webhook平台失败:', error)
res.status(500).json({
error: 'Internal server error',
message: error.message || '更新webhook平台失败'
})
}
})
// 删除webhook平台
router.delete('/platforms/:id', authenticateAdmin, async (req, res) => {
try {
await webhookConfigService.deletePlatform(req.params.id)
res.json({
success: true,
message: 'Webhook平台已删除'
})
} catch (error) {
logger.error('删除webhook平台失败:', error)
res.status(500).json({
error: 'Internal server error',
message: error.message || '删除webhook平台失败'
})
}
})
// 切换webhook平台启用状态
router.post('/platforms/:id/toggle', authenticateAdmin, async (req, res) => {
try {
const platform = await webhookConfigService.togglePlatform(req.params.id)
res.json({
success: true,
message: `Webhook平台已${platform.enabled ? '启用' : '禁用'}`,
platform
})
} catch (error) {
logger.error('切换webhook平台状态失败:', error)
res.status(500).json({
error: 'Internal server error',
message: error.message || '切换webhook平台状态失败'
})
}
})
// 测试Webhook连通性
router.post('/test', authenticateAdmin, async (req, res) => {
try {
const { url } = req.body
const { url, type = 'custom', secret, enableSign } = req.body
if (!url) {
return res.status(400).json({
error: 'Missing webhook URL',
message: 'Please provide a webhook URL to test'
message: '请提供webhook URL'
})
}
@@ -22,99 +129,144 @@ router.post('/test', authenticateAdmin, async (req, res) => {
} catch (urlError) {
return res.status(400).json({
error: 'Invalid URL format',
message: 'Please provide a valid webhook URL'
message: '请提供有效的webhook URL'
})
}
logger.info(`🧪 Testing webhook URL: ${url}`)
logger.info(`🧪 测试webhook: ${type} - ${url}`)
const result = await webhookNotifier.testWebhook(url)
// 创建临时平台配置
const platform = {
type,
url,
secret,
enableSign,
enabled: true,
timeout: 10000
}
const result = await webhookService.testWebhook(platform)
if (result.success) {
logger.info(`✅ Webhook test successful for: ${url}`)
logger.info(`✅ Webhook测试成功: ${url}`)
res.json({
success: true,
message: 'Webhook test successful',
message: 'Webhook测试成功',
url
})
} else {
logger.warn(`❌ Webhook test failed for: ${url} - ${result.error}`)
logger.warn(`❌ Webhook测试失败: ${url} - ${result.error}`)
res.status(400).json({
success: false,
message: 'Webhook test failed',
message: 'Webhook测试失败',
url,
error: result.error
})
}
} catch (error) {
logger.error('❌ Webhook test error:', error)
logger.error('❌ Webhook测试错误:', error)
res.status(500).json({
error: 'Internal server error',
message: 'Failed to test webhook'
message: '测试webhook失败'
})
}
})
// 手动触发账号异常通知(用于测试)
// 手动触发测试通知
router.post('/test-notification', authenticateAdmin, async (req, res) => {
try {
const {
type = 'test',
accountId = 'test-account-id',
accountName = 'Test Account',
accountName = '测试账号',
platform = 'claude-oauth',
status = 'error',
errorCode = 'TEST_ERROR',
reason = 'Manual test notification'
status = 'test',
errorCode = 'TEST_NOTIFICATION',
reason = '手动测试通知',
message = '这是一条测试通知消息,用于验证 Webhook 通知功能是否正常工作'
} = req.body
logger.info(`🧪 Sending test notification for account: ${accountName}`)
logger.info(`🧪 发送测试通知: ${type}`)
await webhookNotifier.sendAccountAnomalyNotification({
// 先检查webhook配置
const config = await webhookConfigService.getConfig()
logger.debug(
`Webhook配置: enabled=${config.enabled}, platforms=${config.platforms?.length || 0}`
)
if (!config.enabled) {
return res.status(400).json({
success: false,
message: 'Webhook通知未启用请先在设置中启用通知功能'
})
}
const enabledPlatforms = await webhookConfigService.getEnabledPlatforms()
logger.info(`找到 ${enabledPlatforms.length} 个启用的通知平台`)
if (enabledPlatforms.length === 0) {
return res.status(400).json({
success: false,
message: '没有启用的通知平台,请先添加并启用至少一个通知平台'
})
}
const testData = {
accountId,
accountName,
platform,
status,
errorCode,
reason
})
reason,
message,
timestamp: new Date().toISOString()
}
logger.info(`✅ Test notification sent successfully`)
const result = await webhookService.sendNotification(type, testData)
// 如果没有返回结果,说明可能是配置问题
if (!result) {
return res.status(400).json({
success: false,
message: 'Webhook服务未返回结果请检查配置和日志',
enabledPlatforms: enabledPlatforms.length
})
}
// 如果没有成功和失败的记录
if (result.succeeded === 0 && result.failed === 0) {
return res.status(400).json({
success: false,
message: '没有发送任何通知,请检查通知类型配置',
result,
enabledPlatforms: enabledPlatforms.length
})
}
if (result.failed > 0) {
logger.warn(`⚠️ 测试通知部分失败: ${result.succeeded}成功, ${result.failed}失败`)
return res.json({
success: true,
message: `测试通知部分成功: ${result.succeeded}个平台成功, ${result.failed}个平台失败`,
data: testData,
result
})
}
logger.info(`✅ 测试通知发送成功到 ${result.succeeded} 个平台`)
res.json({
success: true,
message: 'Test notification sent successfully',
data: {
accountId,
accountName,
platform,
status,
errorCode,
reason
}
message: `测试通知已成功发送到 ${result.succeeded} 个平台`,
data: testData,
result
})
} catch (error) {
logger.error('❌ Failed to send test notification:', error)
logger.error('❌ 发送测试通知失败:', error)
res.status(500).json({
error: 'Internal server error',
message: 'Failed to send test notification'
message: `发送测试通知失败: ${error.message}`
})
}
})
// 获取Webhook配置信息
router.get('/config', authenticateAdmin, (req, res) => {
const config = require('../../config/config')
res.json({
success: true,
config: {
enabled: config.webhook?.enabled !== false,
urls: config.webhook?.urls || [],
timeout: config.webhook?.timeout || 10000,
retries: config.webhook?.retries || 3,
urlCount: (config.webhook?.urls || []).length
}
})
})
module.exports = router