🤯 LobeHub - Super-Agents on Your Team. Add super-agents to the chats your team already uses.They take on work by themselves and get better every week.
TypeScript agentagent-collaborationagent-harnessloop-engineeringmcppersonal-assistantproactive-agentskillssuper-agenttask-management
🐛 fix(tts): bill gateway MiniMax speech models by MiniMax characters (#22844)
Rdmclin2c0141747898b
diff --git a/packages/model-runtime/src/core/openaiCompatible/textToSpeech.test.ts b/packages/model-runtime/src/core/openaiCompatible/textToSpeech.test.tsindex fd6e3273b2c..45f8eddd6cf 100644--- a/packages/model-runtime/src/core/openaiCompatible/textToSpeech.test.ts+++ b/packages/model-runtime/src/core/openaiCompatible/textToSpeech.test.ts@@ -100,6 +100,60 @@ describe('createOpenAICompatibleSpeech', () => {expect(create.mock.calls[0][0].model).toBe('openai/tts-1');});+ it('bills a gateway MiniMax speech model by MiniMax characters (a Chinese character is 2)', async () => {+ create.mockResolvedValue(audioResponse([0]));++ const result = await createOpenAICompatibleSpeech(+ client,+ { input: '你好, hi', model: 'speech-2.8-hd', voice: 'female-shaonv' },+ undefined,+ { ...context, provider: 'lobehub', requestModel: 'minimax/speech-2.8-hd' },+ );++ // 2 Han × 2 + ", hi" (4) = 8 billed characters+ expect(result.usage?.inputCharacters).toBe(8);+ });++ it('recognizes a MiniMax speech model by its gateway id alone', async () => {+ create.mockResolvedValue(audioResponse([0]));++ const result = await createOpenAICompatibleSpeech(+ client,+ { input: '你好', model: 'custom-voice', voice: 'female-shaonv' },+ undefined,+ { ...context, requestModel: 'minimax/speech-2.8-turbo' },+ );++ expect(result.usage?.inputCharacters).toBe(4);+ });++ it('leaves a token-billed MiniMax speech model without token counts unpriced', async () => {+ vi.mocked(getModelPricing).mockResolvedValue(tokenPricing);+ create.mockResolvedValue(audioResponse([0]));++ const result = await createOpenAICompatibleSpeech(+ client,+ { input: '你好', model: 'speech-2.8-hd', voice: 'female-shaonv' },+ undefined,+ { ...context, requestModel: 'minimax/speech-2.8-hd' },+ );++ expect(result.usage).toEqual({ inputCharacters: 2 });+ });++ it('keeps counting code points for non-MiniMax models', async () => {+ create.mockResolvedValue(audioResponse([0]));++ const result = await createOpenAICompatibleSpeech(+ client,+ { input: '你好', model: 'tts-1', voice: 'alloy' },+ undefined,+ context,+ );++ expect(result.usage?.inputCharacters).toBe(2);+ });+it('requests SSE for token-billed models and reads audio and token usage from events', async () => {vi.mocked(getModelPricing).mockResolvedValue(tokenPricing);create.mockResolvedValue(diff --git a/packages/model-runtime/src/core/openaiCompatible/textToSpeech.ts b/packages/model-runtime/src/core/openaiCompatible/textToSpeech.tsindex 5d34a698ba9..81bfe79e25c 100644--- a/packages/model-runtime/src/core/openaiCompatible/textToSpeech.ts+++ b/packages/model-runtime/src/core/openaiCompatible/textToSpeech.ts@@ -15,6 +15,8 @@ import { getModelPricing } from '../../utils/getModelPricing';import {convertOpenAISpeechTokenUsage,convertTextToSpeechUsage,+ countMiniMaxSpeechCharacters,+ isMiniMaxSpeechModel,isTokenBilledTextToSpeech,} from '../usageConverters/tts';@@ -130,7 +132,17 @@ export const createOpenAICompatibleSpeech = async (log('generated audio with size: %d bytes', result.audio.byteLength);- const usage = convertTextToSpeechUsage(payload.input, pricing, result.tokenUsage);+ // Gateways bill MiniMax speech models by MiniMax's own count (a Chinese character is 2).+ const billedCharacters =+ !streamUsage &&+ (isMiniMaxSpeechModel(payload.model) || isMiniMaxSpeechModel(context.requestModel))+ ? { inputCharacters: countMiniMaxSpeechCharacters(payload.input) }+ : undefined;+ const usage = convertTextToSpeechUsage(+ payload.input,+ pricing,+ billedCharacters ? { ...billedCharacters, ...result.tokenUsage } : result.tokenUsage,+ );await options?.onUsage?.(usage);return { audio: result.audio, format, usage };diff --git a/packages/model-runtime/src/core/usageConverters/index.ts b/packages/model-runtime/src/core/usageConverters/index.tsindex 625f4ac0288..b08d04fff8e 100644--- a/packages/model-runtime/src/core/usageConverters/index.ts+++ b/packages/model-runtime/src/core/usageConverters/index.ts@@ -8,7 +8,9 @@ export {export {convertOpenAISpeechTokenUsage,convertTextToSpeechUsage,+ countMiniMaxSpeechCharacters,countTextToSpeechCharacters,+ isMiniMaxSpeechModel,isTokenBilledTextToSpeech,} from './tts';export {diff --git a/packages/model-runtime/src/core/usageConverters/tts.test.ts b/packages/model-runtime/src/core/usageConverters/tts.test.tsindex 64b6c4d093c..813fd3a8fde 100644--- a/packages/model-runtime/src/core/usageConverters/tts.test.ts+++ b/packages/model-runtime/src/core/usageConverters/tts.test.ts@@ -5,7 +5,9 @@ import { describe, expect, it } from 'vitest';import {convertOpenAISpeechTokenUsage,convertTextToSpeechUsage,+ countMiniMaxSpeechCharacters,countTextToSpeechCharacters,+ isMiniMaxSpeechModel,isTokenBilledTextToSpeech,} from './tts';@@ -87,3 +89,21 @@ describe('convertOpenAISpeechTokenUsage', () => {expect(convertOpenAISpeechTokenUsage({ input_tokens: 3 })).toBeUndefined();});});++describe('countMiniMaxSpeechCharacters', () => {+ it('counts a Chinese character as 2 and everything else as 1', () => {+ expect(countMiniMaxSpeechCharacters('你好,世界')).toBe(9);+ expect(countMiniMaxSpeechCharacters('hello')).toBe(5);+ expect(countMiniMaxSpeechCharacters('hi 👋')).toBe(4);+ });+});++describe('isMiniMaxSpeechModel', () => {+ it('matches MiniMax speech catalog and gateway ids only', () => {+ expect(isMiniMaxSpeechModel('speech-2.8-hd')).toBe(true);+ expect(isMiniMaxSpeechModel('minimax/speech-2.8-turbo')).toBe(true);+ expect(isMiniMaxSpeechModel('tts-1')).toBe(false);+ expect(isMiniMaxSpeechModel('openai/gpt-4o-mini-tts')).toBe(false);+ expect(isMiniMaxSpeechModel(undefined)).toBe(false);+ });+});diff --git a/packages/model-runtime/src/core/usageConverters/tts.ts b/packages/model-runtime/src/core/usageConverters/tts.tsindex 3a3041c6afb..0ee04aaa959 100644--- a/packages/model-runtime/src/core/usageConverters/tts.ts+++ b/packages/model-runtime/src/core/usageConverters/tts.ts@@ -12,6 +12,26 @@ const log = debug('lobe-cost:convertTextToSpeechUsage');*/export const countTextToSpeechCharacters = (input: string) => [...input].length;+const HAN_PATTERN = /\p{Script=Han}/u;++/**+ * Billed characters of a MiniMax speech model: a Chinese character counts as 2, every+ * other character (letters, punctuation, spaces, emoji) as 1. Gateways that resell these+ * models (e.g. OpenRouter `minimax/speech-2.8-hd`) bill the same count.+ * @see https://platform.minimaxi.com/docs/guides/pricing-paygo+ */+export const countMiniMaxSpeechCharacters = (input: string) =>+ [...input].reduce((count, char) => count + (HAN_PATTERN.test(char) ? 2 : 1), 0);++const MINIMAX_SPEECH_MODEL_PATTERN = /^(?:minimax\/)?speech-/i;++/**+ * Whether a model id names a MiniMax speech model, either the catalog id+ * (`speech-2.8-hd`) or a gateway id (`minimax/speech-2.8-hd`).+ */+export const isMiniMaxSpeechModel = (model?: string) =>+ !!model && MINIMAX_SPEECH_MODEL_PATTERN.test(model);+/*** Whether the model is billed per token (e.g. `gpt-4o-mini-tts`) rather than per input* character (e.g. `tts-1`). Token-billed models need provider-reported token usage.diff --git a/packages/model-runtime/src/providers/minimax/createSpeech.test.ts b/packages/model-runtime/src/providers/minimax/createSpeech.test.tsindex 11376a9024c..e589245cfd7 100644--- a/packages/model-runtime/src/providers/minimax/createSpeech.test.ts+++ b/packages/model-runtime/src/providers/minimax/createSpeech.test.ts@@ -4,10 +4,10 @@ import type { Pricing } from 'model-bank';import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';import { USD_TO_CNY } from '../../const/currency';+import { countMiniMaxSpeechCharacters } from '../../core/usageConverters/tts';import { getModelPricing } from '../../utils/getModelPricing';import {buildMiniMaxSpeechRequest,- countMiniMaxSpeechCharacters,createMiniMaxSpeech,decodeMiniMaxHexAudio,getMiniMaxSpeechErrorType,diff --git a/packages/model-runtime/src/providers/minimax/createSpeech.ts b/packages/model-runtime/src/providers/minimax/createSpeech.tsindex 306092812b7..d70dde37c2a 100644--- a/packages/model-runtime/src/providers/minimax/createSpeech.ts+++ b/packages/model-runtime/src/providers/minimax/createSpeech.ts@@ -4,7 +4,10 @@ import type { TTSAudioFormat } from 'model-bank';import { DEFAULT_TTS_AUDIO_FORMAT } from 'model-bank';import type { LobeRuntimeAI } from '../../core/BaseAI';-import { convertTextToSpeechUsage } from '../../core/usageConverters/tts';+import {+ convertTextToSpeechUsage,+ countMiniMaxSpeechCharacters,+} from '../../core/usageConverters/tts';import type { ILobeAgentRuntimeErrorType } from '../../types/error';import type {TextToSpeechOptions,@@ -39,16 +42,6 @@ const MINIMAX_AUDIO_FORMATS: Partial<Record<TTSAudioFormat, string>> = {const HEX_PATTERN = /^(?:[\da-f]{2})+$/i;-const HAN_PATTERN = /\p{Script=Han}/u;--/**- * Billed characters when MiniMax omits `usage_characters`: a Chinese character counts- * as 2, every other character (letters, punctuation, spaces, emoji) as 1.- * @see https://platform.minimaxi.com/docs/guides/pricing-paygo- */-export const countMiniMaxSpeechCharacters = (input: string) =>- [...input].reduce((count, char) => count + (HAN_PATTERN.test(char) ? 2 : 1), 0);-/*** `base_resp.status_code` values that map onto a specific runtime error type; everything* else is a provider business error.