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

@@ -143,11 +143,16 @@ export async function countMessagesTokensWithAPI(
): Promise<number | null> {
return withTokenCountVCR(messages, tools, async () => {
try {
const provider = getAPIProvider()
if (provider === 'gemini') {
return roughTokenCountEstimationForAPIRequest(messages, tools)
}
const model = getMainLoopModel()
const betas = getModelBetas(model)
const containsThinking = hasThinkingBlocks(messages)
if (getAPIProvider() === 'bedrock') {
if (provider === 'bedrock') {
// @anthropic-sdk/bedrock-sdk doesn't support countTokens currently
return countTokensWithBedrock({
model: normalizeModelStringForAPI(model),
@@ -252,6 +257,11 @@ export async function countTokensViaHaikuFallback(
messages: Anthropic.Beta.Messages.BetaMessageParam[],
tools: Anthropic.Beta.Messages.BetaToolUnion[],
): Promise<number | null> {
const provider = getAPIProvider()
if (provider === 'gemini') {
return roughTokenCountEstimationForAPIRequest(messages, tools)
}
// Check if messages contain thinking blocks
const containsThinking = hasThinkingBlocks(messages)
@@ -388,6 +398,29 @@ function roughTokenCountEstimationForContent(
return totalTokens
}
function roughTokenCountEstimationForAPIRequest(
messages: Anthropic.Beta.Messages.BetaMessageParam[],
tools: Anthropic.Beta.Messages.BetaToolUnion[],
): number {
let totalTokens = 0
for (const message of messages) {
totalTokens += roughTokenCountEstimationForContent(
message.content as
| string
| Array<Anthropic.ContentBlock>
| Array<Anthropic.ContentBlockParam>
| undefined,
)
}
if (tools.length > 0) {
totalTokens += roughTokenCountEstimation(jsonStringify(tools))
}
return totalTokens
}
function roughTokenCountEstimationForBlock(
block: string | Anthropic.ContentBlock | Anthropic.ContentBlockParam,
): number {