refactor(GeminiAPIClient): separate model and user message handling to adapt vertex (#7511)

- Introduced a new modelParts array to manage model-related messages separately from user messages.
- Updated the logic to push model messages to currentReqMessages only if they exist, improving clarity and structure.
- Adjusted the return order of messages in buildSdkMessages to ensure history is appended correctly.
- Enhanced McpToolChunkMiddleware to reset tool processing state output when output is present.
This commit is contained in:
SuYao 2025-06-25 22:16:27 +08:00 committed by GitHub
parent 17a8f0a724
commit 9362304db0
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 25 additions and 6 deletions

View File

@ -686,16 +686,19 @@ export class GeminiAPIClient extends BaseApiClient<
toolCalls: FunctionCall[] toolCalls: FunctionCall[]
): Content[] { ): Content[] {
const parts: Part[] = [] const parts: Part[] = []
const modelParts: Part[] = []
if (output) { if (output) {
parts.push({ modelParts.push({
text: output text: output
}) })
} }
toolCalls.forEach((toolCall) => { toolCalls.forEach((toolCall) => {
parts.push({ modelParts.push({
functionCall: toolCall functionCall: toolCall
}) })
}) })
parts.push( parts.push(
...toolResults ...toolResults
.map((ts) => ts.parts) .map((ts) => ts.parts)
@ -703,10 +706,22 @@ export class GeminiAPIClient extends BaseApiClient<
.filter((p) => p !== undefined) .filter((p) => p !== undefined)
) )
const lastMessage = currentReqMessages[currentReqMessages.length - 1] const userMessage: Content = {
if (lastMessage) { role: 'user',
lastMessage.parts?.push(...parts) parts: []
} }
if (modelParts.length > 0) {
currentReqMessages.push({
role: 'model',
parts: modelParts
})
}
if (parts.length > 0) {
userMessage.parts?.push(...parts)
currentReqMessages.push(userMessage)
}
return currentReqMessages return currentReqMessages
} }
@ -747,7 +762,7 @@ export class GeminiAPIClient extends BaseApiClient<
} }
}) })
} }
return [messageParam, ...(sdkPayload.history || [])] return [...(sdkPayload.history || []), messageParam]
} }
private async uploadFile(file: FileType): Promise<File> { private async uploadFile(file: FileType): Promise<File> {

View File

@ -255,6 +255,10 @@ function buildParamsWithToolResults(
// 从回复中构建助手消息 // 从回复中构建助手消息
const newReqMessages = apiClient.buildSdkMessages(currentReqMessages, output, toolResults, toolCalls) const newReqMessages = apiClient.buildSdkMessages(currentReqMessages, output, toolResults, toolCalls)
if (output && ctx._internal.toolProcessingState) {
ctx._internal.toolProcessingState.output = undefined
}
// 估算新增消息的 token 消耗并累加到 usage 中 // 估算新增消息的 token 消耗并累加到 usage 中
if (ctx._internal.observer?.usage && newReqMessages.length > currentReqMessages.length) { if (ctx._internal.observer?.usage && newReqMessages.length > currentReqMessages.length) {
try { try {