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
5 changes: 3 additions & 2 deletions app/components/AiConfigForm.vue
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,8 @@ function pickProvider(key: string) {
const canSave = computed(() => {
if (!form.value.name.trim()) return false
if (!form.value.model.trim()) return false
if (!isEdit.value && !form.value.apiKey) return false
// API key required for cloud providers, but optional for custom endpoints (Ollama, etc.)
if (!isEdit.value && !form.value.apiKey && !isCustomProvider.value) return false
if (isCustomProvider.value && !form.value.baseUrl) return false
return true
})
Expand All @@ -148,7 +149,7 @@ async function handleSave() {
outputPricePer1m: form.value.outputPricePer1m,
}
if (isCustomProvider.value) body.baseUrl = form.value.baseUrl
if (form.value.apiKey) body.apiKey = form.value.apiKey
if (form.value.apiKey !== undefined) body.apiKey = form.value.apiKey

if (isEdit.value && props.config) {
await $fetch(`/api/ai-config/${props.config.id}`, {
Expand Down
34 changes: 34 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
"dependencies": {
"@ai-sdk/anthropic": "^3.0.74",
"@ai-sdk/google": "^3.0.67",
"@ai-sdk/mistral": "^3.0.40",
"@ai-sdk/openai": "^3.0.58",
"@aws-sdk/client-s3": "^3.1041.0",
"@better-auth/sso": "^1.6.16",
Expand Down
30 changes: 26 additions & 4 deletions server/utils/ai/provider.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
/**
* AI Provider Abstraction Layer
*
* Supports OpenAI, Anthropic, and custom OpenAI-compatible endpoints.
* Supports OpenAI, Anthropic, Google, Mistral, and custom OpenAI-compatible endpoints.
* Credentials are decrypted per-request from the organization's AI config.
* Never logs or stores raw API keys — only encrypted values in the database.
*/
import { createOpenAI } from '@ai-sdk/openai'
import { createAnthropic } from '@ai-sdk/anthropic'
import { createGoogleGenerativeAI } from '@ai-sdk/google'
import { createMistral } from '@ai-sdk/mistral'
import { generateObject } from 'ai'
import type { z } from 'zod'
import { decrypt } from '../encryption'

export type SupportedProvider = 'openai' | 'anthropic' | 'google' | 'openai_compatible'
export type SupportedProvider = 'openai' | 'anthropic' | 'google' | 'mistral' | 'openai_compatible'

export interface ProviderConfig {
provider: SupportedProvider
Expand Down Expand Up @@ -99,6 +100,20 @@ export const PROVIDER_REGISTRY: Record<string, {
{ id: 'gemini-2.0-flash-lite', label: 'Gemini 2.0 Flash Lite', description: 'Cheapest Gemini option for high-volume light tasks.', inputPricePer1m: 0.075, outputPricePer1m: 0.3, badge: 'cheap' },
],
},
mistral: {
name: 'Mistral AI',
tagline: 'Open-source and enterprise LLMs. European-based, privacy-focused.',
modelsUrl: 'https://docs.mistral.ai/api/#tag/models',
apiKeyUrl: 'https://console.mistral.ai/api-keys',
signupUrl: 'https://console.mistral.ai/',
supportsBaseUrl: false,
defaultModel: 'mistral-large-latest',
models: [
{ id: 'mistral-large-latest', label: 'Mistral Large', description: 'Flagship reasoning model. Best for complex evaluations.', inputPricePer1m: 0.5, outputPricePer1m: 1.5, badge: 'powerful' },
{ id: 'mistral-medium-latest', label: 'Mistral Medium', description: 'Balanced performance and cost.', inputPricePer1m: 1.5, outputPricePer1m: 7.5, badge: 'recommended' },
{ id: 'mistral-small-latest', label: 'Mistral Small', description: 'Fast and cost-effective. Recommended for most scoring tasks.', inputPricePer1m: 0.15, outputPricePer1m: 0.6, badge: 'cheap' }
],
},
openai_compatible: {
name: 'OpenAI-Compatible (Custom)',
tagline: 'Connect any OpenAI-compatible endpoint: Ollama, LM Studio, OpenRouter, Groq, Together AI, vLLM, …',
Expand All @@ -118,7 +133,8 @@ export function createLanguageModel(config: ProviderConfig) {
const secret = env.BETTER_AUTH_SECRET
const apiKey = decrypt(config.apiKeyEncrypted, secret)

if (!apiKey) {
// For openai_compatible, allow empty API key (local endpoints like Ollama don't need auth)
if (!apiKey && config.provider !== 'openai_compatible') {
throw createError({
statusCode: 500,
statusMessage: 'Failed to decrypt AI API key. The key may be corrupted.',
Expand All @@ -129,7 +145,7 @@ export function createLanguageModel(config: ProviderConfig) {
case 'openai':
case 'openai_compatible': {
const openai = createOpenAI({
apiKey,
apiKey: apiKey || 'dummy-key',
...(config.baseUrl ? { baseURL: config.baseUrl } : {}),
})
return openai(config.model)
Expand All @@ -148,6 +164,12 @@ export function createLanguageModel(config: ProviderConfig) {
})
return google(config.model)
}
case 'mistral': {
const mistral = createMistral({
apiKey,
})
return mistral(config.model)
}
default:
throw createError({
statusCode: 400,
Expand Down
8 changes: 4 additions & 4 deletions server/utils/schemas/scoring.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ const safeBaseUrl = z.string().url().max(500)

export const createAiConfigSchema = z.object({
name: z.string().min(1).max(80).trim(),
provider: z.enum(['openai', 'anthropic', 'google', 'openai_compatible']),
provider: z.enum(['openai', 'anthropic', 'google', 'mistral', 'openai_compatible']),
model: z.string().min(1).max(200),
apiKey: z.string().min(1).max(500),
apiKey: z.string().min(0).max(500),
baseUrl: safeBaseUrl.nullish(),
// Modern frontier models support 100K+ output tokens. Cap generously to avoid blocking power users.
maxTokens: z.number().int().min(256).max(200000).optional().default(16384),
Expand All @@ -29,9 +29,9 @@ export const createAiConfigSchema = z.object({

export const updateAiConfigSchema = z.object({
name: z.string().min(1).max(80).trim().optional(),
provider: z.enum(['openai', 'anthropic', 'google', 'openai_compatible']).optional(),
provider: z.enum(['openai', 'anthropic', 'google', 'mistral', 'openai_compatible']).optional(),
model: z.string().min(1).max(200).optional(),
apiKey: z.string().min(1).max(500).optional(),
apiKey: z.string().min(0).max(500).optional(),
baseUrl: safeBaseUrl.nullish(),
maxTokens: z.number().int().min(256).max(200000).optional(),
inputPricePer1m: z.number().min(0).max(9999).nullish(),
Expand Down