Skip to content

🤯 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.

🐛 fix(model-runtime): price router Vertex AI channels with the router card (#23498)

YuTengjingdc1753fbee5f
diff --git a/packages/model-runtime/src/core/RouterRuntime/optionRuntime.test.ts b/packages/model-runtime/src/core/RouterRuntime/optionRuntime.test.ts
new file mode 100644
index 00000000000..d79b527d178
--- /dev/null
+++ b/packages/model-runtime/src/core/RouterRuntime/optionRuntime.test.ts
@@ -0,0 +1,46 @@
+import { describe, expect, it, vi } from 'vitest';
+
+import type { LobeGoogleAI } from '../../providers/google';
+import { LobeVertexAI } from '../../providers/vertexai';
+import { createRuntimeFromOption } from './optionRuntime';
+import type { RouterRuntimeDeps } from './routeSelection';
+
+const createDeps = (id: string): RouterRuntimeDeps => ({
+ id,
+ options: {},
+ params: {},
+ routers: [],
+ trace: vi.fn() as unknown as RouterRuntimeDeps['trace'],
+});
+
+describe('createRuntimeFromOption', () => {
+ it('prices Vertex AI channels with the router provider id, like other channels', async () => {
+ const credentials = JSON.stringify({
+ client_email: '[email protected]',
+ private_key: 'key',
+ type: 'service_account',
+ });
+
+ const { runtime } = await createRuntimeFromOption(
+ createDeps('lobehub'),
+ { apiType: 'google', id: 'gemini', options: [] },
+ { apiKey: credentials, apiType: 'vertexai', id: 'vertexai', project: 'project' },
+ );
+ const { runtime: googleRuntime } = await createRuntimeFromOption(
+ createDeps('lobehub'),
+ { apiType: 'google', id: 'gemini', options: [] },
+ { apiKey: 'google-key', apiType: 'google', id: 'google' },
+ );
+
+ // `provider` selects the pricing card in getModelPricing, so a Vertex channel must not fall
+ // back to the static `vertexai` card (official price) while other channels use the router's.
+ expect((runtime as LobeGoogleAI).provider).toBe('lobehub');
+ expect((googleRuntime as LobeGoogleAI).provider).toBe('lobehub');
+ });
+
+ it('keeps the vertexai provider id for standalone Vertex AI runtimes', () => {
+ const runtime = LobeVertexAI.initFromVertexAI({ project: 'project' });
+
+ expect(runtime.provider).toBe('vertexai');
+ });
+});
diff --git a/packages/model-runtime/src/core/RouterRuntime/optionRuntime.ts b/packages/model-runtime/src/core/RouterRuntime/optionRuntime.ts
index 49981aeb82f..0d4bf12a926 100644
--- a/packages/model-runtime/src/core/RouterRuntime/optionRuntime.ts
+++ b/packages/model-runtime/src/core/RouterRuntime/optionRuntime.ts
@@ -64,7 +64,9 @@ export const createRuntimeFromOption = async (
getDurationMs(startedAt),
);
- const runtime = LobeVertexAI.initFromVertexAI(vertexOptions);
+ // Report the router's provider id like the other channels below: getModelPricing picks the
+ // card by provider, and `vertexai` would bill the static Vertex card instead of the router's.
+ const runtime = LobeVertexAI.initFromVertexAI({ ...vertexOptions, id: deps.id });
setRuntimeSignatureScopeSource(runtime, signatureScopeSource);
return {
diff --git a/packages/model-runtime/src/providers/vertexai/index.ts b/packages/model-runtime/src/providers/vertexai/index.ts
index 36e13973697..90333ed38fa 100644
--- a/packages/model-runtime/src/providers/vertexai/index.ts
+++ b/packages/model-runtime/src/providers/vertexai/index.ts
@@ -7,12 +7,16 @@ import type { ModelIdMappingOptions } from '../../utils/modelIdMapping';
import { LobeGoogleAI } from '../google';
const DEFAULT_VERTEXAI_LOCATION = 'global';
-type VertexAIInitOptions = GoogleGenAIOptions & ModelIdMappingOptions;
+type VertexAIInitOptions = GoogleGenAIOptions &
+ ModelIdMappingOptions & {
+ /** Provider id the runtime reports and prices with; defaults to `vertexai`. */
+ id?: string;
+ };
export class LobeVertexAI extends LobeGoogleAI {
static initFromVertexAI(params?: VertexAIInitOptions) {
try {
- const { modelIdMapping, ...googleOptions } = params ?? {};
+ const { id, modelIdMapping, ...googleOptions } = params ?? {};
const client = new GoogleGenAI({
...googleOptions,
location: googleOptions.location ?? DEFAULT_VERTEXAI_LOCATION, // @google/genai throws an error if location is not provided
@@ -22,6 +26,7 @@ export class LobeVertexAI extends LobeGoogleAI {
return new LobeGoogleAI({
apiKey: 'avoid-error',
client,
+ id,
isVertexAi: true,
modelIdMapping,
});