Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 0 additions & 4 deletions src/api/providers/__tests__/minimax.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,10 @@ import { MiniMaxHandler } from "../minimax"

vitest.mock("@anthropic-ai/sdk", () => {
const mockCreate = vitest.fn()
const mockCountTokens = vitest.fn()
return {
Anthropic: vitest.fn(() => ({
messages: {
create: mockCreate,
countTokens: mockCountTokens,
},
})),
}
Expand All @@ -30,13 +28,11 @@ vitest.mock("@anthropic-ai/sdk", () => {
describe("MiniMaxHandler", () => {
let handler: MiniMaxHandler
let mockCreate: any
let mockCountTokens: any

beforeEach(() => {
vitest.clearAllMocks()
const anthropicInstance = (Anthropic as unknown as any)()
mockCreate = anthropicInstance.messages.create
mockCountTokens = anthropicInstance.messages.countTokens
})

describe("International MiniMax (default)", () => {
Expand Down
26 changes: 0 additions & 26 deletions src/api/providers/anthropic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -402,30 +402,4 @@ export class AnthropicHandler extends BaseProvider implements SingleCompletionHa
const content = message.content.find(({ type }) => type === "text")
return content?.type === "text" ? content.text : ""
}

/**
* Counts tokens for the given content using Anthropic's API
*
* @param content The content blocks to count tokens for
* @returns A promise resolving to the token count
*/
override async countTokens(content: Array<Anthropic.Messages.ContentBlockParam>): Promise<number> {
try {
// Use the current model
const { id: model } = this.getModel()

const response = await this.client.messages.countTokens({
model,
messages: [{ role: "user", content: content }],
})

return response.input_tokens
} catch (error) {
// Log error but fallback to tiktoken estimation
console.warn("Anthropic token counting failed, using fallback", error)

// Use the base provider's implementation as fallback
return super.countTokens(content)
}
}
}
27 changes: 1 addition & 26 deletions src/api/providers/gemini.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import {
type GenerateContentConfig,
type GroundingMetadata,
FunctionCallingConfigMode,
Content,
} from "@google/genai"
import type { JWTInput } from "google-auth-library"

Expand All @@ -15,7 +14,7 @@ import { type ModelInfo, type GeminiModelId, geminiDefaultModelId, geminiModels
import type { ApiHandlerOptions } from "../../shared/api"
import { safeJsonParse } from "../../shared/safeJsonParse"

import { convertAnthropicContentToGemini, convertAnthropicMessageToGemini } from "../transform/gemini-format"
import { convertAnthropicMessageToGemini } from "../transform/gemini-format"
import { t } from "i18next"
import type { ApiStream, GroundingSource } from "../transform/stream"
import { getModelParams } from "../transform/model-params"
Expand Down Expand Up @@ -431,30 +430,6 @@ export class GeminiHandler extends BaseProvider implements SingleCompletionHandl
}
}

override async countTokens(content: Array<Anthropic.Messages.ContentBlockParam>): Promise<number> {
try {
const { id: model } = this.getModel()

const countTokensRequest = {
model,
// Token counting does not need encrypted continuation; always drop thoughtSignature.
contents: convertAnthropicContentToGemini(content, { includeThoughtSignatures: false }),
}

const response = await this.client.models.countTokens(countTokensRequest)

if (response.totalTokens === undefined) {
console.warn("Gemini token counting returned undefined, using fallback")
return super.countTokens(content)
}

return response.totalTokens
} catch (error) {
console.warn("Gemini token counting failed, using fallback", error)
return super.countTokens(content)
}
}

public getThoughtSignature(): string | undefined {
return this.lastThoughtSignature
}
Expand Down
23 changes: 0 additions & 23 deletions src/api/providers/minimax.ts
Original file line number Diff line number Diff line change
Expand Up @@ -303,27 +303,4 @@ export class MiniMaxHandler extends BaseProvider implements SingleCompletionHand
const content = message.content.find(({ type }) => type === "text")
return content?.type === "text" ? content.text : ""
}

/**
* Counts tokens for the given content using Anthropic's token counting
* Falls back to base provider's tiktoken estimation if counting fails
*/
override async countTokens(content: Array<Anthropic.Messages.ContentBlockParam>): Promise<number> {
try {
const { id: model } = this.getModel()

const response = await this.client.messages.countTokens({
model,
messages: [{ role: "user", content: content }],
})

return response.input_tokens
} catch (error) {
// Log error but fallback to tiktoken estimation
console.warn("MiniMax token counting failed, using fallback", error)

// Use the base provider's implementation as fallback
return super.countTokens(content)
}
}
}
Loading