feat: 完善 Gemini 功能与 Claude 保持一致

- 添加 Gemini 账户的 schedulable 字段和调度开关 API
- 实现 Gemini 调度器的模型过滤功能
- 完善 Gemini 数据统计,记录 token 使用量
- 修复 Gemini 流式响应的 SSE 解析和 AbortController 支持
- 在教程页面和 README 中添加 Gemini CLI 环境变量说明
- 修复前端 Gemini 账户调度开关限制

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
shaw
2025-08-04 16:53:11 +08:00
parent 15b4efa353
commit ef4f7483d3
9 changed files with 443 additions and 33 deletions

View File

@@ -1521,6 +1521,30 @@ router.post('/gemini-accounts/:accountId/refresh', authenticateAdmin, async (req
}
});
// 切换 Gemini 账户调度状态
router.put('/gemini-accounts/:accountId/toggle-schedulable', authenticateAdmin, async (req, res) => {
try {
const { accountId } = req.params;
const account = await geminiAccountService.getAccount(accountId);
if (!account) {
return res.status(404).json({ error: 'Account not found' });
}
// 将字符串 'true'/'false' 转换为布尔值,然后取反
const currentSchedulable = account.schedulable === 'true';
const newSchedulable = !currentSchedulable;
await geminiAccountService.updateAccount(accountId, { schedulable: String(newSchedulable) });
logger.success(`🔄 Admin toggled Gemini account schedulable status: ${accountId} -> ${newSchedulable ? 'schedulable' : 'not schedulable'}`);
res.json({ success: true, schedulable: newSchedulable });
} catch (error) {
logger.error('❌ Failed to toggle Gemini account schedulable status:', error);
res.status(500).json({ error: 'Failed to toggle schedulable status', message: error.message });
}
});
// 📊 账户使用统计
// 获取所有账户的使用统计

View File

