mirror of
https://github.com/Wei-Shaw/claude-relay-service.git
synced 2026-01-23 21:17:30 +00:00
Merge branch 'dev'
This commit is contained in:
@@ -318,19 +318,38 @@ async function handleLoadCodeAssist(req, res) {
|
|||||||
sessionHash,
|
sessionHash,
|
||||||
requestedModel
|
requestedModel
|
||||||
)
|
)
|
||||||
const { accessToken, refreshToken } = await geminiAccountService.getAccount(accountId)
|
const account = await geminiAccountService.getAccount(accountId)
|
||||||
|
const { accessToken, refreshToken, projectId } = account
|
||||||
|
|
||||||
const { metadata, cloudaicompanionProject } = req.body
|
const { metadata, cloudaicompanionProject } = req.body
|
||||||
|
|
||||||
const version = req.path.includes('v1beta') ? 'v1beta' : 'v1internal'
|
const version = req.path.includes('v1beta') ? 'v1beta' : 'v1internal'
|
||||||
logger.info(`LoadCodeAssist request (${version})`, {
|
logger.info(`LoadCodeAssist request (${version})`, {
|
||||||
metadata: metadata || {},
|
metadata: metadata || {},
|
||||||
cloudaicompanionProject: cloudaicompanionProject || null,
|
requestedProject: cloudaicompanionProject || null,
|
||||||
|
accountProject: projectId || null,
|
||||||
apiKeyId: req.apiKey?.id || 'unknown'
|
apiKeyId: req.apiKey?.id || 'unknown'
|
||||||
})
|
})
|
||||||
|
|
||||||
const client = await geminiAccountService.getOauthClient(accessToken, refreshToken)
|
const client = await geminiAccountService.getOauthClient(accessToken, refreshToken)
|
||||||
const response = await geminiAccountService.loadCodeAssist(client, cloudaicompanionProject)
|
|
||||||
|
// 根据账户配置决定项目ID:
|
||||||
|
// 1. 如果账户有项目ID -> 使用账户的项目ID(强制覆盖)
|
||||||
|
// 2. 如果账户没有项目ID -> 传递 null(移除项目ID)
|
||||||
|
let effectiveProjectId = null
|
||||||
|
|
||||||
|
if (projectId) {
|
||||||
|
// 账户配置了项目ID,强制使用它
|
||||||
|
effectiveProjectId = projectId
|
||||||
|
logger.info('Using account project ID for loadCodeAssist:', effectiveProjectId)
|
||||||
|
} else {
|
||||||
|
// 账户没有配置项目ID,确保不传递项目ID
|
||||||
|
effectiveProjectId = null
|
||||||
|
logger.info('No project ID in account for loadCodeAssist, removing project parameter')
|
||||||
|
}
|
||||||
|
|
||||||
|
const response = await geminiAccountService.loadCodeAssist(client, effectiveProjectId)
|
||||||
|
|
||||||
res.json(response)
|
res.json(response)
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
const version = req.path.includes('v1beta') ? 'v1beta' : 'v1internal'
|
const version = req.path.includes('v1beta') ? 'v1beta' : 'v1internal'
|
||||||
@@ -345,6 +364,7 @@ async function handleLoadCodeAssist(req, res) {
|
|||||||
// 共用的 onboardUser 处理函数
|
// 共用的 onboardUser 处理函数
|
||||||
async function handleOnboardUser(req, res) {
|
async function handleOnboardUser(req, res) {
|
||||||
try {
|
try {
|
||||||
|
// 提取请求参数
|
||||||
const { tierId, cloudaicompanionProject, metadata } = req.body
|
const { tierId, cloudaicompanionProject, metadata } = req.body
|
||||||
const sessionHash = sessionHelper.generateSessionHash(req.body)
|
const sessionHash = sessionHelper.generateSessionHash(req.body)
|
||||||
|
|
||||||
@@ -355,34 +375,53 @@ async function handleOnboardUser(req, res) {
|
|||||||
sessionHash,
|
sessionHash,
|
||||||
requestedModel
|
requestedModel
|
||||||
)
|
)
|
||||||
const { accessToken, refreshToken } = await geminiAccountService.getAccount(accountId)
|
const account = await geminiAccountService.getAccount(accountId)
|
||||||
|
const { accessToken, refreshToken, projectId } = account
|
||||||
|
|
||||||
const version = req.path.includes('v1beta') ? 'v1beta' : 'v1internal'
|
const version = req.path.includes('v1beta') ? 'v1beta' : 'v1internal'
|
||||||
logger.info(`OnboardUser request (${version})`, {
|
logger.info(`OnboardUser request (${version})`, {
|
||||||
tierId: tierId || 'not provided',
|
tierId: tierId || 'not provided',
|
||||||
cloudaicompanionProject: cloudaicompanionProject || null,
|
requestedProject: cloudaicompanionProject || null,
|
||||||
|
accountProject: projectId || null,
|
||||||
metadata: metadata || {},
|
metadata: metadata || {},
|
||||||
apiKeyId: req.apiKey?.id || 'unknown'
|
apiKeyId: req.apiKey?.id || 'unknown'
|
||||||
})
|
})
|
||||||
|
|
||||||
const client = await geminiAccountService.getOauthClient(accessToken, refreshToken)
|
const client = await geminiAccountService.getOauthClient(accessToken, refreshToken)
|
||||||
|
|
||||||
// 如果提供了完整参数,直接调用onboardUser
|
// 根据账户配置决定项目ID:
|
||||||
if (tierId && metadata) {
|
// 1. 如果账户有项目ID -> 使用账户的项目ID(强制覆盖)
|
||||||
|
// 2. 如果账户没有项目ID -> 传递 null(移除项目ID)
|
||||||
|
let effectiveProjectId = null
|
||||||
|
|
||||||
|
if (projectId) {
|
||||||
|
// 账户配置了项目ID,强制使用它
|
||||||
|
effectiveProjectId = projectId
|
||||||
|
logger.info('Using account project ID:', effectiveProjectId)
|
||||||
|
} else {
|
||||||
|
// 账户没有配置项目ID,确保不传递项目ID(即使客户端传了也要移除)
|
||||||
|
effectiveProjectId = null
|
||||||
|
logger.info('No project ID in account, removing project parameter')
|
||||||
|
}
|
||||||
|
|
||||||
|
// 如果提供了 tierId,直接调用 onboardUser
|
||||||
|
if (tierId) {
|
||||||
const response = await geminiAccountService.onboardUser(
|
const response = await geminiAccountService.onboardUser(
|
||||||
client,
|
client,
|
||||||
tierId,
|
tierId,
|
||||||
cloudaicompanionProject,
|
effectiveProjectId, // 使用处理后的项目ID
|
||||||
metadata
|
metadata
|
||||||
)
|
)
|
||||||
|
|
||||||
res.json(response)
|
res.json(response)
|
||||||
} else {
|
} else {
|
||||||
// 否则执行完整的setupUser流程
|
// 否则执行完整的 setupUser 流程
|
||||||
const response = await geminiAccountService.setupUser(
|
const response = await geminiAccountService.setupUser(
|
||||||
client,
|
client,
|
||||||
cloudaicompanionProject,
|
effectiveProjectId, // 使用处理后的项目ID
|
||||||
metadata
|
metadata
|
||||||
)
|
)
|
||||||
|
|
||||||
res.json(response)
|
res.json(response)
|
||||||
}
|
}
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
@@ -506,7 +545,7 @@ async function handleGenerateContent(req, res) {
|
|||||||
client,
|
client,
|
||||||
{ model, request: actualRequestData },
|
{ model, request: actualRequestData },
|
||||||
user_prompt_id,
|
user_prompt_id,
|
||||||
project || account.projectId,
|
account.projectId, // 始终使用账户配置的项目ID,忽略请求中的project
|
||||||
req.apiKey?.id // 使用 API Key ID 作为 session ID
|
req.apiKey?.id // 使用 API Key ID 作为 session ID
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -533,7 +572,6 @@ async function handleGenerateContent(req, res) {
|
|||||||
|
|
||||||
res.json(response)
|
res.json(response)
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.log(321, error.response)
|
|
||||||
const version = req.path.includes('v1beta') ? 'v1beta' : 'v1internal'
|
const version = req.path.includes('v1beta') ? 'v1beta' : 'v1internal'
|
||||||
logger.error(`Error in generateContent endpoint (${version})`, { error: error.message })
|
logger.error(`Error in generateContent endpoint (${version})`, { error: error.message })
|
||||||
res.status(500).json({
|
res.status(500).json({
|
||||||
@@ -620,7 +658,7 @@ async function handleStreamGenerateContent(req, res) {
|
|||||||
client,
|
client,
|
||||||
{ model, request: actualRequestData },
|
{ model, request: actualRequestData },
|
||||||
user_prompt_id,
|
user_prompt_id,
|
||||||
project || account.projectId,
|
account.projectId, // 始终使用账户配置的项目ID,忽略请求中的project
|
||||||
req.apiKey?.id, // 使用 API Key ID 作为 session ID
|
req.apiKey?.id, // 使用 API Key ID 作为 session ID
|
||||||
abortController.signal // 传递中止信号
|
abortController.signal // 传递中止信号
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -1025,7 +1025,12 @@ async function onboardUser(client, tierId, projectId, clientMetadata) {
|
|||||||
metadata: clientMetadata
|
metadata: clientMetadata
|
||||||
}
|
}
|
||||||
|
|
||||||
logger.info('📋 开始onboardUser API调用', { tierId, projectId })
|
logger.info('📋 开始onboardUser API调用', {
|
||||||
|
tierId,
|
||||||
|
projectId,
|
||||||
|
hasProjectId: !!projectId,
|
||||||
|
isFreeTier: tierId === 'free-tier' || tierId === 'FREE'
|
||||||
|
})
|
||||||
|
|
||||||
// 轮询onboardUser直到长运行操作完成
|
// 轮询onboardUser直到长运行操作完成
|
||||||
let lroRes = await axios({
|
let lroRes = await axios({
|
||||||
|
|||||||
Reference in New Issue
Block a user