Skip to content
Open
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
2 changes: 2 additions & 0 deletions packages/app/server/src/env.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ export const env = createEnv({
GROQ_API_KEY: z.string().optional(),
XAI_API_KEY: z.string().optional(),
OPENROUTER_API_KEY: z.string().optional(),
VERCEL_GATEWAY_API_KEY: z.string().optional(),
VERCEL_GATEWAY_BASE_URL: z.string().url().optional(),
TAVILY_API_KEY: z.string().optional(),
E2B_API_KEY: z.string().optional(),
GOOGLE_SERVICE_ACCOUNT_KEY_ENCODED: z.string().optional(),
Expand Down
6 changes: 6 additions & 0 deletions packages/app/server/src/providers/ProviderFactory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import { OpenAIResponsesProvider } from './OpenAIResponsesProvider';
import { OpenRouterProvider } from './OpenRouterProvider';
import { ProviderType } from './ProviderType';
import { XAIProvider } from './XAIProvider';
import { VercelGatewayProvider } from './VercelGatewayProvider';
import {
VertexAIProvider,
PROXY_PASSTHROUGH_ONLY_MODEL as VertexAIProxyPassthroughOnlyModel,
Expand Down Expand Up @@ -58,6 +59,9 @@ const createChatModelToProviderMapping = (): Record<string, ProviderType> => {
case 'Xai':
mapping[modelConfig.model_id] = ProviderType.XAI;
break;
case 'VercelGateway':
mapping[modelConfig.model_id] = ProviderType.VERCEL_GATEWAY;
break;
// Add other providers as needed
default:
// Skip models with unsupported providers
Expand Down Expand Up @@ -192,6 +196,8 @@ export const getProvider = (
return new GroqProvider(stream, model);
case ProviderType.XAI:
return new XAIProvider(stream, model);
case ProviderType.VERCEL_GATEWAY:
return new VercelGatewayProvider(stream, model);
default:
throw new Error(`Unknown provider type: ${type}`);
}
Expand Down
1 change: 1 addition & 0 deletions packages/app/server/src/providers/ProviderType.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,5 @@ export enum ProviderType {
OPENAI_VIDEOS = 'OPENAI_VIDEOS',
GROQ = 'GROQ',
XAI = 'XAI',
VERCEL_GATEWAY = 'VERCEL_GATEWAY',
}
117 changes: 117 additions & 0 deletions packages/app/server/src/providers/VercelGatewayProvider.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
import { LlmTransactionMetadata, Transaction } from '../types';
import { getCostPerToken } from '../services/AccountingService';
import { BaseProvider } from './BaseProvider';
import { ProviderType } from './ProviderType';
import logger from '../logger';
import { env } from '../env';

interface CompletionStateBody {
id: string;
usage: {
prompt_tokens: number;
completion_tokens: number;
total_tokens: number;
};
}

interface StreamingChunkBody {
id: string;
choices: {
index: number;
delta: {
content?: string;
};
finish_reason: string | null;
}[];
usage: {
prompt_tokens: number;
completion_tokens: number;
total_tokens: number;
} | null;
}

const parseSSEGPTFormat = (data: string): StreamingChunkBody[] => {
const events = data.split('\n\n');
const chunks: StreamingChunkBody[] = [];

for (const event of events) {
if (!event.trim()) continue;
if (event.startsWith('data: ')) {
const jsonStr = event.slice(6);
if (jsonStr.trim() === '[DONE]') continue;
try {
const parsed = JSON.parse(jsonStr);
chunks.push(parsed);
} catch (error) {
logger.error(`Error parsing SSE chunk: ${error}`);
}
}
}

return chunks;
};

export class VercelGatewayProvider extends BaseProvider {
getType(): ProviderType {
return ProviderType.VERCEL_GATEWAY;
}

getBaseUrl(): string {
return env.VERCEL_GATEWAY_BASE_URL || 'https://ai-gateway.vercel.sh/v1/ai';
}

getApiKey(): string | undefined {
return env.VERCEL_GATEWAY_API_KEY;
}

async handleBody(data: string): Promise<Transaction> {
try {
let prompt_tokens = 0;
let completion_tokens = 0;
let total_tokens = 0;
let providerId = 'null';

if (this.getIsStream()) {
const chunks = parseSSEGPTFormat(data);
for (const chunk of chunks) {
if (chunk.usage !== null) {
prompt_tokens += chunk.usage.prompt_tokens;
completion_tokens += chunk.usage.completion_tokens;
total_tokens += chunk.usage.total_tokens;
}
providerId = chunk.id || 'null';
}
} else {
const parsed = JSON.parse(data) as CompletionStateBody;
prompt_tokens += parsed.usage.prompt_tokens;
completion_tokens += parsed.usage.completion_tokens;
total_tokens += parsed.usage.total_tokens;
providerId = parsed.id || 'null';
}

const cost = getCostPerToken(
this.getModel(),
prompt_tokens,
completion_tokens
);

const metadata: LlmTransactionMetadata = {
providerId: providerId,
provider: this.getType(),
model: this.getModel(),
inputTokens: prompt_tokens,
outputTokens: completion_tokens,
totalTokens: total_tokens,
};

return {
rawTransactionCost: cost,
metadata: metadata,
status: 'success',
};
} catch (error) {
logger.error(`Error processing data: ${error}`);
throw error;
}
}
}
2 changes: 2 additions & 0 deletions packages/app/server/src/services/AccountingService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
SupportedImageModel,
SupportedVideoModel,
XAIModels,
VercelGatewayModels,
} from '@merit-systems/echo-typescript-sdk';

import { Decimal } from '@prisma/client/runtime/library';
Expand All @@ -30,6 +31,7 @@ export const ALL_SUPPORTED_MODELS: SupportedModel[] = [
...OpenRouterModels,
...GroqModels,
...XAIModels,
...VercelGatewayModels,
];

// Handle image models separately since they have different pricing structure
Expand Down
1 change: 1 addition & 0 deletions packages/sdk/next/src/ai-providers/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ export * from './google';
export * from './xai';
export * from './groq';
export * from './openai';
export * from './vercel';
10 changes: 10 additions & 0 deletions packages/sdk/next/src/ai-providers/vercel.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { getEchoToken } from '../auth/token-manager';
import {
createEchoVercelGateway as createEchoVercelGatewayBase,
EchoConfig,
GatewayProvider,
} from '@merit-systems/echo-typescript-sdk';

export function createEchoVercelGateway(config: EchoConfig): GatewayProvider {
return createEchoVercelGatewayBase(config, async () => getEchoToken(config));
}
1 change: 1 addition & 0 deletions packages/sdk/ts/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@
"@ai-sdk/openai": "2.0.32",
"@ai-sdk/xai": "2.0.16",
"@openrouter/ai-sdk-provider": "1.2.0",
"@ai-sdk/gateway": "^1.0.12",
"ai": "5.0.47"
}
}
45 changes: 45 additions & 0 deletions packages/sdk/ts/src/__tests__/vercel-gateway-models.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import { describe, it, expect } from 'vitest';
import { VercelGatewayModels } from '../src/supported-models/chat/vercel-gateway';

describe('VercelGatewayModels', () => {
it('should export a non-empty array of models', () => {
expect(VercelGatewayModels.length).toBeGreaterThan(0);
});

it('should have all models with VercelGateway provider', () => {
for (const model of VercelGatewayModels) {
expect(model.provider).toBe('VercelGateway');
}
});

it('should have valid pricing for all models', () => {
for (const model of VercelGatewayModels) {
expect(model.input_cost_per_token).toBeGreaterThan(0);
expect(model.output_cost_per_token).toBeGreaterThan(0);
}
});

it('should have prefixed model IDs (provider/model-name)', () => {
for (const model of VercelGatewayModels) {
expect(model.model_id).toMatch(/^[a-z]+\/[a-z0-9._-]+$/);
}
});

it('should include key OpenAI models', () => {
const ids = VercelGatewayModels.map(m => m.model_id);
expect(ids).toContain('openai/gpt-4o');
expect(ids).toContain('openai/gpt-4o-mini');
});

it('should include key Anthropic models', () => {
const ids = VercelGatewayModels.map(m => m.model_id);
expect(ids).toContain('anthropic/claude-sonnet-4');
expect(ids).toContain('anthropic/claude-3.5-sonnet');
});

it('should include key Google models', () => {
const ids = VercelGatewayModels.map(m => m.model_id);
expect(ids).toContain('google/gemini-2.5-flash');
expect(ids).toContain('google/gemini-2.5-pro');
});
});
66 changes: 66 additions & 0 deletions packages/sdk/ts/src/__tests__/vercel-gateway.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
import { describe, it, expect, vi } from 'vitest';
import { createEchoVercelGateway } from '../src/providers/vercel';
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Test files have incorrect import paths with extra src prefix that would cause module resolution failures.

Fix on Vercel

import { echoFetch } from '../src/providers/index';

// Mock @ai-sdk/gateway
vi.mock('@ai-sdk/gateway', () => ({
createGatewayProvider: vi.fn((options) => ({
_type: 'gateway',
_options: options,
languageModel: vi.fn(),
getAvailableModels: vi.fn(),
getCredits: vi.fn(),
textEmbeddingModel: vi.fn(),
})),
}));

describe('createEchoVercelGateway', () => {
const mockGetTokenFn = vi.fn();
const mockOnInsufficientFunds = vi.fn();

it('should create a gateway provider with echo fetch wrapper', () => {
const provider = createEchoVercelGateway(
{ appId: '60601628-cdb7-481e-8f7e-921981220348' },
mockGetTokenFn,
mockOnInsufficientFunds
);

expect(provider._type).toBe('gateway');
expect(provider._options.apiKey).toBe('placeholder_replaced_by_echoFetch');
expect(provider._options.baseURL).toBe('https://echo.router.merit.systems');
expect(provider._options.fetch).toBeDefined();
});

it('should use custom baseRouterUrl when provided', () => {
const provider = createEchoVercelGateway(
{
appId: '60601628-cdb7-481e-8f7e-921981220348',
baseRouterUrl: 'https://custom-gateway.example.com',
},
mockGetTokenFn,
mockOnInsufficientFunds
);

expect(provider._options.baseURL).toBe('https://custom-gateway.example.com');
});

it('should throw on invalid appId', () => {
expect(() =>
createEchoVercelGateway(
{ appId: 'invalid' },
mockGetTokenFn,
mockOnInsufficientFunds
)
).toThrow('Invalid Echo App ID');
});

it('should throw on empty appId', () => {
expect(() =>
createEchoVercelGateway(
{ appId: '' },
mockGetTokenFn,
mockOnInsufficientFunds
)
).toThrow('Invalid Echo App ID');
});
});
2 changes: 2 additions & 0 deletions packages/sdk/ts/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ export * from './utils/error-handling';
export * from './utils/validation';
export * from './providers';
export { createEchoXAI } from './providers/xai';
export { createEchoVercelGateway } from './providers/vercel';

// Export tool-related types and utilities
export type {
Expand Down Expand Up @@ -48,6 +49,7 @@ export type { OpenRouterModel } from './supported-models/chat/openrouter';
export { GroqModels } from './supported-models/chat/groq';
export type { GroqModel } from './supported-models/chat/groq';
export { XAIModels } from './supported-models/chat/xai';
export { VercelGatewayModels } from './supported-models/chat/vercel-gateway';
export type { XAIModel } from './supported-models/chat/xai';
export { OpenAIImageModels } from './supported-models/image/openai';
export type { OpenAIImageModel } from './supported-models/image/openai';
Expand Down
2 changes: 2 additions & 0 deletions packages/sdk/ts/src/providers/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ export * from './groq';
export * from './xai';
export * from './openai';
export * from './openrouter';
export * from './vercel';

export function echoFetch(
originalFetch: typeof fetch,
Expand Down Expand Up @@ -63,3 +64,4 @@ export { type GroqProvider } from '@ai-sdk/groq';
export { type OpenAIProvider } from '@ai-sdk/openai';
export { type OpenRouterProvider } from '@openrouter/ai-sdk-provider';
export { type XaiProvider } from '@ai-sdk/xai';
export { type GatewayProvider } from '@ai-sdk/gateway';
26 changes: 26 additions & 0 deletions packages/sdk/ts/src/providers/vercel.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import {
createGatewayProvider,
GatewayProvider,
} from '@ai-sdk/gateway';
import { ROUTER_BASE_URL } from 'config';
import { EchoConfig } from '../types';
import { validateAppId } from '../utils/validation';
import { echoFetch } from './index';

export function createEchoVercelGateway(
{ appId, baseRouterUrl = ROUTER_BASE_URL }: EchoConfig,
getTokenFn: (appId: string) => Promise<string | null>,
onInsufficientFunds?: () => void
): GatewayProvider {
validateAppId(appId, 'createEchoVercelGateway');

return createGatewayProvider({
baseURL: baseRouterUrl,
apiKey: 'placeholder_replaced_by_echoFetch',
fetch: echoFetch(
fetch,
async () => await getTokenFn(appId),
onInsufficientFunds
),
});
}
Loading