@@ -7,7 +7,7 @@ const { sendGeminiRequest, getAvailableModels } = require('../services/geminiRel
const crypto = require('crypto');
const sessionHelper = require('../utils/sessionHelper');
const unifiedGeminiScheduler = require('../services/unifiedGeminiScheduler');
const { OAuth2Client } = require('google-auth-library');
// const { OAuth2Client } = require('google-auth-library'); // OAuth2Client is not used in this file
// 生成会话哈希
function generateSessionHash(req) {
@@ -108,7 +108,8 @@ router.post('/messages', authenticateApiKey, async (req, res) => {
proxy: account.proxy,
apiKeyId: apiKeyData.id,
signal: abortController.signal,
projectId: account.projectId
projectId: account.projectId,
accountId: account.id
});
if (stream) {

View File

@@ -279,6 +279,10 @@ async function createAccount(accountData) {
accountType: accountData.accountType || 'shared',
isActive: 'true',
status: 'active',
// 调度相关
schedulable: accountData.schedulable !== undefined ? String(accountData.schedulable) : 'true',
priority: accountData.priority || 50, // 调度优先级 (1-100数字越小优先级越高)
// OAuth 相关字段(加密存储)
geminiOauth: geminiOauth ? encrypt(geminiOauth) : '',
@@ -292,6 +296,9 @@ async function createAccount(accountData) {
// 项目编号Google Cloud/Workspace 账号需要)
projectId: accountData.projectId || '',
// 支持的模型列表(可选)
supportedModels: accountData.supportedModels || [], // 空数组表示支持所有模型
// 时间戳
createdAt: now,

View File

@@ -3,7 +3,7 @@ const { HttpsProxyAgent } = require('https-proxy-agent');
const { SocksProxyAgent } = require('socks-proxy-agent');
const logger = require('../utils/logger');
const config = require('../../config/config');
const { recordUsageMetrics } = require('./apiKeyService');
const apiKeyService = require('./apiKeyService');
// Gemini API 配置
const GEMINI_API_BASE = 'https://cloudcode.googleapis.com/v1';
@@ -123,7 +123,7 @@ function convertGeminiResponse(geminiResponse, model, stream = false) {
}
// 处理流式响应
async function* handleStreamResponse(response, model, apiKeyId) {
async function* handleStreamResponse(response, model, apiKeyId, accountId = null) {
let buffer = '';
let totalUsage = {
promptTokenCount: 0,
@@ -168,10 +168,16 @@ async function* handleStreamResponse(response, model, apiKeyId) {
if (data.candidates?.[0]?.finishReason === 'STOP') {
// 记录使用量
if (apiKeyId && totalUsage.totalTokenCount > 0) {
await recordUsageMetrics(apiKeyId, {
inputTokens: totalUsage.promptTokenCount,
outputTokens: totalUsage.candidatesTokenCount,
model: model
await apiKeyService.recordUsage(
apiKeyId,
totalUsage.promptTokenCount || 0, // inputTokens
totalUsage.candidatesTokenCount || 0, // outputTokens
0, // cacheCreateTokens (Gemini 没有这个概念)
0, // cacheReadTokens (Gemini 没有这个概念)
model,
accountId
).catch(error => {
logger.error('❌ Failed to record Gemini usage:', error);
});
}
@@ -206,13 +212,18 @@ async function* handleStreamResponse(response, model, apiKeyId) {
yield 'data: [DONE]\n\n';
} catch (error) {
logger.error('Stream processing error:', error);
yield `data: ${JSON.stringify({
error: {
message: error.message,
type: 'stream_error'
}
})}\n\n`;
// 检查是否是请求被中止
if (error.name === 'CanceledError' || error.code === 'ECONNABORTED') {
logger.info('Stream request was aborted by client');
} else {
logger.error('Stream processing error:', error);
yield `data: ${JSON.stringify({
error: {
message: error.message,
type: 'stream_error'
}
})}\n\n`;
}
}
}
@@ -226,8 +237,10 @@ async function sendGeminiRequest({
accessToken,
proxy,
apiKeyId,
signal,
projectId,
location = 'us-central1'
location = 'us-central1',
accountId = null
}) {
// 确保模型名称格式正确
if (!model.startsWith('models/')) {
@@ -281,6 +294,12 @@ async function sendGeminiRequest({
logger.debug('Using proxy for Gemini request');
}
// 添加 AbortController 信号支持
if (signal) {
axiosConfig.signal = signal;
logger.debug('AbortController signal attached to request');
}
if (stream) {
axiosConfig.responseType = 'stream';
}
@@ -290,23 +309,42 @@ async function sendGeminiRequest({
const response = await axios(axiosConfig);
if (stream) {
return handleStreamResponse(response, model, apiKeyId);
return handleStreamResponse(response, model, apiKeyId, accountId);
} else {
// 非流式响应
const openaiResponse = convertGeminiResponse(response.data, model, false);
// 记录使用量
if (apiKeyId && openaiResponse.usage) {
await recordUsageMetrics(apiKeyId, {
inputTokens: openaiResponse.usage.prompt_tokens,
outputTokens: openaiResponse.usage.completion_tokens,
model: model
await apiKeyService.recordUsage(
apiKeyId,
openaiResponse.usage.prompt_tokens || 0,
openaiResponse.usage.completion_tokens || 0,
0, // cacheCreateTokens
0, // cacheReadTokens
model,
accountId
).catch(error => {
logger.error('❌ Failed to record Gemini usage:', error);
});
}
return openaiResponse;
}
} catch (error) {
// 检查是否是请求被中止
if (error.name === 'CanceledError' || error.code === 'ECONNABORTED') {
logger.info('Gemini request was aborted by client');
throw {
status: 499,
error: {
message: 'Request canceled by client',
type: 'canceled',
code: 'request_canceled'
}
};
}
logger.error('Gemini API request failed:', error.response?.data || error.message);
// 转换错误格式

View File

@@ -18,7 +18,7 @@ class UnifiedClaudeScheduler {
if (apiKeyData.claudeAccountId.startsWith('group:')) {
const groupId = apiKeyData.claudeAccountId.replace('group:', '');
logger.info(`🎯 API key ${apiKeyData.name} is bound to group ${groupId}, selecting from group`);
return await this.selectAccountFromGroup(groupId, sessionHash, requestedModel, apiKeyData);
return await this.selectAccountFromGroup(groupId, sessionHash, requestedModel);
}
// 普通专属账户
@@ -370,7 +370,7 @@ class UnifiedClaudeScheduler {
}
// 👥 从分组中选择账户
async selectAccountFromGroup(groupId, sessionHash = null, requestedModel = null, apiKeyData = null) {
async selectAccountFromGroup(groupId, sessionHash = null, requestedModel = null) {
try {
// 获取分组信息
const group = await accountGroupService.getGroup(groupId);
@@ -426,7 +426,7 @@ class UnifiedClaudeScheduler {
}
} else if (group.platform === 'gemini') {
// Gemini暂时不支持预留接口
logger.warn(`⚠️ Gemini group scheduling not yet implemented`);
logger.warn('⚠️ Gemini group scheduling not yet implemented');
continue;
}

View File

@@ -95,6 +95,19 @@ class UnifiedGeminiScheduler {
if (boundAccount && boundAccount.isActive === 'true' && boundAccount.status !== 'error') {
const isRateLimited = await this.isAccountRateLimited(boundAccount.id);
if (!isRateLimited) {
// 检查模型支持
if (requestedModel && boundAccount.supportedModels && boundAccount.supportedModels.length > 0) {
// 处理可能带有 models/ 前缀的模型名
const normalizedModel = requestedModel.replace('models/', '');
const modelSupported = boundAccount.supportedModels.some(model =>
model.replace('models/', '') === normalizedModel
);
if (!modelSupported) {
logger.warn(`⚠️ Bound Gemini account ${boundAccount.name} does not support model ${requestedModel}`);
return availableAccounts;
}
}
logger.info(`🎯 Using bound dedicated Gemini account: ${boundAccount.name} (${apiKeyData.geminiAccountId})`);
return [{
...boundAccount,
@@ -124,6 +137,19 @@ class UnifiedGeminiScheduler {
continue;
}
// 检查模型支持
if (requestedModel && account.supportedModels && account.supportedModels.length > 0) {
// 处理可能带有 models/ 前缀的模型名
const normalizedModel = requestedModel.replace('models/', '');
const modelSupported = account.supportedModels.some(model =>
model.replace('models/', '') === normalizedModel
);
if (!modelSupported) {
logger.debug(`⏭️ Skipping Gemini account ${account.name} - doesn't support model ${requestedModel}`);
continue;
}
}
// 检查是否被限流
const isRateLimited = await this.isAccountRateLimited(account.id);
if (!isRateLimited) {
@@ -269,7 +295,7 @@ class UnifiedGeminiScheduler {
}
// 👥 从分组中选择账户
async selectAccountFromGroup(groupId, sessionHash = null, requestedModel = null, apiKeyData = null) {
async selectAccountFromGroup(groupId, sessionHash = null, requestedModel = null) {
try {
// 获取分组信息
const group = await accountGroupService.getGroup(groupId);
@@ -330,6 +356,19 @@ class UnifiedGeminiScheduler {
continue;
}
// 检查模型支持
if (requestedModel && account.supportedModels && account.supportedModels.length > 0) {
// 处理可能带有 models/ 前缀的模型名
const normalizedModel = requestedModel.replace('models/', '');
const modelSupported = account.supportedModels.some(model =>
model.replace('models/', '') === normalizedModel
);
if (!modelSupported) {
logger.debug(`⏭️ Skipping Gemini account ${account.name} in group - doesn't support model ${requestedModel}`);
continue;
}
}
// 检查是否被限流
const isRateLimited = await this.isAccountRateLimited(account.id);
if (!isRateLimited) {