feat: 添加gemini协议适配 (#125)

* feat: 添加gemini协议适配

* Remove unrelated local files from Gemini PR
This commit is contained in:
SaltedFish555
2026-04-06 09:55:20 +08:00
committed by GitHub
parent 27825293bb
commit 0da5ec09e8
24 changed files with 2257 additions and 38 deletions

View File

@@ -640,26 +640,53 @@ export function assistantMessageToMessageParam(
} else {
return {
role: 'assistant',
content: message.message.content.map((_, i) => ({
..._,
...(i === message.message.content.length - 1 &&
_.type !== 'thinking' &&
_.type !== 'redacted_thinking' &&
(feature('CONNECTOR_TEXT') ? !isConnectorTextBlock(_) : true)
? enablePromptCaching
? { cache_control: getCacheControl({ querySource }) }
: {}
: {}),
})),
content: message.message.content.map((_, i) => {
const contentBlock = stripGeminiProviderMetadata(_)
return {
...contentBlock,
...(i === message.message.content.length - 1 &&
contentBlock.type !== 'thinking' &&
contentBlock.type !== 'redacted_thinking' &&
(feature('CONNECTOR_TEXT')
? !isConnectorTextBlock(contentBlock)
: true)
? enablePromptCaching
? { cache_control: getCacheControl({ querySource }) }
: {}
: {}),
}
}),
}
}
}
return {
role: 'assistant',
content: message.message.content,
content:
typeof message.message.content === 'string'
? message.message.content
: message.message.content.map(stripGeminiProviderMetadata),
}
}
function stripGeminiProviderMetadata<T extends BetaContentBlockParam | string>(
contentBlock: T,
): T {
if (
typeof contentBlock === 'string' ||
!('_geminiThoughtSignature' in contentBlock)
) {
return contentBlock
}
const {
_geminiThoughtSignature: _unusedGeminiThoughtSignature,
...rest
} = contentBlock as T & {
_geminiThoughtSignature?: string
}
return rest as T
}
export type Options = {
getToolPermissionContext: () => Promise<ToolPermissionContext>
model: string
@@ -1310,6 +1337,19 @@ async function* queryModel(
return
}
if (getAPIProvider() === 'gemini') {
const { queryModelGemini } = await import('./gemini/index.js')
yield* queryModelGemini(
messagesForAPI,
systemPrompt,
filteredTools,
signal,
options,
thinkingConfig,
)
return
}
// Instrumentation: Track message count after normalization
logEvent('tengu_api_after_normalize', {
postNormalizedMessageCount: messagesForAPI.length,