feat: 支持Gemini-Api接入

This commit is contained in:
shaw
2025-11-23 22:00:13 +08:00
parent b197cba325
commit bae39d5468
13 changed files with 2355 additions and 287 deletions

View File

@@ -3,9 +3,12 @@ const router = express.Router()
const { authenticateApiKey } = require('../middleware/auth')
const logger = require('../utils/logger')
const geminiAccountService = require('../services/geminiAccountService')
const geminiApiAccountService = require('../services/geminiApiAccountService')
const unifiedGeminiScheduler = require('../services/unifiedGeminiScheduler')
const apiKeyService = require('../services/apiKeyService')
const sessionHelper = require('../utils/sessionHelper')
const axios = require('axios')
const ProxyHelper = require('../utils/proxyHelper')
// 导入 geminiRoutes 中导出的处理函数
const { handleLoadCodeAssist, handleOnboardUser, handleCountTokens } = require('./geminiRoutes')
@@ -136,6 +139,8 @@ async function normalizeAxiosStreamError(error) {
async function handleStandardGenerateContent(req, res) {
let account = null
let sessionHash = null
let accountId = null // 提升到外部作用域
let isApiAccount = false // 提升到外部作用域
try {
if (!ensureGeminiPermission(req, res)) {
@@ -210,20 +215,48 @@ async function handleStandardGenerateContent(req, res) {
}
// 使用统一调度选择账号
const { accountId } = await unifiedGeminiScheduler.selectAccountForApiKey(
const schedulerResult = await unifiedGeminiScheduler.selectAccountForApiKey(
req.apiKey,
sessionHash,
model
model,
{ allowApiAccounts: true } // 允许调度 API 账户
)
account = await geminiAccountService.getAccount(accountId)
const { accessToken, refreshToken } = account
;({ accountId } = schedulerResult)
const { accountType } = schedulerResult
// 判断账户类型:根据 accountType 判断,而非 accountId 前缀
isApiAccount = accountType === 'gemini-api' // 赋值而不是声明
const actualAccountId = accountId // accountId 已经是实际 ID无需处理前缀
const version = req.path.includes('v1beta') ? 'v1beta' : 'v1'
logger.info(`Standard Gemini API generateContent request (${version})`, {
model,
projectId: account.projectId,
apiKeyId: req.apiKey?.id || 'unknown'
})
if (isApiAccount) {
// Gemini API 账户:使用 API Key 直接请求
account = await geminiApiAccountService.getAccount(actualAccountId)
if (!account) {
return res.status(404).json({
error: {
message: 'Gemini API account not found',
type: 'account_not_found'
}
})
}
logger.info(`Standard Gemini API generateContent request (${version}) - API Key Account`, {
model,
accountId: actualAccountId,
apiKeyId: req.apiKey?.id || 'unknown'
})
} else {
// OAuth 账户:使用原有流程
account = await geminiAccountService.getAccount(actualAccountId)
logger.info(`Standard Gemini API generateContent request (${version}) - OAuth Account`, {
model,
projectId: account.projectId,
apiKeyId: req.apiKey?.id || 'unknown'
})
}
// 解析账户的代理配置
let proxyConfig = null
@@ -235,63 +268,106 @@ async function handleStandardGenerateContent(req, res) {
}
}
const client = await geminiAccountService.getOauthClient(accessToken, refreshToken, proxyConfig)
let response
// 项目ID优先级账户配置的项目ID > 临时项目ID > 尝试获取
let effectiveProjectId = account.projectId || account.tempProjectId || null
if (isApiAccount) {
// Gemini API 账户:直接使用 API Key 请求
// baseUrl 填写域名,如 https://generativelanguage.googleapis.com版本固定为 v1beta
const apiUrl = `${account.baseUrl}/v1beta/models/${model}:generateContent?key=${account.apiKey}`
// 如果没有任何项目ID尝试调用 loadCodeAssist 获取
if (!effectiveProjectId) {
try {
logger.info('📋 No projectId available, attempting to fetch from loadCodeAssist...')
const loadResponse = await geminiAccountService.loadCodeAssist(client, null, proxyConfig)
if (loadResponse.cloudaicompanionProject) {
effectiveProjectId = loadResponse.cloudaicompanionProject
// 保存临时项目ID
await geminiAccountService.updateTempProjectId(accountId, effectiveProjectId)
logger.info(`📋 Fetched and cached temporary projectId: ${effectiveProjectId}`)
// 构建 Axios 配置
const axiosConfig = {
method: 'POST',
url: apiUrl,
data: actualRequestData,
headers: {
'Content-Type': 'application/json'
}
} catch (loadError) {
logger.warn('Failed to fetch projectId from loadCodeAssist:', loadError.message)
}
}
// 如果还是没有项目ID返回错误
if (!effectiveProjectId) {
return res.status(403).json({
error: {
message:
'This account requires a project ID to be configured. Please configure a project ID in the account settings.',
type: 'configuration_required'
// 添加代理配置
if (proxyConfig) {
const proxyHelper = new ProxyHelper()
axiosConfig.httpsAgent = proxyHelper.createProxyAgent(proxyConfig)
axiosConfig.httpAgent = proxyHelper.createProxyAgent(proxyConfig)
}
try {
const apiResponse = await axios(axiosConfig)
response = { response: apiResponse.data }
} catch (error) {
logger.error('Gemini API request failed:', {
status: error.response?.status,
statusText: error.response?.statusText,
data: error.response?.data
})
throw error
}
} else {
// OAuth 账户:使用原有流程
const { accessToken, refreshToken } = account
const client = await geminiAccountService.getOauthClient(
accessToken,
refreshToken,
proxyConfig
)
// 项目ID优先级账户配置的项目ID > 临时项目ID > 尝试获取
let effectiveProjectId = account.projectId || account.tempProjectId || null
// 如果没有任何项目ID尝试调用 loadCodeAssist 获取
if (!effectiveProjectId) {
try {
logger.info('📋 No projectId available, attempting to fetch from loadCodeAssist...')
const loadResponse = await geminiAccountService.loadCodeAssist(client, null, proxyConfig)
if (loadResponse.cloudaicompanionProject) {
effectiveProjectId = loadResponse.cloudaicompanionProject
// 保存临时项目ID
await geminiAccountService.updateTempProjectId(actualAccountId, effectiveProjectId)
logger.info(`📋 Fetched and cached temporary projectId: ${effectiveProjectId}`)
}
} catch (loadError) {
logger.warn('Failed to fetch projectId from loadCodeAssist:', loadError.message)
}
}
// 如果还是没有项目ID返回错误
if (!effectiveProjectId) {
return res.status(403).json({
error: {
message:
'This account requires a project ID to be configured. Please configure a project ID in the account settings.',
type: 'configuration_required'
}
})
}
logger.info('📋 Standard API 项目ID处理逻辑', {
accountProjectId: account.projectId,
tempProjectId: account.tempProjectId,
effectiveProjectId,
decision: account.projectId
? '使用账户配置'
: account.tempProjectId
? '使用临时项目ID'
: '从loadCodeAssist获取'
})
// 生成一个符合 Gemini CLI 格式的 user_prompt_id
const userPromptId = `${require('crypto').randomUUID()}########0`
// 调用内部 APIcloudcode-pa
response = await geminiAccountService.generateContent(
client,
{ model, request: actualRequestData },
userPromptId, // 使用生成的 user_prompt_id
effectiveProjectId, // 使用处理后的项目ID
req.apiKey?.id, // 使用 API Key ID 作为 session ID
proxyConfig
)
}
logger.info('📋 Standard API 项目ID处理逻辑', {
accountProjectId: account.projectId,
tempProjectId: account.tempProjectId,
effectiveProjectId,
decision: account.projectId
? '使用账户配置'
: account.tempProjectId
? '使用临时项目ID'
: '从loadCodeAssist获取'
})
// 生成一个符合 Gemini CLI 格式的 user_prompt_id
const userPromptId = `${require('crypto').randomUUID()}########0`
// 调用内部 APIcloudcode-pa
const response = await geminiAccountService.generateContent(
client,
{ model, request: actualRequestData },
userPromptId, // 使用生成的 user_prompt_id
effectiveProjectId, // 使用处理后的项目ID
req.apiKey?.id, // 使用 API Key ID 作为 session ID
proxyConfig
)
// 记录使用统计
if (response?.response?.usageMetadata) {
try {
@@ -303,7 +379,7 @@ async function handleStandardGenerateContent(req, res) {
0, // cacheCreateTokens
0, // cacheReadTokens
model,
account.id
accountId // 账户 ID
)
logger.info(
`📊 Recorded Gemini usage - Input: ${usage.promptTokenCount}, Output: ${usage.candidatesTokenCount}, Total: ${usage.totalTokenCount}`
@@ -327,10 +403,15 @@ async function handleStandardGenerateContent(req, res) {
})
// 处理速率限制
if (error.response?.status === 429) {
logger.warn(`⚠️ Gemini account ${account.id} rate limited (Standard API), marking as limited`)
if (error.response?.status === 429 && accountId) {
logger.warn(`⚠️ Gemini account ${accountId} rate limited (Standard API), marking as limited`)
try {
await unifiedGeminiScheduler.markAccountRateLimited(account.id, 'gemini', sessionHash)
const rateLimitAccountType = isApiAccount ? 'gemini-api' : 'gemini'
await unifiedGeminiScheduler.markAccountRateLimited(
accountId, // 账户 ID
rateLimitAccountType,
sessionHash
)
} catch (limitError) {
logger.warn('Failed to mark account as rate limited in scheduler:', limitError)
}
@@ -350,6 +431,8 @@ async function handleStandardStreamGenerateContent(req, res) {
let abortController = null
let account = null
let sessionHash = null
let accountId = null // 提升到外部作用域
let isApiAccount = false // 提升到外部作用域
try {
if (!ensureGeminiPermission(req, res)) {
@@ -424,20 +507,54 @@ async function handleStandardStreamGenerateContent(req, res) {
}
// 使用统一调度选择账号
const { accountId } = await unifiedGeminiScheduler.selectAccountForApiKey(
const schedulerResult = await unifiedGeminiScheduler.selectAccountForApiKey(
req.apiKey,
sessionHash,
model
model,
{ allowApiAccounts: true } // 允许调度 API 账户
)
account = await geminiAccountService.getAccount(accountId)
const { accessToken, refreshToken } = account
;({ accountId } = schedulerResult)
const { accountType } = schedulerResult
// 判断账户类型:根据 accountType 判断,而非 accountId 前缀
isApiAccount = accountType === 'gemini-api' // 赋值而不是声明
const actualAccountId = accountId // accountId 已经是实际 ID无需处理前缀
const version = req.path.includes('v1beta') ? 'v1beta' : 'v1'
logger.info(`Standard Gemini API streamGenerateContent request (${version})`, {
model,
projectId: account.projectId,
apiKeyId: req.apiKey?.id || 'unknown'
})
if (isApiAccount) {
// Gemini API 账户:使用 API Key 直接请求
account = await geminiApiAccountService.getAccount(actualAccountId)
if (!account) {
return res.status(404).json({
error: {
message: 'Gemini API account not found',
type: 'account_not_found'
}
})
}
logger.info(
`Standard Gemini API streamGenerateContent request (${version}) - API Key Account`,
{
model,
accountId: actualAccountId,
apiKeyId: req.apiKey?.id || 'unknown'
}
)
} else {
// OAuth 账户:使用原有流程
account = await geminiAccountService.getAccount(actualAccountId)
logger.info(
`Standard Gemini API streamGenerateContent request (${version}) - OAuth Account`,
{
model,
projectId: account.projectId,
apiKeyId: req.apiKey?.id || 'unknown'
}
)
}
// 创建中止控制器
abortController = new AbortController()
@@ -460,64 +577,109 @@ async function handleStandardStreamGenerateContent(req, res) {
}
}
const client = await geminiAccountService.getOauthClient(accessToken, refreshToken, proxyConfig)
let streamResponse
// 项目ID优先级账户配置的项目ID > 临时项目ID > 尝试获取
let effectiveProjectId = account.projectId || account.tempProjectId || null
if (isApiAccount) {
// Gemini API 账户:直接使用 API Key 请求流式接口
// baseUrl 填写域名,版本固定为 v1beta
const apiUrl = `${account.baseUrl}/v1beta/models/${model}:streamGenerateContent?key=${account.apiKey}&alt=sse`
// 如果没有任何项目ID尝试调用 loadCodeAssist 获取
if (!effectiveProjectId) {
try {
logger.info('📋 No projectId available, attempting to fetch from loadCodeAssist...')
const loadResponse = await geminiAccountService.loadCodeAssist(client, null, proxyConfig)
if (loadResponse.cloudaicompanionProject) {
effectiveProjectId = loadResponse.cloudaicompanionProject
// 保存临时项目ID
await geminiAccountService.updateTempProjectId(accountId, effectiveProjectId)
logger.info(`📋 Fetched and cached temporary projectId: ${effectiveProjectId}`)
}
} catch (loadError) {
logger.warn('Failed to fetch projectId from loadCodeAssist:', loadError.message)
// 构建 Axios 配置
const axiosConfig = {
method: 'POST',
url: apiUrl,
data: actualRequestData,
headers: {
'Content-Type': 'application/json'
},
responseType: 'stream',
signal: abortController.signal
}
}
// 如果还是没有项目ID返回错误
if (!effectiveProjectId) {
return res.status(403).json({
error: {
message:
'This account requires a project ID to be configured. Please configure a project ID in the account settings.',
type: 'configuration_required'
// 添加代理配置
if (proxyConfig) {
const proxyHelper = new ProxyHelper()
axiosConfig.httpsAgent = proxyHelper.createProxyAgent(proxyConfig)
axiosConfig.httpAgent = proxyHelper.createProxyAgent(proxyConfig)
}
try {
const apiResponse = await axios(axiosConfig)
streamResponse = apiResponse.data
} catch (error) {
logger.error('Gemini API stream request failed:', {
status: error.response?.status,
statusText: error.response?.statusText,
data: error.response?.data
})
throw error
}
} else {
// OAuth 账户:使用原有流程
const { accessToken, refreshToken } = account
const client = await geminiAccountService.getOauthClient(
accessToken,
refreshToken,
proxyConfig
)
// 项目ID优先级账户配置的项目ID > 临时项目ID > 尝试获取
let effectiveProjectId = account.projectId || account.tempProjectId || null
// 如果没有任何项目ID尝试调用 loadCodeAssist 获取
if (!effectiveProjectId) {
try {
logger.info('📋 No projectId available, attempting to fetch from loadCodeAssist...')
const loadResponse = await geminiAccountService.loadCodeAssist(client, null, proxyConfig)
if (loadResponse.cloudaicompanionProject) {
effectiveProjectId = loadResponse.cloudaicompanionProject
// 保存临时项目ID
await geminiAccountService.updateTempProjectId(actualAccountId, effectiveProjectId)
logger.info(`📋 Fetched and cached temporary projectId: ${effectiveProjectId}`)
}
} catch (loadError) {
logger.warn('Failed to fetch projectId from loadCodeAssist:', loadError.message)
}
}
// 如果还是没有项目ID返回错误
if (!effectiveProjectId) {
return res.status(403).json({
error: {
message:
'This account requires a project ID to be configured. Please configure a project ID in the account settings.',
type: 'configuration_required'
}
})
}
logger.info('📋 Standard API 流式项目ID处理逻辑', {
accountProjectId: account.projectId,
tempProjectId: account.tempProjectId,
effectiveProjectId,
decision: account.projectId
? '使用账户配置'
: account.tempProjectId
? '使用临时项目ID'
: '从loadCodeAssist获取'
})
// 生成一个符合 Gemini CLI 格式的 user_prompt_id
const userPromptId = `${require('crypto').randomUUID()}########0`
// 调用内部 APIcloudcode-pa的流式接口
streamResponse = await geminiAccountService.generateContentStream(
client,
{ model, request: actualRequestData },
userPromptId, // 使用生成的 user_prompt_id
effectiveProjectId, // 使用处理后的项目ID
req.apiKey?.id, // 使用 API Key ID 作为 session ID
abortController.signal,
proxyConfig
)
}
logger.info('📋 Standard API 流式项目ID处理逻辑', {
accountProjectId: account.projectId,
tempProjectId: account.tempProjectId,
effectiveProjectId,
decision: account.projectId
? '使用账户配置'
: account.tempProjectId
? '使用临时项目ID'
: '从loadCodeAssist获取'
})
// 生成一个符合 Gemini CLI 格式的 user_prompt_id
const userPromptId = `${require('crypto').randomUUID()}########0`
// 调用内部 APIcloudcode-pa的流式接口
const streamResponse = await geminiAccountService.generateContentStream(
client,
{ model, request: actualRequestData },
userPromptId, // 使用生成的 user_prompt_id
effectiveProjectId, // 使用处理后的项目ID
req.apiKey?.id, // 使用 API Key ID 作为 session ID
abortController.signal,
proxyConfig
)
// 设置 SSE 响应头
res.setHeader('Content-Type', 'text/event-stream')
res.setHeader('Cache-Control', 'no-cache')
@@ -672,7 +834,7 @@ async function handleStandardStreamGenerateContent(req, res) {
0, // cacheCreateTokens
0, // cacheReadTokens
model,
account.id
accountId // 使用原始 accountId含前缀
)
.then(() => {
logger.info(
@@ -743,12 +905,17 @@ async function handleStandardStreamGenerateContent(req, res) {
})
// 处理速率限制
if (error.response?.status === 429) {
if (error.response?.status === 429 && accountId) {
logger.warn(
`⚠️ Gemini account ${account.id} rate limited (Standard Stream API), marking as limited`
`⚠️ Gemini account ${accountId} rate limited (Standard Stream API), marking as limited`
)
try {
await unifiedGeminiScheduler.markAccountRateLimited(account.id, 'gemini', sessionHash)
const rateLimitAccountType = isApiAccount ? 'gemini-api' : 'gemini'
await unifiedGeminiScheduler.markAccountRateLimited(
accountId, // 账户 ID
rateLimitAccountType,
sessionHash
)
} catch (limitError) {
logger.warn('Failed to mark account as rate limited in scheduler:', limitError)
}